/src/nss-nspr/nss/lib/util/nssb64e.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 encoding (binary to ascii). |
7 | | */ |
8 | | |
9 | | #include "nssb64.h" |
10 | | #include "nspr.h" |
11 | | #include "secitem.h" |
12 | | #include "secerr.h" |
13 | | |
14 | | /* |
15 | | * XXX See the big comment at the top of nssb64d.c about moving the |
16 | | * bulk of this code over into NSPR (the PL part). It all applies |
17 | | * here but I didn't want to duplicate it, to avoid divergence problems. |
18 | | */ |
19 | | |
20 | | /* |
21 | | ************************************************************** |
22 | | * XXX Beginning of base64 encoding code to be moved into NSPR. |
23 | | */ |
24 | | |
25 | | struct PLBase64EncodeStateStr { |
26 | | unsigned chunks; |
27 | | unsigned saved; |
28 | | unsigned char buf[3]; |
29 | | }; |
30 | | |
31 | | /* |
32 | | * This typedef would belong in the NSPR header file (i.e. plbase64.h). |
33 | | */ |
34 | | typedef struct PLBase64EncoderStr PLBase64Encoder; |
35 | | |
36 | | /* |
37 | | * The following implementation of base64 encoding was based on code |
38 | | * found in libmime (specifically, in mimeenc.c). It has been adapted to |
39 | | * use PR types and naming as well as to provide other necessary semantics |
40 | | * (like buffer-in/buffer-out in addition to "streaming" without undue |
41 | | * performance hit of extra copying if you made the buffer versions |
42 | | * use the output_fn). It also incorporates some aspects of the current |
43 | | * NSPR base64 encoding code. As such, you may find similarities to |
44 | | * both of those implementations. I tried to use names that reflected |
45 | | * the original code when possible. For this reason you may find some |
46 | | * inconsistencies -- libmime used lots of "in" and "out" whereas the |
47 | | * NSPR version uses "src" and "dest"; sometimes I changed one to the other |
48 | | * and sometimes I left them when I thought the subroutines were at least |
49 | | * self-consistent. |
50 | | */ |
51 | | |
52 | | PR_BEGIN_EXTERN_C |
53 | | |
54 | | /* |
55 | | * Opaque object used by the encoder to store state. |
56 | | */ |
57 | | struct PLBase64EncoderStr { |
58 | | /* |
59 | | * The one or two bytes pending. (We need 3 to create a "token", |
60 | | * and hold the leftovers here. in_buffer_count is *only* ever |
61 | | * 0, 1, or 2. |
62 | | */ |
63 | | unsigned char in_buffer[2]; |
64 | | int in_buffer_count; |
65 | | |
66 | | /* |
67 | | * If the caller wants linebreaks added, line_length specifies |
68 | | * where they come out. It must be a multiple of 4; if the caller |
69 | | * provides one that isn't, we round it down to the nearest |
70 | | * multiple of 4. |
71 | | * |
72 | | * The value of current_column counts how many characters have been |
73 | | * added since the last linebreaks (or since the beginning, on the |
74 | | * first line). It is also always a multiple of 4; it is unused when |
75 | | * line_length is 0. |
76 | | */ |
77 | | PRUint32 line_length; |
78 | | PRUint32 current_column; |
79 | | |
80 | | /* |
81 | | * Where to write the encoded data (used when streaming, not when |
82 | | * doing all in-memory (buffer) operations). |
83 | | * |
84 | | * Note that this definition is chosen to be compatible with PR_Write. |
85 | | */ |
86 | | PRInt32 (*output_fn)(void *output_arg, const char *buf, PRInt32 size); |
87 | | void *output_arg; |
88 | | |
89 | | /* |
90 | | * Where the encoded output goes -- either temporarily (in the streaming |
91 | | * case, staged here before it goes to the output function) or what will |
92 | | * be the entire buffered result for users of the buffer version. |
93 | | */ |
94 | | char *output_buffer; |
95 | | PRUint32 output_buflen; /* the total length of allocated buffer */ |
96 | | PRUint32 output_length; /* the length that is currently populated */ |
97 | | }; |
98 | | |
99 | | PR_END_EXTERN_C |
100 | | |
101 | | /* |
102 | | * Table to convert a binary value to its corresponding ascii "code". |
103 | | */ |
104 | | static unsigned char base64_valuetocode[64] = |
105 | | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
106 | | |
107 | 0 | #define B64_PAD '=' |
108 | 0 | #define B64_CR '\r' |
109 | 0 | #define B64_LF '\n' |
110 | | |
111 | | static PRStatus |
112 | | pl_base64_encode_buffer(PLBase64Encoder *data, const unsigned char *in, |
113 | | PRUint32 size) |
114 | 0 | { |
115 | 0 | const unsigned char *end = in + size; |
116 | 0 | char *out = data->output_buffer + data->output_length; |
117 | 0 | unsigned int i = data->in_buffer_count; |
118 | 0 | PRUint32 n = 0; |
119 | 0 | int off; |
120 | 0 | PRUint32 output_threshold; |
121 | | |
122 | | /* If this input buffer is too small, wait until next time. */ |
123 | 0 | if (size < (3 - i)) { |
124 | 0 | data->in_buffer[i++] = in[0]; |
125 | 0 | if (size > 1) |
126 | 0 | data->in_buffer[i++] = in[1]; |
127 | 0 | PR_ASSERT(i < 3); |
128 | 0 | data->in_buffer_count = i; |
129 | 0 | return PR_SUCCESS; |
130 | 0 | } |
131 | | |
132 | | /* If there are bytes that were put back last time, take them now. */ |
133 | 0 | if (i > 0) { |
134 | 0 | n = data->in_buffer[0]; |
135 | 0 | if (i > 1) |
136 | 0 | n = (n << 8) | data->in_buffer[1]; |
137 | 0 | data->in_buffer_count = 0; |
138 | 0 | } |
139 | | |
140 | | /* If our total is not a multiple of three, put one or two bytes back. */ |
141 | 0 | off = (size + i) % 3; |
142 | 0 | if (off > 0) { |
143 | 0 | size -= off; |
144 | 0 | data->in_buffer[0] = in[size]; |
145 | 0 | if (off > 1) |
146 | 0 | data->in_buffer[1] = in[size + 1]; |
147 | 0 | data->in_buffer_count = off; |
148 | 0 | end -= off; |
149 | 0 | } |
150 | |
|
151 | 0 | output_threshold = data->output_buflen - 3; |
152 | | |
153 | | /* |
154 | | * Populate the output buffer with base64 data, one line (or buffer) |
155 | | * at a time. |
156 | | */ |
157 | 0 | while (in < end) { |
158 | 0 | int j, k; |
159 | |
|
160 | 0 | while (i < 3) { |
161 | 0 | n = (n << 8) | *in++; |
162 | 0 | i++; |
163 | 0 | } |
164 | 0 | i = 0; |
165 | |
|
166 | 0 | if (data->line_length > 0) { |
167 | 0 | if (data->current_column >= data->line_length) { |
168 | 0 | data->current_column = 0; |
169 | 0 | *out++ = B64_CR; |
170 | 0 | *out++ = B64_LF; |
171 | 0 | data->output_length += 2; |
172 | 0 | } |
173 | 0 | data->current_column += 4; /* the bytes we are about to add */ |
174 | 0 | } |
175 | |
|
176 | 0 | for (j = 18; j >= 0; j -= 6) { |
177 | 0 | k = (n >> j) & 0x3F; |
178 | 0 | *out++ = base64_valuetocode[k]; |
179 | 0 | } |
180 | 0 | n = 0; |
181 | 0 | data->output_length += 4; |
182 | |
|
183 | 0 | if (data->output_length >= output_threshold) { |
184 | 0 | PR_ASSERT(data->output_length <= data->output_buflen); |
185 | 0 | if (data->output_fn != NULL) { |
186 | 0 | PRInt32 output_result; |
187 | |
|
188 | 0 | output_result = data->output_fn(data->output_arg, |
189 | 0 | data->output_buffer, |
190 | 0 | (PRInt32)data->output_length); |
191 | 0 | if (output_result < 0) |
192 | 0 | return PR_FAILURE; |
193 | | |
194 | 0 | out = data->output_buffer; |
195 | 0 | data->output_length = 0; |
196 | 0 | } else { |
197 | | /* |
198 | | * Check that we are about to exit the loop. (Since we |
199 | | * are over the threshold, there isn't enough room in the |
200 | | * output buffer for another trip around.) |
201 | | */ |
202 | 0 | PR_ASSERT(in == end); |
203 | 0 | if (in < end) { |
204 | 0 | PR_SetError(PR_BUFFER_OVERFLOW_ERROR, 0); |
205 | 0 | return PR_FAILURE; |
206 | 0 | } |
207 | 0 | } |
208 | 0 | } |
209 | 0 | } |
210 | | |
211 | 0 | return PR_SUCCESS; |
212 | 0 | } |
213 | | |
214 | | static PRStatus |
215 | | pl_base64_encode_flush(PLBase64Encoder *data) |
216 | 0 | { |
217 | 0 | int i = data->in_buffer_count; |
218 | |
|
219 | 0 | if (i == 0 && data->output_length == 0) |
220 | 0 | return PR_SUCCESS; |
221 | | |
222 | 0 | if (i > 0) { |
223 | 0 | char *out = data->output_buffer + data->output_length; |
224 | 0 | PRUint32 n; |
225 | 0 | int j, k; |
226 | |
|
227 | 0 | n = ((PRUint32)data->in_buffer[0]) << 16; |
228 | 0 | if (i > 1) |
229 | 0 | n |= ((PRUint32)data->in_buffer[1] << 8); |
230 | |
|
231 | 0 | data->in_buffer_count = 0; |
232 | |
|
233 | 0 | if (data->line_length > 0) { |
234 | 0 | if (data->current_column >= data->line_length) { |
235 | 0 | data->current_column = 0; |
236 | 0 | *out++ = B64_CR; |
237 | 0 | *out++ = B64_LF; |
238 | 0 | data->output_length += 2; |
239 | 0 | } |
240 | 0 | } |
241 | | |
242 | | /* |
243 | | * This will fill in more than we really have data for, but the |
244 | | * valid parts will end up in the correct position and the extras |
245 | | * will be over-written with pad characters below. |
246 | | */ |
247 | 0 | for (j = 18; j >= 0; j -= 6) { |
248 | 0 | k = (n >> j) & 0x3F; |
249 | 0 | *out++ = base64_valuetocode[k]; |
250 | 0 | } |
251 | | |
252 | | /* Pad with equal-signs. */ |
253 | 0 | if (i == 1) |
254 | 0 | out[-2] = B64_PAD; |
255 | 0 | out[-1] = B64_PAD; |
256 | |
|
257 | 0 | data->output_length += 4; |
258 | 0 | } |
259 | |
|
260 | 0 | if (data->output_fn != NULL) { |
261 | 0 | PRInt32 output_result; |
262 | |
|
263 | 0 | output_result = data->output_fn(data->output_arg, data->output_buffer, |
264 | 0 | (PRInt32)data->output_length); |
265 | 0 | data->output_length = 0; |
266 | |
|
267 | 0 | if (output_result < 0) |
268 | 0 | return PR_FAILURE; |
269 | 0 | } |
270 | | |
271 | 0 | return PR_SUCCESS; |
272 | 0 | } |
273 | | |
274 | | /* |
275 | | * The maximum space needed to hold the output of the encoder given input |
276 | | * data of length "size", and allowing for CRLF added at least every |
277 | | * line_length bytes (we will add it at nearest lower multiple of 4). |
278 | | * There is no trailing CRLF. |
279 | | */ |
280 | | static PRUint32 |
281 | | PL_Base64MaxEncodedLength(PRUint32 size, PRUint32 line_length) |
282 | 0 | { |
283 | 0 | PRUint32 tokens, tokens_per_line, full_lines, line_break_chars, remainder; |
284 | | |
285 | | /* This is the maximum length we support. */ |
286 | 0 | if (size > 0x3fffffff) { |
287 | 0 | return 0; |
288 | 0 | } |
289 | | |
290 | 0 | tokens = (size + 2) / 3; |
291 | |
|
292 | 0 | if (line_length == 0) { |
293 | 0 | return tokens * 4; |
294 | 0 | } |
295 | | |
296 | 0 | if (line_length < 4) { /* too small! */ |
297 | 0 | line_length = 4; |
298 | 0 | } |
299 | |
|
300 | 0 | tokens_per_line = line_length / 4; |
301 | 0 | full_lines = tokens / tokens_per_line; |
302 | 0 | remainder = (tokens - (full_lines * tokens_per_line)) * 4; |
303 | 0 | line_break_chars = full_lines * 2; |
304 | 0 | if (remainder == 0) { |
305 | 0 | line_break_chars -= 2; |
306 | 0 | } |
307 | |
|
308 | 0 | return (full_lines * tokens_per_line * 4) + line_break_chars + remainder; |
309 | 0 | } |
310 | | |
311 | | /* |
312 | | * A distinct internal creation function for the buffer version to use. |
313 | | * (It does not want to specify an output_fn, and we want the normal |
314 | | * Create function to require that.) All common initialization of the |
315 | | * encoding context should be done *here*. |
316 | | * |
317 | | * Save "line_length", rounded down to nearest multiple of 4 (if not |
318 | | * already even multiple). Allocate output_buffer, if not provided -- |
319 | | * based on given size if specified, otherwise based on line_length. |
320 | | */ |
321 | | static PLBase64Encoder * |
322 | | pl_base64_create_encoder(PRUint32 line_length, char *output_buffer, |
323 | | PRUint32 output_buflen) |
324 | 0 | { |
325 | 0 | PLBase64Encoder *data; |
326 | 0 | PRUint32 line_tokens; |
327 | |
|
328 | 0 | data = PR_NEWZAP(PLBase64Encoder); |
329 | 0 | if (data == NULL) |
330 | 0 | return NULL; |
331 | | |
332 | 0 | if (line_length > 0 && line_length < 4) /* too small! */ |
333 | 0 | line_length = 4; |
334 | |
|
335 | 0 | line_tokens = line_length / 4; |
336 | 0 | data->line_length = line_tokens * 4; |
337 | |
|
338 | 0 | if (output_buffer == NULL) { |
339 | 0 | if (output_buflen == 0) { |
340 | 0 | if (data->line_length > 0) /* need to include room for CRLF */ |
341 | 0 | output_buflen = data->line_length + 2; |
342 | 0 | else |
343 | 0 | output_buflen = 64; /* XXX what is a good size? */ |
344 | 0 | } |
345 | |
|
346 | 0 | output_buffer = (char *)PR_Malloc(output_buflen); |
347 | 0 | if (output_buffer == NULL) { |
348 | 0 | PR_Free(data); |
349 | 0 | return NULL; |
350 | 0 | } |
351 | 0 | } |
352 | | |
353 | 0 | data->output_buffer = output_buffer; |
354 | 0 | data->output_buflen = output_buflen; |
355 | 0 | return data; |
356 | 0 | } |
357 | | |
358 | | /* |
359 | | * Function to start a base64 encoding context. |
360 | | * An "output_fn" is required; the "output_arg" parameter to that is optional. |
361 | | * If linebreaks in the encoded output are desired, "line_length" specifies |
362 | | * where to place them -- it will be rounded down to the nearest multiple of 4 |
363 | | * (if it is not already an even multiple of 4). If it is zero, no linebreaks |
364 | | * will be added. (FYI, a linebreak is CRLF -- two characters.) |
365 | | */ |
366 | | static PLBase64Encoder * |
367 | | PL_CreateBase64Encoder(PRInt32 (*output_fn)(void *, const char *, PRInt32), |
368 | | void *output_arg, PRUint32 line_length) |
369 | 0 | { |
370 | 0 | PLBase64Encoder *data; |
371 | |
|
372 | 0 | if (output_fn == NULL) { |
373 | 0 | PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); |
374 | 0 | return NULL; |
375 | 0 | } |
376 | | |
377 | 0 | data = pl_base64_create_encoder(line_length, NULL, 0); |
378 | 0 | if (data == NULL) |
379 | 0 | return NULL; |
380 | | |
381 | 0 | data->output_fn = output_fn; |
382 | 0 | data->output_arg = output_arg; |
383 | |
|
384 | 0 | return data; |
385 | 0 | } |
386 | | |
387 | | /* |
388 | | * Push data through the encoder, causing the output_fn (provided to Create) |
389 | | * to be called with the encoded data. |
390 | | */ |
391 | | static PRStatus |
392 | | PL_UpdateBase64Encoder(PLBase64Encoder *data, const unsigned char *buffer, |
393 | | PRUint32 size) |
394 | 0 | { |
395 | | /* XXX Should we do argument checking only in debug build? */ |
396 | 0 | if (data == NULL || buffer == NULL || size == 0) { |
397 | 0 | PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); |
398 | 0 | return PR_FAILURE; |
399 | 0 | } |
400 | | |
401 | 0 | return pl_base64_encode_buffer(data, buffer, size); |
402 | 0 | } |
403 | | |
404 | | /* |
405 | | * When you're done encoding, call this to free the data. If "abort_p" |
406 | | * is false, then calling this may cause the output_fn to be called |
407 | | * one last time (as the last buffered data is flushed out). |
408 | | */ |
409 | | static PRStatus |
410 | | PL_DestroyBase64Encoder(PLBase64Encoder *data, PRBool abort_p) |
411 | 0 | { |
412 | 0 | PRStatus status = PR_SUCCESS; |
413 | | |
414 | | /* XXX Should we do argument checking only in debug build? */ |
415 | 0 | if (data == NULL) { |
416 | 0 | PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); |
417 | 0 | return PR_FAILURE; |
418 | 0 | } |
419 | | |
420 | | /* Flush out the last few buffered characters. */ |
421 | 0 | if (!abort_p) |
422 | 0 | status = pl_base64_encode_flush(data); |
423 | |
|
424 | 0 | if (data->output_buffer != NULL) |
425 | 0 | PR_Free(data->output_buffer); |
426 | 0 | PR_Free(data); |
427 | |
|
428 | 0 | return status; |
429 | 0 | } |
430 | | |
431 | | /* |
432 | | * Perform base64 encoding from an input buffer to an output buffer. |
433 | | * The output buffer can be provided (as "dest"); you can also pass in |
434 | | * a NULL and this function will allocate a buffer large enough for you, |
435 | | * and return it. If you do provide the output buffer, you must also |
436 | | * provide the maximum length of that buffer (as "maxdestlen"). |
437 | | * The actual encoded length of output will be returned to you in |
438 | | * "output_destlen". |
439 | | * |
440 | | * If linebreaks in the encoded output are desired, "line_length" specifies |
441 | | * where to place them -- it will be rounded down to the nearest multiple of 4 |
442 | | * (if it is not already an even multiple of 4). If it is zero, no linebreaks |
443 | | * will be added. (FYI, a linebreak is CRLF -- two characters.) |
444 | | * |
445 | | * Return value is NULL on error, the output buffer (allocated or provided) |
446 | | * otherwise. |
447 | | */ |
448 | | static char * |
449 | | PL_Base64EncodeBuffer(const unsigned char *src, PRUint32 srclen, |
450 | | PRUint32 line_length, char *dest, PRUint32 maxdestlen, |
451 | | PRUint32 *output_destlen) |
452 | 0 | { |
453 | 0 | PRUint32 need_length; |
454 | 0 | PLBase64Encoder *data = NULL; |
455 | 0 | PRStatus status; |
456 | |
|
457 | 0 | PR_ASSERT(srclen > 0); |
458 | 0 | if (srclen == 0) { |
459 | 0 | return dest; |
460 | 0 | } |
461 | | |
462 | | /* |
463 | | * How much space could we possibly need for encoding this input? |
464 | | */ |
465 | 0 | need_length = PL_Base64MaxEncodedLength(srclen, line_length); |
466 | 0 | if (need_length == 0) { |
467 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
468 | 0 | return NULL; |
469 | 0 | } |
470 | | |
471 | | /* |
472 | | * Make sure we have at least that much, if output buffer provided. |
473 | | */ |
474 | 0 | if (dest != NULL) { |
475 | 0 | PR_ASSERT(maxdestlen >= need_length); |
476 | 0 | if (maxdestlen < need_length) { |
477 | 0 | PR_SetError(PR_BUFFER_OVERFLOW_ERROR, 0); |
478 | 0 | return NULL; |
479 | 0 | } |
480 | 0 | } else { |
481 | 0 | maxdestlen = need_length; |
482 | 0 | } |
483 | | |
484 | 0 | data = pl_base64_create_encoder(line_length, dest, maxdestlen); |
485 | 0 | if (data == NULL) |
486 | 0 | return NULL; |
487 | | |
488 | 0 | status = pl_base64_encode_buffer(data, src, srclen); |
489 | | |
490 | | /* |
491 | | * We do not wait for Destroy to flush, because Destroy will also |
492 | | * get rid of our encoder context, which we need to look at first! |
493 | | */ |
494 | 0 | if (status == PR_SUCCESS) |
495 | 0 | status = pl_base64_encode_flush(data); |
496 | |
|
497 | 0 | if (status != PR_SUCCESS) { |
498 | 0 | (void)PL_DestroyBase64Encoder(data, PR_TRUE); |
499 | 0 | return NULL; |
500 | 0 | } |
501 | | |
502 | 0 | dest = data->output_buffer; |
503 | | |
504 | | /* Must clear this or Destroy will free it. */ |
505 | 0 | data->output_buffer = NULL; |
506 | |
|
507 | 0 | *output_destlen = data->output_length; |
508 | 0 | status = PL_DestroyBase64Encoder(data, PR_FALSE); |
509 | 0 | if (status == PR_FAILURE) { |
510 | 0 | PR_Free(dest); |
511 | 0 | return NULL; |
512 | 0 | } |
513 | | |
514 | 0 | return dest; |
515 | 0 | } |
516 | | |
517 | | /* |
518 | | * XXX End of base64 encoding code to be moved into NSPR. |
519 | | ******************************************************** |
520 | | */ |
521 | | |
522 | | /* |
523 | | * This is the beginning of the NSS cover functions. These will |
524 | | * provide the interface we want to expose as NSS-ish. For example, |
525 | | * they will operate on our Items, do any special handling or checking |
526 | | * we want to do, etc. |
527 | | */ |
528 | | |
529 | | PR_BEGIN_EXTERN_C |
530 | | |
531 | | /* |
532 | | * A boring cover structure for now. Perhaps someday it will include |
533 | | * some more interesting fields. |
534 | | */ |
535 | | struct NSSBase64EncoderStr { |
536 | | PLBase64Encoder *pl_data; |
537 | | }; |
538 | | |
539 | | PR_END_EXTERN_C |
540 | | |
541 | | /* |
542 | | * Function to start a base64 encoding context. |
543 | | */ |
544 | | NSSBase64Encoder * |
545 | | NSSBase64Encoder_Create(PRInt32 (*output_fn)(void *, const char *, PRInt32), |
546 | | void *output_arg) |
547 | 0 | { |
548 | 0 | PLBase64Encoder *pl_data; |
549 | 0 | NSSBase64Encoder *nss_data; |
550 | |
|
551 | 0 | nss_data = PORT_ZNew(NSSBase64Encoder); |
552 | 0 | if (nss_data == NULL) |
553 | 0 | return NULL; |
554 | | |
555 | 0 | pl_data = PL_CreateBase64Encoder(output_fn, output_arg, 64); |
556 | 0 | if (pl_data == NULL) { |
557 | 0 | PORT_Free(nss_data); |
558 | 0 | return NULL; |
559 | 0 | } |
560 | | |
561 | 0 | nss_data->pl_data = pl_data; |
562 | 0 | return nss_data; |
563 | 0 | } |
564 | | |
565 | | /* |
566 | | * Push data through the encoder, causing the output_fn (provided to Create) |
567 | | * to be called with the encoded data. |
568 | | */ |
569 | | SECStatus |
570 | | NSSBase64Encoder_Update(NSSBase64Encoder *data, const unsigned char *buffer, |
571 | | PRUint32 size) |
572 | 0 | { |
573 | 0 | PRStatus pr_status; |
574 | | |
575 | | /* XXX Should we do argument checking only in debug build? */ |
576 | 0 | if (data == NULL) { |
577 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
578 | 0 | return SECFailure; |
579 | 0 | } |
580 | | |
581 | 0 | pr_status = PL_UpdateBase64Encoder(data->pl_data, buffer, size); |
582 | 0 | if (pr_status == PR_FAILURE) |
583 | 0 | return SECFailure; |
584 | | |
585 | 0 | return SECSuccess; |
586 | 0 | } |
587 | | |
588 | | /* |
589 | | * When you're done encoding, call this to free the data. If "abort_p" |
590 | | * is false, then calling this may cause the output_fn to be called |
591 | | * one last time (as the last buffered data is flushed out). |
592 | | */ |
593 | | SECStatus |
594 | | NSSBase64Encoder_Destroy(NSSBase64Encoder *data, PRBool abort_p) |
595 | 0 | { |
596 | 0 | PRStatus pr_status; |
597 | | |
598 | | /* XXX Should we do argument checking only in debug build? */ |
599 | 0 | if (data == NULL) { |
600 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
601 | 0 | return SECFailure; |
602 | 0 | } |
603 | | |
604 | 0 | pr_status = PL_DestroyBase64Encoder(data->pl_data, abort_p); |
605 | |
|
606 | 0 | PORT_Free(data); |
607 | |
|
608 | 0 | if (pr_status == PR_FAILURE) |
609 | 0 | return SECFailure; |
610 | | |
611 | 0 | return SECSuccess; |
612 | 0 | } |
613 | | |
614 | | /* |
615 | | * Perform base64 encoding of binary data "inItem" to an ascii string. |
616 | | * The output buffer may be provided (as "outStrOpt"); you can also pass |
617 | | * in a NULL and the buffer will be allocated for you. The result will |
618 | | * be null-terminated, and if the buffer is provided, "maxOutLen" must |
619 | | * specify the maximum length of the buffer and will be checked to |
620 | | * supply sufficient space space for the encoded result. (If "outStrOpt" |
621 | | * is NULL, "maxOutLen" is ignored.) |
622 | | * |
623 | | * If "outStrOpt" is NULL, allocation will happen out of the passed-in |
624 | | * "arenaOpt", if *it* is non-NULL, otherwise standard allocation (heap) |
625 | | * will be used. |
626 | | * |
627 | | * Return value is NULL on error, the output buffer (allocated or provided) |
628 | | * otherwise. |
629 | | */ |
630 | | char * |
631 | | NSSBase64_EncodeItem(PLArenaPool *arenaOpt, char *outStrOpt, |
632 | | unsigned int maxOutLen, SECItem *inItem) |
633 | 0 | { |
634 | 0 | char *out_string = outStrOpt; |
635 | 0 | PRUint32 max_out_len; |
636 | 0 | PRUint32 out_len = 0; |
637 | 0 | void *mark = NULL; |
638 | 0 | char *dummy; |
639 | |
|
640 | 0 | PORT_Assert(inItem != NULL && inItem->data != NULL && inItem->len != 0); |
641 | 0 | if (inItem == NULL || inItem->data == NULL || inItem->len == 0) { |
642 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
643 | 0 | return NULL; |
644 | 0 | } |
645 | | |
646 | 0 | max_out_len = PL_Base64MaxEncodedLength(inItem->len, 64); |
647 | 0 | if (max_out_len == 0) { |
648 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
649 | 0 | return NULL; |
650 | 0 | } |
651 | | |
652 | 0 | if (arenaOpt != NULL) |
653 | 0 | mark = PORT_ArenaMark(arenaOpt); |
654 | |
|
655 | 0 | if (out_string == NULL) { |
656 | 0 | if (arenaOpt != NULL) |
657 | 0 | out_string = PORT_ArenaAlloc(arenaOpt, max_out_len + 1); |
658 | 0 | else |
659 | 0 | out_string = PORT_Alloc(max_out_len + 1); |
660 | |
|
661 | 0 | if (out_string == NULL) { |
662 | 0 | if (arenaOpt != NULL) |
663 | 0 | PORT_ArenaRelease(arenaOpt, mark); |
664 | 0 | return NULL; |
665 | 0 | } |
666 | 0 | } else { |
667 | 0 | if ((max_out_len + 1) > maxOutLen) { |
668 | 0 | PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
669 | 0 | return NULL; |
670 | 0 | } |
671 | 0 | max_out_len = maxOutLen; |
672 | 0 | } |
673 | | |
674 | 0 | dummy = PL_Base64EncodeBuffer(inItem->data, inItem->len, 64, |
675 | 0 | out_string, max_out_len, &out_len); |
676 | 0 | if (dummy == NULL) { |
677 | 0 | if (arenaOpt != NULL) { |
678 | 0 | PORT_ArenaRelease(arenaOpt, mark); |
679 | 0 | } else { |
680 | 0 | PORT_Free(out_string); |
681 | 0 | } |
682 | 0 | return NULL; |
683 | 0 | } |
684 | | |
685 | 0 | if (arenaOpt != NULL) |
686 | 0 | PORT_ArenaUnmark(arenaOpt, mark); |
687 | |
|
688 | 0 | out_string[out_len] = '\0'; |
689 | 0 | return out_string; |
690 | 0 | } |
691 | | |
692 | | /* |
693 | | * XXX Everything below is deprecated. If you add new stuff, put it |
694 | | * *above*, not below. |
695 | | */ |
696 | | |
697 | | /* |
698 | | * XXX The following "BTOA" functions are provided for backward compatibility |
699 | | * with current code. They should be considered strongly deprecated. |
700 | | * When we can convert all our code over to using the new NSSBase64Encoder_ |
701 | | * functions defined above, we should get rid of these altogether. (Remove |
702 | | * protoypes from base64.h as well -- actually, remove that file completely). |
703 | | * If someone thinks either of these functions provides such a very useful |
704 | | * interface (though, as shown, the same functionality can already be |
705 | | * obtained by calling NSSBase64_EncodeItem directly), fine -- but then |
706 | | * that API should be provided with a nice new NSSFoo name and using |
707 | | * appropriate types, etc. |
708 | | */ |
709 | | |
710 | | #include "base64.h" |
711 | | |
712 | | /* |
713 | | ** Return an PORT_Alloc'd ascii string which is the base64 encoded |
714 | | ** version of the input string. |
715 | | */ |
716 | | char * |
717 | | BTOA_DataToAscii(const unsigned char *data, unsigned int len) |
718 | 0 | { |
719 | 0 | SECItem binary_item; |
720 | |
|
721 | 0 | binary_item.data = (unsigned char *)data; |
722 | 0 | binary_item.len = len; |
723 | |
|
724 | 0 | return NSSBase64_EncodeItem(NULL, NULL, 0, &binary_item); |
725 | 0 | } |
726 | | |
727 | | /* |
728 | | ** Convert from binary encoding of an item to ascii. |
729 | | */ |
730 | | char * |
731 | | BTOA_ConvertItemToAscii(SECItem *binary_item) |
732 | 0 | { |
733 | 0 | return NSSBase64_EncodeItem(NULL, NULL, 0, binary_item); |
734 | 0 | } |