62 static const int8_t decode_table[] = {
63 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
64 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
65 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1,
66 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
67 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
68 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63,
69 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
70 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
71 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
72 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
73 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
74 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
75 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
76 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
77 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
78 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
81 std::string padded = value;
82 while (padded.size() % 4 != 0) {
83 padded.push_back(
'=');
86 std::vector<uint8_t> result;
87 result.reserve((padded.size() / 4) * 3);
90 while (i < padded.size()) {
91 int8_t c0 = (padded[i] < 128) ? decode_table[
static_cast<uint8_t
>(padded[i])] : int8_t(-1);
92 int8_t c1 = (padded[i + 1] < 128) ? decode_table[
static_cast<uint8_t
>(padded[i + 1])] : int8_t(-1);
93 int8_t c2 = (padded[i + 2] < 128) ? decode_table[
static_cast<uint8_t
>(padded[i + 2])] : int8_t(-1);
94 int8_t c3 = (padded[i + 3] < 128) ? decode_table[
static_cast<uint8_t
>(padded[i + 3])] : int8_t(-1);
96 if (c0 < 0 || c1 < 0)
break;
97 if (c2 < 0 && padded[i + 2] !=
'=')
break;
98 if (c3 < 0 && padded[i + 3] !=
'=')
break;
100 uint32_t b = (
static_cast<uint32_t
>(c0) << 18)
101 | (
static_cast<uint32_t
>(c1) << 12)
102 | ((c2 >= 0 ?
static_cast<uint32_t
>(c2) : 0u) << 6)
103 | (c3 >= 0 ?
static_cast<uint32_t
>(c3) : 0u);
105 result.push_back(
static_cast<uint8_t
>((b >> 16) & 0xFF));
106 if (padded[i + 2] !=
'=') {
107 result.push_back(
static_cast<uint8_t
>((b >> 8) & 0xFF));
109 if (padded[i + 3] !=
'=') {
110 result.push_back(
static_cast<uint8_t
>(b & 0xFF));