base64.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #if !defined(MBEDTLS_CONFIG_FILE)
  27. #include "config.h"
  28. #else
  29. #include MBEDTLS_CONFIG_FILE
  30. #endif
  31. #include <stddef.h>
  32. #define MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL -0x002A /**< Output buffer too small. */
  33. #define MBEDTLS_ERR_BASE64_INVALID_CHARACTER -0x002C /**< Invalid character in input. */
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /**
  38. * \brief Encode a buffer into base64 format
  39. *
  40. * \param dst destination buffer
  41. * \param dlen size of the destination buffer
  42. * \param olen number of bytes written
  43. * \param src source buffer
  44. * \param slen amount of data to be encoded
  45. *
  46. * \return 0 if successful, or MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL.
  47. * *olen is always updated to reflect the amount
  48. * of data that has (or would have) been written.
  49. * If that length cannot be represented, then no data is
  50. * written to the buffer and *olen is set to the maximum
  51. * length representable as a size_t.
  52. *
  53. * \note Call this function with dlen = 0 to obtain the
  54. * required buffer size in *olen
  55. */
  56. int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
  57. const unsigned char *src, size_t slen );
  58. /**
  59. * \brief Decode a base64-formatted buffer
  60. *
  61. * \param dst destination buffer (can be NULL for checking size)
  62. * \param dlen size of the destination buffer
  63. * \param olen number of bytes written
  64. * \param src source buffer
  65. * \param slen amount of data to be decoded
  66. *
  67. * \return 0 if successful, MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL, or
  68. * MBEDTLS_ERR_BASE64_INVALID_CHARACTER if the input data is
  69. * not correct. *olen is always updated to reflect the amount
  70. * of data that has (or would have) been written.
  71. *
  72. * \note Call this function with *dst = NULL or dlen = 0 to obtain
  73. * the required buffer size in *olen
  74. */
  75. int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
  76. const unsigned char *src, size_t slen );
  77. #if defined(MBEDTLS_SELF_TEST)
  78. /**
  79. * \brief Checkup routine
  80. *
  81. * \return 0 if successful, or 1 if the test failed
  82. */
  83. int mbedtls_base64_self_test( int verbose );
  84. #endif /* MBEDTLS_SELF_TEST */
  85. #ifdef __cplusplus
  86. }
  87. #endif
  88. #endif /* base64.h */