12 [[nodiscard]]
static constexpr
char encode(uint8_t c)
14 constexpr
const char*
const base64_chars =
15 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
16 "abcdefghijklmnopqrstuvwxyz"
19 return base64_chars[c];
22 [[nodiscard]]
static constexpr uint8_t decode(uint8_t c)
24 if (
'A' <= c && c <=
'Z') {
26 }
else if (
'a' <= c && c <=
'z') {
28 }
else if (
'0' <= c && c <=
'9') {
30 }
else if (c ==
'+') {
32 }
else if (c ==
'/') {
39 string encode(
const uint8_t* input,
size_t inSize)
41 constexpr
int CHUNKS = 19;
42 constexpr
int IN_CHUNKS = 3 * CHUNKS;
43 constexpr
int OUT_CHUNKS = 4 * CHUNKS;
45 auto outSize = ((inSize + (IN_CHUNKS - 1)) / IN_CHUNKS) * (OUT_CHUNKS + 1);
46 string ret(outSize, 0);
50 if (out) ret[out++] =
'\n';
51 auto n2 = std::min<size_t>(IN_CHUNKS, inSize);
52 auto n = unsigned(n2);
53 for (; n >= 3; n -= 3) {
54 ret[out++] = encode( (input[0] & 0xfc) >> 2);
55 ret[out++] = encode(((input[0] & 0x03) << 4) +
56 ((input[1] & 0xf0) >> 4));
57 ret[out++] = encode(((input[1] & 0x0f) << 2) +
58 ((input[2] & 0xc0) >> 6));
59 ret[out++] = encode( (input[2] & 0x3f) >> 0);
63 uint8_t buf3[3] = { 0, 0, 0 };
68 buf4[0] = (buf3[0] & 0xfc) >> 2;
69 buf4[1] = ((buf3[0] & 0x03) << 4) +
70 ((buf3[1] & 0xf0) >> 4);
71 buf4[2] = ((buf3[1] & 0x0f) << 2) +
72 ((buf3[2] & 0xc0) >> 6);
73 buf4[3] = (buf3[2] & 0x3f) >> 0;
74 for (
auto j :
xrange(n + 1)) {
75 ret[out++] = encode(buf4[j]);
84 assert(outSize >= out);
89 std::pair<MemBuffer<uint8_t>,
size_t> decode(std::string_view input)
91 auto outSize = (input.size() * 3 + 3) / 4;
97 for (
auto c : input) {
98 uint8_t d = decode(c);
99 if (d == uint8_t(-1))
continue;
103 ret[out++] = char(((buf4[0] & 0xff) << 2) + ((buf4[1] & 0x30) >> 4));
104 ret[out++] = char(((buf4[1] & 0x0f) << 4) + ((buf4[2] & 0x3c) >> 2));
105 ret[out++] = char(((buf4[2] & 0x03) << 6) + ((buf4[3] & 0xff) >> 0));
109 for (
auto j :
xrange(i, 4u)) {
113 buf3[0] = ((buf4[0] & 0xff) << 2) + ((buf4[1] & 0x30) >> 4);
114 buf3[1] = ((buf4[1] & 0x0f) << 4) + ((buf4[2] & 0x3c) >> 2);
115 buf3[2] = ((buf4[2] & 0x03) << 6) + ((buf4[3] & 0xff) >> 0);
116 for (
auto j :
xrange(i - 1)) {
117 ret[out++] = buf3[j];
121 assert(outSize >= out);
123 return std::pair(std::move(ret), out);
131 for (
auto c : input) {
132 uint8_t d = decode(c);
133 if (d == uint8_t(-1))
continue;
137 if (
unlikely((out + 3) > outSize))
return false;
138 output[out++] = char(((buf4[0] & 0xff) << 2) + ((buf4[1] & 0x30) >> 4));
139 output[out++] = char(((buf4[1] & 0x0f) << 4) + ((buf4[2] & 0x3c) >> 2));
140 output[out++] = char(((buf4[2] & 0x03) << 6) + ((buf4[3] & 0xff) >> 0));
144 for (
auto j :
xrange(i, 4u)) {
148 buf3[0] = ((buf4[0] & 0xff) << 2) + ((buf4[1] & 0x30) >> 4);
149 buf3[1] = ((buf4[1] & 0x0f) << 4) + ((buf4[2] & 0x3c) >> 2);
150 buf3[2] = ((buf4[2] & 0x03) << 6) + ((buf4[3] & 0xff) >> 0);
151 for (
auto j :
xrange(i - 1)) {
152 if (
unlikely(out == outSize))
return false;
153 output[out++] = buf3[j];
157 return out == outSize;
This class manages the lifetime of a block of memory.
void resize(size_t size)
Grow or shrink the memory block.
bool decode_inplace(std::string_view input, uint8_t *output, size_t outSize)
constexpr auto xrange(T e)