ReactOS 0.4.16-dev-401-g45b008d
inflate.c
Go to the documentation of this file.
1#ifdef DBGHELP_STATIC_LIB
2#include "compat.h"
3#include "zlib.h"
4#else
5/* inflate.c -- zlib decompression
6 *
7 * Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler
8 *
9 * This software is provided 'as-is', without any express or implied
10 * warranty. In no event will the authors be held liable for any damages
11 * arising from the use of this software.
12 *
13 * Permission is granted to anyone to use this software for any purpose,
14 * including commercial applications, and to alter it and redistribute it
15 * freely, subject to the following restrictions:
16 *
17 * 1. The origin of this software must not be misrepresented; you must not
18 * claim that you wrote the original software. If you use this software
19 * in a product, an acknowledgment in the product documentation would be
20 * appreciated but is not required.
21 * 2. Altered source versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software.
23 * 3. This notice may not be removed or altered from any source distribution.
24 *
25 * Jean-loup Gailly Mark Adler
26 * jloup@gzip.org madler@alumni.caltech.edu
27 */
28
29#include <stdarg.h>
30#include <stdlib.h>
31#include <string.h>
32#include <limits.h>
33#include "winternl.h"
34#include "zlib.h"
35#endif /* DBGHELP_STATIC_LIB */
36
37#define DEF_WBITS MAX_WBITS
38#define zmemcpy memcpy
39#define zmemzero(dest, len) memset(dest, 0, len)
40
41#define Assert(cond,msg)
42#define Trace(x)
43#define Tracev(x)
44#define Tracevv(x)
45#define Tracecv(c,x)
46
47#define GUNZIP
48
49#define ZALLOC(strm, items, size) \
50 (*((strm)->zalloc))((strm)->opaque, (items), (size))
51#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
52#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
53
54/* Reverse the bytes in a 32-bit value */
55#define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
56 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
57
58#define BASE 65521U /* largest prime smaller than 65536 */
59#define NMAX 5552
60/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
61
62#define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
63#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
64#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
65#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
66#define DO16(buf) DO8(buf,0); DO8(buf,8);
67
68#define MOD(a) a %= BASE
69#define MOD28(a) a %= BASE
70#define MOD63(a) a %= BASE
71
72static uLong adler32( uLong adler, const Bytef *buf, uInt len )
73{
74 unsigned long sum2;
75 unsigned n;
76
77 /* split Adler-32 into component sums */
78 sum2 = (adler >> 16) & 0xffff;
79 adler &= 0xffff;
80
81 /* in case user likes doing a byte at a time, keep it fast */
82 if (len == 1) {
83 adler += buf[0];
84 if (adler >= BASE)
85 adler -= BASE;
86 sum2 += adler;
87 if (sum2 >= BASE)
88 sum2 -= BASE;
89 return adler | (sum2 << 16);
90 }
91
92 /* initial Adler-32 value (deferred check for len == 1 speed) */
93 if (buf == Z_NULL)
94 return 1L;
95
96 /* in case short lengths are provided, keep it somewhat fast */
97 if (len < 16) {
98 while (len--) {
99 adler += *buf++;
100 sum2 += adler;
101 }
102 if (adler >= BASE)
103 adler -= BASE;
104 MOD28(sum2); /* only added so many BASE's */
105 return adler | (sum2 << 16);
106 }
107
108 /* do length NMAX blocks -- requires just one modulo operation */
109 while (len >= NMAX) {
110 len -= NMAX;
111 n = NMAX / 16; /* NMAX is divisible by 16 */
112 do {
113 DO16(buf); /* 16 sums unrolled */
114 buf += 16;
115 } while (--n);
116 MOD(adler);
117 MOD(sum2);
118 }
119
120 /* do remaining bytes (less than NMAX, still just one modulo) */
121 if (len) { /* avoid modulos if none remaining */
122 while (len >= 16) {
123 len -= 16;
124 DO16(buf);
125 buf += 16;
126 }
127 while (len--) {
128 adler += *buf++;
129 sum2 += adler;
130 }
131 MOD(adler);
132 MOD(sum2);
133 }
134
135 /* return recombined sums */
136 return adler | (sum2 << 16);
137}
138
139typedef struct {
140 unsigned char op; /* operation, extra bits, table bits */
141 unsigned char bits; /* bits in this part of the code */
142 unsigned short val; /* offset in table or code value */
143} code;
144
145#define ENOUGH_LENS 852
146#define ENOUGH_DISTS 592
147#define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS)
148
149/* Type of code to build for inflate_table() */
150typedef enum {
153 DISTS
155
156/* Possible inflate modes between inflate() calls */
157typedef enum {
158 HEAD = 16180, /* i: waiting for magic header */
159 FLAGS, /* i: waiting for method and flags (gzip) */
160 TIME, /* i: waiting for modification time (gzip) */
161 OS, /* i: waiting for extra flags and operating system (gzip) */
162 EXLEN, /* i: waiting for extra length (gzip) */
163 EXTRA, /* i: waiting for extra bytes (gzip) */
164 NAME, /* i: waiting for end of file name (gzip) */
165 COMMENT, /* i: waiting for end of comment (gzip) */
166 HCRC, /* i: waiting for header crc (gzip) */
167 DICTID, /* i: waiting for dictionary check value */
168 DICT, /* waiting for inflateSetDictionary() call */
169 TYPE, /* i: waiting for type bits, including last-flag bit */
170 TYPEDO, /* i: same, but skip check to exit inflate on new block */
171 STORED, /* i: waiting for stored size (length and complement) */
172 COPY_, /* i/o: same as COPY below, but only first time in */
173 COPY, /* i/o: waiting for input or output to copy stored block */
174 TABLE, /* i: waiting for dynamic block table lengths */
175 LENLENS, /* i: waiting for code length code lengths */
176 CODELENS, /* i: waiting for length/lit and distance code lengths */
177 LEN_, /* i: same as LEN below, but only first time in */
178 LEN, /* i: waiting for length/lit/eob code */
179 LENEXT, /* i: waiting for length extra bits */
180 DIST, /* i: waiting for distance code */
181 DISTEXT, /* i: waiting for distance extra bits */
182 MATCH, /* o: waiting for output space to copy string */
183 LIT, /* o: waiting for output space to write literal */
184 CHECK, /* i: waiting for 32-bit check value */
185 LENGTH, /* i: waiting for 32-bit length (gzip) */
186 DONE, /* finished check, done -- remain here until reset */
187 BAD, /* got a data error -- remain here until reset */
188 MEM, /* got an inflate() memory error -- remain here until reset */
189 SYNC /* looking for synchronization bytes to restart inflate() */
191
192/* State maintained between inflate() calls -- approximately 7K bytes, not
193 including the allocated sliding window, which is up to 32K bytes. */
195 z_streamp strm; /* pointer back to this zlib stream */
196 inflate_mode mode; /* current inflate mode */
197 int last; /* true if processing last block */
198 int wrap; /* bit 0 true for zlib, bit 1 true for gzip,
199 bit 2 true to validate check value */
200 int havedict; /* true if dictionary provided */
201 int flags; /* gzip header method and flags (0 if zlib) */
202 unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */
203 unsigned long check; /* protected copy of check value */
204 unsigned long total; /* protected copy of output count */
205 gz_headerp head; /* where to save gzip header information */
206 /* sliding window */
207 unsigned wbits; /* log base 2 of requested window size */
208 unsigned wsize; /* window size or zero if not using window */
209 unsigned whave; /* valid bytes in the window */
210 unsigned wnext; /* window write index */
211 unsigned char FAR *window; /* allocated sliding window, if needed */
212 /* bit accumulator */
213 unsigned long hold; /* input bit accumulator */
214 unsigned bits; /* number of bits in "in" */
215 /* for string and stored block copying */
216 unsigned length; /* literal or length of data to copy */
217 unsigned offset; /* distance back to copy string from */
218 /* for table and code decoding */
219 unsigned extra; /* extra bits needed */
220 /* fixed and dynamic code tables */
221 code const FAR *lencode; /* starting table for length/literal codes */
222 code const FAR *distcode; /* starting table for distance codes */
223 unsigned lenbits; /* index bits for lencode */
224 unsigned distbits; /* index bits for distcode */
225 /* dynamic table building */
226 unsigned ncode; /* number of code length code lengths */
227 unsigned nlen; /* number of length code lengths */
228 unsigned ndist; /* number of distance code lengths */
229 unsigned have; /* number of code lengths in lens[] */
230 code FAR *next; /* next available space in codes[] */
231 unsigned short lens[320]; /* temporary storage for code lengths */
232 unsigned short work[288]; /* work area for code table building */
233 code codes[ENOUGH]; /* space for code tables */
234 int sane; /* if false, allow invalid distance too far */
235 int back; /* bits back of last unprocessed length/lit */
236 unsigned was; /* initial length of match */
237};
238
239/*
240 Decode literal, length, and distance codes and write out the resulting
241 literal and match bytes until either not enough input or output is
242 available, an end-of-block is encountered, or a data error is encountered.
243 When large enough input and output buffers are supplied to inflate(), for
244 example, a 16K input buffer and a 64K output buffer, more than 95% of the
245 inflate execution time is spent in this routine.
246
247 Entry assumptions:
248
249 state->mode == LEN
250 strm->avail_in >= 6
251 strm->avail_out >= 258
252 start >= strm->avail_out
253 state->bits < 8
254
255 On return, state->mode is one of:
256
257 LEN -- ran out of enough output space or enough available input
258 TYPE -- reached end of block code, inflate() to interpret next block
259 BAD -- error in block data
260
261 Notes:
262
263 - The maximum input bits used by a length/distance pair is 15 bits for the
264 length code, 5 bits for the length extra, 15 bits for the distance code,
265 and 13 bits for the distance extra. This totals 48 bits, or six bytes.
266 Therefore if strm->avail_in >= 6, then there is enough input to avoid
267 checking for available input while decoding.
268
269 - The maximum bytes that a single length/distance pair can output is 258
270 bytes, which is the maximum length that can be coded. inflate_fast()
271 requires strm->avail_out >= 258 for each loop to avoid checking for
272 output space.
273 */
274static void inflate_fast( z_streamp strm, unsigned start )
275{
276 struct inflate_state FAR *state;
277 z_const unsigned char FAR *in; /* local strm->next_in */
278 z_const unsigned char FAR *last; /* have enough input while in < last */
279 unsigned char FAR *out; /* local strm->next_out */
280 unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
281 unsigned char FAR *end; /* while out < end, enough space available */
282#ifdef INFLATE_STRICT
283 unsigned dmax; /* maximum distance from zlib header */
284#endif
285 unsigned wsize; /* window size or zero if not using window */
286 unsigned whave; /* valid bytes in the window */
287 unsigned wnext; /* window write index */
288 unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
289 unsigned long hold; /* local strm->hold */
290 unsigned bits; /* local strm->bits */
291 code const FAR *lcode; /* local strm->lencode */
292 code const FAR *dcode; /* local strm->distcode */
293 unsigned lmask; /* mask for first level of length codes */
294 unsigned dmask; /* mask for first level of distance codes */
295 code here; /* retrieved table entry */
296 unsigned op; /* code bits, operation, extra bits, or */
297 /* window position, window bytes to copy */
298 unsigned len; /* match length, unused bytes */
299 unsigned dist; /* match distance */
300 unsigned char FAR *from; /* where to copy match from */
301
302 /* copy state to local variables */
303 state = (struct inflate_state FAR *)strm->state;
304 in = strm->next_in;
305 last = in + (strm->avail_in - 5);
306 out = strm->next_out;
307 beg = out - (start - strm->avail_out);
308 end = out + (strm->avail_out - 257);
309#ifdef INFLATE_STRICT
310 dmax = state->dmax;
311#endif
312 wsize = state->wsize;
313 whave = state->whave;
314 wnext = state->wnext;
315 window = state->window;
316 hold = state->hold;
317 bits = state->bits;
318 lcode = state->lencode;
319 dcode = state->distcode;
320 lmask = (1U << state->lenbits) - 1;
321 dmask = (1U << state->distbits) - 1;
322
323 /* decode literals and length/distances until end-of-block or not enough
324 input data or output space */
325 do {
326 if (bits < 15) {
327 hold += (unsigned long)(*in++) << bits;
328 bits += 8;
329 hold += (unsigned long)(*in++) << bits;
330 bits += 8;
331 }
332 here = lcode[hold & lmask];
333 dolen:
334 op = (unsigned)(here.bits);
335 hold >>= op;
336 bits -= op;
337 op = (unsigned)(here.op);
338 if (op == 0) { /* literal */
339 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
340 "inflate: literal '%c'\n" :
341 "inflate: literal 0x%02x\n", here.val));
342 *out++ = (unsigned char)(here.val);
343 }
344 else if (op & 16) { /* length base */
345 len = (unsigned)(here.val);
346 op &= 15; /* number of extra bits */
347 if (op) {
348 if (bits < op) {
349 hold += (unsigned long)(*in++) << bits;
350 bits += 8;
351 }
352 len += (unsigned)hold & ((1U << op) - 1);
353 hold >>= op;
354 bits -= op;
355 }
356 Tracevv((stderr, "inflate: length %u\n", len));
357 if (bits < 15) {
358 hold += (unsigned long)(*in++) << bits;
359 bits += 8;
360 hold += (unsigned long)(*in++) << bits;
361 bits += 8;
362 }
363 here = dcode[hold & dmask];
364 dodist:
365 op = (unsigned)(here.bits);
366 hold >>= op;
367 bits -= op;
368 op = (unsigned)(here.op);
369 if (op & 16) { /* distance base */
370 dist = (unsigned)(here.val);
371 op &= 15; /* number of extra bits */
372 if (bits < op) {
373 hold += (unsigned long)(*in++) << bits;
374 bits += 8;
375 if (bits < op) {
376 hold += (unsigned long)(*in++) << bits;
377 bits += 8;
378 }
379 }
380 dist += (unsigned)hold & ((1U << op) - 1);
381#ifdef INFLATE_STRICT
382 if (dist > dmax) {
383 strm->msg = (char *)"invalid distance too far back";
384 state->mode = BAD;
385 break;
386 }
387#endif
388 hold >>= op;
389 bits -= op;
390 Tracevv((stderr, "inflate: distance %u\n", dist));
391 op = (unsigned)(out - beg); /* max distance in output */
392 if (dist > op) { /* see if copy from window */
393 op = dist - op; /* distance back in window */
394 if (op > whave) {
395 if (state->sane) {
396 strm->msg =
397 (char *)"invalid distance too far back";
398 state->mode = BAD;
399 break;
400 }
401#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
402 if (len <= op - whave) {
403 do {
404 *out++ = 0;
405 } while (--len);
406 continue;
407 }
408 len -= op - whave;
409 do {
410 *out++ = 0;
411 } while (--op > whave);
412 if (op == 0) {
413 from = out - dist;
414 do {
415 *out++ = *from++;
416 } while (--len);
417 continue;
418 }
419#endif
420 }
421 from = window;
422 if (wnext == 0) { /* very common case */
423 from += wsize - op;
424 if (op < len) { /* some from window */
425 len -= op;
426 do {
427 *out++ = *from++;
428 } while (--op);
429 from = out - dist; /* rest from output */
430 }
431 }
432 else if (wnext < op) { /* wrap around window */
433 from += wsize + wnext - op;
434 op -= wnext;
435 if (op < len) { /* some from end of window */
436 len -= op;
437 do {
438 *out++ = *from++;
439 } while (--op);
440 from = window;
441 if (wnext < len) { /* some from start of window */
442 op = wnext;
443 len -= op;
444 do {
445 *out++ = *from++;
446 } while (--op);
447 from = out - dist; /* rest from output */
448 }
449 }
450 }
451 else { /* contiguous in window */
452 from += wnext - op;
453 if (op < len) { /* some from window */
454 len -= op;
455 do {
456 *out++ = *from++;
457 } while (--op);
458 from = out - dist; /* rest from output */
459 }
460 }
461 while (len > 2) {
462 *out++ = *from++;
463 *out++ = *from++;
464 *out++ = *from++;
465 len -= 3;
466 }
467 if (len) {
468 *out++ = *from++;
469 if (len > 1)
470 *out++ = *from++;
471 }
472 }
473 else {
474 from = out - dist; /* copy direct from output */
475 do { /* minimum length is three */
476 *out++ = *from++;
477 *out++ = *from++;
478 *out++ = *from++;
479 len -= 3;
480 } while (len > 2);
481 if (len) {
482 *out++ = *from++;
483 if (len > 1)
484 *out++ = *from++;
485 }
486 }
487 }
488 else if ((op & 64) == 0) { /* 2nd level distance code */
489 here = dcode[here.val + (hold & ((1U << op) - 1))];
490 goto dodist;
491 }
492 else {
493 strm->msg = (char *)"invalid distance code";
494 state->mode = BAD;
495 break;
496 }
497 }
498 else if ((op & 64) == 0) { /* 2nd level length code */
499 here = lcode[here.val + (hold & ((1U << op) - 1))];
500 goto dolen;
501 }
502 else if (op & 32) { /* end-of-block */
503 Tracevv((stderr, "inflate: end of block\n"));
504 state->mode = TYPE;
505 break;
506 }
507 else {
508 strm->msg = (char *)"invalid literal/length code";
509 state->mode = BAD;
510 break;
511 }
512 } while (in < last && out < end);
513
514 /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
515 len = bits >> 3;
516 in -= len;
517 bits -= len << 3;
518 hold &= (1U << bits) - 1;
519
520 /* update state and return */
521 strm->next_in = in;
522 strm->next_out = out;
523 strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
524 strm->avail_out = (unsigned)(out < end ?
525 257 + (end - out) : 257 - (out - end));
526 state->hold = hold;
527 state->bits = bits;
528 return;
529}
530
531#define MAXBITS 15
532
533static int inflate_table( codetype type, unsigned short FAR *lens, unsigned codes, code FAR * FAR *table,
534 unsigned FAR *bits, unsigned short FAR *work )
535{
536 unsigned len; /* a code's length in bits */
537 unsigned sym; /* index of code symbols */
538 unsigned min, max; /* minimum and maximum code lengths */
539 unsigned root; /* number of index bits for root table */
540 unsigned curr; /* number of index bits for current table */
541 unsigned drop; /* code bits to drop for sub-table */
542 int left; /* number of prefix codes available */
543 unsigned used; /* code entries in table used */
544 unsigned huff; /* Huffman code */
545 unsigned incr; /* for incrementing code, index */
546 unsigned fill; /* index for replicating entries */
547 unsigned low; /* low bits for current root entry */
548 unsigned mask; /* mask for low root bits */
549 code here; /* table entry for duplication */
550 code FAR *next; /* next available space in table */
551 const unsigned short FAR *base; /* base value table to use */
552 const unsigned short FAR *extra; /* extra bits table to use */
553 unsigned match; /* use base and extra for symbol >= match */
554 unsigned short count[MAXBITS+1]; /* number of codes of each length */
555 unsigned short offs[MAXBITS+1]; /* offsets in table for each length */
556 static const unsigned short lbase[31] = { /* Length codes 257..285 base */
557 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
558 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
559 static const unsigned short lext[31] = { /* Length codes 257..285 extra */
560 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
561 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 77, 202};
562 static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
563 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
564 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
565 8193, 12289, 16385, 24577, 0, 0};
566 static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
567 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
568 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
569 28, 28, 29, 29, 64, 64};
570
571 /*
572 Process a set of code lengths to create a canonical Huffman code. The
573 code lengths are lens[0..codes-1]. Each length corresponds to the
574 symbols 0..codes-1. The Huffman code is generated by first sorting the
575 symbols by length from short to long, and retaining the symbol order
576 for codes with equal lengths. Then the code starts with all zero bits
577 for the first code of the shortest length, and the codes are integer
578 increments for the same length, and zeros are appended as the length
579 increases. For the deflate format, these bits are stored backwards
580 from their more natural integer increment ordering, and so when the
581 decoding tables are built in the large loop below, the integer codes
582 are incremented backwards.
583
584 This routine assumes, but does not check, that all of the entries in
585 lens[] are in the range 0..MAXBITS. The caller must assure this.
586 1..MAXBITS is interpreted as that code length. zero means that that
587 symbol does not occur in this code.
588
589 The codes are sorted by computing a count of codes for each length,
590 creating from that a table of starting indices for each length in the
591 sorted table, and then entering the symbols in order in the sorted
592 table. The sorted table is work[], with that space being provided by
593 the caller.
594
595 The length counts are used for other purposes as well, i.e. finding
596 the minimum and maximum length codes, determining if there are any
597 codes at all, checking for a valid set of lengths, and looking ahead
598 at length counts to determine sub-table sizes when building the
599 decoding tables.
600 */
601
602 /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
603 for (len = 0; len <= MAXBITS; len++)
604 count[len] = 0;
605 for (sym = 0; sym < codes; sym++)
606 count[lens[sym]]++;
607
608 /* bound code lengths, force root to be within code lengths */
609 root = *bits;
610 for (max = MAXBITS; max >= 1; max--)
611 if (count[max] != 0) break;
612 if (root > max) root = max;
613 if (max == 0) { /* no symbols to code at all */
614 here.op = (unsigned char)64; /* invalid code marker */
615 here.bits = (unsigned char)1;
616 here.val = (unsigned short)0;
617 *(*table)++ = here; /* make a table to force an error */
618 *(*table)++ = here;
619 *bits = 1;
620 return 0; /* no symbols, but wait for decoding to report error */
621 }
622 for (min = 1; min < max; min++)
623 if (count[min] != 0) break;
624 if (root < min) root = min;
625
626 /* check for an over-subscribed or incomplete set of lengths */
627 left = 1;
628 for (len = 1; len <= MAXBITS; len++) {
629 left <<= 1;
630 left -= count[len];
631 if (left < 0) return -1; /* over-subscribed */
632 }
633 if (left > 0 && (type == CODES || max != 1))
634 return -1; /* incomplete set */
635
636 /* generate offsets into symbol table for each length for sorting */
637 offs[1] = 0;
638 for (len = 1; len < MAXBITS; len++)
639 offs[len + 1] = offs[len] + count[len];
640
641 /* sort symbols by length, by symbol order within each length */
642 for (sym = 0; sym < codes; sym++)
643 if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
644
645 /*
646 Create and fill in decoding tables. In this loop, the table being
647 filled is at next and has curr index bits. The code being used is huff
648 with length len. That code is converted to an index by dropping drop
649 bits off of the bottom. For codes where len is less than drop + curr,
650 those top drop + curr - len bits are incremented through all values to
651 fill the table with replicated entries.
652
653 root is the number of index bits for the root table. When len exceeds
654 root, sub-tables are created pointed to by the root entry with an index
655 of the low root bits of huff. This is saved in low to check for when a
656 new sub-table should be started. drop is zero when the root table is
657 being filled, and drop is root when sub-tables are being filled.
658
659 When a new sub-table is needed, it is necessary to look ahead in the
660 code lengths to determine what size sub-table is needed. The length
661 counts are used for this, and so count[] is decremented as codes are
662 entered in the tables.
663
664 used keeps track of how many table entries have been allocated from the
665 provided *table space. It is checked for LENS and DIST tables against
666 the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
667 the initial root table size constants. See the comments in inftrees.h
668 for more information.
669
670 sym increments through all symbols, and the loop terminates when
671 all codes of length max, i.e. all codes, have been processed. This
672 routine permits incomplete codes, so another loop after this one fills
673 in the rest of the decoding tables with invalid code markers.
674 */
675
676 /* set up for code type */
677 switch (type) {
678 case CODES:
679 base = extra = work; /* dummy value--not used */
680 match = 20;
681 break;
682 case LENS:
683 base = lbase;
684 extra = lext;
685 match = 257;
686 break;
687 default: /* DISTS */
688 base = dbase;
689 extra = dext;
690 match = 0;
691 }
692
693 /* initialize state for loop */
694 huff = 0; /* starting code */
695 sym = 0; /* starting code symbol */
696 len = min; /* starting code length */
697 next = *table; /* current table to fill in */
698 curr = root; /* current table index bits */
699 drop = 0; /* current bits to drop from code for index */
700 low = (unsigned)(-1); /* trigger new sub-table when len > root */
701 used = 1U << root; /* use root table entries */
702 mask = used - 1; /* mask for comparing low */
703
704 /* check available table space */
705 if ((type == LENS && used > ENOUGH_LENS) ||
706 (type == DISTS && used > ENOUGH_DISTS))
707 return 1;
708
709 /* process all codes and make table entries */
710 for (;;) {
711 /* create table entry */
712 here.bits = (unsigned char)(len - drop);
713 if (work[sym] + 1U < match) {
714 here.op = (unsigned char)0;
715 here.val = work[sym];
716 }
717 else if (work[sym] >= match) {
718 here.op = (unsigned char)(extra[work[sym] - match]);
719 here.val = base[work[sym] - match];
720 }
721 else {
722 here.op = (unsigned char)(32 + 64); /* end of block */
723 here.val = 0;
724 }
725
726 /* replicate for those indices with low len bits equal to huff */
727 incr = 1U << (len - drop);
728 fill = 1U << curr;
729 min = fill; /* save offset to next table */
730 do {
731 fill -= incr;
732 next[(huff >> drop) + fill] = here;
733 } while (fill != 0);
734
735 /* backwards increment the len-bit code huff */
736 incr = 1U << (len - 1);
737 while (huff & incr)
738 incr >>= 1;
739 if (incr != 0) {
740 huff &= incr - 1;
741 huff += incr;
742 }
743 else
744 huff = 0;
745
746 /* go to next symbol, update count, len */
747 sym++;
748 if (--(count[len]) == 0) {
749 if (len == max) break;
750 len = lens[work[sym]];
751 }
752
753 /* create new sub-table if needed */
754 if (len > root && (huff & mask) != low) {
755 /* if first time, transition to sub-tables */
756 if (drop == 0)
757 drop = root;
758
759 /* increment past last table */
760 next += min; /* here min is 1 << curr */
761
762 /* determine length of next table */
763 curr = len - drop;
764 left = (int)(1 << curr);
765 while (curr + drop < max) {
766 left -= count[curr + drop];
767 if (left <= 0) break;
768 curr++;
769 left <<= 1;
770 }
771
772 /* check for enough space */
773 used += 1U << curr;
774 if ((type == LENS && used > ENOUGH_LENS) ||
775 (type == DISTS && used > ENOUGH_DISTS))
776 return 1;
777
778 /* point entry in root table to sub-table */
779 low = huff & mask;
780 (*table)[low].op = (unsigned char)curr;
781 (*table)[low].bits = (unsigned char)root;
782 (*table)[low].val = (unsigned short)(next - *table);
783 }
784 }
785
786 /* fill in remaining table entry if code is incomplete (guaranteed to have
787 at most one remaining entry, since if the code is incomplete, the
788 maximum code length that was allowed to get this far is one bit) */
789 if (huff != 0) {
790 here.op = (unsigned char)64; /* invalid code marker */
791 here.bits = (unsigned char)(len - drop);
792 here.val = (unsigned short)0;
793 next[huff] = here;
794 }
795
796 /* set return parameters */
797 *table += used;
798 *bits = root;
799 return 0;
800}
801
803{
804 struct inflate_state FAR *state;
805 if (strm == Z_NULL ||
806 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
807 return 1;
808 state = (struct inflate_state FAR *)strm->state;
809 if (state == Z_NULL || state->strm != strm ||
810 state->mode < HEAD || state->mode > SYNC)
811 return 1;
812 return 0;
813}
814
816{
817 struct inflate_state FAR *state;
818
820 state = (struct inflate_state FAR *)strm->state;
821 strm->total_in = strm->total_out = state->total = 0;
822 strm->msg = Z_NULL;
823 if (state->wrap) /* to support ill-conceived Java test suite */
824 strm->adler = state->wrap & 1;
825 state->mode = HEAD;
826 state->last = 0;
827 state->havedict = 0;
828 state->dmax = 32768U;
829 state->head = Z_NULL;
830 state->hold = 0;
831 state->bits = 0;
832 state->lencode = state->distcode = state->next = state->codes;
833 state->sane = 1;
834 state->back = -1;
835 Tracev((stderr, "inflate: reset\n"));
836 return Z_OK;
837}
838
840{
841 struct inflate_state FAR *state;
842
844 state = (struct inflate_state FAR *)strm->state;
845 state->wsize = 0;
846 state->whave = 0;
847 state->wnext = 0;
848 return inflateResetKeep(strm);
849}
850
852{
853 int wrap;
854 struct inflate_state FAR *state;
855
856 /* get the state */
858 state = (struct inflate_state FAR *)strm->state;
859
860 /* extract wrap request from windowBits parameter */
861 if (windowBits < 0) {
862 wrap = 0;
864 }
865 else {
866 wrap = (windowBits >> 4) + 5;
867#ifdef GUNZIP
868 if (windowBits < 48)
869 windowBits &= 15;
870#endif
871 }
872
873 /* set number of window bits, free window if different */
874 if (windowBits && (windowBits < 8 || windowBits > 15))
875 return Z_STREAM_ERROR;
876 if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
877 ZFREE(strm, state->window);
878 state->window = Z_NULL;
879 }
880
881 /* update state and reset the rest of it */
882 state->wrap = wrap;
883 state->wbits = (unsigned)windowBits;
884 return inflateReset(strm);
885}
886
888{
889 int ret;
890 struct inflate_state FAR *state;
891
892 strm->msg = Z_NULL; /* in case we return an error */
893 state = (struct inflate_state FAR *)
894 ZALLOC(strm, 1, sizeof(struct inflate_state));
895 if (state == Z_NULL) return Z_MEM_ERROR;
896 Tracev((stderr, "inflate: allocated\n"));
897 strm->state = (struct internal_state FAR *)state;
898 state->strm = strm;
899 state->window = Z_NULL;
900 state->mode = HEAD; /* to pass state test in inflateReset2() */
902 if (ret != Z_OK) {
903 ZFREE(strm, state);
904 strm->state = Z_NULL;
905 }
906 return ret;
907}
908
910{
911 return inflateInit2(strm, DEF_WBITS);
912}
913
914/*
915 Return state with length and distance decoding tables and index sizes set to
916 fixed code decoding. Normally this returns fixed tables from inffixed.h.
917 If BUILDFIXED is defined, then instead this routine builds the tables the
918 first time it's called, and returns those tables the first time and
919 thereafter. This reduces the size of the code by about 2K bytes, in
920 exchange for a little execution time. However, BUILDFIXED should not be
921 used for threaded applications, since the rewriting of the tables and virgin
922 may not be thread-safe.
923 */
924static void fixedtables( struct inflate_state FAR *state )
925{
926 static const code lenfix[512] = {
927 {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48},
928 {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128},
929 {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59},
930 {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176},
931 {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20},
932 {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100},
933 {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8},
934 {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216},
935 {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76},
936 {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114},
937 {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2},
938 {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148},
939 {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42},
940 {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86},
941 {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15},
942 {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236},
943 {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62},
944 {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142},
945 {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31},
946 {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162},
947 {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25},
948 {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105},
949 {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4},
950 {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202},
951 {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69},
952 {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125},
953 {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13},
954 {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195},
955 {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35},
956 {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91},
957 {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19},
958 {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246},
959 {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55},
960 {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135},
961 {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99},
962 {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190},
963 {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16},
964 {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96},
965 {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6},
966 {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209},
967 {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72},
968 {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116},
969 {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4},
970 {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153},
971 {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44},
972 {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82},
973 {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11},
974 {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229},
975 {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58},
976 {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138},
977 {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51},
978 {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173},
979 {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30},
980 {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110},
981 {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0},
982 {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195},
983 {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65},
984 {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121},
985 {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9},
986 {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258},
987 {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37},
988 {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93},
989 {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23},
990 {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251},
991 {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51},
992 {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131},
993 {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67},
994 {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183},
995 {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23},
996 {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103},
997 {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9},
998 {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223},
999 {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79},
1000 {0,9,255}
1001 };
1002
1003 static const code distfix[32] = {
1004 {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025},
1005 {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193},
1006 {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385},
1007 {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577},
1008 {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073},
1009 {22,5,193},{64,5,0}
1010 };
1011
1012 state->lencode = lenfix;
1013 state->lenbits = 9;
1014 state->distcode = distfix;
1015 state->distbits = 5;
1016}
1017
1018/*
1019 Update the window with the last wsize (normally 32K) bytes written before
1020 returning. If window does not exist yet, create it. This is only called
1021 when a window is already in use, or when output has been written during this
1022 inflate call, but the end of the deflate stream has not been reached yet.
1023 It is also called to create a window for dictionary data when a dictionary
1024 is loaded.
1025
1026 Providing output buffers larger than 32K to inflate() should provide a speed
1027 advantage, since only the last 32K of output is copied to the sliding window
1028 upon return from inflate(), and since all distances after the first 32K of
1029 output will fall in the output data, making match copies simpler and faster.
1030 The advantage may be dependent on the size of the processor's data caches.
1031 */
1032static int updatewindow( z_streamp strm, const Bytef *end, unsigned copy )
1033{
1034 struct inflate_state FAR *state;
1035 unsigned dist;
1036
1037 state = (struct inflate_state FAR *)strm->state;
1038
1039 /* if it hasn't been done already, allocate space for the window */
1040 if (state->window == Z_NULL) {
1041 state->window = (unsigned char FAR *)
1042 ZALLOC(strm, 1U << state->wbits,
1043 sizeof(unsigned char));
1044 if (state->window == Z_NULL) return 1;
1045 }
1046
1047 /* if window not in use yet, initialize */
1048 if (state->wsize == 0) {
1049 state->wsize = 1U << state->wbits;
1050 state->wnext = 0;
1051 state->whave = 0;
1052 }
1053
1054 /* copy state->wsize or less output bytes into the circular window */
1055 if (copy >= state->wsize) {
1056 zmemcpy(state->window, end - state->wsize, state->wsize);
1057 state->wnext = 0;
1058 state->whave = state->wsize;
1059 }
1060 else {
1061 dist = state->wsize - state->wnext;
1062 if (dist > copy) dist = copy;
1063 zmemcpy(state->window + state->wnext, end - copy, dist);
1064 copy -= dist;
1065 if (copy) {
1066 zmemcpy(state->window, end - copy, copy);
1067 state->wnext = copy;
1068 state->whave = state->wsize;
1069 }
1070 else {
1071 state->wnext += dist;
1072 if (state->wnext == state->wsize) state->wnext = 0;
1073 if (state->whave < state->wsize) state->whave += dist;
1074 }
1075 }
1076 return 0;
1077}
1078
1079/* Macros for inflate(): */
1080
1081#define crc32(crc,buf,len) RtlComputeCrc32(crc,buf,len)
1082
1083/* check function to use adler32() for zlib or crc32() for gzip */
1084#ifdef GUNZIP
1085# define UPDATE(check, buf, len) \
1086 (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
1087#else
1088# define UPDATE(check, buf, len) adler32(check, buf, len)
1089#endif
1090
1091/* check macros for header crc */
1092#ifdef GUNZIP
1093# define CRC2(check, word) \
1094 do { \
1095 hbuf[0] = (unsigned char)(word); \
1096 hbuf[1] = (unsigned char)((word) >> 8); \
1097 check = crc32(check, hbuf, 2); \
1098 } while (0)
1099
1100# define CRC4(check, word) \
1101 do { \
1102 hbuf[0] = (unsigned char)(word); \
1103 hbuf[1] = (unsigned char)((word) >> 8); \
1104 hbuf[2] = (unsigned char)((word) >> 16); \
1105 hbuf[3] = (unsigned char)((word) >> 24); \
1106 check = crc32(check, hbuf, 4); \
1107 } while (0)
1108#endif
1109
1110/* Load registers with state in inflate() for speed */
1111#define LOAD() \
1112 do { \
1113 put = strm->next_out; \
1114 left = strm->avail_out; \
1115 next = strm->next_in; \
1116 have = strm->avail_in; \
1117 hold = state->hold; \
1118 bits = state->bits; \
1119 } while (0)
1120
1121/* Restore state from registers in inflate() */
1122#define RESTORE() \
1123 do { \
1124 strm->next_out = put; \
1125 strm->avail_out = left; \
1126 strm->next_in = next; \
1127 strm->avail_in = have; \
1128 state->hold = hold; \
1129 state->bits = bits; \
1130 } while (0)
1131
1132/* Clear the input bit accumulator */
1133#define INITBITS() \
1134 do { \
1135 hold = 0; \
1136 bits = 0; \
1137 } while (0)
1138
1139/* Get a byte of input into the bit accumulator, or return from inflate()
1140 if there is no input available. */
1141#define PULLBYTE() \
1142 do { \
1143 if (have == 0) goto inf_leave; \
1144 have--; \
1145 hold += (unsigned long)(*next++) << bits; \
1146 bits += 8; \
1147 } while (0)
1148
1149/* Assure that there are at least n bits in the bit accumulator. If there is
1150 not enough available input to do that, then return from inflate(). */
1151#define NEEDBITS(n) \
1152 do { \
1153 while (bits < (unsigned)(n)) \
1154 PULLBYTE(); \
1155 } while (0)
1156
1157/* Return the low n bits of the bit accumulator (n < 16) */
1158#define BITS(n) \
1159 ((unsigned)hold & ((1U << (n)) - 1))
1160
1161/* Remove n bits from the bit accumulator */
1162#define DROPBITS(n) \
1163 do { \
1164 hold >>= (n); \
1165 bits -= (unsigned)(n); \
1166 } while (0)
1167
1168/* Remove zero to seven bits as needed to go to a byte boundary */
1169#define BYTEBITS() \
1170 do { \
1171 hold >>= bits & 7; \
1172 bits -= bits & 7; \
1173 } while (0)
1174
1175/*
1176 inflate() uses a state machine to process as much input data and generate as
1177 much output data as possible before returning. The state machine is
1178 structured roughly as follows:
1179
1180 for (;;) switch (state) {
1181 ...
1182 case STATEn:
1183 if (not enough input data or output space to make progress)
1184 return;
1185 ... make progress ...
1186 state = STATEm;
1187 break;
1188 ...
1189 }
1190
1191 so when inflate() is called again, the same case is attempted again, and
1192 if the appropriate resources are provided, the machine proceeds to the
1193 next state. The NEEDBITS() macro is usually the way the state evaluates
1194 whether it can proceed or should return. NEEDBITS() does the return if
1195 the requested bits are not available. The typical use of the BITS macros
1196 is:
1197
1198 NEEDBITS(n);
1199 ... do something with BITS(n) ...
1200 DROPBITS(n);
1201
1202 where NEEDBITS(n) either returns from inflate() if there isn't enough
1203 input left to load n bits into the accumulator, or it continues. BITS(n)
1204 gives the low n bits in the accumulator. When done, DROPBITS(n) drops
1205 the low n bits off the accumulator. INITBITS() clears the accumulator
1206 and sets the number of available bits to zero. BYTEBITS() discards just
1207 enough bits to put the accumulator on a byte boundary. After BYTEBITS()
1208 and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
1209
1210 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
1211 if there is no input available. The decoding of variable length codes uses
1212 PULLBYTE() directly in order to pull just enough bytes to decode the next
1213 code, and no more.
1214
1215 Some states loop until they get enough input, making sure that enough
1216 state information is maintained to continue the loop where it left off
1217 if NEEDBITS() returns in the loop. For example, want, need, and keep
1218 would all have to actually be part of the saved state in case NEEDBITS()
1219 returns:
1220
1221 case STATEw:
1222 while (want < need) {
1223 NEEDBITS(n);
1224 keep[want++] = BITS(n);
1225 DROPBITS(n);
1226 }
1227 state = STATEx;
1228 case STATEx:
1229
1230 As shown above, if the next state is also the next case, then the break
1231 is omitted.
1232
1233 A state may also return if there is not enough output space available to
1234 complete that state. Those states are copying stored data, writing a
1235 literal byte, and copying a matching string.
1236
1237 When returning, a "goto inf_leave" is used to update the total counters,
1238 update the check value, and determine whether any progress has been made
1239 during that inflate() call in order to return the proper return code.
1240 Progress is defined as a change in either strm->avail_in or strm->avail_out.
1241 When there is a window, goto inf_leave will update the window with the last
1242 output written. If a goto inf_leave occurs in the middle of decompression
1243 and there is no window currently, goto inf_leave will create one and copy
1244 output to the window for the next call of inflate().
1245
1246 In this implementation, the flush parameter of inflate() only affects the
1247 return code (per zlib.h). inflate() always writes as much as possible to
1248 strm->next_out, given the space available and the provided input--the effect
1249 documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
1250 the allocation of and copying into a sliding window until necessary, which
1251 provides the effect documented in zlib.h for Z_FINISH when the entire input
1252 stream available. So the only thing the flush parameter actually does is:
1253 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
1254 will return Z_BUF_ERROR if it has not reached the end of the stream.
1255 */
1256
1257int inflate( z_streamp strm, int flush )
1258{
1259 struct inflate_state FAR *state;
1260 z_const unsigned char FAR *next; /* next input */
1261 unsigned char FAR *put; /* next output */
1262 unsigned have, left; /* available input and output */
1263 unsigned long hold; /* bit buffer */
1264 unsigned bits; /* bits in bit buffer */
1265 unsigned in, out; /* save starting available input and output */
1266 unsigned copy; /* number of stored or match bytes to copy */
1267 unsigned char FAR *from; /* where to copy match bytes from */
1268 code here; /* current decoding table entry */
1269 code last; /* parent table entry */
1270 unsigned len; /* length to copy for repeats, bits to drop */
1271 int ret; /* return code */
1272#ifdef GUNZIP
1273 unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
1274#endif
1275 static const unsigned short order[19] = /* permutation of code lengths */
1276 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
1277
1278 if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||
1279 (strm->next_in == Z_NULL && strm->avail_in != 0))
1280 return Z_STREAM_ERROR;
1281
1282 state = (struct inflate_state FAR *)strm->state;
1283 if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
1284 LOAD();
1285 in = have;
1286 out = left;
1287 ret = Z_OK;
1288 for (;;)
1289 switch (state->mode) {
1290 case HEAD:
1291 if (state->wrap == 0) {
1292 state->mode = TYPEDO;
1293 break;
1294 }
1295 NEEDBITS(16);
1296#ifdef GUNZIP
1297 if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
1298 if (state->wbits == 0)
1299 state->wbits = 15;
1300 state->check = crc32(0L, Z_NULL, 0);
1301 CRC2(state->check, hold);
1302 INITBITS();
1303 state->mode = FLAGS;
1304 break;
1305 }
1306 state->flags = 0; /* expect zlib header */
1307 if (state->head != Z_NULL)
1308 state->head->done = -1;
1309 if (!(state->wrap & 1) || /* check if zlib header allowed */
1310#else
1311 if (
1312#endif
1313 ((BITS(8) << 8) + (hold >> 8)) % 31) {
1314 strm->msg = (char *)"incorrect header check";
1315 state->mode = BAD;
1316 break;
1317 }
1318 if (BITS(4) != Z_DEFLATED) {
1319 strm->msg = (char *)"unknown compression method";
1320 state->mode = BAD;
1321 break;
1322 }
1323 DROPBITS(4);
1324 len = BITS(4) + 8;
1325 if (state->wbits == 0)
1326 state->wbits = len;
1327 if (len > 15 || len > state->wbits) {
1328 strm->msg = (char *)"invalid window size";
1329 state->mode = BAD;
1330 break;
1331 }
1332 state->dmax = 1U << len;
1333 Tracev((stderr, "inflate: zlib header ok\n"));
1334 strm->adler = state->check = adler32(0L, Z_NULL, 0);
1335 state->mode = hold & 0x200 ? DICTID : TYPE;
1336 INITBITS();
1337 break;
1338#ifdef GUNZIP
1339 case FLAGS:
1340 NEEDBITS(16);
1341 state->flags = (int)(hold);
1342 if ((state->flags & 0xff) != Z_DEFLATED) {
1343 strm->msg = (char *)"unknown compression method";
1344 state->mode = BAD;
1345 break;
1346 }
1347 if (state->flags & 0xe000) {
1348 strm->msg = (char *)"unknown header flags set";
1349 state->mode = BAD;
1350 break;
1351 }
1352 if (state->head != Z_NULL)
1353 state->head->text = (int)((hold >> 8) & 1);
1354 if ((state->flags & 0x0200) && (state->wrap & 4))
1355 CRC2(state->check, hold);
1356 INITBITS();
1357 state->mode = TIME;
1358 case TIME:
1359 NEEDBITS(32);
1360 if (state->head != Z_NULL)
1361 state->head->time = hold;
1362 if ((state->flags & 0x0200) && (state->wrap & 4))
1363 CRC4(state->check, hold);
1364 INITBITS();
1365 state->mode = OS;
1366 case OS:
1367 NEEDBITS(16);
1368 if (state->head != Z_NULL) {
1369 state->head->xflags = (int)(hold & 0xff);
1370 state->head->os = (int)(hold >> 8);
1371 }
1372 if ((state->flags & 0x0200) && (state->wrap & 4))
1373 CRC2(state->check, hold);
1374 INITBITS();
1375 state->mode = EXLEN;
1376 case EXLEN:
1377 if (state->flags & 0x0400) {
1378 NEEDBITS(16);
1379 state->length = (unsigned)(hold);
1380 if (state->head != Z_NULL)
1381 state->head->extra_len = (unsigned)hold;
1382 if ((state->flags & 0x0200) && (state->wrap & 4))
1383 CRC2(state->check, hold);
1384 INITBITS();
1385 }
1386 else if (state->head != Z_NULL)
1387 state->head->extra = Z_NULL;
1388 state->mode = EXTRA;
1389 case EXTRA:
1390 if (state->flags & 0x0400) {
1391 copy = state->length;
1392 if (copy > have) copy = have;
1393 if (copy) {
1394 if (state->head != Z_NULL &&
1395 state->head->extra != Z_NULL) {
1396 len = state->head->extra_len - state->length;
1397 zmemcpy(state->head->extra + len, next,
1398 len + copy > state->head->extra_max ?
1399 state->head->extra_max - len : copy);
1400 }
1401 if ((state->flags & 0x0200) && (state->wrap & 4))
1402 state->check = crc32(state->check, next, copy);
1403 have -= copy;
1404 next += copy;
1405 state->length -= copy;
1406 }
1407 if (state->length) goto inf_leave;
1408 }
1409 state->length = 0;
1410 state->mode = NAME;
1411 case NAME:
1412 if (state->flags & 0x0800) {
1413 if (have == 0) goto inf_leave;
1414 copy = 0;
1415 do {
1416 len = (unsigned)(next[copy++]);
1417 if (state->head != Z_NULL &&
1418 state->head->name != Z_NULL &&
1419 state->length < state->head->name_max)
1420 state->head->name[state->length++] = (Bytef)len;
1421 } while (len && copy < have);
1422 if ((state->flags & 0x0200) && (state->wrap & 4))
1423 state->check = crc32(state->check, next, copy);
1424 have -= copy;
1425 next += copy;
1426 if (len) goto inf_leave;
1427 }
1428 else if (state->head != Z_NULL)
1429 state->head->name = Z_NULL;
1430 state->length = 0;
1431 state->mode = COMMENT;
1432 case COMMENT:
1433 if (state->flags & 0x1000) {
1434 if (have == 0) goto inf_leave;
1435 copy = 0;
1436 do {
1437 len = (unsigned)(next[copy++]);
1438 if (state->head != Z_NULL &&
1439 state->head->comment != Z_NULL &&
1440 state->length < state->head->comm_max)
1441 state->head->comment[state->length++] = (Bytef)len;
1442 } while (len && copy < have);
1443 if ((state->flags & 0x0200) && (state->wrap & 4))
1444 state->check = crc32(state->check, next, copy);
1445 have -= copy;
1446 next += copy;
1447 if (len) goto inf_leave;
1448 }
1449 else if (state->head != Z_NULL)
1450 state->head->comment = Z_NULL;
1451 state->mode = HCRC;
1452 case HCRC:
1453 if (state->flags & 0x0200) {
1454 NEEDBITS(16);
1455 if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
1456 strm->msg = (char *)"header crc mismatch";
1457 state->mode = BAD;
1458 break;
1459 }
1460 INITBITS();
1461 }
1462 if (state->head != Z_NULL) {
1463 state->head->hcrc = (int)((state->flags >> 9) & 1);
1464 state->head->done = 1;
1465 }
1466 strm->adler = state->check = crc32(0L, Z_NULL, 0);
1467 state->mode = TYPE;
1468 break;
1469#endif
1470 case DICTID:
1471 NEEDBITS(32);
1472 strm->adler = state->check = ZSWAP32(hold);
1473 INITBITS();
1474 state->mode = DICT;
1475 case DICT:
1476 if (state->havedict == 0) {
1477 RESTORE();
1478 return Z_NEED_DICT;
1479 }
1480 strm->adler = state->check = adler32(0L, Z_NULL, 0);
1481 state->mode = TYPE;
1482 case TYPE:
1483 if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
1484 case TYPEDO:
1485 if (state->last) {
1486 BYTEBITS();
1487 state->mode = CHECK;
1488 break;
1489 }
1490 NEEDBITS(3);
1491 state->last = BITS(1);
1492 DROPBITS(1);
1493 switch (BITS(2)) {
1494 case 0: /* stored block */
1495 Tracev((stderr, "inflate: stored block%s\n",
1496 state->last ? " (last)" : ""));
1497 state->mode = STORED;
1498 break;
1499 case 1: /* fixed block */
1501 Tracev((stderr, "inflate: fixed codes block%s\n",
1502 state->last ? " (last)" : ""));
1503 state->mode = LEN_; /* decode codes */
1504 if (flush == Z_TREES) {
1505 DROPBITS(2);
1506 goto inf_leave;
1507 }
1508 break;
1509 case 2: /* dynamic block */
1510 Tracev((stderr, "inflate: dynamic codes block%s\n",
1511 state->last ? " (last)" : ""));
1512 state->mode = TABLE;
1513 break;
1514 case 3:
1515 strm->msg = (char *)"invalid block type";
1516 state->mode = BAD;
1517 }
1518 DROPBITS(2);
1519 break;
1520 case STORED:
1521 BYTEBITS(); /* go to byte boundary */
1522 NEEDBITS(32);
1523 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
1524 strm->msg = (char *)"invalid stored block lengths";
1525 state->mode = BAD;
1526 break;
1527 }
1528 state->length = (unsigned)hold & 0xffff;
1529 Tracev((stderr, "inflate: stored length %u\n",
1530 state->length));
1531 INITBITS();
1532 state->mode = COPY_;
1533 if (flush == Z_TREES) goto inf_leave;
1534 case COPY_:
1535 state->mode = COPY;
1536 case COPY:
1537 copy = state->length;
1538 if (copy) {
1539 if (copy > have) copy = have;
1540 if (copy > left) copy = left;
1541 if (copy == 0) goto inf_leave;
1542 zmemcpy(put, next, copy);
1543 have -= copy;
1544 next += copy;
1545 left -= copy;
1546 put += copy;
1547 state->length -= copy;
1548 break;
1549 }
1550 Tracev((stderr, "inflate: stored end\n"));
1551 state->mode = TYPE;
1552 break;
1553 case TABLE:
1554 NEEDBITS(14);
1555 state->nlen = BITS(5) + 257;
1556 DROPBITS(5);
1557 state->ndist = BITS(5) + 1;
1558 DROPBITS(5);
1559 state->ncode = BITS(4) + 4;
1560 DROPBITS(4);
1561#ifndef PKZIP_BUG_WORKAROUND
1562 if (state->nlen > 286 || state->ndist > 30) {
1563 strm->msg = (char *)"too many length or distance symbols";
1564 state->mode = BAD;
1565 break;
1566 }
1567#endif
1568 Tracev((stderr, "inflate: table sizes ok\n"));
1569 state->have = 0;
1570 state->mode = LENLENS;
1571 case LENLENS:
1572 while (state->have < state->ncode) {
1573 NEEDBITS(3);
1574 state->lens[order[state->have++]] = (unsigned short)BITS(3);
1575 DROPBITS(3);
1576 }
1577 while (state->have < 19)
1578 state->lens[order[state->have++]] = 0;
1579 state->next = state->codes;
1580 state->lencode = (const code FAR *)(state->next);
1581 state->lenbits = 7;
1582 ret = inflate_table(CODES, state->lens, 19, &(state->next),
1583 &(state->lenbits), state->work);
1584 if (ret) {
1585 strm->msg = (char *)"invalid code lengths set";
1586 state->mode = BAD;
1587 break;
1588 }
1589 Tracev((stderr, "inflate: code lengths ok\n"));
1590 state->have = 0;
1591 state->mode = CODELENS;
1592 case CODELENS:
1593 while (state->have < state->nlen + state->ndist) {
1594 for (;;) {
1595 here = state->lencode[BITS(state->lenbits)];
1596 if ((unsigned)(here.bits) <= bits) break;
1597 PULLBYTE();
1598 }
1599 if (here.val < 16) {
1600 DROPBITS(here.bits);
1601 state->lens[state->have++] = here.val;
1602 }
1603 else {
1604 if (here.val == 16) {
1605 NEEDBITS(here.bits + 2);
1606 DROPBITS(here.bits);
1607 if (state->have == 0) {
1608 strm->msg = (char *)"invalid bit length repeat";
1609 state->mode = BAD;
1610 break;
1611 }
1612 len = state->lens[state->have - 1];
1613 copy = 3 + BITS(2);
1614 DROPBITS(2);
1615 }
1616 else if (here.val == 17) {
1617 NEEDBITS(here.bits + 3);
1618 DROPBITS(here.bits);
1619 len = 0;
1620 copy = 3 + BITS(3);
1621 DROPBITS(3);
1622 }
1623 else {
1624 NEEDBITS(here.bits + 7);
1625 DROPBITS(here.bits);
1626 len = 0;
1627 copy = 11 + BITS(7);
1628 DROPBITS(7);
1629 }
1630 if (state->have + copy > state->nlen + state->ndist) {
1631 strm->msg = (char *)"invalid bit length repeat";
1632 state->mode = BAD;
1633 break;
1634 }
1635 while (copy--)
1636 state->lens[state->have++] = (unsigned short)len;
1637 }
1638 }
1639
1640 /* handle error breaks in while */
1641 if (state->mode == BAD) break;
1642
1643 /* check for end-of-block code (better have one) */
1644 if (state->lens[256] == 0) {
1645 strm->msg = (char *)"invalid code -- missing end-of-block";
1646 state->mode = BAD;
1647 break;
1648 }
1649
1650 /* build code tables -- note: do not change the lenbits or distbits
1651 values here (9 and 6) without reading the comments in inftrees.h
1652 concerning the ENOUGH constants, which depend on those values */
1653 state->next = state->codes;
1654 state->lencode = (const code FAR *)(state->next);
1655 state->lenbits = 9;
1656 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
1657 &(state->lenbits), state->work);
1658 if (ret) {
1659 strm->msg = (char *)"invalid literal/lengths set";
1660 state->mode = BAD;
1661 break;
1662 }
1663 state->distcode = (const code FAR *)(state->next);
1664 state->distbits = 6;
1665 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
1666 &(state->next), &(state->distbits), state->work);
1667 if (ret) {
1668 strm->msg = (char *)"invalid distances set";
1669 state->mode = BAD;
1670 break;
1671 }
1672 Tracev((stderr, "inflate: codes ok\n"));
1673 state->mode = LEN_;
1674 if (flush == Z_TREES) goto inf_leave;
1675 case LEN_:
1676 state->mode = LEN;
1677 case LEN:
1678 if (have >= 6 && left >= 258) {
1679 RESTORE();
1681 LOAD();
1682 if (state->mode == TYPE)
1683 state->back = -1;
1684 break;
1685 }
1686 state->back = 0;
1687 for (;;) {
1688 here = state->lencode[BITS(state->lenbits)];
1689 if ((unsigned)(here.bits) <= bits) break;
1690 PULLBYTE();
1691 }
1692 if (here.op && (here.op & 0xf0) == 0) {
1693 last = here;
1694 for (;;) {
1695 here = state->lencode[last.val +
1696 (BITS(last.bits + last.op) >> last.bits)];
1697 if ((unsigned)(last.bits + here.bits) <= bits) break;
1698 PULLBYTE();
1699 }
1700 DROPBITS(last.bits);
1701 state->back += last.bits;
1702 }
1703 DROPBITS(here.bits);
1704 state->back += here.bits;
1705 state->length = (unsigned)here.val;
1706 if ((int)(here.op) == 0) {
1707 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
1708 "inflate: literal '%c'\n" :
1709 "inflate: literal 0x%02x\n", here.val));
1710 state->mode = LIT;
1711 break;
1712 }
1713 if (here.op & 32) {
1714 Tracevv((stderr, "inflate: end of block\n"));
1715 state->back = -1;
1716 state->mode = TYPE;
1717 break;
1718 }
1719 if (here.op & 64) {
1720 strm->msg = (char *)"invalid literal/length code";
1721 state->mode = BAD;
1722 break;
1723 }
1724 state->extra = (unsigned)(here.op) & 15;
1725 state->mode = LENEXT;
1726 case LENEXT:
1727 if (state->extra) {
1728 NEEDBITS(state->extra);
1729 state->length += BITS(state->extra);
1730 DROPBITS(state->extra);
1731 state->back += state->extra;
1732 }
1733 Tracevv((stderr, "inflate: length %u\n", state->length));
1734 state->was = state->length;
1735 state->mode = DIST;
1736 case DIST:
1737 for (;;) {
1738 here = state->distcode[BITS(state->distbits)];
1739 if ((unsigned)(here.bits) <= bits) break;
1740 PULLBYTE();
1741 }
1742 if ((here.op & 0xf0) == 0) {
1743 last = here;
1744 for (;;) {
1745 here = state->distcode[last.val +
1746 (BITS(last.bits + last.op) >> last.bits)];
1747 if ((unsigned)(last.bits + here.bits) <= bits) break;
1748 PULLBYTE();
1749 }
1750 DROPBITS(last.bits);
1751 state->back += last.bits;
1752 }
1753 DROPBITS(here.bits);
1754 state->back += here.bits;
1755 if (here.op & 64) {
1756 strm->msg = (char *)"invalid distance code";
1757 state->mode = BAD;
1758 break;
1759 }
1760 state->offset = (unsigned)here.val;
1761 state->extra = (unsigned)(here.op) & 15;
1762 state->mode = DISTEXT;
1763 case DISTEXT:
1764 if (state->extra) {
1765 NEEDBITS(state->extra);
1766 state->offset += BITS(state->extra);
1767 DROPBITS(state->extra);
1768 state->back += state->extra;
1769 }
1770#ifdef INFLATE_STRICT
1771 if (state->offset > state->dmax) {
1772 strm->msg = (char *)"invalid distance too far back";
1773 state->mode = BAD;
1774 break;
1775 }
1776#endif
1777 Tracevv((stderr, "inflate: distance %u\n", state->offset));
1778 state->mode = MATCH;
1779 case MATCH:
1780 if (left == 0) goto inf_leave;
1781 copy = out - left;
1782 if (state->offset > copy) { /* copy from window */
1783 copy = state->offset - copy;
1784 if (copy > state->whave) {
1785 if (state->sane) {
1786 strm->msg = (char *)"invalid distance too far back";
1787 state->mode = BAD;
1788 break;
1789 }
1790#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1791 Trace((stderr, "inflate.c too far\n"));
1792 copy -= state->whave;
1793 if (copy > state->length) copy = state->length;
1794 if (copy > left) copy = left;
1795 left -= copy;
1796 state->length -= copy;
1797 do {
1798 *put++ = 0;
1799 } while (--copy);
1800 if (state->length == 0) state->mode = LEN;
1801 break;
1802#endif
1803 }
1804 if (copy > state->wnext) {
1805 copy -= state->wnext;
1806 from = state->window + (state->wsize - copy);
1807 }
1808 else
1809 from = state->window + (state->wnext - copy);
1810 if (copy > state->length) copy = state->length;
1811 }
1812 else { /* copy from output */
1813 from = put - state->offset;
1814 copy = state->length;
1815 }
1816 if (copy > left) copy = left;
1817 left -= copy;
1818 state->length -= copy;
1819 do {
1820 *put++ = *from++;
1821 } while (--copy);
1822 if (state->length == 0) state->mode = LEN;
1823 break;
1824 case LIT:
1825 if (left == 0) goto inf_leave;
1826 *put++ = (unsigned char)(state->length);
1827 left--;
1828 state->mode = LEN;
1829 break;
1830 case CHECK:
1831 if (state->wrap) {
1832 NEEDBITS(32);
1833 out -= left;
1834 strm->total_out += out;
1835 state->total += out;
1836 if ((state->wrap & 4) && out)
1837 strm->adler = state->check =
1838 UPDATE(state->check, put - out, out);
1839 out = left;
1840 if ((state->wrap & 4) && (
1841#ifdef GUNZIP
1842 state->flags ? hold :
1843#endif
1844 ZSWAP32(hold)) != state->check) {
1845 strm->msg = (char *)"incorrect data check";
1846 state->mode = BAD;
1847 break;
1848 }
1849 INITBITS();
1850 Tracev((stderr, "inflate: check matches trailer\n"));
1851 }
1852#ifdef GUNZIP
1853 state->mode = LENGTH;
1854 case LENGTH:
1855 if (state->wrap && state->flags) {
1856 NEEDBITS(32);
1857 if (hold != (state->total & 0xffffffffUL)) {
1858 strm->msg = (char *)"incorrect length check";
1859 state->mode = BAD;
1860 break;
1861 }
1862 INITBITS();
1863 Tracev((stderr, "inflate: length matches trailer\n"));
1864 }
1865#endif
1866 state->mode = DONE;
1867 case DONE:
1868 ret = Z_STREAM_END;
1869 goto inf_leave;
1870 case BAD:
1871 ret = Z_DATA_ERROR;
1872 goto inf_leave;
1873 case MEM:
1874 return Z_MEM_ERROR;
1875 case SYNC:
1876 default:
1877 return Z_STREAM_ERROR;
1878 }
1879
1880 /*
1881 Return from inflate(), updating the total counts and the check value.
1882 If there was no progress during the inflate() call, return a buffer
1883 error. Call updatewindow() to create and/or update the window state.
1884 Note: a memory error from inflate() is non-recoverable.
1885 */
1886 inf_leave:
1887 RESTORE();
1888 if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1889 (state->mode < CHECK || flush != Z_FINISH)))
1890 if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
1891 state->mode = MEM;
1892 return Z_MEM_ERROR;
1893 }
1894 in -= strm->avail_in;
1895 out -= strm->avail_out;
1896 strm->total_in += in;
1897 strm->total_out += out;
1898 state->total += out;
1899 if ((state->wrap & 4) && out)
1900 strm->adler = state->check =
1901 UPDATE(state->check, strm->next_out - out, out);
1902 strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1903 (state->mode == TYPE ? 128 : 0) +
1904 (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1905 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1906 ret = Z_BUF_ERROR;
1907 return ret;
1908}
1909
1911{
1912 struct inflate_state FAR *state;
1914 return Z_STREAM_ERROR;
1915 state = (struct inflate_state FAR *)strm->state;
1916 if (state->window != Z_NULL) ZFREE(strm, state->window);
1917 ZFREE(strm, strm->state);
1918 strm->state = Z_NULL;
1919 Tracev((stderr, "inflate: end\n"));
1920 return Z_OK;
1921}
_STLP_MOVE_TO_STD_NAMESPACE void fill(_ForwardIter __first, _ForwardIter __last, const _Tp &__val)
Definition: _algobase.h:449
static int used
Definition: adh-main.c:39
static int state
Definition: maze.c:121
INT copy(TCHAR source[MAX_PATH], TCHAR dest[MAX_PATH], INT append, DWORD lpdwFlags, BOOL bTouch)
Definition: copy.c:51
struct _root root
UINT op
Definition: effect.c:236
#define LOAD()
Definition: inflate.c:1111
codetype
Definition: inflate.c:150
@ CODES
Definition: inflate.c:151
@ LENS
Definition: inflate.c:152
@ DISTS
Definition: inflate.c:153
#define crc32(crc, buf, len)
Definition: inflate.c:1081
inflate_mode
Definition: inflate.c:157
@ HEAD
Definition: inflate.c:158
@ MATCH
Definition: inflate.c:182
@ DICT
Definition: inflate.c:168
@ TABLE
Definition: inflate.c:174
@ LENGTH
Definition: inflate.c:185
@ FLAGS
Definition: inflate.c:159
@ LIT
Definition: inflate.c:183
@ SYNC
Definition: inflate.c:189
@ OS
Definition: inflate.c:161
@ EXLEN
Definition: inflate.c:162
@ MEM
Definition: inflate.c:188
@ NAME
Definition: inflate.c:164
@ STORED
Definition: inflate.c:171
@ CODELENS
Definition: inflate.c:176
@ DICTID
Definition: inflate.c:167
@ DONE
Definition: inflate.c:186
@ TYPEDO
Definition: inflate.c:170
@ COMMENT
Definition: inflate.c:165
@ LENLENS
Definition: inflate.c:175
@ TYPE
Definition: inflate.c:169
@ COPY
Definition: inflate.c:173
@ LEN_
Definition: inflate.c:177
@ COPY_
Definition: inflate.c:172
@ DIST
Definition: inflate.c:180
@ LENEXT
Definition: inflate.c:179
@ HCRC
Definition: inflate.c:166
@ TIME
Definition: inflate.c:160
@ CHECK
Definition: inflate.c:184
@ DISTEXT
Definition: inflate.c:181
@ BAD
Definition: inflate.c:187
@ LEN
Definition: inflate.c:178
@ EXTRA
Definition: inflate.c:163
#define MOD28(a)
Definition: inflate.c:69
#define CRC2(check, word)
Definition: inflate.c:1093
#define zmemcpy
Definition: inflate.c:38
static int inflateReset2(z_streamp strm, int windowBits)
Definition: inflate.c:851
static int inflate_table(codetype type, unsigned short FAR *lens, unsigned codes, code FAR *FAR *table, unsigned FAR *bits, unsigned short FAR *work)
Definition: inflate.c:533
#define ZALLOC(strm, items, size)
Definition: inflate.c:49
#define INITBITS()
Definition: inflate.c:1133
static int inflateReset(z_streamp strm)
Definition: inflate.c:839
static int inflateResetKeep(z_streamp strm)
Definition: inflate.c:815
#define CRC4(check, word)
Definition: inflate.c:1100
#define BITS(n)
Definition: inflate.c:1158
static void inflate_fast(z_streamp strm, unsigned start)
Definition: inflate.c:274
#define NMAX
Definition: inflate.c:59
#define UPDATE(check, buf, len)
Definition: inflate.c:1085
#define DROPBITS(n)
Definition: inflate.c:1162
static int inflateStateCheck(z_streamp strm)
Definition: inflate.c:802
#define Tracev(x)
Definition: inflate.c:43
#define DO16(buf)
Definition: inflate.c:66
#define MAXBITS
Definition: inflate.c:531
static void fixedtables(struct inflate_state FAR *state)
Definition: inflate.c:924
#define ZFREE(strm, addr)
Definition: inflate.c:51
int inflate(z_streamp strm, int flush)
Definition: inflate.c:1257
#define BASE
Definition: inflate.c:58
#define Trace(x)
Definition: inflate.c:42
#define ENOUGH_LENS
Definition: inflate.c:145
#define MOD(a)
Definition: inflate.c:68
#define BYTEBITS()
Definition: inflate.c:1169
#define ENOUGH_DISTS
Definition: inflate.c:146
#define ZSWAP32(q)
Definition: inflate.c:55
static uLong adler32(uLong adler, const Bytef *buf, uInt len)
Definition: inflate.c:72
#define NEEDBITS(n)
Definition: inflate.c:1151
int inflateEnd(z_streamp strm)
Definition: inflate.c:1910
#define ENOUGH
Definition: inflate.c:147
#define PULLBYTE()
Definition: inflate.c:1141
#define Tracevv(x)
Definition: inflate.c:44
#define DEF_WBITS
Definition: inflate.c:37
#define GUNZIP
Definition: inflate.c:47
static int updatewindow(z_streamp strm, const Bytef *end, unsigned copy)
Definition: inflate.c:1032
#define RESTORE()
Definition: inflate.c:1122
void(* free_func)(voidpf opaque, voidpf address)
Definition: zlib.h:54
#define Z_TREES
Definition: zlib.h:111
unsigned long uLong
Definition: zlib.h:39
#define Z_DEFLATED
Definition: zlib.h:146
#define Z_NEED_DICT
Definition: zlib.h:116
gz_header FAR * gz_headerp
Definition: zlib.h:103
#define Z_BUF_ERROR
Definition: zlib.h:121
voidpf(* alloc_func)(voidpf opaque, uInt items, uInt size)
Definition: zlib.h:53
z_stream FAR * z_streamp
Definition: zlib.h:80
#define Z_BLOCK
Definition: zlib.h:110
#define Z_STREAM_END
Definition: zlib.h:115
#define Z_FINISH
Definition: zlib.h:109
unsigned int uInt
Definition: zlib.h:38
#define Z_OK
Definition: zlib.h:114
#define z_const
Definition: zlib.h:35
#define Z_DATA_ERROR
Definition: zlib.h:119
#define Z_STREAM_ERROR
Definition: zlib.h:118
#define Z_NULL
Definition: zlib.h:149
#define Z_MEM_ERROR
Definition: zlib.h:120
Byte FAR Bytef
Definition: zlib.h:41
#define FAR
Definition: zlib.h:34
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
unsigned char
Definition: typeof.h:29
unsigned short(__cdecl typeof(TIFFCurrentDirectory))(struct tiff *)
Definition: typeof.h:94
#define wrap(journal, var)
Definition: recovery.c:207
TYPE
Definition: eventcreate.c:652
GLuint start
Definition: gl.h:1545
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLuint GLuint end
Definition: gl.h:1545
GLuint GLuint GLsizei count
Definition: gl.h:1545
GLdouble n
Definition: glext.h:7729
GLenum GLint GLuint mask
Definition: glext.h:6028
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLuint in
Definition: glext.h:9616
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * bits
Definition: glext.h:10929
GLint left
Definition: glext.h:7726
GLuint GLdouble GLdouble GLint GLint order
Definition: glext.h:11194
GLenum GLsizei len
Definition: glext.h:6722
@ extra
Definition: id3.c:95
#define stderr
Definition: stdio.h:100
#define bits
Definition: infblock.c:15
inflate_mode
Definition: inflate.h:20
if(dx< 0)
Definition: linetemp.h:194
#define put(ret, state, sp, n)
Definition: match.c:105
static UINT UINT last
Definition: font.c:45
static IHTMLWindow2 * window
Definition: events.c:77
static unsigned(__cdecl *hash_bstr)(bstr_t s)
#define min(a, b)
Definition: monoChain.cc:55
#define L(x)
Definition: ntvdm.h:50
#define long
Definition: qsort.c:33
static unsigned __int64 next
Definition: rand_nt.c:6
static FILE * out
Definition: regtests2xml.c:44
#define inflateInit2(strm, windowBits)
Definition: zlib.h:1817
#define inflateInit(strm)
Definition: zlib.h:1812
int windowBits
Definition: zlib.h:813
int flush
Definition: zlib.h:309
CardRegion * from
Definition: spigame.cpp:19
Definition: ntuser.h:180
Definition: inflate.c:139
unsigned char op
Definition: inflate.c:140
unsigned char bits
Definition: inflate.c:141
unsigned short val
Definition: inflate.c:142
unsigned was
Definition: inflate.c:236
unsigned wnext
Definition: inflate.c:210
unsigned short work[288]
Definition: inflate.c:232
int havedict
Definition: inflate.c:200
unsigned short lens[320]
Definition: inflate.c:231
unsigned char FAR * window
Definition: inflate.c:211
unsigned lenbits
Definition: inflate.c:223
unsigned ndist
Definition: inflate.c:228
unsigned nlen
Definition: inflate.c:227
unsigned have
Definition: inflate.c:229
unsigned length
Definition: inflate.c:216
unsigned long hold
Definition: inflate.c:213
unsigned extra
Definition: inflate.c:219
code const FAR * distcode
Definition: inflate.c:222
unsigned ncode
Definition: inflate.c:226
z_streamp strm
Definition: inflate.c:195
unsigned whave
Definition: inflate.c:209
unsigned wbits
Definition: inflate.c:207
code FAR * next
Definition: inflate.c:230
unsigned distbits
Definition: inflate.c:224
inflate_mode mode
Definition: inflate.c:196
gz_headerp head
Definition: inflate.c:205
unsigned bits
Definition: inflate.c:214
code const FAR * lencode
Definition: inflate.c:221
unsigned wsize
Definition: inflate.c:208
unsigned dmax
Definition: inflate.c:202
unsigned long check
Definition: inflate.c:203
unsigned offset
Definition: inflate.c:217
code codes[ENOUGH]
Definition: inflate.c:233
unsigned long total
Definition: inflate.c:204
z_streamp strm
Definition: deflate.h:101
Definition: match.c:28
#define max(a, b)
Definition: svc.c:63
int ret
static const code distfix[32]
Definition: inffixed.h:87
static const code lenfix[512]
Definition: inffixed.h:10