| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
#ifndef _plbase64_h |
| 7 |
#define _plbase64_h |
| 8 |
|
| 9 |
#include "prtypes.h" |
| 10 |
|
| 11 |
PR_BEGIN_EXTERN_C |
| 12 |
|
| 13 |
/* |
| 14 |
* PL_Base64Encode |
| 15 |
* |
| 16 |
* This routine encodes the data pointed to by the "src" parameter using the |
| 17 |
* base64 algorithm, and returns a pointer to the result. If the "srclen" |
| 18 |
* parameter is not zero, it specifies the length of the source data. If it |
| 19 |
* is zero, the source data is assumed to be null-terminated, and PL_strlen |
| 20 |
* is used to determine the source length. If the "dest" parameter is not |
| 21 |
* null, it is assumed to point to a buffer of sufficient size (which may be |
| 22 |
* calculated: ((srclen + 2)/3)*4) into which the encoded data is placed |
| 23 |
* (without any termination). If the "dest" parameter is null, a buffer is |
| 24 |
* allocated from the heap to hold the encoded data, and the result *will* |
| 25 |
* be terminated with an extra null character. It is the caller's |
| 26 |
* responsibility to free the result when it is allocated. A null is returned |
| 27 |
* if the allocation fails. |
| 28 |
* |
| 29 |
* NOTE: when calculating ((srclen + 2)/3)*4), first ensure that |
| 30 |
* srclen <= (PR_UINT32_MAX/4) * 3 |
| 31 |
* to avoid PRUint32 overflow. |
| 32 |
*/ |
| 33 |
|
| 34 |
PR_EXTERN(char *) |
| 35 |
PL_Base64Encode |
| 36 |
( |
| 37 |
const char *src, |
| 38 |
PRUint32 srclen, |
| 39 |
char *dest |
| 40 |
); |
| 41 |
|
| 42 |
/* |
| 43 |
* PL_Base64Decode |
| 44 |
* |
| 45 |
* This routine decodes the data pointed to by the "src" parameter using |
| 46 |
* the base64 algorithm, and returns a pointer to the result. The source |
| 47 |
* may either include or exclude any trailing '=' characters. If the |
| 48 |
* "srclen" parameter is not zero, it specifies the length of the source |
| 49 |
* data. If it is zero, PL_strlen will be used to determine the source |
| 50 |
* length. If the "dest" parameter is not null, it is assumed to point to |
| 51 |
* a buffer of sufficient size (which may be calculated: (srclen * 3)/4 |
| 52 |
* when srclen includes the '=' characters) into which the decoded data |
| 53 |
* is placed (without any termination). If the "dest" parameter is null, |
| 54 |
* a buffer is allocated from the heap to hold the decoded data, and the |
| 55 |
* result *will* be terminated with an extra null character. It is the |
| 56 |
* caller's responsibility to free the result when it is allocated. A null |
| 57 |
* is retuned if the allocation fails, or if the source is not well-coded. |
| 58 |
* |
| 59 |
* NOTE: when calculating (srclen * 3)/4, first ensure that |
| 60 |
* srclen <= PR_UINT32_MAX/3 |
| 61 |
* to avoid PRUint32 overflow. Alternatively, calculate |
| 62 |
* (srclen/4) * 3 + ((srclen%4) * 3)/4 |
| 63 |
* which is equivalent but doesn't overflow for any value of srclen. |
| 64 |
*/ |
| 65 |
|
| 66 |
PR_EXTERN(char *) |
| 67 |
PL_Base64Decode |
| 68 |
( |
| 69 |
const char *src, |
| 70 |
PRUint32 srclen, |
| 71 |
char *dest |
| 72 |
); |
| 73 |
|
| 74 |
PR_END_EXTERN_C |
| 75 |
|
| 76 |
#endif /* _plbase64_h */ |