From 332e8f4ba0363ba816109a51a8e46ef37056dde7 Mon Sep 17 00:00:00 2001 From: Joris Vink Date: Tue, 22 Apr 2014 21:45:26 +0200 Subject: [PATCH] Do a better job at base64 decoding stuff --- src/utils.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/utils.c b/src/utils.c index 17ea422..a2da75c 100644 --- a/src/utils.c +++ b/src/utils.c @@ -35,7 +35,7 @@ static struct { { NULL, 0 }, }; -static char b64table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; +static char b64table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; void kore_debug_internal(char *file, int line, const char *fmt, ...) @@ -345,6 +345,9 @@ kore_base64_decode(char *in, u_int8_t **out, u_int32_t *olen) res = kore_buf_create(len); for (idx = 0; idx < len; idx++) { + if (in[idx] == '=') + break; + for (o = 0; o < sizeof(b64table); o++) { if (b64table[o] == in[idx]) { d = o; @@ -352,9 +355,8 @@ kore_base64_decode(char *in, u_int8_t **out, u_int32_t *olen) } } - /* XXX - This could be bad? */ if (o == sizeof(b64table)) - d = 0; + fatal("bad b64 character: %c", in[idx]); b |= (d & 0x3f) << (i * 6); if (i-- == 0) { @@ -368,6 +370,14 @@ kore_base64_decode(char *in, u_int8_t **out, u_int32_t *olen) } } + o = len - idx; + if (o > 0) { + for (i = 2; i >= o; i--) { + n = (b >> (8 * i)); + kore_buf_append(res, &n, 1); + } + } + *out = kore_buf_release(res, olen); return (KORE_RESULT_OK); }