/src/nss/lib/util/nssb64d.c
Line | Count | Source |
1 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
2 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | | |
5 | | /* |
6 | | * Base64 decoding (ascii to binary). |
7 | | */ |
8 | | |
9 | | #include "nssb64.h" |
10 | | #include "nspr.h" |
11 | | #include "secitem.h" |
12 | | #include "secerr.h" |
13 | | |
14 | | /* |
15 | | * XXX We want this basic support to go into NSPR (the PL part). |
16 | | * Until that can happen, the PL interface is going to be kept entirely |
17 | | * internal here -- all static functions and opaque data structures. |
18 | | * When someone can get it moved over into NSPR, that should be done: |
19 | | * - giving everything names that are accepted by the NSPR module owners |
20 | | * (though I tried to choose ones that would work without modification) |
21 | | * - exporting the functions (remove static declarations and add |
22 | | * to nssutil.def as necessary) |
23 | | * - put prototypes into appropriate header file (probably replacing |
24 | | * the entire current lib/libc/include/plbase64.h in NSPR) |
25 | | * along with a typedef for the context structure (which should be |
26 | | * kept opaque -- definition in the source file only, but typedef |
27 | | * ala "typedef struct PLBase64FooStr PLBase64Foo;" in header file) |
28 | | * - modify anything else as necessary to conform to NSPR required style |
29 | | * (I looked but found no formatting guide to follow) |
30 | | * |
31 | | * You will want to move over everything from here down to the comment |
32 | | * which says "XXX End of base64 decoding code to be moved into NSPR", |
33 | | * into a new file in NSPR. |
34 | | */ |
35 | | |
36 | | /* |
37 | | ************************************************************** |
38 | | * XXX Beginning of base64 decoding code to be moved into NSPR. |
39 | | */ |
40 | | |
41 | | /* |
42 | | * This typedef would belong in the NSPR header file (i.e. plbase64.h). |
43 | | */ |
44 | | typedef struct PLBase64DecoderStr PLBase64Decoder; |
45 | | |
46 | | /* |
47 | | * The following implementation of base64 decoding was based on code |
48 | | * found in libmime (specifically, in mimeenc.c). It has been adapted to |
49 | | * use PR types and naming as well as to provide other necessary semantics |
50 | | * (like buffer-in/buffer-out in addition to "streaming" without undue |
51 | | * performance hit of extra copying if you made the buffer versions |
52 | | * use the output_fn). It also incorporates some aspects of the current |
53 | | * NSPR base64 decoding code. As such, you may find similarities to |
54 | | * both of those implementations. I tried to use names that reflected |
55 | | * the original code when possible. For this reason you may find some |
56 | | * inconsistencies -- libmime used lots of "in" and "out" whereas the |
57 | | * NSPR version uses "src" and "dest"; sometimes I changed one to the other |
58 | | * and sometimes I left them when I thought the subroutines were at least |
59 | | * self-consistent. |
60 | | */ |
61 | | |
62 | | PR_BEGIN_EXTERN_C |
63 | | |
64 | | /* |
65 | | * Opaque object used by the decoder to store state. |
66 | | */ |
67 | | struct PLBase64DecoderStr { |
68 | | /* Current token (or portion, if token_size < 4) being decoded. */ |
69 | | unsigned char token[4]; |
70 | | int token_size; |
71 | | |
72 | | /* |
73 | | * Where to write the decoded data (used when streaming, not when |
74 | | * doing all in-memory (buffer) operations). |
75 | | * |
76 | | * Note that this definition is chosen to be compatible with PR_Write. |
77 | | */ |
78 | | PRInt32 (*output_fn)(void *output_arg, const unsigned char *buf, |
79 | | PRInt32 size); |
80 | | void *output_arg; |
81 | | |
82 | | /* |
83 | | * Where the decoded output goes -- either temporarily (in the streaming |
84 | | * case, staged here before it goes to the output function) or what will |
85 | | * be the entire buffered result for users of the buffer version. |
86 | | */ |
87 | | unsigned char *output_buffer; |
88 | | PRUint32 output_buflen; /* the total length of allocated buffer */ |
89 | | PRUint32 output_length; /* the length that is currently populated */ |
90 | | }; |
91 | | |
92 | | PR_END_EXTERN_C |
93 | | |
94 | | /* A constant time range check for unsigned chars. |
95 | | * Returns 255 if a <= x <= b and 0 otherwise. |
96 | | */ |
97 | | static inline unsigned char |
98 | | ct_u8_in_range(unsigned char x, unsigned char a, unsigned char b) |
99 | 19.1M | { |
100 | | /* Let x, a, b be ints in {0, 1, ... 255}. |
101 | | * The value (a - x - 1) is in {-256, ..., 254}, so the low |
102 | | * 8 bits of |
103 | | * (a - x - 1) >> 8 |
104 | | * are all 1 if a <= x and all 0 if a > x. |
105 | | * |
106 | | * Likewise the low 8 bits of |
107 | | * ((a - x - 1) >> 8) & ((x - c - 1) >> 8) |
108 | | * are all 1 if a <= x <= c and all 0 otherwise. |
109 | | * |
110 | | * The same is true if we perform the shift after the AND |
111 | | * ((a - x - 1) & (x - b - 1)) >> 8. |
112 | | */ |
113 | 19.1M | return (unsigned char)(((a - x - 1) & (x - b - 1)) >> 8); |
114 | 19.1M | } |
115 | | |
116 | | /* Convert a base64 code [A-Za-z0-9+/] to its value in {1, 2, ..., 64}. |
117 | | * The use of 1-64 instead of 0-63 is so that the special value of zero can |
118 | | * denote an invalid mapping; that was much easier than trying to fill in the |
119 | | * other values with some value other than zero, and to check for it. |
120 | | * Just remember to SUBTRACT ONE when using the value retrieved. |
121 | | */ |
122 | | static unsigned char |
123 | | pl_base64_codetovaluep1(unsigned char code) |
124 | 3.83M | { |
125 | 3.83M | unsigned char mask; |
126 | 3.83M | unsigned char res = 0; |
127 | | |
128 | | /* The range 'A' to 'Z' is mapped to 1 to 26 */ |
129 | 3.83M | mask = ct_u8_in_range(code, 'A', 'Z'); |
130 | 3.83M | res |= mask & (code - 'A' + 1); |
131 | | |
132 | | /* The range 'a' to 'z' is mapped to 27 to 52 */ |
133 | 3.83M | mask = ct_u8_in_range(code, 'a', 'z'); |
134 | 3.83M | res |= mask & (code - 'a' + 27); |
135 | | |
136 | | /* The range '0' to '9' is mapped to 53 to 62 */ |
137 | 3.83M | mask = ct_u8_in_range(code, '0', '9'); |
138 | 3.83M | res |= mask & (code - '0' + 53); |
139 | | |
140 | | /* The code '+' is mapped to 63 */ |
141 | 3.83M | mask = ct_u8_in_range(code, '+', '+'); |
142 | 3.83M | res |= mask & 63; |
143 | | |
144 | | /* The code '/' is mapped to 64 */ |
145 | 3.83M | mask = ct_u8_in_range(code, '/', '/'); |
146 | 3.83M | res |= mask & 64; |
147 | | |
148 | | /* All other characters, including '=' are mapped to 0. */ |
149 | 3.83M | return res; |
150 | 3.83M | } |
151 | | |
152 | 738k | #define B64_PAD '=' |
153 | | |
154 | | /* |
155 | | * Reads 4; writes 3 (known, or expected, to have no trailing padding). |
156 | | * Returns bytes written; -1 on error (unexpected character). |
157 | | */ |
158 | | static int |
159 | | pl_base64_decode_4to3(const unsigned char *in, unsigned char *out) |
160 | 384k | { |
161 | 384k | int j; |
162 | 384k | PRUint32 num = 0; |
163 | 384k | unsigned char bits; |
164 | | |
165 | 1.91M | for (j = 0; j < 4; j++) { |
166 | 1.53M | bits = pl_base64_codetovaluep1(in[j]); |
167 | 1.53M | if (bits == 0) |
168 | 8.61k | return -1; |
169 | 1.53M | num = (num << 6) | (bits - 1); |
170 | 1.53M | } |
171 | | |
172 | 376k | out[0] = (unsigned char)(num >> 16); |
173 | 376k | out[1] = (unsigned char)((num >> 8) & 0xFF); |
174 | 376k | out[2] = (unsigned char)(num & 0xFF); |
175 | | |
176 | 376k | return 3; |
177 | 384k | } |
178 | | |
179 | | /* |
180 | | * Reads 3; writes 2 (caller already confirmed EOF or trailing padding). |
181 | | * Returns bytes written; -1 on error (unexpected character). |
182 | | */ |
183 | | static int |
184 | | pl_base64_decode_3to2(const unsigned char *in, unsigned char *out) |
185 | 8.59k | { |
186 | 8.59k | PRUint32 num = 0; |
187 | 8.59k | unsigned char bits1, bits2, bits3; |
188 | | |
189 | 8.59k | bits1 = pl_base64_codetovaluep1(in[0]); |
190 | 8.59k | bits2 = pl_base64_codetovaluep1(in[1]); |
191 | 8.59k | bits3 = pl_base64_codetovaluep1(in[2]); |
192 | | |
193 | 8.59k | if ((bits1 == 0) || (bits2 == 0) || (bits3 == 0)) |
194 | 4 | return -1; |
195 | | |
196 | 8.58k | num = ((PRUint32)(bits1 - 1)) << 10; |
197 | 8.58k | num |= ((PRUint32)(bits2 - 1)) << 4; |
198 | 8.58k | num |= ((PRUint32)(bits3 - 1)) >> 2; |
199 | | |
200 | 8.58k | out[0] = (unsigned char)(num >> 8); |
201 | 8.58k | out[1] = (unsigned char)(num & 0xFF); |
202 | | |
203 | 8.58k | return 2; |
204 | 8.59k | } |
205 | | |
206 | | /* |
207 | | * Reads 2; writes 1 (caller already confirmed EOF or trailing padding). |
208 | | * Returns bytes written; -1 on error (unexpected character). |
209 | | */ |
210 | | static int |
211 | | pl_base64_decode_2to1(const unsigned char *in, unsigned char *out) |
212 | 37 | { |
213 | 37 | PRUint32 num = 0; |
214 | 37 | unsigned char bits1, bits2; |
215 | | |
216 | 37 | bits1 = pl_base64_codetovaluep1(in[0]); |
217 | 37 | bits2 = pl_base64_codetovaluep1(in[1]); |
218 | | |
219 | 37 | if ((bits1 == 0) || (bits2 == 0)) |
220 | 16 | return -1; |
221 | | |
222 | 21 | num = ((PRUint32)(bits1 - 1)) << 2; |
223 | 21 | num |= ((PRUint32)(bits2 - 1)) >> 4; |
224 | | |
225 | 21 | out[0] = (unsigned char)num; |
226 | | |
227 | 21 | return 1; |
228 | 37 | } |
229 | | |
230 | | /* |
231 | | * Reads 4; writes 0-3. Returns bytes written or -1 on error. |
232 | | * (Writes less than 3 only at (presumed) EOF.) |
233 | | */ |
234 | | static int |
235 | | pl_base64_decode_token(const unsigned char *in, unsigned char *out) |
236 | 8.64k | { |
237 | 8.64k | if (in[3] != B64_PAD) |
238 | 20 | return pl_base64_decode_4to3(in, out); |
239 | | |
240 | 8.62k | if (in[2] == B64_PAD) |
241 | 37 | return pl_base64_decode_2to1(in, out); |
242 | | |
243 | 8.59k | return pl_base64_decode_3to2(in, out); |
244 | 8.62k | } |
245 | | |
246 | | static PRStatus |
247 | | pl_base64_decode_buffer(PLBase64Decoder *data, const unsigned char *in, |
248 | | PRUint32 length) |
249 | 8.68k | { |
250 | 8.68k | unsigned char *out = data->output_buffer; |
251 | 8.68k | unsigned char *token = data->token; |
252 | 8.68k | int i, n = 0; |
253 | | |
254 | 8.68k | i = data->token_size; |
255 | 8.68k | data->token_size = 0; |
256 | | |
257 | 384k | while (length > 0) { |
258 | 2.63M | while (i < 4 && length > 0) { |
259 | | /* |
260 | | * XXX Note that the following simply ignores any unexpected |
261 | | * characters. This is exactly what the original code in |
262 | | * libmime did, and I am leaving it. We certainly want to skip |
263 | | * over whitespace (we must); this does much more than that. |
264 | | * I am not confident changing it, and I don't want to slow |
265 | | * the processing down doing more complicated checking, but |
266 | | * someone else might have different ideas in the future. |
267 | | */ |
268 | 2.25M | if (pl_base64_codetovaluep1(*in) > 0 || *in == B64_PAD) |
269 | 1.53M | token[i++] = *in; |
270 | 2.25M | in++; |
271 | 2.25M | length--; |
272 | 2.25M | } |
273 | | |
274 | 384k | if (i < 4) { |
275 | | /* Didn't get enough for a complete token. */ |
276 | 81 | data->token_size = i; |
277 | 81 | break; |
278 | 81 | } |
279 | 384k | i = 0; |
280 | | |
281 | 384k | PR_ASSERT((PRUint32)(out - data->output_buffer + 3) <= data->output_buflen); |
282 | | |
283 | | /* |
284 | | * Assume we are not at the end; the following function only works |
285 | | * for an internal token (no trailing padding characters) but is |
286 | | * faster that way. If it hits an invalid character (padding) it |
287 | | * will return an error; we break out of the loop and try again |
288 | | * calling the routine that will handle a final token. |
289 | | * Note that we intentionally do it this way rather than explicitly |
290 | | * add a check for padding here (because that would just slow down |
291 | | * the normal case) nor do we rely on checking whether we have more |
292 | | * input to process (because that would also slow it down but also |
293 | | * because we want to allow trailing garbage, especially white space |
294 | | * and cannot tell that without read-ahead, also a slow proposition). |
295 | | * Whew. Understand? |
296 | | */ |
297 | 384k | n = pl_base64_decode_4to3(token, out); |
298 | 384k | if (n < 0) |
299 | 8.59k | break; |
300 | | |
301 | | /* Advance "out" by the number of bytes just written to it. */ |
302 | 376k | out += n; |
303 | 376k | n = 0; |
304 | 376k | } |
305 | | |
306 | | /* |
307 | | * See big comment above, before call to pl_base64_decode_4to3. |
308 | | * Here we check if we error'd out of loop, and allow for the case |
309 | | * that we are processing the last interesting token. If the routine |
310 | | * which should handle padding characters also fails, then we just |
311 | | * have bad input and give up. |
312 | | */ |
313 | 8.68k | if (n < 0) { |
314 | 8.59k | n = pl_base64_decode_token(token, out); |
315 | 8.59k | if (n < 0) |
316 | 24 | return PR_FAILURE; |
317 | | |
318 | 8.57k | out += n; |
319 | 8.57k | } |
320 | | |
321 | | /* |
322 | | * As explained above, we can get here with more input remaining, but |
323 | | * it should be all characters we do not care about (i.e. would be |
324 | | * ignored when transferring from "in" to "token" in loop above, |
325 | | * except here we choose to ignore extraneous pad characters, too). |
326 | | * Swallow it, performing that check. If we find more characters that |
327 | | * we would expect to decode, something is wrong. |
328 | | */ |
329 | 22.0k | while (length > 0) { |
330 | 13.3k | if (pl_base64_codetovaluep1(*in) > 0) |
331 | 34 | return PR_FAILURE; |
332 | 13.3k | in++; |
333 | 13.3k | length--; |
334 | 13.3k | } |
335 | | |
336 | | /* Record the length of decoded data we have left in output_buffer. */ |
337 | 8.62k | data->output_length = (PRUint32)(out - data->output_buffer); |
338 | 8.62k | return PR_SUCCESS; |
339 | 8.65k | } |
340 | | |
341 | | /* |
342 | | * Flush any remaining buffered characters. Given well-formed input, |
343 | | * this will have nothing to do. If the input was missing the padding |
344 | | * characters at the end, though, there could be 1-3 characters left |
345 | | * behind -- we will tolerate that by adding the padding for them. |
346 | | */ |
347 | | static PRStatus |
348 | | pl_base64_decode_flush(PLBase64Decoder *data) |
349 | 17.2k | { |
350 | 17.2k | int count; |
351 | | |
352 | | /* |
353 | | * If no remaining characters, or all are padding (also not well-formed |
354 | | * input, but again, be tolerant), then nothing more to do. (And, that |
355 | | * is considered successful.) |
356 | | */ |
357 | 17.2k | if (data->token_size == 0 || data->token[0] == B64_PAD) |
358 | 17.1k | return PR_SUCCESS; |
359 | | |
360 | 51 | if (!data->output_buffer) |
361 | 0 | return PR_FAILURE; |
362 | | |
363 | | /* |
364 | | * Assume we have all the interesting input except for some expected |
365 | | * padding characters. Add them and decode the resulting token. |
366 | | */ |
367 | 142 | while (data->token_size < 4) |
368 | 91 | data->token[data->token_size++] = B64_PAD; |
369 | | |
370 | 51 | data->token_size = 0; /* so a subsequent flush call is a no-op */ |
371 | | |
372 | 51 | count = pl_base64_decode_token(data->token, |
373 | 51 | data->output_buffer + data->output_length); |
374 | 51 | if (count < 0) |
375 | 16 | return PR_FAILURE; |
376 | | |
377 | | /* |
378 | | * If there is an output function, call it with this last bit of data. |
379 | | * Otherwise we are doing all buffered output, and the decoded bytes |
380 | | * are now there, we just need to reflect that in the length. |
381 | | */ |
382 | 35 | if (data->output_fn != NULL) { |
383 | 0 | PRInt32 output_result; |
384 | |
|
385 | 0 | PR_ASSERT(data->output_length == 0); |
386 | 0 | output_result = data->output_fn(data->output_arg, |
387 | 0 | data->output_buffer, |
388 | 0 | (PRInt32)count); |
389 | 0 | if (output_result < 0) |
390 | 0 | return PR_FAILURE; |
391 | 35 | } else { |
392 | 35 | data->output_length += count; |
393 | 35 | } |
394 | | |
395 | 35 | return PR_SUCCESS; |
396 | 35 | } |
397 | | |
398 | | /* |
399 | | * The maximum space needed to hold the output of the decoder given |
400 | | * input data of length "size". |
401 | | */ |
402 | | static PRUint32 |
403 | | PL_Base64MaxDecodedLength(PRUint32 size) |
404 | 17.3k | { |
405 | 17.3k | return (((PRUint64)size) * 3) / 4; |
406 | 17.3k | } |
407 | | |
408 | | /* |
409 | | * A distinct internal creation function for the buffer version to use. |
410 | | * (It does not want to specify an output_fn, and we want the normal |
411 | | * Create function to require that.) If more common initialization |
412 | | * of the decoding context needs to be done, it should be done *here*. |
413 | | */ |
414 | | static PLBase64Decoder * |
415 | | pl_base64_create_decoder(void) |
416 | 8.68k | { |
417 | 8.68k | return PR_NEWZAP(PLBase64Decoder); |
418 | 8.68k | } |
419 | | |
420 | | /* |
421 | | * Function to start a base64 decoding context. |
422 | | * An "output_fn" is required; the "output_arg" parameter to that is optional. |
423 | | */ |
424 | | static PLBase64Decoder * |
425 | | PL_CreateBase64Decoder(PRInt32 (*output_fn)(void *, const unsigned char *, |
426 | | PRInt32), |
427 | | void *output_arg) |
428 | 0 | { |
429 | 0 | PLBase64Decoder *data; |
430 | |
|
431 | 0 | if (output_fn == NULL) { |
432 | 0 | PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); |
433 | 0 | return NULL; |
434 | 0 | } |
435 | | |
436 | 0 | data = pl_base64_create_decoder(); |
437 | 0 | if (data != NULL) { |
438 | 0 | data->output_fn = output_fn; |
439 | 0 | data->output_arg = output_arg; |
440 | 0 | } |
441 | 0 | return data; |
442 | 0 | } |
443 | | |
444 | | /* |
445 | | * Push data through the decoder, causing the output_fn (provided to Create) |
446 | | * to be called with the decoded data. |
447 | | */ |
448 | | static PRStatus |
449 | | PL_UpdateBase64Decoder(PLBase64Decoder *data, const char *buffer, |
450 | | PRUint32 size) |
451 | 0 | { |
452 | 0 | PRUint32 need_length; |
453 | 0 | PRStatus status; |
454 | | |
455 | | /* XXX Should we do argument checking only in debug build? */ |
456 | 0 | if (data == NULL || buffer == NULL || size == 0) { |
457 | 0 | PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); |
458 | 0 | return PR_FAILURE; |
459 | 0 | } |
460 | | |
461 | | /* |
462 | | * How much space could this update need for decoding? |
463 | | * Guard against integer overflow: both size and token_size are PRUint32, |
464 | | * so their sum can wrap if size is near PR_UINT32_MAX. |
465 | | */ |
466 | 0 | if (size > PR_UINT32_MAX - data->token_size) { |
467 | 0 | PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); |
468 | 0 | return PR_FAILURE; |
469 | 0 | } |
470 | 0 | need_length = PL_Base64MaxDecodedLength(size + data->token_size); |
471 | | |
472 | | /* |
473 | | * Make sure we have at least that much. If not, (re-)allocate. |
474 | | */ |
475 | 0 | if (need_length > data->output_buflen) { |
476 | 0 | unsigned char *output_buffer = data->output_buffer; |
477 | |
|
478 | 0 | if (output_buffer != NULL) |
479 | 0 | output_buffer = (unsigned char *)PR_Realloc(output_buffer, |
480 | 0 | need_length); |
481 | 0 | else |
482 | 0 | output_buffer = (unsigned char *)PR_Malloc(need_length); |
483 | |
|
484 | 0 | if (output_buffer == NULL) |
485 | 0 | return PR_FAILURE; |
486 | | |
487 | 0 | data->output_buffer = output_buffer; |
488 | 0 | data->output_buflen = need_length; |
489 | 0 | } |
490 | | |
491 | | /* There should not have been any leftover output data in the buffer. */ |
492 | 0 | PR_ASSERT(data->output_length == 0); |
493 | 0 | data->output_length = 0; |
494 | |
|
495 | 0 | status = pl_base64_decode_buffer(data, (const unsigned char *)buffer, |
496 | 0 | size); |
497 | | |
498 | | /* Now that we have some decoded data, write it. */ |
499 | 0 | if (status == PR_SUCCESS && data->output_length > 0) { |
500 | 0 | PRInt32 output_result; |
501 | |
|
502 | 0 | PR_ASSERT(data->output_fn != NULL); |
503 | 0 | PR_ASSERT(data->output_length <= PR_INT32_MAX); |
504 | 0 | output_result = data->output_fn(data->output_arg, |
505 | 0 | data->output_buffer, |
506 | 0 | (PRInt32)data->output_length); |
507 | 0 | if (output_result < 0) |
508 | 0 | status = PR_FAILURE; |
509 | 0 | } |
510 | |
|
511 | 0 | data->output_length = 0; |
512 | 0 | return status; |
513 | 0 | } |
514 | | |
515 | | /* |
516 | | * When you're done decoding, call this to free the data. If "abort_p" |
517 | | * is false, then calling this may cause the output_fn to be called |
518 | | * one last time (as the last buffered data is flushed out). |
519 | | */ |
520 | | static PRStatus |
521 | | PL_DestroyBase64Decoder(PLBase64Decoder *data, PRBool abort_p) |
522 | 8.68k | { |
523 | 8.68k | PRStatus status = PR_SUCCESS; |
524 | | |
525 | | /* XXX Should we do argument checking only in debug build? */ |
526 | 8.68k | if (data == NULL) { |
527 | 0 | PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); |
528 | 0 | return PR_FAILURE; |
529 | 0 | } |
530 | | |
531 | | /* Flush out the last few buffered characters. */ |
532 | 8.68k | if (!abort_p) |
533 | 8.60k | status = pl_base64_decode_flush(data); |
534 | | |
535 | 8.68k | if (data->output_buffer != NULL) |
536 | 0 | PR_Free(data->output_buffer); |
537 | 8.68k | PR_Free(data); |
538 | | |
539 | 8.68k | return status; |
540 | 8.68k | } |
541 | | |
542 | | /* |
543 | | * Perform base64 decoding from an input buffer to an output buffer. |
544 | | * The output buffer can be provided (as "dest"); you can also pass in |
545 | | * a NULL and this function will allocate a buffer large enough for you, |
546 | | * and return it. If you do provide the output buffer, you must also |
547 | | * provide the maximum length of that buffer (as "maxdestlen"). |
548 | | * The actual decoded length of output will be returned to you in |
549 | | * "output_destlen". |
550 | | * |
551 | | * Return value is NULL on error, the output buffer (allocated or provided) |
552 | | * otherwise. |
553 | | */ |
554 | | static unsigned char * |
555 | | PL_Base64DecodeBuffer(const char *src, PRUint32 srclen, unsigned char *dest, |
556 | | PRUint32 maxdestlen, PRUint32 *output_destlen) |
557 | 8.68k | { |
558 | 8.68k | PRUint32 need_length; |
559 | 8.68k | unsigned char *output_buffer = NULL; |
560 | 8.68k | PLBase64Decoder *data = NULL; |
561 | 8.68k | PRStatus status; |
562 | | |
563 | 8.68k | PR_ASSERT(srclen > 0); |
564 | 8.68k | if (srclen == 0) { |
565 | 0 | PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); |
566 | 0 | return NULL; |
567 | 0 | } |
568 | | |
569 | | /* |
570 | | * How much space could we possibly need for decoding this input? |
571 | | */ |
572 | 8.68k | need_length = PL_Base64MaxDecodedLength(srclen); |
573 | | |
574 | | /* |
575 | | * Make sure we have at least that much, if output buffer provided. |
576 | | * If no output buffer provided, then we allocate that much. |
577 | | */ |
578 | 8.68k | if (dest != NULL) { |
579 | 8.68k | PR_ASSERT(maxdestlen >= need_length); |
580 | 8.68k | if (maxdestlen < need_length) { |
581 | 0 | PR_SetError(PR_BUFFER_OVERFLOW_ERROR, 0); |
582 | 0 | goto loser; |
583 | 0 | } |
584 | 8.68k | output_buffer = dest; |
585 | 8.68k | } else { |
586 | 0 | output_buffer = (unsigned char *)PR_Malloc(need_length); |
587 | 0 | if (output_buffer == NULL) |
588 | 0 | goto loser; |
589 | 0 | maxdestlen = need_length; |
590 | 0 | } |
591 | | |
592 | 8.68k | data = pl_base64_create_decoder(); |
593 | 8.68k | if (data == NULL) |
594 | 0 | goto loser; |
595 | | |
596 | 8.68k | data->output_buflen = maxdestlen; |
597 | 8.68k | data->output_buffer = output_buffer; |
598 | | |
599 | 8.68k | status = pl_base64_decode_buffer(data, (const unsigned char *)src, |
600 | 8.68k | srclen); |
601 | | |
602 | | /* |
603 | | * We do not wait for Destroy to flush, because Destroy will also |
604 | | * get rid of our decoder context, which we need to look at first! |
605 | | */ |
606 | 8.68k | if (status == PR_SUCCESS) |
607 | 8.62k | status = pl_base64_decode_flush(data); |
608 | | |
609 | | /* Must clear this or Destroy will free it. */ |
610 | 8.68k | data->output_buffer = NULL; |
611 | | |
612 | 8.68k | if (status == PR_SUCCESS) { |
613 | 8.60k | *output_destlen = data->output_length; |
614 | 8.60k | status = PL_DestroyBase64Decoder(data, PR_FALSE); |
615 | 8.60k | data = NULL; |
616 | 8.60k | if (status == PR_FAILURE) |
617 | 0 | goto loser; |
618 | 8.60k | return output_buffer; |
619 | 8.60k | } |
620 | | |
621 | 74 | loser: |
622 | 74 | if (dest == NULL && output_buffer != NULL) |
623 | 0 | PR_Free(output_buffer); |
624 | 74 | if (data != NULL) |
625 | 74 | (void)PL_DestroyBase64Decoder(data, PR_TRUE); |
626 | 74 | return NULL; |
627 | 8.68k | } |
628 | | |
629 | | /* |
630 | | * XXX End of base64 decoding code to be moved into NSPR. |
631 | | ******************************************************** |
632 | | */ |
633 | | |
634 | | /* |
635 | | * This is the beginning of the NSS cover functions. These will |
636 | | * provide the interface we want to expose as NSS-ish. For example, |
637 | | * they will operate on our Items, do any special handling or checking |
638 | | * we want to do, etc. |
639 | | */ |
640 | | |
641 | | PR_BEGIN_EXTERN_C |
642 | | |
643 | | /* |
644 | | * A boring cover structure for now. Perhaps someday it will include |
645 | | * some more interesting fields. |
646 | | */ |
647 | | struct NSSBase64DecoderStr { |
648 | | PLBase64Decoder *pl_data; |
649 | | }; |
650 | | |
651 | | PR_END_EXTERN_C |
652 | | |
653 | | /* |
654 | | * Function to start a base64 decoding context. |
655 | | */ |
656 | | NSSBase64Decoder * |
657 | | NSSBase64Decoder_Create(PRInt32 (*output_fn)(void *, const unsigned char *, |
658 | | PRInt32), |
659 | | void *output_arg) |
660 | 0 | { |
661 | 0 | PLBase64Decoder *pl_data; |
662 | 0 | NSSBase64Decoder *nss_data; |
663 | |
|
664 | 0 | nss_data = PORT_ZNew(NSSBase64Decoder); |
665 | 0 | if (nss_data == NULL) |
666 | 0 | return NULL; |
667 | | |
668 | 0 | pl_data = PL_CreateBase64Decoder(output_fn, output_arg); |
669 | 0 | if (pl_data == NULL) { |
670 | 0 | PORT_Free(nss_data); |
671 | 0 | return NULL; |
672 | 0 | } |
673 | | |
674 | 0 | nss_data->pl_data = pl_data; |
675 | 0 | return nss_data; |
676 | 0 | } |
677 | | |
678 | | /* |
679 | | * Push data through the decoder, causing the output_fn (provided to Create) |
680 | | * to be called with the decoded data. |
681 | | */ |
682 | | SECStatus |
683 | | NSSBase64Decoder_Update(NSSBase64Decoder *data, const char *buffer, |
684 | | PRUint32 size) |
685 | 0 | { |
686 | 0 | PRStatus pr_status; |
687 | | |
688 | | /* XXX Should we do argument checking only in debug build? */ |
689 | 0 | if (data == NULL) { |
690 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
691 | 0 | return SECFailure; |
692 | 0 | } |
693 | | |
694 | 0 | pr_status = PL_UpdateBase64Decoder(data->pl_data, buffer, size); |
695 | 0 | if (pr_status == PR_FAILURE) |
696 | 0 | return SECFailure; |
697 | | |
698 | 0 | return SECSuccess; |
699 | 0 | } |
700 | | |
701 | | /* |
702 | | * When you're done decoding, call this to free the data. If "abort_p" |
703 | | * is false, then calling this may cause the output_fn to be called |
704 | | * one last time (as the last buffered data is flushed out). |
705 | | */ |
706 | | SECStatus |
707 | | NSSBase64Decoder_Destroy(NSSBase64Decoder *data, PRBool abort_p) |
708 | 0 | { |
709 | 0 | PRStatus pr_status; |
710 | | |
711 | | /* XXX Should we do argument checking only in debug build? */ |
712 | 0 | if (data == NULL) { |
713 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
714 | 0 | return SECFailure; |
715 | 0 | } |
716 | | |
717 | 0 | pr_status = PL_DestroyBase64Decoder(data->pl_data, abort_p); |
718 | |
|
719 | 0 | PORT_Free(data); |
720 | |
|
721 | 0 | if (pr_status == PR_FAILURE) |
722 | 0 | return SECFailure; |
723 | | |
724 | 0 | return SECSuccess; |
725 | 0 | } |
726 | | |
727 | | /* |
728 | | * Perform base64 decoding from an ascii string "inStr" to an Item. |
729 | | * The length of the input must be provided as "inLen". The Item |
730 | | * may be provided (as "outItemOpt"); you can also pass in a NULL |
731 | | * and the Item will be allocated for you. |
732 | | * |
733 | | * In any case, the data within the Item will be allocated for you. |
734 | | * All allocation will happen out of the passed-in "arenaOpt", if non-NULL. |
735 | | * If "arenaOpt" is NULL, standard allocation (heap) will be used and |
736 | | * you will want to free the result via SECITEM_FreeItem. |
737 | | * |
738 | | * Return value is NULL on error, the Item (allocated or provided) otherwise. |
739 | | */ |
740 | | SECItem * |
741 | | NSSBase64_DecodeBuffer(PLArenaPool *arenaOpt, SECItem *outItemOpt, |
742 | | const char *inStr, unsigned int inLen) |
743 | 8.68k | { |
744 | 8.68k | SECItem *out_item = NULL; |
745 | 8.68k | PRUint32 max_out_len = 0; |
746 | 8.68k | void *mark = NULL; |
747 | 8.68k | unsigned char *dummy = NULL; |
748 | | |
749 | 8.68k | if ((outItemOpt != NULL && outItemOpt->data != NULL) || inLen == 0) { |
750 | 1 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
751 | 1 | return NULL; |
752 | 1 | } |
753 | | |
754 | 8.68k | if (arenaOpt != NULL) |
755 | 0 | mark = PORT_ArenaMark(arenaOpt); |
756 | | |
757 | 8.68k | max_out_len = PL_Base64MaxDecodedLength(inLen); |
758 | 8.68k | if (max_out_len == 0) { |
759 | 1 | goto loser; |
760 | 1 | } |
761 | 8.68k | out_item = SECITEM_AllocItem(arenaOpt, outItemOpt, max_out_len); |
762 | 8.68k | if (out_item == NULL) { |
763 | 0 | goto loser; |
764 | 0 | } |
765 | | |
766 | 8.68k | dummy = PL_Base64DecodeBuffer(inStr, inLen, out_item->data, |
767 | 8.68k | max_out_len, &out_item->len); |
768 | 8.68k | if (dummy == NULL) { |
769 | 74 | goto loser; |
770 | 74 | } |
771 | 8.60k | if (arenaOpt != NULL) { |
772 | 0 | PORT_ArenaUnmark(arenaOpt, mark); |
773 | 0 | } |
774 | 8.60k | return out_item; |
775 | | |
776 | 75 | loser: |
777 | 75 | if (arenaOpt != NULL) { |
778 | 0 | PORT_ArenaRelease(arenaOpt, mark); |
779 | 0 | if (outItemOpt != NULL) { |
780 | 0 | outItemOpt->data = NULL; |
781 | 0 | outItemOpt->len = 0; |
782 | 0 | } |
783 | 75 | } else if (dummy == NULL) { |
784 | 75 | SECITEM_FreeItem(out_item, (PRBool)(outItemOpt == NULL)); |
785 | 75 | } |
786 | 75 | return NULL; |
787 | 8.68k | } |
788 | | |
789 | | /* |
790 | | * XXX Everything below is deprecated. If you add new stuff, put it |
791 | | * *above*, not below. |
792 | | */ |
793 | | |
794 | | /* |
795 | | * XXX The following "ATOB" functions are provided for backward compatibility |
796 | | * with current code. They should be considered strongly deprecated. |
797 | | * When we can convert all our code over to using the new NSSBase64Decoder_ |
798 | | * functions defined above, we should get rid of these altogether. (Remove |
799 | | * protoypes from base64.h as well -- actually, remove that file completely). |
800 | | * If someone thinks either of these functions provides such a very useful |
801 | | * interface (though, as shown, the same functionality can already be |
802 | | * obtained by calling NSSBase64_DecodeBuffer directly), fine -- but then |
803 | | * that API should be provided with a nice new NSSFoo name and using |
804 | | * appropriate types, etc. |
805 | | */ |
806 | | |
807 | | #include "base64.h" |
808 | | |
809 | | /* |
810 | | ** Return an PORT_Alloc'd string which is the base64 decoded version |
811 | | ** of the input string; set *lenp to the length of the returned data. |
812 | | */ |
813 | | unsigned char * |
814 | | ATOB_AsciiToData(const char *string, unsigned int *lenp) |
815 | 157 | { |
816 | 157 | SECItem binary_item, *dummy; |
817 | | |
818 | 157 | binary_item.data = NULL; |
819 | 157 | binary_item.len = 0; |
820 | | |
821 | 157 | dummy = NSSBase64_DecodeBuffer(NULL, &binary_item, string, |
822 | 157 | (PRUint32)PORT_Strlen(string)); |
823 | 157 | if (dummy == NULL) |
824 | 76 | return NULL; |
825 | | |
826 | 81 | PORT_Assert(dummy == &binary_item); |
827 | | |
828 | 81 | *lenp = dummy->len; |
829 | 81 | return dummy->data; |
830 | 157 | } |
831 | | |
832 | | /* |
833 | | ** Convert from ascii to binary encoding of an item. |
834 | | */ |
835 | | SECStatus |
836 | | ATOB_ConvertAsciiToItem(SECItem *binary_item, const char *ascii) |
837 | 0 | { |
838 | 0 | SECItem *dummy; |
839 | |
|
840 | 0 | if (binary_item == NULL) { |
841 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
842 | 0 | return SECFailure; |
843 | 0 | } |
844 | | |
845 | | /* |
846 | | * XXX Would prefer to assert here if data is non-null (actually, |
847 | | * don't need to, just let NSSBase64_DecodeBuffer do it), so as to |
848 | | * to catch unintended memory leaks, but callers are not clean in |
849 | | * this respect so we need to explicitly clear here to avoid the |
850 | | * assert in NSSBase64_DecodeBuffer. |
851 | | */ |
852 | 0 | binary_item->data = NULL; |
853 | 0 | binary_item->len = 0; |
854 | |
|
855 | 0 | dummy = NSSBase64_DecodeBuffer(NULL, binary_item, ascii, |
856 | 0 | (PRUint32)PORT_Strlen(ascii)); |
857 | |
|
858 | 0 | if (dummy == NULL) |
859 | 0 | return SECFailure; |
860 | | |
861 | 0 | return SECSuccess; |
862 | 0 | } |