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