andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: * Base64 decoding (ascii to binary). andre@0: */ andre@0: andre@0: #include "nssb64.h" andre@0: #include "nspr.h" andre@0: #include "secitem.h" andre@0: #include "secerr.h" andre@0: andre@0: /* andre@0: * XXX We want this basic support to go into NSPR (the PL part). andre@0: * Until that can happen, the PL interface is going to be kept entirely andre@0: * internal here -- all static functions and opaque data structures. andre@0: * When someone can get it moved over into NSPR, that should be done: andre@0: * - giving everything names that are accepted by the NSPR module owners andre@0: * (though I tried to choose ones that would work without modification) andre@0: * - exporting the functions (remove static declarations and add andre@0: * to nssutil.def as necessary) andre@0: * - put prototypes into appropriate header file (probably replacing andre@0: * the entire current lib/libc/include/plbase64.h in NSPR) andre@0: * along with a typedef for the context structure (which should be andre@0: * kept opaque -- definition in the source file only, but typedef andre@0: * ala "typedef struct PLBase64FooStr PLBase64Foo;" in header file) andre@0: * - modify anything else as necessary to conform to NSPR required style andre@0: * (I looked but found no formatting guide to follow) andre@0: * andre@0: * You will want to move over everything from here down to the comment andre@0: * which says "XXX End of base64 decoding code to be moved into NSPR", andre@0: * into a new file in NSPR. andre@0: */ andre@0: andre@0: /* andre@0: ************************************************************** andre@0: * XXX Beginning of base64 decoding code to be moved into NSPR. andre@0: */ andre@0: andre@0: /* andre@0: * This typedef would belong in the NSPR header file (i.e. plbase64.h). andre@0: */ andre@0: typedef struct PLBase64DecoderStr PLBase64Decoder; andre@0: andre@0: /* andre@0: * The following implementation of base64 decoding was based on code andre@0: * found in libmime (specifically, in mimeenc.c). It has been adapted to andre@0: * use PR types and naming as well as to provide other necessary semantics andre@0: * (like buffer-in/buffer-out in addition to "streaming" without undue andre@0: * performance hit of extra copying if you made the buffer versions andre@0: * use the output_fn). It also incorporates some aspects of the current andre@0: * NSPR base64 decoding code. As such, you may find similarities to andre@0: * both of those implementations. I tried to use names that reflected andre@0: * the original code when possible. For this reason you may find some andre@0: * inconsistencies -- libmime used lots of "in" and "out" whereas the andre@0: * NSPR version uses "src" and "dest"; sometimes I changed one to the other andre@0: * and sometimes I left them when I thought the subroutines were at least andre@0: * self-consistent. andre@0: */ andre@0: andre@0: PR_BEGIN_EXTERN_C andre@0: andre@0: /* andre@0: * Opaque object used by the decoder to store state. andre@0: */ andre@0: struct PLBase64DecoderStr { andre@0: /* Current token (or portion, if token_size < 4) being decoded. */ andre@0: unsigned char token[4]; andre@0: int token_size; andre@0: andre@0: /* andre@0: * Where to write the decoded data (used when streaming, not when andre@0: * doing all in-memory (buffer) operations). andre@0: * andre@0: * Note that this definition is chosen to be compatible with PR_Write. andre@0: */ andre@0: PRInt32 (*output_fn) (void *output_arg, const unsigned char *buf, andre@0: PRInt32 size); andre@0: void *output_arg; andre@0: andre@0: /* andre@0: * Where the decoded output goes -- either temporarily (in the streaming andre@0: * case, staged here before it goes to the output function) or what will andre@0: * be the entire buffered result for users of the buffer version. andre@0: */ andre@0: unsigned char *output_buffer; andre@0: PRUint32 output_buflen; /* the total length of allocated buffer */ andre@0: PRUint32 output_length; /* the length that is currently populated */ andre@0: }; andre@0: andre@0: PR_END_EXTERN_C andre@0: andre@0: andre@0: /* andre@0: * Table to convert an ascii "code" to its corresponding binary value. andre@0: * For ease of use, the binary values in the table are the actual values andre@0: * PLUS ONE. This is so that the special value of zero can denote an andre@0: * invalid mapping; that was much easier than trying to fill in the other andre@0: * values with some value other than zero, and to check for it. andre@0: * Just remember to SUBTRACT ONE when using the value retrieved. andre@0: */ andre@0: static unsigned char base64_codetovaluep1[256] = { andre@0: /* 0: */ 0, 0, 0, 0, 0, 0, 0, 0, andre@0: /* 8: */ 0, 0, 0, 0, 0, 0, 0, 0, andre@0: /* 16: */ 0, 0, 0, 0, 0, 0, 0, 0, andre@0: /* 24: */ 0, 0, 0, 0, 0, 0, 0, 0, andre@0: /* 32: */ 0, 0, 0, 0, 0, 0, 0, 0, andre@0: /* 40: */ 0, 0, 0, 63, 0, 0, 0, 64, andre@0: /* 48: */ 53, 54, 55, 56, 57, 58, 59, 60, andre@0: /* 56: */ 61, 62, 0, 0, 0, 0, 0, 0, andre@0: /* 64: */ 0, 1, 2, 3, 4, 5, 6, 7, andre@0: /* 72: */ 8, 9, 10, 11, 12, 13, 14, 15, andre@0: /* 80: */ 16, 17, 18, 19, 20, 21, 22, 23, andre@0: /* 88: */ 24, 25, 26, 0, 0, 0, 0, 0, andre@0: /* 96: */ 0, 27, 28, 29, 30, 31, 32, 33, andre@0: /* 104: */ 34, 35, 36, 37, 38, 39, 40, 41, andre@0: /* 112: */ 42, 43, 44, 45, 46, 47, 48, 49, andre@0: /* 120: */ 50, 51, 52, 0, 0, 0, 0, 0, andre@0: /* 128: */ 0, 0, 0, 0, 0, 0, 0, 0 andre@0: /* and rest are all zero as well */ andre@0: }; andre@0: andre@0: #define B64_PAD '=' andre@0: andre@0: andre@0: /* andre@0: * Reads 4; writes 3 (known, or expected, to have no trailing padding). andre@0: * Returns bytes written; -1 on error (unexpected character). andre@0: */ andre@0: static int andre@0: pl_base64_decode_4to3 (const unsigned char *in, unsigned char *out) andre@0: { andre@0: int j; andre@0: PRUint32 num = 0; andre@0: unsigned char bits; andre@0: andre@0: for (j = 0; j < 4; j++) { andre@0: bits = base64_codetovaluep1[in[j]]; andre@0: if (bits == 0) andre@0: return -1; andre@0: num = (num << 6) | (bits - 1); andre@0: } andre@0: andre@0: out[0] = (unsigned char) (num >> 16); andre@0: out[1] = (unsigned char) ((num >> 8) & 0xFF); andre@0: out[2] = (unsigned char) (num & 0xFF); andre@0: andre@0: return 3; andre@0: } andre@0: andre@0: /* andre@0: * Reads 3; writes 2 (caller already confirmed EOF or trailing padding). andre@0: * Returns bytes written; -1 on error (unexpected character). andre@0: */ andre@0: static int andre@0: pl_base64_decode_3to2 (const unsigned char *in, unsigned char *out) andre@0: { andre@0: PRUint32 num = 0; andre@0: unsigned char bits1, bits2, bits3; andre@0: andre@0: bits1 = base64_codetovaluep1[in[0]]; andre@0: bits2 = base64_codetovaluep1[in[1]]; andre@0: bits3 = base64_codetovaluep1[in[2]]; andre@0: andre@0: if ((bits1 == 0) || (bits2 == 0) || (bits3 == 0)) andre@0: return -1; andre@0: andre@0: num = ((PRUint32)(bits1 - 1)) << 10; andre@0: num |= ((PRUint32)(bits2 - 1)) << 4; andre@0: num |= ((PRUint32)(bits3 - 1)) >> 2; andre@0: andre@0: out[0] = (unsigned char) (num >> 8); andre@0: out[1] = (unsigned char) (num & 0xFF); andre@0: andre@0: return 2; andre@0: } andre@0: andre@0: /* andre@0: * Reads 2; writes 1 (caller already confirmed EOF or trailing padding). andre@0: * Returns bytes written; -1 on error (unexpected character). andre@0: */ andre@0: static int andre@0: pl_base64_decode_2to1 (const unsigned char *in, unsigned char *out) andre@0: { andre@0: PRUint32 num = 0; andre@0: unsigned char bits1, bits2; andre@0: andre@0: bits1 = base64_codetovaluep1[in[0]]; andre@0: bits2 = base64_codetovaluep1[in[1]]; andre@0: andre@0: if ((bits1 == 0) || (bits2 == 0)) andre@0: return -1; andre@0: andre@0: num = ((PRUint32)(bits1 - 1)) << 2; andre@0: num |= ((PRUint32)(bits2 - 1)) >> 4; andre@0: andre@0: out[0] = (unsigned char) num; andre@0: andre@0: return 1; andre@0: } andre@0: andre@0: /* andre@0: * Reads 4; writes 0-3. Returns bytes written or -1 on error. andre@0: * (Writes less than 3 only at (presumed) EOF.) andre@0: */ andre@0: static int andre@0: pl_base64_decode_token (const unsigned char *in, unsigned char *out) andre@0: { andre@0: if (in[3] != B64_PAD) andre@0: return pl_base64_decode_4to3 (in, out); andre@0: andre@0: if (in[2] == B64_PAD) andre@0: return pl_base64_decode_2to1 (in, out); andre@0: andre@0: return pl_base64_decode_3to2 (in, out); andre@0: } andre@0: andre@0: static PRStatus andre@0: pl_base64_decode_buffer (PLBase64Decoder *data, const unsigned char *in, andre@0: PRUint32 length) andre@0: { andre@0: unsigned char *out = data->output_buffer; andre@0: unsigned char *token = data->token; andre@0: int i, n = 0; andre@0: andre@0: i = data->token_size; andre@0: data->token_size = 0; andre@0: andre@0: while (length > 0) { andre@0: while (i < 4 && length > 0) { andre@0: /* andre@0: * XXX Note that the following simply ignores any unexpected andre@0: * characters. This is exactly what the original code in andre@0: * libmime did, and I am leaving it. We certainly want to skip andre@0: * over whitespace (we must); this does much more than that. andre@0: * I am not confident changing it, and I don't want to slow andre@0: * the processing down doing more complicated checking, but andre@0: * someone else might have different ideas in the future. andre@0: */ andre@0: if (base64_codetovaluep1[*in] > 0 || *in == B64_PAD) andre@0: token[i++] = *in; andre@0: in++; andre@0: length--; andre@0: } andre@0: andre@0: if (i < 4) { andre@0: /* Didn't get enough for a complete token. */ andre@0: data->token_size = i; andre@0: break; andre@0: } andre@0: i = 0; andre@0: andre@0: PR_ASSERT((out - data->output_buffer + 3) <= data->output_buflen); andre@0: andre@0: /* andre@0: * Assume we are not at the end; the following function only works andre@0: * for an internal token (no trailing padding characters) but is andre@0: * faster that way. If it hits an invalid character (padding) it andre@0: * will return an error; we break out of the loop and try again andre@0: * calling the routine that will handle a final token. andre@0: * Note that we intentionally do it this way rather than explicitly andre@0: * add a check for padding here (because that would just slow down andre@0: * the normal case) nor do we rely on checking whether we have more andre@0: * input to process (because that would also slow it down but also andre@0: * because we want to allow trailing garbage, especially white space andre@0: * and cannot tell that without read-ahead, also a slow proposition). andre@0: * Whew. Understand? andre@0: */ andre@0: n = pl_base64_decode_4to3 (token, out); andre@0: if (n < 0) andre@0: break; andre@0: andre@0: /* Advance "out" by the number of bytes just written to it. */ andre@0: out += n; andre@0: n = 0; andre@0: } andre@0: andre@0: /* andre@0: * See big comment above, before call to pl_base64_decode_4to3. andre@0: * Here we check if we error'd out of loop, and allow for the case andre@0: * that we are processing the last interesting token. If the routine andre@0: * which should handle padding characters also fails, then we just andre@0: * have bad input and give up. andre@0: */ andre@0: if (n < 0) { andre@0: n = pl_base64_decode_token (token, out); andre@0: if (n < 0) andre@0: return PR_FAILURE; andre@0: andre@0: out += n; andre@0: } andre@0: andre@0: /* andre@0: * As explained above, we can get here with more input remaining, but andre@0: * it should be all characters we do not care about (i.e. would be andre@0: * ignored when transferring from "in" to "token" in loop above, andre@0: * except here we choose to ignore extraneous pad characters, too). andre@0: * Swallow it, performing that check. If we find more characters that andre@0: * we would expect to decode, something is wrong. andre@0: */ andre@0: while (length > 0) { andre@0: if (base64_codetovaluep1[*in] > 0) andre@0: return PR_FAILURE; andre@0: in++; andre@0: length--; andre@0: } andre@0: andre@0: /* Record the length of decoded data we have left in output_buffer. */ andre@0: data->output_length = (PRUint32) (out - data->output_buffer); andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: /* andre@0: * Flush any remaining buffered characters. Given well-formed input, andre@0: * this will have nothing to do. If the input was missing the padding andre@0: * characters at the end, though, there could be 1-3 characters left andre@0: * behind -- we will tolerate that by adding the padding for them. andre@0: */ andre@0: static PRStatus andre@0: pl_base64_decode_flush (PLBase64Decoder *data) andre@0: { andre@0: int count; andre@0: andre@0: /* andre@0: * If no remaining characters, or all are padding (also not well-formed andre@0: * input, but again, be tolerant), then nothing more to do. (And, that andre@0: * is considered successful.) andre@0: */ andre@0: if (data->token_size == 0 || data->token[0] == B64_PAD) andre@0: return PR_SUCCESS; andre@0: andre@0: /* andre@0: * Assume we have all the interesting input except for some expected andre@0: * padding characters. Add them and decode the resulting token. andre@0: */ andre@0: while (data->token_size < 4) andre@0: data->token[data->token_size++] = B64_PAD; andre@0: andre@0: data->token_size = 0; /* so a subsequent flush call is a no-op */ andre@0: andre@0: count = pl_base64_decode_token (data->token, andre@0: data->output_buffer + data->output_length); andre@0: if (count < 0) andre@0: return PR_FAILURE; andre@0: andre@0: /* andre@0: * If there is an output function, call it with this last bit of data. andre@0: * Otherwise we are doing all buffered output, and the decoded bytes andre@0: * are now there, we just need to reflect that in the length. andre@0: */ andre@0: if (data->output_fn != NULL) { andre@0: PRInt32 output_result; andre@0: andre@0: PR_ASSERT(data->output_length == 0); andre@0: output_result = data->output_fn (data->output_arg, andre@0: data->output_buffer, andre@0: (PRInt32) count); andre@0: if (output_result < 0) andre@0: return PR_FAILURE; andre@0: } else { andre@0: data->output_length += count; andre@0: } andre@0: andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * The maximum space needed to hold the output of the decoder given andre@0: * input data of length "size". andre@0: */ andre@0: static PRUint32 andre@0: PL_Base64MaxDecodedLength (PRUint32 size) andre@0: { andre@0: return ((size * 3) / 4); andre@0: } andre@0: andre@0: andre@0: /* andre@0: * A distinct internal creation function for the buffer version to use. andre@0: * (It does not want to specify an output_fn, and we want the normal andre@0: * Create function to require that.) If more common initialization andre@0: * of the decoding context needs to be done, it should be done *here*. andre@0: */ andre@0: static PLBase64Decoder * andre@0: pl_base64_create_decoder (void) andre@0: { andre@0: return PR_NEWZAP(PLBase64Decoder); andre@0: } andre@0: andre@0: /* andre@0: * Function to start a base64 decoding context. andre@0: * An "output_fn" is required; the "output_arg" parameter to that is optional. andre@0: */ andre@0: static PLBase64Decoder * andre@0: PL_CreateBase64Decoder (PRInt32 (*output_fn) (void *, const unsigned char *, andre@0: PRInt32), andre@0: void *output_arg) andre@0: { andre@0: PLBase64Decoder *data; andre@0: andre@0: if (output_fn == NULL) { andre@0: PR_SetError (PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return NULL; andre@0: } andre@0: andre@0: data = pl_base64_create_decoder (); andre@0: if (data != NULL) { andre@0: data->output_fn = output_fn; andre@0: data->output_arg = output_arg; andre@0: } andre@0: return data; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * Push data through the decoder, causing the output_fn (provided to Create) andre@0: * to be called with the decoded data. andre@0: */ andre@0: static PRStatus andre@0: PL_UpdateBase64Decoder (PLBase64Decoder *data, const char *buffer, andre@0: PRUint32 size) andre@0: { andre@0: PRUint32 need_length; andre@0: PRStatus status; andre@0: andre@0: /* XXX Should we do argument checking only in debug build? */ andre@0: if (data == NULL || buffer == NULL || size == 0) { andre@0: PR_SetError (PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: /* andre@0: * How much space could this update need for decoding? andre@0: */ andre@0: need_length = PL_Base64MaxDecodedLength (size + data->token_size); andre@0: andre@0: /* andre@0: * Make sure we have at least that much. If not, (re-)allocate. andre@0: */ andre@0: if (need_length > data->output_buflen) { andre@0: unsigned char *output_buffer = data->output_buffer; andre@0: andre@0: if (output_buffer != NULL) andre@0: output_buffer = (unsigned char *) PR_Realloc(output_buffer, andre@0: need_length); andre@0: else andre@0: output_buffer = (unsigned char *) PR_Malloc(need_length); andre@0: andre@0: if (output_buffer == NULL) andre@0: return PR_FAILURE; andre@0: andre@0: data->output_buffer = output_buffer; andre@0: data->output_buflen = need_length; andre@0: } andre@0: andre@0: /* There should not have been any leftover output data in the buffer. */ andre@0: PR_ASSERT(data->output_length == 0); andre@0: data->output_length = 0; andre@0: andre@0: status = pl_base64_decode_buffer (data, (const unsigned char *) buffer, andre@0: size); andre@0: andre@0: /* Now that we have some decoded data, write it. */ andre@0: if (status == PR_SUCCESS && data->output_length > 0) { andre@0: PRInt32 output_result; andre@0: andre@0: PR_ASSERT(data->output_fn != NULL); andre@0: output_result = data->output_fn (data->output_arg, andre@0: data->output_buffer, andre@0: (PRInt32) data->output_length); andre@0: if (output_result < 0) andre@0: status = PR_FAILURE; andre@0: } andre@0: andre@0: data->output_length = 0; andre@0: return status; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * When you're done decoding, call this to free the data. If "abort_p" andre@0: * is false, then calling this may cause the output_fn to be called andre@0: * one last time (as the last buffered data is flushed out). andre@0: */ andre@0: static PRStatus andre@0: PL_DestroyBase64Decoder (PLBase64Decoder *data, PRBool abort_p) andre@0: { andre@0: PRStatus status = PR_SUCCESS; andre@0: andre@0: /* XXX Should we do argument checking only in debug build? */ andre@0: if (data == NULL) { andre@0: PR_SetError (PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: /* Flush out the last few buffered characters. */ andre@0: if (!abort_p) andre@0: status = pl_base64_decode_flush (data); andre@0: andre@0: if (data->output_buffer != NULL) andre@0: PR_Free(data->output_buffer); andre@0: PR_Free(data); andre@0: andre@0: return status; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * Perform base64 decoding from an input buffer to an output buffer. andre@0: * The output buffer can be provided (as "dest"); you can also pass in andre@0: * a NULL and this function will allocate a buffer large enough for you, andre@0: * and return it. If you do provide the output buffer, you must also andre@0: * provide the maximum length of that buffer (as "maxdestlen"). andre@0: * The actual decoded length of output will be returned to you in andre@0: * "output_destlen". andre@0: * andre@0: * Return value is NULL on error, the output buffer (allocated or provided) andre@0: * otherwise. andre@0: */ andre@0: static unsigned char * andre@0: PL_Base64DecodeBuffer (const char *src, PRUint32 srclen, unsigned char *dest, andre@0: PRUint32 maxdestlen, PRUint32 *output_destlen) andre@0: { andre@0: PRUint32 need_length; andre@0: unsigned char *output_buffer = NULL; andre@0: PLBase64Decoder *data = NULL; andre@0: PRStatus status; andre@0: andre@0: PR_ASSERT(srclen > 0); andre@0: if (srclen == 0) { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return NULL; andre@0: } andre@0: andre@0: /* andre@0: * How much space could we possibly need for decoding this input? andre@0: */ andre@0: need_length = PL_Base64MaxDecodedLength (srclen); andre@0: andre@0: /* andre@0: * Make sure we have at least that much, if output buffer provided. andre@0: * If no output buffer provided, then we allocate that much. andre@0: */ andre@0: if (dest != NULL) { andre@0: PR_ASSERT(maxdestlen >= need_length); andre@0: if (maxdestlen < need_length) { andre@0: PR_SetError(PR_BUFFER_OVERFLOW_ERROR, 0); andre@0: goto loser; andre@0: } andre@0: output_buffer = dest; andre@0: } else { andre@0: output_buffer = (unsigned char *) PR_Malloc(need_length); andre@0: if (output_buffer == NULL) andre@0: goto loser; andre@0: maxdestlen = need_length; andre@0: } andre@0: andre@0: data = pl_base64_create_decoder(); andre@0: if (data == NULL) andre@0: goto loser; andre@0: andre@0: data->output_buflen = maxdestlen; andre@0: data->output_buffer = output_buffer; andre@0: andre@0: status = pl_base64_decode_buffer (data, (const unsigned char *) src, andre@0: srclen); andre@0: andre@0: /* andre@0: * We do not wait for Destroy to flush, because Destroy will also andre@0: * get rid of our decoder context, which we need to look at first! andre@0: */ andre@0: if (status == PR_SUCCESS) andre@0: status = pl_base64_decode_flush (data); andre@0: andre@0: /* Must clear this or Destroy will free it. */ andre@0: data->output_buffer = NULL; andre@0: andre@0: if (status == PR_SUCCESS) { andre@0: *output_destlen = data->output_length; andre@0: status = PL_DestroyBase64Decoder (data, PR_FALSE); andre@0: data = NULL; andre@0: if (status == PR_FAILURE) andre@0: goto loser; andre@0: return output_buffer; andre@0: } andre@0: andre@0: loser: andre@0: if (dest == NULL && output_buffer != NULL) andre@0: PR_Free(output_buffer); andre@0: if (data != NULL) andre@0: (void) PL_DestroyBase64Decoder (data, PR_TRUE); andre@0: return NULL; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * XXX End of base64 decoding code to be moved into NSPR. andre@0: ******************************************************** andre@0: */ andre@0: andre@0: /* andre@0: * This is the beginning of the NSS cover functions. These will andre@0: * provide the interface we want to expose as NSS-ish. For example, andre@0: * they will operate on our Items, do any special handling or checking andre@0: * we want to do, etc. andre@0: */ andre@0: andre@0: andre@0: PR_BEGIN_EXTERN_C andre@0: andre@0: /* andre@0: * A boring cover structure for now. Perhaps someday it will include andre@0: * some more interesting fields. andre@0: */ andre@0: struct NSSBase64DecoderStr { andre@0: PLBase64Decoder *pl_data; andre@0: }; andre@0: andre@0: PR_END_EXTERN_C andre@0: andre@0: andre@0: /* andre@0: * Function to start a base64 decoding context. andre@0: */ andre@0: NSSBase64Decoder * andre@0: NSSBase64Decoder_Create (PRInt32 (*output_fn) (void *, const unsigned char *, andre@0: PRInt32), andre@0: void *output_arg) andre@0: { andre@0: PLBase64Decoder *pl_data; andre@0: NSSBase64Decoder *nss_data; andre@0: andre@0: nss_data = PORT_ZNew(NSSBase64Decoder); andre@0: if (nss_data == NULL) andre@0: return NULL; andre@0: andre@0: pl_data = PL_CreateBase64Decoder (output_fn, output_arg); andre@0: if (pl_data == NULL) { andre@0: PORT_Free(nss_data); andre@0: return NULL; andre@0: } andre@0: andre@0: nss_data->pl_data = pl_data; andre@0: return nss_data; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * Push data through the decoder, causing the output_fn (provided to Create) andre@0: * to be called with the decoded data. andre@0: */ andre@0: SECStatus andre@0: NSSBase64Decoder_Update (NSSBase64Decoder *data, const char *buffer, andre@0: PRUint32 size) andre@0: { andre@0: PRStatus pr_status; andre@0: andre@0: /* XXX Should we do argument checking only in debug build? */ andre@0: if (data == NULL) { andre@0: PORT_SetError (SEC_ERROR_INVALID_ARGS); andre@0: return SECFailure; andre@0: } andre@0: andre@0: pr_status = PL_UpdateBase64Decoder (data->pl_data, buffer, size); andre@0: if (pr_status == PR_FAILURE) andre@0: return SECFailure; andre@0: andre@0: return SECSuccess; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * When you're done decoding, call this to free the data. If "abort_p" andre@0: * is false, then calling this may cause the output_fn to be called andre@0: * one last time (as the last buffered data is flushed out). andre@0: */ andre@0: SECStatus andre@0: NSSBase64Decoder_Destroy (NSSBase64Decoder *data, PRBool abort_p) andre@0: { andre@0: PRStatus pr_status; andre@0: andre@0: /* XXX Should we do argument checking only in debug build? */ andre@0: if (data == NULL) { andre@0: PORT_SetError (SEC_ERROR_INVALID_ARGS); andre@0: return SECFailure; andre@0: } andre@0: andre@0: pr_status = PL_DestroyBase64Decoder (data->pl_data, abort_p); andre@0: andre@0: PORT_Free(data); andre@0: andre@0: if (pr_status == PR_FAILURE) andre@0: return SECFailure; andre@0: andre@0: return SECSuccess; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * Perform base64 decoding from an ascii string "inStr" to an Item. andre@0: * The length of the input must be provided as "inLen". The Item andre@0: * may be provided (as "outItemOpt"); you can also pass in a NULL andre@0: * and the Item will be allocated for you. andre@0: * andre@0: * In any case, the data within the Item will be allocated for you. andre@0: * All allocation will happen out of the passed-in "arenaOpt", if non-NULL. andre@0: * If "arenaOpt" is NULL, standard allocation (heap) will be used and andre@0: * you will want to free the result via SECITEM_FreeItem. andre@0: * andre@0: * Return value is NULL on error, the Item (allocated or provided) otherwise. andre@0: */ andre@0: SECItem * andre@0: NSSBase64_DecodeBuffer (PLArenaPool *arenaOpt, SECItem *outItemOpt, andre@0: const char *inStr, unsigned int inLen) andre@0: { andre@0: SECItem *out_item = NULL; andre@0: PRUint32 max_out_len = 0; andre@0: PRUint32 out_len; andre@0: void *mark = NULL; andre@0: unsigned char *dummy; andre@0: andre@0: if ((outItemOpt != NULL && outItemOpt->data != NULL) || inLen == 0) { andre@0: PORT_SetError (SEC_ERROR_INVALID_ARGS); andre@0: return NULL; andre@0: } andre@0: andre@0: if (arenaOpt != NULL) andre@0: mark = PORT_ArenaMark (arenaOpt); andre@0: andre@0: max_out_len = PL_Base64MaxDecodedLength (inLen); andre@0: out_item = SECITEM_AllocItem (arenaOpt, outItemOpt, max_out_len); andre@0: if (out_item == NULL) { andre@0: if (arenaOpt != NULL) andre@0: PORT_ArenaRelease (arenaOpt, mark); andre@0: return NULL; andre@0: } andre@0: andre@0: dummy = PL_Base64DecodeBuffer (inStr, inLen, out_item->data, andre@0: max_out_len, &out_len); andre@0: if (dummy == NULL) { andre@0: if (arenaOpt != NULL) { andre@0: PORT_ArenaRelease (arenaOpt, mark); andre@0: if (outItemOpt != NULL) { andre@0: outItemOpt->data = NULL; andre@0: outItemOpt->len = 0; andre@0: } andre@0: } else { andre@0: SECITEM_FreeItem (out_item, andre@0: (outItemOpt == NULL) ? PR_TRUE : PR_FALSE); andre@0: } andre@0: return NULL; andre@0: } andre@0: andre@0: if (arenaOpt != NULL) andre@0: PORT_ArenaUnmark (arenaOpt, mark); andre@0: out_item->len = out_len; andre@0: return out_item; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * XXX Everything below is deprecated. If you add new stuff, put it andre@0: * *above*, not below. andre@0: */ andre@0: andre@0: /* andre@0: * XXX The following "ATOB" functions are provided for backward compatibility andre@0: * with current code. They should be considered strongly deprecated. andre@0: * When we can convert all our code over to using the new NSSBase64Decoder_ andre@0: * functions defined above, we should get rid of these altogether. (Remove andre@0: * protoypes from base64.h as well -- actually, remove that file completely). andre@0: * If someone thinks either of these functions provides such a very useful andre@0: * interface (though, as shown, the same functionality can already be andre@0: * obtained by calling NSSBase64_DecodeBuffer directly), fine -- but then andre@0: * that API should be provided with a nice new NSSFoo name and using andre@0: * appropriate types, etc. andre@0: */ andre@0: andre@0: #include "base64.h" andre@0: andre@0: /* andre@0: ** Return an PORT_Alloc'd string which is the base64 decoded version andre@0: ** of the input string; set *lenp to the length of the returned data. andre@0: */ andre@0: unsigned char * andre@0: ATOB_AsciiToData(const char *string, unsigned int *lenp) andre@0: { andre@0: SECItem binary_item, *dummy; andre@0: andre@0: binary_item.data = NULL; andre@0: binary_item.len = 0; andre@0: andre@0: dummy = NSSBase64_DecodeBuffer (NULL, &binary_item, string, andre@0: (PRUint32) PORT_Strlen(string)); andre@0: if (dummy == NULL) andre@0: return NULL; andre@0: andre@0: PORT_Assert(dummy == &binary_item); andre@0: andre@0: *lenp = dummy->len; andre@0: return dummy->data; andre@0: } andre@0: andre@0: /* andre@0: ** Convert from ascii to binary encoding of an item. andre@0: */ andre@0: SECStatus andre@0: ATOB_ConvertAsciiToItem(SECItem *binary_item, const char *ascii) andre@0: { andre@0: SECItem *dummy; andre@0: andre@0: if (binary_item == NULL) { andre@0: PORT_SetError (SEC_ERROR_INVALID_ARGS); andre@0: return SECFailure; andre@0: } andre@0: andre@0: /* andre@0: * XXX Would prefer to assert here if data is non-null (actually, andre@0: * don't need to, just let NSSBase64_DecodeBuffer do it), so as to andre@0: * to catch unintended memory leaks, but callers are not clean in andre@0: * this respect so we need to explicitly clear here to avoid the andre@0: * assert in NSSBase64_DecodeBuffer. andre@0: */ andre@0: binary_item->data = NULL; andre@0: binary_item->len = 0; andre@0: andre@0: dummy = NSSBase64_DecodeBuffer (NULL, binary_item, ascii, andre@0: (PRUint32) PORT_Strlen(ascii)); andre@0: andre@0: if (dummy == NULL) andre@0: return SECFailure; andre@0: andre@0: return SECSuccess; andre@0: }