base64.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * \file base64.h
  3. *
  4. * \brief RFC 1521 base64 encoding/decoding
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_BASE64_H
  26. #define MBEDTLS_BASE64_H
  27. #include <stddef.h>
  28. #define MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL -0x002A /**< Output buffer too small. */
  29. #define MBEDTLS_ERR_BASE64_INVALID_CHARACTER -0x002C /**< Invalid character in input. */
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /**
  34. * \brief Encode a buffer into base64 format
  35. *
  36. * \param dst destination buffer
  37. * \param dlen size of the destination buffer
  38. * \param olen number of bytes written
  39. * \param src source buffer
  40. * \param slen amount of data to be encoded
  41. *
  42. * \return 0 if successful, or MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL.
  43. * *olen is always updated to reflect the amount
  44. * of data that has (or would have) been written.
  45. * If that length cannot be represented, then no data is
  46. * written to the buffer and *olen is set to the maximum
  47. * length representable as a size_t.
  48. *
  49. * \note Call this function with dlen = 0 to obtain the
  50. * required buffer size in *olen
  51. */
  52. int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
  53. const unsigned char *src, size_t slen );
  54. /**
  55. * \brief Decode a base64-formatted buffer
  56. *
  57. * \param dst destination buffer (can be NULL for checking size)
  58. * \param dlen size of the destination buffer
  59. * \param olen number of bytes written
  60. * \param src source buffer
  61. * \param slen amount of data to be decoded
  62. *
  63. * \return 0 if successful, MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL, or
  64. * MBEDTLS_ERR_BASE64_INVALID_CHARACTER if the input data is
  65. * not correct. *olen is always updated to reflect the amount
  66. * of data that has (or would have) been written.
  67. *
  68. * \note Call this function with *dst = NULL or dlen = 0 to obtain
  69. * the required buffer size in *olen
  70. */
  71. int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
  72. const unsigned char *src, size_t slen );
  73. /**
  74. * \brief Checkup routine
  75. *
  76. * \return 0 if successful, or 1 if the test failed
  77. */
  78. int mbedtls_base64_self_test( int verbose );
  79. #ifdef __cplusplus
  80. }
  81. #endif
  82. #endif /* base64.h */