Coverage Report

Created: 2025-12-10 06:58

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
615k
{
161
615k
  size_t bo = inf->bp >> 3;
162
163
615k
  if (bo + inf->inpos >= inf->inlen)
164
543
    return LWS_SRET_WANT_INPUT;
165
166
615k
  *bits = (uint8_t)((*(inf->in + inf->inpos + bo) >> (inf->bp & 7)) & 1);
167
168
615k
  inf->bp++;
169
170
615k
  return LWS_SRET_OK;
171
615k
}
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
30.9k
{
178
30.9k
  lws_stateful_ret_t r;
179
30.9k
  uint8_t b;
180
181
30.9k
  if (!inf->read_bits_ongoing) {
182
30.9k
    inf->read_bits_ongoing  = 1;
183
30.9k
    inf->read_bits_shifter  = 0;
184
30.9k
    inf->read_bits_limit  = nbits;
185
30.9k
    inf->read_bits_i  = 0;
186
30.9k
  }
187
188
135k
  while (inf->read_bits_i < inf->read_bits_limit) {
189
104k
     r =read_bit(inf, &b);
190
104k
     if (r)
191
188
       return r;
192
193
104k
     inf->read_bits_shifter = inf->read_bits_shifter | (unsigned int)(b << inf->read_bits_i);
194
195
104k
     inf->read_bits_i++;
196
104k
  }
197
198
30.7k
  inf->read_bits_ongoing = 0;
199
30.7k
  *bits = inf->read_bits_shifter;
200
201
30.7k
  return LWS_SRET_OK;
202
30.9k
}
203
204
static lws_stateful_ret_t
205
read_byte(inflator_ctx_t *inf, uint8_t *byte)
206
18.2M
{
207
18.2M
  size_t bo;
208
209
18.2M
  while (inf->bp & 7)
210
0
    inf->bp++;
211
212
18.2M
  bo = inf->bp >> 3;
213
214
18.2M
  if (bo + inf->inpos >= inf->inlen)
215
325
    return LWS_SRET_WANT_INPUT;
216
217
18.2M
  *byte = *(inf->in + inf->inpos + bo);
218
219
18.2M
  inf->bp += 8;
220
221
18.2M
  return LWS_SRET_OK;
222
18.2M
}
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
7.48k
{
229
7.48k
  tree->tree2d = buffer;
230
231
7.48k
  tree->numcodes = numcodes;
232
7.48k
  tree->maxbitlen = maxbitlen;
233
7.48k
}
234
235
1.96M
#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
4.25k
{
245
4.25k
  unsigned int tree1d[NUM_DEFLATE_CODE_SYMBOLS], /* sized to worst */
246
4.25k
         blcount[NUM_DEFLATE_CODE_SYMBOLS], /* sized to worst */
247
4.25k
         nextcode[MAX_BIT_LENGTH + 1], bits, n, i,
248
4.25k
         nodefilled = 0, treepos = 0;
249
250
4.25k
  memset(blcount, 0, sizeof(blcount));
251
4.25k
  memset(nextcode, 0, sizeof(nextcode));
252
253
4.25k
  assert(tree->numcodes <= LWS_ARRAY_SIZE(blcount));
254
255
464k
  for (bits = 0; bits < tree->numcodes; bits++) {
256
    /* any counts exceeding our private buffer length are fatal */
257
460k
    if (bitlen[bits] >= LWS_ARRAY_SIZE(blcount))
258
0
      return LWS_SRET_FATAL + 1;
259
260
460k
    blcount[bitlen[bits]]++;
261
460k
  }
262
263
4.25k
  assert(tree->maxbitlen && tree->maxbitlen - 1u <= LWS_ARRAY_SIZE(blcount));
264
4.25k
  assert(tree->maxbitlen - 1u <= LWS_ARRAY_SIZE(nextcode));
265
266
55.4k
  for (bits = 1; bits <= (unsigned int)tree->maxbitlen; bits++)
267
51.1k
    nextcode[bits] = (nextcode[bits - 1] + blcount[bits - 1]) << 1;
268
269
4.25k
  assert(tree->numcodes <= LWS_ARRAY_SIZE(tree1d));
270
271
464k
  for (n = 0; n < tree->numcodes; n++)
272
460k
    if (bitlen[n])
273
21.3k
      tree1d[n] = nextcode[bitlen[n]]++;
274
275
924k
  for (n = 0; n < (unsigned int)tree->numcodes * 2u; n++)
276
920k
    tree->tree2d[n] = EMPTY;
277
278
462k
  for (n = 0; n < tree->numcodes; n++) { /* the codes */
279
590k
    for (i = 0; i < bitlen[n]; i++) { /* the bits for this code */
280
132k
      uint8_t bit = (uint8_t)((tree1d[n] >>
281
132k
            (bitlen[n] - i - 1)) & 1);
282
283
      /* check if oversubscribed */
284
132k
      if (treepos > tree->numcodes - 2u)
285
40
        return LWS_SRET_FATAL + 1;
286
287
132k
      if (tree->tree2d[2 * treepos + bit] == EMPTY) {
288
52.0k
        if (i + 1 == bitlen[n]) { /* ... last bit */
289
19.9k
          tree->tree2d[2 * treepos + bit] = (huff_t)n;
290
19.9k
          treepos = 0;
291
32.1k
        } else {
292
32.1k
          nodefilled++;
293
32.1k
          tree->tree2d[2 * treepos + bit] =
294
32.1k
            (huff_t)(nodefilled + tree->numcodes);
295
32.1k
          treepos = nodefilled;
296
32.1k
        }
297
52.0k
      } else
298
80.3k
        treepos = (unsigned int)(tree->tree2d[2 * treepos + bit] -
299
80.3k
            tree->numcodes);
300
132k
    }
301
458k
  }
302
303
914k
  for (n = 0; n < tree->numcodes * 2u; n++)
304
910k
    if (tree->tree2d[n] == EMPTY)
305
860k
      tree->tree2d[n] = 0;
306
307
4.21k
  return LWS_SRET_OK;
308
4.25k
}
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
108k
{
315
108k
  lws_stateful_ret_t r;
316
108k
  uint8_t bit;
317
318
496k
  do {
319
496k
    r = read_bit(inf, &bit);
320
496k
    if (r)
321
319
      return r;
322
323
496k
    *uct = ct->tree2d[(inf->treepos << 1) | bit];
324
496k
    if (*uct < ct->numcodes)
325
108k
      return LWS_SRET_OK;
326
327
388k
    inf->treepos = *uct - ct->numcodes;
328
388k
    if (inf->treepos >= ct->numcodes)
329
0
      return LWS_SRET_FATAL + 2;
330
388k
  } while (1);
331
108k
}
332
333
lws_stateful_ret_t
334
_lws_upng_inflate_data(inflator_ctx_t *inf)
335
1.16k
{
336
1.16k
  unsigned int count, val, tu;
337
1.16k
  uint8_t t, done = 0;
338
1.16k
  lws_stateful_ret_t r;
339
1.16k
  size_t virt;
340
341
18.6M
  while (!done) {
342
18.6M
    switch (inf->state) {
343
4.69k
    case UPNS_ID_BL_GB_DONE:
344
4.69k
      r = read_bit(inf, &inf->done);
345
4.69k
      if (r)
346
26
        return r;
347
4.67k
      inf->state++;
348
349
      /* fallthru */
350
4.67k
    case UPNS_ID_BL_GB_BTYPEb0:
351
4.67k
      r = read_bit(inf, &inf->btype);
352
4.67k
      if (r)
353
3
        return r;
354
4.66k
      inf->state++;
355
356
      /* fallthru */
357
4.66k
    case UPNS_ID_BL_GB_BTYPEb1:
358
4.66k
      r = read_bit(inf, &t);
359
4.66k
      if (r)
360
7
        return r;
361
362
4.66k
      inf->btype |= (uint8_t)(t << 1);
363
364
4.66k
      if (inf->btype == 3)
365
10
        return LWS_SRET_FATAL + 3;
366
367
4.65k
      inf->i = 0;
368
369
4.65k
      inf->state = UPNS_ID_BL_GB_BTYPE_0 + inf->btype;
370
371
4.65k
      lwsl_debug("%s: (%lu) block type %d\n", __func__,
372
4.65k
        (unsigned long)inf->archive_pos + (inf->bp >> 3),
373
4.65k
        inf->btype);
374
375
      /* uncompressed starts on a byte boundary */
376
377
4.65k
      if (!inf->btype && (inf->bp & 7)) {
378
1.51k
        lwsl_debug("%s: skipping %d alignment bits for type 0\n",
379
1.51k
            __func__, (int)(8 - (inf->bp & 7)) & 7);
380
1.51k
        inf->bp = ((inf->bp >> 3) + 1) << 3;
381
1.51k
      }
382
4.65k
      continue;
383
384
1.74k
    case UPNS_ID_BL_GB_BTYPE_0: /* no compression */
385
1.74k
      r = read_byte(inf, &t);
386
1.74k
      if (r)
387
23
        return r;
388
389
1.72k
      inf->len = t;
390
1.72k
      inf->state = UPNS_ID_BL_GB_BTYPE_0a;
391
392
      // lwsl_notice("%s: no compression block\n", __func__);
393
394
      /* fallthru */
395
1.72k
    case UPNS_ID_BL_GB_BTYPE_0a:
396
1.72k
      r = read_byte(inf, &t);
397
1.72k
      if (r)
398
25
        return r;
399
400
1.69k
      inf->len = inf->len | (unsigned int)(t << 8);
401
1.69k
      inf->state++;
402
      /* fallthru */
403
404
1.69k
    case UPNS_ID_BL_GB_BTYPE_0b:
405
1.69k
      r = read_byte(inf, &t);
406
1.69k
      if (r)
407
25
        return r;
408
409
1.67k
      inf->nlen = t;
410
1.67k
      inf->state++;
411
412
      /* fallthru */
413
1.67k
    case UPNS_ID_BL_GB_BTYPE_0c:
414
1.67k
      r = read_byte(inf, &t);
415
1.67k
      if (r)
416
18
        return r;
417
418
1.65k
      inf->nlen = inf->nlen | (unsigned int)(t << 8);
419
420
1.65k
      if (inf->len + inf->nlen != 65535)
421
82
        return LWS_SRET_FATAL + 4;
422
423
1.57k
      lwsl_debug("%s: type 0 expects len %d\n", __func__, inf->len);
424
425
1.57k
      inf->state++;
426
1.57k
      inf->n = 0;
427
428
      /* fallthru */
429
42.2k
    case UPNS_ID_BL_GB_BTYPE_0d:
430
431
42.2k
      if (inf->n < inf->len) {
432
433
40.7k
        r = read_byte(inf, &t);
434
40.7k
        if (r)
435
56
          return r;
436
437
40.6k
        inf->out[inf->outpos++] = t;
438
40.6k
        if (inf->outpos >= inf->outlen)
439
0
          inf->outpos = 0;
440
40.6k
        inf->outpos_linear++;
441
40.6k
        inf->n++;
442
443
40.6k
        if (inf->outpos_linear - inf->consumed_linear >=
444
40.6k
            inf->bypl + 1) {
445
6
          return LWS_SRET_WANT_OUTPUT;
446
6
        }
447
448
40.6k
        continue;
449
40.6k
      }
450
451
1.51k
      inf->treepos = 0;
452
1.51k
      inf->state = UPNS_ID_BL_GB_DONE;
453
1.51k
      continue;
454
455
1.23k
    case UPNS_ID_BL_GB_BTYPE_1: /* fixed trees */
456
457
1.23k
      huffman_tree_init(&inf->ct,
458
1.23k
            (huff_t *)FIXED_DEFLATE_CODE_TREE,
459
1.23k
            NUM_DEFLATE_CODE_SYMBOLS,
460
1.23k
            DEFLATE_CODE_BITLEN);
461
1.23k
      huffman_tree_init(&inf->ctD,
462
1.23k
            (huff_t *)FIXED_DISTANCE_TREE,
463
1.23k
            NUM_DISTANCE_SYMBOLS,
464
1.23k
            DISTANCE_BITLEN);
465
466
1.23k
      lwsl_debug("%s: fixed tree init\n", __func__);
467
1.23k
      inf->treepos = 0;
468
1.23k
      inf->state = UPNS_ID_BL_GB_SPIN;
469
1.23k
      continue;
470
471
1.67k
    case UPNS_ID_BL_GB_BTYPE_2: /* dynamic trees */
472
1.67k
      huffman_tree_init(&inf->ct, inf->ct_buffer,
473
1.67k
            NUM_DEFLATE_CODE_SYMBOLS,
474
1.67k
            DEFLATE_CODE_BITLEN);
475
1.67k
      huffman_tree_init(&inf->ctD, inf->ctD_buffer,
476
1.67k
            NUM_DISTANCE_SYMBOLS,
477
1.67k
            DISTANCE_BITLEN);
478
1.67k
      huffman_tree_init(&inf->clct, inf->clct_buffer,
479
1.67k
            NUM_CODE_LENGTH_CODES,
480
1.67k
            CODE_LENGTH_BITLEN);
481
482
      // lwsl_notice("%s: dyn tree init\n", __func__);
483
1.67k
      inf->treepos = 0;
484
485
      /* clear bitlen arrays */
486
1.67k
      memset(inf->bitlen, 0, sizeof(inf->bitlen));
487
1.67k
      memset(inf->bitlenD, 0, sizeof(inf->bitlenD));
488
489
1.67k
      inf->state = UPNS_ID_BL_GB_BTYPE_2a;
490
491
1.67k
      inf->hlit = 0;
492
1.67k
      inf->hdist = 0;
493
1.67k
      inf->hclen = 0;
494
495
      /* fallthru */
496
497
1.67k
    case UPNS_ID_BL_GB_BTYPE_2a:
498
1.67k
      r = read_bits(inf, 5, &inf->hlit);
499
1.67k
      if (r)
500
10
        return r;
501
1.66k
      inf->hlit += 257;
502
1.66k
      inf->state++;
503
504
      /* fallthru */
505
1.66k
    case UPNS_ID_BL_GB_BTYPE_2b:
506
1.66k
      r = read_bits(inf, 5, &inf->hdist);
507
1.66k
      if (r)
508
30
        return r;
509
1.63k
      inf->hdist++;
510
1.63k
      inf->state++;
511
512
      /* fallthru */
513
1.63k
    case UPNS_ID_BL_GB_BTYPE_2c:
514
1.63k
      r = read_bits(inf, 4, &inf->hclen);
515
1.63k
      if (r)
516
15
        return r;
517
1.61k
      inf->hclen += 4;
518
1.61k
      inf->state = UPNS_ID_BL_GB_BTYPE_2d;
519
1.61k
      inf->i = 0;
520
521
      /* fallthru */
522
31.7k
    case UPNS_ID_BL_GB_BTYPE_2d:
523
31.7k
      if (inf->i < NUM_CODE_LENGTH_CODES) {
524
30.1k
        if (inf->i < inf->hclen) {
525
9.30k
          r = read_bits(inf, 3,
526
9.30k
            &inf->clenc[huff_cl_cl[inf->i]]);
527
9.30k
          if (r)
528
42
            return r;
529
9.30k
        } else
530
          /*if not, it must stay 0 */
531
20.8k
          inf->clenc[huff_cl_cl[inf->i]] = 0;
532
533
30.1k
        inf->i++;
534
30.1k
        continue;
535
30.1k
      }
536
537
1.57k
      r = huffman_tree_create_lengths(&inf->clct, inf->clenc);
538
1.57k
      if (r)
539
19
        return r;
540
541
1.55k
      inf->i = 0;
542
1.55k
      inf->state = UPNS_ID_BL_GB_BTYPE_2e;
543
1.55k
      inf->treepos = 0;
544
545
      /* fallthru */
546
26.8k
    case UPNS_ID_BL_GB_BTYPE_2e:
547
548
26.8k
      if (inf->i >= inf->hlit + inf->hdist) {
549
1.35k
        if (inf->bitlen[256] == 0)
550
13
          return LWS_SRET_FATAL + 6;
551
552
1.34k
        if (huffman_tree_create_lengths(&inf->ct,
553
1.34k
                inf->bitlen))
554
16
          return LWS_SRET_FATAL + 7;
555
556
1.33k
        if (huffman_tree_create_lengths(&inf->ctD,
557
1.33k
                inf->bitlenD))
558
5
          return LWS_SRET_FATAL + 8;
559
560
1.32k
        inf->treepos = 0;
561
1.32k
        inf->state = UPNS_ID_BL_GB_SPIN;
562
1.32k
        continue;
563
1.33k
      }
564
565
25.4k
      r = huffman_decode_symbol(inf, &inf->clct, &inf->code);
566
25.4k
      if (r)
567
98
        return r;
568
569
25.3k
      switch (inf->code) {
570
1.57k
      case 16:
571
1.57k
        inf->state = UPNS_ID_BL_GB_BTYPE_2_16;
572
1.57k
        continue;
573
736
      case 17:
574
736
        inf->state = UPNS_ID_BL_GB_BTYPE_2_17;
575
736
        continue;
576
3.04k
      case 18:
577
3.04k
        inf->state = UPNS_ID_BL_GB_BTYPE_2_18;
578
3.04k
        continue;
579
20.0k
      default:
580
20.0k
        if (inf->code > 15)
581
0
          return LWS_SRET_FATAL + 9;
582
583
20.0k
        if (inf->i < inf->hlit)
584
16.5k
          inf->bitlen[inf->i] = inf->code;
585
3.47k
        else
586
3.47k
          inf->bitlenD[inf->i - inf->hlit] =
587
3.47k
                inf->code;
588
589
20.0k
        inf->i++;
590
20.0k
        inf->treepos = 0;
591
592
        /* stay in 2e */
593
20.0k
        continue;
594
25.3k
      }
595
596
1.57k
    case UPNS_ID_BL_GB_BTYPE_2_16: /* repeat previous */
597
1.57k
      r = read_bits(inf, 2, &tu);
598
1.57k
      if (r)
599
19
        return r;
600
1.55k
      count = tu + 3;
601
602
1.55k
      if (!inf->i) /* from google fuzzer */
603
6
        return LWS_SRET_FATAL + 29;
604
605
1.54k
      if (inf->i - 1 < inf->hlit)
606
1.29k
        val = inf->bitlen[inf->i - 1];
607
253
      else
608
253
        val = inf->bitlenD[inf->i - inf->hlit - 1];
609
610
1.54k
      goto fill;
611
612
736
    case UPNS_ID_BL_GB_BTYPE_2_17: /*repeat "0" 3-10 times */
613
736
      r = read_bits(inf, 3, &tu);
614
736
      if (r)
615
22
        return r;
616
714
      count = tu + 3;
617
618
714
      val = 0;
619
714
      goto fill;
620
621
3.04k
    case UPNS_ID_BL_GB_BTYPE_2_18: /*repeat "0" 11-138 times */
622
3.04k
      r = read_bits(inf, 7, &tu);
623
3.04k
      if (r)
624
29
        return r;
625
3.01k
      count = tu + 11;
626
3.01k
      val = 0;
627
5.27k
fill:
628
629
5.27k
      if (inf->i + count > inf->hlit + inf->hdist) {
630
25
        lwsl_err("%s: inf->i (%d) > %d\n", __func__,
631
25
          inf->i + count, inf->hlit + inf->hdist);
632
25
        return LWS_SRET_FATAL + 10;
633
25
      }
634
635
5.24k
      {
636
5.24k
        unsigned int n;
637
638
363k
        for (n = 0; n < count; n++) {
639
640
358k
          if (inf->i < inf->hlit)
641
355k
            inf->bitlen[inf->i] = val;
642
2.27k
          else
643
2.27k
            inf->bitlenD[inf->i - inf->hlit] = val;
644
645
358k
          inf->i++;
646
358k
        }
647
5.24k
      }
648
5.24k
      inf->state = UPNS_ID_BL_GB_BTYPE_2e;
649
5.24k
      inf->treepos = 0;
650
5.24k
      continue;
651
652
653
77.1k
    case UPNS_ID_BL_GB_SPIN:
654
655
77.1k
      r = huffman_decode_symbol(inf, &inf->ct, &inf->code);
656
77.1k
      if (r)
657
171
        return r;
658
659
76.9k
      if (inf->code >= FIRST_LENGTH_CODE_INDEX &&
660
6.82k
          inf->code - FIRST_LENGTH_CODE_INDEX <
661
6.82k
              LWS_ARRAY_SIZE(huff_length_base))
662
6.22k
        inf->length = huff_length_base[inf->code -
663
6.22k
                                     FIRST_LENGTH_CODE_INDEX];
664
70.7k
      else
665
70.7k
        inf->length = 0;
666
667
76.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.29k
        done = inf->done;
673
2.29k
        inf->state = UPNS_ID_BL_GB_DONE;
674
2.29k
        continue;
675
2.29k
      }
676
677
74.6k
      if (inf->code <= 255) {
678
67.8k
        inf->state = UPNS_ID_BL_GB_SPINa;
679
67.8k
        continue;
680
67.8k
      }
681
682
6.82k
      if (inf->code < FIRST_LENGTH_CODE_INDEX ||
683
6.82k
          inf->code > LAST_LENGTH_CODE_INDEX) {
684
1.11k
        inf->treepos = 0;
685
1.11k
        continue;
686
1.11k
      }
687
688
5.71k
      inf->exbits = huff_length_extra[inf->code -
689
5.71k
                                      FIRST_LENGTH_CODE_INDEX];
690
5.71k
      inf->state = UPNS_ID_BL_GB_SPINb;
691
692
      /* fallthru */
693
5.71k
    case UPNS_ID_BL_GB_SPINb:
694
5.71k
      r = read_bits(inf, (unsigned int)inf->exbits, &tu);
695
5.71k
      if (r)
696
2
        return r;
697
698
5.70k
      inf->length += tu;
699
5.70k
      inf->state++;
700
5.70k
      inf->treepos = 0;
701
702
      /* fallthru */
703
5.70k
    case UPNS_ID_BL_GB_SPINc:
704
705
      /* part 3: get distance code */
706
707
5.70k
      r = huffman_decode_symbol(inf, &inf->ctD, &inf->codeD);
708
5.70k
      if (r)
709
50
        return r;
710
711
      /* invalid distance code (30-31 are never used) */
712
5.65k
      if (inf->codeD > 29) {
713
9
        lwsl_err("%s: invalid dist %d\n", __func__, inf->codeD);
714
9
        return LWS_SRET_FATAL + 11;
715
9
      }
716
717
5.65k
      inf->distance = huff_distance_base[inf->codeD];
718
719
      /* part 4: get extra bits from distance */
720
721
5.65k
      inf->exbitsD = huff_distance_extra[inf->codeD];
722
5.65k
      inf->state++;
723
724
      /* fallthru */
725
5.65k
    case UPNS_ID_BL_GB_SPINd:
726
727
5.65k
      r = read_bits(inf, inf->exbitsD, &tu);
728
5.65k
      if (r)
729
19
        return r;
730
731
5.63k
      inf->distance += tu;
732
733
5.63k
      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
5.63k
      inf->start = inf->outpos;
744
5.63k
      inf->forward = 0;
745
5.63k
      inf->backward = inf->distance; /* from inf->start */
746
747
5.63k
      inf->state++;
748
749
      /* fallthru */
750
185k
    case UPNS_ID_BL_GB_SPINe:
751
752
185k
      if (inf->forward >= inf->length) {
753
5.62k
        inf->treepos = 0;
754
5.62k
        inf->state = UPNS_ID_BL_GB_SPIN;
755
5.62k
        continue;
756
5.62k
      }
757
758
180k
      if (inf->backward <= inf->start)
759
126k
        virt = inf->start - inf->backward;
760
53.9k
      else /* wrapped... backward >= start */
761
53.9k
        virt = inf->info_size -
762
53.9k
          (inf->backward - inf->start);
763
764
180k
      if (virt >= inf->info_size)
765
0
        lwsl_err("virt %d\n", (int)virt);
766
767
180k
      inf->out[inf->outpos++] = inf->out[virt];
768
180k
      if (inf->outpos >= inf->outlen)
769
0
        inf->outpos = 0;
770
771
180k
      inf->outpos_linear++;
772
180k
      inf->backward--;
773
774
180k
      if (!inf->backward)
775
56.7k
        inf->backward = inf->distance;
776
777
180k
      inf->forward++;
778
779
180k
      if (inf->outpos_linear - inf->consumed_linear >=
780
180k
                inf->bypl + 1)
781
9
        return LWS_SRET_WANT_OUTPUT;
782
783
180k
      continue;
784
785
180k
    case UPNS_ID_BL_GB_SPINa:
786
787
67.8k
      inf->out[inf->outpos++] = (uint8_t)inf->code;
788
67.8k
      if (inf->outpos >= inf->outlen)
789
0
        inf->outpos = 0;
790
791
67.8k
      inf->outpos_linear++;
792
67.8k
      inf->treepos = 0;
793
67.8k
      inf->state = UPNS_ID_BL_GB_SPIN;
794
795
67.8k
      if (inf->outpos_linear - inf->consumed_linear >=
796
67.8k
                inf->bypl + 1)
797
6
        return LWS_SRET_WANT_OUTPUT;
798
799
67.8k
      continue;
800
801
802
67.8k
    case UPNS_ID_BL_GB_GZIP_ID1:
803
1.16k
      r = read_byte(inf, &t);
804
1.16k
      if (r)
805
29
        return r;
806
1.14k
      if (t != 0x1f)
807
47
        return LWS_SRET_FATAL + 32;
808
1.09k
      inf->state++;
809
810
      /* fallthru */
811
812
1.09k
    case UPNS_ID_BL_GB_GZIP_ID2:
813
1.09k
      r = read_byte(inf, &t);
814
1.09k
      if (r)
815
2
        return r;
816
1.09k
      if (t != 0x8b)
817
17
        return LWS_SRET_FATAL + 33;
818
1.07k
      inf->state++;
819
820
      /* fallthru */
821
822
1.07k
    case UPNS_ID_BL_GB_GZIP_METHOD:
823
1.07k
      r = read_byte(inf, &t);
824
1.07k
      if (r)
825
1
        return r;
826
1.07k
      if (t != 8)
827
14
        return LWS_SRET_FATAL + 34;
828
1.05k
      inf->state++;
829
830
      /* fallthru */
831
832
1.05k
    case UPNS_ID_BL_GB_GZIP_FLAGS:
833
1.05k
      r = read_byte(inf, &t);
834
1.05k
      if (r)
835
1
        return r;
836
1.05k
      if (t & 0xe0)
837
6
        return LWS_SRET_FATAL + 35;
838
1.05k
      inf->gz_flags = t;
839
1.05k
      inf->state++;
840
1.05k
      inf->ctr = 6;
841
842
      /* fallthru */
843
844
7.30k
    case UPNS_ID_BL_GB_GZIP_EOH:
845
      /* we want skip 6 bytes */
846
7.30k
      if (inf->ctr--) {
847
6.26k
        r = read_byte(inf, &t);
848
6.26k
        if (r)
849
11
          return r;
850
851
6.25k
        continue;
852
6.26k
      }
853
854
1.04k
      if (inf->gz_flags & 4)
855
257
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_EXTRA_C1;
856
784
      else
857
784
      if (inf->gz_flags & 8)
858
47
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_FILENAME;
859
737
      else
860
737
      if (inf->gz_flags & 16)
861
35
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_COMMENT;
862
702
      else
863
702
      if (inf->gz_flags & 2) {
864
6
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_CRC;
865
6
        inf->ctr = 2;
866
6
      } else
867
696
        inf->state = UPNS_ID_BL_GB_DONE;
868
869
1.04k
      continue;
870
871
257
    case UPNS_ID_BL_GB_GZIP_SKIP_EXTRA_C1:
872
257
      r = read_byte(inf, &t);
873
257
      if (r)
874
1
        return r;
875
876
256
      inf->ctr = t;
877
878
256
      inf->state++;
879
880
      /* fallthru */
881
882
256
    case UPNS_ID_BL_GB_GZIP_SKIP_EXTRA_C2:
883
256
      r = read_byte(inf, &t);
884
256
      if (r)
885
1
        return r;
886
887
255
      inf->ctr = (uint16_t)(inf->ctr | (t << 8));
888
889
255
      inf->state++;
890
891
      /* fallthru */
892
893
525k
    case UPNS_ID_BL_GB_GZIP_SKIP_EXTRA:
894
525k
      if (inf->ctr--) {
895
525k
        r = read_byte(inf, &t);
896
525k
        if (r)
897
49
          return r;
898
899
525k
        continue;
900
525k
      }
901
902
206
      if (inf->gz_flags & 8)
903
193
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_FILENAME;
904
13
      else
905
13
      if (inf->gz_flags & 16)
906
1
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_COMMENT;
907
12
      else
908
12
      if (inf->gz_flags & 2) {
909
7
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_CRC;
910
7
        inf->ctr = 2;
911
7
      } else
912
5
        inf->state = UPNS_ID_BL_GB_DONE;
913
914
206
      continue;
915
916
13.7M
    case UPNS_ID_BL_GB_GZIP_SKIP_FILENAME: /* zero-terminated */
917
13.7M
      r = read_byte(inf, &t);
918
13.7M
      if (r)
919
35
        return r;
920
921
13.7M
      if (t)
922
13.7M
        continue;
923
924
205
      if (inf->gz_flags & 16)
925
187
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_COMMENT;
926
18
      else
927
18
      if (inf->gz_flags & 2) {
928
7
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_CRC;
929
7
        inf->ctr = 2;
930
7
      } else
931
11
        inf->state = UPNS_ID_BL_GB_DONE;
932
933
205
      continue;
934
935
4.01M
    case UPNS_ID_BL_GB_GZIP_SKIP_COMMENT: /* zero-terminated */
936
4.01M
      r = read_byte(inf, &t);
937
4.01M
      if (r)
938
40
        return r;
939
940
4.01M
      if (t)
941
4.01M
        continue;
942
943
183
      if (inf->gz_flags & 2) {
944
168
        inf->state = UPNS_ID_BL_GB_GZIP_SKIP_CRC;
945
168
        inf->ctr = 2;
946
168
      }
947
15
      else
948
15
        inf->state = UPNS_ID_BL_GB_DONE;
949
950
183
      continue;
951
952
550
    case UPNS_ID_BL_GB_GZIP_SKIP_CRC:
953
550
      if (inf->ctr--) {
954
370
        r = read_byte(inf, &t);
955
370
        if (r)
956
8
          return r;
957
958
362
        continue;
959
370
      }
960
180
      inf->state = UPNS_ID_BL_GB_DONE;
961
180
      continue;
962
18.6M
    }
963
18.6M
  }
964
965
11
  return LWS_SRET_OK;
966
1.16k
}
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.16k
{
972
1.16k
  inflator_ctx_t *inf = lws_zalloc(sizeof(*inf), __func__);
973
974
1.16k
  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.16k
  inf->info_size  = 32768;
982
1.16k
  inf->bypl = 0;
983
1.16k
  inf->outlen = inf->info_size;
984
1.16k
  inf->outpos = 0;
985
1.16k
  inf->state  = UPNS_ID_BL_GB_GZIP_ID1;
986
987
1.16k
  inf->out = (uint8_t *)lws_malloc(inf->info_size, __func__);
988
1.16k
  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.16k
  *outring = inf->out;
998
1.16k
  *outringlen = inf->info_size;
999
1.16k
  *opl = &inf->outpos_linear;
1000
1.16k
  *cl = &inf->consumed_linear;
1001
1002
1.16k
  return inf;
1003
1.16k
}
1004
1005
lws_stateful_ret_t
1006
lws_upng_inflate_data(struct inflator_ctx *inf, const void *buf, size_t len)
1007
1.16k
{
1008
1.16k
  lws_stateful_ret_t r;
1009
1010
1.16k
  if (buf) {
1011
1.16k
    inf->in   = buf;
1012
1.16k
    inf->inlen  = len;
1013
1.16k
    inf->inpos  = 0;
1014
1.16k
    inf->bp   = 0;
1015
1.16k
  }
1016
1017
1.16k
  if (!inf->bypl)
1018
1.16k
    inf->bypl = 4095;
1019
1020
1.16k
  r = _lws_upng_inflate_data(inf);
1021
1022
1.16k
  if ((inf->bp >> 3) == inf->inlen) {
1023
989
    inf->archive_pos += inf->inlen;
1024
989
    inf->inlen = 0;
1025
989
    inf->bp = 0;
1026
989
  }
1027
1028
1.16k
  return r;
1029
1.16k
}
1030
1031
void
1032
lws_upng_inflator_destroy(struct inflator_ctx **inf)
1033
1.16k
{
1034
1.16k
  lws_free_set_NULL((*inf)->out);
1035
  lws_free_set_NULL(*inf);
1036
1.16k
}
1037
1038