base64.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * \file base64.h
  3. *
  4. * \brief RFC 1521 base64 encoding/decoding
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. * not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. #ifndef MBEDTLS_BASE64_H
  23. #define MBEDTLS_BASE64_H
  24. #if !defined(MBEDTLS_CONFIG_FILE)
  25. #include "mbedtls/config.h"
  26. #else
  27. #include MBEDTLS_CONFIG_FILE
  28. #endif
  29. #include <stddef.h>
  30. #define MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL -0x002A /**< Output buffer too small. */
  31. #define MBEDTLS_ERR_BASE64_INVALID_CHARACTER -0x002C /**< Invalid character in input. */
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /**
  36. * \brief Encode a buffer into base64 format
  37. *
  38. * \param dst destination buffer
  39. * \param dlen size of the destination buffer
  40. * \param olen number of bytes written
  41. * \param src source buffer
  42. * \param slen amount of data to be encoded
  43. *
  44. * \return 0 if successful, or MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL.
  45. * *olen is always updated to reflect the amount
  46. * of data that has (or would have) been written.
  47. * If that length cannot be represented, then no data is
  48. * written to the buffer and *olen is set to the maximum
  49. * length representable as a size_t.
  50. *
  51. * \note Call this function with dlen = 0 to obtain the
  52. * required buffer size in *olen
  53. */
  54. int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
  55. const unsigned char *src, size_t slen );
  56. /**
  57. * \brief Decode a base64-formatted buffer
  58. *
  59. * \param dst destination buffer (can be NULL for checking size)
  60. * \param dlen size of the destination buffer
  61. * \param olen number of bytes written
  62. * \param src source buffer
  63. * \param slen amount of data to be decoded
  64. *
  65. * \return 0 if successful, MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL, or
  66. * MBEDTLS_ERR_BASE64_INVALID_CHARACTER if the input data is
  67. * not correct. *olen is always updated to reflect the amount
  68. * of data that has (or would have) been written.
  69. *
  70. * \note Call this function with *dst = NULL or dlen = 0 to obtain
  71. * the required buffer size in *olen
  72. */
  73. int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
  74. const unsigned char *src, size_t slen );
  75. #if defined(MBEDTLS_SELF_TEST)
  76. /**
  77. * \brief Checkup routine
  78. *
  79. * \return 0 if successful, or 1 if the test failed
  80. */
  81. int mbedtls_base64_self_test( int verbose );
  82. #endif /* MBEDTLS_SELF_TEST */
  83. #ifdef __cplusplus
  84. }
  85. #endif
  86. #endif /* base64.h */