Size the distance tree to its alphabet

This commit is contained in:
J. Nick Koston
2026-09-08 13:43:50 +02:00
parent 49f9eaacb1
commit 87dc5014a8
2 changed files with 10 additions and 3 deletions
@@ -195,7 +195,7 @@ static int tinf_decode_symbol(TINF_DATA *d, TINF_TREE *t) {
} while (cur >= 0);
sum += cur;
if (sum < 0 || sum >= TINF_ARRAY_SIZE(t->trans)) {
if (sum < 0 || sum >= t->size) {
return TINF_DATA_ERROR;
}
@@ -433,6 +433,10 @@ void ota_inflate_init(TINF_DATA *d, unsigned char *dict, unsigned int dict_len)
d->dict_ring = dict;
d->dict_idx = 0;
d->curlen = 0;
d->ltree.trans = d->ltrans;
d->ltree.size = TINF_ARRAY_SIZE(d->ltrans);
d->dtree.trans = d->dtrans;
d->dtree.size = TINF_ARRAY_SIZE(d->dtrans);
}
/* inflate next output bytes from compressed stream */
@@ -19,8 +19,9 @@ enum OtaInflateResult {
};
struct OtaInflateTree {
uint16_t table[16]; /* table of code length counts */
uint16_t trans[288]; /* code -> symbol translation table */
uint16_t table[16]; /* table of code length counts */
uint16_t *trans; /* code -> symbol translation table, size entries */
uint16_t size;
};
struct OtaInflateState {
@@ -51,6 +52,8 @@ struct OtaInflateState {
struct OtaInflateTree ltree; /* dynamic length/symbol tree */
struct OtaInflateTree dtree; /* dynamic distance tree */
uint16_t ltrans[288];
uint16_t dtrans[32]; /* the distance alphabet has 30 symbols, so the tree is kept small */
};
/* dict must be at least as large as the window the encoder used (its max back reference distance) */