NOTE: This code came up with the following stackoverflow post: https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c
| 2058 | // NOTE: This code came up with the following stackoverflow post: |
| 2059 | // https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c |
| 2060 | inline std::string base64_encode(const std::string &in) { |
| 2061 | static const auto lookup = |
| 2062 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 2063 | |
| 2064 | std::string out; |
| 2065 | out.reserve(in.size()); |
| 2066 | |
| 2067 | int val = 0; |
| 2068 | int valb = -6; |
| 2069 | |
| 2070 | for (auto c : in) { |
| 2071 | val = (val << 8) + static_cast<uint8_t>(c); |
| 2072 | valb += 8; |
| 2073 | while (valb >= 0) { |
| 2074 | out.push_back(lookup[(val >> valb) & 0x3F]); |
| 2075 | valb -= 6; |
| 2076 | } |
| 2077 | } |
| 2078 | |
| 2079 | if (valb > -6) { out.push_back(lookup[((val << 8) >> (valb + 8)) & 0x3F]); } |
| 2080 | |
| 2081 | while (out.size() % 4) { |
| 2082 | out.push_back('='); |
| 2083 | } |
| 2084 | |
| 2085 | return out; |
| 2086 | } |
| 2087 | |
| 2088 | inline bool is_file(const std::string &path) { |
| 2089 | #ifdef _WIN32 |
no test coverage detected