easyzlib.c
1 /* easyzlib.c implementation
2 
3  easyzlib release 1.0
4  Copyright (C) 2008 First Objective Software, Inc. All rights reserved
5  This entire notice must be retained in this source code
6  Redistributing this source code requires written permission
7  This software is provided "as is", with no warranty.
8  Latest fixes enhancements and documentation at www.firstobject.com
9 
10  Wrapper for zlib 1.2.3, see copyright below
11  This is a modified version of zlib, modified as follows:
12  - All zlib code (including zlib headers) is in this one source file
13  - Removed conditional generation of header files (tree.h inftrees.h crc32.h)
14  - Removed DLL related directives (ZLIB_DLL ZEXPORT ZEXTERN ZLIB_WINAPI)
15  - Removed "GZIP" functionality (gzFile NO_GZIP NO_GZCOMPRESS)
16  - Removed dummy declaration workaround for certain compilers (NO_DUMMY_DECL)
17  - New simple wrapper functions have ez prefix
18  - Disabled three Level 4 warnings warnings for Visual C++
19 */
20 
21 #include "easyzlib.h"
22 
23 #if defined(_MSC_VER)
24 #pragma warning(disable:4127) // conditional expression is constant
25 #pragma warning(disable:4131) // uses old-style declarator
26 #pragma warning(disable:4244) // conversion from 'int ' to 'unsigned short ', possible loss of data
27 #endif
28 
29 /* zlib.h -- interface of the 'zlib' general purpose compression library
30  version 1.2.3, July 18th, 2005
31 
32  Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
33 
34  This software is provided 'as-is', without any express or implied
35  warranty. In no event will the authors be held liable for any damages
36  arising from the use of this software.
37 
38  Permission is granted to anyone to use this software for any purpose,
39  including commercial applications, and to alter it and redistribute it
40  freely, subject to the following restrictions:
41 
42  1. The origin of this software must not be misrepresented; you must not
43  claim that you wrote the original software. If you use this software
44  in a product, an acknowledgment in the product documentation would be
45  appreciated but is not required.
46  2. Altered source versions must be plainly marked as such, and must not be
47  misrepresented as being the original software.
48  3. This notice may not be removed or altered from any source distribution.
49 
50  Jean-loup Gailly Mark Adler
51  jloup@gzip.org madler@alumni.caltech.edu
52 
53 
54  The data format used by the zlib library is described by RFCs (Request for
55  Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
56  (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
57 */
58 
59 #if defined(__MSDOS__) && !defined(MSDOS)
60 # define MSDOS
61 #endif
62 #if (defined(OS_2) || defined(__OS2__)) && !defined(OS2)
63 # define OS2
64 #endif
65 #if defined(_WINDOWS) && !defined(WINDOWS)
66 # define WINDOWS
67 #endif
68 #if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__)
69 # ifndef WIN32
70 # define WIN32
71 # endif
72 #endif
73 #if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32)
74 # if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__)
75 # ifndef SYS16BIT
76 # define SYS16BIT
77 # endif
78 # endif
79 #endif
80 
81 /*
82  * Compile with -DMAXSEG_64K if the alloc function cannot allocate more
83  * than 64k bytes at a time (needed on systems with 16-bit int).
84  */
85 #ifdef SYS16BIT
86 # define MAXSEG_64K
87 #endif
88 #ifdef MSDOS
89 # define UNALIGNED_OK
90 #endif
91 
92 #ifdef __STDC_VERSION__
93 # ifndef STDC
94 # define STDC
95 # endif
96 # if __STDC_VERSION__ >= 199901L
97 # ifndef STDC99
98 # define STDC99
99 # endif
100 # endif
101 #endif
102 #if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus))
103 # define STDC
104 #endif
105 #if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__))
106 # define STDC
107 #endif
108 #if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32))
109 # define STDC
110 #endif
111 #if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__))
112 # define STDC
113 #endif
114 
115 #if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */
116 # define STDC
117 #endif
118 
119 #ifndef STDC
120 # ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */
121 # define const /* note: need a more gentle solution here */
122 # endif
123 #endif
124 
125 /* Maximum value for memLevel in deflateInit2 */
126 #ifndef MAX_MEM_LEVEL
127 # ifdef MAXSEG_64K
128 # define MAX_MEM_LEVEL 8
129 # else
130 # define MAX_MEM_LEVEL 9
131 # endif
132 #endif
133 
134 /* Maximum value for windowBits in deflateInit2 and inflateInit2.
135  * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
136  * created by gzip. (Files created by minigzip can still be extracted by
137  * gzip.)
138  */
139 #ifndef MAX_WBITS
140 # define MAX_WBITS 15 /* 32K LZ77 window */
141 #endif
142 
143 /* The memory requirements for deflate are (in bytes):
144  (1 << (windowBits+2)) + (1 << (memLevel+9))
145  that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
146  plus a few kilobytes for small objects. For example, if you want to reduce
147  the default memory requirements from 256K to 128K, compile with
148  make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
149  Of course this will generally degrade compression (there's no free lunch).
150 
151  The memory requirements for inflate are (in bytes) 1 << windowBits
152  that is, 32K for windowBits=15 (default value) plus a few kilobytes
153  for small objects.
154 */
155 
156 #ifndef OF /* function prototypes */
157 # ifdef STDC
158 # define OF(args) args
159 # else
160 # define OF(args) ()
161 # endif
162 #endif
163 
164 /* The following definitions for FAR are needed only for MSDOS mixed
165  * model programming (small or medium model with some far allocations).
166  * This was tested only with MSC; for other MSDOS compilers you may have
167  * to define NO_MEMCPY in zutil.h. If you don't need the mixed model,
168  * just define FAR to be empty.
169  */
170 #ifdef SYS16BIT
171 # if defined(M_I86SM) || defined(M_I86MM)
172  /* MSC small or medium model */
173 # define SMALL_MEDIUM
174 # ifdef _MSC_VER
175 # define FAR _far
176 # else
177 # define FAR far
178 # endif
179 # endif
180 # if (defined(__SMALL__) || defined(__MEDIUM__))
181  /* Turbo C small or medium model */
182 # define SMALL_MEDIUM
183 # ifdef __BORLANDC__
184 # define FAR _far
185 # else
186 # define FAR far
187 # endif
188 # endif
189 #endif
190 
191 #ifndef FAR
192 # define FAR
193 #endif
194 
195 #if !defined(__MACTYPES__)
196 typedef unsigned char Byte; /* 8 bits */
197 #endif
198 typedef unsigned int uInt; /* 16 bits or more */
199 typedef unsigned long uLong; /* 32 bits or more */
200 
201 #ifdef SMALL_MEDIUM
202  /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */
203 # define Bytef Byte FAR
204 #else
205  typedef Byte FAR Bytef;
206 #endif
207 typedef char FAR charf;
208 typedef int FAR intf;
209 typedef uInt FAR uIntf;
210 typedef uLong FAR uLongf;
211 
212 #ifdef STDC
213  typedef void const *voidpc;
214  typedef void FAR *voidpf;
215  typedef void *voidp;
216 #else
217  typedef Byte const *voidpc;
218  typedef Byte FAR *voidpf;
219  typedef Byte *voidp;
220 #endif
221 
222 #if 0 /* HAVE_UNISTD_H -- this line is updated by ./configure */
223 # include <sys/types.h> /* for off_t */
224 # include <unistd.h> /* for SEEK_* and off_t */
225 # ifdef VMS
226 # include <unixio.h> /* for off_t */
227 # endif
228 # define z_off_t off_t
229 #endif
230 #ifndef SEEK_SET
231 # define SEEK_SET 0 /* Seek from beginning of file. */
232 # define SEEK_CUR 1 /* Seek from current position. */
233 # define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
234 #endif
235 #ifndef z_off_t
236 # define z_off_t long
237 #endif
238 
239 #if defined(__OS400__)
240 # define NO_vsnprintf
241 #endif
242 
243 #if defined(__MVS__)
244 # define NO_vsnprintf
245 # ifdef FAR
246 # undef FAR
247 # endif
248 #endif
249 
250 #ifdef __cplusplus
251 extern "C" {
252 #endif
253 
254 #define ZLIB_VERSION "1.2.3"
255 #define ZLIB_VERNUM 0x1230
256 
257 /*
258  The 'zlib' compression library provides in-memory compression and
259  decompression functions, including integrity checks of the uncompressed
260  data. This version of the library supports only one compression method
261  (deflation) but other algorithms will be added later and will have the same
262  stream interface.
263 
264  Compression can be done in a single step if the buffers are large
265  enough (for example if an input file is mmap'ed), or can be done by
266  repeated calls of the compression function. In the latter case, the
267  application must provide more input and/or consume the output
268  (providing more output space) before each call.
269 
270  The compressed data format used by default by the in-memory functions is
271  the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
272  around a deflate stream, which is itself documented in RFC 1951.
273 
274  The library also supports reading and writing files in gzip (.gz) format
275  with an interface similar to that of stdio using the functions that start
276  with "gz". The gzip format is different from the zlib format. gzip is a
277  gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
278 
279  This library can optionally read and write gzip streams in memory as well.
280 
281  The zlib format was designed to be compact and fast for use in memory
282  and on communications channels. The gzip format was designed for single-
283  file compression on file systems, has a larger header than zlib to maintain
284  directory information, and uses a different, slower check method than zlib.
285 
286  The library does not install any signal handler. The decoder checks
287  the consistency of the compressed data, so the library should never
288  crash even in case of corrupted input.
289 */
290 
291 typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
292 typedef void (*free_func) OF((voidpf opaque, voidpf address));
293 
294 struct internal_state;
295 
296 typedef struct z_stream_s {
297  Bytef *next_in; /* next input byte */
298  uInt avail_in; /* number of bytes available at next_in */
299  uLong total_in; /* total nb of input bytes read so far */
300 
301  Bytef *next_out; /* next output byte should be put there */
302  uInt avail_out; /* remaining free space at next_out */
303  uLong total_out; /* total nb of bytes output so far */
304 
305  char *msg; /* last error message, NULL if no error */
306  struct internal_state FAR *state; /* not visible by applications */
307 
308  alloc_func zalloc; /* used to allocate the internal state */
309  free_func zfree; /* used to free the internal state */
310  voidpf opaque; /* private data object passed to zalloc and zfree */
311 
312  int data_type; /* best guess about the data type: binary or text */
313  uLong adler; /* adler32 value of the uncompressed data */
314  uLong reserved; /* reserved for future use */
315 } z_stream;
316 
317 typedef z_stream FAR *z_streamp;
318 
319 /*
320  gzip header information passed to and from zlib routines. See RFC 1952
321  for more details on the meanings of these fields.
322 */
323 typedef struct gz_header_s {
324  int text; /* true if compressed data believed to be text */
325  uLong time; /* modification time */
326  int xflags; /* extra flags (not used when writing a gzip file) */
327  int os; /* operating system */
328  Bytef *extra; /* pointer to extra field or Z_NULL if none */
329  uInt extra_len; /* extra field length (valid if extra != Z_NULL) */
330  uInt extra_max; /* space at extra (only when reading header) */
331  Bytef *name; /* pointer to zero-terminated file name or Z_NULL */
332  uInt name_max; /* space at name (only when reading header) */
333  Bytef *comment; /* pointer to zero-terminated comment or Z_NULL */
334  uInt comm_max; /* space at comment (only when reading header) */
335  int hcrc; /* true if there was or will be a header crc */
336  int done; /* true when done reading gzip header (not used
337  when writing a gzip file) */
338 } gz_header;
339 
340 typedef gz_header FAR *gz_headerp;
341 
342 /*
343  The application must update next_in and avail_in when avail_in has
344  dropped to zero. It must update next_out and avail_out when avail_out
345  has dropped to zero. The application must initialize zalloc, zfree and
346  opaque before calling the init function. All other fields are set by the
347  compression library and must not be updated by the application.
348 
349  The opaque value provided by the application will be passed as the first
350  parameter for calls of zalloc and zfree. This can be useful for custom
351  memory management. The compression library attaches no meaning to the
352  opaque value.
353 
354  zalloc must return Z_NULL if there is not enough memory for the object.
355  If zlib is used in a multi-threaded application, zalloc and zfree must be
356  thread safe.
357 
358  On 16-bit systems, the functions zalloc and zfree must be able to allocate
359  exactly 65536 bytes, but will not be required to allocate more than this
360  if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
361  pointers returned by zalloc for objects of exactly 65536 bytes *must*
362  have their offset normalized to zero. The default allocation function
363  provided by this library ensures this (see zutil.c). To reduce memory
364  requirements and avoid any allocation of 64K objects, at the expense of
365  compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
366 
367  The fields total_in and total_out can be used for statistics or
368  progress reports. After compression, total_in holds the total size of
369  the uncompressed data and may be saved for use in the decompressor
370  (particularly if the decompressor wants to decompress everything in
371  a single step).
372 */
373 
374 #define Z_NO_FLUSH 0
375 #define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
376 #define Z_SYNC_FLUSH 2
377 #define Z_FULL_FLUSH 3
378 #define Z_FINISH 4
379 #define Z_BLOCK 5
380 /* Allowed flush values; see deflate() and inflate() below for details */
381 
382 #define Z_OK 0
383 #define Z_STREAM_END 1
384 #define Z_NEED_DICT 2
385 #define Z_ERRNO (-1)
386 #define Z_STREAM_ERROR (-2)
387 #define Z_DATA_ERROR (-3)
388 #define Z_MEM_ERROR (-4)
389 #define Z_BUF_ERROR (-5)
390 #define Z_VERSION_ERROR (-6)
391 /* Return codes for the compression/decompression functions. Negative
392  * values are errors, positive values are used for special but normal events.
393  */
394 
395 #define Z_NO_COMPRESSION 0
396 #define Z_BEST_SPEED 1
397 #define Z_BEST_COMPRESSION 9
398 #define Z_DEFAULT_COMPRESSION (-1)
399 /* compression levels */
400 
401 #define Z_FILTERED 1
402 #define Z_HUFFMAN_ONLY 2
403 #define Z_RLE 3
404 #define Z_FIXED 4
405 #define Z_DEFAULT_STRATEGY 0
406 /* compression strategy; see deflateInit2() below for details */
407 
408 #define Z_BINARY 0
409 #define Z_TEXT 1
410 #define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */
411 #define Z_UNKNOWN 2
412 /* Possible values of the data_type field (though see inflate()) */
413 
414 #define Z_DEFLATED 8
415 /* The deflate compression method (the only one supported in this version) */
416 
417 #define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
418 
419 /*
420 int deflateInit OF((z_streamp strm, int level));
421 
422  Initializes the internal stream state for compression. The fields
423  zalloc, zfree and opaque must be initialized before by the caller.
424  If zalloc and zfree are set to Z_NULL, deflateInit updates them to
425  use default allocation functions.
426 
427  The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
428  1 gives best speed, 9 gives best compression, 0 gives no compression at
429  all (the input data is simply copied a block at a time).
430  Z_DEFAULT_COMPRESSION requests a default compromise between speed and
431  compression (currently equivalent to level 6).
432 
433  deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
434  enough memory, Z_STREAM_ERROR if level is not a valid compression level,
435  Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
436  with the version assumed by the caller (ZLIB_VERSION).
437  msg is set to null if there is no error message. deflateInit does not
438  perform any compression: this will be done by deflate().
439 */
440 
441 
442 int deflate OF((z_streamp strm, int flush));
443 /*
444  deflate compresses as much data as possible, and stops when the input
445  buffer becomes empty or the output buffer becomes full. It may introduce some
446  output latency (reading input without producing any output) except when
447  forced to flush.
448 
449  The detailed semantics are as follows. deflate performs one or both of the
450  following actions:
451 
452  - Compress more input starting at next_in and update next_in and avail_in
453  accordingly. If not all input can be processed (because there is not
454  enough room in the output buffer), next_in and avail_in are updated and
455  processing will resume at this point for the next call of deflate().
456 
457  - Provide more output starting at next_out and update next_out and avail_out
458  accordingly. This action is forced if the parameter flush is non zero.
459  Forcing flush frequently degrades the compression ratio, so this parameter
460  should be set only when necessary (in interactive applications).
461  Some output may be provided even if flush is not set.
462 
463  Before the call of deflate(), the application should ensure that at least
464  one of the actions is possible, by providing more input and/or consuming
465  more output, and updating avail_in or avail_out accordingly; avail_out
466  should never be zero before the call. The application can consume the
467  compressed output when it wants, for example when the output buffer is full
468  (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
469  and with zero avail_out, it must be called again after making room in the
470  output buffer because there might be more output pending.
471 
472  Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to
473  decide how much data to accumualte before producing output, in order to
474  maximize compression.
475 
476  If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
477  flushed to the output buffer and the output is aligned on a byte boundary, so
478  that the decompressor can get all input data available so far. (In particular
479  avail_in is zero after the call if enough output space has been provided
480  before the call.) Flushing may degrade compression for some compression
481  algorithms and so it should be used only when necessary.
482 
483  If flush is set to Z_FULL_FLUSH, all output is flushed as with
484  Z_SYNC_FLUSH, and the compression state is reset so that decompression can
485  restart from this point if previous compressed data has been damaged or if
486  random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
487  compression.
488 
489  If deflate returns with avail_out == 0, this function must be called again
490  with the same value of the flush parameter and more output space (updated
491  avail_out), until the flush is complete (deflate returns with non-zero
492  avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that
493  avail_out is greater than six to avoid repeated flush markers due to
494  avail_out == 0 on return.
495 
496  If the parameter flush is set to Z_FINISH, pending input is processed,
497  pending output is flushed and deflate returns with Z_STREAM_END if there
498  was enough output space; if deflate returns with Z_OK, this function must be
499  called again with Z_FINISH and more output space (updated avail_out) but no
500  more input data, until it returns with Z_STREAM_END or an error. After
501  deflate has returned Z_STREAM_END, the only possible operations on the
502  stream are deflateReset or deflateEnd.
503 
504  Z_FINISH can be used immediately after deflateInit if all the compression
505  is to be done in a single step. In this case, avail_out must be at least
506  the value returned by deflateBound (see below). If deflate does not return
507  Z_STREAM_END, then it must be called again as described above.
508 
509  deflate() sets strm->adler to the adler32 checksum of all input read
510  so far (that is, total_in bytes).
511 
512  deflate() may update strm->data_type if it can make a good guess about
513  the input data type (Z_BINARY or Z_TEXT). In doubt, the data is considered
514  binary. This field is only for information purposes and does not affect
515  the compression algorithm in any manner.
516 
517  deflate() returns Z_OK if some progress has been made (more input
518  processed or more output produced), Z_STREAM_END if all input has been
519  consumed and all output has been produced (only when flush is set to
520  Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
521  if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
522  (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not
523  fatal, and deflate() can be called again with more input and more output
524  space to continue compressing.
525 */
526 
527 
528 int deflateEnd OF((z_streamp strm));
529 /*
530  All dynamically allocated data structures for this stream are freed.
531  This function discards any unprocessed input and does not flush any
532  pending output.
533 
534  deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
535  stream state was inconsistent, Z_DATA_ERROR if the stream was freed
536  prematurely (some input or output was discarded). In the error case,
537  msg may be set but then points to a static string (which must not be
538  deallocated).
539 */
540 
541 
542 /*
543 int inflateInit OF((z_streamp strm));
544 
545  Initializes the internal stream state for decompression. The fields
546  next_in, avail_in, zalloc, zfree and opaque must be initialized before by
547  the caller. If next_in is not Z_NULL and avail_in is large enough (the exact
548  value depends on the compression method), inflateInit determines the
549  compression method from the zlib header and allocates all data structures
550  accordingly; otherwise the allocation will be deferred to the first call of
551  inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to
552  use default allocation functions.
553 
554  inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
555  memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
556  version assumed by the caller. msg is set to null if there is no error
557  message. inflateInit does not perform any decompression apart from reading
558  the zlib header if present: this will be done by inflate(). (So next_in and
559  avail_in may be modified, but next_out and avail_out are unchanged.)
560 */
561 
562 
563 int inflate OF((z_streamp strm, int flush));
564 /*
565  inflate decompresses as much data as possible, and stops when the input
566  buffer becomes empty or the output buffer becomes full. It may introduce
567  some output latency (reading input without producing any output) except when
568  forced to flush.
569 
570  The detailed semantics are as follows. inflate performs one or both of the
571  following actions:
572 
573  - Decompress more input starting at next_in and update next_in and avail_in
574  accordingly. If not all input can be processed (because there is not
575  enough room in the output buffer), next_in is updated and processing
576  will resume at this point for the next call of inflate().
577 
578  - Provide more output starting at next_out and update next_out and avail_out
579  accordingly. inflate() provides as much output as possible, until there
580  is no more input data or no more space in the output buffer (see below
581  about the flush parameter).
582 
583  Before the call of inflate(), the application should ensure that at least
584  one of the actions is possible, by providing more input and/or consuming
585  more output, and updating the next_* and avail_* values accordingly.
586  The application can consume the uncompressed output when it wants, for
587  example when the output buffer is full (avail_out == 0), or after each
588  call of inflate(). If inflate returns Z_OK and with zero avail_out, it
589  must be called again after making room in the output buffer because there
590  might be more output pending.
591 
592  The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH,
593  Z_FINISH, or Z_BLOCK. Z_SYNC_FLUSH requests that inflate() flush as much
594  output as possible to the output buffer. Z_BLOCK requests that inflate() stop
595  if and when it gets to the next deflate block boundary. When decoding the
596  zlib or gzip format, this will cause inflate() to return immediately after
597  the header and before the first block. When doing a raw inflate, inflate()
598  will go ahead and process the first block, and will return when it gets to
599  the end of that block, or when it runs out of data.
600 
601  The Z_BLOCK option assists in appending to or combining deflate streams.
602  Also to assist in this, on return inflate() will set strm->data_type to the
603  number of unused bits in the last byte taken from strm->next_in, plus 64
604  if inflate() is currently decoding the last block in the deflate stream,
605  plus 128 if inflate() returned immediately after decoding an end-of-block
606  code or decoding the complete header up to just before the first byte of the
607  deflate stream. The end-of-block will not be indicated until all of the
608  uncompressed data from that block has been written to strm->next_out. The
609  number of unused bits may in general be greater than seven, except when
610  bit 7 of data_type is set, in which case the number of unused bits will be
611  less than eight.
612 
613  inflate() should normally be called until it returns Z_STREAM_END or an
614  error. However if all decompression is to be performed in a single step
615  (a single call of inflate), the parameter flush should be set to
616  Z_FINISH. In this case all pending input is processed and all pending
617  output is flushed; avail_out must be large enough to hold all the
618  uncompressed data. (The size of the uncompressed data may have been saved
619  by the compressor for this purpose.) The next operation on this stream must
620  be inflateEnd to deallocate the decompression state. The use of Z_FINISH
621  is never required, but can be used to inform inflate that a faster approach
622  may be used for the single inflate() call.
623 
624  In this implementation, inflate() always flushes as much output as
625  possible to the output buffer, and always uses the faster approach on the
626  first call. So the only effect of the flush parameter in this implementation
627  is on the return value of inflate(), as noted below, or when it returns early
628  because Z_BLOCK is used.
629 
630  If a preset dictionary is needed after this call (see inflateSetDictionary
631  below), inflate sets strm->adler to the adler32 checksum of the dictionary
632  chosen by the compressor and returns Z_NEED_DICT; otherwise it sets
633  strm->adler to the adler32 checksum of all output produced so far (that is,
634  total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described
635  below. At the end of the stream, inflate() checks that its computed adler32
636  checksum is equal to that saved by the compressor and returns Z_STREAM_END
637  only if the checksum is correct.
638 
639  inflate() will decompress and check either zlib-wrapped or gzip-wrapped
640  deflate data. The header type is detected automatically. Any information
641  contained in the gzip header is not retained, so applications that need that
642  information should instead use raw inflate, see inflateInit2() below, or
643  inflateBack() and perform their own processing of the gzip header and
644  trailer.
645 
646  inflate() returns Z_OK if some progress has been made (more input processed
647  or more output produced), Z_STREAM_END if the end of the compressed data has
648  been reached and all uncompressed output has been produced, Z_NEED_DICT if a
649  preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
650  corrupted (input stream not conforming to the zlib format or incorrect check
651  value), Z_STREAM_ERROR if the stream structure was inconsistent (for example
652  if next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory,
653  Z_BUF_ERROR if no progress is possible or if there was not enough room in the
654  output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and
655  inflate() can be called again with more input and more output space to
656  continue decompressing. If Z_DATA_ERROR is returned, the application may then
657  call inflateSync() to look for a good compression block if a partial recovery
658  of the data is desired.
659 */
660 
661 
662 int inflateEnd OF((z_streamp strm));
663 /*
664  All dynamically allocated data structures for this stream are freed.
665  This function discards any unprocessed input and does not flush any
666  pending output.
667 
668  inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
669  was inconsistent. In the error case, msg may be set but then points to a
670  static string (which must not be deallocated).
671 */
672 
673 /*
674 int deflateInit2 OF((z_streamp strm,
675  int level,
676  int method,
677  int windowBits,
678  int memLevel,
679  int strategy));
680 
681  This is another version of deflateInit with more compression options. The
682  fields next_in, zalloc, zfree and opaque must be initialized before by
683  the caller.
684 
685  The method parameter is the compression method. It must be Z_DEFLATED in
686  this version of the library.
687 
688  The windowBits parameter is the base two logarithm of the window size
689  (the size of the history buffer). It should be in the range 8..15 for this
690  version of the library. Larger values of this parameter result in better
691  compression at the expense of memory usage. The default value is 15 if
692  deflateInit is used instead.
693 
694  windowBits can also be -8..-15 for raw deflate. In this case, -windowBits
695  determines the window size. deflate() will then generate raw deflate data
696  with no zlib header or trailer, and will not compute an adler32 check value.
697 
698  windowBits can also be greater than 15 for optional gzip encoding. Add
699  16 to windowBits to write a simple gzip header and trailer around the
700  compressed data instead of a zlib wrapper. The gzip header will have no
701  file name, no extra data, no comment, no modification time (set to zero),
702  no header crc, and the operating system will be set to 255 (unknown). If a
703  gzip stream is being written, strm->adler is a crc32 instead of an adler32.
704 
705  The memLevel parameter specifies how much memory should be allocated
706  for the internal compression state. memLevel=1 uses minimum memory but
707  is slow and reduces compression ratio; memLevel=9 uses maximum memory
708  for optimal speed. The default value is 8. See zconf.h for total memory
709  usage as a function of windowBits and memLevel.
710 
711  The strategy parameter is used to tune the compression algorithm. Use the
712  value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
713  filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no
714  string match), or Z_RLE to limit match distances to one (run-length
715  encoding). Filtered data consists mostly of small values with a somewhat
716  random distribution. In this case, the compression algorithm is tuned to
717  compress them better. The effect of Z_FILTERED is to force more Huffman
718  coding and less string matching; it is somewhat intermediate between
719  Z_DEFAULT and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as fast as
720  Z_HUFFMAN_ONLY, but give better compression for PNG image data. The strategy
721  parameter only affects the compression ratio but not the correctness of the
722  compressed output even if it is not set appropriately. Z_FIXED prevents the
723  use of dynamic Huffman codes, allowing for a simpler decoder for special
724  applications.
725 
726  deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
727  memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
728  method). msg is set to null if there is no error message. deflateInit2 does
729  not perform any compression: this will be done by deflate().
730 */
731 
732 int deflateSetDictionary OF((z_streamp strm,
733  const Bytef *dictionary,
734  uInt dictLength));
735 /*
736  Initializes the compression dictionary from the given byte sequence
737  without producing any compressed output. This function must be called
738  immediately after deflateInit, deflateInit2 or deflateReset, before any
739  call of deflate. The compressor and decompressor must use exactly the same
740  dictionary (see inflateSetDictionary).
741 
742  The dictionary should consist of strings (byte sequences) that are likely
743  to be encountered later in the data to be compressed, with the most commonly
744  used strings preferably put towards the end of the dictionary. Using a
745  dictionary is most useful when the data to be compressed is short and can be
746  predicted with good accuracy; the data can then be compressed better than
747  with the default empty dictionary.
748 
749  Depending on the size of the compression data structures selected by
750  deflateInit or deflateInit2, a part of the dictionary may in effect be
751  discarded, for example if the dictionary is larger than the window size in
752  deflate or deflate2. Thus the strings most likely to be useful should be
753  put at the end of the dictionary, not at the front. In addition, the
754  current implementation of deflate will use at most the window size minus
755  262 bytes of the provided dictionary.
756 
757  Upon return of this function, strm->adler is set to the adler32 value
758  of the dictionary; the decompressor may later use this value to determine
759  which dictionary has been used by the compressor. (The adler32 value
760  applies to the whole dictionary even if only a subset of the dictionary is
761  actually used by the compressor.) If a raw deflate was requested, then the
762  adler32 value is not computed and strm->adler is not set.
763 
764  deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
765  parameter is invalid (such as NULL dictionary) or the stream state is
766  inconsistent (for example if deflate has already been called for this stream
767  or if the compression method is bsort). deflateSetDictionary does not
768  perform any compression: this will be done by deflate().
769 */
770 
771 int deflateCopy OF((z_streamp dest,
772  z_streamp source));
773 /*
774  Sets the destination stream as a complete copy of the source stream.
775 
776  This function can be useful when several compression strategies will be
777  tried, for example when there are several ways of pre-processing the input
778  data with a filter. The streams that will be discarded should then be freed
779  by calling deflateEnd. Note that deflateCopy duplicates the internal
780  compression state which can be quite large, so this strategy is slow and
781  can consume lots of memory.
782 
783  deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
784  enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
785  (such as zalloc being NULL). msg is left unchanged in both source and
786  destination.
787 */
788 
789 int deflateReset OF((z_streamp strm));
790 /*
791  This function is equivalent to deflateEnd followed by deflateInit,
792  but does not free and reallocate all the internal compression state.
793  The stream will keep the same compression level and any other attributes
794  that may have been set by deflateInit2.
795 
796  deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
797  stream state was inconsistent (such as zalloc or state being NULL).
798 */
799 
800 int deflateParams OF((z_streamp strm,
801  int level,
802  int strategy));
803 /*
804  Dynamically update the compression level and compression strategy. The
805  interpretation of level and strategy is as in deflateInit2. This can be
806  used to switch between compression and straight copy of the input data, or
807  to switch to a different kind of input data requiring a different
808  strategy. If the compression level is changed, the input available so far
809  is compressed with the old level (and may be flushed); the new level will
810  take effect only at the next call of deflate().
811 
812  Before the call of deflateParams, the stream state must be set as for
813  a call of deflate(), since the currently available input may have to
814  be compressed and flushed. In particular, strm->avail_out must be non-zero.
815 
816  deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
817  stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
818  if strm->avail_out was zero.
819 */
820 
821 int deflateTune OF((z_streamp strm,
822  int good_length,
823  int max_lazy,
824  int nice_length,
825  int max_chain));
826 /*
827  Fine tune deflate's internal compression parameters. This should only be
828  used by someone who understands the algorithm used by zlib's deflate for
829  searching for the best matching string, and even then only by the most
830  fanatic optimizer trying to squeeze out the last compressed bit for their
831  specific input data. Read the deflate.c source code for the meaning of the
832  max_lazy, good_length, nice_length, and max_chain parameters.
833 
834  deflateTune() can be called after deflateInit() or deflateInit2(), and
835  returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream.
836  */
837 
838 uLong deflateBound OF((z_streamp strm,
839  uLong sourceLen));
840 /*
841  deflateBound() returns an upper bound on the compressed size after
842  deflation of sourceLen bytes. It must be called after deflateInit()
843  or deflateInit2(). This would be used to allocate an output buffer
844  for deflation in a single pass, and so would be called before deflate().
845 */
846 
847 int deflatePrime OF((z_streamp strm,
848  int bits,
849  int value));
850 /*
851  deflatePrime() inserts bits in the deflate output stream. The intent
852  is that this function is used to start off the deflate output with the
853  bits leftover from a previous deflate stream when appending to it. As such,
854  this function can only be used for raw deflate, and must be used before the
855  first deflate() call after a deflateInit2() or deflateReset(). bits must be
856  less than or equal to 16, and that many of the least significant bits of
857  value will be inserted in the output.
858 
859  deflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
860  stream state was inconsistent.
861 */
862 
863 int deflateSetHeader OF((z_streamp strm,
864  gz_headerp head));
865 /*
866  deflateSetHeader() provides gzip header information for when a gzip
867  stream is requested by deflateInit2(). deflateSetHeader() may be called
868  after deflateInit2() or deflateReset() and before the first call of
869  deflate(). The text, time, os, extra field, name, and comment information
870  in the provided gz_header structure are written to the gzip header (xflag is
871  ignored -- the extra flags are set according to the compression level). The
872  caller must assure that, if not Z_NULL, name and comment are terminated with
873  a zero byte, and that if extra is not Z_NULL, that extra_len bytes are
874  available there. If hcrc is true, a gzip header crc is included. Note that
875  the current versions of the command-line version of gzip (up through version
876  1.3.x) do not support header crc's, and will report that it is a "multi-part
877  gzip file" and give up.
878 
879  If deflateSetHeader is not used, the default gzip header has text false,
880  the time set to zero, and os set to 255, with no extra, name, or comment
881  fields. The gzip header is returned to the default state by deflateReset().
882 
883  deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
884  stream state was inconsistent.
885 */
886 
887 /*
888 int inflateInit2 OF((z_streamp strm,
889  int windowBits));
890 
891  This is another version of inflateInit with an extra parameter. The
892  fields next_in, avail_in, zalloc, zfree and opaque must be initialized
893  before by the caller.
894 
895  The windowBits parameter is the base two logarithm of the maximum window
896  size (the size of the history buffer). It should be in the range 8..15 for
897  this version of the library. The default value is 15 if inflateInit is used
898  instead. windowBits must be greater than or equal to the windowBits value
899  provided to deflateInit2() while compressing, or it must be equal to 15 if
900  deflateInit2() was not used. If a compressed stream with a larger window
901  size is given as input, inflate() will return with the error code
902  Z_DATA_ERROR instead of trying to allocate a larger window.
903 
904  windowBits can also be -8..-15 for raw inflate. In this case, -windowBits
905  determines the window size. inflate() will then process raw deflate data,
906  not looking for a zlib or gzip header, not generating a check value, and not
907  looking for any check values for comparison at the end of the stream. This
908  is for use with other formats that use the deflate compressed data format
909  such as zip. Those formats provide their own check values. If a custom
910  format is developed using the raw deflate format for compressed data, it is
911  recommended that a check value such as an adler32 or a crc32 be applied to
912  the uncompressed data as is done in the zlib, gzip, and zip formats. For
913  most applications, the zlib format should be used as is. Note that comments
914  above on the use in deflateInit2() applies to the magnitude of windowBits.
915 
916  windowBits can also be greater than 15 for optional gzip decoding. Add
917  32 to windowBits to enable zlib and gzip decoding with automatic header
918  detection, or add 16 to decode only the gzip format (the zlib format will
919  return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is
920  a crc32 instead of an adler32.
921 
922  inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
923  memory, Z_STREAM_ERROR if a parameter is invalid (such as a null strm). msg
924  is set to null if there is no error message. inflateInit2 does not perform
925  any decompression apart from reading the zlib header if present: this will
926  be done by inflate(). (So next_in and avail_in may be modified, but next_out
927  and avail_out are unchanged.)
928 */
929 
930 int inflateSetDictionary OF((z_streamp strm,
931  const Bytef *dictionary,
932  uInt dictLength));
933 /*
934  Initializes the decompression dictionary from the given uncompressed byte
935  sequence. This function must be called immediately after a call of inflate,
936  if that call returned Z_NEED_DICT. The dictionary chosen by the compressor
937  can be determined from the adler32 value returned by that call of inflate.
938  The compressor and decompressor must use exactly the same dictionary (see
939  deflateSetDictionary). For raw inflate, this function can be called
940  immediately after inflateInit2() or inflateReset() and before any call of
941  inflate() to set the dictionary. The application must insure that the
942  dictionary that was used for compression is provided.
943 
944  inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
945  parameter is invalid (such as NULL dictionary) or the stream state is
946  inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
947  expected one (incorrect adler32 value). inflateSetDictionary does not
948  perform any decompression: this will be done by subsequent calls of
949  inflate().
950 */
951 
952 int inflateSync OF((z_streamp strm));
953 /*
954  Skips invalid compressed data until a full flush point (see above the
955  description of deflate with Z_FULL_FLUSH) can be found, or until all
956  available input is skipped. No output is provided.
957 
958  inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
959  if no more input was provided, Z_DATA_ERROR if no flush point has been found,
960  or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
961  case, the application may save the current current value of total_in which
962  indicates where valid compressed data was found. In the error case, the
963  application may repeatedly call inflateSync, providing more input each time,
964  until success or end of the input data.
965 */
966 
967 int inflateCopy OF((z_streamp dest,
968  z_streamp source));
969 /*
970  Sets the destination stream as a complete copy of the source stream.
971 
972  This function can be useful when randomly accessing a large stream. The
973  first pass through the stream can periodically record the inflate state,
974  allowing restarting inflate at those points when randomly accessing the
975  stream.
976 
977  inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
978  enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
979  (such as zalloc being NULL). msg is left unchanged in both source and
980  destination.
981 */
982 
983 int inflateReset OF((z_streamp strm));
984 /*
985  This function is equivalent to inflateEnd followed by inflateInit,
986  but does not free and reallocate all the internal decompression state.
987  The stream will keep attributes that may have been set by inflateInit2.
988 
989  inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
990  stream state was inconsistent (such as zalloc or state being NULL).
991 */
992 
993 int inflatePrime OF((z_streamp strm,
994  int bits,
995  int value));
996 /*
997  This function inserts bits in the inflate input stream. The intent is
998  that this function is used to start inflating at a bit position in the
999  middle of a byte. The provided bits will be used before any bytes are used
1000  from next_in. This function should only be used with raw inflate, and
1001  should be used before the first inflate() call after inflateInit2() or
1002  inflateReset(). bits must be less than or equal to 16, and that many of the
1003  least significant bits of value will be inserted in the input.
1004 
1005  inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
1006  stream state was inconsistent.
1007 */
1008 
1009 int inflateGetHeader OF((z_streamp strm,
1010  gz_headerp head));
1011 /*
1012  inflateGetHeader() requests that gzip header information be stored in the
1013  provided gz_header structure. inflateGetHeader() may be called after
1014  inflateInit2() or inflateReset(), and before the first call of inflate().
1015  As inflate() processes the gzip stream, head->done is zero until the header
1016  is completed, at which time head->done is set to one. If a zlib stream is
1017  being decoded, then head->done is set to -1 to indicate that there will be
1018  no gzip header information forthcoming. Note that Z_BLOCK can be used to
1019  force inflate() to return immediately after header processing is complete
1020  and before any actual data is decompressed.
1021 
1022  The text, time, xflags, and os fields are filled in with the gzip header
1023  contents. hcrc is set to true if there is a header CRC. (The header CRC
1024  was valid if done is set to one.) If extra is not Z_NULL, then extra_max
1025  contains the maximum number of bytes to write to extra. Once done is true,
1026  extra_len contains the actual extra field length, and extra contains the
1027  extra field, or that field truncated if extra_max is less than extra_len.
1028  If name is not Z_NULL, then up to name_max characters are written there,
1029  terminated with a zero unless the length is greater than name_max. If
1030  comment is not Z_NULL, then up to comm_max characters are written there,
1031  terminated with a zero unless the length is greater than comm_max. When
1032  any of extra, name, or comment are not Z_NULL and the respective field is
1033  not present in the header, then that field is set to Z_NULL to signal its
1034  absence. This allows the use of deflateSetHeader() with the returned
1035  structure to duplicate the header. However if those fields are set to
1036  allocated memory, then the application will need to save those pointers
1037  elsewhere so that they can be eventually freed.
1038 
1039  If inflateGetHeader is not used, then the header information is simply
1040  discarded. The header is always checked for validity, including the header
1041  CRC if present. inflateReset() will reset the process to discard the header
1042  information. The application would need to call inflateGetHeader() again to
1043  retrieve the header from the next gzip stream.
1044 
1045  inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
1046  stream state was inconsistent.
1047 */
1048 
1049 /*
1050 int inflateBackInit OF((z_streamp strm, int windowBits,
1051  unsigned char FAR *window));
1052 
1053  Initialize the internal stream state for decompression using inflateBack()
1054  calls. The fields zalloc, zfree and opaque in strm must be initialized
1055  before the call. If zalloc and zfree are Z_NULL, then the default library-
1056  derived memory allocation routines are used. windowBits is the base two
1057  logarithm of the window size, in the range 8..15. window is a caller
1058  supplied buffer of that size. Except for special applications where it is
1059  assured that deflate was used with small window sizes, windowBits must be 15
1060  and a 32K byte window must be supplied to be able to decompress general
1061  deflate streams.
1062 
1063  See inflateBack() for the usage of these routines.
1064 
1065  inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of
1066  the paramaters are invalid, Z_MEM_ERROR if the internal state could not
1067  be allocated, or Z_VERSION_ERROR if the version of the library does not
1068  match the version of the header file.
1069 */
1070 
1071 typedef unsigned (*in_func) OF((void FAR *, unsigned char FAR * FAR *));
1072 typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned));
1073 
1074 int inflateBack OF((z_streamp strm,
1075  in_func in, void FAR *in_desc,
1076  out_func out, void FAR *out_desc));
1077 /*
1078  inflateBack() does a raw inflate with a single call using a call-back
1079  interface for input and output. This is more efficient than inflate() for
1080  file i/o applications in that it avoids copying between the output and the
1081  sliding window by simply making the window itself the output buffer. This
1082  function trusts the application to not change the output buffer passed by
1083  the output function, at least until inflateBack() returns.
1084 
1085  inflateBackInit() must be called first to allocate the internal state
1086  and to initialize the state with the user-provided window buffer.
1087  inflateBack() may then be used multiple times to inflate a complete, raw
1088  deflate stream with each call. inflateBackEnd() is then called to free
1089  the allocated state.
1090 
1091  A raw deflate stream is one with no zlib or gzip header or trailer.
1092  This routine would normally be used in a utility that reads zip or gzip
1093  files and writes out uncompressed files. The utility would decode the
1094  header and process the trailer on its own, hence this routine expects
1095  only the raw deflate stream to decompress. This is different from the
1096  normal behavior of inflate(), which expects either a zlib or gzip header and
1097  trailer around the deflate stream.
1098 
1099  inflateBack() uses two subroutines supplied by the caller that are then
1100  called by inflateBack() for input and output. inflateBack() calls those
1101  routines until it reads a complete deflate stream and writes out all of the
1102  uncompressed data, or until it encounters an error. The function's
1103  parameters and return types are defined above in the in_func and out_func
1104  typedefs. inflateBack() will call in(in_desc, &buf) which should return the
1105  number of bytes of provided input, and a pointer to that input in buf. If
1106  there is no input available, in() must return zero--buf is ignored in that
1107  case--and inflateBack() will return a buffer error. inflateBack() will call
1108  out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. out()
1109  should return zero on success, or non-zero on failure. If out() returns
1110  non-zero, inflateBack() will return with an error. Neither in() nor out()
1111  are permitted to change the contents of the window provided to
1112  inflateBackInit(), which is also the buffer that out() uses to write from.
1113  The length written by out() will be at most the window size. Any non-zero
1114  amount of input may be provided by in().
1115 
1116  For convenience, inflateBack() can be provided input on the first call by
1117  setting strm->next_in and strm->avail_in. If that input is exhausted, then
1118  in() will be called. Therefore strm->next_in must be initialized before
1119  calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called
1120  immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in
1121  must also be initialized, and then if strm->avail_in is not zero, input will
1122  initially be taken from strm->next_in[0 .. strm->avail_in - 1].
1123 
1124  The in_desc and out_desc parameters of inflateBack() is passed as the
1125  first parameter of in() and out() respectively when they are called. These
1126  descriptors can be optionally used to pass any information that the caller-
1127  supplied in() and out() functions need to do their job.
1128 
1129  On return, inflateBack() will set strm->next_in and strm->avail_in to
1130  pass back any unused input that was provided by the last in() call. The
1131  return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR
1132  if in() or out() returned an error, Z_DATA_ERROR if there was a format
1133  error in the deflate stream (in which case strm->msg is set to indicate the
1134  nature of the error), or Z_STREAM_ERROR if the stream was not properly
1135  initialized. In the case of Z_BUF_ERROR, an input or output error can be
1136  distinguished using strm->next_in which will be Z_NULL only if in() returned
1137  an error. If strm->next is not Z_NULL, then the Z_BUF_ERROR was due to
1138  out() returning non-zero. (in() will always be called before out(), so
1139  strm->next_in is assured to be defined if out() returns non-zero.) Note
1140  that inflateBack() cannot return Z_OK.
1141 */
1142 
1143 int inflateBackEnd OF((z_streamp strm));
1144 /*
1145  All memory allocated by inflateBackInit() is freed.
1146 
1147  inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream
1148  state was inconsistent.
1149 */
1150 
1151  /* utility functions */
1152 
1153 /*
1154  The following utility functions are implemented on top of the
1155  basic stream-oriented functions. To simplify the interface, some
1156  default options are assumed (compression level and memory usage,
1157  standard memory allocation functions). The source code of these
1158  utility functions can easily be modified if you need special options.
1159 */
1160 
1161 int compress OF((Bytef *dest, uLongf *destLen,
1162  const Bytef *source, uLong sourceLen));
1163 /*
1164  Compresses the source buffer into the destination buffer. sourceLen is
1165  the byte length of the source buffer. Upon entry, destLen is the total
1166  size of the destination buffer, which must be at least the value returned
1167  by compressBound(sourceLen). Upon exit, destLen is the actual size of the
1168  compressed buffer.
1169  This function can be used to compress a whole file at once if the
1170  input file is mmap'ed.
1171  compress returns Z_OK if success, Z_MEM_ERROR if there was not
1172  enough memory, Z_BUF_ERROR if there was not enough room in the output
1173  buffer.
1174 */
1175 
1176 int compress2 OF((Bytef *dest, uLongf *destLen,
1177  const Bytef *source, uLong sourceLen,
1178  int level));
1179 /*
1180  Compresses the source buffer into the destination buffer. The level
1181  parameter has the same meaning as in deflateInit. sourceLen is the byte
1182  length of the source buffer. Upon entry, destLen is the total size of the
1183  destination buffer, which must be at least the value returned by
1184  compressBound(sourceLen). Upon exit, destLen is the actual size of the
1185  compressed buffer.
1186 
1187  compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
1188  memory, Z_BUF_ERROR if there was not enough room in the output buffer,
1189  Z_STREAM_ERROR if the level parameter is invalid.
1190 */
1191 
1192 uLong compressBound OF((uLong sourceLen));
1193 /*
1194  compressBound() returns an upper bound on the compressed size after
1195  compress() or compress2() on sourceLen bytes. It would be used before
1196  a compress() or compress2() call to allocate the destination buffer.
1197 */
1198 
1199 int uncompress OF((Bytef *dest, uLongf *destLen,
1200  const Bytef *source, uLong sourceLen));
1201 /*
1202  Decompresses the source buffer into the destination buffer. sourceLen is
1203  the byte length of the source buffer. Upon entry, destLen is the total
1204  size of the destination buffer, which must be large enough to hold the
1205  entire uncompressed data. (The size of the uncompressed data must have
1206  been saved previously by the compressor and transmitted to the decompressor
1207  by some mechanism outside the scope of this compression library.)
1208  Upon exit, destLen is the actual size of the compressed buffer.
1209  This function can be used to decompress a whole file at once if the
1210  input file is mmap'ed.
1211 
1212  uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
1213  enough memory, Z_BUF_ERROR if there was not enough room in the output
1214  buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.
1215 */
1216 
1217 /*
1218  These functions are not related to compression but are exported
1219  anyway because they might be useful in applications using the
1220  compression library.
1221 */
1222 
1223 uLong adler32 OF((uLong adler, const Bytef *buf, uInt len));
1224 /*
1225  Update a running Adler-32 checksum with the bytes buf[0..len-1] and
1226  return the updated checksum. If buf is NULL, this function returns
1227  the required initial value for the checksum.
1228  An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
1229  much faster. Usage example:
1230 
1231  uLong adler = adler32(0L, Z_NULL, 0);
1232 
1233  while (read_buffer(buffer, length) != EOF) {
1234  adler = adler32(adler, buffer, length);
1235  }
1236  if (adler != original_adler) error();
1237 */
1238 
1239 uLong adler32_combine OF((uLong adler1, uLong adler2,
1240  z_off_t len2));
1241 /*
1242  Combine two Adler-32 checksums into one. For two sequences of bytes, seq1
1243  and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for
1244  each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of
1245  seq1 and seq2 concatenated, requiring only adler1, adler2, and len2.
1246 */
1247 
1248 uLong crc32 OF((uLong crc, const Bytef *buf, uInt len));
1249 /*
1250  Update a running CRC-32 with the bytes buf[0..len-1] and return the
1251  updated CRC-32. If buf is NULL, this function returns the required initial
1252  value for the for the crc. Pre- and post-conditioning (one's complement) is
1253  performed within this function so it shouldn't be done by the application.
1254  Usage example:
1255 
1256  uLong crc = crc32(0L, Z_NULL, 0);
1257 
1258  while (read_buffer(buffer, length) != EOF) {
1259  crc = crc32(crc, buffer, length);
1260  }
1261  if (crc != original_crc) error();
1262 */
1263 
1264 uLong crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2));
1265 
1266 /*
1267  Combine two CRC-32 check values into one. For two sequences of bytes,
1268  seq1 and seq2 with lengths len1 and len2, CRC-32 check values were
1269  calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32
1270  check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and
1271  len2.
1272 */
1273 
1274 
1275  /* various hacks, don't look :) */
1276 
1277 /* deflateInit and inflateInit are macros to allow checking the zlib version
1278  * and the compiler's view of z_stream:
1279  */
1280 int deflateInit_ OF((z_streamp strm, int level,
1281  const char *version, int stream_size));
1282 int inflateInit_ OF((z_streamp strm,
1283  const char *version, int stream_size));
1284 int deflateInit2_ OF((z_streamp strm, int level, int method,
1285  int windowBits, int memLevel,
1286  int strategy, const char *version,
1287  int stream_size));
1288 int inflateInit2_ OF((z_streamp strm, int windowBits,
1289  const char *version, int stream_size));
1290 int inflateBackInit_ OF((z_streamp strm, int windowBits,
1291  unsigned char FAR *window,
1292  const char *version,
1293  int stream_size));
1294 #define deflateInit(strm, level) \
1295  deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream))
1296 #define inflateInit(strm) \
1297  inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream))
1298 #define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
1299  deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
1300  (strategy), ZLIB_VERSION, sizeof(z_stream))
1301 #define inflateInit2(strm, windowBits) \
1302  inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
1303 #define inflateBackInit(strm, windowBits, window) \
1304  inflateBackInit_((strm), (windowBits), (window), \
1305  ZLIB_VERSION, sizeof(z_stream))
1306 
1307 
1308 const char * zError OF((int));
1309 int inflateSyncPoint OF((z_streamp z));
1310 const uLongf * get_crc_table OF((void));
1311 
1312 #ifdef __cplusplus
1313 }
1314 #endif
1315 
1316 
1317 /* zutil.h -- internal interface and configuration of the compression library
1318  * Copyright (C) 1995-2005 Jean-loup Gailly.
1319  * For conditions of distribution and use, see copyright notice in zlib.h
1320  */
1321 
1322 #ifdef STDC
1323 # ifndef _WIN32_WCE
1324 # include <stddef.h>
1325 # endif
1326 # include <string.h>
1327 # include <stdlib.h>
1328 #endif
1329 #ifdef NO_ERRNO_H
1330 # ifdef _WIN32_WCE
1331  /* The Microsoft C Run-Time Library for Windows CE doesn't have
1332  * errno. We define it as a global variable to simplify porting.
1333  * Its value is always 0 and should not be used. We rename it to
1334  * avoid conflict with other libraries that use the same workaround.
1335  */
1336 # define errno z_errno
1337 # endif
1338  extern int errno;
1339 #else
1340 # ifndef _WIN32_WCE
1341 # include <errno.h>
1342 # endif
1343 #endif
1344 
1345 #ifndef local
1346 # define local static
1347 #endif
1348 /* compile with -Dlocal if your debugger can't find static symbols */
1349 
1350 typedef unsigned char uch;
1351 typedef uch FAR uchf;
1352 typedef unsigned short ush;
1353 typedef ush FAR ushf;
1354 typedef unsigned long ulg;
1355 
1356 extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
1357 /* (size given to avoid silly warnings with Visual C++) */
1358 
1359 #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
1360 
1361 #define ERR_RETURN(strm,err) \
1362  return (strm->msg = (char*)ERR_MSG(err), (err))
1363 /* To be used only when the state is known to be valid */
1364 
1365  /* common constants */
1366 
1367 #ifndef DEF_WBITS
1368 # define DEF_WBITS MAX_WBITS
1369 #endif
1370 /* default windowBits for decompression. MAX_WBITS is for compression only */
1371 
1372 #if MAX_MEM_LEVEL >= 8
1373 # define DEF_MEM_LEVEL 8
1374 #else
1375 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
1376 #endif
1377 /* default memLevel */
1378 
1379 #define STORED_BLOCK 0
1380 #define STATIC_TREES 1
1381 #define DYN_TREES 2
1382 /* The three kinds of block type */
1383 
1384 #define MIN_MATCH 3
1385 #define MAX_MATCH 258
1386 /* The minimum and maximum match lengths */
1387 
1388 #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
1389 
1390  /* target dependencies */
1391 
1392 #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
1393 # define OS_CODE 0x00
1394 # if defined(__TURBOC__) || defined(__BORLANDC__)
1395 # if(__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
1396  /* Allow compilation with ANSI keywords only enabled */
1397  void _Cdecl farfree( void *block );
1398  void *_Cdecl farmalloc( unsigned long nbytes );
1399 # else
1400 # include <alloc.h>
1401 # endif
1402 # else /* MSC or DJGPP */
1403 # include <malloc.h>
1404 # endif
1405 #endif
1406 
1407 #ifdef AMIGA
1408 # define OS_CODE 0x01
1409 #endif
1410 
1411 #if defined(VAXC) || defined(VMS)
1412 # define OS_CODE 0x02
1413 # define F_OPEN(name, mode) \
1414  fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
1415 #endif
1416 
1417 #if defined(ATARI) || defined(atarist)
1418 # define OS_CODE 0x05
1419 #endif
1420 
1421 #ifdef OS2
1422 # define OS_CODE 0x06
1423 # ifdef M_I86
1424  #include <malloc.h>
1425 # endif
1426 #endif
1427 
1428 #if defined(MACOS) || defined(TARGET_OS_MAC)
1429 # define OS_CODE 0x07
1430 # if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
1431 # include <unix.h> /* for fdopen */
1432 # else
1433 # ifndef fdopen
1434 # define fdopen(fd,mode) NULL /* No fdopen() */
1435 # endif
1436 # endif
1437 #endif
1438 
1439 #ifdef TOPS20
1440 # define OS_CODE 0x0a
1441 #endif
1442 
1443 #ifdef WIN32
1444 # ifndef __CYGWIN__ /* Cygwin is Unix, not Win32 */
1445 # define OS_CODE 0x0b
1446 # endif
1447 #endif
1448 
1449 #ifdef __50SERIES /* Prime/PRIMOS */
1450 # define OS_CODE 0x0f
1451 #endif
1452 
1453 #if defined(_BEOS_) || defined(RISCOS)
1454 # define fdopen(fd,mode) NULL /* No fdopen() */
1455 #endif
1456 
1457 #if (defined(_MSC_VER) && (_MSC_VER > 600))
1458 # if defined(_WIN32_WCE)
1459 # define fdopen(fd,mode) NULL /* No fdopen() */
1460 # ifndef _PTRDIFF_T_DEFINED
1461  typedef int ptrdiff_t;
1462 # define _PTRDIFF_T_DEFINED
1463 # endif
1464 # else
1465 # define fdopen(fd,type) _fdopen(fd,type)
1466 # endif
1467 #endif
1468 
1469  /* common defaults */
1470 
1471 #ifndef OS_CODE
1472 # define OS_CODE 0x03 /* assume Unix */
1473 #endif
1474 
1475 #ifndef F_OPEN
1476 # define F_OPEN(name, mode) fopen((name), (mode))
1477 #endif
1478 
1479  /* functions */
1480 
1481 #if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
1482 # ifndef HAVE_VSNPRINTF
1483 # define HAVE_VSNPRINTF
1484 # endif
1485 #endif
1486 #if defined(__CYGWIN__)
1487 # ifndef HAVE_VSNPRINTF
1488 # define HAVE_VSNPRINTF
1489 # endif
1490 #endif
1491 #ifndef HAVE_VSNPRINTF
1492 # ifdef MSDOS
1493  /* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
1494  but for now we just assume it doesn't. */
1495 # define NO_vsnprintf
1496 # endif
1497 # ifdef __TURBOC__
1498 # define NO_vsnprintf
1499 # endif
1500 # ifdef WIN32
1501  /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
1502 # if !defined(vsnprintf) && !defined(NO_vsnprintf)
1503 # define vsnprintf _vsnprintf
1504 # endif
1505 # endif
1506 # ifdef __SASC
1507 # define NO_vsnprintf
1508 # endif
1509 #endif
1510 #ifdef VMS
1511 # define NO_vsnprintf
1512 #endif
1513 
1514 #if defined(pyr)
1515 # define NO_MEMCPY
1516 #endif
1517 #if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
1518  /* Use our own functions for small and medium model with MSC <= 5.0.
1519  * You may have to use the same strategy for Borland C (untested).
1520  * The __SC__ check is for Symantec.
1521  */
1522 # define NO_MEMCPY
1523 #endif
1524 #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
1525 # define HAVE_MEMCPY
1526 #endif
1527 #ifdef HAVE_MEMCPY
1528 # ifdef SMALL_MEDIUM /* MSDOS small or medium model */
1529 # define zmemcpy _fmemcpy
1530 # define zmemcmp _fmemcmp
1531 # define zmemzero(dest, len) _fmemset(dest, 0, len)
1532 # else
1533 # define zmemcpy memcpy
1534 # define zmemcmp memcmp
1535 # define zmemzero(dest, len) memset(dest, 0, len)
1536 # endif
1537 #else
1538  extern void zmemcpy OF((Bytef* dest, const Bytef* source, uInt len));
1539  extern int zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len));
1540  extern void zmemzero OF((Bytef* dest, uInt len));
1541 #endif
1542 
1543 /* Diagnostic functions */
1544 #ifdef DEBUG
1545 # include <stdio.h>
1546  extern int z_verbose;
1547  extern void z_error OF((char *m));
1548 # define Assert(cond,msg) {if(!(cond)) z_error(msg);}
1549 # define Trace(x) {if (z_verbose>=0) fprintf x ;}
1550 # define Tracev(x) {if (z_verbose>0) fprintf x ;}
1551 # define Tracevv(x) {if (z_verbose>1) fprintf x ;}
1552 # define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
1553 # define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
1554 #else
1555 # define Assert(cond,msg)
1556 # define Trace(x)
1557 # define Tracev(x)
1558 # define Tracevv(x)
1559 # define Tracec(c,x)
1560 # define Tracecv(c,x)
1561 #endif
1562 
1563 
1564 voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
1565 void zcfree OF((voidpf opaque, voidpf ptr));
1566 
1567 #define ZALLOC(strm, items, size) \
1568  (*((strm)->zalloc))((strm)->opaque, (items), (size))
1569 #define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
1570 #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
1571 
1572 
1573 /* zutil.c -- target dependent utility functions for the compression library
1574  * Copyright (C) 1995-2005 Jean-loup Gailly.
1575  * For conditions of distribution and use, see copyright notice in zlib.h
1576  */
1577 
1578 const char * const z_errmsg[10] = {
1579 "need dictionary", /* Z_NEED_DICT 2 */
1580 "stream end", /* Z_STREAM_END 1 */
1581 "", /* Z_OK 0 */
1582 "file error", /* Z_ERRNO (-1) */
1583 "stream error", /* Z_STREAM_ERROR (-2) */
1584 "data error", /* Z_DATA_ERROR (-3) */
1585 "insufficient memory", /* Z_MEM_ERROR (-4) */
1586 "buffer error", /* Z_BUF_ERROR (-5) */
1587 "incompatible version",/* Z_VERSION_ERROR (-6) */
1588 ""};
1589 
1590 #ifdef DEBUG
1591 
1592 # ifndef verbose
1593 # define verbose 0
1594 # endif
1595 int z_verbose = verbose;
1596 
1597 void z_error (m)
1598  char *m;
1599 {
1600  fprintf(stderr, "%s\n", m);
1601  exit(1);
1602 }
1603 #endif
1604 
1605 /* exported to allow conversion of error code to string for compress() and
1606  * uncompress()
1607  */
1608 const char * zError(err)
1609  int err;
1610 {
1611  return ERR_MSG(err);
1612 }
1613 
1614 #if defined(_WIN32_WCE)
1615  /* The Microsoft C Run-Time Library for Windows CE doesn't have
1616  * errno. We define it as a global variable to simplify porting.
1617  * Its value is always 0 and should not be used.
1618  */
1619  int errno = 0;
1620 #endif
1621 
1622 #ifndef HAVE_MEMCPY
1623 
1624 void zmemcpy(dest, source, len)
1625  Bytef* dest;
1626  const Bytef* source;
1627  uInt len;
1628 {
1629  if (len == 0) return;
1630  do {
1631  *dest++ = *source++; /* ??? to be unrolled */
1632  } while (--len != 0);
1633 }
1634 
1635 int zmemcmp(s1, s2, len)
1636  const Bytef* s1;
1637  const Bytef* s2;
1638  uInt len;
1639 {
1640  uInt j;
1641 
1642  for (j = 0; j < len; j++) {
1643  if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
1644  }
1645  return 0;
1646 }
1647 
1648 void zmemzero(dest, len)
1649  Bytef* dest;
1650  uInt len;
1651 {
1652  if (len == 0) return;
1653  do {
1654  *dest++ = 0; /* ??? to be unrolled */
1655  } while (--len != 0);
1656 }
1657 #endif
1658 
1659 
1660 #ifdef SYS16BIT
1661 
1662 #ifdef __TURBOC__
1663 /* Turbo C in 16-bit mode */
1664 
1665 # define MY_ZCALLOC
1666 
1667 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
1668  * and farmalloc(64K) returns a pointer with an offset of 8, so we
1669  * must fix the pointer. Warning: the pointer must be put back to its
1670  * original form in order to free it, use zcfree().
1671  */
1672 
1673 #define MAX_PTR 10
1674 /* 10*64K = 640K */
1675 
1676 local int next_ptr = 0;
1677 
1678 typedef struct ptr_table_s {
1679  voidpf org_ptr;
1680  voidpf new_ptr;
1681 } ptr_table;
1682 
1683 local ptr_table table[MAX_PTR];
1684 /* This table is used to remember the original form of pointers
1685  * to large buffers (64K). Such pointers are normalized with a zero offset.
1686  * Since MSDOS is not a preemptive multitasking OS, this table is not
1687  * protected from concurrent access. This hack doesn't work anyway on
1688  * a protected system like OS/2. Use Microsoft C instead.
1689  */
1690 
1691 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
1692 {
1693  voidpf buf = opaque; /* just to make some compilers happy */
1694  ulg bsize = (ulg)items*size;
1695 
1696  /* If we allocate less than 65520 bytes, we assume that farmalloc
1697  * will return a usable pointer which doesn't have to be normalized.
1698  */
1699  if (bsize < 65520L) {
1700  buf = farmalloc(bsize);
1701  if (*(ush*)&buf != 0) return buf;
1702  } else {
1703  buf = farmalloc(bsize + 16L);
1704  }
1705  if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
1706  table[next_ptr].org_ptr = buf;
1707 
1708  /* Normalize the pointer to seg:0 */
1709  *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
1710  *(ush*)&buf = 0;
1711  table[next_ptr++].new_ptr = buf;
1712  return buf;
1713 }
1714 
1715 void zcfree (voidpf opaque, voidpf ptr)
1716 {
1717  int n;
1718  if (*(ush*)&ptr != 0) { /* object < 64K */
1719  farfree(ptr);
1720  return;
1721  }
1722  /* Find the original pointer */
1723  for (n = 0; n < next_ptr; n++) {
1724  if (ptr != table[n].new_ptr) continue;
1725 
1726  farfree(table[n].org_ptr);
1727  while (++n < next_ptr) {
1728  table[n-1] = table[n];
1729  }
1730  next_ptr--;
1731  return;
1732  }
1733  ptr = opaque; /* just to make some compilers happy */
1734  Assert(0, "zcfree: ptr not found");
1735 }
1736 
1737 #endif /* __TURBOC__ */
1738 
1739 
1740 #ifdef M_I86
1741 /* Microsoft C in 16-bit mode */
1742 
1743 # define MY_ZCALLOC
1744 
1745 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
1746 # define _halloc halloc
1747 # define _hfree hfree
1748 #endif
1749 
1750 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
1751 {
1752  if (opaque) opaque = 0; /* to make compiler happy */
1753  return _halloc((long)items, size);
1754 }
1755 
1756 void zcfree (voidpf opaque, voidpf ptr)
1757 {
1758  if (opaque) opaque = 0; /* to make compiler happy */
1759  _hfree(ptr);
1760 }
1761 
1762 #endif /* M_I86 */
1763 
1764 #endif /* SYS16BIT */
1765 
1766 
1767 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
1768 
1769 #ifndef STDC
1770 extern voidp malloc OF((uInt size));
1771 extern voidp calloc OF((uInt items, uInt size));
1772 extern void free OF((voidpf ptr));
1773 #endif
1774 
1775 voidpf zcalloc (opaque, items, size)
1776  voidpf opaque;
1777  unsigned items;
1778  unsigned size;
1779 {
1780  if (opaque) items += size - size; /* make compiler happy */
1781  return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
1782  (voidpf)calloc(items, size);
1783 }
1784 
1785 void zcfree (opaque, ptr)
1786  voidpf opaque;
1787  voidpf ptr;
1788 {
1789  free(ptr);
1790  if (opaque) return; /* make compiler happy */
1791 }
1792 
1793 #endif /* MY_ZCALLOC */
1794 
1795 /* deflate.h -- internal compression state
1796  * Copyright (C) 1995-2004 Jean-loup Gailly
1797  * For conditions of distribution and use, see copyright notice in zlib.h
1798  */
1799 
1800 /* ===========================================================================
1801  * Internal compression state.
1802  */
1803 
1804 #define LENGTH_CODES 29
1805 /* number of length codes, not counting the special END_BLOCK code */
1806 
1807 #define LITERALS 256
1808 /* number of literal bytes 0..255 */
1809 
1810 #define L_CODES (LITERALS+1+LENGTH_CODES)
1811 /* number of Literal or Length codes, including the END_BLOCK code */
1812 
1813 #define D_CODES 30
1814 /* number of distance codes */
1815 
1816 #define BL_CODES 19
1817 /* number of codes used to transfer the bit lengths */
1818 
1819 #define HEAP_SIZE (2*L_CODES+1)
1820 /* maximum heap size */
1821 
1822 #define MAX_BITS 15
1823 /* All codes must not exceed MAX_BITS bits */
1824 
1825 #define INIT_STATE 42
1826 #define EXTRA_STATE 69
1827 #define NAME_STATE 73
1828 #define COMMENT_STATE 91
1829 #define HCRC_STATE 103
1830 #define BUSY_STATE 113
1831 #define FINISH_STATE 666
1832 /* Stream status */
1833 
1834 
1835 /* Data structure describing a single value and its code string. */
1836 typedef struct ct_data_s {
1837  union {
1838  ush freq; /* frequency count */
1839  ush code; /* bit string */
1840  } fc;
1841  union {
1842  ush dad; /* father node in Huffman tree */
1843  ush len; /* length of bit string */
1844  } dl;
1845 } FAR ct_data;
1846 
1847 #define Freq fc.freq
1848 #define Code fc.code
1849 #define Dad dl.dad
1850 #define Len dl.len
1851 
1852 typedef struct static_tree_desc_s static_tree_desc;
1853 
1854 typedef struct tree_desc_s {
1855  ct_data *dyn_tree; /* the dynamic tree */
1856  int max_code; /* largest code with non zero frequency */
1857  static_tree_desc *stat_desc; /* the corresponding static tree */
1858 } FAR tree_desc;
1859 
1860 typedef ush Pos;
1861 typedef Pos FAR Posf;
1862 typedef unsigned IPos;
1863 
1864 /* A Pos is an index in the character window. We use short instead of int to
1865  * save space in the various tables. IPos is used only for parameter passing.
1866  */
1867 
1868 typedef struct internal_state {
1869  z_streamp strm; /* pointer back to this zlib stream */
1870  int status; /* as the name implies */
1871  Bytef *pending_buf; /* output still pending */
1872  ulg pending_buf_size; /* size of pending_buf */
1873  Bytef *pending_out; /* next pending byte to output to the stream */
1874  uInt pending; /* nb of bytes in the pending buffer */
1875  int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
1876  gz_headerp gzhead; /* gzip header information to write */
1877  uInt gzindex; /* where in extra, name, or comment */
1878  Byte method; /* STORED (for zip only) or DEFLATED */
1879  int last_flush; /* value of flush param for previous deflate call */
1880 
1881  /* used by deflate.c: */
1882 
1883  uInt w_size; /* LZ77 window size (32K by default) */
1884  uInt w_bits; /* log2(w_size) (8..16) */
1885  uInt w_mask; /* w_size - 1 */
1886 
1887  Bytef *window;
1888  /* Sliding window. Input bytes are read into the second half of the window,
1889  * and move to the first half later to keep a dictionary of at least wSize
1890  * bytes. With this organization, matches are limited to a distance of
1891  * wSize-MAX_MATCH bytes, but this ensures that IO is always
1892  * performed with a length multiple of the block size. Also, it limits
1893  * the window size to 64K, which is quite useful on MSDOS.
1894  * To do: use the user input buffer as sliding window.
1895  */
1896 
1897  ulg window_size;
1898  /* Actual size of window: 2*wSize, except when the user input buffer
1899  * is directly used as sliding window.
1900  */
1901 
1902  Posf *prev;
1903  /* Link to older string with same hash index. To limit the size of this
1904  * array to 64K, this link is maintained only for the last 32K strings.
1905  * An index in this array is thus a window index modulo 32K.
1906  */
1907 
1908  Posf *head; /* Heads of the hash chains or NIL. */
1909 
1910  uInt ins_h; /* hash index of string to be inserted */
1911  uInt hash_size; /* number of elements in hash table */
1912  uInt hash_bits; /* log2(hash_size) */
1913  uInt hash_mask; /* hash_size-1 */
1914 
1915  uInt hash_shift;
1916  /* Number of bits by which ins_h must be shifted at each input
1917  * step. It must be such that after MIN_MATCH steps, the oldest
1918  * byte no longer takes part in the hash key, that is:
1919  * hash_shift * MIN_MATCH >= hash_bits
1920  */
1921 
1922  long block_start;
1923  /* Window position at the beginning of the current output block. Gets
1924  * negative when the window is moved backwards.
1925  */
1926 
1927  uInt match_length; /* length of best match */
1928  IPos prev_match; /* previous match */
1929  int match_available; /* set if previous match exists */
1930  uInt strstart; /* start of string to insert */
1931  uInt match_start; /* start of matching string */
1932  uInt lookahead; /* number of valid bytes ahead in window */
1933 
1934  uInt prev_length;
1935  /* Length of the best match at previous step. Matches not greater than this
1936  * are discarded. This is used in the lazy match evaluation.
1937  */
1938 
1939  uInt max_chain_length;
1940  /* To speed up deflation, hash chains are never searched beyond this
1941  * length. A higher limit improves compression ratio but degrades the
1942  * speed.
1943  */
1944 
1945  uInt max_lazy_match;
1946  /* Attempt to find a better match only when the current match is strictly
1947  * smaller than this value. This mechanism is used only for compression
1948  * levels >= 4.
1949  */
1950 # define max_insert_length max_lazy_match
1951  /* Insert new strings in the hash table only if the match length is not
1952  * greater than this length. This saves time but degrades compression.
1953  * max_insert_length is used only for compression levels <= 3.
1954  */
1955 
1956  int level; /* compression level (1..9) */
1957  int strategy; /* favor or force Huffman coding*/
1958 
1959  uInt good_match;
1960  /* Use a faster search when the previous match is longer than this */
1961 
1962  int nice_match; /* Stop searching when current match exceeds this */
1963 
1964  /* used by trees.c: */
1965  /* Didn't use ct_data typedef below to supress compiler warning */
1966  struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
1967  struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
1968  struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
1969 
1970  struct tree_desc_s l_desc; /* desc. for literal tree */
1971  struct tree_desc_s d_desc; /* desc. for distance tree */
1972  struct tree_desc_s bl_desc; /* desc. for bit length tree */
1973 
1974  ush bl_count[MAX_BITS+1];
1975  /* number of codes at each bit length for an optimal tree */
1976 
1977  int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
1978  int heap_len; /* number of elements in the heap */
1979  int heap_max; /* element of largest frequency */
1980  /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
1981  * The same heap array is used to build all trees.
1982  */
1983 
1984  uch depth[2*L_CODES+1];
1985  /* Depth of each subtree used as tie breaker for trees of equal frequency
1986  */
1987 
1988  uchf *l_buf; /* buffer for literals or lengths */
1989 
1990  uInt lit_bufsize;
1991  /* Size of match buffer for literals/lengths. There are 4 reasons for
1992  * limiting lit_bufsize to 64K:
1993  * - frequencies can be kept in 16 bit counters
1994  * - if compression is not successful for the first block, all input
1995  * data is still in the window so we can still emit a stored block even
1996  * when input comes from standard input. (This can also be done for
1997  * all blocks if lit_bufsize is not greater than 32K.)
1998  * - if compression is not successful for a file smaller than 64K, we can
1999  * even emit a stored file instead of a stored block (saving 5 bytes).
2000  * This is applicable only for zip (not gzip or zlib).
2001  * - creating new Huffman trees less frequently may not provide fast
2002  * adaptation to changes in the input data statistics. (Take for
2003  * example a binary file with poorly compressible code followed by
2004  * a highly compressible string table.) Smaller buffer sizes give
2005  * fast adaptation but have of course the overhead of transmitting
2006  * trees more frequently.
2007  * - I can't count above 4
2008  */
2009 
2010  uInt last_lit; /* running index in l_buf */
2011 
2012  ushf *d_buf;
2013  /* Buffer for distances. To simplify the code, d_buf and l_buf have
2014  * the same number of elements. To use different lengths, an extra flag
2015  * array would be necessary.
2016  */
2017 
2018  ulg opt_len; /* bit length of current block with optimal trees */
2019  ulg static_len; /* bit length of current block with static trees */
2020  uInt matches; /* number of string matches in current block */
2021  int last_eob_len; /* bit length of EOB code for last block */
2022 
2023 #ifdef DEBUG
2024  ulg compressed_len; /* total bit length of compressed file mod 2^32 */
2025  ulg bits_sent; /* bit length of compressed data sent mod 2^32 */
2026 #endif
2027 
2028  ush bi_buf;
2029  /* Output buffer. bits are inserted starting at the bottom (least
2030  * significant bits).
2031  */
2032  int bi_valid;
2033  /* Number of valid bits in bi_buf. All bits above the last valid bit
2034  * are always zero.
2035  */
2036 
2037 } FAR deflate_state;
2038 
2039 /* Output a byte on the stream.
2040  * IN assertion: there is enough room in pending_buf.
2041  */
2042 #define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
2043 
2044 
2045 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
2046 /* Minimum amount of lookahead, except at the end of the input file.
2047  * See deflate.c for comments about the MIN_MATCH+1.
2048  */
2049 
2050 #define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
2051 /* In order to simplify the code, particularly on 16 bit machines, match
2052  * distances are limited to MAX_DIST instead of WSIZE.
2053  */
2054 
2055  /* in trees.c */
2056 void _tr_init OF((deflate_state *s));
2057 int _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
2058 void _tr_flush_block OF((deflate_state *s, charf *buf, ulg stored_len,
2059  int eof));
2060 void _tr_align OF((deflate_state *s));
2061 void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
2062  int eof));
2063 
2064 #define d_code(dist) \
2065  ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
2066 /* Mapping from a distance to a distance code. dist is the distance - 1 and
2067  * must not have side effects. _dist_code[256] and _dist_code[257] are never
2068  * used.
2069  */
2070 
2071 #ifndef DEBUG
2072 /* Inline versions of _tr_tally for speed: */
2073 
2074 #if !defined(STDC)
2075  extern uch _length_code[];
2076  extern uch _dist_code[];
2077 #else
2078  extern const uch _length_code[];
2079  extern const uch _dist_code[];
2080 #endif
2081 
2082 # define _tr_tally_lit(s, c, flush) \
2083  { uch cc = (c); \
2084  s->d_buf[s->last_lit] = 0; \
2085  s->l_buf[s->last_lit++] = cc; \
2086  s->dyn_ltree[cc].Freq++; \
2087  flush = (s->last_lit == s->lit_bufsize-1); \
2088  }
2089 # define _tr_tally_dist(s, distance, length, flush) \
2090  { uch len = (length); \
2091  ush dist = (distance); \
2092  s->d_buf[s->last_lit] = dist; \
2093  s->l_buf[s->last_lit++] = len; \
2094  dist--; \
2095  s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
2096  s->dyn_dtree[d_code(dist)].Freq++; \
2097  flush = (s->last_lit == s->lit_bufsize-1); \
2098  }
2099 #else
2100 # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
2101 # define _tr_tally_dist(s, distance, length, flush) \
2102  flush = _tr_tally(s, distance, length)
2103 #endif
2104 
2105 
2106 /* trees.c -- output deflated data using Huffman coding
2107  * Copyright (C) 1995-2005 Jean-loup Gailly
2108  * For conditions of distribution and use, see copyright notice in zlib.h
2109  */
2110 
2111 /*
2112  * ALGORITHM
2113  *
2114  * The "deflation" process uses several Huffman trees. The more
2115  * common source values are represented by shorter bit sequences.
2116  *
2117  * Each code tree is stored in a compressed form which is itself
2118  * a Huffman encoding of the lengths of all the code strings (in
2119  * ascending order by source values). The actual code strings are
2120  * reconstructed from the lengths in the inflate process, as described
2121  * in the deflate specification.
2122  *
2123  * REFERENCES
2124  *
2125  * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
2126  * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
2127  *
2128  * Storer, James A.
2129  * Data Compression: Methods and Theory, pp. 49-50.
2130  * Computer Science Press, 1988. ISBN 0-7167-8156-5.
2131  *
2132  * Sedgewick, R.
2133  * Algorithms, p290.
2134  * Addison-Wesley, 1983. ISBN 0-201-06672-6.
2135  */
2136 
2137 #ifdef DEBUG
2138 # include <ctype.h>
2139 #endif
2140 
2141 /* ===========================================================================
2142  * Constants
2143  */
2144 
2145 #define MAX_BL_BITS 7
2146 /* Bit length codes must not exceed MAX_BL_BITS bits */
2147 
2148 #define END_BLOCK 256
2149 /* end of block literal code */
2150 
2151 #define REP_3_6 16
2152 /* repeat previous bit length 3-6 times (2 bits of repeat count) */
2153 
2154 #define REPZ_3_10 17
2155 /* repeat a zero length 3-10 times (3 bits of repeat count) */
2156 
2157 #define REPZ_11_138 18
2158 /* repeat a zero length 11-138 times (7 bits of repeat count) */
2159 
2160 local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
2161  = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
2162 
2163 local const int extra_dbits[D_CODES] /* extra bits for each distance code */
2164  = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
2165 
2166 local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */
2167  = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
2168 
2169 local const uch bl_order[BL_CODES]
2170  = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
2171 /* The lengths of the bit length codes are sent in order of decreasing
2172  * probability, to avoid transmitting the lengths for unused bit length codes.
2173  */
2174 
2175 #define Buf_size (8 * 2*sizeof(char))
2176 /* Number of bits used within bi_buf. (bi_buf might be implemented on
2177  * more than 16 bits on some systems.)
2178  */
2179 
2180 /* ===========================================================================
2181  * Local data. These are initialized only once.
2182  */
2183 
2184 #define DIST_CODE_LEN 512 /* see definition of array dist_code below */
2185 
2186 #if !defined(STDC)
2187 /* non ANSI compilers may not accept trees.h */
2188 
2189 local ct_data static_ltree[L_CODES+2];
2190 /* The static literal tree. Since the bit lengths are imposed, there is no
2191  * need for the L_CODES extra codes used during heap construction. However
2192  * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
2193  * below).
2194  */
2195 
2196 local ct_data static_dtree[D_CODES];
2197 /* The static distance tree. (Actually a trivial tree since all codes use
2198  * 5 bits.)
2199  */
2200 
2201 uch _dist_code[DIST_CODE_LEN];
2202 /* Distance codes. The first 256 values correspond to the distances
2203  * 3 .. 258, the last 256 values correspond to the top 8 bits of
2204  * the 15 bit distances.
2205  */
2206 
2207 uch _length_code[MAX_MATCH-MIN_MATCH+1];
2208 /* length code for each normalized match length (0 == MIN_MATCH) */
2209 
2210 local int base_length[LENGTH_CODES];
2211 /* First normalized length for each code (0 = MIN_MATCH) */
2212 
2213 local int base_dist[D_CODES];
2214 /* First normalized distance for each code (0 = distance of 1) */
2215 
2216 #else
2217 
2218 local const ct_data static_ltree[L_CODES+2] = {
2219 {{ 12},{ 8}}, {{140},{ 8}}, {{ 76},{ 8}}, {{204},{ 8}}, {{ 44},{ 8}},
2220 {{172},{ 8}}, {{108},{ 8}}, {{236},{ 8}}, {{ 28},{ 8}}, {{156},{ 8}},
2221 {{ 92},{ 8}}, {{220},{ 8}}, {{ 60},{ 8}}, {{188},{ 8}}, {{124},{ 8}},
2222 {{252},{ 8}}, {{ 2},{ 8}}, {{130},{ 8}}, {{ 66},{ 8}}, {{194},{ 8}},
2223 {{ 34},{ 8}}, {{162},{ 8}}, {{ 98},{ 8}}, {{226},{ 8}}, {{ 18},{ 8}},
2224 {{146},{ 8}}, {{ 82},{ 8}}, {{210},{ 8}}, {{ 50},{ 8}}, {{178},{ 8}},
2225 {{114},{ 8}}, {{242},{ 8}}, {{ 10},{ 8}}, {{138},{ 8}}, {{ 74},{ 8}},
2226 {{202},{ 8}}, {{ 42},{ 8}}, {{170},{ 8}}, {{106},{ 8}}, {{234},{ 8}},
2227 {{ 26},{ 8}}, {{154},{ 8}}, {{ 90},{ 8}}, {{218},{ 8}}, {{ 58},{ 8}},
2228 {{186},{ 8}}, {{122},{ 8}}, {{250},{ 8}}, {{ 6},{ 8}}, {{134},{ 8}},
2229 {{ 70},{ 8}}, {{198},{ 8}}, {{ 38},{ 8}}, {{166},{ 8}}, {{102},{ 8}},
2230 {{230},{ 8}}, {{ 22},{ 8}}, {{150},{ 8}}, {{ 86},{ 8}}, {{214},{ 8}},
2231 {{ 54},{ 8}}, {{182},{ 8}}, {{118},{ 8}}, {{246},{ 8}}, {{ 14},{ 8}},
2232 {{142},{ 8}}, {{ 78},{ 8}}, {{206},{ 8}}, {{ 46},{ 8}}, {{174},{ 8}},
2233 {{110},{ 8}}, {{238},{ 8}}, {{ 30},{ 8}}, {{158},{ 8}}, {{ 94},{ 8}},
2234 {{222},{ 8}}, {{ 62},{ 8}}, {{190},{ 8}}, {{126},{ 8}}, {{254},{ 8}},
2235 {{ 1},{ 8}}, {{129},{ 8}}, {{ 65},{ 8}}, {{193},{ 8}}, {{ 33},{ 8}},
2236 {{161},{ 8}}, {{ 97},{ 8}}, {{225},{ 8}}, {{ 17},{ 8}}, {{145},{ 8}},
2237 {{ 81},{ 8}}, {{209},{ 8}}, {{ 49},{ 8}}, {{177},{ 8}}, {{113},{ 8}},
2238 {{241},{ 8}}, {{ 9},{ 8}}, {{137},{ 8}}, {{ 73},{ 8}}, {{201},{ 8}},
2239 {{ 41},{ 8}}, {{169},{ 8}}, {{105},{ 8}}, {{233},{ 8}}, {{ 25},{ 8}},
2240 {{153},{ 8}}, {{ 89},{ 8}}, {{217},{ 8}}, {{ 57},{ 8}}, {{185},{ 8}},
2241 {{121},{ 8}}, {{249},{ 8}}, {{ 5},{ 8}}, {{133},{ 8}}, {{ 69},{ 8}},
2242 {{197},{ 8}}, {{ 37},{ 8}}, {{165},{ 8}}, {{101},{ 8}}, {{229},{ 8}},
2243 {{ 21},{ 8}}, {{149},{ 8}}, {{ 85},{ 8}}, {{213},{ 8}}, {{ 53},{ 8}},
2244 {{181},{ 8}}, {{117},{ 8}}, {{245},{ 8}}, {{ 13},{ 8}}, {{141},{ 8}},
2245 {{ 77},{ 8}}, {{205},{ 8}}, {{ 45},{ 8}}, {{173},{ 8}}, {{109},{ 8}},
2246 {{237},{ 8}}, {{ 29},{ 8}}, {{157},{ 8}}, {{ 93},{ 8}}, {{221},{ 8}},
2247 {{ 61},{ 8}}, {{189},{ 8}}, {{125},{ 8}}, {{253},{ 8}}, {{ 19},{ 9}},
2248 {{275},{ 9}}, {{147},{ 9}}, {{403},{ 9}}, {{ 83},{ 9}}, {{339},{ 9}},
2249 {{211},{ 9}}, {{467},{ 9}}, {{ 51},{ 9}}, {{307},{ 9}}, {{179},{ 9}},
2250 {{435},{ 9}}, {{115},{ 9}}, {{371},{ 9}}, {{243},{ 9}}, {{499},{ 9}},
2251 {{ 11},{ 9}}, {{267},{ 9}}, {{139},{ 9}}, {{395},{ 9}}, {{ 75},{ 9}},
2252 {{331},{ 9}}, {{203},{ 9}}, {{459},{ 9}}, {{ 43},{ 9}}, {{299},{ 9}},
2253 {{171},{ 9}}, {{427},{ 9}}, {{107},{ 9}}, {{363},{ 9}}, {{235},{ 9}},
2254 {{491},{ 9}}, {{ 27},{ 9}}, {{283},{ 9}}, {{155},{ 9}}, {{411},{ 9}},
2255 {{ 91},{ 9}}, {{347},{ 9}}, {{219},{ 9}}, {{475},{ 9}}, {{ 59},{ 9}},
2256 {{315},{ 9}}, {{187},{ 9}}, {{443},{ 9}}, {{123},{ 9}}, {{379},{ 9}},
2257 {{251},{ 9}}, {{507},{ 9}}, {{ 7},{ 9}}, {{263},{ 9}}, {{135},{ 9}},
2258 {{391},{ 9}}, {{ 71},{ 9}}, {{327},{ 9}}, {{199},{ 9}}, {{455},{ 9}},
2259 {{ 39},{ 9}}, {{295},{ 9}}, {{167},{ 9}}, {{423},{ 9}}, {{103},{ 9}},
2260 {{359},{ 9}}, {{231},{ 9}}, {{487},{ 9}}, {{ 23},{ 9}}, {{279},{ 9}},
2261 {{151},{ 9}}, {{407},{ 9}}, {{ 87},{ 9}}, {{343},{ 9}}, {{215},{ 9}},
2262 {{471},{ 9}}, {{ 55},{ 9}}, {{311},{ 9}}, {{183},{ 9}}, {{439},{ 9}},
2263 {{119},{ 9}}, {{375},{ 9}}, {{247},{ 9}}, {{503},{ 9}}, {{ 15},{ 9}},
2264 {{271},{ 9}}, {{143},{ 9}}, {{399},{ 9}}, {{ 79},{ 9}}, {{335},{ 9}},
2265 {{207},{ 9}}, {{463},{ 9}}, {{ 47},{ 9}}, {{303},{ 9}}, {{175},{ 9}},
2266 {{431},{ 9}}, {{111},{ 9}}, {{367},{ 9}}, {{239},{ 9}}, {{495},{ 9}},
2267 {{ 31},{ 9}}, {{287},{ 9}}, {{159},{ 9}}, {{415},{ 9}}, {{ 95},{ 9}},
2268 {{351},{ 9}}, {{223},{ 9}}, {{479},{ 9}}, {{ 63},{ 9}}, {{319},{ 9}},
2269 {{191},{ 9}}, {{447},{ 9}}, {{127},{ 9}}, {{383},{ 9}}, {{255},{ 9}},
2270 {{511},{ 9}}, {{ 0},{ 7}}, {{ 64},{ 7}}, {{ 32},{ 7}}, {{ 96},{ 7}},
2271 {{ 16},{ 7}}, {{ 80},{ 7}}, {{ 48},{ 7}}, {{112},{ 7}}, {{ 8},{ 7}},
2272 {{ 72},{ 7}}, {{ 40},{ 7}}, {{104},{ 7}}, {{ 24},{ 7}}, {{ 88},{ 7}},
2273 {{ 56},{ 7}}, {{120},{ 7}}, {{ 4},{ 7}}, {{ 68},{ 7}}, {{ 36},{ 7}},
2274 {{100},{ 7}}, {{ 20},{ 7}}, {{ 84},{ 7}}, {{ 52},{ 7}}, {{116},{ 7}},
2275 {{ 3},{ 8}}, {{131},{ 8}}, {{ 67},{ 8}}, {{195},{ 8}}, {{ 35},{ 8}},
2276 {{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}}
2277 };
2278 
2279 local const ct_data static_dtree[D_CODES] = {
2280 {{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}},
2281 {{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}},
2282 {{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}},
2283 {{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}},
2284 {{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}},
2285 {{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}}
2286 };
2287 
2288 const uch _dist_code[DIST_CODE_LEN] = {
2289  0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
2290  8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
2291 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
2292 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
2293 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
2294 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
2295 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
2296 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
2297 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
2298 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
2299 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
2300 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
2301 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
2302 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
2303 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
2304 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
2305 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
2306 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
2307 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
2308 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
2309 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
2310 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
2311 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
2312 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
2313 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
2314 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
2315 };
2316 
2317 const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {
2318  0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
2319 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
2320 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
2321 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
2322 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
2323 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
2324 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
2325 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
2326 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
2327 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
2328 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
2329 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
2330 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
2331 };
2332 
2333 local const int base_length[LENGTH_CODES] = {
2334 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
2335 64, 80, 96, 112, 128, 160, 192, 224, 0
2336 };
2337 
2338 local const int base_dist[D_CODES] = {
2339  0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
2340  32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
2341  1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
2342 };
2343 
2344 #endif
2345 
2346 struct static_tree_desc_s {
2347  const ct_data *static_tree; /* static tree or NULL */
2348  const intf *extra_bits; /* extra bits for each code or NULL */
2349  int extra_base; /* base index for extra_bits */
2350  int elems; /* max number of elements in the tree */
2351  int max_length; /* max bit length for the codes */
2352 };
2353 
2354 local static_tree_desc static_l_desc =
2355 {static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
2356 
2357 local static_tree_desc static_d_desc =
2358 {static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
2359 
2360 local static_tree_desc static_bl_desc =
2361 {(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
2362 
2363 /* ===========================================================================
2364  * Local (static) routines in this file.
2365  */
2366 
2367 local void tr_static_init OF((void));
2368 local void init_block OF((deflate_state *s));
2369 local void pqdownheap OF((deflate_state *s, ct_data *tree, int k));
2370 local void gen_bitlen OF((deflate_state *s, tree_desc *desc));
2371 local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count));
2372 local void build_tree OF((deflate_state *s, tree_desc *desc));
2373 local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code));
2374 local void send_tree OF((deflate_state *s, ct_data *tree, int max_code));
2375 local int build_bl_tree OF((deflate_state *s));
2376 local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
2377  int blcodes));
2378 local void compress_block OF((deflate_state *s, ct_data *ltree,
2379  ct_data *dtree));
2380 local void set_data_type OF((deflate_state *s));
2381 local unsigned bi_reverse OF((unsigned value, int length));
2382 local void bi_windup OF((deflate_state *s));
2383 local void bi_flush OF((deflate_state *s));
2384 local void copy_block OF((deflate_state *s, charf *buf, unsigned len,
2385  int header));
2386 
2387 #ifndef DEBUG
2388 # define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
2389  /* Send a code of the given tree. c and tree must not have side effects */
2390 
2391 #else /* DEBUG */
2392 # define send_code(s, c, tree) \
2393  { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
2394  send_bits(s, tree[c].Code, tree[c].Len); }
2395 #endif
2396 
2397 /* ===========================================================================
2398  * Output a short LSB first on the stream.
2399  * IN assertion: there is enough room in pendingBuf.
2400  */
2401 #define put_short(s, w) { \
2402  put_byte(s, (uch)((w) & 0xff)); \
2403  put_byte(s, (uch)((ush)(w) >> 8)); \
2404 }
2405 
2406 /* ===========================================================================
2407  * Send a value on a given number of bits.
2408  * IN assertion: length <= 16 and value fits in length bits.
2409  */
2410 #ifdef DEBUG
2411 local void send_bits OF((deflate_state *s, int value, int length));
2412 
2413 local void send_bits(s, value, length)
2414  deflate_state *s;
2415  int value; /* value to send */
2416  int length; /* number of bits */
2417 {
2418  Tracevv((stderr," l %2d v %4x ", length, value));
2419  Assert(length > 0 && length <= 15, "invalid length");
2420  s->bits_sent += (ulg)length;
2421 
2422  /* If not enough room in bi_buf, use (valid) bits from bi_buf and
2423  * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
2424  * unused bits in value.
2425  */
2426  if (s->bi_valid > (int)Buf_size - length) {
2427  s->bi_buf |= (value << s->bi_valid);
2428  put_short(s, s->bi_buf);
2429  s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
2430  s->bi_valid += length - Buf_size;
2431  } else {
2432  s->bi_buf |= value << s->bi_valid;
2433  s->bi_valid += length;
2434  }
2435 }
2436 #else /* !DEBUG */
2437 
2438 #define send_bits(s, value, length) \
2439 { int len = length;\
2440  if (s->bi_valid > (int)Buf_size - len) {\
2441  int val = value;\
2442  s->bi_buf |= (val << s->bi_valid);\
2443  put_short(s, s->bi_buf);\
2444  s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
2445  s->bi_valid += len - Buf_size;\
2446  } else {\
2447  s->bi_buf |= (value) << s->bi_valid;\
2448  s->bi_valid += len;\
2449  }\
2450 }
2451 #endif /* DEBUG */
2452 
2453 
2454 /* the arguments must not have side effects */
2455 
2456 /* ===========================================================================
2457  * Initialize the various 'constant' tables.
2458  */
2459 local void tr_static_init()
2460 {
2461 #if !defined(STDC)
2462  static int static_init_done = 0;
2463  int n; /* iterates over tree elements */
2464  int bits; /* bit counter */
2465  int length; /* length value */
2466  int code; /* code value */
2467  int dist; /* distance index */
2468  ush bl_count[MAX_BITS+1];
2469  /* number of codes at each bit length for an optimal tree */
2470 
2471  if (static_init_done) return;
2472 
2473  /* For some embedded targets, global variables are not initialized: */
2474  static_l_desc.static_tree = static_ltree;
2475  static_l_desc.extra_bits = extra_lbits;
2476  static_d_desc.static_tree = static_dtree;
2477  static_d_desc.extra_bits = extra_dbits;
2478  static_bl_desc.extra_bits = extra_blbits;
2479 
2480  /* Initialize the mapping length (0..255) -> length code (0..28) */
2481  length = 0;
2482  for (code = 0; code < LENGTH_CODES-1; code++) {
2483  base_length[code] = length;
2484  for (n = 0; n < (1<<extra_lbits[code]); n++) {
2485  _length_code[length++] = (uch)code;
2486  }
2487  }
2488  Assert (length == 256, "tr_static_init: length != 256");
2489  /* Note that the length 255 (match length 258) can be represented
2490  * in two different ways: code 284 + 5 bits or code 285, so we
2491  * overwrite length_code[255] to use the best encoding:
2492  */
2493  _length_code[length-1] = (uch)code;
2494 
2495  /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
2496  dist = 0;
2497  for (code = 0 ; code < 16; code++) {
2498  base_dist[code] = dist;
2499  for (n = 0; n < (1<<extra_dbits[code]); n++) {
2500  _dist_code[dist++] = (uch)code;
2501  }
2502  }
2503  Assert (dist == 256, "tr_static_init: dist != 256");
2504  dist >>= 7; /* from now on, all distances are divided by 128 */
2505  for ( ; code < D_CODES; code++) {
2506  base_dist[code] = dist << 7;
2507  for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
2508  _dist_code[256 + dist++] = (uch)code;
2509  }
2510  }
2511  Assert (dist == 256, "tr_static_init: 256+dist != 512");
2512 
2513  /* Construct the codes of the static literal tree */
2514  for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
2515  n = 0;
2516  while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
2517  while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
2518  while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
2519  while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
2520  /* Codes 286 and 287 do not exist, but we must include them in the
2521  * tree construction to get a canonical Huffman tree (longest code
2522  * all ones)
2523  */
2524  gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
2525 
2526  /* The static distance tree is trivial: */
2527  for (n = 0; n < D_CODES; n++) {
2528  static_dtree[n].Len = 5;
2529  static_dtree[n].Code = bi_reverse((unsigned)n, 5);
2530  }
2531  static_init_done = 1;
2532 
2533 #endif /* !defined(STDC) */
2534 }
2535 
2536 /* ===========================================================================
2537  * Initialize the tree data structures for a new zlib stream.
2538  */
2539 void _tr_init(s)
2540  deflate_state *s;
2541 {
2542  tr_static_init();
2543 
2544  s->l_desc.dyn_tree = s->dyn_ltree;
2545  s->l_desc.stat_desc = &static_l_desc;
2546 
2547  s->d_desc.dyn_tree = s->dyn_dtree;
2548  s->d_desc.stat_desc = &static_d_desc;
2549 
2550  s->bl_desc.dyn_tree = s->bl_tree;
2551  s->bl_desc.stat_desc = &static_bl_desc;
2552 
2553  s->bi_buf = 0;
2554  s->bi_valid = 0;
2555  s->last_eob_len = 8; /* enough lookahead for inflate */
2556 #ifdef DEBUG
2557  s->compressed_len = 0L;
2558  s->bits_sent = 0L;
2559 #endif
2560 
2561  /* Initialize the first block of the first file: */
2562  init_block(s);
2563 }
2564 
2565 /* ===========================================================================
2566  * Initialize a new block.
2567  */
2568 local void init_block(s)
2569  deflate_state *s;
2570 {
2571  int n; /* iterates over tree elements */
2572 
2573  /* Initialize the trees. */
2574  for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
2575  for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
2576  for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
2577 
2578  s->dyn_ltree[END_BLOCK].Freq = 1;
2579  s->opt_len = s->static_len = 0L;
2580  s->last_lit = s->matches = 0;
2581 }
2582 
2583 #define SMALLEST 1
2584 /* Index within the heap array of least frequent node in the Huffman tree */
2585 
2586 
2587 /* ===========================================================================
2588  * Remove the smallest element from the heap and recreate the heap with
2589  * one less element. Updates heap and heap_len.
2590  */
2591 #define pqremove(s, tree, top) \
2592 {\
2593  top = s->heap[SMALLEST]; \
2594  s->heap[SMALLEST] = s->heap[s->heap_len--]; \
2595  pqdownheap(s, tree, SMALLEST); \
2596 }
2597 
2598 /* ===========================================================================
2599  * Compares to subtrees, using the tree depth as tie breaker when
2600  * the subtrees have equal frequency. This minimizes the worst case length.
2601  */
2602 #define smaller(tree, n, m, depth) \
2603  (tree[n].Freq < tree[m].Freq || \
2604  (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
2605 
2606 /* ===========================================================================
2607  * Restore the heap property by moving down the tree starting at node k,
2608  * exchanging a node with the smallest of its two sons if necessary, stopping
2609  * when the heap property is re-established (each father smaller than its
2610  * two sons).
2611  */
2612 local void pqdownheap(s, tree, k)
2613  deflate_state *s;
2614  ct_data *tree; /* the tree to restore */
2615  int k; /* node to move down */
2616 {
2617  int v = s->heap[k];
2618  int j = k << 1; /* left son of k */
2619  while (j <= s->heap_len) {
2620  /* Set j to the smallest of the two sons: */
2621  if (j < s->heap_len &&
2622  smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
2623  j++;
2624  }
2625  /* Exit if v is smaller than both sons */
2626  if (smaller(tree, v, s->heap[j], s->depth)) break;
2627 
2628  /* Exchange v with the smallest son */
2629  s->heap[k] = s->heap[j]; k = j;
2630 
2631  /* And continue down the tree, setting j to the left son of k */
2632  j <<= 1;
2633  }
2634  s->heap[k] = v;
2635 }
2636 
2637 /* ===========================================================================
2638  * Compute the optimal bit lengths for a tree and update the total bit length
2639  * for the current block.
2640  * IN assertion: the fields freq and dad are set, heap[heap_max] and
2641  * above are the tree nodes sorted by increasing frequency.
2642  * OUT assertions: the field len is set to the optimal bit length, the
2643  * array bl_count contains the frequencies for each bit length.
2644  * The length opt_len is updated; static_len is also updated if stree is
2645  * not null.
2646  */
2647 local void gen_bitlen(s, desc)
2648  deflate_state *s;
2649  tree_desc *desc; /* the tree descriptor */
2650 {
2651  ct_data *tree = desc->dyn_tree;
2652  int max_code = desc->max_code;
2653  const ct_data *stree = desc->stat_desc->static_tree;
2654  const intf *extra = desc->stat_desc->extra_bits;
2655  int base = desc->stat_desc->extra_base;
2656  int max_length = desc->stat_desc->max_length;
2657  int h; /* heap index */
2658  int n, m; /* iterate over the tree elements */
2659  int bits; /* bit length */
2660  int xbits; /* extra bits */
2661  ush f; /* frequency */
2662  int overflow = 0; /* number of elements with bit length too large */
2663 
2664  for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
2665 
2666  /* In a first pass, compute the optimal bit lengths (which may
2667  * overflow in the case of the bit length tree).
2668  */
2669  tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
2670 
2671  for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
2672  n = s->heap[h];
2673  bits = tree[tree[n].Dad].Len + 1;
2674  if (bits > max_length) bits = max_length, overflow++;
2675  tree[n].Len = (ush)bits;
2676  /* We overwrite tree[n].Dad which is no longer needed */
2677 
2678  if (n > max_code) continue; /* not a leaf node */
2679 
2680  s->bl_count[bits]++;
2681  xbits = 0;
2682  if (n >= base) xbits = extra[n-base];
2683  f = tree[n].Freq;
2684  s->opt_len += (ulg)f * (bits + xbits);
2685  if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
2686  }
2687  if (overflow == 0) return;
2688 
2689  Trace((stderr,"\nbit length overflow\n"));
2690  /* This happens for example on obj2 and pic of the Calgary corpus */
2691 
2692  /* Find the first bit length which could increase: */
2693  do {
2694  bits = max_length-1;
2695 #pragma warning(suppress: 6385)
2696  while (s->bl_count[bits] == 0) bits--;
2697  s->bl_count[bits]--; /* move one leaf down the tree */
2698  s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
2699  s->bl_count[max_length]--;
2700  /* The brother of the overflow item also moves one step up,
2701  * but this does not affect bl_count[max_length]
2702  */
2703  overflow -= 2;
2704  } while (overflow > 0);
2705 
2706  /* Now recompute all bit lengths, scanning in increasing frequency.
2707  * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
2708  * lengths instead of fixing only the wrong ones. This idea is taken
2709  * from 'ar' written by Haruhiko Okumura.)
2710  */
2711  for (bits = max_length; bits != 0; bits--) {
2712  n = s->bl_count[bits];
2713  while (n != 0) {
2714  m = s->heap[--h];
2715  if (m > max_code) continue;
2716  if ((unsigned) tree[m].Len != (unsigned) bits) {
2717  Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
2718  s->opt_len += ((long)bits - (long)tree[m].Len)
2719  *(long)tree[m].Freq;
2720  tree[m].Len = (ush)bits;
2721  }
2722  n--;
2723  }
2724  }
2725 }
2726 
2727 /* ===========================================================================
2728  * Generate the codes for a given tree and bit counts (which need not be
2729  * optimal).
2730  * IN assertion: the array bl_count contains the bit length statistics for
2731  * the given tree and the field len is set for all tree elements.
2732  * OUT assertion: the field code is set for all tree elements of non
2733  * zero code length.
2734  */
2735 local void gen_codes (tree, max_code, bl_count)
2736  ct_data *tree; /* the tree to decorate */
2737  int max_code; /* largest code with non zero frequency */
2738  ushf *bl_count; /* number of codes at each bit length */
2739 {
2740  ush next_code[MAX_BITS+1]; /* next code value for each bit length */
2741  ush code = 0; /* running code value */
2742  int bits; /* bit index */
2743  int n; /* code index */
2744 
2745  /* The distribution counts are first used to generate the code values
2746  * without bit reversal.
2747  */
2748  for (bits = 1; bits <= MAX_BITS; bits++) {
2749  next_code[bits] = code = (code + bl_count[bits-1]) << 1;
2750  }
2751  /* Check that the bit counts in bl_count are consistent. The last code
2752  * must be all ones.
2753  */
2754  Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
2755  "inconsistent bit counts");
2756  Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
2757 
2758  for (n = 0; n <= max_code; n++) {
2759  int len = tree[n].Len;
2760  if (len == 0) continue;
2761  /* Now reverse the bits */
2762  tree[n].Code = bi_reverse(next_code[len]++, len);
2763 
2764  Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
2765  n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
2766  }
2767 }
2768 
2769 /* ===========================================================================
2770  * Construct one Huffman tree and assigns the code bit strings and lengths.
2771  * Update the total bit length for the current block.
2772  * IN assertion: the field freq is set for all tree elements.
2773  * OUT assertions: the fields len and code are set to the optimal bit length
2774  * and corresponding code. The length opt_len is updated; static_len is
2775  * also updated if stree is not null. The field max_code is set.
2776  */
2777 local void build_tree(s, desc)
2778  deflate_state *s;
2779  tree_desc *desc; /* the tree descriptor */
2780 {
2781  ct_data *tree = desc->dyn_tree;
2782  const ct_data *stree = desc->stat_desc->static_tree;
2783  int elems = desc->stat_desc->elems;
2784  int n, m; /* iterate over heap elements */
2785  int max_code = -1; /* largest code with non zero frequency */
2786  int node; /* new node being created */
2787 
2788  /* Construct the initial heap, with least frequent element in
2789  * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
2790  * heap[0] is not used.
2791  */
2792  s->heap_len = 0, s->heap_max = HEAP_SIZE;
2793 
2794  for (n = 0; n < elems; n++) {
2795  if (tree[n].Freq != 0) {
2796  s->heap[++(s->heap_len)] = max_code = n;
2797  s->depth[n] = 0;
2798  } else {
2799  tree[n].Len = 0;
2800  }
2801  }
2802 
2803  /* The pkzip format requires that at least one distance code exists,
2804  * and that at least one bit should be sent even if there is only one
2805  * possible code. So to avoid special checks later on we force at least
2806  * two codes of non zero frequency.
2807  */
2808  while (s->heap_len < 2) {
2809  node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
2810  tree[node].Freq = 1;
2811  s->depth[node] = 0;
2812  s->opt_len--; if (stree) s->static_len -= stree[node].Len;
2813  /* node is 0 or 1 so it does not have extra bits */
2814  }
2815  desc->max_code = max_code;
2816 
2817  /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
2818  * establish sub-heaps of increasing lengths:
2819  */
2820  for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
2821 
2822  /* Construct the Huffman tree by repeatedly combining the least two
2823  * frequent nodes.
2824  */
2825  node = elems; /* next internal node of the tree */
2826  do {
2827  pqremove(s, tree, n); /* n = node of least frequency */
2828  m = s->heap[SMALLEST]; /* m = node of next least frequency */
2829 
2830  s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
2831  s->heap[--(s->heap_max)] = m;
2832 
2833  /* Create a new node father of n and m */
2834  tree[node].Freq = tree[n].Freq + tree[m].Freq;
2835  s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ?
2836  s->depth[n] : s->depth[m]) + 1);
2837  tree[n].Dad = tree[m].Dad = (ush)node;
2838 #ifdef DUMP_BL_TREE
2839  if (tree == s->bl_tree) {
2840  fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
2841  node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
2842  }
2843 #endif
2844  /* and insert the new node in the heap */
2845  s->heap[SMALLEST] = node++;
2846  pqdownheap(s, tree, SMALLEST);
2847 
2848  } while (s->heap_len >= 2);
2849 
2850  s->heap[--(s->heap_max)] = s->heap[SMALLEST];
2851 
2852  /* At this point, the fields freq and dad are set. We can now
2853  * generate the bit lengths.
2854  */
2855  gen_bitlen(s, (tree_desc *)desc);
2856 
2857  /* The field len is now set, we can generate the bit codes */
2858  gen_codes ((ct_data *)tree, max_code, s->bl_count);
2859 }
2860 
2861 /* ===========================================================================
2862  * Scan a literal or distance tree to determine the frequencies of the codes
2863  * in the bit length tree.
2864  */
2865 local void scan_tree (s, tree, max_code)
2866  deflate_state *s;
2867  ct_data *tree; /* the tree to be scanned */
2868  int max_code; /* and its largest code of non zero frequency */
2869 {
2870  int n; /* iterates over all tree elements */
2871  int prevlen = -1; /* last emitted length */
2872  int curlen; /* length of current code */
2873  int nextlen = tree[0].Len; /* length of next code */
2874  int count = 0; /* repeat count of the current code */
2875  int max_count = 7; /* max repeat count */
2876  int min_count = 4; /* min repeat count */
2877 
2878  if (nextlen == 0) max_count = 138, min_count = 3;
2879  tree[max_code+1].Len = (ush)0xffff; /* guard */
2880 
2881  for (n = 0; n <= max_code; n++) {
2882  curlen = nextlen; nextlen = tree[n+1].Len;
2883  if (++count < max_count && curlen == nextlen) {
2884  continue;
2885  } else if (count < min_count) {
2886  s->bl_tree[curlen].Freq += count;
2887  } else if (curlen != 0) {
2888  if (curlen != prevlen) s->bl_tree[curlen].Freq++;
2889  s->bl_tree[REP_3_6].Freq++;
2890  } else if (count <= 10) {
2891  s->bl_tree[REPZ_3_10].Freq++;
2892  } else {
2893  s->bl_tree[REPZ_11_138].Freq++;
2894  }
2895  count = 0; prevlen = curlen;
2896  if (nextlen == 0) {
2897  max_count = 138, min_count = 3;
2898  } else if (curlen == nextlen) {
2899  max_count = 6, min_count = 3;
2900  } else {
2901  max_count = 7, min_count = 4;
2902  }
2903  }
2904 }
2905 
2906 /* ===========================================================================
2907  * Send a literal or distance tree in compressed form, using the codes in
2908  * bl_tree.
2909  */
2910 local void send_tree (s, tree, max_code)
2911  deflate_state *s;
2912  ct_data *tree; /* the tree to be scanned */
2913  int max_code; /* and its largest code of non zero frequency */
2914 {
2915  int n; /* iterates over all tree elements */
2916  int prevlen = -1; /* last emitted length */
2917  int curlen; /* length of current code */
2918  int nextlen = tree[0].Len; /* length of next code */
2919  int count = 0; /* repeat count of the current code */
2920  int max_count = 7; /* max repeat count */
2921  int min_count = 4; /* min repeat count */
2922 
2923  /* tree[max_code+1].Len = -1; */ /* guard already set */
2924  if (nextlen == 0) max_count = 138, min_count = 3;
2925 
2926  for (n = 0; n <= max_code; n++) {
2927  curlen = nextlen; nextlen = tree[n+1].Len;
2928  if (++count < max_count && curlen == nextlen) {
2929  continue;
2930  } else if (count < min_count) {
2931  do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
2932 
2933  } else if (curlen != 0) {
2934  if (curlen != prevlen) {
2935  send_code(s, curlen, s->bl_tree); count--;
2936  }
2937  Assert(count >= 3 && count <= 6, " 3_6?");
2938  send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
2939 
2940  } else if (count <= 10) {
2941  send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
2942 
2943  } else {
2944  send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
2945  }
2946  count = 0; prevlen = curlen;
2947  if (nextlen == 0) {
2948  max_count = 138, min_count = 3;
2949  } else if (curlen == nextlen) {
2950  max_count = 6, min_count = 3;
2951  } else {
2952  max_count = 7, min_count = 4;
2953  }
2954  }
2955 }
2956 
2957 /* ===========================================================================
2958  * Construct the Huffman tree for the bit lengths and return the index in
2959  * bl_order of the last bit length code to send.
2960  */
2961 local int build_bl_tree(s)
2962  deflate_state *s;
2963 {
2964  int max_blindex; /* index of last bit length code of non zero freq */
2965 
2966  /* Determine the bit length frequencies for literal and distance trees */
2967  scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
2968  scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
2969 
2970  /* Build the bit length tree: */
2971  build_tree(s, (tree_desc *)(&(s->bl_desc)));
2972  /* opt_len now includes the length of the tree representations, except
2973  * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
2974  */
2975 
2976  /* Determine the number of bit length codes to send. The pkzip format
2977  * requires that at least 4 bit length codes be sent. (appnote.txt says
2978  * 3 but the actual value used is 4.)
2979  */
2980  for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
2981  if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
2982  }
2983  /* Update opt_len to include the bit length tree and counts */
2984  s->opt_len += 3*(max_blindex+1) + 5+5+4;
2985  Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
2986  s->opt_len, s->static_len));
2987 
2988  return max_blindex;
2989 }
2990 
2991 /* ===========================================================================
2992  * Send the header for a block using dynamic Huffman trees: the counts, the
2993  * lengths of the bit length codes, the literal tree and the distance tree.
2994  * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
2995  */
2996 local void send_all_trees(s, lcodes, dcodes, blcodes)
2997  deflate_state *s;
2998  int lcodes, dcodes, blcodes; /* number of codes for each tree */
2999 {
3000  int rank; /* index in bl_order */
3001 
3002  Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
3003  Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
3004  "too many codes");
3005  Tracev((stderr, "\nbl counts: "));
3006  send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
3007  send_bits(s, dcodes-1, 5);
3008  send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
3009  for (rank = 0; rank < blcodes; rank++) {
3010  Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
3011  send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
3012  }
3013  Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
3014 
3015  send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
3016  Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
3017 
3018  send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
3019  Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
3020 }
3021 
3022 /* ===========================================================================
3023  * Send a stored block
3024  */
3025 void _tr_stored_block(s, buf, stored_len, eof)
3026  deflate_state *s;
3027  charf *buf; /* input block */
3028  ulg stored_len; /* length of input block */
3029  int eof; /* true if this is the last block for a file */
3030 {
3031  send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */
3032 #ifdef DEBUG
3033  s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
3034  s->compressed_len += (stored_len + 4) << 3;
3035 #endif
3036  copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
3037 }
3038 
3039 /* ===========================================================================
3040  * Send one empty static block to give enough lookahead for inflate.
3041  * This takes 10 bits, of which 7 may remain in the bit buffer.
3042  * The current inflate code requires 9 bits of lookahead. If the
3043  * last two codes for the previous block (real code plus EOB) were coded
3044  * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
3045  * the last real code. In this case we send two empty static blocks instead
3046  * of one. (There are no problems if the previous block is stored or fixed.)
3047  * To simplify the code, we assume the worst case of last real code encoded
3048  * on one bit only.
3049  */
3050 void _tr_align(s)
3051  deflate_state *s;
3052 {
3053  send_bits(s, STATIC_TREES<<1, 3);
3054  send_code(s, END_BLOCK, static_ltree);
3055 #ifdef DEBUG
3056  s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
3057 #endif
3058  bi_flush(s);
3059  /* Of the 10 bits for the empty block, we have already sent
3060  * (10 - bi_valid) bits. The lookahead for the last real code (before
3061  * the EOB of the previous block) was thus at least one plus the length
3062  * of the EOB plus what we have just sent of the empty static block.
3063  */
3064  if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
3065  send_bits(s, STATIC_TREES<<1, 3);
3066  send_code(s, END_BLOCK, static_ltree);
3067 #ifdef DEBUG
3068  s->compressed_len += 10L;
3069 #endif
3070  bi_flush(s);
3071  }
3072  s->last_eob_len = 7;
3073 }
3074 
3075 /* ===========================================================================
3076  * Determine the best encoding for the current block: dynamic trees, static
3077  * trees or store, and output the encoded block to the zip file.
3078  */
3079 void _tr_flush_block(s, buf, stored_len, eof)
3080  deflate_state *s;
3081  charf *buf; /* input block, or NULL if too old */
3082  ulg stored_len; /* length of input block */
3083  int eof; /* true if this is the last block for a file */
3084 {
3085  ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
3086  int max_blindex = 0; /* index of last bit length code of non zero freq */
3087 
3088  /* Build the Huffman trees unless a stored block is forced */
3089  if (s->level > 0) {
3090 
3091  /* Check if the file is binary or text */
3092  if (stored_len > 0 && s->strm->data_type == Z_UNKNOWN)
3093  set_data_type(s);
3094 
3095  /* Construct the literal and distance trees */
3096  build_tree(s, (tree_desc *)(&(s->l_desc)));
3097  Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
3098  s->static_len));
3099 
3100  build_tree(s, (tree_desc *)(&(s->d_desc)));
3101  Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
3102  s->static_len));
3103  /* At this point, opt_len and static_len are the total bit lengths of
3104  * the compressed block data, excluding the tree representations.
3105  */
3106 
3107  /* Build the bit length tree for the above two trees, and get the index
3108  * in bl_order of the last bit length code to send.
3109  */
3110  max_blindex = build_bl_tree(s);
3111 
3112  /* Determine the best encoding. Compute the block lengths in bytes. */
3113  opt_lenb = (s->opt_len+3+7)>>3;
3114  static_lenb = (s->static_len+3+7)>>3;
3115 
3116  Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
3117  opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
3118  s->last_lit));
3119 
3120  if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
3121 
3122  } else {
3123  Assert(buf != (char*)0, "lost buf");
3124  opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
3125  }
3126 
3127 #ifdef FORCE_STORED
3128  if (buf != (char*)0) { /* force stored block */
3129 #else
3130  if (stored_len+4 <= opt_lenb && buf != (char*)0) {
3131  /* 4: two words for the lengths */
3132 #endif
3133  /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
3134  * Otherwise we can't have processed more than WSIZE input bytes since
3135  * the last block flush, because compression would have been
3136  * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
3137  * transform a block into a stored block.
3138  */
3139  _tr_stored_block(s, buf, stored_len, eof);
3140 
3141 #ifdef FORCE_STATIC
3142  } else if (static_lenb >= 0) { /* force static trees */
3143 #else
3144  } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
3145 #endif
3146  send_bits(s, (STATIC_TREES<<1)+eof, 3);
3147  compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);
3148 #ifdef DEBUG
3149  s->compressed_len += 3 + s->static_len;
3150 #endif
3151  } else {
3152  send_bits(s, (DYN_TREES<<1)+eof, 3);
3153  send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
3154  max_blindex+1);
3155  compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree);
3156 #ifdef DEBUG
3157  s->compressed_len += 3 + s->opt_len;
3158 #endif
3159  }
3160  Assert (s->compressed_len == s->bits_sent, "bad compressed size");
3161  /* The above check is made mod 2^32, for files larger than 512 MB
3162  * and uLong implemented on 32 bits.
3163  */
3164  init_block(s);
3165 
3166  if (eof) {
3167  bi_windup(s);
3168 #ifdef DEBUG
3169  s->compressed_len += 7; /* align on byte boundary */
3170 #endif
3171  }
3172  Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
3173  s->compressed_len-7*eof));
3174 }
3175 
3176 /* ===========================================================================
3177  * Save the match info and tally the frequency counts. Return true if
3178  * the current block must be flushed.
3179  */
3180 int _tr_tally (s, dist, lc)
3181  deflate_state *s;
3182  unsigned dist; /* distance of matched string */
3183  unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
3184 {
3185  s->d_buf[s->last_lit] = (ush)dist;
3186  s->l_buf[s->last_lit++] = (uch)lc;
3187  if (dist == 0) {
3188  /* lc is the unmatched char */
3189  s->dyn_ltree[lc].Freq++;
3190  } else {
3191  s->matches++;
3192  /* Here, lc is the match length - MIN_MATCH */
3193  dist--; /* dist = match distance - 1 */
3194  Assert((ush)dist < (ush)MAX_DIST(s) &&
3195  (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
3196  (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
3197 
3198  s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
3199  s->dyn_dtree[d_code(dist)].Freq++;
3200  }
3201 
3202 #ifdef TRUNCATE_BLOCK
3203  /* Try to guess if it is profitable to stop the current block here */
3204  if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
3205  /* Compute an upper bound for the compressed length */
3206  ulg out_length = (ulg)s->last_lit*8L;
3207  ulg in_length = (ulg)((long)s->strstart - s->block_start);
3208  int dcode;
3209  for (dcode = 0; dcode < D_CODES; dcode++) {
3210  out_length += (ulg)s->dyn_dtree[dcode].Freq *
3211  (5L+extra_dbits[dcode]);
3212  }
3213  out_length >>= 3;
3214  Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
3215  s->last_lit, in_length, out_length,
3216  100L - out_length*100L/in_length));
3217  if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
3218  }
3219 #endif
3220  return (s->last_lit == s->lit_bufsize-1);
3221  /* We avoid equality with lit_bufsize because of wraparound at 64K
3222  * on 16 bit machines and because stored blocks are restricted to
3223  * 64K-1 bytes.
3224  */
3225 }
3226 
3227 /* ===========================================================================
3228  * Send the block data compressed using the given Huffman trees
3229  */
3230 local void compress_block(s, ltree, dtree)
3231  deflate_state *s;
3232  ct_data *ltree; /* literal tree */
3233  ct_data *dtree; /* distance tree */
3234 {
3235  unsigned dist; /* distance of matched string */
3236  int lc; /* match length or unmatched char (if dist == 0) */
3237  unsigned lx = 0; /* running index in l_buf */
3238  unsigned code; /* the code to send */
3239  int extra; /* number of extra bits to send */
3240 
3241  if (s->last_lit != 0) do {
3242  dist = s->d_buf[lx];
3243  lc = s->l_buf[lx++];
3244  if (dist == 0) {
3245  send_code(s, lc, ltree); /* send a literal byte */
3246  Tracecv(isgraph(lc), (stderr," '%c' ", lc));
3247  } else {
3248  /* Here, lc is the match length - MIN_MATCH */
3249  code = _length_code[lc];
3250  send_code(s, code+LITERALS+1, ltree); /* send the length code */
3251  extra = extra_lbits[code];
3252  if (extra != 0) {
3253  lc -= base_length[code];
3254  send_bits(s, lc, extra); /* send the extra length bits */
3255  }
3256  dist--; /* dist is now the match distance - 1 */
3257  code = d_code(dist);
3258  Assert (code < D_CODES, "bad d_code");
3259 
3260  send_code(s, code, dtree); /* send the distance code */
3261  extra = extra_dbits[code];
3262  if (extra != 0) {
3263  dist -= base_dist[code];
3264  send_bits(s, dist, extra); /* send the extra distance bits */
3265  }
3266  } /* literal or match pair ? */
3267 
3268  /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
3269  Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
3270  "pendingBuf overflow");
3271 
3272  } while (lx < s->last_lit);
3273 
3274  send_code(s, END_BLOCK, ltree);
3275  s->last_eob_len = ltree[END_BLOCK].Len;
3276 }
3277 
3278 /* ===========================================================================
3279  * Set the data type to BINARY or TEXT, using a crude approximation:
3280  * set it to Z_TEXT if all symbols are either printable characters (33 to 255)
3281  * or white spaces (9 to 13, or 32); or set it to Z_BINARY otherwise.
3282  * IN assertion: the fields Freq of dyn_ltree are set.
3283  */
3284 local void set_data_type(s)
3285  deflate_state *s;
3286 {
3287  int n;
3288 
3289  for (n = 0; n < 9; n++)
3290  if (s->dyn_ltree[n].Freq != 0)
3291  break;
3292  if (n == 9)
3293  for (n = 14; n < 32; n++)
3294  if (s->dyn_ltree[n].Freq != 0)
3295  break;
3296  s->strm->data_type = (n == 32) ? Z_TEXT : Z_BINARY;
3297 }
3298 
3299 /* ===========================================================================
3300  * Reverse the first len bits of a code, using straightforward code (a faster
3301  * method would use a table)
3302  * IN assertion: 1 <= len <= 15
3303  */
3304 local unsigned bi_reverse(code, len)
3305  unsigned code; /* the value to invert */
3306  int len; /* its bit length */
3307 {
3308  register unsigned res = 0;
3309  do {
3310  res |= code & 1;
3311  code >>= 1, res <<= 1;
3312  } while (--len > 0);
3313  return res >> 1;
3314 }
3315 
3316 /* ===========================================================================
3317  * Flush the bit buffer, keeping at most 7 bits in it.
3318  */
3319 local void bi_flush(s)
3320  deflate_state *s;
3321 {
3322  if (s->bi_valid == 16) {
3323  put_short(s, s->bi_buf);
3324  s->bi_buf = 0;
3325  s->bi_valid = 0;
3326  } else if (s->bi_valid >= 8) {
3327  put_byte(s, (Byte)s->bi_buf);
3328  s->bi_buf >>= 8;
3329  s->bi_valid -= 8;
3330  }
3331 }
3332 
3333 /* ===========================================================================
3334  * Flush the bit buffer and align the output on a byte boundary
3335  */
3336 local void bi_windup(s)
3337  deflate_state *s;
3338 {
3339  if (s->bi_valid > 8) {
3340  put_short(s, s->bi_buf);
3341  } else if (s->bi_valid > 0) {
3342  put_byte(s, (Byte)s->bi_buf);
3343  }
3344  s->bi_buf = 0;
3345  s->bi_valid = 0;
3346 #ifdef DEBUG
3347  s->bits_sent = (s->bits_sent+7) & ~7;
3348 #endif
3349 }
3350 
3351 /* ===========================================================================
3352  * Copy a stored block, storing first the length and its
3353  * one's complement if requested.
3354  */
3355 local void copy_block(s, buf, len, header)
3356  deflate_state *s;
3357  charf *buf; /* the input data */
3358  unsigned len; /* its length */
3359  int header; /* true if block header must be written */
3360 {
3361  bi_windup(s); /* align on byte boundary */
3362  s->last_eob_len = 8; /* enough lookahead for inflate */
3363 
3364  if (header) {
3365  put_short(s, (ush)len);
3366  put_short(s, (ush)~len);
3367 #ifdef DEBUG
3368  s->bits_sent += 2*16;
3369 #endif
3370  }
3371 #ifdef DEBUG
3372  s->bits_sent += (ulg)len<<3;
3373 #endif
3374  while (len--) {
3375  put_byte(s, *buf++);
3376  }
3377 }
3378 
3379 /* deflate.c -- compress data using the deflation algorithm
3380  * Copyright (C) 1995-2005 Jean-loup Gailly.
3381  * For conditions of distribution and use, see copyright notice in zlib.h
3382  */
3383 
3384 /*
3385  * ALGORITHM
3386  *
3387  * The "deflation" process depends on being able to identify portions
3388  * of the input text which are identical to earlier input (within a
3389  * sliding window trailing behind the input currently being processed).
3390  *
3391  * The most straightforward technique turns out to be the fastest for
3392  * most input files: try all possible matches and select the longest.
3393  * The key feature of this algorithm is that insertions into the string
3394  * dictionary are very simple and thus fast, and deletions are avoided
3395  * completely. Insertions are performed at each input character, whereas
3396  * string matches are performed only when the previous match ends. So it
3397  * is preferable to spend more time in matches to allow very fast string
3398  * insertions and avoid deletions. The matching algorithm for small
3399  * strings is inspired from that of Rabin & Karp. A brute force approach
3400  * is used to find longer strings when a small match has been found.
3401  * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
3402  * (by Leonid Broukhis).
3403  * A previous version of this file used a more sophisticated algorithm
3404  * (by Fiala and Greene) which is guaranteed to run in linear amortized
3405  * time, but has a larger average cost, uses more memory and is patented.
3406  * However the F&G algorithm may be faster for some highly redundant
3407  * files if the parameter max_chain_length (described below) is too large.
3408  *
3409  * ACKNOWLEDGEMENTS
3410  *
3411  * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
3412  * I found it in 'freeze' written by Leonid Broukhis.
3413  * Thanks to many people for bug reports and testing.
3414  *
3415  * REFERENCES
3416  *
3417  * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
3418  * Available in http://www.ietf.org/rfc/rfc1951.txt
3419  *
3420  * A description of the Rabin and Karp algorithm is given in the book
3421  * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
3422  *
3423  * Fiala,E.R., and Greene,D.H.
3424  * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
3425  *
3426  */
3427 
3428 const char deflate_copyright[] =
3429  " deflate 1.2.3 Copyright 1995-2005 Jean-loup Gailly ";
3430 /*
3431  If you use the zlib library in a product, an acknowledgment is welcome
3432  in the documentation of your product. If for some reason you cannot
3433  include such an acknowledgment, I would appreciate that you keep this
3434  copyright string in the executable of your product.
3435  */
3436 
3437 /* ===========================================================================
3438  * Function prototypes.
3439  */
3440 typedef enum {
3441  need_more, /* block not completed, need more input or more output */
3442  block_done, /* block flush performed */
3443  finish_started, /* finish started, need only more output at next deflate */
3444  finish_done /* finish done, accept no more input or output */
3445 } block_state;
3446 
3447 typedef block_state (*compress_func) OF((deflate_state *s, int flush));
3448 /* Compression function. Returns the block state after the call. */
3449 
3450 local void fill_window OF((deflate_state *s));
3451 local block_state deflate_stored OF((deflate_state *s, int flush));
3452 local block_state deflate_fast OF((deflate_state *s, int flush));
3453 #ifndef FASTEST
3454 local block_state deflate_slow OF((deflate_state *s, int flush));
3455 #endif
3456 local void lm_init OF((deflate_state *s));
3457 local void putShortMSB OF((deflate_state *s, uInt b));
3458 local void flush_pending OF((z_streamp strm));
3459 local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
3460 #ifndef FASTEST
3461 #ifdef ASMV
3462  void match_init OF((void)); /* asm code initialization */
3463  uInt longest_match OF((deflate_state *s, IPos cur_match));
3464 #else
3465 local uInt longest_match OF((deflate_state *s, IPos cur_match));
3466 #endif
3467 #endif
3468 local uInt longest_match_fast OF((deflate_state *s, IPos cur_match));
3469 
3470 #ifdef DEBUG
3471 local void check_match OF((deflate_state *s, IPos start, IPos match,
3472  int length));
3473 #endif
3474 
3475 /* ===========================================================================
3476  * Local data
3477  */
3478 
3479 #define NIL 0
3480 /* Tail of hash chains */
3481 
3482 #ifndef TOO_FAR
3483 # define TOO_FAR 4096
3484 #endif
3485 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
3486 
3487 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
3488 /* Minimum amount of lookahead, except at the end of the input file.
3489  * See deflate.c for comments about the MIN_MATCH+1.
3490  */
3491 
3492 /* Values for max_lazy_match, good_match and max_chain_length, depending on
3493  * the desired pack level (0..9). The values given below have been tuned to
3494  * exclude worst case performance for pathological files. Better values may be
3495  * found for specific files.
3496  */
3497 typedef struct config_s {
3498  ush good_length; /* reduce lazy search above this match length */
3499  ush max_lazy; /* do not perform lazy search above this match length */
3500  ush nice_length; /* quit search above this match length */
3501  ush max_chain;
3502  compress_func func;
3503 } config;
3504 
3505 #ifdef FASTEST
3506 local const config configuration_table[2] = {
3507 /* good lazy nice chain */
3508 /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
3509 /* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */
3510 #else
3511 local const config configuration_table[10] = {
3512 /* good lazy nice chain */
3513 /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
3514 /* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */
3515 /* 2 */ {4, 5, 16, 8, deflate_fast},
3516 /* 3 */ {4, 6, 32, 32, deflate_fast},
3517 
3518 /* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
3519 /* 5 */ {8, 16, 32, 32, deflate_slow},
3520 /* 6 */ {8, 16, 128, 128, deflate_slow},
3521 /* 7 */ {8, 32, 128, 256, deflate_slow},
3522 /* 8 */ {32, 128, 258, 1024, deflate_slow},
3523 /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
3524 #endif
3525 
3526 /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
3527  * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
3528  * meaning.
3529  */
3530 
3531 #define EQUAL 0
3532 /* result of memcmp for equal strings */
3533 
3534 /* ===========================================================================
3535  * Update a hash value with the given input byte
3536  * IN assertion: all calls to to UPDATE_HASH are made with consecutive
3537  * input characters, so that a running hash key can be computed from the
3538  * previous key instead of complete recalculation each time.
3539  */
3540 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
3541 
3542 
3543 /* ===========================================================================
3544  * Insert string str in the dictionary and set match_head to the previous head
3545  * of the hash chain (the most recent string with same hash key). Return
3546  * the previous length of the hash chain.
3547  * If this file is compiled with -DFASTEST, the compression level is forced
3548  * to 1, and no hash chains are maintained.
3549  * IN assertion: all calls to to INSERT_STRING are made with consecutive
3550  * input characters and the first MIN_MATCH bytes of str are valid
3551  * (except for the last MIN_MATCH-1 bytes of the input file).
3552  */
3553 #ifdef FASTEST
3554 #define INSERT_STRING(s, str, match_head) \
3555  (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
3556  match_head = s->head[s->ins_h], \
3557  s->head[s->ins_h] = (Pos)(str))
3558 #else
3559 #define INSERT_STRING(s, str, match_head) \
3560  (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
3561  match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
3562  s->head[s->ins_h] = (Pos)(str))
3563 #endif
3564 
3565 /* ===========================================================================
3566  * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
3567  * prev[] will be initialized on the fly.
3568  */
3569 #define CLEAR_HASH(s) \
3570  s->head[s->hash_size-1] = NIL; \
3571  zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
3572 
3573 /* ========================================================================= */
3574 int deflateInit_(strm, level, version, stream_size)
3575  z_streamp strm;
3576  int level;
3577  const char *version;
3578  int stream_size;
3579 {
3580  return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
3581  Z_DEFAULT_STRATEGY, version, stream_size);
3582  /* To do: ignore strm->next_in if we use it as window */
3583 }
3584 
3585 /* ========================================================================= */
3586 int deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
3587  version, stream_size)
3588  z_streamp strm;
3589  int level;
3590  int method;
3591  int windowBits;
3592  int memLevel;
3593  int strategy;
3594  const char *version;
3595  int stream_size;
3596 {
3597  deflate_state *s;
3598  int wrap = 1;
3599  static const char my_version[] = ZLIB_VERSION;
3600 
3601  ushf *overlay;
3602  /* We overlay pending_buf and d_buf+l_buf. This works since the average
3603  * output size for (length,distance) codes is <= 24 bits.
3604  */
3605 
3606  if (version == Z_NULL || version[0] != my_version[0] ||
3607  stream_size != sizeof(z_stream)) {
3608  return Z_VERSION_ERROR;
3609  }
3610  if (strm == Z_NULL) return Z_STREAM_ERROR;
3611 
3612  strm->msg = Z_NULL;
3613  if (strm->zalloc == (alloc_func)0) {
3614  strm->zalloc = zcalloc;
3615  strm->opaque = (voidpf)0;
3616  }
3617  if (strm->zfree == (free_func)0) strm->zfree = zcfree;
3618 
3619 #ifdef FASTEST
3620  if (level != 0) level = 1;
3621 #else
3622  if (level == Z_DEFAULT_COMPRESSION) level = 6;
3623 #endif
3624 
3625  if (windowBits < 0) { /* suppress zlib wrapper */
3626  wrap = 0;
3627  windowBits = -windowBits;
3628  }
3629  if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
3630  windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
3631  strategy < 0 || strategy > Z_FIXED) {
3632  return Z_STREAM_ERROR;
3633  }
3634  if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
3635  s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
3636  if (s == Z_NULL) return Z_MEM_ERROR;
3637  strm->state = (struct internal_state FAR *)s;
3638  s->strm = strm;
3639 
3640  s->wrap = wrap;
3641  s->gzhead = Z_NULL;
3642  s->w_bits = windowBits;
3643  s->w_size = 1 << s->w_bits;
3644  s->w_mask = s->w_size - 1;
3645 
3646  s->hash_bits = memLevel + 7;
3647  s->hash_size = 1 << s->hash_bits;
3648  s->hash_mask = s->hash_size - 1;
3649  s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
3650 
3651  s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
3652  s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
3653  s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
3654 
3655  s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
3656 
3657  overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
3658  s->pending_buf = (uchf *) overlay;
3659  s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
3660 
3661  if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
3662  s->pending_buf == Z_NULL) {
3663  s->status = FINISH_STATE;
3664  strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
3665  deflateEnd (strm);
3666  return Z_MEM_ERROR;
3667  }
3668  s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
3669  s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
3670 
3671  s->level = level;
3672  s->strategy = strategy;
3673  s->method = (Byte)method;
3674 
3675  return deflateReset(strm);
3676 }
3677 
3678 /* ========================================================================= */
3679 int deflateSetDictionary (strm, dictionary, dictLength)
3680  z_streamp strm;
3681  const Bytef *dictionary;
3682  uInt dictLength;
3683 {
3684  deflate_state *s;
3685  uInt length = dictLength;
3686  uInt n;
3687  IPos hash_head = 0;
3688 
3689  if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
3690  strm->state->wrap == 2 ||
3691  (strm->state->wrap == 1 && strm->state->status != INIT_STATE))
3692  return Z_STREAM_ERROR;
3693 
3694  s = strm->state;
3695  if (s->wrap)
3696  strm->adler = adler32(strm->adler, dictionary, dictLength);
3697 
3698  if (length < MIN_MATCH) return Z_OK;
3699  if (length > MAX_DIST(s)) {
3700  length = MAX_DIST(s);
3701  dictionary += dictLength - length; /* use the tail of the dictionary */
3702  }
3703  zmemcpy(s->window, dictionary, length);
3704  s->strstart = length;
3705  s->block_start = (long)length;
3706 
3707  /* Insert all strings in the hash table (except for the last two bytes).
3708  * s->lookahead stays null, so s->ins_h will be recomputed at the next
3709  * call of fill_window.
3710  */
3711  s->ins_h = s->window[0];
3712  UPDATE_HASH(s, s->ins_h, s->window[1]);
3713  for (n = 0; n <= length - MIN_MATCH; n++) {
3714 #pragma warning(suppress: 6385)
3715  INSERT_STRING(s, n, hash_head);
3716  }
3717  if (hash_head) hash_head = 0; /* to make compiler happy */
3718  return Z_OK;
3719 }
3720 
3721 /* ========================================================================= */
3722 int deflateReset (strm)
3723  z_streamp strm;
3724 {
3725  deflate_state *s;
3726 
3727  if (strm == Z_NULL || strm->state == Z_NULL ||
3728  strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
3729  return Z_STREAM_ERROR;
3730  }
3731 
3732  strm->total_in = strm->total_out = 0;
3733  strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
3734  strm->data_type = Z_UNKNOWN;
3735 
3736  s = (deflate_state *)strm->state;
3737  s->pending = 0;
3738  s->pending_out = s->pending_buf;
3739 
3740  if (s->wrap < 0) {
3741  s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
3742  }
3743  s->status = s->wrap ? INIT_STATE : BUSY_STATE;
3744  strm->adler =
3745  adler32(0L, Z_NULL, 0);
3746  s->last_flush = Z_NO_FLUSH;
3747 
3748  _tr_init(s);
3749  lm_init(s);
3750 
3751  return Z_OK;
3752 }
3753 
3754 /* ========================================================================= */
3755 int deflateSetHeader (strm, head)
3756  z_streamp strm;
3757  gz_headerp head;
3758 {
3759  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
3760  if (strm->state->wrap != 2) return Z_STREAM_ERROR;
3761  strm->state->gzhead = head;
3762  return Z_OK;
3763 }
3764 
3765 /* ========================================================================= */
3766 int deflatePrime (strm, bits, value)
3767  z_streamp strm;
3768  int bits;
3769  int value;
3770 {
3771  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
3772  strm->state->bi_valid = bits;
3773  strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
3774  return Z_OK;
3775 }
3776 
3777 /* ========================================================================= */
3778 int deflateParams(strm, level, strategy)
3779  z_streamp strm;
3780  int level;
3781  int strategy;
3782 {
3783  deflate_state *s;
3784  compress_func func;
3785  int err = Z_OK;
3786 
3787  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
3788  s = strm->state;
3789 
3790 #ifdef FASTEST
3791  if (level != 0) level = 1;
3792 #else
3793  if (level == Z_DEFAULT_COMPRESSION) level = 6;
3794 #endif
3795  if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
3796  return Z_STREAM_ERROR;
3797  }
3798  func = configuration_table[s->level].func;
3799 
3800  if (func != configuration_table[level].func && strm->total_in != 0) {
3801  /* Flush the last buffer: */
3802  err = deflate(strm, Z_PARTIAL_FLUSH);
3803  }
3804  if (s->level != level) {
3805  s->level = level;
3806  s->max_lazy_match = configuration_table[level].max_lazy;
3807  s->good_match = configuration_table[level].good_length;
3808  s->nice_match = configuration_table[level].nice_length;
3809  s->max_chain_length = configuration_table[level].max_chain;
3810  }
3811  s->strategy = strategy;
3812  return err;
3813 }
3814 
3815 /* ========================================================================= */
3816 int deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
3817  z_streamp strm;
3818  int good_length;
3819  int max_lazy;
3820  int nice_length;
3821  int max_chain;
3822 {
3823  deflate_state *s;
3824 
3825  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
3826  s = strm->state;
3827  s->good_match = good_length;
3828  s->max_lazy_match = max_lazy;
3829  s->nice_match = nice_length;
3830  s->max_chain_length = max_chain;
3831  return Z_OK;
3832 }
3833 
3834 /* =========================================================================
3835  * For the default windowBits of 15 and memLevel of 8, this function returns
3836  * a close to exact, as well as small, upper bound on the compressed size.
3837  * They are coded as constants here for a reason--if the #define's are
3838  * changed, then this function needs to be changed as well. The return
3839  * value for 15 and 8 only works for those exact settings.
3840  *
3841  * For any setting other than those defaults for windowBits and memLevel,
3842  * the value returned is a conservative worst case for the maximum expansion
3843  * resulting from using fixed blocks instead of stored blocks, which deflate
3844  * can emit on compressed data for some combinations of the parameters.
3845  *
3846  * This function could be more sophisticated to provide closer upper bounds
3847  * for every combination of windowBits and memLevel, as well as wrap.
3848  * But even the conservative upper bound of about 14% expansion does not
3849  * seem onerous for output buffer allocation.
3850  */
3851 uLong deflateBound(strm, sourceLen)
3852  z_streamp strm;
3853  uLong sourceLen;
3854 {
3855  deflate_state *s;
3856  uLong destLen;
3857 
3858  /* conservative upper bound */
3859  destLen = sourceLen +
3860  ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;
3861 
3862  /* if can't get parameters, return conservative bound */
3863  if (strm == Z_NULL || strm->state == Z_NULL)
3864  return destLen;
3865 
3866  /* if not default parameters, return conservative bound */
3867  s = strm->state;
3868  if (s->w_bits != 15 || s->hash_bits != 8 + 7)
3869  return destLen;
3870 
3871  /* default settings: return tight bound for that case */
3872  return compressBound(sourceLen);
3873 }
3874 
3875 /* =========================================================================
3876  * Put a short in the pending buffer. The 16-bit value is put in MSB order.
3877  * IN assertion: the stream state is correct and there is enough room in
3878  * pending_buf.
3879  */
3880 local void putShortMSB (s, b)
3881  deflate_state *s;
3882  uInt b;
3883 {
3884  put_byte(s, (Byte)(b >> 8));
3885  put_byte(s, (Byte)(b & 0xff));
3886 }
3887 
3888 /* =========================================================================
3889  * Flush as much pending output as possible. All deflate() output goes
3890  * through this function so some applications may wish to modify it
3891  * to avoid allocating a large strm->next_out buffer and copying into it.
3892  * (See also read_buf()).
3893  */
3894 local void flush_pending(strm)
3895  z_streamp strm;
3896 {
3897  unsigned len = strm->state->pending;
3898 
3899  if (len > strm->avail_out) len = strm->avail_out;
3900  if (len == 0) return;
3901 
3902  zmemcpy(strm->next_out, strm->state->pending_out, len);
3903  strm->next_out += len;
3904  strm->state->pending_out += len;
3905  strm->total_out += len;
3906  strm->avail_out -= len;
3907  strm->state->pending -= len;
3908  if (strm->state->pending == 0) {
3909  strm->state->pending_out = strm->state->pending_buf;
3910  }
3911 }
3912 
3913 /* ========================================================================= */
3914 int deflate (strm, flush)
3915  z_streamp strm;
3916  int flush;
3917 {
3918  int old_flush; /* value of flush param for previous deflate call */
3919  deflate_state *s;
3920 
3921  if (strm == Z_NULL || strm->state == Z_NULL ||
3922  flush > Z_FINISH || flush < 0) {
3923  return Z_STREAM_ERROR;
3924  }
3925  s = strm->state;
3926 
3927  if (strm->next_out == Z_NULL ||
3928  (strm->next_in == Z_NULL && strm->avail_in != 0) ||
3929  (s->status == FINISH_STATE && flush != Z_FINISH)) {
3930  ERR_RETURN(strm, Z_STREAM_ERROR);
3931  }
3932  if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
3933 
3934  s->strm = strm; /* just in case */
3935  old_flush = s->last_flush;
3936  s->last_flush = flush;
3937 
3938  /* Write the header */
3939  if (s->status == INIT_STATE) {
3940  {
3941  uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
3942  uInt level_flags;
3943 
3944  if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
3945  level_flags = 0;
3946  else if (s->level < 6)
3947  level_flags = 1;
3948  else if (s->level == 6)
3949  level_flags = 2;
3950  else
3951  level_flags = 3;
3952  header |= (level_flags << 6);
3953  if (s->strstart != 0) header |= PRESET_DICT;
3954  header += 31 - (header % 31);
3955 
3956  s->status = BUSY_STATE;
3957  putShortMSB(s, header);
3958 
3959  /* Save the adler32 of the preset dictionary: */
3960  if (s->strstart != 0) {
3961  putShortMSB(s, (uInt)(strm->adler >> 16));
3962  putShortMSB(s, (uInt)(strm->adler & 0xffff));
3963  }
3964  strm->adler = adler32(0L, Z_NULL, 0);
3965  }
3966  }
3967 
3968  /* Flush as much pending output as possible */
3969  if (s->pending != 0) {
3970  flush_pending(strm);
3971  if (strm->avail_out == 0) {
3972  /* Since avail_out is 0, deflate will be called again with
3973  * more output space, but possibly with both pending and
3974  * avail_in equal to zero. There won't be anything to do,
3975  * but this is not an error situation so make sure we
3976  * return OK instead of BUF_ERROR at next call of deflate:
3977  */
3978  s->last_flush = -1;
3979  return Z_OK;
3980  }
3981 
3982  /* Make sure there is something to do and avoid duplicate consecutive
3983  * flushes. For repeated and useless calls with Z_FINISH, we keep
3984  * returning Z_STREAM_END instead of Z_BUF_ERROR.
3985  */
3986  } else if (strm->avail_in == 0 && flush <= old_flush &&
3987  flush != Z_FINISH) {
3988  ERR_RETURN(strm, Z_BUF_ERROR);
3989  }
3990 
3991  /* User must not provide more input after the first FINISH: */
3992  if (s->status == FINISH_STATE && strm->avail_in != 0) {
3993  ERR_RETURN(strm, Z_BUF_ERROR);
3994  }
3995 
3996  /* Start a new block or continue the current one.
3997  */
3998  if (strm->avail_in != 0 || s->lookahead != 0 ||
3999  (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
4000  block_state bstate;
4001 
4002  bstate = (*(configuration_table[s->level].func))(s, flush);
4003 
4004  if (bstate == finish_started || bstate == finish_done) {
4005  s->status = FINISH_STATE;
4006  }
4007  if (bstate == need_more || bstate == finish_started) {
4008  if (strm->avail_out == 0) {
4009  s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
4010  }
4011  return Z_OK;
4012  /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
4013  * of deflate should use the same flush parameter to make sure
4014  * that the flush is complete. So we don't have to output an
4015  * empty block here, this will be done at next call. This also
4016  * ensures that for a very small output buffer, we emit at most
4017  * one empty block.
4018  */
4019  }
4020  if (bstate == block_done) {
4021  if (flush == Z_PARTIAL_FLUSH) {
4022  _tr_align(s);
4023  } else { /* FULL_FLUSH or SYNC_FLUSH */
4024  _tr_stored_block(s, (char*)0, 0L, 0);
4025  /* For a full flush, this empty block will be recognized
4026  * as a special marker by inflate_sync().
4027  */
4028  if (flush == Z_FULL_FLUSH) {
4029  CLEAR_HASH(s); /* forget history */
4030  }
4031  }
4032  flush_pending(strm);
4033  if (strm->avail_out == 0) {
4034  s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
4035  return Z_OK;
4036  }
4037  }
4038  }
4039  Assert(strm->avail_out > 0, "bug2");
4040 
4041  if (flush != Z_FINISH) return Z_OK;
4042  if (s->wrap <= 0) return Z_STREAM_END;
4043 
4044  /* Write the trailer */
4045  {
4046  putShortMSB(s, (uInt)(strm->adler >> 16));
4047  putShortMSB(s, (uInt)(strm->adler & 0xffff));
4048  }
4049  flush_pending(strm);
4050  /* If avail_out is zero, the application will call deflate again
4051  * to flush the rest.
4052  */
4053  if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
4054  return s->pending != 0 ? Z_OK : Z_STREAM_END;
4055 }
4056 
4057 /* ========================================================================= */
4058 int deflateEnd (strm)
4059  z_streamp strm;
4060 {
4061  int status;
4062 
4063  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
4064 
4065  status = strm->state->status;
4066  if (status != INIT_STATE &&
4067  status != EXTRA_STATE &&
4068  status != NAME_STATE &&
4069  status != COMMENT_STATE &&
4070  status != HCRC_STATE &&
4071  status != BUSY_STATE &&
4072  status != FINISH_STATE) {
4073  return Z_STREAM_ERROR;
4074  }
4075 
4076  /* Deallocate in reverse order of allocations: */
4077  TRY_FREE(strm, strm->state->pending_buf);
4078  TRY_FREE(strm, strm->state->head);
4079  TRY_FREE(strm, strm->state->prev);
4080  TRY_FREE(strm, strm->state->window);
4081 
4082  ZFREE(strm, strm->state);
4083  strm->state = Z_NULL;
4084 
4085  return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
4086 }
4087 
4088 /* =========================================================================
4089  * Copy the source state to the destination state.
4090  * To simplify the source, this is not supported for 16-bit MSDOS (which
4091  * doesn't have enough memory anyway to duplicate compression states).
4092  */
4093 int deflateCopy (dest, source)
4094  z_streamp dest;
4095  z_streamp source;
4096 {
4097 #ifdef MAXSEG_64K
4098  return Z_STREAM_ERROR;
4099 #else
4100  deflate_state *ds;
4101  deflate_state *ss;
4102  ushf *overlay;
4103 
4104 
4105  if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
4106  return Z_STREAM_ERROR;
4107  }
4108 
4109  ss = source->state;
4110 
4111  zmemcpy(dest, source, sizeof(z_stream));
4112 
4113  ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
4114  if (ds == Z_NULL) return Z_MEM_ERROR;
4115  dest->state = (struct internal_state FAR *) ds;
4116  zmemcpy(ds, ss, sizeof(deflate_state));
4117  ds->strm = dest;
4118 
4119  ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
4120  ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
4121  ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
4122  overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
4123  ds->pending_buf = (uchf *) overlay;
4124 
4125  if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
4126  ds->pending_buf == Z_NULL) {
4127  deflateEnd (dest);
4128  return Z_MEM_ERROR;
4129  }
4130  /* following zmemcpy do not work for 16-bit MSDOS */
4131  zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
4132  zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
4133  zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
4134  zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
4135 
4136  ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
4137  ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
4138  ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
4139 
4140  ds->l_desc.dyn_tree = ds->dyn_ltree;
4141  ds->d_desc.dyn_tree = ds->dyn_dtree;
4142  ds->bl_desc.dyn_tree = ds->bl_tree;
4143 
4144  return Z_OK;
4145 #endif /* MAXSEG_64K */
4146 }
4147 
4148 /* ===========================================================================
4149  * Read a new buffer from the current input stream, update the adler32
4150  * and total number of bytes read. All deflate() input goes through
4151  * this function so some applications may wish to modify it to avoid
4152  * allocating a large strm->next_in buffer and copying from it.
4153  * (See also flush_pending()).
4154  */
4155 local int read_buf(strm, buf, size)
4156  z_streamp strm;
4157  Bytef *buf;
4158  unsigned size;
4159 {
4160  unsigned len = strm->avail_in;
4161 
4162  if (len > size) len = size;
4163  if (len == 0) return 0;
4164 
4165  strm->avail_in -= len;
4166 
4167  if (strm->state->wrap == 1) {
4168  strm->adler = adler32(strm->adler, strm->next_in, len);
4169  }
4170  zmemcpy(buf, strm->next_in, len);
4171  strm->next_in += len;
4172  strm->total_in += len;
4173 
4174  return (int)len;
4175 }
4176 
4177 /* ===========================================================================
4178  * Initialize the "longest match" routines for a new zlib stream
4179  */
4180 local void lm_init (s)
4181  deflate_state *s;
4182 {
4183  s->window_size = (ulg)2L*s->w_size;
4184 
4185  CLEAR_HASH(s);
4186 
4187  /* Set the default configuration parameters:
4188  */
4189  s->max_lazy_match = configuration_table[s->level].max_lazy;
4190  s->good_match = configuration_table[s->level].good_length;
4191  s->nice_match = configuration_table[s->level].nice_length;
4192  s->max_chain_length = configuration_table[s->level].max_chain;
4193 
4194  s->strstart = 0;
4195  s->block_start = 0L;
4196  s->lookahead = 0;
4197  s->match_length = s->prev_length = MIN_MATCH-1;
4198  s->match_available = 0;
4199  s->ins_h = 0;
4200 #ifndef FASTEST
4201 #ifdef ASMV
4202  match_init(); /* initialize the asm code */
4203 #endif
4204 #endif
4205 }
4206 
4207 #ifndef FASTEST
4208 /* ===========================================================================
4209  * Set match_start to the longest match starting at the given string and
4210  * return its length. Matches shorter or equal to prev_length are discarded,
4211  * in which case the result is equal to prev_length and match_start is
4212  * garbage.
4213  * IN assertions: cur_match is the head of the hash chain for the current
4214  * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
4215  * OUT assertion: the match length is not greater than s->lookahead.
4216  */
4217 #ifndef ASMV
4218 /* For 80x86 and 680x0, an optimized version will be provided in match.asm or
4219  * match.S. The code will be functionally equivalent.
4220  */
4221 local uInt longest_match(s, cur_match)
4222  deflate_state *s;
4223  IPos cur_match; /* current match */
4224 {
4225  unsigned chain_length = s->max_chain_length;/* max hash chain length */
4226  register Bytef *scan = s->window + s->strstart; /* current string */
4227  register Bytef *match; /* matched string */
4228  register int len; /* length of current match */
4229  int best_len = s->prev_length; /* best match length so far */
4230  int nice_match = s->nice_match; /* stop if match long enough */
4231  IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
4232  s->strstart - (IPos)MAX_DIST(s) : NIL;
4233  /* Stop when cur_match becomes <= limit. To simplify the code,
4234  * we prevent matches with the string of window index 0.
4235  */
4236  Posf *prev = s->prev;
4237  uInt wmask = s->w_mask;
4238 
4239 #ifdef UNALIGNED_OK
4240  /* Compare two bytes at a time. Note: this is not always beneficial.
4241  * Try with and without -DUNALIGNED_OK to check.
4242  */
4243  register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
4244  register ush scan_start = *(ushf*)scan;
4245  register ush scan_end = *(ushf*)(scan+best_len-1);
4246 #else
4247  register Bytef *strend = s->window + s->strstart + MAX_MATCH;
4248  register Byte scan_end1 = scan[best_len-1];
4249  register Byte scan_end = scan[best_len];
4250 #endif
4251 
4252  /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
4253  * It is easy to get rid of this optimization if necessary.
4254  */
4255  Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
4256 
4257  /* Do not waste too much time if we already have a good match: */
4258  if (s->prev_length >= s->good_match) {
4259  chain_length >>= 2;
4260  }
4261  /* Do not look for matches beyond the end of the input. This is necessary
4262  * to make deflate deterministic.
4263  */
4264  if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
4265 
4266  Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
4267 
4268  do {
4269  Assert(cur_match < s->strstart, "no future");
4270  match = s->window + cur_match;
4271 
4272  /* Skip to next match if the match length cannot increase
4273  * or if the match length is less than 2. Note that the checks below
4274  * for insufficient lookahead only occur occasionally for performance
4275  * reasons. Therefore uninitialized memory will be accessed, and
4276  * conditional jumps will be made that depend on those values.
4277  * However the length of the match is limited to the lookahead, so
4278  * the output of deflate is not affected by the uninitialized values.
4279  */
4280 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
4281  /* This code assumes sizeof(unsigned short) == 2. Do not use
4282  * UNALIGNED_OK if your compiler uses a different size.
4283  */
4284  if (*(ushf*)(match+best_len-1) != scan_end ||
4285  *(ushf*)match != scan_start) continue;
4286 
4287  /* It is not necessary to compare scan[2] and match[2] since they are
4288  * always equal when the other bytes match, given that the hash keys
4289  * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
4290  * strstart+3, +5, ... up to strstart+257. We check for insufficient
4291  * lookahead only every 4th comparison; the 128th check will be made
4292  * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
4293  * necessary to put more guard bytes at the end of the window, or
4294  * to check more often for insufficient lookahead.
4295  */
4296  Assert(scan[2] == match[2], "scan[2]?");
4297  scan++, match++;
4298  do {
4299  } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
4300  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
4301  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
4302  *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
4303  scan < strend);
4304  /* The funny "do {}" generates better code on most compilers */
4305 
4306  /* Here, scan <= window+strstart+257 */
4307  Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
4308  if (*scan == *match) scan++;
4309 
4310  len = (MAX_MATCH - 1) - (int)(strend-scan);
4311  scan = strend - (MAX_MATCH-1);
4312 
4313 #else /* UNALIGNED_OK */
4314 
4315  if (match[best_len] != scan_end ||
4316  match[best_len-1] != scan_end1 ||
4317  *match != *scan ||
4318  *++match != scan[1]) continue;
4319 
4320  /* The check at best_len-1 can be removed because it will be made
4321  * again later. (This heuristic is not always a win.)
4322  * It is not necessary to compare scan[2] and match[2] since they
4323  * are always equal when the other bytes match, given that
4324  * the hash keys are equal and that HASH_BITS >= 8.
4325  */
4326  scan += 2, match++;
4327  Assert(*scan == *match, "match[2]?");
4328 
4329  /* We check for insufficient lookahead only every 8th comparison;
4330  * the 256th check will be made at strstart+258.
4331  */
4332  do {
4333  } while (*++scan == *++match && *++scan == *++match &&
4334  *++scan == *++match && *++scan == *++match &&
4335  *++scan == *++match && *++scan == *++match &&
4336  *++scan == *++match && *++scan == *++match &&
4337  scan < strend);
4338 
4339  Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
4340 
4341  len = MAX_MATCH - (int)(strend - scan);
4342  scan = strend - MAX_MATCH;
4343 
4344 #endif /* UNALIGNED_OK */
4345 
4346  if (len > best_len) {
4347  s->match_start = cur_match;
4348  best_len = len;
4349  if (len >= nice_match) break;
4350 #ifdef UNALIGNED_OK
4351  scan_end = *(ushf*)(scan+best_len-1);
4352 #else
4353  scan_end1 = scan[best_len-1];
4354  scan_end = scan[best_len];
4355 #endif
4356  }
4357  } while ((cur_match = prev[cur_match & wmask]) > limit
4358  && --chain_length != 0);
4359 
4360  if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
4361  return s->lookahead;
4362 }
4363 #endif /* ASMV */
4364 #endif /* FASTEST */
4365 
4366 /* ---------------------------------------------------------------------------
4367  * Optimized version for level == 1 or strategy == Z_RLE only
4368  */
4369 local uInt longest_match_fast(s, cur_match)
4370  deflate_state *s;
4371  IPos cur_match; /* current match */
4372 {
4373  register Bytef *scan = s->window + s->strstart; /* current string */
4374  register Bytef *match; /* matched string */
4375  register int len; /* length of current match */
4376  register Bytef *strend = s->window + s->strstart + MAX_MATCH;
4377 
4378  /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
4379  * It is easy to get rid of this optimization if necessary.
4380  */
4381  Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
4382 
4383  Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
4384 
4385  Assert(cur_match < s->strstart, "no future");
4386 
4387  match = s->window + cur_match;
4388 
4389  /* Return failure if the match length is less than 2:
4390  */
4391  if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
4392 
4393  /* The check at best_len-1 can be removed because it will be made
4394  * again later. (This heuristic is not always a win.)
4395  * It is not necessary to compare scan[2] and match[2] since they
4396  * are always equal when the other bytes match, given that
4397  * the hash keys are equal and that HASH_BITS >= 8.
4398  */
4399  scan += 2, match += 2;
4400  Assert(*scan == *match, "match[2]?");
4401 
4402  /* We check for insufficient lookahead only every 8th comparison;
4403  * the 256th check will be made at strstart+258.
4404  */
4405  do {
4406  } while (*++scan == *++match && *++scan == *++match &&
4407  *++scan == *++match && *++scan == *++match &&
4408  *++scan == *++match && *++scan == *++match &&
4409  *++scan == *++match && *++scan == *++match &&
4410  scan < strend);
4411 
4412  Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
4413 
4414  len = MAX_MATCH - (int)(strend - scan);
4415 
4416  if (len < MIN_MATCH) return MIN_MATCH - 1;
4417 
4418  s->match_start = cur_match;
4419  return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
4420 }
4421 
4422 #ifdef DEBUG
4423 /* ===========================================================================
4424  * Check that the match at match_start is indeed a match.
4425  */
4426 local void check_match(s, start, match, length)
4427  deflate_state *s;
4428  IPos start, match;
4429  int length;
4430 {
4431  /* check that the match is indeed a match */
4432  if (zmemcmp(s->window + match,
4433  s->window + start, length) != EQUAL) {
4434  fprintf(stderr, " start %u, match %u, length %d\n",
4435  start, match, length);
4436  do {
4437  fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
4438  } while (--length != 0);
4439  z_error("invalid match");
4440  }
4441  if (z_verbose > 1) {
4442  fprintf(stderr,"\\[%d,%d]", start-match, length);
4443  do { putc(s->window[start++], stderr); } while (--length != 0);
4444  }
4445 }
4446 #else
4447 # define check_match(s, start, match, length)
4448 #endif /* DEBUG */
4449 
4450 /* ===========================================================================
4451  * Fill the window when the lookahead becomes insufficient.
4452  * Updates strstart and lookahead.
4453  *
4454  * IN assertion: lookahead < MIN_LOOKAHEAD
4455  * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
4456  * At least one byte has been read, or avail_in == 0; reads are
4457  * performed for at least two bytes (required for the zip translate_eol
4458  * option -- not supported here).
4459  */
4460 local void fill_window(s)
4461  deflate_state *s;
4462 {
4463  register unsigned n, m;
4464  register Posf *p;
4465  unsigned more; /* Amount of free space at the end of the window. */
4466  uInt wsize = s->w_size;
4467 
4468  do {
4469  more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
4470 
4471  /* Deal with !@#$% 64K limit: */
4472  if (sizeof(int) <= 2) {
4473  if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
4474  more = wsize;
4475 
4476  } else if (more == (unsigned)(-1)) {
4477  /* Very unlikely, but possible on 16 bit machine if
4478  * strstart == 0 && lookahead == 1 (input done a byte at time)
4479  */
4480  more--;
4481  }
4482  }
4483 
4484  /* If the window is almost full and there is insufficient lookahead,
4485  * move the upper half to the lower one to make room in the upper half.
4486  */
4487  if (s->strstart >= wsize+MAX_DIST(s)) {
4488 
4489  zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
4490  s->match_start -= wsize;
4491  s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
4492  s->block_start -= (long) wsize;
4493 
4494  /* Slide the hash table (could be avoided with 32 bit values
4495  at the expense of memory usage). We slide even when level == 0
4496  to keep the hash table consistent if we switch back to level > 0
4497  later. (Using level 0 permanently is not an optimal usage of
4498  zlib, so we don't care about this pathological case.)
4499  */
4500  /* %%% avoid this when Z_RLE */
4501  n = s->hash_size;
4502  p = &s->head[n];
4503  do {
4504  m = *--p;
4505  *p = (Pos)(m >= wsize ? m-wsize : NIL);
4506  } while (--n);
4507 
4508  n = wsize;
4509 #ifndef FASTEST
4510  p = &s->prev[n];
4511  do {
4512  m = *--p;
4513  *p = (Pos)(m >= wsize ? m-wsize : NIL);
4514  /* If n is not on any hash chain, prev[n] is garbage but
4515  * its value will never be used.
4516  */
4517  } while (--n);
4518 #endif
4519  more += wsize;
4520  }
4521  if (s->strm->avail_in == 0) return;
4522 
4523  /* If there was no sliding:
4524  * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
4525  * more == window_size - lookahead - strstart
4526  * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
4527  * => more >= window_size - 2*WSIZE + 2
4528  * In the BIG_MEM or MMAP case (not yet supported),
4529  * window_size == input_size + MIN_LOOKAHEAD &&
4530  * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
4531  * Otherwise, window_size == 2*WSIZE so more >= 2.
4532  * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
4533  */
4534  Assert(more >= 2, "more < 2");
4535 
4536  n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
4537  s->lookahead += n;
4538 
4539  /* Initialize the hash value now that we have some input: */
4540  if (s->lookahead >= MIN_MATCH) {
4541  s->ins_h = s->window[s->strstart];
4542  UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
4543 #if MIN_MATCH != 3
4544  Call UPDATE_HASH() MIN_MATCH-3 more times
4545 #endif
4546  }
4547  /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
4548  * but this is not important since only literal bytes will be emitted.
4549  */
4550 
4551  } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
4552 }
4553 
4554 /* ===========================================================================
4555  * Flush the current block, with given end-of-file flag.
4556  * IN assertion: strstart is set to the end of the current match.
4557  */
4558 #define FLUSH_BLOCK_ONLY(s, eof) { \
4559  _tr_flush_block(s, (s->block_start >= 0L ? \
4560  (charf *)&s->window[(unsigned)s->block_start] : \
4561  (charf *)Z_NULL), \
4562  (ulg)((long)s->strstart - s->block_start), \
4563  (eof)); \
4564  s->block_start = s->strstart; \
4565  flush_pending(s->strm); \
4566  Tracev((stderr,"[FLUSH]")); \
4567 }
4568 
4569 /* Same but force premature exit if necessary. */
4570 #define FLUSH_BLOCK(s, eof) { \
4571  FLUSH_BLOCK_ONLY(s, eof); \
4572  if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
4573 }
4574 
4575 /* ===========================================================================
4576  * Copy without compression as much as possible from the input stream, return
4577  * the current block state.
4578  * This function does not insert new strings in the dictionary since
4579  * uncompressible data is probably not useful. This function is used
4580  * only for the level=0 compression option.
4581  * NOTE: this function should be optimized to avoid extra copying from
4582  * window to pending_buf.
4583  */
4584 local block_state deflate_stored(s, flush)
4585  deflate_state *s;
4586  int flush;
4587 {
4588  /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
4589  * to pending_buf_size, and each stored block has a 5 byte header:
4590  */
4591  ulg max_block_size = 0xffff;
4592  ulg max_start;
4593 
4594  if (max_block_size > s->pending_buf_size - 5) {
4595  max_block_size = s->pending_buf_size - 5;
4596  }
4597 
4598  /* Copy as much as possible from input to output: */
4599  for (;;) {
4600  /* Fill the window as much as possible: */
4601  if (s->lookahead <= 1) {
4602 
4603  Assert(s->strstart < s->w_size+MAX_DIST(s) ||
4604  s->block_start >= (long)s->w_size, "slide too late");
4605 
4606  fill_window(s);
4607  if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
4608 
4609  if (s->lookahead == 0) break; /* flush the current block */
4610  }
4611  Assert(s->block_start >= 0L, "block gone");
4612 
4613  s->strstart += s->lookahead;
4614  s->lookahead = 0;
4615 
4616  /* Emit a stored block if pending_buf will be full: */
4617  max_start = s->block_start + max_block_size;
4618  if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
4619  /* strstart == 0 is possible when wraparound on 16-bit machine */
4620  s->lookahead = (uInt)(s->strstart - max_start);
4621  s->strstart = (uInt)max_start;
4622  FLUSH_BLOCK(s, 0);
4623  }
4624  /* Flush if we may have to slide, otherwise block_start may become
4625  * negative and the data will be gone:
4626  */
4627  if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
4628  FLUSH_BLOCK(s, 0);
4629  }
4630  }
4631  FLUSH_BLOCK(s, flush == Z_FINISH);
4632  return flush == Z_FINISH ? finish_done : block_done;
4633 }
4634 
4635 /* ===========================================================================
4636  * Compress as much as possible from the input stream, return the current
4637  * block state.
4638  * This function does not perform lazy evaluation of matches and inserts
4639  * new strings in the dictionary only for unmatched strings or for short
4640  * matches. It is used only for the fast compression options.
4641  */
4642 local block_state deflate_fast(s, flush)
4643  deflate_state *s;
4644  int flush;
4645 {
4646  IPos hash_head = NIL; /* head of the hash chain */
4647  int bflush; /* set if current block must be flushed */
4648 
4649  for (;;) {
4650  /* Make sure that we always have enough lookahead, except
4651  * at the end of the input file. We need MAX_MATCH bytes
4652  * for the next match, plus MIN_MATCH bytes to insert the
4653  * string following the next match.
4654  */
4655  if (s->lookahead < MIN_LOOKAHEAD) {
4656  fill_window(s);
4657  if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
4658  return need_more;
4659  }
4660  if (s->lookahead == 0) break; /* flush the current block */
4661  }
4662 
4663  /* Insert the string window[strstart .. strstart+2] in the
4664  * dictionary, and set hash_head to the head of the hash chain:
4665  */
4666  if (s->lookahead >= MIN_MATCH) {
4667  INSERT_STRING(s, s->strstart, hash_head);
4668  }
4669 
4670  /* Find the longest match, discarding those <= prev_length.
4671  * At this point we have always match_length < MIN_MATCH
4672  */
4673  if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
4674  /* To simplify the code, we prevent matches with the string
4675  * of window index 0 (in particular we have to avoid a match
4676  * of the string with itself at the start of the input file).
4677  */
4678 #ifdef FASTEST
4679  if ((s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) ||
4680  (s->strategy == Z_RLE && s->strstart - hash_head == 1)) {
4681  s->match_length = longest_match_fast (s, hash_head);
4682  }
4683 #else
4684  if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
4685  s->match_length = longest_match (s, hash_head);
4686  } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
4687  s->match_length = longest_match_fast (s, hash_head);
4688  }
4689 #endif
4690  /* longest_match() or longest_match_fast() sets match_start */
4691  }
4692  if (s->match_length >= MIN_MATCH) {
4693  check_match(s, s->strstart, s->match_start, s->match_length);
4694 
4695  _tr_tally_dist(s, s->strstart - s->match_start,
4696  s->match_length - MIN_MATCH, bflush);
4697 
4698  s->lookahead -= s->match_length;
4699 
4700  /* Insert new strings in the hash table only if the match length
4701  * is not too large. This saves time but degrades compression.
4702  */
4703 #ifndef FASTEST
4704  if (s->match_length <= s->max_insert_length &&
4705  s->lookahead >= MIN_MATCH) {
4706  s->match_length--; /* string at strstart already in table */
4707  do {
4708  s->strstart++;
4709  INSERT_STRING(s, s->strstart, hash_head);
4710  /* strstart never exceeds WSIZE-MAX_MATCH, so there are
4711  * always MIN_MATCH bytes ahead.
4712  */
4713  } while (--s->match_length != 0);
4714  s->strstart++;
4715  } else
4716 #endif
4717  {
4718  s->strstart += s->match_length;
4719  s->match_length = 0;
4720  s->ins_h = s->window[s->strstart];
4721  UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
4722 #if MIN_MATCH != 3
4723  Call UPDATE_HASH() MIN_MATCH-3 more times
4724 #endif
4725  /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
4726  * matter since it will be recomputed at next deflate call.
4727  */
4728  }
4729  } else {
4730  /* No match, output a literal byte */
4731  Tracevv((stderr,"%c", s->window[s->strstart]));
4732  _tr_tally_lit (s, s->window[s->strstart], bflush);
4733  s->lookahead--;
4734  s->strstart++;
4735  }
4736  if (bflush) FLUSH_BLOCK(s, 0);
4737  }
4738  FLUSH_BLOCK(s, flush == Z_FINISH);
4739  return flush == Z_FINISH ? finish_done : block_done;
4740 }
4741 
4742 #ifndef FASTEST
4743 /* ===========================================================================
4744  * Same as above, but achieves better compression. We use a lazy
4745  * evaluation for matches: a match is finally adopted only if there is
4746  * no better match at the next window position.
4747  */
4748 local block_state deflate_slow(s, flush)
4749  deflate_state *s;
4750  int flush;
4751 {
4752  IPos hash_head = NIL; /* head of hash chain */
4753  int bflush; /* set if current block must be flushed */
4754 
4755  /* Process the input block. */
4756  for (;;) {
4757  /* Make sure that we always have enough lookahead, except
4758  * at the end of the input file. We need MAX_MATCH bytes
4759  * for the next match, plus MIN_MATCH bytes to insert the
4760  * string following the next match.
4761  */
4762  if (s->lookahead < MIN_LOOKAHEAD) {
4763  fill_window(s);
4764  if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
4765  return need_more;
4766  }
4767  if (s->lookahead == 0) break; /* flush the current block */
4768  }
4769 
4770  /* Insert the string window[strstart .. strstart+2] in the
4771  * dictionary, and set hash_head to the head of the hash chain:
4772  */
4773  if (s->lookahead >= MIN_MATCH) {
4774  INSERT_STRING(s, s->strstart, hash_head);
4775  }
4776 
4777  /* Find the longest match, discarding those <= prev_length.
4778  */
4779  s->prev_length = s->match_length, s->prev_match = s->match_start;
4780  s->match_length = MIN_MATCH-1;
4781 
4782  if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
4783  s->strstart - hash_head <= MAX_DIST(s)) {
4784  /* To simplify the code, we prevent matches with the string
4785  * of window index 0 (in particular we have to avoid a match
4786  * of the string with itself at the start of the input file).
4787  */
4788  if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
4789  s->match_length = longest_match (s, hash_head);
4790  } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
4791  s->match_length = longest_match_fast (s, hash_head);
4792  }
4793  /* longest_match() or longest_match_fast() sets match_start */
4794 
4795  if (s->match_length <= 5 && (s->strategy == Z_FILTERED
4796 #if TOO_FAR <= 32767
4797  || (s->match_length == MIN_MATCH &&
4798  s->strstart - s->match_start > TOO_FAR)
4799 #endif
4800  )) {
4801 
4802  /* If prev_match is also MIN_MATCH, match_start is garbage
4803  * but we will ignore the current match anyway.
4804  */
4805  s->match_length = MIN_MATCH-1;
4806  }
4807  }
4808  /* If there was a match at the previous step and the current
4809  * match is not better, output the previous match:
4810  */
4811  if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
4812  uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
4813  /* Do not insert strings in hash table beyond this. */
4814 
4815  check_match(s, s->strstart-1, s->prev_match, s->prev_length);
4816 
4817  _tr_tally_dist(s, s->strstart -1 - s->prev_match,
4818  s->prev_length - MIN_MATCH, bflush);
4819 
4820  /* Insert in hash table all strings up to the end of the match.
4821  * strstart-1 and strstart are already inserted. If there is not
4822  * enough lookahead, the last two strings are not inserted in
4823  * the hash table.
4824  */
4825  s->lookahead -= s->prev_length-1;
4826  s->prev_length -= 2;
4827  do {
4828  if (++s->strstart <= max_insert) {
4829  INSERT_STRING(s, s->strstart, hash_head);
4830  }
4831  } while (--s->prev_length != 0);
4832  s->match_available = 0;
4833  s->match_length = MIN_MATCH-1;
4834  s->strstart++;
4835 
4836  if (bflush) FLUSH_BLOCK(s, 0);
4837 
4838  } else if (s->match_available) {
4839  /* If there was no match at the previous position, output a
4840  * single literal. If there was a match but the current match
4841  * is longer, truncate the previous match to a single literal.
4842  */
4843  Tracevv((stderr,"%c", s->window[s->strstart-1]));
4844  _tr_tally_lit(s, s->window[s->strstart-1], bflush);
4845  if (bflush) {
4846  FLUSH_BLOCK_ONLY(s, 0);
4847  }
4848  s->strstart++;
4849  s->lookahead--;
4850  if (s->strm->avail_out == 0) return need_more;
4851  } else {
4852  /* There is no previous match to compare with, wait for
4853  * the next step to decide.
4854  */
4855  s->match_available = 1;
4856  s->strstart++;
4857  s->lookahead--;
4858  }
4859  }
4860  Assert (flush != Z_NO_FLUSH, "no flush?");
4861  if (s->match_available) {
4862  Tracevv((stderr,"%c", s->window[s->strstart-1]));
4863  _tr_tally_lit(s, s->window[s->strstart-1], bflush);
4864  s->match_available = 0;
4865  }
4866  FLUSH_BLOCK(s, flush == Z_FINISH);
4867  return flush == Z_FINISH ? finish_done : block_done;
4868 }
4869 #endif /* FASTEST */
4870 
4871 /* inftrees.h -- header to use inftrees.c
4872  * Copyright (C) 1995-2005 Mark Adler
4873  * For conditions of distribution and use, see copyright notice in zlib.h
4874  */
4875 
4876 /* Structure for decoding tables. Each entry provides either the
4877  information needed to do the operation requested by the code that
4878  indexed that table entry, or it provides a pointer to another
4879  table that indexes more bits of the code. op indicates whether
4880  the entry is a pointer to another table, a literal, a length or
4881  distance, an end-of-block, or an invalid code. For a table
4882  pointer, the low four bits of op is the number of index bits of
4883  that table. For a length or distance, the low four bits of op
4884  is the number of extra bits to get after the code. bits is
4885  the number of bits in this code or part of the code to drop off
4886  of the bit buffer. val is the actual byte to output in the case
4887  of a literal, the base length or distance, or the offset from
4888  the current table to the next table. Each entry is four bytes. */
4889 typedef struct {
4890  unsigned char op; /* operation, extra bits, table bits */
4891  unsigned char bits; /* bits in this part of the code */
4892  unsigned short val; /* offset in table or code value */
4893 } code;
4894 
4895 /* op values as set by inflate_table():
4896  00000000 - literal
4897  0000tttt - table link, tttt != 0 is the number of table index bits
4898  0001eeee - length or distance, eeee is the number of extra bits
4899  01100000 - end of block
4900  01000000 - invalid code
4901  */
4902 
4903 /* Maximum size of dynamic tree. The maximum found in a long but non-
4904  exhaustive search was 1444 code structures (852 for length/literals
4905  and 592 for distances, the latter actually the result of an
4906  exhaustive search). The true maximum is not known, but the value
4907  below is more than safe. */
4908 #define ENOUGH 2048
4909 #define MAXD 592
4910 
4911 /* Type of code to build for inftable() */
4912 typedef enum {
4913  CODES,
4914  LENS,
4915  DISTS
4916 } codetype;
4917 
4918 int inflate_table OF((codetype type, unsigned short FAR *lens,
4919  unsigned codes, code FAR * FAR *table,
4920  unsigned FAR *bits, unsigned short FAR *work));
4921 
4922 
4923 /* inflate.h -- internal inflate state definition
4924  * Copyright (C) 1995-2004 Mark Adler
4925  * For conditions of distribution and use, see copyright notice in zlib.h
4926  */
4927 
4928 /* Possible inflate modes between inflate() calls */
4929 typedef enum {
4930  HEAD, /* i: waiting for magic header */
4931  FLAGS, /* i: waiting for method and flags (gzip) */
4932  TIME, /* i: waiting for modification time (gzip) */
4933  OS, /* i: waiting for extra flags and operating system (gzip) */
4934  EXLEN, /* i: waiting for extra length (gzip) */
4935  EXTRA, /* i: waiting for extra bytes (gzip) */
4936  NAME, /* i: waiting for end of file name (gzip) */
4937  COMMENT, /* i: waiting for end of comment (gzip) */
4938  HCRC, /* i: waiting for header crc (gzip) */
4939  DICTID, /* i: waiting for dictionary check value */
4940  DICT, /* waiting for inflateSetDictionary() call */
4941  TYPE, /* i: waiting for type bits, including last-flag bit */
4942  TYPEDO, /* i: same, but skip check to exit inflate on new block */
4943  STORED, /* i: waiting for stored size (length and complement) */
4944  COPY, /* i/o: waiting for input or output to copy stored block */
4945  TABLE, /* i: waiting for dynamic block table lengths */
4946  LENLENS, /* i: waiting for code length code lengths */
4947  CODELENS, /* i: waiting for length/lit and distance code lengths */
4948  LEN, /* i: waiting for length/lit code */
4949  LENEXT, /* i: waiting for length extra bits */
4950  DIST, /* i: waiting for distance code */
4951  DISTEXT, /* i: waiting for distance extra bits */
4952  MATCH, /* o: waiting for output space to copy string */
4953  LIT, /* o: waiting for output space to write literal */
4954  CHECK, /* i: waiting for 32-bit check value */
4955  LENGTH, /* i: waiting for 32-bit length (gzip) */
4956  DONE, /* finished check, done -- remain here until reset */
4957  BAD, /* got a data error -- remain here until reset */
4958  MEM, /* got an inflate() memory error -- remain here until reset */
4959  SYNC /* looking for synchronization bytes to restart inflate() */
4960 } inflate_mode;
4961 
4962 /*
4963  State transitions between above modes -
4964 
4965  (most modes can go to the BAD or MEM mode -- not shown for clarity)
4966 
4967  Process header:
4968  HEAD -> (gzip) or (zlib)
4969  (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME
4970  NAME -> COMMENT -> HCRC -> TYPE
4971  (zlib) -> DICTID or TYPE
4972  DICTID -> DICT -> TYPE
4973  Read deflate blocks:
4974  TYPE -> STORED or TABLE or LEN or CHECK
4975  STORED -> COPY -> TYPE
4976  TABLE -> LENLENS -> CODELENS -> LEN
4977  Read deflate codes:
4978  LEN -> LENEXT or LIT or TYPE
4979  LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
4980  LIT -> LEN
4981  Process trailer:
4982  CHECK -> LENGTH -> DONE
4983  */
4984 
4985 /* state maintained between inflate() calls. Approximately 7K bytes. */
4986 struct inflate_state {
4987  inflate_mode mode; /* current inflate mode */
4988  int last; /* true if processing last block */
4989  int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
4990  int havedict; /* true if dictionary provided */
4991  int flags; /* gzip header method and flags (0 if zlib) */
4992  unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */
4993  unsigned long check; /* protected copy of check value */
4994  unsigned long total; /* protected copy of output count */
4995  gz_headerp head; /* where to save gzip header information */
4996  /* sliding window */
4997  unsigned wbits; /* log base 2 of requested window size */
4998  unsigned wsize; /* window size or zero if not using window */
4999  unsigned whave; /* valid bytes in the window */
5000  unsigned write; /* window write index */
5001  unsigned char FAR *window; /* allocated sliding window, if needed */
5002  /* bit accumulator */
5003  unsigned long hold; /* input bit accumulator */
5004  unsigned bits; /* number of bits in "in" */
5005  /* for string and stored block copying */
5006  unsigned length; /* literal or length of data to copy */
5007  unsigned offset; /* distance back to copy string from */
5008  /* for table and code decoding */
5009  unsigned extra; /* extra bits needed */
5010  /* fixed and dynamic code tables */
5011  code const FAR *lencode; /* starting table for length/literal codes */
5012  code const FAR *distcode; /* starting table for distance codes */
5013  unsigned lenbits; /* index bits for lencode */
5014  unsigned distbits; /* index bits for distcode */
5015  /* dynamic table building */
5016  unsigned ncode; /* number of code length code lengths */
5017  unsigned nlen; /* number of length code lengths */
5018  unsigned ndist; /* number of distance code lengths */
5019  unsigned have; /* number of code lengths in lens[] */
5020  code FAR *next; /* next available space in codes[] */
5021  unsigned short lens[320]; /* temporary storage for code lengths */
5022  unsigned short work[288]; /* work area for code table building */
5023  code codes[ENOUGH]; /* space for code tables */
5024 };
5025 
5026 /* inffast.h -- header to use inffast.c
5027  * Copyright (C) 1995-2003 Mark Adler
5028  * For conditions of distribution and use, see copyright notice in zlib.h
5029  */
5030 
5031 void inflate_fast OF((z_streamp strm, unsigned start));
5032 
5033 /* inflate.c -- zlib decompression
5034  * Copyright (C) 1995-2005 Mark Adler
5035  * For conditions of distribution and use, see copyright notice in zlib.h
5036  */
5037 
5038 /*
5039  * Change history:
5040  *
5041  * 1.2.beta0 24 Nov 2002
5042  * - First version -- complete rewrite of inflate to simplify code, avoid
5043  * creation of window when not needed, minimize use of window when it is
5044  * needed, make inffast.c even faster, implement gzip decoding, and to
5045  * improve code readability and style over the previous zlib inflate code
5046  *
5047  * 1.2.beta1 25 Nov 2002
5048  * - Use pointers for available input and output checking in inffast.c
5049  * - Remove input and output counters in inffast.c
5050  * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
5051  * - Remove unnecessary second byte pull from length extra in inffast.c
5052  * - Unroll direct copy to three copies per loop in inffast.c
5053  *
5054  * 1.2.beta2 4 Dec 2002
5055  * - Change external routine names to reduce potential conflicts
5056  * - Correct filename to inffixed.h for fixed tables in inflate.c
5057  * - Make hbuf[] unsigned char to match parameter type in inflate.c
5058  * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
5059  * to avoid negation problem on Alphas (64 bit) in inflate.c
5060  *
5061  * 1.2.beta3 22 Dec 2002
5062  * - Add comments on state->bits assertion in inffast.c
5063  * - Add comments on op field in inftrees.h
5064  * - Fix bug in reuse of allocated window after inflateReset()
5065  * - Remove bit fields--back to byte structure for speed
5066  * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
5067  * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
5068  * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
5069  * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
5070  * - Use local copies of stream next and avail values, as well as local bit
5071  * buffer and bit count in inflate()--for speed when inflate_fast() not used
5072  *
5073  * 1.2.beta4 1 Jan 2003
5074  * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
5075  * - Move a comment on output buffer sizes from inffast.c to inflate.c
5076  * - Add comments in inffast.c to introduce the inflate_fast() routine
5077  * - Rearrange window copies in inflate_fast() for speed and simplification
5078  * - Unroll last copy for window match in inflate_fast()
5079  * - Use local copies of window variables in inflate_fast() for speed
5080  * - Pull out common write == 0 case for speed in inflate_fast()
5081  * - Make op and len in inflate_fast() unsigned for consistency
5082  * - Add FAR to lcode and dcode declarations in inflate_fast()
5083  * - Simplified bad distance check in inflate_fast()
5084  * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
5085  * source file infback.c to provide a call-back interface to inflate for
5086  * programs like gzip and unzip -- uses window as output buffer to avoid
5087  * window copying
5088  *
5089  * 1.2.beta5 1 Jan 2003
5090  * - Improved inflateBack() interface to allow the caller to provide initial
5091  * input in strm.
5092  * - Fixed stored blocks bug in inflateBack()
5093  *
5094  * 1.2.beta6 4 Jan 2003
5095  * - Added comments in inffast.c on effectiveness of POSTINC
5096  * - Typecasting all around to reduce compiler warnings
5097  * - Changed loops from while (1) or do {} while (1) to for (;;), again to
5098  * make compilers happy
5099  * - Changed type of window in inflateBackInit() to unsigned char *
5100  *
5101  * 1.2.beta7 27 Jan 2003
5102  * - Changed many types to unsigned or unsigned short to avoid warnings
5103  * - Added inflateCopy() function
5104  *
5105  * 1.2.0 9 Mar 2003
5106  * - Changed inflateBack() interface to provide separate opaque descriptors
5107  * for the in() and out() functions
5108  * - Changed inflateBack() argument and in_func typedef to swap the length
5109  * and buffer address return values for the input function
5110  * - Check next_in and next_out for Z_NULL on entry to inflate()
5111  *
5112  * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
5113  */
5114 
5115 #define MAXBITS 15
5116 
5117 const char inflate_copyright[] =
5118  " inflate 1.2.3 Copyright 1995-2005 Mark Adler ";
5119 /*
5120  If you use the zlib library in a product, an acknowledgment is welcome
5121  in the documentation of your product. If for some reason you cannot
5122  include such an acknowledgment, I would appreciate that you keep this
5123  copyright string in the executable of your product.
5124  */
5125 
5126 /*
5127  Build a set of tables to decode the provided canonical Huffman code.
5128  The code lengths are lens[0..codes-1]. The result starts at *table,
5129  whose indices are 0..2^bits-1. work is a writable array of at least
5130  lens shorts, which is used as a work area. type is the type of code
5131  to be generated, CODES, LENS, or DISTS. On return, zero is success,
5132  -1 is an invalid code, and +1 means that ENOUGH isn't enough. table
5133  on return points to the next available entry's address. bits is the
5134  requested root table index bits, and on return it is the actual root
5135  table index bits. It will differ if the request is greater than the
5136  longest code or if it is less than the shortest code.
5137  */
5138 int inflate_table(type, lens, codes, table, bits, work)
5139 codetype type;
5140 unsigned short FAR *lens;
5141 unsigned codes;
5142 code FAR * FAR *table;
5143 unsigned FAR *bits;
5144 unsigned short FAR *work;
5145 {
5146  unsigned len; /* a code's length in bits */
5147  unsigned sym; /* index of code symbols */
5148  unsigned min, max; /* minimum and maximum code lengths */
5149  unsigned root; /* number of index bits for root table */
5150  unsigned curr; /* number of index bits for current table */
5151  unsigned drop; /* code bits to drop for sub-table */
5152  int left; /* number of prefix codes available */
5153  unsigned used; /* code entries in table used */
5154  unsigned huff; /* Huffman code */
5155  unsigned incr; /* for incrementing code, index */
5156  unsigned fill; /* index for replicating entries */
5157  unsigned low; /* low bits for current root entry */
5158  unsigned mask; /* mask for low root bits */
5159  code this; /* table entry for duplication */
5160  code FAR *next; /* next available space in table */
5161  const unsigned short FAR *base; /* base value table to use */
5162  const unsigned short FAR *extra; /* extra bits table to use */
5163  int end; /* use base and extra for symbol > end */
5164  unsigned short count[MAXBITS+1]; /* number of codes of each length */
5165  unsigned short offs[MAXBITS+1]; /* offsets in table for each length */
5166  static const unsigned short lbase[31] = { /* Length codes 257..285 base */
5167  3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
5168  35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
5169  static const unsigned short lext[31] = { /* Length codes 257..285 extra */
5170  16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
5171  19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 201, 196};
5172  static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
5173  1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
5174  257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
5175  8193, 12289, 16385, 24577, 0, 0};
5176  static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
5177  16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
5178  23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
5179  28, 28, 29, 29, 64, 64};
5180 
5181  /*
5182  Process a set of code lengths to create a canonical Huffman code. The
5183  code lengths are lens[0..codes-1]. Each length corresponds to the
5184  symbols 0..codes-1. The Huffman code is generated by first sorting the
5185  symbols by length from short to long, and retaining the symbol order
5186  for codes with equal lengths. Then the code starts with all zero bits
5187  for the first code of the shortest length, and the codes are integer
5188  increments for the same length, and zeros are appended as the length
5189  increases. For the deflate format, these bits are stored backwards
5190  from their more natural integer increment ordering, and so when the
5191  decoding tables are built in the large loop below, the integer codes
5192  are incremented backwards.
5193 
5194  This routine assumes, but does not check, that all of the entries in
5195  lens[] are in the range 0..MAXBITS. The caller must assure this.
5196  1..MAXBITS is interpreted as that code length. zero means that that
5197  symbol does not occur in this code.
5198 
5199  The codes are sorted by computing a count of codes for each length,
5200  creating from that a table of starting indices for each length in the
5201  sorted table, and then entering the symbols in order in the sorted
5202  table. The sorted table is work[], with that space being provided by
5203  the caller.
5204 
5205  The length counts are used for other purposes as well, i.e. finding
5206  the minimum and maximum length codes, determining if there are any
5207  codes at all, checking for a valid set of lengths, and looking ahead
5208  at length counts to determine sub-table sizes when building the
5209  decoding tables.
5210  */
5211 
5212  /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
5213  for (len = 0; len <= MAXBITS; len++)
5214  count[len] = 0;
5215  for (sym = 0; sym < codes; sym++)
5216  count[lens[sym]]++;
5217 
5218  /* bound code lengths, force root to be within code lengths */
5219  root = *bits;
5220  for (max = MAXBITS; max >= 1; max--)
5221  if (count[max] != 0) break;
5222  if (root > max) root = max;
5223  if (max == 0) { /* no symbols to code at all */
5224  this.op = (unsigned char)64; /* invalid code marker */
5225  this.bits = (unsigned char)1;
5226  this.val = (unsigned short)0;
5227  *(*table)++ = this; /* make a table to force an error */
5228  *(*table)++ = this;
5229  *bits = 1;
5230  return 0; /* no symbols, but wait for decoding to report error */
5231  }
5232  for (min = 1; min <= MAXBITS; min++)
5233  if (count[min] != 0) break;
5234  if (root < min) root = min;
5235 
5236  /* check for an over-subscribed or incomplete set of lengths */
5237  left = 1;
5238  for (len = 1; len <= MAXBITS; len++) {
5239  left <<= 1;
5240  left -= count[len];
5241  if (left < 0) return -1; /* over-subscribed */
5242  }
5243  if (left > 0 && (type == CODES || max != 1))
5244  return -1; /* incomplete set */
5245 
5246  /* generate offsets into symbol table for each length for sorting */
5247  offs[1] = 0;
5248  for (len = 1; len < MAXBITS; len++)
5249  offs[len + 1] = offs[len] + count[len];
5250 
5251  /* sort symbols by length, by symbol order within each length */
5252  for (sym = 0; sym < codes; sym++)
5253  if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
5254 
5255  /*
5256  Create and fill in decoding tables. In this loop, the table being
5257  filled is at next and has curr index bits. The code being used is huff
5258  with length len. That code is converted to an index by dropping drop
5259  bits off of the bottom. For codes where len is less than drop + curr,
5260  those top drop + curr - len bits are incremented through all values to
5261  fill the table with replicated entries.
5262 
5263  root is the number of index bits for the root table. When len exceeds
5264  root, sub-tables are created pointed to by the root entry with an index
5265  of the low root bits of huff. This is saved in low to check for when a
5266  new sub-table should be started. drop is zero when the root table is
5267  being filled, and drop is root when sub-tables are being filled.
5268 
5269  When a new sub-table is needed, it is necessary to look ahead in the
5270  code lengths to determine what size sub-table is needed. The length
5271  counts are used for this, and so count[] is decremented as codes are
5272  entered in the tables.
5273 
5274  used keeps track of how many table entries have been allocated from the
5275  provided *table space. It is checked when a LENS table is being made
5276  against the space in *table, ENOUGH, minus the maximum space needed by
5277  the worst case distance code, MAXD. This should never happen, but the
5278  sufficiency of ENOUGH has not been proven exhaustively, hence the check.
5279  This assumes that when type == LENS, bits == 9.
5280 
5281  sym increments through all symbols, and the loop terminates when
5282  all codes of length max, i.e. all codes, have been processed. This
5283  routine permits incomplete codes, so another loop after this one fills
5284  in the rest of the decoding tables with invalid code markers.
5285  */
5286 
5287  /* set up for code type */
5288  switch (type) {
5289  case CODES:
5290  base = extra = work; /* dummy value--not used */
5291  end = 19;
5292  break;
5293  case LENS:
5294  base = lbase;
5295  base -= 257;
5296  extra = lext;
5297  extra -= 257;
5298  end = 256;
5299  break;
5300  default: /* DISTS */
5301  base = dbase;
5302  extra = dext;
5303  end = -1;
5304  }
5305 
5306  /* initialize state for loop */
5307  huff = 0; /* starting code */
5308  sym = 0; /* starting code symbol */
5309  len = min; /* starting code length */
5310  next = *table; /* current table to fill in */
5311  curr = root; /* current table index bits */
5312  drop = 0; /* current bits to drop from code for index */
5313  low = (unsigned)(-1); /* trigger new sub-table when len > root */
5314  used = 1U << root; /* use root table entries */
5315  mask = used - 1; /* mask for comparing low */
5316 
5317  /* check available table space */
5318  if (type == LENS && used >= ENOUGH - MAXD)
5319  return 1;
5320 
5321  /* process all codes and make table entries */
5322  for (;;) {
5323  /* create table entry */
5324  this.bits = (unsigned char)(len - drop);
5325  if ((int)(work[sym]) < end) {
5326  this.op = (unsigned char)0;
5327  this.val = work[sym];
5328  }
5329  else if ((int)(work[sym]) > end) {
5330  this.op = (unsigned char)(extra[work[sym]]);
5331  this.val = base[work[sym]];
5332  }
5333  else {
5334  this.op = (unsigned char)(32 + 64); /* end of block */
5335  this.val = 0;
5336  }
5337 
5338  /* replicate for those indices with low len bits equal to huff */
5339  incr = 1U << (len - drop);
5340  fill = 1U << curr;
5341  min = fill; /* save offset to next table */
5342  do {
5343  fill -= incr;
5344  next[(huff >> drop) + fill] = this;
5345  } while (fill != 0);
5346 
5347  /* backwards increment the len-bit code huff */
5348  incr = 1U << (len - 1);
5349  while (huff & incr)
5350  incr >>= 1;
5351  if (incr != 0) {
5352  huff &= incr - 1;
5353  huff += incr;
5354  }
5355  else
5356  huff = 0;
5357 
5358  /* go to next symbol, update count, len */
5359  sym++;
5360  if (--(count[len]) == 0) {
5361  if (len == max) break;
5362  len = lens[work[sym]];
5363  }
5364 
5365  /* create new sub-table if needed */
5366  if (len > root && (huff & mask) != low) {
5367  /* if first time, transition to sub-tables */
5368  if (drop == 0)
5369  drop = root;
5370 
5371  /* increment past last table */
5372  next += min; /* here min is 1 << curr */
5373 
5374  /* determine length of next table */
5375  curr = len - drop;
5376  left = (int)(1 << curr);
5377  while (curr + drop < max) {
5378  left -= count[curr + drop];
5379  if (left <= 0) break;
5380  curr++;
5381  left <<= 1;
5382  }
5383 
5384  /* check for enough space */
5385  used += 1U << curr;
5386  if (type == LENS && used >= ENOUGH - MAXD)
5387  return 1;
5388 
5389  /* point entry in root table to sub-table */
5390  low = huff & mask;
5391  (*table)[low].op = (unsigned char)curr;
5392  (*table)[low].bits = (unsigned char)root;
5393  (*table)[low].val = (unsigned short)(next - *table);
5394  }
5395  }
5396 
5397  /*
5398  Fill in rest of table for incomplete codes. This loop is similar to the
5399  loop above in incrementing huff for table indices. It is assumed that
5400  len is equal to curr + drop, so there is no loop needed to increment
5401  through high index bits. When the current sub-table is filled, the loop
5402  drops back to the root table to fill in any remaining entries there.
5403  */
5404  this.op = (unsigned char)64; /* invalid code marker */
5405  this.bits = (unsigned char)(len - drop);
5406  this.val = (unsigned short)0;
5407  while (huff != 0) {
5408  /* when done with sub-table, drop back to root table */
5409  if (drop != 0 && (huff & mask) != low) {
5410  drop = 0;
5411  len = root;
5412  next = *table;
5413  this.bits = (unsigned char)len;
5414  }
5415 
5416  /* put invalid code marker in table */
5417  next[huff >> drop] = this;
5418 
5419  /* backwards increment the len-bit code huff */
5420  incr = 1U << (len - 1);
5421  while (huff & incr)
5422  incr >>= 1;
5423  if (incr != 0) {
5424  huff &= incr - 1;
5425  huff += incr;
5426  }
5427  else
5428  huff = 0;
5429  }
5430 
5431  /* set return parameters */
5432  *table += used;
5433  *bits = root;
5434  return 0;
5435 }
5436 
5437 #ifndef ASMINF
5438 
5439 /* Allow machine dependent optimization for post-increment or pre-increment.
5440  Based on testing to date,
5441  Pre-increment preferred for:
5442  - PowerPC G3 (Adler)
5443  - MIPS R5000 (Randers-Pehrson)
5444  Post-increment preferred for:
5445  - none
5446  No measurable difference:
5447  - Pentium III (Anderson)
5448  - M68060 (Nikl)
5449  */
5450 #ifdef POSTINC
5451 # define OFF 0
5452 # define PUP(a) *(a)++
5453 #else
5454 # define OFF 1
5455 # define PUP(a) *++(a)
5456 #endif
5457 
5458 /*
5459  Decode literal, length, and distance codes and write out the resulting
5460  literal and match bytes until either not enough input or output is
5461  available, an end-of-block is encountered, or a data error is encountered.
5462  When large enough input and output buffers are supplied to inflate(), for
5463  example, a 16K input buffer and a 64K output buffer, more than 95% of the
5464  inflate execution time is spent in this routine.
5465 
5466  Entry assumptions:
5467 
5468  state->mode == LEN
5469  strm->avail_in >= 6
5470  strm->avail_out >= 258
5471  start >= strm->avail_out
5472  state->bits < 8
5473 
5474  On return, state->mode is one of:
5475 
5476  LEN -- ran out of enough output space or enough available input
5477  TYPE -- reached end of block code, inflate() to interpret next block
5478  BAD -- error in block data
5479 
5480  Notes:
5481 
5482  - The maximum input bits used by a length/distance pair is 15 bits for the
5483  length code, 5 bits for the length extra, 15 bits for the distance code,
5484  and 13 bits for the distance extra. This totals 48 bits, or six bytes.
5485  Therefore if strm->avail_in >= 6, then there is enough input to avoid
5486  checking for available input while decoding.
5487 
5488  - The maximum bytes that a single length/distance pair can output is 258
5489  bytes, which is the maximum length that can be coded. inflate_fast()
5490  requires strm->avail_out >= 258 for each loop to avoid checking for
5491  output space.
5492  */
5493 void inflate_fast(strm, start)
5494 z_streamp strm;
5495 unsigned start; /* inflate()'s starting value for strm->avail_out */
5496 {
5497  struct inflate_state FAR *state;
5498  unsigned char FAR *in; /* local strm->next_in */
5499  unsigned char FAR *last; /* while in < last, enough input available */
5500  unsigned char FAR *out; /* local strm->next_out */
5501  unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
5502  unsigned char FAR *end; /* while out < end, enough space available */
5503 #ifdef INFLATE_STRICT
5504  unsigned dmax; /* maximum distance from zlib header */
5505 #endif
5506  unsigned wsize; /* window size or zero if not using window */
5507  unsigned whave; /* valid bytes in the window */
5508  unsigned write; /* window write index */
5509  unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
5510  unsigned long hold; /* local strm->hold */
5511  unsigned bits; /* local strm->bits */
5512  code const FAR *lcode; /* local strm->lencode */
5513  code const FAR *dcode; /* local strm->distcode */
5514  unsigned lmask; /* mask for first level of length codes */
5515  unsigned dmask; /* mask for first level of distance codes */
5516  code this; /* retrieved table entry */
5517  unsigned op; /* code bits, operation, extra bits, or */
5518  /* window position, window bytes to copy */
5519  unsigned len; /* match length, unused bytes */
5520  unsigned dist; /* match distance */
5521  unsigned char FAR *from; /* where to copy match from */
5522 
5523  /* copy state to local variables */
5524  state = (struct inflate_state FAR *)strm->state;
5525  in = strm->next_in - OFF;
5526  last = in + (strm->avail_in - 5);
5527  out = strm->next_out - OFF;
5528  beg = out - (start - strm->avail_out);
5529  end = out + (strm->avail_out - 257);
5530 #ifdef INFLATE_STRICT
5531  dmax = state->dmax;
5532 #endif
5533  wsize = state->wsize;
5534  whave = state->whave;
5535  write = state->write;
5536  window = state->window;
5537  hold = state->hold;
5538  bits = state->bits;
5539  lcode = state->lencode;
5540  dcode = state->distcode;
5541  lmask = (1U << state->lenbits) - 1;
5542  dmask = (1U << state->distbits) - 1;
5543 
5544  /* decode literals and length/distances until end-of-block or not enough
5545  input data or output space */
5546  do {
5547  if (bits < 15) {
5548  hold += (unsigned long)(PUP(in)) << bits;
5549  bits += 8;
5550  hold += (unsigned long)(PUP(in)) << bits;
5551  bits += 8;
5552  }
5553  this = lcode[hold & lmask];
5554  dolen:
5555  op = (unsigned)(this.bits);
5556  hold >>= op;
5557  bits -= op;
5558  op = (unsigned)(this.op);
5559  if (op == 0) { /* literal */
5560  Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
5561  "inflate: literal '%c'\n" :
5562  "inflate: literal 0x%02x\n", this.val));
5563  PUP(out) = (unsigned char)(this.val);
5564  }
5565  else if (op & 16) { /* length base */
5566  len = (unsigned)(this.val);
5567  op &= 15; /* number of extra bits */
5568  if (op) {
5569  if (bits < op) {
5570  hold += (unsigned long)(PUP(in)) << bits;
5571  bits += 8;
5572  }
5573  len += (unsigned)hold & ((1U << op) - 1);
5574  hold >>= op;
5575  bits -= op;
5576  }
5577  Tracevv((stderr, "inflate: length %u\n", len));
5578  if (bits < 15) {
5579  hold += (unsigned long)(PUP(in)) << bits;
5580  bits += 8;
5581  hold += (unsigned long)(PUP(in)) << bits;
5582  bits += 8;
5583  }
5584  this = dcode[hold & dmask];
5585  dodist:
5586  op = (unsigned)(this.bits);
5587  hold >>= op;
5588  bits -= op;
5589  op = (unsigned)(this.op);
5590  if (op & 16) { /* distance base */
5591  dist = (unsigned)(this.val);
5592  op &= 15; /* number of extra bits */
5593  if (bits < op) {
5594  hold += (unsigned long)(PUP(in)) << bits;
5595  bits += 8;
5596  if (bits < op) {
5597  hold += (unsigned long)(PUP(in)) << bits;
5598  bits += 8;
5599  }
5600  }
5601  dist += (unsigned)hold & ((1U << op) - 1);
5602 #ifdef INFLATE_STRICT
5603  if (dist > dmax) {
5604  strm->msg = (char *)"invalid distance too far back";
5605  state->mode = BAD;
5606  break;
5607  }
5608 #endif
5609  hold >>= op;
5610  bits -= op;
5611  Tracevv((stderr, "inflate: distance %u\n", dist));
5612  op = (unsigned)(out - beg); /* max distance in output */
5613  if (dist > op) { /* see if copy from window */
5614  op = dist - op; /* distance back in window */
5615  if (op > whave) {
5616  strm->msg = (char *)"invalid distance too far back";
5617  state->mode = BAD;
5618  break;
5619  }
5620  from = window - OFF;
5621  if (write == 0) { /* very common case */
5622  from += wsize - op;
5623  if (op < len) { /* some from window */
5624  len -= op;
5625  do {
5626  PUP(out) = PUP(from);
5627  } while (--op);
5628  from = out - dist; /* rest from output */
5629  }
5630  }
5631  else if (write < op) { /* wrap around window */
5632  from += wsize + write - op;
5633  op -= write;
5634  if (op < len) { /* some from end of window */
5635  len -= op;
5636  do {
5637  PUP(out) = PUP(from);
5638  } while (--op);
5639  from = window - OFF;
5640  if (write < len) { /* some from start of window */
5641  op = write;
5642  len -= op;
5643  do {
5644  PUP(out) = PUP(from);
5645  } while (--op);
5646  from = out - dist; /* rest from output */
5647  }
5648  }
5649  }
5650  else { /* contiguous in window */
5651  from += write - op;
5652  if (op < len) { /* some from window */
5653  len -= op;
5654  do {
5655  PUP(out) = PUP(from);
5656  } while (--op);
5657  from = out - dist; /* rest from output */
5658  }
5659  }
5660  while (len > 2) {
5661  PUP(out) = PUP(from);
5662  PUP(out) = PUP(from);
5663  PUP(out) = PUP(from);
5664  len -= 3;
5665  }
5666  if (len) {
5667  PUP(out) = PUP(from);
5668  if (len > 1)
5669  PUP(out) = PUP(from);
5670  }
5671  }
5672  else {
5673  from = out - dist; /* copy direct from output */
5674  do { /* minimum length is three */
5675  PUP(out) = PUP(from);
5676  PUP(out) = PUP(from);
5677  PUP(out) = PUP(from);
5678  len -= 3;
5679  } while (len > 2);
5680  if (len) {
5681  PUP(out) = PUP(from);
5682  if (len > 1)
5683  PUP(out) = PUP(from);
5684  }
5685  }
5686  }
5687  else if ((op & 64) == 0) { /* 2nd level distance code */
5688  this = dcode[this.val + (hold & ((1U << op) - 1))];
5689  goto dodist;
5690  }
5691  else {
5692  strm->msg = (char *)"invalid distance code";
5693  state->mode = BAD;
5694  break;
5695  }
5696  }
5697  else if ((op & 64) == 0) { /* 2nd level length code */
5698  this = lcode[this.val + (hold & ((1U << op) - 1))];
5699  goto dolen;
5700  }
5701  else if (op & 32) { /* end-of-block */
5702  Tracevv((stderr, "inflate: end of block\n"));
5703  state->mode = TYPE;
5704  break;
5705  }
5706  else {
5707  strm->msg = (char *)"invalid literal/length code";
5708  state->mode = BAD;
5709  break;
5710  }
5711  } while (in < last && out < end);
5712 
5713  /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
5714  len = bits >> 3;
5715  in -= len;
5716  bits -= len << 3;
5717  hold &= (1U << bits) - 1;
5718 
5719  /* update state and return */
5720  strm->next_in = in + OFF;
5721  strm->next_out = out + OFF;
5722  strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
5723  strm->avail_out = (unsigned)(out < end ?
5724  257 + (end - out) : 257 - (out - end));
5725  state->hold = hold;
5726  state->bits = bits;
5727  return;
5728 }
5729 
5730 /*
5731  inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
5732  - Using bit fields for code structure
5733  - Different op definition to avoid & for extra bits (do & for table bits)
5734  - Three separate decoding do-loops for direct, window, and write == 0
5735  - Special case for distance > 1 copies to do overlapped load and store copy
5736  - Explicit branch predictions (based on measured branch probabilities)
5737  - Deferring match copy and interspersed it with decoding subsequent codes
5738  - Swapping literal/length else
5739  - Swapping window/direct else
5740  - Larger unrolled copy loops (three is about right)
5741  - Moving len -= 3 statement into middle of loop
5742  */
5743 
5744 #endif /* !ASMINF */
5745 
5746 /* function prototypes */
5747 local void fixedtables OF((struct inflate_state FAR *state));
5748 local int updatewindow OF((z_streamp strm, unsigned out));
5749 local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
5750  unsigned len));
5751 
5752 int inflateReset(strm)
5753 z_streamp strm;
5754 {
5755  struct inflate_state FAR *state;
5756 
5757  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
5758  state = (struct inflate_state FAR *)strm->state;
5759  strm->total_in = strm->total_out = state->total = 0;
5760  strm->msg = Z_NULL;
5761  strm->adler = 1; /* to support ill-conceived Java test suite */
5762  state->mode = HEAD;
5763  state->last = 0;
5764  state->havedict = 0;
5765  state->dmax = 32768U;
5766  state->head = Z_NULL;
5767  state->wsize = 0;
5768  state->whave = 0;
5769  state->write = 0;
5770  state->hold = 0;
5771  state->bits = 0;
5772  state->lencode = state->distcode = state->next = state->codes;
5773  Tracev((stderr, "inflate: reset\n"));
5774  return Z_OK;
5775 }
5776 
5777 int inflatePrime(strm, bits, value)
5778 z_streamp strm;
5779 int bits;
5780 int value;
5781 {
5782  struct inflate_state FAR *state;
5783 
5784  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
5785  state = (struct inflate_state FAR *)strm->state;
5786  if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
5787  value &= (1L << bits) - 1;
5788  state->hold += value << state->bits;
5789  state->bits += bits;
5790  return Z_OK;
5791 }
5792 
5793 int inflateInit2_(strm, windowBits, version, stream_size)
5794 z_streamp strm;
5795 int windowBits;
5796 const char *version;
5797 int stream_size;
5798 {
5799  struct inflate_state FAR *state;
5800 
5801  if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
5802  stream_size != (int)(sizeof(z_stream)))
5803  return Z_VERSION_ERROR;
5804  if (strm == Z_NULL) return Z_STREAM_ERROR;
5805  strm->msg = Z_NULL; /* in case we return an error */
5806  if (strm->zalloc == (alloc_func)0) {
5807  strm->zalloc = zcalloc;
5808  strm->opaque = (voidpf)0;
5809  }
5810  if (strm->zfree == (free_func)0) strm->zfree = zcfree;
5811  state = (struct inflate_state FAR *)
5812  ZALLOC(strm, 1, sizeof(struct inflate_state));
5813  if (state == Z_NULL) return Z_MEM_ERROR;
5814  Tracev((stderr, "inflate: allocated\n"));
5815  strm->state = (struct internal_state FAR *)state;
5816  if (windowBits < 0) {
5817  state->wrap = 0;
5818  windowBits = -windowBits;
5819  }
5820  else {
5821  state->wrap = (windowBits >> 4) + 1;
5822 #ifdef GUNZIP
5823  if (windowBits < 48) windowBits &= 15;
5824 #endif
5825  }
5826  if (windowBits < 8 || windowBits > 15) {
5827  ZFREE(strm, state);
5828  strm->state = Z_NULL;
5829  return Z_STREAM_ERROR;
5830  }
5831  state->wbits = (unsigned)windowBits;
5832  state->window = Z_NULL;
5833  return inflateReset(strm);
5834 }
5835 
5836 int inflateInit_(strm, version, stream_size)
5837 z_streamp strm;
5838 const char *version;
5839 int stream_size;
5840 {
5841  return inflateInit2_(strm, DEF_WBITS, version, stream_size);
5842 }
5843 
5844 /*
5845  Return state with length and distance decoding tables and index sizes set to
5846  fixed code decoding.
5847  */
5848 local void fixedtables(state)
5849 struct inflate_state FAR *state;
5850 {
5851  static const code lenfix[512] = {
5852  {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48},
5853  {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128},
5854  {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59},
5855  {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176},
5856  {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20},
5857  {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100},
5858  {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8},
5859  {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216},
5860  {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76},
5861  {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114},
5862  {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2},
5863  {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148},
5864  {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42},
5865  {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86},
5866  {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15},
5867  {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236},
5868  {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62},
5869  {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142},
5870  {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31},
5871  {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162},
5872  {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25},
5873  {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105},
5874  {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4},
5875  {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202},
5876  {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69},
5877  {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125},
5878  {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13},
5879  {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195},
5880  {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35},
5881  {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91},
5882  {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19},
5883  {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246},
5884  {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55},
5885  {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135},
5886  {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99},
5887  {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190},
5888  {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16},
5889  {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96},
5890  {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6},
5891  {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209},
5892  {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72},
5893  {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116},
5894  {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4},
5895  {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153},
5896  {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44},
5897  {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82},
5898  {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11},
5899  {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229},
5900  {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58},
5901  {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138},
5902  {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51},
5903  {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173},
5904  {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30},
5905  {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110},
5906  {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0},
5907  {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195},
5908  {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65},
5909  {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121},
5910  {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9},
5911  {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258},
5912  {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37},
5913  {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93},
5914  {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23},
5915  {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251},
5916  {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51},
5917  {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131},
5918  {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67},
5919  {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183},
5920  {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23},
5921  {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103},
5922  {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9},
5923  {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223},
5924  {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79},
5925  {0,9,255}
5926  };
5927 
5928  static const code distfix[32] = {
5929  {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025},
5930  {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193},
5931  {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385},
5932  {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577},
5933  {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073},
5934  {22,5,193},{64,5,0}
5935  };
5936  state->lencode = lenfix;
5937  state->lenbits = 9;
5938  state->distcode = distfix;
5939  state->distbits = 5;
5940 }
5941 
5942 
5943 /*
5944  Update the window with the last wsize (normally 32K) bytes written before
5945  returning. If window does not exist yet, create it. This is only called
5946  when a window is already in use, or when output has been written during this
5947  inflate call, but the end of the deflate stream has not been reached yet.
5948  It is also called to create a window for dictionary data when a dictionary
5949  is loaded.
5950 
5951  Providing output buffers larger than 32K to inflate() should provide a speed
5952  advantage, since only the last 32K of output is copied to the sliding window
5953  upon return from inflate(), and since all distances after the first 32K of
5954  output will fall in the output data, making match copies simpler and faster.
5955  The advantage may be dependent on the size of the processor's data caches.
5956  */
5957 local int updatewindow(strm, out)
5958 z_streamp strm;
5959 unsigned out;
5960 {
5961  struct inflate_state FAR *state;
5962  unsigned copy, dist;
5963 
5964  state = (struct inflate_state FAR *)strm->state;
5965 
5966  /* if it hasn't been done already, allocate space for the window */
5967  if (state->window == Z_NULL) {
5968  state->window = (unsigned char FAR *)
5969  ZALLOC(strm, 1U << state->wbits,
5970  sizeof(unsigned char));
5971  if (state->window == Z_NULL) return 1;
5972  }
5973 
5974  /* if window not in use yet, initialize */
5975  if (state->wsize == 0) {
5976  state->wsize = 1U << state->wbits;
5977  state->write = 0;
5978  state->whave = 0;
5979  }
5980 
5981  /* copy state->wsize or less output bytes into the circular window */
5982  copy = out - strm->avail_out;
5983  if (copy >= state->wsize) {
5984  zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
5985  state->write = 0;
5986  state->whave = state->wsize;
5987  }
5988  else {
5989  dist = state->wsize - state->write;
5990  if (dist > copy) dist = copy;
5991  zmemcpy(state->window + state->write, strm->next_out - copy, dist);
5992  copy -= dist;
5993  if (copy) {
5994  zmemcpy(state->window, strm->next_out - copy, copy);
5995  state->write = copy;
5996  state->whave = state->wsize;
5997  }
5998  else {
5999  state->write += dist;
6000  if (state->write == state->wsize) state->write = 0;
6001  if (state->whave < state->wsize) state->whave += dist;
6002  }
6003  }
6004  return 0;
6005 }
6006 
6007 /* Macros for inflate(): */
6008 
6009 /* check function to use adler32() for zlib or crc32() for gzip */
6010 #ifdef GUNZIP
6011 # define UPDATE(check, buf, len) \
6012  (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
6013 #else
6014 # define UPDATE(check, buf, len) adler32(check, buf, len)
6015 #endif
6016 
6017 /* check macros for header crc */
6018 #ifdef GUNZIP
6019 # define CRC2(check, word) \
6020  do { \
6021  hbuf[0] = (unsigned char)(word); \
6022  hbuf[1] = (unsigned char)((word) >> 8); \
6023  check = crc32(check, hbuf, 2); \
6024  } while (0)
6025 
6026 # define CRC4(check, word) \
6027  do { \
6028  hbuf[0] = (unsigned char)(word); \
6029  hbuf[1] = (unsigned char)((word) >> 8); \
6030  hbuf[2] = (unsigned char)((word) >> 16); \
6031  hbuf[3] = (unsigned char)((word) >> 24); \
6032  check = crc32(check, hbuf, 4); \
6033  } while (0)
6034 #endif
6035 
6036 /* Load registers with state in inflate() for speed */
6037 #define LOAD() \
6038  do { \
6039  put = strm->next_out; \
6040  left = strm->avail_out; \
6041  next = strm->next_in; \
6042  have = strm->avail_in; \
6043  hold = state->hold; \
6044  bits = state->bits; \
6045  } while (0)
6046 
6047 /* Restore state from registers in inflate() */
6048 #define RESTORE() \
6049  do { \
6050  strm->next_out = put; \
6051  strm->avail_out = left; \
6052  strm->next_in = next; \
6053  strm->avail_in = have; \
6054  state->hold = hold; \
6055  state->bits = bits; \
6056  } while (0)
6057 
6058 /* Clear the input bit accumulator */
6059 #define INITBITS() \
6060  do { \
6061  hold = 0; \
6062  bits = 0; \
6063  } while (0)
6064 
6065 /* Get a byte of input into the bit accumulator, or return from inflate()
6066  if there is no input available. */
6067 #define PULLBYTE() \
6068  do { \
6069  if (have == 0) goto inf_leave; \
6070  have--; \
6071  hold += (unsigned long)(*next++) << bits; \
6072  bits += 8; \
6073  } while (0)
6074 
6075 /* Assure that there are at least n bits in the bit accumulator. If there is
6076  not enough available input to do that, then return from inflate(). */
6077 #define NEEDBITS(n) \
6078  do { \
6079  while (bits < (unsigned)(n)) \
6080  PULLBYTE(); \
6081  } while (0)
6082 
6083 /* Return the low n bits of the bit accumulator (n < 16) */
6084 #define BITS(n) \
6085  ((unsigned)hold & ((1U << (n)) - 1))
6086 
6087 /* Remove n bits from the bit accumulator */
6088 #define DROPBITS(n) \
6089  do { \
6090  hold >>= (n); \
6091  bits -= (unsigned)(n); \
6092  } while (0)
6093 
6094 /* Remove zero to seven bits as needed to go to a byte boundary */
6095 #define BYTEBITS() \
6096  do { \
6097  hold >>= bits & 7; \
6098  bits -= bits & 7; \
6099  } while (0)
6100 
6101 /* Reverse the bytes in a 32-bit value */
6102 #define REVERSE(q) \
6103  ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
6104  (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
6105 
6106 /*
6107  inflate() uses a state machine to process as much input data and generate as
6108  much output data as possible before returning. The state machine is
6109  structured roughly as follows:
6110 
6111  for (;;) switch (state) {
6112  ...
6113  case STATEn:
6114  if (not enough input data or output space to make progress)
6115  return;
6116  ... make progress ...
6117  state = STATEm;
6118  break;
6119  ...
6120  }
6121 
6122  so when inflate() is called again, the same case is attempted again, and
6123  if the appropriate resources are provided, the machine proceeds to the
6124  next state. The NEEDBITS() macro is usually the way the state evaluates
6125  whether it can proceed or should return. NEEDBITS() does the return if
6126  the requested bits are not available. The typical use of the BITS macros
6127  is:
6128 
6129  NEEDBITS(n);
6130  ... do something with BITS(n) ...
6131  DROPBITS(n);
6132 
6133  where NEEDBITS(n) either returns from inflate() if there isn't enough
6134  input left to load n bits into the accumulator, or it continues. BITS(n)
6135  gives the low n bits in the accumulator. When done, DROPBITS(n) drops
6136  the low n bits off the accumulator. INITBITS() clears the accumulator
6137  and sets the number of available bits to zero. BYTEBITS() discards just
6138  enough bits to put the accumulator on a byte boundary. After BYTEBITS()
6139  and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
6140 
6141  NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
6142  if there is no input available. The decoding of variable length codes uses
6143  PULLBYTE() directly in order to pull just enough bytes to decode the next
6144  code, and no more.
6145 
6146  Some states loop until they get enough input, making sure that enough
6147  state information is maintained to continue the loop where it left off
6148  if NEEDBITS() returns in the loop. For example, want, need, and keep
6149  would all have to actually be part of the saved state in case NEEDBITS()
6150  returns:
6151 
6152  case STATEw:
6153  while (want < need) {
6154  NEEDBITS(n);
6155  keep[want++] = BITS(n);
6156  DROPBITS(n);
6157  }
6158  state = STATEx;
6159  case STATEx:
6160 
6161  As shown above, if the next state is also the next case, then the break
6162  is omitted.
6163 
6164  A state may also return if there is not enough output space available to
6165  complete that state. Those states are copying stored data, writing a
6166  literal byte, and copying a matching string.
6167 
6168  When returning, a "goto inf_leave" is used to update the total counters,
6169  update the check value, and determine whether any progress has been made
6170  during that inflate() call in order to return the proper return code.
6171  Progress is defined as a change in either strm->avail_in or strm->avail_out.
6172  When there is a window, goto inf_leave will update the window with the last
6173  output written. If a goto inf_leave occurs in the middle of decompression
6174  and there is no window currently, goto inf_leave will create one and copy
6175  output to the window for the next call of inflate().
6176 
6177  In this implementation, the flush parameter of inflate() only affects the
6178  return code (per zlib.h). inflate() always writes as much as possible to
6179  strm->next_out, given the space available and the provided input--the effect
6180  documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
6181  the allocation of and copying into a sliding window until necessary, which
6182  provides the effect documented in zlib.h for Z_FINISH when the entire input
6183  stream available. So the only thing the flush parameter actually does is:
6184  when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
6185  will return Z_BUF_ERROR if it has not reached the end of the stream.
6186  */
6187 
6188 int inflate(strm, flush)
6189 z_streamp strm;
6190 int flush;
6191 {
6192  struct inflate_state FAR *state;
6193  unsigned char FAR *next; /* next input */
6194  unsigned char FAR *put; /* next output */
6195  unsigned have, left; /* available input and output */
6196  unsigned long hold; /* bit buffer */
6197  unsigned bits; /* bits in bit buffer */
6198  unsigned in, out; /* save starting available input and output */
6199  unsigned copy; /* number of stored or match bytes to copy */
6200  unsigned char FAR *from; /* where to copy match bytes from */
6201  code this; /* current decoding table entry */
6202  code last; /* parent table entry */
6203  unsigned len; /* length to copy for repeats, bits to drop */
6204  int ret; /* return code */
6205 #ifdef GUNZIP
6206  unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
6207 #endif
6208  static const unsigned short order[19] = /* permutation of code lengths */
6209  {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
6210 
6211  if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
6212  (strm->next_in == Z_NULL && strm->avail_in != 0))
6213  return Z_STREAM_ERROR;
6214 
6215  state = (struct inflate_state FAR *)strm->state;
6216  if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
6217  LOAD();
6218  in = have;
6219  out = left;
6220  ret = Z_OK;
6221  for (;;)
6222  switch (state->mode) {
6223  case HEAD:
6224  if (state->wrap == 0) {
6225  state->mode = TYPEDO;
6226  break;
6227  }
6228  NEEDBITS(16);
6229 #ifdef GUNZIP
6230  if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
6231  state->check = crc32(0L, Z_NULL, 0);
6232  CRC2(state->check, hold);
6233  INITBITS();
6234  state->mode = FLAGS;
6235  break;
6236  }
6237  state->flags = 0; /* expect zlib header */
6238  if (state->head != Z_NULL)
6239  state->head->done = -1;
6240  if (!(state->wrap & 1) || /* check if zlib header allowed */
6241 #else
6242  if (
6243 #endif
6244  ((BITS(8) << 8) + (hold >> 8)) % 31) {
6245  strm->msg = (char *)"incorrect header check";
6246  state->mode = BAD;
6247  break;
6248  }
6249  if (BITS(4) != Z_DEFLATED) {
6250  strm->msg = (char *)"unknown compression method";
6251  state->mode = BAD;
6252  break;
6253  }
6254  DROPBITS(4);
6255  len = BITS(4) + 8;
6256  if (len > state->wbits) {
6257  strm->msg = (char *)"invalid window size";
6258  state->mode = BAD;
6259  break;
6260  }
6261  state->dmax = 1U << len;
6262  Tracev((stderr, "inflate: zlib header ok\n"));
6263  strm->adler = state->check = adler32(0L, Z_NULL, 0);
6264  state->mode = hold & 0x200 ? DICTID : TYPE;
6265  INITBITS();
6266  break;
6267 #ifdef GUNZIP
6268  case FLAGS:
6269  NEEDBITS(16);
6270  state->flags = (int)(hold);
6271  if ((state->flags & 0xff) != Z_DEFLATED) {
6272  strm->msg = (char *)"unknown compression method";
6273  state->mode = BAD;
6274  break;
6275  }
6276  if (state->flags & 0xe000) {
6277  strm->msg = (char *)"unknown header flags set";
6278  state->mode = BAD;
6279  break;
6280  }
6281  if (state->head != Z_NULL)
6282  state->head->text = (int)((hold >> 8) & 1);
6283  if (state->flags & 0x0200) CRC2(state->check, hold);
6284  INITBITS();
6285  state->mode = TIME;
6286  case TIME:
6287  NEEDBITS(32);
6288  if (state->head != Z_NULL)
6289  state->head->time = hold;
6290  if (state->flags & 0x0200) CRC4(state->check, hold);
6291  INITBITS();
6292  state->mode = OS;
6293  case OS:
6294  NEEDBITS(16);
6295  if (state->head != Z_NULL) {
6296  state->head->xflags = (int)(hold & 0xff);
6297  state->head->os = (int)(hold >> 8);
6298  }
6299  if (state->flags & 0x0200) CRC2(state->check, hold);
6300  INITBITS();
6301  state->mode = EXLEN;
6302  case EXLEN:
6303  if (state->flags & 0x0400) {
6304  NEEDBITS(16);
6305  state->length = (unsigned)(hold);
6306  if (state->head != Z_NULL)
6307  state->head->extra_len = (unsigned)hold;
6308  if (state->flags & 0x0200) CRC2(state->check, hold);
6309  INITBITS();
6310  }
6311  else if (state->head != Z_NULL)
6312  state->head->extra = Z_NULL;
6313  state->mode = EXTRA;
6314  case EXTRA:
6315  if (state->flags & 0x0400) {
6316  copy = state->length;
6317  if (copy > have) copy = have;
6318  if (copy) {
6319  if (state->head != Z_NULL &&
6320  state->head->extra != Z_NULL) {
6321  len = state->head->extra_len - state->length;
6322  zmemcpy(state->head->extra + len, next,
6323  len + copy > state->head->extra_max ?
6324  state->head->extra_max - len : copy);
6325  }
6326  if (state->flags & 0x0200)
6327  state->check = crc32(state->check, next, copy);
6328  have -= copy;
6329  next += copy;
6330  state->length -= copy;
6331  }
6332  if (state->length) goto inf_leave;
6333  }
6334  state->length = 0;
6335  state->mode = NAME;
6336  case NAME:
6337  if (state->flags & 0x0800) {
6338  if (have == 0) goto inf_leave;
6339  copy = 0;
6340  do {
6341  len = (unsigned)(next[copy++]);
6342  if (state->head != Z_NULL &&
6343  state->head->name != Z_NULL &&
6344  state->length < state->head->name_max)
6345  state->head->name[state->length++] = len;
6346  } while (len && copy < have);
6347  if (state->flags & 0x0200)
6348  state->check = crc32(state->check, next, copy);
6349  have -= copy;
6350  next += copy;
6351  if (len) goto inf_leave;
6352  }
6353  else if (state->head != Z_NULL)
6354  state->head->name = Z_NULL;
6355  state->length = 0;
6356  state->mode = COMMENT;
6357  case COMMENT:
6358  if (state->flags & 0x1000) {
6359  if (have == 0) goto inf_leave;
6360  copy = 0;
6361  do {
6362  len = (unsigned)(next[copy++]);
6363  if (state->head != Z_NULL &&
6364  state->head->comment != Z_NULL &&
6365  state->length < state->head->comm_max)
6366  state->head->comment[state->length++] = len;
6367  } while (len && copy < have);
6368  if (state->flags & 0x0200)
6369  state->check = crc32(state->check, next, copy);
6370  have -= copy;
6371  next += copy;
6372  if (len) goto inf_leave;
6373  }
6374  else if (state->head != Z_NULL)
6375  state->head->comment = Z_NULL;
6376  state->mode = HCRC;
6377  case HCRC:
6378  if (state->flags & 0x0200) {
6379  NEEDBITS(16);
6380  if (hold != (state->check & 0xffff)) {
6381  strm->msg = (char *)"header crc mismatch";
6382  state->mode = BAD;
6383  break;
6384  }
6385  INITBITS();
6386  }
6387  if (state->head != Z_NULL) {
6388  state->head->hcrc = (int)((state->flags >> 9) & 1);
6389  state->head->done = 1;
6390  }
6391  strm->adler = state->check = crc32(0L, Z_NULL, 0);
6392  state->mode = TYPE;
6393  break;
6394 #endif
6395  case DICTID:
6396  NEEDBITS(32);
6397  strm->adler = state->check = REVERSE(hold);
6398  INITBITS();
6399  state->mode = DICT;
6400  case DICT:
6401  if (state->havedict == 0) {
6402  RESTORE();
6403  return Z_NEED_DICT;
6404  }
6405  strm->adler = state->check = adler32(0L, Z_NULL, 0);
6406  state->mode = TYPE;
6407  case TYPE:
6408  if (flush == Z_BLOCK) goto inf_leave;
6409  case TYPEDO:
6410  if (state->last) {
6411  BYTEBITS();
6412  state->mode = CHECK;
6413  break;
6414  }
6415  NEEDBITS(3);
6416  state->last = BITS(1);
6417  DROPBITS(1);
6418  switch (BITS(2)) {
6419  case 0: /* stored block */
6420  Tracev((stderr, "inflate: stored block%s\n",
6421  state->last ? " (last)" : ""));
6422  state->mode = STORED;
6423  break;
6424  case 1: /* fixed block */
6425  fixedtables(state);
6426  Tracev((stderr, "inflate: fixed codes block%s\n",
6427  state->last ? " (last)" : ""));
6428  state->mode = LEN; /* decode codes */
6429  break;
6430  case 2: /* dynamic block */
6431  Tracev((stderr, "inflate: dynamic codes block%s\n",
6432  state->last ? " (last)" : ""));
6433  state->mode = TABLE;
6434  break;
6435  case 3:
6436  strm->msg = (char *)"invalid block type";
6437  state->mode = BAD;
6438  }
6439  DROPBITS(2);
6440  break;
6441  case STORED:
6442  BYTEBITS(); /* go to byte boundary */
6443  NEEDBITS(32);
6444  if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
6445  strm->msg = (char *)"invalid stored block lengths";
6446  state->mode = BAD;
6447  break;
6448  }
6449  state->length = (unsigned)hold & 0xffff;
6450  Tracev((stderr, "inflate: stored length %u\n",
6451  state->length));
6452  INITBITS();
6453  state->mode = COPY;
6454  case COPY:
6455  copy = state->length;
6456  if (copy) {
6457  if (copy > have) copy = have;
6458  if (copy > left) copy = left;
6459  if (copy == 0) goto inf_leave;
6460  zmemcpy(put, next, copy);
6461  have -= copy;
6462  next += copy;
6463  left -= copy;
6464  put += copy;
6465  state->length -= copy;
6466  break;
6467  }
6468  Tracev((stderr, "inflate: stored end\n"));
6469  state->mode = TYPE;
6470  break;
6471  case TABLE:
6472  NEEDBITS(14);
6473  state->nlen = BITS(5) + 257;
6474  DROPBITS(5);
6475  state->ndist = BITS(5) + 1;
6476  DROPBITS(5);
6477  state->ncode = BITS(4) + 4;
6478  DROPBITS(4);
6479 #ifndef PKZIP_BUG_WORKAROUND
6480  if (state->nlen > 286 || state->ndist > 30) {
6481  strm->msg = (char *)"too many length or distance symbols";
6482  state->mode = BAD;
6483  break;
6484  }
6485 #endif
6486  Tracev((stderr, "inflate: table sizes ok\n"));
6487  state->have = 0;
6488  state->mode = LENLENS;
6489  case LENLENS:
6490  while (state->have < state->ncode) {
6491  NEEDBITS(3);
6492  state->lens[order[state->have++]] = (unsigned short)BITS(3);
6493  DROPBITS(3);
6494  }
6495  while (state->have < 19)
6496  state->lens[order[state->have++]] = 0;
6497  state->next = state->codes;
6498  state->lencode = (code const FAR *)(state->next);
6499  state->lenbits = 7;
6500  ret = inflate_table(CODES, state->lens, 19, &(state->next),
6501  &(state->lenbits), state->work);
6502  if (ret) {
6503  strm->msg = (char *)"invalid code lengths set";
6504  state->mode = BAD;
6505  break;
6506  }
6507  Tracev((stderr, "inflate: code lengths ok\n"));
6508  state->have = 0;
6509  state->mode = CODELENS;
6510  case CODELENS:
6511  while (state->have < state->nlen + state->ndist) {
6512  for (;;) {
6513  this = state->lencode[BITS(state->lenbits)];
6514  if ((unsigned)(this.bits) <= bits) break;
6515  PULLBYTE();
6516  }
6517  if (this.val < 16) {
6518  NEEDBITS(this.bits);
6519  DROPBITS(this.bits);
6520  state->lens[state->have++] = this.val;
6521  }
6522  else {
6523  if (this.val == 16) {
6524  NEEDBITS(this.bits + 2);
6525  DROPBITS(this.bits);
6526  if (state->have == 0) {
6527  strm->msg = (char *)"invalid bit length repeat";
6528  state->mode = BAD;
6529  break;
6530  }
6531  len = state->lens[state->have - 1];
6532  copy = 3 + BITS(2);
6533  DROPBITS(2);
6534  }
6535  else if (this.val == 17) {
6536  NEEDBITS(this.bits + 3);
6537  DROPBITS(this.bits);
6538  len = 0;
6539  copy = 3 + BITS(3);
6540  DROPBITS(3);
6541  }
6542  else {
6543  NEEDBITS(this.bits + 7);
6544  DROPBITS(this.bits);
6545  len = 0;
6546  copy = 11 + BITS(7);
6547  DROPBITS(7);
6548  }
6549  if (state->have + copy > state->nlen + state->ndist) {
6550  strm->msg = (char *)"invalid bit length repeat";
6551  state->mode = BAD;
6552  break;
6553  }
6554  while (copy--)
6555  state->lens[state->have++] = (unsigned short)len;
6556  }
6557  }
6558 
6559  /* handle error breaks in while */
6560  if (state->mode == BAD) break;
6561 
6562  /* build code tables */
6563  state->next = state->codes;
6564  state->lencode = (code const FAR *)(state->next);
6565  state->lenbits = 9;
6566  ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
6567  &(state->lenbits), state->work);
6568  if (ret) {
6569  strm->msg = (char *)"invalid literal/lengths set";
6570  state->mode = BAD;
6571  break;
6572  }
6573  state->distcode = (code const FAR *)(state->next);
6574  state->distbits = 6;
6575  ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
6576  &(state->next), &(state->distbits), state->work);
6577  if (ret) {
6578  strm->msg = (char *)"invalid distances set";
6579  state->mode = BAD;
6580  break;
6581  }
6582  Tracev((stderr, "inflate: codes ok\n"));
6583  state->mode = LEN;
6584  case LEN:
6585  if (have >= 6 && left >= 258) {
6586  RESTORE();
6587  inflate_fast(strm, out);
6588  LOAD();
6589  break;
6590  }
6591  for (;;) {
6592  this = state->lencode[BITS(state->lenbits)];
6593  if ((unsigned)(this.bits) <= bits) break;
6594  PULLBYTE();
6595  }
6596  if (this.op && (this.op & 0xf0) == 0) {
6597  last = this;
6598  for (;;) {
6599  this = state->lencode[last.val +
6600  (BITS(last.bits + last.op) >> last.bits)];
6601  if ((unsigned)(last.bits + this.bits) <= bits) break;
6602  PULLBYTE();
6603  }
6604  DROPBITS(last.bits);
6605  }
6606  DROPBITS(this.bits);
6607  state->length = (unsigned)this.val;
6608  if ((int)(this.op) == 0) {
6609  Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
6610  "inflate: literal '%c'\n" :
6611  "inflate: literal 0x%02x\n", this.val));
6612  state->mode = LIT;
6613  break;
6614  }
6615  if (this.op & 32) {
6616  Tracevv((stderr, "inflate: end of block\n"));
6617  state->mode = TYPE;
6618  break;
6619  }
6620  if (this.op & 64) {
6621  strm->msg = (char *)"invalid literal/length code";
6622  state->mode = BAD;
6623  break;
6624  }
6625  state->extra = (unsigned)(this.op) & 15;
6626  state->mode = LENEXT;
6627  case LENEXT:
6628  if (state->extra) {
6629  NEEDBITS(state->extra);
6630  state->length += BITS(state->extra);
6631  DROPBITS(state->extra);
6632  }
6633  Tracevv((stderr, "inflate: length %u\n", state->length));
6634  state->mode = DIST;
6635  case DIST:
6636  for (;;) {
6637  this = state->distcode[BITS(state->distbits)];
6638  if ((unsigned)(this.bits) <= bits) break;
6639  PULLBYTE();
6640  }
6641  if ((this.op & 0xf0) == 0) {
6642  last = this;
6643  for (;;) {
6644  this = state->distcode[last.val +
6645  (BITS(last.bits + last.op) >> last.bits)];
6646  if ((unsigned)(last.bits + this.bits) <= bits) break;
6647  PULLBYTE();
6648  }
6649  DROPBITS(last.bits);
6650  }
6651  DROPBITS(this.bits);
6652  if (this.op & 64) {
6653  strm->msg = (char *)"invalid distance code";
6654  state->mode = BAD;
6655  break;
6656  }
6657  state->offset = (unsigned)this.val;
6658  state->extra = (unsigned)(this.op) & 15;
6659  state->mode = DISTEXT;
6660  case DISTEXT:
6661  if (state->extra) {
6662  NEEDBITS(state->extra);
6663  state->offset += BITS(state->extra);
6664  DROPBITS(state->extra);
6665  }
6666 #ifdef INFLATE_STRICT
6667  if (state->offset > state->dmax) {
6668  strm->msg = (char *)"invalid distance too far back";
6669  state->mode = BAD;
6670  break;
6671  }
6672 #endif
6673  if (state->offset > state->whave + out - left) {
6674  strm->msg = (char *)"invalid distance too far back";
6675  state->mode = BAD;
6676  break;
6677  }
6678  Tracevv((stderr, "inflate: distance %u\n", state->offset));
6679  state->mode = MATCH;
6680  case MATCH:
6681  if (left == 0) goto inf_leave;
6682  copy = out - left;
6683  if (state->offset > copy) { /* copy from window */
6684  copy = state->offset - copy;
6685  if (copy > state->write) {
6686  copy -= state->write;
6687  from = state->window + (state->wsize - copy);
6688  }
6689  else
6690  from = state->window + (state->write - copy);
6691  if (copy > state->length) copy = state->length;
6692  }
6693  else { /* copy from output */
6694  from = put - state->offset;
6695  copy = state->length;
6696  }
6697  if (copy > left) copy = left;
6698  left -= copy;
6699  state->length -= copy;
6700  do {
6701  *put++ = *from++;
6702  } while (--copy);
6703  if (state->length == 0) state->mode = LEN;
6704  break;
6705  case LIT:
6706  if (left == 0) goto inf_leave;
6707  *put++ = (unsigned char)(state->length);
6708  left--;
6709  state->mode = LEN;
6710  break;
6711  case CHECK:
6712  if (state->wrap) {
6713  NEEDBITS(32);
6714  out -= left;
6715  strm->total_out += out;
6716  state->total += out;
6717  if (out)
6718  strm->adler = state->check =
6719  UPDATE(state->check, put - out, out);
6720  out = left;
6721  if ((
6722 #ifdef GUNZIP
6723  state->flags ? hold :
6724 #endif
6725  REVERSE(hold)) != state->check) {
6726  strm->msg = (char *)"incorrect data check";
6727  state->mode = BAD;
6728  break;
6729  }
6730  INITBITS();
6731  Tracev((stderr, "inflate: check matches trailer\n"));
6732  }
6733 #ifdef GUNZIP
6734  state->mode = LENGTH;
6735  case LENGTH:
6736  if (state->wrap && state->flags) {
6737  NEEDBITS(32);
6738  if (hold != (state->total & 0xffffffffUL)) {
6739  strm->msg = (char *)"incorrect length check";
6740  state->mode = BAD;
6741  break;
6742  }
6743  INITBITS();
6744  Tracev((stderr, "inflate: length matches trailer\n"));
6745  }
6746 #endif
6747  state->mode = DONE;
6748  case DONE:
6749  ret = Z_STREAM_END;
6750  goto inf_leave;
6751  case BAD:
6752  ret = Z_DATA_ERROR;
6753  goto inf_leave;
6754  case MEM:
6755  return Z_MEM_ERROR;
6756  case SYNC:
6757  default:
6758  return Z_STREAM_ERROR;
6759  }
6760 
6761  /*
6762  Return from inflate(), updating the total counts and the check value.
6763  If there was no progress during the inflate() call, return a buffer
6764  error. Call updatewindow() to create and/or update the window state.
6765  Note: a memory error from inflate() is non-recoverable.
6766  */
6767  inf_leave:
6768  RESTORE();
6769  if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
6770  if (updatewindow(strm, out)) {
6771  state->mode = MEM;
6772  return Z_MEM_ERROR;
6773  }
6774  in -= strm->avail_in;
6775  out -= strm->avail_out;
6776  strm->total_in += in;
6777  strm->total_out += out;
6778  state->total += out;
6779  if (state->wrap && out)
6780  strm->adler = state->check =
6781  UPDATE(state->check, strm->next_out - out, out);
6782  strm->data_type = state->bits + (state->last ? 64 : 0) +
6783  (state->mode == TYPE ? 128 : 0);
6784  if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
6785  ret = Z_BUF_ERROR;
6786  return ret;
6787 }
6788 
6789 int inflateEnd(strm)
6790 z_streamp strm;
6791 {
6792  struct inflate_state FAR *state;
6793  if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
6794  return Z_STREAM_ERROR;
6795  state = (struct inflate_state FAR *)strm->state;
6796  if (state->window != Z_NULL) ZFREE(strm, state->window);
6797  ZFREE(strm, strm->state);
6798  strm->state = Z_NULL;
6799  Tracev((stderr, "inflate: end\n"));
6800  return Z_OK;
6801 }
6802 
6803 int inflateSetDictionary(strm, dictionary, dictLength)
6804 z_streamp strm;
6805 const Bytef *dictionary;
6806 uInt dictLength;
6807 {
6808  struct inflate_state FAR *state;
6809  unsigned long id;
6810 
6811  /* check state */
6812  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
6813  state = (struct inflate_state FAR *)strm->state;
6814  if (state->wrap != 0 && state->mode != DICT)
6815  return Z_STREAM_ERROR;
6816 
6817  /* check for correct dictionary id */
6818  if (state->mode == DICT) {
6819  id = adler32(0L, Z_NULL, 0);
6820  id = adler32(id, dictionary, dictLength);
6821  if (id != state->check)
6822  return Z_DATA_ERROR;
6823  }
6824 
6825  /* copy dictionary to window */
6826  if (updatewindow(strm, strm->avail_out)) {
6827  state->mode = MEM;
6828  return Z_MEM_ERROR;
6829  }
6830  if (dictLength > state->wsize) {
6831  zmemcpy(state->window, dictionary + dictLength - state->wsize,
6832  state->wsize);
6833  state->whave = state->wsize;
6834  }
6835  else {
6836  zmemcpy(state->window + state->wsize - dictLength, dictionary,
6837  dictLength);
6838  state->whave = dictLength;
6839  }
6840  state->havedict = 1;
6841  Tracev((stderr, "inflate: dictionary set\n"));
6842  return Z_OK;
6843 }
6844 
6845 int inflateGetHeader(strm, head)
6846 z_streamp strm;
6847 gz_headerp head;
6848 {
6849  struct inflate_state FAR *state;
6850 
6851  /* check state */
6852  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
6853  state = (struct inflate_state FAR *)strm->state;
6854  if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
6855 
6856  /* save header structure */
6857  state->head = head;
6858  head->done = 0;
6859  return Z_OK;
6860 }
6861 
6862 /*
6863  Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
6864  or when out of input. When called, *have is the number of pattern bytes
6865  found in order so far, in 0..3. On return *have is updated to the new
6866  state. If on return *have equals four, then the pattern was found and the
6867  return value is how many bytes were read including the last byte of the
6868  pattern. If *have is less than four, then the pattern has not been found
6869  yet and the return value is len. In the latter case, syncsearch() can be
6870  called again with more data and the *have state. *have is initialized to
6871  zero for the first call.
6872  */
6873 local unsigned syncsearch(have, buf, len)
6874 unsigned FAR *have;
6875 unsigned char FAR *buf;
6876 unsigned len;
6877 {
6878  unsigned got;
6879  unsigned next;
6880 
6881  got = *have;
6882  next = 0;
6883  while (next < len && got < 4) {
6884  if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
6885  got++;
6886  else if (buf[next])
6887  got = 0;
6888  else
6889  got = 4 - got;
6890  next++;
6891  }
6892  *have = got;
6893  return next;
6894 }
6895 
6896 int inflateSync(strm)
6897 z_streamp strm;
6898 {
6899  unsigned len; /* number of bytes to look at or looked at */
6900  unsigned long in, out; /* temporary to save total_in and total_out */
6901  unsigned char buf[4]; /* to restore bit buffer to byte string */
6902  struct inflate_state FAR *state;
6903 
6904  /* check parameters */
6905  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
6906  state = (struct inflate_state FAR *)strm->state;
6907  if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
6908 
6909  /* if first time, start search in bit buffer */
6910  if (state->mode != SYNC) {
6911  state->mode = SYNC;
6912  state->hold <<= state->bits & 7;
6913  state->bits -= state->bits & 7;
6914  len = 0;
6915  while (state->bits >= 8) {
6916  buf[len++] = (unsigned char)(state->hold);
6917  state->hold >>= 8;
6918  state->bits -= 8;
6919  }
6920  state->have = 0;
6921  syncsearch(&(state->have), buf, len);
6922  }
6923 
6924  /* search available input */
6925  len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
6926  strm->avail_in -= len;
6927  strm->next_in += len;
6928  strm->total_in += len;
6929 
6930  /* return no joy or set up to restart inflate() on a new block */
6931  if (state->have != 4) return Z_DATA_ERROR;
6932  in = strm->total_in; out = strm->total_out;
6933  inflateReset(strm);
6934  strm->total_in = in; strm->total_out = out;
6935  state->mode = TYPE;
6936  return Z_OK;
6937 }
6938 
6939 /*
6940  Returns true if inflate is currently at the end of a block generated by
6941  Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
6942  implementation to provide an additional safety check. PPP uses
6943  Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
6944  block. When decompressing, PPP checks that at the end of input packet,
6945  inflate is waiting for these length bytes.
6946  */
6947 int inflateSyncPoint(strm)
6948 z_streamp strm;
6949 {
6950  struct inflate_state FAR *state;
6951 
6952  if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
6953  state = (struct inflate_state FAR *)strm->state;
6954  return state->mode == STORED && state->bits == 0;
6955 }
6956 
6957 int inflateCopy(dest, source)
6958 z_streamp dest;
6959 z_streamp source;
6960 {
6961  struct inflate_state FAR *state;
6962  struct inflate_state FAR *copy;
6963  unsigned char FAR *window;
6964  unsigned wsize;
6965 
6966  /* check input */
6967  if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
6968  source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
6969  return Z_STREAM_ERROR;
6970  state = (struct inflate_state FAR *)source->state;
6971 
6972  /* allocate space */
6973  copy = (struct inflate_state FAR *)
6974  ZALLOC(source, 1, sizeof(struct inflate_state));
6975  if (copy == Z_NULL) return Z_MEM_ERROR;
6976  window = Z_NULL;
6977  if (state->window != Z_NULL) {
6978  window = (unsigned char FAR *)
6979  ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
6980  if (window == Z_NULL) {
6981  ZFREE(source, copy);
6982  return Z_MEM_ERROR;
6983  }
6984  }
6985 
6986  /* copy state */
6987  zmemcpy(dest, source, sizeof(z_stream));
6988  zmemcpy(copy, state, sizeof(struct inflate_state));
6989  if (state->lencode >= state->codes &&
6990  state->lencode <= state->codes + ENOUGH - 1) {
6991  copy->lencode = copy->codes + (state->lencode - state->codes);
6992  copy->distcode = copy->codes + (state->distcode - state->codes);
6993  }
6994  copy->next = copy->codes + (state->next - state->codes);
6995  if (window != Z_NULL) {
6996  wsize = 1U << state->wbits;
6997  zmemcpy(window, state->window, wsize);
6998  }
6999  copy->window = window;
7000  dest->state = (struct internal_state FAR *)copy;
7001  return Z_OK;
7002 }
7003 
7004 /* crc32.c -- compute the CRC-32 of a data stream
7005  * Copyright (C) 1995-2005 Mark Adler
7006  * For conditions of distribution and use, see copyright notice in zlib.h
7007  *
7008  * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
7009  * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing
7010  * tables for updating the shift register in one step with three exclusive-ors
7011  * instead of four steps with four exclusive-ors. This results in about a
7012  * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
7013  */
7014 
7015 #define local static
7016 
7017 /* Find a four-byte integer type for crc32_little() and crc32_big(). */
7018 #ifndef NOBYFOUR
7019 # ifdef STDC /* need ANSI C limits.h to determine sizes */
7020 # include <limits.h>
7021 # define BYFOUR
7022 # if (UINT_MAX == 0xffffffffUL)
7023  typedef unsigned int u4;
7024 # else
7025 # if (ULONG_MAX == 0xffffffffUL)
7026  typedef unsigned long u4;
7027 # else
7028 # if (USHRT_MAX == 0xffffffffUL)
7029  typedef unsigned short u4;
7030 # else
7031 # undef BYFOUR /* can't find a four-byte integer type! */
7032 # endif
7033 # endif
7034 # endif
7035 # endif /* STDC */
7036 #endif /* !NOBYFOUR */
7037 
7038 /* Definitions for doing the crc four data bytes at a time. */
7039 #ifdef BYFOUR
7040 # define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \
7041  (((w)&0xff00)<<8)+(((w)&0xff)<<24))
7042  local unsigned long crc32_little OF((unsigned long,
7043  const unsigned char FAR *, unsigned));
7044  local unsigned long crc32_big OF((unsigned long,
7045  const unsigned char FAR *, unsigned));
7046 # define TBLS 8
7047 #else
7048 # define TBLS 1
7049 #endif /* BYFOUR */
7050 
7051 /* Local functions for crc concatenation */
7052 local unsigned long gf2_matrix_times OF((unsigned long *mat,
7053  unsigned long vec));
7054 local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
7055 
7056 /* ========================================================================
7057  * Tables of CRC-32s of all single-byte values, made by make_crc_table().
7058  */
7059 /* crc32.h -- tables for rapid CRC calculation
7060  * Generated automatically by crc32.c
7061  */
7062 
7063 local const unsigned long FAR crc_table[TBLS][256] =
7064 {
7065  {
7066  0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL,
7067  0x706af48fUL, 0xe963a535UL, 0x9e6495a3UL, 0x0edb8832UL, 0x79dcb8a4UL,
7068  0xe0d5e91eUL, 0x97d2d988UL, 0x09b64c2bUL, 0x7eb17cbdUL, 0xe7b82d07UL,
7069  0x90bf1d91UL, 0x1db71064UL, 0x6ab020f2UL, 0xf3b97148UL, 0x84be41deUL,
7070  0x1adad47dUL, 0x6ddde4ebUL, 0xf4d4b551UL, 0x83d385c7UL, 0x136c9856UL,
7071  0x646ba8c0UL, 0xfd62f97aUL, 0x8a65c9ecUL, 0x14015c4fUL, 0x63066cd9UL,
7072  0xfa0f3d63UL, 0x8d080df5UL, 0x3b6e20c8UL, 0x4c69105eUL, 0xd56041e4UL,
7073  0xa2677172UL, 0x3c03e4d1UL, 0x4b04d447UL, 0xd20d85fdUL, 0xa50ab56bUL,
7074  0x35b5a8faUL, 0x42b2986cUL, 0xdbbbc9d6UL, 0xacbcf940UL, 0x32d86ce3UL,
7075  0x45df5c75UL, 0xdcd60dcfUL, 0xabd13d59UL, 0x26d930acUL, 0x51de003aUL,
7076  0xc8d75180UL, 0xbfd06116UL, 0x21b4f4b5UL, 0x56b3c423UL, 0xcfba9599UL,
7077  0xb8bda50fUL, 0x2802b89eUL, 0x5f058808UL, 0xc60cd9b2UL, 0xb10be924UL,
7078  0x2f6f7c87UL, 0x58684c11UL, 0xc1611dabUL, 0xb6662d3dUL, 0x76dc4190UL,
7079  0x01db7106UL, 0x98d220bcUL, 0xefd5102aUL, 0x71b18589UL, 0x06b6b51fUL,
7080  0x9fbfe4a5UL, 0xe8b8d433UL, 0x7807c9a2UL, 0x0f00f934UL, 0x9609a88eUL,
7081  0xe10e9818UL, 0x7f6a0dbbUL, 0x086d3d2dUL, 0x91646c97UL, 0xe6635c01UL,
7082  0x6b6b51f4UL, 0x1c6c6162UL, 0x856530d8UL, 0xf262004eUL, 0x6c0695edUL,
7083  0x1b01a57bUL, 0x8208f4c1UL, 0xf50fc457UL, 0x65b0d9c6UL, 0x12b7e950UL,
7084  0x8bbeb8eaUL, 0xfcb9887cUL, 0x62dd1ddfUL, 0x15da2d49UL, 0x8cd37cf3UL,
7085  0xfbd44c65UL, 0x4db26158UL, 0x3ab551ceUL, 0xa3bc0074UL, 0xd4bb30e2UL,
7086  0x4adfa541UL, 0x3dd895d7UL, 0xa4d1c46dUL, 0xd3d6f4fbUL, 0x4369e96aUL,
7087  0x346ed9fcUL, 0xad678846UL, 0xda60b8d0UL, 0x44042d73UL, 0x33031de5UL,
7088  0xaa0a4c5fUL, 0xdd0d7cc9UL, 0x5005713cUL, 0x270241aaUL, 0xbe0b1010UL,
7089  0xc90c2086UL, 0x5768b525UL, 0x206f85b3UL, 0xb966d409UL, 0xce61e49fUL,
7090  0x5edef90eUL, 0x29d9c998UL, 0xb0d09822UL, 0xc7d7a8b4UL, 0x59b33d17UL,
7091  0x2eb40d81UL, 0xb7bd5c3bUL, 0xc0ba6cadUL, 0xedb88320UL, 0x9abfb3b6UL,
7092  0x03b6e20cUL, 0x74b1d29aUL, 0xead54739UL, 0x9dd277afUL, 0x04db2615UL,
7093  0x73dc1683UL, 0xe3630b12UL, 0x94643b84UL, 0x0d6d6a3eUL, 0x7a6a5aa8UL,
7094  0xe40ecf0bUL, 0x9309ff9dUL, 0x0a00ae27UL, 0x7d079eb1UL, 0xf00f9344UL,
7095  0x8708a3d2UL, 0x1e01f268UL, 0x6906c2feUL, 0xf762575dUL, 0x806567cbUL,
7096  0x196c3671UL, 0x6e6b06e7UL, 0xfed41b76UL, 0x89d32be0UL, 0x10da7a5aUL,
7097  0x67dd4accUL, 0xf9b9df6fUL, 0x8ebeeff9UL, 0x17b7be43UL, 0x60b08ed5UL,
7098  0xd6d6a3e8UL, 0xa1d1937eUL, 0x38d8c2c4UL, 0x4fdff252UL, 0xd1bb67f1UL,
7099  0xa6bc5767UL, 0x3fb506ddUL, 0x48b2364bUL, 0xd80d2bdaUL, 0xaf0a1b4cUL,
7100  0x36034af6UL, 0x41047a60UL, 0xdf60efc3UL, 0xa867df55UL, 0x316e8eefUL,
7101  0x4669be79UL, 0xcb61b38cUL, 0xbc66831aUL, 0x256fd2a0UL, 0x5268e236UL,
7102  0xcc0c7795UL, 0xbb0b4703UL, 0x220216b9UL, 0x5505262fUL, 0xc5ba3bbeUL,
7103  0xb2bd0b28UL, 0x2bb45a92UL, 0x5cb36a04UL, 0xc2d7ffa7UL, 0xb5d0cf31UL,
7104  0x2cd99e8bUL, 0x5bdeae1dUL, 0x9b64c2b0UL, 0xec63f226UL, 0x756aa39cUL,
7105  0x026d930aUL, 0x9c0906a9UL, 0xeb0e363fUL, 0x72076785UL, 0x05005713UL,
7106  0x95bf4a82UL, 0xe2b87a14UL, 0x7bb12baeUL, 0x0cb61b38UL, 0x92d28e9bUL,
7107  0xe5d5be0dUL, 0x7cdcefb7UL, 0x0bdbdf21UL, 0x86d3d2d4UL, 0xf1d4e242UL,
7108  0x68ddb3f8UL, 0x1fda836eUL, 0x81be16cdUL, 0xf6b9265bUL, 0x6fb077e1UL,
7109  0x18b74777UL, 0x88085ae6UL, 0xff0f6a70UL, 0x66063bcaUL, 0x11010b5cUL,
7110  0x8f659effUL, 0xf862ae69UL, 0x616bffd3UL, 0x166ccf45UL, 0xa00ae278UL,
7111  0xd70dd2eeUL, 0x4e048354UL, 0x3903b3c2UL, 0xa7672661UL, 0xd06016f7UL,
7112  0x4969474dUL, 0x3e6e77dbUL, 0xaed16a4aUL, 0xd9d65adcUL, 0x40df0b66UL,
7113  0x37d83bf0UL, 0xa9bcae53UL, 0xdebb9ec5UL, 0x47b2cf7fUL, 0x30b5ffe9UL,
7114  0xbdbdf21cUL, 0xcabac28aUL, 0x53b39330UL, 0x24b4a3a6UL, 0xbad03605UL,
7115  0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL,
7116  0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL,
7117  0x2d02ef8dUL
7118 #ifdef BYFOUR
7119  },
7120  {
7121  0x00000000UL, 0x191b3141UL, 0x32366282UL, 0x2b2d53c3UL, 0x646cc504UL,
7122  0x7d77f445UL, 0x565aa786UL, 0x4f4196c7UL, 0xc8d98a08UL, 0xd1c2bb49UL,
7123  0xfaefe88aUL, 0xe3f4d9cbUL, 0xacb54f0cUL, 0xb5ae7e4dUL, 0x9e832d8eUL,
7124  0x87981ccfUL, 0x4ac21251UL, 0x53d92310UL, 0x78f470d3UL, 0x61ef4192UL,
7125  0x2eaed755UL, 0x37b5e614UL, 0x1c98b5d7UL, 0x05838496UL, 0x821b9859UL,
7126  0x9b00a918UL, 0xb02dfadbUL, 0xa936cb9aUL, 0xe6775d5dUL, 0xff6c6c1cUL,
7127  0xd4413fdfUL, 0xcd5a0e9eUL, 0x958424a2UL, 0x8c9f15e3UL, 0xa7b24620UL,
7128  0xbea97761UL, 0xf1e8e1a6UL, 0xe8f3d0e7UL, 0xc3de8324UL, 0xdac5b265UL,
7129  0x5d5daeaaUL, 0x44469febUL, 0x6f6bcc28UL, 0x7670fd69UL, 0x39316baeUL,
7130  0x202a5aefUL, 0x0b07092cUL, 0x121c386dUL, 0xdf4636f3UL, 0xc65d07b2UL,
7131  0xed705471UL, 0xf46b6530UL, 0xbb2af3f7UL, 0xa231c2b6UL, 0x891c9175UL,
7132  0x9007a034UL, 0x179fbcfbUL, 0x0e848dbaUL, 0x25a9de79UL, 0x3cb2ef38UL,
7133  0x73f379ffUL, 0x6ae848beUL, 0x41c51b7dUL, 0x58de2a3cUL, 0xf0794f05UL,
7134  0xe9627e44UL, 0xc24f2d87UL, 0xdb541cc6UL, 0x94158a01UL, 0x8d0ebb40UL,
7135  0xa623e883UL, 0xbf38d9c2UL, 0x38a0c50dUL, 0x21bbf44cUL, 0x0a96a78fUL,
7136  0x138d96ceUL, 0x5ccc0009UL, 0x45d73148UL, 0x6efa628bUL, 0x77e153caUL,
7137  0xbabb5d54UL, 0xa3a06c15UL, 0x888d3fd6UL, 0x91960e97UL, 0xded79850UL,
7138  0xc7cca911UL, 0xece1fad2UL, 0xf5facb93UL, 0x7262d75cUL, 0x6b79e61dUL,
7139  0x4054b5deUL, 0x594f849fUL, 0x160e1258UL, 0x0f152319UL, 0x243870daUL,
7140  0x3d23419bUL, 0x65fd6ba7UL, 0x7ce65ae6UL, 0x57cb0925UL, 0x4ed03864UL,
7141  0x0191aea3UL, 0x188a9fe2UL, 0x33a7cc21UL, 0x2abcfd60UL, 0xad24e1afUL,
7142  0xb43fd0eeUL, 0x9f12832dUL, 0x8609b26cUL, 0xc94824abUL, 0xd05315eaUL,
7143  0xfb7e4629UL, 0xe2657768UL, 0x2f3f79f6UL, 0x362448b7UL, 0x1d091b74UL,
7144  0x04122a35UL, 0x4b53bcf2UL, 0x52488db3UL, 0x7965de70UL, 0x607eef31UL,
7145  0xe7e6f3feUL, 0xfefdc2bfUL, 0xd5d0917cUL, 0xcccba03dUL, 0x838a36faUL,
7146  0x9a9107bbUL, 0xb1bc5478UL, 0xa8a76539UL, 0x3b83984bUL, 0x2298a90aUL,
7147  0x09b5fac9UL, 0x10aecb88UL, 0x5fef5d4fUL, 0x46f46c0eUL, 0x6dd93fcdUL,
7148  0x74c20e8cUL, 0xf35a1243UL, 0xea412302UL, 0xc16c70c1UL, 0xd8774180UL,
7149  0x9736d747UL, 0x8e2de606UL, 0xa500b5c5UL, 0xbc1b8484UL, 0x71418a1aUL,
7150  0x685abb5bUL, 0x4377e898UL, 0x5a6cd9d9UL, 0x152d4f1eUL, 0x0c367e5fUL,
7151  0x271b2d9cUL, 0x3e001cddUL, 0xb9980012UL, 0xa0833153UL, 0x8bae6290UL,
7152  0x92b553d1UL, 0xddf4c516UL, 0xc4eff457UL, 0xefc2a794UL, 0xf6d996d5UL,
7153  0xae07bce9UL, 0xb71c8da8UL, 0x9c31de6bUL, 0x852aef2aUL, 0xca6b79edUL,
7154  0xd37048acUL, 0xf85d1b6fUL, 0xe1462a2eUL, 0x66de36e1UL, 0x7fc507a0UL,
7155  0x54e85463UL, 0x4df36522UL, 0x02b2f3e5UL, 0x1ba9c2a4UL, 0x30849167UL,
7156  0x299fa026UL, 0xe4c5aeb8UL, 0xfdde9ff9UL, 0xd6f3cc3aUL, 0xcfe8fd7bUL,
7157  0x80a96bbcUL, 0x99b25afdUL, 0xb29f093eUL, 0xab84387fUL, 0x2c1c24b0UL,
7158  0x350715f1UL, 0x1e2a4632UL, 0x07317773UL, 0x4870e1b4UL, 0x516bd0f5UL,
7159  0x7a468336UL, 0x635db277UL, 0xcbfad74eUL, 0xd2e1e60fUL, 0xf9ccb5ccUL,
7160  0xe0d7848dUL, 0xaf96124aUL, 0xb68d230bUL, 0x9da070c8UL, 0x84bb4189UL,
7161  0x03235d46UL, 0x1a386c07UL, 0x31153fc4UL, 0x280e0e85UL, 0x674f9842UL,
7162  0x7e54a903UL, 0x5579fac0UL, 0x4c62cb81UL, 0x8138c51fUL, 0x9823f45eUL,
7163  0xb30ea79dUL, 0xaa1596dcUL, 0xe554001bUL, 0xfc4f315aUL, 0xd7626299UL,
7164  0xce7953d8UL, 0x49e14f17UL, 0x50fa7e56UL, 0x7bd72d95UL, 0x62cc1cd4UL,
7165  0x2d8d8a13UL, 0x3496bb52UL, 0x1fbbe891UL, 0x06a0d9d0UL, 0x5e7ef3ecUL,
7166  0x4765c2adUL, 0x6c48916eUL, 0x7553a02fUL, 0x3a1236e8UL, 0x230907a9UL,
7167  0x0824546aUL, 0x113f652bUL, 0x96a779e4UL, 0x8fbc48a5UL, 0xa4911b66UL,
7168  0xbd8a2a27UL, 0xf2cbbce0UL, 0xebd08da1UL, 0xc0fdde62UL, 0xd9e6ef23UL,
7169  0x14bce1bdUL, 0x0da7d0fcUL, 0x268a833fUL, 0x3f91b27eUL, 0x70d024b9UL,
7170  0x69cb15f8UL, 0x42e6463bUL, 0x5bfd777aUL, 0xdc656bb5UL, 0xc57e5af4UL,
7171  0xee530937UL, 0xf7483876UL, 0xb809aeb1UL, 0xa1129ff0UL, 0x8a3fcc33UL,
7172  0x9324fd72UL
7173  },
7174  {
7175  0x00000000UL, 0x01c26a37UL, 0x0384d46eUL, 0x0246be59UL, 0x0709a8dcUL,
7176  0x06cbc2ebUL, 0x048d7cb2UL, 0x054f1685UL, 0x0e1351b8UL, 0x0fd13b8fUL,
7177  0x0d9785d6UL, 0x0c55efe1UL, 0x091af964UL, 0x08d89353UL, 0x0a9e2d0aUL,
7178  0x0b5c473dUL, 0x1c26a370UL, 0x1de4c947UL, 0x1fa2771eUL, 0x1e601d29UL,
7179  0x1b2f0bacUL, 0x1aed619bUL, 0x18abdfc2UL, 0x1969b5f5UL, 0x1235f2c8UL,
7180  0x13f798ffUL, 0x11b126a6UL, 0x10734c91UL, 0x153c5a14UL, 0x14fe3023UL,
7181  0x16b88e7aUL, 0x177ae44dUL, 0x384d46e0UL, 0x398f2cd7UL, 0x3bc9928eUL,
7182  0x3a0bf8b9UL, 0x3f44ee3cUL, 0x3e86840bUL, 0x3cc03a52UL, 0x3d025065UL,
7183  0x365e1758UL, 0x379c7d6fUL, 0x35dac336UL, 0x3418a901UL, 0x3157bf84UL,
7184  0x3095d5b3UL, 0x32d36beaUL, 0x331101ddUL, 0x246be590UL, 0x25a98fa7UL,
7185  0x27ef31feUL, 0x262d5bc9UL, 0x23624d4cUL, 0x22a0277bUL, 0x20e69922UL,
7186  0x2124f315UL, 0x2a78b428UL, 0x2bbade1fUL, 0x29fc6046UL, 0x283e0a71UL,
7187  0x2d711cf4UL, 0x2cb376c3UL, 0x2ef5c89aUL, 0x2f37a2adUL, 0x709a8dc0UL,
7188  0x7158e7f7UL, 0x731e59aeUL, 0x72dc3399UL, 0x7793251cUL, 0x76514f2bUL,
7189  0x7417f172UL, 0x75d59b45UL, 0x7e89dc78UL, 0x7f4bb64fUL, 0x7d0d0816UL,
7190  0x7ccf6221UL, 0x798074a4UL, 0x78421e93UL, 0x7a04a0caUL, 0x7bc6cafdUL,
7191  0x6cbc2eb0UL, 0x6d7e4487UL, 0x6f38fadeUL, 0x6efa90e9UL, 0x6bb5866cUL,
7192  0x6a77ec5bUL, 0x68315202UL, 0x69f33835UL, 0x62af7f08UL, 0x636d153fUL,
7193  0x612bab66UL, 0x60e9c151UL, 0x65a6d7d4UL, 0x6464bde3UL, 0x662203baUL,
7194  0x67e0698dUL, 0x48d7cb20UL, 0x4915a117UL, 0x4b531f4eUL, 0x4a917579UL,
7195  0x4fde63fcUL, 0x4e1c09cbUL, 0x4c5ab792UL, 0x4d98dda5UL, 0x46c49a98UL,
7196  0x4706f0afUL, 0x45404ef6UL, 0x448224c1UL, 0x41cd3244UL, 0x400f5873UL,
7197  0x4249e62aUL, 0x438b8c1dUL, 0x54f16850UL, 0x55330267UL, 0x5775bc3eUL,
7198  0x56b7d609UL, 0x53f8c08cUL, 0x523aaabbUL, 0x507c14e2UL, 0x51be7ed5UL,
7199  0x5ae239e8UL, 0x5b2053dfUL, 0x5966ed86UL, 0x58a487b1UL, 0x5deb9134UL,
7200  0x5c29fb03UL, 0x5e6f455aUL, 0x5fad2f6dUL, 0xe1351b80UL, 0xe0f771b7UL,
7201  0xe2b1cfeeUL, 0xe373a5d9UL, 0xe63cb35cUL, 0xe7fed96bUL, 0xe5b86732UL,
7202  0xe47a0d05UL, 0xef264a38UL, 0xeee4200fUL, 0xeca29e56UL, 0xed60f461UL,
7203  0xe82fe2e4UL, 0xe9ed88d3UL, 0xebab368aUL, 0xea695cbdUL, 0xfd13b8f0UL,
7204  0xfcd1d2c7UL, 0xfe976c9eUL, 0xff5506a9UL, 0xfa1a102cUL, 0xfbd87a1bUL,
7205  0xf99ec442UL, 0xf85cae75UL, 0xf300e948UL, 0xf2c2837fUL, 0xf0843d26UL,
7206  0xf1465711UL, 0xf4094194UL, 0xf5cb2ba3UL, 0xf78d95faUL, 0xf64fffcdUL,
7207  0xd9785d60UL, 0xd8ba3757UL, 0xdafc890eUL, 0xdb3ee339UL, 0xde71f5bcUL,
7208  0xdfb39f8bUL, 0xddf521d2UL, 0xdc374be5UL, 0xd76b0cd8UL, 0xd6a966efUL,
7209  0xd4efd8b6UL, 0xd52db281UL, 0xd062a404UL, 0xd1a0ce33UL, 0xd3e6706aUL,
7210  0xd2241a5dUL, 0xc55efe10UL, 0xc49c9427UL, 0xc6da2a7eUL, 0xc7184049UL,
7211  0xc25756ccUL, 0xc3953cfbUL, 0xc1d382a2UL, 0xc011e895UL, 0xcb4dafa8UL,
7212  0xca8fc59fUL, 0xc8c97bc6UL, 0xc90b11f1UL, 0xcc440774UL, 0xcd866d43UL,
7213  0xcfc0d31aUL, 0xce02b92dUL, 0x91af9640UL, 0x906dfc77UL, 0x922b422eUL,
7214  0x93e92819UL, 0x96a63e9cUL, 0x976454abUL, 0x9522eaf2UL, 0x94e080c5UL,
7215  0x9fbcc7f8UL, 0x9e7eadcfUL, 0x9c381396UL, 0x9dfa79a1UL, 0x98b56f24UL,
7216  0x99770513UL, 0x9b31bb4aUL, 0x9af3d17dUL, 0x8d893530UL, 0x8c4b5f07UL,
7217  0x8e0de15eUL, 0x8fcf8b69UL, 0x8a809decUL, 0x8b42f7dbUL, 0x89044982UL,
7218  0x88c623b5UL, 0x839a6488UL, 0x82580ebfUL, 0x801eb0e6UL, 0x81dcdad1UL,
7219  0x8493cc54UL, 0x8551a663UL, 0x8717183aUL, 0x86d5720dUL, 0xa9e2d0a0UL,
7220  0xa820ba97UL, 0xaa6604ceUL, 0xaba46ef9UL, 0xaeeb787cUL, 0xaf29124bUL,
7221  0xad6fac12UL, 0xacadc625UL, 0xa7f18118UL, 0xa633eb2fUL, 0xa4755576UL,
7222  0xa5b73f41UL, 0xa0f829c4UL, 0xa13a43f3UL, 0xa37cfdaaUL, 0xa2be979dUL,
7223  0xb5c473d0UL, 0xb40619e7UL, 0xb640a7beUL, 0xb782cd89UL, 0xb2cddb0cUL,
7224  0xb30fb13bUL, 0xb1490f62UL, 0xb08b6555UL, 0xbbd72268UL, 0xba15485fUL,
7225  0xb853f606UL, 0xb9919c31UL, 0xbcde8ab4UL, 0xbd1ce083UL, 0xbf5a5edaUL,
7226  0xbe9834edUL
7227  },
7228  {
7229  0x00000000UL, 0xb8bc6765UL, 0xaa09c88bUL, 0x12b5afeeUL, 0x8f629757UL,
7230  0x37def032UL, 0x256b5fdcUL, 0x9dd738b9UL, 0xc5b428efUL, 0x7d084f8aUL,
7231  0x6fbde064UL, 0xd7018701UL, 0x4ad6bfb8UL, 0xf26ad8ddUL, 0xe0df7733UL,
7232  0x58631056UL, 0x5019579fUL, 0xe8a530faUL, 0xfa109f14UL, 0x42acf871UL,
7233  0xdf7bc0c8UL, 0x67c7a7adUL, 0x75720843UL, 0xcdce6f26UL, 0x95ad7f70UL,
7234  0x2d111815UL, 0x3fa4b7fbUL, 0x8718d09eUL, 0x1acfe827UL, 0xa2738f42UL,
7235  0xb0c620acUL, 0x087a47c9UL, 0xa032af3eUL, 0x188ec85bUL, 0x0a3b67b5UL,
7236  0xb28700d0UL, 0x2f503869UL, 0x97ec5f0cUL, 0x8559f0e2UL, 0x3de59787UL,
7237  0x658687d1UL, 0xdd3ae0b4UL, 0xcf8f4f5aUL, 0x7733283fUL, 0xeae41086UL,
7238  0x525877e3UL, 0x40edd80dUL, 0xf851bf68UL, 0xf02bf8a1UL, 0x48979fc4UL,
7239  0x5a22302aUL, 0xe29e574fUL, 0x7f496ff6UL, 0xc7f50893UL, 0xd540a77dUL,
7240  0x6dfcc018UL, 0x359fd04eUL, 0x8d23b72bUL, 0x9f9618c5UL, 0x272a7fa0UL,
7241  0xbafd4719UL, 0x0241207cUL, 0x10f48f92UL, 0xa848e8f7UL, 0x9b14583dUL,
7242  0x23a83f58UL, 0x311d90b6UL, 0x89a1f7d3UL, 0x1476cf6aUL, 0xaccaa80fUL,
7243  0xbe7f07e1UL, 0x06c36084UL, 0x5ea070d2UL, 0xe61c17b7UL, 0xf4a9b859UL,
7244  0x4c15df3cUL, 0xd1c2e785UL, 0x697e80e0UL, 0x7bcb2f0eUL, 0xc377486bUL,
7245  0xcb0d0fa2UL, 0x73b168c7UL, 0x6104c729UL, 0xd9b8a04cUL, 0x446f98f5UL,
7246  0xfcd3ff90UL, 0xee66507eUL, 0x56da371bUL, 0x0eb9274dUL, 0xb6054028UL,
7247  0xa4b0efc6UL, 0x1c0c88a3UL, 0x81dbb01aUL, 0x3967d77fUL, 0x2bd27891UL,
7248  0x936e1ff4UL, 0x3b26f703UL, 0x839a9066UL, 0x912f3f88UL, 0x299358edUL,
7249  0xb4446054UL, 0x0cf80731UL, 0x1e4da8dfUL, 0xa6f1cfbaUL, 0xfe92dfecUL,
7250  0x462eb889UL, 0x549b1767UL, 0xec277002UL, 0x71f048bbUL, 0xc94c2fdeUL,
7251  0xdbf98030UL, 0x6345e755UL, 0x6b3fa09cUL, 0xd383c7f9UL, 0xc1366817UL,
7252  0x798a0f72UL, 0xe45d37cbUL, 0x5ce150aeUL, 0x4e54ff40UL, 0xf6e89825UL,
7253  0xae8b8873UL, 0x1637ef16UL, 0x048240f8UL, 0xbc3e279dUL, 0x21e91f24UL,
7254  0x99557841UL, 0x8be0d7afUL, 0x335cb0caUL, 0xed59b63bUL, 0x55e5d15eUL,
7255  0x47507eb0UL, 0xffec19d5UL, 0x623b216cUL, 0xda874609UL, 0xc832e9e7UL,
7256  0x708e8e82UL, 0x28ed9ed4UL, 0x9051f9b1UL, 0x82e4565fUL, 0x3a58313aUL,
7257  0xa78f0983UL, 0x1f336ee6UL, 0x0d86c108UL, 0xb53aa66dUL, 0xbd40e1a4UL,
7258  0x05fc86c1UL, 0x1749292fUL, 0xaff54e4aUL, 0x322276f3UL, 0x8a9e1196UL,
7259  0x982bbe78UL, 0x2097d91dUL, 0x78f4c94bUL, 0xc048ae2eUL, 0xd2fd01c0UL,
7260  0x6a4166a5UL, 0xf7965e1cUL, 0x4f2a3979UL, 0x5d9f9697UL, 0xe523f1f2UL,
7261  0x4d6b1905UL, 0xf5d77e60UL, 0xe762d18eUL, 0x5fdeb6ebUL, 0xc2098e52UL,
7262  0x7ab5e937UL, 0x680046d9UL, 0xd0bc21bcUL, 0x88df31eaUL, 0x3063568fUL,
7263  0x22d6f961UL, 0x9a6a9e04UL, 0x07bda6bdUL, 0xbf01c1d8UL, 0xadb46e36UL,
7264  0x15080953UL, 0x1d724e9aUL, 0xa5ce29ffUL, 0xb77b8611UL, 0x0fc7e174UL,
7265  0x9210d9cdUL, 0x2aacbea8UL, 0x38191146UL, 0x80a57623UL, 0xd8c66675UL,
7266  0x607a0110UL, 0x72cfaefeUL, 0xca73c99bUL, 0x57a4f122UL, 0xef189647UL,
7267  0xfdad39a9UL, 0x45115eccUL, 0x764dee06UL, 0xcef18963UL, 0xdc44268dUL,
7268  0x64f841e8UL, 0xf92f7951UL, 0x41931e34UL, 0x5326b1daUL, 0xeb9ad6bfUL,
7269  0xb3f9c6e9UL, 0x0b45a18cUL, 0x19f00e62UL, 0xa14c6907UL, 0x3c9b51beUL,
7270  0x842736dbUL, 0x96929935UL, 0x2e2efe50UL, 0x2654b999UL, 0x9ee8defcUL,
7271  0x8c5d7112UL, 0x34e11677UL, 0xa9362eceUL, 0x118a49abUL, 0x033fe645UL,
7272  0xbb838120UL, 0xe3e09176UL, 0x5b5cf613UL, 0x49e959fdUL, 0xf1553e98UL,
7273  0x6c820621UL, 0xd43e6144UL, 0xc68bceaaUL, 0x7e37a9cfUL, 0xd67f4138UL,
7274  0x6ec3265dUL, 0x7c7689b3UL, 0xc4caeed6UL, 0x591dd66fUL, 0xe1a1b10aUL,
7275  0xf3141ee4UL, 0x4ba87981UL, 0x13cb69d7UL, 0xab770eb2UL, 0xb9c2a15cUL,
7276  0x017ec639UL, 0x9ca9fe80UL, 0x241599e5UL, 0x36a0360bUL, 0x8e1c516eUL,
7277  0x866616a7UL, 0x3eda71c2UL, 0x2c6fde2cUL, 0x94d3b949UL, 0x090481f0UL,
7278  0xb1b8e695UL, 0xa30d497bUL, 0x1bb12e1eUL, 0x43d23e48UL, 0xfb6e592dUL,
7279  0xe9dbf6c3UL, 0x516791a6UL, 0xccb0a91fUL, 0x740cce7aUL, 0x66b96194UL,
7280  0xde0506f1UL
7281  },
7282  {
7283  0x00000000UL, 0x96300777UL, 0x2c610eeeUL, 0xba510999UL, 0x19c46d07UL,
7284  0x8ff46a70UL, 0x35a563e9UL, 0xa395649eUL, 0x3288db0eUL, 0xa4b8dc79UL,
7285  0x1ee9d5e0UL, 0x88d9d297UL, 0x2b4cb609UL, 0xbd7cb17eUL, 0x072db8e7UL,
7286  0x911dbf90UL, 0x6410b71dUL, 0xf220b06aUL, 0x4871b9f3UL, 0xde41be84UL,
7287  0x7dd4da1aUL, 0xebe4dd6dUL, 0x51b5d4f4UL, 0xc785d383UL, 0x56986c13UL,
7288  0xc0a86b64UL, 0x7af962fdUL, 0xecc9658aUL, 0x4f5c0114UL, 0xd96c0663UL,
7289  0x633d0ffaUL, 0xf50d088dUL, 0xc8206e3bUL, 0x5e10694cUL, 0xe44160d5UL,
7290  0x727167a2UL, 0xd1e4033cUL, 0x47d4044bUL, 0xfd850dd2UL, 0x6bb50aa5UL,
7291  0xfaa8b535UL, 0x6c98b242UL, 0xd6c9bbdbUL, 0x40f9bcacUL, 0xe36cd832UL,
7292  0x755cdf45UL, 0xcf0dd6dcUL, 0x593dd1abUL, 0xac30d926UL, 0x3a00de51UL,
7293  0x8051d7c8UL, 0x1661d0bfUL, 0xb5f4b421UL, 0x23c4b356UL, 0x9995bacfUL,
7294  0x0fa5bdb8UL, 0x9eb80228UL, 0x0888055fUL, 0xb2d90cc6UL, 0x24e90bb1UL,
7295  0x877c6f2fUL, 0x114c6858UL, 0xab1d61c1UL, 0x3d2d66b6UL, 0x9041dc76UL,
7296  0x0671db01UL, 0xbc20d298UL, 0x2a10d5efUL, 0x8985b171UL, 0x1fb5b606UL,
7297  0xa5e4bf9fUL, 0x33d4b8e8UL, 0xa2c90778UL, 0x34f9000fUL, 0x8ea80996UL,
7298  0x18980ee1UL, 0xbb0d6a7fUL, 0x2d3d6d08UL, 0x976c6491UL, 0x015c63e6UL,
7299  0xf4516b6bUL, 0x62616c1cUL, 0xd8306585UL, 0x4e0062f2UL, 0xed95066cUL,
7300  0x7ba5011bUL, 0xc1f40882UL, 0x57c40ff5UL, 0xc6d9b065UL, 0x50e9b712UL,
7301  0xeab8be8bUL, 0x7c88b9fcUL, 0xdf1ddd62UL, 0x492dda15UL, 0xf37cd38cUL,
7302  0x654cd4fbUL, 0x5861b24dUL, 0xce51b53aUL, 0x7400bca3UL, 0xe230bbd4UL,
7303  0x41a5df4aUL, 0xd795d83dUL, 0x6dc4d1a4UL, 0xfbf4d6d3UL, 0x6ae96943UL,
7304  0xfcd96e34UL, 0x468867adUL, 0xd0b860daUL, 0x732d0444UL, 0xe51d0333UL,
7305  0x5f4c0aaaUL, 0xc97c0dddUL, 0x3c710550UL, 0xaa410227UL, 0x10100bbeUL,
7306  0x86200cc9UL, 0x25b56857UL, 0xb3856f20UL, 0x09d466b9UL, 0x9fe461ceUL,
7307  0x0ef9de5eUL, 0x98c9d929UL, 0x2298d0b0UL, 0xb4a8d7c7UL, 0x173db359UL,
7308  0x810db42eUL, 0x3b5cbdb7UL, 0xad6cbac0UL, 0x2083b8edUL, 0xb6b3bf9aUL,
7309  0x0ce2b603UL, 0x9ad2b174UL, 0x3947d5eaUL, 0xaf77d29dUL, 0x1526db04UL,
7310  0x8316dc73UL, 0x120b63e3UL, 0x843b6494UL, 0x3e6a6d0dUL, 0xa85a6a7aUL,
7311  0x0bcf0ee4UL, 0x9dff0993UL, 0x27ae000aUL, 0xb19e077dUL, 0x44930ff0UL,
7312  0xd2a30887UL, 0x68f2011eUL, 0xfec20669UL, 0x5d5762f7UL, 0xcb676580UL,
7313  0x71366c19UL, 0xe7066b6eUL, 0x761bd4feUL, 0xe02bd389UL, 0x5a7ada10UL,
7314  0xcc4add67UL, 0x6fdfb9f9UL, 0xf9efbe8eUL, 0x43beb717UL, 0xd58eb060UL,
7315  0xe8a3d6d6UL, 0x7e93d1a1UL, 0xc4c2d838UL, 0x52f2df4fUL, 0xf167bbd1UL,
7316  0x6757bca6UL, 0xdd06b53fUL, 0x4b36b248UL, 0xda2b0dd8UL, 0x4c1b0aafUL,
7317  0xf64a0336UL, 0x607a0441UL, 0xc3ef60dfUL, 0x55df67a8UL, 0xef8e6e31UL,
7318  0x79be6946UL, 0x8cb361cbUL, 0x1a8366bcUL, 0xa0d26f25UL, 0x36e26852UL,
7319  0x95770cccUL, 0x03470bbbUL, 0xb9160222UL, 0x2f260555UL, 0xbe3bbac5UL,
7320  0x280bbdb2UL, 0x925ab42bUL, 0x046ab35cUL, 0xa7ffd7c2UL, 0x31cfd0b5UL,
7321  0x8b9ed92cUL, 0x1daede5bUL, 0xb0c2649bUL, 0x26f263ecUL, 0x9ca36a75UL,
7322  0x0a936d02UL, 0xa906099cUL, 0x3f360eebUL, 0x85670772UL, 0x13570005UL,
7323  0x824abf95UL, 0x147ab8e2UL, 0xae2bb17bUL, 0x381bb60cUL, 0x9b8ed292UL,
7324  0x0dbed5e5UL, 0xb7efdc7cUL, 0x21dfdb0bUL, 0xd4d2d386UL, 0x42e2d4f1UL,
7325  0xf8b3dd68UL, 0x6e83da1fUL, 0xcd16be81UL, 0x5b26b9f6UL, 0xe177b06fUL,
7326  0x7747b718UL, 0xe65a0888UL, 0x706a0fffUL, 0xca3b0666UL, 0x5c0b0111UL,
7327  0xff9e658fUL, 0x69ae62f8UL, 0xd3ff6b61UL, 0x45cf6c16UL, 0x78e20aa0UL,
7328  0xeed20dd7UL, 0x5483044eUL, 0xc2b30339UL, 0x612667a7UL, 0xf71660d0UL,
7329  0x4d476949UL, 0xdb776e3eUL, 0x4a6ad1aeUL, 0xdc5ad6d9UL, 0x660bdf40UL,
7330  0xf03bd837UL, 0x53aebca9UL, 0xc59ebbdeUL, 0x7fcfb247UL, 0xe9ffb530UL,
7331  0x1cf2bdbdUL, 0x8ac2bacaUL, 0x3093b353UL, 0xa6a3b424UL, 0x0536d0baUL,
7332  0x9306d7cdUL, 0x2957de54UL, 0xbf67d923UL, 0x2e7a66b3UL, 0xb84a61c4UL,
7333  0x021b685dUL, 0x942b6f2aUL, 0x37be0bb4UL, 0xa18e0cc3UL, 0x1bdf055aUL,
7334  0x8def022dUL
7335  },
7336  {
7337  0x00000000UL, 0x41311b19UL, 0x82623632UL, 0xc3532d2bUL, 0x04c56c64UL,
7338  0x45f4777dUL, 0x86a75a56UL, 0xc796414fUL, 0x088ad9c8UL, 0x49bbc2d1UL,
7339  0x8ae8effaUL, 0xcbd9f4e3UL, 0x0c4fb5acUL, 0x4d7eaeb5UL, 0x8e2d839eUL,
7340  0xcf1c9887UL, 0x5112c24aUL, 0x1023d953UL, 0xd370f478UL, 0x9241ef61UL,
7341  0x55d7ae2eUL, 0x14e6b537UL, 0xd7b5981cUL, 0x96848305UL, 0x59981b82UL,
7342  0x18a9009bUL, 0xdbfa2db0UL, 0x9acb36a9UL, 0x5d5d77e6UL, 0x1c6c6cffUL,
7343  0xdf3f41d4UL, 0x9e0e5acdUL, 0xa2248495UL, 0xe3159f8cUL, 0x2046b2a7UL,
7344  0x6177a9beUL, 0xa6e1e8f1UL, 0xe7d0f3e8UL, 0x2483dec3UL, 0x65b2c5daUL,
7345  0xaaae5d5dUL, 0xeb9f4644UL, 0x28cc6b6fUL, 0x69fd7076UL, 0xae6b3139UL,
7346  0xef5a2a20UL, 0x2c09070bUL, 0x6d381c12UL, 0xf33646dfUL, 0xb2075dc6UL,
7347  0x715470edUL, 0x30656bf4UL, 0xf7f32abbUL, 0xb6c231a2UL, 0x75911c89UL,
7348  0x34a00790UL, 0xfbbc9f17UL, 0xba8d840eUL, 0x79dea925UL, 0x38efb23cUL,
7349  0xff79f373UL, 0xbe48e86aUL, 0x7d1bc541UL, 0x3c2ade58UL, 0x054f79f0UL,
7350  0x447e62e9UL, 0x872d4fc2UL, 0xc61c54dbUL, 0x018a1594UL, 0x40bb0e8dUL,
7351  0x83e823a6UL, 0xc2d938bfUL, 0x0dc5a038UL, 0x4cf4bb21UL, 0x8fa7960aUL,
7352  0xce968d13UL, 0x0900cc5cUL, 0x4831d745UL, 0x8b62fa6eUL, 0xca53e177UL,
7353  0x545dbbbaUL, 0x156ca0a3UL, 0xd63f8d88UL, 0x970e9691UL, 0x5098d7deUL,
7354  0x11a9ccc7UL, 0xd2fae1ecUL, 0x93cbfaf5UL, 0x5cd76272UL, 0x1de6796bUL,
7355  0xdeb55440UL, 0x9f844f59UL, 0x58120e16UL, 0x1923150fUL, 0xda703824UL,
7356  0x9b41233dUL, 0xa76bfd65UL, 0xe65ae67cUL, 0x2509cb57UL, 0x6438d04eUL,
7357  0xa3ae9101UL, 0xe29f8a18UL, 0x21cca733UL, 0x60fdbc2aUL, 0xafe124adUL,
7358  0xeed03fb4UL, 0x2d83129fUL, 0x6cb20986UL, 0xab2448c9UL, 0xea1553d0UL,
7359  0x29467efbUL, 0x687765e2UL, 0xf6793f2fUL, 0xb7482436UL, 0x741b091dUL,
7360  0x352a1204UL, 0xf2bc534bUL, 0xb38d4852UL, 0x70de6579UL, 0x31ef7e60UL,
7361  0xfef3e6e7UL, 0xbfc2fdfeUL, 0x7c91d0d5UL, 0x3da0cbccUL, 0xfa368a83UL,
7362  0xbb07919aUL, 0x7854bcb1UL, 0x3965a7a8UL, 0x4b98833bUL, 0x0aa99822UL,
7363  0xc9fab509UL, 0x88cbae10UL, 0x4f5def5fUL, 0x0e6cf446UL, 0xcd3fd96dUL,
7364  0x8c0ec274UL, 0x43125af3UL, 0x022341eaUL, 0xc1706cc1UL, 0x804177d8UL,
7365  0x47d73697UL, 0x06e62d8eUL, 0xc5b500a5UL, 0x84841bbcUL, 0x1a8a4171UL,
7366  0x5bbb5a68UL, 0x98e87743UL, 0xd9d96c5aUL, 0x1e4f2d15UL, 0x5f7e360cUL,
7367  0x9c2d1b27UL, 0xdd1c003eUL, 0x120098b9UL, 0x533183a0UL, 0x9062ae8bUL,
7368  0xd153b592UL, 0x16c5f4ddUL, 0x57f4efc4UL, 0x94a7c2efUL, 0xd596d9f6UL,
7369  0xe9bc07aeUL, 0xa88d1cb7UL, 0x6bde319cUL, 0x2aef2a85UL, 0xed796bcaUL,
7370  0xac4870d3UL, 0x6f1b5df8UL, 0x2e2a46e1UL, 0xe136de66UL, 0xa007c57fUL,
7371  0x6354e854UL, 0x2265f34dUL, 0xe5f3b202UL, 0xa4c2a91bUL, 0x67918430UL,
7372  0x26a09f29UL, 0xb8aec5e4UL, 0xf99fdefdUL, 0x3accf3d6UL, 0x7bfde8cfUL,
7373  0xbc6ba980UL, 0xfd5ab299UL, 0x3e099fb2UL, 0x7f3884abUL, 0xb0241c2cUL,
7374  0xf1150735UL, 0x32462a1eUL, 0x73773107UL, 0xb4e17048UL, 0xf5d06b51UL,
7375  0x3683467aUL, 0x77b25d63UL, 0x4ed7facbUL, 0x0fe6e1d2UL, 0xccb5ccf9UL,
7376  0x8d84d7e0UL, 0x4a1296afUL, 0x0b238db6UL, 0xc870a09dUL, 0x8941bb84UL,
7377  0x465d2303UL, 0x076c381aUL, 0xc43f1531UL, 0x850e0e28UL, 0x42984f67UL,
7378  0x03a9547eUL, 0xc0fa7955UL, 0x81cb624cUL, 0x1fc53881UL, 0x5ef42398UL,
7379  0x9da70eb3UL, 0xdc9615aaUL, 0x1b0054e5UL, 0x5a314ffcUL, 0x996262d7UL,
7380  0xd85379ceUL, 0x174fe149UL, 0x567efa50UL, 0x952dd77bUL, 0xd41ccc62UL,
7381  0x138a8d2dUL, 0x52bb9634UL, 0x91e8bb1fUL, 0xd0d9a006UL, 0xecf37e5eUL,
7382  0xadc26547UL, 0x6e91486cUL, 0x2fa05375UL, 0xe836123aUL, 0xa9070923UL,
7383  0x6a542408UL, 0x2b653f11UL, 0xe479a796UL, 0xa548bc8fUL, 0x661b91a4UL,
7384  0x272a8abdUL, 0xe0bccbf2UL, 0xa18dd0ebUL, 0x62defdc0UL, 0x23efe6d9UL,
7385  0xbde1bc14UL, 0xfcd0a70dUL, 0x3f838a26UL, 0x7eb2913fUL, 0xb924d070UL,
7386  0xf815cb69UL, 0x3b46e642UL, 0x7a77fd5bUL, 0xb56b65dcUL, 0xf45a7ec5UL,
7387  0x370953eeUL, 0x763848f7UL, 0xb1ae09b8UL, 0xf09f12a1UL, 0x33cc3f8aUL,
7388  0x72fd2493UL
7389  },
7390  {
7391  0x00000000UL, 0x376ac201UL, 0x6ed48403UL, 0x59be4602UL, 0xdca80907UL,
7392  0xebc2cb06UL, 0xb27c8d04UL, 0x85164f05UL, 0xb851130eUL, 0x8f3bd10fUL,
7393  0xd685970dUL, 0xe1ef550cUL, 0x64f91a09UL, 0x5393d808UL, 0x0a2d9e0aUL,
7394  0x3d475c0bUL, 0x70a3261cUL, 0x47c9e41dUL, 0x1e77a21fUL, 0x291d601eUL,
7395  0xac0b2f1bUL, 0x9b61ed1aUL, 0xc2dfab18UL, 0xf5b56919UL, 0xc8f23512UL,
7396  0xff98f713UL, 0xa626b111UL, 0x914c7310UL, 0x145a3c15UL, 0x2330fe14UL,
7397  0x7a8eb816UL, 0x4de47a17UL, 0xe0464d38UL, 0xd72c8f39UL, 0x8e92c93bUL,
7398  0xb9f80b3aUL, 0x3cee443fUL, 0x0b84863eUL, 0x523ac03cUL, 0x6550023dUL,
7399  0x58175e36UL, 0x6f7d9c37UL, 0x36c3da35UL, 0x01a91834UL, 0x84bf5731UL,
7400  0xb3d59530UL, 0xea6bd332UL, 0xdd011133UL, 0x90e56b24UL, 0xa78fa925UL,
7401  0xfe31ef27UL, 0xc95b2d26UL, 0x4c4d6223UL, 0x7b27a022UL, 0x2299e620UL,
7402  0x15f32421UL, 0x28b4782aUL, 0x1fdeba2bUL, 0x4660fc29UL, 0x710a3e28UL,
7403  0xf41c712dUL, 0xc376b32cUL, 0x9ac8f52eUL, 0xada2372fUL, 0xc08d9a70UL,
7404  0xf7e75871UL, 0xae591e73UL, 0x9933dc72UL, 0x1c259377UL, 0x2b4f5176UL,
7405  0x72f11774UL, 0x459bd575UL, 0x78dc897eUL, 0x4fb64b7fUL, 0x16080d7dUL,
7406  0x2162cf7cUL, 0xa4748079UL, 0x931e4278UL, 0xcaa0047aUL, 0xfdcac67bUL,
7407  0xb02ebc6cUL, 0x87447e6dUL, 0xdefa386fUL, 0xe990fa6eUL, 0x6c86b56bUL,
7408  0x5bec776aUL, 0x02523168UL, 0x3538f369UL, 0x087faf62UL, 0x3f156d63UL,
7409  0x66ab2b61UL, 0x51c1e960UL, 0xd4d7a665UL, 0xe3bd6464UL, 0xba032266UL,
7410  0x8d69e067UL, 0x20cbd748UL, 0x17a11549UL, 0x4e1f534bUL, 0x7975914aUL,
7411  0xfc63de4fUL, 0xcb091c4eUL, 0x92b75a4cUL, 0xa5dd984dUL, 0x989ac446UL,
7412  0xaff00647UL, 0xf64e4045UL, 0xc1248244UL, 0x4432cd41UL, 0x73580f40UL,
7413  0x2ae64942UL, 0x1d8c8b43UL, 0x5068f154UL, 0x67023355UL, 0x3ebc7557UL,
7414  0x09d6b756UL, 0x8cc0f853UL, 0xbbaa3a52UL, 0xe2147c50UL, 0xd57ebe51UL,
7415  0xe839e25aUL, 0xdf53205bUL, 0x86ed6659UL, 0xb187a458UL, 0x3491eb5dUL,
7416  0x03fb295cUL, 0x5a456f5eUL, 0x6d2fad5fUL, 0x801b35e1UL, 0xb771f7e0UL,
7417  0xeecfb1e2UL, 0xd9a573e3UL, 0x5cb33ce6UL, 0x6bd9fee7UL, 0x3267b8e5UL,
7418  0x050d7ae4UL, 0x384a26efUL, 0x0f20e4eeUL, 0x569ea2ecUL, 0x61f460edUL,
7419  0xe4e22fe8UL, 0xd388ede9UL, 0x8a36abebUL, 0xbd5c69eaUL, 0xf0b813fdUL,
7420  0xc7d2d1fcUL, 0x9e6c97feUL, 0xa90655ffUL, 0x2c101afaUL, 0x1b7ad8fbUL,
7421  0x42c49ef9UL, 0x75ae5cf8UL, 0x48e900f3UL, 0x7f83c2f2UL, 0x263d84f0UL,
7422  0x115746f1UL, 0x944109f4UL, 0xa32bcbf5UL, 0xfa958df7UL, 0xcdff4ff6UL,
7423  0x605d78d9UL, 0x5737bad8UL, 0x0e89fcdaUL, 0x39e33edbUL, 0xbcf571deUL,
7424  0x8b9fb3dfUL, 0xd221f5ddUL, 0xe54b37dcUL, 0xd80c6bd7UL, 0xef66a9d6UL,
7425  0xb6d8efd4UL, 0x81b22dd5UL, 0x04a462d0UL, 0x33cea0d1UL, 0x6a70e6d3UL,
7426  0x5d1a24d2UL, 0x10fe5ec5UL, 0x27949cc4UL, 0x7e2adac6UL, 0x494018c7UL,
7427  0xcc5657c2UL, 0xfb3c95c3UL, 0xa282d3c1UL, 0x95e811c0UL, 0xa8af4dcbUL,
7428  0x9fc58fcaUL, 0xc67bc9c8UL, 0xf1110bc9UL, 0x740744ccUL, 0x436d86cdUL,
7429  0x1ad3c0cfUL, 0x2db902ceUL, 0x4096af91UL, 0x77fc6d90UL, 0x2e422b92UL,
7430  0x1928e993UL, 0x9c3ea696UL, 0xab546497UL, 0xf2ea2295UL, 0xc580e094UL,
7431  0xf8c7bc9fUL, 0xcfad7e9eUL, 0x9613389cUL, 0xa179fa9dUL, 0x246fb598UL,
7432  0x13057799UL, 0x4abb319bUL, 0x7dd1f39aUL, 0x3035898dUL, 0x075f4b8cUL,
7433  0x5ee10d8eUL, 0x698bcf8fUL, 0xec9d808aUL, 0xdbf7428bUL, 0x82490489UL,
7434  0xb523c688UL, 0x88649a83UL, 0xbf0e5882UL, 0xe6b01e80UL, 0xd1dadc81UL,
7435  0x54cc9384UL, 0x63a65185UL, 0x3a181787UL, 0x0d72d586UL, 0xa0d0e2a9UL,
7436  0x97ba20a8UL, 0xce0466aaUL, 0xf96ea4abUL, 0x7c78ebaeUL, 0x4b1229afUL,
7437  0x12ac6fadUL, 0x25c6adacUL, 0x1881f1a7UL, 0x2feb33a6UL, 0x765575a4UL,
7438  0x413fb7a5UL, 0xc429f8a0UL, 0xf3433aa1UL, 0xaafd7ca3UL, 0x9d97bea2UL,
7439  0xd073c4b5UL, 0xe71906b4UL, 0xbea740b6UL, 0x89cd82b7UL, 0x0cdbcdb2UL,
7440  0x3bb10fb3UL, 0x620f49b1UL, 0x55658bb0UL, 0x6822d7bbUL, 0x5f4815baUL,
7441  0x06f653b8UL, 0x319c91b9UL, 0xb48adebcUL, 0x83e01cbdUL, 0xda5e5abfUL,
7442  0xed3498beUL
7443  },
7444  {
7445  0x00000000UL, 0x6567bcb8UL, 0x8bc809aaUL, 0xeeafb512UL, 0x5797628fUL,
7446  0x32f0de37UL, 0xdc5f6b25UL, 0xb938d79dUL, 0xef28b4c5UL, 0x8a4f087dUL,
7447  0x64e0bd6fUL, 0x018701d7UL, 0xb8bfd64aUL, 0xddd86af2UL, 0x3377dfe0UL,
7448  0x56106358UL, 0x9f571950UL, 0xfa30a5e8UL, 0x149f10faUL, 0x71f8ac42UL,
7449  0xc8c07bdfUL, 0xada7c767UL, 0x43087275UL, 0x266fcecdUL, 0x707fad95UL,
7450  0x1518112dUL, 0xfbb7a43fUL, 0x9ed01887UL, 0x27e8cf1aUL, 0x428f73a2UL,
7451  0xac20c6b0UL, 0xc9477a08UL, 0x3eaf32a0UL, 0x5bc88e18UL, 0xb5673b0aUL,
7452  0xd00087b2UL, 0x6938502fUL, 0x0c5fec97UL, 0xe2f05985UL, 0x8797e53dUL,
7453  0xd1878665UL, 0xb4e03addUL, 0x5a4f8fcfUL, 0x3f283377UL, 0x8610e4eaUL,
7454  0xe3775852UL, 0x0dd8ed40UL, 0x68bf51f8UL, 0xa1f82bf0UL, 0xc49f9748UL,
7455  0x2a30225aUL, 0x4f579ee2UL, 0xf66f497fUL, 0x9308f5c7UL, 0x7da740d5UL,
7456  0x18c0fc6dUL, 0x4ed09f35UL, 0x2bb7238dUL, 0xc518969fUL, 0xa07f2a27UL,
7457  0x1947fdbaUL, 0x7c204102UL, 0x928ff410UL, 0xf7e848a8UL, 0x3d58149bUL,
7458  0x583fa823UL, 0xb6901d31UL, 0xd3f7a189UL, 0x6acf7614UL, 0x0fa8caacUL,
7459  0xe1077fbeUL, 0x8460c306UL, 0xd270a05eUL, 0xb7171ce6UL, 0x59b8a9f4UL,
7460  0x3cdf154cUL, 0x85e7c2d1UL, 0xe0807e69UL, 0x0e2fcb7bUL, 0x6b4877c3UL,
7461  0xa20f0dcbUL, 0xc768b173UL, 0x29c70461UL, 0x4ca0b8d9UL, 0xf5986f44UL,
7462  0x90ffd3fcUL, 0x7e5066eeUL, 0x1b37da56UL, 0x4d27b90eUL, 0x284005b6UL,
7463  0xc6efb0a4UL, 0xa3880c1cUL, 0x1ab0db81UL, 0x7fd76739UL, 0x9178d22bUL,
7464  0xf41f6e93UL, 0x03f7263bUL, 0x66909a83UL, 0x883f2f91UL, 0xed589329UL,
7465  0x546044b4UL, 0x3107f80cUL, 0xdfa84d1eUL, 0xbacff1a6UL, 0xecdf92feUL,
7466  0x89b82e46UL, 0x67179b54UL, 0x027027ecUL, 0xbb48f071UL, 0xde2f4cc9UL,
7467  0x3080f9dbUL, 0x55e74563UL, 0x9ca03f6bUL, 0xf9c783d3UL, 0x176836c1UL,
7468  0x720f8a79UL, 0xcb375de4UL, 0xae50e15cUL, 0x40ff544eUL, 0x2598e8f6UL,
7469  0x73888baeUL, 0x16ef3716UL, 0xf8408204UL, 0x9d273ebcUL, 0x241fe921UL,
7470  0x41785599UL, 0xafd7e08bUL, 0xcab05c33UL, 0x3bb659edUL, 0x5ed1e555UL,
7471  0xb07e5047UL, 0xd519ecffUL, 0x6c213b62UL, 0x094687daUL, 0xe7e932c8UL,
7472  0x828e8e70UL, 0xd49eed28UL, 0xb1f95190UL, 0x5f56e482UL, 0x3a31583aUL,
7473  0x83098fa7UL, 0xe66e331fUL, 0x08c1860dUL, 0x6da63ab5UL, 0xa4e140bdUL,
7474  0xc186fc05UL, 0x2f294917UL, 0x4a4ef5afUL, 0xf3762232UL, 0x96119e8aUL,
7475  0x78be2b98UL, 0x1dd99720UL, 0x4bc9f478UL, 0x2eae48c0UL, 0xc001fdd2UL,
7476  0xa566416aUL, 0x1c5e96f7UL, 0x79392a4fUL, 0x97969f5dUL, 0xf2f123e5UL,
7477  0x05196b4dUL, 0x607ed7f5UL, 0x8ed162e7UL, 0xebb6de5fUL, 0x528e09c2UL,
7478  0x37e9b57aUL, 0xd9460068UL, 0xbc21bcd0UL, 0xea31df88UL, 0x8f566330UL,
7479  0x61f9d622UL, 0x049e6a9aUL, 0xbda6bd07UL, 0xd8c101bfUL, 0x366eb4adUL,
7480  0x53090815UL, 0x9a4e721dUL, 0xff29cea5UL, 0x11867bb7UL, 0x74e1c70fUL,
7481  0xcdd91092UL, 0xa8beac2aUL, 0x46111938UL, 0x2376a580UL, 0x7566c6d8UL,
7482  0x10017a60UL, 0xfeaecf72UL, 0x9bc973caUL, 0x22f1a457UL, 0x479618efUL,
7483  0xa939adfdUL, 0xcc5e1145UL, 0x06ee4d76UL, 0x6389f1ceUL, 0x8d2644dcUL,
7484  0xe841f864UL, 0x51792ff9UL, 0x341e9341UL, 0xdab12653UL, 0xbfd69aebUL,
7485  0xe9c6f9b3UL, 0x8ca1450bUL, 0x620ef019UL, 0x07694ca1UL, 0xbe519b3cUL,
7486  0xdb362784UL, 0x35999296UL, 0x50fe2e2eUL, 0x99b95426UL, 0xfcdee89eUL,
7487  0x12715d8cUL, 0x7716e134UL, 0xce2e36a9UL, 0xab498a11UL, 0x45e63f03UL,
7488  0x208183bbUL, 0x7691e0e3UL, 0x13f65c5bUL, 0xfd59e949UL, 0x983e55f1UL,
7489  0x2106826cUL, 0x44613ed4UL, 0xaace8bc6UL, 0xcfa9377eUL, 0x38417fd6UL,
7490  0x5d26c36eUL, 0xb389767cUL, 0xd6eecac4UL, 0x6fd61d59UL, 0x0ab1a1e1UL,
7491  0xe41e14f3UL, 0x8179a84bUL, 0xd769cb13UL, 0xb20e77abUL, 0x5ca1c2b9UL,
7492  0x39c67e01UL, 0x80fea99cUL, 0xe5991524UL, 0x0b36a036UL, 0x6e511c8eUL,
7493  0xa7166686UL, 0xc271da3eUL, 0x2cde6f2cUL, 0x49b9d394UL, 0xf0810409UL,
7494  0x95e6b8b1UL, 0x7b490da3UL, 0x1e2eb11bUL, 0x483ed243UL, 0x2d596efbUL,
7495  0xc3f6dbe9UL, 0xa6916751UL, 0x1fa9b0ccUL, 0x7ace0c74UL, 0x9461b966UL,
7496  0xf10605deUL
7497 #endif
7498  }
7499 };
7500 
7501 /* =========================================================================
7502  * This function can be used by asm versions of crc32()
7503  */
7504 const unsigned long FAR * get_crc_table()
7505 {
7506  return (const unsigned long FAR *)crc_table;
7507 }
7508 
7509 /* ========================================================================= */
7510 #define CRC_DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
7511 #define CRC_DO8 CRC_DO1; CRC_DO1; CRC_DO1; CRC_DO1; CRC_DO1; CRC_DO1; CRC_DO1; CRC_DO1
7512 
7513 /* ========================================================================= */
7514 unsigned long crc32(crc, buf, len)
7515  unsigned long crc;
7516  const unsigned char FAR *buf;
7517  unsigned len;
7518 {
7519  if (buf == Z_NULL) return 0UL;
7520 
7521 #ifdef BYFOUR
7522  if (sizeof(void *) == sizeof(ptrdiff_t)) {
7523  u4 endian;
7524 
7525  endian = 1;
7526  if (*((unsigned char *)(&endian)))
7527  return crc32_little(crc, buf, len);
7528  else
7529  return crc32_big(crc, buf, len);
7530  }
7531 #endif /* BYFOUR */
7532  crc = crc ^ 0xffffffffUL;
7533  while (len >= 8) {
7534  CRC_DO8;
7535  len -= 8;
7536  }
7537  if (len) do {
7538  CRC_DO1;
7539  } while (--len);
7540  return crc ^ 0xffffffffUL;
7541 }
7542 
7543 #ifdef BYFOUR
7544 
7545 /* ========================================================================= */
7546 #define DOLIT4 c ^= *buf4++; \
7547  c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
7548  crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
7549 #define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
7550 
7551 /* ========================================================================= */
7552 local unsigned long crc32_little(crc, buf, len)
7553  unsigned long crc;
7554  const unsigned char FAR *buf;
7555  unsigned len;
7556 {
7557  register u4 c;
7558  register const u4 FAR *buf4;
7559 
7560  c = (u4)crc;
7561  c = ~c;
7562  while (len && ((ptrdiff_t)buf & 3)) {
7563  c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
7564  len--;
7565  }
7566 
7567  buf4 = (const u4 FAR *)(const void FAR *)buf;
7568  while (len >= 32) {
7569  DOLIT32;
7570  len -= 32;
7571  }
7572  while (len >= 4) {
7573  DOLIT4;
7574  len -= 4;
7575  }
7576  buf = (const unsigned char FAR *)buf4;
7577 
7578  if (len) do {
7579  c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
7580  } while (--len);
7581  c = ~c;
7582  return (unsigned long)c;
7583 }
7584 
7585 /* ========================================================================= */
7586 #define DOBIG4 c ^= *++buf4; \
7587  c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
7588  crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
7589 #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
7590 
7591 /* ========================================================================= */
7592 local unsigned long crc32_big(crc, buf, len)
7593  unsigned long crc;
7594  const unsigned char FAR *buf;
7595  unsigned len;
7596 {
7597  register u4 c;
7598  register const u4 FAR *buf4;
7599 
7600  c = REV((u4)crc);
7601  c = ~c;
7602  while (len && ((ptrdiff_t)buf & 3)) {
7603  c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
7604  len--;
7605  }
7606 
7607  buf4 = (const u4 FAR *)(const void FAR *)buf;
7608  buf4--;
7609  while (len >= 32) {
7610  DOBIG32;
7611  len -= 32;
7612  }
7613  while (len >= 4) {
7614  DOBIG4;
7615  len -= 4;
7616  }
7617  buf4++;
7618  buf = (const unsigned char FAR *)buf4;
7619 
7620  if (len) do {
7621  c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
7622  } while (--len);
7623  c = ~c;
7624  return (unsigned long)(REV(c));
7625 }
7626 
7627 #endif /* BYFOUR */
7628 
7629 #define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */
7630 
7631 /* ========================================================================= */
7632 local unsigned long gf2_matrix_times(mat, vec)
7633  unsigned long *mat;
7634  unsigned long vec;
7635 {
7636  unsigned long sum;
7637 
7638  sum = 0;
7639  while (vec) {
7640  if (vec & 1)
7641  sum ^= *mat;
7642  vec >>= 1;
7643  mat++;
7644  }
7645  return sum;
7646 }
7647 
7648 /* ========================================================================= */
7649 local void gf2_matrix_square(square, mat)
7650  unsigned long *square;
7651  unsigned long *mat;
7652 {
7653  int n;
7654 
7655  for (n = 0; n < GF2_DIM; n++)
7656  square[n] = gf2_matrix_times(mat, mat[n]);
7657 }
7658 
7659 /* ========================================================================= */
7660 uLong crc32_combine(crc1, crc2, len2)
7661  uLong crc1;
7662  uLong crc2;
7663  z_off_t len2;
7664 {
7665  int n;
7666  unsigned long row;
7667  unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */
7668  unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */
7669 
7670  /* degenerate case */
7671  if (len2 == 0)
7672  return crc1;
7673 
7674  /* put operator for one zero bit in odd */
7675  odd[0] = 0xedb88320L; /* CRC-32 polynomial */
7676  row = 1;
7677  for (n = 1; n < GF2_DIM; n++) {
7678  odd[n] = row;
7679  row <<= 1;
7680  }
7681 
7682  /* put operator for two zero bits in even */
7683  gf2_matrix_square(even, odd);
7684 
7685  /* put operator for four zero bits in odd */
7686  gf2_matrix_square(odd, even);
7687 
7688  /* apply len2 zeros to crc1 (first square will put the operator for one
7689  zero byte, eight zero bits, in even) */
7690  do {
7691  /* apply zeros operator for this bit of len2 */
7692  gf2_matrix_square(even, odd);
7693  if (len2 & 1)
7694  crc1 = gf2_matrix_times(even, crc1);
7695  len2 >>= 1;
7696 
7697  /* if no more bits set, then done */
7698  if (len2 == 0)
7699  break;
7700 
7701  /* another iteration of the loop with odd and even swapped */
7702  gf2_matrix_square(odd, even);
7703  if (len2 & 1)
7704  crc1 = gf2_matrix_times(odd, crc1);
7705  len2 >>= 1;
7706 
7707  /* if no more bits set, then done */
7708  } while (len2 != 0);
7709 
7710  /* return combined crc */
7711  crc1 ^= crc2;
7712  return crc1;
7713 }
7714 
7715 /* ===========================================================================
7716  Compresses the source buffer into the destination buffer. The level
7717  parameter has the same meaning as in deflateInit. sourceLen is the byte
7718  length of the source buffer. Upon entry, destLen is the total size of the
7719  destination buffer, which must be at least 0.1% larger than sourceLen plus
7720  12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
7721 
7722  compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
7723  memory, Z_BUF_ERROR if there was not enough room in the output buffer,
7724  Z_STREAM_ERROR if the level parameter is invalid.
7725 */
7726 int compress2 (dest, destLen, source, sourceLen, level)
7727  Bytef *dest;
7728  uLongf *destLen;
7729  const Bytef *source;
7730  uLong sourceLen;
7731  int level;
7732 {
7733  z_stream stream;
7734  int err;
7735 
7736  stream.next_in = (Bytef*)source;
7737  stream.avail_in = (uInt)sourceLen;
7738 #ifdef MAXSEG_64K
7739  /* Check for source > 64K on 16-bit machine: */
7740  if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
7741 #endif
7742  stream.next_out = dest;
7743  stream.avail_out = (uInt)*destLen;
7744  if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
7745 
7746  stream.zalloc = (alloc_func)0;
7747  stream.zfree = (free_func)0;
7748  stream.opaque = (voidpf)0;
7749 
7750  err = deflateInit(&stream, level);
7751  if (err != Z_OK) return err;
7752 
7753  err = deflate(&stream, Z_FINISH);
7754  if (err != Z_STREAM_END) {
7755  deflateEnd(&stream);
7756  return err == Z_OK ? Z_BUF_ERROR : err;
7757  }
7758  *destLen = stream.total_out;
7759 
7760  err = deflateEnd(&stream);
7761  return err;
7762 }
7763 
7764 /* ===========================================================================
7765  */
7766 int compress (dest, destLen, source, sourceLen)
7767  Bytef *dest;
7768  uLongf *destLen;
7769  const Bytef *source;
7770  uLong sourceLen;
7771 {
7772  return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
7773 }
7774 
7775 /* ===========================================================================
7776  If the default memLevel or windowBits for deflateInit() is changed, then
7777  this function needs to be updated.
7778  */
7779 uLong compressBound (sourceLen)
7780  uLong sourceLen;
7781 {
7782  return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
7783 }
7784 
7785 /* ===========================================================================
7786  Decompresses the source buffer into the destination buffer. sourceLen is
7787  the byte length of the source buffer. Upon entry, destLen is the total
7788  size of the destination buffer, which must be large enough to hold the
7789  entire uncompressed data. (The size of the uncompressed data must have
7790  been saved previously by the compressor and transmitted to the decompressor
7791  by some mechanism outside the scope of this compression library.)
7792  Upon exit, destLen is the actual size of the compressed buffer.
7793  This function can be used to decompress a whole file at once if the
7794  input file is mmap'ed.
7795 
7796  uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
7797  enough memory, Z_BUF_ERROR if there was not enough room in the output
7798  buffer, or Z_DATA_ERROR if the input data was corrupted.
7799 */
7800 int uncompress (dest, destLen, source, sourceLen)
7801  Bytef *dest;
7802  uLongf *destLen;
7803  const Bytef *source;
7804  uLong sourceLen;
7805 {
7806  z_stream stream;
7807  int err;
7808 
7809  stream.next_in = (Bytef*)source;
7810  stream.avail_in = (uInt)sourceLen;
7811  /* Check for source > 64K on 16-bit machine: */
7812  if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
7813 
7814  stream.next_out = dest;
7815  stream.avail_out = (uInt)*destLen;
7816  if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
7817 
7818  stream.zalloc = (alloc_func)0;
7819  stream.zfree = (free_func)0;
7820 
7821  err = inflateInit(&stream);
7822  if (err != Z_OK) return err;
7823 
7824  err = inflate(&stream, Z_FINISH);
7825  if (err != Z_STREAM_END) {
7826  inflateEnd(&stream);
7827  if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
7828  return Z_DATA_ERROR;
7829  return err;
7830  }
7831  *destLen = stream.total_out;
7832 
7833  err = inflateEnd(&stream);
7834  return err;
7835 }
7836 
7837 #define BASE 65521UL /* largest prime smaller than 65536 */
7838 #define NMAX 5552
7839 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
7840 
7841 #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
7842 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
7843 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
7844 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
7845 #define DO16(buf) DO8(buf,0); DO8(buf,8);
7846 
7847 /* use NO_DIVIDE if your processor does not do division in hardware */
7848 #ifdef NO_DIVIDE
7849 # define MOD(a) \
7850  do { \
7851  if (a >= (BASE << 16)) a -= (BASE << 16); \
7852  if (a >= (BASE << 15)) a -= (BASE << 15); \
7853  if (a >= (BASE << 14)) a -= (BASE << 14); \
7854  if (a >= (BASE << 13)) a -= (BASE << 13); \
7855  if (a >= (BASE << 12)) a -= (BASE << 12); \
7856  if (a >= (BASE << 11)) a -= (BASE << 11); \
7857  if (a >= (BASE << 10)) a -= (BASE << 10); \
7858  if (a >= (BASE << 9)) a -= (BASE << 9); \
7859  if (a >= (BASE << 8)) a -= (BASE << 8); \
7860  if (a >= (BASE << 7)) a -= (BASE << 7); \
7861  if (a >= (BASE << 6)) a -= (BASE << 6); \
7862  if (a >= (BASE << 5)) a -= (BASE << 5); \
7863  if (a >= (BASE << 4)) a -= (BASE << 4); \
7864  if (a >= (BASE << 3)) a -= (BASE << 3); \
7865  if (a >= (BASE << 2)) a -= (BASE << 2); \
7866  if (a >= (BASE << 1)) a -= (BASE << 1); \
7867  if (a >= BASE) a -= BASE; \
7868  } while (0)
7869 # define MOD4(a) \
7870  do { \
7871  if (a >= (BASE << 4)) a -= (BASE << 4); \
7872  if (a >= (BASE << 3)) a -= (BASE << 3); \
7873  if (a >= (BASE << 2)) a -= (BASE << 2); \
7874  if (a >= (BASE << 1)) a -= (BASE << 1); \
7875  if (a >= BASE) a -= BASE; \
7876  } while (0)
7877 #else
7878 # define MOD(a) a %= BASE
7879 # define MOD4(a) a %= BASE
7880 #endif
7881 
7882 /* ========================================================================= */
7883 uLong adler32(adler, buf, len)
7884  uLong adler;
7885  const Bytef *buf;
7886  uInt len;
7887 {
7888  unsigned long sum2;
7889  unsigned n;
7890 
7891  /* split Adler-32 into component sums */
7892  sum2 = (adler >> 16) & 0xffff;
7893  adler &= 0xffff;
7894 
7895  /* in case user likes doing a byte at a time, keep it fast */
7896  if (len == 1) {
7897  adler += buf[0];
7898  if (adler >= BASE)
7899  adler -= BASE;
7900  sum2 += adler;
7901  if (sum2 >= BASE)
7902  sum2 -= BASE;
7903  return adler | (sum2 << 16);
7904  }
7905 
7906  /* initial Adler-32 value (deferred check for len == 1 speed) */
7907  if (buf == Z_NULL)
7908  return 1L;
7909 
7910  /* in case short lengths are provided, keep it somewhat fast */
7911  if (len < 16) {
7912  while (len--) {
7913  adler += *buf++;
7914  sum2 += adler;
7915  }
7916  if (adler >= BASE)
7917  adler -= BASE;
7918  MOD4(sum2); /* only added so many BASE's */
7919  return adler | (sum2 << 16);
7920  }
7921 
7922  /* do length NMAX blocks -- requires just one modulo operation */
7923  while (len >= NMAX) {
7924  len -= NMAX;
7925  n = NMAX / 16; /* NMAX is divisible by 16 */
7926  do {
7927  DO16(buf); /* 16 sums unrolled */
7928  buf += 16;
7929  } while (--n);
7930  MOD(adler);
7931  MOD(sum2);
7932  }
7933 
7934  /* do remaining bytes (less than NMAX, still just one modulo) */
7935  if (len) { /* avoid modulos if none remaining */
7936  while (len >= 16) {
7937  len -= 16;
7938  DO16(buf);
7939  buf += 16;
7940  }
7941  while (len--) {
7942  adler += *buf++;
7943  sum2 += adler;
7944  }
7945  MOD(adler);
7946  MOD(sum2);
7947  }
7948 
7949  /* return recombined sums */
7950  return adler | (sum2 << 16);
7951 }
7952 
7953 /* ========================================================================= */
7954 uLong adler32_combine(adler1, adler2, len2)
7955  uLong adler1;
7956  uLong adler2;
7957  z_off_t len2;
7958 {
7959  unsigned long sum1;
7960  unsigned long sum2;
7961  unsigned rem;
7962 
7963  /* the derivation of this formula is left as an exercise for the reader */
7964  rem = (unsigned)(len2 % BASE);
7965  sum1 = adler1 & 0xffff;
7966  sum2 = rem * sum1;
7967  MOD(sum2);
7968  sum1 += (adler2 & 0xffff) + BASE - 1;
7969  sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
7970  if (sum1 > BASE) sum1 -= BASE;
7971  if (sum1 > BASE) sum1 -= BASE;
7972  if (sum2 > (BASE << 1)) sum2 -= (BASE << 1);
7973  if (sum2 > BASE) sum2 -= BASE;
7974  return sum1 | (sum2 << 16);
7975 }
7976 
7977 /* easy zlib functions
7978  if destination buffer is not large enough, destination length set to required length
7979 */
7980 int ezcompress( unsigned char* pDest, long* pnDestLen, const unsigned char* pSrc, long nSrcLen )
7981 {
7982  z_stream stream;
7983  int err;
7984 
7985  int nExtraChunks;
7986  uInt destlen;
7987 
7988  stream.next_in = (Bytef*)pSrc;
7989  stream.avail_in = (uInt)nSrcLen;
7990 #ifdef MAXSEG_64K
7991  /* Check for source > 64K on 16-bit machine: */
7992  if ((uLong)stream.avail_in != nSrcLen) return Z_BUF_ERROR;
7993 #endif
7994  destlen = (uInt)*pnDestLen;
7995  if ((uLong)destlen != (uLong)*pnDestLen) return Z_BUF_ERROR;
7996 
7997  stream.zalloc = (alloc_func)0;
7998  stream.zfree = (free_func)0;
7999  stream.opaque = (voidpf)0;
8000 
8001  err = deflateInit(&stream, Z_DEFAULT_COMPRESSION);
8002  if (err != Z_OK) return err;
8003 
8004  nExtraChunks = 0;
8005  do {
8006  stream.next_out = pDest;
8007  stream.avail_out = destlen;
8008  err = deflate(&stream, Z_FINISH);
8009  if (err == Z_STREAM_END )
8010  break;
8011  if (err != Z_OK) {
8012  deflateEnd(&stream);
8013  return err;
8014  }
8015  nExtraChunks += 1;
8016  } while (stream.avail_out == 0);
8017 
8018  *pnDestLen = stream.total_out;
8019 
8020  err = deflateEnd(&stream);
8021  if (err != Z_OK) return err;
8022 
8023  return nExtraChunks ? Z_BUF_ERROR : Z_OK;
8024 }
8025 
8026 int ezuncompress( unsigned char* pDest, long* pnDestLen, const unsigned char* pSrc, long nSrcLen )
8027 {
8028  z_stream stream;
8029  int err;
8030 
8031  int nExtraChunks;
8032  uInt destlen;
8033 
8034  stream.next_in = (Bytef*)pSrc;
8035  stream.avail_in = (uInt)nSrcLen;
8036  /* Check for source > 64K on 16-bit machine: */
8037  if ((uLong)stream.avail_in != (uLong)nSrcLen) return Z_BUF_ERROR;
8038 
8039  destlen = (uInt)*pnDestLen;
8040  if ((uLong)destlen != (uLong)*pnDestLen) return Z_BUF_ERROR;
8041 
8042  stream.zalloc = (alloc_func)0;
8043  stream.zfree = (free_func)0;
8044 
8045  err = inflateInit(&stream);
8046  if (err != Z_OK) return err;
8047 
8048  nExtraChunks = 0;
8049  do {
8050  stream.next_out = pDest;
8051  stream.avail_out = destlen;
8052  err = inflate(&stream, Z_FINISH);
8053  if (err == Z_STREAM_END )
8054  break;
8055  if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
8056  err = Z_DATA_ERROR;
8057  if (err != Z_BUF_ERROR) {
8058  inflateEnd(&stream);
8059  return err;
8060  }
8061  nExtraChunks += 1;
8062  } while (stream.avail_out == 0);
8063 
8064  *pnDestLen = stream.total_out;
8065 
8066  err = inflateEnd(&stream);
8067  if (err != Z_OK) return err;
8068 
8069  return nExtraChunks ? Z_BUF_ERROR : Z_OK;
8070 }
8071