From 392135c7db4d9cb4a14ff5935d7c4c6e21363847 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Sat, 22 Oct 2016 04:39:47 +0200 Subject: Remove libxml2 dependency in favor of custom XML parsing --- src/base64.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/base64.c') diff --git a/src/base64.c b/src/base64.c index 7128a5a..e59d963 100644 --- a/src/base64.c +++ b/src/base64.c @@ -43,32 +43,32 @@ static const signed char base64_table[256] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; -char *base64encode(const unsigned char *buf, size_t *size) +size_t base64encode(char *outbuf, const unsigned char *buf, size_t size) { - if (!buf || !size || !(*size > 0)) return NULL; - int outlen = (*size / 3) * 4; - char *outbuf = (char*)malloc(outlen+5); // 4 spare bytes + 1 for '\0' + if (!outbuf || !buf || (size <= 0)) { + return 0; + } + size_t n = 0; size_t m = 0; unsigned char input[3]; unsigned int output[4]; - while (n < *size) { + while (n < size) { input[0] = buf[n]; - input[1] = (n+1 < *size) ? buf[n+1] : 0; - input[2] = (n+2 < *size) ? buf[n+2] : 0; + input[1] = (n+1 < size) ? buf[n+1] : 0; + input[2] = (n+2 < size) ? buf[n+2] : 0; output[0] = input[0] >> 2; output[1] = ((input[0] & 3) << 4) + (input[1] >> 4); output[2] = ((input[1] & 15) << 2) + (input[2] >> 6); output[3] = input[2] & 63; outbuf[m++] = base64_str[(int)output[0]]; outbuf[m++] = base64_str[(int)output[1]]; - outbuf[m++] = (n+1 < *size) ? base64_str[(int)output[2]] : base64_pad; - outbuf[m++] = (n+2 < *size) ? base64_str[(int)output[3]] : base64_pad; + outbuf[m++] = (n+1 < size) ? base64_str[(int)output[2]] : base64_pad; + outbuf[m++] = (n+2 < size) ? base64_str[(int)output[3]] : base64_pad; n+=3; } outbuf[m] = 0; // 0-termination! - *size = m; - return outbuf; + return m; } static int base64decode_block(unsigned char *target, const char *data, size_t data_size) -- cgit v1.1-32-gdbae