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