4 constexpr char CODE[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
6 uint8_t indexOf(
char search) {
7 for(uint8_t i = 0; i < 64; i++) {
8 if(::CODE[i] == search) {
16 void to6x4(uint8_t* input, uint8_t* output) {
17 output[0] = (input[0] & 0xFC) >> 2;
18 output[1] = ((input[0] & 0x03) << 4) + ((input[1] & 0xF0) >> 4);
19 output[2] = ((input[1] & 0x0F) << 2) + ((input[2] & 0xC0) >> 6);
20 output[3] = input[2] & 0x3F;
23 void to8x3(uint8_t* input, uint8_t* output) {
24 output[0] = (input[0] << 2) + ((input[1] & 0x30) >> 4);
25 output[1] = ((input[1] & 0x0F) << 4) + ((input[2] & 0x3C) >> 2);
26 output[2] = ((input[2] & 0x03) << 6) + input[3];
32 uint8_t bit8x3[3] = {};
33 uint8_t bit6x4[4] = {};
35 while(inputLength--) {
36 bit8x3[position++] = *input++;
39 ::to6x4(bit8x3, bit6x4);
41 for(
const auto &v: bit6x4) {
42 *output++ = ::CODE[v];
50 for(uint8_t i = position; i < 3; i++) {
54 ::to6x4(bit8x3, bit6x4);
56 for(uint8_t i = 0; i < position + 1; i++) {
57 *output++ = ::CODE[bit6x4[i]];
60 while(position++ < 3) {
69 return (inputLength + 2 - ((inputLength + 2) % 3)) / 3 * 4 + 1;
74 uint8_t bit8x3[3] = {};
75 uint8_t bit6x4[4] = {};
77 while(inputLength--) {
82 bit6x4[position++] = ::indexOf(*input++);
85 ::to8x3(bit6x4, bit8x3);
87 for(
const auto &v: bit8x3) {
96 for(uint8_t i = position; i < 4; i++) {
100 ::to8x3(bit6x4, bit8x3);
102 for(uint8_t i = 0; i < position - 1; i++) {
103 *output++ = bit8x3[i];
109 auto inputLength = strlen(input);
112 input += inputLength - 1;
114 while(*input-- ==
'=') {
118 return 6 * inputLength / 8 - equal;
size_t decodeLength(const char *input)
void decode(const char *input, size_t inputLength, uint8_t *output)
void encode(const uint8_t *input, size_t inputLength, char *output)
size_t encodeLength(size_t inputLength)