Fit Nordic Full KISS with lossless assets and firmware lifetime trimming

This commit is contained in:
mikecarper committed 2026-10-26 18:21:18 -07:00
1 parent 2369a3d28c
commit b4bec19e88
51 files changed
+7739 -17

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
#if defined(NRF52_PLATFORM) && defined(MESH_NRF52_NO_PROGRAM_EXIT) && MESH_NRF52_NO_PROGRAM_EXIT
// The qualified Adafruit nRF52 Arduino main never exits: the scheduler runs
// forever and its only return path invokes NVIC_SystemReset(). Device reboot
// and update paths also reset the MCU rather than terminating a C++ process.
// Do not retain registrations for global process-exit cleanup which cannot
// run. Automatic RAII destructors, explicit close/shutdown and dynamic delete
// remain ordinary C++ operations and are unaffected by this ABI entry point.
// nrf52_flash_trim.py enables this only for the exact reviewed core main hash.
extern "C" __attribute__((visibility("hidden"))) int __aeabi_atexit(
void*, void (*)(void*), void*) {
return 0;
}
#endif
+4
View File
@@ -4,7 +4,11 @@
#include <Arduino.h>
#include "bible/ReaderLookup.h"
#if defined(NRF52_PLATFORM)
#include "bible/ReaderStreamData.generated.h"
#else
#include "bible/ReaderData.generated.h"
#endif
namespace mesh {
bible::LookupResult readReaderVerse(bible::Reference ref, char* scratch,
File diff suppressed because it is too large. Load diff
+126
View File
@@ -0,0 +1,126 @@
#pragma once
#include "ReaderLookup.h"
namespace mesh { namespace bible {
// Independent bounded-history streams trade cold Reader decode time for flash.
// The existing ESP32 block corpus and its decoder remain unchanged.
struct StreamTokenCorpus {
const char* translation;
const char* attribution;
const uint8_t* data;
uint32_t data_size;
uint32_t encoded_size;
const uint8_t* words;
uint16_t words_size;
uint16_t word_plain_size;
uint16_t word_count;
uint16_t verse_count;
uint16_t chunk_count; // Zero retains the single-stream fixture format.
};
inline LookupResult lookup(const StreamTokenCorpus& corpus, Reference ref,
char* scratch, size_t capacity, const char*& text) {
text = nullptr;
uint16_t index;
if (!verseNumber(ref, index)) return LookupResult::Invalid;
if (!scratch || capacity < kBlockSize || !corpus.data || !corpus.data_size
|| !corpus.encoded_size || corpus.verse_count != kVerseCount
|| corpus.chunk_count > kVerseCount
|| corpus.word_count > 128 || corpus.word_plain_size > kBlockSize
|| (corpus.word_count && (!corpus.words || !corpus.words_size
|| corpus.word_plain_size < corpus.word_count * 2U))
|| (!corpus.word_count && (corpus.words_size || corpus.word_plain_size)))
return LookupResult::Corrupt;
const uint8_t* source = corpus.data;
uint32_t source_size = corpus.data_size, output_limit = corpus.encoded_size;
if (corpus.chunk_count) {
// Each independently compressed stream has a bounded six-byte header:
// first verse, decoded byte limit and compressed byte length, little-endian.
const uint8_t* cursor = corpus.data;
uint32_t remaining = corpus.data_size, total = 0;
const uint16_t wanted = index;
uint16_t previous = 0;
for (uint16_t n = 0; n < corpus.chunk_count; ++n) {
if (remaining < 6) return LookupResult::Corrupt;
const uint16_t first = cursor[0] | (uint16_t(cursor[1]) << 8);
const uint16_t plain = cursor[2] | (uint16_t(cursor[3]) << 8);
const uint16_t packed = cursor[4] | (uint16_t(cursor[5]) << 8);
cursor += 6;
remaining -= 6;
if (!plain || !packed || packed > remaining || first >= kVerseCount
|| (n ? first <= previous : first != 0)) return LookupResult::Corrupt;
if (first <= wanted) {
source = cursor;
source_size = packed;
output_limit = plain;
index = wanted - first;
}
previous = first;
total += plain;
cursor += packed;
remaining -= packed;
}
if (remaining || total != corpus.encoded_size) return LookupResult::Corrupt;
}
unsigned int encoded_size = 0;
if (tinf_uncompress_record(scratch, kBlockSize, &encoded_size,
source, source_size, output_limit, index) != TINF_OK
|| encoded_size >= kBlockSize) return LookupResult::Corrupt;
if (!encoded_size) return LookupResult::Missing;
bool tokenized = false;
for (unsigned int i = 0; i < encoded_size; ++i) {
const uint8_t byte = static_cast<uint8_t>(scratch[i]);
if (byte >= 128) {
if (byte - 128U >= corpus.word_count) return LookupResult::Corrupt;
tokenized = true;
} else if (byte < 32 || byte >= 127) return LookupResult::Corrupt;
}
if (!tokenized) { text = scratch; return LookupResult::Found; }
// Reuse the history after inflation. Keep the selected encoded record at
// the far end, the decoded vocabulary at the beginning, and expand into the
// gap. The host packer uses literal fallback for a long verse that would
// not fit these three regions, preserving the original 4095-byte limit.
uint8_t* encoded = reinterpret_cast<uint8_t*>(scratch) + kBlockSize - encoded_size - 1;
if (encoded <= reinterpret_cast<uint8_t*>(scratch) + corpus.word_plain_size)
return LookupResult::Corrupt;
memmove(encoded, scratch, encoded_size + 1);
unsigned int dictionary_size = corpus.word_plain_size;
if (tinf_uncompress_exact(scratch, &dictionary_size, corpus.words, corpus.words_size) != TINF_OK
|| dictionary_size != corpus.word_plain_size) return LookupResult::Corrupt;
const char* dictionary_end = scratch + dictionary_size;
const char* check = scratch;
for (unsigned int n = 0; n < corpus.word_count; ++n) {
const char* zero = static_cast<const char*>(memchr(check, 0, dictionary_end - check));
if (!zero || zero == check) return LookupResult::Corrupt;
for (const char* p = check; p != zero; ++p)
if (*p < 32 || static_cast<uint8_t>(*p) >= 127) return LookupResult::Corrupt;
check = zero + 1;
}
if (check != dictionary_end) return LookupResult::Corrupt;
char* dest = scratch + dictionary_size;
const char* const output = dest;
const char* const end = reinterpret_cast<char*>(encoded) - 1;
while (*encoded) {
const uint8_t byte = *encoded++;
const uint8_t* word = &byte;
size_t length = 1;
if (byte >= 128) {
const unsigned int token = byte - 128;
const char* value = scratch;
for (unsigned int n = 0; n < token; ++n) value += strlen(value) + 1;
word = reinterpret_cast<const uint8_t*>(value);
length = strlen(value);
}
if (length > static_cast<size_t>(end - dest)) return LookupResult::Corrupt;
memcpy(dest, word, length);
dest += length;
}
*dest = 0;
text = output;
return LookupResult::Found;
}
}} // namespace mesh::bible
+7
View File
@@ -11,8 +11,15 @@
/* nRF52's Arduino recipe defaults to -Ofast, which more than doubles this
* decoder. Keep the size policy local to tinf instead of changing MeshCore. */
#pragma GCC push_options
#if defined(NRF52_PLATFORM)
#pragma GCC optimize ("Oz")
#else
#pragma GCC optimize ("Os")
#endif
#endif
#if (defined(NRF52_PLATFORM) && COMPANION_FEATURE_READER) || defined(TINF_READER_STREAM_TEST)
#define MESHCORE_TINF_RECORD 1
#endif
#define MESHCORE_TINF_IMPLEMENTATION 1
#include "tinf/tinflate.c"
#if MESHCORE_OTA_DEVICE_DEFLATE
+8
View File
@@ -22,3 +22,11 @@ with neither feature links no decoder code or persistent RAM.
Only the raw RFC 1951 decoder is integrated. The zlib/gzip wrappers and checksum
sources are intentionally omitted. The original zlib license is preserved in
LICENSE and in the source headers.
The nRF52 Full Companion Reader also enables reader_record.inc. This MeshCore
extension shares the same bounded bit reader and Huffman trees, keeps only
4096 bytes of circular history in the caller's existing Reader scratch, and
stops at the requested NUL-delimited record. It rejects distances beyond that
history, records exceeding the original 4095-byte limit, truncated inputs and
output beyond the generated corpus limit. No heap or retained decode cache is
used. The ordinary linear decoder and ESP32 Reader remain available unchanged.
+137
View File
@@ -0,0 +1,137 @@
/* MeshCore extension (2026): read a NUL-delimited record through a caller-owned
* 4 KiB DEFLATE history. It shares tinf's bounded bit reader and Huffman trees.
* No heap, permanent cache, or second output/history buffer is used. */
struct tinf_record_output {
unsigned char *window;
unsigned int size, produced, limit, remaining, length;
};
static int tinf_record_emit(struct tinf_record_output *out, unsigned char byte)
{
if (out->produced >= out->limit) return TINF_DATA_ERROR;
out->window[out->produced++ & (out->size - 1)] = byte;
if (byte == 0) {
if (!out->remaining) return 1;
--out->remaining;
out->length = 0;
} else if (++out->length >= out->size) {
return TINF_BUF_ERROR;
}
return TINF_OK;
}
static int tinf_record_block(struct tinf_data *d, struct tinf_record_output *out)
{
static const unsigned char length_bits[29] = {
0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0
};
static const unsigned short length_base[29] = {
3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258
};
static const unsigned char dist_bits[30] = {
0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13
};
static const unsigned short dist_base[30] = {
1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,
1025,1537,2049,3073,4097,6145,8193,12289,16385,24577
};
for (;;) {
int sym = tinf_decode_symbol(d, &d->ltree);
if (d->overflow) return TINF_DATA_ERROR;
if (sym == 256) return TINF_OK;
if (sym < 256) {
const int result = tinf_record_emit(out, (unsigned char) sym);
if (result) return result;
} else {
int length, distance, offset;
if (sym > d->ltree.max_sym || sym - 257 > 28 || d->dtree.max_sym == -1)
return TINF_DATA_ERROR;
sym -= 257;
length = tinf_getbits_base(d, length_bits[sym], length_base[sym]);
distance = tinf_decode_symbol(d, &d->dtree);
if (distance > d->dtree.max_sym || distance > 29) return TINF_DATA_ERROR;
offset = tinf_getbits_base(d, dist_bits[distance], dist_base[distance]);
if (d->overflow || (unsigned int) offset > out->produced
|| (unsigned int) offset > out->size) return TINF_DATA_ERROR;
while (length--) {
const unsigned char byte = out->window[(out->produced - offset) & (out->size - 1)];
const int result = tinf_record_emit(out, byte);
if (result) return result;
}
}
}
}
static void tinf_record_reverse(unsigned char *begin, unsigned char *end)
{
while (begin < end) {
unsigned char byte;
--end;
if (begin == end) break;
byte = *begin;
*begin++ = *end;
*end = byte;
}
}
int tinf_uncompress_record(void *window, unsigned int window_size,
unsigned int *record_length, const void *source, unsigned int source_len,
unsigned int output_limit, unsigned int record_index)
{
struct tinf_data d;
struct tinf_record_output out;
int final = 0;
if (!window || !record_length || !source || !source_len || !output_limit
|| window_size != 4096) return TINF_DATA_ERROR;
*record_length = 0;
d.source = (const unsigned char *) source;
d.source_end = d.source + source_len;
d.tag = 0;
d.bitcount = d.overflow = 0;
out.window = (unsigned char *) window;
out.size = window_size;
out.produced = out.length = 0;
out.limit = output_limit;
out.remaining = record_index;
do {
unsigned int type;
int result;
final = tinf_getbits(&d, 1);
type = tinf_getbits(&d, 2);
if (d.overflow) return TINF_DATA_ERROR;
if (type == 0) {
unsigned int length;
if (d.source_end - d.source < 4) return TINF_DATA_ERROR;
length = read_le16(d.source);
if (length != (~read_le16(d.source + 2) & 0xffff)) return TINF_DATA_ERROR;
d.source += 4;
if ((unsigned int) (d.source_end - d.source) < length) return TINF_DATA_ERROR;
d.tag = 0;
d.bitcount = 0;
result = TINF_OK;
while (length--) {
result = tinf_record_emit(&out, *d.source++);
if (result) break;
}
} else if (type == 1 || type == 2) {
if (type == 1) {
tinf_build_fixed_trees(&d.ltree, &d.dtree);
result = TINF_OK;
} else result = tinf_decode_trees(&d, &d.ltree, &d.dtree);
if (result == TINF_OK) result = tinf_record_block(&d, &out);
} else return TINF_DATA_ERROR;
if (result == 1) {
/* Rotate in place: the selected record may cross the circular boundary.
* Include its NUL and use only initialized history bytes. */
const unsigned int used = out.produced < out.size ? out.produced : out.size;
const unsigned int begin = (out.produced - out.length - 1) & (out.size - 1);
tinf_record_reverse(out.window, out.window + begin);
tinf_record_reverse(out.window + begin, out.window + used);
tinf_record_reverse(out.window, out.window + used);
*record_length = out.length;
return TINF_OK;
}
if (result != TINF_OK) return result;
} while (!final);
return TINF_DATA_ERROR; /* Selected record or its terminator is missing. */
}
+7
View File
@@ -93,6 +93,13 @@ int TINFCC tinf_uncompress(void *dest, unsigned int *destLen,
int TINFCC tinf_uncompress_exact(void *dest, unsigned int *destLen,
const void *source, unsigned int sourceLen);
/* MeshCore: stop after one NUL-delimited record, using the caller's 4096-byte
* circular history as the final contiguous record buffer. output_limit bounds
* total inflated bytes, including preceding records. No heap is used. */
int TINFCC tinf_uncompress_record(void *window, unsigned int window_size,
unsigned int *record_length, const void *source, unsigned int source_len,
unsigned int output_limit, unsigned int record_index);
/**
* Decompress `sourceLen` bytes of gzip data from `source` to `dest`.
*
+4
View File
@@ -643,6 +643,10 @@ int tinf_uncompress_exact(void *dest, unsigned int *destLen,
return tinf_uncompress_internal(dest, destLen, source, sourceLen, 1);
}
#if defined(MESHCORE_TINF_RECORD)
#include "reader_record.inc"
#endif
/* clang -g -O1 -fsanitize=fuzzer,address -DTINF_FUZZING tinflate.c */
#if defined(TINF_FUZZING)
#include <limits.h>
+13
View File
@@ -6,11 +6,24 @@ namespace mesh {
namespace ui {
// Packed monochrome bitmap metrics, with GFX-style baseline offsets.
#if defined(NRF52_PLATFORM)
// Both fixed ASCII fonts use offsets <= 209, dimensions <= 7, advances <= 6
// and baseline offsets -5..1. Keep every metric/pixel while storing the two
// 95-glyph tables in 570 bytes instead of 1520; no decoded cache is needed.
// Native and Cortex-M4 tests check every source metric and rendered glyph.
struct __attribute__((packed)) SmallFontGlyph {
uint8_t bitmap_offset;
uint16_t width : 3, height : 3, advance : 3;
int16_t x_offset : 3, y_offset : 4;
};
static_assert(sizeof(SmallFontGlyph) == 3, "Compact font metric layout changed");
#else
struct SmallFontGlyph {
uint16_t bitmap_offset;
uint8_t width, height, advance;
int8_t x_offset, y_offset;
};
#endif
} // namespace ui
} // namespace mesh