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