blob: 082101f3bc71e2f16eb6b6ccae42761366469f7d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#include "base64.h"
#include "server_util.h"
#include <stdint.h>
char *base64_encode(const unsigned char *src, size_t n) {
static const char tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
size_t max_groups = (SIZE_MAX - 1) / 4;
if (n > max_groups * 3)
die_oom();
size_t out_len = ((n + 2) / 3) * 4;
char *out = xmalloc(out_len + 1);
const unsigned char *p = src;
const unsigned char *end = src + (n / 3) * 3;
char *q = out;
while (p < end) {
uint32_t v = ((uint32_t)p[0] << 16) | ((uint32_t)p[1] << 8) | (uint32_t)p[2];
q[0] = tbl[(v >> 18) & 63];
q[1] = tbl[(v >> 12) & 63];
q[2] = tbl[(v >> 6) & 63];
q[3] = tbl[v & 63];
p += 3;
q += 4;
}
size_t rem = n - (size_t)(p - src);
if (rem == 1) {
uint32_t v = (uint32_t)p[0] << 16;
q[0] = tbl[(v >> 18) & 63];
q[1] = tbl[(v >> 12) & 63];
q[2] = '=';
q[3] = '=';
q += 4;
} else if (rem == 2) {
uint32_t v = ((uint32_t)p[0] << 16) | ((uint32_t)p[1] << 8);
q[0] = tbl[(v >> 18) & 63];
q[1] = tbl[(v >> 12) & 63];
q[2] = tbl[(v >> 6) & 63];
q[3] = '=';
q += 4;
}
*q = '\0';
return out;
}
|