forked from aklomp/base64
-
Notifications
You must be signed in to change notification settings - Fork 1
/
base64_std.c
186 lines (172 loc) · 4.05 KB
/
base64_std.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Standard architecture-independent versions.
*/
#include "base64.h"
void
base64_stream_encode_std (struct base64_state *state, const char *const src, size_t srclen, char *const out, size_t *const outlen)
{
/* Assume that *out is large enough to contain the output.
* Theoretically it should be 4/3 the length of src. */
const unsigned char *c = (unsigned char *)src;
char *o = out;
/* Use local temporaries to avoid cache thrashing: */
size_t outl = 0;
struct base64_state st;
st.bytes = state->bytes;
st.carry = state->carry;
/* Turn three bytes into four 6-bit numbers: */
/* in[0] = 00111111 */
/* in[1] = 00112222 */
/* in[2] = 00222233 */
/* in[3] = 00333333 */
/* Duff's device, a for() loop inside a switch() statement. Legal! */
switch (st.bytes)
{
for (;;)
{
case 0:
if (srclen-- == 0) {
break;
}
*o++ = state->base64_table_enc[*c >> 2];
st.carry = (*c++ << 4) & 0x30;
st.bytes++;
outl += 1;
case 1: if (srclen-- == 0) {
break;
}
*o++ = state->base64_table_enc[st.carry | (*c >> 4)];
st.carry = (*c++ << 2) & 0x3C;
st.bytes++;
outl += 1;
case 2: if (srclen-- == 0) {
break;
}
*o++ = state->base64_table_enc[st.carry | (*c >> 6)];
*o++ = state->base64_table_enc[*c++ & 0x3F];
st.bytes = 0;
outl += 2;
}
}
state->bytes = st.bytes;
state->carry = st.carry;
*outlen = outl;
}
int
base64_stream_decode_std (struct base64_state *state, const char *const src, size_t srclen, char *const out, size_t *const outlen)
{
int ret = 0;
const char *c = src;
char *o = out;
unsigned char q;
/* Use local temporaries to avoid cache thrashing: */
size_t outl = 0;
struct base64_state st;
st.eof = state->eof;
st.bytes = state->bytes;
st.carry = state->carry;
/* If we previously saw an EOF or an invalid character, bail out: */
if (st.eof) {
*outlen = 0;
return 0;
}
/* Turn four 6-bit numbers into three bytes: */
/* out[0] = 11111122 */
/* out[1] = 22223333 */
/* out[2] = 33444444 */
/* Duff's device again: */
switch (st.bytes)
{
for (;;)
{
case 0:
#ifdef SKIP_INVALID
label0:;
#endif
if (srclen-- == 0) {
ret = 1;
break;
}
if ((q = base64_table_dec[(unsigned char)*c++]) >= 254) {
#ifdef SKIP_INVALID
goto label0;
#endif
st.eof = 1;
/* Treat character '=' as invalid for byte 0: */
break;
}
st.carry = q << 2;
st.bytes++;
case 1:
#ifdef SKIP_INVALID
label1:;
#endif
if (srclen-- == 0) {
ret = 1;
break;
}
if ((q = base64_table_dec[(unsigned char)*c++]) >= 254) {
#ifdef SKIP_INVALID
goto label1;
#endif
st.eof = 1;
/* Treat character '=' as invalid for byte 1: */
break;
}
*o++ = st.carry | (q >> 4);
st.carry = q << 4;
st.bytes++;
outl++;
case 2:
#ifdef SKIP_INVALID
label2:;
#endif
if (srclen-- == 0) {
ret = 1;
break;
}
if ((q = base64_table_dec[(unsigned char)*c++]) >= 254) {
#ifdef SKIP_INVALID
goto label2;
#endif
st.eof = 1;
/* When q == 254, the input char is '='. Return 1 and EOF.
* Technically, should check if next byte is also '=', but never mind.
* When q == 255, the input char is invalid. Return 0 and EOF. */
ret = (q == 254) ? 1 : 0;
break;
}
*o++ = st.carry | (q >> 2);
st.carry = q << 6;
st.bytes++;
outl++;
case 3:
#ifdef SKIP_INVALID
label3:;
#endif
if (srclen-- == 0) {
ret = 1;
break;
}
if ((q = base64_table_dec[(unsigned char)*c++]) >= 254) {
#ifdef SKIP_INVALID
goto label3;
#endif
st.eof = 1;
/* When q == 254, the input char is '='. Return 1 and EOF.
* When q == 255, the input char is invalid. Return 0 and EOF. */
ret = (q == 254) ? 1 : 0;
break;
}
*o++ = st.carry | q;
st.carry = 0;
st.bytes = 0;
outl++;
}
}
state->eof = st.eof;
state->bytes = st.bytes;
state->carry = st.carry;
*outlen = outl;
return ret;
}