/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 | 461k | { |
161 | 461k | size_t bo = inf->bp >> 3; |
162 | | |
163 | 461k | if (bo + inf->inpos >= inf->inlen) |
164 | 525 | return LWS_SRET_WANT_INPUT; |
165 | | |
166 | 460k | *bits = (uint8_t)((*(inf->in + inf->inpos + bo) >> (inf->bp & 7)) & 1); |
167 | | |
168 | 460k | inf->bp++; |
169 | | |
170 | 460k | return LWS_SRET_OK; |
171 | 461k | } |
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 | 36.0k | { |
178 | 36.0k | lws_stateful_ret_t r; |
179 | 36.0k | uint8_t b; |
180 | | |
181 | 36.0k | if (!inf->read_bits_ongoing) { |
182 | 36.0k | inf->read_bits_ongoing = 1; |
183 | 36.0k | inf->read_bits_shifter = 0; |
184 | 36.0k | inf->read_bits_limit = nbits; |
185 | 36.0k | inf->read_bits_i = 0; |
186 | 36.0k | } |
187 | | |
188 | 151k | while (inf->read_bits_i < inf->read_bits_limit) { |
189 | 115k | r =read_bit(inf, &b); |
190 | 115k | if (r) |
191 | 197 | return r; |
192 | | |
193 | 115k | inf->read_bits_shifter = inf->read_bits_shifter | (unsigned int)(b << inf->read_bits_i); |
194 | | |
195 | 115k | inf->read_bits_i++; |
196 | 115k | } |
197 | | |
198 | 35.8k | inf->read_bits_ongoing = 0; |
199 | 35.8k | *bits = inf->read_bits_shifter; |
200 | | |
201 | 35.8k | return LWS_SRET_OK; |
202 | 36.0k | } |
203 | | |
204 | | static lws_stateful_ret_t |
205 | | read_byte(inflator_ctx_t *inf, uint8_t *byte) |
206 | 21.6M | { |
207 | 21.6M | size_t bo; |
208 | | |
209 | 21.6M | while (inf->bp & 7) |
210 | 896 | inf->bp++; |
211 | | |
212 | 21.6M | bo = inf->bp >> 3; |
213 | | |
214 | 21.6M | if (bo + inf->inpos >= inf->inlen) |
215 | 301 | return LWS_SRET_WANT_INPUT; |
216 | | |
217 | 21.6M | *byte = *(inf->in + inf->inpos + bo); |
218 | | |
219 | 21.6M | inf->bp += 8; |
220 | | |
221 | 21.6M | return LWS_SRET_OK; |
222 | 21.6M | } |
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 | 9.97k | { |
229 | 9.97k | tree->tree2d = buffer; |
230 | | |
231 | 9.97k | tree->numcodes = numcodes; |
232 | 9.97k | tree->maxbitlen = maxbitlen; |
233 | 9.97k | } |
234 | | |
235 | 2.72M | #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.86k | { |
245 | 5.86k | unsigned int tree1d[NUM_DEFLATE_CODE_SYMBOLS], /* sized to worst */ |
246 | 5.86k | blcount[NUM_DEFLATE_CODE_SYMBOLS], /* sized to worst */ |
247 | 5.86k | nextcode[MAX_BIT_LENGTH + 1], bits, n, i, |
248 | 5.86k | nodefilled = 0, treepos = 0; |
249 | | |
250 | 5.86k | memset(blcount, 0, sizeof(blcount)); |
251 | 5.86k | memset(nextcode, 0, sizeof(nextcode)); |
252 | | |
253 | 5.86k | assert(tree->numcodes <= LWS_ARRAY_SIZE(blcount)); |
254 | | |
255 | 646k | for (bits = 0; bits < tree->numcodes; bits++) { |
256 | | /* any counts exceeding our private buffer length are fatal */ |
257 | 640k | if (bitlen[bits] >= LWS_ARRAY_SIZE(blcount)) |
258 | 0 | return LWS_SRET_FATAL + 1; |
259 | | |
260 | 640k | blcount[bitlen[bits]]++; |
261 | 640k | } |
262 | | |
263 | 5.86k | assert(tree->maxbitlen && tree->maxbitlen - 1u <= LWS_ARRAY_SIZE(blcount)); |
264 | 5.86k | assert(tree->maxbitlen - 1u <= LWS_ARRAY_SIZE(nextcode)); |
265 | | |
266 | 76.8k | for (bits = 1; bits <= (unsigned int)tree->maxbitlen; bits++) |
267 | 70.9k | nextcode[bits] = (nextcode[bits - 1] + blcount[bits - 1]) << 1; |
268 | | |
269 | 5.86k | assert(tree->numcodes <= LWS_ARRAY_SIZE(tree1d)); |
270 | | |
271 | 646k | for (n = 0; n < tree->numcodes; n++) |
272 | 640k | if (bitlen[n]) |
273 | 27.2k | tree1d[n] = nextcode[bitlen[n]]++; |
274 | | |
275 | 1.28M | for (n = 0; n < (unsigned int)tree->numcodes * 2u; n++) |
276 | 1.28M | tree->tree2d[n] = EMPTY; |
277 | | |
278 | 644k | for (n = 0; n < tree->numcodes; n++) { /* the codes */ |
279 | 807k | for (i = 0; i < bitlen[n]; i++) { /* the bits for this code */ |
280 | 168k | uint8_t bit = (uint8_t)((tree1d[n] >> |
281 | 168k | (bitlen[n] - i - 1)) & 1); |
282 | | |
283 | | /* check if oversubscribed */ |
284 | 168k | if (treepos > tree->numcodes - 2u) |
285 | 30 | return LWS_SRET_FATAL + 1; |
286 | | |
287 | 168k | if (tree->tree2d[2 * treepos + bit] == EMPTY) { |
288 | 69.8k | if (i + 1 == bitlen[n]) { /* ... last bit */ |
289 | 26.5k | tree->tree2d[2 * treepos + bit] = (huff_t)n; |
290 | 26.5k | treepos = 0; |
291 | 43.2k | } else { |
292 | 43.2k | nodefilled++; |
293 | 43.2k | tree->tree2d[2 * treepos + bit] = |
294 | 43.2k | (huff_t)(nodefilled + tree->numcodes); |
295 | 43.2k | treepos = nodefilled; |
296 | 43.2k | } |
297 | 69.8k | } else |
298 | 98.9k | treepos = (unsigned int)(tree->tree2d[2 * treepos + bit] - |
299 | 98.9k | tree->numcodes); |
300 | 168k | } |
301 | 639k | } |
302 | | |
303 | 1.27M | for (n = 0; n < tree->numcodes * 2u; n++) |
304 | 1.27M | if (tree->tree2d[n] == EMPTY) |
305 | 1.20M | tree->tree2d[n] = 0; |
306 | | |
307 | 5.83k | return LWS_SRET_OK; |
308 | 5.86k | } |
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 | 106k | { |
315 | 106k | lws_stateful_ret_t r; |
316 | 106k | uint8_t bit; |
317 | | |
318 | 331k | do { |
319 | 331k | r = read_bit(inf, &bit); |
320 | 331k | if (r) |
321 | 288 | return r; |
322 | | |
323 | 330k | *uct = ct->tree2d[(inf->treepos << 1) | bit]; |
324 | 330k | if (*uct < ct->numcodes) |
325 | 106k | return LWS_SRET_OK; |
326 | | |
327 | 224k | inf->treepos = *uct - ct->numcodes; |
328 | 224k | if (inf->treepos >= ct->numcodes) |
329 | 0 | return LWS_SRET_FATAL + 2; |
330 | 224k | } while (1); |
331 | 106k | } |
332 | | |
333 | | /* |
334 | | * Every decoded byte goes through here, into the sliding window ringbuffer, |
335 | | * which is also our output buffer. A gzip member's trailer carries the CRC32 |
336 | | * of its decoded data, so we track it as we go. |
337 | | */ |
338 | | |
339 | | static void |
340 | | inflate_emit(inflator_ctx_t *inf, uint8_t b) |
341 | 202k | { |
342 | 202k | lws_upng_win(inf, inf->outpos) = b; |
343 | 202k | inf->outpos++; |
344 | 202k | if (inf->outpos >= inf->outlen) |
345 | 0 | inf->outpos = 0; |
346 | 202k | inf->outpos_linear++; |
347 | | |
348 | 202k | if (inf->gzip) |
349 | 202k | inf->crc32 = lws_crc32(inf->crc32, &b, 1); |
350 | 202k | } |
351 | | |
352 | | /* |
353 | | * A block of any type has ended. RFC1951 3.2.3: if its BFINAL bit was set, |
354 | | * it was the last block of the deflate stream, and what follows belongs to |
355 | | * the container: for gzip, its trailer; for PNG's zlib, the Adler-32 that |
356 | | * upng does not check. Otherwise, the next block header follows. |
357 | | * |
358 | | * Returns nonzero if inflation is complete and we should stop. |
359 | | */ |
360 | | |
361 | | static int |
362 | | inflate_block_ended(inflator_ctx_t *inf) |
363 | 4.04k | { |
364 | 4.04k | inf->treepos = 0; |
365 | | |
366 | 4.04k | if (!inf->done) { |
367 | 3.89k | inf->state = UPNS_ID_BL_GB_DONE; |
368 | | |
369 | 3.89k | return 0; |
370 | 3.89k | } |
371 | | |
372 | 155 | if (inf->gzip) { |
373 | 155 | inf->state = UPNS_ID_BL_GB_GZIP_TRAILER; |
374 | 155 | inf->ctr = 0; |
375 | | |
376 | 155 | return 0; |
377 | 155 | } |
378 | | |
379 | 0 | inf->state = UPNS_ID_BL_GB_END; |
380 | |
|
381 | 0 | return 1; |
382 | 155 | } |
383 | | |
384 | | lws_stateful_ret_t |
385 | | _lws_upng_inflate_data(inflator_ctx_t *inf) |
386 | 1.33k | { |
387 | 1.33k | unsigned int count, val, tu; |
388 | 1.33k | uint8_t t, done = 0; |
389 | 1.33k | lws_stateful_ret_t r; |
390 | 1.33k | size_t virt; |
391 | | |
392 | 21.9M | while (!done) { |
393 | 21.9M | switch (inf->state) { |
394 | 4.99k | case UPNS_ID_BL_GB_DONE: |
395 | 4.99k | r = read_bit(inf, &inf->done); |
396 | 4.99k | if (r) |
397 | 31 | return r; |
398 | 4.96k | inf->state++; |
399 | | |
400 | | /* fallthru */ |
401 | 4.96k | case UPNS_ID_BL_GB_BTYPEb0: |
402 | 4.96k | r = read_bit(inf, &inf->btype); |
403 | 4.96k | if (r) |
404 | 3 | return r; |
405 | 4.95k | inf->state++; |
406 | | |
407 | | /* fallthru */ |
408 | 4.95k | case UPNS_ID_BL_GB_BTYPEb1: |
409 | 4.95k | r = read_bit(inf, &t); |
410 | 4.95k | if (r) |
411 | 6 | return r; |
412 | | |
413 | 4.95k | inf->btype |= (uint8_t)(t << 1); |
414 | | |
415 | 4.95k | if (inf->btype == 3) |
416 | 12 | return LWS_SRET_FATAL + 3; |
417 | | |
418 | 4.94k | inf->i = 0; |
419 | | |
420 | 4.94k | inf->state = UPNS_ID_BL_GB_BTYPE_0 + inf->btype; |
421 | | |
422 | 4.94k | lwsl_debug("%s: (%lu) block type %d\n", __func__, |
423 | 4.94k | (unsigned long)inf->archive_pos + (inf->bp >> 3), |
424 | 4.94k | inf->btype); |
425 | | |
426 | | /* uncompressed starts on a byte boundary */ |
427 | | |
428 | 4.94k | if (!inf->btype && (inf->bp & 7)) { |
429 | 865 | lwsl_debug("%s: skipping %d alignment bits for type 0\n", |
430 | 865 | __func__, (int)(8 - (inf->bp & 7)) & 7); |
431 | 865 | inf->bp = ((inf->bp >> 3) + 1) << 3; |
432 | 865 | } |
433 | 4.94k | continue; |
434 | | |
435 | 1.06k | case UPNS_ID_BL_GB_BTYPE_0: /* no compression */ |
436 | 1.06k | r = read_byte(inf, &t); |
437 | 1.06k | if (r) |
438 | 19 | return r; |
439 | | |
440 | 1.04k | inf->len = t; |
441 | 1.04k | inf->state = UPNS_ID_BL_GB_BTYPE_0a; |
442 | | |
443 | | // lwsl_notice("%s: no compression block\n", __func__); |
444 | | |
445 | | /* fallthru */ |
446 | 1.04k | case UPNS_ID_BL_GB_BTYPE_0a: |
447 | 1.04k | r = read_byte(inf, &t); |
448 | 1.04k | if (r) |
449 | 25 | return r; |
450 | | |
451 | 1.02k | inf->len = inf->len | (unsigned int)(t << 8); |
452 | 1.02k | inf->state++; |
453 | | /* fallthru */ |
454 | | |
455 | 1.02k | case UPNS_ID_BL_GB_BTYPE_0b: |
456 | 1.02k | r = read_byte(inf, &t); |
457 | 1.02k | if (r) |
458 | 10 | return r; |
459 | | |
460 | 1.01k | inf->nlen = t; |
461 | 1.01k | inf->state++; |
462 | | |
463 | | /* fallthru */ |
464 | 1.01k | case UPNS_ID_BL_GB_BTYPE_0c: |
465 | 1.01k | r = read_byte(inf, &t); |
466 | 1.01k | if (r) |
467 | 23 | return r; |
468 | | |
469 | 987 | inf->nlen = inf->nlen | (unsigned int)(t << 8); |
470 | | |
471 | 987 | if (inf->len + inf->nlen != 65535) |
472 | 68 | return LWS_SRET_FATAL + 4; |
473 | | |
474 | 919 | lwsl_debug("%s: type 0 expects len %d\n", __func__, inf->len); |
475 | | |
476 | 919 | inf->state++; |
477 | 919 | inf->n = 0; |
478 | | |
479 | | /* fallthru */ |
480 | 3.79k | case UPNS_ID_BL_GB_BTYPE_0d: |
481 | | |
482 | 3.79k | if (inf->n < inf->len) { |
483 | | |
484 | 2.92k | r = read_byte(inf, &t); |
485 | 2.92k | if (r) |
486 | 42 | return r; |
487 | | |
488 | 2.88k | inflate_emit(inf, t); |
489 | 2.88k | inf->n++; |
490 | | |
491 | 2.88k | if (inf->outpos_linear - inf->consumed_linear >= |
492 | 2.88k | inf->bypl + 1) { |
493 | 1 | return LWS_SRET_WANT_OUTPUT; |
494 | 1 | } |
495 | | |
496 | 2.88k | continue; |
497 | 2.88k | } |
498 | | |
499 | 876 | done = (uint8_t)inflate_block_ended(inf); |
500 | 876 | continue; |
501 | | |
502 | 1.66k | case UPNS_ID_BL_GB_BTYPE_1: /* fixed trees */ |
503 | | |
504 | 1.66k | huffman_tree_init(&inf->ct, |
505 | 1.66k | (huff_t *)FIXED_DEFLATE_CODE_TREE, |
506 | 1.66k | NUM_DEFLATE_CODE_SYMBOLS, |
507 | 1.66k | DEFLATE_CODE_BITLEN); |
508 | 1.66k | huffman_tree_init(&inf->ctD, |
509 | 1.66k | (huff_t *)FIXED_DISTANCE_TREE, |
510 | 1.66k | NUM_DISTANCE_SYMBOLS, |
511 | 1.66k | DISTANCE_BITLEN); |
512 | | |
513 | 1.66k | lwsl_debug("%s: fixed tree init\n", __func__); |
514 | 1.66k | inf->treepos = 0; |
515 | 1.66k | inf->state = UPNS_ID_BL_GB_SPIN; |
516 | 1.66k | continue; |
517 | | |
518 | 2.21k | case UPNS_ID_BL_GB_BTYPE_2: /* dynamic trees */ |
519 | 2.21k | huffman_tree_init(&inf->ct, inf->ct_buffer, |
520 | 2.21k | NUM_DEFLATE_CODE_SYMBOLS, |
521 | 2.21k | DEFLATE_CODE_BITLEN); |
522 | 2.21k | huffman_tree_init(&inf->ctD, inf->ctD_buffer, |
523 | 2.21k | NUM_DISTANCE_SYMBOLS, |
524 | 2.21k | DISTANCE_BITLEN); |
525 | 2.21k | huffman_tree_init(&inf->clct, inf->clct_buffer, |
526 | 2.21k | NUM_CODE_LENGTH_CODES, |
527 | 2.21k | CODE_LENGTH_BITLEN); |
528 | | |
529 | | // lwsl_notice("%s: dyn tree init\n", __func__); |
530 | 2.21k | inf->treepos = 0; |
531 | | |
532 | | /* clear bitlen arrays */ |
533 | 2.21k | memset(inf->bitlen, 0, sizeof(inf->bitlen)); |
534 | 2.21k | memset(inf->bitlenD, 0, sizeof(inf->bitlenD)); |
535 | | |
536 | 2.21k | inf->state = UPNS_ID_BL_GB_BTYPE_2a; |
537 | | |
538 | 2.21k | inf->hlit = 0; |
539 | 2.21k | inf->hdist = 0; |
540 | 2.21k | inf->hclen = 0; |
541 | | |
542 | | /* fallthru */ |
543 | | |
544 | 2.21k | case UPNS_ID_BL_GB_BTYPE_2a: |
545 | 2.21k | r = read_bits(inf, 5, &inf->hlit); |
546 | 2.21k | if (r) |
547 | 12 | return r; |
548 | 2.20k | inf->hlit += 257; |
549 | 2.20k | inf->state++; |
550 | | |
551 | | /* fallthru */ |
552 | 2.20k | case UPNS_ID_BL_GB_BTYPE_2b: |
553 | 2.20k | r = read_bits(inf, 5, &inf->hdist); |
554 | 2.20k | if (r) |
555 | 31 | return r; |
556 | 2.17k | inf->hdist++; |
557 | 2.17k | inf->state++; |
558 | | |
559 | | /* fallthru */ |
560 | 2.17k | case UPNS_ID_BL_GB_BTYPE_2c: |
561 | 2.17k | r = read_bits(inf, 4, &inf->hclen); |
562 | 2.17k | if (r) |
563 | 14 | return r; |
564 | 2.15k | inf->hclen += 4; |
565 | 2.15k | inf->state = UPNS_ID_BL_GB_BTYPE_2d; |
566 | 2.15k | inf->i = 0; |
567 | | |
568 | | /* fallthru */ |
569 | 42.5k | case UPNS_ID_BL_GB_BTYPE_2d: |
570 | 42.5k | if (inf->i < NUM_CODE_LENGTH_CODES) { |
571 | 40.4k | if (inf->i < inf->hclen) { |
572 | 12.3k | r = read_bits(inf, 3, |
573 | 12.3k | &inf->clenc[huff_cl_cl[inf->i]]); |
574 | 12.3k | if (r) |
575 | 41 | return r; |
576 | 12.3k | } else |
577 | | /*if not, it must stay 0 */ |
578 | 28.1k | inf->clenc[huff_cl_cl[inf->i]] = 0; |
579 | | |
580 | 40.4k | inf->i++; |
581 | 40.4k | continue; |
582 | 40.4k | } |
583 | | |
584 | 2.11k | r = huffman_tree_create_lengths(&inf->clct, inf->clenc); |
585 | 2.11k | if (r) |
586 | 15 | return r; |
587 | | |
588 | 2.10k | inf->i = 0; |
589 | 2.10k | inf->state = UPNS_ID_BL_GB_BTYPE_2e; |
590 | 2.10k | inf->treepos = 0; |
591 | | |
592 | | /* fallthru */ |
593 | 30.2k | case UPNS_ID_BL_GB_BTYPE_2e: |
594 | | |
595 | 30.2k | if (inf->i >= inf->hlit + inf->hdist) { |
596 | 1.88k | if (inf->bitlen[256] == 0) |
597 | 7 | return LWS_SRET_FATAL + 6; |
598 | | |
599 | 1.87k | if (huffman_tree_create_lengths(&inf->ct, |
600 | 1.87k | inf->bitlen)) |
601 | 11 | return LWS_SRET_FATAL + 7; |
602 | | |
603 | 1.86k | if (huffman_tree_create_lengths(&inf->ctD, |
604 | 1.86k | inf->bitlenD)) |
605 | 4 | return LWS_SRET_FATAL + 8; |
606 | | |
607 | 1.86k | inf->treepos = 0; |
608 | 1.86k | inf->state = UPNS_ID_BL_GB_SPIN; |
609 | 1.86k | continue; |
610 | 1.86k | } |
611 | | |
612 | 28.3k | r = huffman_decode_symbol(inf, &inf->clct, &inf->code); |
613 | 28.3k | if (r) |
614 | 93 | return r; |
615 | | |
616 | 28.2k | switch (inf->code) { |
617 | 4.56k | case 16: |
618 | 4.56k | inf->state = UPNS_ID_BL_GB_BTYPE_2_16; |
619 | 4.56k | continue; |
620 | 637 | case 17: |
621 | 637 | inf->state = UPNS_ID_BL_GB_BTYPE_2_17; |
622 | 637 | continue; |
623 | 4.21k | case 18: |
624 | 4.21k | inf->state = UPNS_ID_BL_GB_BTYPE_2_18; |
625 | 4.21k | continue; |
626 | 18.8k | default: |
627 | 18.8k | if (inf->code > 15) |
628 | 0 | return LWS_SRET_FATAL + 9; |
629 | | |
630 | 18.8k | if (inf->i < inf->hlit) |
631 | 13.8k | inf->bitlen[inf->i] = inf->code; |
632 | 5.04k | else |
633 | 5.04k | inf->bitlenD[inf->i - inf->hlit] = |
634 | 5.04k | inf->code; |
635 | | |
636 | 18.8k | inf->i++; |
637 | 18.8k | inf->treepos = 0; |
638 | | |
639 | | /* stay in 2e */ |
640 | 18.8k | continue; |
641 | 28.2k | } |
642 | | |
643 | 4.56k | case UPNS_ID_BL_GB_BTYPE_2_16: /* repeat previous */ |
644 | 4.56k | r = read_bits(inf, 2, &tu); |
645 | 4.56k | if (r) |
646 | 25 | return r; |
647 | 4.54k | count = tu + 3; |
648 | | |
649 | 4.54k | if (!inf->i) /* from google fuzzer */ |
650 | 2 | return LWS_SRET_FATAL + 29; |
651 | | |
652 | 4.54k | if (inf->i - 1 < inf->hlit) |
653 | 3.30k | val = inf->bitlen[inf->i - 1]; |
654 | 1.23k | else |
655 | 1.23k | val = inf->bitlenD[inf->i - inf->hlit - 1]; |
656 | | |
657 | 4.54k | goto fill_l; |
658 | | |
659 | 637 | case UPNS_ID_BL_GB_BTYPE_2_17: /*repeat "0" 3-10 times */ |
660 | 637 | r = read_bits(inf, 3, &tu); |
661 | 637 | if (r) |
662 | 19 | return r; |
663 | 618 | count = tu + 3; |
664 | | |
665 | 618 | val = 0; |
666 | 618 | goto fill_l; |
667 | | |
668 | 4.21k | case UPNS_ID_BL_GB_BTYPE_2_18: /*repeat "0" 11-138 times */ |
669 | 4.21k | r = read_bits(inf, 7, &tu); |
670 | 4.21k | if (r) |
671 | 35 | return r; |
672 | 4.18k | count = tu + 11; |
673 | 4.18k | val = 0; |
674 | 9.33k | fill_l: |
675 | | |
676 | 9.33k | if (inf->i + count > inf->hlit + inf->hdist) { |
677 | 45 | lwsl_err("%s: inf->i (%d) > %d\n", __func__, |
678 | 45 | inf->i + count, inf->hlit + inf->hdist); |
679 | 45 | return LWS_SRET_FATAL + 10; |
680 | 45 | } |
681 | | |
682 | 9.29k | { |
683 | 9.29k | unsigned int n; |
684 | | |
685 | 517k | for (n = 0; n < count; n++) { |
686 | | |
687 | 508k | if (inf->i < inf->hlit) |
688 | 501k | inf->bitlen[inf->i] = val; |
689 | 6.90k | else |
690 | 6.90k | inf->bitlenD[inf->i - inf->hlit] = val; |
691 | | |
692 | 508k | inf->i++; |
693 | 508k | } |
694 | 9.29k | } |
695 | 9.29k | inf->state = UPNS_ID_BL_GB_BTYPE_2e; |
696 | 9.29k | inf->treepos = 0; |
697 | 9.29k | continue; |
698 | | |
699 | | |
700 | 74.1k | case UPNS_ID_BL_GB_SPIN: |
701 | | |
702 | 74.1k | r = huffman_decode_symbol(inf, &inf->ct, &inf->code); |
703 | 74.1k | if (r) |
704 | 159 | return r; |
705 | | |
706 | 74.0k | if (inf->code >= FIRST_LENGTH_CODE_INDEX && |
707 | 4.34k | inf->code - FIRST_LENGTH_CODE_INDEX < |
708 | 4.34k | LWS_ARRAY_SIZE(huff_length_base)) |
709 | 4.11k | inf->length = huff_length_base[inf->code - |
710 | 4.11k | FIRST_LENGTH_CODE_INDEX]; |
711 | 69.8k | else |
712 | 69.8k | inf->length = 0; |
713 | | |
714 | 74.0k | if (inf->code == 256) { |
715 | | /* We're finished with this huffman block */ |
716 | 3.17k | done = (uint8_t)inflate_block_ended(inf); |
717 | 3.17k | continue; |
718 | 3.17k | } |
719 | | |
720 | 70.8k | if (inf->code <= 255) { |
721 | 66.4k | inf->state = UPNS_ID_BL_GB_SPINa; |
722 | 66.4k | continue; |
723 | 66.4k | } |
724 | | |
725 | 4.34k | if (inf->code < FIRST_LENGTH_CODE_INDEX || |
726 | 4.34k | inf->code > LAST_LENGTH_CODE_INDEX) { |
727 | 458 | inf->treepos = 0; |
728 | 458 | continue; |
729 | 458 | } |
730 | | |
731 | 3.89k | inf->exbits = huff_length_extra[inf->code - |
732 | 3.89k | FIRST_LENGTH_CODE_INDEX]; |
733 | 3.89k | inf->state = UPNS_ID_BL_GB_SPINb; |
734 | | |
735 | | /* fallthru */ |
736 | 3.89k | case UPNS_ID_BL_GB_SPINb: |
737 | 3.89k | r = read_bits(inf, (unsigned int)inf->exbits, &tu); |
738 | 3.89k | if (r) |
739 | 7 | return r; |
740 | | |
741 | 3.88k | inf->length += tu; |
742 | 3.88k | inf->state++; |
743 | 3.88k | inf->treepos = 0; |
744 | | |
745 | | /* fallthru */ |
746 | 3.88k | case UPNS_ID_BL_GB_SPINc: |
747 | | |
748 | | /* part 3: get distance code */ |
749 | | |
750 | 3.88k | r = huffman_decode_symbol(inf, &inf->ctD, &inf->codeD); |
751 | 3.88k | if (r) |
752 | 36 | return r; |
753 | | |
754 | | /* invalid distance code (30-31 are never used) */ |
755 | 3.84k | if (inf->codeD > 29) { |
756 | 14 | lwsl_err("%s: invalid dist %d\n", __func__, inf->codeD); |
757 | 14 | return LWS_SRET_FATAL + 11; |
758 | 14 | } |
759 | | |
760 | 3.83k | inf->distance = huff_distance_base[inf->codeD]; |
761 | | |
762 | | /* part 4: get extra bits from distance */ |
763 | | |
764 | 3.83k | inf->exbitsD = huff_distance_extra[inf->codeD]; |
765 | 3.83k | inf->state++; |
766 | | |
767 | | /* fallthru */ |
768 | 3.83k | case UPNS_ID_BL_GB_SPINd: |
769 | | |
770 | 3.83k | r = read_bits(inf, inf->exbitsD, &tu); |
771 | 3.83k | if (r) |
772 | 13 | return r; |
773 | | |
774 | 3.82k | inf->distance += tu; |
775 | | |
776 | | /* |
777 | | * RFC1951: a match may not reference further back than |
778 | | * the number of bytes we already produced, ie, "invalid |
779 | | * distance too far back". Without this, the first |
780 | | * bytes of the stream can copy out of the part of the |
781 | | * sliding window we never wrote, leaking uninitialized |
782 | | * heap into the decode output. It must also not exceed |
783 | | * the sliding window itself, since anything older than |
784 | | * that has already been overwritten in the ring. |
785 | | */ |
786 | | |
787 | 3.82k | if (inf->distance > inf->outpos_linear || |
788 | 3.70k | inf->distance > inf->info_size) { |
789 | 114 | lwsl_err("%s: distance %lu too far back\n", |
790 | 114 | __func__, |
791 | 114 | (unsigned long)inf->distance); |
792 | | |
793 | 114 | return LWS_SRET_FATAL + 12; |
794 | 114 | } |
795 | | |
796 | | /* |
797 | | * Part 5: fill in all the out[n] values based |
798 | | * on the length and dist |
799 | | */ |
800 | 3.70k | inf->start = inf->outpos; |
801 | 3.70k | inf->forward = 0; |
802 | 3.70k | inf->backward = inf->distance; /* from inf->start */ |
803 | | |
804 | 3.70k | inf->state++; |
805 | | |
806 | | /* fallthru */ |
807 | 136k | case UPNS_ID_BL_GB_SPINe: |
808 | | |
809 | 136k | if (inf->forward >= inf->length) { |
810 | 3.70k | inf->treepos = 0; |
811 | 3.70k | inf->state = UPNS_ID_BL_GB_SPIN; |
812 | 3.70k | continue; |
813 | 3.70k | } |
814 | | |
815 | 133k | if (inf->backward <= inf->start) |
816 | 133k | virt = inf->start - inf->backward; |
817 | 0 | else /* wrapped... backward >= start */ |
818 | 0 | virt = inf->info_size - |
819 | 0 | (inf->backward - inf->start); |
820 | | |
821 | 133k | if (virt >= inf->info_size) { |
822 | 0 | lwsl_err("virt %d\n", (int)virt); |
823 | 0 | return LWS_SRET_FATAL + 10; |
824 | 0 | } |
825 | | |
826 | 133k | inflate_emit(inf, lws_upng_win(inf, virt)); |
827 | 133k | inf->backward--; |
828 | | |
829 | 133k | if (!inf->backward) |
830 | 88.8k | inf->backward = inf->distance; |
831 | | |
832 | 133k | inf->forward++; |
833 | | |
834 | 133k | if (inf->outpos_linear - inf->consumed_linear >= |
835 | 133k | inf->bypl + 1) |
836 | 5 | return LWS_SRET_WANT_OUTPUT; |
837 | | |
838 | 133k | continue; |
839 | | |
840 | 133k | case UPNS_ID_BL_GB_SPINa: |
841 | | |
842 | 66.4k | inflate_emit(inf, (uint8_t)inf->code); |
843 | 66.4k | inf->treepos = 0; |
844 | 66.4k | inf->state = UPNS_ID_BL_GB_SPIN; |
845 | | |
846 | 66.4k | if (inf->outpos_linear - inf->consumed_linear >= |
847 | 66.4k | inf->bypl + 1) |
848 | 3 | return LWS_SRET_WANT_OUTPUT; |
849 | | |
850 | 66.4k | continue; |
851 | | |
852 | | |
853 | 66.4k | case UPNS_ID_BL_GB_GZIP_ID1: |
854 | 1.33k | r = read_byte(inf, &t); |
855 | 1.33k | if (r) |
856 | 19 | return r; |
857 | 1.31k | if (t != 0x1f) |
858 | 44 | return LWS_SRET_FATAL + 32; |
859 | 1.27k | inf->state++; |
860 | | |
861 | | /* fallthru */ |
862 | | |
863 | 1.27k | case UPNS_ID_BL_GB_GZIP_ID2: |
864 | 1.27k | r = read_byte(inf, &t); |
865 | 1.27k | if (r) |
866 | 1 | return r; |
867 | 1.27k | if (t != 0x8b) |
868 | 13 | return LWS_SRET_FATAL + 33; |
869 | 1.25k | inf->state++; |
870 | | |
871 | | /* fallthru */ |
872 | | |
873 | 1.25k | case UPNS_ID_BL_GB_GZIP_METHOD: |
874 | 1.25k | r = read_byte(inf, &t); |
875 | 1.25k | if (r) |
876 | 1 | return r; |
877 | 1.25k | if (t != 8) |
878 | 10 | return LWS_SRET_FATAL + 34; |
879 | 1.24k | inf->state++; |
880 | | |
881 | | /* fallthru */ |
882 | | |
883 | 1.24k | case UPNS_ID_BL_GB_GZIP_FLAGS: |
884 | 1.24k | r = read_byte(inf, &t); |
885 | 1.24k | if (r) |
886 | 1 | return r; |
887 | 1.24k | if (t & 0xe0) |
888 | 6 | return LWS_SRET_FATAL + 35; |
889 | 1.24k | inf->gz_flags = t; |
890 | 1.24k | inf->state++; |
891 | 1.24k | inf->ctr = 6; |
892 | | |
893 | | /* fallthru */ |
894 | | |
895 | 8.62k | case UPNS_ID_BL_GB_GZIP_EOH: |
896 | | /* we want skip 6 bytes */ |
897 | 8.62k | if (inf->ctr) { |
898 | 7.39k | r = read_byte(inf, &t); |
899 | 7.39k | if (r) |
900 | 13 | return r; |
901 | | |
902 | | /* count it once read: input may run out first */ |
903 | 7.38k | inf->ctr--; |
904 | 7.38k | continue; |
905 | 7.39k | } |
906 | | |
907 | 1.22k | if (inf->gz_flags & 4) |
908 | 288 | inf->state = UPNS_ID_BL_GB_GZIP_SKIP_EXTRA_C1; |
909 | 940 | else if (inf->gz_flags & 8) |
910 | 43 | inf->state = UPNS_ID_BL_GB_GZIP_SKIP_FILENAME; |
911 | 897 | else if (inf->gz_flags & 16) |
912 | 33 | inf->state = UPNS_ID_BL_GB_GZIP_SKIP_COMMENT; |
913 | 864 | else if (inf->gz_flags & 2) { |
914 | 7 | inf->state = UPNS_ID_BL_GB_GZIP_SKIP_CRC; |
915 | 7 | inf->ctr = 2; |
916 | 7 | } else |
917 | 857 | inf->state = UPNS_ID_BL_GB_DONE; |
918 | | |
919 | 1.22k | continue; |
920 | | |
921 | 288 | case UPNS_ID_BL_GB_GZIP_SKIP_EXTRA_C1: |
922 | 288 | r = read_byte(inf, &t); |
923 | 288 | if (r) |
924 | 1 | return r; |
925 | | |
926 | 287 | inf->ctr = t; |
927 | | |
928 | 287 | inf->state++; |
929 | | |
930 | | /* fallthru */ |
931 | | |
932 | 287 | case UPNS_ID_BL_GB_GZIP_SKIP_EXTRA_C2: |
933 | 287 | r = read_byte(inf, &t); |
934 | 287 | if (r) |
935 | 1 | return r; |
936 | | |
937 | 286 | inf->ctr = (uint16_t)(inf->ctr | (t << 8)); |
938 | | |
939 | 286 | inf->state++; |
940 | | |
941 | | /* fallthru */ |
942 | | |
943 | 1.39M | case UPNS_ID_BL_GB_GZIP_SKIP_EXTRA: |
944 | 1.39M | if (inf->ctr) { |
945 | 1.39M | r = read_byte(inf, &t); |
946 | 1.39M | if (r) |
947 | 44 | return r; |
948 | | |
949 | | /* count it once read: input may run out first */ |
950 | 1.39M | inf->ctr--; |
951 | 1.39M | continue; |
952 | 1.39M | } |
953 | | |
954 | 242 | if (inf->gz_flags & 8) |
955 | 227 | inf->state = UPNS_ID_BL_GB_GZIP_SKIP_FILENAME; |
956 | 15 | else if (inf->gz_flags & 16) |
957 | 10 | inf->state = UPNS_ID_BL_GB_GZIP_SKIP_COMMENT; |
958 | 5 | else if (inf->gz_flags & 2) { |
959 | 3 | inf->state = UPNS_ID_BL_GB_GZIP_SKIP_CRC; |
960 | 3 | inf->ctr = 2; |
961 | 3 | } else |
962 | 2 | inf->state = UPNS_ID_BL_GB_DONE; |
963 | | |
964 | 242 | continue; |
965 | | |
966 | 17.8M | case UPNS_ID_BL_GB_GZIP_SKIP_FILENAME: /* zero-terminated */ |
967 | 17.8M | r = read_byte(inf, &t); |
968 | 17.8M | if (r) |
969 | 35 | return r; |
970 | | |
971 | 17.8M | if (t) |
972 | 17.8M | continue; |
973 | | |
974 | 235 | if (inf->gz_flags & 16) |
975 | 214 | inf->state = UPNS_ID_BL_GB_GZIP_SKIP_COMMENT; |
976 | 21 | else if (inf->gz_flags & 2) { |
977 | 9 | inf->state = UPNS_ID_BL_GB_GZIP_SKIP_CRC; |
978 | 9 | inf->ctr = 2; |
979 | 9 | } else |
980 | 12 | inf->state = UPNS_ID_BL_GB_DONE; |
981 | | |
982 | 235 | continue; |
983 | | |
984 | 2.39M | case UPNS_ID_BL_GB_GZIP_SKIP_COMMENT: /* zero-terminated */ |
985 | 2.39M | r = read_byte(inf, &t); |
986 | 2.39M | if (r) |
987 | 36 | return r; |
988 | | |
989 | 2.39M | if (t) |
990 | 2.39M | continue; |
991 | | |
992 | 221 | if (inf->gz_flags & 2) { |
993 | 214 | inf->state = UPNS_ID_BL_GB_GZIP_SKIP_CRC; |
994 | 214 | inf->ctr = 2; |
995 | 214 | } |
996 | 7 | else |
997 | 7 | inf->state = UPNS_ID_BL_GB_DONE; |
998 | | |
999 | 221 | continue; |
1000 | | |
1001 | 682 | case UPNS_ID_BL_GB_GZIP_SKIP_CRC: |
1002 | 682 | if (inf->ctr) { |
1003 | 460 | r = read_byte(inf, &t); |
1004 | 460 | if (r) |
1005 | 11 | return r; |
1006 | | |
1007 | | /* count it once read: input may run out first */ |
1008 | 449 | inf->ctr--; |
1009 | 449 | continue; |
1010 | 460 | } |
1011 | 222 | inf->state = UPNS_ID_BL_GB_DONE; |
1012 | 222 | continue; |
1013 | | |
1014 | 155 | case UPNS_ID_BL_GB_GZIP_TRAILER: |
1015 | | /* |
1016 | | * RFC1952 2.3.1: the CRC32 of the uncompressed data, |
1017 | | * then its length modulo 2^32, both little-endian, on |
1018 | | * the byte boundary after the final block |
1019 | | */ |
1020 | 1.30k | while (inf->ctr < sizeof(inf->trailer)) { |
1021 | 1.16k | r = read_byte(inf, &t); |
1022 | 1.16k | if (r) |
1023 | 19 | return r; |
1024 | | |
1025 | 1.14k | inf->trailer[inf->ctr++] = t; |
1026 | 1.14k | } |
1027 | | |
1028 | 136 | if (inf->crc32 != ((uint32_t)inf->trailer[0] | |
1029 | 136 | ((uint32_t)inf->trailer[1] << 8) | |
1030 | 136 | ((uint32_t)inf->trailer[2] << 16) | |
1031 | 136 | ((uint32_t)inf->trailer[3] << 24))) { |
1032 | 66 | lwsl_notice("%s: gzip CRC32 mismatch\n", __func__); |
1033 | | |
1034 | 66 | return LWS_SRET_FATAL + 36; |
1035 | 66 | } |
1036 | | |
1037 | 70 | if ((uint32_t)inf->outpos_linear != |
1038 | 70 | ((uint32_t)inf->trailer[4] | |
1039 | 70 | ((uint32_t)inf->trailer[5] << 8) | |
1040 | 70 | ((uint32_t)inf->trailer[6] << 16) | |
1041 | 70 | ((uint32_t)inf->trailer[7] << 24))) { |
1042 | 69 | lwsl_notice("%s: gzip ISIZE mismatch\n", __func__); |
1043 | | |
1044 | 69 | return LWS_SRET_FATAL + 37; |
1045 | 69 | } |
1046 | | |
1047 | 1 | inf->state = UPNS_ID_BL_GB_END; |
1048 | 1 | done = 1; |
1049 | 1 | continue; |
1050 | | |
1051 | 0 | case UPNS_ID_BL_GB_END: |
1052 | 0 | if (inf->gzip) |
1053 | | /* |
1054 | | * The member is complete and checked. We |
1055 | | * don't support further members, so input |
1056 | | * after it is left unconsumed. |
1057 | | */ |
1058 | 0 | return LWS_SRET_OK; |
1059 | | |
1060 | | /* |
1061 | | * PNG's zlib stream: the IDAT may still hold the |
1062 | | * Adler-32 we don't check. Take it so upng moves on |
1063 | | * to the next chunk, and never read it as a block. |
1064 | | */ |
1065 | 0 | inf->bp = (inf->inlen - inf->inpos) << 3; |
1066 | |
|
1067 | 0 | return LWS_SRET_WANT_INPUT; |
1068 | 21.9M | } |
1069 | 21.9M | } |
1070 | | |
1071 | 1 | return LWS_SRET_OK; |
1072 | 1.33k | } |
1073 | | |
1074 | | struct inflator_ctx * |
1075 | | lws_upng_inflator_create(const uint8_t **outring, size_t *outringlen, |
1076 | | size_t **opl, size_t **cl) |
1077 | 1.33k | { |
1078 | 1.33k | inflator_ctx_t *inf = lws_zalloc(sizeof(*inf), __func__); |
1079 | 1.33k | unsigned int n; |
1080 | | |
1081 | 1.33k | if (!inf) { |
1082 | 0 | lwsl_notice("%s: OOM\n", __func__); |
1083 | |
|
1084 | 0 | return NULL; |
1085 | 0 | } |
1086 | | |
1087 | | /* 32KB gz sliding window */ |
1088 | 1.33k | inf->info_size = 32768; |
1089 | 1.33k | inf->bypl = 0; |
1090 | 1.33k | inf->outlen = inf->info_size; |
1091 | 1.33k | inf->outpos = 0; |
1092 | 1.33k | inf->state = UPNS_ID_BL_GB_GZIP_ID1; |
1093 | 1.33k | inf->gzip = 1; |
1094 | | |
1095 | | /* |
1096 | | * Our caller reads the ring directly, so it's one piece, reached |
1097 | | * through the window's unit table like the png's |
1098 | | */ |
1099 | 1.33k | inf->out_linear = (uint8_t *)lws_malloc(inf->info_size, __func__); |
1100 | 1.33k | if (!inf->out_linear) { |
1101 | 0 | lwsl_notice("%s: inf malloc %u OOM\n", |
1102 | 0 | __func__, (unsigned int)inf->info_size); |
1103 | |
|
1104 | 0 | lws_free(inf); |
1105 | |
|
1106 | 0 | return NULL; |
1107 | 0 | } |
1108 | | |
1109 | 44.0k | for (n = 0; n < inf->info_size >> LWS_UPNG_WIN_UNIT_SHIFT; n++) |
1110 | 42.7k | inf->out_u[n] = inf->out_linear + (n * LWS_UPNG_WIN_UNIT); |
1111 | | |
1112 | 1.33k | *outring = inf->out_linear; |
1113 | 1.33k | *outringlen = inf->info_size; |
1114 | 1.33k | *opl = &inf->outpos_linear; |
1115 | 1.33k | *cl = &inf->consumed_linear; |
1116 | | |
1117 | 1.33k | return inf; |
1118 | 1.33k | } |
1119 | | |
1120 | | lws_stateful_ret_t |
1121 | | lws_upng_inflate_data(struct inflator_ctx *inf, const void *buf, size_t len) |
1122 | 1.33k | { |
1123 | 1.33k | lws_stateful_ret_t r; |
1124 | | |
1125 | 1.33k | if (buf) { |
1126 | 1.33k | inf->in = buf; |
1127 | 1.33k | inf->inlen = len; |
1128 | 1.33k | inf->inpos = 0; |
1129 | 1.33k | inf->bp = 0; |
1130 | 1.33k | } |
1131 | | |
1132 | 1.33k | if (!inf->bypl) |
1133 | 1.33k | inf->bypl = 4095; |
1134 | | |
1135 | 1.33k | r = _lws_upng_inflate_data(inf); |
1136 | | |
1137 | 1.33k | if ((inf->bp >> 3) == inf->inlen) { |
1138 | 1.09k | inf->archive_pos += inf->inlen; |
1139 | 1.09k | inf->inlen = 0; |
1140 | 1.09k | inf->bp = 0; |
1141 | 1.09k | } |
1142 | | |
1143 | 1.33k | return r; |
1144 | 1.33k | } |
1145 | | |
1146 | | void |
1147 | | lws_upng_inflator_destroy(struct inflator_ctx **inf) |
1148 | 1.33k | { |
1149 | 1.33k | lws_free_set_NULL((*inf)->out_linear); |
1150 | | lws_free_set_NULL(*inf); |
1151 | 1.33k | } |
1152 | | |
1153 | | |