| 198 | */ |
| 199 | template<typename Input_iterator, typename Output_iterator> |
| 200 | static Output_iterator decode(Input_iterator in_begin, Input_iterator in_end, Output_iterator out, |
| 201 | alphabet alphabet = alphabet::auto_, |
| 202 | decoding_behavior behavior = decoding_behavior::moderate) |
| 203 | { |
| 204 | //constexpr auto pad = '='; |
| 205 | std::uint8_t last = 0; |
| 206 | auto bits = 0; |
| 207 | |
| 208 | while (in_begin != in_end) { |
| 209 | auto c = *in_begin; |
| 210 | ++in_begin; |
| 211 | |
| 212 | if (c == '=') { |
| 213 | break; |
| 214 | } |
| 215 | |
| 216 | auto part = _base64_value(alphabet, c); |
| 217 | |
| 218 | // enough bits for one byte |
| 219 | if (bits + 6 >= 8) { |
| 220 | *out = (last << (8 - bits)) | (part >> (bits - 2)); |
| 221 | ++out; |
| 222 | |
| 223 | bits -= 2; |
| 224 | } else { |
| 225 | bits += 6; |
| 226 | } |
| 227 | |
| 228 | last = part; |
| 229 | } |
| 230 | |
| 231 | // check padding |
| 232 | if (behavior != decoding_behavior::loose) { |
| 233 | while (in_begin != in_end) { |
| 234 | auto c = *in_begin; |
| 235 | ++in_begin; |
| 236 | |
| 237 | if (c != '=') { |
| 238 | throw base64_error("invalid base64 character."); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return out; |
| 244 | } |
| 245 | /** |
| 246 | Decodes a string. |
| 247 |
no test coverage detected