5#include "ll/api/base/StdInt.h"
7namespace ll::inline utils::base64_utils {
9constexpr uchar encodeLookup(uchar c) {
10 return "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
11 "abcdefghijklmnopqrstuvwxyz"
14constexpr uchar decodeLookup(uchar c) {
15 if (c >=
'A' && c <=
'Z')
return c -
'A';
16 if (c >=
'a' && c <=
'z')
return c - 71;
17 if (c >=
'0' && c <=
'9')
return c + 4;
18 if (c ==
'+')
return 62;
19 if (c ==
'/')
return 63;
24constexpr size_t getEncodeLength(
size_t len) {
return (len + 2 - ((len + 2) % 3)) / 3 * 4; }
26constexpr size_t getEncodeLength(std::string_view str) {
return getEncodeLength(str.length()); }
28constexpr size_t getDecodeLength(std::string_view in) {
30 size_t input_size = in.size();
31 for (
auto it = in.rbegin(); it != in.rend() && *it ==
'='; ++it) {
36 while (input_size % 4) {
40 return ((6 * input_size) / 8) - count;
43constexpr std::string encode(std::string_view str) {
45 result.reserve(getEncodeLength(str));
49 i = (i << 8) + static_cast<uint8_t>(c);
52 result += detail::encodeLookup((i >> j) & 0x3F);
57 result += detail::encodeLookup(((i << 8) >> (j + 8)) & 0x3F);
59 while (result.size() % 4) {
60 result.push_back(
'=');
65constexpr std::string decode(std::string_view str) {
66 size_t input_size = str.size();
67 size_t output_size = getDecodeLength(str);
69 out.resize(output_size);
70 for (
size_t i = 0, j = 0; i < input_size;) {
71 uint32_t c1 = (i > input_size || str[i] ==
'=') ? 0 & i++ : detail::decodeLookup(str[i++]);
72 uint32_t c2 = (i > input_size || str[i] ==
'=') ? 0 & i++ : detail::decodeLookup(str[i++]);
73 uint32_t c3 = (i > input_size || str[i] ==
'=') ? 0 & i++ : detail::decodeLookup(str[i++]);
74 uint32_t c4 = (i > input_size || str[i] ==
'=') ? 0 & i++ : detail::decodeLookup(str[i++]);
76 uint32_t data = (c1 << 3 * 6) + (c2 << 2 * 6) + (c3 << 1 * 6) + (c4 << 0 * 6);
78 if (j < output_size) out[j++] =
static_cast<char>((data >> 2 * 8) & 0xFF);
79 if (j < output_size) out[j++] =
static_cast<char>((data >> 1 * 8) & 0xFF);
80 if (j < output_size) out[j++] =
static_cast<char>((data >> 0 * 8) & 0xFF);