base64 encoder snippet altered from https://stackoverflow.com/a/37109258
| 40 | |
| 41 | /// base64 encoder snippet altered from https://stackoverflow.com/a/37109258 |
| 42 | std::string encode(const std::vector<byte>& buf) |
| 43 | { |
| 44 | std::size_t len = buf.size(); |
| 45 | std::vector<byte> res_vec((len + 2) / 3 * 4, '='); |
| 46 | std::size_t j = 0; |
| 47 | std::size_t remaining = len % 3; |
| 48 | const size_t last = len - remaining; |
| 49 | |
| 50 | for(size_t i = 0; i < last; i += 3) |
| 51 | { |
| 52 | std::size_t n = static_cast<std::size_t>(buf.at(i)) << 16u | |
| 53 | static_cast<std::size_t>(buf.at(i + 1)) << 8u | |
| 54 | static_cast<std::size_t>(buf.at(i + 2)); |
| 55 | res_vec.at(j++) = b64_chars.at(n >> 18u); |
| 56 | res_vec.at(j++) = b64_chars.at(n >> 12u & 0x3Fu); |
| 57 | res_vec.at(j++) = b64_chars.at(n >> 6u & 0x3Fu); |
| 58 | res_vec.at(j++) = b64_chars.at(n & 0x3Fu); |
| 59 | } |
| 60 | // Set padding |
| 61 | if(remaining != 0) |
| 62 | { |
| 63 | std::size_t n = --remaining == 0 ? static_cast<std::size_t>(buf.at(last)) |
| 64 | : static_cast<std::size_t>(buf.at(last)) << 8u | |
| 65 | static_cast<std::size_t>(buf.at(last + 1)); |
| 66 | res_vec.at(j++) = b64_chars.at(remaining == 0 ? n >> 2u : n >> 10u & 0x3Fu); |
| 67 | res_vec.at(j++) = b64_chars.at(remaining == 0 ? n << 4u & 0x3Fu : n >> 4u & 0x03Fu); |
| 68 | res_vec.at(j++) = remaining == 0 ? '=' : b64_chars.at(n << 2u & 0x3Fu); |
| 69 | } |
| 70 | return {res_vec.begin(), res_vec.end()}; |
| 71 | } |
| 72 | |
| 73 | } // namespace |
| 74 |
no test coverage detected