Back to index

lightning-sunbird  0.9+nobinonly
nssb64e.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 /*
00038  * Base64 encoding (binary to ascii).
00039  *
00040  * $Id: nssb64e.c,v 1.3 2004/04/25 15:03:17 gerv%gerv.net Exp $
00041  */
00042 
00043 #include "nssb64.h"
00044 #include "nspr.h"
00045 #include "secitem.h"
00046 #include "secerr.h"
00047 
00048 /*
00049  * XXX See the big comment at the top of nssb64d.c about moving the
00050  * bulk of this code over into NSPR (the PL part).  It all applies
00051  * here but I didn't want to duplicate it, to avoid divergence problems.
00052  */ 
00053 
00054 /*
00055  **************************************************************
00056  * XXX Beginning of base64 encoding code to be moved into NSPR.
00057  */
00058 
00059 
00060 struct PLBase64EncodeStateStr {
00061     unsigned chunks;
00062     unsigned saved;
00063     unsigned char buf[3];
00064 };
00065 
00066 /*
00067  * This typedef would belong in the NSPR header file (i.e. plbase64.h).
00068  */
00069 typedef struct PLBase64EncoderStr PLBase64Encoder;
00070 
00071 /*
00072  * The following implementation of base64 encoding was based on code
00073  * found in libmime (specifically, in mimeenc.c).  It has been adapted to
00074  * use PR types and naming as well as to provide other necessary semantics
00075  * (like buffer-in/buffer-out in addition to "streaming" without undue
00076  * performance hit of extra copying if you made the buffer versions
00077  * use the output_fn).  It also incorporates some aspects of the current
00078  * NSPR base64 encoding code.  As such, you may find similarities to
00079  * both of those implementations.  I tried to use names that reflected
00080  * the original code when possible.  For this reason you may find some
00081  * inconsistencies -- libmime used lots of "in" and "out" whereas the
00082  * NSPR version uses "src" and "dest"; sometimes I changed one to the other
00083  * and sometimes I left them when I thought the subroutines were at least
00084  * self-consistent.
00085  */
00086 
00087 PR_BEGIN_EXTERN_C
00088 
00089 /*
00090  * Opaque object used by the encoder to store state.
00091  */
00092 struct PLBase64EncoderStr {
00093     /*
00094      * The one or two bytes pending.  (We need 3 to create a "token",
00095      * and hold the leftovers here.  in_buffer_count is *only* ever
00096      * 0, 1, or 2.
00097      */
00098     unsigned char in_buffer[2];
00099     int in_buffer_count;
00100 
00101     /*
00102      * If the caller wants linebreaks added, line_length specifies
00103      * where they come out.  It must be a multiple of 4; if the caller
00104      * provides one that isn't, we round it down to the nearest
00105      * multiple of 4.
00106      *
00107      * The value of current_column counts how many characters have been
00108      * added since the last linebreaks (or since the beginning, on the
00109      * first line).  It is also always a multiple of 4; it is unused when
00110      * line_length is 0.
00111      */ 
00112     PRUint32 line_length;
00113     PRUint32 current_column;
00114 
00115     /*
00116      * Where to write the encoded data (used when streaming, not when
00117      * doing all in-memory (buffer) operations).
00118      *
00119      * Note that this definition is chosen to be compatible with PR_Write.
00120      */
00121     PRInt32 (*output_fn) (void *output_arg, const char *buf, PRInt32 size);
00122     void *output_arg;
00123 
00124     /*
00125      * Where the encoded output goes -- either temporarily (in the streaming
00126      * case, staged here before it goes to the output function) or what will
00127      * be the entire buffered result for users of the buffer version.
00128      */
00129     char *output_buffer;
00130     PRUint32 output_buflen; /* the total length of allocated buffer */
00131     PRUint32 output_length; /* the length that is currently populated */
00132 };
00133 
00134 PR_END_EXTERN_C
00135 
00136 
00137 /*
00138  * Table to convert a binary value to its corresponding ascii "code".
00139  */
00140 static unsigned char base64_valuetocode[64] =
00141     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00142 
00143 #define B64_PAD      '='
00144 #define B64_CR       '\r'
00145 #define B64_LF       '\n'
00146 
00147 static PRStatus
00148 pl_base64_encode_buffer (PLBase64Encoder *data, const unsigned char *in,
00149                       PRUint32 size)
00150 {
00151     const unsigned char *end = in + size;
00152     char *out = data->output_buffer + data->output_length;
00153     unsigned int i = data->in_buffer_count;
00154     PRUint32 n = 0;
00155     int off;
00156     PRUint32 output_threshold;
00157 
00158     /* If this input buffer is too small, wait until next time. */
00159     if (size < (3 - i)) {
00160        data->in_buffer[i++] = in[0];
00161        if (size > 1)
00162            data->in_buffer[i++] = in[1];
00163        PR_ASSERT(i < 3);
00164        data->in_buffer_count = i;
00165        return PR_SUCCESS;
00166     }
00167 
00168     /* If there are bytes that were put back last time, take them now. */
00169     if (i > 0) {
00170        n = data->in_buffer[0];
00171        if (i > 1)
00172            n = (n << 8) | data->in_buffer[1];
00173        data->in_buffer_count = 0;
00174     }
00175 
00176     /* If our total is not a multiple of three, put one or two bytes back. */
00177     off = (size + i) % 3;
00178     if (off > 0) {
00179        size -= off;
00180        data->in_buffer[0] = in[size];
00181        if (off > 1)
00182            data->in_buffer[1] = in[size + 1];
00183        data->in_buffer_count = off;
00184        end -= off;
00185     }
00186 
00187     output_threshold = data->output_buflen - 3;
00188 
00189     /*
00190      * Populate the output buffer with base64 data, one line (or buffer)
00191      * at a time.
00192      */
00193     while (in < end) {
00194        int j, k;
00195 
00196        while (i < 3) {
00197            n = (n << 8) | *in++;
00198            i++;
00199        }
00200        i = 0;
00201 
00202        if (data->line_length > 0) {
00203            if (data->current_column >= data->line_length) {
00204               data->current_column = 0;
00205               *out++ = B64_CR;
00206               *out++ = B64_LF;
00207               data->output_length += 2;
00208            }
00209            data->current_column += 4;     /* the bytes we are about to add */
00210        }
00211 
00212        for (j = 18; j >= 0; j -= 6) {
00213            k = (n >> j) & 0x3F;
00214            *out++ = base64_valuetocode[k];
00215        }
00216        n = 0;
00217        data->output_length += 4;
00218 
00219        if (data->output_length >= output_threshold) {
00220            PR_ASSERT(data->output_length <= data->output_buflen);
00221            if (data->output_fn != NULL) {
00222               PRInt32 output_result;
00223 
00224               output_result = data->output_fn (data->output_arg,
00225                                            data->output_buffer,
00226                                            (PRInt32) data->output_length);
00227               if (output_result < 0)
00228                   return PR_FAILURE;
00229 
00230               out = data->output_buffer;
00231               data->output_length = 0;
00232            } else {
00233               /*
00234                * Check that we are about to exit the loop.  (Since we
00235                * are over the threshold, there isn't enough room in the
00236                * output buffer for another trip around.)
00237                */
00238               PR_ASSERT(in == end);
00239               if (in < end) {
00240                   PR_SetError (PR_BUFFER_OVERFLOW_ERROR, 0);
00241                   return PR_FAILURE;
00242               }
00243            }
00244        }
00245     }
00246 
00247     return PR_SUCCESS;
00248 }
00249 
00250 static PRStatus
00251 pl_base64_encode_flush (PLBase64Encoder *data)
00252 {
00253     int i = data->in_buffer_count;
00254 
00255     if (i == 0 && data->output_length == 0)
00256        return PR_SUCCESS;
00257 
00258     if (i > 0) {
00259        char *out = data->output_buffer + data->output_length;
00260        PRUint32 n;
00261        int j, k;
00262 
00263        n = ((PRUint32) data->in_buffer[0]) << 16;
00264        if (i > 1)
00265            n |= ((PRUint32) data->in_buffer[1] << 8);
00266 
00267        data->in_buffer_count = 0;
00268 
00269        if (data->line_length > 0) {
00270            if (data->current_column >= data->line_length) {
00271               data->current_column = 0;
00272               *out++ = B64_CR;
00273               *out++ = B64_LF;
00274               data->output_length += 2;
00275            }
00276        }
00277 
00278        /*
00279         * This will fill in more than we really have data for, but the
00280         * valid parts will end up in the correct position and the extras
00281         * will be over-written with pad characters below.
00282         */
00283        for (j = 18; j >= 0; j -= 6) {
00284            k = (n >> j) & 0x3F;
00285            *out++ = base64_valuetocode[k];
00286        }
00287 
00288        /* Pad with equal-signs. */
00289        if (i == 1)
00290            out[-2] = B64_PAD;
00291        out[-1] = B64_PAD;
00292 
00293        data->output_length += 4;
00294     }
00295 
00296     if (data->output_fn != NULL) {
00297        PRInt32 output_result;
00298 
00299        output_result = data->output_fn (data->output_arg, data->output_buffer,
00300                                     (PRInt32) data->output_length);
00301        data->output_length = 0;
00302 
00303        if (output_result < 0)
00304            return PR_FAILURE;
00305     }
00306 
00307     return PR_SUCCESS;
00308 }
00309 
00310 
00311 /*
00312  * The maximum space needed to hold the output of the encoder given input
00313  * data of length "size", and allowing for CRLF added at least every
00314  * line_length bytes (we will add it at nearest lower multiple of 4).
00315  * There is no trailing CRLF.
00316  */
00317 static PRUint32
00318 PL_Base64MaxEncodedLength (PRUint32 size, PRUint32 line_length)
00319 {
00320     PRUint32 tokens, tokens_per_line, full_lines, line_break_chars, remainder;
00321 
00322     tokens = (size + 2) / 3;
00323 
00324     if (line_length == 0)
00325        return tokens * 4;
00326 
00327     if (line_length < 4)    /* too small! */
00328        line_length = 4;
00329 
00330     tokens_per_line = line_length / 4;
00331     full_lines = tokens / tokens_per_line;
00332     remainder = (tokens - (full_lines * tokens_per_line)) * 4;
00333     line_break_chars = full_lines * 2;
00334     if (remainder == 0)
00335        line_break_chars -= 2;
00336 
00337     return (full_lines * tokens_per_line * 4) + line_break_chars + remainder;
00338 }
00339 
00340 
00341 /*
00342  * A distinct internal creation function for the buffer version to use.
00343  * (It does not want to specify an output_fn, and we want the normal
00344  * Create function to require that.)  All common initialization of the
00345  * encoding context should be done *here*.
00346  *
00347  * Save "line_length", rounded down to nearest multiple of 4 (if not
00348  * already even multiple).  Allocate output_buffer, if not provided --
00349  * based on given size if specified, otherwise based on line_length.
00350  */
00351 static PLBase64Encoder *
00352 pl_base64_create_encoder (PRUint32 line_length, char *output_buffer,
00353                        PRUint32 output_buflen)
00354 {
00355     PLBase64Encoder *data;
00356     PRUint32 line_tokens;
00357 
00358     data = PR_NEWZAP(PLBase64Encoder);
00359     if (data == NULL)
00360        return NULL;
00361 
00362     if (line_length > 0 && line_length < 4)      /* too small! */
00363        line_length = 4;
00364 
00365     line_tokens = line_length / 4;
00366     data->line_length = line_tokens * 4;
00367 
00368     if (output_buffer == NULL) {
00369        if (output_buflen == 0) {
00370            if (data->line_length > 0)     /* need to include room for CRLF */
00371               output_buflen = data->line_length + 2;
00372            else
00373               output_buflen = 64;         /* XXX what is a good size? */
00374        }
00375 
00376        output_buffer = (char *) PR_Malloc(output_buflen);
00377        if (output_buffer == NULL) {
00378            PR_Free(data);
00379            return NULL;
00380        }
00381     }
00382 
00383     data->output_buffer = output_buffer;
00384     data->output_buflen = output_buflen;
00385     return data;
00386 }
00387 
00388 /*
00389  * Function to start a base64 encoding context.
00390  * An "output_fn" is required; the "output_arg" parameter to that is optional.
00391  * If linebreaks in the encoded output are desired, "line_length" specifies
00392  * where to place them -- it will be rounded down to the nearest multiple of 4
00393  * (if it is not already an even multiple of 4).  If it is zero, no linebreaks
00394  * will be added.  (FYI, a linebreak is CRLF -- two characters.)
00395  */
00396 static PLBase64Encoder *
00397 PL_CreateBase64Encoder (PRInt32 (*output_fn) (void *, const char *, PRInt32),
00398                      void *output_arg, PRUint32 line_length)
00399 {
00400     PLBase64Encoder *data;
00401 
00402     if (output_fn == NULL) {
00403        PR_SetError (PR_INVALID_ARGUMENT_ERROR, 0);
00404        return NULL;
00405     }
00406 
00407     data = pl_base64_create_encoder (line_length, NULL, 0);
00408     if (data == NULL)
00409        return NULL;
00410 
00411     data->output_fn = output_fn;
00412     data->output_arg = output_arg;
00413 
00414     return data;
00415 }
00416 
00417 
00418 /*
00419  * Push data through the encoder, causing the output_fn (provided to Create)
00420  * to be called with the encoded data.
00421  */
00422 static PRStatus
00423 PL_UpdateBase64Encoder (PLBase64Encoder *data, const unsigned char *buffer,
00424                      PRUint32 size)
00425 {
00426     /* XXX Should we do argument checking only in debug build? */
00427     if (data == NULL || buffer == NULL || size == 0) {
00428        PR_SetError (PR_INVALID_ARGUMENT_ERROR, 0);
00429        return PR_FAILURE;
00430     }
00431 
00432     return pl_base64_encode_buffer (data, buffer, size);
00433 }
00434 
00435 
00436 /*
00437  * When you're done encoding, call this to free the data.  If "abort_p"
00438  * is false, then calling this may cause the output_fn to be called
00439  * one last time (as the last buffered data is flushed out).
00440  */
00441 static PRStatus
00442 PL_DestroyBase64Encoder (PLBase64Encoder *data, PRBool abort_p)
00443 {
00444     PRStatus status = PR_SUCCESS;
00445 
00446     /* XXX Should we do argument checking only in debug build? */
00447     if (data == NULL) {
00448        PR_SetError (PR_INVALID_ARGUMENT_ERROR, 0);
00449        return PR_FAILURE;
00450     }
00451 
00452     /* Flush out the last few buffered characters. */
00453     if (!abort_p)
00454        status = pl_base64_encode_flush (data);
00455 
00456     if (data->output_buffer != NULL)
00457        PR_Free(data->output_buffer);
00458     PR_Free(data);
00459 
00460     return status;
00461 }
00462 
00463 
00464 /*
00465  * Perform base64 encoding from an input buffer to an output buffer.
00466  * The output buffer can be provided (as "dest"); you can also pass in
00467  * a NULL and this function will allocate a buffer large enough for you,
00468  * and return it.  If you do provide the output buffer, you must also
00469  * provide the maximum length of that buffer (as "maxdestlen").
00470  * The actual encoded length of output will be returned to you in
00471  * "output_destlen".
00472  *
00473  * If linebreaks in the encoded output are desired, "line_length" specifies
00474  * where to place them -- it will be rounded down to the nearest multiple of 4
00475  * (if it is not already an even multiple of 4).  If it is zero, no linebreaks
00476  * will be added.  (FYI, a linebreak is CRLF -- two characters.)
00477  *
00478  * Return value is NULL on error, the output buffer (allocated or provided)
00479  * otherwise.
00480  */
00481 static char *
00482 PL_Base64EncodeBuffer (const unsigned char *src, PRUint32 srclen,
00483                      PRUint32 line_length, char *dest, PRUint32 maxdestlen,
00484                      PRUint32 *output_destlen)
00485 {
00486     PRUint32 need_length;
00487     PLBase64Encoder *data = NULL;
00488     PRStatus status;
00489 
00490     PR_ASSERT(srclen > 0);
00491     if (srclen == 0)
00492        return dest;
00493 
00494     /*
00495      * How much space could we possibly need for encoding this input?
00496      */
00497     need_length = PL_Base64MaxEncodedLength (srclen, line_length);
00498 
00499     /*
00500      * Make sure we have at least that much, if output buffer provided.
00501      */
00502     if (dest != NULL) {
00503        PR_ASSERT(maxdestlen >= need_length);
00504        if (maxdestlen < need_length) {
00505            PR_SetError(PR_BUFFER_OVERFLOW_ERROR, 0);
00506            return NULL;
00507        }
00508     } else {
00509        maxdestlen = need_length;
00510     }
00511 
00512     data = pl_base64_create_encoder(line_length, dest, maxdestlen);
00513     if (data == NULL)
00514        return NULL;
00515 
00516     status = pl_base64_encode_buffer (data, src, srclen);
00517 
00518     /*
00519      * We do not wait for Destroy to flush, because Destroy will also
00520      * get rid of our encoder context, which we need to look at first!
00521      */
00522     if (status == PR_SUCCESS)
00523        status = pl_base64_encode_flush (data);
00524 
00525     if (status != PR_SUCCESS) {
00526        (void) PL_DestroyBase64Encoder (data, PR_TRUE);
00527        return NULL;
00528     }
00529 
00530     dest = data->output_buffer;
00531 
00532     /* Must clear this or Destroy will free it. */
00533     data->output_buffer = NULL;
00534 
00535     *output_destlen = data->output_length;
00536     status = PL_DestroyBase64Encoder (data, PR_FALSE);
00537     if (status == PR_FAILURE) {
00538        PR_Free(dest);
00539        return NULL;
00540     }
00541 
00542     return dest;
00543 }
00544 
00545 /*
00546  * XXX End of base64 encoding code to be moved into NSPR.
00547  ********************************************************
00548  */
00549 
00550 /*
00551  * This is the beginning of the NSS cover functions.  These will
00552  * provide the interface we want to expose as NSS-ish.  For example,
00553  * they will operate on our Items, do any special handling or checking
00554  * we want to do, etc.
00555  */
00556 
00557 
00558 PR_BEGIN_EXTERN_C
00559 
00560 /*
00561  * A boring cover structure for now.  Perhaps someday it will include
00562  * some more interesting fields.
00563  */
00564 struct NSSBase64EncoderStr {
00565     PLBase64Encoder *pl_data;
00566 };
00567 
00568 PR_END_EXTERN_C
00569 
00570 
00571 /*
00572  * Function to start a base64 encoding context.
00573  */
00574 NSSBase64Encoder *
00575 NSSBase64Encoder_Create (PRInt32 (*output_fn) (void *, const char *, PRInt32),
00576                       void *output_arg)
00577 {
00578     PLBase64Encoder *pl_data;
00579     NSSBase64Encoder *nss_data;
00580 
00581     nss_data = PORT_ZNew(NSSBase64Encoder);
00582     if (nss_data == NULL)
00583        return NULL;
00584 
00585     pl_data = PL_CreateBase64Encoder (output_fn, output_arg, 64);
00586     if (pl_data == NULL) {
00587        PORT_Free(nss_data);
00588        return NULL;
00589     }
00590 
00591     nss_data->pl_data = pl_data;
00592     return nss_data;
00593 }
00594 
00595 
00596 /*
00597  * Push data through the encoder, causing the output_fn (provided to Create)
00598  * to be called with the encoded data.
00599  */
00600 SECStatus
00601 NSSBase64Encoder_Update (NSSBase64Encoder *data, const unsigned char *buffer,
00602                       PRUint32 size)
00603 {
00604     PRStatus pr_status;
00605 
00606     /* XXX Should we do argument checking only in debug build? */
00607     if (data == NULL) {
00608        PORT_SetError (SEC_ERROR_INVALID_ARGS);
00609        return SECFailure;
00610     }
00611 
00612     pr_status = PL_UpdateBase64Encoder (data->pl_data, buffer, size);
00613     if (pr_status == PR_FAILURE)
00614        return SECFailure;
00615 
00616     return SECSuccess;
00617 }
00618 
00619 
00620 /*
00621  * When you're done encoding, call this to free the data.  If "abort_p"
00622  * is false, then calling this may cause the output_fn to be called
00623  * one last time (as the last buffered data is flushed out).
00624  */
00625 SECStatus
00626 NSSBase64Encoder_Destroy (NSSBase64Encoder *data, PRBool abort_p)
00627 {
00628     PRStatus pr_status;
00629 
00630     /* XXX Should we do argument checking only in debug build? */
00631     if (data == NULL) {
00632        PORT_SetError (SEC_ERROR_INVALID_ARGS);
00633        return SECFailure;
00634     }
00635 
00636     pr_status = PL_DestroyBase64Encoder (data->pl_data, abort_p);
00637 
00638     PORT_Free(data);
00639 
00640     if (pr_status == PR_FAILURE)
00641        return SECFailure;
00642 
00643     return SECSuccess;
00644 }
00645 
00646 
00647 /*
00648  * Perform base64 encoding of binary data "inItem" to an ascii string.
00649  * The output buffer may be provided (as "outStrOpt"); you can also pass
00650  * in a NULL and the buffer will be allocated for you.  The result will
00651  * be null-terminated, and if the buffer is provided, "maxOutLen" must
00652  * specify the maximum length of the buffer and will be checked to
00653  * supply sufficient space space for the encoded result.  (If "outStrOpt"
00654  * is NULL, "maxOutLen" is ignored.)
00655  *
00656  * If "outStrOpt" is NULL, allocation will happen out of the passed-in
00657  * "arenaOpt", if *it* is non-NULL, otherwise standard allocation (heap)
00658  * will be used.
00659  *
00660  * Return value is NULL on error, the output buffer (allocated or provided)
00661  * otherwise.
00662  */
00663 char *
00664 NSSBase64_EncodeItem (PRArenaPool *arenaOpt, char *outStrOpt,
00665                     unsigned int maxOutLen, SECItem *inItem)
00666 {
00667     char *out_string = outStrOpt;
00668     PRUint32 max_out_len;
00669     PRUint32 out_len;
00670     void *mark = NULL;
00671     char *dummy;
00672 
00673     PORT_Assert(inItem != NULL && inItem->data != NULL && inItem->len != 0);
00674     if (inItem == NULL || inItem->data == NULL || inItem->len == 0) {
00675        PORT_SetError (SEC_ERROR_INVALID_ARGS);
00676        return NULL;
00677     }
00678 
00679     max_out_len = PL_Base64MaxEncodedLength (inItem->len, 64);
00680 
00681     if (arenaOpt != NULL)
00682        mark = PORT_ArenaMark (arenaOpt);
00683 
00684     if (out_string == NULL) {
00685        if (arenaOpt != NULL)
00686            out_string = PORT_ArenaAlloc (arenaOpt, max_out_len + 1);
00687        else
00688            out_string = PORT_Alloc (max_out_len + 1);
00689 
00690        if (out_string == NULL) {
00691            if (arenaOpt != NULL)
00692               PORT_ArenaRelease (arenaOpt, mark);
00693            return NULL;
00694        }
00695     } else {
00696        if ((max_out_len + 1) > maxOutLen) {
00697            PORT_SetError (SEC_ERROR_OUTPUT_LEN);
00698            return NULL;
00699        }
00700        max_out_len = maxOutLen;
00701     }
00702 
00703 
00704     dummy = PL_Base64EncodeBuffer (inItem->data, inItem->len, 64,
00705                                out_string, max_out_len, &out_len);
00706     if (dummy == NULL) {
00707        if (arenaOpt != NULL) {
00708            PORT_ArenaRelease (arenaOpt, mark);
00709        } else {
00710            PORT_Free (out_string);
00711        }
00712        return NULL;
00713     }
00714 
00715     if (arenaOpt != NULL)
00716        PORT_ArenaUnmark (arenaOpt, mark);
00717 
00718     out_string[out_len] = '\0';
00719     return out_string;
00720 }
00721 
00722 
00723 /*
00724  * XXX Everything below is deprecated.  If you add new stuff, put it
00725  * *above*, not below.
00726  */
00727 
00728 /*
00729  * XXX The following "BTOA" functions are provided for backward compatibility
00730  * with current code.  They should be considered strongly deprecated.
00731  * When we can convert all our code over to using the new NSSBase64Encoder_
00732  * functions defined above, we should get rid of these altogether.  (Remove
00733  * protoypes from base64.h as well -- actually, remove that file completely).
00734  * If someone thinks either of these functions provides such a very useful
00735  * interface (though, as shown, the same functionality can already be
00736  * obtained by calling NSSBase64_EncodeItem directly), fine -- but then
00737  * that API should be provided with a nice new NSSFoo name and using
00738  * appropriate types, etc.
00739  */
00740 
00741 #include "base64.h"
00742 
00743 /*
00744 ** Return an PORT_Alloc'd ascii string which is the base64 encoded
00745 ** version of the input string.
00746 */
00747 char *
00748 BTOA_DataToAscii(const unsigned char *data, unsigned int len)
00749 {
00750     SECItem binary_item;
00751 
00752     binary_item.data = (unsigned char *)data;
00753     binary_item.len = len;
00754 
00755     return NSSBase64_EncodeItem (NULL, NULL, 0, &binary_item);
00756 }
00757 
00758 /*
00759 ** Convert from binary encoding of an item to ascii.
00760 */
00761 char *
00762 BTOA_ConvertItemToAscii (SECItem *binary_item)
00763 {
00764     return NSSBase64_EncodeItem (NULL, NULL, 0, binary_item);
00765 }