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 OR GPL-2.0-or-later 9 */ 10 #ifndef MBEDTLS_BASE64_H 11 #define MBEDTLS_BASE64_H 12 13 #if !defined(MBEDTLS_CONFIG_FILE) 14 #include "mbedtls/config.h" 15 #else 16 #include MBEDTLS_CONFIG_FILE 17 #endif 18 19 #include <stddef.h> 20 21 /** Output buffer too small. */ 22 #define MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL -0x002A 23 /** Invalid character in input. */ 24 #define MBEDTLS_ERR_BASE64_INVALID_CHARACTER -0x002C 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif 29 30 /** 31 * \brief Encode a buffer into base64 format 32 * 33 * \param dst destination buffer 34 * \param dlen size of the destination buffer 35 * \param olen number of bytes written 36 * \param src source buffer 37 * \param slen amount of data to be encoded 38 * 39 * \return 0 if successful, or MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL. 40 * *olen is always updated to reflect the amount 41 * of data that has (or would have) been written. 42 * If that length cannot be represented, then no data is 43 * written to the buffer and *olen is set to the maximum 44 * length representable as a size_t. 45 * 46 * \note Call this function with dlen = 0 to obtain the 47 * required buffer size in *olen 48 */ 49 int mbedtls_base64_encode(unsigned char *dst, size_t dlen, size_t *olen, 50 const unsigned char *src, size_t slen); 51 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 #if defined(MBEDTLS_SELF_TEST) 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 80 #endif /* MBEDTLS_SELF_TEST */ 81 82 #ifdef __cplusplus 83 } 84 #endif 85 86 #endif /* base64.h */ 87