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