59 static inline unsigned char
71 char *
out,
size_t outlen)
73 static const char*
b64str =
74 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
76 while (inlen && outlen)
78 *out++ = b64str[(
to_uchar (in[0]) >> 2) & 0x3f];
81 *out++ = b64str[((
to_uchar (in[0]) << 4)
82 + (--inlen ?
to_uchar (in[1]) >> 4 : 0))
89 + (--inlen ?
to_uchar (in[2]) >> 6 : 0))
94 *out++ = inlen ? b64str[
to_uchar (in[2]) & 0x3f] :
'=';
139 *out =
static_cast<char*
>(malloc (outlen));
224 static const signed char b64[0x100] = {
292 # define uchar_in_range(c) true
294 # define uchar_in_range(c) ((c) <= 255)
322 char const **
in,
char const *in_end,
323 size_t *n_non_newline)
331 if (4 <= in_end - *in && memchr (t,
'\n', 4) ==
NULL)
348 ctx->
buf[ctx->
i++] =
c;
355 *n_non_newline = ctx->
i;
360 #define return_false \
376 char **outp,
size_t *outleft)
461 const char *
in,
size_t inlen,
462 char *
out,
size_t *outlen)
464 size_t outleft = *outlen;
465 bool ignore_newlines = ctx !=
NULL;
466 bool flush_ctx =
false;
467 unsigned int ctx_i = 0;
472 flush_ctx = inlen == 0;
478 size_t outleft_save = outleft;
479 if (ctx_i == 0 && !flush_ctx)
485 outleft_save = outleft;
486 if (!
decode_4 (in, inlen, &out, &outleft))
494 if (inlen == 0 && !flush_ctx)
499 if (inlen && *in ==
'\n' && ignore_newlines)
507 out -= outleft_save - outleft;
508 outleft = outleft_save;
511 char const *in_end = in + inlen;
515 non_nl =
get_4 (ctx, &in, in_end, &inlen);
522 if (inlen == 0 || (inlen < 4 && !flush_ctx && ignore_newlines))
527 if (!
decode_4 (non_nl, inlen, &out, &outleft))
552 const char *
in,
size_t inlen,
char **
out,
560 size_t needlen = 3 * (inlen / 4) + 3;
562 *out =
static_cast<char*
>(malloc (needlen));
static const signed char b64[0x100]
std::string base64_encode(unsigned char const *, unsigned int len)
static const char * b64str
static unsigned char to_uchar(char ch)
#define BASE64_LENGTH(inlen)
void base64_decode_ctx_init(struct base64_decode_context *ctx)
size_t base64_encode_alloc(const char *in, size_t inlen, char **out)
static bool decode_4(char const *in, size_t inlen, char **outp, size_t *outleft)
bool base64_decode_ctx(struct base64_decode_context *ctx, const char *in, size_t inlen, char *out, size_t *outlen)
#define uchar_in_range(c)
bool base64_decode_alloc_ctx(struct base64_decode_context *ctx, const char *in, size_t inlen, char **out, size_t *outlen)
static char * get_4(struct base64_decode_context *ctx, char const **in, char const *in_end, size_t *n_non_newline)