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