13 #ifndef __STOUT_BASE64_HPP__
14 #define __STOUT_BASE64_HPP__
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
34 "abcdefghijklmnopqrstuvwxyz"
38 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
39 "abcdefghijklmnopqrstuvwxyz"
45 const std::string& chars,
51 unsigned char array3[3];
52 unsigned char array4[4];
53 const char* bytesToEncode = s.c_str();
54 size_t length = s.size();
57 array3[i++] = *(bytesToEncode++);
59 array4[0] = (array3[0] & 0xfc) >> 2;
60 array4[1] = ((array3[0] & 0x03) << 4) + ((array3[1] & 0xf0) >> 4);
61 array4[2] = ((array3[1] & 0x0f) << 2) + ((array3[2] & 0xc0) >> 6);
62 array4[3] = array3[2] & 0x3f;
63 for (i = 0; i < 4; i++) {
64 result += chars[array4[i]];
71 for (j = i; j < 3; j++) {
74 array4[0] = (array3[0] & 0xfc) >> 2;
75 array4[1] = ((array3[0] & 0x03) << 4) + ((array3[1] & 0xf0) >> 4);
76 array4[2] = ((array3[1] & 0x0f) << 2) + ((array3[2] & 0xc0) >> 6);
77 array4[3] = array3[2] & 0x3f;
78 for (j = 0; j < i + 1; j++) {
79 result += chars[array4[j]];
94 auto isBase64 = [&chars](
unsigned char c) ->
bool {
95 return (isalnum(c) || (c == chars[62]) || (c == chars[63]));
99 unsigned char array3[3];
100 unsigned char array4[4];
103 foreach (
unsigned char c, s) {
117 for (i = 0; i < 4; i++) {
118 array4[i] =
static_cast<unsigned char>(chars.find(array4[i]));
120 array3[0] = (array4[0] << 2) + ((array4[1] & 0x30) >> 4);
121 array3[1] = ((array4[1] & 0xf) << 4) + ((array4[2] & 0x3c) >> 2);
122 array3[2] = ((array4[2] & 0x3) << 6) + array4[3];
123 for (i = 0; i < 3; i++) {
133 for (j = i; j < 4; j++) {
136 for (j = 0; j < 4; j++) {
137 array4[j] =
static_cast<unsigned char>(chars.find(array4[j]));
139 array3[0] = (array4[0] << 2) + ((array4[1] & 0x30) >> 4);
140 array3[1] = ((array4[1] & 0xf) << 4) + ((array4[2] & 0x3c) >> 2);
141 array3[2] = ((array4[2] & 0x3) << 6) + array4[3];
142 for (j = 0; (j < i - 1); j++) {
159 inline std::string
encode(
const std::string& s)
205 #endif // __STOUT_BASE64_HPP__
Try< std::string > decode(const std::string &s)
Decode a string that is Base64-encoded with the standard Base64 alphabet.
Definition: base64.hpp:172
Definition: errorbase.hpp:35
std::string encode(const std::string &s, const std::string &chars, bool padding)
Definition: base64.hpp:43
constexpr char URL_SAFE_CHARS[]
Definition: base64.hpp:37
std::string encode(const std::string &s)
Encode a string to Base64 with the standard Base64 alphabet.
Definition: base64.hpp:159
std::string encode_url_safe(const std::string &s, bool padding=true)
Encode a string to Base64 with a URL and filename safe alphabet.
Definition: base64.hpp:185
Try< std::string > decode_url_safe(const std::string &s)
Decode a string that is Base64-encoded with a URL and filename safe alphabet.
Definition: base64.hpp:198
std::string stringify(int flags)
constexpr char STANDARD_CHARS[]
Definition: base64.hpp:32
Try< std::string > decode(const std::string &s, const std::string &chars)
Definition: base64.hpp:92