mirror of
https://github.com/ratspeak/microReticulum
synced 2026-11-22 23:10:38 +00:00
Initial implementation of Base64 support in Bytes
This commit is contained in:
1 parent
491fbd034e
commit
23b285f3bf
6 files changed
+156
-8
No files matched your search
@@ -22,6 +22,9 @@ build_flags =
|
||||
-Wno-missing-field-initializers
|
||||
-Wno-format
|
||||
-Wno-unused-parameter
|
||||
-Wno-unused-variable
|
||||
-Wno-unused-but-set-variable
|
||||
-Wno-unused-private-field
|
||||
-Isrc
|
||||
-DLIBRARY_TEST
|
||||
;-DNDEBUG
|
||||
@@ -100,6 +103,10 @@ build_flags =
|
||||
-g
|
||||
;-O1
|
||||
-O0
|
||||
-Wunused-parameter
|
||||
-Wunused-variable
|
||||
-Wunused-but-set-variable
|
||||
-Wunused-private-field
|
||||
;-DBOARD_HAS_PSRAM=1
|
||||
;-DRNS_DEFAULT_ALLOCATOR=RNS_HEAP_ALLOCATOR
|
||||
-DRNS_DEFAULT_ALLOCATOR=RNS_HEAP_POOL_ALLOCATOR
|
||||
|
||||
@@ -162,6 +162,18 @@ void Bytes::appendHex(const uint8_t* hex, size_t hex_size) {
|
||||
const char hex_upper_chars[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
||||
const char hex_lower_chars[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
||||
|
||||
static const char base64_chars[65] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
static int8_t b64_decode_char(uint8_t c) {
|
||||
if (c >= 'A' && c <= 'Z') return c - 'A';
|
||||
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
|
||||
if (c >= '0' && c <= '9') return c - '0' + 52;
|
||||
if (c == '+') return 62;
|
||||
if (c == '/') return 63;
|
||||
return -1; // invalid / padding
|
||||
}
|
||||
|
||||
std::string RNS::hexFromByte(uint8_t byte, bool upper /*= true*/) {
|
||||
std::string hex;
|
||||
if (upper) {
|
||||
@@ -193,6 +205,77 @@ std::string Bytes::toHex(bool upper /*= false*/) const {
|
||||
return hex;
|
||||
}
|
||||
|
||||
std::string Bytes::toBase64() const {
|
||||
if (!_data) {
|
||||
return "";
|
||||
}
|
||||
const uint8_t* in = _data->data();
|
||||
size_t in_size = _data->size();
|
||||
std::string out;
|
||||
out.reserve(((in_size + 2) / 3) * 4);
|
||||
for (size_t i = 0; i < in_size; i += 3) {
|
||||
uint8_t b0 = in[i];
|
||||
uint8_t b1 = (i + 1 < in_size) ? in[i + 1] : 0;
|
||||
uint8_t b2 = (i + 2 < in_size) ? in[i + 2] : 0;
|
||||
out += base64_chars[(b0 >> 2) & 0x3F];
|
||||
out += base64_chars[((b0 << 4) | (b1 >> 4)) & 0x3F];
|
||||
out += (i + 1 < in_size) ? base64_chars[((b1 << 2) | (b2 >> 6)) & 0x3F] : '=';
|
||||
out += (i + 2 < in_size) ? base64_chars[b2 & 0x3F] : '=';
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
void Bytes::assignBase64(const uint8_t* b64, size_t b64_size) {
|
||||
if (b64 == nullptr || b64_size == 0) {
|
||||
_data = nullptr;
|
||||
_exclusive = true;
|
||||
return;
|
||||
}
|
||||
// strip trailing padding
|
||||
while (b64_size > 0 && b64[b64_size - 1] == '=') {
|
||||
--b64_size;
|
||||
}
|
||||
size_t out_size = (b64_size * 3) / 4;
|
||||
exclusiveData(false, out_size);
|
||||
_data->clear();
|
||||
for (size_t i = 0; i < b64_size; ) {
|
||||
int8_t v0 = (i < b64_size) ? b64_decode_char(b64[i++]) : 0;
|
||||
int8_t v1 = (i < b64_size) ? b64_decode_char(b64[i++]) : 0;
|
||||
int8_t v2 = (i < b64_size) ? b64_decode_char(b64[i++]) : -1;
|
||||
int8_t v3 = (i < b64_size) ? b64_decode_char(b64[i++]) : -1;
|
||||
if (v0 < 0 || v1 < 0) break;
|
||||
_data->push_back((uint8_t)((v0 << 2) | (v1 >> 4)));
|
||||
if (v2 >= 0) _data->push_back((uint8_t)((v1 << 4) | (v2 >> 2)));
|
||||
if (v3 >= 0) _data->push_back((uint8_t)((v2 << 6) | v3));
|
||||
}
|
||||
if (_data->empty()) {
|
||||
_data = nullptr;
|
||||
_exclusive = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Bytes::appendBase64(const uint8_t* b64, size_t b64_size) {
|
||||
if (b64 == nullptr || b64_size == 0) {
|
||||
return;
|
||||
}
|
||||
// strip trailing padding
|
||||
while (b64_size > 0 && b64[b64_size - 1] == '=') {
|
||||
--b64_size;
|
||||
}
|
||||
size_t out_size = (b64_size * 3) / 4;
|
||||
exclusiveData(true, size() + out_size);
|
||||
for (size_t i = 0; i < b64_size; ) {
|
||||
int8_t v0 = (i < b64_size) ? b64_decode_char(b64[i++]) : 0;
|
||||
int8_t v1 = (i < b64_size) ? b64_decode_char(b64[i++]) : 0;
|
||||
int8_t v2 = (i < b64_size) ? b64_decode_char(b64[i++]) : -1;
|
||||
int8_t v3 = (i < b64_size) ? b64_decode_char(b64[i++]) : -1;
|
||||
if (v0 < 0 || v1 < 0) break;
|
||||
_data->push_back((uint8_t)((v0 << 2) | (v1 >> 4)));
|
||||
if (v2 >= 0) _data->push_back((uint8_t)((v1 << 4) | (v2 >> 2)));
|
||||
if (v3 >= 0) _data->push_back((uint8_t)((v2 << 6) | v3));
|
||||
}
|
||||
}
|
||||
|
||||
// mid
|
||||
Bytes Bytes::mid(size_t beginpos, size_t len) const {
|
||||
if (!_data || beginpos >= size()) {
|
||||
|
||||
+12
-3
@@ -310,6 +310,13 @@ MEM("Creating from data-move...");
|
||||
void appendHex(const uint8_t* hex, size_t hex_size);
|
||||
inline void appendHex(const char* hex) { if (!hex) return; appendHex((uint8_t*)hex, strlen(hex)); }
|
||||
|
||||
void assignBase64(const uint8_t* b64, size_t b64_size);
|
||||
inline void assignBase64(const char* b64) { if (!b64) return; assignBase64((uint8_t*)b64, strlen(b64)); }
|
||||
|
||||
void appendBase64(const uint8_t* b64, size_t b64_size);
|
||||
inline void appendBase64(const char* b64) { if (!b64) return; appendBase64((uint8_t*)b64, strlen(b64)); }
|
||||
|
||||
|
||||
inline uint8_t* writable(size_t size) {
|
||||
if (size > 0) {
|
||||
// create exclusive data with reserved capacity
|
||||
@@ -354,6 +361,7 @@ MEM("Creating from data-move...");
|
||||
|
||||
inline std::string toString() const { if (!_data) return ""; return {(const char*)data(), size()}; }
|
||||
std::string toHex(bool upper = false) const;
|
||||
std::string toBase64() const;
|
||||
Bytes mid(size_t beginpos, size_t len) const;
|
||||
Bytes mid(size_t beginpos) const;
|
||||
inline Bytes left(size_t len) const { if (!_data) return NONE; if (len > size()) len = size(); return {data(), len}; }
|
||||
@@ -409,6 +417,7 @@ MEM("Creating from data-move...");
|
||||
|
||||
inline std::string stringFromBytes(const Bytes& bytes) { return bytes.toString(); }
|
||||
inline std::string hexFromBytes(const Bytes& bytes) { return bytes.toHex(); }
|
||||
inline std::string base64FromBytes(const Bytes& bytes) { return bytes.toBase64(); }
|
||||
std::string hexFromByte(uint8_t byte, bool upper = true);
|
||||
|
||||
}
|
||||
@@ -438,12 +447,12 @@ inline RNS::Bytes& operator << (RNS::Bytes& lhbytes, const char* rhstr) {
|
||||
namespace ArduinoJson {
|
||||
// Serialize
|
||||
inline bool convertToJson(const RNS::Bytes& src, JsonVariant dst) {
|
||||
return dst.set(src.toHex());
|
||||
return dst.set(src.toBase64());
|
||||
}
|
||||
// Deserialize
|
||||
inline void convertFromJson(JsonVariantConst src, RNS::Bytes& dst) {
|
||||
const char* hex = src.as<const char*>();
|
||||
if (hex) dst.assignHex(hex);
|
||||
const char* b64 = src.as<const char*>();
|
||||
if (b64) dst.assignBase64(b64);
|
||||
}
|
||||
inline bool canConvertFromJson(JsonVariantConst src, const RNS::Bytes&) {
|
||||
return src.is<const char*>();
|
||||
|
||||
+4
-3
@@ -3886,11 +3886,12 @@ TRACEF("Transport::write_path_table: buffer size %zu bytes", Persistence::_buffe
|
||||
for (const auto& [timestamp, destination_hash] : sorted_keys) {
|
||||
TRACEF("Transport::cull_path_table: Removing destination %s from path table", destination_hash.toHex().c_str());
|
||||
#if defined(RNS_USE_FS) && defined(RNS_PERSIST_PATHS)
|
||||
auto& destination_entry = get_path(destination_hash);
|
||||
if (destination_entry) {
|
||||
// Not using get_path() here because it performs an unnecessary announce packet load
|
||||
auto iter = _path_table.find(destination_hash);
|
||||
if (iter != _path_table.end()) {
|
||||
// Remove cached packet file associated with this destination
|
||||
char packet_cache_path[Type::Reticulum::FILEPATH_MAXSIZE];
|
||||
snprintf(packet_cache_path, Type::Reticulum::FILEPATH_MAXSIZE, "%s/%s", Reticulum::_cachepath, destination_entry.announce_packet_hash().toHex().c_str());
|
||||
snprintf(packet_cache_path, Type::Reticulum::FILEPATH_MAXSIZE, "%s/%s", Reticulum::_cachepath, (*iter).second.announce_packet_hash().toHex().c_str());
|
||||
if (OS::file_exists(packet_cache_path)) {
|
||||
OS::remove_file(packet_cache_path);
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ namespace ArduinoJson {
|
||||
JsonObject obj = dst.to<JsonObject>();
|
||||
for (const auto& item : src) {
|
||||
//obj[item.first] = item.second;
|
||||
obj[item.first.toHex()] = item.second;
|
||||
obj[item.first.toBase64()] = item.second;
|
||||
}
|
||||
}
|
||||
static std::map<RNS::Bytes, T> fromJson(JsonVariantConst src) {
|
||||
@@ -102,7 +102,7 @@ namespace ArduinoJson {
|
||||
for (JsonPairConst item : src.as<JsonObjectConst>()) {
|
||||
//dst[item.key().c_str()] = item.value().as<T>();
|
||||
RNS::Bytes key;
|
||||
key.assignHex(item.key().c_str());
|
||||
key.assignBase64(item.key().c_str());
|
||||
//dst[key] = item.value().as<T>();
|
||||
dst.insert({key, item.value().as<T>()});
|
||||
}
|
||||
|
||||
@@ -301,6 +301,53 @@ void testBytesConversion() {
|
||||
|
||||
}
|
||||
|
||||
void testBytesBase64() {
|
||||
|
||||
// encode "Hello World" → known base64 value
|
||||
{
|
||||
RNS::Bytes bytes("Hello World");
|
||||
std::string b64 = bytes.toBase64();
|
||||
TRACEF("text: \"%s\" base64: \"%s\"", bytes.toString().c_str(), b64.c_str());
|
||||
TEST_ASSERT_EQUAL_STRING("SGVsbG8gV29ybGQ=", b64.c_str());
|
||||
}
|
||||
|
||||
// decode known base64 string → "Hello World"
|
||||
{
|
||||
RNS::Bytes bytes;
|
||||
bytes.assignBase64("SGVsbG8gV29ybGQ=");
|
||||
std::string text = bytes.toString();
|
||||
TRACEF("base64: \"SGVsbG8gV29ybGQ=\" text: \"%s\"", text.c_str());
|
||||
TEST_ASSERT_EQUAL_size_t(11, text.length());
|
||||
TEST_ASSERT_EQUAL_STRING("Hello World", text.c_str());
|
||||
}
|
||||
|
||||
// round-trip stability
|
||||
{
|
||||
RNS::Bytes original("Hello World");
|
||||
std::string b64 = original.toBase64();
|
||||
RNS::Bytes restored;
|
||||
restored.assignBase64(b64.c_str());
|
||||
TEST_ASSERT_EQUAL_size_t(original.size(), restored.size());
|
||||
TEST_ASSERT_EQUAL_MEMORY(original.data(), restored.data(), original.size());
|
||||
}
|
||||
|
||||
// empty bytes → toBase64 returns ""
|
||||
{
|
||||
RNS::Bytes empty;
|
||||
TEST_ASSERT_EQUAL_STRING("", empty.toBase64().c_str());
|
||||
}
|
||||
|
||||
// assignBase64 then appendBase64 produces concatenated result
|
||||
{
|
||||
RNS::Bytes bytes;
|
||||
bytes.assignBase64("SGVsbG8="); // "Hello"
|
||||
bytes.appendBase64("IFdvcmxk"); // " World"
|
||||
TEST_ASSERT_EQUAL_size_t(11, bytes.size());
|
||||
TEST_ASSERT_EQUAL_MEMORY("Hello World", bytes.data(), bytes.size());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void testBytesResize() {
|
||||
TRACE("Testing downsize of Bytes with initial capacity");
|
||||
|
||||
@@ -667,6 +714,7 @@ int runUnityTests(void) {
|
||||
RUN_TEST(testBytesMain);
|
||||
RUN_TEST(testCowBytes);
|
||||
RUN_TEST(testBytesConversion);
|
||||
RUN_TEST(testBytesBase64);
|
||||
RUN_TEST(testBytesResize);
|
||||
RUN_TEST(testBytesStream);
|
||||
RUN_TEST(testBytesReserve);
|
||||
|
||||
Reference in new issue
Block a user