plbase64.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #ifndef _plbase64_h
  6. #define _plbase64_h
  7. #include "prtypes.h"
  8. PR_BEGIN_EXTERN_C
  9. /*
  10. * PL_Base64Encode
  11. *
  12. * This routine encodes the data pointed to by the "src" parameter using the
  13. * base64 algorithm, and returns a pointer to the result. If the "srclen"
  14. * parameter is not zero, it specifies the length of the source data. If it
  15. * is zero, the source data is assumed to be null-terminated, and PL_strlen
  16. * is used to determine the source length. If the "dest" parameter is not
  17. * null, it is assumed to point to a buffer of sufficient size (which may be
  18. * calculated: ((srclen + 2)/3)*4) into which the encoded data is placed
  19. * (without any termination). If the "dest" parameter is null, a buffer is
  20. * allocated from the heap to hold the encoded data, and the result *will*
  21. * be terminated with an extra null character. It is the caller's
  22. * responsibility to free the result when it is allocated. A null is returned
  23. * if the allocation fails.
  24. *
  25. * NOTE: when calculating ((srclen + 2)/3)*4), first ensure that
  26. * srclen <= (PR_UINT32_MAX/4) * 3
  27. * to avoid PRUint32 overflow.
  28. */
  29. PR_EXTERN(char *)
  30. PL_Base64Encode
  31. (
  32. const char *src,
  33. PRUint32 srclen,
  34. char *dest
  35. );
  36. /*
  37. * PL_Base64Decode
  38. *
  39. * This routine decodes the data pointed to by the "src" parameter using
  40. * the base64 algorithm, and returns a pointer to the result. The source
  41. * may either include or exclude any trailing '=' characters. If the
  42. * "srclen" parameter is not zero, it specifies the length of the source
  43. * data. If it is zero, PL_strlen will be used to determine the source
  44. * length. If the "dest" parameter is not null, it is assumed to point to
  45. * a buffer of sufficient size (which may be calculated: (srclen * 3)/4
  46. * when srclen includes the '=' characters) into which the decoded data
  47. * is placed (without any termination). If the "dest" parameter is null,
  48. * a buffer is allocated from the heap to hold the decoded data, and the
  49. * result *will* be terminated with an extra null character. It is the
  50. * caller's responsibility to free the result when it is allocated. A null
  51. * is retuned if the allocation fails, or if the source is not well-coded.
  52. *
  53. * NOTE: when calculating (srclen * 3)/4, first ensure that
  54. * srclen <= PR_UINT32_MAX/3
  55. * to avoid PRUint32 overflow. Alternatively, calculate
  56. * (srclen/4) * 3 + ((srclen%4) * 3)/4
  57. * which is equivalent but doesn't overflow for any value of srclen.
  58. */
  59. PR_EXTERN(char *)
  60. PL_Base64Decode
  61. (
  62. const char *src,
  63. PRUint32 srclen,
  64. char *dest
  65. );
  66. PR_END_EXTERN_C
  67. #endif /* _plbase64_h */