Coverage Report

Created: 2026-08-31 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebsockets/lib/misc/upng-gzip.c
Line
Count
Source
1
/*
2
 * LWS PNG -- derived from uPNG -- derived from LodePNG version 20100808
3
 * Stateful, linewise PNG decode requiring ~36KB fixed heap
4
 *
5
 * Copyright (c) 2005-2010 Lode Vandevenne (LodePNG)
6
 * Copyright (c) 2010 Sean Middleditch (uPNG)
7
 * Copyright (c) 2021 Andy Green <andy@warmcat.com> (Stateful, incremental)
8
 *
9
 * This software is provided 'as-is', without any express or implied
10
 * warranty. In no event will the authors be held liable for any damages
11
 * arising from the use of this software.
12
13
 * Permission is granted to anyone to use this software for any purpose,
14
 * including commercial applications, and to alter it and redistribute it
15
 * freely, subject to the following restrictions:
16
 *
17
 *   1. The origin of this software must not be misrepresented; you must not
18
 *      claim that you wrote the original software. If you use this software
19
 *      in a product, an acknowledgment in the product documentation would be
20
 *  appreciated but is not required.
21
 *
22
 *   2. Altered source versions must be plainly marked as such, and must not be
23
 *  misrepresented as being the original software.
24
 *
25
 *   3. This notice may not be removed or altered from any source
26
 *  distribution.
27
 *
28
 *  AG: The above notice is the ZLIB license, libpng also uses it.
29
 *
30
 * This version was rewritten from the upng project's fork of lodepng and
31
 * adapted to be a stateful stream parser.  This rewrite retains the ZLIB
32
 * license of the source material for simplicity.
33
 *
34
 * That allows it to use a fixed 32KB ringbuffer to hold decodes, and
35
 * incrementally decode chunks into it as we want output lines that are not yet
36
 * present there.  The input png nor the output bitmap need to be all in one
37
 * place at one time.
38
 */
39
40
#include <private-lib-core.h>
41
42
#include <stdio.h>
43
#include <stdlib.h>
44
#include <string.h>
45
#include <limits.h>
46
47
static const huff_t huff_length_base[] = {
48
  /* the base lengths represented by codes 257-285 */
49
  3, 4, 5, 6, 7, 8, 9, 10,
50
  11, 13, 15, 17, 19, 23, 27, 31,
51
  35, 43, 51, 59, 67, 83, 99, 115,
52
  131, 163, 195, 227, 258, 0
53
};
54
55
static const huff_t huff_length_extra[] = {
56
  /* the extra bits used by codes 257-285 (added to base length) */
57
  0, 0, 0, 0, 0, 0, 0, 0,
58
  1, 1, 1, 1, 2, 2, 2, 2,
59
  3, 3, 3, 3, 4, 4, 4, 4,
60
  5, 5, 5, 5, 0, 127
61
};
62
63
static const huff_t huff_distance_base[] = {
64
  /*
65
   * The base backwards distances (the bits of distance codes appear
66
   * after length codes and use their own huffman tree)
67
   */
68
  1, 2, 3, 4, 5, 7, 9, 13,
69
  17, 25, 33, 49, 65, 97, 129, 193,
70
  257, 385, 513, 769, 1025, 1537, 2049, 3073,
71
  4097, 6145, 8193, 12289, 16385, 24577, 0, 0
72
};
73
74
static const huff_t huff_distance_extra[] = {
75
  /* the extra bits of backwards distances (added to base) */
76
  0, 0, 0, 0, 1, 1, 2, 2,
77
  3, 3, 4, 4, 5, 5, 6, 6,
78
  7, 7, 8, 8, 9, 9, 10, 10,
79
  11, 11, 12, 12, 13, 13, 0, 0
80
};
81
82
static const huff_t huff_cl_cl[] = {
83
  /*
84
   * The order in which "code length alphabet code lengths" are stored,
85
   * out of this the huffman tree of the dynamic huffman tree lengths
86
   * is generated
87
   */
88
  16, 17, 18, 0,
89
  8, 7, 9, 6,
90
  10, 5, 11, 4,
91
  12, 3, 13, 2,
92
  14, 1, 15
93
};
94
95
static const huff_t FIXED_DEFLATE_CODE_TREE[NUM_DEFLATE_CODE_SYMBOLS * 2] = {
96
  289, 370,
97
  290, 307,
98
  546, 291,
99
  561, 292,
100
  293, 300,
101
  294, 297,
102
  295, 296,
103
  0, 1,
104
  2, 3,
105
  298, 299,
106
  4, 5, 6, 7, 301, 304, 302, 303, 8, 9, 10, 11, 305,
107
  306, 12, 13, 14, 15, 308, 339, 309, 324, 310, 317, 311, 314, 312, 313,
108
  16, 17, 18, 19, 315, 316, 20, 21, 22, 23, 318, 321, 319, 320, 24, 25,
109
  26, 27, 322, 323, 28, 29, 30, 31, 325, 332, 326, 329, 327, 328, 32, 33,
110
  34, 35, 330, 331, 36, 37, 38, 39, 333, 336, 334, 335, 40, 41, 42, 43,
111
  337, 338, 44, 45, 46, 47, 340, 355, 341, 348, 342, 345, 343, 344, 48,
112
  49, 50, 51, 346, 347, 52, 53, 54, 55, 349, 352, 350, 351, 56, 57, 58,
113
  59, 353, 354, 60, 61, 62, 63, 356, 363, 357, 360, 358, 359, 64, 65, 66,
114
  67, 361, 362, 68, 69, 70, 71, 364, 367, 365, 366, 72, 73, 74, 75, 368,
115
  369, 76, 77, 78, 79, 371, 434, 372, 403, 373, 388, 374, 381, 375, 378,
116
  376, 377, 80, 81, 82, 83, 379, 380, 84, 85, 86, 87, 382, 385, 383, 384,
117
  88, 89, 90, 91, 386, 387, 92, 93, 94, 95, 389, 396, 390, 393, 391, 392,
118
  96, 97, 98, 99, 394, 395, 100, 101, 102, 103, 397, 400, 398, 399, 104,
119
  105, 106, 107, 401, 402, 108, 109, 110, 111, 404, 419, 405, 412, 406,
120
  409, 407, 408, 112, 113, 114, 115, 410, 411, 116, 117, 118, 119, 413,
121
  416, 414, 415, 120, 121, 122, 123, 417, 418, 124, 125, 126, 127, 420,
122
  427, 421, 424, 422, 423, 128, 129, 130, 131, 425, 426, 132, 133, 134,
123
  135, 428, 431, 429, 430, 136, 137, 138, 139, 432, 433, 140, 141, 142,
124
  143, 435, 483, 436, 452, 568, 437, 438, 445, 439, 442, 440, 441, 144,
125
  145, 146, 147, 443, 444, 148, 149, 150, 151, 446, 449, 447, 448, 152,
126
  153, 154, 155, 450, 451, 156, 157, 158, 159, 453, 468, 454, 461, 455,
127
  458, 456, 457, 160, 161, 162, 163, 459, 460, 164, 165, 166, 167, 462,
128
  465, 463, 464, 168, 169, 170, 171, 466, 467, 172, 173, 174, 175, 469,
129
  476, 470, 473, 471, 472, 176, 177, 178, 179, 474, 475, 180, 181, 182,
130
  183, 477, 480, 478, 479, 184, 185, 186, 187, 481, 482, 188, 189, 190,
131
  191, 484, 515, 485, 500, 486, 493, 487, 490, 488, 489, 192, 193, 194,
132
  195, 491, 492, 196, 197, 198, 199, 494, 497, 495, 496, 200, 201, 202,
133
  203, 498, 499, 204, 205, 206, 207, 501, 508, 502, 505, 503, 504, 208,
134
  209, 210, 211, 506, 507, 212, 213, 214, 215, 509, 512, 510, 511, 216,
135
  217, 218, 219, 513, 514, 220, 221, 222, 223, 516, 531, 517, 524, 518,
136
  521, 519, 520, 224, 225, 226, 227, 522, 523, 228, 229, 230, 231, 525,
137
  528, 526, 527, 232, 233, 234, 235, 529, 530, 236, 237, 238, 239, 532,
138
  539, 533, 536, 534, 535, 240, 241, 242, 243, 537, 538, 244, 245, 246,
139
  247, 540, 543, 541, 542, 248, 249, 250, 251, 544, 545, 252, 253, 254,
140
  255, 547, 554, 548, 551, 549, 550, 256, 257, 258, 259, 552, 553, 260,
141
  261, 262, 263, 555, 558, 556, 557, 264, 265, 266, 267, 559, 560, 268,
142
  269, 270, 271, 562, 565, 563, 564, 272, 273, 274, 275, 566, 567, 276,
143
  277, 278, 279, 569, 572, 570, 571, 280, 281, 282, 283, 573, 574, 284,
144
  285, 286, 287, 0, 0
145
};
146
147
static const huff_t FIXED_DISTANCE_TREE[NUM_DISTANCE_SYMBOLS * 2] = {
148
  33, 48, 34, 41, 35, 38, 36, 37,
149
  0, 1, 2, 3, 39, 40, 4, 5,
150
  6, 7, 42, 45, 43, 44, 8, 9,
151
  10, 11, 46, 47, 12, 13, 14, 15,
152
  49, 56, 50, 53, 51, 52, 16, 17,
153
  18, 19, 54, 55, 20, 21, 22, 23,
154
  57, 60, 58, 59, 24, 25, 26, 27,
155
  61, 62, 28, 29, 30, 31, 0, 0
156
};
157
158
static lws_stateful_ret_t
159
read_bit(inflator_ctx_t *inf, uint8_t *bits)
160
531k
{
161
531k
  size_t bo = inf->bp >> 3;
162
163
531k
  if (bo + inf->inpos >= inf->inlen)
164
554
    return LWS_SRET_WANT_INPUT;
165
166
530k
  *bits = (uint8_t)((*(inf->in + inf->inpos + bo) >> (inf->bp & 7)) & 1);
167
168
530k
  inf->bp++;
169
170
530k
  return LWS_SRET_OK;
171
531k
}
172
173
/* Stateful, so it can pick up after running out of input partway thru */
174
175
static lws_stateful_ret_t
176
read_bits(inflator_ctx_t *inf, unsigned int nbits, unsigned int *bits)
177
41.2k
{
178
41.2k
  lws_stateful_ret_t r;
179
41.2k
  uint8_t b;
180
181
41.2k
  if (!inf->read_bits_ongoing) {
182
41.2k
    inf->read_bits_ongoing  = 1;
183
41.2k
    inf->read_bits_shifter  = 0;
184
41.2k
    inf->read_bits_limit  = nbits;
185
41.2k
    inf->read_bits_i  = 0;
186
41.2k
  }
187
188
175k
  while (inf->read_bits_i < inf->read_bits_limit) {
189
134k
     r =read_bit(inf, &b);
190
134k
     if (r)
191
193
       return r;
192
193
134k
     inf->read_bits_shifter = inf->read_bits_shifter | (unsigned int)(b << inf->read_bits_i);
194
195
134k
     inf->read_bits_i++;
196
134k
  }
197
198
41.0k
  inf->read_bits_ongoing = 0;
199
41.0k
  *bits = inf->read_bits_shifter;
200
201
41.0k
  return LWS_SRET_OK;
202
41.2k
}
203
204
static lws_stateful_ret_t
205
read_byte(inflator_ctx_t *inf, uint8_t *byte)
206
17.8M
{
207
17.8M
  size_t bo;
208
209
17.8M
  while (inf->bp & 7)
210
0
    inf->bp++;
211
212
17.8M
  bo = inf->bp >> 3;
213
214
17.8M
  if (bo + inf->inpos >= inf->inlen)
215
319
    return LWS_SRET_WANT_INPUT;
216
217
17.8M
  *byte = *(inf->in + inf->inpos + bo);
218
219
17.8M
  inf->bp += 8;
220
221
17.8M
  return LWS_SRET_OK;
222
17.8M
}
223
224
/* buffer must be numcodes*2 in size! */
225
static void
226
huffman_tree_init(htree_t *tree, huff_t *buffer, uint16_t numcodes,
227
      uint16_t maxbitlen)
228
8.94k
{
229
8.94k
  tree->tree2d = buffer;
230
231
8.94k
  tree->numcodes = numcodes;
232
8.94k
  tree->maxbitlen = maxbitlen;
233
8.94k
}
234
235
2.54M
#define EMPTY 32767
236
237
/*
238
 * Given the code lengths (as stored in the PNG file), generate the tree as
239
 * defined by Deflate. maxbitlen is the maximum bits that a code in the tree
240
 * can have.
241
 */
242
static lws_stateful_ret_t
243
huffman_tree_create_lengths(htree_t *tree, const unsigned *bitlen)
244
5.53k
{
245
5.53k
  unsigned int tree1d[NUM_DEFLATE_CODE_SYMBOLS], /* sized to worst */
246
5.53k
         blcount[NUM_DEFLATE_CODE_SYMBOLS], /* sized to worst */
247
5.53k
         nextcode[MAX_BIT_LENGTH + 1], bits, n, i,
248
5.53k
         nodefilled = 0, treepos = 0;
249
250
5.53k
  memset(blcount, 0, sizeof(blcount));
251
5.53k
  memset(nextcode, 0, sizeof(nextcode));
252
253
5.53k
  assert(tree->numcodes <= LWS_ARRAY_SIZE(blcount));
254
255
603k
  for (bits = 0; bits < tree->numcodes; bits++) {
256
    /* any counts exceeding our private buffer length are fatal */
257
598k
    if (bitlen[bits] >= LWS_ARRAY_SIZE(blcount))
258
0
      return LWS_SRET_FATAL + 1;
259
260
598k
    blcount[bitlen[bits]]++;
261
598k
  }
262
263
5.53k
  assert(tree->maxbitlen && tree->maxbitlen - 1u <= LWS_ARRAY_SIZE(blcount));
264
5.53k
  assert(tree->maxbitlen - 1u <= LWS_ARRAY_SIZE(nextcode));
265
266
72.2k
  for (bits = 1; bits <= (unsigned int)tree->maxbitlen; bits++)
267
66.6k
    nextcode[bits] = (nextcode[bits - 1] + blcount[bits - 1]) << 1;
268
269
5.53k
  assert(tree->numcodes <= LWS_ARRAY_SIZE(tree1d));
270
271
603k
  for (n = 0; n < tree->numcodes; n++)
272
598k
    if (bitlen[n])
273
25.5k
      tree1d[n] = nextcode[bitlen[n]]++;
274
275
1.20M
  for (n = 0; n < (unsigned int)tree->numcodes * 2u; n++)
276
1.19M
    tree->tree2d[n] = EMPTY;
277
278
602k
  for (n = 0; n < tree->numcodes; n++) { /* the codes */
279
753k
    for (i = 0; i < bitlen[n]; i++) { /* the bits for this code */
280
156k
      uint8_t bit = (uint8_t)((tree1d[n] >>
281
156k
            (bitlen[n] - i - 1)) & 1);
282
283
      /* check if oversubscribed */
284
156k
      if (treepos > tree->numcodes - 2u)
285
39
        return LWS_SRET_FATAL + 1;
286
287
156k
      if (tree->tree2d[2 * treepos + bit] == EMPTY) {
288
65.0k
        if (i + 1 == bitlen[n]) { /* ... last bit */
289
24.9k
          tree->tree2d[2 * treepos + bit] = (huff_t)n;
290
24.9k
          treepos = 0;
291
40.0k
        } else {
292
40.0k
          nodefilled++;
293
40.0k
          tree->tree2d[2 * treepos + bit] =
294
40.0k
            (huff_t)(nodefilled + tree->numcodes);
295
40.0k
          treepos = nodefilled;
296
40.0k
        }
297
65.0k
      } else
298
91.4k
        treepos = (unsigned int)(tree->tree2d[2 * treepos + bit] -
299
91.4k
            tree->numcodes);
300
156k
    }
301
596k
  }
302
303
1.19M
  for (n = 0; n < tree->numcodes * 2u; n++)
304
1.18M
    if (tree->tree2d[n] == EMPTY)
305
1.12M
      tree->tree2d[n] = 0;
306
307
5.49k
  return LWS_SRET_OK;
308
5.53k
}
309
310
311
312
static lws_stateful_ret_t
313
huffman_decode_symbol(inflator_ctx_t *inf, const htree_t *ct, unsigned int *uct)
314
89.1k
{
315
89.1k
  lws_stateful_ret_t r;
316
89.1k
  uint8_t bit;
317
318
382k
  do {
319
382k
    r = read_bit(inf, &bit);
320
382k
    if (r)
321
327
      return r;
322
323
381k
    *uct = ct->tree2d[(inf->treepos << 1) | bit];
324
381k
    if (*uct < ct->numcodes)
325
88.7k
      return LWS_SRET_OK;
326
327
292k
    inf->treepos = *uct - ct->numcodes;
328
292k
    if (inf->treepos >= ct->numcodes)
329
0
      return LWS_SRET_FATAL + 2;
330
292k
  } while (1);
331
89.1k
}
332
333
lws_stateful_ret_t
334
_lws_upng_inflate_data(inflator_ctx_t *inf)
335
1.22k
{
336
1.22k
  unsigned int count, val, tu;
337
1.22k
  uint8_t t, done = 0;
338
1.22k
  lws_stateful_ret_t r;
339
1.22k
  size_t virt;
340
341
18.2M
  while (!done) {
342
18.2M
    switch (inf->state) {
343
4.92k
    case UPNS_ID_BL_GB_DONE:
344
4.92k
      r = read_bit(inf, &inf->done);
345
4.92k
      if (r)
346
26
        return r;
347
4.89k
      inf->state++;
348
349
      /* fallthru */
350
4.89k
    case UPNS_ID_BL_GB_BTYPEb0:
351
4.89k
      r = read_bit(inf, &inf->btype);
352
4.89k
      if (r)
353
1
        return r;
354
4.89k
      inf->state++;
355
356
      /* fallthru */
357
4.89k
    case UPNS_ID_BL_GB_BTYPEb1:
358
4.89k
      r = read_bit(inf, &t);
359
4.89k
      if (r)
360
7
        return r;
361
362
4.88k
      inf->btype |= (uint8_t)(t << 1);
363
364
4.88k
      if (inf->btype == 3)
365
12
        return LWS_SRET_FATAL + 3;
366
367
4.87k
      inf->i = 0;
368
369
4.87k
      inf->state = UPNS_ID_BL_GB_BTYPE_0 + inf->btype;
370
371
4.87k
      lwsl_debug("%s: (%lu) block type %d\n", __func__,
372
4.87k
        (unsigned long)inf->archive_pos + (inf->bp >> 3),
373
4.87k
        inf->btype);
374
375
      /* uncompressed starts on a byte boundary */
376
377
4.87k
      if (!inf->btype && (inf->bp & 7)) {
378
1.21k
        lwsl_debug("%s: skipping %d alignment bits for type 0\n",
379
1.21k
            __func__, (int)(8 - (inf->bp & 7)) & 7);
380
1.21k
        inf->bp = ((inf->bp >> 3) + 1) << 3;
381
1.21k
      }
382
4.87k
      continue;
383
384
1.47k
    case UPNS_ID_BL_GB_BTYPE_0: /* no compression */
385
1.47k
      r = read_byte(inf, &t);
386
1.47k
      if (r)
387
20
        return r;
388
389
1.45k
      inf->len = t;
390
1.45k
      inf->state = UPNS_ID_BL_GB_BTYPE_0a;
391
392
      // lwsl_notice("%s: no compression block\n", __func__);
393
394
      /* fallthru */
395
1.45k
    case UPNS_ID_BL_GB_BTYPE_0a:
396
1.45k
      r = read_byte(inf, &t);
397
1.45k
      if (r)
398
25
        return r;
399
400
1.43k
      inf->len = inf->len | (unsigned int)(t << 8);
401
1.43k
      inf->state++;
402
      /* fallthru */
403
404
1.43k
    case UPNS_ID_BL_GB_BTYPE_0b:
405
1.43k
      r = read_byte(inf, &t);
406
1.43k
      if (r)
407
23
        return r;
408
409
1.41k
      inf->nlen = t;
410
1.41k
      inf->state++;
411
412
      /* fallthru */
413
1.41k
    case UPNS_ID_BL_GB_BTYPE_0c:
414
1.41k
      r = read_byte(inf, &t);
415
1.41k
      if (r)
416
15
        return r;
417
418
1.39k
      inf->nlen = inf->nlen | (unsigned int)(t << 8);
419
420
1.39k
      if (inf->len + inf->nlen != 65535)
421
77
        return LWS_SRET_FATAL + 4;
422
423
1.31k
      lwsl_debug("%s: type 0 expects len %d\n", __func__, inf->len);
424
425
1.31k
      inf->state++;
426
1.31k
      inf->n = 0;
427
428
      /* fallthru */
429
25.2k
    case UPNS_ID_BL_GB_BTYPE_0d:
430
431
25.2k
      if (inf->n < inf->len) {
432
433
23.9k
        r = read_byte(inf, &t);
434
23.9k
        if (r)
435
54
          return r;
436
437
23.9k
        inf->out[inf->outpos++] = t;
438
23.9k
        if (inf->outpos >= inf->outlen)
439
0
          inf->outpos = 0;
440
23.9k
        inf->outpos_linear++;
441
23.9k
        inf->n++;
442
443
23.9k
        if (inf->outpos_linear - inf->consumed_linear >=
444
23.9k
            inf->bypl + 1) {
445
2
          return LWS_SRET_WANT_OUTPUT;
446
2
        }
447
448
23.9k
        continue;
449
23.9k
      }
450
451
1.26k
      inf->treepos = 0;
452
1.26k
      inf->state = UPNS_ID_BL_GB_DONE;
453
1.26k
      continue;
454
455
1.24k
    case UPNS_ID_BL_GB_BTYPE_1: /* fixed trees */
456
457
1.24k
      huffman_tree_init(&inf->ct,
458
1.24k
            (huff_t *)FIXED_DEFLATE_CODE_TREE,
459
1.24k
            NUM_DEFLATE_CODE_SYMBOLS,
460
1.24k
            DEFLATE_CODE_BITLEN);
461
1.24k
      huffman_tree_init(&inf->ctD,
462
1.24k
            (huff_t *)FIXED_DISTANCE_TREE,
463
1.24k
            NUM_DISTANCE_SYMBOLS,
464
1.24k
            DISTANCE_BITLEN);
465
466
1.24k
      lwsl_debug("%s: fixed tree init\n", __func__);
467
1.24k
      inf->treepos = 0;
468
1.24k
      inf->state = UPNS_ID_BL_GB_SPIN;
469
1.24k
      continue;
470
471
2.15k
    case UPNS_ID_BL_GB_BTYPE_2: /* dynamic trees */
472
2.15k
      huffman_tree_init(&inf->ct, inf->ct_buffer,
473
2.15k
            NUM_DEFLATE_CODE_SYMBOLS,
474
2.15k
            DEFLATE_CODE_BITLEN);
475
2.15k
      huffman_tree_init(&inf->ctD, inf->ctD_buffer,
476
2.15k
            NUM_DISTANCE_SYMBOLS,
477
2.15k
            DISTANCE_BITLEN);
478
2.15k
      huffman_tree_init(&inf->clct, inf->clct_buffer,
479
2.15k
            NUM_CODE_LENGTH_CODES,
480
2.15k
            CODE_LENGTH_BITLEN);
481
482
      // lwsl_notice("%s: dyn tree init\n", __func__);
483
2.15k
      inf->treepos = 0;
484
485
      /* clear bitlen arrays */
486
2.15k
      memset(inf->bitlen, 0, sizeof(inf->bitlen));
487
2.15k
      memset(inf->bitlenD, 0, sizeof(inf->bitlenD));
488
489
2.15k
      inf->state = UPNS_ID_BL_GB_BTYPE_2a;
490
491
2.15k
      inf->hlit = 0;
492
2.15k
      inf->hdist = 0;
493
2.15k
      inf->hclen = 0;
494
495
      /* fallthru */
496
497
2.15k
    case UPNS_ID_BL_GB_BTYPE_2a:
498
2.15k
      r = read_bits(inf, 5, &inf->hlit);
499
2.15k
      if (r)
500
14
        return r;
501
2.13k
      inf->hlit += 257;
502
2.13k
      inf->state++;
503
504
      /* fallthru */
505
2.13k
    case UPNS_ID_BL_GB_BTYPE_2b:
506
2.13k
      r = read_bits(inf, 5, &inf->hdist);
507
2.13k
      if (r)
508
22
        return r;
509
2.11k
      inf->hdist++;
510
2.11k
      inf->state++;
511
512
      /* fallthru */
513
2.11k
    case UPNS_ID_BL_GB_BTYPE_2c:
514
2.11k
      r = read_bits(inf, 4, &inf->hclen);
515
2.11k
      if (r)
516
24
        return r;
517
2.09k
      inf->hclen += 4;
518
2.09k
      inf->state = UPNS_ID_BL_GB_BTYPE_2d;
519
2.09k
      inf->i = 0;
520
521
      /* fallthru */
522
41.2k
    case UPNS_ID_BL_GB_BTYPE_2d:
523
41.2k
      if (inf->i < NUM_CODE_LENGTH_CODES) {
524
39.1k
        if (inf->i < inf->hclen) {
525
11.9k
          r = read_bits(inf, 3,
526
11.9k
            &inf->clenc[huff_cl_cl[inf->i]]);
527
11.9k
          if (r)
528
41
            return r;
529
11.9k
        } else
530
          /*if not, it must stay 0 */
531
27.2k
          inf->clenc[huff_cl_cl[inf->i]] = 0;
532
533
39.1k
        inf->i++;
534
39.1k
        continue;
535
39.1k
      }
536
537
2.05k
      r = huffman_tree_create_lengths(&inf->clct, inf->clenc);
538
2.05k
      if (r)
539
25
        return r;
540
541
2.02k
      inf->i = 0;
542
2.02k
      inf->state = UPNS_ID_BL_GB_BTYPE_2e;
543
2.02k
      inf->treepos = 0;
544
545
      /* fallthru */
546
30.9k
    case UPNS_ID_BL_GB_BTYPE_2e:
547
548
30.9k
      if (inf->i >= inf->hlit + inf->hdist) {
549
1.75k
        if (inf->bitlen[256] == 0)
550
8
          return LWS_SRET_FATAL + 6;
551
552
1.74k
        if (huffman_tree_create_lengths(&inf->ct,
553
1.74k
                inf->bitlen))
554
11
          return LWS_SRET_FATAL + 7;
555
556
1.73k
        if (huffman_tree_create_lengths(&inf->ctD,
557
1.73k
                inf->bitlenD))
558
3
          return LWS_SRET_FATAL + 8;
559
560
1.73k
        inf->treepos = 0;
561
1.73k
        inf->state = UPNS_ID_BL_GB_SPIN;
562
1.73k
        continue;
563
1.73k
      }
564
565
29.2k
      r = huffman_decode_symbol(inf, &inf->clct, &inf->code);
566
29.2k
      if (r)
567
110
        return r;
568
569
29.1k
      switch (inf->code) {
570
4.59k
      case 16:
571
4.59k
        inf->state = UPNS_ID_BL_GB_BTYPE_2_16;
572
4.59k
        continue;
573
938
      case 17:
574
938
        inf->state = UPNS_ID_BL_GB_BTYPE_2_17;
575
938
        continue;
576
4.05k
      case 18:
577
4.05k
        inf->state = UPNS_ID_BL_GB_BTYPE_2_18;
578
4.05k
        continue;
579
19.5k
      default:
580
19.5k
        if (inf->code > 15)
581
0
          return LWS_SRET_FATAL + 9;
582
583
19.5k
        if (inf->i < inf->hlit)
584
15.1k
          inf->bitlen[inf->i] = inf->code;
585
4.41k
        else
586
4.41k
          inf->bitlenD[inf->i - inf->hlit] =
587
4.41k
                inf->code;
588
589
19.5k
        inf->i++;
590
19.5k
        inf->treepos = 0;
591
592
        /* stay in 2e */
593
19.5k
        continue;
594
29.1k
      }
595
596
4.59k
    case UPNS_ID_BL_GB_BTYPE_2_16: /* repeat previous */
597
4.59k
      r = read_bits(inf, 2, &tu);
598
4.59k
      if (r)
599
16
        return r;
600
4.57k
      count = tu + 3;
601
602
4.57k
      if (!inf->i) /* from google fuzzer */
603
4
        return LWS_SRET_FATAL + 29;
604
605
4.57k
      if (inf->i - 1 < inf->hlit)
606
3.57k
        val = inf->bitlen[inf->i - 1];
607
999
      else
608
999
        val = inf->bitlenD[inf->i - inf->hlit - 1];
609
610
4.57k
      goto fill_l;
611
612
938
    case UPNS_ID_BL_GB_BTYPE_2_17: /*repeat "0" 3-10 times */
613
938
      r = read_bits(inf, 3, &tu);
614
938
      if (r)
615
18
        return r;
616
920
      count = tu + 3;
617
618
920
      val = 0;
619
920
      goto fill_l;
620
621
4.05k
    case UPNS_ID_BL_GB_BTYPE_2_18: /*repeat "0" 11-138 times */
622
4.05k
      r = read_bits(inf, 7, &tu);
623
4.05k
      if (r)
624
34
        return r;
625
4.02k
      count = tu + 11;
626
4.02k
      val = 0;
627
9.51k
fill_l:
628
629
9.51k
      if (inf->i + count > inf->hlit + inf->hdist) {
630
87
        lwsl_err("%s: inf->i (%d) > %d\n", __func__,
631
87
          inf->i + count, inf->hlit + inf->hdist);
632
87
        return LWS_SRET_FATAL + 10;
633
87
      }
634
635
9.42k
      {
636
9.42k
        unsigned int n;
637
638
493k
        for (n = 0; n < count; n++) {
639
640
483k
          if (inf->i < inf->hlit)
641
478k
            inf->bitlen[inf->i] = val;
642
5.45k
          else
643
5.45k
            inf->bitlenD[inf->i - inf->hlit] = val;
644
645
483k
          inf->i++;
646
483k
        }
647
9.42k
      }
648
9.42k
      inf->state = UPNS_ID_BL_GB_BTYPE_2e;
649
9.42k
      inf->treepos = 0;
650
9.42k
      continue;
651
652
653
53.1k
    case UPNS_ID_BL_GB_SPIN:
654
655
53.1k
      r = huffman_decode_symbol(inf, &inf->ct, &inf->code);
656
53.1k
      if (r)
657
178
        return r;
658
659
52.9k
      if (inf->code >= FIRST_LENGTH_CODE_INDEX &&
660
7.86k
          inf->code - FIRST_LENGTH_CODE_INDEX <
661
7.86k
              LWS_ARRAY_SIZE(huff_length_base))
662
7.06k
        inf->length = huff_length_base[inf->code -
663
7.06k
                                     FIRST_LENGTH_CODE_INDEX];
664
45.9k
      else
665
45.9k
        inf->length = 0;
666
667
52.9k
      if (inf->code == 256) {
668
        /*
669
         * We're finished with this huffman block, we
670
         * need to go back up a level
671
         */
672
2.70k
        done = inf->done;
673
2.70k
        inf->state = UPNS_ID_BL_GB_DONE;
674
2.70k
        continue;
675
2.70k
      }
676
677
50.2k
      if (inf->code <= 255) {
678
42.4k
        inf->state = UPNS_ID_BL_GB_SPINa;
679
42.4k
        continue;
680
42.4k
      }
681
682
7.86k
      if (inf->code < FIRST_LENGTH_CODE_INDEX ||
683
7.86k
          inf->code > LAST_LENGTH_CODE_INDEX) {
684
1.15k
        inf->treepos = 0;
685
1.15k
        continue;
686
1.15k
      }
687
688
6.71k
      inf->exbits = huff_length_extra[inf->code -
689
6.71k
                                      FIRST_LENGTH_CODE_INDEX];
690
6.71k
      inf->state = UPNS_ID_BL_GB_SPINb;
691
692
      /* fallthru */
693
6.71k
    case UPNS_ID_BL_GB_SPINb:
694
6.71k
      r = read_bits(inf, (unsigned int)inf->exbits, &tu);
695
6.71k
      if (r)
696
6
        return r;
697
698
6.70k
      inf->length += tu;
699
6.70k
      inf->state++;
700
6.70k
      inf->treepos = 0;
701
702
      /* fallthru */
703
6.70k
    case UPNS_ID_BL_GB_SPINc:
704
705
      /* part 3: get distance code */
706
707
6.70k
      r = huffman_decode_symbol(inf, &inf->ctD, &inf->codeD);
708
6.70k
      if (r)
709
39
        return r;
710
711
      /* invalid distance code (30-31 are never used) */
712
6.66k
      if (inf->codeD > 29) {
713
29
        lwsl_err("%s: invalid dist %d\n", __func__, inf->codeD);
714
29
        return LWS_SRET_FATAL + 11;
715
29
      }
716
717
6.63k
      inf->distance = huff_distance_base[inf->codeD];
718
719
      /* part 4: get extra bits from distance */
720
721
6.63k
      inf->exbitsD = huff_distance_extra[inf->codeD];
722
6.63k
      inf->state++;
723
724
      /* fallthru */
725
6.63k
    case UPNS_ID_BL_GB_SPINd:
726
727
6.63k
      r = read_bits(inf, inf->exbitsD, &tu);
728
6.63k
      if (r)
729
18
        return r;
730
731
6.61k
      inf->distance += tu;
732
733
6.61k
      if (inf->distance > inf->info_size) {
734
0
        lwsl_err("%s: distance %lu\n", __func__,
735
0
            (unsigned long)inf->distance);
736
0
        assert(0);
737
0
      }
738
739
      /*
740
       * Part 5: fill in all the out[n] values based
741
       * on the length and dist
742
       */
743
6.61k
      inf->start = inf->outpos;
744
6.61k
      inf->forward = 0;
745
6.61k
      inf->backward = inf->distance; /* from inf->start */
746
747
6.61k
      inf->state++;
748
749
      /* fallthru */
750
196k
    case UPNS_ID_BL_GB_SPINe:
751
752
196k
      if (inf->forward >= inf->length) {
753
6.61k
        inf->treepos = 0;
754
6.61k
        inf->state = UPNS_ID_BL_GB_SPIN;
755
6.61k
        continue;
756
6.61k
      }
757
758
189k
      if (inf->backward <= inf->start)
759
141k
        virt = inf->start - inf->backward;
760
48.3k
      else /* wrapped... backward >= start */
761
48.3k
        virt = inf->info_size -
762
48.3k
          (inf->backward - inf->start);
763
764
189k
      if (virt >= inf->info_size)
765
0
        lwsl_err("virt %d\n", (int)virt);
766
767
189k
      inf->out[inf->outpos++] = inf->out[virt];
768
189k
      if (inf->outpos >= inf->outlen)
769
0
        inf->outpos = 0;
770
771
189k
      inf->outpos_linear++;
772
189k
      inf->backward--;
773
774
189k
      if (!inf->backward)
775
72.5k
        inf->backward = inf->distance;
776
777
189k
      inf->forward++;
778
779
189k
      if (inf->outpos_linear - inf->consumed_linear >=
780
189k
                inf->bypl + 1)
781
3
        return LWS_SRET_WANT_OUTPUT;
782
783
189k
      continue;
784
785
189k
    case UPNS_ID_BL_GB_SPINa:
786
787
42.4k
      inf->out[inf->outpos++] = (uint8_t)inf->code;
788
42.4k
      if (inf->outpos >= inf->outlen)
789
0
        inf->outpos = 0;
790
791
42.4k
      inf->outpos_linear++;
792
42.4k
      inf->treepos = 0;
793
42.4k
      inf->state = UPNS_ID_BL_GB_SPIN;
794
795
42.4k
      if (inf->outpos_linear - inf->consumed_linear >=
796
42.4k
                inf->bypl + 1)
797
2
        return LWS_SRET_WANT_OUTPUT;
798
799
42.4k
      continue;
800
801
802
42.4k
    case UPNS_ID_BL_GB_GZIP_ID1:
803
1.22k
      r = read_byte(inf, &t);
804
1.22k
      if (r)
805
34
        return r;
806
1.18k
      if (t != 0x1f)
807
48
        return LWS_SRET_FATAL + 32;
808
1.14k
      inf->state++;
809
810
      /* fallthru */
811
812
1.14k
    case UPNS_ID_BL_GB_GZIP_ID2:
813
1.14k
      r = read_byte(inf, &t);
814
1.14k
      if (r)
815
2
        return r;
816
1.13k
      if (t != 0x8b)
817
15
        return LWS_SRET_FATAL + 33;
818
1.12k
      inf->state++;
819
820
      /* fallthru */
821
822
1.12k
    case UPNS_ID_BL_GB_GZIP_METHOD:
823
1.12k
      r = read_byte(inf, &t);
824
1.12k
      if (r)
825
1
        return r;
826
1.12k
      if (t != 8)
827
11
        return LWS_SRET_FATAL + 34;
828
1.11k
      inf->state++;
829
830
      /* fallthru */
831
832
1.11k
    case UPNS_ID_BL_GB_GZIP_FLAGS:
833
1.11k
      r = read_byte(inf, &t);
834
1.11k
      if (r)
835
1
        return r;
836
1.11k
      if (t & 0xe0)
837
9
        return LWS_SRET_FATAL + 35;
838
1.10k
      inf->gz_flags = t;
839
1.10k
      inf->state++;
840
1.10k
      inf->ctr = 6;
841
842
      /* fallthru */
843
844
7.66k
    case UPNS_ID_BL_GB_GZIP_EOH:
845
      /* we want skip 6 bytes */
846
7.66k
      if (inf->ctr--) {
847
6.57k
        r = read_byte(inf, &t);
848
6.57k
        if (r)
849
10
          return r;
850
851
6.56k
        continue;
852
6.57k
      }
853
854
1.09k
      if (inf->gz_flags & 4)
855
295
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_EXTRA_C1;
856
797
      else
857
797
      if (inf->gz_flags & 8)
858
39
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_FILENAME;
859
758
      else
860
758
      if (inf->gz_flags & 16)
861
42
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_COMMENT;
862
716
      else
863
716
      if (inf->gz_flags & 2) {
864
4
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_CRC;
865
4
        inf->ctr = 2;
866
4
      } else
867
712
        inf->state = UPNS_ID_BL_GB_DONE;
868
869
1.09k
      continue;
870
871
295
    case UPNS_ID_BL_GB_GZIP_SKIP_EXTRA_C1:
872
295
      r = read_byte(inf, &t);
873
295
      if (r)
874
1
        return r;
875
876
294
      inf->ctr = t;
877
878
294
      inf->state++;
879
880
      /* fallthru */
881
882
294
    case UPNS_ID_BL_GB_GZIP_SKIP_EXTRA_C2:
883
294
      r = read_byte(inf, &t);
884
294
      if (r)
885
1
        return r;
886
887
293
      inf->ctr = (uint16_t)(inf->ctr | (t << 8));
888
889
293
      inf->state++;
890
891
      /* fallthru */
892
893
814k
    case UPNS_ID_BL_GB_GZIP_SKIP_EXTRA:
894
814k
      if (inf->ctr--) {
895
813k
        r = read_byte(inf, &t);
896
813k
        if (r)
897
45
          return r;
898
899
813k
        continue;
900
813k
      }
901
902
248
      if (inf->gz_flags & 8)
903
244
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_FILENAME;
904
4
      else
905
4
      if (inf->gz_flags & 16)
906
2
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_COMMENT;
907
2
      else
908
2
      if (inf->gz_flags & 2) {
909
1
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_CRC;
910
1
        inf->ctr = 2;
911
1
      } else
912
1
        inf->state = UPNS_ID_BL_GB_DONE;
913
914
248
      continue;
915
916
12.5M
    case UPNS_ID_BL_GB_GZIP_SKIP_FILENAME: /* zero-terminated */
917
12.5M
      r = read_byte(inf, &t);
918
12.5M
      if (r)
919
38
        return r;
920
921
12.5M
      if (t)
922
12.5M
        continue;
923
924
245
      if (inf->gz_flags & 16)
925
236
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_COMMENT;
926
9
      else
927
9
      if (inf->gz_flags & 2) {
928
2
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_CRC;
929
2
        inf->ctr = 2;
930
2
      } else
931
7
        inf->state = UPNS_ID_BL_GB_DONE;
932
933
245
      continue;
934
935
4.51M
    case UPNS_ID_BL_GB_GZIP_SKIP_COMMENT: /* zero-terminated */
936
4.51M
      r = read_byte(inf, &t);
937
4.51M
      if (r)
938
38
        return r;
939
940
4.51M
      if (t)
941
4.51M
        continue;
942
943
242
      if (inf->gz_flags & 2) {
944
227
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_CRC;
945
227
        inf->ctr = 2;
946
227
      }
947
15
      else
948
15
        inf->state = UPNS_ID_BL_GB_DONE;
949
950
242
      continue;
951
952
684
    case UPNS_ID_BL_GB_GZIP_SKIP_CRC:
953
684
      if (inf->ctr--) {
954
461
        r = read_byte(inf, &t);
955
461
        if (r)
956
11
          return r;
957
958
450
        continue;
959
461
      }
960
223
      inf->state = UPNS_ID_BL_GB_DONE;
961
223
      continue;
962
18.2M
    }
963
18.2M
  }
964
965
4
  return LWS_SRET_OK;
966
1.22k
}
967
968
struct inflator_ctx *
969
lws_upng_inflator_create(const uint8_t **outring, size_t *outringlen,
970
       size_t **opl, size_t **cl)
971
1.22k
{
972
1.22k
  inflator_ctx_t *inf = lws_zalloc(sizeof(*inf), __func__);
973
974
1.22k
  if (!inf) {
975
0
    lwsl_notice("%s: OOM\n", __func__);
976
977
0
    return NULL;
978
0
  }
979
980
  /* 32KB gz sliding window */
981
1.22k
  inf->info_size  = 32768;
982
1.22k
  inf->bypl = 0;
983
1.22k
  inf->outlen = inf->info_size;
984
1.22k
  inf->outpos = 0;
985
1.22k
  inf->state  = UPNS_ID_BL_GB_GZIP_ID1;
986
987
1.22k
  inf->out = (uint8_t *)lws_malloc(inf->info_size, __func__);
988
1.22k
  if (!inf->out) {
989
0
    lwsl_notice("%s: inf malloc %u OOM\n",
990
0
      __func__, (unsigned int)inf->info_size);
991
992
0
    lws_free(inf);
993
994
0
    return NULL;
995
0
  }
996
997
1.22k
  *outring = inf->out;
998
1.22k
  *outringlen = inf->info_size;
999
1.22k
  *opl = &inf->outpos_linear;
1000
1.22k
  *cl = &inf->consumed_linear;
1001
1002
1.22k
  return inf;
1003
1.22k
}
1004
1005
lws_stateful_ret_t
1006
lws_upng_inflate_data(struct inflator_ctx *inf, const void *buf, size_t len)
1007
1.22k
{
1008
1.22k
  lws_stateful_ret_t r;
1009
1010
1.22k
  if (buf) {
1011
1.22k
    inf->in   = buf;
1012
1.22k
    inf->inlen  = len;
1013
1.22k
    inf->inpos  = 0;
1014
1.22k
    inf->bp   = 0;
1015
1.22k
  }
1016
1017
1.22k
  if (!inf->bypl)
1018
1.22k
    inf->bypl = 4095;
1019
1020
1.22k
  r = _lws_upng_inflate_data(inf);
1021
1022
1.22k
  if ((inf->bp >> 3) == inf->inlen) {
1023
1.01k
    inf->archive_pos += inf->inlen;
1024
1.01k
    inf->inlen = 0;
1025
1.01k
    inf->bp = 0;
1026
1.01k
  }
1027
1028
1.22k
  return r;
1029
1.22k
}
1030
1031
void
1032
lws_upng_inflator_destroy(struct inflator_ctx **inf)
1033
1.22k
{
1034
1.22k
  lws_free_set_NULL((*inf)->out);
1035
  lws_free_set_NULL(*inf);
1036
1.22k
}
1037
1038