1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 01:18:00 +00:00
putty-source/utils/base64_decode.c
Simon Tatham 043c24844a Improve the base64 utility functions.
The low-level functions to handle a single atom of base64 at a time
have been in 'utils' / misc.h for ages, but the higher-level family of
base64_encode functions that handle a whole data block were hidden
away in sshpubk.c, and there was no higher-level decode function at
all.

Now moved both into 'utils' modules and declared them in misc.h rather
than ssh.h. Also, improved the APIs: they all take ptrlen in place of
separate data and length arguments, their naming is more consistent
and more explicit (the previous base64_encode which didn't name its
destination is now base64_encode_fp), and the encode functions now
accept cpl == 0 as a special case meaning that the output base64 data
is wanted in the form of an unbroken single-line string with no
trailing \n.
2022-04-25 14:10:16 +01:00

38 lines
842 B
C

#include "misc.h"
void base64_decode_bs(BinarySink *bs, ptrlen input)
{
BinarySource src[1];
BinarySource_BARE_INIT_PL(src, input);
while (get_avail(src)) {
char b64atom[4];
unsigned char binatom[3];
for (size_t i = 0; i < 4 ;) {
char c = get_byte(src);
if (get_err(src))
c = '=';
if (c == '\n' || c == '\r')
continue;
b64atom[i++] = c;
}
put_data(bs, binatom, base64_decode_atom(b64atom, binatom));
}
}
void base64_decode_fp(FILE *fp, ptrlen input)
{
stdio_sink ss;
stdio_sink_init(&ss, fp);
base64_decode_bs(BinarySink_UPCAST(&ss), input);
}
strbuf *base64_decode_sb(ptrlen input)
{
strbuf *sb = strbuf_new_nm();
base64_decode_bs(BinarySink_UPCAST(sb), input);
return sb;
}