This repository has been archived by the owner on Jan 23, 2024. It is now read-only.
forked from google/token_bind
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
base64.c
347 lines (330 loc) · 13.8 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* Copyright 2003-2009, 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
/* This was adapted from Google's Omaha open-source project. */
#include "base64.h"
size_t CalculateBase64EscapedLen(size_t input_len, bool do_padding) {
/* these formulae were copied from comments that used to go with the base64
encoding functions */
if (((input_len << 4) >> 4) != input_len) {
/* Too large to allocate */
return 0;
}
size_t intermediate_result = 8 * input_len + 5;
int len = intermediate_result / 6;
if (do_padding) len = ((len + 3) / 4) * 4;
return len;
}
static size_t Base64EscapeInternal(const char *src, size_t szsrc,
char *dest, size_t szdest,
const char *base64,
bool do_padding) {
if (base64 == NULL || dest == NULL || src == NULL || szsrc <= 0) {
return 0;
}
static const char kPad64 = '=';
char* cur_dest = dest;
const unsigned char *cur_src = (const unsigned char*)src;
/* Three bytes of data encodes to four characters of cyphertext.
So we can pump through three-byte chunks atomically. */
while (szsrc > 2) {
/* Keep going until we have less than 24 bits. */
if (szdest < 4 ) {
return 0;
}
szdest -= 4;
cur_dest[0] = base64[cur_src[0] >> 2];
cur_dest[1] = base64[((cur_src[0] & 0x03) << 4) + (cur_src[1] >> 4)];
cur_dest[2] = base64[((cur_src[1] & 0x0f) << 2) + (cur_src[2] >> 6)];
cur_dest[3] = base64[cur_src[2] & 0x3f];
cur_dest += 4;
cur_src += 3;
szsrc -= 3;
}
/* now deal with the tail (<=2 bytes) */
switch (szsrc) {
case 0:
/* Nothing left; nothing more to do. */
break;
case 1:
/* One byte left: this encodes to two characters, and (optionally)
two pad characters to round out the four-character cypherblock. */
if (szdest < 2) {
return 0;
}
szdest -= 2;
cur_dest[0] = base64[cur_src[0] >> 2];
cur_dest[1] = base64[(cur_src[0] & 0x03) << 4];
cur_dest += 2;
if (do_padding) {
if (szdest < 2) {
return 0;
}
szdest -= 2;
cur_dest[0] = kPad64;
cur_dest[1] = kPad64;
cur_dest += 2;
}
break;
case 2:
/* Two bytes left: this encodes to three characters, and (optionally)
one pad character to round out the four-character cypherblock. */
if (szdest < 3) {
return 0;
}
szdest -= 3;
cur_dest[0] = base64[cur_src[0] >> 2];
cur_dest[1] = base64[((cur_src[0] & 0x03) << 4) + (cur_src[1] >> 4)];
cur_dest[2] = base64[(cur_src[1] & 0x0f) << 2];
cur_dest += 3;
if (do_padding) {
if (szdest < 1) {
return 0;
}
szdest -= 1;
cur_dest[0] = kPad64;
cur_dest += 1;
}
break;
default:
/* Should not be reached: blocks of 3 bytes are handled
in the while loop before this switch statement. */
return 0;
}
return cur_dest - dest;
}
#define kBase64Chars "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
#define kWebSafeBase64Chars "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
size_t Base64Escape(const char* src, size_t szsrc, char* dest, size_t szdest,
bool do_padding) {
return Base64EscapeInternal(src, szsrc, dest, szdest, kBase64Chars, do_padding);
}
size_t WebSafeBase64Escape(const char* src, size_t szsrc, char* dest,
size_t szdest, bool do_padding) {
return Base64EscapeInternal(src, szsrc, dest, szdest, kWebSafeBase64Chars,
do_padding);
}
/* Check out
http://www.cis.ohio-state.edu/htbin/rfc/rfc2045.html for formal
description, but what we care about is that...
Take the encoded stuff in groups of 4 characters and turn each
character into a code 0 to 63 thus:
A-Z map to 0 to 25
a-z map to 26 to 51
0-9 map to 52 to 61
+(- for WebSafe) maps to 62
/(_ for WebSafe) maps to 63
There will be four numbers, all less than 64 which can be represented
by a 6 digit binary number (aaaaaa, bbbbbb, cccccc, dddddd respectively).
Arrange the 6 digit binary numbers into three bytes as such:
aaaaaabb bbbbcccc ccdddddd
Equals signs (one or two) are used at the end of the encoded block to
indicate that the text was not an integer multiple of three bytes long. */
static size_t Base64UnescapeInternal(const char* src,
char* dest, size_t len_dest,
const char* unbase64) {
if (unbase64 == NULL || src == NULL || dest == NULL) {
return 0;
}
static const char kPad64 = '=';
int decode;
size_t destidx = 0;
int state = 0;
/* Used an unsigned char, since ch is used as an array index
(into unbase64). */
unsigned char ch = 0;
while ((ch = *src++) != '\0') {
if (ch < ' ') {
continue; /* Skip whitespace */
}
if (ch == kPad64) {
break;
}
decode = unbase64[ch];
if (decode == 99) {
return 0; /* A non-base64 character */
}
/* Four cyphertext characters decode to three bytes. Therefore we can be
in one of four states. */
switch (state) {
case 0:
/* We're at the beginning of a four-character cyphertext block.
This sets the high six bits of the first byte of the plaintext
block. */
if (destidx >= len_dest) {
return 0;
}
dest[destidx] = (char)(decode << 2);
state = 1;
break;
case 1:
/* We're one character into a four-character cyphertext block. This
sets the low two bits of the first plaintext byte, and the high four
bits of the second plaintext byte. However, if this is the end of
data, and those four bits are zero, it could be that those four bits
are leftovers from the encoding of data that had a length of one mod
three. */
if (destidx >= len_dest) {
return 0;
}
dest[destidx] |= decode >> 4;
if (destidx + 1 >= len_dest) {
if ((decode & 0x0f) != 0) {
return 0;
}
} else {
dest[destidx + 1] = (char)((decode & 0x0f) << 4);
}
destidx++;
state = 2;
break;
case 2:
/* We're two characters into a four-character cyphertext block. This
sets the low four bits of the second plaintext byte, and the high
two bits of the third plaintext byte. However, if this is the end
of data, and those two bits are zero, it could be that those two
bits are leftovers from the encoding of data that had a length of
two mod three. */
if (destidx >= len_dest) {
return 0;
}
dest[destidx] |= decode >> 2;
if (destidx +1 >= len_dest) {
if ((decode & 0x03) != 0) {
return (-1);
}
} else {
dest[destidx + 1] = (char)((decode & 0x03) << 6);
}
destidx++;
state = 3;
break;
case 3:
/* We're at the last character of a four-character cyphertext block.
This sets the low six bits of the third plaintext byte. */
if (destidx >= len_dest) {
return 0;
}
dest[destidx] |= decode;
destidx++;
state = 0;
break;
default:
return 0;
break;
}
}
/* We are done decoding Base-64 chars. Let's see if we ended on a byte
boundary, and/or with erroneous trailing characters. */
if (ch == kPad64) { /* We got a pad char */
if (state == 0 || state == 1) {
return 0; /* Invalid '=' in first or second position */
}
if (state == 2) {
/* need another '=' */
while ((ch = *src++) != '\0') {
if (ch < ' ') {
break;
}
}
if (ch != kPad64) {
return 0;
}
}
/* state = 1 or 2, check if all remain padding is space/ */
while ((ch = *src++) != '\0') {
if (ch > ' ') {
return 0;
}
}
} else {
/* We ended by seeing the end of the string. Make sure we have no partial
bytes lying around. Note that we do not require trailing '=', so states
2 and 3 are okay too. */
if (state == 1)
return 0;
}
return destidx;
}
size_t Base64Unescape(const char* src, char* dest,
size_t len_dest) {
static const char UnBase64[] = {
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 62/*+*/, 99, 99, 99, 63/*/ */,
52/*0*/, 53/*1*/, 54/*2*/, 55/*3*/, 56/*4*/, 57/*5*/, 58/*6*/, 59/*7*/,
60/*8*/, 61/*9*/, 99, 99, 99, 99, 99, 99,
99, 0/*A*/, 1/*B*/, 2/*C*/, 3/*D*/, 4/*E*/, 5/*F*/, 6/*G*/,
7/*H*/, 8/*I*/, 9/*J*/, 10/*K*/, 11/*L*/, 12/*M*/, 13/*N*/, 14/*O*/,
15/*P*/, 16/*Q*/, 17/*R*/, 18/*S*/, 19/*T*/, 20/*U*/, 21/*V*/, 22/*W*/,
23/*X*/, 24/*Y*/, 25/*Z*/, 99, 99, 99, 99, 99,
99, 26/*a*/, 27/*b*/, 28/*c*/, 29/*d*/, 30/*e*/, 31/*f*/, 32/*g*/,
33/*h*/, 34/*i*/, 35/*j*/, 36/*k*/, 37/*l*/, 38/*m*/, 39/*n*/, 40/*o*/,
41/*p*/, 42/*q*/, 43/*r*/, 44/*s*/, 45/*t*/, 46/*u*/, 47/*v*/, 48/*w*/,
49/*x*/, 50/*y*/, 51/*z*/, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99
};
return Base64UnescapeInternal(src, dest, len_dest, UnBase64);
}
size_t WebSafeBase64Unescape(const char *src, char *dest, size_t szdest) {
static const char UnBase64[] = {
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 62/*-*/, 99, 99,
52/*0*/, 53/*1*/, 54/*2*/, 55/*3*/, 56/*4*/, 57/*5*/, 58/*6*/, 59/*7*/,
60/*8*/, 61/*9*/, 99, 99, 99, 99, 99, 99,
99, 0/*A*/, 1/*B*/, 2/*C*/, 3/*D*/, 4/*E*/, 5/*F*/, 6/*G*/,
7/*H*/, 8/*I*/, 9/*J*/, 10/*K*/, 11/*L*/, 12/*M*/, 13/*N*/, 14/*O*/,
15/*P*/, 16/*Q*/, 17/*R*/, 18/*S*/, 19/*T*/, 20/*U*/, 21/*V*/, 22/*W*/,
23/*X*/, 24/*Y*/, 25/*Z*/, 99, 99, 99, 99, 63/*_*/,
99, 26/*a*/, 27/*b*/, 28/*c*/, 29/*d*/, 30/*e*/, 31/*f*/, 32/*g*/,
33/*h*/, 34/*i*/, 35/*j*/, 36/*k*/, 37/*l*/, 38/*m*/, 39/*n*/, 40/*o*/,
41/*p*/, 42/*q*/, 43/*r*/, 44/*s*/, 45/*t*/, 46/*u*/, 47/*v*/, 48/*w*/,
49/*x*/, 50/*y*/, 51/*z*/, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99
};
return Base64UnescapeInternal(src, dest, szdest, UnBase64);
}