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) {
128 for (i = 0; i < 4; i++) {
129 array4[i] =
static_cast<unsigned char>(chars.find(array4[i]));
131 array3[0] = (array4[0] << 2) + ((array4[1] & 0x30) >> 4);
132 array3[1] = ((array4[1] & 0xf) << 4) + ((array4[2] & 0x3c) >> 2);
133 array3[2] = ((array4[2] & 0x3) << 6) + array4[3];
134 for (i = 0; i < 3; i++) {
144 for (j = i; j < 4; j++) {
147 for (j = 0; j < 4; j++) {
148 array4[j] =
static_cast<unsigned char>(chars.find(array4[j]));
150 array3[0] = (array4[0] << 2) + ((array4[1] & 0x30) >> 4);
151 array3[1] = ((array4[1] & 0xf) << 4) + ((array4[2] & 0x3c) >> 2);
152 array3[2] = ((array4[2] & 0x3) << 6) + array4[3];
153 for (j = 0; (j < i - 1); j++) {
170 inline std::string
encode(
const std::string& s)
216 #endif // __STOUT_BASE64_HPP__ Definition: errorbase.hpp:36
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_url_safe(const std::string &s, bool padding=true)
Encode a string to Base64 with a URL and filename safe alphabet.
Definition: base64.hpp:196
Definition: attributes.hpp:24
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:209
std::string stringify(int flags)
Definition: base64.hpp:24
constexpr char STANDARD_CHARS[]
Definition: base64.hpp:32
Try< std::string > decode(const std::string &s, const std::string &chars)
Definition: base64.hpp:92