rapidyaml  0.11.0
parse and emit YAML, and do it fast
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 #include "c4/substr.hpp"
10 #include "c4/blob.hpp"
11 
12 namespace c4 {
13 
14 /** @defgroup doc_base64 Base64 encoding/decoding
15  * @see https://en.wikipedia.org/wiki/Base64
16  * @see https://www.base64encode.org/
17  * @{ */
18 
19 /** check that the given buffer is a valid base64 encoding
20  * @see https://en.wikipedia.org/wiki/Base64 */
21 C4CORE_EXPORT bool base64_valid(csubstr encoded);
22 
23 
24 /** base64-encode binary data.
25  * @param encoded [out] output buffer for encoded data
26  * @param data [in] the input buffer with the binary data
27  *
28  * @return the number of bytes needed to return the output (ie the
29  * required size for @p encoded). No writes occur beyond the end of
30  * the output buffer, so it is safe to do a speculative call where the
31  * encoded buffer is empty, or maybe too small. The caller should
32  * ensure that the returned size is smaller than the size of the
33  * encoded buffer.
34  *
35  * @note the result depends on endianness. If transfer between
36  * little/big endian systems is desired, the caller should normalize
37  * @p data before encoding.
38  *
39  * @see https://en.wikipedia.org/wiki/Base64 */
40 C4CORE_EXPORT size_t base64_encode(substr encoded, cblob data);
41 
42 
43 /** decode the base64 encoding in the given buffer
44  * @param encoded [in] the encoded base64
45  * @param data [out] the output buffer
46  *
47  * @return the number of bytes needed to return the output (ie the
48  * required size for @p data). No writes occur beyond the end of the
49  * output buffer, so it is safe to do a speculative call where the
50  * data buffer is empty, or maybe too small. The caller should ensure
51  * that the returned size is smaller than the size of the data buffer.
52  *
53  * @note the result depends on endianness. If transfer between
54  * little/big endian systems is desired, the caller should normalize
55  * @p data after decoding.
56  *
57  * @see https://en.wikipedia.org/wiki/Base64 */
58 C4CORE_EXPORT size_t base64_decode(csubstr encoded, blob data);
59 
60 /** @} */ // base64
61 
62 namespace fmt {
63 
64 /** @addtogroup doc_format_specifiers
65  * @{ */
66 
67 /** @defgroup doc_base64_fmt Base64
68  * @{ */
69 
70 template<typename CharOrConstChar>
72 {
73  blob_<CharOrConstChar> data;
75  base64_wrapper_(blob_<CharOrConstChar> blob) : data(blob) {}
76 };
77 /** a tag type to mark a payload as base64-encoded */
79 /** a tag type to mark a payload to be encoded as base64 */
81 
82 
83 /** mark a variable to be written in base64 format */
84 template<class ...Args>
85 C4_ALWAYS_INLINE const_base64_wrapper cbase64(Args const& C4_RESTRICT ...args)
86 {
87  return const_base64_wrapper(cblob(args...));
88 }
89 /** mark a csubstr to be written in base64 format */
90 C4_ALWAYS_INLINE const_base64_wrapper cbase64(csubstr s)
91 {
92  return const_base64_wrapper(cblob(s.str, s.len));
93 }
94 /** mark a variable to be written in base64 format */
95 template<class ...Args>
96 C4_ALWAYS_INLINE const_base64_wrapper base64(Args const& C4_RESTRICT ...args)
97 {
98  return const_base64_wrapper(cblob(args...));
99 }
100 /** mark a csubstr to be written in base64 format */
101 C4_ALWAYS_INLINE const_base64_wrapper base64(csubstr s)
102 {
103  return const_base64_wrapper(cblob(s.str, s.len));
104 }
105 
106 /** mark a variable to be read in base64 format */
107 template<class ...Args>
108 C4_ALWAYS_INLINE base64_wrapper base64(Args &... args)
109 {
110  return base64_wrapper(blob(args...));
111 }
112 /** mark a variable to be read in base64 format */
113 C4_ALWAYS_INLINE base64_wrapper base64(substr s)
114 {
115  return base64_wrapper(blob(s.str, s.len));
116 }
117 
118 /** @} */ // base64_fmt
119 
120 /** @} */ // format_specifiers
121 
122 } // namespace fmt
123 
124 
125 /** write a variable in base64 format
126  * @ingroup doc_to_chars */
127 inline size_t to_chars(substr buf, fmt::const_base64_wrapper b)
128 {
129  return base64_encode(buf, b.data);
130 }
131 
132 /** read a variable in base64 format
133  * @ingroup doc_from_chars */
134 inline size_t from_chars(csubstr buf, fmt::base64_wrapper *b)
135 {
136  return base64_decode(buf, b->data);
137 }
138 
139 } // namespace c4
140 
141 #endif /* _C4_BASE64_HPP_ */
base64_wrapper_< byte > base64_wrapper
a tag type to mark a payload to be encoded as base64
Definition: base64.hpp:80
const_base64_wrapper cbase64(Args const &...args)
mark a variable to be written in base64 format
Definition: base64.hpp:85
const_base64_wrapper base64(Args const &...args)
mark a variable to be written in base64 format
Definition: base64.hpp:96
base64_wrapper_< cbyte > const_base64_wrapper
a tag type to mark a payload as base64-encoded
Definition: base64.hpp:78
size_t base64_encode(substr encoded, cblob data)
base64-encode binary data.
bool base64_valid(csubstr encoded)
check that the given buffer is a valid base64 encoding
size_t base64_decode(csubstr encoded, blob data)
decode the base64 encoding in the given buffer
bool from_chars(csubstr buf, uint8_t *v) noexcept
Definition: charconv.hpp:2366
size_t to_chars(substr buf, uint8_t v) noexcept
Definition: charconv.hpp:2331
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
Definition: common.cpp:14
base64_wrapper_(blob_< CharOrConstChar > blob)
Definition: base64.hpp:75
blob_< CharOrConstChar > data
Definition: base64.hpp:73
read+write string views