0
|
1 /* |
|
2 * base64.hpp -- base64 encoding and decoding |
|
3 * |
|
4 * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr> |
|
5 * |
|
6 * Permission to use, copy, modify, and/or distribute this software for any |
|
7 * purpose with or without fee is hereby granted, provided that the above |
|
8 * copyright notice and this permission notice appear in all copies. |
|
9 * |
|
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
|
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
|
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
17 */ |
|
18 |
|
19 #ifndef BASE64_HPP |
|
20 #define BASE64_HPP |
|
21 |
|
22 /** |
|
23 * \file base64.hpp |
|
24 * \brief Base64 encoding and decoding. |
|
25 * \author David Demelier <markand@malikania.fr> |
5
|
26 * \version 1.0.0 |
0
|
27 */ |
|
28 |
|
29 /** |
|
30 * \page Base64 Base64 |
|
31 * \brief Base64 encoding and decoding. |
|
32 * |
|
33 * The %base64 library let you encode and decode %base64 data from std::string |
|
34 * and iterators. |
|
35 * |
|
36 * ## Encoding |
|
37 * |
|
38 * You can encode like this: |
|
39 * |
|
40 * ````cpp |
|
41 * std::string b64 = base64::encode("Hello world!"); |
|
42 * ```` |
|
43 * |
|
44 * ## Decoding |
|
45 * |
|
46 * And you can decode like this: |
|
47 * |
|
48 * ```` |
|
49 * try { |
|
50 * std::string text = base64::decode(msg); |
|
51 * } catch (const std::exception &ex) { |
|
52 * std::cerr << ex.what() << std::endl; |
|
53 * } |
|
54 * ```` |
|
55 */ |
|
56 |
|
57 #include <cassert> |
|
58 #include <cctype> |
|
59 #include <stdexcept> |
|
60 #include <string> |
|
61 |
|
62 /** |
|
63 * \brief main %base64 namespace. |
|
64 */ |
|
65 namespace base64 { |
|
66 |
|
67 /** |
|
68 * Check if the character is a %base64 character, A-Za-z0-9 and +/. |
|
69 * |
|
70 * \param ch the character to test |
|
71 * \return true if valid |
|
72 */ |
|
73 inline bool isBase64(char ch) noexcept |
|
74 { |
|
75 return std::isalnum(ch) != 0 || ch == '+' || ch == '/'; |
|
76 } |
|
77 |
|
78 /** |
|
79 * Check if the given character is a valid character in %base64 string, |
|
80 * including '='. |
|
81 * |
|
82 * \param ch the character |
|
83 * \return true if the character is valid |
|
84 */ |
|
85 inline bool isValid(char ch) noexcept |
|
86 { |
|
87 return isBase64(ch) || ch == '='; |
|
88 } |
|
89 |
|
90 /** |
|
91 * Get the %base64 character from the 6-bits value. |
|
92 * |
|
93 * \pre value must be valid (< 64) |
|
94 * \param value the value |
|
95 * \return the %base64 character for value |
|
96 */ |
|
97 inline char lookup(unsigned char value) noexcept |
|
98 { |
|
99 static const char *table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
|
100 |
|
101 assert(value < 64); |
|
102 |
|
103 return table[value]; |
|
104 } |
|
105 |
|
106 /** |
|
107 * Get the integer value from the %base64 character. |
|
108 * |
|
109 * \pre isBase64(ch) |
|
110 * \param ch the %base64 character |
|
111 * \return the integer value for the %base64 character ch |
|
112 * ```` |
|
113 * auto b64 = base64::rlookup('D') // 3 |
|
114 * ```` |
|
115 */ |
|
116 inline unsigned char rlookup(char ch) noexcept |
|
117 { |
|
118 assert(isBase64(ch)); |
|
119 |
|
120 if (ch >= '0' && ch <= '9') |
|
121 return static_cast<unsigned char>(ch + 4); |
|
122 if (ch >= 'A' && ch <= 'Z') |
|
123 return static_cast<unsigned char>(ch - 65); |
|
124 if (ch >= 'a' && ch <= 'z') |
|
125 return static_cast<unsigned char>(ch - 71); |
|
126 |
|
127 return (ch == '+') ? 62U : 63U; |
|
128 } |
|
129 |
|
130 /** |
|
131 * Encode the input to the output. |
|
132 * |
|
133 * Requirements: |
|
134 * - **InputIt** must be [InputIterator](http://en.cppreference.com/w/cpp/concept/InputIterator) |
|
135 * - **OutputIt** must be [OutputIterator](http://en.cppreference.com/w/cpp/concept/OutputIterator) |
|
136 * |
|
137 * \param input the beginning |
|
138 * \param end the end of the data |
|
139 * \param output the output destination |
|
140 * \return output |
|
141 */ |
|
142 template <typename InputIt, typename OutputIt> |
|
143 OutputIt encode(InputIt input, InputIt end, OutputIt output) |
|
144 { |
|
145 while (input != end) { |
|
146 char inputbuf[3] = { 0, 0, 0 }; |
|
147 int count; |
|
148 |
|
149 for (count = 0; count < 3 && input != end; ++count) |
|
150 inputbuf[count] = *input++; |
|
151 |
|
152 *output++ = lookup(inputbuf[0] >> 2 & 0x3f); |
|
153 *output++ = lookup((inputbuf[0] << 4 & 0x3f) | (inputbuf[1] >> 4 & 0x0f)); |
|
154 *output++ = (count < 2) ? '=' : lookup((inputbuf[1] << 2 & 0x3c) | (inputbuf[2] >> 6 & 0x03)); |
|
155 *output++ = (count < 3) ? '=' : lookup(inputbuf[2] & 0x3f); |
|
156 } |
|
157 |
|
158 return output; |
|
159 } |
|
160 |
|
161 /** |
|
162 * Decode the input to the output. |
|
163 * |
|
164 * Requirements: |
|
165 * - **InputIt** must be [InputIterator](http://en.cppreference.com/w/cpp/concept/InputIterator) |
|
166 * - **OutputIt** must be [OutputIterator](http://en.cppreference.com/w/cpp/concept/OutputIterator) |
|
167 * |
|
168 * \param input the beginning |
|
169 * \param end the end of the data |
|
170 * \param output the output destination |
|
171 * \return output |
|
172 * \throw std::invalid_argument on bad %base64 string |
|
173 */ |
|
174 template <typename InputIt, typename OutputIt> |
|
175 OutputIt decode(InputIt input, InputIt end, OutputIt output) |
|
176 { |
|
177 while (input != end) { |
|
178 char inputbuf[4] = { -1, -1, -1, -1 }; |
|
179 int count; |
|
180 |
|
181 for (count = 0; count < 4 && input != end; ++count) { |
|
182 // Check if the character is valid and get its value. |
|
183 if ((*input == '=' && count <= 1) || !isValid(*input)) |
|
184 throw std::invalid_argument("invalid base64 string"); |
|
185 if (isBase64(*input)) |
|
186 inputbuf[count] = static_cast<char>(rlookup(*input)); |
|
187 |
|
188 input++; |
|
189 } |
|
190 |
|
191 if (count != 4) |
|
192 throw std::invalid_argument("truncated string"); |
|
193 |
|
194 *output++ = static_cast<char>(((inputbuf[0] << 2) & 0xfc) | ((inputbuf[1] >> 4) & 0x03)); |
|
195 |
|
196 if (inputbuf[2] != -1) |
|
197 *output++ = static_cast<char>(((inputbuf[1] << 4) & 0xf0) | ((inputbuf[2] >> 2) & 0x0f)); |
|
198 if (inputbuf[3] != -1) { |
|
199 // "XY=Z" is not allowed. |
|
200 if (inputbuf[2] == -1) |
|
201 throw std::invalid_argument("invalid base64 string"); |
|
202 |
|
203 *output++ = static_cast<char>(((inputbuf[2] << 6) & 0xc0) | (inputbuf[3] & 0x3f)); |
|
204 } |
|
205 } |
|
206 |
|
207 return output; |
|
208 } |
|
209 |
|
210 /** |
|
211 * Encode a string. |
|
212 * |
|
213 * \param input the input string |
|
214 * \return the %base64 formatted string |
|
215 */ |
|
216 inline std::string encode(const std::string &input) |
|
217 { |
|
218 std::string result; |
|
219 |
|
220 encode(input.begin(), input.end(), std::back_inserter(result)); |
|
221 |
|
222 return result; |
|
223 } |
|
224 |
|
225 /** |
|
226 * Decode a string. |
|
227 * |
|
228 * \param input the %base64 formatted string |
|
229 * \return the original string |
|
230 * \throw std::invalid_argument on bad %base64 string |
|
231 */ |
|
232 inline std::string decode(const std::string &input) |
|
233 { |
|
234 std::string result; |
|
235 |
|
236 decode(input.begin(), input.end(), std::back_inserter(result)); |
|
237 |
|
238 return result; |
|
239 } |
|
240 |
|
241 } // !base64 |
|
242 |
|
243 #endif // !BASE64_HPP |