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