Commit fde488d1 authored by Nikias Bassen's avatar Nikias Bassen

Fix base64 encoding

parent bfd8c56c
...@@ -53,20 +53,18 @@ char *base64encode(const unsigned char *buf, size_t *size) ...@@ -53,20 +53,18 @@ char *base64encode(const unsigned char *buf, size_t *size)
unsigned char input[3]; unsigned char input[3];
unsigned int output[4]; unsigned int output[4];
while (n < *size) { while (n < *size) {
input[0] = buf[n++]; input[0] = buf[n];
input[1] = (n < *size) ? buf[n++] : 0; input[1] = (n+1 < *size) ? buf[n+1] : 0;
input[2] = (n < *size) ? buf[n++] : 0; input[2] = (n+2 < *size) ? buf[n+2] : 0;
output[0] = input[0] >> 2; output[0] = input[0] >> 2;
output[1] = ((input[0] & 3) << 4) + (input[1] >> 4); output[1] = ((input[0] & 3) << 4) + (input[1] >> 4);
output[2] = ((input[1] & 15) << 2) + (input[2] >> 6); output[2] = ((input[1] & 15) << 2) + (input[2] >> 6);
output[3] = input[2] & 63; output[3] = input[2] & 63;
outbuf[m++] = base64_str[(int)output[0]]; outbuf[m++] = base64_str[(int)output[0]];
outbuf[m++] = base64_str[(int)output[1]]; outbuf[m++] = base64_str[(int)output[1]];
outbuf[m++] = base64_str[(int)output[2]]; outbuf[m++] = (n+1 < *size) ? base64_str[(int)output[2]] : base64_pad;
outbuf[m++] = base64_str[(int)output[3]]; outbuf[m++] = (n+2 < *size) ? base64_str[(int)output[3]] : base64_pad;
} n+=3;
while ((m % 4) != 0) {
outbuf[m++] = base64_pad;
} }
outbuf[m] = 0; // 0-termination! outbuf[m] = 0; // 0-termination!
*size = m; *size = m;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment