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