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