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