blob: 21b4622710ed05a5e28137b0eb79150dc6a89208 [file] [log] [blame]
Jaeden Ameroe54e6932018-08-06 16:19:58 +01001/**
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 Crypto (https://tls.mbed.org)
23 */
24#ifndef MBEDCRYPTO_BASE64_H
25#define MBEDCRYPTO_BASE64_H
26
27#include <stddef.h>
28
29#define MBEDCRYPTO_ERR_BASE64_BUFFER_TOO_SMALL -0x002A /**< Output buffer too small. */
30#define MBEDCRYPTO_ERR_BASE64_INVALID_CHARACTER -0x002C /**< Invalid character in input. */
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36/**
37 * \brief Encode a buffer into base64 format
38 *
39 * \param dst destination buffer
40 * \param dlen size of the destination buffer
41 * \param olen number of bytes written
42 * \param src source buffer
43 * \param slen amount of data to be encoded
44 *
45 * \return 0 if successful, or MBEDCRYPTO_ERR_BASE64_BUFFER_TOO_SMALL.
46 * *olen is always updated to reflect the amount
47 * of data that has (or would have) been written.
48 * If that length cannot be represented, then no data is
49 * written to the buffer and *olen is set to the maximum
50 * length representable as a size_t.
51 *
52 * \note Call this function with dlen = 0 to obtain the
53 * required buffer size in *olen
54 */
55int mbedcrypto_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
56 const unsigned char *src, size_t slen );
57
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, MBEDCRYPTO_ERR_BASE64_BUFFER_TOO_SMALL, or
68 * MBEDCRYPTO_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 */
75int mbedcrypto_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
76 const unsigned char *src, size_t slen );
77
78/**
79 * \brief Checkup routine
80 *
81 * \return 0 if successful, or 1 if the test failed
82 */
83int mbedcrypto_base64_self_test( int verbose );
84
85#ifdef __cplusplus
86}
87#endif
88
89#endif /* base64.h */