-
Notifications
You must be signed in to change notification settings - Fork 5
/
base64.cpp
203 lines (178 loc) · 4.34 KB
/
base64.cpp
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
#include "base64.h"
#include <string.h>
#define B0(a) (a & 0xFF)
#define B1(a) (a >> 8 & 0xFF)
#define B2(a) (a >> 16 & 0xFF)
#define B3(a) (a >> 24 & 0xFF)
ZBase64::ZBase64(void)
{
}
ZBase64::~ZBase64(void)
{
if (!m_arrEnc.empty())
{
for (size_t i = 0; i < m_arrEnc.size(); i++)
{
delete[] m_arrEnc[i];
}
m_arrEnc.clear();
}
if (!m_arrDec.empty())
{
for (size_t i = 0; i < m_arrDec.size(); i++)
{
delete[] m_arrDec[i];
}
m_arrDec.clear();
}
}
char ZBase64::GetB64char(int nIndex)
{
static const char szTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
if (nIndex >= 0 && nIndex < 64)
{
return szTable[nIndex];
}
return '=';
}
int ZBase64::GetB64Index(char ch)
{
int index = -1;
if (ch >= 'A' && ch <= 'Z')
{
index = ch - 'A';
}
else if (ch >= 'a' && ch <= 'z')
{
index = ch - 'a' + 26;
}
else if (ch >= '0' && ch <= '9')
{
index = ch - '0' + 52;
}
else if (ch == '+')
{
index = 62;
}
else if (ch == '/')
{
index = 63;
}
return index;
}
const char *ZBase64::Encode(const char *szSrc, int nSrcLen)
{
if (0 == nSrcLen)
{
nSrcLen = (int)strlen(szSrc);
}
if (nSrcLen <= 0)
{
return "";
}
char *szEnc = new char[nSrcLen * 3 + 128];
m_arrEnc.push_back(szEnc);
int i = 0;
int len = 0;
unsigned char *psrc = (unsigned char *)szSrc;
char *p64 = szEnc;
for (i = 0; i < nSrcLen - 3; i += 3)
{
unsigned long ulTmp = *(unsigned long *)psrc;
register int b0 = GetB64char((B0(ulTmp) >> 2) & 0x3F);
register int b1 = GetB64char((B0(ulTmp) << 6 >> 2 | B1(ulTmp) >> 4) & 0x3F);
register int b2 = GetB64char((B1(ulTmp) << 4 >> 2 | B2(ulTmp) >> 6) & 0x3F);
register int b3 = GetB64char((B2(ulTmp) << 2 >> 2) & 0x3F);
*((unsigned long *)p64) = b0 | b1 << 8 | b2 << 16 | b3 << 24;
len += 4;
p64 += 4;
psrc += 3;
}
if (i < nSrcLen)
{
int rest = nSrcLen - i;
unsigned long ulTmp = 0;
for (int j = 0; j < rest; ++j)
{
*(((unsigned char *)&ulTmp) + j) = *psrc++;
}
p64[0] = GetB64char((B0(ulTmp) >> 2) & 0x3F);
p64[1] = GetB64char((B0(ulTmp) << 6 >> 2 | B1(ulTmp) >> 4) & 0x3F);
p64[2] = rest > 1 ? GetB64char((B1(ulTmp) << 4 >> 2 | B2(ulTmp) >> 6) & 0x3F) : '=';
p64[3] = rest > 2 ? GetB64char((B2(ulTmp) << 2 >> 2) & 0x3F) : '=';
p64 += 4;
len += 4;
}
*p64 = '\0';
return szEnc;
}
const char *ZBase64::Encode(const string &strInput)
{
return Encode(strInput.data(), strInput.size());
}
const char *ZBase64::Decode(const char *szSrc, int nSrcLen, int *pDecLen)
{
if (0 == nSrcLen)
{
nSrcLen = (int)strlen(szSrc);
}
if (nSrcLen <= 0)
{
return "";
}
char *szDec = new char[nSrcLen];
m_arrDec.push_back(szDec);
int i = 0;
int len = 0;
unsigned char *psrc = (unsigned char *)szSrc;
char *pbuf = szDec;
for (i = 0; i < nSrcLen - 4; i += 4)
{
unsigned long ulTmp = *(unsigned long *)psrc;
register int b0 = (GetB64Index((char)B0(ulTmp)) << 2 | GetB64Index((char)B1(ulTmp)) << 2 >> 6) & 0xFF;
register int b1 = (GetB64Index((char)B1(ulTmp)) << 4 | GetB64Index((char)B2(ulTmp)) << 2 >> 4) & 0xFF;
register int b2 = (GetB64Index((char)B2(ulTmp)) << 6 | GetB64Index((char)B3(ulTmp)) << 2 >> 2) & 0xFF;
*((unsigned long *)pbuf) = b0 | b1 << 8 | b2 << 16;
psrc += 4;
pbuf += 3;
len += 3;
}
if (i < nSrcLen)
{
int rest = nSrcLen - i;
unsigned long ulTmp = 0;
for (int j = 0; j < rest; ++j)
{
*(((unsigned char *)&ulTmp) + j) = *psrc++;
}
register int b0 = (GetB64Index((char)B0(ulTmp)) << 2 | GetB64Index((char)B1(ulTmp)) << 2 >> 6) & 0xFF;
*pbuf++ = b0;
len++;
if ('=' != B1(ulTmp) && '=' != B2(ulTmp))
{
register int b1 = (GetB64Index((char)B1(ulTmp)) << 4 | GetB64Index((char)B2(ulTmp)) << 2 >> 4) & 0xFF;
*pbuf++ = b1;
len++;
}
if ('=' != B2(ulTmp) && '=' != B3(ulTmp))
{
register int b2 = (GetB64Index((char)B2(ulTmp)) << 6 | GetB64Index((char)B3(ulTmp)) << 2 >> 2) & 0xFF;
*pbuf++ = b2;
len++;
}
}
*pbuf = '\0';
if (NULL != pDecLen)
{
*pDecLen = (int)(pbuf - szDec);
}
return szDec;
}
const char *ZBase64::Decode(const char *szSrc, string &strOutput)
{
strOutput.clear();
int nDecLen = 0;
const char *p = Decode(szSrc, 0, &nDecLen);
strOutput.append(p, nDecLen);
return strOutput.data();
}