-
Notifications
You must be signed in to change notification settings - Fork 0
/
base64.c
165 lines (135 loc) · 5.57 KB
/
base64.c
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <stddef.h>
#include <string.h>
#include "base64.h"
#define MASK 0x3f
static char ALPHA[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static char LOOKUP[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x3e, 0, 0, 0, 0x3f,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0, 0, 0, 0, 0, 0,
0, 0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0, 0, 0, 0, 0,
0, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
void b64a(const char *alpha) {
const char *c;
memset(LOOKUP, 0, sizeof(LOOKUP));
for (c = alpha; *c; c++) {
ALPHA[c-alpha] = *c;
LOOKUP[*c] = c-alpha;
}
}
/*
[0] [1] [2]
8b: 01234567 01234567 01234567
5b: 012345 670123 456701 234567
^ ^ ^ ^
| | | `-- ( [2] ) & 0x3f
| | `--------- ([1] << 2 | [2] >> 6) & 0x3f
| `---------------- ([0] << 4 | [1] >> 4) & 0x3f
`----------------------- ( [0] >> 2) & 0x3f
01234567
012345-- [0] >> 2
01234567 01234567
------67 0123---- [0] << 4 | [1] >> 4
01234567 01234567
----4567 01------ [1] << 2 | [2] >> 6
01234567
--234567 [2]
012345 670123 456701 234567
012345 670123
012345 67---- [0] << 2 | [1] >> 4
670123 456701
--0123 4567-- [1] << 4 | [2] >> 2
456701 234567
----01 234567 [2] << 6 | [3]
*/
int
b64e(char *dst, const char *src, size_t inlen)
{
char buf[3];
for (; inlen >= 3; src += 3, inlen -= 3) {
*dst++ = ALPHA[ ( (0xfc & src[0]) >> 2) & MASK];
*dst++ = ALPHA[ ((0x03 & src[0]) << 4) | ((0xf0 & src[1]) >> 4) & MASK];
*dst++ = ALPHA[ ((0x0f & src[1]) << 2) | ((0xc0 & src[2]) >> 6) & MASK];
*dst++ = ALPHA[ ( (0x3f & src[2]) ) & MASK];
}
if (inlen >= 1) {
/* bounce through a temporary (stack-local)
buffer for our required 0-padding. */
memset(buf, 0, 4);
memcpy(buf, src, inlen);
src = buf;
*dst++ = ALPHA[ ( (0xfc & src[0]) >> 2) & MASK];
*dst++ = ALPHA[ ((0x03 & src[0]) << 4) | ((0xf0 & src[1]) >> 4) & MASK];
}
if (inlen >= 2) {
*dst++ = ALPHA[ ((0x0f & src[1]) << 2) | ((0xc0 & src[2]) >> 6) & MASK];
}
if (inlen >= 3) {
*dst++ = ALPHA[ ( (0x3f & src[2]) ) & MASK];
}
return 0;
}
int
b64d(char *dst, const char *src, size_t inlen)
{
if (inlen % 4 == 1)
return 1;
for (; inlen >= 4; src += 4, inlen -= 4) {
*dst++ = (LOOKUP[src[0]] << 2) | (LOOKUP[src[1]] >> 4);
*dst++ = (LOOKUP[src[1]] << 4) | (LOOKUP[src[2]] >> 2);
*dst++ = (LOOKUP[src[2]] << 6) | (LOOKUP[src[3]] );
}
if (inlen >= 2) *dst++ = (LOOKUP[src[0]] << 2) | (LOOKUP[src[1]] >> 4);
if (inlen >= 3) *dst++ = (LOOKUP[src[1]] << 4) | (LOOKUP[src[2]] >> 2);
return 0;
}
#ifdef O_TESTS
#include "ctap.h"
#define b64_is(buf, in, out) do {\
memset(buf, 0, sizeof(buf)); \
ok(b64e(buf, in, strlen(in)) == 0, "b64e(" in ") should succeed"); \
buf[b64elen(strlen(in))] = '\0'; \
is(buf, out, "[" in "] is [" out "] in base64"); \
\
memset(buf, 0, sizeof(buf)); \
ok(b64d(buf, out, strlen(out)) == 0, "b64d(" out ") should succeed"); \
buf[b64dlen(strlen(out))] = '\0'; \
is(buf, in, "[" out "] in base64 is [" in "]"); \
} while (0)
#define b64_uses(e,d) do {\
cmp_ok(b64elen(e), "==", d, "base-64 needs %d bytes to encode %d bytes", e, d); \
cmp_ok(b64dlen(d), "==", e, "base-64 needs %d bytes to decode %d bytes", d, e); \
} while (0)
#define b64_noop(buf, s) do {\
memset(buf, 0, sizeof(buf)); \
ok(b64e(buf, s, strlen(s)) == 0, "b64e(" s ") should succeed"); \
ok(b64d(buf, buf, strlen(buf)) == 0, "b64d(b64e(" s ")) should also succeed"); \
buf[b64dlen(b64elen(strlen(s)))] = '\0'; \
is(buf, s, "D(E(s)) should equal (s)"); \
} while (0)
TESTS {
char buf[256];
b64_uses(1,2); b64_uses(2,3); b64_uses(3,4);
b64_uses(4,6); /* and repeat! */
b64_is(buf, "f", "Zg");
b64_is(buf, "fo", "Zm8");
b64_is(buf, "foo", "Zm9v");
b64_is(buf, "foob", "Zm9vYg");
b64_is(buf, "fooba", "Zm9vYmE");
b64_is(buf, "foobar", "Zm9vYmFy");
b64_noop(buf, "people say nothing is impossible, but i do nothing every day.");
b64_noop(buf, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
}
#endif