blob: b4c72cc2c5c745af0cf6164a5188942038716862 [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;
423 case TIME:
424 NEEDBITS(32);
425 if (state->head != Z_NULL)
426 state->head->time = hold;
427 if (state->flags & 0x0200) CRC4(state->check, hold);
428 INITBITS();
429 state->mode = OS;
430 case OS:
431 NEEDBITS(16);
432 if (state->head != Z_NULL) {
433 state->head->xflags = (int)(hold & 0xff);
434 state->head->os = (int)(hold >> 8);
435 }
436 if (state->flags & 0x0200) CRC2(state->check, hold);
437 INITBITS();
438 state->mode = EXLEN;
439 case EXLEN:
440 if (state->flags & 0x0400) {
441 NEEDBITS(16);
442 state->length = (unsigned)(hold);
443 if (state->head != Z_NULL)
444 state->head->extra_len = (unsigned)hold;
445 if (state->flags & 0x0200) CRC2(state->check, hold);
446 INITBITS();
447 }
448 else if (state->head != Z_NULL)
449 state->head->extra = Z_NULL;
450 state->mode = EXTRA;
451 case EXTRA:
452 if (state->flags & 0x0400) {
453 copy = state->length;
454 if (copy > have) copy = have;
455 if (copy) {
456 if (state->head != Z_NULL &&
Oleksandr Suvorov1b4014b2023-06-15 17:54:34 +0300457 state->head->extra != Z_NULL &&
458 (len = state->head->extra_len - state->length) <
459 state->head->extra_max) {
Mike Frysinger99596bd2011-04-08 12:23:30 +0000460 zmemcpy(state->head->extra + len, next,
461 len + copy > state->head->extra_max ?
462 state->head->extra_max - len : copy);
463 }
464 if (state->flags & 0x0200)
465 state->check = crc32(state->check, next, copy);
466 have -= copy;
467 next += copy;
468 state->length -= copy;
469 }
470 if (state->length) goto inf_leave;
471 }
472 state->length = 0;
473 state->mode = NAME;
474 case NAME:
475 if (state->flags & 0x0800) {
476 if (have == 0) goto inf_leave;
477 copy = 0;
478 do {
479 len = (unsigned)(next[copy++]);
480 if (state->head != Z_NULL &&
481 state->head->name != Z_NULL &&
482 state->length < state->head->name_max)
483 state->head->name[state->length++] = len;
484 } while (len && copy < have);
485 if (state->flags & 0x0200)
486 state->check = crc32(state->check, next, copy);
487 have -= copy;
488 next += copy;
489 if (len) goto inf_leave;
490 }
491 else if (state->head != Z_NULL)
492 state->head->name = Z_NULL;
493 state->length = 0;
494 state->mode = COMMENT;
495 case COMMENT:
496 if (state->flags & 0x1000) {
497 if (have == 0) goto inf_leave;
498 copy = 0;
499 do {
500 len = (unsigned)(next[copy++]);
501 if (state->head != Z_NULL &&
502 state->head->comment != Z_NULL &&
503 state->length < state->head->comm_max)
504 state->head->comment[state->length++] = len;
505 } while (len && copy < have);
506 if (state->flags & 0x0200)
507 state->check = crc32(state->check, next, copy);
508 have -= copy;
509 next += copy;
510 if (len) goto inf_leave;
511 }
512 else if (state->head != Z_NULL)
513 state->head->comment = Z_NULL;
514 state->mode = HCRC;
515 case HCRC:
516 if (state->flags & 0x0200) {
517 NEEDBITS(16);
518 if (hold != (state->check & 0xffff)) {
519 strm->msg = (char *)"header crc mismatch";
520 state->mode = BAD;
521 break;
522 }
523 INITBITS();
524 }
525 if (state->head != Z_NULL) {
526 state->head->hcrc = (int)((state->flags >> 9) & 1);
527 state->head->done = 1;
528 }
529 strm->adler = state->check = crc32(0L, Z_NULL, 0);
530 state->mode = TYPE;
531 break;
532#endif
533 case DICTID:
534 NEEDBITS(32);
535 strm->adler = state->check = REVERSE(hold);
536 INITBITS();
537 state->mode = DICT;
538 case DICT:
539 if (state->havedict == 0) {
540 RESTORE();
541 return Z_NEED_DICT;
542 }
543 strm->adler = state->check = adler32(0L, Z_NULL, 0);
544 state->mode = TYPE;
545 case TYPE:
Stefan Roese80877fa2022-09-02 14:10:46 +0200546 schedule();
Mike Frysinger99596bd2011-04-08 12:23:30 +0000547 if (flush == Z_BLOCK) goto inf_leave;
548 case TYPEDO:
549 if (state->last) {
550 BYTEBITS();
551 state->mode = CHECK;
552 break;
553 }
554 NEEDBITS(3);
555 state->last = BITS(1);
556 DROPBITS(1);
557 switch (BITS(2)) {
558 case 0: /* stored block */
559 Tracev((stderr, "inflate: stored block%s\n",
560 state->last ? " (last)" : ""));
561 state->mode = STORED;
562 break;
563 case 1: /* fixed block */
564 fixedtables(state);
565 Tracev((stderr, "inflate: fixed codes block%s\n",
566 state->last ? " (last)" : ""));
567 state->mode = LEN; /* decode codes */
568 break;
569 case 2: /* dynamic block */
570 Tracev((stderr, "inflate: dynamic codes block%s\n",
571 state->last ? " (last)" : ""));
572 state->mode = TABLE;
573 break;
574 case 3:
575 strm->msg = (char *)"invalid block type";
576 state->mode = BAD;
577 }
578 DROPBITS(2);
579 break;
580 case STORED:
581 BYTEBITS(); /* go to byte boundary */
582 NEEDBITS(32);
583 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
584 strm->msg = (char *)"invalid stored block lengths";
585 state->mode = BAD;
586 break;
587 }
588 state->length = (unsigned)hold & 0xffff;
589 Tracev((stderr, "inflate: stored length %u\n",
590 state->length));
591 INITBITS();
592 state->mode = COPY;
593 case COPY:
594 copy = state->length;
595 if (copy) {
596 if (copy > have) copy = have;
597 if (copy > left) copy = left;
598 if (copy == 0) goto inf_leave;
599 zmemcpy(put, next, copy);
600 have -= copy;
601 next += copy;
602 left -= copy;
603 put += copy;
604 state->length -= copy;
605 break;
606 }
607 Tracev((stderr, "inflate: stored end\n"));
608 state->mode = TYPE;
609 break;
610 case TABLE:
611 NEEDBITS(14);
612 state->nlen = BITS(5) + 257;
613 DROPBITS(5);
614 state->ndist = BITS(5) + 1;
615 DROPBITS(5);
616 state->ncode = BITS(4) + 4;
617 DROPBITS(4);
618#ifndef PKZIP_BUG_WORKAROUND
619 if (state->nlen > 286 || state->ndist > 30) {
620 strm->msg = (char *)"too many length or distance symbols";
621 state->mode = BAD;
622 break;
623 }
624#endif
625 Tracev((stderr, "inflate: table sizes ok\n"));
626 state->have = 0;
627 state->mode = LENLENS;
628 case LENLENS:
629 while (state->have < state->ncode) {
630 NEEDBITS(3);
631 state->lens[order[state->have++]] = (unsigned short)BITS(3);
632 DROPBITS(3);
633 }
634 while (state->have < 19)
635 state->lens[order[state->have++]] = 0;
636 state->next = state->codes;
637 state->lencode = (code const FAR *)(state->next);
638 state->lenbits = 7;
639 ret = inflate_table(CODES, state->lens, 19, &(state->next),
640 &(state->lenbits), state->work);
641 if (ret) {
642 strm->msg = (char *)"invalid code lengths set";
643 state->mode = BAD;
644 break;
645 }
646 Tracev((stderr, "inflate: code lengths ok\n"));
647 state->have = 0;
648 state->mode = CODELENS;
649 case CODELENS:
650 while (state->have < state->nlen + state->ndist) {
651 for (;;) {
652 this = state->lencode[BITS(state->lenbits)];
653 if ((unsigned)(this.bits) <= bits) break;
654 PULLBYTE();
655 }
656 if (this.val < 16) {
657 NEEDBITS(this.bits);
658 DROPBITS(this.bits);
659 state->lens[state->have++] = this.val;
660 }
661 else {
662 if (this.val == 16) {
663 NEEDBITS(this.bits + 2);
664 DROPBITS(this.bits);
665 if (state->have == 0) {
666 strm->msg = (char *)"invalid bit length repeat";
667 state->mode = BAD;
668 break;
669 }
670 len = state->lens[state->have - 1];
671 copy = 3 + BITS(2);
672 DROPBITS(2);
673 }
674 else if (this.val == 17) {
675 NEEDBITS(this.bits + 3);
676 DROPBITS(this.bits);
677 len = 0;
678 copy = 3 + BITS(3);
679 DROPBITS(3);
680 }
681 else {
682 NEEDBITS(this.bits + 7);
683 DROPBITS(this.bits);
684 len = 0;
685 copy = 11 + BITS(7);
686 DROPBITS(7);
687 }
688 if (state->have + copy > state->nlen + state->ndist) {
689 strm->msg = (char *)"invalid bit length repeat";
690 state->mode = BAD;
691 break;
692 }
693 while (copy--)
694 state->lens[state->have++] = (unsigned short)len;
695 }
696 }
697
698 /* handle error breaks in while */
699 if (state->mode == BAD) break;
700
701 /* build code tables */
702 state->next = state->codes;
703 state->lencode = (code const FAR *)(state->next);
704 state->lenbits = 9;
705 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
706 &(state->lenbits), state->work);
707 if (ret) {
708 strm->msg = (char *)"invalid literal/lengths set";
709 state->mode = BAD;
710 break;
711 }
712 state->distcode = (code const FAR *)(state->next);
713 state->distbits = 6;
714 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
715 &(state->next), &(state->distbits), state->work);
716 if (ret) {
717 strm->msg = (char *)"invalid distances set";
718 state->mode = BAD;
719 break;
720 }
721 Tracev((stderr, "inflate: codes ok\n"));
722 state->mode = LEN;
723 case LEN:
Stefan Roese80877fa2022-09-02 14:10:46 +0200724 schedule();
Mike Frysinger99596bd2011-04-08 12:23:30 +0000725 if (have >= 6 && left >= 258) {
726 RESTORE();
727 inflate_fast(strm, out);
728 LOAD();
729 break;
730 }
731 for (;;) {
732 this = state->lencode[BITS(state->lenbits)];
733 if ((unsigned)(this.bits) <= bits) break;
734 PULLBYTE();
735 }
736 if (this.op && (this.op & 0xf0) == 0) {
737 last = this;
738 for (;;) {
739 this = state->lencode[last.val +
740 (BITS(last.bits + last.op) >> last.bits)];
741 if ((unsigned)(last.bits + this.bits) <= bits) break;
742 PULLBYTE();
743 }
744 DROPBITS(last.bits);
745 }
746 DROPBITS(this.bits);
747 state->length = (unsigned)this.val;
748 if ((int)(this.op) == 0) {
749 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
750 "inflate: literal '%c'\n" :
751 "inflate: literal 0x%02x\n", this.val));
752 state->mode = LIT;
753 break;
754 }
755 if (this.op & 32) {
756 Tracevv((stderr, "inflate: end of block\n"));
757 state->mode = TYPE;
758 break;
759 }
760 if (this.op & 64) {
761 strm->msg = (char *)"invalid literal/length code";
762 state->mode = BAD;
763 break;
764 }
765 state->extra = (unsigned)(this.op) & 15;
766 state->mode = LENEXT;
767 case LENEXT:
768 if (state->extra) {
769 NEEDBITS(state->extra);
770 state->length += BITS(state->extra);
771 DROPBITS(state->extra);
772 }
773 Tracevv((stderr, "inflate: length %u\n", state->length));
774 state->mode = DIST;
775 case DIST:
776 for (;;) {
777 this = state->distcode[BITS(state->distbits)];
778 if ((unsigned)(this.bits) <= bits) break;
779 PULLBYTE();
780 }
781 if ((this.op & 0xf0) == 0) {
782 last = this;
783 for (;;) {
784 this = state->distcode[last.val +
785 (BITS(last.bits + last.op) >> last.bits)];
786 if ((unsigned)(last.bits + this.bits) <= bits) break;
787 PULLBYTE();
788 }
789 DROPBITS(last.bits);
790 }
791 DROPBITS(this.bits);
792 if (this.op & 64) {
793 strm->msg = (char *)"invalid distance code";
794 state->mode = BAD;
795 break;
796 }
797 state->offset = (unsigned)this.val;
798 state->extra = (unsigned)(this.op) & 15;
799 state->mode = DISTEXT;
800 case DISTEXT:
801 if (state->extra) {
802 NEEDBITS(state->extra);
803 state->offset += BITS(state->extra);
804 DROPBITS(state->extra);
805 }
806#ifdef INFLATE_STRICT
807 if (state->offset > state->dmax) {
808 strm->msg = (char *)"invalid distance too far back";
809 state->mode = BAD;
810 break;
811 }
812#endif
813 if (state->offset > state->whave + out - left) {
814 strm->msg = (char *)"invalid distance too far back";
815 state->mode = BAD;
816 break;
817 }
818 Tracevv((stderr, "inflate: distance %u\n", state->offset));
819 state->mode = MATCH;
820 case MATCH:
821 if (left == 0) goto inf_leave;
822 copy = out - left;
823 if (state->offset > copy) { /* copy from window */
824 copy = state->offset - copy;
Tom Rini9796bdd2024-07-05 09:44:54 -0600825 if (copy > state->wnext) {
826 copy -= state->wnext;
Mike Frysinger99596bd2011-04-08 12:23:30 +0000827 from = state->window + (state->wsize - copy);
828 }
829 else
Tom Rini9796bdd2024-07-05 09:44:54 -0600830 from = state->window + (state->wnext - copy);
Mike Frysinger99596bd2011-04-08 12:23:30 +0000831 if (copy > state->length) copy = state->length;
832 }
833 else { /* copy from output */
834 from = put - state->offset;
835 copy = state->length;
836 }
837 if (copy > left) copy = left;
838 left -= copy;
839 state->length -= copy;
840 do {
841 *put++ = *from++;
842 } while (--copy);
843 if (state->length == 0) state->mode = LEN;
844 break;
845 case LIT:
846 if (left == 0) goto inf_leave;
847 *put++ = (unsigned char)(state->length);
848 left--;
849 state->mode = LEN;
850 break;
851 case CHECK:
852 if (state->wrap) {
853 NEEDBITS(32);
854 out -= left;
855 strm->total_out += out;
856 state->total += out;
857 if (out)
858 strm->adler = state->check =
859 UPDATE(state->check, put - out, out);
860 out = left;
861 if ((
862#ifdef GUNZIP
863 state->flags ? hold :
864#endif
865 REVERSE(hold)) != state->check) {
866 strm->msg = (char *)"incorrect data check";
867 state->mode = BAD;
868 break;
869 }
870 INITBITS();
871 Tracev((stderr, "inflate: check matches trailer\n"));
872 }
873#ifdef GUNZIP
874 state->mode = LENGTH;
875 case LENGTH:
876 if (state->wrap && state->flags) {
877 NEEDBITS(32);
878 if (hold != (state->total & 0xffffffffUL)) {
879 strm->msg = (char *)"incorrect length check";
880 state->mode = BAD;
881 break;
882 }
883 INITBITS();
884 Tracev((stderr, "inflate: length matches trailer\n"));
885 }
886#endif
887 state->mode = DONE;
888 case DONE:
889 ret = Z_STREAM_END;
890 goto inf_leave;
891 case BAD:
892 ret = Z_DATA_ERROR;
893 goto inf_leave;
894 case MEM:
895 return Z_MEM_ERROR;
896 case SYNC:
897 default:
898 return Z_STREAM_ERROR;
899 }
900
901 /*
902 Return from inflate(), updating the total counts and the check value.
903 If there was no progress during the inflate() call, return a buffer
904 error. Call updatewindow() to create and/or update the window state.
905 Note: a memory error from inflate() is non-recoverable.
906 */
907 inf_leave:
908 RESTORE();
909 if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
910 if (updatewindow(strm, out)) {
911 state->mode = MEM;
912 return Z_MEM_ERROR;
913 }
914 in -= strm->avail_in;
915 out -= strm->avail_out;
916 strm->total_in += in;
917 strm->total_out += out;
918 state->total += out;
919 if (state->wrap && out)
920 strm->adler = state->check =
921 UPDATE(state->check, strm->next_out - out, out);
922 strm->data_type = state->bits + (state->last ? 64 : 0) +
923 (state->mode == TYPE ? 128 : 0);
924 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
925 ret = Z_BUF_ERROR;
926 return ret;
927}
928
Simon Glass653ac142025-01-26 11:43:24 -0700929__rcode int ZEXPORT inflateEnd(z_streamp strm)
Mike Frysinger99596bd2011-04-08 12:23:30 +0000930{
931 struct inflate_state FAR *state;
932 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
933 return Z_STREAM_ERROR;
934 state = (struct inflate_state FAR *)strm->state;
935 if (state->window != Z_NULL) {
Stefan Roese80877fa2022-09-02 14:10:46 +0200936 schedule();
Mike Frysinger99596bd2011-04-08 12:23:30 +0000937 ZFREE(strm, state->window);
938 }
939 ZFREE(strm, strm->state);
940 strm->state = Z_NULL;
941 Tracev((stderr, "inflate: end\n"));
942 return Z_OK;
943}