blob: bb223af9c573809d76e15d4a2c92ca9eb8ccd2c4 [file] [log] [blame]
Tim Van Pattenf2981ed2020-07-27 19:25:23 +00001/* inflate.c -- zlib decompression
2 * Copyright (C) 1995-2016 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/*
7 * Change history:
8 *
9 * 1.2.beta0 24 Nov 2002
10 * - First version -- complete rewrite of inflate to simplify code, avoid
11 * creation of window when not needed, minimize use of window when it is
12 * needed, make inffast.c even faster, implement gzip decoding, and to
13 * improve code readability and style over the previous zlib inflate code
14 *
15 * 1.2.beta1 25 Nov 2002
16 * - Use pointers for available input and output checking in inffast.c
17 * - Remove input and output counters in inffast.c
18 * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
19 * - Remove unnecessary second byte pull from length extra in inffast.c
20 * - Unroll direct copy to three copies per loop in inffast.c
21 *
22 * 1.2.beta2 4 Dec 2002
23 * - Change external routine names to reduce potential conflicts
24 * - Correct filename to inffixed.h for fixed tables in inflate.c
25 * - Make hbuf[] unsigned char to match parameter type in inflate.c
26 * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
27 * to avoid negation problem on Alphas (64 bit) in inflate.c
28 *
29 * 1.2.beta3 22 Dec 2002
30 * - Add comments on state->bits assertion in inffast.c
31 * - Add comments on op field in inftrees.h
32 * - Fix bug in reuse of allocated window after inflateReset()
33 * - Remove bit fields--back to byte structure for speed
34 * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
35 * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
36 * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
37 * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
38 * - Use local copies of stream next and avail values, as well as local bit
39 * buffer and bit count in inflate()--for speed when inflate_fast() not used
40 *
41 * 1.2.beta4 1 Jan 2003
42 * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
43 * - Move a comment on output buffer sizes from inffast.c to inflate.c
44 * - Add comments in inffast.c to introduce the inflate_fast() routine
45 * - Rearrange window copies in inflate_fast() for speed and simplification
46 * - Unroll last copy for window match in inflate_fast()
47 * - Use local copies of window variables in inflate_fast() for speed
48 * - Pull out common wnext == 0 case for speed in inflate_fast()
49 * - Make op and len in inflate_fast() unsigned for consistency
50 * - Add FAR to lcode and dcode declarations in inflate_fast()
51 * - Simplified bad distance check in inflate_fast()
52 * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
53 * source file infback.c to provide a call-back interface to inflate for
54 * programs like gzip and unzip -- uses window as output buffer to avoid
55 * window copying
56 *
57 * 1.2.beta5 1 Jan 2003
58 * - Improved inflateBack() interface to allow the caller to provide initial
59 * input in strm.
60 * - Fixed stored blocks bug in inflateBack()
61 *
62 * 1.2.beta6 4 Jan 2003
63 * - Added comments in inffast.c on effectiveness of POSTINC
64 * - Typecasting all around to reduce compiler warnings
65 * - Changed loops from while (1) or do {} while (1) to for (;;), again to
66 * make compilers happy
67 * - Changed type of window in inflateBackInit() to unsigned char *
68 *
69 * 1.2.beta7 27 Jan 2003
70 * - Changed many types to unsigned or unsigned short to avoid warnings
71 * - Added inflateCopy() function
72 *
73 * 1.2.0 9 Mar 2003
74 * - Changed inflateBack() interface to provide separate opaque descriptors
75 * for the in() and out() functions
76 * - Changed inflateBack() argument and in_func typedef to swap the length
77 * and buffer address return values for the input function
78 * - Check next_in and next_out for Z_NULL on entry to inflate()
79 *
80 * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
81 */
82
83#include "zutil.h"
84#include "inftrees.h"
85#include "inflate.h"
86#include "contrib/optimizations/inffast_chunk.h"
87#include "contrib/optimizations/chunkcopy.h"
88
89#ifdef MAKEFIXED
90# ifndef BUILDFIXED
91# define BUILDFIXED
92# endif
93#endif
94
95/* function prototypes */
96local int inflateStateCheck OF((z_streamp strm));
97local void fixedtables OF((struct inflate_state FAR *state));
98local int updatewindow OF((z_streamp strm, const unsigned char FAR *end,
99 unsigned copy));
100#ifdef BUILDFIXED
101 void makefixed OF((void));
102#endif
103local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf,
104 unsigned len));
105
106local int inflateStateCheck(strm)
107z_streamp strm;
108{
109 struct inflate_state FAR *state;
110 if (strm == Z_NULL ||
111 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
112 return 1;
113 state = (struct inflate_state FAR *)strm->state;
114 if (state == Z_NULL || state->strm != strm ||
115 state->mode < HEAD || state->mode > SYNC)
116 return 1;
117 return 0;
118}
119
120int ZEXPORT inflateResetKeep(strm)
121z_streamp strm;
122{
123 struct inflate_state FAR *state;
124
125 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
126 state = (struct inflate_state FAR *)strm->state;
127 strm->total_in = strm->total_out = state->total = 0;
128 strm->msg = Z_NULL;
129 if (state->wrap) /* to support ill-conceived Java test suite */
130 strm->adler = state->wrap & 1;
131 state->mode = HEAD;
132 state->last = 0;
133 state->havedict = 0;
android-autoroll058f9bd2022-04-07 15:32:56 +0000134 state->flags = -1;
Tim Van Pattenf2981ed2020-07-27 19:25:23 +0000135 state->dmax = 32768U;
136 state->head = Z_NULL;
137 state->hold = 0;
138 state->bits = 0;
139 state->lencode = state->distcode = state->next = state->codes;
140 state->sane = 1;
141 state->back = -1;
142 Tracev((stderr, "inflate: reset\n"));
143 return Z_OK;
144}
145
146int ZEXPORT inflateReset(strm)
147z_streamp strm;
148{
149 struct inflate_state FAR *state;
150
151 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
152 state = (struct inflate_state FAR *)strm->state;
153 state->wsize = 0;
154 state->whave = 0;
155 state->wnext = 0;
156 return inflateResetKeep(strm);
157}
158
159int ZEXPORT inflateReset2(strm, windowBits)
160z_streamp strm;
161int windowBits;
162{
163 int wrap;
164 struct inflate_state FAR *state;
165
166 /* get the state */
167 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
168 state = (struct inflate_state FAR *)strm->state;
169
170 /* extract wrap request from windowBits parameter */
171 if (windowBits < 0) {
172 wrap = 0;
173 windowBits = -windowBits;
174 }
175 else {
176 wrap = (windowBits >> 4) + 5;
177#ifdef GUNZIP
178 if (windowBits < 48)
179 windowBits &= 15;
180#endif
181 }
182
183 /* set number of window bits, free window if different */
184 if (windowBits && (windowBits < 8 || windowBits > 15))
185 return Z_STREAM_ERROR;
186 if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
187 ZFREE(strm, state->window);
188 state->window = Z_NULL;
189 }
190
191 /* update state and reset the rest of it */
192 state->wrap = wrap;
193 state->wbits = (unsigned)windowBits;
194 return inflateReset(strm);
195}
196
197int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
198z_streamp strm;
199int windowBits;
200const char *version;
201int stream_size;
202{
203 int ret;
204 struct inflate_state FAR *state;
205
206 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
207 stream_size != (int)(sizeof(z_stream)))
208 return Z_VERSION_ERROR;
209 if (strm == Z_NULL) return Z_STREAM_ERROR;
210 strm->msg = Z_NULL; /* in case we return an error */
211 if (strm->zalloc == (alloc_func)0) {
212#ifdef Z_SOLO
213 return Z_STREAM_ERROR;
214#else
215 strm->zalloc = zcalloc;
216 strm->opaque = (voidpf)0;
217#endif
218 }
219 if (strm->zfree == (free_func)0)
220#ifdef Z_SOLO
221 return Z_STREAM_ERROR;
222#else
223 strm->zfree = zcfree;
224#endif
225 state = (struct inflate_state FAR *)
226 ZALLOC(strm, 1, sizeof(struct inflate_state));
227 if (state == Z_NULL) return Z_MEM_ERROR;
228 Tracev((stderr, "inflate: allocated\n"));
229 strm->state = (struct internal_state FAR *)state;
230 state->strm = strm;
231 state->window = Z_NULL;
232 state->mode = HEAD; /* to pass state test in inflateReset2() */
233 state->check = 1L; /* 1L is the result of adler32() zero length data */
234 ret = inflateReset2(strm, windowBits);
235 if (ret != Z_OK) {
236 ZFREE(strm, state);
237 strm->state = Z_NULL;
238 }
239 return ret;
240}
241
242int ZEXPORT inflateInit_(strm, version, stream_size)
243z_streamp strm;
244const char *version;
245int stream_size;
246{
247 return inflateInit2_(strm, DEF_WBITS, version, stream_size);
248}
249
250int ZEXPORT inflatePrime(strm, bits, value)
251z_streamp strm;
252int bits;
253int value;
254{
255 struct inflate_state FAR *state;
256
257 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
258 state = (struct inflate_state FAR *)strm->state;
259 if (bits < 0) {
260 state->hold = 0;
261 state->bits = 0;
262 return Z_OK;
263 }
264 if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR;
265 value &= (1L << bits) - 1;
266 state->hold += (unsigned)value << state->bits;
267 state->bits += (uInt)bits;
268 return Z_OK;
269}
270
271/*
272 Return state with length and distance decoding tables and index sizes set to
273 fixed code decoding. Normally this returns fixed tables from inffixed.h.
274 If BUILDFIXED is defined, then instead this routine builds the tables the
275 first time it's called, and returns those tables the first time and
276 thereafter. This reduces the size of the code by about 2K bytes, in
277 exchange for a little execution time. However, BUILDFIXED should not be
278 used for threaded applications, since the rewriting of the tables and virgin
279 may not be thread-safe.
280 */
281local void fixedtables(state)
282struct inflate_state FAR *state;
283{
284#ifdef BUILDFIXED
285 static int virgin = 1;
286 static code *lenfix, *distfix;
287 static code fixed[544];
288
289 /* build fixed huffman tables if first call (may not be thread safe) */
290 if (virgin) {
291 unsigned sym, bits;
292 static code *next;
293
294 /* literal/length table */
295 sym = 0;
296 while (sym < 144) state->lens[sym++] = 8;
297 while (sym < 256) state->lens[sym++] = 9;
298 while (sym < 280) state->lens[sym++] = 7;
299 while (sym < 288) state->lens[sym++] = 8;
300 next = fixed;
301 lenfix = next;
302 bits = 9;
303 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
304
305 /* distance table */
306 sym = 0;
307 while (sym < 32) state->lens[sym++] = 5;
308 distfix = next;
309 bits = 5;
310 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
311
312 /* do this just once */
313 virgin = 0;
314 }
315#else /* !BUILDFIXED */
316# include "inffixed.h"
317#endif /* BUILDFIXED */
318 state->lencode = lenfix;
319 state->lenbits = 9;
320 state->distcode = distfix;
321 state->distbits = 5;
322}
323
324#ifdef MAKEFIXED
325#include <stdio.h>
326
327/*
328 Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
329 defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
330 those tables to stdout, which would be piped to inffixed.h. A small program
331 can simply call makefixed to do this:
332
333 void makefixed(void);
334
335 int main(void)
336 {
337 makefixed();
338 return 0;
339 }
340
341 Then that can be linked with zlib built with MAKEFIXED defined and run:
342
343 a.out > inffixed.h
344 */
345void makefixed()
346{
347 unsigned low, size;
348 struct inflate_state state;
349
350 fixedtables(&state);
351 puts(" /* inffixed.h -- table for decoding fixed codes");
352 puts(" * Generated automatically by makefixed().");
353 puts(" */");
354 puts("");
355 puts(" /* WARNING: this file should *not* be used by applications.");
356 puts(" It is part of the implementation of this library and is");
357 puts(" subject to change. Applications should only use zlib.h.");
358 puts(" */");
359 puts("");
360 size = 1U << 9;
361 printf(" static const code lenfix[%u] = {", size);
362 low = 0;
363 for (;;) {
364 if ((low % 7) == 0) printf("\n ");
365 printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
366 state.lencode[low].bits, state.lencode[low].val);
367 if (++low == size) break;
368 putchar(',');
369 }
370 puts("\n };");
371 size = 1U << 5;
372 printf("\n static const code distfix[%u] = {", size);
373 low = 0;
374 for (;;) {
375 if ((low % 6) == 0) printf("\n ");
376 printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
377 state.distcode[low].val);
378 if (++low == size) break;
379 putchar(',');
380 }
381 puts("\n };");
382}
383#endif /* MAKEFIXED */
384
385/*
386 Update the window with the last wsize (normally 32K) bytes written before
387 returning. If window does not exist yet, create it. This is only called
388 when a window is already in use, or when output has been written during this
389 inflate call, but the end of the deflate stream has not been reached yet.
390 It is also called to create a window for dictionary data when a dictionary
391 is loaded.
392
393 Providing output buffers larger than 32K to inflate() should provide a speed
394 advantage, since only the last 32K of output is copied to the sliding window
395 upon return from inflate(), and since all distances after the first 32K of
396 output will fall in the output data, making match copies simpler and faster.
397 The advantage may be dependent on the size of the processor's data caches.
398 */
399local int updatewindow(strm, end, copy)
400z_streamp strm;
401const Bytef *end;
402unsigned copy;
403{
404 struct inflate_state FAR *state;
405 unsigned dist;
406
407 state = (struct inflate_state FAR *)strm->state;
408
409 /* if it hasn't been done already, allocate space for the window */
410 if (state->window == Z_NULL) {
411 unsigned wsize = 1U << state->wbits;
412 state->window = (unsigned char FAR *)
413 ZALLOC(strm, wsize + CHUNKCOPY_CHUNK_SIZE,
414 sizeof(unsigned char));
415 if (state->window == Z_NULL) return 1;
416#ifdef INFLATE_CLEAR_UNUSED_UNDEFINED
417 /* Copies from the overflow portion of this buffer are undefined and
418 may cause analysis tools to raise a warning if we don't initialize
419 it. However, this undefined data overwrites other undefined data
420 and is subsequently either overwritten or left deliberately
421 undefined at the end of decode; so there's really no point.
422 */
423 zmemzero(state->window + wsize, CHUNKCOPY_CHUNK_SIZE);
424#endif
425 }
426
427 /* if window not in use yet, initialize */
428 if (state->wsize == 0) {
429 state->wsize = 1U << state->wbits;
430 state->wnext = 0;
431 state->whave = 0;
432 }
433
434 /* copy state->wsize or less output bytes into the circular window */
435 if (copy >= state->wsize) {
436 zmemcpy(state->window, end - state->wsize, state->wsize);
437 state->wnext = 0;
438 state->whave = state->wsize;
439 }
440 else {
441 dist = state->wsize - state->wnext;
442 if (dist > copy) dist = copy;
443 zmemcpy(state->window + state->wnext, end - copy, dist);
444 copy -= dist;
445 if (copy) {
446 zmemcpy(state->window, end - copy, copy);
447 state->wnext = copy;
448 state->whave = state->wsize;
449 }
450 else {
451 state->wnext += dist;
452 if (state->wnext == state->wsize) state->wnext = 0;
453 if (state->whave < state->wsize) state->whave += dist;
454 }
455 }
456 return 0;
457}
458
459/* Macros for inflate(): */
460
461/* check function to use adler32() for zlib or crc32() for gzip */
462#ifdef GUNZIP
463# define UPDATE(check, buf, len) \
464 (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
465#else
466# define UPDATE(check, buf, len) adler32(check, buf, len)
467#endif
468
469/* check macros for header crc */
470#ifdef GUNZIP
471# define CRC2(check, word) \
472 do { \
473 hbuf[0] = (unsigned char)(word); \
474 hbuf[1] = (unsigned char)((word) >> 8); \
475 check = crc32(check, hbuf, 2); \
476 } while (0)
477
478# define CRC4(check, word) \
479 do { \
480 hbuf[0] = (unsigned char)(word); \
481 hbuf[1] = (unsigned char)((word) >> 8); \
482 hbuf[2] = (unsigned char)((word) >> 16); \
483 hbuf[3] = (unsigned char)((word) >> 24); \
484 check = crc32(check, hbuf, 4); \
485 } while (0)
486#endif
487
488/* Load registers with state in inflate() for speed */
489#define LOAD() \
490 do { \
491 put = strm->next_out; \
492 left = strm->avail_out; \
493 next = strm->next_in; \
494 have = strm->avail_in; \
495 hold = state->hold; \
496 bits = state->bits; \
497 } while (0)
498
499/* Restore state from registers in inflate() */
500#define RESTORE() \
501 do { \
502 strm->next_out = put; \
503 strm->avail_out = left; \
504 strm->next_in = next; \
505 strm->avail_in = have; \
506 state->hold = hold; \
507 state->bits = bits; \
508 } while (0)
509
510/* Clear the input bit accumulator */
511#define INITBITS() \
512 do { \
513 hold = 0; \
514 bits = 0; \
515 } while (0)
516
517/* Get a byte of input into the bit accumulator, or return from inflate()
518 if there is no input available. */
519#define PULLBYTE() \
520 do { \
521 if (have == 0) goto inf_leave; \
522 have--; \
523 hold += (unsigned long)(*next++) << bits; \
524 bits += 8; \
525 } while (0)
526
527/* Assure that there are at least n bits in the bit accumulator. If there is
528 not enough available input to do that, then return from inflate(). */
529#define NEEDBITS(n) \
530 do { \
531 while (bits < (unsigned)(n)) \
532 PULLBYTE(); \
533 } while (0)
534
535/* Return the low n bits of the bit accumulator (n < 16) */
536#define BITS(n) \
537 ((unsigned)hold & ((1U << (n)) - 1))
538
539/* Remove n bits from the bit accumulator */
540#define DROPBITS(n) \
541 do { \
542 hold >>= (n); \
543 bits -= (unsigned)(n); \
544 } while (0)
545
546/* Remove zero to seven bits as needed to go to a byte boundary */
547#define BYTEBITS() \
548 do { \
549 hold >>= bits & 7; \
550 bits -= bits & 7; \
551 } while (0)
552
553/*
554 inflate() uses a state machine to process as much input data and generate as
555 much output data as possible before returning. The state machine is
556 structured roughly as follows:
557
558 for (;;) switch (state) {
559 ...
560 case STATEn:
561 if (not enough input data or output space to make progress)
562 return;
563 ... make progress ...
564 state = STATEm;
565 break;
566 ...
567 }
568
569 so when inflate() is called again, the same case is attempted again, and
570 if the appropriate resources are provided, the machine proceeds to the
571 next state. The NEEDBITS() macro is usually the way the state evaluates
572 whether it can proceed or should return. NEEDBITS() does the return if
573 the requested bits are not available. The typical use of the BITS macros
574 is:
575
576 NEEDBITS(n);
577 ... do something with BITS(n) ...
578 DROPBITS(n);
579
580 where NEEDBITS(n) either returns from inflate() if there isn't enough
581 input left to load n bits into the accumulator, or it continues. BITS(n)
582 gives the low n bits in the accumulator. When done, DROPBITS(n) drops
583 the low n bits off the accumulator. INITBITS() clears the accumulator
584 and sets the number of available bits to zero. BYTEBITS() discards just
585 enough bits to put the accumulator on a byte boundary. After BYTEBITS()
586 and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
587
588 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
589 if there is no input available. The decoding of variable length codes uses
590 PULLBYTE() directly in order to pull just enough bytes to decode the next
591 code, and no more.
592
593 Some states loop until they get enough input, making sure that enough
594 state information is maintained to continue the loop where it left off
595 if NEEDBITS() returns in the loop. For example, want, need, and keep
596 would all have to actually be part of the saved state in case NEEDBITS()
597 returns:
598
599 case STATEw:
600 while (want < need) {
601 NEEDBITS(n);
602 keep[want++] = BITS(n);
603 DROPBITS(n);
604 }
605 state = STATEx;
606 case STATEx:
607
608 As shown above, if the next state is also the next case, then the break
609 is omitted.
610
611 A state may also return if there is not enough output space available to
612 complete that state. Those states are copying stored data, writing a
613 literal byte, and copying a matching string.
614
615 When returning, a "goto inf_leave" is used to update the total counters,
616 update the check value, and determine whether any progress has been made
617 during that inflate() call in order to return the proper return code.
618 Progress is defined as a change in either strm->avail_in or strm->avail_out.
619 When there is a window, goto inf_leave will update the window with the last
620 output written. If a goto inf_leave occurs in the middle of decompression
621 and there is no window currently, goto inf_leave will create one and copy
622 output to the window for the next call of inflate().
623
624 In this implementation, the flush parameter of inflate() only affects the
625 return code (per zlib.h). inflate() always writes as much as possible to
626 strm->next_out, given the space available and the provided input--the effect
627 documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
628 the allocation of and copying into a sliding window until necessary, which
629 provides the effect documented in zlib.h for Z_FINISH when the entire input
630 stream available. So the only thing the flush parameter actually does is:
631 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
632 will return Z_BUF_ERROR if it has not reached the end of the stream.
633 */
634
635int ZEXPORT inflate(strm, flush)
636z_streamp strm;
637int flush;
638{
639 struct inflate_state FAR *state;
640 z_const unsigned char FAR *next; /* next input */
641 unsigned char FAR *put; /* next output */
642 unsigned have, left; /* available input and output */
643 unsigned long hold; /* bit buffer */
644 unsigned bits; /* bits in bit buffer */
645 unsigned in, out; /* save starting available input and output */
646 unsigned copy; /* number of stored or match bytes to copy */
647 unsigned char FAR *from; /* where to copy match bytes from */
648 code here; /* current decoding table entry */
649 code last; /* parent table entry */
650 unsigned len; /* length to copy for repeats, bits to drop */
651 int ret; /* return code */
652#ifdef GUNZIP
653 unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
654#endif
655 static const unsigned short order[19] = /* permutation of code lengths */
656 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
657
658 if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||
659 (strm->next_in == Z_NULL && strm->avail_in != 0))
660 return Z_STREAM_ERROR;
661
662 state = (struct inflate_state FAR *)strm->state;
663 if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
664 LOAD();
665 in = have;
666 out = left;
667 ret = Z_OK;
668 for (;;)
669 switch (state->mode) {
670 case HEAD:
671 if (state->wrap == 0) {
672 state->mode = TYPEDO;
673 break;
674 }
675 NEEDBITS(16);
676#ifdef GUNZIP
677 if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
678 if (state->wbits == 0)
679 state->wbits = 15;
680 state->check = crc32(0L, Z_NULL, 0);
681 CRC2(state->check, hold);
682 INITBITS();
683 state->mode = FLAGS;
684 break;
685 }
Tim Van Pattenf2981ed2020-07-27 19:25:23 +0000686 if (state->head != Z_NULL)
687 state->head->done = -1;
688 if (!(state->wrap & 1) || /* check if zlib header allowed */
689#else
690 if (
691#endif
692 ((BITS(8) << 8) + (hold >> 8)) % 31) {
693 strm->msg = (char *)"incorrect header check";
694 state->mode = BAD;
695 break;
696 }
697 if (BITS(4) != Z_DEFLATED) {
698 strm->msg = (char *)"unknown compression method";
699 state->mode = BAD;
700 break;
701 }
702 DROPBITS(4);
703 len = BITS(4) + 8;
704 if (state->wbits == 0)
705 state->wbits = len;
706 if (len > 15 || len > state->wbits) {
707 strm->msg = (char *)"invalid window size";
708 state->mode = BAD;
709 break;
710 }
711 state->dmax = 1U << len;
android-autoroll058f9bd2022-04-07 15:32:56 +0000712 state->flags = 0; /* indicate zlib header */
Tim Van Pattenf2981ed2020-07-27 19:25:23 +0000713 Tracev((stderr, "inflate: zlib header ok\n"));
714 strm->adler = state->check = adler32(0L, Z_NULL, 0);
715 state->mode = hold & 0x200 ? DICTID : TYPE;
716 INITBITS();
717 break;
718#ifdef GUNZIP
719 case FLAGS:
720 NEEDBITS(16);
721 state->flags = (int)(hold);
722 if ((state->flags & 0xff) != Z_DEFLATED) {
723 strm->msg = (char *)"unknown compression method";
724 state->mode = BAD;
725 break;
726 }
727 if (state->flags & 0xe000) {
728 strm->msg = (char *)"unknown header flags set";
729 state->mode = BAD;
730 break;
731 }
732 if (state->head != Z_NULL)
733 state->head->text = (int)((hold >> 8) & 1);
734 if ((state->flags & 0x0200) && (state->wrap & 4))
735 CRC2(state->check, hold);
736 INITBITS();
737 state->mode = TIME;
738 case TIME:
739 NEEDBITS(32);
740 if (state->head != Z_NULL)
741 state->head->time = hold;
742 if ((state->flags & 0x0200) && (state->wrap & 4))
743 CRC4(state->check, hold);
744 INITBITS();
745 state->mode = OS;
746 case OS:
747 NEEDBITS(16);
748 if (state->head != Z_NULL) {
749 state->head->xflags = (int)(hold & 0xff);
750 state->head->os = (int)(hold >> 8);
751 }
752 if ((state->flags & 0x0200) && (state->wrap & 4))
753 CRC2(state->check, hold);
754 INITBITS();
755 state->mode = EXLEN;
756 case EXLEN:
757 if (state->flags & 0x0400) {
758 NEEDBITS(16);
759 state->length = (unsigned)(hold);
760 if (state->head != Z_NULL)
761 state->head->extra_len = (unsigned)hold;
762 if ((state->flags & 0x0200) && (state->wrap & 4))
763 CRC2(state->check, hold);
764 INITBITS();
765 }
766 else if (state->head != Z_NULL)
767 state->head->extra = Z_NULL;
768 state->mode = EXTRA;
769 case EXTRA:
770 if (state->flags & 0x0400) {
771 copy = state->length;
772 if (copy > have) copy = have;
773 if (copy) {
774 if (state->head != Z_NULL &&
Sadaf Ebrahimi70703712023-01-05 05:02:31 +0000775 state->head->extra != Z_NULL &&
776 (len = state->head->extra_len - state->length) <
777 state->head->extra_max) {
Tim Van Pattenf2981ed2020-07-27 19:25:23 +0000778 zmemcpy(state->head->extra + len, next,
779 len + copy > state->head->extra_max ?
780 state->head->extra_max - len : copy);
781 }
782 if ((state->flags & 0x0200) && (state->wrap & 4))
783 state->check = crc32(state->check, next, copy);
784 have -= copy;
785 next += copy;
786 state->length -= copy;
787 }
788 if (state->length) goto inf_leave;
789 }
790 state->length = 0;
791 state->mode = NAME;
792 case NAME:
793 if (state->flags & 0x0800) {
794 if (have == 0) goto inf_leave;
795 copy = 0;
796 do {
797 len = (unsigned)(next[copy++]);
798 if (state->head != Z_NULL &&
799 state->head->name != Z_NULL &&
800 state->length < state->head->name_max)
801 state->head->name[state->length++] = (Bytef)len;
802 } while (len && copy < have);
803 if ((state->flags & 0x0200) && (state->wrap & 4))
804 state->check = crc32(state->check, next, copy);
805 have -= copy;
806 next += copy;
807 if (len) goto inf_leave;
808 }
809 else if (state->head != Z_NULL)
810 state->head->name = Z_NULL;
811 state->length = 0;
812 state->mode = COMMENT;
813 case COMMENT:
814 if (state->flags & 0x1000) {
815 if (have == 0) goto inf_leave;
816 copy = 0;
817 do {
818 len = (unsigned)(next[copy++]);
819 if (state->head != Z_NULL &&
820 state->head->comment != Z_NULL &&
821 state->length < state->head->comm_max)
822 state->head->comment[state->length++] = (Bytef)len;
823 } while (len && copy < have);
824 if ((state->flags & 0x0200) && (state->wrap & 4))
825 state->check = crc32(state->check, next, copy);
826 have -= copy;
827 next += copy;
828 if (len) goto inf_leave;
829 }
830 else if (state->head != Z_NULL)
831 state->head->comment = Z_NULL;
832 state->mode = HCRC;
833 case HCRC:
834 if (state->flags & 0x0200) {
835 NEEDBITS(16);
836 if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
837 strm->msg = (char *)"header crc mismatch";
838 state->mode = BAD;
839 break;
840 }
841 INITBITS();
842 }
843 if (state->head != Z_NULL) {
844 state->head->hcrc = (int)((state->flags >> 9) & 1);
845 state->head->done = 1;
846 }
847 strm->adler = state->check = crc32(0L, Z_NULL, 0);
848 state->mode = TYPE;
849 break;
850#endif
851 case DICTID:
852 NEEDBITS(32);
853 strm->adler = state->check = ZSWAP32(hold);
854 INITBITS();
855 state->mode = DICT;
856 case DICT:
857 if (state->havedict == 0) {
858 RESTORE();
859 return Z_NEED_DICT;
860 }
861 strm->adler = state->check = adler32(0L, Z_NULL, 0);
862 state->mode = TYPE;
863 case TYPE:
864 if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
865 case TYPEDO:
866 if (state->last) {
867 BYTEBITS();
868 state->mode = CHECK;
869 break;
870 }
871 NEEDBITS(3);
872 state->last = BITS(1);
873 DROPBITS(1);
874 switch (BITS(2)) {
875 case 0: /* stored block */
876 Tracev((stderr, "inflate: stored block%s\n",
877 state->last ? " (last)" : ""));
878 state->mode = STORED;
879 break;
880 case 1: /* fixed block */
881 fixedtables(state);
882 Tracev((stderr, "inflate: fixed codes block%s\n",
883 state->last ? " (last)" : ""));
884 state->mode = LEN_; /* decode codes */
885 if (flush == Z_TREES) {
886 DROPBITS(2);
887 goto inf_leave;
888 }
889 break;
890 case 2: /* dynamic block */
891 Tracev((stderr, "inflate: dynamic codes block%s\n",
892 state->last ? " (last)" : ""));
893 state->mode = TABLE;
894 break;
895 case 3:
896 strm->msg = (char *)"invalid block type";
897 state->mode = BAD;
898 }
899 DROPBITS(2);
900 break;
901 case STORED:
902 BYTEBITS(); /* go to byte boundary */
903 NEEDBITS(32);
904 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
905 strm->msg = (char *)"invalid stored block lengths";
906 state->mode = BAD;
907 break;
908 }
909 state->length = (unsigned)hold & 0xffff;
910 Tracev((stderr, "inflate: stored length %u\n",
911 state->length));
912 INITBITS();
913 state->mode = COPY_;
914 if (flush == Z_TREES) goto inf_leave;
915 case COPY_:
916 state->mode = COPY;
917 case COPY:
918 copy = state->length;
919 if (copy) {
920 if (copy > have) copy = have;
921 if (copy > left) copy = left;
922 if (copy == 0) goto inf_leave;
923 zmemcpy(put, next, copy);
924 have -= copy;
925 next += copy;
926 left -= copy;
927 put += copy;
928 state->length -= copy;
929 break;
930 }
931 Tracev((stderr, "inflate: stored end\n"));
932 state->mode = TYPE;
933 break;
934 case TABLE:
935 NEEDBITS(14);
936 state->nlen = BITS(5) + 257;
937 DROPBITS(5);
938 state->ndist = BITS(5) + 1;
939 DROPBITS(5);
940 state->ncode = BITS(4) + 4;
941 DROPBITS(4);
942#ifndef PKZIP_BUG_WORKAROUND
943 if (state->nlen > 286 || state->ndist > 30) {
944 strm->msg = (char *)"too many length or distance symbols";
945 state->mode = BAD;
946 break;
947 }
948#endif
949 Tracev((stderr, "inflate: table sizes ok\n"));
950 state->have = 0;
951 state->mode = LENLENS;
952 case LENLENS:
953 while (state->have < state->ncode) {
954 NEEDBITS(3);
955 state->lens[order[state->have++]] = (unsigned short)BITS(3);
956 DROPBITS(3);
957 }
958 while (state->have < 19)
959 state->lens[order[state->have++]] = 0;
960 state->next = state->codes;
961 state->lencode = (const code FAR *)(state->next);
962 state->lenbits = 7;
963 ret = inflate_table(CODES, state->lens, 19, &(state->next),
964 &(state->lenbits), state->work);
965 if (ret) {
966 strm->msg = (char *)"invalid code lengths set";
967 state->mode = BAD;
968 break;
969 }
970 Tracev((stderr, "inflate: code lengths ok\n"));
971 state->have = 0;
972 state->mode = CODELENS;
973 case CODELENS:
974 while (state->have < state->nlen + state->ndist) {
975 for (;;) {
976 here = state->lencode[BITS(state->lenbits)];
977 if ((unsigned)(here.bits) <= bits) break;
978 PULLBYTE();
979 }
980 if (here.val < 16) {
981 DROPBITS(here.bits);
982 state->lens[state->have++] = here.val;
983 }
984 else {
985 if (here.val == 16) {
986 NEEDBITS(here.bits + 2);
987 DROPBITS(here.bits);
988 if (state->have == 0) {
989 strm->msg = (char *)"invalid bit length repeat";
990 state->mode = BAD;
991 break;
992 }
993 len = state->lens[state->have - 1];
994 copy = 3 + BITS(2);
995 DROPBITS(2);
996 }
997 else if (here.val == 17) {
998 NEEDBITS(here.bits + 3);
999 DROPBITS(here.bits);
1000 len = 0;
1001 copy = 3 + BITS(3);
1002 DROPBITS(3);
1003 }
1004 else {
1005 NEEDBITS(here.bits + 7);
1006 DROPBITS(here.bits);
1007 len = 0;
1008 copy = 11 + BITS(7);
1009 DROPBITS(7);
1010 }
1011 if (state->have + copy > state->nlen + state->ndist) {
1012 strm->msg = (char *)"invalid bit length repeat";
1013 state->mode = BAD;
1014 break;
1015 }
1016 while (copy--)
1017 state->lens[state->have++] = (unsigned short)len;
1018 }
1019 }
1020
1021 /* handle error breaks in while */
1022 if (state->mode == BAD) break;
1023
1024 /* check for end-of-block code (better have one) */
1025 if (state->lens[256] == 0) {
1026 strm->msg = (char *)"invalid code -- missing end-of-block";
1027 state->mode = BAD;
1028 break;
1029 }
1030
1031 /* build code tables -- note: do not change the lenbits or distbits
1032 values here (9 and 6) without reading the comments in inftrees.h
1033 concerning the ENOUGH constants, which depend on those values */
1034 state->next = state->codes;
1035 state->lencode = (const code FAR *)(state->next);
1036 state->lenbits = 9;
1037 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
1038 &(state->lenbits), state->work);
1039 if (ret) {
1040 strm->msg = (char *)"invalid literal/lengths set";
1041 state->mode = BAD;
1042 break;
1043 }
1044 state->distcode = (const code FAR *)(state->next);
1045 state->distbits = 6;
1046 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
1047 &(state->next), &(state->distbits), state->work);
1048 if (ret) {
1049 strm->msg = (char *)"invalid distances set";
1050 state->mode = BAD;
1051 break;
1052 }
1053 Tracev((stderr, "inflate: codes ok\n"));
1054 state->mode = LEN_;
1055 if (flush == Z_TREES) goto inf_leave;
1056 case LEN_:
1057 state->mode = LEN;
1058 case LEN:
1059 if (have >= INFLATE_FAST_MIN_INPUT &&
1060 left >= INFLATE_FAST_MIN_OUTPUT) {
1061 RESTORE();
1062 inflate_fast_chunk_(strm, out);
1063 LOAD();
1064 if (state->mode == TYPE)
1065 state->back = -1;
1066 break;
1067 }
1068 state->back = 0;
1069 for (;;) {
1070 here = state->lencode[BITS(state->lenbits)];
1071 if ((unsigned)(here.bits) <= bits) break;
1072 PULLBYTE();
1073 }
1074 if (here.op && (here.op & 0xf0) == 0) {
1075 last = here;
1076 for (;;) {
1077 here = state->lencode[last.val +
1078 (BITS(last.bits + last.op) >> last.bits)];
1079 if ((unsigned)(last.bits + here.bits) <= bits) break;
1080 PULLBYTE();
1081 }
1082 DROPBITS(last.bits);
1083 state->back += last.bits;
1084 }
1085 DROPBITS(here.bits);
1086 state->back += here.bits;
1087 state->length = (unsigned)here.val;
1088 if ((int)(here.op) == 0) {
1089 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
1090 "inflate: literal '%c'\n" :
1091 "inflate: literal 0x%02x\n", here.val));
1092 state->mode = LIT;
1093 break;
1094 }
1095 if (here.op & 32) {
1096 Tracevv((stderr, "inflate: end of block\n"));
1097 state->back = -1;
1098 state->mode = TYPE;
1099 break;
1100 }
1101 if (here.op & 64) {
1102 strm->msg = (char *)"invalid literal/length code";
1103 state->mode = BAD;
1104 break;
1105 }
1106 state->extra = (unsigned)(here.op) & 15;
1107 state->mode = LENEXT;
1108 case LENEXT:
1109 if (state->extra) {
1110 NEEDBITS(state->extra);
1111 state->length += BITS(state->extra);
1112 DROPBITS(state->extra);
1113 state->back += state->extra;
1114 }
1115 Tracevv((stderr, "inflate: length %u\n", state->length));
1116 state->was = state->length;
1117 state->mode = DIST;
1118 case DIST:
1119 for (;;) {
1120 here = state->distcode[BITS(state->distbits)];
1121 if ((unsigned)(here.bits) <= bits) break;
1122 PULLBYTE();
1123 }
1124 if ((here.op & 0xf0) == 0) {
1125 last = here;
1126 for (;;) {
1127 here = state->distcode[last.val +
1128 (BITS(last.bits + last.op) >> last.bits)];
1129 if ((unsigned)(last.bits + here.bits) <= bits) break;
1130 PULLBYTE();
1131 }
1132 DROPBITS(last.bits);
1133 state->back += last.bits;
1134 }
1135 DROPBITS(here.bits);
1136 state->back += here.bits;
1137 if (here.op & 64) {
1138 strm->msg = (char *)"invalid distance code";
1139 state->mode = BAD;
1140 break;
1141 }
1142 state->offset = (unsigned)here.val;
1143 state->extra = (unsigned)(here.op) & 15;
1144 state->mode = DISTEXT;
1145 case DISTEXT:
1146 if (state->extra) {
1147 NEEDBITS(state->extra);
1148 state->offset += BITS(state->extra);
1149 DROPBITS(state->extra);
1150 state->back += state->extra;
1151 }
1152#ifdef INFLATE_STRICT
1153 if (state->offset > state->dmax) {
1154 strm->msg = (char *)"invalid distance too far back";
1155 state->mode = BAD;
1156 break;
1157 }
1158#endif
1159 Tracevv((stderr, "inflate: distance %u\n", state->offset));
1160 state->mode = MATCH;
1161 case MATCH:
1162 if (left == 0) goto inf_leave;
1163 copy = out - left;
1164 if (state->offset > copy) { /* copy from window */
1165 copy = state->offset - copy;
1166 if (copy > state->whave) {
1167 if (state->sane) {
1168 strm->msg = (char *)"invalid distance too far back";
1169 state->mode = BAD;
1170 break;
1171 }
1172#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1173 Trace((stderr, "inflate.c too far\n"));
1174 copy -= state->whave;
1175 if (copy > state->length) copy = state->length;
1176 if (copy > left) copy = left;
1177 left -= copy;
1178 state->length -= copy;
1179 do {
1180 *put++ = 0;
1181 } while (--copy);
1182 if (state->length == 0) state->mode = LEN;
1183 break;
1184#endif
1185 }
1186 if (copy > state->wnext) {
1187 copy -= state->wnext;
1188 from = state->window + (state->wsize - copy);
1189 }
1190 else
1191 from = state->window + (state->wnext - copy);
1192 if (copy > state->length) copy = state->length;
1193 if (copy > left) copy = left;
1194 put = chunkcopy_safe(put, from, copy, put + left);
1195 }
1196 else { /* copy from output */
1197 copy = state->length;
1198 if (copy > left) copy = left;
1199 put = chunkcopy_lapped_safe(put, state->offset, copy, put + left);
1200 }
1201 left -= copy;
1202 state->length -= copy;
1203 if (state->length == 0) state->mode = LEN;
1204 break;
1205 case LIT:
1206 if (left == 0) goto inf_leave;
1207 *put++ = (unsigned char)(state->length);
1208 left--;
1209 state->mode = LEN;
1210 break;
1211 case CHECK:
1212 if (state->wrap) {
1213 NEEDBITS(32);
1214 out -= left;
1215 strm->total_out += out;
1216 state->total += out;
1217 if ((state->wrap & 4) && out)
1218 strm->adler = state->check =
1219 UPDATE(state->check, put - out, out);
1220 out = left;
1221 if ((state->wrap & 4) && (
1222#ifdef GUNZIP
1223 state->flags ? hold :
1224#endif
1225 ZSWAP32(hold)) != state->check) {
1226 strm->msg = (char *)"incorrect data check";
1227 state->mode = BAD;
1228 break;
1229 }
1230 INITBITS();
1231 Tracev((stderr, "inflate: check matches trailer\n"));
1232 }
1233#ifdef GUNZIP
1234 state->mode = LENGTH;
1235 case LENGTH:
1236 if (state->wrap && state->flags) {
1237 NEEDBITS(32);
android-autoroll058f9bd2022-04-07 15:32:56 +00001238 if ((state->wrap & 4) && hold != (state->total & 0xffffffff)) {
Tim Van Pattenf2981ed2020-07-27 19:25:23 +00001239 strm->msg = (char *)"incorrect length check";
1240 state->mode = BAD;
1241 break;
1242 }
1243 INITBITS();
1244 Tracev((stderr, "inflate: length matches trailer\n"));
1245 }
1246#endif
1247 state->mode = DONE;
1248 case DONE:
1249 ret = Z_STREAM_END;
1250 goto inf_leave;
1251 case BAD:
1252 ret = Z_DATA_ERROR;
1253 goto inf_leave;
1254 case MEM:
1255 return Z_MEM_ERROR;
1256 case SYNC:
1257 default:
1258 return Z_STREAM_ERROR;
1259 }
1260
1261 /*
1262 Return from inflate(), updating the total counts and the check value.
1263 If there was no progress during the inflate() call, return a buffer
1264 error. Call updatewindow() to create and/or update the window state.
1265 Note: a memory error from inflate() is non-recoverable.
1266 */
1267 inf_leave:
1268 /* We write a defined value in the unused space to help mark
1269 * where the stream has ended. We don't use zeros as that can
1270 * mislead clients relying on undefined behavior (i.e. assuming
1271 * that the data is over when the buffer has a zero/null value).
1272 */
1273 if (left >= CHUNKCOPY_CHUNK_SIZE)
1274 memset(put, 0x55, CHUNKCOPY_CHUNK_SIZE);
1275 else
1276 memset(put, 0x55, left);
1277
1278 RESTORE();
1279 if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1280 (state->mode < CHECK || flush != Z_FINISH)))
1281 if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
1282 state->mode = MEM;
1283 return Z_MEM_ERROR;
1284 }
1285 in -= strm->avail_in;
1286 out -= strm->avail_out;
1287 strm->total_in += in;
1288 strm->total_out += out;
1289 state->total += out;
1290 if ((state->wrap & 4) && out)
1291 strm->adler = state->check =
1292 UPDATE(state->check, strm->next_out - out, out);
1293 strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1294 (state->mode == TYPE ? 128 : 0) +
1295 (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1296 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1297 ret = Z_BUF_ERROR;
1298 return ret;
1299}
1300
1301int ZEXPORT inflateEnd(strm)
1302z_streamp strm;
1303{
1304 struct inflate_state FAR *state;
1305 if (inflateStateCheck(strm))
1306 return Z_STREAM_ERROR;
1307 state = (struct inflate_state FAR *)strm->state;
1308 if (state->window != Z_NULL) ZFREE(strm, state->window);
1309 ZFREE(strm, strm->state);
1310 strm->state = Z_NULL;
1311 Tracev((stderr, "inflate: end\n"));
1312 return Z_OK;
1313}
1314
1315int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength)
1316z_streamp strm;
1317Bytef *dictionary;
1318uInt *dictLength;
1319{
1320 struct inflate_state FAR *state;
1321
1322 /* check state */
1323 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1324 state = (struct inflate_state FAR *)strm->state;
1325
1326 /* copy dictionary */
1327 if (state->whave && dictionary != Z_NULL) {
1328 zmemcpy(dictionary, state->window + state->wnext,
1329 state->whave - state->wnext);
1330 zmemcpy(dictionary + state->whave - state->wnext,
1331 state->window, state->wnext);
1332 }
1333 if (dictLength != Z_NULL)
1334 *dictLength = state->whave;
1335 return Z_OK;
1336}
1337
1338int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
1339z_streamp strm;
1340const Bytef *dictionary;
1341uInt dictLength;
1342{
1343 struct inflate_state FAR *state;
1344 unsigned long dictid;
1345 int ret;
1346
1347 /* check state */
1348 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1349 state = (struct inflate_state FAR *)strm->state;
1350 if (state->wrap != 0 && state->mode != DICT)
1351 return Z_STREAM_ERROR;
1352
1353 /* check for correct dictionary identifier */
1354 if (state->mode == DICT) {
1355 dictid = adler32(0L, Z_NULL, 0);
1356 dictid = adler32(dictid, dictionary, dictLength);
1357 if (dictid != state->check)
1358 return Z_DATA_ERROR;
1359 }
1360
1361 /* copy dictionary to window using updatewindow(), which will amend the
1362 existing dictionary if appropriate */
1363 ret = updatewindow(strm, dictionary + dictLength, dictLength);
1364 if (ret) {
1365 state->mode = MEM;
1366 return Z_MEM_ERROR;
1367 }
1368 state->havedict = 1;
1369 Tracev((stderr, "inflate: dictionary set\n"));
1370 return Z_OK;
1371}
1372
1373int ZEXPORT inflateGetHeader(strm, head)
1374z_streamp strm;
1375gz_headerp head;
1376{
1377 struct inflate_state FAR *state;
1378
1379 /* check state */
1380 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1381 state = (struct inflate_state FAR *)strm->state;
1382 if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
1383
1384 /* save header structure */
1385 state->head = head;
1386 head->done = 0;
1387 return Z_OK;
1388}
1389
1390/*
1391 Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
1392 or when out of input. When called, *have is the number of pattern bytes
1393 found in order so far, in 0..3. On return *have is updated to the new
1394 state. If on return *have equals four, then the pattern was found and the
1395 return value is how many bytes were read including the last byte of the
1396 pattern. If *have is less than four, then the pattern has not been found
1397 yet and the return value is len. In the latter case, syncsearch() can be
1398 called again with more data and the *have state. *have is initialized to
1399 zero for the first call.
1400 */
1401local unsigned syncsearch(have, buf, len)
1402unsigned FAR *have;
1403const unsigned char FAR *buf;
1404unsigned len;
1405{
1406 unsigned got;
1407 unsigned next;
1408
1409 got = *have;
1410 next = 0;
1411 while (next < len && got < 4) {
1412 if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1413 got++;
1414 else if (buf[next])
1415 got = 0;
1416 else
1417 got = 4 - got;
1418 next++;
1419 }
1420 *have = got;
1421 return next;
1422}
1423
1424int ZEXPORT inflateSync(strm)
1425z_streamp strm;
1426{
1427 unsigned len; /* number of bytes to look at or looked at */
android-autoroll058f9bd2022-04-07 15:32:56 +00001428 int flags; /* temporary to save header status */
Tim Van Pattenf2981ed2020-07-27 19:25:23 +00001429 unsigned long in, out; /* temporary to save total_in and total_out */
1430 unsigned char buf[4]; /* to restore bit buffer to byte string */
1431 struct inflate_state FAR *state;
1432
1433 /* check parameters */
1434 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1435 state = (struct inflate_state FAR *)strm->state;
1436 if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
1437
1438 /* if first time, start search in bit buffer */
1439 if (state->mode != SYNC) {
1440 state->mode = SYNC;
1441 state->hold <<= state->bits & 7;
1442 state->bits -= state->bits & 7;
1443 len = 0;
1444 while (state->bits >= 8) {
1445 buf[len++] = (unsigned char)(state->hold);
1446 state->hold >>= 8;
1447 state->bits -= 8;
1448 }
1449 state->have = 0;
1450 syncsearch(&(state->have), buf, len);
1451 }
1452
1453 /* search available input */
1454 len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1455 strm->avail_in -= len;
1456 strm->next_in += len;
1457 strm->total_in += len;
1458
1459 /* return no joy or set up to restart inflate() on a new block */
1460 if (state->have != 4) return Z_DATA_ERROR;
android-autoroll058f9bd2022-04-07 15:32:56 +00001461 if (state->flags == -1)
1462 state->wrap = 0; /* if no header yet, treat as raw */
1463 else
1464 state->wrap &= ~4; /* no point in computing a check value now */
1465 flags = state->flags;
Tim Van Pattenf2981ed2020-07-27 19:25:23 +00001466 in = strm->total_in; out = strm->total_out;
1467 inflateReset(strm);
1468 strm->total_in = in; strm->total_out = out;
android-autoroll058f9bd2022-04-07 15:32:56 +00001469 state->flags = flags;
Tim Van Pattenf2981ed2020-07-27 19:25:23 +00001470 state->mode = TYPE;
1471 return Z_OK;
1472}
1473
1474/*
1475 Returns true if inflate is currently at the end of a block generated by
1476 Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1477 implementation to provide an additional safety check. PPP uses
1478 Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1479 block. When decompressing, PPP checks that at the end of input packet,
1480 inflate is waiting for these length bytes.
1481 */
1482int ZEXPORT inflateSyncPoint(strm)
1483z_streamp strm;
1484{
1485 struct inflate_state FAR *state;
1486
1487 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1488 state = (struct inflate_state FAR *)strm->state;
1489 return state->mode == STORED && state->bits == 0;
1490}
1491
1492int ZEXPORT inflateCopy(dest, source)
1493z_streamp dest;
1494z_streamp source;
1495{
1496 struct inflate_state FAR *state;
1497 struct inflate_state FAR *copy;
1498 unsigned char FAR *window;
1499 unsigned wsize;
1500
1501 /* check input */
1502 if (inflateStateCheck(source) || dest == Z_NULL)
1503 return Z_STREAM_ERROR;
1504 state = (struct inflate_state FAR *)source->state;
1505
1506 /* allocate space */
1507 copy = (struct inflate_state FAR *)
1508 ZALLOC(source, 1, sizeof(struct inflate_state));
1509 if (copy == Z_NULL) return Z_MEM_ERROR;
1510 window = Z_NULL;
1511 if (state->window != Z_NULL) {
1512 window = (unsigned char FAR *)
1513 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
1514 if (window == Z_NULL) {
1515 ZFREE(source, copy);
1516 return Z_MEM_ERROR;
1517 }
1518 }
1519
1520 /* copy state */
1521 zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1522 zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
1523 copy->strm = dest;
1524 if (state->lencode >= state->codes &&
1525 state->lencode <= state->codes + ENOUGH - 1) {
1526 copy->lencode = copy->codes + (state->lencode - state->codes);
1527 copy->distcode = copy->codes + (state->distcode - state->codes);
1528 }
1529 copy->next = copy->codes + (state->next - state->codes);
1530 if (window != Z_NULL) {
1531 wsize = 1U << state->wbits;
1532 zmemcpy(window, state->window, wsize);
1533 }
1534 copy->window = window;
1535 dest->state = (struct internal_state FAR *)copy;
1536 return Z_OK;
1537}
1538
1539int ZEXPORT inflateUndermine(strm, subvert)
1540z_streamp strm;
1541int subvert;
1542{
1543 struct inflate_state FAR *state;
1544
1545 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1546 state = (struct inflate_state FAR *)strm->state;
1547#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1548 state->sane = !subvert;
1549 return Z_OK;
1550#else
1551 (void)subvert;
1552 state->sane = 1;
1553 return Z_DATA_ERROR;
1554#endif
1555}
1556
1557int ZEXPORT inflateValidate(strm, check)
1558z_streamp strm;
1559int check;
1560{
1561 struct inflate_state FAR *state;
1562
1563 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1564 state = (struct inflate_state FAR *)strm->state;
android-autoroll058f9bd2022-04-07 15:32:56 +00001565 if (check && state->wrap)
Tim Van Pattenf2981ed2020-07-27 19:25:23 +00001566 state->wrap |= 4;
1567 else
1568 state->wrap &= ~4;
1569 return Z_OK;
1570}
1571
1572long ZEXPORT inflateMark(strm)
1573z_streamp strm;
1574{
1575 struct inflate_state FAR *state;
1576
1577 if (inflateStateCheck(strm))
1578 return -(1L << 16);
1579 state = (struct inflate_state FAR *)strm->state;
1580 return (long)(((unsigned long)((long)state->back)) << 16) +
1581 (state->mode == COPY ? state->length :
1582 (state->mode == MATCH ? state->was - state->length : 0));
1583}
1584
1585unsigned long ZEXPORT inflateCodesUsed(strm)
1586z_streamp strm;
1587{
1588 struct inflate_state FAR *state;
1589 if (inflateStateCheck(strm)) return (unsigned long)-1;
1590 state = (struct inflate_state FAR *)strm->state;
1591 return (unsigned long)(state->next - state->codes);
1592}