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