rapidyaml 0.14.0
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
base64.hpp
Go to the documentation of this file.
1#ifndef _C4_BASE64_HPP_
2#define _C4_BASE64_HPP_
3
4/** @file base64.hpp encoding/decoding for base64.
5 * @see https://en.wikipedia.org/wiki/Base64
6 * @see https://www.base64encode.org/
7 * */
8
9#ifndef _C4_EXPORT_HPP_
10#include "c4/export.hpp"
11#endif
12#include <stddef.h>
13
14namespace c4 {
15
16/** @defgroup doc_base64 Base64 encoding/decoding
17 * @see https://en.wikipedia.org/wiki/Base64
18 * @see https://www.base64encode.org/
19 * @{ */
20
21
22/** check that the given buffer is a valid base64 encoding
23 * @see https://en.wikipedia.org/wiki/Base64 */
24C4CORE_EXPORT bool base64_valid(const char* encoded, size_t encoded_sz);
25
26
27/** base64-encode binary data. This is a plain implementation with a
28 * focus on simplicity and small footprint, such that it runs
29 * reasonably well in constrained platforms. On larger platforms it is
30 * reasonably fast (reaching 3GB/s and over), but it is not the
31 * fastest. If ultimate base64 speed in x64 platforms is your
32 * objective, there are faster implementations available. One
33 * recommendation is https://github.com/aklomp/base64, which uses a
34 * larger Look-Up Table (4096B as compared with 64B in c4core), making
35 * it between 1.5x~2x faster than c4core for larger payloads (but also
36 * slower for small payloads), and much faster when using AVX2 or
37 * AVX512 processing. But this speed comes at a cost in constrained
38 * platforms: eg c4core encodes ~2.5x faster in armv4 and armv5.
39 *
40 * @param encoded [out] output buffer for encoded data
41 *
42 * @param encoded_sz [in] size of the output buffer for encoded data
43 *
44 * @param data [in] the input buffer with the binary data
45 *
46 * @param data_sz [in] size of the input buffer with the binary data
47 *
48 * @return the number of bytes required for the output buffer. No
49 * writes occur beyond the end of the output buffer, so it is
50 * safe to do a speculative call where the encoded buffer is
51 * empty, or maybe too small. The caller should ensure that
52 * the returned size is smaller than the size of the encoded
53 * buffer.
54 *
55 * @note the result depends on endianness. If transfer between
56 * little/big endian systems is desired, the caller should
57 * normalize @p data before encoding.
58 *
59 * @see https://en.wikipedia.org/wiki/Base64 */
60C4CORE_EXPORT size_t base64_encode(char *encoded, size_t encoded_sz,
61 void const* data, size_t data_sz);
62
63
64/** decode the base64 encoding in the given buffer. This is a plain
65 * implementation with a focus on simplicity and small footprint, such
66 * that it runs reasonably well in constrained platforms. On larger
67 * platforms it is reasonably fast, but it is not the fastest. If
68 * ultimate base64 speed in x64 platforms is your objective, there are
69 * faster implementations available. One recommendation is
70 * https://github.com/aklomp/base64, which uses up to 16x larger
71 * Look-Up Tables, making it between 1.5x~2x faster than c4core (but
72 * also slower for small payloads), and much faster when using AVX2 or
73 * AVX512 processing. But this x64 speed comes at a cost in
74 * constrained platforms: eg c4core decodes ~4x faster in armv4 and
75 * armv5.
76 *
77 * @param encoded [in] the encoded base64
78 *
79 * @param encoded_sz [in] the size of the encoded buffer
80 *
81 * @param data [out] the output decoded buffer
82 *
83 * @param data_sz [in] the size of the output decoded buffer
84 *
85 * @param data_sz_required [out] the size required for the output
86 * decoded buffer, ie, the number of bytes needed to return the
87 * output (ie the required size for @p data). No writes occur
88 * beyond the end of the output buffer, so it is safe to do a
89 * speculative call where the data buffer is empty, or maybe
90 * too small. The caller should ensure that this value
91 * is smaller than data_sz.
92 *
93 * @return false if the encoding was invalid or the data size was
94 * too small, and true otherwise.
95 *
96 * @note the result depends on endianness. If transfer between
97 * little/big endian systems is desired, the caller should
98 * normalize @p data after decoding.
99 *
100 * @see https://en.wikipedia.org/wiki/Base64 */
101C4CORE_EXPORT bool base64_decode(char const* encoded, size_t encoded_sz,
102 void * data, size_t data_sz,
103 size_t *data_sz_required);
104
105/** @} */ // base64
106
107} // namespace c4
108
109#endif /* _C4_BASE64_HPP_ */
bool base64_decode(char const *encoded, size_t encoded_sz, void *data, size_t data_sz, size_t *data_sz_required)
decode the base64 encoding in the given buffer.
size_t base64_encode(char *encoded, size_t encoded_sz, void const *data, size_t data_sz)
base64-encode binary data.
bool base64_valid(const char *encoded, size_t encoded_sz)
check that the given buffer is a valid base64 encoding
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
Definition common.cpp:14