4 #include "AudioBasic/Str.h"
5 #include "AudioCodecs/AudioEncoded.h"
10 enum Base46Logic { NoCR, CRforFrame, CRforWrite };
11 static char encoding_table[] = {
12 'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
13 'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
14 'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
15 'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
16 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'+',
'/'};
17 static int mod_table[] = {0, 2, 1};
19 static const int B64index[256] = {
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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
22 0, 0, 0, 0, 0, 0, 0, 62, 63, 62, 62, 63, 52, 53, 54, 55, 56, 57,
23 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6,
24 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25 25, 0, 0, 0, 0, 63, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
26 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};
60 void setNewLine(Base46Logic logic) { newline_logic = logic; }
62 bool begin()
override {
64 is_valid = newline_logic == NoCR;
81 size_t write(
const void *data,
size_t byteCount)
override {
82 if (p_print ==
nullptr)
return 0;
84 addToBuffer((uint8_t *)data, byteCount);
86 while (buffer.
available() >= decode_size) {
87 uint8_t tmp[decode_size];
89 decodeLine(tmp, decode_size);
95 operator bool()
override {
return active; }
99 bool is_valid =
false;
100 Base46Logic newline_logic = CRforFrame;
101 Vector<uint8_t> result;
102 RingBuffer<uint8_t> buffer{1500};
105 void decodeLine(uint8_t *data,
size_t byteCount) {
106 LOGD(
"decode: %d", (
int)byteCount);
109 unsigned char *p = (
unsigned char *)data;
110 int pad = len > 0 && (len % 4 || p[len - 1] ==
'=');
111 const size_t L = ((len + 3) / 4 - pad) * 4;
112 result.resize(L / 4 * 3 + pad);
113 memset(result.data(), 0, result.size());
115 for (
size_t i = 0, j = 0; i < L; i += 4) {
116 int32_t n =
static_cast<int32_t
>(B64index[p[i]]) << 18 | B64index[p[i + 1]] << 12 |
117 B64index[p[i + 2]] << 6 | B64index[p[i + 3]];
118 result[j++] = n >> 16;
119 result[j++] = n >> 8 & 0xFF;
120 result[j++] = n & 0xFF;
123 int32_t n =
static_cast<int32_t
>(B64index[p[L]]) << 18 | B64index[p[L + 1]] << 12;
124 result[result.size() - 1] = n >> 16;
126 if (len > L + 2 && p[L + 2] !=
'=') {
127 n |= B64index[p[L + 2]] << 6;
128 result.push_back(n >> 8 & 0xFF);
131 writeBlocking(p_print, result.data(), result.size());
134 void addToBuffer(uint8_t *data,
size_t len) {
136 if (buffer.
size() < len) {
142 for (
int j = 0; j < len; j++) {
143 if (data[j] ==
'\n') {
153 for (
int j = start; j < len; j++) {
154 if (!isspace(data[j])) {
155 buffer.
write(data[j]);
156 }
else if (data[j] ==
'\n') {
159 LOGW(
"Resync %d (-%d)...", buffer.
available(), offset);
166 LOGD(
"buffer: %d, is_valid: %s", buffer.
available(),
167 is_valid ?
"true" :
"false");
192 p_print = &out_buffeream;
196 const char *
mime()
override {
return "text/base64"; }
204 frame_size = info.bits_per_sample * info.channels / 8;
205 if (newline_logic != NoCR) {
207 LOGW(
"AudioInfo not defined");
211 p_print->write(
'\n');
218 void end()
override { is_open =
false; }
221 virtual size_t write(
const void *binary,
size_t len)
override {
222 LOGD(
"EncoderBase64::write: %d", (
int)len);
223 uint8_t *data = (uint8_t *)binary;
225 switch (newline_logic) {
228 encodeLine((uint8_t *)binary, len);
231 int frames = len / frame_size;
235 int write_size = min(frame_size, open);
236 encodeLine(data + offset, write_size);
238 offset += write_size;
247 operator bool()
override {
return is_open; }
249 bool isOpen() {
return is_open; }
252 Print *p_print =
nullptr;
254 Base46Logic newline_logic = CRforFrame;
261 # if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(3, 3, 5)
269 void encodeLine(uint8_t *data,
size_t input_length) {
270 LOGD(
"EncoderBase64::encodeLine: %d", (
int)input_length);
271 int output_length = 4 * ((input_length + 2) / 3);
272 if (ret.size() < output_length + 1) {
273 ret.resize(output_length + 1);
276 for (
int i = 0, j = 0; i < input_length;) {
277 uint32_t octet_a = i < input_length ? (
unsigned char)data[i++] : 0;
278 uint32_t octet_b = i < input_length ? (
unsigned char)data[i++] : 0;
279 uint32_t octet_c = i < input_length ? (
unsigned char)data[i++] : 0;
281 uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
283 ret[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
284 ret[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
285 ret[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
286 ret[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
289 for (
int i = 0; i < mod_table[input_length % 3]; i++)
290 ret[output_length - 1 - i] =
'=';
293 if (newline_logic != NoCR) {
294 ret[output_length] =
'\n';
298 writeBlocking(p_print, ret.data(), output_length);