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