6#define WHO "mess822_base64"
8static uint8 *b64alpha =
9 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
11static const unsigned char *b64alphaurl =
12 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
14static unsigned int base64_dec(
int c)
16 if (
'A' <=
c &&
c <=
'Z')
return c -
'A';
17 if (
'a' <=
c &&
c <=
'z')
return 26 +
c -
'a';
18 if (
'0' <=
c &&
c <=
'9')
return 52 +
c -
'0';
19 if (
c ==
'+')
return 62;
20 if (
c ==
'/')
return 63;
37 if (!stralloc_copys(
out,
""))
return -1;
38 if (len == 0)
return 0;
40 if (!stralloc_ready(
out,len))
return -1;
42 for (
int remaining = 0; i < len; i++) {
43 if (in[i] ==
'\0')
break;
44 if (in[i] ==
'=')
continue;
45 if (in[i] ==
'\n')
continue;
47 if (in[i] ==
'_') {
if (!stralloc_append(
out,
"/"))
return -1; r++;
continue; }
48 if (in[i] ==
'-') {
if (!stralloc_append(
out,
"+"))
return -1; r++;
continue; }
50 if ((v = base64_dec(in[i])) < 64) {
52 case 0:
c = v; remaining = 6;
break;
53 case 6: b = (
c << 2) | (v >> 4);
54 if (!stralloc_append(
out,&b))
return -1;
55 c = (v & 0x0f); remaining = 4; r++;
break;
56 case 4: b = (
c << 4) | (v >> 2);
57 if (!stralloc_append(
out,&b))
return -1;
58 c = (v & 0x03); remaining = 2; r++;
break;
59 case 2: b = (
c << 6) | v;
60 if (!stralloc_append(
out,&b))
return -1;
61 c = 0; remaining = 0; r++;
break;
63 }
else if (
flag == 1) {
64 char ch[] = {
'\0',
'\0' };
66 logmsg(
WHO,0,INFO,B(
"Erroneous character on input: >",ch,
"<"));
81 if (!stralloc_cats(
out,
""))
return -1;
87 int len = in->len % 3;
88 int blen = in->len - len;
89 int lines = (4 * in->len / 3) / 76 + 1;
90 if (!stralloc_ready(
out,4*in->len/3 + 2 + lines))
return -1;
92 for (i = 0; i < blen; i += 3) {
97 b64[0] = b64alpha[
a >> 2];
98 b64[1] = b64alpha[((
a & 0x03) << 4) | (b >> 4)];
99 b64[2] = b64alpha[((b & 0x0f) << 2) | (
c >> 6)];
100 b64[3] = b64alpha[
c & 0x3f];
101 if (!stralloc_catb(
out,b64,4))
return -1;
103 if (r % 76 == 0)
if (!stralloc_append(
out,
"\n"))
return -1;
110 b64[0] = b64alpha[
a >> 2];
111 b64[1] = b64alpha[((
a & 0x03) << 4) | (b >> 4)];
112 b64[2] = b64alpha[b & 0x0f << 2];
114 if (!stralloc_catb(
out,b64,4))
return -1;
116 }
else if (len == 1) {
119 b64[0] = b64alpha[
a >> 2];
120 b64[1] = b64alpha[(
a & 0x03) << 4];
123 if (!stralloc_catb(
out,b64,4))
return -1;
137 if (!stralloc_cats(
out,
""))
return -1;
143 int len = in->len % 3;
144 int blen = in->len - len;
145 int lines = (4 * in->len / 3) / 76 + 1;
146 if (!stralloc_ready(
out,4*in->len/3 + 2 + lines))
return -1;
148 for (i = 0; i < blen; i += 3) {
153 b64[0] = b64alphaurl[
a >> 2];
154 b64[1] = b64alphaurl[((
a & 0x03) << 4) | (b >> 4)];
155 b64[2] = b64alphaurl[((b & 0x0f) << 2) | (
c >> 6)];
156 b64[3] = b64alphaurl[
c & 0x3f];
157 if (!stralloc_catb(
out,b64,4))
return -1;
159 if (r % 76 == 0)
if (!stralloc_append(
out,
"\n"))
return -1;
166 b64[0] = b64alphaurl[
a >> 2];
167 b64[1] = b64alphaurl[((
a & 0x03) << 4) | (b >> 4)];
168 b64[2] = b64alphaurl[b & 0x0f << 2];
170 if (!stralloc_catb(
out,b64,4))
return -1;
172 }
else if (len == 1) {
175 b64[0] = b64alphaurl[
a >> 2];
176 b64[1] = b64alphaurl[(
a & 0x03) << 4];
179 if (!stralloc_catb(
out,b64,4))
return -1;
void c(char *, char *, char *, int, int, int)
int mess822_b64decode(stralloc *out, const char *in, int len, int flag)
int mess822_b64urlencode(stralloc *out, stralloc *in)
int mess822_b64encode(stralloc *out, stralloc *in)