49#define TINF_OK OTA_INFLATE_OK
50#define TINF_DONE OTA_INFLATE_DONE
51#define TINF_DATA_ERROR OTA_INFLATE_DATA_ERROR
52#define TINF_DICT_ERROR OTA_INFLATE_DICT_ERROR
53#define TINF_DATA struct OtaInflateState
54#define TINF_TREE struct OtaInflateTree
60static const unsigned char LENGTH_BITS[30] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2,
61 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5};
62static const unsigned short LENGTH_BASE[30] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27,
63 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
65static const unsigned char DIST_BITS[30] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6,
66 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
67static const unsigned short DIST_BASE[30] = {1, 2, 3, 4, 5, 7, 9, 13, 17, 25,
68 33, 49, 65, 97, 129, 193, 257, 385, 513, 769,
69 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
72static const unsigned char CLCIDX[] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
89static void tinf_build_tree(TINF_TREE *t,
const unsigned char *lengths,
unsigned int num,
unsigned int fast_bits) {
90 const unsigned int fast_size = 1u << fast_bits;
91 unsigned short offs[16],
next[16];
92 unsigned int i, sum, code;
94 for (i = 0; i < 16; ++i)
96 for (i = 0; i < num; ++i)
97 t->table[lengths[i]]++;
100 for (sum = 0, code = 0, i = 0; i < 16; ++i) {
104 code = (code +
t->table[i - 1]) << 1;
108 for (i = 0; i < fast_size; ++i)
111 for (i = 0; i < num; ++i) {
112 unsigned int len = lengths[i];
115 t->trans[offs[
len]++] = i;
116 if (
len <= fast_bits) {
118 unsigned int c =
next[
len]++, rev = 0, j;
119 for (j = 0; j < len; ++j, c >>= 1)
120 rev = (rev << 1) | (c & 1);
121 for (j = rev; j < fast_size; j += 1u <<
len)
132static inline void tinf_refill(TINF_DATA *d) {
133 while (d->bitcount <= 24 && d->source < d->source_limit) {
134 d->tag |= (
unsigned int) *d->source++ << d->bitcount;
140static inline void tinf_need(TINF_DATA *d,
unsigned int num) {
141 while (d->bitcount < num) {
143 if (d->source < d->source_limit) {
145 }
else if (!d->eof) {
146 val = d->source_read_cb(d);
152 d->tag |= (
unsigned int)
val << d->bitcount;
157static inline unsigned int tinf_take(TINF_DATA *d,
unsigned int num) {
158 unsigned int val = d->tag & ((1u << num) - 1);
165static inline unsigned int tinf_read_bits(TINF_DATA *d,
unsigned int num,
unsigned int base) {
167 return tinf_take(d, num) + base;
172static int tinf_decode_long(
const TINF_TREE *t,
unsigned int tag,
unsigned int *
len) {
173 unsigned int code = 0, first = 0, index = 0,
l;
175 unsigned int count =
t->table[
l];
177 if (code - first <
count) {
179 return t->trans[index + code - first];
182 first = (first +
count) << 1;
185 return TINF_DATA_ERROR;
190static int tinf_decode_symbol(TINF_DATA *d,
const TINF_TREE *t) {
194 int sym = tinf_decode_long(t, d->tag, &
len);
199 if (d->bitcount >= 15)
200 return TINF_DATA_ERROR;
201 tinf_need(d, d->bitcount + 1);
206static int tinf_decode_trees(TINF_DATA *d, TINF_TREE *lt, TINF_TREE *dt) {
208 unsigned char lengths[288 + 32];
209 unsigned int hlit, hdist, hclen, hlimit;
210 unsigned int i, num,
length;
213 hlit = tinf_read_bits(d, 5, 257);
216 hdist = tinf_read_bits(d, 5, 1);
219 hclen = tinf_read_bits(d, 4, 4);
221 for (i = 0; i < 19; ++i)
225 for (i = 0; i < hclen; ++i) {
227 unsigned int clen = tinf_read_bits(d, 3, 0);
229 lengths[CLCIDX[i]] = clen;
236 hlimit = hlit + hdist;
237 for (num = 0; num < hlimit;) {
238 int sym = tinf_decode_symbol(d, lt);
239 unsigned char fill_value = 0;
240 int lbits, lbase = 3;
250 return TINF_DATA_ERROR;
251 fill_value = lengths[num - 1];
265 lengths[num++] = sym;
271 length = tinf_read_bits(d, lbits, lbase);
272 if (num +
length > hlimit)
273 return TINF_DATA_ERROR;
275 lengths[num++] = fill_value;
280 if (lengths[256] == 0) {
281 return TINF_DATA_ERROR;
293static void tinf_build_fixed_trees(TINF_TREE *lt, TINF_TREE *dt) {
294 unsigned char lengths[288];
297 for (i = 0; i < 144; ++i)
307 for (i = 0; i < 32; ++i)
314static int tinf_inflate_fast(TINF_DATA *d,
const TINF_TREE *lt,
const TINF_TREE *dt) {
315 const unsigned char *
src = d->source;
316 const unsigned char *
const src_end = d->source_limit;
317 unsigned char *
const ring = d->dict_ring;
318 unsigned char *
const ring_end = ring + d->dict_size;
319 unsigned char *
out = d->dest;
320 const uint16_t *
const lfast = lt->fast;
321 const uint16_t *
const dfast = dt->fast;
322 unsigned int tag = d->tag, bits = d->bitcount;
326 while (bits <= 24) { \
327 tag |= (unsigned int) *src++ << bits; \
330#define DROP(n) (tag >>= (n), bits -= (n))
342 sym = tinf_decode_long(lt, tag, &
len);
360 res = TINF_DATA_ERROR;
364 unsigned int length = LENGTH_BASE[sym] + (
tag & ((1u << LENGTH_BITS[sym]) - 1));
365 DROP(LENGTH_BITS[sym]);
373 sym = tinf_decode_long(dt, tag, &
len);
375 if (sym < 0 || sym >= 30) {
376 res = TINF_DATA_ERROR;
381 unsigned int offs = DIST_BASE[sym] + (
tag & ((1u << DIST_BITS[sym]) - 1));
382 DROP(DIST_BITS[sym]);
383 if (offs > d->dict_size) {
384 res = TINF_DICT_ERROR;
389 unsigned int at =
out - ring;
390 const unsigned char *from = ring + (at >= offs ? at - offs : at + d->dict_size - offs);
391 unsigned int n = ring_end -
out;
397 unsigned int run = ring_end - from;
404 if (from == ring_end)
409 d->lz_off = from - ring;
427static int tinf_inflate_block_data(TINF_DATA *d, TINF_TREE *lt, TINF_TREE *dt) {
428 unsigned char *
const ring_end = d->dict_ring + d->dict_size;
429 while (d->dest < ring_end) {
430 if (d->curlen != 0) {
431 unsigned int n = ring_end - d->dest;
436 *d->dest++ = d->dict_ring[d->lz_off];
437 if ((
unsigned) ++d->lz_off == d->dict_size)
444 int res = tinf_inflate_fast(d, lt, dt);
452 int sym = tinf_decode_symbol(d, lt);
455 return TINF_DATA_ERROR;
472 return TINF_DATA_ERROR;
474 d->curlen = tinf_read_bits(d, LENGTH_BITS[sym], LENGTH_BASE[sym]);
476 dist = tinf_decode_symbol(d, dt);
477 if (dist < 0 || dist >= 30)
478 return TINF_DATA_ERROR;
480 offs = tinf_read_bits(d, DIST_BITS[dist], DIST_BASE[dist]);
481 if (offs > d->dict_size)
482 return TINF_DICT_ERROR;
484 d->lz_off = (int) (d->dest - d->dict_ring) - (int) offs;
486 d->lz_off += d->dict_size;
492static int tinf_inflate_uncompressed_block(TINF_DATA *d) {
493 if (d->curlen == 0) {
494 unsigned int length, invlength;
497 tinf_take(d, d->bitcount & 7);
498 length = tinf_read_bits(d, 16, 0);
499 invlength = tinf_read_bits(d, 16, 0);
500 if (
length != (~invlength & 0x0000ffff))
501 return TINF_DATA_ERROR;
506 if (--d->curlen == 0)
510 unsigned char c = tinf_read_bits(d, 8, 0);
521 d->source_limit = NULL;
529 d->dict_size = dict_len;
532 d->ltree.trans = d->ltrans;
533 d->ltree.fast = d->lfast;
534 d->dtree.trans = d->dtrans;
535 d->dtree.fast = d->dfast;
540 if (d->dest == d->dict_ring + d->dict_size)
541 d->dest = d->dict_ring;
546 if (d->btype == -1) {
549 old_btype = d->btype;
550 d->bfinal = tinf_read_bits(d, 1, 0);
551 d->btype = tinf_read_bits(d, 2, 0);
553 if (d->btype == 1 && old_btype != 1) {
554 tinf_build_fixed_trees(&d->ltree, &d->dtree);
555 }
else if (d->btype == 2) {
556 res = tinf_decode_trees(d, &d->ltree, &d->dtree);
564 res = tinf_inflate_uncompressed_block(d);
568 res = tinf_inflate_block_data(d, &d->ltree, &d->dtree);
571 return TINF_DATA_ERROR;
574 if (res == TINF_DONE && !d->bfinal)
580 }
while (d->dest < d->dict_ring + d->dict_size);
std::vector< uint8_t > & out
static float float float t
void ota_inflate_init(TINF_DATA *d, unsigned char *dict, unsigned int dict_len)
int ota_inflate(TINF_DATA *d)
@ OTA_INFLATE_LIT_FAST_BITS
@ OTA_INFLATE_DIST_FAST_BITS