ntdll: Use cpu_info to check for AVX availability.
[wine.git] / dlls / wininet / inflate.c
blob1f2d65ea3271c04cdc66eb8087360056501a15f4
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
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <limits.h>
29 #include "winternl.h"
30 #include "zlib.h"
32 #define DEF_WBITS MAX_WBITS
33 #define zmemcpy memcpy
34 #define zmemzero(dest, len) memset(dest, 0, len)
36 #define Assert(cond,msg)
37 #define Trace(x)
38 #define Tracev(x)
39 #define Tracevv(x)
40 #define Tracecv(c,x)
42 #define GUNZIP
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 */
54 #define NMAX 5552
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 )
69 unsigned long sum2;
70 unsigned n;
72 /* split Adler-32 into component sums */
73 sum2 = (adler >> 16) & 0xffff;
74 adler &= 0xffff;
76 /* in case user likes doing a byte at a time, keep it fast */
77 if (len == 1) {
78 adler += buf[0];
79 if (adler >= BASE)
80 adler -= BASE;
81 sum2 += adler;
82 if (sum2 >= BASE)
83 sum2 -= BASE;
84 return adler | (sum2 << 16);
87 /* initial Adler-32 value (deferred check for len == 1 speed) */
88 if (buf == Z_NULL)
89 return 1L;
91 /* in case short lengths are provided, keep it somewhat fast */
92 if (len < 16) {
93 while (len--) {
94 adler += *buf++;
95 sum2 += adler;
97 if (adler >= BASE)
98 adler -= BASE;
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) {
105 len -= NMAX;
106 n = NMAX / 16; /* NMAX is divisible by 16 */
107 do {
108 DO16(buf); /* 16 sums unrolled */
109 buf += 16;
110 } while (--n);
111 MOD(adler);
112 MOD(sum2);
115 /* do remaining bytes (less than NMAX, still just one modulo) */
116 if (len) { /* avoid modulos if none remaining */
117 while (len >= 16) {
118 len -= 16;
119 DO16(buf);
120 buf += 16;
122 while (len--) {
123 adler += *buf++;
124 sum2 += adler;
126 MOD(adler);
127 MOD(sum2);
130 /* return recombined sums */
131 return adler | (sum2 << 16);
134 typedef struct {
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 */
138 } code;
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() */
145 typedef enum {
146 CODES,
147 LENS,
148 DISTS
149 } codetype;
151 /* Possible inflate modes between inflate() calls */
152 typedef enum {
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() */
185 } inflate_mode;
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 */
201 /* sliding window */
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.
242 Entry assumptions:
244 state->mode == LEN
245 strm->avail_in >= 6
246 strm->avail_out >= 258
247 start >= strm->avail_out
248 state->bits < 8
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
256 Notes:
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
267 output space.
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 */
279 #endif
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;
299 in = strm->next_in;
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
305 dmax = state->dmax;
306 #endif
307 wsize = state->wsize;
308 whave = state->whave;
309 wnext = state->wnext;
310 window = state->window;
311 hold = state->hold;
312 bits = state->bits;
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 */
320 do {
321 if (bits < 15) {
322 hold += (unsigned long)(*in++) << bits;
323 bits += 8;
324 hold += (unsigned long)(*in++) << bits;
325 bits += 8;
327 here = lcode[hold & lmask];
328 dolen:
329 op = (unsigned)(here.bits);
330 hold >>= op;
331 bits -= op;
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 */
342 if (op) {
343 if (bits < op) {
344 hold += (unsigned long)(*in++) << bits;
345 bits += 8;
347 len += (unsigned)hold & ((1U << op) - 1);
348 hold >>= op;
349 bits -= op;
351 Tracevv((stderr, "inflate: length %u\n", len));
352 if (bits < 15) {
353 hold += (unsigned long)(*in++) << bits;
354 bits += 8;
355 hold += (unsigned long)(*in++) << bits;
356 bits += 8;
358 here = dcode[hold & dmask];
359 dodist:
360 op = (unsigned)(here.bits);
361 hold >>= op;
362 bits -= op;
363 op = (unsigned)(here.op);
364 if (op & 16) { /* distance base */
365 dist = (unsigned)(here.val);
366 op &= 15; /* number of extra bits */
367 if (bits < op) {
368 hold += (unsigned long)(*in++) << bits;
369 bits += 8;
370 if (bits < op) {
371 hold += (unsigned long)(*in++) << bits;
372 bits += 8;
375 dist += (unsigned)hold & ((1U << op) - 1);
376 #ifdef INFLATE_STRICT
377 if (dist > dmax) {
378 strm->msg = (char *)"invalid distance too far back";
379 state->mode = BAD;
380 break;
382 #endif
383 hold >>= op;
384 bits -= op;
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 */
389 if (op > whave) {
390 if (state->sane) {
391 strm->msg =
392 (char *)"invalid distance too far back";
393 state->mode = BAD;
394 break;
396 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
397 if (len <= op - whave) {
398 do {
399 *out++ = 0;
400 } while (--len);
401 continue;
403 len -= op - whave;
404 do {
405 *out++ = 0;
406 } while (--op > whave);
407 if (op == 0) {
408 from = out - dist;
409 do {
410 *out++ = *from++;
411 } while (--len);
412 continue;
414 #endif
416 from = window;
417 if (wnext == 0) { /* very common case */
418 from += wsize - op;
419 if (op < len) { /* some from window */
420 len -= op;
421 do {
422 *out++ = *from++;
423 } while (--op);
424 from = out - dist; /* rest from output */
427 else if (wnext < op) { /* wrap around window */
428 from += wsize + wnext - op;
429 op -= wnext;
430 if (op < len) { /* some from end of window */
431 len -= op;
432 do {
433 *out++ = *from++;
434 } while (--op);
435 from = window;
436 if (wnext < len) { /* some from start of window */
437 op = wnext;
438 len -= op;
439 do {
440 *out++ = *from++;
441 } while (--op);
442 from = out - dist; /* rest from output */
446 else { /* contiguous in window */
447 from += wnext - op;
448 if (op < len) { /* some from window */
449 len -= op;
450 do {
451 *out++ = *from++;
452 } while (--op);
453 from = out - dist; /* rest from output */
456 while (len > 2) {
457 *out++ = *from++;
458 *out++ = *from++;
459 *out++ = *from++;
460 len -= 3;
462 if (len) {
463 *out++ = *from++;
464 if (len > 1)
465 *out++ = *from++;
468 else {
469 from = out - dist; /* copy direct from output */
470 do { /* minimum length is three */
471 *out++ = *from++;
472 *out++ = *from++;
473 *out++ = *from++;
474 len -= 3;
475 } while (len > 2);
476 if (len) {
477 *out++ = *from++;
478 if (len > 1)
479 *out++ = *from++;
483 else if ((op & 64) == 0) { /* 2nd level distance code */
484 here = dcode[here.val + (hold & ((1U << op) - 1))];
485 goto dodist;
487 else {
488 strm->msg = (char *)"invalid distance code";
489 state->mode = BAD;
490 break;
493 else if ((op & 64) == 0) { /* 2nd level length code */
494 here = lcode[here.val + (hold & ((1U << op) - 1))];
495 goto dolen;
497 else if (op & 32) { /* end-of-block */
498 Tracevv((stderr, "inflate: end of block\n"));
499 state->mode = TYPE;
500 break;
502 else {
503 strm->msg = (char *)"invalid literal/length code";
504 state->mode = BAD;
505 break;
507 } while (in < last && out < end);
509 /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
510 len = bits >> 3;
511 in -= len;
512 bits -= len << 3;
513 hold &= (1U << bits) - 1;
515 /* update state and return */
516 strm->next_in = in;
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));
521 state->hold = hold;
522 state->bits = bits;
523 return;
526 #define MAXBITS 15
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
588 the caller.
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
594 decoding tables.
597 /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
598 for (len = 0; len <= MAXBITS; len++)
599 count[len] = 0;
600 for (sym = 0; sym < codes; sym++)
601 count[lens[sym]]++;
603 /* bound code lengths, force root to be within code lengths */
604 root = *bits;
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 */
613 *(*table)++ = here;
614 *bits = 1;
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 */
622 left = 1;
623 for (len = 1; len <= MAXBITS; len++) {
624 left <<= 1;
625 left -= count[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 */
632 offs[1] = 0;
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 */
672 switch (type) {
673 case CODES:
674 base = extra = work; /* dummy value--not used */
675 match = 20;
676 break;
677 case LENS:
678 base = lbase;
679 extra = lext;
680 match = 257;
681 break;
682 default: /* DISTS */
683 base = dbase;
684 extra = dext;
685 match = 0;
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))
702 return 1;
704 /* process all codes and make table entries */
705 for (;;) {
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];
716 else {
717 here.op = (unsigned char)(32 + 64); /* end of block */
718 here.val = 0;
721 /* replicate for those indices with low len bits equal to huff */
722 incr = 1U << (len - drop);
723 fill = 1U << curr;
724 min = fill; /* save offset to next table */
725 do {
726 fill -= incr;
727 next[(huff >> drop) + fill] = here;
728 } while (fill != 0);
730 /* backwards increment the len-bit code huff */
731 incr = 1U << (len - 1);
732 while (huff & incr)
733 incr >>= 1;
734 if (incr != 0) {
735 huff &= incr - 1;
736 huff += incr;
738 else
739 huff = 0;
741 /* go to next symbol, update count, len */
742 sym++;
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 */
751 if (drop == 0)
752 drop = root;
754 /* increment past last table */
755 next += min; /* here min is 1 << curr */
757 /* determine length of next table */
758 curr = len - drop;
759 left = (int)(1 << curr);
760 while (curr + drop < max) {
761 left -= count[curr + drop];
762 if (left <= 0) break;
763 curr++;
764 left <<= 1;
767 /* check for enough space */
768 used += 1U << curr;
769 if ((type == LENS && used > ENOUGH_LENS) ||
770 (type == DISTS && used > ENOUGH_DISTS))
771 return 1;
773 /* point entry in root table to sub-table */
774 low = huff & mask;
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) */
784 if (huff != 0) {
785 here.op = (unsigned char)64; /* invalid code marker */
786 here.bits = (unsigned char)(len - drop);
787 here.val = (unsigned short)0;
788 next[huff] = here;
791 /* set return parameters */
792 *table += used;
793 *bits = root;
794 return 0;
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)
802 return 1;
803 state = (struct inflate_state FAR *)strm->state;
804 if (state == Z_NULL || state->strm != strm ||
805 state->mode < HEAD || state->mode > SYNC)
806 return 1;
807 return 0;
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;
817 strm->msg = Z_NULL;
818 if (state->wrap) /* to support ill-conceived Java test suite */
819 strm->adler = state->wrap & 1;
820 state->mode = HEAD;
821 state->last = 0;
822 state->havedict = 0;
823 state->dmax = 32768U;
824 state->head = Z_NULL;
825 state->hold = 0;
826 state->bits = 0;
827 state->lencode = state->distcode = state->next = state->codes;
828 state->sane = 1;
829 state->back = -1;
830 Tracev((stderr, "inflate: reset\n"));
831 return Z_OK;
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;
840 state->wsize = 0;
841 state->whave = 0;
842 state->wnext = 0;
843 return inflateResetKeep(strm);
846 static int inflateReset2( z_streamp strm, int windowBits )
848 int wrap;
849 struct inflate_state FAR *state;
851 /* get the 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) {
857 wrap = 0;
858 windowBits = -windowBits;
860 else {
861 wrap = (windowBits >> 4) + 5;
862 #ifdef GUNZIP
863 if (windowBits < 48)
864 windowBits &= 15;
865 #endif
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 */
877 state->wrap = wrap;
878 state->wbits = (unsigned)windowBits;
879 return inflateReset(strm);
882 int inflateInit2( z_streamp strm, int windowBits )
884 int ret;
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;
893 state->strm = strm;
894 state->window = Z_NULL;
895 state->mode = HEAD; /* to pass state test in inflateReset2() */
896 ret = inflateReset2(strm, windowBits);
897 if (ret != Z_OK) {
898 ZFREE(strm, state);
899 strm->state = Z_NULL;
901 return ret;
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},
995 {0,9,255}
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},
1004 {22,5,193},{64,5,0}
1007 state->lencode = lenfix;
1008 state->lenbits = 9;
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
1019 is loaded.
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;
1030 unsigned dist;
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;
1045 state->wnext = 0;
1046 state->whave = 0;
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);
1052 state->wnext = 0;
1053 state->whave = state->wsize;
1055 else {
1056 dist = state->wsize - state->wnext;
1057 if (dist > copy) dist = copy;
1058 zmemcpy(state->window + state->wnext, end - copy, dist);
1059 copy -= dist;
1060 if (copy) {
1061 zmemcpy(state->window, end - copy, copy);
1062 state->wnext = copy;
1063 state->whave = state->wsize;
1065 else {
1066 state->wnext += dist;
1067 if (state->wnext == state->wsize) state->wnext = 0;
1068 if (state->whave < state->wsize) state->whave += dist;
1071 return 0;
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 */
1079 #ifdef GUNZIP
1080 # define UPDATE(check, buf, len) \
1081 (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
1082 #else
1083 # define UPDATE(check, buf, len) adler32(check, buf, len)
1084 #endif
1086 /* check macros for header crc */
1087 #ifdef GUNZIP
1088 # define CRC2(check, word) \
1089 do { \
1090 hbuf[0] = (unsigned char)(word); \
1091 hbuf[1] = (unsigned char)((word) >> 8); \
1092 check = crc32(check, hbuf, 2); \
1093 } while (0)
1095 # define CRC4(check, word) \
1096 do { \
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); \
1102 } while (0)
1103 #endif
1105 /* Load registers with state in inflate() for speed */
1106 #define LOAD() \
1107 do { \
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; \
1114 } while (0)
1116 /* Restore state from registers in inflate() */
1117 #define RESTORE() \
1118 do { \
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; \
1125 } while (0)
1127 /* Clear the input bit accumulator */
1128 #define INITBITS() \
1129 do { \
1130 hold = 0; \
1131 bits = 0; \
1132 } while (0)
1134 /* Get a byte of input into the bit accumulator, or return from inflate()
1135 if there is no input available. */
1136 #define PULLBYTE() \
1137 do { \
1138 if (have == 0) goto inf_leave; \
1139 have--; \
1140 hold += (unsigned long)(*next++) << bits; \
1141 bits += 8; \
1142 } while (0)
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) \
1147 do { \
1148 while (bits < (unsigned)(n)) \
1149 PULLBYTE(); \
1150 } while (0)
1152 /* Return the low n bits of the bit accumulator (n < 16) */
1153 #define BITS(n) \
1154 ((unsigned)hold & ((1U << (n)) - 1))
1156 /* Remove n bits from the bit accumulator */
1157 #define DROPBITS(n) \
1158 do { \
1159 hold >>= (n); \
1160 bits -= (unsigned)(n); \
1161 } while (0)
1163 /* Remove zero to seven bits as needed to go to a byte boundary */
1164 #define BYTEBITS() \
1165 do { \
1166 hold >>= bits & 7; \
1167 bits -= bits & 7; \
1168 } while (0)
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) {
1177 case STATEn:
1178 if (not enough input data or output space to make progress)
1179 return;
1180 ... make progress ...
1181 state = STATEm;
1182 break;
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
1193 NEEDBITS(n);
1194 ... do something with BITS(n) ...
1195 DROPBITS(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
1208 code, and no more.
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()
1214 returns:
1216 case STATEw:
1217 while (want < need) {
1218 NEEDBITS(n);
1219 keep[want++] = BITS(n);
1220 DROPBITS(n);
1222 state = STATEx;
1223 case STATEx:
1225 As shown above, if the next state is also the next case, then the break
1226 is omitted.
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 */
1267 #ifdef GUNZIP
1268 unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
1269 #endif
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 */
1279 LOAD();
1280 in = have;
1281 out = left;
1282 ret = Z_OK;
1283 for (;;)
1284 switch (state->mode) {
1285 case HEAD:
1286 if (state->wrap == 0) {
1287 state->mode = TYPEDO;
1288 break;
1290 NEEDBITS(16);
1291 #ifdef GUNZIP
1292 if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
1293 if (state->wbits == 0)
1294 state->wbits = 15;
1295 state->check = crc32(0L, Z_NULL, 0);
1296 CRC2(state->check, hold);
1297 INITBITS();
1298 state->mode = FLAGS;
1299 break;
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 */
1305 #else
1306 if (
1307 #endif
1308 ((BITS(8) << 8) + (hold >> 8)) % 31) {
1309 strm->msg = (char *)"incorrect header check";
1310 state->mode = BAD;
1311 break;
1313 if (BITS(4) != Z_DEFLATED) {
1314 strm->msg = (char *)"unknown compression method";
1315 state->mode = BAD;
1316 break;
1318 DROPBITS(4);
1319 len = BITS(4) + 8;
1320 if (state->wbits == 0)
1321 state->wbits = len;
1322 if (len > 15 || len > state->wbits) {
1323 strm->msg = (char *)"invalid window size";
1324 state->mode = BAD;
1325 break;
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;
1331 INITBITS();
1332 break;
1333 #ifdef GUNZIP
1334 case FLAGS:
1335 NEEDBITS(16);
1336 state->flags = (int)(hold);
1337 if ((state->flags & 0xff) != Z_DEFLATED) {
1338 strm->msg = (char *)"unknown compression method";
1339 state->mode = BAD;
1340 break;
1342 if (state->flags & 0xe000) {
1343 strm->msg = (char *)"unknown header flags set";
1344 state->mode = BAD;
1345 break;
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);
1351 INITBITS();
1352 state->mode = TIME;
1353 case TIME:
1354 NEEDBITS(32);
1355 if (state->head != Z_NULL)
1356 state->head->time = hold;
1357 if ((state->flags & 0x0200) && (state->wrap & 4))
1358 CRC4(state->check, hold);
1359 INITBITS();
1360 state->mode = OS;
1361 case OS:
1362 NEEDBITS(16);
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);
1369 INITBITS();
1370 state->mode = EXLEN;
1371 case EXLEN:
1372 if (state->flags & 0x0400) {
1373 NEEDBITS(16);
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);
1379 INITBITS();
1381 else if (state->head != Z_NULL)
1382 state->head->extra = Z_NULL;
1383 state->mode = EXTRA;
1384 case EXTRA:
1385 if (state->flags & 0x0400) {
1386 copy = state->length;
1387 if (copy > have) copy = have;
1388 if (copy) {
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);
1398 have -= copy;
1399 next += copy;
1400 state->length -= copy;
1402 if (state->length) goto inf_leave;
1404 state->length = 0;
1405 state->mode = NAME;
1406 case NAME:
1407 if (state->flags & 0x0800) {
1408 if (have == 0) goto inf_leave;
1409 copy = 0;
1410 do {
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);
1419 have -= copy;
1420 next += copy;
1421 if (len) goto inf_leave;
1423 else if (state->head != Z_NULL)
1424 state->head->name = Z_NULL;
1425 state->length = 0;
1426 state->mode = COMMENT;
1427 case COMMENT:
1428 if (state->flags & 0x1000) {
1429 if (have == 0) goto inf_leave;
1430 copy = 0;
1431 do {
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);
1440 have -= copy;
1441 next += copy;
1442 if (len) goto inf_leave;
1444 else if (state->head != Z_NULL)
1445 state->head->comment = Z_NULL;
1446 state->mode = HCRC;
1447 case HCRC:
1448 if (state->flags & 0x0200) {
1449 NEEDBITS(16);
1450 if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
1451 strm->msg = (char *)"header crc mismatch";
1452 state->mode = BAD;
1453 break;
1455 INITBITS();
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);
1462 state->mode = TYPE;
1463 break;
1464 #endif
1465 case DICTID:
1466 NEEDBITS(32);
1467 strm->adler = state->check = ZSWAP32(hold);
1468 INITBITS();
1469 state->mode = DICT;
1470 case DICT:
1471 if (state->havedict == 0) {
1472 RESTORE();
1473 return Z_NEED_DICT;
1475 strm->adler = state->check = adler32(0L, Z_NULL, 0);
1476 state->mode = TYPE;
1477 case TYPE:
1478 if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
1479 case TYPEDO:
1480 if (state->last) {
1481 BYTEBITS();
1482 state->mode = CHECK;
1483 break;
1485 NEEDBITS(3);
1486 state->last = BITS(1);
1487 DROPBITS(1);
1488 switch (BITS(2)) {
1489 case 0: /* stored block */
1490 Tracev((stderr, "inflate: stored block%s\n",
1491 state->last ? " (last)" : ""));
1492 state->mode = STORED;
1493 break;
1494 case 1: /* fixed block */
1495 fixedtables(state);
1496 Tracev((stderr, "inflate: fixed codes block%s\n",
1497 state->last ? " (last)" : ""));
1498 state->mode = LEN_; /* decode codes */
1499 if (flush == Z_TREES) {
1500 DROPBITS(2);
1501 goto inf_leave;
1503 break;
1504 case 2: /* dynamic block */
1505 Tracev((stderr, "inflate: dynamic codes block%s\n",
1506 state->last ? " (last)" : ""));
1507 state->mode = TABLE;
1508 break;
1509 case 3:
1510 strm->msg = (char *)"invalid block type";
1511 state->mode = BAD;
1513 DROPBITS(2);
1514 break;
1515 case STORED:
1516 BYTEBITS(); /* go to byte boundary */
1517 NEEDBITS(32);
1518 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
1519 strm->msg = (char *)"invalid stored block lengths";
1520 state->mode = BAD;
1521 break;
1523 state->length = (unsigned)hold & 0xffff;
1524 Tracev((stderr, "inflate: stored length %u\n",
1525 state->length));
1526 INITBITS();
1527 state->mode = COPY_;
1528 if (flush == Z_TREES) goto inf_leave;
1529 case COPY_:
1530 state->mode = COPY;
1531 case COPY:
1532 copy = state->length;
1533 if (copy) {
1534 if (copy > have) copy = have;
1535 if (copy > left) copy = left;
1536 if (copy == 0) goto inf_leave;
1537 zmemcpy(put, next, copy);
1538 have -= copy;
1539 next += copy;
1540 left -= copy;
1541 put += copy;
1542 state->length -= copy;
1543 break;
1545 Tracev((stderr, "inflate: stored end\n"));
1546 state->mode = TYPE;
1547 break;
1548 case TABLE:
1549 NEEDBITS(14);
1550 state->nlen = BITS(5) + 257;
1551 DROPBITS(5);
1552 state->ndist = BITS(5) + 1;
1553 DROPBITS(5);
1554 state->ncode = BITS(4) + 4;
1555 DROPBITS(4);
1556 #ifndef PKZIP_BUG_WORKAROUND
1557 if (state->nlen > 286 || state->ndist > 30) {
1558 strm->msg = (char *)"too many length or distance symbols";
1559 state->mode = BAD;
1560 break;
1562 #endif
1563 Tracev((stderr, "inflate: table sizes ok\n"));
1564 state->have = 0;
1565 state->mode = LENLENS;
1566 case LENLENS:
1567 while (state->have < state->ncode) {
1568 NEEDBITS(3);
1569 state->lens[order[state->have++]] = (unsigned short)BITS(3);
1570 DROPBITS(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);
1576 state->lenbits = 7;
1577 ret = inflate_table(CODES, state->lens, 19, &(state->next),
1578 &(state->lenbits), state->work);
1579 if (ret) {
1580 strm->msg = (char *)"invalid code lengths set";
1581 state->mode = BAD;
1582 break;
1584 Tracev((stderr, "inflate: code lengths ok\n"));
1585 state->have = 0;
1586 state->mode = CODELENS;
1587 case CODELENS:
1588 while (state->have < state->nlen + state->ndist) {
1589 for (;;) {
1590 here = state->lencode[BITS(state->lenbits)];
1591 if ((unsigned)(here.bits) <= bits) break;
1592 PULLBYTE();
1594 if (here.val < 16) {
1595 DROPBITS(here.bits);
1596 state->lens[state->have++] = here.val;
1598 else {
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";
1604 state->mode = BAD;
1605 break;
1607 len = state->lens[state->have - 1];
1608 copy = 3 + BITS(2);
1609 DROPBITS(2);
1611 else if (here.val == 17) {
1612 NEEDBITS(here.bits + 3);
1613 DROPBITS(here.bits);
1614 len = 0;
1615 copy = 3 + BITS(3);
1616 DROPBITS(3);
1618 else {
1619 NEEDBITS(here.bits + 7);
1620 DROPBITS(here.bits);
1621 len = 0;
1622 copy = 11 + BITS(7);
1623 DROPBITS(7);
1625 if (state->have + copy > state->nlen + state->ndist) {
1626 strm->msg = (char *)"invalid bit length repeat";
1627 state->mode = BAD;
1628 break;
1630 while (copy--)
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";
1641 state->mode = BAD;
1642 break;
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);
1650 state->lenbits = 9;
1651 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
1652 &(state->lenbits), state->work);
1653 if (ret) {
1654 strm->msg = (char *)"invalid literal/lengths set";
1655 state->mode = BAD;
1656 break;
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);
1662 if (ret) {
1663 strm->msg = (char *)"invalid distances set";
1664 state->mode = BAD;
1665 break;
1667 Tracev((stderr, "inflate: codes ok\n"));
1668 state->mode = LEN_;
1669 if (flush == Z_TREES) goto inf_leave;
1670 case LEN_:
1671 state->mode = LEN;
1672 case LEN:
1673 if (have >= 6 && left >= 258) {
1674 RESTORE();
1675 inflate_fast(strm, out);
1676 LOAD();
1677 if (state->mode == TYPE)
1678 state->back = -1;
1679 break;
1681 state->back = 0;
1682 for (;;) {
1683 here = state->lencode[BITS(state->lenbits)];
1684 if ((unsigned)(here.bits) <= bits) break;
1685 PULLBYTE();
1687 if (here.op && (here.op & 0xf0) == 0) {
1688 last = here;
1689 for (;;) {
1690 here = state->lencode[last.val +
1691 (BITS(last.bits + last.op) >> last.bits)];
1692 if ((unsigned)(last.bits + here.bits) <= bits) break;
1693 PULLBYTE();
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));
1705 state->mode = LIT;
1706 break;
1708 if (here.op & 32) {
1709 Tracevv((stderr, "inflate: end of block\n"));
1710 state->back = -1;
1711 state->mode = TYPE;
1712 break;
1714 if (here.op & 64) {
1715 strm->msg = (char *)"invalid literal/length code";
1716 state->mode = BAD;
1717 break;
1719 state->extra = (unsigned)(here.op) & 15;
1720 state->mode = LENEXT;
1721 case LENEXT:
1722 if (state->extra) {
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;
1730 state->mode = DIST;
1731 case DIST:
1732 for (;;) {
1733 here = state->distcode[BITS(state->distbits)];
1734 if ((unsigned)(here.bits) <= bits) break;
1735 PULLBYTE();
1737 if ((here.op & 0xf0) == 0) {
1738 last = here;
1739 for (;;) {
1740 here = state->distcode[last.val +
1741 (BITS(last.bits + last.op) >> last.bits)];
1742 if ((unsigned)(last.bits + here.bits) <= bits) break;
1743 PULLBYTE();
1745 DROPBITS(last.bits);
1746 state->back += last.bits;
1748 DROPBITS(here.bits);
1749 state->back += here.bits;
1750 if (here.op & 64) {
1751 strm->msg = (char *)"invalid distance code";
1752 state->mode = BAD;
1753 break;
1755 state->offset = (unsigned)here.val;
1756 state->extra = (unsigned)(here.op) & 15;
1757 state->mode = DISTEXT;
1758 case DISTEXT:
1759 if (state->extra) {
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";
1768 state->mode = BAD;
1769 break;
1771 #endif
1772 Tracevv((stderr, "inflate: distance %u\n", state->offset));
1773 state->mode = MATCH;
1774 case MATCH:
1775 if (left == 0) goto inf_leave;
1776 copy = out - left;
1777 if (state->offset > copy) { /* copy from window */
1778 copy = state->offset - copy;
1779 if (copy > state->whave) {
1780 if (state->sane) {
1781 strm->msg = (char *)"invalid distance too far back";
1782 state->mode = BAD;
1783 break;
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;
1790 left -= copy;
1791 state->length -= copy;
1792 do {
1793 *put++ = 0;
1794 } while (--copy);
1795 if (state->length == 0) state->mode = LEN;
1796 break;
1797 #endif
1799 if (copy > state->wnext) {
1800 copy -= state->wnext;
1801 from = state->window + (state->wsize - copy);
1803 else
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;
1812 left -= copy;
1813 state->length -= copy;
1814 do {
1815 *put++ = *from++;
1816 } while (--copy);
1817 if (state->length == 0) state->mode = LEN;
1818 break;
1819 case LIT:
1820 if (left == 0) goto inf_leave;
1821 *put++ = (unsigned char)(state->length);
1822 left--;
1823 state->mode = LEN;
1824 break;
1825 case CHECK:
1826 if (state->wrap) {
1827 NEEDBITS(32);
1828 out -= left;
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);
1834 out = left;
1835 if ((state->wrap & 4) && (
1836 #ifdef GUNZIP
1837 state->flags ? hold :
1838 #endif
1839 ZSWAP32(hold)) != state->check) {
1840 strm->msg = (char *)"incorrect data check";
1841 state->mode = BAD;
1842 break;
1844 INITBITS();
1845 Tracev((stderr, "inflate: check matches trailer\n"));
1847 #ifdef GUNZIP
1848 state->mode = LENGTH;
1849 case LENGTH:
1850 if (state->wrap && state->flags) {
1851 NEEDBITS(32);
1852 if (hold != (state->total & 0xffffffffUL)) {
1853 strm->msg = (char *)"incorrect length check";
1854 state->mode = BAD;
1855 break;
1857 INITBITS();
1858 Tracev((stderr, "inflate: length matches trailer\n"));
1860 #endif
1861 state->mode = DONE;
1862 case DONE:
1863 ret = Z_STREAM_END;
1864 goto inf_leave;
1865 case BAD:
1866 ret = Z_DATA_ERROR;
1867 goto inf_leave;
1868 case MEM:
1869 return Z_MEM_ERROR;
1870 case SYNC:
1871 default:
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.
1881 inf_leave:
1882 RESTORE();
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)) {
1886 state->mode = MEM;
1887 return Z_MEM_ERROR;
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)
1901 ret = Z_BUF_ERROR;
1902 return ret;
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"));
1915 return Z_OK;