OLD | NEW |
| (Empty) |
1 // Copyright 2014 PDFium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
6 | |
7 #include "xfa/src/fgas/crt/fgas_algorithm.h" | |
8 | |
9 #include "core/include/fxcrt/fx_basic.h" | |
10 | |
11 #ifdef __cplusplus | |
12 extern "C" { | |
13 #endif | |
14 | |
15 static const FX_CHAR g_FXBase64EncoderMap[64] = { | |
16 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', | |
17 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', | |
18 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', | |
19 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', | |
20 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', | |
21 }; | |
22 | |
23 struct FX_BASE64DATA { | |
24 FX_DWORD data1 : 2; | |
25 FX_DWORD data2 : 6; | |
26 FX_DWORD data3 : 4; | |
27 FX_DWORD data4 : 4; | |
28 FX_DWORD data5 : 6; | |
29 FX_DWORD data6 : 2; | |
30 FX_DWORD data7 : 8; | |
31 }; | |
32 | |
33 static void FX_Base64EncodePiece(const FX_BASE64DATA& src, | |
34 int32_t iBytes, | |
35 FX_CHAR dst[4]) { | |
36 dst[0] = g_FXBase64EncoderMap[src.data2]; | |
37 FX_DWORD b = src.data1 << 4; | |
38 if (iBytes > 1) { | |
39 b |= src.data4; | |
40 } | |
41 dst[1] = g_FXBase64EncoderMap[b]; | |
42 if (iBytes > 1) { | |
43 b = src.data3 << 2; | |
44 if (iBytes > 2) { | |
45 b |= src.data6; | |
46 } | |
47 dst[2] = g_FXBase64EncoderMap[b]; | |
48 if (iBytes > 2) { | |
49 dst[3] = g_FXBase64EncoderMap[src.data5]; | |
50 } else { | |
51 dst[3] = '='; | |
52 } | |
53 } else { | |
54 dst[2] = dst[3] = '='; | |
55 } | |
56 } | |
57 int32_t FX_Base64EncodeA(const uint8_t* pSrc, int32_t iSrcLen, FX_CHAR* pDst) { | |
58 FXSYS_assert(pSrc != NULL); | |
59 if (iSrcLen < 1) { | |
60 return 0; | |
61 } | |
62 if (pDst == NULL) { | |
63 int32_t iDstLen = iSrcLen / 3 * 4; | |
64 if ((iSrcLen % 3) != 0) { | |
65 iDstLen += 4; | |
66 } | |
67 return iDstLen; | |
68 } | |
69 FX_BASE64DATA srcData; | |
70 int32_t iBytes = 3; | |
71 FX_CHAR* pDstEnd = pDst; | |
72 while (iSrcLen > 0) { | |
73 if (iSrcLen > 2) { | |
74 ((uint8_t*)&srcData)[0] = *pSrc++; | |
75 ((uint8_t*)&srcData)[1] = *pSrc++; | |
76 ((uint8_t*)&srcData)[2] = *pSrc++; | |
77 iSrcLen -= 3; | |
78 } else { | |
79 *((FX_DWORD*)&srcData) = 0; | |
80 ((uint8_t*)&srcData)[0] = *pSrc++; | |
81 if (iSrcLen > 1) { | |
82 ((uint8_t*)&srcData)[1] = *pSrc++; | |
83 } | |
84 iBytes = iSrcLen; | |
85 iSrcLen = 0; | |
86 } | |
87 FX_Base64EncodePiece(srcData, iBytes, pDstEnd); | |
88 pDstEnd += 4; | |
89 } | |
90 return pDstEnd - pDst; | |
91 } | |
92 | |
93 static const uint8_t g_FXBase64DecoderMap[256] = { | |
94 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, | |
95 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, | |
96 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, | |
97 0xFF, 0xFF, 0xFF,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 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, | |
112 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, | |
113 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, | |
114 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, | |
115 0xFF, 0xFF, 0xFF, 0xFF, | |
116 }; | |
117 static void FX_Base64DecodePiece(const FX_CHAR src[4], | |
118 int32_t iChars, | |
119 FX_BASE64DATA& dst, | |
120 int32_t& iBytes) { | |
121 FXSYS_assert(iChars > 0 && iChars < 5); | |
122 iBytes = 1; | |
123 dst.data2 = g_FXBase64DecoderMap[(uint8_t)src[0]]; | |
124 if (iChars > 1) { | |
125 uint8_t b = g_FXBase64DecoderMap[(uint8_t)src[1]]; | |
126 dst.data1 = b >> 4; | |
127 dst.data4 = b; | |
128 if (iChars > 2) { | |
129 iBytes = 2; | |
130 b = g_FXBase64DecoderMap[(uint8_t)src[2]]; | |
131 dst.data3 = b >> 2; | |
132 dst.data6 = b; | |
133 if (iChars > 3) { | |
134 iBytes = 3; | |
135 dst.data5 = g_FXBase64DecoderMap[(uint8_t)src[3]]; | |
136 } else { | |
137 dst.data5 = 0; | |
138 } | |
139 } else { | |
140 dst.data3 = 0; | |
141 } | |
142 } else { | |
143 dst.data1 = 0; | |
144 } | |
145 } | |
146 int32_t FX_Base64DecodeA(const FX_CHAR* pSrc, int32_t iSrcLen, uint8_t* pDst) { | |
147 FXSYS_assert(pSrc != NULL); | |
148 if (iSrcLen < 1) { | |
149 return 0; | |
150 } | |
151 while (iSrcLen > 0 && pSrc[iSrcLen - 1] == '=') { | |
152 iSrcLen--; | |
153 } | |
154 if (iSrcLen < 1) { | |
155 return 0; | |
156 } | |
157 if (pDst == NULL) { | |
158 int32_t iDstLen = iSrcLen / 4 * 3; | |
159 iSrcLen %= 4; | |
160 if (iSrcLen == 1) { | |
161 iDstLen += 1; | |
162 } else if (iSrcLen == 2) { | |
163 iDstLen += 1; | |
164 } else if (iSrcLen == 3) { | |
165 iDstLen += 2; | |
166 } | |
167 return iDstLen; | |
168 } | |
169 FX_CHAR srcData[4]; | |
170 FX_BASE64DATA dstData; | |
171 int32_t iChars = 4, iBytes; | |
172 uint8_t* pDstEnd = pDst; | |
173 while (iSrcLen > 0) { | |
174 if (iSrcLen > 3) { | |
175 *((FX_DWORD*)srcData) = *((FX_DWORD*)pSrc); | |
176 pSrc += 4; | |
177 iSrcLen -= 4; | |
178 } else { | |
179 *((FX_DWORD*)&dstData) = 0; | |
180 *((FX_DWORD*)srcData) = 0; | |
181 srcData[0] = *pSrc++; | |
182 if (iSrcLen > 1) { | |
183 srcData[1] = *pSrc++; | |
184 } | |
185 if (iSrcLen > 2) { | |
186 srcData[2] = *pSrc++; | |
187 } | |
188 iChars = iSrcLen; | |
189 iSrcLen = 0; | |
190 } | |
191 FX_Base64DecodePiece(srcData, iChars, dstData, iBytes); | |
192 *pDstEnd++ = ((uint8_t*)&dstData)[0]; | |
193 if (iBytes > 1) { | |
194 *pDstEnd++ = ((uint8_t*)&dstData)[1]; | |
195 } | |
196 if (iBytes > 2) { | |
197 *pDstEnd++ = ((uint8_t*)&dstData)[2]; | |
198 } | |
199 } | |
200 return pDstEnd - pDst; | |
201 } | |
202 int32_t FX_Base64DecodeW(const FX_WCHAR* pSrc, int32_t iSrcLen, uint8_t* pDst) { | |
203 FXSYS_assert(pSrc != NULL); | |
204 if (iSrcLen < 1) { | |
205 return 0; | |
206 } | |
207 while (iSrcLen > 0 && pSrc[iSrcLen - 1] == '=') { | |
208 iSrcLen--; | |
209 } | |
210 if (iSrcLen < 1) { | |
211 return 0; | |
212 } | |
213 if (pDst == NULL) { | |
214 int32_t iDstLen = iSrcLen / 4 * 3; | |
215 iSrcLen %= 4; | |
216 if (iSrcLen == 1) { | |
217 iDstLen += 1; | |
218 } else if (iSrcLen == 2) { | |
219 iDstLen += 1; | |
220 } else if (iSrcLen == 3) { | |
221 iDstLen += 2; | |
222 } | |
223 return iDstLen; | |
224 } | |
225 FX_CHAR srcData[4]; | |
226 FX_BASE64DATA dstData; | |
227 int32_t iChars = 4, iBytes; | |
228 uint8_t* pDstEnd = pDst; | |
229 while (iSrcLen > 0) { | |
230 if (iSrcLen > 3) { | |
231 srcData[0] = (FX_CHAR)*pSrc++; | |
232 srcData[1] = (FX_CHAR)*pSrc++; | |
233 srcData[2] = (FX_CHAR)*pSrc++; | |
234 srcData[3] = (FX_CHAR)*pSrc++; | |
235 iSrcLen -= 4; | |
236 } else { | |
237 *((FX_DWORD*)&dstData) = 0; | |
238 *((FX_DWORD*)srcData) = 0; | |
239 srcData[0] = (FX_CHAR)*pSrc++; | |
240 if (iSrcLen > 1) { | |
241 srcData[1] = (FX_CHAR)*pSrc++; | |
242 } | |
243 if (iSrcLen > 2) { | |
244 srcData[2] = (FX_CHAR)*pSrc++; | |
245 } | |
246 iChars = iSrcLen; | |
247 iSrcLen = 0; | |
248 } | |
249 FX_Base64DecodePiece(srcData, iChars, dstData, iBytes); | |
250 *pDstEnd++ = ((uint8_t*)&dstData)[0]; | |
251 if (iBytes > 1) { | |
252 *pDstEnd++ = ((uint8_t*)&dstData)[1]; | |
253 } | |
254 if (iBytes > 2) { | |
255 *pDstEnd++ = ((uint8_t*)&dstData)[2]; | |
256 } | |
257 } | |
258 return pDstEnd - pDst; | |
259 } | |
260 | |
261 static const uint8_t g_FXHex2DecMap[256] = { | |
262 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
263 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
264 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, | |
265 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
266 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, | |
267 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
268 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
269 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
270 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
271 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
272 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
273 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
274 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
275 }; | |
276 uint8_t FX_Hex2Dec(uint8_t hexHigh, uint8_t hexLow) { | |
277 return (g_FXHex2DecMap[hexHigh] << 4) + g_FXHex2DecMap[hexLow]; | |
278 } | |
279 int32_t FX_SeparateStringW(const FX_WCHAR* pStr, | |
280 int32_t iStrLen, | |
281 FX_WCHAR delimiter, | |
282 CFX_WideStringArray& pieces) { | |
283 if (pStr == NULL) { | |
284 return 0; | |
285 } | |
286 if (iStrLen < 0) { | |
287 iStrLen = FXSYS_wcslen(pStr); | |
288 } | |
289 const FX_WCHAR* pToken = pStr; | |
290 const FX_WCHAR* pEnd = pStr + iStrLen; | |
291 while (TRUE) { | |
292 if (pStr >= pEnd || delimiter == *pStr) { | |
293 CFX_WideString sub(pToken, pStr - pToken); | |
294 pieces.Add(sub); | |
295 pToken = pStr + 1; | |
296 if (pStr >= pEnd) { | |
297 break; | |
298 } | |
299 } | |
300 pStr++; | |
301 } | |
302 return pieces.GetSize(); | |
303 } | |
304 #ifdef __cplusplus | |
305 } | |
306 #endif | |
OLD | NEW |