Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Typedefs | Functions | Variables
nssb64d.c File Reference
#include "nssb64.h"
#include "nspr.h"
#include "secitem.h"
#include "secerr.h"
#include "base64.h"

Go to the source code of this file.

Classes

struct  PLBase64DecoderStr
struct  NSSBase64DecoderStr

Defines

#define B64_PAD   '='

Typedefs

typedef struct PLBase64DecoderStr

Functions

static int pl_base64_decode_4to3 (const unsigned char *in, unsigned char *out)
static int pl_base64_decode_3to2 (const unsigned char *in, unsigned char *out)
static int pl_base64_decode_2to1 (const unsigned char *in, unsigned char *out)
static int pl_base64_decode_token (const unsigned char *in, unsigned char *out)
static PRStatus pl_base64_decode_buffer (PLBase64Decoder *data, const unsigned char *in, PRUint32 length)
static PRStatus pl_base64_decode_flush (PLBase64Decoder *data)
static PRUint32 PL_Base64MaxDecodedLength (PRUint32 size)
static PLBase64Decoder * pl_base64_create_decoder (void)
static PLBase64Decoder * PL_CreateBase64Decoder (PRInt32(*output_fn)(void *, const unsigned char *, PRInt32), void *output_arg)
static PRStatus PL_UpdateBase64Decoder (PLBase64Decoder *data, const char *buffer, PRUint32 size)
static PRStatus PL_DestroyBase64Decoder (PLBase64Decoder *data, PRBool abort_p)
static unsigned char * PL_Base64DecodeBuffer (const char *src, PRUint32 srclen, unsigned char *dest, PRUint32 maxdestlen, PRUint32 *output_destlen)
PR_END_EXTERN_C NSSBase64Decoder * NSSBase64Decoder_Create (PRInt32(*output_fn)(void *, const unsigned char *, PRInt32), void *output_arg)
SECStatus NSSBase64Decoder_Update (NSSBase64Decoder *data, const char *buffer, PRUint32 size)
SECStatus NSSBase64Decoder_Destroy (NSSBase64Decoder *data, PRBool abort_p)
SECItem * NSSBase64_DecodeBuffer (PRArenaPool *arenaOpt, SECItem *outItemOpt, const char *inStr, unsigned int inLen)
unsigned char * ATOB_AsciiToData (const char *string, unsigned int *lenp)
SECStatus ATOB_ConvertAsciiToItem (SECItem *binary_item, char *ascii)

Variables

static PR_END_EXTERN_C
unsigned char 
base64_codetovaluep1 [256]

Class Documentation

struct NSSBase64DecoderStr

Definition at line 649 of file nssb64d.c.

Class Members
PLBase64Decoder * pl_data

Define Documentation

#define B64_PAD   '='

Definition at line 158 of file nssb64d.c.


Typedef Documentation

typedef struct PLBase64DecoderStr

Definition at line 78 of file nssb64d.c.


Function Documentation

unsigned char* ATOB_AsciiToData ( const char *  string,
unsigned int lenp 
)

Definition at line 816 of file nssb64d.c.

{
    SECItem binary_item, *dummy;

    binary_item.data = NULL;
    binary_item.len = 0;

    dummy = NSSBase64_DecodeBuffer (NULL, &binary_item, string,
                                (PRUint32) PORT_Strlen(string));
    if (dummy == NULL)
       return NULL;

    PORT_Assert(dummy == &binary_item);

    *lenp = dummy->len;
    return dummy->data;
}
SECStatus ATOB_ConvertAsciiToItem ( SECItem *  binary_item,
char *  ascii 
)

Definition at line 838 of file nssb64d.c.

{
    SECItem *dummy;

    if (binary_item == NULL) {
       PORT_SetError (SEC_ERROR_INVALID_ARGS);
       return SECFailure;
    }

    /*
     * XXX Would prefer to assert here if data is non-null (actually,
     * don't need to, just let NSSBase64_DecodeBuffer do it), so as to
     * to catch unintended memory leaks, but callers are not clean in
     * this respect so we need to explicitly clear here to avoid the
     * assert in NSSBase64_DecodeBuffer.
     */
    binary_item->data = NULL;
    binary_item->len = 0;

    dummy = NSSBase64_DecodeBuffer (NULL, binary_item, ascii,
                                (PRUint32) PORT_Strlen(ascii));

    if (dummy == NULL)
       return SECFailure;

    return SECSuccess;
}
SECItem* NSSBase64_DecodeBuffer ( PRArenaPool arenaOpt,
SECItem *  outItemOpt,
const char *  inStr,
unsigned int  inLen 
)

Definition at line 747 of file nssb64d.c.

{
    SECItem *out_item = outItemOpt;
    PRUint32 max_out_len = PL_Base64MaxDecodedLength (inLen);
    PRUint32 out_len;
    void *mark = NULL;
    unsigned char *dummy;

    PORT_Assert(outItemOpt == NULL || outItemOpt->data == NULL);

    if (arenaOpt != NULL)
       mark = PORT_ArenaMark (arenaOpt);

    out_item = SECITEM_AllocItem (arenaOpt, outItemOpt, max_out_len);
    if (out_item == NULL) {
       if (arenaOpt != NULL)
           PORT_ArenaRelease (arenaOpt, mark);
       return NULL;
    }

    dummy = PL_Base64DecodeBuffer (inStr, inLen, out_item->data,
                               max_out_len, &out_len);
    if (dummy == NULL) {
       if (arenaOpt != NULL) {
           PORT_ArenaRelease (arenaOpt, mark);
           if (outItemOpt != NULL) {
              outItemOpt->data = NULL;
              outItemOpt->len = 0;
           }
       } else {
           SECITEM_FreeItem (out_item,
                           (outItemOpt == NULL) ? PR_TRUE : PR_FALSE);
       }
       return NULL;
    }

    if (arenaOpt != NULL)
       PORT_ArenaUnmark (arenaOpt, mark);
    out_item->len = out_len;
    return out_item;
}
PR_END_EXTERN_C NSSBase64Decoder* NSSBase64Decoder_Create ( PRInt32(*)(void *, const unsigned char *, PRInt32 output_fn,
void output_arg 
)

Definition at line 660 of file nssb64d.c.

{
    PLBase64Decoder *pl_data;
    NSSBase64Decoder *nss_data;

    nss_data = PORT_ZNew(NSSBase64Decoder);
    if (nss_data == NULL)
       return NULL;

    pl_data = PL_CreateBase64Decoder (output_fn, output_arg);
    if (pl_data == NULL) {
       PORT_Free(nss_data);
       return NULL;
    }

    nss_data->pl_data = pl_data;
    return nss_data;
}
SECStatus NSSBase64Decoder_Destroy ( NSSBase64Decoder *  data,
PRBool  abort_p 
)

Definition at line 712 of file nssb64d.c.

{
    PRStatus pr_status;

    /* XXX Should we do argument checking only in debug build? */
    if (data == NULL) {
       PORT_SetError (SEC_ERROR_INVALID_ARGS);
       return SECFailure;
    }

    pr_status = PL_DestroyBase64Decoder (data->pl_data, abort_p);

    PORT_Free(data);

    if (pr_status == PR_FAILURE)
       return SECFailure;

    return SECSuccess;
}
SECStatus NSSBase64Decoder_Update ( NSSBase64Decoder *  data,
const char *  buffer,
PRUint32  size 
)

Definition at line 687 of file nssb64d.c.

{
    PRStatus pr_status;

    /* XXX Should we do argument checking only in debug build? */
    if (data == NULL) {
       PORT_SetError (SEC_ERROR_INVALID_ARGS);
       return SECFailure;
    }

    pr_status = PL_UpdateBase64Decoder (data->pl_data, buffer, size);
    if (pr_status == PR_FAILURE)
       return SECFailure;

    return SECSuccess;
}
static PLBase64Decoder* pl_base64_create_decoder ( void  ) [static]

Definition at line 421 of file nssb64d.c.

{
    return PR_NEWZAP(PLBase64Decoder);
}

Here is the caller graph for this function:

static int pl_base64_decode_2to1 ( const unsigned char *  in,
unsigned char *  out 
) [static]

Definition at line 218 of file nssb64d.c.

{
    PRUint32 num = 0;
    unsigned char bits1, bits2;

    bits1 = base64_codetovaluep1[in[0]];
    bits2 = base64_codetovaluep1[in[1]];

    if ((bits1 == 0) || (bits2 == 0))
       return -1;

    num = ((PRUint32)(bits1 - 1)) << 2;
    num |= ((PRUint32)(bits2 - 1)) >> 4;

    out[0] = (unsigned char) num;

    return 1;
}

Here is the caller graph for this function:

static int pl_base64_decode_3to2 ( const unsigned char *  in,
unsigned char *  out 
) [static]

Definition at line 191 of file nssb64d.c.

{
    PRUint32 num = 0;
    unsigned char bits1, bits2, bits3;

    bits1 = base64_codetovaluep1[in[0]];
    bits2 = base64_codetovaluep1[in[1]];
    bits3 = base64_codetovaluep1[in[2]];

    if ((bits1 == 0) || (bits2 == 0) || (bits3 == 0))
       return -1;

    num = ((PRUint32)(bits1 - 1)) << 10;
    num |= ((PRUint32)(bits2 - 1)) << 4;
    num |= ((PRUint32)(bits3 - 1)) >> 2;

    out[0] = (unsigned char) (num >> 8);
    out[1] = (unsigned char) (num & 0xFF);

    return 2;
}

Here is the caller graph for this function:

static int pl_base64_decode_4to3 ( const unsigned char *  in,
unsigned char *  out 
) [static]

Definition at line 166 of file nssb64d.c.

{
    int j;
    PRUint32 num = 0;
    unsigned char bits;

    for (j = 0; j < 4; j++) {
       bits = base64_codetovaluep1[in[j]];
       if (bits == 0)
           return -1;
       num = (num << 6) | (bits - 1);
    }

    out[0] = (unsigned char) (num >> 16);
    out[1] = (unsigned char) ((num >> 8) & 0xFF);
    out[2] = (unsigned char) (num & 0xFF);

    return 3;
}

Here is the caller graph for this function:

static PRStatus pl_base64_decode_buffer ( PLBase64Decoder *  data,
const unsigned char *  in,
PRUint32  length 
) [static]

Definition at line 254 of file nssb64d.c.

{
    unsigned char *out = data->output_buffer;
    unsigned char *token = data->token;
    int i, n = 0;

    i = data->token_size;
    data->token_size = 0;

    while (length > 0) {
       while (i < 4 && length > 0) {
           /*
            * XXX Note that the following simply ignores any unexpected
            * characters.  This is exactly what the original code in
            * libmime did, and I am leaving it.  We certainly want to skip
            * over whitespace (we must); this does much more than that.
            * I am not confident changing it, and I don't want to slow
            * the processing down doing more complicated checking, but
            * someone else might have different ideas in the future.
            */
           if (base64_codetovaluep1[*in] > 0 || *in == B64_PAD)
              token[i++] = *in;
           in++;
           length--;
       }

       if (i < 4) {
           /* Didn't get enough for a complete token. */
           data->token_size = i;
           break;
       }
       i = 0;

       PR_ASSERT((out - data->output_buffer + 3) <= data->output_buflen);

       /*
        * Assume we are not at the end; the following function only works
        * for an internal token (no trailing padding characters) but is
        * faster that way.  If it hits an invalid character (padding) it
        * will return an error; we break out of the loop and try again
        * calling the routine that will handle a final token.
        * Note that we intentionally do it this way rather than explicitly
        * add a check for padding here (because that would just slow down
        * the normal case) nor do we rely on checking whether we have more
        * input to process (because that would also slow it down but also
        * because we want to allow trailing garbage, especially white space
        * and cannot tell that without read-ahead, also a slow proposition).
        * Whew.  Understand?
        */
       n = pl_base64_decode_4to3 (token, out);
       if (n < 0)
           break;

       /* Advance "out" by the number of bytes just written to it. */
       out += n;
       n = 0;
    }

    /*
     * See big comment above, before call to pl_base64_decode_4to3.
     * Here we check if we error'd out of loop, and allow for the case
     * that we are processing the last interesting token.  If the routine
     * which should handle padding characters also fails, then we just
     * have bad input and give up.
     */
    if (n < 0) {
       n = pl_base64_decode_token (token, out);
       if (n < 0)
           return PR_FAILURE;

       out += n;
    }

    /*
     * As explained above, we can get here with more input remaining, but
     * it should be all characters we do not care about (i.e. would be
     * ignored when transferring from "in" to "token" in loop above,
     * except here we choose to ignore extraneous pad characters, too).
     * Swallow it, performing that check.  If we find more characters that
     * we would expect to decode, something is wrong.
     */
    while (length > 0) {
       if (base64_codetovaluep1[*in] > 0)
           return PR_FAILURE;
       in++;
       length--;
    }

    /* Record the length of decoded data we have left in output_buffer. */
    data->output_length = (PRUint32) (out - data->output_buffer);
    return PR_SUCCESS;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PRStatus pl_base64_decode_flush ( PLBase64Decoder *  data) [static]

Definition at line 355 of file nssb64d.c.

{
    int count;

    /*
     * If no remaining characters, or all are padding (also not well-formed
     * input, but again, be tolerant), then nothing more to do.  (And, that
     * is considered successful.)
     */
    if (data->token_size == 0 || data->token[0] == B64_PAD)
       return PR_SUCCESS;

    /*
     * Assume we have all the interesting input except for some expected
     * padding characters.  Add them and decode the resulting token.
     */
    while (data->token_size < 4)
       data->token[data->token_size++] = B64_PAD;

    data->token_size = 0;   /* so a subsequent flush call is a no-op */

    count = pl_base64_decode_token (data->token,
                                data->output_buffer + data->output_length);
    if (count < 0)
       return PR_FAILURE;

    /*
     * If there is an output function, call it with this last bit of data.
     * Otherwise we are doing all buffered output, and the decoded bytes
     * are now there, we just need to reflect that in the length.
     */
    if (data->output_fn != NULL) {
       PRInt32 output_result;

       PR_ASSERT(data->output_length == 0);
       output_result = data->output_fn (data->output_arg,
                                    data->output_buffer,
                                    (PRInt32) count);
       if (output_result < 0)
           return  PR_FAILURE;
    } else {
       data->output_length += count;
    }

    return PR_SUCCESS;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int pl_base64_decode_token ( const unsigned char *  in,
unsigned char *  out 
) [static]

Definition at line 242 of file nssb64d.c.

{
    if (in[3] != B64_PAD)
       return pl_base64_decode_4to3 (in, out);

    if (in[2] == B64_PAD)
       return pl_base64_decode_2to1 (in, out);

    return pl_base64_decode_3to2 (in, out);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static unsigned char* PL_Base64DecodeBuffer ( const char *  src,
PRUint32  srclen,
unsigned char *  dest,
PRUint32  maxdestlen,
PRUint32 output_destlen 
) [static]

Definition at line 557 of file nssb64d.c.

{
    PRUint32 need_length;
    unsigned char *output_buffer = NULL;
    PLBase64Decoder *data = NULL;
    PRStatus status;

    PR_ASSERT(srclen > 0);
    if (srclen == 0)
       return dest;

    /*
     * How much space could we possibly need for decoding this input?
     */
    need_length = PL_Base64MaxDecodedLength (srclen);

    /*
     * Make sure we have at least that much, if output buffer provided.
     * If no output buffer provided, then we allocate that much.
     */
    if (dest != NULL) {
       PR_ASSERT(maxdestlen >= need_length);
       if (maxdestlen < need_length) {
           PR_SetError(PR_BUFFER_OVERFLOW_ERROR, 0);
           goto loser;
       }
       output_buffer = dest;
    } else {
       output_buffer = (unsigned char *) PR_Malloc(need_length);
       if (output_buffer == NULL)
           goto loser;
       maxdestlen = need_length;
    }

    data = pl_base64_create_decoder();
    if (data == NULL)
       goto loser;

    data->output_buflen = maxdestlen;
    data->output_buffer = output_buffer;

    status = pl_base64_decode_buffer (data, (const unsigned char *) src,
                                  srclen);

    /*
     * We do not wait for Destroy to flush, because Destroy will also
     * get rid of our decoder context, which we need to look at first!
     */
    if (status == PR_SUCCESS)
       status = pl_base64_decode_flush (data);

    /* Must clear this or Destroy will free it. */
    data->output_buffer = NULL;

    if (status == PR_SUCCESS) {
       *output_destlen = data->output_length;
       status = PL_DestroyBase64Decoder (data, PR_FALSE);
       data = NULL;
       if (status == PR_FAILURE)
           goto loser;
       return output_buffer;
    }

loser:
    if (dest == NULL && output_buffer != NULL)
       PR_Free(output_buffer);
    if (data != NULL)
       (void) PL_DestroyBase64Decoder (data, PR_TRUE);
    return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PRUint32 PL_Base64MaxDecodedLength ( PRUint32  size) [static]

Definition at line 408 of file nssb64d.c.

{
    return ((size * 3) / 4);
}

Here is the caller graph for this function:

static PLBase64Decoder* PL_CreateBase64Decoder ( PRInt32(*)(void *, const unsigned char *, PRInt32 output_fn,
void output_arg 
) [static]

Definition at line 431 of file nssb64d.c.

{
    PLBase64Decoder *data;

    if (output_fn == NULL) {
       PR_SetError (PR_INVALID_ARGUMENT_ERROR, 0);
       return NULL;
    }

    data = pl_base64_create_decoder ();
    if (data != NULL) {
       data->output_fn = output_fn;
       data->output_arg = output_arg;
    }
    return data;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PRStatus PL_DestroyBase64Decoder ( PLBase64Decoder *  data,
PRBool  abort_p 
) [static]

Definition at line 522 of file nssb64d.c.

{
    PRStatus status = PR_SUCCESS;

    /* XXX Should we do argument checking only in debug build? */
    if (data == NULL) {
       PR_SetError (PR_INVALID_ARGUMENT_ERROR, 0);
       return PR_FAILURE;
    }

    /* Flush out the last few buffered characters. */
    if (!abort_p)
       status = pl_base64_decode_flush (data);

    if (data->output_buffer != NULL)
       PR_Free(data->output_buffer);
    PR_Free(data);

    return status;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PRStatus PL_UpdateBase64Decoder ( PLBase64Decoder *  data,
const char *  buffer,
PRUint32  size 
) [static]

Definition at line 456 of file nssb64d.c.

{
    PRUint32 need_length;
    PRStatus status;

    /* XXX Should we do argument checking only in debug build? */
    if (data == NULL || buffer == NULL || size == 0) {
       PR_SetError (PR_INVALID_ARGUMENT_ERROR, 0);
       return PR_FAILURE;
    }

    /*
     * How much space could this update need for decoding?
     */
    need_length = PL_Base64MaxDecodedLength (size + data->token_size);

    /*
     * Make sure we have at least that much.  If not, (re-)allocate.
     */
    if (need_length > data->output_buflen) {
       unsigned char *output_buffer = data->output_buffer;

       if (output_buffer != NULL)
           output_buffer = (unsigned char *) PR_Realloc(output_buffer,
                                                  need_length);
       else
           output_buffer = (unsigned char *) PR_Malloc(need_length);

       if (output_buffer == NULL)
           return PR_FAILURE;

       data->output_buffer = output_buffer;
       data->output_buflen = need_length;
    }

    /* There should not have been any leftover output data in the buffer. */
    PR_ASSERT(data->output_length == 0);
    data->output_length = 0;

    status = pl_base64_decode_buffer (data, (const unsigned char *) buffer,
                                  size);

    /* Now that we have some decoded data, write it. */
    if (status == PR_SUCCESS && data->output_length > 0) {
       PRInt32 output_result;

       PR_ASSERT(data->output_fn != NULL);
       output_result = data->output_fn (data->output_arg,
                                    data->output_buffer,
                                    (PRInt32) data->output_length);
       if (output_result < 0)
           status = PR_FAILURE;
    }

    data->output_length = 0;
    return status;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

PR_END_EXTERN_C unsigned char base64_codetovaluep1[256] [static]
Initial value:
 {
         0,     0,     0,     0,     0,     0,     0,     0,
         0,     0,     0,     0,     0,     0,     0,     0,
         0,     0,     0,     0,     0,     0,     0,     0,
         0,     0,     0,     0,     0,     0,     0,     0,
         0,     0,     0,     0,     0,     0,     0,     0,
         0,     0,     0,    63,     0,     0,     0,    64,
        53,    54,    55,    56,    57,    58,    59,    60,
        61,    62,     0,     0,     0,     0,     0,     0,
         0,     1,     2,     3,     4,     5,     6,     7,
         8,     9,    10,    11,    12,    13,    14,    15,
        16,    17,    18,    19,    20,    21,    22,    23,
        24,    25,    26,     0,     0,     0,     0,     0,
         0,    27,    28,    29,    30,    31,    32,    33,
        34,    35,    36,    37,    38,    39,    40,    41,
        42,    43,    44,    45,    46,    47,    48,    49,
        50,    51,    52,     0,     0,     0,     0,     0,
         0,     0,     0,     0,     0,     0,     0,     0

}

Definition at line 137 of file nssb64d.c.