blob: 2b7a464f74f5899a5271ad6f5e71516a4561a1d8 [file] [log] [blame]
Mike Frysinger99596bd2011-04-08 12:23:30 +00001/* inflate.c -- zlib decompression
2 * Copyright (C) 1995-2005 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
Simon Glass653ac142025-01-26 11:43:24 -07005#include <asm/sections.h>
6
Mike Frysinger99596bd2011-04-08 12:23:30 +00007local void fixedtables OF((struct inflate_state FAR *state));
8local int updatewindow OF((z_streamp strm, unsigned out));
9
Simon Glass653ac142025-01-26 11:43:24 -070010__rcode int ZEXPORT inflateReset(z_streamp strm)
Mike Frysinger99596bd2011-04-08 12:23:30 +000011{
12 struct inflate_state FAR *state;
13
14 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
15 state = (struct inflate_state FAR *)strm->state;
16 strm->total_in = strm->total_out = state->total = 0;
17 strm->msg = Z_NULL;
18 strm->adler = 1; /* to support ill-conceived Java test suite */
19 state->mode = HEAD;
20 state->last = 0;
21 state->havedict = 0;
22 state->dmax = 32768U;
23 state->head = Z_NULL;
24 state->wsize = 0;
25 state->whave = 0;
Tom Rini9796bdd2024-07-05 09:44:54 -060026 state->wnext = 0;
Mike Frysinger99596bd2011-04-08 12:23:30 +000027 state->hold = 0;
28 state->bits = 0;
29 state->lencode = state->distcode = state->next = state->codes;
Stefan Roese80877fa2022-09-02 14:10:46 +020030 schedule();
Mike Frysinger99596bd2011-04-08 12:23:30 +000031 Tracev((stderr, "inflate: reset\n"));
32 return Z_OK;
33}
34
Simon Glass653ac142025-01-26 11:43:24 -070035__rcode int ZEXPORT inflateInit2_(z_streamp strm, int windowBits,
36 int stream_size)
Mike Frysinger99596bd2011-04-08 12:23:30 +000037{
38 struct inflate_state FAR *state;
39
Mike Frysinger99596bd2011-04-08 12:23:30 +000040 if (strm == Z_NULL) return Z_STREAM_ERROR;
41 strm->msg = Z_NULL; /* in case we return an error */
42 if (strm->zalloc == (alloc_func)0) {
43 strm->zalloc = zcalloc;
44 strm->opaque = (voidpf)0;
45 }
46 if (strm->zfree == (free_func)0) strm->zfree = zcfree;
47 state = (struct inflate_state FAR *)
48 ZALLOC(strm, 1, sizeof(struct inflate_state));
49 if (state == Z_NULL) return Z_MEM_ERROR;
50 Tracev((stderr, "inflate: allocated\n"));
51 strm->state = (struct internal_state FAR *)state;
52 if (windowBits < 0) {
53 state->wrap = 0;
54 windowBits = -windowBits;
55 }
56 else {
57 state->wrap = (windowBits >> 4) + 1;
58#ifdef GUNZIP
59 if (windowBits < 48) windowBits &= 15;
60#endif
61 }
62 if (windowBits < 8 || windowBits > 15) {
63 ZFREE(strm, state);
64 strm->state = Z_NULL;
65 return Z_STREAM_ERROR;
66 }
67 state->wbits = (unsigned)windowBits;
68 state->window = Z_NULL;
69 return inflateReset(strm);
70}
71
Simon Glass653ac142025-01-26 11:43:24 -070072__rcode int ZEXPORT inflateInit_(z_streamp strm, int stream_size)
Mike Frysinger99596bd2011-04-08 12:23:30 +000073{
Tom Rini9796bdd2024-07-05 09:44:54 -060074 return inflateInit2_(strm, DEF_WBITS, stream_size);
Mike Frysinger99596bd2011-04-08 12:23:30 +000075}
76
Simon Glass653ac142025-01-26 11:43:24 -070077__rcode local void fixedtables(struct inflate_state FAR *state)
Mike Frysinger99596bd2011-04-08 12:23:30 +000078{
79 state->lencode = lenfix;
80 state->lenbits = 9;
81 state->distcode = distfix;
82 state->distbits = 5;
83}
84
85/*
86 Update the window with the last wsize (normally 32K) bytes written before
87 returning. If window does not exist yet, create it. This is only called
88 when a window is already in use, or when output has been written during this
89 inflate call, but the end of the deflate stream has not been reached yet.
90 It is also called to create a window for dictionary data when a dictionary
91 is loaded.
92
93 Providing output buffers larger than 32K to inflate() should provide a speed
94 advantage, since only the last 32K of output is copied to the sliding window
95 upon return from inflate(), and since all distances after the first 32K of
96 output will fall in the output data, making match copies simpler and faster.
97 The advantage may be dependent on the size of the processor's data caches.
98 */
Simon Glass653ac142025-01-26 11:43:24 -070099__rcode local int updatewindow(z_streamp strm, unsigned int out)
Mike Frysinger99596bd2011-04-08 12:23:30 +0000100{
101 struct inflate_state FAR *state;
102 unsigned copy, dist;
103
104 state = (struct inflate_state FAR *)strm->state;
105
106 /* if it hasn't been done already, allocate space for the window */
107 if (state->window == Z_NULL) {
108 state->window = (unsigned char FAR *)
109 ZALLOC(strm, 1U << state->wbits,
110 sizeof(unsigned char));
111 if (state->window == Z_NULL) return 1;
112 }
113
114 /* if window not in use yet, initialize */
115 if (state->wsize == 0) {
116 state->wsize = 1U << state->wbits;
Tom Rini9796bdd2024-07-05 09:44:54 -0600117 state->wnext = 0;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000118 state->whave = 0;
119 }
120
121 /* copy state->wsize or less output bytes into the circular window */
122 copy = out - strm->avail_out;
123 if (copy >= state->wsize) {
124 zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
Tom Rini9796bdd2024-07-05 09:44:54 -0600125 state->wnext = 0;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000126 state->whave = state->wsize;
127 }
128 else {
Tom Rini9796bdd2024-07-05 09:44:54 -0600129 dist = state->wsize - state->wnext;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000130 if (dist > copy) dist = copy;
Tom Rini9796bdd2024-07-05 09:44:54 -0600131 zmemcpy(state->window + state->wnext, strm->next_out - copy, dist);
Mike Frysinger99596bd2011-04-08 12:23:30 +0000132 copy -= dist;
133 if (copy) {
134 zmemcpy(state->window, strm->next_out - copy, copy);
Tom Rini9796bdd2024-07-05 09:44:54 -0600135 state->wnext = copy;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000136 state->whave = state->wsize;
137 }
138 else {
Tom Rini9796bdd2024-07-05 09:44:54 -0600139 state->wnext += dist;
140 if (state->wnext == state->wsize) state->wnext = 0;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000141 if (state->whave < state->wsize) state->whave += dist;
142 }
143 }
144 return 0;
145}
146
147/* Macros for inflate(): */
148
149/* check function to use adler32() for zlib or crc32() for gzip */
150#ifdef GUNZIP
151# define UPDATE(check, buf, len) \
152 (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
153#else
154# define UPDATE(check, buf, len) adler32(check, buf, len)
155#endif
156
157/* check macros for header crc */
158#ifdef GUNZIP
159# define CRC2(check, word) \
160 do { \
161 hbuf[0] = (unsigned char)(word); \
162 hbuf[1] = (unsigned char)((word) >> 8); \
163 check = crc32(check, hbuf, 2); \
164 } while (0)
165
166# define CRC4(check, word) \
167 do { \
168 hbuf[0] = (unsigned char)(word); \
169 hbuf[1] = (unsigned char)((word) >> 8); \
170 hbuf[2] = (unsigned char)((word) >> 16); \
171 hbuf[3] = (unsigned char)((word) >> 24); \
172 check = crc32(check, hbuf, 4); \
173 } while (0)
174#endif
175
176/* Load registers with state in inflate() for speed */
177#define LOAD() \
178 do { \
179 put = strm->next_out; \
180 left = strm->avail_out; \
181 next = strm->next_in; \
182 have = strm->avail_in; \
183 hold = state->hold; \
184 bits = state->bits; \
185 } while (0)
186
187/* Restore state from registers in inflate() */
188#define RESTORE() \
189 do { \
190 strm->next_out = put; \
191 strm->avail_out = left; \
192 strm->next_in = next; \
193 strm->avail_in = have; \
194 state->hold = hold; \
195 state->bits = bits; \
196 } while (0)
197
198/* Clear the input bit accumulator */
199#define INITBITS() \
200 do { \
201 hold = 0; \
202 bits = 0; \
203 } while (0)
204
205/* Get a byte of input into the bit accumulator, or return from inflate()
206 if there is no input available. */
207#define PULLBYTE() \
208 do { \
209 if (have == 0) goto inf_leave; \
210 have--; \
211 hold += (unsigned long)(*next++) << bits; \
212 bits += 8; \
213 } while (0)
214
215/* Assure that there are at least n bits in the bit accumulator. If there is
216 not enough available input to do that, then return from inflate(). */
217#define NEEDBITS(n) \
218 do { \
219 while (bits < (unsigned)(n)) \
220 PULLBYTE(); \
221 } while (0)
222
223/* Return the low n bits of the bit accumulator (n < 16) */
224#define BITS(n) \
225 ((unsigned)hold & ((1U << (n)) - 1))
226
227/* Remove n bits from the bit accumulator */
228#define DROPBITS(n) \
229 do { \
230 hold >>= (n); \
231 bits -= (unsigned)(n); \
232 } while (0)
233
234/* Remove zero to seven bits as needed to go to a byte boundary */
235#define BYTEBITS() \
236 do { \
237 hold >>= bits & 7; \
238 bits -= bits & 7; \
239 } while (0)
240
241/* Reverse the bytes in a 32-bit value */
242#define REVERSE(q) \
243 ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
244 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
245
246/*
247 inflate() uses a state machine to process as much input data and generate as
248 much output data as possible before returning. The state machine is
249 structured roughly as follows:
250
251 for (;;) switch (state) {
252 ...
253 case STATEn:
254 if (not enough input data or output space to make progress)
255 return;
256 ... make progress ...
257 state = STATEm;
258 break;
259 ...
260 }
261
262 so when inflate() is called again, the same case is attempted again, and
263 if the appropriate resources are provided, the machine proceeds to the
264 next state. The NEEDBITS() macro is usually the way the state evaluates
265 whether it can proceed or should return. NEEDBITS() does the return if
266 the requested bits are not available. The typical use of the BITS macros
267 is:
268
269 NEEDBITS(n);
270 ... do something with BITS(n) ...
271 DROPBITS(n);
272
273 where NEEDBITS(n) either returns from inflate() if there isn't enough
274 input left to load n bits into the accumulator, or it continues. BITS(n)
275 gives the low n bits in the accumulator. When done, DROPBITS(n) drops
276 the low n bits off the accumulator. INITBITS() clears the accumulator
277 and sets the number of available bits to zero. BYTEBITS() discards just
278 enough bits to put the accumulator on a byte boundary. After BYTEBITS()
279 and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
280
281 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
282 if there is no input available. The decoding of variable length codes uses
283 PULLBYTE() directly in order to pull just enough bytes to decode the next
284 code, and no more.
285
286 Some states loop until they get enough input, making sure that enough
287 state information is maintained to continue the loop where it left off
288 if NEEDBITS() returns in the loop. For example, want, need, and keep
289 would all have to actually be part of the saved state in case NEEDBITS()
290 returns:
291
292 case STATEw:
293 while (want < need) {
294 NEEDBITS(n);
295 keep[want++] = BITS(n);
296 DROPBITS(n);
297 }
298 state = STATEx;
299 case STATEx:
300
301 As shown above, if the next state is also the next case, then the break
302 is omitted.
303
304 A state may also return if there is not enough output space available to
305 complete that state. Those states are copying stored data, writing a
306 literal byte, and copying a matching string.
307
308 When returning, a "goto inf_leave" is used to update the total counters,
309 update the check value, and determine whether any progress has been made
310 during that inflate() call in order to return the proper return code.
311 Progress is defined as a change in either strm->avail_in or strm->avail_out.
312 When there is a window, goto inf_leave will update the window with the last
313 output written. If a goto inf_leave occurs in the middle of decompression
314 and there is no window currently, goto inf_leave will create one and copy
315 output to the window for the next call of inflate().
316
317 In this implementation, the flush parameter of inflate() only affects the
318 return code (per zlib.h). inflate() always writes as much as possible to
319 strm->next_out, given the space available and the provided input--the effect
320 documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
321 the allocation of and copying into a sliding window until necessary, which
322 provides the effect documented in zlib.h for Z_FINISH when the entire input
323 stream available. So the only thing the flush parameter actually does is:
324 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
325 will return Z_BUF_ERROR if it has not reached the end of the stream.
326 */
Simon Glass653ac142025-01-26 11:43:24 -0700327__rcode int ZEXPORT inflate(z_streamp strm, int flush)
Mike Frysinger99596bd2011-04-08 12:23:30 +0000328{
329 struct inflate_state FAR *state;
330 unsigned char FAR *next; /* next input */
331 unsigned char FAR *put; /* next output */
332 unsigned have, left; /* available input and output */
333 unsigned long hold; /* bit buffer */
334 unsigned bits; /* bits in bit buffer */
335 unsigned in, out; /* save starting available input and output */
336 unsigned copy; /* number of stored or match bytes to copy */
337 unsigned char FAR *from; /* where to copy match bytes from */
338 code this; /* current decoding table entry */
339 code last; /* parent table entry */
340 unsigned len; /* length to copy for repeats, bits to drop */
341 int ret; /* return code */
342#ifdef GUNZIP
343 unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
344#endif
345 static const unsigned short order[19] = /* permutation of code lengths */
346 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
347
348 if (strm == Z_NULL || strm->state == Z_NULL ||
349 (strm->next_in == Z_NULL && strm->avail_in != 0))
350 return Z_STREAM_ERROR;
351
352 state = (struct inflate_state FAR *)strm->state;
353 if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
354 LOAD();
355 in = have;
356 out = left;
357 ret = Z_OK;
358 for (;;)
359 switch (state->mode) {
360 case HEAD:
361 if (state->wrap == 0) {
362 state->mode = TYPEDO;
363 break;
364 }
365 NEEDBITS(16);
366#ifdef GUNZIP
367 if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
368 state->check = crc32(0L, Z_NULL, 0);
369 CRC2(state->check, hold);
370 INITBITS();
371 state->mode = FLAGS;
372 break;
373 }
374 state->flags = 0; /* expect zlib header */
375 if (state->head != Z_NULL)
376 state->head->done = -1;
377 if (!(state->wrap & 1) || /* check if zlib header allowed */
378#else
379 if (
380#endif
381 ((BITS(8) << 8) + (hold >> 8)) % 31) {
382 strm->msg = (char *)"incorrect header check";
383 state->mode = BAD;
384 break;
385 }
386 if (BITS(4) != Z_DEFLATED) {
387 strm->msg = (char *)"unknown compression method";
388 state->mode = BAD;
389 break;
390 }
391 DROPBITS(4);
392 len = BITS(4) + 8;
393 if (len > state->wbits) {
394 strm->msg = (char *)"invalid window size";
395 state->mode = BAD;
396 break;
397 }
398 state->dmax = 1U << len;
399 Tracev((stderr, "inflate: zlib header ok\n"));
400 strm->adler = state->check = adler32(0L, Z_NULL, 0);
401 state->mode = hold & 0x200 ? DICTID : TYPE;
402 INITBITS();
403 break;
404#ifdef GUNZIP
405 case FLAGS:
406 NEEDBITS(16);
407 state->flags = (int)(hold);
408 if ((state->flags & 0xff) != Z_DEFLATED) {
409 strm->msg = (char *)"unknown compression method";
410 state->mode = BAD;
411 break;
412 }
413 if (state->flags & 0xe000) {
414 strm->msg = (char *)"unknown header flags set";
415 state->mode = BAD;
416 break;
417 }
418 if (state->head != Z_NULL)
419 state->head->text = (int)((hold >> 8) & 1);
420 if (state->flags & 0x0200) CRC2(state->check, hold);
421 INITBITS();
422 state->mode = TIME;
Andre Przywara48078d72025-03-27 15:32:57 +0000423 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000424 case TIME:
425 NEEDBITS(32);
426 if (state->head != Z_NULL)
427 state->head->time = hold;
428 if (state->flags & 0x0200) CRC4(state->check, hold);
429 INITBITS();
430 state->mode = OS;
Andre Przywara48078d72025-03-27 15:32:57 +0000431 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000432 case OS:
433 NEEDBITS(16);
434 if (state->head != Z_NULL) {
435 state->head->xflags = (int)(hold & 0xff);
436 state->head->os = (int)(hold >> 8);
437 }
438 if (state->flags & 0x0200) CRC2(state->check, hold);
439 INITBITS();
440 state->mode = EXLEN;
Andre Przywara48078d72025-03-27 15:32:57 +0000441 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000442 case EXLEN:
443 if (state->flags & 0x0400) {
444 NEEDBITS(16);
445 state->length = (unsigned)(hold);
446 if (state->head != Z_NULL)
447 state->head->extra_len = (unsigned)hold;
448 if (state->flags & 0x0200) CRC2(state->check, hold);
449 INITBITS();
450 }
451 else if (state->head != Z_NULL)
452 state->head->extra = Z_NULL;
453 state->mode = EXTRA;
Andre Przywara48078d72025-03-27 15:32:57 +0000454 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000455 case EXTRA:
456 if (state->flags & 0x0400) {
457 copy = state->length;
458 if (copy > have) copy = have;
459 if (copy) {
460 if (state->head != Z_NULL &&
Oleksandr Suvorov1b4014b2023-06-15 17:54:34 +0300461 state->head->extra != Z_NULL &&
462 (len = state->head->extra_len - state->length) <
463 state->head->extra_max) {
Mike Frysinger99596bd2011-04-08 12:23:30 +0000464 zmemcpy(state->head->extra + len, next,
465 len + copy > state->head->extra_max ?
466 state->head->extra_max - len : copy);
467 }
468 if (state->flags & 0x0200)
469 state->check = crc32(state->check, next, copy);
470 have -= copy;
471 next += copy;
472 state->length -= copy;
473 }
474 if (state->length) goto inf_leave;
475 }
476 state->length = 0;
477 state->mode = NAME;
Andre Przywara48078d72025-03-27 15:32:57 +0000478 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000479 case NAME:
480 if (state->flags & 0x0800) {
481 if (have == 0) goto inf_leave;
482 copy = 0;
483 do {
484 len = (unsigned)(next[copy++]);
485 if (state->head != Z_NULL &&
486 state->head->name != Z_NULL &&
487 state->length < state->head->name_max)
488 state->head->name[state->length++] = len;
489 } while (len && copy < have);
490 if (state->flags & 0x0200)
491 state->check = crc32(state->check, next, copy);
492 have -= copy;
493 next += copy;
494 if (len) goto inf_leave;
495 }
496 else if (state->head != Z_NULL)
497 state->head->name = Z_NULL;
498 state->length = 0;
499 state->mode = COMMENT;
Andre Przywara48078d72025-03-27 15:32:57 +0000500 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000501 case COMMENT:
502 if (state->flags & 0x1000) {
503 if (have == 0) goto inf_leave;
504 copy = 0;
505 do {
506 len = (unsigned)(next[copy++]);
507 if (state->head != Z_NULL &&
508 state->head->comment != Z_NULL &&
509 state->length < state->head->comm_max)
510 state->head->comment[state->length++] = len;
511 } while (len && copy < have);
512 if (state->flags & 0x0200)
513 state->check = crc32(state->check, next, copy);
514 have -= copy;
515 next += copy;
516 if (len) goto inf_leave;
517 }
518 else if (state->head != Z_NULL)
519 state->head->comment = Z_NULL;
520 state->mode = HCRC;
Andre Przywara48078d72025-03-27 15:32:57 +0000521 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000522 case HCRC:
523 if (state->flags & 0x0200) {
524 NEEDBITS(16);
525 if (hold != (state->check & 0xffff)) {
526 strm->msg = (char *)"header crc mismatch";
527 state->mode = BAD;
528 break;
529 }
530 INITBITS();
531 }
532 if (state->head != Z_NULL) {
533 state->head->hcrc = (int)((state->flags >> 9) & 1);
534 state->head->done = 1;
535 }
536 strm->adler = state->check = crc32(0L, Z_NULL, 0);
537 state->mode = TYPE;
538 break;
539#endif
540 case DICTID:
541 NEEDBITS(32);
542 strm->adler = state->check = REVERSE(hold);
543 INITBITS();
544 state->mode = DICT;
Andre Przywara48078d72025-03-27 15:32:57 +0000545 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000546 case DICT:
547 if (state->havedict == 0) {
548 RESTORE();
549 return Z_NEED_DICT;
550 }
551 strm->adler = state->check = adler32(0L, Z_NULL, 0);
552 state->mode = TYPE;
Andre Przywara48078d72025-03-27 15:32:57 +0000553 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000554 case TYPE:
Stefan Roese80877fa2022-09-02 14:10:46 +0200555 schedule();
Mike Frysinger99596bd2011-04-08 12:23:30 +0000556 if (flush == Z_BLOCK) goto inf_leave;
Andre Przywara48078d72025-03-27 15:32:57 +0000557 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000558 case TYPEDO:
559 if (state->last) {
560 BYTEBITS();
561 state->mode = CHECK;
562 break;
563 }
564 NEEDBITS(3);
565 state->last = BITS(1);
566 DROPBITS(1);
567 switch (BITS(2)) {
568 case 0: /* stored block */
569 Tracev((stderr, "inflate: stored block%s\n",
570 state->last ? " (last)" : ""));
571 state->mode = STORED;
572 break;
573 case 1: /* fixed block */
574 fixedtables(state);
575 Tracev((stderr, "inflate: fixed codes block%s\n",
576 state->last ? " (last)" : ""));
577 state->mode = LEN; /* decode codes */
578 break;
579 case 2: /* dynamic block */
580 Tracev((stderr, "inflate: dynamic codes block%s\n",
581 state->last ? " (last)" : ""));
582 state->mode = TABLE;
583 break;
584 case 3:
585 strm->msg = (char *)"invalid block type";
586 state->mode = BAD;
587 }
588 DROPBITS(2);
589 break;
590 case STORED:
591 BYTEBITS(); /* go to byte boundary */
592 NEEDBITS(32);
593 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
594 strm->msg = (char *)"invalid stored block lengths";
595 state->mode = BAD;
596 break;
597 }
598 state->length = (unsigned)hold & 0xffff;
599 Tracev((stderr, "inflate: stored length %u\n",
600 state->length));
601 INITBITS();
602 state->mode = COPY;
Andre Przywara48078d72025-03-27 15:32:57 +0000603 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000604 case COPY:
605 copy = state->length;
606 if (copy) {
607 if (copy > have) copy = have;
608 if (copy > left) copy = left;
609 if (copy == 0) goto inf_leave;
610 zmemcpy(put, next, copy);
611 have -= copy;
612 next += copy;
613 left -= copy;
614 put += copy;
615 state->length -= copy;
616 break;
617 }
618 Tracev((stderr, "inflate: stored end\n"));
619 state->mode = TYPE;
620 break;
621 case TABLE:
622 NEEDBITS(14);
623 state->nlen = BITS(5) + 257;
624 DROPBITS(5);
625 state->ndist = BITS(5) + 1;
626 DROPBITS(5);
627 state->ncode = BITS(4) + 4;
628 DROPBITS(4);
629#ifndef PKZIP_BUG_WORKAROUND
630 if (state->nlen > 286 || state->ndist > 30) {
631 strm->msg = (char *)"too many length or distance symbols";
632 state->mode = BAD;
633 break;
634 }
635#endif
636 Tracev((stderr, "inflate: table sizes ok\n"));
637 state->have = 0;
638 state->mode = LENLENS;
Andre Przywara48078d72025-03-27 15:32:57 +0000639 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000640 case LENLENS:
641 while (state->have < state->ncode) {
642 NEEDBITS(3);
643 state->lens[order[state->have++]] = (unsigned short)BITS(3);
644 DROPBITS(3);
645 }
646 while (state->have < 19)
647 state->lens[order[state->have++]] = 0;
648 state->next = state->codes;
649 state->lencode = (code const FAR *)(state->next);
650 state->lenbits = 7;
651 ret = inflate_table(CODES, state->lens, 19, &(state->next),
652 &(state->lenbits), state->work);
653 if (ret) {
654 strm->msg = (char *)"invalid code lengths set";
655 state->mode = BAD;
656 break;
657 }
658 Tracev((stderr, "inflate: code lengths ok\n"));
659 state->have = 0;
660 state->mode = CODELENS;
Andre Przywara48078d72025-03-27 15:32:57 +0000661 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000662 case CODELENS:
663 while (state->have < state->nlen + state->ndist) {
664 for (;;) {
665 this = state->lencode[BITS(state->lenbits)];
666 if ((unsigned)(this.bits) <= bits) break;
667 PULLBYTE();
668 }
669 if (this.val < 16) {
670 NEEDBITS(this.bits);
671 DROPBITS(this.bits);
672 state->lens[state->have++] = this.val;
673 }
674 else {
675 if (this.val == 16) {
676 NEEDBITS(this.bits + 2);
677 DROPBITS(this.bits);
678 if (state->have == 0) {
679 strm->msg = (char *)"invalid bit length repeat";
680 state->mode = BAD;
681 break;
682 }
683 len = state->lens[state->have - 1];
684 copy = 3 + BITS(2);
685 DROPBITS(2);
686 }
687 else if (this.val == 17) {
688 NEEDBITS(this.bits + 3);
689 DROPBITS(this.bits);
690 len = 0;
691 copy = 3 + BITS(3);
692 DROPBITS(3);
693 }
694 else {
695 NEEDBITS(this.bits + 7);
696 DROPBITS(this.bits);
697 len = 0;
698 copy = 11 + BITS(7);
699 DROPBITS(7);
700 }
701 if (state->have + copy > state->nlen + state->ndist) {
702 strm->msg = (char *)"invalid bit length repeat";
703 state->mode = BAD;
704 break;
705 }
706 while (copy--)
707 state->lens[state->have++] = (unsigned short)len;
708 }
709 }
710
711 /* handle error breaks in while */
712 if (state->mode == BAD) break;
713
714 /* build code tables */
715 state->next = state->codes;
716 state->lencode = (code const FAR *)(state->next);
717 state->lenbits = 9;
718 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
719 &(state->lenbits), state->work);
720 if (ret) {
721 strm->msg = (char *)"invalid literal/lengths set";
722 state->mode = BAD;
723 break;
724 }
725 state->distcode = (code const FAR *)(state->next);
726 state->distbits = 6;
727 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
728 &(state->next), &(state->distbits), state->work);
729 if (ret) {
730 strm->msg = (char *)"invalid distances set";
731 state->mode = BAD;
732 break;
733 }
734 Tracev((stderr, "inflate: codes ok\n"));
735 state->mode = LEN;
Andre Przywara48078d72025-03-27 15:32:57 +0000736 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000737 case LEN:
Stefan Roese80877fa2022-09-02 14:10:46 +0200738 schedule();
Mike Frysinger99596bd2011-04-08 12:23:30 +0000739 if (have >= 6 && left >= 258) {
740 RESTORE();
741 inflate_fast(strm, out);
742 LOAD();
743 break;
744 }
745 for (;;) {
746 this = state->lencode[BITS(state->lenbits)];
747 if ((unsigned)(this.bits) <= bits) break;
748 PULLBYTE();
749 }
750 if (this.op && (this.op & 0xf0) == 0) {
751 last = this;
752 for (;;) {
753 this = state->lencode[last.val +
754 (BITS(last.bits + last.op) >> last.bits)];
755 if ((unsigned)(last.bits + this.bits) <= bits) break;
756 PULLBYTE();
757 }
758 DROPBITS(last.bits);
759 }
760 DROPBITS(this.bits);
761 state->length = (unsigned)this.val;
762 if ((int)(this.op) == 0) {
763 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
764 "inflate: literal '%c'\n" :
765 "inflate: literal 0x%02x\n", this.val));
766 state->mode = LIT;
767 break;
768 }
769 if (this.op & 32) {
770 Tracevv((stderr, "inflate: end of block\n"));
771 state->mode = TYPE;
772 break;
773 }
774 if (this.op & 64) {
775 strm->msg = (char *)"invalid literal/length code";
776 state->mode = BAD;
777 break;
778 }
779 state->extra = (unsigned)(this.op) & 15;
780 state->mode = LENEXT;
Andre Przywara48078d72025-03-27 15:32:57 +0000781 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000782 case LENEXT:
783 if (state->extra) {
784 NEEDBITS(state->extra);
785 state->length += BITS(state->extra);
786 DROPBITS(state->extra);
787 }
788 Tracevv((stderr, "inflate: length %u\n", state->length));
789 state->mode = DIST;
Andre Przywara48078d72025-03-27 15:32:57 +0000790 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000791 case DIST:
792 for (;;) {
793 this = state->distcode[BITS(state->distbits)];
794 if ((unsigned)(this.bits) <= bits) break;
795 PULLBYTE();
796 }
797 if ((this.op & 0xf0) == 0) {
798 last = this;
799 for (;;) {
800 this = state->distcode[last.val +
801 (BITS(last.bits + last.op) >> last.bits)];
802 if ((unsigned)(last.bits + this.bits) <= bits) break;
803 PULLBYTE();
804 }
805 DROPBITS(last.bits);
806 }
807 DROPBITS(this.bits);
808 if (this.op & 64) {
809 strm->msg = (char *)"invalid distance code";
810 state->mode = BAD;
811 break;
812 }
813 state->offset = (unsigned)this.val;
814 state->extra = (unsigned)(this.op) & 15;
815 state->mode = DISTEXT;
Andre Przywara48078d72025-03-27 15:32:57 +0000816 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000817 case DISTEXT:
818 if (state->extra) {
819 NEEDBITS(state->extra);
820 state->offset += BITS(state->extra);
821 DROPBITS(state->extra);
822 }
823#ifdef INFLATE_STRICT
824 if (state->offset > state->dmax) {
825 strm->msg = (char *)"invalid distance too far back";
826 state->mode = BAD;
827 break;
828 }
829#endif
830 if (state->offset > state->whave + out - left) {
831 strm->msg = (char *)"invalid distance too far back";
832 state->mode = BAD;
833 break;
834 }
835 Tracevv((stderr, "inflate: distance %u\n", state->offset));
836 state->mode = MATCH;
Andre Przywara48078d72025-03-27 15:32:57 +0000837 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000838 case MATCH:
839 if (left == 0) goto inf_leave;
840 copy = out - left;
841 if (state->offset > copy) { /* copy from window */
842 copy = state->offset - copy;
Tom Rini9796bdd2024-07-05 09:44:54 -0600843 if (copy > state->wnext) {
844 copy -= state->wnext;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000845 from = state->window + (state->wsize - copy);
846 }
847 else
Tom Rini9796bdd2024-07-05 09:44:54 -0600848 from = state->window + (state->wnext - copy);
Mike Frysinger99596bd2011-04-08 12:23:30 +0000849 if (copy > state->length) copy = state->length;
850 }
851 else { /* copy from output */
852 from = put - state->offset;
853 copy = state->length;
854 }
855 if (copy > left) copy = left;
856 left -= copy;
857 state->length -= copy;
858 do {
859 *put++ = *from++;
860 } while (--copy);
861 if (state->length == 0) state->mode = LEN;
862 break;
863 case LIT:
864 if (left == 0) goto inf_leave;
865 *put++ = (unsigned char)(state->length);
866 left--;
867 state->mode = LEN;
868 break;
869 case CHECK:
870 if (state->wrap) {
871 NEEDBITS(32);
872 out -= left;
873 strm->total_out += out;
874 state->total += out;
875 if (out)
876 strm->adler = state->check =
877 UPDATE(state->check, put - out, out);
878 out = left;
879 if ((
880#ifdef GUNZIP
881 state->flags ? hold :
882#endif
883 REVERSE(hold)) != state->check) {
884 strm->msg = (char *)"incorrect data check";
885 state->mode = BAD;
886 break;
887 }
888 INITBITS();
889 Tracev((stderr, "inflate: check matches trailer\n"));
890 }
891#ifdef GUNZIP
892 state->mode = LENGTH;
Andre Przywara48078d72025-03-27 15:32:57 +0000893 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000894 case LENGTH:
895 if (state->wrap && state->flags) {
896 NEEDBITS(32);
897 if (hold != (state->total & 0xffffffffUL)) {
898 strm->msg = (char *)"incorrect length check";
899 state->mode = BAD;
900 break;
901 }
902 INITBITS();
903 Tracev((stderr, "inflate: length matches trailer\n"));
904 }
905#endif
906 state->mode = DONE;
Andre Przywara48078d72025-03-27 15:32:57 +0000907 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000908 case DONE:
909 ret = Z_STREAM_END;
910 goto inf_leave;
911 case BAD:
912 ret = Z_DATA_ERROR;
913 goto inf_leave;
914 case MEM:
915 return Z_MEM_ERROR;
916 case SYNC:
Andre Przywara48078d72025-03-27 15:32:57 +0000917 fallthrough;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000918 default:
919 return Z_STREAM_ERROR;
920 }
921
922 /*
923 Return from inflate(), updating the total counts and the check value.
924 If there was no progress during the inflate() call, return a buffer
925 error. Call updatewindow() to create and/or update the window state.
926 Note: a memory error from inflate() is non-recoverable.
927 */
928 inf_leave:
929 RESTORE();
930 if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
931 if (updatewindow(strm, out)) {
932 state->mode = MEM;
933 return Z_MEM_ERROR;
934 }
935 in -= strm->avail_in;
936 out -= strm->avail_out;
937 strm->total_in += in;
938 strm->total_out += out;
939 state->total += out;
940 if (state->wrap && out)
941 strm->adler = state->check =
942 UPDATE(state->check, strm->next_out - out, out);
943 strm->data_type = state->bits + (state->last ? 64 : 0) +
944 (state->mode == TYPE ? 128 : 0);
945 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
946 ret = Z_BUF_ERROR;
947 return ret;
948}
949
Simon Glass653ac142025-01-26 11:43:24 -0700950__rcode int ZEXPORT inflateEnd(z_streamp strm)
Mike Frysinger99596bd2011-04-08 12:23:30 +0000951{
952 struct inflate_state FAR *state;
953 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
954 return Z_STREAM_ERROR;
955 state = (struct inflate_state FAR *)strm->state;
956 if (state->window != Z_NULL) {
Stefan Roese80877fa2022-09-02 14:10:46 +0200957 schedule();
Mike Frysinger99596bd2011-04-08 12:23:30 +0000958 ZFREE(strm, state->window);
959 }
960 ZFREE(strm, strm->state);
961 strm->state = Z_NULL;
962 Tracev((stderr, "inflate: end\n"));
963 return Z_OK;
964}