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