| 1 |
/* |
| 2 |
base64.cpp and base64.h |
| 3 |
|
| 4 |
base64 encoding and decoding with C++. |
| 5 |
More information at |
| 6 |
https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp |
| 7 |
|
| 8 |
Version: 2.rc.04 (release candidate) |
| 9 |
|
| 10 |
Copyright (C) 2004-2017, 2020 René Nyffenegger |
| 11 |
|
| 12 |
This source code is provided 'as-is', without any express or implied |
| 13 |
warranty. In no event will the author be held liable for any damages |
| 14 |
arising from the use of this software. |
| 15 |
|
| 16 |
Permission is granted to anyone to use this software for any purpose, |
| 17 |
including commercial applications, and to alter it and redistribute it |
| 18 |
freely, subject to the following restrictions: |
| 19 |
|
| 20 |
1. The origin of this source code must not be misrepresented; you must not |
| 21 |
claim that you wrote the original source code. If you use this source code |
| 22 |
in a product, an acknowledgment in the product documentation would be |
| 23 |
appreciated but is not required. |
| 24 |
|
| 25 |
2. Altered source versions must be plainly marked as such, and must not be |
| 26 |
misrepresented as being the original source code. |
| 27 |
|
| 28 |
3. This notice may not be removed or altered from any source distribution. |
| 29 |
|
| 30 |
René Nyffenegger [email protected] |
| 31 |
|
| 32 |
*/ |
| 33 |
|
| 34 |
#include "base64.h" |
| 35 |
|
| 36 |
// |
| 37 |
// Depending on the url parameter in base64_chars, one of |
| 38 |
// two sets of base64 characters needs to be chosen. |
| 39 |
// They differ in their last two characters. |
| 40 |
// |
| 41 |
const char* base64_chars[2] = { |
| 42 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 43 |
"abcdefghijklmnopqrstuvwxyz" |
| 44 |
"0123456789" |
| 45 |
"+/", |
| 46 |
|
| 47 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 48 |
"abcdefghijklmnopqrstuvwxyz" |
| 49 |
"0123456789" |
| 50 |
"-_"}; |
| 51 |
|
| 52 |
static unsigned int pos_of_char(const unsigned char chr) |
| 53 |
{ |
| 54 |
// |
| 55 |
// Return the position of chr within base64_encode() |
| 56 |
// |
| 57 |
|
| 58 |
if (chr >= 'A' && chr <= 'Z') |
| 59 |
return chr - 'A'; |
| 60 |
else if (chr >= 'a' && chr <= 'z') |
| 61 |
return chr - 'a' + ('Z' - 'A') + 1; |
| 62 |
else if (chr >= '0' && chr <= '9') |
| 63 |
return chr - '0' + ('Z' - 'A') + ('z' - 'a') + 2; |
| 64 |
else if (chr == '+' || chr == '-') |
| 65 |
return 62; // Be liberal with input and accept both url ('-') and non-url ('+') base 64 characters ( |
| 66 |
else if (chr == '/' || chr == '_') |
| 67 |
return 63; // Ditto for '/' and '_' |
| 68 |
|
| 69 |
throw "If input is correct, this line should never be reached."; |
| 70 |
} |
| 71 |
|
| 72 |
static std::string insert_linebreaks(std::string str, size_t distance) |
| 73 |
{ |
| 74 |
// |
| 75 |
// Provided by https://github.com/JomaCorpFX, adapted by me. |
| 76 |
// |
| 77 |
if (!str.length()) { |
| 78 |
return ""; |
| 79 |
} |
| 80 |
|
| 81 |
size_t pos = distance; |
| 82 |
|
| 83 |
while (pos < str.size()) { |
| 84 |
str.insert(pos, "\n"); |
| 85 |
pos += distance + 1; |
| 86 |
} |
| 87 |
|
| 88 |
return str; |
| 89 |
} |
| 90 |
|
| 91 |
template <typename String, unsigned int line_length> |
| 92 |
static std::string encode_with_line_breaks(String s) |
| 93 |
{ |
| 94 |
return insert_linebreaks(base64_encode(s, false), line_length); |
| 95 |
} |
| 96 |
|
| 97 |
template <typename String> |
| 98 |
static std::string encode_pem(String s) |
| 99 |
{ |
| 100 |
return encode_with_line_breaks<String, 64>(s); |
| 101 |
} |
| 102 |
|
| 103 |
template <typename String> |
| 104 |
static std::string encode_mime(String s) |
| 105 |
{ |
| 106 |
return encode_with_line_breaks<String, 76>(s); |
| 107 |
} |
| 108 |
|
| 109 |
template <typename String> |
| 110 |
static std::string encode(String s, bool url) |
| 111 |
{ |
| 112 |
return base64_encode(reinterpret_cast<const unsigned char*>(s.data()), s.length(), url); |
| 113 |
} |
| 114 |
|
| 115 |
std::string base64_encode(unsigned char const* bytes_to_encode, size_t in_len, bool url) |
| 116 |
{ |
| 117 |
|
| 118 |
size_t len_encoded = (in_len + 2) / 3 * 4; |
| 119 |
|
| 120 |
unsigned char trailing_char = url ? '.' : '='; |
| 121 |
|
| 122 |
// |
| 123 |
// Choose set of base64 characters. They differ |
| 124 |
// for the last two positions, depending on the url |
| 125 |
// parameter. |
| 126 |
// A bool (as is the parameter url) is guaranteed |
| 127 |
// to evaluate to either 0 or 1 in C++ therfore, |
| 128 |
// the correct character set is chosen by subscripting |
| 129 |
// base64_chars with url. |
| 130 |
// |
| 131 |
const char* base64_chars_ = base64_chars[url]; |
| 132 |
|
| 133 |
std::string ret; |
| 134 |
ret.reserve(len_encoded); |
| 135 |
|
| 136 |
unsigned int pos = 0; |
| 137 |
|
| 138 |
while (pos < in_len) { |
| 139 |
ret.push_back(base64_chars_[(bytes_to_encode[pos + 0] & 0xfc) >> 2]); |
| 140 |
|
| 141 |
if (pos + 1 < in_len) { |
| 142 |
ret.push_back(base64_chars_[((bytes_to_encode[pos + 0] & 0x03) << 4) + |
| 143 |
((bytes_to_encode[pos + 1] & 0xf0) >> 4)]); |
| 144 |
|
| 145 |
if (pos + 2 < in_len) { |
| 146 |
ret.push_back(base64_chars_[((bytes_to_encode[pos + 1] & 0x0f) << 2) + |
| 147 |
((bytes_to_encode[pos + 2] & 0xc0) >> 6)]); |
| 148 |
ret.push_back(base64_chars_[bytes_to_encode[pos + 2] & 0x3f]); |
| 149 |
} else { |
| 150 |
ret.push_back(base64_chars_[(bytes_to_encode[pos + 1] & 0x0f) << 2]); |
| 151 |
ret.push_back(trailing_char); |
| 152 |
} |
| 153 |
} else { |
| 154 |
|
| 155 |
ret.push_back(base64_chars_[(bytes_to_encode[pos + 0] & 0x03) << 4]); |
| 156 |
ret.push_back(trailing_char); |
| 157 |
ret.push_back(trailing_char); |
| 158 |
} |
| 159 |
|
| 160 |
pos += 3; |
| 161 |
} |
| 162 |
|
| 163 |
return ret; |
| 164 |
} |
| 165 |
|
| 166 |
template <typename String> |
| 167 |
static std::string decode(String encoded_string, bool remove_linebreaks) |
| 168 |
{ |
| 169 |
// |
| 170 |
// decode(…) is templated so that it can be used with String = const std::string& |
| 171 |
// or std::string_view (requires at least C++17) |
| 172 |
// |
| 173 |
|
| 174 |
if (remove_linebreaks) { |
| 175 |
|
| 176 |
if (!encoded_string.length()) { |
| 177 |
return ""; |
| 178 |
} |
| 179 |
|
| 180 |
std::string copy(encoded_string); |
| 181 |
|
| 182 |
size_t pos = 0; |
| 183 |
while ((pos = copy.find("\n", pos)) != std::string::npos) { |
| 184 |
copy.erase(pos, 1); |
| 185 |
} |
| 186 |
|
| 187 |
return base64_decode(copy, false); |
| 188 |
} |
| 189 |
|
| 190 |
size_t length_of_string = encoded_string.length(); |
| 191 |
if (!length_of_string) return std::string(""); |
| 192 |
|
| 193 |
size_t in_len = length_of_string; |
| 194 |
size_t pos = 0; |
| 195 |
|
| 196 |
// |
| 197 |
// The approximate length (bytes) of the decoded string might be one ore |
| 198 |
// two bytes smaller, depending on the amount of trailing equal signs |
| 199 |
// in the encoded string. This approximation is needed to reserve |
| 200 |
// enough space in the string to be returned. |
| 201 |
// |
| 202 |
size_t approx_length_of_decoded_string = length_of_string / 4 * 3; |
| 203 |
std::string ret; |
| 204 |
ret.reserve(approx_length_of_decoded_string); |
| 205 |
|
| 206 |
while (pos < in_len) { |
| 207 |
|
| 208 |
unsigned int pos_of_char_1 = pos_of_char(encoded_string[pos + 1]); |
| 209 |
|
| 210 |
ret.push_back(static_cast<std::string::value_type>( |
| 211 |
((pos_of_char(encoded_string[pos + 0])) << 2) + ((pos_of_char_1 & 0x30) >> 4))); |
| 212 |
|
| 213 |
if (encoded_string[pos + 2] != '=' && |
| 214 |
encoded_string[pos + 2] != |
| 215 |
'.') { // accept URL-safe base 64 strings, too, so check for '.' also. |
| 216 |
|
| 217 |
unsigned int pos_of_char_2 = pos_of_char(encoded_string[pos + 2]); |
| 218 |
ret.push_back(static_cast<std::string::value_type>(((pos_of_char_1 & 0x0f) << 4) + |
| 219 |
((pos_of_char_2 & 0x3c) >> 2))); |
| 220 |
|
| 221 |
if (encoded_string[pos + 3] != '=' && encoded_string[pos + 3] != '.') { |
| 222 |
ret.push_back(static_cast<std::string::value_type>( |
| 223 |
((pos_of_char_2 & 0x03) << 6) + pos_of_char(encoded_string[pos + 3]))); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
pos += 4; |
| 228 |
} |
| 229 |
|
| 230 |
return ret; |
| 231 |
} |
| 232 |
|
| 233 |
std::string base64_decode(std::string const& s, bool remove_linebreaks) |
| 234 |
{ |
| 235 |
return decode(s, remove_linebreaks); |
| 236 |
} |
| 237 |
|
| 238 |
std::string base64_encode(std::string const& s, bool url) { return encode(s, url); } |
| 239 |
|
| 240 |
std::string base64_encode_pem(std::string const& s) { return encode_pem(s); } |
| 241 |
|
| 242 |
std::string base64_encode_mime(std::string const& s) { return encode_mime(s); } |
| 243 |
|
| 244 |
#if __cplusplus >= 201703L |
| 245 |
// |
| 246 |
// Interface with std::string_view rather than const std::string& |
| 247 |
// Requires C++17 |
| 248 |
// Provided by Yannic Bonenberger (https://github.com/Yannic) |
| 249 |
// |
| 250 |
|
| 251 |
std::string base64_encode(std::string_view s, bool url) { return encode(s, url); } |
| 252 |
|
| 253 |
std::string base64_encode_pem(std::string_view s) { return encode_pem(s); } |
| 254 |
|
| 255 |
std::string base64_encode_mime(std::string_view s) { return encode_mime(s); } |
| 256 |
|
| 257 |
std::string base64_decode(std::string_view s, bool remove_linebreaks) |
| 258 |
{ |
| 259 |
return decode(s, remove_linebreaks); |
| 260 |
} |
| 261 |
|
| 262 |
#endif // __cplusplus >= 201703L |