Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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, unsigned out));
97 #ifdef BUILDFIXED
98  void makefixed OF((void));
99 #endif
100 local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
101  unsigned len));
102 
103 int ZEXPORT inflateResetKeep(z_streamp strm)
104 {
105  struct inflate_state FAR *state;
106 
107  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
108  state = (struct inflate_state FAR *)strm->state;
109  strm->total_in = strm->total_out = state->total = 0;
110  strm->msg = Z_NULL;
111  if (state->wrap) /* to support ill-conceived Java test suite */
112  strm->adler = state->wrap & 1;
113  state->mode = HEAD;
114  state->last = 0;
115  state->havedict = 0;
116  state->dmax = 32768U;
117  state->head = Z_NULL;
118  state->hold = 0;
119  state->bits = 0;
120  state->lencode = state->distcode = state->next = state->codes;
121  state->sane = 1;
122  state->back = -1;
123  Tracev((stderr, "inflate: reset\n"));
124  return Z_OK;
125 }
126 
127 int ZEXPORT inflateReset(z_streamp strm)
128 {
129  struct inflate_state FAR *state;
130 
131  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
132  state = (struct inflate_state FAR *)strm->state;
133  state->wsize = 0;
134  state->whave = 0;
135  state->wnext = 0;
136  return inflateResetKeep(strm);
137 }
138 
139 int ZEXPORT inflateReset2(z_streamp strm, int windowBits)
140 {
141  int wrap;
142  struct inflate_state FAR *state;
143 
144  /* get the state */
145  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
146  state = (struct inflate_state FAR *)strm->state;
147 
148  /* extract wrap request from windowBits parameter */
149  if (windowBits < 0) {
150  wrap = 0;
151  windowBits = -windowBits;
152  }
153  else {
154  wrap = (windowBits >> 4) + 1;
155 #ifdef GUNZIP
156  if (windowBits < 48)
157  windowBits &= 15;
158 #endif
159  }
160 
161  /* set number of window bits, free window if different */
162  if (windowBits && (windowBits < 8 || windowBits > 15))
163  return Z_STREAM_ERROR;
164  if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
165  ZFREE(strm, state->window);
166  state->window = Z_NULL;
167  }
168 
169  /* update state and reset the rest of it */
170  state->wrap = wrap;
171  state->wbits = (unsigned)windowBits;
172  return inflateReset(strm);
173 }
174 
175 int ZEXPORT inflateInit2_(z_streamp strm, int windowBits, const char *version, int stream_size)
176 {
177  int ret;
178  struct inflate_state FAR *state;
179 
180  if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
181  stream_size != (int)(sizeof(z_stream)))
182  return Z_VERSION_ERROR;
183  if (strm == Z_NULL) return Z_STREAM_ERROR;
184  strm->msg = Z_NULL; /* in case we return an error */
185  if (strm->zalloc == (alloc_func)0) {
186 #ifdef Z_SOLO
187  return Z_STREAM_ERROR;
188 #else
189  strm->zalloc = zcalloc;
190  strm->opaque = (voidpf)0;
191 #endif
192  }
193  if (strm->zfree == (free_func)0)
194 #ifdef Z_SOLO
195  return Z_STREAM_ERROR;
196 #else
197  strm->zfree = zcfree;
198 #endif
199  state = (struct inflate_state FAR *)
200  ZALLOC(strm, 1, sizeof(struct inflate_state));
201  if (state == Z_NULL) return Z_MEM_ERROR;
202  Tracev((stderr, "inflate: allocated\n"));
203  strm->state = (struct internal_state FAR *)state;
204  state->window = Z_NULL;
205  ret = inflateReset2(strm, windowBits);
206  if (ret != Z_OK) {
207  ZFREE(strm, state);
208  strm->state = Z_NULL;
209  }
210  return ret;
211 }
212 
213 int ZEXPORT inflateInit_(z_streamp strm, const char *version, int stream_size)
214 {
215  return inflateInit2_(strm, DEF_WBITS, version, stream_size);
216 }
217 
218 int ZEXPORT inflatePrime(z_streamp strm, int bits, int value)
219 {
220  struct inflate_state FAR *state;
221 
222  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
223  state = (struct inflate_state FAR *)strm->state;
224  if (bits < 0) {
225  state->hold = 0;
226  state->bits = 0;
227  return Z_OK;
228  }
229  if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
230  value &= (1L << bits) - 1;
231  state->hold += value << state->bits;
232  state->bits += bits;
233  return Z_OK;
234 }
235 
236 /*
237  Return state with length and distance decoding tables and index sizes set to
238  fixed code decoding. Normally this returns fixed tables from inffixed.h.
239  If BUILDFIXED is defined, then instead this routine builds the tables the
240  first time it's called, and returns those tables the first time and
241  thereafter. This reduces the size of the code by about 2K bytes, in
242  exchange for a little execution time. However, BUILDFIXED should not be
243  used for threaded applications, since the rewriting of the tables and virgin
244  may not be thread-safe.
245  */
246 local void fixedtables(struct inflate_state FAR *state)
247 {
248 #ifdef BUILDFIXED
249  static int virgin = 1;
250  static code *lenfix, *distfix;
251  static code fixed[544];
252 
253  /* build fixed huffman tables if first call (may not be thread safe) */
254  if (virgin) {
255  unsigned sym, bits;
256  static code *next;
257 
258  /* literal/length table */
259  sym = 0;
260  while (sym < 144) state->lens[sym++] = 8;
261  while (sym < 256) state->lens[sym++] = 9;
262  while (sym < 280) state->lens[sym++] = 7;
263  while (sym < 288) state->lens[sym++] = 8;
264  next = fixed;
265  lenfix = next;
266  bits = 9;
267  inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
268 
269  /* distance table */
270  sym = 0;
271  while (sym < 32) state->lens[sym++] = 5;
272  distfix = next;
273  bits = 5;
274  inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
275 
276  /* do this just once */
277  virgin = 0;
278  }
279 #else /* !BUILDFIXED */
280 # include "inffixed.h"
281 #endif /* BUILDFIXED */
282  state->lencode = lenfix;
283  state->lenbits = 9;
284  state->distcode = distfix;
285  state->distbits = 5;
286 }
287 
288 #ifdef MAKEFIXED
289 #include <stdio.h>
290 
291 /*
292  Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
293  defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
294  those tables to stdout, which would be piped to inffixed.h. A small program
295  can simply call makefixed to do this:
296 
297  void makefixed(void);
298 
299  int main(void)
300  {
301  makefixed();
302  return 0;
303  }
304 
305  Then that can be linked with zlib built with MAKEFIXED defined and run:
306 
307  a.out > inffixed.h
308  */
309 void makefixed()
310 {
311  unsigned low, size;
312  struct inflate_state state;
313 
314  fixedtables(&state);
315  puts(" /* inffixed.h -- table for decoding fixed codes");
316  puts(" * Generated automatically by makefixed().");
317  puts(" */");
318  puts("");
319  puts(" /* WARNING: this file should *not* be used by applications.");
320  puts(" It is part of the implementation of this library and is");
321  puts(" subject to change. Applications should only use zlib.h.");
322  puts(" */");
323  puts("");
324  size = 1U << 9;
325  printf(" static const code lenfix[%u] = {", size);
326  low = 0;
327  for (;;) {
328  if ((low % 7) == 0) printf("\n ");
329  printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
330  state.lencode[low].bits, state.lencode[low].val);
331  if (++low == size) break;
332  putchar(',');
333  }
334  puts("\n };");
335  size = 1U << 5;
336  printf("\n static const code distfix[%u] = {", size);
337  low = 0;
338  for (;;) {
339  if ((low % 6) == 0) printf("\n ");
340  printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
341  state.distcode[low].val);
342  if (++low == size) break;
343  putchar(',');
344  }
345  puts("\n };");
346 }
347 #endif /* MAKEFIXED */
348 
349 /*
350  Update the window with the last wsize (normally 32K) bytes written before
351  returning. If window does not exist yet, create it. This is only called
352  when a window is already in use, or when output has been written during this
353  inflate call, but the end of the deflate stream has not been reached yet.
354  It is also called to create a window for dictionary data when a dictionary
355  is loaded.
356 
357  Providing output buffers larger than 32K to inflate() should provide a speed
358  advantage, since only the last 32K of output is copied to the sliding window
359  upon return from inflate(), and since all distances after the first 32K of
360  output will fall in the output data, making match copies simpler and faster.
361  The advantage may be dependent on the size of the processor's data caches.
362  */
363 local int updatewindow(z_streamp strm, unsigned out)
364 {
365  struct inflate_state FAR *state;
366  unsigned copy, dist;
367 
368  state = (struct inflate_state FAR *)strm->state;
369 
370  /* if it hasn't been done already, allocate space for the window */
371  if (state->window == Z_NULL) {
372  state->window = (unsigned char FAR *)
373  ZALLOC(strm, 1U << state->wbits,
374  sizeof(unsigned char));
375  if (state->window == Z_NULL) return 1;
376  }
377 
378  /* if window not in use yet, initialize */
379  if (state->wsize == 0) {
380  state->wsize = 1U << state->wbits;
381  state->wnext = 0;
382  state->whave = 0;
383  }
384 
385  /* copy state->wsize or less output bytes into the circular window */
386  copy = out - strm->avail_out;
387  if (copy >= state->wsize) {
388  zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
389  state->wnext = 0;
390  state->whave = state->wsize;
391  }
392  else {
393  dist = state->wsize - state->wnext;
394  if (dist > copy) dist = copy;
395  zmemcpy(state->window + state->wnext, strm->next_out - copy, dist);
396  copy -= dist;
397  if (copy) {
398  zmemcpy(state->window, strm->next_out - copy, copy);
399  state->wnext = copy;
400  state->whave = state->wsize;
401  }
402  else {
403  state->wnext += dist;
404  if (state->wnext == state->wsize) state->wnext = 0;
405  if (state->whave < state->wsize) state->whave += dist;
406  }
407  }
408  return 0;
409 }
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  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 = (code const 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 = (code const 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 = (code const 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, 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 inflateSetDictionary(z_streamp strm, const Bytef *dictionary, uInt dictLength)
1248 {
1249  struct inflate_state FAR *state;
1250  unsigned long dictid;
1251  unsigned char *next;
1252  unsigned avail;
1253  int ret;
1254 
1255  /* check state */
1256  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1257  state = (struct inflate_state FAR *)strm->state;
1258  if (state->wrap != 0 && state->mode != DICT)
1259  return Z_STREAM_ERROR;
1260 
1261  /* check for correct dictionary identifier */
1262  if (state->mode == DICT) {
1263  dictid = adler32(0L, Z_NULL, 0);
1264  dictid = adler32(dictid, dictionary, dictLength);
1265  if (dictid != state->check)
1266  return Z_DATA_ERROR;
1267  }
1268 
1269  /* copy dictionary to window using updatewindow(), which will amend the
1270  existing dictionary if appropriate */
1271  next = strm->next_out;
1272  avail = strm->avail_out;
1273  strm->next_out = (Bytef *)dictionary + dictLength;
1274  strm->avail_out = 0;
1275  ret = updatewindow(strm, dictLength);
1276  strm->avail_out = avail;
1277  strm->next_out = next;
1278  if (ret) {
1279  state->mode = MEM;
1280  return Z_MEM_ERROR;
1281  }
1282  state->havedict = 1;
1283  Tracev((stderr, "inflate: dictionary set\n"));
1284  return Z_OK;
1285 }
1286 
1288 {
1289  struct inflate_state FAR *state;
1290 
1291  /* check state */
1292  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1293  state = (struct inflate_state FAR *)strm->state;
1294  if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
1295 
1296  /* save header structure */
1297  state->head = head;
1298  head->done = 0;
1299  return Z_OK;
1300 }
1301 
1302 /*
1303  Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
1304  or when out of input. When called, *have is the number of pattern bytes
1305  found in order so far, in 0..3. On return *have is updated to the new
1306  state. If on return *have equals four, then the pattern was found and the
1307  return value is how many bytes were read including the last byte of the
1308  pattern. If *have is less than four, then the pattern has not been found
1309  yet and the return value is len. In the latter case, syncsearch() can be
1310  called again with more data and the *have state. *have is initialized to
1311  zero for the first call.
1312  */
1313 local unsigned syncsearch(unsigned FAR *have, unsigned char FAR *buf, unsigned len)
1314 {
1315  unsigned got;
1316  unsigned next;
1317 
1318  got = *have;
1319  next = 0;
1320  while (next < len && got < 4) {
1321  if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1322  got++;
1323  else if (buf[next])
1324  got = 0;
1325  else
1326  got = 4 - got;
1327  next++;
1328  }
1329  *have = got;
1330  return next;
1331 }
1332 
1333 int ZEXPORT inflateSync(z_streamp strm)
1334 {
1335  unsigned len; /* number of bytes to look at or looked at */
1336  unsigned long in, out; /* temporary to save total_in and total_out */
1337  unsigned char buf[4]; /* to restore bit buffer to byte string */
1338  struct inflate_state FAR *state;
1339 
1340  /* check parameters */
1341  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1342  state = (struct inflate_state FAR *)strm->state;
1343  if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
1344 
1345  /* if first time, start search in bit buffer */
1346  if (state->mode != SYNC) {
1347  state->mode = SYNC;
1348  state->hold <<= state->bits & 7;
1349  state->bits -= state->bits & 7;
1350  len = 0;
1351  while (state->bits >= 8) {
1352  buf[len++] = (unsigned char)(state->hold);
1353  state->hold >>= 8;
1354  state->bits -= 8;
1355  }
1356  state->have = 0;
1357  syncsearch(&(state->have), buf, len);
1358  }
1359 
1360  /* search available input */
1361  len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1362  strm->avail_in -= len;
1363  strm->next_in += len;
1364  strm->total_in += len;
1365 
1366  /* return no joy or set up to restart inflate() on a new block */
1367  if (state->have != 4) return Z_DATA_ERROR;
1368  in = strm->total_in; out = strm->total_out;
1369  inflateReset(strm);
1370  strm->total_in = in; strm->total_out = out;
1371  state->mode = TYPE;
1372  return Z_OK;
1373 }
1374 
1375 /*
1376  Returns true if inflate is currently at the end of a block generated by
1377  Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1378  implementation to provide an additional safety check. PPP uses
1379  Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1380  block. When decompressing, PPP checks that at the end of input packet,
1381  inflate is waiting for these length bytes.
1382  */
1383 int ZEXPORT inflateSyncPoint(z_streamp strm)
1384 {
1385  struct inflate_state FAR *state;
1386 
1387  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1388  state = (struct inflate_state FAR *)strm->state;
1389  return state->mode == STORED && state->bits == 0;
1390 }
1391 
1392 int ZEXPORT inflateCopy(z_streamp dest, z_streamp source)
1393 {
1394  struct inflate_state FAR *state;
1395  struct inflate_state FAR *copy;
1396  unsigned char FAR *window;
1397  unsigned wsize;
1398 
1399  /* check input */
1400  if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
1401  source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
1402  return Z_STREAM_ERROR;
1403  state = (struct inflate_state FAR *)source->state;
1404 
1405  /* allocate space */
1406  copy = (struct inflate_state FAR *)
1407  ZALLOC(source, 1, sizeof(struct inflate_state));
1408  if (copy == Z_NULL) return Z_MEM_ERROR;
1409  window = Z_NULL;
1410  if (state->window != Z_NULL) {
1411  window = (unsigned char FAR *)
1412  ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
1413  if (window == Z_NULL) {
1414  ZFREE(source, copy);
1415  return Z_MEM_ERROR;
1416  }
1417  }
1418 
1419  /* copy state */
1420  zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1421  zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
1422  if (state->lencode >= state->codes &&
1423  state->lencode <= state->codes + ENOUGH - 1) {
1424  copy->lencode = copy->codes + (state->lencode - state->codes);
1425  copy->distcode = copy->codes + (state->distcode - state->codes);
1426  }
1427  copy->next = copy->codes + (state->next - state->codes);
1428  if (window != Z_NULL) {
1429  wsize = 1U << state->wbits;
1430  zmemcpy(window, state->window, wsize);
1431  }
1432  copy->window = window;
1433  dest->state = (struct internal_state FAR *)copy;
1434  return Z_OK;
1435 }
1436 
1437 int ZEXPORT inflateUndermine(z_streamp strm, int subvert)
1438 {
1439  struct inflate_state FAR *state;
1440 
1441  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1442  state = (struct inflate_state FAR *)strm->state;
1443  state->sane = !subvert;
1444 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1445  return Z_OK;
1446 #else
1447  state->sane = 1;
1448  return Z_DATA_ERROR;
1449 #endif
1450 }
1451 
1452 long ZEXPORT inflateMark(z_streamp strm)
1453 {
1454  struct inflate_state FAR *state;
1455 
1456  if (strm == Z_NULL || strm->state == Z_NULL) return -1L << 16;
1457  state = (struct inflate_state FAR *)strm->state;
1458  return ((long)(state->back) << 16) +
1459  (state->mode == COPY ? state->length :
1460  (state->mode == MATCH ? state->was - state->length : 0));
1461 }
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
const XML_Char * version
#define Z_BLOCK
Definition: zlib.h:169
unsigned nlen
Definition: inflate.h:112
Definition: inflate.h:36
unsigned short val
Definition: inftrees.h:27
int ZEXPORT inflateInit2_(z_streamp strm, int windowBits, const char *version, int stream_size)
Definition: inflate.cc:175
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:1333
voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size)
Definition: zutil.cc:294
int havedict
Definition: inflate.h:85
Definition: inflate.h:34
#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:213
unsigned wnext
Definition: inflate.h:95
int ZEXPORT inflateReset2(z_streamp strm, int windowBits)
Definition: inflate.cc:139
unsigned ndist
Definition: inflate.h:113
unsigned long total
Definition: inflate.h:89
int ZEXPORT inflateUndermine(z_streamp strm, int subvert)
Definition: inflate.cc:1437
gz_headerp head
Definition: inflate.h:90
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
unsigned wsize
Definition: inflate.h:93
unsigned have
Definition: inflate.h:114
unsigned distbits
Definition: inflate.h:109
#define ENOUGH
Definition: inftrees.h:51
#define BITS(n)
Definition: inflate.cc:488
#define Z_NEED_DICT
Definition: zlib.h:175
int ZEXPORT inflateCopy(z_streamp dest, z_streamp source)
Definition: inflate.cc:1392
unsigned extra
Definition: inflate.h:104
Definition: inftrees.h:57
code const FAR * distcode
Definition: inflate.h:107
#define Tracev(x)
Definition: zutil.h:231
unsigned long check
Definition: inflate.h:88
uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len)
Definition: adler32.cc:65
#define ZSWAP32(q)
Definition: zutil.h:249
Definition: inflate.h:49
Definition: inflate.h:27
unsigned char op
Definition: inftrees.h:25
#define RESTORE()
Definition: inflate.cc:452
void ZLIB_INTERNAL zmemcpy(Bytef *dest, const Bytef *source, uInt len)
Definition: zutil.cc:150
Definition: inflate.h:32
Definition: inflate.h:30
gz_header FAR * gz_headerp
Definition: zlib.h:129
#define Z_STREAM_ERROR
Definition: zlib.h:177
#define local
Definition: adler32.cc:10
unsigned lenbits
Definition: inflate.h:108
unsigned short lens[320]
Definition: inflate.h:116
#define DEF_WBITS
Definition: zutil.h:59
Definition: inflate.h:41
unsigned ncode
Definition: inflate.h:111
Definition: inflate.h:37
#define CRC2(check, word)
Definition: inflate.cc:423
#define ZALLOC(strm, items, size)
Definition: zutil.h:243
void copy(std::vector< T > &main, const std::vector< T > &data)
Definition: DicomRun.hh:91
#define Z_FINISH
Definition: zlib.h:168
int ZEXPORT inflateResetKeep(z_streamp strm)
Definition: inflate.cc:103
Definition: inflate.h:48
code FAR * next
Definition: inflate.h:115
z_streamp strm
Definition: deflate.h:98
Definition: inflate.h:52
Definition: inflate.h:26
#define BYTEBITS()
Definition: inflate.cc:499
code codes[ENOUGH]
Definition: inflate.h:118
local void fixedtables OF((struct inflate_state FAR *state))
unsigned long hold
Definition: inflate.h:98
Definition: inflate.h:47
Definition: inftrees.h:55
#define Z_DEFLATED
Definition: zlib.h:205
Definition: inflate.h:42
#define Z_DATA_ERROR
Definition: zlib.h:178
Definition: inflate.h:46
int ZEXPORT inflateSyncPoint(z_streamp strm)
Definition: inflate.cc:1383
#define Z_TREES
Definition: zlib.h:170
#define ZFREE(strm, addr)
Definition: zutil.h:245
Definition: inflate.h:23
#define Z_STREAM_END
Definition: zlib.h:174
Definition: inflate.h:31
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
Definition: zutil.cc:301
#define GUNZIP
Definition: inflate.h:16
unsigned short work[288]
Definition: inflate.h:117
#define UPDATE(check, buf, len)
Definition: inflate.cc:415
Definition: inflate.h:29
Definition: inflate.h:21
#define Z_MEM_ERROR
Definition: zlib.h:179
unsigned char bits
Definition: inftrees.h:26
local unsigned syncsearch(unsigned FAR *have, unsigned char FAR *buf, unsigned len)
Definition: inflate.cc:1313
#define Trace(x)
Definition: zutil.h:230
#define NEEDBITS(n)
Definition: inflate.cc:481
#define ZLIB_VERSION
Definition: zlib.h:40
Definition: inftrees.h:24
unsigned dmax
Definition: inflate.h:87
unsigned bits
Definition: inflate.h:99
Definition: inflate.h:45
int ZEXPORT inflateSetDictionary(z_streamp strm, const Bytef *dictionary, uInt dictLength)
Definition: inflate.cc:1247
#define CRC4(check, word)
Definition: inflate.cc:430
#define Z_VERSION_ERROR
Definition: zlib.h:181
Definition: inflate.h:33
#define DROPBITS(n)
Definition: inflate.cc:492
#define Z_BUF_ERROR
Definition: zlib.h:180
Definition: inflate.h:25
unsigned char FAR * window
Definition: inflate.h:96
inflate_mode mode
Definition: inflate.h:82
const XML_Char int const XML_Char * value
const XML_Char int len
Definition: inflate.h:40
#define Z_OK
Definition: zlib.h:173
Definition: inflate.h:35
Definition: inftrees.h:56
#define INITBITS()
Definition: inflate.cc:463
int ZEXPORT inflatePrime(z_streamp strm, int bits, int value)
Definition: inflate.cc:218
Definition: inflate.h:50
int ZEXPORT inflateEnd(z_streamp strm)
Definition: inflate.cc:1234
#define PULLBYTE()
Definition: inflate.cc:471
#define Z_NULL
Definition: zlib.h:208
unsigned whave
Definition: inflate.h:94
unsigned offset
Definition: inflate.h:102
z_stream FAR * z_streamp
Definition: zlib.h:106
Definition: inflate.h:43
int ZEXPORT inflateGetHeader(z_streamp strm, gz_headerp head)
Definition: inflate.cc:1287
unsigned was
Definition: inflate.h:121
int ZEXPORT inflate(z_streamp strm, int flush)
Definition: inflate.cc:587
code const FAR * lencode
Definition: inflate.h:106
local void fixedtables(struct inflate_state FAR *state)
Definition: inflate.cc:246
long ZEXPORT inflateMark(z_streamp strm)
Definition: inflate.cc:1452
Definition: inflate.h:22
unsigned wbits
Definition: inflate.h:92
local int updatewindow(z_streamp strm, unsigned out)
Definition: inflate.cc:363
void wrap(std::string &str, const size_t lineLength=78, const std::string &separators=" \t")
Definition: inflate.h:51
#define Tracevv(x)
Definition: zutil.h:232
unsigned length
Definition: inflate.h:101
int ZEXPORT inflateReset(z_streamp strm)
Definition: inflate.cc:127