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