mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-07-05 21:42:47 -05:00
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.
This commit is contained in:
37
utils/base64_decode.c
Normal file
37
utils/base64_decode.c
Normal file
@ -0,0 +1,37 @@
|
||||
#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;
|
||||
}
|
Reference in New Issue
Block a user