]>
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
,
130 int ZEXPORT
inflateReset(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"));
155 int ZEXPORT
inflatePrime(strm
, bits
, value
)
160 struct inflate_state FAR
*state
;
162 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
) return Z_STREAM_ERROR
;
163 state
= (struct inflate_state FAR
*)strm
->state
;
164 if (bits
> 16 || state
->bits
+ bits
> 32) return Z_STREAM_ERROR
;
165 value
&= (1L << bits
) - 1;
166 state
->hold
+= value
<< state
->bits
;
171 int ZEXPORT
inflateInit2_(strm
, windowBits
, version
, stream_size
)
177 struct inflate_state FAR
*state
;
179 if (version
== Z_NULL
|| version
[0] != ZLIB_VERSION
[0] ||
180 stream_size
!= (int)(sizeof(z_stream
)))
181 return Z_VERSION_ERROR
;
182 if (strm
== Z_NULL
) return Z_STREAM_ERROR
;
183 strm
->msg
= Z_NULL
; /* in case we return an error */
185 if (strm
->zalloc
== (alloc_func
)0) {
186 strm
->zalloc
= zcalloc
;
187 strm
->opaque
= (voidpf
)0;
189 if (strm
->zfree
== (free_func
)0) strm
->zfree
= zcfree
;
190 #endif /* NO_ZCFUNCS */
191 state
= (struct inflate_state FAR
*)
192 ZALLOC(strm
, 1, sizeof(struct inflate_state
));
193 if (state
== Z_NULL
) return Z_MEM_ERROR
;
194 Tracev((stderr
, "inflate: allocated\n"));
195 strm
->state
= (struct internal_state FAR
*)state
;
196 if (windowBits
< 0) {
198 windowBits
= -windowBits
;
201 state
->wrap
= (windowBits
>> 4) + 1;
203 if (windowBits
< 48) windowBits
&= 15;
206 if (windowBits
< 8 || windowBits
> 15) {
208 strm
->state
= Z_NULL
;
209 return Z_STREAM_ERROR
;
211 state
->wbits
= (unsigned)windowBits
;
212 state
->window
= Z_NULL
;
213 return inflateReset(strm
);
216 int ZEXPORT
inflateInit_(strm
, version
, stream_size
)
221 return inflateInit2_(strm
, DEF_WBITS
, version
, stream_size
);
225 Return state with length and distance decoding tables and index sizes set to
226 fixed code decoding. Normally this returns fixed tables from inffixed.h.
227 If BUILDFIXED is defined, then instead this routine builds the tables the
228 first time it's called, and returns those tables the first time and
229 thereafter. This reduces the size of the code by about 2K bytes, in
230 exchange for a little execution time. However, BUILDFIXED should not be
231 used for threaded applications, since the rewriting of the tables and virgin
232 may not be thread-safe.
234 local
void fixedtables(state
)
235 struct inflate_state FAR
*state
;
238 static int virgin
= 1;
239 static code
*lenfix
, *distfix
;
240 static code fixed
[544];
242 /* build fixed huffman tables if first call (may not be thread safe) */
247 /* literal/length table */
249 while (sym
< 144) state
->lens
[sym
++] = 8;
250 while (sym
< 256) state
->lens
[sym
++] = 9;
251 while (sym
< 280) state
->lens
[sym
++] = 7;
252 while (sym
< 288) state
->lens
[sym
++] = 8;
256 inflate_table(LENS
, state
->lens
, 288, &(next
), &(bits
), state
->work
);
260 while (sym
< 32) state
->lens
[sym
++] = 5;
263 inflate_table(DISTS
, state
->lens
, 32, &(next
), &(bits
), state
->work
);
265 /* do this just once */
268 #else /* !BUILDFIXED */
269 # include "inffixed.h"
270 #endif /* BUILDFIXED */
271 state
->lencode
= lenfix
;
273 state
->distcode
= distfix
;
281 Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
282 defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
283 those tables to stdout, which would be piped to inffixed.h. A small program
284 can simply call makefixed to do this:
286 void makefixed(void);
294 Then that can be linked with zlib built with MAKEFIXED defined and run:
301 struct inflate_state state
;
304 puts(" /* inffixed.h -- table for decoding fixed codes");
305 puts(" * Generated automatically by makefixed().");
308 puts(" /* WARNING: this file should *not* be used by applications.");
309 puts(" It is part of the implementation of this library and is");
310 puts(" subject to change. Applications should only use zlib.h.");
314 printf(" static const code lenfix[%u] = {", size
);
317 if ((low
% 7) == 0) printf("\n ");
318 printf("{%u,%u,%d}", state
.lencode
[low
].op
, state
.lencode
[low
].bits
,
319 state
.lencode
[low
].val
);
320 if (++low
== size
) break;
325 printf("\n static const code distfix[%u] = {", size
);
328 if ((low
% 6) == 0) printf("\n ");
329 printf("{%u,%u,%d}", state
.distcode
[low
].op
, state
.distcode
[low
].bits
,
330 state
.distcode
[low
].val
);
331 if (++low
== size
) break;
336 #endif /* MAKEFIXED */
339 Update the window with the last wsize (normally 32K) bytes written before
340 returning. If window does not exist yet, create it. This is only called
341 when a window is already in use, or when output has been written during this
342 inflate call, but the end of the deflate stream has not been reached yet.
343 It is also called to create a window for dictionary data when a dictionary
346 Providing output buffers larger than 32K to inflate() should provide a speed
347 advantage, since only the last 32K of output is copied to the sliding window
348 upon return from inflate(), and since all distances after the first 32K of
349 output will fall in the output data, making match copies simpler and faster.
350 The advantage may be dependent on the size of the processor's data caches.
352 local
int updatewindow(strm
, out
)
356 struct inflate_state FAR
*state
;
359 state
= (struct inflate_state FAR
*)strm
->state
;
361 /* if it hasn't been done already, allocate space for the window */
362 if (state
->window
== Z_NULL
) {
363 state
->window
= (unsigned char FAR
*)
364 ZALLOC(strm
, 1U << state
->wbits
,
365 sizeof(unsigned char));
366 if (state
->window
== Z_NULL
) return 1;
369 /* if window not in use yet, initialize */
370 if (state
->wsize
== 0) {
371 state
->wsize
= 1U << state
->wbits
;
376 /* copy state->wsize or less output bytes into the circular window */
377 copy
= out
- strm
->avail_out
;
378 if (copy
>= state
->wsize
) {
379 zmemcpy(state
->window
, strm
->next_out
- state
->wsize
, state
->wsize
);
381 state
->whave
= state
->wsize
;
384 dist
= state
->wsize
- state
->write
;
385 if (dist
> copy
) dist
= copy
;
386 zmemcpy(state
->window
+ state
->write
, strm
->next_out
- copy
, dist
);
389 zmemcpy(state
->window
, strm
->next_out
- copy
, copy
);
391 state
->whave
= state
->wsize
;
394 state
->write
+= dist
;
395 if (state
->write
== state
->wsize
) state
->write
= 0;
396 if (state
->whave
< state
->wsize
) state
->whave
+= dist
;
402 /* Macros for inflate(): */
404 /* check function to use adler32() for zlib or z_crc32() for gzip */
406 # define UPDATE(check, buf, len) \
407 (state->flags ? z_crc32(check, buf, len) : adler32(check, buf, len))
409 # define UPDATE(check, buf, len) adler32(check, buf, len)
412 /* check macros for header crc */
414 # define CRC2(check, word) \
416 hbuf[0] = (unsigned char)(word); \
417 hbuf[1] = (unsigned char)((word) >> 8); \
418 check = z_crc32(check, hbuf, 2); \
421 # define CRC4(check, word) \
423 hbuf[0] = (unsigned char)(word); \
424 hbuf[1] = (unsigned char)((word) >> 8); \
425 hbuf[2] = (unsigned char)((word) >> 16); \
426 hbuf[3] = (unsigned char)((word) >> 24); \
427 check = z_crc32(check, hbuf, 4); \
431 /* Load registers with state in inflate() for speed */
434 put = strm->next_out; \
435 left = strm->avail_out; \
436 next = strm->next_in; \
437 have = strm->avail_in; \
438 hold = state->hold; \
439 bits = state->bits; \
442 /* Restore state from registers in inflate() */
445 strm->next_out = put; \
446 strm->avail_out = left; \
447 strm->next_in = next; \
448 strm->avail_in = have; \
449 state->hold = hold; \
450 state->bits = bits; \
453 /* Clear the input bit accumulator */
460 /* Get a byte of input into the bit accumulator, or return from inflate()
461 if there is no input available. */
464 if (have == 0) goto inf_leave; \
466 hold += (unsigned long)(*next++) << bits; \
470 /* Assure that there are at least n bits in the bit accumulator. If there is
471 not enough available input to do that, then return from inflate(). */
472 #define NEEDBITS(n) \
474 while (bits < (unsigned)(n)) \
478 /* Return the low n bits of the bit accumulator (n < 16) */
480 ((unsigned)hold & ((1U << (n)) - 1))
482 /* Remove n bits from the bit accumulator */
483 #define DROPBITS(n) \
486 bits -= (unsigned)(n); \
489 /* Remove zero to seven bits as needed to go to a byte boundary */
496 /* Reverse the bytes in a 32-bit value */
498 ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
499 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
502 inflate() uses a state machine to process as much input data and generate as
503 much output data as possible before returning. The state machine is
504 structured roughly as follows:
506 for (;;) switch (state) {
509 if (not enough input data or output space to make progress)
511 ... make progress ...
517 so when inflate() is called again, the same case is attempted again, and
518 if the appropriate resources are provided, the machine proceeds to the
519 next state. The NEEDBITS() macro is usually the way the state evaluates
520 whether it can proceed or should return. NEEDBITS() does the return if
521 the requested bits are not available. The typical use of the BITS macros
525 ... do something with BITS(n) ...
528 where NEEDBITS(n) either returns from inflate() if there isn't enough
529 input left to load n bits into the accumulator, or it continues. BITS(n)
530 gives the low n bits in the accumulator. When done, DROPBITS(n) drops
531 the low n bits off the accumulator. INITBITS() clears the accumulator
532 and sets the number of available bits to zero. BYTEBITS() discards just
533 enough bits to put the accumulator on a byte boundary. After BYTEBITS()
534 and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
536 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
537 if there is no input available. The decoding of variable length codes uses
538 PULLBYTE() directly in order to pull just enough bytes to decode the next
541 Some states loop until they get enough input, making sure that enough
542 state information is maintained to continue the loop where it left off
543 if NEEDBITS() returns in the loop. For example, want, need, and keep
544 would all have to actually be part of the saved state in case NEEDBITS()
548 while (want < need) {
550 keep[want++] = BITS(n);
556 As shown above, if the next state is also the next case, then the break
559 A state may also return if there is not enough output space available to
560 complete that state. Those states are copying stored data, writing a
561 literal byte, and copying a matching string.
563 When returning, a "goto inf_leave" is used to update the total counters,
564 update the check value, and determine whether any progress has been made
565 during that inflate() call in order to return the proper return code.
566 Progress is defined as a change in either strm->avail_in or strm->avail_out.
567 When there is a window, goto inf_leave will update the window with the last
568 output written. If a goto inf_leave occurs in the middle of decompression
569 and there is no window currently, goto inf_leave will create one and copy
570 output to the window for the next call of inflate().
572 In this implementation, the flush parameter of inflate() only affects the
573 return code (per zlib.h). inflate() always writes as much as possible to
574 strm->next_out, given the space available and the provided input--the effect
575 documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
576 the allocation of and copying into a sliding window until necessary, which
577 provides the effect documented in zlib.h for Z_FINISH when the entire input
578 stream available. So the only thing the flush parameter actually does is:
579 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
580 will return Z_BUF_ERROR if it has not reached the end of the stream.
583 int ZEXPORT
inflate(strm
, flush
)
587 struct inflate_state FAR
*state
;
588 unsigned char FAR
*next
; /* next input */
589 unsigned char FAR
*put
; /* next output */
590 unsigned have
, left
; /* available input and output */
591 unsigned long hold
; /* bit buffer */
592 unsigned bits
; /* bits in bit buffer */
593 unsigned in
, out
; /* save starting available input and output */
594 unsigned copy
; /* number of stored or match bytes to copy */
595 unsigned char FAR
*from
; /* where to copy match bytes from */
596 code
this; /* current decoding table entry */
597 code last
; /* parent table entry */
598 unsigned len
; /* length to copy for repeats, bits to drop */
599 int ret
; /* return code */
601 unsigned char hbuf
[4]; /* buffer for gzip header crc calculation */
603 static const unsigned short order
[19] = /* permutation of code lengths */
604 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
606 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
|| strm
->next_out
== Z_NULL
||
607 (strm
->next_in
== Z_NULL
&& strm
->avail_in
!= 0))
608 return Z_STREAM_ERROR
;
610 state
= (struct inflate_state FAR
*)strm
->state
;
611 if (state
->mode
== TYPE
) state
->mode
= TYPEDO
; /* skip check */
617 switch (state
->mode
) {
619 if (state
->wrap
== 0) {
620 state
->mode
= TYPEDO
;
625 if ((state
->wrap
& 2) && hold
== 0x8b1f) { /* gzip header */
626 state
->check
= z_crc32(0L, Z_NULL
, 0);
627 CRC2(state
->check
, hold
);
632 state
->flags
= 0; /* expect zlib header */
633 if (state
->head
!= Z_NULL
)
634 state
->head
->done
= -1;
635 if (!(state
->wrap
& 1) || /* check if zlib header allowed */
639 ((BITS(8) << 8) + (hold
>> 8)) % 31) {
640 strm
->msg
= (char *)"incorrect header check";
644 if (BITS(4) != Z_DEFLATED
) {
645 strm
->msg
= (char *)"unknown compression method";
651 if (len
> state
->wbits
) {
652 strm
->msg
= (char *)"invalid window size";
656 state
->dmax
= 1U << len
;
657 Tracev((stderr
, "inflate: zlib header ok\n"));
658 strm
->adler
= state
->check
= adler32(0L, Z_NULL
, 0);
659 state
->mode
= hold
& 0x200 ? DICTID
: TYPE
;
665 state
->flags
= (int)(hold
);
666 if ((state
->flags
& 0xff) != Z_DEFLATED
) {
667 strm
->msg
= (char *)"unknown compression method";
671 if (state
->flags
& 0xe000) {
672 strm
->msg
= (char *)"unknown header flags set";
676 if (state
->head
!= Z_NULL
)
677 state
->head
->text
= (int)((hold
>> 8) & 1);
678 if (state
->flags
& 0x0200) CRC2(state
->check
, hold
);
683 if (state
->head
!= Z_NULL
)
684 state
->head
->time
= hold
;
685 if (state
->flags
& 0x0200) CRC4(state
->check
, hold
);
690 if (state
->head
!= Z_NULL
) {
691 state
->head
->xflags
= (int)(hold
& 0xff);
692 state
->head
->os
= (int)(hold
>> 8);
694 if (state
->flags
& 0x0200) CRC2(state
->check
, hold
);
698 if (state
->flags
& 0x0400) {
700 state
->length
= (unsigned)(hold
);
701 if (state
->head
!= Z_NULL
)
702 state
->head
->extra_len
= (unsigned)hold
;
703 if (state
->flags
& 0x0200) CRC2(state
->check
, hold
);
706 else if (state
->head
!= Z_NULL
)
707 state
->head
->extra
= Z_NULL
;
710 if (state
->flags
& 0x0400) {
711 copy
= state
->length
;
712 if (copy
> have
) copy
= have
;
714 if (state
->head
!= Z_NULL
&&
715 state
->head
->extra
!= Z_NULL
) {
716 len
= state
->head
->extra_len
- state
->length
;
717 zmemcpy(state
->head
->extra
+ len
, next
,
718 len
+ copy
> state
->head
->extra_max
?
719 state
->head
->extra_max
- len
: copy
);
721 if (state
->flags
& 0x0200)
722 state
->check
= z_crc32(state
->check
, next
, copy
);
725 state
->length
-= copy
;
727 if (state
->length
) goto inf_leave
;
732 if (state
->flags
& 0x0800) {
733 if (have
== 0) goto inf_leave
;
736 len
= (unsigned)(next
[copy
++]);
737 if (state
->head
!= Z_NULL
&&
738 state
->head
->name
!= Z_NULL
&&
739 state
->length
< state
->head
->name_max
)
740 state
->head
->name
[state
->length
++] = len
;
741 } while (len
&& copy
< have
);
742 if (state
->flags
& 0x0200)
743 state
->check
= z_crc32(state
->check
, next
, copy
);
746 if (len
) goto inf_leave
;
748 else if (state
->head
!= Z_NULL
)
749 state
->head
->name
= Z_NULL
;
751 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
++] = 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
;
773 if (state
->flags
& 0x0200) {
775 if (hold
!= (state
->check
& 0xffff)) {
776 strm
->msg
= (char *)"header crc mismatch";
782 if (state
->head
!= Z_NULL
) {
783 state
->head
->hcrc
= (int)((state
->flags
>> 9) & 1);
784 state
->head
->done
= 1;
786 strm
->adler
= state
->check
= z_crc32(0L, Z_NULL
, 0);
792 strm
->adler
= state
->check
= REVERSE(hold
);
796 if (state
->havedict
== 0) {
800 strm
->adler
= state
->check
= adler32(0L, Z_NULL
, 0);
803 if (flush
== Z_BLOCK
) goto inf_leave
;
811 state
->last
= BITS(1);
814 case 0: /* stored block */
815 Tracev((stderr
, "inflate: stored block%s\n",
816 state
->last
? " (last)" : ""));
817 state
->mode
= STORED
;
819 case 1: /* fixed block */
821 Tracev((stderr
, "inflate: fixed codes block%s\n",
822 state
->last
? " (last)" : ""));
823 state
->mode
= LEN
; /* decode codes */
825 case 2: /* dynamic block */
826 Tracev((stderr
, "inflate: dynamic codes block%s\n",
827 state
->last
? " (last)" : ""));
831 strm
->msg
= (char *)"invalid block type";
837 BYTEBITS(); /* go to byte boundary */
839 if ((hold
& 0xffff) != ((hold
>> 16) ^ 0xffff)) {
840 strm
->msg
= (char *)"invalid stored block lengths";
844 state
->length
= (unsigned)hold
& 0xffff;
845 Tracev((stderr
, "inflate: stored length %u\n",
850 copy
= state
->length
;
852 if (copy
> have
) copy
= have
;
853 if (copy
> left
) copy
= left
;
854 if (copy
== 0) goto inf_leave
;
855 zmemcpy(put
, next
, copy
);
860 state
->length
-= copy
;
863 Tracev((stderr
, "inflate: stored end\n"));
868 state
->nlen
= BITS(5) + 257;
870 state
->ndist
= BITS(5) + 1;
872 state
->ncode
= BITS(4) + 4;
874 #ifndef PKZIP_BUG_WORKAROUND
875 if (state
->nlen
> 286 || state
->ndist
> 30) {
876 strm
->msg
= (char *)"too many length or distance symbols";
881 Tracev((stderr
, "inflate: table sizes ok\n"));
883 state
->mode
= LENLENS
;
885 while (state
->have
< state
->ncode
) {
887 state
->lens
[order
[state
->have
++]] = (unsigned short)BITS(3);
890 while (state
->have
< 19)
891 state
->lens
[order
[state
->have
++]] = 0;
892 state
->next
= state
->codes
;
893 state
->lencode
= (code
const FAR
*)(state
->next
);
895 ret
= inflate_table(CODES
, state
->lens
, 19, &(state
->next
),
896 &(state
->lenbits
), state
->work
);
898 strm
->msg
= (char *)"invalid code lengths set";
902 Tracev((stderr
, "inflate: code lengths ok\n"));
904 state
->mode
= CODELENS
;
906 while (state
->have
< state
->nlen
+ state
->ndist
) {
908 this = state
->lencode
[BITS(state
->lenbits
)];
909 if ((unsigned)(this.bits
) <= bits
) break;
915 state
->lens
[state
->have
++] = this.val
;
918 if (this.val
== 16) {
919 NEEDBITS(this.bits
+ 2);
921 if (state
->have
== 0) {
922 strm
->msg
= (char *)"invalid bit length repeat";
926 len
= state
->lens
[state
->have
- 1];
930 else if (this.val
== 17) {
931 NEEDBITS(this.bits
+ 3);
938 NEEDBITS(this.bits
+ 7);
944 if (state
->have
+ copy
> state
->nlen
+ state
->ndist
) {
945 strm
->msg
= (char *)"invalid bit length repeat";
950 state
->lens
[state
->have
++] = (unsigned short)len
;
954 /* handle error breaks in while */
955 if (state
->mode
== BAD
) break;
957 /* build code tables */
958 state
->next
= state
->codes
;
959 state
->lencode
= (code
const FAR
*)(state
->next
);
961 ret
= inflate_table(LENS
, state
->lens
, state
->nlen
, &(state
->next
),
962 &(state
->lenbits
), state
->work
);
964 strm
->msg
= (char *)"invalid literal/lengths set";
968 state
->distcode
= (code
const FAR
*)(state
->next
);
970 ret
= inflate_table(DISTS
, state
->lens
+ state
->nlen
, state
->ndist
,
971 &(state
->next
), &(state
->distbits
), state
->work
);
973 strm
->msg
= (char *)"invalid distances set";
977 Tracev((stderr
, "inflate: codes ok\n"));
980 if (have
>= 6 && left
>= 258) {
982 inflate_fast(strm
, out
);
987 this = state
->lencode
[BITS(state
->lenbits
)];
988 if ((unsigned)(this.bits
) <= bits
) break;
991 if (this.op
&& (this.op
& 0xf0) == 0) {
994 this = state
->lencode
[last
.val
+
995 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
996 if ((unsigned)(last
.bits
+ this.bits
) <= bits
) break;
1001 DROPBITS(this.bits
);
1002 state
->length
= (unsigned)this.val
;
1003 if ((int)(this.op
) == 0) {
1004 Tracevv((stderr
, this.val
>= 0x20 && this.val
< 0x7f ?
1005 "inflate: literal '%c'\n" :
1006 "inflate: literal 0x%02x\n", this.val
));
1011 Tracevv((stderr
, "inflate: end of block\n"));
1016 strm
->msg
= (char *)"invalid literal/length code";
1020 state
->extra
= (unsigned)(this.op
) & 15;
1021 state
->mode
= LENEXT
;
1024 NEEDBITS(state
->extra
);
1025 state
->length
+= BITS(state
->extra
);
1026 DROPBITS(state
->extra
);
1028 Tracevv((stderr
, "inflate: length %u\n", state
->length
));
1032 this = state
->distcode
[BITS(state
->distbits
)];
1033 if ((unsigned)(this.bits
) <= bits
) break;
1036 if ((this.op
& 0xf0) == 0) {
1039 this = state
->distcode
[last
.val
+
1040 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
1041 if ((unsigned)(last
.bits
+ this.bits
) <= bits
) break;
1044 DROPBITS(last
.bits
);
1046 DROPBITS(this.bits
);
1048 strm
->msg
= (char *)"invalid distance code";
1052 state
->offset
= (unsigned)this.val
;
1053 state
->extra
= (unsigned)(this.op
) & 15;
1054 state
->mode
= DISTEXT
;
1057 NEEDBITS(state
->extra
);
1058 state
->offset
+= BITS(state
->extra
);
1059 DROPBITS(state
->extra
);
1061 #ifdef INFLATE_STRICT
1062 if (state
->offset
> state
->dmax
) {
1063 strm
->msg
= (char *)"invalid distance too far back";
1068 if (state
->offset
> state
->whave
+ out
- left
) {
1069 strm
->msg
= (char *)"invalid distance too far back";
1073 Tracevv((stderr
, "inflate: distance %u\n", state
->offset
));
1074 state
->mode
= MATCH
;
1076 if (left
== 0) goto inf_leave
;
1078 if (state
->offset
> copy
) { /* copy from window */
1079 copy
= state
->offset
- copy
;
1080 if (copy
> state
->write
) {
1081 copy
-= state
->write
;
1082 from
= state
->window
+ (state
->wsize
- copy
);
1085 from
= state
->window
+ (state
->write
- copy
);
1086 if (copy
> state
->length
) copy
= state
->length
;
1088 else { /* copy from output */
1089 from
= put
- state
->offset
;
1090 copy
= state
->length
;
1092 if (copy
> left
) copy
= left
;
1094 state
->length
-= copy
;
1098 if (state
->length
== 0) state
->mode
= LEN
;
1101 if (left
== 0) goto inf_leave
;
1102 *put
++ = (unsigned char)(state
->length
);
1110 strm
->total_out
+= out
;
1111 state
->total
+= out
;
1113 strm
->adler
= state
->check
=
1114 UPDATE(state
->check
, put
- out
, out
);
1118 state
->flags
? hold
:
1120 REVERSE(hold
)) != state
->check
) {
1121 strm
->msg
= (char *)"incorrect data check";
1126 Tracev((stderr
, "inflate: check matches trailer\n"));
1129 state
->mode
= LENGTH
;
1131 if (state
->wrap
&& state
->flags
) {
1133 if (hold
!= (state
->total
& 0xffffffffUL
)) {
1134 strm
->msg
= (char *)"incorrect length check";
1139 Tracev((stderr
, "inflate: length matches trailer\n"));
1153 return Z_STREAM_ERROR
;
1157 Return from inflate(), updating the total counts and the check value.
1158 If there was no progress during the inflate() call, return a buffer
1159 error. Call updatewindow() to create and/or update the window state.
1160 Note: a memory error from inflate() is non-recoverable.
1164 if (state
->wsize
|| (state
->mode
< CHECK
&& out
!= strm
->avail_out
))
1165 if (updatewindow(strm
, out
)) {
1169 in
-= strm
->avail_in
;
1170 out
-= strm
->avail_out
;
1171 strm
->total_in
+= in
;
1172 strm
->total_out
+= out
;
1173 state
->total
+= out
;
1174 if (state
->wrap
&& out
)
1175 strm
->adler
= state
->check
=
1176 UPDATE(state
->check
, strm
->next_out
- out
, out
);
1177 strm
->data_type
= state
->bits
+ (state
->last
? 64 : 0) +
1178 (state
->mode
== TYPE
? 128 : 0);
1179 if (((in
== 0 && out
== 0) || flush
== Z_FINISH
) && ret
== Z_OK
)
1184 int ZEXPORT
inflateEnd(strm
)
1187 struct inflate_state FAR
*state
;
1188 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
|| strm
->zfree
== (free_func
)0)
1189 return Z_STREAM_ERROR
;
1190 state
= (struct inflate_state FAR
*)strm
->state
;
1191 if (state
->window
!= Z_NULL
) ZFREE(strm
, state
->window
);
1192 ZFREE(strm
, strm
->state
);
1193 strm
->state
= Z_NULL
;
1194 Tracev((stderr
, "inflate: end\n"));
1198 int ZEXPORT
inflateSetDictionary(strm
, dictionary
, dictLength
)
1200 const Bytef
*dictionary
;
1203 struct inflate_state FAR
*state
;
1207 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
) return Z_STREAM_ERROR
;
1208 state
= (struct inflate_state FAR
*)strm
->state
;
1209 if (state
->wrap
!= 0 && state
->mode
!= DICT
)
1210 return Z_STREAM_ERROR
;
1212 /* check for correct dictionary id */
1213 if (state
->mode
== DICT
) {
1214 id
= adler32(0L, Z_NULL
, 0);
1215 id
= adler32(id
, dictionary
, dictLength
);
1216 if (id
!= state
->check
)
1217 return Z_DATA_ERROR
;
1220 /* copy dictionary to window */
1221 if (updatewindow(strm
, strm
->avail_out
)) {
1225 if (dictLength
> state
->wsize
) {
1226 zmemcpy(state
->window
, dictionary
+ dictLength
- state
->wsize
,
1228 state
->whave
= state
->wsize
;
1231 zmemcpy(state
->window
+ state
->wsize
- dictLength
, dictionary
,
1233 state
->whave
= dictLength
;
1235 state
->havedict
= 1;
1236 Tracev((stderr
, "inflate: dictionary set\n"));
1240 int ZEXPORT
inflateGetHeader(strm
, head
)
1244 struct inflate_state FAR
*state
;
1247 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
) return Z_STREAM_ERROR
;
1248 state
= (struct inflate_state FAR
*)strm
->state
;
1249 if ((state
->wrap
& 2) == 0) return Z_STREAM_ERROR
;
1251 /* save header structure */
1258 Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
1259 or when out of input. When called, *have is the number of pattern bytes
1260 found in order so far, in 0..3. On return *have is updated to the new
1261 state. If on return *have equals four, then the pattern was found and the
1262 return value is how many bytes were read including the last byte of the
1263 pattern. If *have is less than four, then the pattern has not been found
1264 yet and the return value is len. In the latter case, syncsearch() can be
1265 called again with more data and the *have state. *have is initialized to
1266 zero for the first call.
1268 local
unsigned syncsearch(have
, buf
, len
)
1270 unsigned char FAR
*buf
;
1278 while (next
< len
&& got
< 4) {
1279 if ((int)(buf
[next
]) == (got
< 2 ? 0 : 0xff))
1291 int ZEXPORT
inflateSync(strm
)
1294 unsigned len
; /* number of bytes to look at or looked at */
1295 unsigned long in
, out
; /* temporary to save total_in and total_out */
1296 unsigned char buf
[4]; /* to restore bit buffer to byte string */
1297 struct inflate_state FAR
*state
;
1299 /* check parameters */
1300 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
) return Z_STREAM_ERROR
;
1301 state
= (struct inflate_state FAR
*)strm
->state
;
1302 if (strm
->avail_in
== 0 && state
->bits
< 8) return Z_BUF_ERROR
;
1304 /* if first time, start search in bit buffer */
1305 if (state
->mode
!= SYNC
) {
1307 state
->hold
<<= state
->bits
& 7;
1308 state
->bits
-= state
->bits
& 7;
1310 while (state
->bits
>= 8) {
1311 buf
[len
++] = (unsigned char)(state
->hold
);
1316 syncsearch(&(state
->have
), buf
, len
);
1319 /* search available input */
1320 len
= syncsearch(&(state
->have
), strm
->next_in
, strm
->avail_in
);
1321 strm
->avail_in
-= len
;
1322 strm
->next_in
+= len
;
1323 strm
->total_in
+= len
;
1325 /* return no joy or set up to restart inflate() on a new block */
1326 if (state
->have
!= 4) return Z_DATA_ERROR
;
1327 in
= strm
->total_in
; out
= strm
->total_out
;
1329 strm
->total_in
= in
; strm
->total_out
= out
;
1335 Returns true if inflate is currently at the end of a block generated by
1336 Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1337 implementation to provide an additional safety check. PPP uses
1338 Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1339 block. When decompressing, PPP checks that at the end of input packet,
1340 inflate is waiting for these length bytes.
1342 int ZEXPORT
inflateSyncPoint(strm
)
1345 struct inflate_state FAR
*state
;
1347 if (strm
== Z_NULL
|| strm
->state
== Z_NULL
) return Z_STREAM_ERROR
;
1348 state
= (struct inflate_state FAR
*)strm
->state
;
1349 return state
->mode
== STORED
&& state
->bits
== 0;
1352 int ZEXPORT
inflateCopy(dest
, source
)
1356 struct inflate_state FAR
*state
;
1357 struct inflate_state FAR
*copy
;
1358 unsigned char FAR
*window
;
1362 if (dest
== Z_NULL
|| source
== Z_NULL
|| source
->state
== Z_NULL
||
1363 source
->zalloc
== (alloc_func
)0 || source
->zfree
== (free_func
)0)
1364 return Z_STREAM_ERROR
;
1365 state
= (struct inflate_state FAR
*)source
->state
;
1367 /* allocate space */
1368 copy
= (struct inflate_state FAR
*)
1369 ZALLOC(source
, 1, sizeof(struct inflate_state
));
1370 if (copy
== Z_NULL
) return Z_MEM_ERROR
;
1372 if (state
->window
!= Z_NULL
) {
1373 window
= (unsigned char FAR
*)
1374 ZALLOC(source
, 1U << state
->wbits
, sizeof(unsigned char));
1375 if (window
== Z_NULL
) {
1376 ZFREE(source
, copy
);
1382 zmemcpy(dest
, source
, sizeof(z_stream
));
1383 zmemcpy(copy
, state
, sizeof(struct inflate_state
));
1384 if (state
->lencode
>= state
->codes
&&
1385 state
->lencode
<= state
->codes
+ ENOUGH
- 1) {
1386 copy
->lencode
= copy
->codes
+ (state
->lencode
- state
->codes
);
1387 copy
->distcode
= copy
->codes
+ (state
->distcode
- state
->codes
);
1389 copy
->next
= copy
->codes
+ (state
->next
- state
->codes
);
1390 if (window
!= Z_NULL
) {
1391 wsize
= 1U << state
->wbits
;
1392 zmemcpy(window
, state
->window
, wsize
);
1394 copy
->window
= window
;
1395 dest
->state
= (struct internal_state FAR
*)copy
;