Debian Codegraph @

src/Base/Base64.h

raw
1 // SPDX-License-Identifier: Zlib
2
3 /*
4 base64.cpp and base64.h
5
6 Copyright (C) 2004-2008 René Nyffenegger
7
8 This source code is provided 'as-is', without any express or implied
9 warranty. In no event will the author be held liable for any damages
10 arising from the use of this software.
11
12 Permission is granted to anyone to use this software for any purpose,
13 including commercial applications, and to alter it and redistribute it
14 freely, subject to the following restrictions:
15
16 1. The origin of this source code must not be misrepresented; you must not
17 claim that you wrote the original source code. If you use this source code
18 in a product, an acknowledgment in the product documentation would be
19 appreciated but is not required.
20
21 2. Altered source versions must be plainly marked as such, and must not be
22 misrepresented as being the original source code.
23
24 3. This notice may not be removed or altered from any source distribution.
25
26 RenĂ© Nyffenegger [email protected]
27
28
29 NOTICE: The source code here has been altered from the original to use a provided character buffer
30 rather than returning a new string for each call.
31 These modifications are Copyright (c) 2019 Zheng Lei ([email protected])
32 */
33 #ifndef BASE_BASE64_H
34 #define BASE_BASE64_H
35
36 #include <array>
37 #include <string>
38
39 #include "FCGlobal.h"
40
41 // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic,
42 // cppcoreguidelines-pro-bounds-constant-array-index, cppcoreguidelines-avoid-magic-numbers,
43 // readability-magic-numbers)
44
45 namespace Base
46 {
47
48 static constexpr size_t base64DecodeTableSize {256};
49
50 /// Returns the max bytes of a encoded base64 string
51 inline std::size_t base64_encode_size(std::size_t len)
52 {
53 return 4 * ((len + 2) / 3);
54 }
55
56 /// Returns the max bytes of a decoded base64 binary string
57 inline std::size_t base64_decode_size(std::size_t len)
58 {
59 return len / 4 * 3;
60 }
61
62 /** Encode input binary with base64
63 * @param out: output buffer with minimum size of base64_encode(len)
64 * appending new data.
65 * @param in: input binary data
66 * @param len: input length
67 * @return The character count written to output.
68 */
69 BaseExport std::size_t base64_encode(char* out, void const* in, std::size_t len);
70
71 /** Return the internal base64 decoding table
72 *
73 * The table maps from any 8-bit character to the decoded binary bits.
74 * Valid base64 characters are mapped to the corresponding 6-bit binary
75 * data. White space (space, tab, vtab, CR and LF) characters are mapped
76 * to -2. Other invalid characters are mapped to -1.
77 */
78 BaseExport std::array<const signed char, base64DecodeTableSize> base64_decode_table();
79
80 /** Decode the input base64 string into binary data
81 * @param out: output buffer with minimum size of base64_encode(len)
82 * appending new data.
83 * @param in: input binary data
84 * @param len: input length
85 * @return Return a pair of output size and input read size. Compare the
86 * read size to input size to check for error.
87 */
88 BaseExport std::pair<std::size_t, std::size_t> base64_decode(void* out, char const*, std::size_t len);
89
90 /** Encode input binary into base64 string
91 * @param out: output string. Note that the string is not cleared before
92 * adding new content.
93 * @param in: input binary data
94 * @param len: input length
95 */
96 inline void base64_encode(std::string& out, void const* in, std::size_t len)
97 {
98 std::size_t size = out.size();
99 out.resize(size + base64_encode_size(len));
100 len = base64_encode(&out[size], in, len);
101 out.resize(size + len);
102 }
103
104 /** Encode input binary into base64 string
105 * @param in: input binary data
106 * @param len: input length
107 * @return Return the base64 string.
108 */
109 inline std::string base64_encode(void const* in, std::size_t len)
110 {
111 std::string out;
112 base64_encode(out, in, len);
113 return out;
114 }
115
116 /** Decode base64 string into binary data
117 * @param out: output binary data. Note that the data is not cleared before
118 * adding new content.
119 * @param in: input base64 string
120 * @param len: input length
121 * @return Return the processed input length. Compare this with the
122 * argument \c len to check for error.
123 */
124 template<typename T>
125 inline std::size_t base64_decode(T& out, char const* in, std::size_t len)
126 {
127 std::size_t size = out.size();
128 out.resize(size + base64_decode_size(len));
129 std::pair<std::size_t, std::size_t> res = base64_decode(&out[size], in, len);
130 out.resize(size + res.first);
131 return res.second;
132 }
133
134 /** Decode base64 string into binary data
135 * @param out: output binary data. Note that the data is not cleared before
136 * adding new content.
137 * @param str: input base64 string
138 * @return Return the processed input length. Compare this with the
139 * argument \c len to check for error.
140 */
141 template<typename T>
142 inline std::size_t base64_decode(T& out, std::string const& str)
143 {
144 return base64_decode(out, str.c_str(), str.size());
145 }
146
147 /** Decode base64 string into binary data
148 * @param out adding new content.
149 * @param str: input base64 string
150 * @return Return the decoded binary data.
151 */
152 inline std::string base64_decode(std::string const& str)
153 {
154 std::string out;
155 base64_decode(out, str.c_str(), str.size());
156 return out;
157 }
158
159 } // namespace Base
160
161 // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic,
162 // cppcoreguidelines-pro-bounds-constant-array-index, cppcoreguidelines-avoid-magic-numbers,
163 // readability-magic-numbers)
164
165 #endif