4 #include "AudioTools/AudioCodecs/AudioCodecsBase.h"
9 enum Base46Logic { NoCR, CRforFrame, CRforWrite };
10 static char encoding_table[] = {
11 'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
12 'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
13 'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
14 'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
15 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'+',
'/'};
16 static int mod_table[] = {0, 2, 1};
18 static const int B64index[256] = {
19 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
21 0, 0, 0, 0, 0, 0, 0, 62, 63, 62, 62, 63, 52, 53, 54, 55, 56, 57,
22 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6,
23 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
24 25, 0, 0, 0, 0, 63, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
25 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};
59 void setNewLine(Base46Logic logic) { newline_logic = logic; }
61 bool begin()
override {
63 is_valid = newline_logic == NoCR;
80 size_t write(
const uint8_t *data,
size_t len)
override {
81 if (p_print ==
nullptr)
return 0;
83 addToBuffer((uint8_t *)data, len);
85 while (buffer.
available() >= decode_size) {
86 uint8_t tmp[decode_size];
88 decodeLine(tmp, decode_size);
94 operator bool()
override {
return active; }
98 bool is_valid =
false;
99 Base46Logic newline_logic = CRforFrame;
100 Vector<uint8_t> result;
101 RingBuffer<uint8_t> buffer{1500};
104 void decodeLine(uint8_t *data,
size_t byteCount) {
105 LOGD(
"decode: %d", (
int)byteCount);
108 unsigned char *p = (
unsigned char *)data;
109 int pad = len > 0 && (len % 4 || p[len - 1] ==
'=');
110 const size_t L = ((len + 3) / 4 - pad) * 4;
111 result.resize(L / 4 * 3 + pad);
112 memset(result.data(), 0, result.size());
114 for (
size_t i = 0, j = 0; i < L; i += 4) {
115 int32_t n =
static_cast<int32_t
>(B64index[p[i]]) << 18 | B64index[p[i + 1]] << 12 |
116 B64index[p[i + 2]] << 6 | B64index[p[i + 3]];
117 result[j++] = n >> 16;
118 result[j++] = n >> 8 & 0xFF;
119 result[j++] = n & 0xFF;
122 int32_t n =
static_cast<int32_t
>(B64index[p[L]]) << 18 | B64index[p[L + 1]] << 12;
123 result[result.size() - 1] = n >> 16;
125 if (len > L + 2 && p[L + 2] !=
'=') {
126 n |= B64index[p[L + 2]] << 6;
127 result.push_back(n >> 8 & 0xFF);
130 writeBlocking(p_print, result.data(), result.size());
133 void addToBuffer(uint8_t *data,
size_t len) {
135 if (buffer.
size() < len) {
141 for (
int j = 0; j < len; j++) {
142 if (data[j] ==
'\n') {
152 for (
int j = start; j < len; j++) {
153 if (!isspace(data[j])) {
154 buffer.
write(data[j]);
155 }
else if (data[j] ==
'\n') {
158 LOGW(
"Resync %d (-%d)...", buffer.
available(), offset);
165 LOGD(
"buffer: %d, is_valid: %s", buffer.
available(),
166 is_valid ?
"true" :
"false");
191 p_print = &out_buffeream;
195 const char *
mime()
override {
return "text/base64"; }
203 frame_size = info.bits_per_sample * info.channels / 8;
204 if (newline_logic != NoCR) {
206 LOGW(
"AudioInfo not defined");
210 p_print->write(
'\n');
217 void end()
override { is_open =
false; }
220 virtual size_t write(
const uint8_t *data,
size_t len)
override {
221 LOGD(
"EncoderBase64::write: %d", (
int)len);
223 switch (newline_logic) {
226 encodeLine(data, len);
229 int frames = len / frame_size;
233 int write_size = min(frame_size, open);
234 encodeLine(data + offset, write_size);
236 offset += write_size;
245 operator bool()
override {
return is_open; }
247 bool isOpen() {
return is_open; }
250 Print *p_print =
nullptr;
252 Base46Logic newline_logic = CRforFrame;
259 # if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(3, 3, 5)
267 void encodeLine(
const uint8_t *data,
size_t input_length) {
268 LOGD(
"EncoderBase64::encodeLine: %d", (
int)input_length);
269 int output_length = 4 * ((input_length + 2) / 3);
270 if (ret.size() < output_length + 1) {
271 ret.resize(output_length + 1);
274 for (
int i = 0, j = 0; i < input_length;) {
275 uint32_t octet_a = i < input_length ? (
unsigned char)data[i++] : 0;
276 uint32_t octet_b = i < input_length ? (
unsigned char)data[i++] : 0;
277 uint32_t octet_c = i < input_length ? (
unsigned char)data[i++] : 0;
279 uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
281 ret[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
282 ret[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
283 ret[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
284 ret[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
287 for (
int i = 0; i < mod_table[input_length % 3]; i++)
288 ret[output_length - 1 - i] =
'=';
291 if (newline_logic != NoCR) {
292 ret[output_length] =
'\n';
296 writeBlocking(p_print, ret.data(), output_length);