Loading...
Searching...
No Matches
base64.h
1/*
2 * SPDX-FileCopyrightText: 2014 HAW Hamburg
3 * SPDX-FileCopyrightText: 2014 Martin Landsmann <Martin.Landsmann@HAW-Hamburg.de>
4 * SPDX-License-Identifier: LGPL-2.1-only
5 */
6
7#pragma once
8
18
19#include <stddef.h> /* for size_t */
20#include <stdbool.h> /* for bool */
21
22#include "compiler_hints.h"
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28#define BASE64_SUCCESS (0)
29#define BASE64_ERROR_BUFFER_OUT (-1)
30#define BASE64_ERROR_BUFFER_OUT_SIZE (-2)
31#define BASE64_ERROR_DATA_IN (-3)
32#define BASE64_ERROR_DATA_IN_SIZE (-4)
33#define BASE64_ERROR_OVERFLOW (-5)
34
43static inline size_t base64_estimate_decode_size(size_t base64_in_size)
44{
45 return (((base64_in_size + 3) / 4) * 3);
46}
47
59static inline size_t base64_estimate_encode_size(size_t data_in_size)
60{
61 return (4 * ((data_in_size + 2) / 3));
62}
63
73static inline bool base64_can_estimate_encode_size(size_t data_in_size)
74{
75 return (data_in_size <= (SIZE_MAX / 4) * 3);
76}
77
98ACCESS(read_only, 1, 2)
99int base64_encode(const void *data_in, size_t data_in_size,
100 void *base64_out, size_t *base64_out_size);
101
127ACCESS(read_only, 1, 2)
128int base64url_encode(const void *data_in, size_t data_in_size,
129 void *base64_out, size_t *base64_out_size);
130
151ACCESS(read_only, 1, 2)
152int base64_decode(const void *base64_in, size_t base64_in_size,
153 void *data_out, size_t *data_out_size);
154
155#ifdef __cplusplus
156}
157#endif
158
Common macros and compiler attributes/pragmas configuration.
#define ACCESS(mode, ptr_idx, size_idx)
Emit an attribute (if supported by the compiler) that declares how a function will access its paramet...
int base64_decode(const void *base64_in, size_t base64_in_size, void *data_out, size_t *data_out_size)
Decodes a given base64 string and save the result to the given destination.
static size_t base64_estimate_encode_size(size_t data_in_size)
Estimates the length of the resulting string after encoding data_in_size bytes into base64.
Definition base64.h:59
int base64url_encode(const void *data_in, size_t data_in_size, void *base64_out, size_t *base64_out_size)
Encodes a given datum to base64 with URL and Filename Safe Alphabet and save the result to the given ...
int base64_encode(const void *data_in, size_t data_in_size, void *base64_out, size_t *base64_out_size)
Encodes a given datum to base64 and save the result to the given destination.
static size_t base64_estimate_decode_size(size_t base64_in_size)
Estimates the amount of bytes needed for decoding base64_in_size characters from base64.
Definition base64.h:43
static bool base64_can_estimate_encode_size(size_t data_in_size)
Checks if the given size of data can return a valid estimate for the size without an integer overflow...
Definition base64.h:73