mirror of
https://github.com/brazilofmux/tinymux
synced 2026-08-13 00:23:11 -04:00
Moved base64 encode/decode from websocket.cpp and grpc_web.cpp into a single shared implementation. Three overloads: raw bytes, std::string encode, and std::string decode. Removed declarations from grpc_web.h. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
77 lines
2.9 KiB
C++
77 lines
2.9 KiB
C++
#include "base64.h"
|
|
|
|
static const char b64chars[] =
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
|
static const uint8_t b64decode_table[256] = {
|
|
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
|
|
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
|
|
255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63,
|
|
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255,
|
|
255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
|
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255,
|
|
255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
|
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255,
|
|
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
|
|
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
|
|
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
|
|
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
|
|
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
|
|
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
|
|
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
|
|
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
|
|
};
|
|
|
|
std::string base64Encode(const uint8_t* p, size_t len) {
|
|
std::string out;
|
|
out.reserve(((len + 2) / 3) * 4);
|
|
|
|
size_t i = 0;
|
|
while (i + 2 < len) {
|
|
uint32_t v = (p[i] << 16) | (p[i+1] << 8) | p[i+2];
|
|
out.push_back(b64chars[(v >> 18) & 0x3F]);
|
|
out.push_back(b64chars[(v >> 12) & 0x3F]);
|
|
out.push_back(b64chars[(v >> 6) & 0x3F]);
|
|
out.push_back(b64chars[(v ) & 0x3F]);
|
|
i += 3;
|
|
}
|
|
if (i + 1 == len) {
|
|
uint32_t v = p[i] << 16;
|
|
out.push_back(b64chars[(v >> 18) & 0x3F]);
|
|
out.push_back(b64chars[(v >> 12) & 0x3F]);
|
|
out.push_back('=');
|
|
out.push_back('=');
|
|
} else if (i + 2 == len) {
|
|
uint32_t v = (p[i] << 16) | (p[i+1] << 8);
|
|
out.push_back(b64chars[(v >> 18) & 0x3F]);
|
|
out.push_back(b64chars[(v >> 12) & 0x3F]);
|
|
out.push_back(b64chars[(v >> 6) & 0x3F]);
|
|
out.push_back('=');
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::string base64Encode(const std::string& data) {
|
|
return base64Encode(reinterpret_cast<const uint8_t*>(data.data()),
|
|
data.size());
|
|
}
|
|
|
|
std::string base64Decode(const std::string& data) {
|
|
std::string out;
|
|
out.reserve(data.size() * 3 / 4);
|
|
|
|
uint32_t buf = 0;
|
|
int bits = 0;
|
|
for (char c : data) {
|
|
if (c == '=' || c == '\n' || c == '\r') continue;
|
|
uint8_t val = b64decode_table[static_cast<uint8_t>(c)];
|
|
if (val == 255) continue;
|
|
buf = (buf << 6) | val;
|
|
bits += 6;
|
|
if (bits >= 8) {
|
|
bits -= 8;
|
|
out.push_back(static_cast<char>((buf >> bits) & 0xFF));
|
|
}
|
|
}
|
|
return out;
|
|
}
|