-
Notifications
You must be signed in to change notification settings - Fork 149
/
base64_from.c
93 lines (82 loc) · 3.06 KB
/
base64_from.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
#include <stddef.h>
#include <stdint.h>
#include "types.h"
#include "base64.h"
static const u8 base64f[256] = {
//00 01 02 03 04 05 06 07
//08 09 0A 0B 0C 0D 0E 0F
// 0x00..0x3F
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x00
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x08
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x10
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x18
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x20
0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xFF, 0xFF, 0x3F, // 0x28
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, // 0x30
0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x38
// 0x40..0x7F
0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // 0x40
0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 0x48
0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, // 0x50
0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x58
0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, // 0x60
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srclen);
dstlen -= (src[srclen - 1] == '=');
dstlen -= (src[srclen - 2] == '=');
for (size_t i = 0, j = 0; i < srclen;) {
u32 sixbits[4];
sixbits[0] = base64f[(unsigned char)src[i++]];
sixbits[1] = base64f[(unsigned char)src[i++]];
sixbits[2] = (src[i] == '=' ? (0 & i++) : base64f[(unsigned char)src[i++]]);
sixbits[3] = (src[i] == '=' ? (0 & i++) : base64f[(unsigned char)src[i++]]);
u32 threebytes = 0
| (sixbits[0] << (3 * 6))
| (sixbits[1] << (2 * 6))
| (sixbits[2] << (1 * 6))
| (sixbits[3] << (0 * 6));
if (j < dstlen) dst[j++] = (threebytes >> (2 * 8));
if (j < dstlen) dst[j++] = (threebytes >> (1 * 8)) & 0xff;
if (j < dstlen) dst[j++] = (threebytes >> (0 * 8)) & 0xff;
}
return dstlen;
}
int base64_valid(const char *src,size_t *count)
{
const char *p;
for (p = src;base64f[(u8)*p] != 0xFF;++p)
;
for (;((size_t) (p - src)) % 4 != 0 && *p == '=';++p)
;
if (count)
*count = (size_t) (p - src);
return !*p && ((size_t) (p - src)) % 4 == 0;
}