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