1 /* inflate.c -- zlib decompression
3 * Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 * 2. Altered source versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software.
19 * 3. This notice may not be removed or altered from any source distribution.
21 * Jean-loup Gailly Mark Adler
22 * jloup@gzip.org madler@alumni.caltech.edu
32 #define DEF_WBITS MAX_WBITS
33 #define zmemcpy memcpy
34 #define zmemzero(dest, len) memset(dest, 0, len)
36 #define Assert(cond,msg)
44 #define ZALLOC(strm, items, size) \
45 (*((strm)->zalloc))((strm)->opaque, (items), (size))
46 #define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
47 #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
49 /* Reverse the bytes in a 32-bit value */
50 #define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
51 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
53 #define BASE 65521U /* largest prime smaller than 65536 */
55 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
57 #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
58 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
59 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
60 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
61 #define DO16(buf) DO8(buf,0); DO8(buf,8);
63 #define MOD(a) a %= BASE
64 #define MOD28(a) a %= BASE
65 #define MOD63(a) a %= BASE
67 static uLong
adler32( uLong adler
, const Bytef
*buf
, uInt len
)
72 /* split Adler-32 into component sums */
73 sum2
= (adler
>> 16) & 0xffff;
76 /* in case user likes doing a byte at a time, keep it fast */
84 return adler
| (sum2
<< 16);
87 /* initial Adler-32 value (deferred check for len == 1 speed) */
91 /* in case short lengths are provided, keep it somewhat fast */
99 MOD28(sum2
); /* only added so many BASE's */
100 return adler
| (sum2
<< 16);
103 /* do length NMAX blocks -- requires just one modulo operation */
104 while (len
>= NMAX
) {
106 n
= NMAX
/ 16; /* NMAX is divisible by 16 */
108 DO16(buf
); /* 16 sums unrolled */
115 /* do remaining bytes (less than NMAX, still just one modulo) */
116 if (len
) { /* avoid modulos if none remaining */
130 /* return recombined sums */
131 return adler
| (sum2
<< 16);
135 unsigned char op
; /* operation, extra bits, table bits */
136 unsigned char bits
; /* bits in this part of the code */
137 unsigned short val
; /* offset in table or code value */
140 #define ENOUGH_LENS 852
141 #define ENOUGH_DISTS 592
142 #define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS)
144 /* Type of code to build for inflate_table() */
151 /* Possible inflate modes between inflate() calls */
153 HEAD
= 16180, /* i: waiting for magic header */
154 FLAGS
, /* i: waiting for method and flags (gzip) */
155 TIME
, /* i: waiting for modification time (gzip) */
156 OS
, /* i: waiting for extra flags and operating system (gzip) */
157 EXLEN
, /* i: waiting for extra length (gzip) */
158 EXTRA
, /* i: waiting for extra bytes (gzip) */
159 NAME
, /* i: waiting for end of file name (gzip) */
160 COMMENT
, /* i: waiting for end of comment (gzip) */
161 HCRC
, /* i: waiting for header crc (gzip) */
162 DICTID
, /* i: waiting for dictionary check value */
163 DICT
, /* waiting for inflateSetDictionary() call */
164 TYPE
, /* i: waiting for type bits, including last-flag bit */
165 TYPEDO
, /* i: same, but skip check to exit inflate on new block */
166 STORED
, /* i: waiting for stored size (length and complement) */
167 COPY_
, /* i/o: same as COPY below, but only first time in */
168 COPY
, /* i/o: waiting for input or output to copy stored block */
169 TABLE
, /* i: waiting for dynamic block table lengths */
170 LENLENS
, /* i: waiting for code length code lengths */
171 CODELENS
, /* i: waiting for length/lit and distance code lengths */
172 LEN_
, /* i: same as LEN below, but only first time in */
173 LEN
, /* i: waiting for length/lit/eob code */
174 LENEXT
, /* i: waiting for length extra bits */
175 DIST
, /* i: waiting for distance code */
176 DISTEXT
, /* i: waiting for distance extra bits */
177 MATCH
, /* o: waiting for output space to copy string */
178 LIT
, /* o: waiting for output space to write literal */
179 CHECK
, /* i: waiting for 32-bit check value */
180 LENGTH
, /* i: waiting for 32-bit length (gzip) */
181 DONE
, /* finished check, done -- remain here until reset */
182 BAD
, /* got a data error -- remain here until reset */
183 MEM
, /* got an inflate() memory error -- remain here until reset */
184 SYNC
/* looking for synchronization bytes to restart inflate() */
187 /* State maintained between inflate() calls -- approximately 7K bytes, not
188 including the allocated sliding window, which is up to 32K bytes. */
189 struct inflate_state
{
190 z_streamp strm
; /* pointer back to this zlib stream */
191 inflate_mode mode
; /* current inflate mode */
192 int last
; /* true if processing last block */
193 int wrap
; /* bit 0 true for zlib, bit 1 true for gzip,
194 bit 2 true to validate check value */
195 int havedict
; /* true if dictionary provided */
196 int flags
; /* gzip header method and flags (0 if zlib) */
197 unsigned dmax
; /* zlib header max distance (INFLATE_STRICT) */
198 unsigned long check
; /* protected copy of check value */
199 unsigned long total
; /* protected copy of output count */
200 gz_headerp head
; /* where to save gzip header information */
202 unsigned wbits
; /* log base 2 of requested window size */
203 unsigned wsize
; /* window size or zero if not using window */
204 unsigned whave
; /* valid bytes in the window */
205 unsigned wnext
; /* window write index */
206 unsigned char FAR
*window
; /* allocated sliding window, if needed */
207 /* bit accumulator */
208 unsigned long hold
; /* input bit accumulator */
209 unsigned bits
; /* number of bits in "in" */
210 /* for string and stored block copying */
211 unsigned length
; /* literal or length of data to copy */
212 unsigned offset
; /* distance back to copy string from */
213 /* for table and code decoding */
214 unsigned extra
; /* extra bits needed */
215 /* fixed and dynamic code tables */
216 code
const FAR
*lencode
; /* starting table for length/literal codes */
217 code
const FAR
*distcode
; /* starting table for distance codes */
218 unsigned lenbits
; /* index bits for lencode */
219 unsigned distbits
; /* index bits for distcode */
220 /* dynamic table building */
221 unsigned ncode
; /* number of code length code lengths */
222 unsigned nlen
; /* number of length code lengths */
223 unsigned ndist
; /* number of distance code lengths */
224 unsigned have
; /* number of code lengths in lens[] */
225 code FAR
*next
; /* next available space in codes[] */
226 unsigned short lens
[320]; /* temporary storage for code lengths */
227 unsigned short work
[288]; /* work area for code table building */
228 code codes
[ENOUGH
]; /* space for code tables */
229 int sane
; /* if false, allow invalid distance too far */
230 int back
; /* bits back of last unprocessed length/lit */
231 unsigned was
; /* initial length of match */
235 Decode literal, length, and distance codes and write out the resulting
236 literal and match bytes until either not enough input or output is
237 available, an end-of-block is encountered, or a data error is encountered.
238 When large enough input and output buffers are supplied to inflate(), for
239 example, a 16K input buffer and a 64K output buffer, more than 95% of the
240 inflate execution time is spent in this routine.
246 strm->avail_out >= 258
247 start >= strm->avail_out
250 On return, state->mode is one of:
252 LEN -- ran out of enough output space or enough available input
253 TYPE -- reached end of block code, inflate() to interpret next block
254 BAD -- error in block data
258 - The maximum input bits used by a length/distance pair is 15 bits for the
259 length code, 5 bits for the length extra, 15 bits for the distance code,
260 and 13 bits for the distance extra. This totals 48 bits, or six bytes.
261 Therefore if strm->avail_in >= 6, then there is enough input to avoid
262 checking for available input while decoding.
264 - The maximum bytes that a single length/distance pair can output is 258
265 bytes, which is the maximum length that can be coded. inflate_fast()
266 requires strm->avail_out >= 258 for each loop to avoid checking for
269 static void inflate_fast( z_streamp strm
, unsigned start
)
271 struct inflate_state FAR
*state
;
272 z_const
unsigned char FAR
*in
; /* local strm->next_in */
273 z_const
unsigned char FAR
*last
; /* have enough input while in < last */
274 unsigned char FAR
*out
; /* local strm->next_out */
275 unsigned char FAR
*beg
; /* inflate()'s initial strm->next_out */
276 unsigned char FAR
*end
; /* while out < end, enough space available */
277 #ifdef INFLATE_STRICT
278 unsigned dmax
; /* maximum distance from zlib header */
280 unsigned wsize
; /* window size or zero if not using window */
281 unsigned whave
; /* valid bytes in the window */
282 unsigned wnext
; /* window write index */
283 unsigned char FAR
*window
; /* allocated sliding window, if wsize != 0 */
284 unsigned long hold
; /* local strm->hold */
285 unsigned bits
; /* local strm->bits */
286 code
const FAR
*lcode
; /* local strm->lencode */
287 code
const FAR
*dcode
; /* local strm->distcode */
288 unsigned lmask
; /* mask for first level of length codes */
289 unsigned dmask
; /* mask for first level of distance codes */
290 code here
; /* retrieved table entry */
291 unsigned op
; /* code bits, operation, extra bits, or */
292 /* window position, window bytes to copy */
293 unsigned len
; /* match length, unused bytes */
294 unsigned dist
; /* match distance */
295 unsigned char FAR
*from
; /* where to copy match from */
297 /* copy state to local variables */
298 state
= (struct inflate_state FAR
*)strm
->state
;
300 last
= in
+ (strm
->avail_in
- 5);
301 out
= strm
->next_out
;
302 beg
= out
- (start
- strm
->avail_out
);
303 end
= out
+ (strm
->avail_out
- 257);
304 #ifdef INFLATE_STRICT
307 wsize
= state
->wsize
;
308 whave
= state
->whave
;
309 wnext
= state
->wnext
;
310 window
= state
->window
;
313 lcode
= state
->lencode
;
314 dcode
= state
->distcode
;
315 lmask
= (1U << state
->lenbits
) - 1;
316 dmask
= (1U << state
->distbits
) - 1;
318 /* decode literals and length/distances until end-of-block or not enough
319 input data or output space */
322 hold
+= (unsigned long)(*in
++) << bits
;
324 hold
+= (unsigned long)(*in
++) << bits
;
327 here
= lcode
[hold
& lmask
];
329 op
= (unsigned)(here
.bits
);
332 op
= (unsigned)(here
.op
);
333 if (op
== 0) { /* literal */
334 Tracevv((stderr
, here
.val
>= 0x20 && here
.val
< 0x7f ?
335 "inflate: literal '%c'\n" :
336 "inflate: literal 0x%02x\n", here
.val
));
337 *out
++ = (unsigned char)(here
.val
);
339 else if (op
& 16) { /* length base */
340 len
= (unsigned)(here
.val
);
341 op
&= 15; /* number of extra bits */
344 hold
+= (unsigned long)(*in
++) << bits
;
347 len
+= (unsigned)hold
& ((1U << op
) - 1);
351 Tracevv((stderr
, "inflate: length %u\n", len
));
353 hold
+= (unsigned long)(*in
++) << bits
;
355 hold
+= (unsigned long)(*in
++) << bits
;
358 here
= dcode
[hold
& dmask
];
360 op
= (unsigned)(here
.bits
);
363 op
= (unsigned)(here
.op
);
364 if (op
& 16) { /* distance base */
365 dist
= (unsigned)(here
.val
);
366 op
&= 15; /* number of extra bits */
368 hold
+= (unsigned long)(*in
++) << bits
;
371 hold
+= (unsigned long)(*in
++) << bits
;
375 dist
+= (unsigned)hold
& ((1U << op
) - 1);
376 #ifdef INFLATE_STRICT
378 strm
->msg
= (char *)"invalid distance too far back";
385 Tracevv((stderr
, "inflate: distance %u\n", dist
));
386 op
= (unsigned)(out
- beg
); /* max distance in output */
387 if (dist
> op
) { /* see if copy from window */
388 op
= dist
- op
; /* distance back in window */
392 (char *)"invalid distance too far back";
396 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
397 if (len
<= op
- whave
) {
406 } while (--op
> whave
);
417 if (wnext
== 0) { /* very common case */
419 if (op
< len
) { /* some from window */
424 from
= out
- dist
; /* rest from output */
427 else if (wnext
< op
) { /* wrap around window */
428 from
+= wsize
+ wnext
- op
;
430 if (op
< len
) { /* some from end of window */
436 if (wnext
< len
) { /* some from start of window */
442 from
= out
- dist
; /* rest from output */
446 else { /* contiguous in window */
448 if (op
< len
) { /* some from window */
453 from
= out
- dist
; /* rest from output */
469 from
= out
- dist
; /* copy direct from output */
470 do { /* minimum length is three */
483 else if ((op
& 64) == 0) { /* 2nd level distance code */
484 here
= dcode
[here
.val
+ (hold
& ((1U << op
) - 1))];
488 strm
->msg
= (char *)"invalid distance code";
493 else if ((op
& 64) == 0) { /* 2nd level length code */
494 here
= lcode
[here
.val
+ (hold
& ((1U << op
) - 1))];
497 else if (op
& 32) { /* end-of-block */
498 Tracevv((stderr
, "inflate: end of block\n"));
503 strm
->msg
= (char *)"invalid literal/length code";
507 } while (in
< last
&& out
< end
);
509 /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
513 hold
&= (1U << bits
) - 1;
515 /* update state and return */
517 strm
->next_out
= out
;
518 strm
->avail_in
= (unsigned)(in
< last
? 5 + (last
- in
) : 5 - (in
- last
));
519 strm
->avail_out
= (unsigned)(out
< end
?
520 257 + (end
- out
) : 257 - (out
- end
));
528 static int inflate_table( codetype type
, unsigned short FAR
*lens
, unsigned codes
, code FAR
* FAR
*table
,
529 unsigned FAR
*bits
, unsigned short FAR
*work
)
531 unsigned len
; /* a code's length in bits */
532 unsigned sym
; /* index of code symbols */
533 unsigned min
, max
; /* minimum and maximum code lengths */
534 unsigned root
; /* number of index bits for root table */
535 unsigned curr
; /* number of index bits for current table */
536 unsigned drop
; /* code bits to drop for sub-table */
537 int left
; /* number of prefix codes available */
538 unsigned used
; /* code entries in table used */
539 unsigned huff
; /* Huffman code */
540 unsigned incr
; /* for incrementing code, index */
541 unsigned fill
; /* index for replicating entries */
542 unsigned low
; /* low bits for current root entry */
543 unsigned mask
; /* mask for low root bits */
544 code here
; /* table entry for duplication */
545 code FAR
*next
; /* next available space in table */
546 const unsigned short FAR
*base
; /* base value table to use */
547 const unsigned short FAR
*extra
; /* extra bits table to use */
548 unsigned match
; /* use base and extra for symbol >= match */
549 unsigned short count
[MAXBITS
+1]; /* number of codes of each length */
550 unsigned short offs
[MAXBITS
+1]; /* offsets in table for each length */
551 static const unsigned short lbase
[31] = { /* Length codes 257..285 base */
552 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
553 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
554 static const unsigned short lext
[31] = { /* Length codes 257..285 extra */
555 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
556 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 77, 202};
557 static const unsigned short dbase
[32] = { /* Distance codes 0..29 base */
558 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
559 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
560 8193, 12289, 16385, 24577, 0, 0};
561 static const unsigned short dext
[32] = { /* Distance codes 0..29 extra */
562 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
563 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
564 28, 28, 29, 29, 64, 64};
567 Process a set of code lengths to create a canonical Huffman code. The
568 code lengths are lens[0..codes-1]. Each length corresponds to the
569 symbols 0..codes-1. The Huffman code is generated by first sorting the
570 symbols by length from short to long, and retaining the symbol order
571 for codes with equal lengths. Then the code starts with all zero bits
572 for the first code of the shortest length, and the codes are integer
573 increments for the same length, and zeros are appended as the length
574 increases. For the deflate format, these bits are stored backwards
575 from their more natural integer increment ordering, and so when the
576 decoding tables are built in the large loop below, the integer codes
577 are incremented backwards.
579 This routine assumes, but does not check, that all of the entries in
580 lens[] are in the range 0..MAXBITS. The caller must assure this.
581 1..MAXBITS is interpreted as that code length. zero means that that
582 symbol does not occur in this code.
584 The codes are sorted by computing a count of codes for each length,
585 creating from that a table of starting indices for each length in the
586 sorted table, and then entering the symbols in order in the sorted
587 table. The sorted table is work[], with that space being provided by
590 The length counts are used for other purposes as well, i.e. finding
591 the minimum and maximum length codes, determining if there are any
592 codes at all, checking for a valid set of lengths, and looking ahead
593 at length counts to determine sub-table sizes when building the
597 /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
598 for (len
= 0; len
<= MAXBITS
; len
++)
600 for (sym
= 0; sym
< codes
; sym
++)
603 /* bound code lengths, force root to be within code lengths */
605 for (max
= MAXBITS
; max
>= 1; max
--)
606 if (count
[max
] != 0) break;
607 if (root
> max
) root
= max
;
608 if (max
== 0) { /* no symbols to code at all */
609 here
.op
= (unsigned char)64; /* invalid code marker */
610 here
.bits
= (unsigned char)1;
611 here
.val
= (unsigned short)0;
612 *(*table
)++ = here
; /* make a table to force an error */
615 return 0; /* no symbols, but wait for decoding to report error */
617 for (min
= 1; min
< max
; min
++)
618 if (count
[min
] != 0) break;
619 if (root
< min
) root
= min
;
621 /* check for an over-subscribed or incomplete set of lengths */
623 for (len
= 1; len
<= MAXBITS
; len
++) {
626 if (left
< 0) return -1; /* over-subscribed */
628 if (left
> 0 && (type
== CODES
|| max
!= 1))
629 return -1; /* incomplete set */
631 /* generate offsets into symbol table for each length for sorting */
633 for (len
= 1; len
< MAXBITS
; len
++)
634 offs
[len
+ 1] = offs
[len
] + count
[len
];
636 /* sort symbols by length, by symbol order within each length */
637 for (sym
= 0; sym
< codes
; sym
++)
638 if (lens
[sym
] != 0) work
[offs
[lens
[sym
]]++] = (unsigned short)sym
;
641 Create and fill in decoding tables. In this loop, the table being
642 filled is at next and has curr index bits. The code being used is huff
643 with length len. That code is converted to an index by dropping drop
644 bits off of the bottom. For codes where len is less than drop + curr,
645 those top drop + curr - len bits are incremented through all values to
646 fill the table with replicated entries.
648 root is the number of index bits for the root table. When len exceeds
649 root, sub-tables are created pointed to by the root entry with an index
650 of the low root bits of huff. This is saved in low to check for when a
651 new sub-table should be started. drop is zero when the root table is
652 being filled, and drop is root when sub-tables are being filled.
654 When a new sub-table is needed, it is necessary to look ahead in the
655 code lengths to determine what size sub-table is needed. The length
656 counts are used for this, and so count[] is decremented as codes are
657 entered in the tables.
659 used keeps track of how many table entries have been allocated from the
660 provided *table space. It is checked for LENS and DIST tables against
661 the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
662 the initial root table size constants. See the comments in inftrees.h
663 for more information.
665 sym increments through all symbols, and the loop terminates when
666 all codes of length max, i.e. all codes, have been processed. This
667 routine permits incomplete codes, so another loop after this one fills
668 in the rest of the decoding tables with invalid code markers.
671 /* set up for code type */
674 base
= extra
= work
; /* dummy value--not used */
688 /* initialize state for loop */
689 huff
= 0; /* starting code */
690 sym
= 0; /* starting code symbol */
691 len
= min
; /* starting code length */
692 next
= *table
; /* current table to fill in */
693 curr
= root
; /* current table index bits */
694 drop
= 0; /* current bits to drop from code for index */
695 low
= (unsigned)(-1); /* trigger new sub-table when len > root */
696 used
= 1U << root
; /* use root table entries */
697 mask
= used
- 1; /* mask for comparing low */
699 /* check available table space */
700 if ((type
== LENS
&& used
> ENOUGH_LENS
) ||
701 (type
== DISTS
&& used
> ENOUGH_DISTS
))
704 /* process all codes and make table entries */
706 /* create table entry */
707 here
.bits
= (unsigned char)(len
- drop
);
708 if (work
[sym
] + 1U < match
) {
709 here
.op
= (unsigned char)0;
710 here
.val
= work
[sym
];
712 else if (work
[sym
] >= match
) {
713 here
.op
= (unsigned char)(extra
[work
[sym
] - match
]);
714 here
.val
= base
[work
[sym
] - match
];
717 here
.op
= (unsigned char)(32 + 64); /* end of block */
721 /* replicate for those indices with low len bits equal to huff */
722 incr
= 1U << (len
- drop
);
724 min
= fill
; /* save offset to next table */
727 next
[(huff
>> drop
) + fill
] = here
;
730 /* backwards increment the len-bit code huff */
731 incr
= 1U << (len
- 1);
741 /* go to next symbol, update count, len */
743 if (--(count
[len
]) == 0) {
744 if (len
== max
) break;
745 len
= lens
[work
[sym
]];
748 /* create new sub-table if needed */
749 if (len
> root
&& (huff
& mask
) != low
) {
750 /* if first time, transition to sub-tables */
754 /* increment past last table */
755 next
+= min
; /* here min is 1 << curr */
757 /* determine length of next table */
759 left
= (int)(1 << curr
);
760 while (curr
+ drop
< max
) {
761 left
-= count
[curr
+ drop
];
762 if (left
<= 0) break;
767 /* check for enough space */
769 if ((type
== LENS
&& used
> ENOUGH_LENS
) ||
770 (type
== DISTS
&& used
> ENOUGH_DISTS
))
773 /* point entry in root table to sub-table */
775 (*table
)[low
].op
= (unsigned char)curr
;
776 (*table
)[low
].bits
= (unsigned char)root
;
777 (*table
)[low
].val
= (unsigned short)(next
- *table
);
781 /* fill in remaining table entry if code is incomplete (guaranteed to have
782 at most one remaining entry, since if the code is incomplete, the
783 maximum code length that was allowed to get this far is one bit) */
785 here
.op
= (unsigned char)64; /* invalid code marker */
786 here
.bits
= (unsigned char)(len
- drop
);
787 here
.val
= (unsigned short)0;
791 /* set return parameters */
797 static int inflateStateCheck( z_streamp strm
)
799 struct inflate_state FAR
*state
;
800 if (strm
== Z_NULL
||
801 strm
->zalloc
== (alloc_func
)0 || strm
->zfree
== (free_func
)0)
803 state
= (struct inflate_state FAR
*)strm
->state
;
804 if (state
== Z_NULL
|| state
->strm
!= strm
||
805 state
->mode
< HEAD
|| state
->mode
> SYNC
)
810 static int inflateResetKeep( z_streamp strm
)
812 struct inflate_state FAR
*state
;
814 if (inflateStateCheck(strm
)) return Z_STREAM_ERROR
;
815 state
= (struct inflate_state FAR
*)strm
->state
;
816 strm
->total_in
= strm
->total_out
= state
->total
= 0;
818 if (state
->wrap
) /* to support ill-conceived Java test suite */
819 strm
->adler
= state
->wrap
& 1;
823 state
->dmax
= 32768U;
824 state
->head
= Z_NULL
;
827 state
->lencode
= state
->distcode
= state
->next
= state
->codes
;
830 Tracev((stderr
, "inflate: reset\n"));
834 static int inflateReset( z_streamp strm
)
836 struct inflate_state FAR
*state
;
838 if (inflateStateCheck(strm
)) return Z_STREAM_ERROR
;
839 state
= (struct inflate_state FAR
*)strm
->state
;
843 return inflateResetKeep(strm
);
846 static int inflateReset2( z_streamp strm
, int windowBits
)
849 struct inflate_state FAR
*state
;
852 if (inflateStateCheck(strm
)) return Z_STREAM_ERROR
;
853 state
= (struct inflate_state FAR
*)strm
->state
;
855 /* extract wrap request from windowBits parameter */
856 if (windowBits
< 0) {
858 windowBits
= -windowBits
;
861 wrap
= (windowBits
>> 4) + 5;
868 /* set number of window bits, free window if different */
869 if (windowBits
&& (windowBits
< 8 || windowBits
> 15))
870 return Z_STREAM_ERROR
;
871 if (state
->window
!= Z_NULL
&& state
->wbits
!= (unsigned)windowBits
) {
872 ZFREE(strm
, state
->window
);
873 state
->window
= Z_NULL
;
876 /* update state and reset the rest of it */
878 state
->wbits
= (unsigned)windowBits
;
879 return inflateReset(strm
);
882 int inflateInit2( z_streamp strm
, int windowBits
)
885 struct inflate_state FAR
*state
;
887 strm
->msg
= Z_NULL
; /* in case we return an error */
888 state
= (struct inflate_state FAR
*)
889 ZALLOC(strm
, 1, sizeof(struct inflate_state
));
890 if (state
== Z_NULL
) return Z_MEM_ERROR
;
891 Tracev((stderr
, "inflate: allocated\n"));
892 strm
->state
= (struct internal_state FAR
*)state
;
894 state
->window
= Z_NULL
;
895 state
->mode
= HEAD
; /* to pass state test in inflateReset2() */
896 ret
= inflateReset2(strm
, windowBits
);
899 strm
->state
= Z_NULL
;
904 int inflateInit( z_streamp strm
)
906 return inflateInit2(strm
, DEF_WBITS
);
910 Return state with length and distance decoding tables and index sizes set to
911 fixed code decoding. Normally this returns fixed tables from inffixed.h.
912 If BUILDFIXED is defined, then instead this routine builds the tables the
913 first time it's called, and returns those tables the first time and
914 thereafter. This reduces the size of the code by about 2K bytes, in
915 exchange for a little execution time. However, BUILDFIXED should not be
916 used for threaded applications, since the rewriting of the tables and virgin
917 may not be thread-safe.
919 static void fixedtables( struct inflate_state FAR
*state
)
921 static const code lenfix
[512] = {
922 {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48},
923 {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128},
924 {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59},
925 {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176},
926 {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20},
927 {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100},
928 {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8},
929 {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216},
930 {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76},
931 {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114},
932 {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2},
933 {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148},
934 {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42},
935 {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86},
936 {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15},
937 {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236},
938 {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62},
939 {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142},
940 {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31},
941 {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162},
942 {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25},
943 {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105},
944 {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4},
945 {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202},
946 {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69},
947 {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125},
948 {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13},
949 {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195},
950 {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35},
951 {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91},
952 {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19},
953 {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246},
954 {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55},
955 {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135},
956 {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99},
957 {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190},
958 {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16},
959 {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96},
960 {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6},
961 {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209},
962 {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72},
963 {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116},
964 {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4},
965 {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153},
966 {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44},
967 {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82},
968 {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11},
969 {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229},
970 {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58},
971 {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138},
972 {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51},
973 {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173},
974 {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30},
975 {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110},
976 {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0},
977 {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195},
978 {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65},
979 {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121},
980 {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9},
981 {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258},
982 {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37},
983 {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93},
984 {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23},
985 {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251},
986 {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51},
987 {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131},
988 {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67},
989 {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183},
990 {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23},
991 {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103},
992 {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9},
993 {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223},
994 {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79},
998 static const code distfix
[32] = {
999 {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025},
1000 {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193},
1001 {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385},
1002 {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577},
1003 {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073},
1007 state
->lencode
= lenfix
;
1009 state
->distcode
= distfix
;
1010 state
->distbits
= 5;
1014 Update the window with the last wsize (normally 32K) bytes written before
1015 returning. If window does not exist yet, create it. This is only called
1016 when a window is already in use, or when output has been written during this
1017 inflate call, but the end of the deflate stream has not been reached yet.
1018 It is also called to create a window for dictionary data when a dictionary
1021 Providing output buffers larger than 32K to inflate() should provide a speed
1022 advantage, since only the last 32K of output is copied to the sliding window
1023 upon return from inflate(), and since all distances after the first 32K of
1024 output will fall in the output data, making match copies simpler and faster.
1025 The advantage may be dependent on the size of the processor's data caches.
1027 static int updatewindow( z_streamp strm
, const Bytef
*end
, unsigned copy
)
1029 struct inflate_state FAR
*state
;
1032 state
= (struct inflate_state FAR
*)strm
->state
;
1034 /* if it hasn't been done already, allocate space for the window */
1035 if (state
->window
== Z_NULL
) {
1036 state
->window
= (unsigned char FAR
*)
1037 ZALLOC(strm
, 1U << state
->wbits
,
1038 sizeof(unsigned char));
1039 if (state
->window
== Z_NULL
) return 1;
1042 /* if window not in use yet, initialize */
1043 if (state
->wsize
== 0) {
1044 state
->wsize
= 1U << state
->wbits
;
1049 /* copy state->wsize or less output bytes into the circular window */
1050 if (copy
>= state
->wsize
) {
1051 zmemcpy(state
->window
, end
- state
->wsize
, state
->wsize
);
1053 state
->whave
= state
->wsize
;
1056 dist
= state
->wsize
- state
->wnext
;
1057 if (dist
> copy
) dist
= copy
;
1058 zmemcpy(state
->window
+ state
->wnext
, end
- copy
, dist
);
1061 zmemcpy(state
->window
, end
- copy
, copy
);
1062 state
->wnext
= copy
;
1063 state
->whave
= state
->wsize
;
1066 state
->wnext
+= dist
;
1067 if (state
->wnext
== state
->wsize
) state
->wnext
= 0;
1068 if (state
->whave
< state
->wsize
) state
->whave
+= dist
;
1074 /* Macros for inflate(): */
1076 #define crc32(crc,buf,len) RtlComputeCrc32(crc,buf,len)
1078 /* check function to use adler32() for zlib or crc32() for gzip */
1080 # define UPDATE(check, buf, len) \
1081 (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
1083 # define UPDATE(check, buf, len) adler32(check, buf, len)
1086 /* check macros for header crc */
1088 # define CRC2(check, word) \
1090 hbuf[0] = (unsigned char)(word); \
1091 hbuf[1] = (unsigned char)((word) >> 8); \
1092 check = crc32(check, hbuf, 2); \
1095 # define CRC4(check, word) \
1097 hbuf[0] = (unsigned char)(word); \
1098 hbuf[1] = (unsigned char)((word) >> 8); \
1099 hbuf[2] = (unsigned char)((word) >> 16); \
1100 hbuf[3] = (unsigned char)((word) >> 24); \
1101 check = crc32(check, hbuf, 4); \
1105 /* Load registers with state in inflate() for speed */
1108 put = strm->next_out; \
1109 left = strm->avail_out; \
1110 next = strm->next_in; \
1111 have = strm->avail_in; \
1112 hold = state->hold; \
1113 bits = state->bits; \
1116 /* Restore state from registers in inflate() */
1119 strm->next_out = put; \
1120 strm->avail_out = left; \
1121 strm->next_in = next; \
1122 strm->avail_in = have; \
1123 state->hold = hold; \
1124 state->bits = bits; \
1127 /* Clear the input bit accumulator */
1128 #define INITBITS() \
1134 /* Get a byte of input into the bit accumulator, or return from inflate()
1135 if there is no input available. */
1136 #define PULLBYTE() \
1138 if (have == 0) goto inf_leave; \
1140 hold += (unsigned long)(*next++) << bits; \
1144 /* Assure that there are at least n bits in the bit accumulator. If there is
1145 not enough available input to do that, then return from inflate(). */
1146 #define NEEDBITS(n) \
1148 while (bits < (unsigned)(n)) \
1152 /* Return the low n bits of the bit accumulator (n < 16) */
1154 ((unsigned)hold & ((1U << (n)) - 1))
1156 /* Remove n bits from the bit accumulator */
1157 #define DROPBITS(n) \
1160 bits -= (unsigned)(n); \
1163 /* Remove zero to seven bits as needed to go to a byte boundary */
1164 #define BYTEBITS() \
1166 hold >>= bits & 7; \
1171 inflate() uses a state machine to process as much input data and generate as
1172 much output data as possible before returning. The state machine is
1173 structured roughly as follows:
1175 for (;;) switch (state) {
1178 if (not enough input data or output space to make progress)
1180 ... make progress ...
1186 so when inflate() is called again, the same case is attempted again, and
1187 if the appropriate resources are provided, the machine proceeds to the
1188 next state. The NEEDBITS() macro is usually the way the state evaluates
1189 whether it can proceed or should return. NEEDBITS() does the return if
1190 the requested bits are not available. The typical use of the BITS macros
1194 ... do something with BITS(n) ...
1197 where NEEDBITS(n) either returns from inflate() if there isn't enough
1198 input left to load n bits into the accumulator, or it continues. BITS(n)
1199 gives the low n bits in the accumulator. When done, DROPBITS(n) drops
1200 the low n bits off the accumulator. INITBITS() clears the accumulator
1201 and sets the number of available bits to zero. BYTEBITS() discards just
1202 enough bits to put the accumulator on a byte boundary. After BYTEBITS()
1203 and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
1205 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
1206 if there is no input available. The decoding of variable length codes uses
1207 PULLBYTE() directly in order to pull just enough bytes to decode the next
1210 Some states loop until they get enough input, making sure that enough
1211 state information is maintained to continue the loop where it left off
1212 if NEEDBITS() returns in the loop. For example, want, need, and keep
1213 would all have to actually be part of the saved state in case NEEDBITS()
1217 while (want < need) {
1219 keep[want++] = BITS(n);
1225 As shown above, if the next state is also the next case, then the break
1228 A state may also return if there is not enough output space available to
1229 complete that state. Those states are copying stored data, writing a
1230 literal byte, and copying a matching string.
1232 When returning, a "goto inf_leave" is used to update the total counters,
1233 update the check value, and determine whether any progress has been made
1234 during that inflate() call in order to return the proper return code.
1235 Progress is defined as a change in either strm->avail_in or strm->avail_out.
1236 When there is a window, goto inf_leave will update the window with the last
1237 output written. If a goto inf_leave occurs in the middle of decompression
1238 and there is no window currently, goto inf_leave will create one and copy
1239 output to the window for the next call of inflate().
1241 In this implementation, the flush parameter of inflate() only affects the
1242 return code (per zlib.h). inflate() always writes as much as possible to
1243 strm->next_out, given the space available and the provided input--the effect
1244 documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
1245 the allocation of and copying into a sliding window until necessary, which
1246 provides the effect documented in zlib.h for Z_FINISH when the entire input
1247 stream available. So the only thing the flush parameter actually does is:
1248 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
1249 will return Z_BUF_ERROR if it has not reached the end of the stream.
1252 int inflate( z_streamp strm
, int flush
)
1254 struct inflate_state FAR
*state
;
1255 z_const
unsigned char FAR
*next
; /* next input */
1256 unsigned char FAR
*put
; /* next output */
1257 unsigned have
, left
; /* available input and output */
1258 unsigned long hold
; /* bit buffer */
1259 unsigned bits
; /* bits in bit buffer */
1260 unsigned in
, out
; /* save starting available input and output */
1261 unsigned copy
; /* number of stored or match bytes to copy */
1262 unsigned char FAR
*from
; /* where to copy match bytes from */
1263 code here
; /* current decoding table entry */
1264 code last
; /* parent table entry */
1265 unsigned len
; /* length to copy for repeats, bits to drop */
1266 int ret
; /* return code */
1268 unsigned char hbuf
[4]; /* buffer for gzip header crc calculation */
1270 static const unsigned short order
[19] = /* permutation of code lengths */
1271 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
1273 if (inflateStateCheck(strm
) || strm
->next_out
== Z_NULL
||
1274 (strm
->next_in
== Z_NULL
&& strm
->avail_in
!= 0))
1275 return Z_STREAM_ERROR
;
1277 state
= (struct inflate_state FAR
*)strm
->state
;
1278 if (state
->mode
== TYPE
) state
->mode
= TYPEDO
; /* skip check */
1284 switch (state
->mode
) {
1286 if (state
->wrap
== 0) {
1287 state
->mode
= TYPEDO
;
1292 if ((state
->wrap
& 2) && hold
== 0x8b1f) { /* gzip header */
1293 if (state
->wbits
== 0)
1295 state
->check
= crc32(0L, Z_NULL
, 0);
1296 CRC2(state
->check
, hold
);
1298 state
->mode
= FLAGS
;
1301 state
->flags
= 0; /* expect zlib header */
1302 if (state
->head
!= Z_NULL
)
1303 state
->head
->done
= -1;
1304 if (!(state
->wrap
& 1) || /* check if zlib header allowed */
1308 ((BITS(8) << 8) + (hold
>> 8)) % 31) {
1309 strm
->msg
= (char *)"incorrect header check";
1313 if (BITS(4) != Z_DEFLATED
) {
1314 strm
->msg
= (char *)"unknown compression method";
1320 if (state
->wbits
== 0)
1322 if (len
> 15 || len
> state
->wbits
) {
1323 strm
->msg
= (char *)"invalid window size";
1327 state
->dmax
= 1U << len
;
1328 Tracev((stderr
, "inflate: zlib header ok\n"));
1329 strm
->adler
= state
->check
= adler32(0L, Z_NULL
, 0);
1330 state
->mode
= hold
& 0x200 ? DICTID
: TYPE
;
1336 state
->flags
= (int)(hold
);
1337 if ((state
->flags
& 0xff) != Z_DEFLATED
) {
1338 strm
->msg
= (char *)"unknown compression method";
1342 if (state
->flags
& 0xe000) {
1343 strm
->msg
= (char *)"unknown header flags set";
1347 if (state
->head
!= Z_NULL
)
1348 state
->head
->text
= (int)((hold
>> 8) & 1);
1349 if ((state
->flags
& 0x0200) && (state
->wrap
& 4))
1350 CRC2(state
->check
, hold
);
1355 if (state
->head
!= Z_NULL
)
1356 state
->head
->time
= hold
;
1357 if ((state
->flags
& 0x0200) && (state
->wrap
& 4))
1358 CRC4(state
->check
, hold
);
1363 if (state
->head
!= Z_NULL
) {
1364 state
->head
->xflags
= (int)(hold
& 0xff);
1365 state
->head
->os
= (int)(hold
>> 8);
1367 if ((state
->flags
& 0x0200) && (state
->wrap
& 4))
1368 CRC2(state
->check
, hold
);
1370 state
->mode
= EXLEN
;
1372 if (state
->flags
& 0x0400) {
1374 state
->length
= (unsigned)(hold
);
1375 if (state
->head
!= Z_NULL
)
1376 state
->head
->extra_len
= (unsigned)hold
;
1377 if ((state
->flags
& 0x0200) && (state
->wrap
& 4))
1378 CRC2(state
->check
, hold
);
1381 else if (state
->head
!= Z_NULL
)
1382 state
->head
->extra
= Z_NULL
;
1383 state
->mode
= EXTRA
;
1385 if (state
->flags
& 0x0400) {
1386 copy
= state
->length
;
1387 if (copy
> have
) copy
= have
;
1389 if (state
->head
!= Z_NULL
&&
1390 state
->head
->extra
!= Z_NULL
) {
1391 len
= state
->head
->extra_len
- state
->length
;
1392 zmemcpy(state
->head
->extra
+ len
, next
,
1393 len
+ copy
> state
->head
->extra_max
?
1394 state
->head
->extra_max
- len
: copy
);
1396 if ((state
->flags
& 0x0200) && (state
->wrap
& 4))
1397 state
->check
= crc32(state
->check
, next
, copy
);
1400 state
->length
-= copy
;
1402 if (state
->length
) goto inf_leave
;
1407 if (state
->flags
& 0x0800) {
1408 if (have
== 0) goto inf_leave
;
1411 len
= (unsigned)(next
[copy
++]);
1412 if (state
->head
!= Z_NULL
&&
1413 state
->head
->name
!= Z_NULL
&&
1414 state
->length
< state
->head
->name_max
)
1415 state
->head
->name
[state
->length
++] = (Bytef
)len
;
1416 } while (len
&& copy
< have
);
1417 if ((state
->flags
& 0x0200) && (state
->wrap
& 4))
1418 state
->check
= crc32(state
->check
, next
, copy
);
1421 if (len
) goto inf_leave
;
1423 else if (state
->head
!= Z_NULL
)
1424 state
->head
->name
= Z_NULL
;
1426 state
->mode
= COMMENT
;
1428 if (state
->flags
& 0x1000) {
1429 if (have
== 0) goto inf_leave
;
1432 len
= (unsigned)(next
[copy
++]);
1433 if (state
->head
!= Z_NULL
&&
1434 state
->head
->comment
!= Z_NULL
&&
1435 state
->length
< state
->head
->comm_max
)
1436 state
->head
->comment
[state
->length
++] = (Bytef
)len
;
1437 } while (len
&& copy
< have
);
1438 if ((state
->flags
& 0x0200) && (state
->wrap
& 4))
1439 state
->check
= crc32(state
->check
, next
, copy
);
1442 if (len
) goto inf_leave
;
1444 else if (state
->head
!= Z_NULL
)
1445 state
->head
->comment
= Z_NULL
;
1448 if (state
->flags
& 0x0200) {
1450 if ((state
->wrap
& 4) && hold
!= (state
->check
& 0xffff)) {
1451 strm
->msg
= (char *)"header crc mismatch";
1457 if (state
->head
!= Z_NULL
) {
1458 state
->head
->hcrc
= (int)((state
->flags
>> 9) & 1);
1459 state
->head
->done
= 1;
1461 strm
->adler
= state
->check
= crc32(0L, Z_NULL
, 0);
1467 strm
->adler
= state
->check
= ZSWAP32(hold
);
1471 if (state
->havedict
== 0) {
1475 strm
->adler
= state
->check
= adler32(0L, Z_NULL
, 0);
1478 if (flush
== Z_BLOCK
|| flush
== Z_TREES
) goto inf_leave
;
1482 state
->mode
= CHECK
;
1486 state
->last
= BITS(1);
1489 case 0: /* stored block */
1490 Tracev((stderr
, "inflate: stored block%s\n",
1491 state
->last
? " (last)" : ""));
1492 state
->mode
= STORED
;
1494 case 1: /* fixed block */
1496 Tracev((stderr
, "inflate: fixed codes block%s\n",
1497 state
->last
? " (last)" : ""));
1498 state
->mode
= LEN_
; /* decode codes */
1499 if (flush
== Z_TREES
) {
1504 case 2: /* dynamic block */
1505 Tracev((stderr
, "inflate: dynamic codes block%s\n",
1506 state
->last
? " (last)" : ""));
1507 state
->mode
= TABLE
;
1510 strm
->msg
= (char *)"invalid block type";
1516 BYTEBITS(); /* go to byte boundary */
1518 if ((hold
& 0xffff) != ((hold
>> 16) ^ 0xffff)) {
1519 strm
->msg
= (char *)"invalid stored block lengths";
1523 state
->length
= (unsigned)hold
& 0xffff;
1524 Tracev((stderr
, "inflate: stored length %u\n",
1527 state
->mode
= COPY_
;
1528 if (flush
== Z_TREES
) goto inf_leave
;
1532 copy
= state
->length
;
1534 if (copy
> have
) copy
= have
;
1535 if (copy
> left
) copy
= left
;
1536 if (copy
== 0) goto inf_leave
;
1537 zmemcpy(put
, next
, copy
);
1542 state
->length
-= copy
;
1545 Tracev((stderr
, "inflate: stored end\n"));
1550 state
->nlen
= BITS(5) + 257;
1552 state
->ndist
= BITS(5) + 1;
1554 state
->ncode
= BITS(4) + 4;
1556 #ifndef PKZIP_BUG_WORKAROUND
1557 if (state
->nlen
> 286 || state
->ndist
> 30) {
1558 strm
->msg
= (char *)"too many length or distance symbols";
1563 Tracev((stderr
, "inflate: table sizes ok\n"));
1565 state
->mode
= LENLENS
;
1567 while (state
->have
< state
->ncode
) {
1569 state
->lens
[order
[state
->have
++]] = (unsigned short)BITS(3);
1572 while (state
->have
< 19)
1573 state
->lens
[order
[state
->have
++]] = 0;
1574 state
->next
= state
->codes
;
1575 state
->lencode
= (const code FAR
*)(state
->next
);
1577 ret
= inflate_table(CODES
, state
->lens
, 19, &(state
->next
),
1578 &(state
->lenbits
), state
->work
);
1580 strm
->msg
= (char *)"invalid code lengths set";
1584 Tracev((stderr
, "inflate: code lengths ok\n"));
1586 state
->mode
= CODELENS
;
1588 while (state
->have
< state
->nlen
+ state
->ndist
) {
1590 here
= state
->lencode
[BITS(state
->lenbits
)];
1591 if ((unsigned)(here
.bits
) <= bits
) break;
1594 if (here
.val
< 16) {
1595 DROPBITS(here
.bits
);
1596 state
->lens
[state
->have
++] = here
.val
;
1599 if (here
.val
== 16) {
1600 NEEDBITS(here
.bits
+ 2);
1601 DROPBITS(here
.bits
);
1602 if (state
->have
== 0) {
1603 strm
->msg
= (char *)"invalid bit length repeat";
1607 len
= state
->lens
[state
->have
- 1];
1611 else if (here
.val
== 17) {
1612 NEEDBITS(here
.bits
+ 3);
1613 DROPBITS(here
.bits
);
1619 NEEDBITS(here
.bits
+ 7);
1620 DROPBITS(here
.bits
);
1622 copy
= 11 + BITS(7);
1625 if (state
->have
+ copy
> state
->nlen
+ state
->ndist
) {
1626 strm
->msg
= (char *)"invalid bit length repeat";
1631 state
->lens
[state
->have
++] = (unsigned short)len
;
1635 /* handle error breaks in while */
1636 if (state
->mode
== BAD
) break;
1638 /* check for end-of-block code (better have one) */
1639 if (state
->lens
[256] == 0) {
1640 strm
->msg
= (char *)"invalid code -- missing end-of-block";
1645 /* build code tables -- note: do not change the lenbits or distbits
1646 values here (9 and 6) without reading the comments in inftrees.h
1647 concerning the ENOUGH constants, which depend on those values */
1648 state
->next
= state
->codes
;
1649 state
->lencode
= (const code FAR
*)(state
->next
);
1651 ret
= inflate_table(LENS
, state
->lens
, state
->nlen
, &(state
->next
),
1652 &(state
->lenbits
), state
->work
);
1654 strm
->msg
= (char *)"invalid literal/lengths set";
1658 state
->distcode
= (const code FAR
*)(state
->next
);
1659 state
->distbits
= 6;
1660 ret
= inflate_table(DISTS
, state
->lens
+ state
->nlen
, state
->ndist
,
1661 &(state
->next
), &(state
->distbits
), state
->work
);
1663 strm
->msg
= (char *)"invalid distances set";
1667 Tracev((stderr
, "inflate: codes ok\n"));
1669 if (flush
== Z_TREES
) goto inf_leave
;
1673 if (have
>= 6 && left
>= 258) {
1675 inflate_fast(strm
, out
);
1677 if (state
->mode
== TYPE
)
1683 here
= state
->lencode
[BITS(state
->lenbits
)];
1684 if ((unsigned)(here
.bits
) <= bits
) break;
1687 if (here
.op
&& (here
.op
& 0xf0) == 0) {
1690 here
= state
->lencode
[last
.val
+
1691 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
1692 if ((unsigned)(last
.bits
+ here
.bits
) <= bits
) break;
1695 DROPBITS(last
.bits
);
1696 state
->back
+= last
.bits
;
1698 DROPBITS(here
.bits
);
1699 state
->back
+= here
.bits
;
1700 state
->length
= (unsigned)here
.val
;
1701 if ((int)(here
.op
) == 0) {
1702 Tracevv((stderr
, here
.val
>= 0x20 && here
.val
< 0x7f ?
1703 "inflate: literal '%c'\n" :
1704 "inflate: literal 0x%02x\n", here
.val
));
1709 Tracevv((stderr
, "inflate: end of block\n"));
1715 strm
->msg
= (char *)"invalid literal/length code";
1719 state
->extra
= (unsigned)(here
.op
) & 15;
1720 state
->mode
= LENEXT
;
1723 NEEDBITS(state
->extra
);
1724 state
->length
+= BITS(state
->extra
);
1725 DROPBITS(state
->extra
);
1726 state
->back
+= state
->extra
;
1728 Tracevv((stderr
, "inflate: length %u\n", state
->length
));
1729 state
->was
= state
->length
;
1733 here
= state
->distcode
[BITS(state
->distbits
)];
1734 if ((unsigned)(here
.bits
) <= bits
) break;
1737 if ((here
.op
& 0xf0) == 0) {
1740 here
= state
->distcode
[last
.val
+
1741 (BITS(last
.bits
+ last
.op
) >> last
.bits
)];
1742 if ((unsigned)(last
.bits
+ here
.bits
) <= bits
) break;
1745 DROPBITS(last
.bits
);
1746 state
->back
+= last
.bits
;
1748 DROPBITS(here
.bits
);
1749 state
->back
+= here
.bits
;
1751 strm
->msg
= (char *)"invalid distance code";
1755 state
->offset
= (unsigned)here
.val
;
1756 state
->extra
= (unsigned)(here
.op
) & 15;
1757 state
->mode
= DISTEXT
;
1760 NEEDBITS(state
->extra
);
1761 state
->offset
+= BITS(state
->extra
);
1762 DROPBITS(state
->extra
);
1763 state
->back
+= state
->extra
;
1765 #ifdef INFLATE_STRICT
1766 if (state
->offset
> state
->dmax
) {
1767 strm
->msg
= (char *)"invalid distance too far back";
1772 Tracevv((stderr
, "inflate: distance %u\n", state
->offset
));
1773 state
->mode
= MATCH
;
1775 if (left
== 0) goto inf_leave
;
1777 if (state
->offset
> copy
) { /* copy from window */
1778 copy
= state
->offset
- copy
;
1779 if (copy
> state
->whave
) {
1781 strm
->msg
= (char *)"invalid distance too far back";
1785 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1786 Trace((stderr
, "inflate.c too far\n"));
1787 copy
-= state
->whave
;
1788 if (copy
> state
->length
) copy
= state
->length
;
1789 if (copy
> left
) copy
= left
;
1791 state
->length
-= copy
;
1795 if (state
->length
== 0) state
->mode
= LEN
;
1799 if (copy
> state
->wnext
) {
1800 copy
-= state
->wnext
;
1801 from
= state
->window
+ (state
->wsize
- copy
);
1804 from
= state
->window
+ (state
->wnext
- copy
);
1805 if (copy
> state
->length
) copy
= state
->length
;
1807 else { /* copy from output */
1808 from
= put
- state
->offset
;
1809 copy
= state
->length
;
1811 if (copy
> left
) copy
= left
;
1813 state
->length
-= copy
;
1817 if (state
->length
== 0) state
->mode
= LEN
;
1820 if (left
== 0) goto inf_leave
;
1821 *put
++ = (unsigned char)(state
->length
);
1829 strm
->total_out
+= out
;
1830 state
->total
+= out
;
1831 if ((state
->wrap
& 4) && out
)
1832 strm
->adler
= state
->check
=
1833 UPDATE(state
->check
, put
- out
, out
);
1835 if ((state
->wrap
& 4) && (
1837 state
->flags
? hold
:
1839 ZSWAP32(hold
)) != state
->check
) {
1840 strm
->msg
= (char *)"incorrect data check";
1845 Tracev((stderr
, "inflate: check matches trailer\n"));
1848 state
->mode
= LENGTH
;
1850 if (state
->wrap
&& state
->flags
) {
1852 if (hold
!= (state
->total
& 0xffffffffUL
)) {
1853 strm
->msg
= (char *)"incorrect length check";
1858 Tracev((stderr
, "inflate: length matches trailer\n"));
1872 return Z_STREAM_ERROR
;
1876 Return from inflate(), updating the total counts and the check value.
1877 If there was no progress during the inflate() call, return a buffer
1878 error. Call updatewindow() to create and/or update the window state.
1879 Note: a memory error from inflate() is non-recoverable.
1883 if (state
->wsize
|| (out
!= strm
->avail_out
&& state
->mode
< BAD
&&
1884 (state
->mode
< CHECK
|| flush
!= Z_FINISH
)))
1885 if (updatewindow(strm
, strm
->next_out
, out
- strm
->avail_out
)) {
1889 in
-= strm
->avail_in
;
1890 out
-= strm
->avail_out
;
1891 strm
->total_in
+= in
;
1892 strm
->total_out
+= out
;
1893 state
->total
+= out
;
1894 if ((state
->wrap
& 4) && out
)
1895 strm
->adler
= state
->check
=
1896 UPDATE(state
->check
, strm
->next_out
- out
, out
);
1897 strm
->data_type
= (int)state
->bits
+ (state
->last
? 64 : 0) +
1898 (state
->mode
== TYPE
? 128 : 0) +
1899 (state
->mode
== LEN_
|| state
->mode
== COPY_
? 256 : 0);
1900 if (((in
== 0 && out
== 0) || flush
== Z_FINISH
) && ret
== Z_OK
)
1905 int inflateEnd(z_streamp strm
)
1907 struct inflate_state FAR
*state
;
1908 if (inflateStateCheck(strm
))
1909 return Z_STREAM_ERROR
;
1910 state
= (struct inflate_state FAR
*)strm
->state
;
1911 if (state
->window
!= Z_NULL
) ZFREE(strm
, state
->window
);
1912 ZFREE(strm
, strm
->state
);
1913 strm
->state
= Z_NULL
;
1914 Tracev((stderr
, "inflate: end\n"));