From 23b285f3bfd9939b441c3b6cda62b8ba11b52c52 Mon Sep 17 00:00:00 2001 From: Chad Attermann Date: Sun, 8 Mar 2026 12:25:36 -0600 Subject: [PATCH] Initial implementation of Base64 support in Bytes --- platformio.ini | 7 +++ src/Bytes.cpp | 83 +++++++++++++++++++++++++++++++++++ src/Bytes.h | 15 +++++-- src/Transport.cpp | 7 +-- src/Utilities/Persistence.h | 4 +- test/test_bytes/test_main.cpp | 48 ++++++++++++++++++++ 6 files changed, 156 insertions(+), 8 deletions(-) diff --git a/platformio.ini b/platformio.ini index 4c19681..bc00782 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/src/Bytes.cpp b/src/Bytes.cpp index 393bc93..5e83de9 100644 --- a/src/Bytes.cpp +++ b/src/Bytes.cpp @@ -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()) { diff --git a/src/Bytes.h b/src/Bytes.h index cc91064..0674855 100644 --- a/src/Bytes.h +++ b/src/Bytes.h @@ -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(); - if (hex) dst.assignHex(hex); + const char* b64 = src.as(); + if (b64) dst.assignBase64(b64); } inline bool canConvertFromJson(JsonVariantConst src, const RNS::Bytes&) { return src.is(); diff --git a/src/Transport.cpp b/src/Transport.cpp index 05def93..a5cedf9 100644 --- a/src/Transport.cpp +++ b/src/Transport.cpp @@ -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); } diff --git a/src/Utilities/Persistence.h b/src/Utilities/Persistence.h index c39adb5..092d3e7 100644 --- a/src/Utilities/Persistence.h +++ b/src/Utilities/Persistence.h @@ -94,7 +94,7 @@ namespace ArduinoJson { JsonObject obj = dst.to(); 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 fromJson(JsonVariantConst src) { @@ -102,7 +102,7 @@ namespace ArduinoJson { for (JsonPairConst item : src.as()) { //dst[item.key().c_str()] = item.value().as(); RNS::Bytes key; - key.assignHex(item.key().c_str()); + key.assignBase64(item.key().c_str()); //dst[key] = item.value().as(); dst.insert({key, item.value().as()}); } diff --git a/test/test_bytes/test_main.cpp b/test/test_bytes/test_main.cpp index e554b41..7ee87e1 100644 --- a/test/test_bytes/test_main.cpp +++ b/test/test_bytes/test_main.cpp @@ -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);