NOTE: This code came up with the following stackoverflow post: https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c
| 1936 | // NOTE: This code came up with the following stackoverflow post: |
| 1937 | // https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c |
| 1938 | inline std::string base64_encode(const std::string &in) { |
| 1939 | static const auto lookup = |
| 1940 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 1941 | |
| 1942 | std::string out; |
| 1943 | out.reserve(in.size()); |
| 1944 | |
| 1945 | int val = 0; |
| 1946 | int valb = -6; |
| 1947 | |
| 1948 | for (auto c : in) { |
| 1949 | val = (val << 8) + static_cast<uint8_t>(c); |
| 1950 | valb += 8; |
| 1951 | while (valb >= 0) { |
| 1952 | out.push_back(lookup[(val >> valb) & 0x3F]); |
| 1953 | valb -= 6; |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | if (valb > -6) { out.push_back(lookup[((val << 8) >> (valb + 8)) & 0x3F]); } |
| 1958 | |
| 1959 | while (out.size() % 4) { |
| 1960 | out.push_back('='); |
| 1961 | } |
| 1962 | |
| 1963 | return out; |
| 1964 | } |
| 1965 | |
| 1966 | inline bool is_file(const std::string &path) { |
| 1967 | #ifdef _WIN32 |
no test coverage detected