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 encoding (binary to ascii). 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 See the big comment at the top of nssb64d.c about moving the andre@0: * bulk of this code over into NSPR (the PL part). It all applies andre@0: * here but I didn't want to duplicate it, to avoid divergence problems. andre@0: */ andre@0: andre@0: /* andre@0: ************************************************************** andre@0: * XXX Beginning of base64 encoding code to be moved into NSPR. andre@0: */ andre@0: andre@0: andre@0: struct PLBase64EncodeStateStr { andre@0: unsigned chunks; andre@0: unsigned saved; andre@0: unsigned char buf[3]; 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 PLBase64EncoderStr PLBase64Encoder; andre@0: andre@0: /* andre@0: * The following implementation of base64 encoding 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 encoding 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 encoder to store state. andre@0: */ andre@0: struct PLBase64EncoderStr { andre@0: /* andre@0: * The one or two bytes pending. (We need 3 to create a "token", andre@0: * and hold the leftovers here. in_buffer_count is *only* ever andre@0: * 0, 1, or 2. andre@0: */ andre@0: unsigned char in_buffer[2]; andre@0: int in_buffer_count; andre@0: andre@0: /* andre@0: * If the caller wants linebreaks added, line_length specifies andre@0: * where they come out. It must be a multiple of 4; if the caller andre@0: * provides one that isn't, we round it down to the nearest andre@0: * multiple of 4. andre@0: * andre@0: * The value of current_column counts how many characters have been andre@0: * added since the last linebreaks (or since the beginning, on the andre@0: * first line). It is also always a multiple of 4; it is unused when andre@0: * line_length is 0. andre@0: */ andre@0: PRUint32 line_length; andre@0: PRUint32 current_column; andre@0: andre@0: /* andre@0: * Where to write the encoded 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 char *buf, PRInt32 size); andre@0: void *output_arg; andre@0: andre@0: /* andre@0: * Where the encoded 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: 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 a binary value to its corresponding ascii "code". andre@0: */ andre@0: static unsigned char base64_valuetocode[64] = andre@0: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; andre@0: andre@0: #define B64_PAD '=' andre@0: #define B64_CR '\r' andre@0: #define B64_LF '\n' andre@0: andre@0: static PRStatus andre@0: pl_base64_encode_buffer (PLBase64Encoder *data, const unsigned char *in, andre@0: PRUint32 size) andre@0: { andre@0: const unsigned char *end = in + size; andre@0: char *out = data->output_buffer + data->output_length; andre@0: unsigned int i = data->in_buffer_count; andre@0: PRUint32 n = 0; andre@0: int off; andre@0: PRUint32 output_threshold; andre@0: andre@0: /* If this input buffer is too small, wait until next time. */ andre@0: if (size < (3 - i)) { andre@0: data->in_buffer[i++] = in[0]; andre@0: if (size > 1) andre@0: data->in_buffer[i++] = in[1]; andre@0: PR_ASSERT(i < 3); andre@0: data->in_buffer_count = i; andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: /* If there are bytes that were put back last time, take them now. */ andre@0: if (i > 0) { andre@0: n = data->in_buffer[0]; andre@0: if (i > 1) andre@0: n = (n << 8) | data->in_buffer[1]; andre@0: data->in_buffer_count = 0; andre@0: } andre@0: andre@0: /* If our total is not a multiple of three, put one or two bytes back. */ andre@0: off = (size + i) % 3; andre@0: if (off > 0) { andre@0: size -= off; andre@0: data->in_buffer[0] = in[size]; andre@0: if (off > 1) andre@0: data->in_buffer[1] = in[size + 1]; andre@0: data->in_buffer_count = off; andre@0: end -= off; andre@0: } andre@0: andre@0: output_threshold = data->output_buflen - 3; andre@0: andre@0: /* andre@0: * Populate the output buffer with base64 data, one line (or buffer) andre@0: * at a time. andre@0: */ andre@0: while (in < end) { andre@0: int j, k; andre@0: andre@0: while (i < 3) { andre@0: n = (n << 8) | *in++; andre@0: i++; andre@0: } andre@0: i = 0; andre@0: andre@0: if (data->line_length > 0) { andre@0: if (data->current_column >= data->line_length) { andre@0: data->current_column = 0; andre@0: *out++ = B64_CR; andre@0: *out++ = B64_LF; andre@0: data->output_length += 2; andre@0: } andre@0: data->current_column += 4; /* the bytes we are about to add */ andre@0: } andre@0: andre@0: for (j = 18; j >= 0; j -= 6) { andre@0: k = (n >> j) & 0x3F; andre@0: *out++ = base64_valuetocode[k]; andre@0: } andre@0: n = 0; andre@0: data->output_length += 4; andre@0: andre@0: if (data->output_length >= output_threshold) { andre@0: PR_ASSERT(data->output_length <= data->output_buflen); andre@0: if (data->output_fn != NULL) { andre@0: PRInt32 output_result; andre@0: 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: return PR_FAILURE; andre@0: andre@0: out = data->output_buffer; andre@0: data->output_length = 0; andre@0: } else { andre@0: /* andre@0: * Check that we are about to exit the loop. (Since we andre@0: * are over the threshold, there isn't enough room in the andre@0: * output buffer for another trip around.) andre@0: */ andre@0: PR_ASSERT(in == end); andre@0: if (in < end) { andre@0: PR_SetError (PR_BUFFER_OVERFLOW_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: } andre@0: } andre@0: } andre@0: andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: static PRStatus andre@0: pl_base64_encode_flush (PLBase64Encoder *data) andre@0: { andre@0: int i = data->in_buffer_count; andre@0: andre@0: if (i == 0 && data->output_length == 0) andre@0: return PR_SUCCESS; andre@0: andre@0: if (i > 0) { andre@0: char *out = data->output_buffer + data->output_length; andre@0: PRUint32 n; andre@0: int j, k; andre@0: andre@0: n = ((PRUint32) data->in_buffer[0]) << 16; andre@0: if (i > 1) andre@0: n |= ((PRUint32) data->in_buffer[1] << 8); andre@0: andre@0: data->in_buffer_count = 0; andre@0: andre@0: if (data->line_length > 0) { andre@0: if (data->current_column >= data->line_length) { andre@0: data->current_column = 0; andre@0: *out++ = B64_CR; andre@0: *out++ = B64_LF; andre@0: data->output_length += 2; andre@0: } andre@0: } andre@0: andre@0: /* andre@0: * This will fill in more than we really have data for, but the andre@0: * valid parts will end up in the correct position and the extras andre@0: * will be over-written with pad characters below. andre@0: */ andre@0: for (j = 18; j >= 0; j -= 6) { andre@0: k = (n >> j) & 0x3F; andre@0: *out++ = base64_valuetocode[k]; andre@0: } andre@0: andre@0: /* Pad with equal-signs. */ andre@0: if (i == 1) andre@0: out[-2] = B64_PAD; andre@0: out[-1] = B64_PAD; andre@0: andre@0: data->output_length += 4; andre@0: } andre@0: andre@0: if (data->output_fn != NULL) { andre@0: PRInt32 output_result; andre@0: andre@0: output_result = data->output_fn (data->output_arg, data->output_buffer, andre@0: (PRInt32) data->output_length); andre@0: data->output_length = 0; andre@0: andre@0: if (output_result < 0) andre@0: return PR_FAILURE; 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 encoder given input andre@0: * data of length "size", and allowing for CRLF added at least every andre@0: * line_length bytes (we will add it at nearest lower multiple of 4). andre@0: * There is no trailing CRLF. andre@0: */ andre@0: static PRUint32 andre@0: PL_Base64MaxEncodedLength (PRUint32 size, PRUint32 line_length) andre@0: { andre@0: PRUint32 tokens, tokens_per_line, full_lines, line_break_chars, remainder; andre@0: andre@0: tokens = (size + 2) / 3; andre@0: andre@0: if (line_length == 0) andre@0: return tokens * 4; andre@0: andre@0: if (line_length < 4) /* too small! */ andre@0: line_length = 4; andre@0: andre@0: tokens_per_line = line_length / 4; andre@0: full_lines = tokens / tokens_per_line; andre@0: remainder = (tokens - (full_lines * tokens_per_line)) * 4; andre@0: line_break_chars = full_lines * 2; andre@0: if (remainder == 0) andre@0: line_break_chars -= 2; andre@0: andre@0: return (full_lines * tokens_per_line * 4) + line_break_chars + remainder; 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.) All common initialization of the andre@0: * encoding context should be done *here*. andre@0: * andre@0: * Save "line_length", rounded down to nearest multiple of 4 (if not andre@0: * already even multiple). Allocate output_buffer, if not provided -- andre@0: * based on given size if specified, otherwise based on line_length. andre@0: */ andre@0: static PLBase64Encoder * andre@0: pl_base64_create_encoder (PRUint32 line_length, char *output_buffer, andre@0: PRUint32 output_buflen) andre@0: { andre@0: PLBase64Encoder *data; andre@0: PRUint32 line_tokens; andre@0: andre@0: data = PR_NEWZAP(PLBase64Encoder); andre@0: if (data == NULL) andre@0: return NULL; andre@0: andre@0: if (line_length > 0 && line_length < 4) /* too small! */ andre@0: line_length = 4; andre@0: andre@0: line_tokens = line_length / 4; andre@0: data->line_length = line_tokens * 4; andre@0: andre@0: if (output_buffer == NULL) { andre@0: if (output_buflen == 0) { andre@0: if (data->line_length > 0) /* need to include room for CRLF */ andre@0: output_buflen = data->line_length + 2; andre@0: else andre@0: output_buflen = 64; /* XXX what is a good size? */ andre@0: } andre@0: andre@0: output_buffer = (char *) PR_Malloc(output_buflen); andre@0: if (output_buffer == NULL) { andre@0: PR_Free(data); andre@0: return NULL; andre@0: } andre@0: } andre@0: andre@0: data->output_buffer = output_buffer; andre@0: data->output_buflen = output_buflen; andre@0: return data; andre@0: } andre@0: andre@0: /* andre@0: * Function to start a base64 encoding context. andre@0: * An "output_fn" is required; the "output_arg" parameter to that is optional. andre@0: * If linebreaks in the encoded output are desired, "line_length" specifies andre@0: * where to place them -- it will be rounded down to the nearest multiple of 4 andre@0: * (if it is not already an even multiple of 4). If it is zero, no linebreaks andre@0: * will be added. (FYI, a linebreak is CRLF -- two characters.) andre@0: */ andre@0: static PLBase64Encoder * andre@0: PL_CreateBase64Encoder (PRInt32 (*output_fn) (void *, const char *, PRInt32), andre@0: void *output_arg, PRUint32 line_length) andre@0: { andre@0: PLBase64Encoder *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_encoder (line_length, NULL, 0); andre@0: if (data == NULL) andre@0: return NULL; andre@0: 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 encoder, causing the output_fn (provided to Create) andre@0: * to be called with the encoded data. andre@0: */ andre@0: static PRStatus andre@0: PL_UpdateBase64Encoder (PLBase64Encoder *data, const unsigned char *buffer, andre@0: PRUint32 size) 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: return pl_base64_encode_buffer (data, buffer, size); andre@0: } andre@0: andre@0: andre@0: /* andre@0: * When you're done encoding, 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_DestroyBase64Encoder (PLBase64Encoder *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_encode_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 encoding 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 encoded length of output will be returned to you in andre@0: * "output_destlen". andre@0: * andre@0: * If linebreaks in the encoded output are desired, "line_length" specifies andre@0: * where to place them -- it will be rounded down to the nearest multiple of 4 andre@0: * (if it is not already an even multiple of 4). If it is zero, no linebreaks andre@0: * will be added. (FYI, a linebreak is CRLF -- two characters.) andre@0: * andre@0: * Return value is NULL on error, the output buffer (allocated or provided) andre@0: * otherwise. andre@0: */ andre@0: static char * andre@0: PL_Base64EncodeBuffer (const unsigned char *src, PRUint32 srclen, andre@0: PRUint32 line_length, char *dest, PRUint32 maxdestlen, andre@0: PRUint32 *output_destlen) andre@0: { andre@0: PRUint32 need_length; andre@0: PLBase64Encoder *data = NULL; andre@0: PRStatus status; andre@0: andre@0: PR_ASSERT(srclen > 0); andre@0: if (srclen == 0) andre@0: return dest; andre@0: andre@0: /* andre@0: * How much space could we possibly need for encoding this input? andre@0: */ andre@0: need_length = PL_Base64MaxEncodedLength (srclen, line_length); andre@0: andre@0: /* andre@0: * Make sure we have at least that much, if output buffer provided. 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: return NULL; andre@0: } andre@0: } else { andre@0: maxdestlen = need_length; andre@0: } andre@0: andre@0: data = pl_base64_create_encoder(line_length, dest, maxdestlen); andre@0: if (data == NULL) andre@0: return NULL; andre@0: andre@0: status = pl_base64_encode_buffer (data, src, 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 encoder context, which we need to look at first! andre@0: */ andre@0: if (status == PR_SUCCESS) andre@0: status = pl_base64_encode_flush (data); andre@0: andre@0: if (status != PR_SUCCESS) { andre@0: (void) PL_DestroyBase64Encoder (data, PR_TRUE); andre@0: return NULL; andre@0: } andre@0: andre@0: dest = data->output_buffer; andre@0: andre@0: /* Must clear this or Destroy will free it. */ andre@0: data->output_buffer = NULL; andre@0: andre@0: *output_destlen = data->output_length; andre@0: status = PL_DestroyBase64Encoder (data, PR_FALSE); andre@0: if (status == PR_FAILURE) { andre@0: PR_Free(dest); andre@0: return NULL; andre@0: } andre@0: andre@0: return dest; andre@0: } andre@0: andre@0: /* andre@0: * XXX End of base64 encoding 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 NSSBase64EncoderStr { andre@0: PLBase64Encoder *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 encoding context. andre@0: */ andre@0: NSSBase64Encoder * andre@0: NSSBase64Encoder_Create (PRInt32 (*output_fn) (void *, const char *, PRInt32), andre@0: void *output_arg) andre@0: { andre@0: PLBase64Encoder *pl_data; andre@0: NSSBase64Encoder *nss_data; andre@0: andre@0: nss_data = PORT_ZNew(NSSBase64Encoder); andre@0: if (nss_data == NULL) andre@0: return NULL; andre@0: andre@0: pl_data = PL_CreateBase64Encoder (output_fn, output_arg, 64); 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 encoder, causing the output_fn (provided to Create) andre@0: * to be called with the encoded data. andre@0: */ andre@0: SECStatus andre@0: NSSBase64Encoder_Update (NSSBase64Encoder *data, const unsigned 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_UpdateBase64Encoder (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 encoding, 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: NSSBase64Encoder_Destroy (NSSBase64Encoder *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_DestroyBase64Encoder (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 encoding of binary data "inItem" to an ascii string. andre@0: * The output buffer may be provided (as "outStrOpt"); you can also pass andre@0: * in a NULL and the buffer will be allocated for you. The result will andre@0: * be null-terminated, and if the buffer is provided, "maxOutLen" must andre@0: * specify the maximum length of the buffer and will be checked to andre@0: * supply sufficient space space for the encoded result. (If "outStrOpt" andre@0: * is NULL, "maxOutLen" is ignored.) andre@0: * andre@0: * If "outStrOpt" is NULL, allocation will happen out of the passed-in andre@0: * "arenaOpt", if *it* is non-NULL, otherwise standard allocation (heap) andre@0: * will be used. andre@0: * andre@0: * Return value is NULL on error, the output buffer (allocated or provided) andre@0: * otherwise. andre@0: */ andre@0: char * andre@0: NSSBase64_EncodeItem (PLArenaPool *arenaOpt, char *outStrOpt, andre@0: unsigned int maxOutLen, SECItem *inItem) andre@0: { andre@0: char *out_string = outStrOpt; andre@0: PRUint32 max_out_len; andre@0: PRUint32 out_len; andre@0: void *mark = NULL; andre@0: char *dummy; andre@0: andre@0: PORT_Assert(inItem != NULL && inItem->data != NULL && inItem->len != 0); andre@0: if (inItem == NULL || inItem->data == NULL || inItem->len == 0) { andre@0: PORT_SetError (SEC_ERROR_INVALID_ARGS); andre@0: return NULL; andre@0: } andre@0: andre@0: max_out_len = PL_Base64MaxEncodedLength (inItem->len, 64); andre@0: andre@0: if (arenaOpt != NULL) andre@0: mark = PORT_ArenaMark (arenaOpt); andre@0: andre@0: if (out_string == NULL) { andre@0: if (arenaOpt != NULL) andre@0: out_string = PORT_ArenaAlloc (arenaOpt, max_out_len + 1); andre@0: else andre@0: out_string = PORT_Alloc (max_out_len + 1); andre@0: andre@0: if (out_string == NULL) { andre@0: if (arenaOpt != NULL) andre@0: PORT_ArenaRelease (arenaOpt, mark); andre@0: return NULL; andre@0: } andre@0: } else { andre@0: if ((max_out_len + 1) > maxOutLen) { andre@0: PORT_SetError (SEC_ERROR_OUTPUT_LEN); andre@0: return NULL; andre@0: } andre@0: max_out_len = maxOutLen; andre@0: } andre@0: andre@0: andre@0: dummy = PL_Base64EncodeBuffer (inItem->data, inItem->len, 64, andre@0: out_string, max_out_len, &out_len); andre@0: if (dummy == NULL) { andre@0: if (arenaOpt != NULL) { andre@0: PORT_ArenaRelease (arenaOpt, mark); andre@0: } else { andre@0: PORT_Free (out_string); andre@0: } andre@0: return NULL; andre@0: } andre@0: andre@0: if (arenaOpt != NULL) andre@0: PORT_ArenaUnmark (arenaOpt, mark); andre@0: andre@0: out_string[out_len] = '\0'; andre@0: return out_string; 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 "BTOA" 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 NSSBase64Encoder_ 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_EncodeItem 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 ascii string which is the base64 encoded andre@0: ** version of the input string. andre@0: */ andre@0: char * andre@0: BTOA_DataToAscii(const unsigned char *data, unsigned int len) andre@0: { andre@0: SECItem binary_item; andre@0: andre@0: binary_item.data = (unsigned char *)data; andre@0: binary_item.len = len; andre@0: andre@0: return NSSBase64_EncodeItem (NULL, NULL, 0, &binary_item); andre@0: } andre@0: andre@0: /* andre@0: ** Convert from binary encoding of an item to ascii. andre@0: */ andre@0: char * andre@0: BTOA_ConvertItemToAscii (SECItem *binary_item) andre@0: { andre@0: return NSSBase64_EncodeItem (NULL, NULL, 0, binary_item); andre@0: }