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