michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Base64 encoding (binary to ascii). michael@0: */ michael@0: michael@0: #include "nssb64.h" michael@0: #include "nspr.h" michael@0: #include "secitem.h" michael@0: #include "secerr.h" michael@0: michael@0: /* michael@0: * XXX See the big comment at the top of nssb64d.c about moving the michael@0: * bulk of this code over into NSPR (the PL part). It all applies michael@0: * here but I didn't want to duplicate it, to avoid divergence problems. michael@0: */ michael@0: michael@0: /* michael@0: ************************************************************** michael@0: * XXX Beginning of base64 encoding code to be moved into NSPR. michael@0: */ michael@0: michael@0: michael@0: struct PLBase64EncodeStateStr { michael@0: unsigned chunks; michael@0: unsigned saved; michael@0: unsigned char buf[3]; michael@0: }; michael@0: michael@0: /* michael@0: * This typedef would belong in the NSPR header file (i.e. plbase64.h). michael@0: */ michael@0: typedef struct PLBase64EncoderStr PLBase64Encoder; michael@0: michael@0: /* michael@0: * The following implementation of base64 encoding was based on code michael@0: * found in libmime (specifically, in mimeenc.c). It has been adapted to michael@0: * use PR types and naming as well as to provide other necessary semantics michael@0: * (like buffer-in/buffer-out in addition to "streaming" without undue michael@0: * performance hit of extra copying if you made the buffer versions michael@0: * use the output_fn). It also incorporates some aspects of the current michael@0: * NSPR base64 encoding code. As such, you may find similarities to michael@0: * both of those implementations. I tried to use names that reflected michael@0: * the original code when possible. For this reason you may find some michael@0: * inconsistencies -- libmime used lots of "in" and "out" whereas the michael@0: * NSPR version uses "src" and "dest"; sometimes I changed one to the other michael@0: * and sometimes I left them when I thought the subroutines were at least michael@0: * self-consistent. michael@0: */ michael@0: michael@0: PR_BEGIN_EXTERN_C michael@0: michael@0: /* michael@0: * Opaque object used by the encoder to store state. michael@0: */ michael@0: struct PLBase64EncoderStr { michael@0: /* michael@0: * The one or two bytes pending. (We need 3 to create a "token", michael@0: * and hold the leftovers here. in_buffer_count is *only* ever michael@0: * 0, 1, or 2. michael@0: */ michael@0: unsigned char in_buffer[2]; michael@0: int in_buffer_count; michael@0: michael@0: /* michael@0: * If the caller wants linebreaks added, line_length specifies michael@0: * where they come out. It must be a multiple of 4; if the caller michael@0: * provides one that isn't, we round it down to the nearest michael@0: * multiple of 4. michael@0: * michael@0: * The value of current_column counts how many characters have been michael@0: * added since the last linebreaks (or since the beginning, on the michael@0: * first line). It is also always a multiple of 4; it is unused when michael@0: * line_length is 0. michael@0: */ michael@0: PRUint32 line_length; michael@0: PRUint32 current_column; michael@0: michael@0: /* michael@0: * Where to write the encoded data (used when streaming, not when michael@0: * doing all in-memory (buffer) operations). michael@0: * michael@0: * Note that this definition is chosen to be compatible with PR_Write. michael@0: */ michael@0: PRInt32 (*output_fn) (void *output_arg, const char *buf, PRInt32 size); michael@0: void *output_arg; michael@0: michael@0: /* michael@0: * Where the encoded output goes -- either temporarily (in the streaming michael@0: * case, staged here before it goes to the output function) or what will michael@0: * be the entire buffered result for users of the buffer version. michael@0: */ michael@0: char *output_buffer; michael@0: PRUint32 output_buflen; /* the total length of allocated buffer */ michael@0: PRUint32 output_length; /* the length that is currently populated */ michael@0: }; michael@0: michael@0: PR_END_EXTERN_C michael@0: michael@0: michael@0: /* michael@0: * Table to convert a binary value to its corresponding ascii "code". michael@0: */ michael@0: static unsigned char base64_valuetocode[64] = michael@0: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; michael@0: michael@0: #define B64_PAD '=' michael@0: #define B64_CR '\r' michael@0: #define B64_LF '\n' michael@0: michael@0: static PRStatus michael@0: pl_base64_encode_buffer (PLBase64Encoder *data, const unsigned char *in, michael@0: PRUint32 size) michael@0: { michael@0: const unsigned char *end = in + size; michael@0: char *out = data->output_buffer + data->output_length; michael@0: unsigned int i = data->in_buffer_count; michael@0: PRUint32 n = 0; michael@0: int off; michael@0: PRUint32 output_threshold; michael@0: michael@0: /* If this input buffer is too small, wait until next time. */ michael@0: if (size < (3 - i)) { michael@0: data->in_buffer[i++] = in[0]; michael@0: if (size > 1) michael@0: data->in_buffer[i++] = in[1]; michael@0: PR_ASSERT(i < 3); michael@0: data->in_buffer_count = i; michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: /* If there are bytes that were put back last time, take them now. */ michael@0: if (i > 0) { michael@0: n = data->in_buffer[0]; michael@0: if (i > 1) michael@0: n = (n << 8) | data->in_buffer[1]; michael@0: data->in_buffer_count = 0; michael@0: } michael@0: michael@0: /* If our total is not a multiple of three, put one or two bytes back. */ michael@0: off = (size + i) % 3; michael@0: if (off > 0) { michael@0: size -= off; michael@0: data->in_buffer[0] = in[size]; michael@0: if (off > 1) michael@0: data->in_buffer[1] = in[size + 1]; michael@0: data->in_buffer_count = off; michael@0: end -= off; michael@0: } michael@0: michael@0: output_threshold = data->output_buflen - 3; michael@0: michael@0: /* michael@0: * Populate the output buffer with base64 data, one line (or buffer) michael@0: * at a time. michael@0: */ michael@0: while (in < end) { michael@0: int j, k; michael@0: michael@0: while (i < 3) { michael@0: n = (n << 8) | *in++; michael@0: i++; michael@0: } michael@0: i = 0; michael@0: michael@0: if (data->line_length > 0) { michael@0: if (data->current_column >= data->line_length) { michael@0: data->current_column = 0; michael@0: *out++ = B64_CR; michael@0: *out++ = B64_LF; michael@0: data->output_length += 2; michael@0: } michael@0: data->current_column += 4; /* the bytes we are about to add */ michael@0: } michael@0: michael@0: for (j = 18; j >= 0; j -= 6) { michael@0: k = (n >> j) & 0x3F; michael@0: *out++ = base64_valuetocode[k]; michael@0: } michael@0: n = 0; michael@0: data->output_length += 4; michael@0: michael@0: if (data->output_length >= output_threshold) { michael@0: PR_ASSERT(data->output_length <= data->output_buflen); michael@0: if (data->output_fn != NULL) { michael@0: PRInt32 output_result; michael@0: michael@0: output_result = data->output_fn (data->output_arg, michael@0: data->output_buffer, michael@0: (PRInt32) data->output_length); michael@0: if (output_result < 0) michael@0: return PR_FAILURE; michael@0: michael@0: out = data->output_buffer; michael@0: data->output_length = 0; michael@0: } else { michael@0: /* michael@0: * Check that we are about to exit the loop. (Since we michael@0: * are over the threshold, there isn't enough room in the michael@0: * output buffer for another trip around.) michael@0: */ michael@0: PR_ASSERT(in == end); michael@0: if (in < end) { michael@0: PR_SetError (PR_BUFFER_OVERFLOW_ERROR, 0); michael@0: return PR_FAILURE; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: static PRStatus michael@0: pl_base64_encode_flush (PLBase64Encoder *data) michael@0: { michael@0: int i = data->in_buffer_count; michael@0: michael@0: if (i == 0 && data->output_length == 0) michael@0: return PR_SUCCESS; michael@0: michael@0: if (i > 0) { michael@0: char *out = data->output_buffer + data->output_length; michael@0: PRUint32 n; michael@0: int j, k; michael@0: michael@0: n = ((PRUint32) data->in_buffer[0]) << 16; michael@0: if (i > 1) michael@0: n |= ((PRUint32) data->in_buffer[1] << 8); michael@0: michael@0: data->in_buffer_count = 0; michael@0: michael@0: if (data->line_length > 0) { michael@0: if (data->current_column >= data->line_length) { michael@0: data->current_column = 0; michael@0: *out++ = B64_CR; michael@0: *out++ = B64_LF; michael@0: data->output_length += 2; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * This will fill in more than we really have data for, but the michael@0: * valid parts will end up in the correct position and the extras michael@0: * will be over-written with pad characters below. michael@0: */ michael@0: for (j = 18; j >= 0; j -= 6) { michael@0: k = (n >> j) & 0x3F; michael@0: *out++ = base64_valuetocode[k]; michael@0: } michael@0: michael@0: /* Pad with equal-signs. */ michael@0: if (i == 1) michael@0: out[-2] = B64_PAD; michael@0: out[-1] = B64_PAD; michael@0: michael@0: data->output_length += 4; michael@0: } michael@0: michael@0: if (data->output_fn != NULL) { michael@0: PRInt32 output_result; michael@0: michael@0: output_result = data->output_fn (data->output_arg, data->output_buffer, michael@0: (PRInt32) data->output_length); michael@0: data->output_length = 0; michael@0: michael@0: if (output_result < 0) michael@0: return PR_FAILURE; michael@0: } michael@0: michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * The maximum space needed to hold the output of the encoder given input michael@0: * data of length "size", and allowing for CRLF added at least every michael@0: * line_length bytes (we will add it at nearest lower multiple of 4). michael@0: * There is no trailing CRLF. michael@0: */ michael@0: static PRUint32 michael@0: PL_Base64MaxEncodedLength (PRUint32 size, PRUint32 line_length) michael@0: { michael@0: PRUint32 tokens, tokens_per_line, full_lines, line_break_chars, remainder; michael@0: michael@0: tokens = (size + 2) / 3; michael@0: michael@0: if (line_length == 0) michael@0: return tokens * 4; michael@0: michael@0: if (line_length < 4) /* too small! */ michael@0: line_length = 4; michael@0: michael@0: tokens_per_line = line_length / 4; michael@0: full_lines = tokens / tokens_per_line; michael@0: remainder = (tokens - (full_lines * tokens_per_line)) * 4; michael@0: line_break_chars = full_lines * 2; michael@0: if (remainder == 0) michael@0: line_break_chars -= 2; michael@0: michael@0: return (full_lines * tokens_per_line * 4) + line_break_chars + remainder; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * A distinct internal creation function for the buffer version to use. michael@0: * (It does not want to specify an output_fn, and we want the normal michael@0: * Create function to require that.) All common initialization of the michael@0: * encoding context should be done *here*. michael@0: * michael@0: * Save "line_length", rounded down to nearest multiple of 4 (if not michael@0: * already even multiple). Allocate output_buffer, if not provided -- michael@0: * based on given size if specified, otherwise based on line_length. michael@0: */ michael@0: static PLBase64Encoder * michael@0: pl_base64_create_encoder (PRUint32 line_length, char *output_buffer, michael@0: PRUint32 output_buflen) michael@0: { michael@0: PLBase64Encoder *data; michael@0: PRUint32 line_tokens; michael@0: michael@0: data = PR_NEWZAP(PLBase64Encoder); michael@0: if (data == NULL) michael@0: return NULL; michael@0: michael@0: if (line_length > 0 && line_length < 4) /* too small! */ michael@0: line_length = 4; michael@0: michael@0: line_tokens = line_length / 4; michael@0: data->line_length = line_tokens * 4; michael@0: michael@0: if (output_buffer == NULL) { michael@0: if (output_buflen == 0) { michael@0: if (data->line_length > 0) /* need to include room for CRLF */ michael@0: output_buflen = data->line_length + 2; michael@0: else michael@0: output_buflen = 64; /* XXX what is a good size? */ michael@0: } michael@0: michael@0: output_buffer = (char *) PR_Malloc(output_buflen); michael@0: if (output_buffer == NULL) { michael@0: PR_Free(data); michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: data->output_buffer = output_buffer; michael@0: data->output_buflen = output_buflen; michael@0: return data; michael@0: } michael@0: michael@0: /* michael@0: * Function to start a base64 encoding context. michael@0: * An "output_fn" is required; the "output_arg" parameter to that is optional. michael@0: * If linebreaks in the encoded output are desired, "line_length" specifies michael@0: * where to place them -- it will be rounded down to the nearest multiple of 4 michael@0: * (if it is not already an even multiple of 4). If it is zero, no linebreaks michael@0: * will be added. (FYI, a linebreak is CRLF -- two characters.) michael@0: */ michael@0: static PLBase64Encoder * michael@0: PL_CreateBase64Encoder (PRInt32 (*output_fn) (void *, const char *, PRInt32), michael@0: void *output_arg, PRUint32 line_length) michael@0: { michael@0: PLBase64Encoder *data; michael@0: michael@0: if (output_fn == NULL) { michael@0: PR_SetError (PR_INVALID_ARGUMENT_ERROR, 0); michael@0: return NULL; michael@0: } michael@0: michael@0: data = pl_base64_create_encoder (line_length, NULL, 0); michael@0: if (data == NULL) michael@0: return NULL; michael@0: michael@0: data->output_fn = output_fn; michael@0: data->output_arg = output_arg; michael@0: michael@0: return data; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Push data through the encoder, causing the output_fn (provided to Create) michael@0: * to be called with the encoded data. michael@0: */ michael@0: static PRStatus michael@0: PL_UpdateBase64Encoder (PLBase64Encoder *data, const unsigned char *buffer, michael@0: PRUint32 size) michael@0: { michael@0: /* XXX Should we do argument checking only in debug build? */ michael@0: if (data == NULL || buffer == NULL || size == 0) { michael@0: PR_SetError (PR_INVALID_ARGUMENT_ERROR, 0); michael@0: return PR_FAILURE; michael@0: } michael@0: michael@0: return pl_base64_encode_buffer (data, buffer, size); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * When you're done encoding, call this to free the data. If "abort_p" michael@0: * is false, then calling this may cause the output_fn to be called michael@0: * one last time (as the last buffered data is flushed out). michael@0: */ michael@0: static PRStatus michael@0: PL_DestroyBase64Encoder (PLBase64Encoder *data, PRBool abort_p) michael@0: { michael@0: PRStatus status = PR_SUCCESS; michael@0: michael@0: /* XXX Should we do argument checking only in debug build? */ michael@0: if (data == NULL) { michael@0: PR_SetError (PR_INVALID_ARGUMENT_ERROR, 0); michael@0: return PR_FAILURE; michael@0: } michael@0: michael@0: /* Flush out the last few buffered characters. */ michael@0: if (!abort_p) michael@0: status = pl_base64_encode_flush (data); michael@0: michael@0: if (data->output_buffer != NULL) michael@0: PR_Free(data->output_buffer); michael@0: PR_Free(data); michael@0: michael@0: return status; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Perform base64 encoding from an input buffer to an output buffer. michael@0: * The output buffer can be provided (as "dest"); you can also pass in michael@0: * a NULL and this function will allocate a buffer large enough for you, michael@0: * and return it. If you do provide the output buffer, you must also michael@0: * provide the maximum length of that buffer (as "maxdestlen"). michael@0: * The actual encoded length of output will be returned to you in michael@0: * "output_destlen". michael@0: * michael@0: * If linebreaks in the encoded output are desired, "line_length" specifies michael@0: * where to place them -- it will be rounded down to the nearest multiple of 4 michael@0: * (if it is not already an even multiple of 4). If it is zero, no linebreaks michael@0: * will be added. (FYI, a linebreak is CRLF -- two characters.) michael@0: * michael@0: * Return value is NULL on error, the output buffer (allocated or provided) michael@0: * otherwise. michael@0: */ michael@0: static char * michael@0: PL_Base64EncodeBuffer (const unsigned char *src, PRUint32 srclen, michael@0: PRUint32 line_length, char *dest, PRUint32 maxdestlen, michael@0: PRUint32 *output_destlen) michael@0: { michael@0: PRUint32 need_length; michael@0: PLBase64Encoder *data = NULL; michael@0: PRStatus status; michael@0: michael@0: PR_ASSERT(srclen > 0); michael@0: if (srclen == 0) michael@0: return dest; michael@0: michael@0: /* michael@0: * How much space could we possibly need for encoding this input? michael@0: */ michael@0: need_length = PL_Base64MaxEncodedLength (srclen, line_length); michael@0: michael@0: /* michael@0: * Make sure we have at least that much, if output buffer provided. michael@0: */ michael@0: if (dest != NULL) { michael@0: PR_ASSERT(maxdestlen >= need_length); michael@0: if (maxdestlen < need_length) { michael@0: PR_SetError(PR_BUFFER_OVERFLOW_ERROR, 0); michael@0: return NULL; michael@0: } michael@0: } else { michael@0: maxdestlen = need_length; michael@0: } michael@0: michael@0: data = pl_base64_create_encoder(line_length, dest, maxdestlen); michael@0: if (data == NULL) michael@0: return NULL; michael@0: michael@0: status = pl_base64_encode_buffer (data, src, srclen); michael@0: michael@0: /* michael@0: * We do not wait for Destroy to flush, because Destroy will also michael@0: * get rid of our encoder context, which we need to look at first! michael@0: */ michael@0: if (status == PR_SUCCESS) michael@0: status = pl_base64_encode_flush (data); michael@0: michael@0: if (status != PR_SUCCESS) { michael@0: (void) PL_DestroyBase64Encoder (data, PR_TRUE); michael@0: return NULL; michael@0: } michael@0: michael@0: dest = data->output_buffer; michael@0: michael@0: /* Must clear this or Destroy will free it. */ michael@0: data->output_buffer = NULL; michael@0: michael@0: *output_destlen = data->output_length; michael@0: status = PL_DestroyBase64Encoder (data, PR_FALSE); michael@0: if (status == PR_FAILURE) { michael@0: PR_Free(dest); michael@0: return NULL; michael@0: } michael@0: michael@0: return dest; michael@0: } michael@0: michael@0: /* michael@0: * XXX End of base64 encoding code to be moved into NSPR. michael@0: ******************************************************** michael@0: */ michael@0: michael@0: /* michael@0: * This is the beginning of the NSS cover functions. These will michael@0: * provide the interface we want to expose as NSS-ish. For example, michael@0: * they will operate on our Items, do any special handling or checking michael@0: * we want to do, etc. michael@0: */ michael@0: michael@0: michael@0: PR_BEGIN_EXTERN_C michael@0: michael@0: /* michael@0: * A boring cover structure for now. Perhaps someday it will include michael@0: * some more interesting fields. michael@0: */ michael@0: struct NSSBase64EncoderStr { michael@0: PLBase64Encoder *pl_data; michael@0: }; michael@0: michael@0: PR_END_EXTERN_C michael@0: michael@0: michael@0: /* michael@0: * Function to start a base64 encoding context. michael@0: */ michael@0: NSSBase64Encoder * michael@0: NSSBase64Encoder_Create (PRInt32 (*output_fn) (void *, const char *, PRInt32), michael@0: void *output_arg) michael@0: { michael@0: PLBase64Encoder *pl_data; michael@0: NSSBase64Encoder *nss_data; michael@0: michael@0: nss_data = PORT_ZNew(NSSBase64Encoder); michael@0: if (nss_data == NULL) michael@0: return NULL; michael@0: michael@0: pl_data = PL_CreateBase64Encoder (output_fn, output_arg, 64); michael@0: if (pl_data == NULL) { michael@0: PORT_Free(nss_data); michael@0: return NULL; michael@0: } michael@0: michael@0: nss_data->pl_data = pl_data; michael@0: return nss_data; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Push data through the encoder, causing the output_fn (provided to Create) michael@0: * to be called with the encoded data. michael@0: */ michael@0: SECStatus michael@0: NSSBase64Encoder_Update (NSSBase64Encoder *data, const unsigned char *buffer, michael@0: PRUint32 size) michael@0: { michael@0: PRStatus pr_status; michael@0: michael@0: /* XXX Should we do argument checking only in debug build? */ michael@0: if (data == NULL) { michael@0: PORT_SetError (SEC_ERROR_INVALID_ARGS); michael@0: return SECFailure; michael@0: } michael@0: michael@0: pr_status = PL_UpdateBase64Encoder (data->pl_data, buffer, size); michael@0: if (pr_status == PR_FAILURE) michael@0: return SECFailure; michael@0: michael@0: return SECSuccess; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * When you're done encoding, call this to free the data. If "abort_p" michael@0: * is false, then calling this may cause the output_fn to be called michael@0: * one last time (as the last buffered data is flushed out). michael@0: */ michael@0: SECStatus michael@0: NSSBase64Encoder_Destroy (NSSBase64Encoder *data, PRBool abort_p) michael@0: { michael@0: PRStatus pr_status; michael@0: michael@0: /* XXX Should we do argument checking only in debug build? */ michael@0: if (data == NULL) { michael@0: PORT_SetError (SEC_ERROR_INVALID_ARGS); michael@0: return SECFailure; michael@0: } michael@0: michael@0: pr_status = PL_DestroyBase64Encoder (data->pl_data, abort_p); michael@0: michael@0: PORT_Free(data); michael@0: michael@0: if (pr_status == PR_FAILURE) michael@0: return SECFailure; michael@0: michael@0: return SECSuccess; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Perform base64 encoding of binary data "inItem" to an ascii string. michael@0: * The output buffer may be provided (as "outStrOpt"); you can also pass michael@0: * in a NULL and the buffer will be allocated for you. The result will michael@0: * be null-terminated, and if the buffer is provided, "maxOutLen" must michael@0: * specify the maximum length of the buffer and will be checked to michael@0: * supply sufficient space space for the encoded result. (If "outStrOpt" michael@0: * is NULL, "maxOutLen" is ignored.) michael@0: * michael@0: * If "outStrOpt" is NULL, allocation will happen out of the passed-in michael@0: * "arenaOpt", if *it* is non-NULL, otherwise standard allocation (heap) michael@0: * will be used. michael@0: * michael@0: * Return value is NULL on error, the output buffer (allocated or provided) michael@0: * otherwise. michael@0: */ michael@0: char * michael@0: NSSBase64_EncodeItem (PLArenaPool *arenaOpt, char *outStrOpt, michael@0: unsigned int maxOutLen, SECItem *inItem) michael@0: { michael@0: char *out_string = outStrOpt; michael@0: PRUint32 max_out_len; michael@0: PRUint32 out_len; michael@0: void *mark = NULL; michael@0: char *dummy; michael@0: michael@0: PORT_Assert(inItem != NULL && inItem->data != NULL && inItem->len != 0); michael@0: if (inItem == NULL || inItem->data == NULL || inItem->len == 0) { michael@0: PORT_SetError (SEC_ERROR_INVALID_ARGS); michael@0: return NULL; michael@0: } michael@0: michael@0: max_out_len = PL_Base64MaxEncodedLength (inItem->len, 64); michael@0: michael@0: if (arenaOpt != NULL) michael@0: mark = PORT_ArenaMark (arenaOpt); michael@0: michael@0: if (out_string == NULL) { michael@0: if (arenaOpt != NULL) michael@0: out_string = PORT_ArenaAlloc (arenaOpt, max_out_len + 1); michael@0: else michael@0: out_string = PORT_Alloc (max_out_len + 1); michael@0: michael@0: if (out_string == NULL) { michael@0: if (arenaOpt != NULL) michael@0: PORT_ArenaRelease (arenaOpt, mark); michael@0: return NULL; michael@0: } michael@0: } else { michael@0: if ((max_out_len + 1) > maxOutLen) { michael@0: PORT_SetError (SEC_ERROR_OUTPUT_LEN); michael@0: return NULL; michael@0: } michael@0: max_out_len = maxOutLen; michael@0: } michael@0: michael@0: michael@0: dummy = PL_Base64EncodeBuffer (inItem->data, inItem->len, 64, michael@0: out_string, max_out_len, &out_len); michael@0: if (dummy == NULL) { michael@0: if (arenaOpt != NULL) { michael@0: PORT_ArenaRelease (arenaOpt, mark); michael@0: } else { michael@0: PORT_Free (out_string); michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: if (arenaOpt != NULL) michael@0: PORT_ArenaUnmark (arenaOpt, mark); michael@0: michael@0: out_string[out_len] = '\0'; michael@0: return out_string; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * XXX Everything below is deprecated. If you add new stuff, put it michael@0: * *above*, not below. michael@0: */ michael@0: michael@0: /* michael@0: * XXX The following "BTOA" functions are provided for backward compatibility michael@0: * with current code. They should be considered strongly deprecated. michael@0: * When we can convert all our code over to using the new NSSBase64Encoder_ michael@0: * functions defined above, we should get rid of these altogether. (Remove michael@0: * protoypes from base64.h as well -- actually, remove that file completely). michael@0: * If someone thinks either of these functions provides such a very useful michael@0: * interface (though, as shown, the same functionality can already be michael@0: * obtained by calling NSSBase64_EncodeItem directly), fine -- but then michael@0: * that API should be provided with a nice new NSSFoo name and using michael@0: * appropriate types, etc. michael@0: */ michael@0: michael@0: #include "base64.h" michael@0: michael@0: /* michael@0: ** Return an PORT_Alloc'd ascii string which is the base64 encoded michael@0: ** version of the input string. michael@0: */ michael@0: char * michael@0: BTOA_DataToAscii(const unsigned char *data, unsigned int len) michael@0: { michael@0: SECItem binary_item; michael@0: michael@0: binary_item.data = (unsigned char *)data; michael@0: binary_item.len = len; michael@0: michael@0: return NSSBase64_EncodeItem (NULL, NULL, 0, &binary_item); michael@0: } michael@0: michael@0: /* michael@0: ** Convert from binary encoding of an item to ascii. michael@0: */ michael@0: char * michael@0: BTOA_ConvertItemToAscii (SECItem *binary_item) michael@0: { michael@0: return NSSBase64_EncodeItem (NULL, NULL, 0, binary_item); michael@0: }