| 66 | }; |
| 67 | |
| 68 | std::vector<unsigned char> DecodeBase64(const std::string &input) { |
| 69 | using ret_type = std::vector<unsigned char>; |
| 70 | if (input.empty()) |
| 71 | return ret_type(); |
| 72 | |
| 73 | ret_type ret(3 * input.size() / 4 + 1); |
| 74 | unsigned char *out = &ret[0]; |
| 75 | |
| 76 | unsigned value = 0; |
| 77 | for (std::size_t i = 0, cnt = 0; i < input.size(); i++) { |
| 78 | if (std::isspace(static_cast<unsigned char>(input[i]))) { |
| 79 | // skip newlines |
| 80 | continue; |
| 81 | } |
| 82 | unsigned char d = decoding[static_cast<unsigned char>(input[i])]; |
| 83 | if (d == 255) |
| 84 | return ret_type(); |
| 85 | |
| 86 | value = (value << 6) | d; |
| 87 | if (cnt % 4 == 3) { |
| 88 | *out++ = value >> 16; |
| 89 | if (i > 0 && input[i - 1] != '=') |
| 90 | *out++ = value >> 8; |
| 91 | if (input[i] != '=') |
| 92 | *out++ = value; |
| 93 | } |
| 94 | ++cnt; |
| 95 | } |
| 96 | |
| 97 | ret.resize(out - &ret[0]); |
| 98 | return ret; |
| 99 | } |
| 100 | } // namespace YAML |