nsprpub/lib/libc/include/plbase64.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/lib/libc/include/plbase64.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,76 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef _plbase64_h
    1.10 +#define _plbase64_h
    1.11 +
    1.12 +#include "prtypes.h"
    1.13 +
    1.14 +PR_BEGIN_EXTERN_C
    1.15 +
    1.16 +/*
    1.17 + * PL_Base64Encode
    1.18 + *
    1.19 + * This routine encodes the data pointed to by the "src" parameter using the
    1.20 + * base64 algorithm, and returns a pointer to the result.  If the "srclen"
    1.21 + * parameter is not zero, it specifies the length of the source data.  If it
    1.22 + * is zero, the source data is assumed to be null-terminated, and PL_strlen
    1.23 + * is used to determine the source length.  If the "dest" parameter is not
    1.24 + * null, it is assumed to point to a buffer of sufficient size (which may be
    1.25 + * calculated: ((srclen + 2)/3)*4) into which the encoded data is placed 
    1.26 + * (without any termination).  If the "dest" parameter is null, a buffer is
    1.27 + * allocated from the heap to hold the encoded data, and the result *will*
    1.28 + * be terminated with an extra null character.  It is the caller's 
    1.29 + * responsibility to free the result when it is allocated.  A null is returned 
    1.30 + * if the allocation fails.
    1.31 + *
    1.32 + * NOTE: when calculating ((srclen + 2)/3)*4), first ensure that
    1.33 + *     srclen <= (PR_UINT32_MAX/4) * 3
    1.34 + * to avoid PRUint32 overflow.
    1.35 + */
    1.36 +
    1.37 +PR_EXTERN(char *)
    1.38 +PL_Base64Encode
    1.39 +(
    1.40 +    const char *src,
    1.41 +    PRUint32    srclen,
    1.42 +    char       *dest
    1.43 +);
    1.44 +
    1.45 +/*
    1.46 + * PL_Base64Decode
    1.47 + *
    1.48 + * This routine decodes the data pointed to by the "src" parameter using
    1.49 + * the base64 algorithm, and returns a pointer to the result.  The source
    1.50 + * may either include or exclude any trailing '=' characters.  If the
    1.51 + * "srclen" parameter is not zero, it specifies the length of the source
    1.52 + * data.  If it is zero, PL_strlen will be used to determine the source
    1.53 + * length.  If the "dest" parameter is not null, it is assumed to point to
    1.54 + * a buffer of sufficient size (which may be calculated: (srclen * 3)/4
    1.55 + * when srclen includes the '=' characters) into which the decoded data
    1.56 + * is placed (without any termination).  If the "dest" parameter is null,
    1.57 + * a buffer is allocated from the heap to hold the decoded data, and the
    1.58 + * result *will* be terminated with an extra null character.  It is the
    1.59 + * caller's responsibility to free the result when it is allocated.  A null
    1.60 + * is retuned if the allocation fails, or if the source is not well-coded.
    1.61 + *
    1.62 + * NOTE: when calculating (srclen * 3)/4, first ensure that 
    1.63 + *     srclen <= PR_UINT32_MAX/3
    1.64 + * to avoid PRUint32 overflow.  Alternatively, calculate
    1.65 + *     (srclen/4) * 3 + ((srclen%4) * 3)/4
    1.66 + * which is equivalent but doesn't overflow for any value of srclen.
    1.67 + */
    1.68 +
    1.69 +PR_EXTERN(char *)
    1.70 +PL_Base64Decode
    1.71 +(
    1.72 +    const char *src,
    1.73 +    PRUint32    srclen,
    1.74 +    char       *dest
    1.75 +);
    1.76 +
    1.77 +PR_END_EXTERN_C
    1.78 +
    1.79 +#endif /* _plbase64_h */

mercurial