base64.h 3.1 KB

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