|
5 | 5 | More information at
|
6 | 6 | https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp
|
7 | 7 |
|
8 |
| - Version: 2.rc.08 (release candidate) |
| 8 | + Version: 2.rc.09 (release candidate) |
9 | 9 |
|
10 |
| - Copyright (C) 2004-2017, 2020, 2021 René Nyffenegger |
| 10 | + Copyright (C) 2004-2017, 2020-2022 René Nyffenegger |
11 | 11 |
|
12 | 12 | This source code is provided 'as-is', without any express or implied
|
13 | 13 | warranty. In no event will the author be held liable for any damages
|
@@ -160,7 +160,7 @@ std::string base64_encode(unsigned char const* bytes_to_encode, size_t in_len, b
|
160 | 160 | }
|
161 | 161 |
|
162 | 162 | template <typename String>
|
163 |
| -static std::string decode(String encoded_string, bool remove_linebreaks) { |
| 163 | +static std::string decode(String const& encoded_string, bool remove_linebreaks) { |
164 | 164 | //
|
165 | 165 | // decode(…) is templated so that it can be used with String = const std::string&
|
166 | 166 | // or std::string_view (requires at least C++17)
|
@@ -204,33 +204,33 @@ static std::string decode(String encoded_string, bool remove_linebreaks) {
|
204 | 204 | // The last chunk produces at least one and up to three bytes.
|
205 | 205 | //
|
206 | 206 |
|
207 |
| - size_t pos_of_char_1 = pos_of_char(encoded_string[pos+1] ); |
| 207 | + size_t pos_of_char_1 = pos_of_char(encoded_string.at(pos+1) ); |
208 | 208 |
|
209 | 209 | //
|
210 | 210 | // Emit the first output byte that is produced in each chunk:
|
211 | 211 | //
|
212 |
| - ret.push_back(static_cast<std::string::value_type>( ( (pos_of_char(encoded_string[pos+0]) ) << 2 ) + ( (pos_of_char_1 & 0x30 ) >> 4))); |
| 212 | + ret.push_back(static_cast<std::string::value_type>( ( (pos_of_char(encoded_string.at(pos+0)) ) << 2 ) + ( (pos_of_char_1 & 0x30 ) >> 4))); |
213 | 213 |
|
214 | 214 | if ( ( pos + 2 < length_of_string ) && // Check for data that is not padded with equal signs (which is allowed by RFC 2045)
|
215 |
| - encoded_string[pos+2] != '=' && |
216 |
| - encoded_string[pos+2] != '.' // accept URL-safe base 64 strings, too, so check for '.' also. |
| 215 | + encoded_string.at(pos+2) != '=' && |
| 216 | + encoded_string.at(pos+2) != '.' // accept URL-safe base 64 strings, too, so check for '.' also. |
217 | 217 | )
|
218 | 218 | {
|
219 | 219 | //
|
220 | 220 | // Emit a chunk's second byte (which might not be produced in the last chunk).
|
221 | 221 | //
|
222 |
| - unsigned int pos_of_char_2 = pos_of_char(encoded_string[pos+2] ); |
| 222 | + unsigned int pos_of_char_2 = pos_of_char(encoded_string.at(pos+2) ); |
223 | 223 | ret.push_back(static_cast<std::string::value_type>( (( pos_of_char_1 & 0x0f) << 4) + (( pos_of_char_2 & 0x3c) >> 2)));
|
224 | 224 |
|
225 | 225 | if ( ( pos + 3 < length_of_string ) &&
|
226 |
| - encoded_string[pos+3] != '=' && |
227 |
| - encoded_string[pos+3] != '.' |
| 226 | + encoded_string.at(pos+3) != '=' && |
| 227 | + encoded_string.at(pos+3) != '.' |
228 | 228 | )
|
229 | 229 | {
|
230 | 230 | //
|
231 | 231 | // Emit a chunk's third byte (which might not be produced in the last chunk).
|
232 | 232 | //
|
233 |
| - ret.push_back(static_cast<std::string::value_type>( ( (pos_of_char_2 & 0x03 ) << 6 ) + pos_of_char(encoded_string[pos+3]) )); |
| 233 | + ret.push_back(static_cast<std::string::value_type>( ( (pos_of_char_2 & 0x03 ) << 6 ) + pos_of_char(encoded_string.at(pos+3)) )); |
234 | 234 | }
|
235 | 235 | }
|
236 | 236 |
|
|
0 commit comments