Coverage Report

Created: 2025-08-03 06:52

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