base64.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * \file base64.h
  3. *
  4. * \brief RFC 1521 base64 encoding/decoding
  5. */
  6. /*
  7. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  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. * This file is part of mbed TLS (https://tls.mbed.org)
  23. */
  24. #ifndef MBEDTLS_BASE64_H
  25. #define MBEDTLS_BASE64_H
  26. #include <stddef.h>
  27. #define MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL -0x002A /**< Output buffer too small. */
  28. #define MBEDTLS_ERR_BASE64_INVALID_CHARACTER -0x002C /**< Invalid character in input. */
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /**
  33. * \brief Encode a buffer into base64 format
  34. *
  35. * \param dst destination buffer
  36. * \param dlen size of the destination buffer
  37. * \param olen number of bytes written
  38. * \param src source buffer
  39. * \param slen amount of data to be encoded
  40. *
  41. * \return 0 if successful, or MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL.
  42. * *olen is always updated to reflect the amount
  43. * of data that has (or would have) been written.
  44. * If that length cannot be represented, then no data is
  45. * written to the buffer and *olen is set to the maximum
  46. * length representable as a size_t.
  47. *
  48. * \note Call this function with dlen = 0 to obtain the
  49. * required buffer size in *olen
  50. */
  51. int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
  52. const unsigned char *src, size_t slen );
  53. /**
  54. * \brief Decode a base64-formatted buffer
  55. *
  56. * \param dst destination buffer (can be NULL for checking size)
  57. * \param dlen size of the destination buffer
  58. * \param olen number of bytes written
  59. * \param src source buffer
  60. * \param slen amount of data to be decoded
  61. *
  62. * \return 0 if successful, MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL, or
  63. * MBEDTLS_ERR_BASE64_INVALID_CHARACTER if the input data is
  64. * not correct. *olen is always updated to reflect the amount
  65. * of data that has (or would have) been written.
  66. *
  67. * \note Call this function with *dst = NULL or dlen = 0 to obtain
  68. * the required buffer size in *olen
  69. */
  70. int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
  71. const unsigned char *src, size_t slen );
  72. /**
  73. * \brief Checkup routine
  74. *
  75. * \return 0 if successful, or 1 if the test failed
  76. */
  77. int mbedtls_base64_self_test( int verbose );
  78. #ifdef __cplusplus
  79. }
  80. #endif
  81. #endif /* base64.h */