-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase64.c
149 lines (126 loc) · 4.33 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
#include "base64.h"
/* BASE64 encode table 64 */
static const unsigned char base64en[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/',
};
/* BASE64 decode table 256, character '=' is 0xFE, 0xFF in unused character */
static const unsigned char base64de[] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xFF, 0xFF, 0x3F,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 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FF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
int base64_encode(const unsigned char *in, unsigned int inlen, char *out, unsigned int *outlen)
{
unsigned int i, n = inlen / 3 * 3;
if(*outlen < BASE64_ENCODE_SIZE(inlen))
{
return BASE64_INVALID;
}
*outlen = BASE64_ENCODE_SIZE(inlen);
for(i = 0; i < n; i += 3, in += 3, out += 4)
{
out[0] = base64en[(in[0] >> 2) & 0x3F];
out[1] = base64en[((in[0] & 0x3) << 4) + ((in[1] >> 4) & 0xF)];
out[2] = base64en[((in[1] & 0xF) << 2) + ((in[2] >> 6) & 0x3)];
out[3] = base64en[in[2] & 0x3F];
}
switch(inlen - n)
{
case 1:
{
out[0] = base64en[(in[0] >> 2) & 0x3F];
out[1] = base64en[(in[0] & 0x3) << 4];
out[2] = BASE64_PADDING;
out[3] = BASE64_PADDING;
break;
}
case 2:
{
out[0] = base64en[(in[0] >> 2) & 0x3F];
out[1] = base64en[((in[0] & 0x3) << 4) + ((in[1] >> 4) & 0xF)];
out[2] = base64en[(in[1] & 0xF) << 2];
out[3] = BASE64_PADDING;
break;
}
}
return BASE64_OK;
}
int base64_decode(const char *in, unsigned int inlen, unsigned char *out, unsigned int *outlen)
{
unsigned char B1, B2, B3, B4, *orgout = out;
unsigned int i, n = inlen / 4 * 4 - 4, ret = BASE64_OK;
if(*outlen < BASE64_DECODE_SIZE(inlen))
{
return BASE64_INVALID;
}
for(i = 0; i < n; i += 4, in += 4, out += 3)
{
B1 = base64de[in[0]];
B2 = base64de[in[1]];
B3 = base64de[in[2]];
B4 = base64de[in[3]];
// invalid base64 data
if(B1 >= 0xFE || B2 >= 0xFE || B3 >= 0xFE || B4 >= 0xFE)
{
*outlen = (unsigned int)(out - orgout);
return BASE64_INVALID;
}
out[0] = (B1 << 2) | ((B2 >> 4) & 0x3);
out[1] = ((B2 & 0xF) << 4) | ((B3 >> 2) & 0xF);
out[2] = ((B3 & 0x3) << 6) | B4;
}
*outlen = (unsigned int)(out - orgout);
// last 4 characters
do
{
B1 = base64de[in[0]]; B2 = base64de[in[1]];
if(B1 >= 0xFE || B2 >= 0xFE)
{
ret = BASE64_INVALID;
break;
}
*outlen += 1;
out[0] = (B1 << 2) | ((B2 >> 4) & 0x3);
if((B3 = base64de[in[2]]) >= 0xFE) // 2 '=' must be one byte
{
if(B3 == 0xFF)
{
ret = BASE64_INVALID;
}
break;
}
*outlen += 1;
out[1] = ((B2 & 0xF) << 4) | ((B3 >> 2) & 0xF);
if((B4 = base64de[in[3]]) >= 0xFE) // 1 '=' must be two byte
{
if(B4 == 0xFF)
{
ret = BASE64_INVALID;
}
break;
}
*outlen += 1;
out[2] = ((B3 & 0x3) << 6) | B4;
} while (0);
return ret;
}