]>
git.saurik.com Git - apple/xnu.git/blob - libkern/zlib/inflate.c
2 * Copyright (c) 2008-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* inflate.c -- zlib decompression
29 * Copyright (C) 1995-2005 Mark Adler
30 * For conditions of distribution and use, see copyright notice in zlib.h
36 * 1.2.beta0 24 Nov 2002
37 * - First version -- complete rewrite of inflate to simplify code, avoid
38 * creation of window when not needed, minimize use of window when it is
39 * needed, make inffast.c even faster, implement gzip decoding, and to
40 * improve code readability and style over the previous zlib inflate code
42 * 1.2.beta1 25 Nov 2002
43 * - Use pointers for available input and output checking in inffast.c
44 * - Remove input and output counters in inffast.c
45 * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
46 * - Remove unnecessary second byte pull from length extra in inffast.c
47 * - Unroll direct copy to three copies per loop in inffast.c
49 * 1.2.beta2 4 Dec 2002
50 * - Change external routine names to reduce potential conflicts
51 * - Correct filename to inffixed.h for fixed tables in inflate.c
52 * - Make hbuf[] unsigned char to match parameter type in inflate.c
53 * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
54 * to avoid negation problem on Alphas (64 bit) in inflate.c
56 * 1.2.beta3 22 Dec 2002
57 * - Add comments on state->bits assertion in inffast.c
58 * - Add comments on op field in inftrees.h
59 * - Fix bug in reuse of allocated window after inflateReset()
60 * - Remove bit fields--back to byte structure for speed
61 * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
62 * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
63 * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
64 * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
65 * - Use local copies of stream next and avail values, as well as local bit
66 * buffer and bit count in inflate()--for speed when inflate_fast() not used
68 * 1.2.beta4 1 Jan 2003
69 * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
70 * - Move a comment on output buffer sizes from inffast.c to inflate.c
71 * - Add comments in inffast.c to introduce the inflate_fast() routine
72 * - Rearrange window copies in inflate_fast() for speed and simplification
73 * - Unroll last copy for window match in inflate_fast()
74 * - Use local copies of window variables in inflate_fast() for speed
75 * - Pull out common write == 0 case for speed in inflate_fast()
76 * - Make op and len in inflate_fast() unsigned for consistency
77 * - Add FAR to lcode and dcode declarations in inflate_fast()
78 * - Simplified bad distance check in inflate_fast()
79 * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
80 * source file infback.c to provide a call-back interface to inflate for
81 * programs like gzip and unzip -- uses window as output buffer to avoid
84 * 1.2.beta5 1 Jan 2003
85 * - Improved inflateBack() interface to allow the caller to provide initial
87 * - Fixed stored blocks bug in inflateBack()
89 * 1.2.beta6 4 Jan 2003
90 * - Added comments in inffast.c on effectiveness of POSTINC
91 * - Typecasting all around to reduce compiler warnings
92 * - Changed loops from while (1) or do {} while (1) to for (;;), again to
93 * make compilers happy
94 * - Changed type of window in inflateBackInit() to unsigned char *
96 * 1.2.beta7 27 Jan 2003
97 * - Changed many types to unsigned or unsigned short to avoid warnings
98 * - Added inflateCopy() function
101 * - Changed inflateBack() interface to provide separate opaque descriptors
102 * for the in() and out() functions
103 * - Changed inflateBack() argument and in_func typedef to swap the length
104 * and buffer address return values for the input function
105 * - Check next_in and next_out for Z_NULL on entry to inflate()
107 * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
111 #include "inftrees.h"
122 /* function prototypes */
123 local
void fixedtables
OF((struct inflate_state FAR
*state
));
124 local
int updatewindow
OF((z_streamp strm
, unsigned out
));
126 void makefixed
OF((void));
128 local
unsigned syncsearch
OF((unsigned FAR
*have
, unsigned char FAR
*buf
,
132 inflateReset(z_streamp strm
)
134 struct inflate_state FAR
*state
;
136 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
) return Z_STREAM_ERROR
;
137 state
= (struct inflate_state FAR
*)strm
->state
;
138 strm
->total_in
= strm
->total_out
= state
->total
= 0;
140 strm
->adler
= 1; /* to support ill-conceived Java test suite */
144 state
->dmax
= 32768U;
145 state
->head
= Z_NULL
;
151 state
->lencode
= state
->distcode
= state
->next
= state
->codes
;
152 Tracev((stderr
, "inflate: reset\n"));
157 inflatePrime(z_streamp strm
, int bits
, int value
)
159 struct inflate_state FAR
*state
;
161 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
) return Z_STREAM_ERROR
;
162 state
= (struct inflate_state FAR
*)strm
->state
;
163 if (bits
> 16 || state
->bits
+ bits
> 32) return Z_STREAM_ERROR
;
164 value
&= (1L << bits
) - 1;
165 state
->hold
+= value
<< state
->bits
;
171 inflateInit2_(z_streamp strm
, int windowBits
, const char *version
,
174 struct inflate_state FAR
*state
;
176 if (version
== Z_NULL
|| version
[0] != ZLIB_VERSION
[0] ||
177 stream_size
!= (int)(sizeof(z_stream
)))
178 return Z_VERSION_ERROR
;
179 if (strm
== Z_NULL
) return Z_STREAM_ERROR
;
180 strm
->msg
= Z_NULL
; /* in case we return an error */
182 if (strm
->zalloc
== (alloc_func
)0) {
183 strm
->zalloc
= zcalloc
;
184 strm
->opaque
= (voidpf
)0;
186 if (strm
->zfree
== (free_func
)0) strm
->zfree
= zcfree
;
187 #endif /* NO_ZCFUNCS */
188 state
= (struct inflate_state FAR
*)
189 ZALLOC(strm
, 1, sizeof(struct inflate_state
));
190 if (state
== Z_NULL
) return Z_MEM_ERROR
;
191 Tracev((stderr
, "inflate: allocated\n"));
192 strm
->state
= (struct internal_state FAR
*)state
;
193 if (windowBits
< 0) {
195 windowBits
= -windowBits
;
198 state
->wrap
= (windowBits
>> 4) + 1;
200 if (windowBits
< 48) windowBits
&= 15;
203 if (windowBits
< 8 || windowBits
> 15) {
205 strm
->state
= Z_NULL
;
206 return Z_STREAM_ERROR
;
208 state
->wbits
= (unsigned)windowBits
;
209 state
->window
= Z_NULL
;
210 return inflateReset(strm
);
214 inflateInit_(z_streamp strm
, const char *version
, int stream_size
)
216 return inflateInit2_(strm
, DEF_WBITS
, version
, stream_size
);
220 Return state with length and distance decoding tables and index sizes set to
221 fixed code decoding. Normally this returns fixed tables from inffixed.h.
222 If BUILDFIXED is defined, then instead this routine builds the tables the
223 first time it's called, and returns those tables the first time and
224 thereafter. This reduces the size of the code by about 2K bytes, in
225 exchange for a little execution time. However, BUILDFIXED should not be
226 used for threaded applications, since the rewriting of the tables and virgin
227 may not be thread-safe.
230 fixedtables(struct inflate_state FAR
*state
)
233 static int virgin
= 1;
234 static code
*lenfix
, *distfix
;
235 static code fixed
[544];
237 /* build fixed huffman tables if first call (may not be thread safe) */
242 /* literal/length table */
244 while (sym
< 144) state
->lens
[sym
++] = 8;
245 while (sym
< 256) state
->lens
[sym
++] = 9;
246 while (sym
< 280) state
->lens
[sym
++] = 7;
247 while (sym
< 288) state
->lens
[sym
++] = 8;
251 inflate_table(LENS
, state
->lens
, 288, &(next
), &(bits
), state
->work
);
255 while (sym
< 32) state
->lens
[sym
++] = 5;
258 inflate_table(DISTS
, state
->lens
, 32, &(next
), &(bits
), state
->work
);
260 /* do this just once */
263 #else /* !BUILDFIXED */
264 # include "inffixed.h"
265 #endif /* BUILDFIXED */
266 state
->lencode
= lenfix
;
268 state
->distcode
= distfix
;
276 Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
277 defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
278 those tables to stdout, which would be piped to inffixed.h. A small program
279 can simply call makefixed to do this:
281 void makefixed(void);
289 Then that can be linked with zlib built with MAKEFIXED defined and run:
297 struct inflate_state state
;
300 puts(" /* inffixed.h -- table for decoding fixed codes");
301 puts(" * Generated automatically by makefixed().");
304 puts(" /* WARNING: this file should *not* be used by applications.");
305 puts(" It is part of the implementation of this library and is");
306 puts(" subject to change. Applications should only use zlib.h.");
310 printf(" static const code lenfix[%u] = {", size
);
313 if ((low
% 7) == 0) printf("\n ");
314 printf("{%u,%u,%d}", state
.lencode
[low
].op
, state
.lencode
[low
].bits
,
315 state
.lencode
[low
].val
);
316 if (++low
== size
) break;
321 printf("\n static const code distfix[%u] = {", size
);
324 if ((low
% 6) == 0) printf("\n ");
325 printf("{%u,%u,%d}", state
.distcode
[low
].op
, state
.distcode
[low
].bits
,
326 state
.distcode
[low
].val
);
327 if (++low
== size
) break;
332 #endif /* MAKEFIXED */
335 Update the window with the last wsize (normally 32K) bytes written before
336 returning. If window does not exist yet, create it. This is only called
337 when a window is already in use, or when output has been written during this
338 inflate call, but the end of the deflate stream has not been reached yet.
339 It is also called to create a window for dictionary data when a dictionary
342 Providing output buffers larger than 32K to inflate() should provide a speed
343 advantage, since only the last 32K of output is copied to the sliding window
344 upon return from inflate(), and since all distances after the first 32K of
345 output will fall in the output data, making match copies simpler and faster.
346 The advantage may be dependent on the size of the processor's data caches.
349 updatewindow(z_streamp strm
, unsigned out
)
351 struct inflate_state FAR
*state
;
354 state
= (struct inflate_state FAR
*)strm
->state
;
356 /* if it hasn't been done already, allocate space for the window */
357 if (state
->window
== Z_NULL
) {
358 state
->window
= (unsigned char FAR
*)
359 ZALLOC(strm
, 1U << state
->wbits
,
360 sizeof(unsigned char));
361 if (state
->window
== Z_NULL
) return 1;
364 /* if window not in use yet, initialize */
365 if (state
->wsize
== 0) {
366 state
->wsize
= 1U << state
->wbits
;
371 /* copy state->wsize or less output bytes into the circular window */
372 copy
= out
- strm
->avail_out
;
373 if (copy
>= state
->wsize
) {
374 zmemcpy(state
->window
, strm
->next_out
- state
->wsize
, state
->wsize
);
376 state
->whave
= state
->wsize
;
379 dist
= state
->wsize
- state
->write
;
380 if (dist
> copy
) dist
= copy
;
381 zmemcpy(state
->window
+ state
->write
, strm
->next_out
- copy
, dist
);
384 zmemcpy(state
->window
, strm
->next_out
- copy
, copy
);
386 state
->whave
= state
->wsize
;
389 state
->write
+= dist
;
390 if (state
->write
== state
->wsize
) state
->write
= 0;
391 if (state
->whave
< state
->wsize
) state
->whave
+= dist
;
397 /* Macros for inflate(): */
399 /* check function to use adler32() for zlib or z_crc32() for gzip */
401 # define UPDATE(check, buf, len) \
402 (state->flags ? z_crc32(check, buf, len) : adler32(check, buf, len))
404 # define UPDATE(check, buf, len) adler32(check, buf, len)
407 /* check macros for header crc */
409 # define CRC2(check, word) \
411 hbuf[0] = (unsigned char)(word); \
412 hbuf[1] = (unsigned char)((word) >> 8); \
413 check = z_crc32(check, hbuf, 2); \
416 # define CRC4(check, word) \
418 hbuf[0] = (unsigned char)(word); \
419 hbuf[1] = (unsigned char)((word) >> 8); \
420 hbuf[2] = (unsigned char)((word) >> 16); \
421 hbuf[3] = (unsigned char)((word) >> 24); \
422 check = z_crc32(check, hbuf, 4); \
426 /* Load registers with state in inflate() for speed */
429 put = strm->next_out; \
430 left = strm->avail_out; \
431 next = strm->next_in; \
432 have = strm->avail_in; \
433 hold = state->hold; \
434 bits = state->bits; \
437 /* Restore state from registers in inflate() */
440 strm->next_out = put; \
441 strm->avail_out = left; \
442 strm->next_in = next; \
443 strm->avail_in = have; \
444 state->hold = hold; \
445 state->bits = bits; \
448 /* Clear the input bit accumulator */
455 /* Get a byte of input into the bit accumulator, or return from inflate()
456 if there is no input available. */
459 if (have == 0) goto inf_leave; \
461 hold += (unsigned long)(*next++) << bits; \
465 /* Assure that there are at least n bits in the bit accumulator. If there is
466 not enough available input to do that, then return from inflate(). */
467 #define NEEDBITS(n) \
469 while (bits < (unsigned)(n)) \
473 /* Return the low n bits of the bit accumulator (n < 16) */
475 ((unsigned)hold & ((1U << (n)) - 1))
477 /* Remove n bits from the bit accumulator */
478 #define DROPBITS(n) \
481 bits -= (unsigned)(n); \
484 /* Remove zero to seven bits as needed to go to a byte boundary */
491 /* Reverse the bytes in a 32-bit value */
493 ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
494 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
497 inflate() uses a state machine to process as much input data and generate as
498 much output data as possible before returning. The state machine is
499 structured roughly as follows:
501 for (;;) switch (state) {
504 if (not enough input data or output space to make progress)
506 ... make progress ...
512 so when inflate() is called again, the same case is attempted again, and
513 if the appropriate resources are provided, the machine proceeds to the
514 next state. The NEEDBITS() macro is usually the way the state evaluates
515 whether it can proceed or should return. NEEDBITS() does the return if
516 the requested bits are not available. The typical use of the BITS macros
520 ... do something with BITS(n) ...
523 where NEEDBITS(n) either returns from inflate() if there isn't enough
524 input left to load n bits into the accumulator, or it continues. BITS(n)
525 gives the low n bits in the accumulator. When done, DROPBITS(n) drops
526 the low n bits off the accumulator. INITBITS() clears the accumulator
527 and sets the number of available bits to zero. BYTEBITS() discards just
528 enough bits to put the accumulator on a byte boundary. After BYTEBITS()
529 and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
531 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
532 if there is no input available. The decoding of variable length codes uses
533 PULLBYTE() directly in order to pull just enough bytes to decode the next
536 Some states loop until they get enough input, making sure that enough
537 state information is maintained to continue the loop where it left off
538 if NEEDBITS() returns in the loop. For example, want, need, and keep
539 would all have to actually be part of the saved state in case NEEDBITS()
543 while (want < need) {
545 keep[want++] = BITS(n);
551 As shown above, if the next state is also the next case, then the break
554 A state may also return if there is not enough output space available to
555 complete that state. Those states are copying stored data, writing a
556 literal byte, and copying a matching string.
558 When returning, a "goto inf_leave" is used to update the total counters,
559 update the check value, and determine whether any progress has been made
560 during that inflate() call in order to return the proper return code.
561 Progress is defined as a change in either strm->avail_in or strm->avail_out.
562 When there is a window, goto inf_leave will update the window with the last
563 output written. If a goto inf_leave occurs in the middle of decompression
564 and there is no window currently, goto inf_leave will create one and copy
565 output to the window for the next call of inflate().
567 In this implementation, the flush parameter of inflate() only affects the
568 return code (per zlib.h). inflate() always writes as much as possible to
569 strm->next_out, given the space available and the provided input--the effect
570 documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
571 the allocation of and copying into a sliding window until necessary, which
572 provides the effect documented in zlib.h for Z_FINISH when the entire input
573 stream available. So the only thing the flush parameter actually does is:
574 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
575 will return Z_BUF_ERROR if it has not reached the end of the stream.
579 inflate(z_streamp strm
, int flush
)
581 struct inflate_state FAR
*state
;
582 unsigned char FAR
*next
; /* next input */
583 unsigned char FAR
*put
; /* next output */
584 unsigned have
, left
; /* available input and output */
585 unsigned long hold
; /* bit buffer */
586 unsigned bits
; /* bits in bit buffer */
587 unsigned in
, out
; /* save starting available input and output */
588 unsigned copy
; /* number of stored or match bytes to copy */
589 unsigned char FAR
*from
; /* where to copy match bytes from */
590 code
this; /* current decoding table entry */
591 code last
; /* parent table entry */
592 unsigned len
; /* length to copy for repeats, bits to drop */
593 int ret
; /* return code */
595 unsigned char hbuf
[4]; /* buffer for gzip header crc calculation */
597 static const unsigned short order
[19] = /* permutation of code lengths */
598 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
600 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
|| strm
->next_out
== Z_NULL
||
601 (strm
->next_in
== Z_NULL
&& strm
->avail_in
!= 0))
602 return Z_STREAM_ERROR
;
604 state
= (struct inflate_state FAR
*)strm
->state
;
605 if (state
->mode
== TYPE
) state
->mode
= TYPEDO
; /* skip check */
611 switch (state
->mode
) {
613 if (state
->wrap
== 0) {
614 state
->mode
= TYPEDO
;
619 if ((state
->wrap
& 2) && hold
== 0x8b1f) { /* gzip header */
620 state
->check
= z_crc32(0L, Z_NULL
, 0);
621 CRC2(state
->check
, hold
);
626 state
->flags
= 0; /* expect zlib header */
627 if (state
->head
!= Z_NULL
)
628 state
->head
->done
= -1;
629 if (!(state
->wrap
& 1) || /* check if zlib header allowed */
633 ((BITS(8) << 8) + (hold
>> 8)) % 31) {
634 strm
->msg
= (char *)"incorrect header check";
638 if (BITS(4) != Z_DEFLATED
) {
639 strm
->msg
= (char *)"unknown compression method";
645 if (len
> state
->wbits
) {
646 strm
->msg
= (char *)"invalid window size";
650 state
->dmax
= 1U << len
;
651 Tracev((stderr
, "inflate: zlib header ok\n"));
652 strm
->adler
= state
->check
= adler32(0L, Z_NULL
, 0);
653 state
->mode
= hold
& 0x200 ? DICTID
: TYPE
;
659 state
->flags
= (int)(hold
);
660 if ((state
->flags
& 0xff) != Z_DEFLATED
) {
661 strm
->msg
= (char *)"unknown compression method";
665 if (state
->flags
& 0xe000) {
666 strm
->msg
= (char *)"unknown header flags set";
670 if (state
->head
!= Z_NULL
)
671 state
->head
->text
= (int)((hold
>> 8) & 1);
672 if (state
->flags
& 0x0200) CRC2(state
->check
, hold
);
678 if (state
->head
!= Z_NULL
)
679 state
->head
->time
= hold
;
680 if (state
->flags
& 0x0200) CRC4(state
->check
, hold
);
686 if (state
->head
!= Z_NULL
) {
687 state
->head
->xflags
= (int)(hold
& 0xff);
688 state
->head
->os
= (int)(hold
>> 8);
690 if (state
->flags
& 0x0200) CRC2(state
->check
, hold
);
695 if (state
->flags
& 0x0400) {
697 state
->length
= (unsigned)(hold
);
698 if (state
->head
!= Z_NULL
)
699 state
->head
->extra_len
= (unsigned)hold
;
700 if (state
->flags
& 0x0200) CRC2(state
->check
, hold
);
703 else if (state
->head
!= Z_NULL
)
704 state
->head
->extra
= Z_NULL
;
708 if (state
->flags
& 0x0400) {
709 copy
= state
->length
;
710 if (copy
> have
) copy
= have
;
712 if (state
->head
!= Z_NULL
&&
713 state
->head
->extra
!= Z_NULL
) {
714 len
= state
->head
->extra_len
- state
->length
;
715 zmemcpy(state
->head
->extra
+ len
, next
,
716 len
+ copy
> state
->head
->extra_max
?
717 state
->head
->extra_max
- len
: copy
);
719 if (state
->flags
& 0x0200)
720 state
->check
= z_crc32(state
->check
, next
, copy
);
723 state
->length
-= copy
;
725 if (state
->length
) goto inf_leave
;
731 if (state
->flags
& 0x0800) {
732 if (have
== 0) goto inf_leave
;
735 len
= (unsigned)(next
[copy
++]);
736 if (state
->head
!= Z_NULL
&&
737 state
->head
->name
!= Z_NULL
&&
738 state
->length
< state
->head
->name_max
)
739 state
->head
->name
[state
->length
++] = (Bytef
)len
;
740 } while (len
&& copy
< have
);
741 if (state
->flags
& 0x0200)
742 state
->check
= z_crc32(state
->check
, next
, copy
);
745 if (len
) goto inf_leave
;
747 else if (state
->head
!= Z_NULL
)
748 state
->head
->name
= Z_NULL
;
750 state
->mode
= COMMENT
;
753 if (state
->flags
& 0x1000) {
754 if (have
== 0) goto inf_leave
;
757 len
= (unsigned)(next
[copy
++]);
758 if (state
->head
!= Z_NULL
&&
759 state
->head
->comment
!= Z_NULL
&&
760 state
->length
< state
->head
->comm_max
)
761 state
->head
->comment
[state
->length
++] = (Bytef
)len
;
762 } while (len
&& copy
< have
);
763 if (state
->flags
& 0x0200)
764 state
->check
= z_crc32(state
->check
, next
, copy
);
767 if (len
) goto inf_leave
;
769 else if (state
->head
!= Z_NULL
)
770 state
->head
->comment
= Z_NULL
;
774 if (state
->flags
& 0x0200) {
776 if (hold
!= (state
->check
& 0xffff)) {
777 strm
->msg
= (char *)"header crc mismatch";
783 if (state
->head
!= Z_NULL
) {
784 state
->head
->hcrc
= (int)((state
->flags
>> 9) & 1);
785 state
->head
->done
= 1;
787 strm
->adler
= state
->check
= z_crc32(0L, Z_NULL
, 0);
795 strm
->adler
= state
->check
= REVERSE(hold
);
800 if (state
->havedict
== 0) {
804 strm
->adler
= state
->check
= adler32(0L, Z_NULL
, 0);
808 if (flush
== Z_BLOCK
) goto inf_leave
;
817 state
->last
= BITS(1);
820 case 0: /* stored block */
821 Tracev((stderr
, "inflate: stored block%s\n",
822 state
->last
? " (last)" : ""));
823 state
->mode
= STORED
;
825 case 1: /* fixed block */
827 Tracev((stderr
, "inflate: fixed codes block%s\n",
828 state
->last
? " (last)" : ""));
829 state
->mode
= LEN
; /* decode codes */
831 case 2: /* dynamic block */
832 Tracev((stderr
, "inflate: dynamic codes block%s\n",
833 state
->last
? " (last)" : ""));
837 strm
->msg
= (char *)"invalid block type";
843 BYTEBITS(); /* go to byte boundary */
845 if ((hold
& 0xffff) != ((hold
>> 16) ^ 0xffff)) {
846 strm
->msg
= (char *)"invalid stored block lengths";
850 state
->length
= (unsigned)hold
& 0xffff;
851 Tracev((stderr
, "inflate: stored length %u\n",
857 copy
= state
->length
;
859 if (copy
> have
) copy
= have
;
860 if (copy
> left
) copy
= left
;
861 if (copy
== 0) goto inf_leave
;
862 zmemcpy(put
, next
, copy
);
867 state
->length
-= copy
;
870 Tracev((stderr
, "inflate: stored end\n"));
875 state
->nlen
= BITS(5) + 257;
877 state
->ndist
= BITS(5) + 1;
879 state
->ncode
= BITS(4) + 4;
881 #ifndef PKZIP_BUG_WORKAROUND
882 if (state
->nlen
> 286 || state
->ndist
> 30) {
883 strm
->msg
= (char *)"too many length or distance symbols";
888 Tracev((stderr
, "inflate: table sizes ok\n"));
890 state
->mode
= LENLENS
;
893 while (state
->have
< state
->ncode
) {
895 state
->lens
[order
[state
->have
++]] = (unsigned short)BITS(3);
898 while (state
->have
< 19)
899 state
->lens
[order
[state
->have
++]] = 0;
900 state
->next
= state
->codes
;
901 state
->lencode
= (code
const FAR
*)(state
->next
);
903 ret
= inflate_table(CODES
, state
->lens
, 19, &(state
->next
),
904 &(state
->lenbits
), state
->work
);
906 strm
->msg
= (char *)"invalid code lengths set";
910 Tracev((stderr
, "inflate: code lengths ok\n"));
912 state
->mode
= CODELENS
;
915 while (state
->have
< state
->nlen
+ state
->ndist
) {
917 this = state
->lencode
[BITS(state
->lenbits
)];
918 if ((unsigned)(this.bits
) <= bits
) break;
924 state
->lens
[state
->have
++] = this.val
;
927 if (this.val
== 16) {
928 NEEDBITS(this.bits
+ 2);
930 if (state
->have
== 0) {
931 strm
->msg
= (char *)"invalid bit length repeat";
935 len
= state
->lens
[state
->have
- 1];
939 else if (this.val
== 17) {
940 NEEDBITS(this.bits
+ 3);
947 NEEDBITS(this.bits
+ 7);
953 if (state
->have
+ copy
> state
->nlen
+ state
->ndist
) {
954 strm
->msg
= (char *)"invalid bit length repeat";
959 state
->lens
[state
->have
++] = (unsigned short)len
;
963 /* handle error breaks in while */
964 if (state
->mode
== BAD
) break;
966 /* build code tables */
967 state
->next
= state
->codes
;
968 state
->lencode
= (code
const FAR
*)(state
->next
);
970 ret
= inflate_table(LENS
, state
->lens
, state
->nlen
, &(state
->next
),
971 &(state
->lenbits
), state
->work
);
973 strm
->msg
= (char *)"invalid literal/lengths set";
977 state
->distcode
= (code
const FAR
*)(state
->next
);
979 ret
= inflate_table(DISTS
, state
->lens
+ state
->nlen
, state
->ndist
,
980 &(state
->next
), &(state
->distbits
), state
->work
);
982 strm
->msg
= (char *)"invalid distances set";
986 Tracev((stderr
, "inflate: codes ok\n"));
990 if (have
>= 6 && left
>= 258) {
992 inflate_fast(strm
, out
);
997 this = state
->lencode
[BITS(state
->lenbits
)];
998 if ((unsigned)(this.bits
) <= bits
) break;
1001 if (this.op
&& (this.op
& 0xf0) == 0) {
1004 this = state
->lencode
[last
.val
+
1005 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
1006 if ((unsigned)(last
.bits
+ this.bits
) <= bits
) break;
1009 DROPBITS(last
.bits
);
1011 DROPBITS(this.bits
);
1012 state
->length
= (unsigned)this.val
;
1013 if ((int)(this.op
) == 0) {
1014 Tracevv((stderr
, this.val
>= 0x20 && this.val
< 0x7f ?
1015 "inflate: literal '%c'\n" :
1016 "inflate: literal 0x%02x\n", this.val
));
1021 Tracevv((stderr
, "inflate: end of block\n"));
1026 strm
->msg
= (char *)"invalid literal/length code";
1030 state
->extra
= (unsigned)(this.op
) & 15;
1031 state
->mode
= LENEXT
;
1035 NEEDBITS(state
->extra
);
1036 state
->length
+= BITS(state
->extra
);
1037 DROPBITS(state
->extra
);
1039 Tracevv((stderr
, "inflate: length %u\n", state
->length
));
1044 this = state
->distcode
[BITS(state
->distbits
)];
1045 if ((unsigned)(this.bits
) <= bits
) break;
1048 if ((this.op
& 0xf0) == 0) {
1051 this = state
->distcode
[last
.val
+
1052 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
1053 if ((unsigned)(last
.bits
+ this.bits
) <= bits
) break;
1056 DROPBITS(last
.bits
);
1058 DROPBITS(this.bits
);
1060 strm
->msg
= (char *)"invalid distance code";
1064 state
->offset
= (unsigned)this.val
;
1065 state
->extra
= (unsigned)(this.op
) & 15;
1066 state
->mode
= DISTEXT
;
1070 NEEDBITS(state
->extra
);
1071 state
->offset
+= BITS(state
->extra
);
1072 DROPBITS(state
->extra
);
1074 #ifdef INFLATE_STRICT
1075 if (state
->offset
> state
->dmax
) {
1076 strm
->msg
= (char *)"invalid distance too far back";
1081 if (state
->offset
> state
->whave
+ out
- left
) {
1082 strm
->msg
= (char *)"invalid distance too far back";
1086 Tracevv((stderr
, "inflate: distance %u\n", state
->offset
));
1087 state
->mode
= MATCH
;
1090 if (left
== 0) goto inf_leave
;
1092 if (state
->offset
> copy
) { /* copy from window */
1093 copy
= state
->offset
- copy
;
1094 if (copy
> state
->write
) {
1095 copy
-= state
->write
;
1096 from
= state
->window
+ (state
->wsize
- copy
);
1099 from
= state
->window
+ (state
->write
- copy
);
1100 if (copy
> state
->length
) copy
= state
->length
;
1102 else { /* copy from output */
1103 from
= put
- state
->offset
;
1104 copy
= state
->length
;
1106 if (copy
> left
) copy
= left
;
1108 state
->length
-= copy
;
1112 if (state
->length
== 0) state
->mode
= LEN
;
1115 if (left
== 0) goto inf_leave
;
1116 *put
++ = (unsigned char)(state
->length
);
1124 strm
->total_out
+= out
;
1125 state
->total
+= out
;
1127 strm
->adler
= state
->check
=
1128 UPDATE(state
->check
, put
- out
, out
);
1132 state
->flags
? hold
:
1134 REVERSE(hold
)) != state
->check
) {
1135 strm
->msg
= (char *)"incorrect data check";
1140 Tracev((stderr
, "inflate: check matches trailer\n"));
1143 state
->mode
= LENGTH
;
1146 if (state
->wrap
&& state
->flags
) {
1148 if (hold
!= (state
->total
& 0xffffffffUL
)) {
1149 strm
->msg
= (char *)"incorrect length check";
1154 Tracev((stderr
, "inflate: length matches trailer\n"));
1169 return Z_STREAM_ERROR
;
1173 Return from inflate(), updating the total counts and the check value.
1174 If there was no progress during the inflate() call, return a buffer
1175 error. Call updatewindow() to create and/or update the window state.
1176 Note: a memory error from inflate() is non-recoverable.
1180 if (state
->wsize
|| (state
->mode
< CHECK
&& out
!= strm
->avail_out
))
1181 if (updatewindow(strm
, out
)) {
1185 in
-= strm
->avail_in
;
1186 out
-= strm
->avail_out
;
1187 strm
->total_in
+= in
;
1188 strm
->total_out
+= out
;
1189 state
->total
+= out
;
1190 if (state
->wrap
&& out
)
1191 strm
->adler
= state
->check
=
1192 UPDATE(state
->check
, strm
->next_out
- out
, out
);
1193 strm
->data_type
= state
->bits
+ (state
->last
? 64 : 0) +
1194 (state
->mode
== TYPE
? 128 : 0);
1195 if (((in
== 0 && out
== 0) || flush
== Z_FINISH
) && ret
== Z_OK
)
1201 inflateEnd(z_streamp strm
)
1203 struct inflate_state FAR
*state
;
1204 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
|| strm
->zfree
== (free_func
)0)
1205 return Z_STREAM_ERROR
;
1206 state
= (struct inflate_state FAR
*)strm
->state
;
1207 if (state
->window
!= Z_NULL
) ZFREE(strm
, state
->window
);
1208 ZFREE(strm
, strm
->state
);
1209 strm
->state
= Z_NULL
;
1210 Tracev((stderr
, "inflate: end\n"));
1215 inflateSetDictionary(z_streamp strm
, const Bytef
*dictionary
, uInt dictLength
)
1217 struct inflate_state FAR
*state
;
1221 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
) return Z_STREAM_ERROR
;
1222 state
= (struct inflate_state FAR
*)strm
->state
;
1223 if (state
->wrap
!= 0 && state
->mode
!= DICT
)
1224 return Z_STREAM_ERROR
;
1226 /* check for correct dictionary id */
1227 if (state
->mode
== DICT
) {
1228 id
= adler32(0L, Z_NULL
, 0);
1229 id
= adler32(id
, dictionary
, dictLength
);
1230 if (id
!= state
->check
)
1231 return Z_DATA_ERROR
;
1234 /* copy dictionary to window */
1235 if (updatewindow(strm
, strm
->avail_out
)) {
1239 if (dictLength
> state
->wsize
) {
1240 zmemcpy(state
->window
, dictionary
+ dictLength
- state
->wsize
,
1242 state
->whave
= state
->wsize
;
1245 zmemcpy(state
->window
+ state
->wsize
- dictLength
, dictionary
,
1247 state
->whave
= dictLength
;
1249 state
->havedict
= 1;
1250 Tracev((stderr
, "inflate: dictionary set\n"));
1255 inflateGetHeader(z_streamp strm
, gz_headerp head
)
1257 struct inflate_state FAR
*state
;
1260 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
) return Z_STREAM_ERROR
;
1261 state
= (struct inflate_state FAR
*)strm
->state
;
1262 if ((state
->wrap
& 2) == 0) return Z_STREAM_ERROR
;
1264 /* save header structure */
1271 Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
1272 or when out of input. When called, *have is the number of pattern bytes
1273 found in order so far, in 0..3. On return *have is updated to the new
1274 state. If on return *have equals four, then the pattern was found and the
1275 return value is how many bytes were read including the last byte of the
1276 pattern. If *have is less than four, then the pattern has not been found
1277 yet and the return value is len. In the latter case, syncsearch() can be
1278 called again with more data and the *have state. *have is initialized to
1279 zero for the first call.
1282 syncsearch(unsigned FAR
*have
, unsigned char FAR
*buf
, unsigned len
)
1289 while (next
< len
&& got
< 4) {
1290 if ((int)(buf
[next
]) == (got
< 2 ? 0 : 0xff))
1303 inflateSync(z_streamp strm
)
1305 unsigned len
; /* number of bytes to look at or looked at */
1306 unsigned long in
, out
; /* temporary to save total_in and total_out */
1307 unsigned char buf
[4]; /* to restore bit buffer to byte string */
1308 struct inflate_state FAR
*state
;
1310 /* check parameters */
1311 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
) return Z_STREAM_ERROR
;
1312 state
= (struct inflate_state FAR
*)strm
->state
;
1313 if (strm
->avail_in
== 0 && state
->bits
< 8) return Z_BUF_ERROR
;
1315 /* if first time, start search in bit buffer */
1316 if (state
->mode
!= SYNC
) {
1318 state
->hold
<<= state
->bits
& 7;
1319 state
->bits
-= state
->bits
& 7;
1321 while (state
->bits
>= 8) {
1322 buf
[len
++] = (unsigned char)(state
->hold
);
1327 syncsearch(&(state
->have
), buf
, len
);
1330 /* search available input */
1331 len
= syncsearch(&(state
->have
), strm
->next_in
, strm
->avail_in
);
1332 strm
->avail_in
-= len
;
1333 strm
->next_in
+= len
;
1334 strm
->total_in
+= len
;
1336 /* return no joy or set up to restart inflate() on a new block */
1337 if (state
->have
!= 4) return Z_DATA_ERROR
;
1338 in
= strm
->total_in
; out
= strm
->total_out
;
1340 strm
->total_in
= in
; strm
->total_out
= out
;
1346 Returns true if inflate is currently at the end of a block generated by
1347 Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1348 implementation to provide an additional safety check. PPP uses
1349 Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1350 block. When decompressing, PPP checks that at the end of input packet,
1351 inflate is waiting for these length bytes.
1354 inflateSyncPoint(z_streamp strm
)
1356 struct inflate_state FAR
*state
;
1358 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
) return Z_STREAM_ERROR
;
1359 state
= (struct inflate_state FAR
*)strm
->state
;
1360 return state
->mode
== STORED
&& state
->bits
== 0;
1364 inflateCopy(z_streamp dest
, z_streamp source
)
1366 struct inflate_state FAR
*state
;
1367 struct inflate_state FAR
*copy
;
1368 unsigned char FAR
*window
;
1372 if (dest
== Z_NULL
|| source
== Z_NULL
|| source
->state
== Z_NULL
||
1373 source
->zalloc
== (alloc_func
)0 || source
->zfree
== (free_func
)0)
1374 return Z_STREAM_ERROR
;
1375 state
= (struct inflate_state FAR
*)source
->state
;
1377 /* allocate space */
1378 copy
= (struct inflate_state FAR
*)
1379 ZALLOC(source
, 1, sizeof(struct inflate_state
));
1380 if (copy
== Z_NULL
) return Z_MEM_ERROR
;
1382 if (state
->window
!= Z_NULL
) {
1383 window
= (unsigned char FAR
*)
1384 ZALLOC(source
, 1U << state
->wbits
, sizeof(unsigned char));
1385 if (window
== Z_NULL
) {
1386 ZFREE(source
, copy
);
1392 zmemcpy(dest
, source
, sizeof(z_stream
));
1393 zmemcpy(copy
, state
, sizeof(struct inflate_state
));
1394 if (state
->lencode
>= state
->codes
&&
1395 state
->lencode
<= state
->codes
+ ENOUGH
- 1) {
1396 copy
->lencode
= copy
->codes
+ (state
->lencode
- state
->codes
);
1397 copy
->distcode
= copy
->codes
+ (state
->distcode
- state
->codes
);
1399 copy
->next
= copy
->codes
+ (state
->next
- state
->codes
);
1400 if (window
!= Z_NULL
) {
1401 wsize
= 1U << state
->wbits
;
1402 zmemcpy(window
, state
->window
, wsize
);
1404 copy
->window
= window
;
1405 dest
->state
= (struct internal_state FAR
*)copy
;