···11+/* inflate.c -- zlib decompression
22+ *
33+ * Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler
44+ *
55+ * This software is provided 'as-is', without any express or implied
66+ * warranty. In no event will the authors be held liable for any damages
77+ * arising from the use of this software.
88+ *
99+ * Permission is granted to anyone to use this software for any purpose,
1010+ * including commercial applications, and to alter it and redistribute it
1111+ * freely, subject to the following restrictions:
1212+ *
1313+ * 1. The origin of this software must not be misrepresented; you must not
1414+ * claim that you wrote the original software. If you use this software
1515+ * in a product, an acknowledgment in the product documentation would be
1616+ * appreciated but is not required.
1717+ * 2. Altered source versions must be plainly marked as such, and must not be
1818+ * misrepresented as being the original software.
1919+ * 3. This notice may not be removed or altered from any source distribution.
2020+ *
2121+ * Jean-loup Gailly Mark Adler
2222+ * jloup@gzip.org madler@alumni.caltech.edu
2323+ */
2424+2525+#include <stdarg.h>
2626+#include <stdlib.h>
2727+#include <string.h>
2828+#include <limits.h>
2929+#include "winternl.h"
3030+#include "zlib.h"
3131+3232+#define DEF_WBITS MAX_WBITS
3333+#define zmemcpy memcpy
3434+#define zmemzero(dest, len) memset(dest, 0, len)
3535+3636+#define Assert(cond,msg)
3737+#define Trace(x)
3838+#define Tracev(x)
3939+#define Tracevv(x)
4040+#define Tracecv(c,x)
4141+4242+#define GUNZIP
4343+4444+#define ZALLOC(strm, items, size) \
4545+ (*((strm)->zalloc))((strm)->opaque, (items), (size))
4646+#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
4747+#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
4848+4949+/* Reverse the bytes in a 32-bit value */
5050+#define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
5151+ (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
5252+5353+#define BASE 65521U /* largest prime smaller than 65536 */
5454+#define NMAX 5552
5555+/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
5656+5757+#define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
5858+#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
5959+#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
6060+#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
6161+#define DO16(buf) DO8(buf,0); DO8(buf,8);
6262+6363+#define MOD(a) a %= BASE
6464+#define MOD28(a) a %= BASE
6565+#define MOD63(a) a %= BASE
6666+6767+static uLong adler32( uLong adler, const Bytef *buf, uInt len )
6868+{
6969+ unsigned long sum2;
7070+ unsigned n;
7171+7272+ /* split Adler-32 into component sums */
7373+ sum2 = (adler >> 16) & 0xffff;
7474+ adler &= 0xffff;
7575+7676+ /* in case user likes doing a byte at a time, keep it fast */
7777+ if (len == 1) {
7878+ adler += buf[0];
7979+ if (adler >= BASE)
8080+ adler -= BASE;
8181+ sum2 += adler;
8282+ if (sum2 >= BASE)
8383+ sum2 -= BASE;
8484+ return adler | (sum2 << 16);
8585+ }
8686+8787+ /* initial Adler-32 value (deferred check for len == 1 speed) */
8888+ if (buf == Z_NULL)
8989+ return 1L;
9090+9191+ /* in case short lengths are provided, keep it somewhat fast */
9292+ if (len < 16) {
9393+ while (len--) {
9494+ adler += *buf++;
9595+ sum2 += adler;
9696+ }
9797+ if (adler >= BASE)
9898+ adler -= BASE;
9999+ MOD28(sum2); /* only added so many BASE's */
100100+ return adler | (sum2 << 16);
101101+ }
102102+103103+ /* do length NMAX blocks -- requires just one modulo operation */
104104+ while (len >= NMAX) {
105105+ len -= NMAX;
106106+ n = NMAX / 16; /* NMAX is divisible by 16 */
107107+ do {
108108+ DO16(buf); /* 16 sums unrolled */
109109+ buf += 16;
110110+ } while (--n);
111111+ MOD(adler);
112112+ MOD(sum2);
113113+ }
114114+115115+ /* do remaining bytes (less than NMAX, still just one modulo) */
116116+ if (len) { /* avoid modulos if none remaining */
117117+ while (len >= 16) {
118118+ len -= 16;
119119+ DO16(buf);
120120+ buf += 16;
121121+ }
122122+ while (len--) {
123123+ adler += *buf++;
124124+ sum2 += adler;
125125+ }
126126+ MOD(adler);
127127+ MOD(sum2);
128128+ }
129129+130130+ /* return recombined sums */
131131+ return adler | (sum2 << 16);
132132+}
133133+134134+typedef struct {
135135+ unsigned char op; /* operation, extra bits, table bits */
136136+ unsigned char bits; /* bits in this part of the code */
137137+ unsigned short val; /* offset in table or code value */
138138+} code;
139139+140140+#define ENOUGH_LENS 852
141141+#define ENOUGH_DISTS 592
142142+#define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS)
143143+144144+/* Type of code to build for inflate_table() */
145145+typedef enum {
146146+ CODES,
147147+ LENS,
148148+ DISTS
149149+} codetype;
150150+151151+/* Possible inflate modes between inflate() calls */
152152+typedef enum {
153153+ HEAD = 16180, /* i: waiting for magic header */
154154+ FLAGS, /* i: waiting for method and flags (gzip) */
155155+ TIME, /* i: waiting for modification time (gzip) */
156156+ OS, /* i: waiting for extra flags and operating system (gzip) */
157157+ EXLEN, /* i: waiting for extra length (gzip) */
158158+ EXTRA, /* i: waiting for extra bytes (gzip) */
159159+ NAME, /* i: waiting for end of file name (gzip) */
160160+ COMMENT, /* i: waiting for end of comment (gzip) */
161161+ HCRC, /* i: waiting for header crc (gzip) */
162162+ DICTID, /* i: waiting for dictionary check value */
163163+ DICT, /* waiting for inflateSetDictionary() call */
164164+ TYPE, /* i: waiting for type bits, including last-flag bit */
165165+ TYPEDO, /* i: same, but skip check to exit inflate on new block */
166166+ STORED, /* i: waiting for stored size (length and complement) */
167167+ COPY_, /* i/o: same as COPY below, but only first time in */
168168+ COPY, /* i/o: waiting for input or output to copy stored block */
169169+ TABLE, /* i: waiting for dynamic block table lengths */
170170+ LENLENS, /* i: waiting for code length code lengths */
171171+ CODELENS, /* i: waiting for length/lit and distance code lengths */
172172+ LEN_, /* i: same as LEN below, but only first time in */
173173+ LEN, /* i: waiting for length/lit/eob code */
174174+ LENEXT, /* i: waiting for length extra bits */
175175+ DIST, /* i: waiting for distance code */
176176+ DISTEXT, /* i: waiting for distance extra bits */
177177+ MATCH, /* o: waiting for output space to copy string */
178178+ LIT, /* o: waiting for output space to write literal */
179179+ CHECK, /* i: waiting for 32-bit check value */
180180+ LENGTH, /* i: waiting for 32-bit length (gzip) */
181181+ DONE, /* finished check, done -- remain here until reset */
182182+ BAD, /* got a data error -- remain here until reset */
183183+ MEM, /* got an inflate() memory error -- remain here until reset */
184184+ SYNC /* looking for synchronization bytes to restart inflate() */
185185+} inflate_mode;
186186+187187+/* State maintained between inflate() calls -- approximately 7K bytes, not
188188+ including the allocated sliding window, which is up to 32K bytes. */
189189+struct inflate_state {
190190+ z_streamp strm; /* pointer back to this zlib stream */
191191+ inflate_mode mode; /* current inflate mode */
192192+ int last; /* true if processing last block */
193193+ int wrap; /* bit 0 true for zlib, bit 1 true for gzip,
194194+ bit 2 true to validate check value */
195195+ int havedict; /* true if dictionary provided */
196196+ int flags; /* gzip header method and flags (0 if zlib) */
197197+ unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */
198198+ unsigned long check; /* protected copy of check value */
199199+ unsigned long total; /* protected copy of output count */
200200+ gz_headerp head; /* where to save gzip header information */
201201+ /* sliding window */
202202+ unsigned wbits; /* log base 2 of requested window size */
203203+ unsigned wsize; /* window size or zero if not using window */
204204+ unsigned whave; /* valid bytes in the window */
205205+ unsigned wnext; /* window write index */
206206+ unsigned char FAR *window; /* allocated sliding window, if needed */
207207+ /* bit accumulator */
208208+ unsigned long hold; /* input bit accumulator */
209209+ unsigned bits; /* number of bits in "in" */
210210+ /* for string and stored block copying */
211211+ unsigned length; /* literal or length of data to copy */
212212+ unsigned offset; /* distance back to copy string from */
213213+ /* for table and code decoding */
214214+ unsigned extra; /* extra bits needed */
215215+ /* fixed and dynamic code tables */
216216+ code const FAR *lencode; /* starting table for length/literal codes */
217217+ code const FAR *distcode; /* starting table for distance codes */
218218+ unsigned lenbits; /* index bits for lencode */
219219+ unsigned distbits; /* index bits for distcode */
220220+ /* dynamic table building */
221221+ unsigned ncode; /* number of code length code lengths */
222222+ unsigned nlen; /* number of length code lengths */
223223+ unsigned ndist; /* number of distance code lengths */
224224+ unsigned have; /* number of code lengths in lens[] */
225225+ code FAR *next; /* next available space in codes[] */
226226+ unsigned short lens[320]; /* temporary storage for code lengths */
227227+ unsigned short work[288]; /* work area for code table building */
228228+ code codes[ENOUGH]; /* space for code tables */
229229+ int sane; /* if false, allow invalid distance too far */
230230+ int back; /* bits back of last unprocessed length/lit */
231231+ unsigned was; /* initial length of match */
232232+};
233233+234234+/*
235235+ Decode literal, length, and distance codes and write out the resulting
236236+ literal and match bytes until either not enough input or output is
237237+ available, an end-of-block is encountered, or a data error is encountered.
238238+ When large enough input and output buffers are supplied to inflate(), for
239239+ example, a 16K input buffer and a 64K output buffer, more than 95% of the
240240+ inflate execution time is spent in this routine.
241241+242242+ Entry assumptions:
243243+244244+ state->mode == LEN
245245+ strm->avail_in >= 6
246246+ strm->avail_out >= 258
247247+ start >= strm->avail_out
248248+ state->bits < 8
249249+250250+ On return, state->mode is one of:
251251+252252+ LEN -- ran out of enough output space or enough available input
253253+ TYPE -- reached end of block code, inflate() to interpret next block
254254+ BAD -- error in block data
255255+256256+ Notes:
257257+258258+ - The maximum input bits used by a length/distance pair is 15 bits for the
259259+ length code, 5 bits for the length extra, 15 bits for the distance code,
260260+ and 13 bits for the distance extra. This totals 48 bits, or six bytes.
261261+ Therefore if strm->avail_in >= 6, then there is enough input to avoid
262262+ checking for available input while decoding.
263263+264264+ - The maximum bytes that a single length/distance pair can output is 258
265265+ bytes, which is the maximum length that can be coded. inflate_fast()
266266+ requires strm->avail_out >= 258 for each loop to avoid checking for
267267+ output space.
268268+ */
269269+static void inflate_fast( z_streamp strm, unsigned start )
270270+{
271271+ struct inflate_state FAR *state;
272272+ z_const unsigned char FAR *in; /* local strm->next_in */
273273+ z_const unsigned char FAR *last; /* have enough input while in < last */
274274+ unsigned char FAR *out; /* local strm->next_out */
275275+ unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
276276+ unsigned char FAR *end; /* while out < end, enough space available */
277277+#ifdef INFLATE_STRICT
278278+ unsigned dmax; /* maximum distance from zlib header */
279279+#endif
280280+ unsigned wsize; /* window size or zero if not using window */
281281+ unsigned whave; /* valid bytes in the window */
282282+ unsigned wnext; /* window write index */
283283+ unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
284284+ unsigned long hold; /* local strm->hold */
285285+ unsigned bits; /* local strm->bits */
286286+ code const FAR *lcode; /* local strm->lencode */
287287+ code const FAR *dcode; /* local strm->distcode */
288288+ unsigned lmask; /* mask for first level of length codes */
289289+ unsigned dmask; /* mask for first level of distance codes */
290290+ code here; /* retrieved table entry */
291291+ unsigned op; /* code bits, operation, extra bits, or */
292292+ /* window position, window bytes to copy */
293293+ unsigned len; /* match length, unused bytes */
294294+ unsigned dist; /* match distance */
295295+ unsigned char FAR *from; /* where to copy match from */
296296+297297+ /* copy state to local variables */
298298+ state = (struct inflate_state FAR *)strm->state;
299299+ in = strm->next_in;
300300+ last = in + (strm->avail_in - 5);
301301+ out = strm->next_out;
302302+ beg = out - (start - strm->avail_out);
303303+ end = out + (strm->avail_out - 257);
304304+#ifdef INFLATE_STRICT
305305+ dmax = state->dmax;
306306+#endif
307307+ wsize = state->wsize;
308308+ whave = state->whave;
309309+ wnext = state->wnext;
310310+ window = state->window;
311311+ hold = state->hold;
312312+ bits = state->bits;
313313+ lcode = state->lencode;
314314+ dcode = state->distcode;
315315+ lmask = (1U << state->lenbits) - 1;
316316+ dmask = (1U << state->distbits) - 1;
317317+318318+ /* decode literals and length/distances until end-of-block or not enough
319319+ input data or output space */
320320+ do {
321321+ if (bits < 15) {
322322+ hold += (unsigned long)(*in++) << bits;
323323+ bits += 8;
324324+ hold += (unsigned long)(*in++) << bits;
325325+ bits += 8;
326326+ }
327327+ here = lcode[hold & lmask];
328328+ dolen:
329329+ op = (unsigned)(here.bits);
330330+ hold >>= op;
331331+ bits -= op;
332332+ op = (unsigned)(here.op);
333333+ if (op == 0) { /* literal */
334334+ Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
335335+ "inflate: literal '%c'\n" :
336336+ "inflate: literal 0x%02x\n", here.val));
337337+ *out++ = (unsigned char)(here.val);
338338+ }
339339+ else if (op & 16) { /* length base */
340340+ len = (unsigned)(here.val);
341341+ op &= 15; /* number of extra bits */
342342+ if (op) {
343343+ if (bits < op) {
344344+ hold += (unsigned long)(*in++) << bits;
345345+ bits += 8;
346346+ }
347347+ len += (unsigned)hold & ((1U << op) - 1);
348348+ hold >>= op;
349349+ bits -= op;
350350+ }
351351+ Tracevv((stderr, "inflate: length %u\n", len));
352352+ if (bits < 15) {
353353+ hold += (unsigned long)(*in++) << bits;
354354+ bits += 8;
355355+ hold += (unsigned long)(*in++) << bits;
356356+ bits += 8;
357357+ }
358358+ here = dcode[hold & dmask];
359359+ dodist:
360360+ op = (unsigned)(here.bits);
361361+ hold >>= op;
362362+ bits -= op;
363363+ op = (unsigned)(here.op);
364364+ if (op & 16) { /* distance base */
365365+ dist = (unsigned)(here.val);
366366+ op &= 15; /* number of extra bits */
367367+ if (bits < op) {
368368+ hold += (unsigned long)(*in++) << bits;
369369+ bits += 8;
370370+ if (bits < op) {
371371+ hold += (unsigned long)(*in++) << bits;
372372+ bits += 8;
373373+ }
374374+ }
375375+ dist += (unsigned)hold & ((1U << op) - 1);
376376+#ifdef INFLATE_STRICT
377377+ if (dist > dmax) {
378378+ strm->msg = (char *)"invalid distance too far back";
379379+ state->mode = BAD;
380380+ break;
381381+ }
382382+#endif
383383+ hold >>= op;
384384+ bits -= op;
385385+ Tracevv((stderr, "inflate: distance %u\n", dist));
386386+ op = (unsigned)(out - beg); /* max distance in output */
387387+ if (dist > op) { /* see if copy from window */
388388+ op = dist - op; /* distance back in window */
389389+ if (op > whave) {
390390+ if (state->sane) {
391391+ strm->msg =
392392+ (char *)"invalid distance too far back";
393393+ state->mode = BAD;
394394+ break;
395395+ }
396396+#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
397397+ if (len <= op - whave) {
398398+ do {
399399+ *out++ = 0;
400400+ } while (--len);
401401+ continue;
402402+ }
403403+ len -= op - whave;
404404+ do {
405405+ *out++ = 0;
406406+ } while (--op > whave);
407407+ if (op == 0) {
408408+ from = out - dist;
409409+ do {
410410+ *out++ = *from++;
411411+ } while (--len);
412412+ continue;
413413+ }
414414+#endif
415415+ }
416416+ from = window;
417417+ if (wnext == 0) { /* very common case */
418418+ from += wsize - op;
419419+ if (op < len) { /* some from window */
420420+ len -= op;
421421+ do {
422422+ *out++ = *from++;
423423+ } while (--op);
424424+ from = out - dist; /* rest from output */
425425+ }
426426+ }
427427+ else if (wnext < op) { /* wrap around window */
428428+ from += wsize + wnext - op;
429429+ op -= wnext;
430430+ if (op < len) { /* some from end of window */
431431+ len -= op;
432432+ do {
433433+ *out++ = *from++;
434434+ } while (--op);
435435+ from = window;
436436+ if (wnext < len) { /* some from start of window */
437437+ op = wnext;
438438+ len -= op;
439439+ do {
440440+ *out++ = *from++;
441441+ } while (--op);
442442+ from = out - dist; /* rest from output */
443443+ }
444444+ }
445445+ }
446446+ else { /* contiguous in window */
447447+ from += wnext - op;
448448+ if (op < len) { /* some from window */
449449+ len -= op;
450450+ do {
451451+ *out++ = *from++;
452452+ } while (--op);
453453+ from = out - dist; /* rest from output */
454454+ }
455455+ }
456456+ while (len > 2) {
457457+ *out++ = *from++;
458458+ *out++ = *from++;
459459+ *out++ = *from++;
460460+ len -= 3;
461461+ }
462462+ if (len) {
463463+ *out++ = *from++;
464464+ if (len > 1)
465465+ *out++ = *from++;
466466+ }
467467+ }
468468+ else {
469469+ from = out - dist; /* copy direct from output */
470470+ do { /* minimum length is three */
471471+ *out++ = *from++;
472472+ *out++ = *from++;
473473+ *out++ = *from++;
474474+ len -= 3;
475475+ } while (len > 2);
476476+ if (len) {
477477+ *out++ = *from++;
478478+ if (len > 1)
479479+ *out++ = *from++;
480480+ }
481481+ }
482482+ }
483483+ else if ((op & 64) == 0) { /* 2nd level distance code */
484484+ here = dcode[here.val + (hold & ((1U << op) - 1))];
485485+ goto dodist;
486486+ }
487487+ else {
488488+ strm->msg = (char *)"invalid distance code";
489489+ state->mode = BAD;
490490+ break;
491491+ }
492492+ }
493493+ else if ((op & 64) == 0) { /* 2nd level length code */
494494+ here = lcode[here.val + (hold & ((1U << op) - 1))];
495495+ goto dolen;
496496+ }
497497+ else if (op & 32) { /* end-of-block */
498498+ Tracevv((stderr, "inflate: end of block\n"));
499499+ state->mode = TYPE;
500500+ break;
501501+ }
502502+ else {
503503+ strm->msg = (char *)"invalid literal/length code";
504504+ state->mode = BAD;
505505+ break;
506506+ }
507507+ } while (in < last && out < end);
508508+509509+ /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
510510+ len = bits >> 3;
511511+ in -= len;
512512+ bits -= len << 3;
513513+ hold &= (1U << bits) - 1;
514514+515515+ /* update state and return */
516516+ strm->next_in = in;
517517+ strm->next_out = out;
518518+ strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
519519+ strm->avail_out = (unsigned)(out < end ?
520520+ 257 + (end - out) : 257 - (out - end));
521521+ state->hold = hold;
522522+ state->bits = bits;
523523+ return;
524524+}
525525+526526+#define MAXBITS 15
527527+528528+static int inflate_table( codetype type, unsigned short FAR *lens, unsigned codes, code FAR * FAR *table,
529529+ unsigned FAR *bits, unsigned short FAR *work )
530530+{
531531+ unsigned len; /* a code's length in bits */
532532+ unsigned sym; /* index of code symbols */
533533+ unsigned min, max; /* minimum and maximum code lengths */
534534+ unsigned root; /* number of index bits for root table */
535535+ unsigned curr; /* number of index bits for current table */
536536+ unsigned drop; /* code bits to drop for sub-table */
537537+ int left; /* number of prefix codes available */
538538+ unsigned used; /* code entries in table used */
539539+ unsigned huff; /* Huffman code */
540540+ unsigned incr; /* for incrementing code, index */
541541+ unsigned fill; /* index for replicating entries */
542542+ unsigned low; /* low bits for current root entry */
543543+ unsigned mask; /* mask for low root bits */
544544+ code here; /* table entry for duplication */
545545+ code FAR *next; /* next available space in table */
546546+ const unsigned short FAR *base; /* base value table to use */
547547+ const unsigned short FAR *extra; /* extra bits table to use */
548548+ unsigned match; /* use base and extra for symbol >= match */
549549+ unsigned short count[MAXBITS+1]; /* number of codes of each length */
550550+ unsigned short offs[MAXBITS+1]; /* offsets in table for each length */
551551+ static const unsigned short lbase[31] = { /* Length codes 257..285 base */
552552+ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
553553+ 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
554554+ static const unsigned short lext[31] = { /* Length codes 257..285 extra */
555555+ 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
556556+ 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 77, 202};
557557+ static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
558558+ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
559559+ 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
560560+ 8193, 12289, 16385, 24577, 0, 0};
561561+ static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
562562+ 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
563563+ 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
564564+ 28, 28, 29, 29, 64, 64};
565565+566566+ /*
567567+ Process a set of code lengths to create a canonical Huffman code. The
568568+ code lengths are lens[0..codes-1]. Each length corresponds to the
569569+ symbols 0..codes-1. The Huffman code is generated by first sorting the
570570+ symbols by length from short to long, and retaining the symbol order
571571+ for codes with equal lengths. Then the code starts with all zero bits
572572+ for the first code of the shortest length, and the codes are integer
573573+ increments for the same length, and zeros are appended as the length
574574+ increases. For the deflate format, these bits are stored backwards
575575+ from their more natural integer increment ordering, and so when the
576576+ decoding tables are built in the large loop below, the integer codes
577577+ are incremented backwards.
578578+579579+ This routine assumes, but does not check, that all of the entries in
580580+ lens[] are in the range 0..MAXBITS. The caller must assure this.
581581+ 1..MAXBITS is interpreted as that code length. zero means that that
582582+ symbol does not occur in this code.
583583+584584+ The codes are sorted by computing a count of codes for each length,
585585+ creating from that a table of starting indices for each length in the
586586+ sorted table, and then entering the symbols in order in the sorted
587587+ table. The sorted table is work[], with that space being provided by
588588+ the caller.
589589+590590+ The length counts are used for other purposes as well, i.e. finding
591591+ the minimum and maximum length codes, determining if there are any
592592+ codes at all, checking for a valid set of lengths, and looking ahead
593593+ at length counts to determine sub-table sizes when building the
594594+ decoding tables.
595595+ */
596596+597597+ /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
598598+ for (len = 0; len <= MAXBITS; len++)
599599+ count[len] = 0;
600600+ for (sym = 0; sym < codes; sym++)
601601+ count[lens[sym]]++;
602602+603603+ /* bound code lengths, force root to be within code lengths */
604604+ root = *bits;
605605+ for (max = MAXBITS; max >= 1; max--)
606606+ if (count[max] != 0) break;
607607+ if (root > max) root = max;
608608+ if (max == 0) { /* no symbols to code at all */
609609+ here.op = (unsigned char)64; /* invalid code marker */
610610+ here.bits = (unsigned char)1;
611611+ here.val = (unsigned short)0;
612612+ *(*table)++ = here; /* make a table to force an error */
613613+ *(*table)++ = here;
614614+ *bits = 1;
615615+ return 0; /* no symbols, but wait for decoding to report error */
616616+ }
617617+ for (min = 1; min < max; min++)
618618+ if (count[min] != 0) break;
619619+ if (root < min) root = min;
620620+621621+ /* check for an over-subscribed or incomplete set of lengths */
622622+ left = 1;
623623+ for (len = 1; len <= MAXBITS; len++) {
624624+ left <<= 1;
625625+ left -= count[len];
626626+ if (left < 0) return -1; /* over-subscribed */
627627+ }
628628+ if (left > 0 && (type == CODES || max != 1))
629629+ return -1; /* incomplete set */
630630+631631+ /* generate offsets into symbol table for each length for sorting */
632632+ offs[1] = 0;
633633+ for (len = 1; len < MAXBITS; len++)
634634+ offs[len + 1] = offs[len] + count[len];
635635+636636+ /* sort symbols by length, by symbol order within each length */
637637+ for (sym = 0; sym < codes; sym++)
638638+ if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
639639+640640+ /*
641641+ Create and fill in decoding tables. In this loop, the table being
642642+ filled is at next and has curr index bits. The code being used is huff
643643+ with length len. That code is converted to an index by dropping drop
644644+ bits off of the bottom. For codes where len is less than drop + curr,
645645+ those top drop + curr - len bits are incremented through all values to
646646+ fill the table with replicated entries.
647647+648648+ root is the number of index bits for the root table. When len exceeds
649649+ root, sub-tables are created pointed to by the root entry with an index
650650+ of the low root bits of huff. This is saved in low to check for when a
651651+ new sub-table should be started. drop is zero when the root table is
652652+ being filled, and drop is root when sub-tables are being filled.
653653+654654+ When a new sub-table is needed, it is necessary to look ahead in the
655655+ code lengths to determine what size sub-table is needed. The length
656656+ counts are used for this, and so count[] is decremented as codes are
657657+ entered in the tables.
658658+659659+ used keeps track of how many table entries have been allocated from the
660660+ provided *table space. It is checked for LENS and DIST tables against
661661+ the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
662662+ the initial root table size constants. See the comments in inftrees.h
663663+ for more information.
664664+665665+ sym increments through all symbols, and the loop terminates when
666666+ all codes of length max, i.e. all codes, have been processed. This
667667+ routine permits incomplete codes, so another loop after this one fills
668668+ in the rest of the decoding tables with invalid code markers.
669669+ */
670670+671671+ /* set up for code type */
672672+ switch (type) {
673673+ case CODES:
674674+ base = extra = work; /* dummy value--not used */
675675+ match = 20;
676676+ break;
677677+ case LENS:
678678+ base = lbase;
679679+ extra = lext;
680680+ match = 257;
681681+ break;
682682+ default: /* DISTS */
683683+ base = dbase;
684684+ extra = dext;
685685+ match = 0;
686686+ }
687687+688688+ /* initialize state for loop */
689689+ huff = 0; /* starting code */
690690+ sym = 0; /* starting code symbol */
691691+ len = min; /* starting code length */
692692+ next = *table; /* current table to fill in */
693693+ curr = root; /* current table index bits */
694694+ drop = 0; /* current bits to drop from code for index */
695695+ low = (unsigned)(-1); /* trigger new sub-table when len > root */
696696+ used = 1U << root; /* use root table entries */
697697+ mask = used - 1; /* mask for comparing low */
698698+699699+ /* check available table space */
700700+ if ((type == LENS && used > ENOUGH_LENS) ||
701701+ (type == DISTS && used > ENOUGH_DISTS))
702702+ return 1;
703703+704704+ /* process all codes and make table entries */
705705+ for (;;) {
706706+ /* create table entry */
707707+ here.bits = (unsigned char)(len - drop);
708708+ if (work[sym] + 1U < match) {
709709+ here.op = (unsigned char)0;
710710+ here.val = work[sym];
711711+ }
712712+ else if (work[sym] >= match) {
713713+ here.op = (unsigned char)(extra[work[sym] - match]);
714714+ here.val = base[work[sym] - match];
715715+ }
716716+ else {
717717+ here.op = (unsigned char)(32 + 64); /* end of block */
718718+ here.val = 0;
719719+ }
720720+721721+ /* replicate for those indices with low len bits equal to huff */
722722+ incr = 1U << (len - drop);
723723+ fill = 1U << curr;
724724+ min = fill; /* save offset to next table */
725725+ do {
726726+ fill -= incr;
727727+ next[(huff >> drop) + fill] = here;
728728+ } while (fill != 0);
729729+730730+ /* backwards increment the len-bit code huff */
731731+ incr = 1U << (len - 1);
732732+ while (huff & incr)
733733+ incr >>= 1;
734734+ if (incr != 0) {
735735+ huff &= incr - 1;
736736+ huff += incr;
737737+ }
738738+ else
739739+ huff = 0;
740740+741741+ /* go to next symbol, update count, len */
742742+ sym++;
743743+ if (--(count[len]) == 0) {
744744+ if (len == max) break;
745745+ len = lens[work[sym]];
746746+ }
747747+748748+ /* create new sub-table if needed */
749749+ if (len > root && (huff & mask) != low) {
750750+ /* if first time, transition to sub-tables */
751751+ if (drop == 0)
752752+ drop = root;
753753+754754+ /* increment past last table */
755755+ next += min; /* here min is 1 << curr */
756756+757757+ /* determine length of next table */
758758+ curr = len - drop;
759759+ left = (int)(1 << curr);
760760+ while (curr + drop < max) {
761761+ left -= count[curr + drop];
762762+ if (left <= 0) break;
763763+ curr++;
764764+ left <<= 1;
765765+ }
766766+767767+ /* check for enough space */
768768+ used += 1U << curr;
769769+ if ((type == LENS && used > ENOUGH_LENS) ||
770770+ (type == DISTS && used > ENOUGH_DISTS))
771771+ return 1;
772772+773773+ /* point entry in root table to sub-table */
774774+ low = huff & mask;
775775+ (*table)[low].op = (unsigned char)curr;
776776+ (*table)[low].bits = (unsigned char)root;
777777+ (*table)[low].val = (unsigned short)(next - *table);
778778+ }
779779+ }
780780+781781+ /* fill in remaining table entry if code is incomplete (guaranteed to have
782782+ at most one remaining entry, since if the code is incomplete, the
783783+ maximum code length that was allowed to get this far is one bit) */
784784+ if (huff != 0) {
785785+ here.op = (unsigned char)64; /* invalid code marker */
786786+ here.bits = (unsigned char)(len - drop);
787787+ here.val = (unsigned short)0;
788788+ next[huff] = here;
789789+ }
790790+791791+ /* set return parameters */
792792+ *table += used;
793793+ *bits = root;
794794+ return 0;
795795+}
796796+797797+static int inflateStateCheck( z_streamp strm )
798798+{
799799+ struct inflate_state FAR *state;
800800+ if (strm == Z_NULL ||
801801+ strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
802802+ return 1;
803803+ state = (struct inflate_state FAR *)strm->state;
804804+ if (state == Z_NULL || state->strm != strm ||
805805+ state->mode < HEAD || state->mode > SYNC)
806806+ return 1;
807807+ return 0;
808808+}
809809+810810+static int inflateResetKeep( z_streamp strm )
811811+{
812812+ struct inflate_state FAR *state;
813813+814814+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
815815+ state = (struct inflate_state FAR *)strm->state;
816816+ strm->total_in = strm->total_out = state->total = 0;
817817+ strm->msg = Z_NULL;
818818+ if (state->wrap) /* to support ill-conceived Java test suite */
819819+ strm->adler = state->wrap & 1;
820820+ state->mode = HEAD;
821821+ state->last = 0;
822822+ state->havedict = 0;
823823+ state->dmax = 32768U;
824824+ state->head = Z_NULL;
825825+ state->hold = 0;
826826+ state->bits = 0;
827827+ state->lencode = state->distcode = state->next = state->codes;
828828+ state->sane = 1;
829829+ state->back = -1;
830830+ Tracev((stderr, "inflate: reset\n"));
831831+ return Z_OK;
832832+}
833833+834834+static int inflateReset( z_streamp strm )
835835+{
836836+ struct inflate_state FAR *state;
837837+838838+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
839839+ state = (struct inflate_state FAR *)strm->state;
840840+ state->wsize = 0;
841841+ state->whave = 0;
842842+ state->wnext = 0;
843843+ return inflateResetKeep(strm);
844844+}
845845+846846+static int inflateReset2( z_streamp strm, int windowBits )
847847+{
848848+ int wrap;
849849+ struct inflate_state FAR *state;
850850+851851+ /* get the state */
852852+ if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
853853+ state = (struct inflate_state FAR *)strm->state;
854854+855855+ /* extract wrap request from windowBits parameter */
856856+ if (windowBits < 0) {
857857+ wrap = 0;
858858+ windowBits = -windowBits;
859859+ }
860860+ else {
861861+ wrap = (windowBits >> 4) + 5;
862862+#ifdef GUNZIP
863863+ if (windowBits < 48)
864864+ windowBits &= 15;
865865+#endif
866866+ }
867867+868868+ /* set number of window bits, free window if different */
869869+ if (windowBits && (windowBits < 8 || windowBits > 15))
870870+ return Z_STREAM_ERROR;
871871+ if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
872872+ ZFREE(strm, state->window);
873873+ state->window = Z_NULL;
874874+ }
875875+876876+ /* update state and reset the rest of it */
877877+ state->wrap = wrap;
878878+ state->wbits = (unsigned)windowBits;
879879+ return inflateReset(strm);
880880+}
881881+882882+int inflateInit2( z_streamp strm, int windowBits )
883883+{
884884+ int ret;
885885+ struct inflate_state FAR *state;
886886+887887+ strm->msg = Z_NULL; /* in case we return an error */
888888+ state = (struct inflate_state FAR *)
889889+ ZALLOC(strm, 1, sizeof(struct inflate_state));
890890+ if (state == Z_NULL) return Z_MEM_ERROR;
891891+ Tracev((stderr, "inflate: allocated\n"));
892892+ strm->state = (struct internal_state FAR *)state;
893893+ state->strm = strm;
894894+ state->window = Z_NULL;
895895+ state->mode = HEAD; /* to pass state test in inflateReset2() */
896896+ ret = inflateReset2(strm, windowBits);
897897+ if (ret != Z_OK) {
898898+ ZFREE(strm, state);
899899+ strm->state = Z_NULL;
900900+ }
901901+ return ret;
902902+}
903903+904904+int inflateInit( z_streamp strm )
905905+{
906906+ return inflateInit2(strm, DEF_WBITS);
907907+}
908908+909909+/*
910910+ Return state with length and distance decoding tables and index sizes set to
911911+ fixed code decoding. Normally this returns fixed tables from inffixed.h.
912912+ If BUILDFIXED is defined, then instead this routine builds the tables the
913913+ first time it's called, and returns those tables the first time and
914914+ thereafter. This reduces the size of the code by about 2K bytes, in
915915+ exchange for a little execution time. However, BUILDFIXED should not be
916916+ used for threaded applications, since the rewriting of the tables and virgin
917917+ may not be thread-safe.
918918+ */
919919+static void fixedtables( struct inflate_state FAR *state )
920920+{
921921+ static const code lenfix[512] = {
922922+ {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48},
923923+ {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128},
924924+ {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59},
925925+ {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176},
926926+ {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20},
927927+ {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100},
928928+ {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8},
929929+ {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216},
930930+ {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76},
931931+ {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114},
932932+ {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2},
933933+ {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148},
934934+ {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42},
935935+ {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86},
936936+ {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15},
937937+ {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236},
938938+ {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62},
939939+ {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142},
940940+ {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31},
941941+ {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162},
942942+ {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25},
943943+ {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105},
944944+ {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4},
945945+ {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202},
946946+ {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69},
947947+ {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125},
948948+ {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13},
949949+ {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195},
950950+ {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35},
951951+ {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91},
952952+ {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19},
953953+ {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246},
954954+ {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55},
955955+ {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135},
956956+ {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99},
957957+ {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190},
958958+ {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16},
959959+ {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96},
960960+ {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6},
961961+ {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209},
962962+ {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72},
963963+ {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116},
964964+ {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4},
965965+ {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153},
966966+ {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44},
967967+ {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82},
968968+ {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11},
969969+ {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229},
970970+ {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58},
971971+ {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138},
972972+ {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51},
973973+ {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173},
974974+ {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30},
975975+ {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110},
976976+ {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0},
977977+ {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195},
978978+ {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65},
979979+ {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121},
980980+ {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9},
981981+ {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258},
982982+ {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37},
983983+ {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93},
984984+ {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23},
985985+ {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251},
986986+ {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51},
987987+ {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131},
988988+ {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67},
989989+ {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183},
990990+ {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23},
991991+ {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103},
992992+ {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9},
993993+ {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223},
994994+ {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79},
995995+ {0,9,255}
996996+ };
997997+998998+ static const code distfix[32] = {
999999+ {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025},
10001000+ {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193},
10011001+ {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385},
10021002+ {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577},
10031003+ {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073},
10041004+ {22,5,193},{64,5,0}
10051005+ };
10061006+10071007+ state->lencode = lenfix;
10081008+ state->lenbits = 9;
10091009+ state->distcode = distfix;
10101010+ state->distbits = 5;
10111011+}
10121012+10131013+/*
10141014+ Update the window with the last wsize (normally 32K) bytes written before
10151015+ returning. If window does not exist yet, create it. This is only called
10161016+ when a window is already in use, or when output has been written during this
10171017+ inflate call, but the end of the deflate stream has not been reached yet.
10181018+ It is also called to create a window for dictionary data when a dictionary
10191019+ is loaded.
10201020+10211021+ Providing output buffers larger than 32K to inflate() should provide a speed
10221022+ advantage, since only the last 32K of output is copied to the sliding window
10231023+ upon return from inflate(), and since all distances after the first 32K of
10241024+ output will fall in the output data, making match copies simpler and faster.
10251025+ The advantage may be dependent on the size of the processor's data caches.
10261026+ */
10271027+static int updatewindow( z_streamp strm, const Bytef *end, unsigned copy )
10281028+{
10291029+ struct inflate_state FAR *state;
10301030+ unsigned dist;
10311031+10321032+ state = (struct inflate_state FAR *)strm->state;
10331033+10341034+ /* if it hasn't been done already, allocate space for the window */
10351035+ if (state->window == Z_NULL) {
10361036+ state->window = (unsigned char FAR *)
10371037+ ZALLOC(strm, 1U << state->wbits,
10381038+ sizeof(unsigned char));
10391039+ if (state->window == Z_NULL) return 1;
10401040+ }
10411041+10421042+ /* if window not in use yet, initialize */
10431043+ if (state->wsize == 0) {
10441044+ state->wsize = 1U << state->wbits;
10451045+ state->wnext = 0;
10461046+ state->whave = 0;
10471047+ }
10481048+10491049+ /* copy state->wsize or less output bytes into the circular window */
10501050+ if (copy >= state->wsize) {
10511051+ zmemcpy(state->window, end - state->wsize, state->wsize);
10521052+ state->wnext = 0;
10531053+ state->whave = state->wsize;
10541054+ }
10551055+ else {
10561056+ dist = state->wsize - state->wnext;
10571057+ if (dist > copy) dist = copy;
10581058+ zmemcpy(state->window + state->wnext, end - copy, dist);
10591059+ copy -= dist;
10601060+ if (copy) {
10611061+ zmemcpy(state->window, end - copy, copy);
10621062+ state->wnext = copy;
10631063+ state->whave = state->wsize;
10641064+ }
10651065+ else {
10661066+ state->wnext += dist;
10671067+ if (state->wnext == state->wsize) state->wnext = 0;
10681068+ if (state->whave < state->wsize) state->whave += dist;
10691069+ }
10701070+ }
10711071+ return 0;
10721072+}
10731073+10741074+/* Macros for inflate(): */
10751075+10761076+#define crc32(crc,buf,len) RtlComputeCrc32(crc,buf,len)
10771077+10781078+/* check function to use adler32() for zlib or crc32() for gzip */
10791079+#ifdef GUNZIP
10801080+# define UPDATE(check, buf, len) \
10811081+ (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
10821082+#else
10831083+# define UPDATE(check, buf, len) adler32(check, buf, len)
10841084+#endif
10851085+10861086+/* check macros for header crc */
10871087+#ifdef GUNZIP
10881088+# define CRC2(check, word) \
10891089+ do { \
10901090+ hbuf[0] = (unsigned char)(word); \
10911091+ hbuf[1] = (unsigned char)((word) >> 8); \
10921092+ check = crc32(check, hbuf, 2); \
10931093+ } while (0)
10941094+10951095+# define CRC4(check, word) \
10961096+ do { \
10971097+ hbuf[0] = (unsigned char)(word); \
10981098+ hbuf[1] = (unsigned char)((word) >> 8); \
10991099+ hbuf[2] = (unsigned char)((word) >> 16); \
11001100+ hbuf[3] = (unsigned char)((word) >> 24); \
11011101+ check = crc32(check, hbuf, 4); \
11021102+ } while (0)
11031103+#endif
11041104+11051105+/* Load registers with state in inflate() for speed */
11061106+#define LOAD() \
11071107+ do { \
11081108+ put = strm->next_out; \
11091109+ left = strm->avail_out; \
11101110+ next = strm->next_in; \
11111111+ have = strm->avail_in; \
11121112+ hold = state->hold; \
11131113+ bits = state->bits; \
11141114+ } while (0)
11151115+11161116+/* Restore state from registers in inflate() */
11171117+#define RESTORE() \
11181118+ do { \
11191119+ strm->next_out = put; \
11201120+ strm->avail_out = left; \
11211121+ strm->next_in = next; \
11221122+ strm->avail_in = have; \
11231123+ state->hold = hold; \
11241124+ state->bits = bits; \
11251125+ } while (0)
11261126+11271127+/* Clear the input bit accumulator */
11281128+#define INITBITS() \
11291129+ do { \
11301130+ hold = 0; \
11311131+ bits = 0; \
11321132+ } while (0)
11331133+11341134+/* Get a byte of input into the bit accumulator, or return from inflate()
11351135+ if there is no input available. */
11361136+#define PULLBYTE() \
11371137+ do { \
11381138+ if (have == 0) goto inf_leave; \
11391139+ have--; \
11401140+ hold += (unsigned long)(*next++) << bits; \
11411141+ bits += 8; \
11421142+ } while (0)
11431143+11441144+/* Assure that there are at least n bits in the bit accumulator. If there is
11451145+ not enough available input to do that, then return from inflate(). */
11461146+#define NEEDBITS(n) \
11471147+ do { \
11481148+ while (bits < (unsigned)(n)) \
11491149+ PULLBYTE(); \
11501150+ } while (0)
11511151+11521152+/* Return the low n bits of the bit accumulator (n < 16) */
11531153+#define BITS(n) \
11541154+ ((unsigned)hold & ((1U << (n)) - 1))
11551155+11561156+/* Remove n bits from the bit accumulator */
11571157+#define DROPBITS(n) \
11581158+ do { \
11591159+ hold >>= (n); \
11601160+ bits -= (unsigned)(n); \
11611161+ } while (0)
11621162+11631163+/* Remove zero to seven bits as needed to go to a byte boundary */
11641164+#define BYTEBITS() \
11651165+ do { \
11661166+ hold >>= bits & 7; \
11671167+ bits -= bits & 7; \
11681168+ } while (0)
11691169+11701170+/*
11711171+ inflate() uses a state machine to process as much input data and generate as
11721172+ much output data as possible before returning. The state machine is
11731173+ structured roughly as follows:
11741174+11751175+ for (;;) switch (state) {
11761176+ ...
11771177+ case STATEn:
11781178+ if (not enough input data or output space to make progress)
11791179+ return;
11801180+ ... make progress ...
11811181+ state = STATEm;
11821182+ break;
11831183+ ...
11841184+ }
11851185+11861186+ so when inflate() is called again, the same case is attempted again, and
11871187+ if the appropriate resources are provided, the machine proceeds to the
11881188+ next state. The NEEDBITS() macro is usually the way the state evaluates
11891189+ whether it can proceed or should return. NEEDBITS() does the return if
11901190+ the requested bits are not available. The typical use of the BITS macros
11911191+ is:
11921192+11931193+ NEEDBITS(n);
11941194+ ... do something with BITS(n) ...
11951195+ DROPBITS(n);
11961196+11971197+ where NEEDBITS(n) either returns from inflate() if there isn't enough
11981198+ input left to load n bits into the accumulator, or it continues. BITS(n)
11991199+ gives the low n bits in the accumulator. When done, DROPBITS(n) drops
12001200+ the low n bits off the accumulator. INITBITS() clears the accumulator
12011201+ and sets the number of available bits to zero. BYTEBITS() discards just
12021202+ enough bits to put the accumulator on a byte boundary. After BYTEBITS()
12031203+ and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
12041204+12051205+ NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
12061206+ if there is no input available. The decoding of variable length codes uses
12071207+ PULLBYTE() directly in order to pull just enough bytes to decode the next
12081208+ code, and no more.
12091209+12101210+ Some states loop until they get enough input, making sure that enough
12111211+ state information is maintained to continue the loop where it left off
12121212+ if NEEDBITS() returns in the loop. For example, want, need, and keep
12131213+ would all have to actually be part of the saved state in case NEEDBITS()
12141214+ returns:
12151215+12161216+ case STATEw:
12171217+ while (want < need) {
12181218+ NEEDBITS(n);
12191219+ keep[want++] = BITS(n);
12201220+ DROPBITS(n);
12211221+ }
12221222+ state = STATEx;
12231223+ case STATEx:
12241224+12251225+ As shown above, if the next state is also the next case, then the break
12261226+ is omitted.
12271227+12281228+ A state may also return if there is not enough output space available to
12291229+ complete that state. Those states are copying stored data, writing a
12301230+ literal byte, and copying a matching string.
12311231+12321232+ When returning, a "goto inf_leave" is used to update the total counters,
12331233+ update the check value, and determine whether any progress has been made
12341234+ during that inflate() call in order to return the proper return code.
12351235+ Progress is defined as a change in either strm->avail_in or strm->avail_out.
12361236+ When there is a window, goto inf_leave will update the window with the last
12371237+ output written. If a goto inf_leave occurs in the middle of decompression
12381238+ and there is no window currently, goto inf_leave will create one and copy
12391239+ output to the window for the next call of inflate().
12401240+12411241+ In this implementation, the flush parameter of inflate() only affects the
12421242+ return code (per zlib.h). inflate() always writes as much as possible to
12431243+ strm->next_out, given the space available and the provided input--the effect
12441244+ documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
12451245+ the allocation of and copying into a sliding window until necessary, which
12461246+ provides the effect documented in zlib.h for Z_FINISH when the entire input
12471247+ stream available. So the only thing the flush parameter actually does is:
12481248+ when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
12491249+ will return Z_BUF_ERROR if it has not reached the end of the stream.
12501250+ */
12511251+12521252+int inflate( z_streamp strm, int flush )
12531253+{
12541254+ struct inflate_state FAR *state;
12551255+ z_const unsigned char FAR *next; /* next input */
12561256+ unsigned char FAR *put; /* next output */
12571257+ unsigned have, left; /* available input and output */
12581258+ unsigned long hold; /* bit buffer */
12591259+ unsigned bits; /* bits in bit buffer */
12601260+ unsigned in, out; /* save starting available input and output */
12611261+ unsigned copy; /* number of stored or match bytes to copy */
12621262+ unsigned char FAR *from; /* where to copy match bytes from */
12631263+ code here; /* current decoding table entry */
12641264+ code last; /* parent table entry */
12651265+ unsigned len; /* length to copy for repeats, bits to drop */
12661266+ int ret; /* return code */
12671267+#ifdef GUNZIP
12681268+ unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
12691269+#endif
12701270+ static const unsigned short order[19] = /* permutation of code lengths */
12711271+ {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
12721272+12731273+ if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||
12741274+ (strm->next_in == Z_NULL && strm->avail_in != 0))
12751275+ return Z_STREAM_ERROR;
12761276+12771277+ state = (struct inflate_state FAR *)strm->state;
12781278+ if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
12791279+ LOAD();
12801280+ in = have;
12811281+ out = left;
12821282+ ret = Z_OK;
12831283+ for (;;)
12841284+ switch (state->mode) {
12851285+ case HEAD:
12861286+ if (state->wrap == 0) {
12871287+ state->mode = TYPEDO;
12881288+ break;
12891289+ }
12901290+ NEEDBITS(16);
12911291+#ifdef GUNZIP
12921292+ if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
12931293+ if (state->wbits == 0)
12941294+ state->wbits = 15;
12951295+ state->check = crc32(0L, Z_NULL, 0);
12961296+ CRC2(state->check, hold);
12971297+ INITBITS();
12981298+ state->mode = FLAGS;
12991299+ break;
13001300+ }
13011301+ state->flags = 0; /* expect zlib header */
13021302+ if (state->head != Z_NULL)
13031303+ state->head->done = -1;
13041304+ if (!(state->wrap & 1) || /* check if zlib header allowed */
13051305+#else
13061306+ if (
13071307+#endif
13081308+ ((BITS(8) << 8) + (hold >> 8)) % 31) {
13091309+ strm->msg = (char *)"incorrect header check";
13101310+ state->mode = BAD;
13111311+ break;
13121312+ }
13131313+ if (BITS(4) != Z_DEFLATED) {
13141314+ strm->msg = (char *)"unknown compression method";
13151315+ state->mode = BAD;
13161316+ break;
13171317+ }
13181318+ DROPBITS(4);
13191319+ len = BITS(4) + 8;
13201320+ if (state->wbits == 0)
13211321+ state->wbits = len;
13221322+ if (len > 15 || len > state->wbits) {
13231323+ strm->msg = (char *)"invalid window size";
13241324+ state->mode = BAD;
13251325+ break;
13261326+ }
13271327+ state->dmax = 1U << len;
13281328+ Tracev((stderr, "inflate: zlib header ok\n"));
13291329+ strm->adler = state->check = adler32(0L, Z_NULL, 0);
13301330+ state->mode = hold & 0x200 ? DICTID : TYPE;
13311331+ INITBITS();
13321332+ break;
13331333+#ifdef GUNZIP
13341334+ case FLAGS:
13351335+ NEEDBITS(16);
13361336+ state->flags = (int)(hold);
13371337+ if ((state->flags & 0xff) != Z_DEFLATED) {
13381338+ strm->msg = (char *)"unknown compression method";
13391339+ state->mode = BAD;
13401340+ break;
13411341+ }
13421342+ if (state->flags & 0xe000) {
13431343+ strm->msg = (char *)"unknown header flags set";
13441344+ state->mode = BAD;
13451345+ break;
13461346+ }
13471347+ if (state->head != Z_NULL)
13481348+ state->head->text = (int)((hold >> 8) & 1);
13491349+ if ((state->flags & 0x0200) && (state->wrap & 4))
13501350+ CRC2(state->check, hold);
13511351+ INITBITS();
13521352+ state->mode = TIME;
13531353+ case TIME:
13541354+ NEEDBITS(32);
13551355+ if (state->head != Z_NULL)
13561356+ state->head->time = hold;
13571357+ if ((state->flags & 0x0200) && (state->wrap & 4))
13581358+ CRC4(state->check, hold);
13591359+ INITBITS();
13601360+ state->mode = OS;
13611361+ case OS:
13621362+ NEEDBITS(16);
13631363+ if (state->head != Z_NULL) {
13641364+ state->head->xflags = (int)(hold & 0xff);
13651365+ state->head->os = (int)(hold >> 8);
13661366+ }
13671367+ if ((state->flags & 0x0200) && (state->wrap & 4))
13681368+ CRC2(state->check, hold);
13691369+ INITBITS();
13701370+ state->mode = EXLEN;
13711371+ case EXLEN:
13721372+ if (state->flags & 0x0400) {
13731373+ NEEDBITS(16);
13741374+ state->length = (unsigned)(hold);
13751375+ if (state->head != Z_NULL)
13761376+ state->head->extra_len = (unsigned)hold;
13771377+ if ((state->flags & 0x0200) && (state->wrap & 4))
13781378+ CRC2(state->check, hold);
13791379+ INITBITS();
13801380+ }
13811381+ else if (state->head != Z_NULL)
13821382+ state->head->extra = Z_NULL;
13831383+ state->mode = EXTRA;
13841384+ case EXTRA:
13851385+ if (state->flags & 0x0400) {
13861386+ copy = state->length;
13871387+ if (copy > have) copy = have;
13881388+ if (copy) {
13891389+ if (state->head != Z_NULL &&
13901390+ state->head->extra != Z_NULL) {
13911391+ len = state->head->extra_len - state->length;
13921392+ zmemcpy(state->head->extra + len, next,
13931393+ len + copy > state->head->extra_max ?
13941394+ state->head->extra_max - len : copy);
13951395+ }
13961396+ if ((state->flags & 0x0200) && (state->wrap & 4))
13971397+ state->check = crc32(state->check, next, copy);
13981398+ have -= copy;
13991399+ next += copy;
14001400+ state->length -= copy;
14011401+ }
14021402+ if (state->length) goto inf_leave;
14031403+ }
14041404+ state->length = 0;
14051405+ state->mode = NAME;
14061406+ case NAME:
14071407+ if (state->flags & 0x0800) {
14081408+ if (have == 0) goto inf_leave;
14091409+ copy = 0;
14101410+ do {
14111411+ len = (unsigned)(next[copy++]);
14121412+ if (state->head != Z_NULL &&
14131413+ state->head->name != Z_NULL &&
14141414+ state->length < state->head->name_max)
14151415+ state->head->name[state->length++] = (Bytef)len;
14161416+ } while (len && copy < have);
14171417+ if ((state->flags & 0x0200) && (state->wrap & 4))
14181418+ state->check = crc32(state->check, next, copy);
14191419+ have -= copy;
14201420+ next += copy;
14211421+ if (len) goto inf_leave;
14221422+ }
14231423+ else if (state->head != Z_NULL)
14241424+ state->head->name = Z_NULL;
14251425+ state->length = 0;
14261426+ state->mode = COMMENT;
14271427+ case COMMENT:
14281428+ if (state->flags & 0x1000) {
14291429+ if (have == 0) goto inf_leave;
14301430+ copy = 0;
14311431+ do {
14321432+ len = (unsigned)(next[copy++]);
14331433+ if (state->head != Z_NULL &&
14341434+ state->head->comment != Z_NULL &&
14351435+ state->length < state->head->comm_max)
14361436+ state->head->comment[state->length++] = (Bytef)len;
14371437+ } while (len && copy < have);
14381438+ if ((state->flags & 0x0200) && (state->wrap & 4))
14391439+ state->check = crc32(state->check, next, copy);
14401440+ have -= copy;
14411441+ next += copy;
14421442+ if (len) goto inf_leave;
14431443+ }
14441444+ else if (state->head != Z_NULL)
14451445+ state->head->comment = Z_NULL;
14461446+ state->mode = HCRC;
14471447+ case HCRC:
14481448+ if (state->flags & 0x0200) {
14491449+ NEEDBITS(16);
14501450+ if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
14511451+ strm->msg = (char *)"header crc mismatch";
14521452+ state->mode = BAD;
14531453+ break;
14541454+ }
14551455+ INITBITS();
14561456+ }
14571457+ if (state->head != Z_NULL) {
14581458+ state->head->hcrc = (int)((state->flags >> 9) & 1);
14591459+ state->head->done = 1;
14601460+ }
14611461+ strm->adler = state->check = crc32(0L, Z_NULL, 0);
14621462+ state->mode = TYPE;
14631463+ break;
14641464+#endif
14651465+ case DICTID:
14661466+ NEEDBITS(32);
14671467+ strm->adler = state->check = ZSWAP32(hold);
14681468+ INITBITS();
14691469+ state->mode = DICT;
14701470+ case DICT:
14711471+ if (state->havedict == 0) {
14721472+ RESTORE();
14731473+ return Z_NEED_DICT;
14741474+ }
14751475+ strm->adler = state->check = adler32(0L, Z_NULL, 0);
14761476+ state->mode = TYPE;
14771477+ case TYPE:
14781478+ if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
14791479+ case TYPEDO:
14801480+ if (state->last) {
14811481+ BYTEBITS();
14821482+ state->mode = CHECK;
14831483+ break;
14841484+ }
14851485+ NEEDBITS(3);
14861486+ state->last = BITS(1);
14871487+ DROPBITS(1);
14881488+ switch (BITS(2)) {
14891489+ case 0: /* stored block */
14901490+ Tracev((stderr, "inflate: stored block%s\n",
14911491+ state->last ? " (last)" : ""));
14921492+ state->mode = STORED;
14931493+ break;
14941494+ case 1: /* fixed block */
14951495+ fixedtables(state);
14961496+ Tracev((stderr, "inflate: fixed codes block%s\n",
14971497+ state->last ? " (last)" : ""));
14981498+ state->mode = LEN_; /* decode codes */
14991499+ if (flush == Z_TREES) {
15001500+ DROPBITS(2);
15011501+ goto inf_leave;
15021502+ }
15031503+ break;
15041504+ case 2: /* dynamic block */
15051505+ Tracev((stderr, "inflate: dynamic codes block%s\n",
15061506+ state->last ? " (last)" : ""));
15071507+ state->mode = TABLE;
15081508+ break;
15091509+ case 3:
15101510+ strm->msg = (char *)"invalid block type";
15111511+ state->mode = BAD;
15121512+ }
15131513+ DROPBITS(2);
15141514+ break;
15151515+ case STORED:
15161516+ BYTEBITS(); /* go to byte boundary */
15171517+ NEEDBITS(32);
15181518+ if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
15191519+ strm->msg = (char *)"invalid stored block lengths";
15201520+ state->mode = BAD;
15211521+ break;
15221522+ }
15231523+ state->length = (unsigned)hold & 0xffff;
15241524+ Tracev((stderr, "inflate: stored length %u\n",
15251525+ state->length));
15261526+ INITBITS();
15271527+ state->mode = COPY_;
15281528+ if (flush == Z_TREES) goto inf_leave;
15291529+ case COPY_:
15301530+ state->mode = COPY;
15311531+ case COPY:
15321532+ copy = state->length;
15331533+ if (copy) {
15341534+ if (copy > have) copy = have;
15351535+ if (copy > left) copy = left;
15361536+ if (copy == 0) goto inf_leave;
15371537+ zmemcpy(put, next, copy);
15381538+ have -= copy;
15391539+ next += copy;
15401540+ left -= copy;
15411541+ put += copy;
15421542+ state->length -= copy;
15431543+ break;
15441544+ }
15451545+ Tracev((stderr, "inflate: stored end\n"));
15461546+ state->mode = TYPE;
15471547+ break;
15481548+ case TABLE:
15491549+ NEEDBITS(14);
15501550+ state->nlen = BITS(5) + 257;
15511551+ DROPBITS(5);
15521552+ state->ndist = BITS(5) + 1;
15531553+ DROPBITS(5);
15541554+ state->ncode = BITS(4) + 4;
15551555+ DROPBITS(4);
15561556+#ifndef PKZIP_BUG_WORKAROUND
15571557+ if (state->nlen > 286 || state->ndist > 30) {
15581558+ strm->msg = (char *)"too many length or distance symbols";
15591559+ state->mode = BAD;
15601560+ break;
15611561+ }
15621562+#endif
15631563+ Tracev((stderr, "inflate: table sizes ok\n"));
15641564+ state->have = 0;
15651565+ state->mode = LENLENS;
15661566+ case LENLENS:
15671567+ while (state->have < state->ncode) {
15681568+ NEEDBITS(3);
15691569+ state->lens[order[state->have++]] = (unsigned short)BITS(3);
15701570+ DROPBITS(3);
15711571+ }
15721572+ while (state->have < 19)
15731573+ state->lens[order[state->have++]] = 0;
15741574+ state->next = state->codes;
15751575+ state->lencode = (const code FAR *)(state->next);
15761576+ state->lenbits = 7;
15771577+ ret = inflate_table(CODES, state->lens, 19, &(state->next),
15781578+ &(state->lenbits), state->work);
15791579+ if (ret) {
15801580+ strm->msg = (char *)"invalid code lengths set";
15811581+ state->mode = BAD;
15821582+ break;
15831583+ }
15841584+ Tracev((stderr, "inflate: code lengths ok\n"));
15851585+ state->have = 0;
15861586+ state->mode = CODELENS;
15871587+ case CODELENS:
15881588+ while (state->have < state->nlen + state->ndist) {
15891589+ for (;;) {
15901590+ here = state->lencode[BITS(state->lenbits)];
15911591+ if ((unsigned)(here.bits) <= bits) break;
15921592+ PULLBYTE();
15931593+ }
15941594+ if (here.val < 16) {
15951595+ DROPBITS(here.bits);
15961596+ state->lens[state->have++] = here.val;
15971597+ }
15981598+ else {
15991599+ if (here.val == 16) {
16001600+ NEEDBITS(here.bits + 2);
16011601+ DROPBITS(here.bits);
16021602+ if (state->have == 0) {
16031603+ strm->msg = (char *)"invalid bit length repeat";
16041604+ state->mode = BAD;
16051605+ break;
16061606+ }
16071607+ len = state->lens[state->have - 1];
16081608+ copy = 3 + BITS(2);
16091609+ DROPBITS(2);
16101610+ }
16111611+ else if (here.val == 17) {
16121612+ NEEDBITS(here.bits + 3);
16131613+ DROPBITS(here.bits);
16141614+ len = 0;
16151615+ copy = 3 + BITS(3);
16161616+ DROPBITS(3);
16171617+ }
16181618+ else {
16191619+ NEEDBITS(here.bits + 7);
16201620+ DROPBITS(here.bits);
16211621+ len = 0;
16221622+ copy = 11 + BITS(7);
16231623+ DROPBITS(7);
16241624+ }
16251625+ if (state->have + copy > state->nlen + state->ndist) {
16261626+ strm->msg = (char *)"invalid bit length repeat";
16271627+ state->mode = BAD;
16281628+ break;
16291629+ }
16301630+ while (copy--)
16311631+ state->lens[state->have++] = (unsigned short)len;
16321632+ }
16331633+ }
16341634+16351635+ /* handle error breaks in while */
16361636+ if (state->mode == BAD) break;
16371637+16381638+ /* check for end-of-block code (better have one) */
16391639+ if (state->lens[256] == 0) {
16401640+ strm->msg = (char *)"invalid code -- missing end-of-block";
16411641+ state->mode = BAD;
16421642+ break;
16431643+ }
16441644+16451645+ /* build code tables -- note: do not change the lenbits or distbits
16461646+ values here (9 and 6) without reading the comments in inftrees.h
16471647+ concerning the ENOUGH constants, which depend on those values */
16481648+ state->next = state->codes;
16491649+ state->lencode = (const code FAR *)(state->next);
16501650+ state->lenbits = 9;
16511651+ ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
16521652+ &(state->lenbits), state->work);
16531653+ if (ret) {
16541654+ strm->msg = (char *)"invalid literal/lengths set";
16551655+ state->mode = BAD;
16561656+ break;
16571657+ }
16581658+ state->distcode = (const code FAR *)(state->next);
16591659+ state->distbits = 6;
16601660+ ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
16611661+ &(state->next), &(state->distbits), state->work);
16621662+ if (ret) {
16631663+ strm->msg = (char *)"invalid distances set";
16641664+ state->mode = BAD;
16651665+ break;
16661666+ }
16671667+ Tracev((stderr, "inflate: codes ok\n"));
16681668+ state->mode = LEN_;
16691669+ if (flush == Z_TREES) goto inf_leave;
16701670+ case LEN_:
16711671+ state->mode = LEN;
16721672+ case LEN:
16731673+ if (have >= 6 && left >= 258) {
16741674+ RESTORE();
16751675+ inflate_fast(strm, out);
16761676+ LOAD();
16771677+ if (state->mode == TYPE)
16781678+ state->back = -1;
16791679+ break;
16801680+ }
16811681+ state->back = 0;
16821682+ for (;;) {
16831683+ here = state->lencode[BITS(state->lenbits)];
16841684+ if ((unsigned)(here.bits) <= bits) break;
16851685+ PULLBYTE();
16861686+ }
16871687+ if (here.op && (here.op & 0xf0) == 0) {
16881688+ last = here;
16891689+ for (;;) {
16901690+ here = state->lencode[last.val +
16911691+ (BITS(last.bits + last.op) >> last.bits)];
16921692+ if ((unsigned)(last.bits + here.bits) <= bits) break;
16931693+ PULLBYTE();
16941694+ }
16951695+ DROPBITS(last.bits);
16961696+ state->back += last.bits;
16971697+ }
16981698+ DROPBITS(here.bits);
16991699+ state->back += here.bits;
17001700+ state->length = (unsigned)here.val;
17011701+ if ((int)(here.op) == 0) {
17021702+ Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
17031703+ "inflate: literal '%c'\n" :
17041704+ "inflate: literal 0x%02x\n", here.val));
17051705+ state->mode = LIT;
17061706+ break;
17071707+ }
17081708+ if (here.op & 32) {
17091709+ Tracevv((stderr, "inflate: end of block\n"));
17101710+ state->back = -1;
17111711+ state->mode = TYPE;
17121712+ break;
17131713+ }
17141714+ if (here.op & 64) {
17151715+ strm->msg = (char *)"invalid literal/length code";
17161716+ state->mode = BAD;
17171717+ break;
17181718+ }
17191719+ state->extra = (unsigned)(here.op) & 15;
17201720+ state->mode = LENEXT;
17211721+ case LENEXT:
17221722+ if (state->extra) {
17231723+ NEEDBITS(state->extra);
17241724+ state->length += BITS(state->extra);
17251725+ DROPBITS(state->extra);
17261726+ state->back += state->extra;
17271727+ }
17281728+ Tracevv((stderr, "inflate: length %u\n", state->length));
17291729+ state->was = state->length;
17301730+ state->mode = DIST;
17311731+ case DIST:
17321732+ for (;;) {
17331733+ here = state->distcode[BITS(state->distbits)];
17341734+ if ((unsigned)(here.bits) <= bits) break;
17351735+ PULLBYTE();
17361736+ }
17371737+ if ((here.op & 0xf0) == 0) {
17381738+ last = here;
17391739+ for (;;) {
17401740+ here = state->distcode[last.val +
17411741+ (BITS(last.bits + last.op) >> last.bits)];
17421742+ if ((unsigned)(last.bits + here.bits) <= bits) break;
17431743+ PULLBYTE();
17441744+ }
17451745+ DROPBITS(last.bits);
17461746+ state->back += last.bits;
17471747+ }
17481748+ DROPBITS(here.bits);
17491749+ state->back += here.bits;
17501750+ if (here.op & 64) {
17511751+ strm->msg = (char *)"invalid distance code";
17521752+ state->mode = BAD;
17531753+ break;
17541754+ }
17551755+ state->offset = (unsigned)here.val;
17561756+ state->extra = (unsigned)(here.op) & 15;
17571757+ state->mode = DISTEXT;
17581758+ case DISTEXT:
17591759+ if (state->extra) {
17601760+ NEEDBITS(state->extra);
17611761+ state->offset += BITS(state->extra);
17621762+ DROPBITS(state->extra);
17631763+ state->back += state->extra;
17641764+ }
17651765+#ifdef INFLATE_STRICT
17661766+ if (state->offset > state->dmax) {
17671767+ strm->msg = (char *)"invalid distance too far back";
17681768+ state->mode = BAD;
17691769+ break;
17701770+ }
17711771+#endif
17721772+ Tracevv((stderr, "inflate: distance %u\n", state->offset));
17731773+ state->mode = MATCH;
17741774+ case MATCH:
17751775+ if (left == 0) goto inf_leave;
17761776+ copy = out - left;
17771777+ if (state->offset > copy) { /* copy from window */
17781778+ copy = state->offset - copy;
17791779+ if (copy > state->whave) {
17801780+ if (state->sane) {
17811781+ strm->msg = (char *)"invalid distance too far back";
17821782+ state->mode = BAD;
17831783+ break;
17841784+ }
17851785+#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
17861786+ Trace((stderr, "inflate.c too far\n"));
17871787+ copy -= state->whave;
17881788+ if (copy > state->length) copy = state->length;
17891789+ if (copy > left) copy = left;
17901790+ left -= copy;
17911791+ state->length -= copy;
17921792+ do {
17931793+ *put++ = 0;
17941794+ } while (--copy);
17951795+ if (state->length == 0) state->mode = LEN;
17961796+ break;
17971797+#endif
17981798+ }
17991799+ if (copy > state->wnext) {
18001800+ copy -= state->wnext;
18011801+ from = state->window + (state->wsize - copy);
18021802+ }
18031803+ else
18041804+ from = state->window + (state->wnext - copy);
18051805+ if (copy > state->length) copy = state->length;
18061806+ }
18071807+ else { /* copy from output */
18081808+ from = put - state->offset;
18091809+ copy = state->length;
18101810+ }
18111811+ if (copy > left) copy = left;
18121812+ left -= copy;
18131813+ state->length -= copy;
18141814+ do {
18151815+ *put++ = *from++;
18161816+ } while (--copy);
18171817+ if (state->length == 0) state->mode = LEN;
18181818+ break;
18191819+ case LIT:
18201820+ if (left == 0) goto inf_leave;
18211821+ *put++ = (unsigned char)(state->length);
18221822+ left--;
18231823+ state->mode = LEN;
18241824+ break;
18251825+ case CHECK:
18261826+ if (state->wrap) {
18271827+ NEEDBITS(32);
18281828+ out -= left;
18291829+ strm->total_out += out;
18301830+ state->total += out;
18311831+ if ((state->wrap & 4) && out)
18321832+ strm->adler = state->check =
18331833+ UPDATE(state->check, put - out, out);
18341834+ out = left;
18351835+ if ((state->wrap & 4) && (
18361836+#ifdef GUNZIP
18371837+ state->flags ? hold :
18381838+#endif
18391839+ ZSWAP32(hold)) != state->check) {
18401840+ strm->msg = (char *)"incorrect data check";
18411841+ state->mode = BAD;
18421842+ break;
18431843+ }
18441844+ INITBITS();
18451845+ Tracev((stderr, "inflate: check matches trailer\n"));
18461846+ }
18471847+#ifdef GUNZIP
18481848+ state->mode = LENGTH;
18491849+ case LENGTH:
18501850+ if (state->wrap && state->flags) {
18511851+ NEEDBITS(32);
18521852+ if (hold != (state->total & 0xffffffffUL)) {
18531853+ strm->msg = (char *)"incorrect length check";
18541854+ state->mode = BAD;
18551855+ break;
18561856+ }
18571857+ INITBITS();
18581858+ Tracev((stderr, "inflate: length matches trailer\n"));
18591859+ }
18601860+#endif
18611861+ state->mode = DONE;
18621862+ case DONE:
18631863+ ret = Z_STREAM_END;
18641864+ goto inf_leave;
18651865+ case BAD:
18661866+ ret = Z_DATA_ERROR;
18671867+ goto inf_leave;
18681868+ case MEM:
18691869+ return Z_MEM_ERROR;
18701870+ case SYNC:
18711871+ default:
18721872+ return Z_STREAM_ERROR;
18731873+ }
18741874+18751875+ /*
18761876+ Return from inflate(), updating the total counts and the check value.
18771877+ If there was no progress during the inflate() call, return a buffer
18781878+ error. Call updatewindow() to create and/or update the window state.
18791879+ Note: a memory error from inflate() is non-recoverable.
18801880+ */
18811881+ inf_leave:
18821882+ RESTORE();
18831883+ if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
18841884+ (state->mode < CHECK || flush != Z_FINISH)))
18851885+ if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
18861886+ state->mode = MEM;
18871887+ return Z_MEM_ERROR;
18881888+ }
18891889+ in -= strm->avail_in;
18901890+ out -= strm->avail_out;
18911891+ strm->total_in += in;
18921892+ strm->total_out += out;
18931893+ state->total += out;
18941894+ if ((state->wrap & 4) && out)
18951895+ strm->adler = state->check =
18961896+ UPDATE(state->check, strm->next_out - out, out);
18971897+ strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
18981898+ (state->mode == TYPE ? 128 : 0) +
18991899+ (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
19001900+ if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
19011901+ ret = Z_BUF_ERROR;
19021902+ return ret;
19031903+}
19041904+19051905+int inflateEnd(z_streamp strm)
19061906+{
19071907+ struct inflate_state FAR *state;
19081908+ if (inflateStateCheck(strm))
19091909+ return Z_STREAM_ERROR;
19101910+ state = (struct inflate_state FAR *)strm->state;
19111911+ if (state->window != Z_NULL) ZFREE(strm, state->window);
19121912+ ZFREE(strm, strm->state);
19131913+ strm->state = Z_NULL;
19141914+ Tracev((stderr, "inflate: end\n"));
19151915+ return Z_OK;
19161916+}
+162
dll/win32/wininet/zlib.h
···11+/* zlib.h -- interface of the 'zlib' general purpose compression library
22+ * version 1.2.11, January 15th, 2017
33+ *
44+ * Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler
55+ *
66+ * This software is provided 'as-is', without any express or implied
77+ * warranty. In no event will the authors be held liable for any damages
88+ * arising from the use of this software.
99+ *
1010+ * Permission is granted to anyone to use this software for any purpose,
1111+ * including commercial applications, and to alter it and redistribute it
1212+ * freely, subject to the following restrictions:
1313+ *
1414+ * 1. The origin of this software must not be misrepresented; you must not
1515+ * claim that you wrote the original software. If you use this software
1616+ * in a product, an acknowledgment in the product documentation would be
1717+ * appreciated but is not required.
1818+ * 2. Altered source versions must be plainly marked as such, and must not be
1919+ * misrepresented as being the original software.
2020+ * 3. This notice may not be removed or altered from any source distribution.
2121+ *
2222+ * Jean-loup Gailly Mark Adler
2323+ * jloup@gzip.org madler@alumni.caltech.edu
2424+ */
2525+2626+#ifndef ZLIB_H
2727+#define ZLIB_H
2828+2929+#include "windef.h"
3030+3131+#undef FAR
3232+#define FAR
3333+#define z_const const
3434+3535+typedef unsigned char Byte; /* 8 bits */
3636+typedef unsigned int uInt; /* 16 bits or more */
3737+typedef unsigned long uLong; /* 32 bits or more */
3838+3939+typedef Byte FAR Bytef;
4040+typedef void FAR *voidpf;
4141+4242+typedef char FAR charf;
4343+typedef int FAR intf;
4444+4545+typedef unsigned char uch;
4646+typedef uch FAR uchf;
4747+typedef unsigned short ush;
4848+typedef ush FAR ushf;
4949+typedef unsigned long ulg;
5050+5151+typedef voidpf (*alloc_func)(voidpf opaque, uInt items, uInt size);
5252+typedef void (*free_func)(voidpf opaque, voidpf address);
5353+5454+struct internal_state;
5555+5656+typedef struct z_stream_s {
5757+ z_const Bytef *next_in; /* next input byte */
5858+ uInt avail_in; /* number of bytes available at next_in */
5959+ uLong total_in; /* total number of input bytes read so far */
6060+6161+ Bytef *next_out; /* next output byte will go here */
6262+ uInt avail_out; /* remaining free space at next_out */
6363+ uLong total_out; /* total number of bytes output so far */
6464+6565+ z_const char *msg; /* last error message, NULL if no error */
6666+ struct internal_state FAR *state; /* not visible by applications */
6767+6868+ alloc_func zalloc; /* used to allocate the internal state */
6969+ free_func zfree; /* used to free the internal state */
7070+ voidpf opaque; /* private data object passed to zalloc and zfree */
7171+7272+ int data_type; /* best guess about the data type: binary or text
7373+ for deflate, or the decoding state for inflate */
7474+ uLong adler; /* Adler-32 or CRC-32 value of the uncompressed data */
7575+ uLong reserved; /* reserved for future use */
7676+} z_stream;
7777+7878+typedef z_stream FAR *z_streamp;
7979+8080+/*
8181+ gzip header information passed to and from zlib routines. See RFC 1952
8282+ for more details on the meanings of these fields.
8383+*/
8484+typedef struct gz_header_s {
8585+ int text; /* true if compressed data believed to be text */
8686+ uLong time; /* modification time */
8787+ int xflags; /* extra flags (not used when writing a gzip file) */
8888+ int os; /* operating system */
8989+ Bytef *extra; /* pointer to extra field or Z_NULL if none */
9090+ uInt extra_len; /* extra field length (valid if extra != Z_NULL) */
9191+ uInt extra_max; /* space at extra (only when reading header) */
9292+ Bytef *name; /* pointer to zero-terminated file name or Z_NULL */
9393+ uInt name_max; /* space at name (only when reading header) */
9494+ Bytef *comment; /* pointer to zero-terminated comment or Z_NULL */
9595+ uInt comm_max; /* space at comment (only when reading header) */
9696+ int hcrc; /* true if there was or will be a header crc */
9797+ int done; /* true when done reading gzip header (not used
9898+ when writing a gzip file) */
9999+} gz_header;
100100+101101+typedef gz_header FAR *gz_headerp;
102102+103103+#define Z_NO_FLUSH 0
104104+#define Z_PARTIAL_FLUSH 1
105105+#define Z_SYNC_FLUSH 2
106106+#define Z_FULL_FLUSH 3
107107+#define Z_FINISH 4
108108+#define Z_BLOCK 5
109109+#define Z_TREES 6
110110+/* Allowed flush values; see deflate() and inflate() below for details */
111111+112112+#define Z_OK 0
113113+#define Z_STREAM_END 1
114114+#define Z_NEED_DICT 2
115115+#define Z_ERRNO (-1)
116116+#define Z_STREAM_ERROR (-2)
117117+#define Z_DATA_ERROR (-3)
118118+#define Z_MEM_ERROR (-4)
119119+#define Z_BUF_ERROR (-5)
120120+#define Z_VERSION_ERROR (-6)
121121+/* Return codes for the compression/decompression functions. Negative values
122122+ * are errors, positive values are used for special but normal events.
123123+ */
124124+125125+#define Z_NO_COMPRESSION 0
126126+#define Z_BEST_SPEED 1
127127+#define Z_BEST_COMPRESSION 9
128128+#define Z_DEFAULT_COMPRESSION (-1)
129129+/* compression levels */
130130+131131+#define Z_FILTERED 1
132132+#define Z_HUFFMAN_ONLY 2
133133+#define Z_RLE 3
134134+#define Z_FIXED 4
135135+#define Z_DEFAULT_STRATEGY 0
136136+/* compression strategy; see deflateInit2() below for details */
137137+138138+#define Z_BINARY 0
139139+#define Z_TEXT 1
140140+#define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */
141141+#define Z_UNKNOWN 2
142142+/* Possible values of the data_type field for deflate() */
143143+144144+#define Z_DEFLATED 8
145145+/* The deflate compression method (the only one supported in this version) */
146146+147147+#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
148148+149149+#define MAX_WBITS 15 /* 32K LZ77 window */
150150+#define MAX_MEM_LEVEL 9
151151+152152+extern int inflateInit(z_streamp strm) DECLSPEC_HIDDEN;
153153+extern int inflateInit2(z_streamp strm, int windowBits) DECLSPEC_HIDDEN;
154154+extern int inflate(z_streamp strm, int flush) DECLSPEC_HIDDEN;
155155+extern int inflateEnd(z_streamp strm) DECLSPEC_HIDDEN;
156156+157157+extern int deflateInit(z_streamp strm, int level) DECLSPEC_HIDDEN;
158158+extern int deflateInit2(z_streamp strm, int level, int method, int windowBits, int memLevel, int strategy) DECLSPEC_HIDDEN;
159159+extern int deflate(z_streamp strm, int flush) DECLSPEC_HIDDEN;
160160+extern int deflateEnd(z_streamp strm) DECLSPEC_HIDDEN;
161161+162162+#endif /* ZLIB_H */