ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
ota_esphome_inflate.c
Go to the documentation of this file.
1/*
2 * uzlib - tiny deflate/inflate library (deflate, gzip, zlib)
3 *
4 * Copyright (c) 2003 by Joergen Ibsen / Jibz
5 * All Rights Reserved
6 * http://www.ibsensoftware.com/
7 *
8 * Copyright (c) 2014-2018 by Paul Sokolovsky
9 *
10 * This software is provided 'as-is', without any express
11 * or implied warranty. In no event will the authors be
12 * held liable for any damages arising from the use of
13 * this software.
14 *
15 * Permission is granted to anyone to use this software
16 * for any purpose, including commercial applications,
17 * and to alter it and redistribute it freely, subject to
18 * the following restrictions:
19 *
20 * 1. The origin of this software must not be
21 * misrepresented; you must not claim that you
22 * wrote the original software. If you use this
23 * software in a product, an acknowledgment in
24 * the product documentation would be appreciated
25 * but is not required.
26 *
27 * 2. Altered source versions must be plainly marked
28 * as such, and must not be misrepresented as
29 * being the original software.
30 *
31 * 3. This notice may not be removed or altered from
32 * any source distribution.
33 */
34
35/*
36 * Altered for ESPHome: this is the raw deflate decoder from uzlib's
37 * tinflate.c (v2.9.5) with the gzip/zlib header parsers, checksums,
38 * runtime table builder and in-memory (non ring window) output path
39 * removed, and the public names prefixed with ota_inflate. Symbols are
40 * decoded from a 32 bit bit buffer through 9 bit lookup tables, with a
41 * fast loop for when a whole symbol fits in both the input and the output,
42 * and the output is written straight into the ring window.
43 */
44
45#include "ota_esphome_inflate.h"
46
47#include <stddef.h>
48
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
55
56/* --------------------------------------------------- *
57 * -- constant tables (upstream builds them at runtime) -- *
58 * --------------------------------------------------- */
59
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};
64
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};
70
71/* special ordering of code length codes */
72static const unsigned char CLCIDX[] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
73
74/* ----------------------- *
75 * -- utility functions -- *
76 * ----------------------- */
77
78enum {
81 /* a lookup table entry is (code length << SYM_BITS) | symbol; 0 marks a longer code */
83 SYM_MASK = (1 << SYM_BITS) - 1,
84 /* input bytes one pass of the fast loop may take: three refills of up to 4 bytes */
86};
87
88/* given an array of code lengths, build a tree and its lookup table */
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;
93
94 for (i = 0; i < 16; ++i)
95 t->table[i] = 0;
96 for (i = 0; i < num; ++i)
97 t->table[lengths[i]]++;
98 t->table[0] = 0;
99
100 for (sum = 0, code = 0, i = 0; i < 16; ++i) {
101 offs[i] = sum;
102 sum += t->table[i];
103 if (i > 0)
104 code = (code + t->table[i - 1]) << 1;
105 next[i] = code;
106 }
107
108 for (i = 0; i < fast_size; ++i)
109 t->fast[i] = 0;
110
111 for (i = 0; i < num; ++i) {
112 unsigned int len = lengths[i];
113 if (!len)
114 continue;
115 t->trans[offs[len]++] = i;
116 if (len <= fast_bits) {
117 /* codes arrive MSB first, the bit buffer is LSB first */
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)
122 t->fast[j] = (unsigned short) ((len << SYM_BITS) | i);
123 }
124 }
125}
126
127/* ---------------------- *
128 * -- decode functions -- *
129 * ---------------------- */
130
131/* top the bit buffer up from the input buffer, without blocking for more */
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;
135 d->bitcount += 8;
136 }
137}
138
139/* make at least num (<= 16) bits available; past the end of input reads zeros and sets eof */
140static inline void tinf_need(TINF_DATA *d, unsigned int num) {
141 while (d->bitcount < num) {
142 int val = -1;
143 if (d->source < d->source_limit) {
144 val = *d->source++;
145 } else if (!d->eof) {
146 val = d->source_read_cb(d);
147 }
148 if (val < 0) {
149 d->eof = true;
150 val = 0;
151 }
152 d->tag |= (unsigned int) val << d->bitcount;
153 d->bitcount += 8;
154 }
155}
156
157static inline unsigned int tinf_take(TINF_DATA *d, unsigned int num) {
158 unsigned int val = d->tag & ((1u << num) - 1);
159 d->tag >>= num;
160 d->bitcount -= num;
161 return val;
162}
163
164/* read a num bit value from a stream and add base */
165static inline unsigned int tinf_read_bits(TINF_DATA *d, unsigned int num, unsigned int base) {
166 tinf_need(d, num);
167 return tinf_take(d, num) + base;
168}
169
170/* canonical decode of the code at the bottom of tag; len gets the code length.
171 The trans index stays below the tree's symbol count, which trans always holds */
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;
174 for (l = 1; l < 16; ++l, tag >>= 1) {
175 unsigned int count = t->table[l];
176 code |= tag & 1;
177 if (code - first < count) {
178 *len = l;
179 return t->trans[index + code - first];
180 }
181 index += count;
182 first = (first + count) << 1;
183 code <<= 1;
184 }
185 return TINF_DATA_ERROR;
186}
187
188/* decode a symbol, reading more input only while the code is not all in the bit
189 buffer; bits above bitcount are zero, so a code no longer than bitcount is real */
190static int tinf_decode_symbol(TINF_DATA *d, const TINF_TREE *t) {
191 tinf_refill(d);
192 for (;;) {
193 unsigned int len;
194 int sym = tinf_decode_long(t, d->tag, &len);
195 if (sym >= 0 && len <= d->bitcount) {
196 tinf_take(d, len);
197 return sym;
198 }
199 if (d->bitcount >= 15)
200 return TINF_DATA_ERROR;
201 tinf_need(d, d->bitcount + 1);
202 }
203}
204
205/* given a data stream, decode dynamic trees from it */
206static int tinf_decode_trees(TINF_DATA *d, TINF_TREE *lt, TINF_TREE *dt) {
207 /* code lengths for 288 literal/len symbols and 32 dist symbols */
208 unsigned char lengths[288 + 32];
209 unsigned int hlit, hdist, hclen, hlimit;
210 unsigned int i, num, length;
211
212 /* get 5 bits HLIT (257-286) */
213 hlit = tinf_read_bits(d, 5, 257);
214
215 /* get 5 bits HDIST (1-32) */
216 hdist = tinf_read_bits(d, 5, 1);
217
218 /* get 4 bits HCLEN (4-19) */
219 hclen = tinf_read_bits(d, 4, 4);
220
221 for (i = 0; i < 19; ++i)
222 lengths[i] = 0;
223
224 /* read code lengths for code length alphabet */
225 for (i = 0; i < hclen; ++i) {
226 /* get 3 bits code length (0-7) */
227 unsigned int clen = tinf_read_bits(d, 3, 0);
228
229 lengths[CLCIDX[i]] = clen;
230 }
231
232 /* build code length tree, temporarily use length tree */
233 tinf_build_tree(lt, lengths, 19, LIT_FAST_BITS);
234
235 /* decode code lengths for the dynamic trees */
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;
241
242 /* error decoding */
243 if (sym < 0)
244 return sym;
245
246 switch (sym) {
247 case 16:
248 /* copy previous code length 3-6 times (read 2 bits) */
249 if (num == 0)
250 return TINF_DATA_ERROR;
251 fill_value = lengths[num - 1];
252 lbits = 2;
253 break;
254 case 17:
255 /* repeat code length 0 for 3-10 times (read 3 bits) */
256 lbits = 3;
257 break;
258 case 18:
259 /* repeat code length 0 for 11-138 times (read 7 bits) */
260 lbits = 7;
261 lbase = 11;
262 break;
263 default:
264 /* values 0-15 represent the actual code lengths */
265 lengths[num++] = sym;
266 /* continue the for loop */
267 continue;
268 }
269
270 /* special code length 16-18 are handled here */
271 length = tinf_read_bits(d, lbits, lbase);
272 if (num + length > hlimit)
273 return TINF_DATA_ERROR;
274 for (; length; --length) {
275 lengths[num++] = fill_value;
276 }
277 }
278
279 /* Check that there's "end of block" symbol */
280 if (lengths[256] == 0) {
281 return TINF_DATA_ERROR;
282 }
283
284 /* build dynamic trees */
285 tinf_build_tree(lt, lengths, hlit, LIT_FAST_BITS);
286 tinf_build_tree(dt, lengths + hlit, hdist, DIST_FAST_BITS);
287
288 return TINF_OK;
289}
290
291/* build the fixed huffman trees (RFC 1951 3.2.6) through the generic tree
292 builder; altered from upstream, which unrolls them by hand */
293static void tinf_build_fixed_trees(TINF_TREE *lt, TINF_TREE *dt) {
294 unsigned char lengths[288];
295 unsigned int i;
296
297 for (i = 0; i < 144; ++i)
298 lengths[i] = 8;
299 for (; i < 256; ++i)
300 lengths[i] = 9;
301 for (; i < 280; ++i)
302 lengths[i] = 7;
303 for (; i < 288; ++i)
304 lengths[i] = 8;
305 tinf_build_tree(lt, lengths, 288, LIT_FAST_BITS);
306
307 for (i = 0; i < 32; ++i)
308 lengths[i] = 5;
309 tinf_build_tree(dt, lengths, 32, DIST_FAST_BITS);
310}
311
312/* inflate with the bit buffer and both cursors in locals while the input holds a
313 whole symbol; a match that runs past the end of the ring is left in curlen */
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;
323 int res = TINF_OK;
324
325#define REFILL() \
326 while (bits <= 24) { \
327 tag |= (unsigned int) *src++ << bits; \
328 bits += 8; \
329 }
330#define DROP(n) (tag >>= (n), bits -= (n))
331
332 while (src_end - src >= FAST_IN_MIN && out < ring_end) {
333 unsigned int e, len;
334 int sym;
335
336 REFILL();
337 e = lfast[tag & ((1u << LIT_FAST_BITS) - 1)];
338 if (e) {
339 len = e >> SYM_BITS;
340 sym = e & SYM_MASK;
341 } else {
342 sym = tinf_decode_long(lt, tag, &len);
343 if (sym < 0) {
344 res = sym;
345 break;
346 }
347 }
348 DROP(len);
349
350 if (sym < 256) {
351 *out++ = sym;
352 continue;
353 }
354 if (sym == 256) {
355 res = TINF_DONE;
356 break;
357 }
358 sym -= 257;
359 if (sym >= 29) {
360 res = TINF_DATA_ERROR;
361 break;
362 }
363 /* the lit/len code and its extra bits fit the refilled buffer together */
364 unsigned int length = LENGTH_BASE[sym] + (tag & ((1u << LENGTH_BITS[sym]) - 1));
365 DROP(LENGTH_BITS[sym]);
366
367 REFILL();
368 e = dfast[tag & ((1u << DIST_FAST_BITS) - 1)];
369 if (e) {
370 len = e >> SYM_BITS;
371 sym = e & SYM_MASK;
372 } else {
373 sym = tinf_decode_long(dt, tag, &len);
374 }
375 if (sym < 0 || sym >= 30) {
376 res = TINF_DATA_ERROR;
377 break;
378 }
379 DROP(len);
380 REFILL();
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;
385 break;
386 }
387
388 /* wrap as an index: a pointer before the ring would be undefined */
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;
392 if (n > length)
393 n = length;
394 length -= n;
395 /* at most two runs: up to the end of the ring, then on from its start */
396 while (n) {
397 unsigned int run = ring_end - from;
398 if (run > n)
399 run = n;
400 n -= run;
401 do {
402 *out++ = *from++;
403 } while (--run);
404 if (from == ring_end)
405 from = ring;
406 }
407 if (length) {
408 d->curlen = length;
409 d->lz_off = from - ring;
410 }
411 }
412#undef REFILL
413#undef DROP
414
415 d->source = src;
416 d->dest = out;
417 d->tag = tag;
418 d->bitcount = bits;
419 return res;
420}
421
422/* ----------------------------- *
423 * -- block inflate functions -- *
424 * ----------------------------- */
425
426/* given a stream and two trees, inflate until the ring is full or the block ends */
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;
432 if (n > d->curlen)
433 n = d->curlen;
434 d->curlen -= n;
435 do {
436 *d->dest++ = d->dict_ring[d->lz_off];
437 if ((unsigned) ++d->lz_off == d->dict_size)
438 d->lz_off = 0;
439 } while (--n);
440 continue;
441 }
442
443 if (d->source_limit - d->source >= FAST_IN_MIN) {
444 int res = tinf_inflate_fast(d, lt, dt);
445 if (res != TINF_OK)
446 return res;
447 continue;
448 }
449
450 unsigned int offs;
451 int dist;
452 int sym = tinf_decode_symbol(d, lt);
453
454 if (d->eof)
455 return TINF_DATA_ERROR;
456 if (sym < 0)
457 return sym;
458
459 /* literal byte */
460 if (sym < 256) {
461 *d->dest++ = sym;
462 continue;
463 }
464
465 /* end of block */
466 if (sym == 256)
467 return TINF_DONE;
468
469 /* substring from sliding dictionary */
470 sym -= 257;
471 if (sym >= 29)
472 return TINF_DATA_ERROR;
473
474 d->curlen = tinf_read_bits(d, LENGTH_BITS[sym], LENGTH_BASE[sym]);
475
476 dist = tinf_decode_symbol(d, dt);
477 if (dist < 0 || dist >= 30)
478 return TINF_DATA_ERROR;
479
480 offs = tinf_read_bits(d, DIST_BITS[dist], DIST_BASE[dist]);
481 if (offs > d->dict_size)
482 return TINF_DICT_ERROR;
483
484 d->lz_off = (int) (d->dest - d->dict_ring) - (int) offs;
485 if (d->lz_off < 0)
486 d->lz_off += d->dict_size;
487 }
488 return TINF_OK;
489}
490
491/* inflate next byte from uncompressed block of data */
492static int tinf_inflate_uncompressed_block(TINF_DATA *d) {
493 if (d->curlen == 0) {
494 unsigned int length, invlength;
495
496 /* the block starts on a byte boundary */
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;
502
503 d->curlen = length + 1;
504 }
505
506 if (--d->curlen == 0)
507 return TINF_DONE;
508
509 /* read first: a read that needs input may flush the output up to dest */
510 unsigned char c = tinf_read_bits(d, 8, 0);
511 *d->dest++ = c;
512 return TINF_OK;
513}
514
515/* ---------------------- *
516 * -- public functions -- *
517 * ---------------------- */
518
519void ota_inflate_init(TINF_DATA *d, unsigned char *dict, unsigned int dict_len) {
520 d->source = NULL;
521 d->source_limit = NULL;
522 d->dest = dict;
523 d->tag = 0;
524 d->eof = 0;
525 d->bitcount = 0;
526 d->lz_off = 0;
527 d->bfinal = 0;
528 d->btype = -1;
529 d->dict_size = dict_len;
530 d->dict_ring = dict;
531 d->curlen = 0;
532 d->ltree.trans = d->ltrans;
533 d->ltree.fast = d->lfast;
534 d->dtree.trans = d->dtrans;
535 d->dtree.fast = d->dfast;
536}
537
538int ota_inflate(TINF_DATA *d) {
539 /* the caller has taken the previous ringful */
540 if (d->dest == d->dict_ring + d->dict_size)
541 d->dest = d->dict_ring;
542
543 do {
544 int res;
545
546 if (d->btype == -1) {
547 int old_btype;
548 next_blk:
549 old_btype = d->btype;
550 d->bfinal = tinf_read_bits(d, 1, 0);
551 d->btype = tinf_read_bits(d, 2, 0);
552
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);
557 if (res != TINF_OK)
558 return res;
559 }
560 }
561
562 switch (d->btype) {
563 case 0:
564 res = tinf_inflate_uncompressed_block(d);
565 break;
566 case 1:
567 case 2:
568 res = tinf_inflate_block_data(d, &d->ltree, &d->dtree);
569 break;
570 default:
571 return TINF_DATA_ERROR;
572 }
573
574 if (res == TINF_DONE && !d->bfinal)
575 goto next_blk;
576
577 if (res != TINF_OK)
578 return res;
579
580 } while (d->dest < d->dict_ring + d->dict_size);
581
582 return TINF_OK;
583}
uint8_t l
Definition bl0906.h:0
NameCopy * next
std::vector< uint8_t > & out
Definition gif.cpp:49
unsigned count
Definition gif.cpp:51
mopeka_std_values val[3]
static float float float t
const void * src
Definition hal.h:68
void const char * tag
Definition log.h:110
void ota_inflate_init(TINF_DATA *d, unsigned char *dict, unsigned int dict_len)
@ LIT_FAST_BITS
@ DIST_FAST_BITS
@ FAST_IN_MIN
int ota_inflate(TINF_DATA *d)
@ OTA_INFLATE_LIT_FAST_BITS
@ OTA_INFLATE_DIST_FAST_BITS
uint32_t len
uint16_t length
Definition tt21100.cpp:0