blob: 93776ac579c93dc548dfe351b307540f82610970 [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;
134 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 unsigned wsize = 1U << state->wbits;
411 state->window = (unsigned char FAR *)
412 ZALLOC(strm, wsize + CHUNKCOPY_CHUNK_SIZE,
413 sizeof(unsigned char));
414 if (state->window == Z_NULL) return 1;
415#ifdef INFLATE_CLEAR_UNUSED_UNDEFINED
416 /* Copies from the overflow portion of this buffer are undefined and
417 may cause analysis tools to raise a warning if we don't initialize
418 it. However, this undefined data overwrites other undefined data
419 and is subsequently either overwritten or left deliberately
420 undefined at the end of decode; so there's really no point.
421 */
422 zmemzero(state->window + wsize, CHUNKCOPY_CHUNK_SIZE);
423#endif
424 }
425
426 /* if window not in use yet, initialize */
427 if (state->wsize == 0) {
428 state->wsize = 1U << state->wbits;
429 state->wnext = 0;
430 state->whave = 0;
431 }
432
433 /* copy state->wsize or less output bytes into the circular window */
434 if (copy >= state->wsize) {
435 zmemcpy(state->window, end - state->wsize, state->wsize);
436 state->wnext = 0;
437 state->whave = state->wsize;
438 }
439 else {
440 dist = state->wsize - state->wnext;
441 if (dist > copy) dist = copy;
442 zmemcpy(state->window + state->wnext, end - copy, dist);
443 copy -= dist;
444 if (copy) {
445 zmemcpy(state->window, end - copy, copy);
446 state->wnext = copy;
447 state->whave = state->wsize;
448 }
449 else {
450 state->wnext += dist;
451 if (state->wnext == state->wsize) state->wnext = 0;
452 if (state->whave < state->wsize) state->whave += dist;
453 }
454 }
455 return 0;
456}
457
458/* Macros for inflate(): */
459
460/* check function to use adler32() for zlib or crc32() for gzip */
461#ifdef GUNZIP
462# define UPDATE(check, buf, len) \
463 (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
464#else
465# define UPDATE(check, buf, len) adler32(check, buf, len)
466#endif
467
468/* check macros for header crc */
469#ifdef GUNZIP
470# define CRC2(check, word) \
471 do { \
472 hbuf[0] = (unsigned char)(word); \
473 hbuf[1] = (unsigned char)((word) >> 8); \
474 check = crc32(check, hbuf, 2); \
475 } while (0)
476
477# define CRC4(check, word) \
478 do { \
479 hbuf[0] = (unsigned char)(word); \
480 hbuf[1] = (unsigned char)((word) >> 8); \
481 hbuf[2] = (unsigned char)((word) >> 16); \
482 hbuf[3] = (unsigned char)((word) >> 24); \
483 check = crc32(check, hbuf, 4); \
484 } while (0)
485#endif
486
487/* Load registers with state in inflate() for speed */
488#define LOAD() \
489 do { \
490 put = strm->next_out; \
491 left = strm->avail_out; \
492 next = strm->next_in; \
493 have = strm->avail_in; \
494 hold = state->hold; \
495 bits = state->bits; \
496 } while (0)
497
498/* Restore state from registers in inflate() */
499#define RESTORE() \
500 do { \
501 strm->next_out = put; \
502 strm->avail_out = left; \
503 strm->next_in = next; \
504 strm->avail_in = have; \
505 state->hold = hold; \
506 state->bits = bits; \
507 } while (0)
508
509/* Clear the input bit accumulator */
510#define INITBITS() \
511 do { \
512 hold = 0; \
513 bits = 0; \
514 } while (0)
515
516/* Get a byte of input into the bit accumulator, or return from inflate()
517 if there is no input available. */
518#define PULLBYTE() \
519 do { \
520 if (have == 0) goto inf_leave; \
521 have--; \
522 hold += (unsigned long)(*next++) << bits; \
523 bits += 8; \
524 } while (0)
525
526/* Assure that there are at least n bits in the bit accumulator. If there is
527 not enough available input to do that, then return from inflate(). */
528#define NEEDBITS(n) \
529 do { \
530 while (bits < (unsigned)(n)) \
531 PULLBYTE(); \
532 } while (0)
533
534/* Return the low n bits of the bit accumulator (n < 16) */
535#define BITS(n) \
536 ((unsigned)hold & ((1U << (n)) - 1))
537
538/* Remove n bits from the bit accumulator */
539#define DROPBITS(n) \
540 do { \
541 hold >>= (n); \
542 bits -= (unsigned)(n); \
543 } while (0)
544
545/* Remove zero to seven bits as needed to go to a byte boundary */
546#define BYTEBITS() \
547 do { \
548 hold >>= bits & 7; \
549 bits -= bits & 7; \
550 } while (0)
551
552/*
553 inflate() uses a state machine to process as much input data and generate as
554 much output data as possible before returning. The state machine is
555 structured roughly as follows:
556
557 for (;;) switch (state) {
558 ...
559 case STATEn:
560 if (not enough input data or output space to make progress)
561 return;
562 ... make progress ...
563 state = STATEm;
564 break;
565 ...
566 }
567
568 so when inflate() is called again, the same case is attempted again, and
569 if the appropriate resources are provided, the machine proceeds to the
570 next state. The NEEDBITS() macro is usually the way the state evaluates
571 whether it can proceed or should return. NEEDBITS() does the return if
572 the requested bits are not available. The typical use of the BITS macros
573 is:
574
575 NEEDBITS(n);
576 ... do something with BITS(n) ...
577 DROPBITS(n);
578
579 where NEEDBITS(n) either returns from inflate() if there isn't enough
580 input left to load n bits into the accumulator, or it continues. BITS(n)
581 gives the low n bits in the accumulator. When done, DROPBITS(n) drops
582 the low n bits off the accumulator. INITBITS() clears the accumulator
583 and sets the number of available bits to zero. BYTEBITS() discards just
584 enough bits to put the accumulator on a byte boundary. After BYTEBITS()
585 and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
586
587 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
588 if there is no input available. The decoding of variable length codes uses
589 PULLBYTE() directly in order to pull just enough bytes to decode the next
590 code, and no more.
591
592 Some states loop until they get enough input, making sure that enough
593 state information is maintained to continue the loop where it left off
594 if NEEDBITS() returns in the loop. For example, want, need, and keep
595 would all have to actually be part of the saved state in case NEEDBITS()
596 returns:
597
598 case STATEw:
599 while (want < need) {
600 NEEDBITS(n);
601 keep[want++] = BITS(n);
602 DROPBITS(n);
603 }
604 state = STATEx;
605 case STATEx:
606
607 As shown above, if the next state is also the next case, then the break
608 is omitted.
609
610 A state may also return if there is not enough output space available to
611 complete that state. Those states are copying stored data, writing a
612 literal byte, and copying a matching string.
613
614 When returning, a "goto inf_leave" is used to update the total counters,
615 update the check value, and determine whether any progress has been made
616 during that inflate() call in order to return the proper return code.
617 Progress is defined as a change in either strm->avail_in or strm->avail_out.
618 When there is a window, goto inf_leave will update the window with the last
619 output written. If a goto inf_leave occurs in the middle of decompression
620 and there is no window currently, goto inf_leave will create one and copy
621 output to the window for the next call of inflate().
622
623 In this implementation, the flush parameter of inflate() only affects the
624 return code (per zlib.h). inflate() always writes as much as possible to
625 strm->next_out, given the space available and the provided input--the effect
626 documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
627 the allocation of and copying into a sliding window until necessary, which
628 provides the effect documented in zlib.h for Z_FINISH when the entire input
629 stream available. So the only thing the flush parameter actually does is:
630 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
631 will return Z_BUF_ERROR if it has not reached the end of the stream.
632 */
633
634int ZEXPORT inflate(strm, flush)
635z_streamp strm;
636int flush;
637{
638 struct inflate_state FAR *state;
639 z_const unsigned char FAR *next; /* next input */
640 unsigned char FAR *put; /* next output */
641 unsigned have, left; /* available input and output */
642 unsigned long hold; /* bit buffer */
643 unsigned bits; /* bits in bit buffer */
644 unsigned in, out; /* save starting available input and output */
645 unsigned copy; /* number of stored or match bytes to copy */
646 unsigned char FAR *from; /* where to copy match bytes from */
647 code here; /* current decoding table entry */
648 code last; /* parent table entry */
649 unsigned len; /* length to copy for repeats, bits to drop */
650 int ret; /* return code */
651#ifdef GUNZIP
652 unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
653#endif
654 static const unsigned short order[19] = /* permutation of code lengths */
655 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
656
657 if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||
658 (strm->next_in == Z_NULL && strm->avail_in != 0))
659 return Z_STREAM_ERROR;
660
661 state = (struct inflate_state FAR *)strm->state;
662 if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
663 LOAD();
664 in = have;
665 out = left;
666 ret = Z_OK;
667 for (;;)
668 switch (state->mode) {
669 case HEAD:
670 if (state->wrap == 0) {
671 state->mode = TYPEDO;
672 break;
673 }
674 NEEDBITS(16);
675#ifdef GUNZIP
676 if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
677 if (state->wbits == 0)
678 state->wbits = 15;
679 state->check = crc32(0L, Z_NULL, 0);
680 CRC2(state->check, hold);
681 INITBITS();
682 state->mode = FLAGS;
683 break;
684 }
685 state->flags = 0; /* expect zlib header */
686 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;
712 Tracev((stderr, "inflate: zlib header ok\n"));
713 strm->adler = state->check = adler32(0L, Z_NULL, 0);
714 state->mode = hold & 0x200 ? DICTID : TYPE;
715 INITBITS();
716 break;
717#ifdef GUNZIP
718 case FLAGS:
719 NEEDBITS(16);
720 state->flags = (int)(hold);
721 if ((state->flags & 0xff) != Z_DEFLATED) {
722 strm->msg = (char *)"unknown compression method";
723 state->mode = BAD;
724 break;
725 }
726 if (state->flags & 0xe000) {
727 strm->msg = (char *)"unknown header flags set";
728 state->mode = BAD;
729 break;
730 }
731 if (state->head != Z_NULL)
732 state->head->text = (int)((hold >> 8) & 1);
733 if ((state->flags & 0x0200) && (state->wrap & 4))
734 CRC2(state->check, hold);
735 INITBITS();
736 state->mode = TIME;
737 case TIME:
738 NEEDBITS(32);
739 if (state->head != Z_NULL)
740 state->head->time = hold;
741 if ((state->flags & 0x0200) && (state->wrap & 4))
742 CRC4(state->check, hold);
743 INITBITS();
744 state->mode = OS;
745 case OS:
746 NEEDBITS(16);
747 if (state->head != Z_NULL) {
748 state->head->xflags = (int)(hold & 0xff);
749 state->head->os = (int)(hold >> 8);
750 }
751 if ((state->flags & 0x0200) && (state->wrap & 4))
752 CRC2(state->check, hold);
753 INITBITS();
754 state->mode = EXLEN;
755 case EXLEN:
756 if (state->flags & 0x0400) {
757 NEEDBITS(16);
758 state->length = (unsigned)(hold);
759 if (state->head != Z_NULL)
760 state->head->extra_len = (unsigned)hold;
761 if ((state->flags & 0x0200) && (state->wrap & 4))
762 CRC2(state->check, hold);
763 INITBITS();
764 }
765 else if (state->head != Z_NULL)
766 state->head->extra = Z_NULL;
767 state->mode = EXTRA;
768 case EXTRA:
769 if (state->flags & 0x0400) {
770 copy = state->length;
771 if (copy > have) copy = have;
772 if (copy) {
773 if (state->head != Z_NULL &&
Sadaf Ebrahimi2199d762023-01-05 05:02:31 +0000774 state->head->extra != Z_NULL &&
775 (len = state->head->extra_len - state->length) <
776 state->head->extra_max) {
Tim Van Pattenf2981ed2020-07-27 19:25:23 +0000777 zmemcpy(state->head->extra + len, next,
778 len + copy > state->head->extra_max ?
779 state->head->extra_max - len : copy);
780 }
781 if ((state->flags & 0x0200) && (state->wrap & 4))
782 state->check = crc32(state->check, next, copy);
783 have -= copy;
784 next += copy;
785 state->length -= copy;
786 }
787 if (state->length) goto inf_leave;
788 }
789 state->length = 0;
790 state->mode = NAME;
791 case NAME:
792 if (state->flags & 0x0800) {
793 if (have == 0) goto inf_leave;
794 copy = 0;
795 do {
796 len = (unsigned)(next[copy++]);
797 if (state->head != Z_NULL &&
798 state->head->name != Z_NULL &&
799 state->length < state->head->name_max)
800 state->head->name[state->length++] = (Bytef)len;
801 } while (len && copy < have);
802 if ((state->flags & 0x0200) && (state->wrap & 4))
803 state->check = crc32(state->check, next, copy);
804 have -= copy;
805 next += copy;
806 if (len) goto inf_leave;
807 }
808 else if (state->head != Z_NULL)
809 state->head->name = Z_NULL;
810 state->length = 0;
811 state->mode = COMMENT;
812 case COMMENT:
813 if (state->flags & 0x1000) {
814 if (have == 0) goto inf_leave;
815 copy = 0;
816 do {
817 len = (unsigned)(next[copy++]);
818 if (state->head != Z_NULL &&
819 state->head->comment != Z_NULL &&
820 state->length < state->head->comm_max)
821 state->head->comment[state->length++] = (Bytef)len;
822 } while (len && copy < have);
823 if ((state->flags & 0x0200) && (state->wrap & 4))
824 state->check = crc32(state->check, next, copy);
825 have -= copy;
826 next += copy;
827 if (len) goto inf_leave;
828 }
829 else if (state->head != Z_NULL)
830 state->head->comment = Z_NULL;
831 state->mode = HCRC;
832 case HCRC:
833 if (state->flags & 0x0200) {
834 NEEDBITS(16);
835 if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
836 strm->msg = (char *)"header crc mismatch";
837 state->mode = BAD;
838 break;
839 }
840 INITBITS();
841 }
842 if (state->head != Z_NULL) {
843 state->head->hcrc = (int)((state->flags >> 9) & 1);
844 state->head->done = 1;
845 }
846 strm->adler = state->check = crc32(0L, Z_NULL, 0);
847 state->mode = TYPE;
848 break;
849#endif
850 case DICTID:
851 NEEDBITS(32);
852 strm->adler = state->check = ZSWAP32(hold);
853 INITBITS();
854 state->mode = DICT;
855 case DICT:
856 if (state->havedict == 0) {
857 RESTORE();
858 return Z_NEED_DICT;
859 }
860 strm->adler = state->check = adler32(0L, Z_NULL, 0);
861 state->mode = TYPE;
862 case TYPE:
863 if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
864 case TYPEDO:
865 if (state->last) {
866 BYTEBITS();
867 state->mode = CHECK;
868 break;
869 }
870 NEEDBITS(3);
871 state->last = BITS(1);
872 DROPBITS(1);
873 switch (BITS(2)) {
874 case 0: /* stored block */
875 Tracev((stderr, "inflate: stored block%s\n",
876 state->last ? " (last)" : ""));
877 state->mode = STORED;
878 break;
879 case 1: /* fixed block */
880 fixedtables(state);
881 Tracev((stderr, "inflate: fixed codes block%s\n",
882 state->last ? " (last)" : ""));
883 state->mode = LEN_; /* decode codes */
884 if (flush == Z_TREES) {
885 DROPBITS(2);
886 goto inf_leave;
887 }
888 break;
889 case 2: /* dynamic block */
890 Tracev((stderr, "inflate: dynamic codes block%s\n",
891 state->last ? " (last)" : ""));
892 state->mode = TABLE;
893 break;
894 case 3:
895 strm->msg = (char *)"invalid block type";
896 state->mode = BAD;
897 }
898 DROPBITS(2);
899 break;
900 case STORED:
901 BYTEBITS(); /* go to byte boundary */
902 NEEDBITS(32);
903 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
904 strm->msg = (char *)"invalid stored block lengths";
905 state->mode = BAD;
906 break;
907 }
908 state->length = (unsigned)hold & 0xffff;
909 Tracev((stderr, "inflate: stored length %u\n",
910 state->length));
911 INITBITS();
912 state->mode = COPY_;
913 if (flush == Z_TREES) goto inf_leave;
914 case COPY_:
915 state->mode = COPY;
916 case COPY:
917 copy = state->length;
918 if (copy) {
919 if (copy > have) copy = have;
920 if (copy > left) copy = left;
921 if (copy == 0) goto inf_leave;
922 zmemcpy(put, next, copy);
923 have -= copy;
924 next += copy;
925 left -= copy;
926 put += copy;
927 state->length -= copy;
928 break;
929 }
930 Tracev((stderr, "inflate: stored end\n"));
931 state->mode = TYPE;
932 break;
933 case TABLE:
934 NEEDBITS(14);
935 state->nlen = BITS(5) + 257;
936 DROPBITS(5);
937 state->ndist = BITS(5) + 1;
938 DROPBITS(5);
939 state->ncode = BITS(4) + 4;
940 DROPBITS(4);
941#ifndef PKZIP_BUG_WORKAROUND
942 if (state->nlen > 286 || state->ndist > 30) {
943 strm->msg = (char *)"too many length or distance symbols";
944 state->mode = BAD;
945 break;
946 }
947#endif
948 Tracev((stderr, "inflate: table sizes ok\n"));
949 state->have = 0;
950 state->mode = LENLENS;
951 case LENLENS:
952 while (state->have < state->ncode) {
953 NEEDBITS(3);
954 state->lens[order[state->have++]] = (unsigned short)BITS(3);
955 DROPBITS(3);
956 }
957 while (state->have < 19)
958 state->lens[order[state->have++]] = 0;
959 state->next = state->codes;
960 state->lencode = (const code FAR *)(state->next);
961 state->lenbits = 7;
962 ret = inflate_table(CODES, state->lens, 19, &(state->next),
963 &(state->lenbits), state->work);
964 if (ret) {
965 strm->msg = (char *)"invalid code lengths set";
966 state->mode = BAD;
967 break;
968 }
969 Tracev((stderr, "inflate: code lengths ok\n"));
970 state->have = 0;
971 state->mode = CODELENS;
972 case CODELENS:
973 while (state->have < state->nlen + state->ndist) {
974 for (;;) {
975 here = state->lencode[BITS(state->lenbits)];
976 if ((unsigned)(here.bits) <= bits) break;
977 PULLBYTE();
978 }
979 if (here.val < 16) {
980 DROPBITS(here.bits);
981 state->lens[state->have++] = here.val;
982 }
983 else {
984 if (here.val == 16) {
985 NEEDBITS(here.bits + 2);
986 DROPBITS(here.bits);
987 if (state->have == 0) {
988 strm->msg = (char *)"invalid bit length repeat";
989 state->mode = BAD;
990 break;
991 }
992 len = state->lens[state->have - 1];
993 copy = 3 + BITS(2);
994 DROPBITS(2);
995 }
996 else if (here.val == 17) {
997 NEEDBITS(here.bits + 3);
998 DROPBITS(here.bits);
999 len = 0;
1000 copy = 3 + BITS(3);
1001 DROPBITS(3);
1002 }
1003 else {
1004 NEEDBITS(here.bits + 7);
1005 DROPBITS(here.bits);
1006 len = 0;
1007 copy = 11 + BITS(7);
1008 DROPBITS(7);
1009 }
1010 if (state->have + copy > state->nlen + state->ndist) {
1011 strm->msg = (char *)"invalid bit length repeat";
1012 state->mode = BAD;
1013 break;
1014 }
1015 while (copy--)
1016 state->lens[state->have++] = (unsigned short)len;
1017 }
1018 }
1019
1020 /* handle error breaks in while */
1021 if (state->mode == BAD) break;
1022
1023 /* check for end-of-block code (better have one) */
1024 if (state->lens[256] == 0) {
1025 strm->msg = (char *)"invalid code -- missing end-of-block";
1026 state->mode = BAD;
1027 break;
1028 }
1029
1030 /* build code tables -- note: do not change the lenbits or distbits
1031 values here (9 and 6) without reading the comments in inftrees.h
1032 concerning the ENOUGH constants, which depend on those values */
1033 state->next = state->codes;
1034 state->lencode = (const code FAR *)(state->next);
1035 state->lenbits = 9;
1036 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
1037 &(state->lenbits), state->work);
1038 if (ret) {
1039 strm->msg = (char *)"invalid literal/lengths set";
1040 state->mode = BAD;
1041 break;
1042 }
1043 state->distcode = (const code FAR *)(state->next);
1044 state->distbits = 6;
1045 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
1046 &(state->next), &(state->distbits), state->work);
1047 if (ret) {
1048 strm->msg = (char *)"invalid distances set";
1049 state->mode = BAD;
1050 break;
1051 }
1052 Tracev((stderr, "inflate: codes ok\n"));
1053 state->mode = LEN_;
1054 if (flush == Z_TREES) goto inf_leave;
1055 case LEN_:
1056 state->mode = LEN;
1057 case LEN:
1058 if (have >= INFLATE_FAST_MIN_INPUT &&
1059 left >= INFLATE_FAST_MIN_OUTPUT) {
1060 RESTORE();
1061 inflate_fast_chunk_(strm, out);
1062 LOAD();
1063 if (state->mode == TYPE)
1064 state->back = -1;
1065 break;
1066 }
1067 state->back = 0;
1068 for (;;) {
1069 here = state->lencode[BITS(state->lenbits)];
1070 if ((unsigned)(here.bits) <= bits) break;
1071 PULLBYTE();
1072 }
1073 if (here.op && (here.op & 0xf0) == 0) {
1074 last = here;
1075 for (;;) {
1076 here = state->lencode[last.val +
1077 (BITS(last.bits + last.op) >> last.bits)];
1078 if ((unsigned)(last.bits + here.bits) <= bits) break;
1079 PULLBYTE();
1080 }
1081 DROPBITS(last.bits);
1082 state->back += last.bits;
1083 }
1084 DROPBITS(here.bits);
1085 state->back += here.bits;
1086 state->length = (unsigned)here.val;
1087 if ((int)(here.op) == 0) {
1088 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
1089 "inflate: literal '%c'\n" :
1090 "inflate: literal 0x%02x\n", here.val));
1091 state->mode = LIT;
1092 break;
1093 }
1094 if (here.op & 32) {
1095 Tracevv((stderr, "inflate: end of block\n"));
1096 state->back = -1;
1097 state->mode = TYPE;
1098 break;
1099 }
1100 if (here.op & 64) {
1101 strm->msg = (char *)"invalid literal/length code";
1102 state->mode = BAD;
1103 break;
1104 }
1105 state->extra = (unsigned)(here.op) & 15;
1106 state->mode = LENEXT;
1107 case LENEXT:
1108 if (state->extra) {
1109 NEEDBITS(state->extra);
1110 state->length += BITS(state->extra);
1111 DROPBITS(state->extra);
1112 state->back += state->extra;
1113 }
1114 Tracevv((stderr, "inflate: length %u\n", state->length));
1115 state->was = state->length;
1116 state->mode = DIST;
1117 case DIST:
1118 for (;;) {
1119 here = state->distcode[BITS(state->distbits)];
1120 if ((unsigned)(here.bits) <= bits) break;
1121 PULLBYTE();
1122 }
1123 if ((here.op & 0xf0) == 0) {
1124 last = here;
1125 for (;;) {
1126 here = state->distcode[last.val +
1127 (BITS(last.bits + last.op) >> last.bits)];
1128 if ((unsigned)(last.bits + here.bits) <= bits) break;
1129 PULLBYTE();
1130 }
1131 DROPBITS(last.bits);
1132 state->back += last.bits;
1133 }
1134 DROPBITS(here.bits);
1135 state->back += here.bits;
1136 if (here.op & 64) {
1137 strm->msg = (char *)"invalid distance code";
1138 state->mode = BAD;
1139 break;
1140 }
1141 state->offset = (unsigned)here.val;
1142 state->extra = (unsigned)(here.op) & 15;
1143 state->mode = DISTEXT;
1144 case DISTEXT:
1145 if (state->extra) {
1146 NEEDBITS(state->extra);
1147 state->offset += BITS(state->extra);
1148 DROPBITS(state->extra);
1149 state->back += state->extra;
1150 }
1151#ifdef INFLATE_STRICT
1152 if (state->offset > state->dmax) {
1153 strm->msg = (char *)"invalid distance too far back";
1154 state->mode = BAD;
1155 break;
1156 }
1157#endif
1158 Tracevv((stderr, "inflate: distance %u\n", state->offset));
1159 state->mode = MATCH;
1160 case MATCH:
1161 if (left == 0) goto inf_leave;
1162 copy = out - left;
1163 if (state->offset > copy) { /* copy from window */
1164 copy = state->offset - copy;
1165 if (copy > state->whave) {
1166 if (state->sane) {
1167 strm->msg = (char *)"invalid distance too far back";
1168 state->mode = BAD;
1169 break;
1170 }
1171#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1172 Trace((stderr, "inflate.c too far\n"));
1173 copy -= state->whave;
1174 if (copy > state->length) copy = state->length;
1175 if (copy > left) copy = left;
1176 left -= copy;
1177 state->length -= copy;
1178 do {
1179 *put++ = 0;
1180 } while (--copy);
1181 if (state->length == 0) state->mode = LEN;
1182 break;
1183#endif
1184 }
1185 if (copy > state->wnext) {
1186 copy -= state->wnext;
1187 from = state->window + (state->wsize - copy);
1188 }
1189 else
1190 from = state->window + (state->wnext - copy);
1191 if (copy > state->length) copy = state->length;
1192 if (copy > left) copy = left;
1193 put = chunkcopy_safe(put, from, copy, put + left);
1194 }
1195 else { /* copy from output */
1196 copy = state->length;
1197 if (copy > left) copy = left;
1198 put = chunkcopy_lapped_safe(put, state->offset, copy, put + left);
1199 }
1200 left -= copy;
1201 state->length -= copy;
1202 if (state->length == 0) state->mode = LEN;
1203 break;
1204 case LIT:
1205 if (left == 0) goto inf_leave;
1206 *put++ = (unsigned char)(state->length);
1207 left--;
1208 state->mode = LEN;
1209 break;
1210 case CHECK:
1211 if (state->wrap) {
1212 NEEDBITS(32);
1213 out -= left;
1214 strm->total_out += out;
1215 state->total += out;
1216 if ((state->wrap & 4) && out)
1217 strm->adler = state->check =
1218 UPDATE(state->check, put - out, out);
1219 out = left;
1220 if ((state->wrap & 4) && (
1221#ifdef GUNZIP
1222 state->flags ? hold :
1223#endif
1224 ZSWAP32(hold)) != state->check) {
1225 strm->msg = (char *)"incorrect data check";
1226 state->mode = BAD;
1227 break;
1228 }
1229 INITBITS();
1230 Tracev((stderr, "inflate: check matches trailer\n"));
1231 }
1232#ifdef GUNZIP
1233 state->mode = LENGTH;
1234 case LENGTH:
1235 if (state->wrap && state->flags) {
1236 NEEDBITS(32);
1237 if (hold != (state->total & 0xffffffffUL)) {
1238 strm->msg = (char *)"incorrect length check";
1239 state->mode = BAD;
1240 break;
1241 }
1242 INITBITS();
1243 Tracev((stderr, "inflate: length matches trailer\n"));
1244 }
1245#endif
1246 state->mode = DONE;
1247 case DONE:
1248 ret = Z_STREAM_END;
1249 goto inf_leave;
1250 case BAD:
1251 ret = Z_DATA_ERROR;
1252 goto inf_leave;
1253 case MEM:
1254 return Z_MEM_ERROR;
1255 case SYNC:
1256 default:
1257 return Z_STREAM_ERROR;
1258 }
1259
1260 /*
1261 Return from inflate(), updating the total counts and the check value.
1262 If there was no progress during the inflate() call, return a buffer
1263 error. Call updatewindow() to create and/or update the window state.
1264 Note: a memory error from inflate() is non-recoverable.
1265 */
1266 inf_leave:
1267 /* We write a defined value in the unused space to help mark
1268 * where the stream has ended. We don't use zeros as that can
1269 * mislead clients relying on undefined behavior (i.e. assuming
1270 * that the data is over when the buffer has a zero/null value).
1271 */
1272 if (left >= CHUNKCOPY_CHUNK_SIZE)
1273 memset(put, 0x55, CHUNKCOPY_CHUNK_SIZE);
1274 else
1275 memset(put, 0x55, left);
1276
1277 RESTORE();
1278 if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1279 (state->mode < CHECK || flush != Z_FINISH)))
1280 if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
1281 state->mode = MEM;
1282 return Z_MEM_ERROR;
1283 }
1284 in -= strm->avail_in;
1285 out -= strm->avail_out;
1286 strm->total_in += in;
1287 strm->total_out += out;
1288 state->total += out;
1289 if ((state->wrap & 4) && out)
1290 strm->adler = state->check =
1291 UPDATE(state->check, strm->next_out - out, out);
1292 strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1293 (state->mode == TYPE ? 128 : 0) +
1294 (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1295 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1296 ret = Z_BUF_ERROR;
1297 return ret;
1298}
1299
1300int ZEXPORT inflateEnd(strm)
1301z_streamp strm;
1302{
1303 struct inflate_state FAR *state;
1304 if (inflateStateCheck(strm))
1305 return Z_STREAM_ERROR;
1306 state = (struct inflate_state FAR *)strm->state;
1307 if (state->window != Z_NULL) ZFREE(strm, state->window);
1308 ZFREE(strm, strm->state);
1309 strm->state = Z_NULL;
1310 Tracev((stderr, "inflate: end\n"));
1311 return Z_OK;
1312}
1313
1314int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength)
1315z_streamp strm;
1316Bytef *dictionary;
1317uInt *dictLength;
1318{
1319 struct inflate_state FAR *state;
1320
1321 /* check state */
1322 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1323 state = (struct inflate_state FAR *)strm->state;
1324
1325 /* copy dictionary */
1326 if (state->whave && dictionary != Z_NULL) {
1327 zmemcpy(dictionary, state->window + state->wnext,
1328 state->whave - state->wnext);
1329 zmemcpy(dictionary + state->whave - state->wnext,
1330 state->window, state->wnext);
1331 }
1332 if (dictLength != Z_NULL)
1333 *dictLength = state->whave;
1334 return Z_OK;
1335}
1336
1337int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
1338z_streamp strm;
1339const Bytef *dictionary;
1340uInt dictLength;
1341{
1342 struct inflate_state FAR *state;
1343 unsigned long dictid;
1344 int ret;
1345
1346 /* check state */
1347 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1348 state = (struct inflate_state FAR *)strm->state;
1349 if (state->wrap != 0 && state->mode != DICT)
1350 return Z_STREAM_ERROR;
1351
1352 /* check for correct dictionary identifier */
1353 if (state->mode == DICT) {
1354 dictid = adler32(0L, Z_NULL, 0);
1355 dictid = adler32(dictid, dictionary, dictLength);
1356 if (dictid != state->check)
1357 return Z_DATA_ERROR;
1358 }
1359
1360 /* copy dictionary to window using updatewindow(), which will amend the
1361 existing dictionary if appropriate */
1362 ret = updatewindow(strm, dictionary + dictLength, dictLength);
1363 if (ret) {
1364 state->mode = MEM;
1365 return Z_MEM_ERROR;
1366 }
1367 state->havedict = 1;
1368 Tracev((stderr, "inflate: dictionary set\n"));
1369 return Z_OK;
1370}
1371
1372int ZEXPORT inflateGetHeader(strm, head)
1373z_streamp strm;
1374gz_headerp head;
1375{
1376 struct inflate_state FAR *state;
1377
1378 /* check state */
1379 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1380 state = (struct inflate_state FAR *)strm->state;
1381 if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
1382
1383 /* save header structure */
1384 state->head = head;
1385 head->done = 0;
1386 return Z_OK;
1387}
1388
1389/*
1390 Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
1391 or when out of input. When called, *have is the number of pattern bytes
1392 found in order so far, in 0..3. On return *have is updated to the new
1393 state. If on return *have equals four, then the pattern was found and the
1394 return value is how many bytes were read including the last byte of the
1395 pattern. If *have is less than four, then the pattern has not been found
1396 yet and the return value is len. In the latter case, syncsearch() can be
1397 called again with more data and the *have state. *have is initialized to
1398 zero for the first call.
1399 */
1400local unsigned syncsearch(have, buf, len)
1401unsigned FAR *have;
1402const unsigned char FAR *buf;
1403unsigned len;
1404{
1405 unsigned got;
1406 unsigned next;
1407
1408 got = *have;
1409 next = 0;
1410 while (next < len && got < 4) {
1411 if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1412 got++;
1413 else if (buf[next])
1414 got = 0;
1415 else
1416 got = 4 - got;
1417 next++;
1418 }
1419 *have = got;
1420 return next;
1421}
1422
1423int ZEXPORT inflateSync(strm)
1424z_streamp strm;
1425{
1426 unsigned len; /* number of bytes to look at or looked at */
1427 unsigned long in, out; /* temporary to save total_in and total_out */
1428 unsigned char buf[4]; /* to restore bit buffer to byte string */
1429 struct inflate_state FAR *state;
1430
1431 /* check parameters */
1432 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1433 state = (struct inflate_state FAR *)strm->state;
1434 if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
1435
1436 /* if first time, start search in bit buffer */
1437 if (state->mode != SYNC) {
1438 state->mode = SYNC;
1439 state->hold <<= state->bits & 7;
1440 state->bits -= state->bits & 7;
1441 len = 0;
1442 while (state->bits >= 8) {
1443 buf[len++] = (unsigned char)(state->hold);
1444 state->hold >>= 8;
1445 state->bits -= 8;
1446 }
1447 state->have = 0;
1448 syncsearch(&(state->have), buf, len);
1449 }
1450
1451 /* search available input */
1452 len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1453 strm->avail_in -= len;
1454 strm->next_in += len;
1455 strm->total_in += len;
1456
1457 /* return no joy or set up to restart inflate() on a new block */
1458 if (state->have != 4) return Z_DATA_ERROR;
1459 in = strm->total_in; out = strm->total_out;
1460 inflateReset(strm);
1461 strm->total_in = in; strm->total_out = out;
1462 state->mode = TYPE;
1463 return Z_OK;
1464}
1465
1466/*
1467 Returns true if inflate is currently at the end of a block generated by
1468 Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1469 implementation to provide an additional safety check. PPP uses
1470 Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1471 block. When decompressing, PPP checks that at the end of input packet,
1472 inflate is waiting for these length bytes.
1473 */
1474int ZEXPORT inflateSyncPoint(strm)
1475z_streamp strm;
1476{
1477 struct inflate_state FAR *state;
1478
1479 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1480 state = (struct inflate_state FAR *)strm->state;
1481 return state->mode == STORED && state->bits == 0;
1482}
1483
1484int ZEXPORT inflateCopy(dest, source)
1485z_streamp dest;
1486z_streamp source;
1487{
1488 struct inflate_state FAR *state;
1489 struct inflate_state FAR *copy;
1490 unsigned char FAR *window;
1491 unsigned wsize;
1492
1493 /* check input */
1494 if (inflateStateCheck(source) || dest == Z_NULL)
1495 return Z_STREAM_ERROR;
1496 state = (struct inflate_state FAR *)source->state;
1497
1498 /* allocate space */
1499 copy = (struct inflate_state FAR *)
1500 ZALLOC(source, 1, sizeof(struct inflate_state));
1501 if (copy == Z_NULL) return Z_MEM_ERROR;
1502 window = Z_NULL;
1503 if (state->window != Z_NULL) {
1504 window = (unsigned char FAR *)
1505 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
1506 if (window == Z_NULL) {
1507 ZFREE(source, copy);
1508 return Z_MEM_ERROR;
1509 }
1510 }
1511
1512 /* copy state */
1513 zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1514 zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
1515 copy->strm = dest;
1516 if (state->lencode >= state->codes &&
1517 state->lencode <= state->codes + ENOUGH - 1) {
1518 copy->lencode = copy->codes + (state->lencode - state->codes);
1519 copy->distcode = copy->codes + (state->distcode - state->codes);
1520 }
1521 copy->next = copy->codes + (state->next - state->codes);
1522 if (window != Z_NULL) {
1523 wsize = 1U << state->wbits;
1524 zmemcpy(window, state->window, wsize);
1525 }
1526 copy->window = window;
1527 dest->state = (struct internal_state FAR *)copy;
1528 return Z_OK;
1529}
1530
1531int ZEXPORT inflateUndermine(strm, subvert)
1532z_streamp strm;
1533int subvert;
1534{
1535 struct inflate_state FAR *state;
1536
1537 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1538 state = (struct inflate_state FAR *)strm->state;
1539#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1540 state->sane = !subvert;
1541 return Z_OK;
1542#else
1543 (void)subvert;
1544 state->sane = 1;
1545 return Z_DATA_ERROR;
1546#endif
1547}
1548
1549int ZEXPORT inflateValidate(strm, check)
1550z_streamp strm;
1551int check;
1552{
1553 struct inflate_state FAR *state;
1554
1555 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1556 state = (struct inflate_state FAR *)strm->state;
1557 if (check)
1558 state->wrap |= 4;
1559 else
1560 state->wrap &= ~4;
1561 return Z_OK;
1562}
1563
1564long ZEXPORT inflateMark(strm)
1565z_streamp strm;
1566{
1567 struct inflate_state FAR *state;
1568
1569 if (inflateStateCheck(strm))
1570 return -(1L << 16);
1571 state = (struct inflate_state FAR *)strm->state;
1572 return (long)(((unsigned long)((long)state->back)) << 16) +
1573 (state->mode == COPY ? state->length :
1574 (state->mode == MATCH ? state->was - state->length : 0));
1575}
1576
1577unsigned long ZEXPORT inflateCodesUsed(strm)
1578z_streamp strm;
1579{
1580 struct inflate_state FAR *state;
1581 if (inflateStateCheck(strm)) return (unsigned long)-1;
1582 state = (struct inflate_state FAR *)strm->state;
1583 return (unsigned long)(state->next - state->codes);
1584}