/src/nspr/lib/libc/src/base64.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
3 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
5 | | |
6 | | #include "plbase64.h" |
7 | | #include "prlog.h" /* For PR_NOT_REACHED */ |
8 | | #include "prmem.h" /* for malloc / PR_MALLOC */ |
9 | | |
10 | | #include <string.h> /* for strlen */ |
11 | | |
12 | | static unsigned char *base = (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
13 | | |
14 | 0 | static void encode3to4(const unsigned char* src, unsigned char* dest) { |
15 | 0 | PRUint32 b32 = (PRUint32)0; |
16 | 0 | PRIntn i, j = 18; |
17 | |
|
18 | 0 | for (i = 0; i < 3; i++) { |
19 | 0 | b32 <<= 8; |
20 | 0 | b32 |= (PRUint32)src[i]; |
21 | 0 | } |
22 | |
|
23 | 0 | for (i = 0; i < 4; i++) { |
24 | 0 | dest[i] = base[(PRUint32)((b32 >> j) & 0x3F)]; |
25 | 0 | j -= 6; |
26 | 0 | } |
27 | |
|
28 | 0 | return; |
29 | 0 | } |
30 | | |
31 | 0 | static void encode2to4(const unsigned char* src, unsigned char* dest) { |
32 | 0 | dest[0] = base[(PRUint32)((src[0] >> 2) & 0x3F)]; |
33 | 0 | dest[1] = base[(PRUint32)(((src[0] & 0x03) << 4) | ((src[1] >> 4) & 0x0F))]; |
34 | 0 | dest[2] = base[(PRUint32)((src[1] & 0x0F) << 2)]; |
35 | 0 | dest[3] = (unsigned char)'='; |
36 | 0 | return; |
37 | 0 | } |
38 | | |
39 | 0 | static void encode1to4(const unsigned char* src, unsigned char* dest) { |
40 | 0 | dest[0] = base[(PRUint32)((src[0] >> 2) & 0x3F)]; |
41 | 0 | dest[1] = base[(PRUint32)((src[0] & 0x03) << 4)]; |
42 | 0 | dest[2] = (unsigned char)'='; |
43 | 0 | dest[3] = (unsigned char)'='; |
44 | 0 | return; |
45 | 0 | } |
46 | | |
47 | | static void encode(const unsigned char* src, PRUint32 srclen, |
48 | 0 | unsigned char* dest) { |
49 | 0 | while (srclen >= 3) { |
50 | 0 | encode3to4(src, dest); |
51 | 0 | src += 3; |
52 | 0 | dest += 4; |
53 | 0 | srclen -= 3; |
54 | 0 | } |
55 | |
|
56 | 0 | switch (srclen) { |
57 | 0 | case 2: |
58 | 0 | encode2to4(src, dest); |
59 | 0 | break; |
60 | 0 | case 1: |
61 | 0 | encode1to4(src, dest); |
62 | 0 | break; |
63 | 0 | case 0: |
64 | 0 | break; |
65 | 0 | default: |
66 | 0 | PR_NOT_REACHED("coding error"); |
67 | 0 | } |
68 | | |
69 | 0 | return; |
70 | 0 | } |
71 | | |
72 | | /* |
73 | | * PL_Base64Encode |
74 | | * |
75 | | * If the destination argument is NULL, a return buffer is |
76 | | * allocated, and the data therein will be null-terminated. |
77 | | * If the destination argument is not NULL, it is assumed to |
78 | | * be of sufficient size, and the contents will not be null- |
79 | | * terminated by this routine. |
80 | | * |
81 | | * Returns null if the allocation fails. |
82 | | */ |
83 | | |
84 | | PR_IMPLEMENT(char*) |
85 | 0 | PL_Base64Encode(const char* src, PRUint32 srclen, char* dest) { |
86 | 0 | if (0 == srclen) { |
87 | 0 | size_t len = strlen(src); |
88 | 0 | srclen = len; |
89 | | /* Detect truncation. */ |
90 | 0 | if (srclen != len) { |
91 | 0 | return (char*)0; |
92 | 0 | } |
93 | 0 | } |
94 | | |
95 | 0 | if ((char*)0 == dest) { |
96 | 0 | PRUint32 destlen; |
97 | | /* Ensure all PRUint32 values stay within range. */ |
98 | 0 | if (srclen > (PR_UINT32_MAX / 4) * 3) { |
99 | 0 | return (char*)0; |
100 | 0 | } |
101 | 0 | destlen = ((srclen + 2) / 3) * 4; |
102 | 0 | dest = (char*)PR_MALLOC(destlen + 1); |
103 | 0 | if ((char*)0 == dest) { |
104 | 0 | return (char*)0; |
105 | 0 | } |
106 | 0 | dest[destlen] = (char)0; /* null terminate */ |
107 | 0 | } |
108 | | |
109 | 0 | encode((const unsigned char*)src, srclen, (unsigned char*)dest); |
110 | 0 | return dest; |
111 | 0 | } |
112 | | |
113 | 0 | static PRInt32 codetovalue(unsigned char c) { |
114 | 0 | if ((c >= (unsigned char)'A') && (c <= (unsigned char)'Z')) { |
115 | 0 | return (PRInt32)(c - (unsigned char)'A'); |
116 | 0 | } else if ((c >= (unsigned char)'a') && (c <= (unsigned char)'z')) { |
117 | 0 | return ((PRInt32)(c - (unsigned char)'a') + 26); |
118 | 0 | } else if ((c >= (unsigned char)'0') && (c <= (unsigned char)'9')) { |
119 | 0 | return ((PRInt32)(c - (unsigned char)'0') + 52); |
120 | 0 | } else if ((unsigned char)'+' == c) { |
121 | 0 | return (PRInt32)62; |
122 | 0 | } else if ((unsigned char)'/' == c) { |
123 | 0 | return (PRInt32)63; |
124 | 0 | } else { |
125 | 0 | return -1; |
126 | 0 | } |
127 | 0 | } |
128 | | |
129 | 0 | static PRStatus decode4to3(const unsigned char* src, unsigned char* dest) { |
130 | 0 | PRUint32 b32 = (PRUint32)0; |
131 | 0 | PRInt32 bits; |
132 | 0 | PRIntn i; |
133 | |
|
134 | 0 | for (i = 0; i < 4; i++) { |
135 | 0 | bits = codetovalue(src[i]); |
136 | 0 | if (bits < 0) { |
137 | 0 | return PR_FAILURE; |
138 | 0 | } |
139 | | |
140 | 0 | b32 <<= 6; |
141 | 0 | b32 |= bits; |
142 | 0 | } |
143 | | |
144 | 0 | dest[0] = (unsigned char)((b32 >> 16) & 0xFF); |
145 | 0 | dest[1] = (unsigned char)((b32 >> 8) & 0xFF); |
146 | 0 | dest[2] = (unsigned char)((b32) & 0xFF); |
147 | |
|
148 | 0 | return PR_SUCCESS; |
149 | 0 | } |
150 | | |
151 | 0 | static PRStatus decode3to2(const unsigned char* src, unsigned char* dest) { |
152 | 0 | PRUint32 b32 = (PRUint32)0; |
153 | 0 | PRInt32 bits; |
154 | 0 | PRUint32 ubits; |
155 | |
|
156 | 0 | bits = codetovalue(src[0]); |
157 | 0 | if (bits < 0) { |
158 | 0 | return PR_FAILURE; |
159 | 0 | } |
160 | | |
161 | 0 | b32 = (PRUint32)bits; |
162 | 0 | b32 <<= 6; |
163 | |
|
164 | 0 | bits = codetovalue(src[1]); |
165 | 0 | if (bits < 0) { |
166 | 0 | return PR_FAILURE; |
167 | 0 | } |
168 | | |
169 | 0 | b32 |= (PRUint32)bits; |
170 | 0 | b32 <<= 4; |
171 | |
|
172 | 0 | bits = codetovalue(src[2]); |
173 | 0 | if (bits < 0) { |
174 | 0 | return PR_FAILURE; |
175 | 0 | } |
176 | | |
177 | 0 | ubits = (PRUint32)bits; |
178 | 0 | b32 |= (ubits >> 2); |
179 | |
|
180 | 0 | dest[0] = (unsigned char)((b32 >> 8) & 0xFF); |
181 | 0 | dest[1] = (unsigned char)((b32) & 0xFF); |
182 | |
|
183 | 0 | return PR_SUCCESS; |
184 | 0 | } |
185 | | |
186 | 0 | static PRStatus decode2to1(const unsigned char* src, unsigned char* dest) { |
187 | 0 | PRUint32 b32; |
188 | 0 | PRUint32 ubits; |
189 | 0 | PRInt32 bits; |
190 | |
|
191 | 0 | bits = codetovalue(src[0]); |
192 | 0 | if (bits < 0) { |
193 | 0 | return PR_FAILURE; |
194 | 0 | } |
195 | | |
196 | 0 | ubits = (PRUint32)bits; |
197 | 0 | b32 = (ubits << 2); |
198 | |
|
199 | 0 | bits = codetovalue(src[1]); |
200 | 0 | if (bits < 0) { |
201 | 0 | return PR_FAILURE; |
202 | 0 | } |
203 | | |
204 | 0 | ubits = (PRUint32)bits; |
205 | 0 | b32 |= (ubits >> 4); |
206 | |
|
207 | 0 | dest[0] = (unsigned char)b32; |
208 | |
|
209 | 0 | return PR_SUCCESS; |
210 | 0 | } |
211 | | |
212 | | static PRStatus decode(const unsigned char* src, PRUint32 srclen, |
213 | 0 | unsigned char* dest) { |
214 | 0 | PRStatus rv; |
215 | |
|
216 | 0 | while (srclen >= 4) { |
217 | 0 | rv = decode4to3(src, dest); |
218 | 0 | if (PR_SUCCESS != rv) { |
219 | 0 | return PR_FAILURE; |
220 | 0 | } |
221 | | |
222 | 0 | src += 4; |
223 | 0 | dest += 3; |
224 | 0 | srclen -= 4; |
225 | 0 | } |
226 | | |
227 | 0 | switch (srclen) { |
228 | 0 | case 3: |
229 | 0 | rv = decode3to2(src, dest); |
230 | 0 | break; |
231 | 0 | case 2: |
232 | 0 | rv = decode2to1(src, dest); |
233 | 0 | break; |
234 | 0 | case 1: |
235 | 0 | rv = PR_FAILURE; |
236 | 0 | break; |
237 | 0 | case 0: |
238 | 0 | rv = PR_SUCCESS; |
239 | 0 | break; |
240 | 0 | default: |
241 | 0 | PR_NOT_REACHED("coding error"); |
242 | 0 | } |
243 | | |
244 | 0 | return rv; |
245 | 0 | } |
246 | | |
247 | | /* |
248 | | * PL_Base64Decode |
249 | | * |
250 | | * If the destination argument is NULL, a return buffer is |
251 | | * allocated and the data therein will be null-terminated. |
252 | | * If the destination argument is not null, it is assumed |
253 | | * to be of sufficient size, and the data will not be null- |
254 | | * terminated by this routine. |
255 | | * |
256 | | * Returns null if the allocation fails, or if the source string is |
257 | | * not well-formed. |
258 | | */ |
259 | | |
260 | | PR_IMPLEMENT(char*) |
261 | 0 | PL_Base64Decode(const char* src, PRUint32 srclen, char* dest) { |
262 | 0 | PRStatus status; |
263 | 0 | PRBool allocated = PR_FALSE; |
264 | |
|
265 | 0 | if ((char*)0 == src) { |
266 | 0 | return (char*)0; |
267 | 0 | } |
268 | | |
269 | 0 | if (0 == srclen) { |
270 | 0 | size_t len = strlen(src); |
271 | 0 | srclen = len; |
272 | | /* Detect truncation. */ |
273 | 0 | if (srclen != len) { |
274 | 0 | return (char*)0; |
275 | 0 | } |
276 | 0 | } |
277 | | |
278 | 0 | if (srclen && (0 == (srclen & 3))) { |
279 | 0 | if ((char)'=' == src[srclen - 1]) { |
280 | 0 | if ((char)'=' == src[srclen - 2]) { |
281 | 0 | srclen -= 2; |
282 | 0 | } else { |
283 | 0 | srclen -= 1; |
284 | 0 | } |
285 | 0 | } |
286 | 0 | } |
287 | |
|
288 | 0 | if ((char*)0 == dest) { |
289 | | /* The following computes ((srclen * 3) / 4) without overflow. */ |
290 | 0 | PRUint32 destlen = (srclen / 4) * 3 + ((srclen % 4) * 3) / 4; |
291 | 0 | dest = (char*)PR_MALLOC(destlen + 1); |
292 | 0 | if ((char*)0 == dest) { |
293 | 0 | return (char*)0; |
294 | 0 | } |
295 | 0 | dest[destlen] = (char)0; /* null terminate */ |
296 | 0 | allocated = PR_TRUE; |
297 | 0 | } |
298 | | |
299 | 0 | status = decode((const unsigned char*)src, srclen, (unsigned char*)dest); |
300 | 0 | if (PR_SUCCESS != status) { |
301 | 0 | if (PR_TRUE == allocated) { |
302 | 0 | PR_DELETE(dest); |
303 | 0 | } |
304 | |
|
305 | 0 | return (char*)0; |
306 | 0 | } |
307 | | |
308 | 0 | return dest; |
309 | 0 | } |