1 /*
2  * RFC 1521 base64 encoding/decoding
3  *
4  * Copyright (C) 2018, Nordic Semiconductor ASA
5  * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
6  * SPDX-License-Identifier: Apache-2.0
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License"); you may
9  * not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * Adapted for Zephyr by Carles Cufi (carles.cufi@nordicsemi.no)
21  *  - Removed mbedtls_ prefixes
22  *  - Reworked coding style
23  */
24 #ifndef ZEPHYR_INCLUDE_SYS_BASE64_H_
25 #define ZEPHYR_INCLUDE_SYS_BASE64_H_
26 
27 #include <stddef.h>
28 #include <zephyr/types.h>
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /**
35  * @file
36  *
37  * @defgroup base64 Base64
38  * @brief Base64 encoding/decoding functions
39  * @ingroup utilities
40  * @{
41  */
42 
43 /**
44  * @brief          Encode a buffer into base64 format
45  *
46  * @param dst      destination buffer
47  * @param dlen     size of the destination buffer
48  * @param olen     number of bytes written
49  * @param src      source buffer
50  * @param slen     amount of data to be encoded
51  *
52  * @return         0 if successful, or -ENOMEM if the buffer is too small.
53  *                 *olen is always updated to reflect the amount
54  *                 of data that has (or would have) been written.
55  *                 If that length cannot be represented, then no data is
56  *                 written to the buffer and *olen is set to the maximum
57  *                 length representable as a size_t.
58  *
59  * @note           Call this function with dlen = 0 to obtain the
60  *                 required buffer size in *olen
61  */
62 int base64_encode(uint8_t *dst, size_t dlen, size_t *olen, const uint8_t *src,
63 		  size_t slen);
64 
65 /**
66  * @brief          Decode a base64-formatted buffer
67  *
68  * @param dst      destination buffer (can be NULL for checking size)
69  * @param dlen     size of the destination buffer
70  * @param olen     number of bytes written
71  * @param src      source buffer
72  * @param slen     amount of data to be decoded
73  *
74  * @return         0 if successful, -ENOMEM, or -EINVAL if the input data is
75  *                 not correct. *olen is always updated to reflect the amount
76  *                 of data that has (or would have) been written.
77  *
78  * @note           Call this function with *dst = NULL or dlen = 0 to obtain
79  *                 the required buffer size in *olen
80  */
81 int base64_decode(uint8_t *dst, size_t dlen, size_t *olen, const uint8_t *src,
82 		  size_t slen);
83 
84 /**
85  * @}
86  */
87 
88 #ifdef __cplusplus
89 }
90 #endif
91 
92 #endif /* ZEPHYR_INCLUDE_SYS_BASE64_H_ */
93