Skip to content

Commit fde488d

Browse files
committed
Fix base64 encoding
1 parent bfd8c56 commit fde488d

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/base64.c

+6-8
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,18 @@ char *base64encode(const unsigned char *buf, size_t *size)
5353
unsigned char input[3];
5454
unsigned int output[4];
5555
while (n < *size) {
56-
input[0] = buf[n++];
57-
input[1] = (n < *size) ? buf[n++] : 0;
58-
input[2] = (n < *size) ? buf[n++] : 0;
56+
input[0] = buf[n];
57+
input[1] = (n+1 < *size) ? buf[n+1] : 0;
58+
input[2] = (n+2 < *size) ? buf[n+2] : 0;
5959
output[0] = input[0] >> 2;
6060
output[1] = ((input[0] & 3) << 4) + (input[1] >> 4);
6161
output[2] = ((input[1] & 15) << 2) + (input[2] >> 6);
6262
output[3] = input[2] & 63;
6363
outbuf[m++] = base64_str[(int)output[0]];
6464
outbuf[m++] = base64_str[(int)output[1]];
65-
outbuf[m++] = base64_str[(int)output[2]];
66-
outbuf[m++] = base64_str[(int)output[3]];
67-
}
68-
while ((m % 4) != 0) {
69-
outbuf[m++] = base64_pad;
65+
outbuf[m++] = (n+1 < *size) ? base64_str[(int)output[2]] : base64_pad;
66+
outbuf[m++] = (n+2 < *size) ? base64_str[(int)output[3]] : base64_pad;
67+
n+=3;
7068
}
7169
outbuf[m] = 0; // 0-termination!
7270
*size = m;

0 commit comments

Comments
 (0)