summaryrefslogtreecommitdiffstats
path: root/src/base64.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/base64.c')
-rw-r--r--src/base64.c14
1 files changed, 6 insertions, 8 deletions
diff --git a/src/base64.c b/src/base64.c
index 2326168..65c6061 100644
--- a/src/base64.c
+++ b/src/base64.c
@@ -53,20 +53,18 @@ char *base64encode(const unsigned char *buf, size_t *size)
unsigned char input[3];
unsigned int output[4];
while (n < *size) {
- input[0] = buf[n++];
- input[1] = (n < *size) ? buf[n++] : 0;
- input[2] = (n < *size) ? buf[n++] : 0;
+ input[0] = buf[n];
+ 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++] = base64_str[(int)output[2]];
- outbuf[m++] = base64_str[(int)output[3]];
- }
- while ((m % 4) != 0) {
- outbuf[m++] = 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;