NOTE: This code came up with the following stackoverflow post: https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c
| 1989 | // NOTE: This code came up with the following stackoverflow post: |
| 1990 | // https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c |
| 1991 | inline std::string base64_encode(const std::string &in) { |
| 1992 | static const auto lookup = |
| 1993 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 1994 | |
| 1995 | std::string out; |
| 1996 | out.reserve(in.size()); |
| 1997 | |
| 1998 | int val = 0; |
| 1999 | int valb = -6; |
| 2000 | |
| 2001 | for (auto c : in) { |
| 2002 | val = (val << 8) + static_cast<uint8_t>(c); |
| 2003 | valb += 8; |
| 2004 | while (valb >= 0) { |
| 2005 | out.push_back(lookup[(val >> valb) & 0x3F]); |
| 2006 | valb -= 6; |
| 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | if (valb > -6) { out.push_back(lookup[((val << 8) >> (valb + 8)) & 0x3F]); } |
| 2011 | |
| 2012 | while (out.size() % 4) { |
| 2013 | out.push_back('='); |
| 2014 | } |
| 2015 | |
| 2016 | return out; |
| 2017 | } |
| 2018 | |
| 2019 | inline bool is_file(const std::string &path) { |
| 2020 | #ifdef _WIN32 |
no test coverage detected