7#define WHO "mess822_base64"
10static const char *b64alpha =
11 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
13static const char *b64alphaurl =
14 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
31 if (!stralloc_copys(
out,
""))
return -1;
35 while (in[len -1] ==
B64PAD) {
42 if (!stralloc_ready(
out,i))
return -1;
44 s = (
unsigned char*)
out->s;
46 for (i = 0; i < n - 1; i++) {
48 for (j = 0; j < 4; j++) {
49 if (in[j] >=
'A' && in[j] <=
'Z')
50 x = (x << 6) + (
unsigned int) (in[j] -
'A' + 0);
51 else if (in[j] >=
'a' && in[j] <=
'z')
52 x = (x << 6) + (
unsigned int) (in[j] -
'a' + 26);
53 else if (in[j] >=
'0' && in[j] <=
'9')
54 x = (x << 6) + (
unsigned int) (in[j] -
'0' + 52);
55 else if (in[j] ==
'+')
57 else if (in[j] ==
'/')
59 else if (in[j] ==
'-')
61 else if (in[j] ==
'_')
63 else if (in[j] ==
'=')
68 s[2] = (
unsigned char) (x & 255); x >>= 8;
69 s[1] = (
unsigned char) (x & 255); x >>= 8;
70 s[0] = (
unsigned char) (x & 255); x >>= 8;
75 for (j = 0; j < 4; j++) {
76 if (in[j] >=
'A' && in[j] <=
'Z')
77 x = (x << 6) + (
unsigned int) (in[j] -
'A' + 0);
78 else if (in[j] >=
'a' && in[j] <=
'z')
79 x = (x << 6) + (
unsigned int) (in[j] -
'a' + 26);
80 else if (in[j] >=
'0' && in[j] <=
'9')
81 x = (x << 6) + (
unsigned int) (in[j] -
'0' + 52);
82 else if (in[j] ==
'+')
84 else if (in[j] ==
'/')
86 else if (in[j] ==
'-')
88 else if (in[j] ==
'_')
90 else if (in[j] ==
'=')
95 b[2] = (
unsigned char) (x & 255); x >>= 8;
96 b[1] = (
unsigned char) (x & 255); x >>= 8;
97 b[0] = (
unsigned char) (x & 255); x >>= 8;
99 for (i = 0; i < 3 -
p; i++)
107 unsigned char a, b,
c;
113 if (!stralloc_copys(
out,
""))
return -1;
117 i = in->len / 3 * 4 + 4;
118 if (!stralloc_ready(
out,i))
return -1;
119 s = (
unsigned char *)
out->s;
121 for (i = 0; i < in->len; i += 3) {
123 b = i + 1 < in->len ? in->s[i + 1] : 0;
124 c = i + 2 < in->len ? in->s[i + 2] : 0;
126 *s++ = b64alpha[
a >> 2];
127 *s++ = b64alpha[((
a & 3 ) << 4) | (b >> 4)];
129 if (i + 1 >= in->len) *s++ =
B64PAD;
130 else *s++ = b64alpha[((b & 0x0f) << 2) | (
c >> 6)];
132 if (i + 2 >= in->len) *s++ =
B64PAD;
133 else *s++ = b64alpha[
c & 0x3f];
135 if (r % 76 == 0) *s++ =
'\n';
137 out->len = s - (
unsigned char *)
out->s;
144 unsigned char a, b,
c;
150 if (!stralloc_copys(
out,
""))
return -1;
154 i = in->len / 3 * 4 + 4;
155 if (!stralloc_ready(
out,i))
return -1;
156 s = (
unsigned char *)
out->s;
158 for (i = 0; i < in->len; i += 3) {
160 b = i + 1 < in->len ? in->s[i + 1] : 0;
161 c = i + 2 < in->len ? in->s[i + 2] : 0;
163 *s++ = b64alphaurl[
a >> 2];
164 *s++ = b64alphaurl[((
a & 3 ) << 4) | (b >> 4)];
166 if (i + 1 >= in->len) *s++ =
B64PAD;
167 else *s++ = b64alphaurl[((b & 0x0f) << 2) | (
c >> 6)];
169 if (i + 2 >= in->len) *s++ =
B64PAD;
170 else *s++ = b64alphaurl[
c & 0x3f];
172 if (r % 76 == 0) *s++ =
'\n';
174 out->len = s - (
unsigned char *)
out->s;
void p(char *home, char *fifo, int uid, int gid, int mode)
int mess822_b64urlencode(stralloc *out, stralloc *in)
int mess822_b64decode(stralloc *out, const char *in, int len)
int mess822_b64encode(stralloc *out, stralloc *in)
void c(char *, char *, char *, int, int, int)