andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 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: #ifndef _plbase64_h andre@0: #define _plbase64_h andre@0: andre@0: #include "prtypes.h" andre@0: andre@0: PR_BEGIN_EXTERN_C andre@0: andre@0: /* andre@0: * PL_Base64Encode andre@0: * andre@0: * This routine encodes the data pointed to by the "src" parameter using the andre@0: * base64 algorithm, and returns a pointer to the result. If the "srclen" andre@0: * parameter is not zero, it specifies the length of the source data. If it andre@0: * is zero, the source data is assumed to be null-terminated, and PL_strlen andre@0: * is used to determine the source length. If the "dest" parameter is not andre@0: * null, it is assumed to point to a buffer of sufficient size (which may be andre@0: * calculated: ((srclen + 2)/3)*4) into which the encoded data is placed andre@0: * (without any termination). If the "dest" parameter is null, a buffer is andre@0: * allocated from the heap to hold the encoded data, and the result *will* andre@0: * be terminated with an extra null character. It is the caller's andre@0: * responsibility to free the result when it is allocated. A null is returned andre@0: * if the allocation fails. andre@0: * andre@0: * NOTE: when calculating ((srclen + 2)/3)*4), first ensure that andre@0: * srclen <= (PR_UINT32_MAX/4) * 3 andre@0: * to avoid PRUint32 overflow. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_Base64Encode andre@0: ( andre@0: const char *src, andre@0: PRUint32 srclen, andre@0: char *dest andre@0: ); andre@0: andre@0: /* andre@0: * PL_Base64Decode andre@0: * andre@0: * This routine decodes the data pointed to by the "src" parameter using andre@0: * the base64 algorithm, and returns a pointer to the result. The source andre@0: * may either include or exclude any trailing '=' characters. If the andre@0: * "srclen" parameter is not zero, it specifies the length of the source andre@0: * data. If it is zero, PL_strlen will be used to determine the source andre@0: * length. If the "dest" parameter is not null, it is assumed to point to andre@0: * a buffer of sufficient size (which may be calculated: (srclen * 3)/4 andre@0: * when srclen includes the '=' characters) into which the decoded data andre@0: * is placed (without any termination). If the "dest" parameter is null, andre@0: * a buffer is allocated from the heap to hold the decoded data, and the andre@0: * result *will* be terminated with an extra null character. It is the andre@0: * caller's responsibility to free the result when it is allocated. A null andre@0: * is retuned if the allocation fails, or if the source is not well-coded. andre@0: * andre@0: * NOTE: when calculating (srclen * 3)/4, first ensure that andre@0: * srclen <= PR_UINT32_MAX/3 andre@0: * to avoid PRUint32 overflow. Alternatively, calculate andre@0: * (srclen/4) * 3 + ((srclen%4) * 3)/4 andre@0: * which is equivalent but doesn't overflow for any value of srclen. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_Base64Decode andre@0: ( andre@0: const char *src, andre@0: PRUint32 srclen, andre@0: char *dest andre@0: ); andre@0: andre@0: PR_END_EXTERN_C andre@0: andre@0: #endif /* _plbase64_h */