1 /* inflate.c -- zlib decompression
\r
2 * Copyright (C) 1995-2005 Mark Adler
\r
3 * For conditions of distribution and use, see copyright notice in zlib.h
\r
9 * 1.2.beta0 24 Nov 2002
\r
10 * - First version -- complete rewrite of inflate to simplify code, avoid
\r
11 * creation of window when not needed, minimize use of window when it is
\r
12 * needed, make inffast.c even faster, implement gzip decoding, and to
\r
13 * improve code readability and style over the previous zlib inflate code
\r
15 * 1.2.beta1 25 Nov 2002
\r
16 * - Use pointers for available input and output checking in inffast.c
\r
17 * - Remove input and output counters in inffast.c
\r
18 * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
\r
19 * - Remove unnecessary second byte pull from length extra in inffast.c
\r
20 * - Unroll direct copy to three copies per loop in inffast.c
\r
22 * 1.2.beta2 4 Dec 2002
\r
23 * - Change external routine names to reduce potential conflicts
\r
24 * - Correct filename to inffixed.h for fixed tables in inflate.c
\r
25 * - Make hbuf[] unsigned char to match parameter type in inflate.c
\r
26 * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
\r
27 * to avoid negation problem on Alphas (64 bit) in inflate.c
\r
29 * 1.2.beta3 22 Dec 2002
\r
30 * - Add comments on state->bits assertion in inffast.c
\r
31 * - Add comments on op field in inftrees.h
\r
32 * - Fix bug in reuse of allocated window after inflateReset()
\r
33 * - Remove bit fields--back to byte structure for speed
\r
34 * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
\r
35 * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
\r
36 * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
\r
37 * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
\r
38 * - Use local copies of stream next and avail values, as well as local bit
\r
39 * buffer and bit count in inflate()--for speed when inflate_fast() not used
\r
41 * 1.2.beta4 1 Jan 2003
\r
42 * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
\r
43 * - Move a comment on output buffer sizes from inffast.c to inflate.c
\r
44 * - Add comments in inffast.c to introduce the inflate_fast() routine
\r
45 * - Rearrange window copies in inflate_fast() for speed and simplification
\r
46 * - Unroll last copy for window match in inflate_fast()
\r
47 * - Use local copies of window variables in inflate_fast() for speed
\r
48 * - Pull out common write == 0 case for speed in inflate_fast()
\r
49 * - Make op and len in inflate_fast() unsigned for consistency
\r
50 * - Add FAR to lcode and dcode declarations in inflate_fast()
\r
51 * - Simplified bad distance check in inflate_fast()
\r
52 * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
\r
53 * source file infback.c to provide a call-back interface to inflate for
\r
54 * programs like gzip and unzip -- uses window as output buffer to avoid
\r
57 * 1.2.beta5 1 Jan 2003
\r
58 * - Improved inflateBack() interface to allow the caller to provide initial
\r
60 * - Fixed stored blocks bug in inflateBack()
\r
62 * 1.2.beta6 4 Jan 2003
\r
63 * - Added comments in inffast.c on effectiveness of POSTINC
\r
64 * - Typecasting all around to reduce compiler warnings
\r
65 * - Changed loops from while (1) or do {} while (1) to for (;;), again to
\r
66 * make compilers happy
\r
67 * - Changed type of window in inflateBackInit() to unsigned char *
\r
69 * 1.2.beta7 27 Jan 2003
\r
70 * - Changed many types to unsigned or unsigned short to avoid warnings
\r
71 * - Added inflateCopy() function
\r
74 * - Changed inflateBack() interface to provide separate opaque descriptors
\r
75 * for the in() and out() functions
\r
76 * - Changed inflateBack() argument and in_func typedef to swap the length
\r
77 * and buffer address return values for the input function
\r
78 * - Check next_in and next_out for Z_NULL on entry to inflate()
\r
80 * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
\r
84 #include "inftrees.h"
\r
85 #include "inflate.h"
\r
86 #include "inffast.h"
\r
94 /* function prototypes */
\r
95 local void fixedtables OF((struct inflate_state FAR *state));
\r
96 local int updatewindow OF((z_streamp strm, unsigned out));
\r
98 void makefixed OF((void));
\r
100 local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
\r
103 int ZEXPORT inflateReset(strm)
\r
106 struct inflate_state FAR *state;
\r
108 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
\r
109 state = (struct inflate_state FAR *)strm->state;
\r
110 strm->total_in = strm->total_out = state->total = 0;
\r
111 strm->msg = Z_NULL;
\r
112 strm->adler = 1; /* to support ill-conceived Java test suite */
\r
113 state->mode = HEAD;
\r
115 state->havedict = 0;
\r
116 state->dmax = 32768U;
\r
117 state->head = Z_NULL;
\r
123 state->lencode = state->distcode = state->next = state->codes;
\r
124 Tracev((stderr, "inflate: reset\n"));
\r
128 int ZEXPORT inflatePrime(strm, bits, value)
\r
133 struct inflate_state FAR *state;
\r
135 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
\r
136 state = (struct inflate_state FAR *)strm->state;
\r
137 if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
\r
138 value &= (1L << bits) - 1;
\r
139 state->hold += value << state->bits;
\r
140 state->bits += bits;
\r
144 int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
\r
147 const char *version;
\r
150 struct inflate_state FAR *state;
\r
152 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
\r
153 stream_size != (int)(sizeof(z_stream)))
\r
154 return Z_VERSION_ERROR;
\r
155 if (strm == Z_NULL) return Z_STREAM_ERROR;
\r
156 strm->msg = Z_NULL; /* in case we return an error */
\r
157 if (strm->zalloc == (alloc_func)0) {
\r
158 strm->zalloc = zcalloc;
\r
159 strm->opaque = (voidpf)0;
\r
161 if (strm->zfree == (free_func)0) strm->zfree = zcfree;
\r
162 state = (struct inflate_state FAR *)
\r
163 ZALLOC(strm, 1, sizeof(struct inflate_state));
\r
164 if (state == Z_NULL) return Z_MEM_ERROR;
\r
165 Tracev((stderr, "inflate: allocated\n"));
\r
166 strm->state = (struct internal_state FAR *)state;
\r
167 if (windowBits < 0) {
\r
169 windowBits = -windowBits;
\r
172 state->wrap = (windowBits >> 4) + 1;
\r
174 if (windowBits < 48) windowBits &= 15;
\r
177 if (windowBits < 8 || windowBits > 15) {
\r
178 ZFREE(strm, state);
\r
179 strm->state = Z_NULL;
\r
180 return Z_STREAM_ERROR;
\r
182 state->wbits = (unsigned)windowBits;
\r
183 state->window = Z_NULL;
\r
184 return inflateReset(strm);
\r
187 int ZEXPORT inflateInit_(strm, version, stream_size)
\r
189 const char *version;
\r
192 return inflateInit2_(strm, DEF_WBITS, version, stream_size);
\r
196 Return state with length and distance decoding tables and index sizes set to
\r
197 fixed code decoding. Normally this returns fixed tables from inffixed.h.
\r
198 If BUILDFIXED is defined, then instead this routine builds the tables the
\r
199 first time it's called, and returns those tables the first time and
\r
200 thereafter. This reduces the size of the code by about 2K bytes, in
\r
201 exchange for a little execution time. However, BUILDFIXED should not be
\r
202 used for threaded applications, since the rewriting of the tables and virgin
\r
203 may not be thread-safe.
\r
205 local void fixedtables(state)
\r
206 struct inflate_state FAR *state;
\r
209 static int virgin = 1;
\r
210 static code *lenfix, *distfix;
\r
211 static code fixed[544];
\r
213 /* build fixed huffman tables if first call (may not be thread safe) */
\r
215 unsigned sym, bits;
\r
218 /* literal/length table */
\r
220 while (sym < 144) state->lens[sym++] = 8;
\r
221 while (sym < 256) state->lens[sym++] = 9;
\r
222 while (sym < 280) state->lens[sym++] = 7;
\r
223 while (sym < 288) state->lens[sym++] = 8;
\r
227 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
\r
229 /* distance table */
\r
231 while (sym < 32) state->lens[sym++] = 5;
\r
234 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
\r
236 /* do this just once */
\r
239 #else /* !BUILDFIXED */
\r
240 # include "inffixed.h"
\r
241 #endif /* BUILDFIXED */
\r
242 state->lencode = lenfix;
\r
243 state->lenbits = 9;
\r
244 state->distcode = distfix;
\r
245 state->distbits = 5;
\r
252 Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
\r
253 defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
\r
254 those tables to stdout, which would be piped to inffixed.h. A small program
\r
255 can simply call makefixed to do this:
\r
257 void makefixed(void);
\r
265 Then that can be linked with zlib built with MAKEFIXED defined and run:
\r
271 unsigned low, size;
\r
272 struct inflate_state state;
\r
274 fixedtables(&state);
\r
275 puts(" /* inffixed.h -- table for decoding fixed codes");
\r
276 puts(" * Generated automatically by makefixed().");
\r
279 puts(" /* WARNING: this file should *not* be used by applications.");
\r
280 puts(" It is part of the implementation of this library and is");
\r
281 puts(" subject to change. Applications should only use zlib.h.");
\r
285 printf(" static const code lenfix[%u] = {", size);
\r
288 if ((low % 7) == 0) printf("\n ");
\r
289 printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits,
\r
290 state.lencode[low].val);
\r
291 if (++low == size) break;
\r
296 printf("\n static const code distfix[%u] = {", size);
\r
299 if ((low % 6) == 0) printf("\n ");
\r
300 printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
\r
301 state.distcode[low].val);
\r
302 if (++low == size) break;
\r
307 #endif /* MAKEFIXED */
\r
310 Update the window with the last wsize (normally 32K) bytes written before
\r
311 returning. If window does not exist yet, create it. This is only called
\r
312 when a window is already in use, or when output has been written during this
\r
313 inflate call, but the end of the deflate stream has not been reached yet.
\r
314 It is also called to create a window for dictionary data when a dictionary
\r
317 Providing output buffers larger than 32K to inflate() should provide a speed
\r
318 advantage, since only the last 32K of output is copied to the sliding window
\r
319 upon return from inflate(), and since all distances after the first 32K of
\r
320 output will fall in the output data, making match copies simpler and faster.
\r
321 The advantage may be dependent on the size of the processor's data caches.
\r
323 local int updatewindow(strm, out)
\r
327 struct inflate_state FAR *state;
\r
328 unsigned copy, dist;
\r
330 state = (struct inflate_state FAR *)strm->state;
\r
332 /* if it hasn't been done already, allocate space for the window */
\r
333 if (state->window == Z_NULL) {
\r
334 state->window = (unsigned char FAR *)
\r
335 ZALLOC(strm, 1U << state->wbits,
\r
336 sizeof(unsigned char));
\r
337 if (state->window == Z_NULL) return 1;
\r
340 /* if window not in use yet, initialize */
\r
341 if (state->wsize == 0) {
\r
342 state->wsize = 1U << state->wbits;
\r
347 /* copy state->wsize or less output bytes into the circular window */
\r
348 copy = out - strm->avail_out;
\r
349 if (copy >= state->wsize) {
\r
350 zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
\r
352 state->whave = state->wsize;
\r
355 dist = state->wsize - state->write;
\r
356 if (dist > copy) dist = copy;
\r
357 zmemcpy(state->window + state->write, strm->next_out - copy, dist);
\r
360 zmemcpy(state->window, strm->next_out - copy, copy);
\r
361 state->write = copy;
\r
362 state->whave = state->wsize;
\r
365 state->write += dist;
\r
366 if (state->write == state->wsize) state->write = 0;
\r
367 if (state->whave < state->wsize) state->whave += dist;
\r
373 /* Macros for inflate(): */
\r
375 /* check function to use adler32() for zlib or crc32() for gzip */
\r
377 # define UPDATE(check, buf, len) \
\r
378 (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
\r
380 # define UPDATE(check, buf, len) adler32(check, buf, len)
\r
383 /* check macros for header crc */
\r
385 # define CRC2(check, word) \
\r
387 hbuf[0] = (unsigned char)(word); \
\r
388 hbuf[1] = (unsigned char)((word) >> 8); \
\r
389 check = crc32(check, hbuf, 2); \
\r
392 # define CRC4(check, word) \
\r
394 hbuf[0] = (unsigned char)(word); \
\r
395 hbuf[1] = (unsigned char)((word) >> 8); \
\r
396 hbuf[2] = (unsigned char)((word) >> 16); \
\r
397 hbuf[3] = (unsigned char)((word) >> 24); \
\r
398 check = crc32(check, hbuf, 4); \
\r
402 /* Load registers with state in inflate() for speed */
\r
405 put = strm->next_out; \
\r
406 left = strm->avail_out; \
\r
407 next = strm->next_in; \
\r
408 have = strm->avail_in; \
\r
409 hold = state->hold; \
\r
410 bits = state->bits; \
\r
413 /* Restore state from registers in inflate() */
\r
414 #define RESTORE() \
\r
416 strm->next_out = put; \
\r
417 strm->avail_out = left; \
\r
418 strm->next_in = next; \
\r
419 strm->avail_in = have; \
\r
420 state->hold = hold; \
\r
421 state->bits = bits; \
\r
424 /* Clear the input bit accumulator */
\r
425 #define INITBITS() \
\r
431 /* Get a byte of input into the bit accumulator, or return from inflate()
\r
432 if there is no input available. */
\r
433 #define PULLBYTE() \
\r
435 if (have == 0) goto inf_leave; \
\r
437 hold += (unsigned long)(*next++) << bits; \
\r
441 /* Assure that there are at least n bits in the bit accumulator. If there is
\r
442 not enough available input to do that, then return from inflate(). */
\r
443 #define NEEDBITS(n) \
\r
445 while (bits < (unsigned)(n)) \
\r
449 /* Return the low n bits of the bit accumulator (n < 16) */
\r
451 ((unsigned)hold & ((1U << (n)) - 1))
\r
453 /* Remove n bits from the bit accumulator */
\r
454 #define DROPBITS(n) \
\r
457 bits -= (unsigned)(n); \
\r
460 /* Remove zero to seven bits as needed to go to a byte boundary */
\r
461 #define BYTEBITS() \
\r
463 hold >>= bits & 7; \
\r
464 bits -= bits & 7; \
\r
467 /* Reverse the bytes in a 32-bit value */
\r
468 #define REVERSE(q) \
\r
469 ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
\r
470 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
\r
473 inflate() uses a state machine to process as much input data and generate as
\r
474 much output data as possible before returning. The state machine is
\r
475 structured roughly as follows:
\r
477 for (;;) switch (state) {
\r
480 if (not enough input data or output space to make progress)
\r
482 ... make progress ...
\r
488 so when inflate() is called again, the same case is attempted again, and
\r
489 if the appropriate resources are provided, the machine proceeds to the
\r
490 next state. The NEEDBITS() macro is usually the way the state evaluates
\r
491 whether it can proceed or should return. NEEDBITS() does the return if
\r
492 the requested bits are not available. The typical use of the BITS macros
\r
496 ... do something with BITS(n) ...
\r
499 where NEEDBITS(n) either returns from inflate() if there isn't enough
\r
500 input left to load n bits into the accumulator, or it continues. BITS(n)
\r
501 gives the low n bits in the accumulator. When done, DROPBITS(n) drops
\r
502 the low n bits off the accumulator. INITBITS() clears the accumulator
\r
503 and sets the number of available bits to zero. BYTEBITS() discards just
\r
504 enough bits to put the accumulator on a byte boundary. After BYTEBITS()
\r
505 and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
\r
507 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
\r
508 if there is no input available. The decoding of variable length codes uses
\r
509 PULLBYTE() directly in order to pull just enough bytes to decode the next
\r
512 Some states loop until they get enough input, making sure that enough
\r
513 state information is maintained to continue the loop where it left off
\r
514 if NEEDBITS() returns in the loop. For example, want, need, and keep
\r
515 would all have to actually be part of the saved state in case NEEDBITS()
\r
519 while (want < need) {
\r
521 keep[want++] = BITS(n);
\r
527 As shown above, if the next state is also the next case, then the break
\r
530 A state may also return if there is not enough output space available to
\r
531 complete that state. Those states are copying stored data, writing a
\r
532 literal byte, and copying a matching string.
\r
534 When returning, a "goto inf_leave" is used to update the total counters,
\r
535 update the check value, and determine whether any progress has been made
\r
536 during that inflate() call in order to return the proper return code.
\r
537 Progress is defined as a change in either strm->avail_in or strm->avail_out.
\r
538 When there is a window, goto inf_leave will update the window with the last
\r
539 output written. If a goto inf_leave occurs in the middle of decompression
\r
540 and there is no window currently, goto inf_leave will create one and copy
\r
541 output to the window for the next call of inflate().
\r
543 In this implementation, the flush parameter of inflate() only affects the
\r
544 return code (per zlib.h). inflate() always writes as much as possible to
\r
545 strm->next_out, given the space available and the provided input--the effect
\r
546 documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
\r
547 the allocation of and copying into a sliding window until necessary, which
\r
548 provides the effect documented in zlib.h for Z_FINISH when the entire input
\r
549 stream available. So the only thing the flush parameter actually does is:
\r
550 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
\r
551 will return Z_BUF_ERROR if it has not reached the end of the stream.
\r
554 int ZEXPORT inflate(strm, flush)
\r
558 struct inflate_state FAR *state;
\r
559 unsigned char FAR *next; /* next input */
\r
560 unsigned char FAR *put; /* next output */
\r
561 unsigned have, left; /* available input and output */
\r
562 unsigned long hold; /* bit buffer */
\r
563 unsigned bits; /* bits in bit buffer */
\r
564 unsigned in, out; /* save starting available input and output */
\r
565 unsigned copy; /* number of stored or match bytes to copy */
\r
566 unsigned char FAR *from; /* where to copy match bytes from */
\r
567 code this; /* current decoding table entry */
\r
568 code last; /* parent table entry */
\r
569 unsigned len; /* length to copy for repeats, bits to drop */
\r
570 int ret; /* return code */
\r
572 unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
\r
574 static const unsigned short order[19] = /* permutation of code lengths */
\r
575 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
\r
577 if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
\r
578 (strm->next_in == Z_NULL && strm->avail_in != 0))
\r
579 return Z_STREAM_ERROR;
\r
581 state = (struct inflate_state FAR *)strm->state;
\r
582 if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
\r
588 switch (state->mode) {
\r
590 if (state->wrap == 0) {
\r
591 state->mode = TYPEDO;
\r
596 if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
\r
597 state->check = crc32(0L, Z_NULL, 0);
\r
598 CRC2(state->check, hold);
\r
600 state->mode = FLAGS;
\r
603 state->flags = 0; /* expect zlib header */
\r
604 if (state->head != Z_NULL)
\r
605 state->head->done = -1;
\r
606 if (!(state->wrap & 1) || /* check if zlib header allowed */
\r
610 ((BITS(8) << 8) + (hold >> 8)) % 31) {
\r
611 strm->msg = (char *)"incorrect header check";
\r
615 if (BITS(4) != Z_DEFLATED) {
\r
616 strm->msg = (char *)"unknown compression method";
\r
622 if (len > state->wbits) {
\r
623 strm->msg = (char *)"invalid window size";
\r
627 state->dmax = 1U << len;
\r
628 Tracev((stderr, "inflate: zlib header ok\n"));
\r
629 strm->adler = state->check = adler32(0L, Z_NULL, 0);
\r
630 state->mode = hold & 0x200 ? DICTID : TYPE;
\r
636 state->flags = (int)(hold);
\r
637 if ((state->flags & 0xff) != Z_DEFLATED) {
\r
638 strm->msg = (char *)"unknown compression method";
\r
642 if (state->flags & 0xe000) {
\r
643 strm->msg = (char *)"unknown header flags set";
\r
647 if (state->head != Z_NULL)
\r
648 state->head->text = (int)((hold >> 8) & 1);
\r
649 if (state->flags & 0x0200) CRC2(state->check, hold);
\r
651 state->mode = TIME;
\r
654 if (state->head != Z_NULL)
\r
655 state->head->time = hold;
\r
656 if (state->flags & 0x0200) CRC4(state->check, hold);
\r
661 if (state->head != Z_NULL) {
\r
662 state->head->xflags = (int)(hold & 0xff);
\r
663 state->head->os = (int)(hold >> 8);
\r
665 if (state->flags & 0x0200) CRC2(state->check, hold);
\r
667 state->mode = EXLEN;
\r
669 if (state->flags & 0x0400) {
\r
671 state->length = (unsigned)(hold);
\r
672 if (state->head != Z_NULL)
\r
673 state->head->extra_len = (unsigned)hold;
\r
674 if (state->flags & 0x0200) CRC2(state->check, hold);
\r
677 else if (state->head != Z_NULL)
\r
678 state->head->extra = Z_NULL;
\r
679 state->mode = EXTRA;
\r
681 if (state->flags & 0x0400) {
\r
682 copy = state->length;
\r
683 if (copy > have) copy = have;
\r
685 if (state->head != Z_NULL &&
\r
686 state->head->extra != Z_NULL) {
\r
687 len = state->head->extra_len - state->length;
\r
688 zmemcpy(state->head->extra + len, next,
\r
689 len + copy > state->head->extra_max ?
\r
690 state->head->extra_max - len : copy);
\r
692 if (state->flags & 0x0200)
\r
693 state->check = crc32(state->check, next, copy);
\r
696 state->length -= copy;
\r
698 if (state->length) goto inf_leave;
\r
701 state->mode = NAME;
\r
703 if (state->flags & 0x0800) {
\r
704 if (have == 0) goto inf_leave;
\r
707 len = (unsigned)(next[copy++]);
\r
708 if (state->head != Z_NULL &&
\r
709 state->head->name != Z_NULL &&
\r
710 state->length < state->head->name_max)
\r
711 state->head->name[state->length++] = len;
\r
712 } while (len && copy < have);
\r
713 if (state->flags & 0x0200)
\r
714 state->check = crc32(state->check, next, copy);
\r
717 if (len) goto inf_leave;
\r
719 else if (state->head != Z_NULL)
\r
720 state->head->name = Z_NULL;
\r
722 state->mode = COMMENT;
\r
724 if (state->flags & 0x1000) {
\r
725 if (have == 0) goto inf_leave;
\r
728 len = (unsigned)(next[copy++]);
\r
729 if (state->head != Z_NULL &&
\r
730 state->head->comment != Z_NULL &&
\r
731 state->length < state->head->comm_max)
\r
732 state->head->comment[state->length++] = len;
\r
733 } while (len && copy < have);
\r
734 if (state->flags & 0x0200)
\r
735 state->check = crc32(state->check, next, copy);
\r
738 if (len) goto inf_leave;
\r
740 else if (state->head != Z_NULL)
\r
741 state->head->comment = Z_NULL;
\r
742 state->mode = HCRC;
\r
744 if (state->flags & 0x0200) {
\r
746 if (hold != (state->check & 0xffff)) {
\r
747 strm->msg = (char *)"header crc mismatch";
\r
753 if (state->head != Z_NULL) {
\r
754 state->head->hcrc = (int)((state->flags >> 9) & 1);
\r
755 state->head->done = 1;
\r
757 strm->adler = state->check = crc32(0L, Z_NULL, 0);
\r
758 state->mode = TYPE;
\r
763 strm->adler = state->check = REVERSE(hold);
\r
765 state->mode = DICT;
\r
767 if (state->havedict == 0) {
\r
769 return Z_NEED_DICT;
\r
771 strm->adler = state->check = adler32(0L, Z_NULL, 0);
\r
772 state->mode = TYPE;
\r
774 if (flush == Z_BLOCK) goto inf_leave;
\r
778 state->mode = CHECK;
\r
782 state->last = BITS(1);
\r
785 case 0: /* stored block */
\r
786 Tracev((stderr, "inflate: stored block%s\n",
\r
787 state->last ? " (last)" : ""));
\r
788 state->mode = STORED;
\r
790 case 1: /* fixed block */
\r
791 fixedtables(state);
\r
792 Tracev((stderr, "inflate: fixed codes block%s\n",
\r
793 state->last ? " (last)" : ""));
\r
794 state->mode = LEN; /* decode codes */
\r
796 case 2: /* dynamic block */
\r
797 Tracev((stderr, "inflate: dynamic codes block%s\n",
\r
798 state->last ? " (last)" : ""));
\r
799 state->mode = TABLE;
\r
802 strm->msg = (char *)"invalid block type";
\r
808 BYTEBITS(); /* go to byte boundary */
\r
810 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
\r
811 strm->msg = (char *)"invalid stored block lengths";
\r
815 state->length = (unsigned)hold & 0xffff;
\r
816 Tracev((stderr, "inflate: stored length %u\n",
\r
819 state->mode = COPY;
\r
821 copy = state->length;
\r
823 if (copy > have) copy = have;
\r
824 if (copy > left) copy = left;
\r
825 if (copy == 0) goto inf_leave;
\r
826 zmemcpy(put, next, copy);
\r
831 state->length -= copy;
\r
834 Tracev((stderr, "inflate: stored end\n"));
\r
835 state->mode = TYPE;
\r
839 state->nlen = BITS(5) + 257;
\r
841 state->ndist = BITS(5) + 1;
\r
843 state->ncode = BITS(4) + 4;
\r
845 #ifndef PKZIP_BUG_WORKAROUND
\r
846 if (state->nlen > 286 || state->ndist > 30) {
\r
847 strm->msg = (char *)"too many length or distance symbols";
\r
852 Tracev((stderr, "inflate: table sizes ok\n"));
\r
854 state->mode = LENLENS;
\r
856 while (state->have < state->ncode) {
\r
858 state->lens[order[state->have++]] = (unsigned short)BITS(3);
\r
861 while (state->have < 19)
\r
862 state->lens[order[state->have++]] = 0;
\r
863 state->next = state->codes;
\r
864 state->lencode = (code const FAR *)(state->next);
\r
865 state->lenbits = 7;
\r
866 ret = inflate_table(CODES, state->lens, 19, &(state->next),
\r
867 &(state->lenbits), state->work);
\r
869 strm->msg = (char *)"invalid code lengths set";
\r
873 Tracev((stderr, "inflate: code lengths ok\n"));
\r
875 state->mode = CODELENS;
\r
877 while (state->have < state->nlen + state->ndist) {
\r
879 this = state->lencode[BITS(state->lenbits)];
\r
880 if ((unsigned)(this.bits) <= bits) break;
\r
883 if (this.val < 16) {
\r
884 NEEDBITS(this.bits);
\r
885 DROPBITS(this.bits);
\r
886 state->lens[state->have++] = this.val;
\r
889 if (this.val == 16) {
\r
890 NEEDBITS(this.bits + 2);
\r
891 DROPBITS(this.bits);
\r
892 if (state->have == 0) {
\r
893 strm->msg = (char *)"invalid bit length repeat";
\r
897 len = state->lens[state->have - 1];
\r
898 copy = 3 + BITS(2);
\r
901 else if (this.val == 17) {
\r
902 NEEDBITS(this.bits + 3);
\r
903 DROPBITS(this.bits);
\r
905 copy = 3 + BITS(3);
\r
909 NEEDBITS(this.bits + 7);
\r
910 DROPBITS(this.bits);
\r
912 copy = 11 + BITS(7);
\r
915 if (state->have + copy > state->nlen + state->ndist) {
\r
916 strm->msg = (char *)"invalid bit length repeat";
\r
921 state->lens[state->have++] = (unsigned short)len;
\r
925 /* handle error breaks in while */
\r
926 if (state->mode == BAD) break;
\r
928 /* build code tables */
\r
929 state->next = state->codes;
\r
930 state->lencode = (code const FAR *)(state->next);
\r
931 state->lenbits = 9;
\r
932 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
\r
933 &(state->lenbits), state->work);
\r
935 strm->msg = (char *)"invalid literal/lengths set";
\r
939 state->distcode = (code const FAR *)(state->next);
\r
940 state->distbits = 6;
\r
941 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
\r
942 &(state->next), &(state->distbits), state->work);
\r
944 strm->msg = (char *)"invalid distances set";
\r
948 Tracev((stderr, "inflate: codes ok\n"));
\r
951 if (have >= 6 && left >= 258) {
\r
953 inflate_fast(strm, out);
\r
958 this = state->lencode[BITS(state->lenbits)];
\r
959 if ((unsigned)(this.bits) <= bits) break;
\r
962 if (this.op && (this.op & 0xf0) == 0) {
\r
965 this = state->lencode[last.val +
\r
966 (BITS(last.bits + last.op) >> last.bits)];
\r
967 if ((unsigned)(last.bits + this.bits) <= bits) break;
\r
970 DROPBITS(last.bits);
\r
972 DROPBITS(this.bits);
\r
973 state->length = (unsigned)this.val;
\r
974 if ((int)(this.op) == 0) {
\r
975 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
\r
976 "inflate: literal '%c'\n" :
\r
977 "inflate: literal 0x%02x\n", this.val));
\r
981 if (this.op & 32) {
\r
982 Tracevv((stderr, "inflate: end of block\n"));
\r
983 state->mode = TYPE;
\r
986 if (this.op & 64) {
\r
987 strm->msg = (char *)"invalid literal/length code";
\r
991 state->extra = (unsigned)(this.op) & 15;
\r
992 state->mode = LENEXT;
\r
994 if (state->extra) {
\r
995 NEEDBITS(state->extra);
\r
996 state->length += BITS(state->extra);
\r
997 DROPBITS(state->extra);
\r
999 Tracevv((stderr, "inflate: length %u\n", state->length));
\r
1000 state->mode = DIST;
\r
1003 this = state->distcode[BITS(state->distbits)];
\r
1004 if ((unsigned)(this.bits) <= bits) break;
\r
1007 if ((this.op & 0xf0) == 0) {
\r
1010 this = state->distcode[last.val +
\r
1011 (BITS(last.bits + last.op) >> last.bits)];
\r
1012 if ((unsigned)(last.bits + this.bits) <= bits) break;
\r
1015 DROPBITS(last.bits);
\r
1017 DROPBITS(this.bits);
\r
1018 if (this.op & 64) {
\r
1019 strm->msg = (char *)"invalid distance code";
\r
1020 state->mode = BAD;
\r
1023 state->offset = (unsigned)this.val;
\r
1024 state->extra = (unsigned)(this.op) & 15;
\r
1025 state->mode = DISTEXT;
\r
1027 if (state->extra) {
\r
1028 NEEDBITS(state->extra);
\r
1029 state->offset += BITS(state->extra);
\r
1030 DROPBITS(state->extra);
\r
1032 #ifdef INFLATE_STRICT
\r
1033 if (state->offset > state->dmax) {
\r
1034 strm->msg = (char *)"invalid distance too far back";
\r
1035 state->mode = BAD;
\r
1039 if (state->offset > state->whave + out - left) {
\r
1040 strm->msg = (char *)"invalid distance too far back";
\r
1041 state->mode = BAD;
\r
1044 Tracevv((stderr, "inflate: distance %u\n", state->offset));
\r
1045 state->mode = MATCH;
\r
1047 if (left == 0) goto inf_leave;
\r
1048 copy = out - left;
\r
1049 if (state->offset > copy) { /* copy from window */
\r
1050 copy = state->offset - copy;
\r
1051 if (copy > state->write) {
\r
1052 copy -= state->write;
\r
1053 from = state->window + (state->wsize - copy);
\r
1056 from = state->window + (state->write - copy);
\r
1057 if (copy > state->length) copy = state->length;
\r
1059 else { /* copy from output */
\r
1060 from = put - state->offset;
\r
1061 copy = state->length;
\r
1063 if (copy > left) copy = left;
\r
1065 state->length -= copy;
\r
1069 if (state->length == 0) state->mode = LEN;
\r
1072 if (left == 0) goto inf_leave;
\r
1073 *put++ = (unsigned char)(state->length);
\r
1075 state->mode = LEN;
\r
1078 if (state->wrap) {
\r
1081 strm->total_out += out;
\r
1082 state->total += out;
\r
1084 strm->adler = state->check =
\r
1085 UPDATE(state->check, put - out, out);
\r
1089 state->flags ? hold :
\r
1091 REVERSE(hold)) != state->check) {
\r
1092 strm->msg = (char *)"incorrect data check";
\r
1093 state->mode = BAD;
\r
1097 Tracev((stderr, "inflate: check matches trailer\n"));
\r
1100 state->mode = LENGTH;
\r
1102 if (state->wrap && state->flags) {
\r
1104 if (hold != (state->total & 0xffffffffUL)) {
\r
1105 strm->msg = (char *)"incorrect length check";
\r
1106 state->mode = BAD;
\r
1110 Tracev((stderr, "inflate: length matches trailer\n"));
\r
1113 state->mode = DONE;
\r
1115 ret = Z_STREAM_END;
\r
1118 ret = Z_DATA_ERROR;
\r
1121 return Z_MEM_ERROR;
\r
1124 return Z_STREAM_ERROR;
\r
1128 Return from inflate(), updating the total counts and the check value.
\r
1129 If there was no progress during the inflate() call, return a buffer
\r
1130 error. Call updatewindow() to create and/or update the window state.
\r
1131 Note: a memory error from inflate() is non-recoverable.
\r
1135 if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
\r
1136 if (updatewindow(strm, out)) {
\r
1137 state->mode = MEM;
\r
1138 return Z_MEM_ERROR;
\r
1140 in -= strm->avail_in;
\r
1141 out -= strm->avail_out;
\r
1142 strm->total_in += in;
\r
1143 strm->total_out += out;
\r
1144 state->total += out;
\r
1145 if (state->wrap && out)
\r
1146 strm->adler = state->check =
\r
1147 UPDATE(state->check, strm->next_out - out, out);
\r
1148 strm->data_type = state->bits + (state->last ? 64 : 0) +
\r
1149 (state->mode == TYPE ? 128 : 0);
\r
1150 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
\r
1151 ret = Z_BUF_ERROR;
\r
1155 int ZEXPORT inflateEnd(strm)
\r
1158 struct inflate_state FAR *state;
\r
1159 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
\r
1160 return Z_STREAM_ERROR;
\r
1161 state = (struct inflate_state FAR *)strm->state;
\r
1162 if (state->window != Z_NULL) ZFREE(strm, state->window);
\r
1163 ZFREE(strm, strm->state);
\r
1164 strm->state = Z_NULL;
\r
1165 Tracev((stderr, "inflate: end\n"));
\r
1169 int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
\r
1171 const Bytef *dictionary;
\r
1174 struct inflate_state FAR *state;
\r
1178 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
\r
1179 state = (struct inflate_state FAR *)strm->state;
\r
1180 if (state->wrap != 0 && state->mode != DICT)
\r
1181 return Z_STREAM_ERROR;
\r
1183 /* check for correct dictionary id */
\r
1184 if (state->mode == DICT) {
\r
1185 id = adler32(0L, Z_NULL, 0);
\r
1186 id = adler32(id, dictionary, dictLength);
\r
1187 if (id != state->check)
\r
1188 return Z_DATA_ERROR;
\r
1191 /* copy dictionary to window */
\r
1192 if (updatewindow(strm, strm->avail_out)) {
\r
1193 state->mode = MEM;
\r
1194 return Z_MEM_ERROR;
\r
1196 if (dictLength > state->wsize) {
\r
1197 zmemcpy(state->window, dictionary + dictLength - state->wsize,
\r
1199 state->whave = state->wsize;
\r
1202 zmemcpy(state->window + state->wsize - dictLength, dictionary,
\r
1204 state->whave = dictLength;
\r
1206 state->havedict = 1;
\r
1207 Tracev((stderr, "inflate: dictionary set\n"));
\r
1211 int ZEXPORT inflateGetHeader(strm, head)
\r
1215 struct inflate_state FAR *state;
\r
1218 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
\r
1219 state = (struct inflate_state FAR *)strm->state;
\r
1220 if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
\r
1222 /* save header structure */
\r
1223 state->head = head;
\r
1229 Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
\r
1230 or when out of input. When called, *have is the number of pattern bytes
\r
1231 found in order so far, in 0..3. On return *have is updated to the new
\r
1232 state. If on return *have equals four, then the pattern was found and the
\r
1233 return value is how many bytes were read including the last byte of the
\r
1234 pattern. If *have is less than four, then the pattern has not been found
\r
1235 yet and the return value is len. In the latter case, syncsearch() can be
\r
1236 called again with more data and the *have state. *have is initialized to
\r
1237 zero for the first call.
\r
1239 local unsigned syncsearch(have, buf, len)
\r
1240 unsigned FAR *have;
\r
1241 unsigned char FAR *buf;
\r
1249 while (next < len && got < 4) {
\r
1250 if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
\r
1252 else if (buf[next])
\r
1262 int ZEXPORT inflateSync(strm)
\r
1265 unsigned len; /* number of bytes to look at or looked at */
\r
1266 unsigned long in, out; /* temporary to save total_in and total_out */
\r
1267 unsigned char buf[4]; /* to restore bit buffer to byte string */
\r
1268 struct inflate_state FAR *state;
\r
1270 /* check parameters */
\r
1271 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
\r
1272 state = (struct inflate_state FAR *)strm->state;
\r
1273 if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
\r
1275 /* if first time, start search in bit buffer */
\r
1276 if (state->mode != SYNC) {
\r
1277 state->mode = SYNC;
\r
1278 state->hold <<= state->bits & 7;
\r
1279 state->bits -= state->bits & 7;
\r
1281 while (state->bits >= 8) {
\r
1282 buf[len++] = (unsigned char)(state->hold);
\r
1283 state->hold >>= 8;
\r
1287 syncsearch(&(state->have), buf, len);
\r
1290 /* search available input */
\r
1291 len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
\r
1292 strm->avail_in -= len;
\r
1293 strm->next_in += len;
\r
1294 strm->total_in += len;
\r
1296 /* return no joy or set up to restart inflate() on a new block */
\r
1297 if (state->have != 4) return Z_DATA_ERROR;
\r
1298 in = strm->total_in; out = strm->total_out;
\r
1299 inflateReset(strm);
\r
1300 strm->total_in = in; strm->total_out = out;
\r
1301 state->mode = TYPE;
\r
1306 Returns true if inflate is currently at the end of a block generated by
\r
1307 Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
\r
1308 implementation to provide an additional safety check. PPP uses
\r
1309 Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
\r
1310 block. When decompressing, PPP checks that at the end of input packet,
\r
1311 inflate is waiting for these length bytes.
\r
1313 int ZEXPORT inflateSyncPoint(strm)
\r
1316 struct inflate_state FAR *state;
\r
1318 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
\r
1319 state = (struct inflate_state FAR *)strm->state;
\r
1320 return state->mode == STORED && state->bits == 0;
\r
1323 int ZEXPORT inflateCopy(dest, source)
\r
1327 struct inflate_state FAR *state;
\r
1328 struct inflate_state FAR *copy;
\r
1329 unsigned char FAR *window;
\r
1333 if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
\r
1334 source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
\r
1335 return Z_STREAM_ERROR;
\r
1336 state = (struct inflate_state FAR *)source->state;
\r
1338 /* allocate space */
\r
1339 copy = (struct inflate_state FAR *)
\r
1340 ZALLOC(source, 1, sizeof(struct inflate_state));
\r
1341 if (copy == Z_NULL) return Z_MEM_ERROR;
\r
1343 if (state->window != Z_NULL) {
\r
1344 window = (unsigned char FAR *)
\r
1345 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
\r
1346 if (window == Z_NULL) {
\r
1347 ZFREE(source, copy);
\r
1348 return Z_MEM_ERROR;
\r
1353 zmemcpy(dest, source, sizeof(z_stream));
\r
1354 zmemcpy(copy, state, sizeof(struct inflate_state));
\r
1355 if (state->lencode >= state->codes &&
\r
1356 state->lencode <= state->codes + ENOUGH - 1) {
\r
1357 copy->lencode = copy->codes + (state->lencode - state->codes);
\r
1358 copy->distcode = copy->codes + (state->distcode - state->codes);
\r
1360 copy->next = copy->codes + (state->next - state->codes);
\r
1361 if (window != Z_NULL) {
\r
1362 wsize = 1U << state->wbits;
\r
1363 zmemcpy(window, state->window, wsize);
\r
1365 copy->window = window;
\r
1366 dest->state = (struct internal_state FAR *)copy;
\r