]>
git.saurik.com Git - apple/xnu.git/blob - libkern/zlib/inflate.c
2 * Copyright (c) 2008 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"
121 /* function prototypes */
122 local
void fixedtables
OF((struct inflate_state FAR
*state
));
123 local
int updatewindow
OF((z_streamp strm
, unsigned out
));
125 void makefixed
OF((void));
127 local
unsigned syncsearch
OF((unsigned FAR
*have
, unsigned char FAR
*buf
,
131 inflateReset(z_streamp strm
)
133 struct inflate_state FAR
*state
;
135 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
) return Z_STREAM_ERROR
;
136 state
= (struct inflate_state FAR
*)strm
->state
;
137 strm
->total_in
= strm
->total_out
= state
->total
= 0;
139 strm
->adler
= 1; /* to support ill-conceived Java test suite */
143 state
->dmax
= 32768U;
144 state
->head
= Z_NULL
;
150 state
->lencode
= state
->distcode
= state
->next
= state
->codes
;
151 Tracev((stderr
, "inflate: reset\n"));
156 inflatePrime(z_streamp strm
, int bits
, int value
)
158 struct inflate_state FAR
*state
;
160 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
) return Z_STREAM_ERROR
;
161 state
= (struct inflate_state FAR
*)strm
->state
;
162 if (bits
> 16 || state
->bits
+ bits
> 32) return Z_STREAM_ERROR
;
163 value
&= (1L << bits
) - 1;
164 state
->hold
+= value
<< state
->bits
;
170 inflateInit2_(z_streamp strm
, int windowBits
, const char *version
,
173 struct inflate_state FAR
*state
;
175 if (version
== Z_NULL
|| version
[0] != ZLIB_VERSION
[0] ||
176 stream_size
!= (int)(sizeof(z_stream
)))
177 return Z_VERSION_ERROR
;
178 if (strm
== Z_NULL
) return Z_STREAM_ERROR
;
179 strm
->msg
= Z_NULL
; /* in case we return an error */
181 if (strm
->zalloc
== (alloc_func
)0) {
182 strm
->zalloc
= zcalloc
;
183 strm
->opaque
= (voidpf
)0;
185 if (strm
->zfree
== (free_func
)0) strm
->zfree
= zcfree
;
186 #endif /* NO_ZCFUNCS */
187 state
= (struct inflate_state FAR
*)
188 ZALLOC(strm
, 1, sizeof(struct inflate_state
));
189 if (state
== Z_NULL
) return Z_MEM_ERROR
;
190 Tracev((stderr
, "inflate: allocated\n"));
191 strm
->state
= (struct internal_state FAR
*)state
;
192 if (windowBits
< 0) {
194 windowBits
= -windowBits
;
197 state
->wrap
= (windowBits
>> 4) + 1;
199 if (windowBits
< 48) windowBits
&= 15;
202 if (windowBits
< 8 || windowBits
> 15) {
204 strm
->state
= Z_NULL
;
205 return Z_STREAM_ERROR
;
207 state
->wbits
= (unsigned)windowBits
;
208 state
->window
= Z_NULL
;
209 return inflateReset(strm
);
213 inflateInit_(z_streamp strm
, const char *version
, int stream_size
)
215 return inflateInit2_(strm
, DEF_WBITS
, version
, stream_size
);
219 Return state with length and distance decoding tables and index sizes set to
220 fixed code decoding. Normally this returns fixed tables from inffixed.h.
221 If BUILDFIXED is defined, then instead this routine builds the tables the
222 first time it's called, and returns those tables the first time and
223 thereafter. This reduces the size of the code by about 2K bytes, in
224 exchange for a little execution time. However, BUILDFIXED should not be
225 used for threaded applications, since the rewriting of the tables and virgin
226 may not be thread-safe.
229 fixedtables(struct inflate_state FAR
*state
)
232 static int virgin
= 1;
233 static code
*lenfix
, *distfix
;
234 static code fixed
[544];
236 /* build fixed huffman tables if first call (may not be thread safe) */
241 /* literal/length table */
243 while (sym
< 144) state
->lens
[sym
++] = 8;
244 while (sym
< 256) state
->lens
[sym
++] = 9;
245 while (sym
< 280) state
->lens
[sym
++] = 7;
246 while (sym
< 288) state
->lens
[sym
++] = 8;
250 inflate_table(LENS
, state
->lens
, 288, &(next
), &(bits
), state
->work
);
254 while (sym
< 32) state
->lens
[sym
++] = 5;
257 inflate_table(DISTS
, state
->lens
, 32, &(next
), &(bits
), state
->work
);
259 /* do this just once */
262 #else /* !BUILDFIXED */
263 # include "inffixed.h"
264 #endif /* BUILDFIXED */
265 state
->lencode
= lenfix
;
267 state
->distcode
= distfix
;
275 Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
276 defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
277 those tables to stdout, which would be piped to inffixed.h. A small program
278 can simply call makefixed to do this:
280 void makefixed(void);
288 Then that can be linked with zlib built with MAKEFIXED defined and run:
296 struct inflate_state state
;
299 puts(" /* inffixed.h -- table for decoding fixed codes");
300 puts(" * Generated automatically by makefixed().");
303 puts(" /* WARNING: this file should *not* be used by applications.");
304 puts(" It is part of the implementation of this library and is");
305 puts(" subject to change. Applications should only use zlib.h.");
309 printf(" static const code lenfix[%u] = {", size
);
312 if ((low
% 7) == 0) printf("\n ");
313 printf("{%u,%u,%d}", state
.lencode
[low
].op
, state
.lencode
[low
].bits
,
314 state
.lencode
[low
].val
);
315 if (++low
== size
) break;
320 printf("\n static const code distfix[%u] = {", size
);
323 if ((low
% 6) == 0) printf("\n ");
324 printf("{%u,%u,%d}", state
.distcode
[low
].op
, state
.distcode
[low
].bits
,
325 state
.distcode
[low
].val
);
326 if (++low
== size
) break;
331 #endif /* MAKEFIXED */
334 Update the window with the last wsize (normally 32K) bytes written before
335 returning. If window does not exist yet, create it. This is only called
336 when a window is already in use, or when output has been written during this
337 inflate call, but the end of the deflate stream has not been reached yet.
338 It is also called to create a window for dictionary data when a dictionary
341 Providing output buffers larger than 32K to inflate() should provide a speed
342 advantage, since only the last 32K of output is copied to the sliding window
343 upon return from inflate(), and since all distances after the first 32K of
344 output will fall in the output data, making match copies simpler and faster.
345 The advantage may be dependent on the size of the processor's data caches.
348 updatewindow(z_streamp strm
, unsigned out
)
350 struct inflate_state FAR
*state
;
353 state
= (struct inflate_state FAR
*)strm
->state
;
355 /* if it hasn't been done already, allocate space for the window */
356 if (state
->window
== Z_NULL
) {
357 state
->window
= (unsigned char FAR
*)
358 ZALLOC(strm
, 1U << state
->wbits
,
359 sizeof(unsigned char));
360 if (state
->window
== Z_NULL
) return 1;
363 /* if window not in use yet, initialize */
364 if (state
->wsize
== 0) {
365 state
->wsize
= 1U << state
->wbits
;
370 /* copy state->wsize or less output bytes into the circular window */
371 copy
= out
- strm
->avail_out
;
372 if (copy
>= state
->wsize
) {
373 zmemcpy(state
->window
, strm
->next_out
- state
->wsize
, state
->wsize
);
375 state
->whave
= state
->wsize
;
378 dist
= state
->wsize
- state
->write
;
379 if (dist
> copy
) dist
= copy
;
380 zmemcpy(state
->window
+ state
->write
, strm
->next_out
- copy
, dist
);
383 zmemcpy(state
->window
, strm
->next_out
- copy
, copy
);
385 state
->whave
= state
->wsize
;
388 state
->write
+= dist
;
389 if (state
->write
== state
->wsize
) state
->write
= 0;
390 if (state
->whave
< state
->wsize
) state
->whave
+= dist
;
396 /* Macros for inflate(): */
398 /* check function to use adler32() for zlib or z_crc32() for gzip */
400 # define UPDATE(check, buf, len) \
401 (state->flags ? z_crc32(check, buf, len) : adler32(check, buf, len))
403 # define UPDATE(check, buf, len) adler32(check, buf, len)
406 /* check macros for header crc */
408 # define CRC2(check, word) \
410 hbuf[0] = (unsigned char)(word); \
411 hbuf[1] = (unsigned char)((word) >> 8); \
412 check = z_crc32(check, hbuf, 2); \
415 # define CRC4(check, word) \
417 hbuf[0] = (unsigned char)(word); \
418 hbuf[1] = (unsigned char)((word) >> 8); \
419 hbuf[2] = (unsigned char)((word) >> 16); \
420 hbuf[3] = (unsigned char)((word) >> 24); \
421 check = z_crc32(check, hbuf, 4); \
425 /* Load registers with state in inflate() for speed */
428 put = strm->next_out; \
429 left = strm->avail_out; \
430 next = strm->next_in; \
431 have = strm->avail_in; \
432 hold = state->hold; \
433 bits = state->bits; \
436 /* Restore state from registers in inflate() */
439 strm->next_out = put; \
440 strm->avail_out = left; \
441 strm->next_in = next; \
442 strm->avail_in = have; \
443 state->hold = hold; \
444 state->bits = bits; \
447 /* Clear the input bit accumulator */
454 /* Get a byte of input into the bit accumulator, or return from inflate()
455 if there is no input available. */
458 if (have == 0) goto inf_leave; \
460 hold += (unsigned long)(*next++) << bits; \
464 /* Assure that there are at least n bits in the bit accumulator. If there is
465 not enough available input to do that, then return from inflate(). */
466 #define NEEDBITS(n) \
468 while (bits < (unsigned)(n)) \
472 /* Return the low n bits of the bit accumulator (n < 16) */
474 ((unsigned)hold & ((1U << (n)) - 1))
476 /* Remove n bits from the bit accumulator */
477 #define DROPBITS(n) \
480 bits -= (unsigned)(n); \
483 /* Remove zero to seven bits as needed to go to a byte boundary */
490 /* Reverse the bytes in a 32-bit value */
492 ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
493 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
496 inflate() uses a state machine to process as much input data and generate as
497 much output data as possible before returning. The state machine is
498 structured roughly as follows:
500 for (;;) switch (state) {
503 if (not enough input data or output space to make progress)
505 ... make progress ...
511 so when inflate() is called again, the same case is attempted again, and
512 if the appropriate resources are provided, the machine proceeds to the
513 next state. The NEEDBITS() macro is usually the way the state evaluates
514 whether it can proceed or should return. NEEDBITS() does the return if
515 the requested bits are not available. The typical use of the BITS macros
519 ... do something with BITS(n) ...
522 where NEEDBITS(n) either returns from inflate() if there isn't enough
523 input left to load n bits into the accumulator, or it continues. BITS(n)
524 gives the low n bits in the accumulator. When done, DROPBITS(n) drops
525 the low n bits off the accumulator. INITBITS() clears the accumulator
526 and sets the number of available bits to zero. BYTEBITS() discards just
527 enough bits to put the accumulator on a byte boundary. After BYTEBITS()
528 and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
530 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
531 if there is no input available. The decoding of variable length codes uses
532 PULLBYTE() directly in order to pull just enough bytes to decode the next
535 Some states loop until they get enough input, making sure that enough
536 state information is maintained to continue the loop where it left off
537 if NEEDBITS() returns in the loop. For example, want, need, and keep
538 would all have to actually be part of the saved state in case NEEDBITS()
542 while (want < need) {
544 keep[want++] = BITS(n);
550 As shown above, if the next state is also the next case, then the break
553 A state may also return if there is not enough output space available to
554 complete that state. Those states are copying stored data, writing a
555 literal byte, and copying a matching string.
557 When returning, a "goto inf_leave" is used to update the total counters,
558 update the check value, and determine whether any progress has been made
559 during that inflate() call in order to return the proper return code.
560 Progress is defined as a change in either strm->avail_in or strm->avail_out.
561 When there is a window, goto inf_leave will update the window with the last
562 output written. If a goto inf_leave occurs in the middle of decompression
563 and there is no window currently, goto inf_leave will create one and copy
564 output to the window for the next call of inflate().
566 In this implementation, the flush parameter of inflate() only affects the
567 return code (per zlib.h). inflate() always writes as much as possible to
568 strm->next_out, given the space available and the provided input--the effect
569 documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
570 the allocation of and copying into a sliding window until necessary, which
571 provides the effect documented in zlib.h for Z_FINISH when the entire input
572 stream available. So the only thing the flush parameter actually does is:
573 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
574 will return Z_BUF_ERROR if it has not reached the end of the stream.
578 inflate(z_streamp strm
, int flush
)
580 struct inflate_state FAR
*state
;
581 unsigned char FAR
*next
; /* next input */
582 unsigned char FAR
*put
; /* next output */
583 unsigned have
, left
; /* available input and output */
584 unsigned long hold
; /* bit buffer */
585 unsigned bits
; /* bits in bit buffer */
586 unsigned in
, out
; /* save starting available input and output */
587 unsigned copy
; /* number of stored or match bytes to copy */
588 unsigned char FAR
*from
; /* where to copy match bytes from */
589 code
this; /* current decoding table entry */
590 code last
; /* parent table entry */
591 unsigned len
; /* length to copy for repeats, bits to drop */
592 int ret
; /* return code */
594 unsigned char hbuf
[4]; /* buffer for gzip header crc calculation */
596 static const unsigned short order
[19] = /* permutation of code lengths */
597 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
599 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
|| strm
->next_out
== Z_NULL
||
600 (strm
->next_in
== Z_NULL
&& strm
->avail_in
!= 0))
601 return Z_STREAM_ERROR
;
603 state
= (struct inflate_state FAR
*)strm
->state
;
604 if (state
->mode
== TYPE
) state
->mode
= TYPEDO
; /* skip check */
610 switch (state
->mode
) {
612 if (state
->wrap
== 0) {
613 state
->mode
= TYPEDO
;
618 if ((state
->wrap
& 2) && hold
== 0x8b1f) { /* gzip header */
619 state
->check
= z_crc32(0L, Z_NULL
, 0);
620 CRC2(state
->check
, hold
);
625 state
->flags
= 0; /* expect zlib header */
626 if (state
->head
!= Z_NULL
)
627 state
->head
->done
= -1;
628 if (!(state
->wrap
& 1) || /* check if zlib header allowed */
632 ((BITS(8) << 8) + (hold
>> 8)) % 31) {
633 strm
->msg
= (char *)"incorrect header check";
637 if (BITS(4) != Z_DEFLATED
) {
638 strm
->msg
= (char *)"unknown compression method";
644 if (len
> state
->wbits
) {
645 strm
->msg
= (char *)"invalid window size";
649 state
->dmax
= 1U << len
;
650 Tracev((stderr
, "inflate: zlib header ok\n"));
651 strm
->adler
= state
->check
= adler32(0L, Z_NULL
, 0);
652 state
->mode
= hold
& 0x200 ? DICTID
: TYPE
;
658 state
->flags
= (int)(hold
);
659 if ((state
->flags
& 0xff) != Z_DEFLATED
) {
660 strm
->msg
= (char *)"unknown compression method";
664 if (state
->flags
& 0xe000) {
665 strm
->msg
= (char *)"unknown header flags set";
669 if (state
->head
!= Z_NULL
)
670 state
->head
->text
= (int)((hold
>> 8) & 1);
671 if (state
->flags
& 0x0200) CRC2(state
->check
, hold
);
676 if (state
->head
!= Z_NULL
)
677 state
->head
->time
= hold
;
678 if (state
->flags
& 0x0200) CRC4(state
->check
, hold
);
683 if (state
->head
!= Z_NULL
) {
684 state
->head
->xflags
= (int)(hold
& 0xff);
685 state
->head
->os
= (int)(hold
>> 8);
687 if (state
->flags
& 0x0200) CRC2(state
->check
, hold
);
691 if (state
->flags
& 0x0400) {
693 state
->length
= (unsigned)(hold
);
694 if (state
->head
!= Z_NULL
)
695 state
->head
->extra_len
= (unsigned)hold
;
696 if (state
->flags
& 0x0200) CRC2(state
->check
, hold
);
699 else if (state
->head
!= Z_NULL
)
700 state
->head
->extra
= Z_NULL
;
703 if (state
->flags
& 0x0400) {
704 copy
= state
->length
;
705 if (copy
> have
) copy
= have
;
707 if (state
->head
!= Z_NULL
&&
708 state
->head
->extra
!= Z_NULL
) {
709 len
= state
->head
->extra_len
- state
->length
;
710 zmemcpy(state
->head
->extra
+ len
, next
,
711 len
+ copy
> state
->head
->extra_max
?
712 state
->head
->extra_max
- len
: copy
);
714 if (state
->flags
& 0x0200)
715 state
->check
= z_crc32(state
->check
, next
, copy
);
718 state
->length
-= copy
;
720 if (state
->length
) goto inf_leave
;
725 if (state
->flags
& 0x0800) {
726 if (have
== 0) goto inf_leave
;
729 len
= (unsigned)(next
[copy
++]);
730 if (state
->head
!= Z_NULL
&&
731 state
->head
->name
!= Z_NULL
&&
732 state
->length
< state
->head
->name_max
)
733 state
->head
->name
[state
->length
++] = len
;
734 } while (len
&& copy
< have
);
735 if (state
->flags
& 0x0200)
736 state
->check
= z_crc32(state
->check
, next
, copy
);
739 if (len
) goto inf_leave
;
741 else if (state
->head
!= Z_NULL
)
742 state
->head
->name
= Z_NULL
;
744 state
->mode
= COMMENT
;
746 if (state
->flags
& 0x1000) {
747 if (have
== 0) goto inf_leave
;
750 len
= (unsigned)(next
[copy
++]);
751 if (state
->head
!= Z_NULL
&&
752 state
->head
->comment
!= Z_NULL
&&
753 state
->length
< state
->head
->comm_max
)
754 state
->head
->comment
[state
->length
++] = len
;
755 } while (len
&& copy
< have
);
756 if (state
->flags
& 0x0200)
757 state
->check
= z_crc32(state
->check
, next
, copy
);
760 if (len
) goto inf_leave
;
762 else if (state
->head
!= Z_NULL
)
763 state
->head
->comment
= Z_NULL
;
766 if (state
->flags
& 0x0200) {
768 if (hold
!= (state
->check
& 0xffff)) {
769 strm
->msg
= (char *)"header crc mismatch";
775 if (state
->head
!= Z_NULL
) {
776 state
->head
->hcrc
= (int)((state
->flags
>> 9) & 1);
777 state
->head
->done
= 1;
779 strm
->adler
= state
->check
= z_crc32(0L, Z_NULL
, 0);
785 strm
->adler
= state
->check
= REVERSE(hold
);
789 if (state
->havedict
== 0) {
793 strm
->adler
= state
->check
= adler32(0L, Z_NULL
, 0);
796 if (flush
== Z_BLOCK
) goto inf_leave
;
804 state
->last
= BITS(1);
807 case 0: /* stored block */
808 Tracev((stderr
, "inflate: stored block%s\n",
809 state
->last
? " (last)" : ""));
810 state
->mode
= STORED
;
812 case 1: /* fixed block */
814 Tracev((stderr
, "inflate: fixed codes block%s\n",
815 state
->last
? " (last)" : ""));
816 state
->mode
= LEN
; /* decode codes */
818 case 2: /* dynamic block */
819 Tracev((stderr
, "inflate: dynamic codes block%s\n",
820 state
->last
? " (last)" : ""));
824 strm
->msg
= (char *)"invalid block type";
830 BYTEBITS(); /* go to byte boundary */
832 if ((hold
& 0xffff) != ((hold
>> 16) ^ 0xffff)) {
833 strm
->msg
= (char *)"invalid stored block lengths";
837 state
->length
= (unsigned)hold
& 0xffff;
838 Tracev((stderr
, "inflate: stored length %u\n",
843 copy
= state
->length
;
845 if (copy
> have
) copy
= have
;
846 if (copy
> left
) copy
= left
;
847 if (copy
== 0) goto inf_leave
;
848 zmemcpy(put
, next
, copy
);
853 state
->length
-= copy
;
856 Tracev((stderr
, "inflate: stored end\n"));
861 state
->nlen
= BITS(5) + 257;
863 state
->ndist
= BITS(5) + 1;
865 state
->ncode
= BITS(4) + 4;
867 #ifndef PKZIP_BUG_WORKAROUND
868 if (state
->nlen
> 286 || state
->ndist
> 30) {
869 strm
->msg
= (char *)"too many length or distance symbols";
874 Tracev((stderr
, "inflate: table sizes ok\n"));
876 state
->mode
= LENLENS
;
878 while (state
->have
< state
->ncode
) {
880 state
->lens
[order
[state
->have
++]] = (unsigned short)BITS(3);
883 while (state
->have
< 19)
884 state
->lens
[order
[state
->have
++]] = 0;
885 state
->next
= state
->codes
;
886 state
->lencode
= (code
const FAR
*)(state
->next
);
888 ret
= inflate_table(CODES
, state
->lens
, 19, &(state
->next
),
889 &(state
->lenbits
), state
->work
);
891 strm
->msg
= (char *)"invalid code lengths set";
895 Tracev((stderr
, "inflate: code lengths ok\n"));
897 state
->mode
= CODELENS
;
899 while (state
->have
< state
->nlen
+ state
->ndist
) {
901 this = state
->lencode
[BITS(state
->lenbits
)];
902 if ((unsigned)(this.bits
) <= bits
) break;
908 state
->lens
[state
->have
++] = this.val
;
911 if (this.val
== 16) {
912 NEEDBITS(this.bits
+ 2);
914 if (state
->have
== 0) {
915 strm
->msg
= (char *)"invalid bit length repeat";
919 len
= state
->lens
[state
->have
- 1];
923 else if (this.val
== 17) {
924 NEEDBITS(this.bits
+ 3);
931 NEEDBITS(this.bits
+ 7);
937 if (state
->have
+ copy
> state
->nlen
+ state
->ndist
) {
938 strm
->msg
= (char *)"invalid bit length repeat";
943 state
->lens
[state
->have
++] = (unsigned short)len
;
947 /* handle error breaks in while */
948 if (state
->mode
== BAD
) break;
950 /* build code tables */
951 state
->next
= state
->codes
;
952 state
->lencode
= (code
const FAR
*)(state
->next
);
954 ret
= inflate_table(LENS
, state
->lens
, state
->nlen
, &(state
->next
),
955 &(state
->lenbits
), state
->work
);
957 strm
->msg
= (char *)"invalid literal/lengths set";
961 state
->distcode
= (code
const FAR
*)(state
->next
);
963 ret
= inflate_table(DISTS
, state
->lens
+ state
->nlen
, state
->ndist
,
964 &(state
->next
), &(state
->distbits
), state
->work
);
966 strm
->msg
= (char *)"invalid distances set";
970 Tracev((stderr
, "inflate: codes ok\n"));
973 if (have
>= 6 && left
>= 258) {
975 inflate_fast(strm
, out
);
980 this = state
->lencode
[BITS(state
->lenbits
)];
981 if ((unsigned)(this.bits
) <= bits
) break;
984 if (this.op
&& (this.op
& 0xf0) == 0) {
987 this = state
->lencode
[last
.val
+
988 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
989 if ((unsigned)(last
.bits
+ this.bits
) <= bits
) break;
995 state
->length
= (unsigned)this.val
;
996 if ((int)(this.op
) == 0) {
997 Tracevv((stderr
, this.val
>= 0x20 && this.val
< 0x7f ?
998 "inflate: literal '%c'\n" :
999 "inflate: literal 0x%02x\n", this.val
));
1004 Tracevv((stderr
, "inflate: end of block\n"));
1009 strm
->msg
= (char *)"invalid literal/length code";
1013 state
->extra
= (unsigned)(this.op
) & 15;
1014 state
->mode
= LENEXT
;
1017 NEEDBITS(state
->extra
);
1018 state
->length
+= BITS(state
->extra
);
1019 DROPBITS(state
->extra
);
1021 Tracevv((stderr
, "inflate: length %u\n", state
->length
));
1025 this = state
->distcode
[BITS(state
->distbits
)];
1026 if ((unsigned)(this.bits
) <= bits
) break;
1029 if ((this.op
& 0xf0) == 0) {
1032 this = state
->distcode
[last
.val
+
1033 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
1034 if ((unsigned)(last
.bits
+ this.bits
) <= bits
) break;
1037 DROPBITS(last
.bits
);
1039 DROPBITS(this.bits
);
1041 strm
->msg
= (char *)"invalid distance code";
1045 state
->offset
= (unsigned)this.val
;
1046 state
->extra
= (unsigned)(this.op
) & 15;
1047 state
->mode
= DISTEXT
;
1050 NEEDBITS(state
->extra
);
1051 state
->offset
+= BITS(state
->extra
);
1052 DROPBITS(state
->extra
);
1054 #ifdef INFLATE_STRICT
1055 if (state
->offset
> state
->dmax
) {
1056 strm
->msg
= (char *)"invalid distance too far back";
1061 if (state
->offset
> state
->whave
+ out
- left
) {
1062 strm
->msg
= (char *)"invalid distance too far back";
1066 Tracevv((stderr
, "inflate: distance %u\n", state
->offset
));
1067 state
->mode
= MATCH
;
1069 if (left
== 0) goto inf_leave
;
1071 if (state
->offset
> copy
) { /* copy from window */
1072 copy
= state
->offset
- copy
;
1073 if (copy
> state
->write
) {
1074 copy
-= state
->write
;
1075 from
= state
->window
+ (state
->wsize
- copy
);
1078 from
= state
->window
+ (state
->write
- copy
);
1079 if (copy
> state
->length
) copy
= state
->length
;
1081 else { /* copy from output */
1082 from
= put
- state
->offset
;
1083 copy
= state
->length
;
1085 if (copy
> left
) copy
= left
;
1087 state
->length
-= copy
;
1091 if (state
->length
== 0) state
->mode
= LEN
;
1094 if (left
== 0) goto inf_leave
;
1095 *put
++ = (unsigned char)(state
->length
);
1103 strm
->total_out
+= out
;
1104 state
->total
+= out
;
1106 strm
->adler
= state
->check
=
1107 UPDATE(state
->check
, put
- out
, out
);
1111 state
->flags
? hold
:
1113 REVERSE(hold
)) != state
->check
) {
1114 strm
->msg
= (char *)"incorrect data check";
1119 Tracev((stderr
, "inflate: check matches trailer\n"));
1122 state
->mode
= LENGTH
;
1124 if (state
->wrap
&& state
->flags
) {
1126 if (hold
!= (state
->total
& 0xffffffffUL
)) {
1127 strm
->msg
= (char *)"incorrect length check";
1132 Tracev((stderr
, "inflate: length matches trailer\n"));
1146 return Z_STREAM_ERROR
;
1150 Return from inflate(), updating the total counts and the check value.
1151 If there was no progress during the inflate() call, return a buffer
1152 error. Call updatewindow() to create and/or update the window state.
1153 Note: a memory error from inflate() is non-recoverable.
1157 if (state
->wsize
|| (state
->mode
< CHECK
&& out
!= strm
->avail_out
))
1158 if (updatewindow(strm
, out
)) {
1162 in
-= strm
->avail_in
;
1163 out
-= strm
->avail_out
;
1164 strm
->total_in
+= in
;
1165 strm
->total_out
+= out
;
1166 state
->total
+= out
;
1167 if (state
->wrap
&& out
)
1168 strm
->adler
= state
->check
=
1169 UPDATE(state
->check
, strm
->next_out
- out
, out
);
1170 strm
->data_type
= state
->bits
+ (state
->last
? 64 : 0) +
1171 (state
->mode
== TYPE
? 128 : 0);
1172 if (((in
== 0 && out
== 0) || flush
== Z_FINISH
) && ret
== Z_OK
)
1178 inflateEnd(z_streamp strm
)
1180 struct inflate_state FAR
*state
;
1181 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
|| strm
->zfree
== (free_func
)0)
1182 return Z_STREAM_ERROR
;
1183 state
= (struct inflate_state FAR
*)strm
->state
;
1184 if (state
->window
!= Z_NULL
) ZFREE(strm
, state
->window
);
1185 ZFREE(strm
, strm
->state
);
1186 strm
->state
= Z_NULL
;
1187 Tracev((stderr
, "inflate: end\n"));
1192 inflateSetDictionary(z_streamp strm
, const Bytef
*dictionary
, uInt dictLength
)
1194 struct inflate_state FAR
*state
;
1198 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
) return Z_STREAM_ERROR
;
1199 state
= (struct inflate_state FAR
*)strm
->state
;
1200 if (state
->wrap
!= 0 && state
->mode
!= DICT
)
1201 return Z_STREAM_ERROR
;
1203 /* check for correct dictionary id */
1204 if (state
->mode
== DICT
) {
1205 id
= adler32(0L, Z_NULL
, 0);
1206 id
= adler32(id
, dictionary
, dictLength
);
1207 if (id
!= state
->check
)
1208 return Z_DATA_ERROR
;
1211 /* copy dictionary to window */
1212 if (updatewindow(strm
, strm
->avail_out
)) {
1216 if (dictLength
> state
->wsize
) {
1217 zmemcpy(state
->window
, dictionary
+ dictLength
- state
->wsize
,
1219 state
->whave
= state
->wsize
;
1222 zmemcpy(state
->window
+ state
->wsize
- dictLength
, dictionary
,
1224 state
->whave
= dictLength
;
1226 state
->havedict
= 1;
1227 Tracev((stderr
, "inflate: dictionary set\n"));
1232 inflateGetHeader(z_streamp strm
, gz_headerp head
)
1234 struct inflate_state FAR
*state
;
1237 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
) return Z_STREAM_ERROR
;
1238 state
= (struct inflate_state FAR
*)strm
->state
;
1239 if ((state
->wrap
& 2) == 0) return Z_STREAM_ERROR
;
1241 /* save header structure */
1248 Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
1249 or when out of input. When called, *have is the number of pattern bytes
1250 found in order so far, in 0..3. On return *have is updated to the new
1251 state. If on return *have equals four, then the pattern was found and the
1252 return value is how many bytes were read including the last byte of the
1253 pattern. If *have is less than four, then the pattern has not been found
1254 yet and the return value is len. In the latter case, syncsearch() can be
1255 called again with more data and the *have state. *have is initialized to
1256 zero for the first call.
1259 syncsearch(unsigned FAR
*have
, unsigned char FAR
*buf
, unsigned len
)
1266 while (next
< len
&& got
< 4) {
1267 if ((int)(buf
[next
]) == (got
< 2 ? 0 : 0xff))
1280 inflateSync(z_streamp strm
)
1282 unsigned len
; /* number of bytes to look at or looked at */
1283 unsigned long in
, out
; /* temporary to save total_in and total_out */
1284 unsigned char buf
[4]; /* to restore bit buffer to byte string */
1285 struct inflate_state FAR
*state
;
1287 /* check parameters */
1288 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
) return Z_STREAM_ERROR
;
1289 state
= (struct inflate_state FAR
*)strm
->state
;
1290 if (strm
->avail_in
== 0 && state
->bits
< 8) return Z_BUF_ERROR
;
1292 /* if first time, start search in bit buffer */
1293 if (state
->mode
!= SYNC
) {
1295 state
->hold
<<= state
->bits
& 7;
1296 state
->bits
-= state
->bits
& 7;
1298 while (state
->bits
>= 8) {
1299 buf
[len
++] = (unsigned char)(state
->hold
);
1304 syncsearch(&(state
->have
), buf
, len
);
1307 /* search available input */
1308 len
= syncsearch(&(state
->have
), strm
->next_in
, strm
->avail_in
);
1309 strm
->avail_in
-= len
;
1310 strm
->next_in
+= len
;
1311 strm
->total_in
+= len
;
1313 /* return no joy or set up to restart inflate() on a new block */
1314 if (state
->have
!= 4) return Z_DATA_ERROR
;
1315 in
= strm
->total_in
; out
= strm
->total_out
;
1317 strm
->total_in
= in
; strm
->total_out
= out
;
1323 Returns true if inflate is currently at the end of a block generated by
1324 Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1325 implementation to provide an additional safety check. PPP uses
1326 Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1327 block. When decompressing, PPP checks that at the end of input packet,
1328 inflate is waiting for these length bytes.
1331 inflateSyncPoint(z_streamp strm
)
1333 struct inflate_state FAR
*state
;
1335 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
) return Z_STREAM_ERROR
;
1336 state
= (struct inflate_state FAR
*)strm
->state
;
1337 return state
->mode
== STORED
&& state
->bits
== 0;
1341 inflateCopy(z_streamp dest
, z_streamp source
)
1343 struct inflate_state FAR
*state
;
1344 struct inflate_state FAR
*copy
;
1345 unsigned char FAR
*window
;
1349 if (dest
== Z_NULL
|| source
== Z_NULL
|| source
->state
== Z_NULL
||
1350 source
->zalloc
== (alloc_func
)0 || source
->zfree
== (free_func
)0)
1351 return Z_STREAM_ERROR
;
1352 state
= (struct inflate_state FAR
*)source
->state
;
1354 /* allocate space */
1355 copy
= (struct inflate_state FAR
*)
1356 ZALLOC(source
, 1, sizeof(struct inflate_state
));
1357 if (copy
== Z_NULL
) return Z_MEM_ERROR
;
1359 if (state
->window
!= Z_NULL
) {
1360 window
= (unsigned char FAR
*)
1361 ZALLOC(source
, 1U << state
->wbits
, sizeof(unsigned char));
1362 if (window
== Z_NULL
) {
1363 ZFREE(source
, copy
);
1369 zmemcpy(dest
, source
, sizeof(z_stream
));
1370 zmemcpy(copy
, state
, sizeof(struct inflate_state
));
1371 if (state
->lencode
>= state
->codes
&&
1372 state
->lencode
<= state
->codes
+ ENOUGH
- 1) {
1373 copy
->lencode
= copy
->codes
+ (state
->lencode
- state
->codes
);
1374 copy
->distcode
= copy
->codes
+ (state
->distcode
- state
->codes
);
1376 copy
->next
= copy
->codes
+ (state
->next
- state
->codes
);
1377 if (window
!= Z_NULL
) {
1378 wsize
= 1U << state
->wbits
;
1379 zmemcpy(window
, state
->window
, wsize
);
1381 copy
->window
= window
;
1382 dest
->state
= (struct internal_state FAR
*)copy
;