]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/pak/unzip.cpp
transfer from internal tree r5311 branches/1.4-gpl
[xonotic/netradiant.git] / libs / pak / unzip.cpp
1 /*****************************************************************************\r
2  * name:                unzip.c\r
3  *\r
4  * desc:                IO on .zip files using portions of zlib \r
5  *\r
6  *\r
7  *****************************************************************************/\r
8 \r
9 #include <stdlib.h>\r
10 #include <stdio.h>\r
11 #include <string.h>\r
12 #include "unzip.h"\r
13 \r
14 typedef unsigned char byte;\r
15 \r
16 /* unzip.h -- IO for uncompress .zip files using zlib \r
17    Version 0.15 beta, Mar 19th, 1998,\r
18 \r
19    Copyright (C) 1998 Gilles Vollant\r
20 \r
21    This unzip package allow extract file from .ZIP file, compatible with PKZip 2.04g\r
22      WinZip, InfoZip tools and compatible.\r
23    Encryption and multi volume ZipFile (span) are not supported.\r
24    Old compressions used by old PKZip 1.x are not supported\r
25 \r
26    THIS IS AN ALPHA VERSION. AT THIS STAGE OF DEVELOPPEMENT, SOMES API OR STRUCTURE\r
27    CAN CHANGE IN FUTURE VERSION !!\r
28    I WAIT FEEDBACK at mail info@winimage.com\r
29    Visit also http://www.winimage.com/zLibDll/unzip.htm for evolution\r
30 \r
31    Condition of use and distribution are the same than zlib :\r
32 \r
33   This software is provided 'as-is', without any express or implied\r
34   warranty.  In no event will the authors be held liable for any damages\r
35   arising from the use of this software.\r
36 \r
37   Permission is granted to anyone to use this software for any purpose,\r
38   including commercial applications, and to alter it and redistribute it\r
39   freely, subject to the following restrictions:\r
40 \r
41   1. The origin of this software must not be misrepresented; you must not\r
42      claim that you wrote the original software. If you use this software\r
43      in a product, an acknowledgment in the product documentation would be\r
44      appreciated but is not required.\r
45   2. Altered source versions must be plainly marked as such, and must not be\r
46      misrepresented as being the original software.\r
47   3. This notice may not be removed or altered from any source distribution.\r
48 \r
49 \r
50 */\r
51 /* for more info about .ZIP format, see \r
52       ftp://ftp.cdrom.com/pub/infozip/doc/appnote-970311-iz.zip\r
53    PkWare has also a specification at :\r
54       ftp://ftp.pkware.com/probdesc.zip */\r
55 \r
56 /* zlib.h -- interface of the 'zlib' general purpose compression library\r
57   version 1.1.3, July 9th, 1998\r
58 \r
59   Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler\r
60 \r
61   This software is provided 'as-is', without any express or implied\r
62   warranty.  In no event will the authors be held liable for any damages\r
63   arising from the use of this software.\r
64 \r
65   Permission is granted to anyone to use this software for any purpose,\r
66   including commercial applications, and to alter it and redistribute it\r
67   freely, subject to the following restrictions:\r
68 \r
69   1. The origin of this software must not be misrepresented; you must not\r
70      claim that you wrote the original software. If you use this software\r
71      in a product, an acknowledgment in the product documentation would be\r
72      appreciated but is not required.\r
73   2. Altered source versions must be plainly marked as such, and must not be\r
74      misrepresented as being the original software.\r
75   3. This notice may not be removed or altered from any source distribution.\r
76 \r
77   Jean-loup Gailly        Mark Adler\r
78   jloup@gzip.org          madler@alumni.caltech.edu\r
79 \r
80 \r
81   The data format used by the zlib library is described by RFCs (Request for\r
82   Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt\r
83   (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).\r
84 */\r
85 \r
86 /* zconf.h -- configuration of the zlib compression library\r
87  * Copyright (C) 1995-1998 Jean-loup Gailly.\r
88  * For conditions of distribution and use, see copyright notice in zlib.h \r
89  */\r
90 \r
91 #ifndef _ZCONF_H\r
92 #define _ZCONF_H\r
93 \r
94 /* Maximum value for memLevel in deflateInit2 */\r
95 #ifndef MAX_MEM_LEVEL\r
96 #  ifdef MAXSEG_64K\r
97 #    define MAX_MEM_LEVEL 8\r
98 #  else\r
99 #    define MAX_MEM_LEVEL 9\r
100 #  endif\r
101 #endif\r
102 \r
103 /* Maximum value for windowBits in deflateInit2 and inflateInit2.\r
104  * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files\r
105  * created by gzip. (Files created by minigzip can still be extracted by\r
106  * gzip.)\r
107  */\r
108 #ifndef MAX_WBITS\r
109 #  define MAX_WBITS   15 /* 32K LZ77 window */\r
110 #endif\r
111 \r
112 /* The memory requirements for deflate are (in bytes):\r
113             (1 << (windowBits+2)) +  (1 << (memLevel+9))\r
114  that is: 128K for windowBits=15  +  128K for memLevel = 8  (default values)\r
115  plus a few kilobytes for small objects. For example, if you want to reduce\r
116  the default memory requirements from 256K to 128K, compile with\r
117      make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"\r
118  Of course this will generally degrade compression (there's no free lunch).\r
119 \r
120    The memory requirements for inflate are (in bytes) 1 << windowBits\r
121  that is, 32K for windowBits=15 (default value) plus a few kilobytes\r
122  for small objects.\r
123 */\r
124 \r
125                         /* Type declarations */\r
126 \r
127 #ifndef OF /* function prototypes */\r
128 #define OF(args)  args\r
129 #endif\r
130 \r
131 typedef unsigned char  Byte;  /* 8 bits */\r
132 typedef unsigned int   uInt;  /* 16 bits or more */\r
133 typedef unsigned long  uLong; /* 32 bits or more */\r
134 typedef Byte    *voidp;\r
135 \r
136 #ifndef SEEK_SET\r
137 #  define SEEK_SET        0       /* Seek from beginning of file.  */\r
138 #  define SEEK_CUR        1       /* Seek from current position.  */\r
139 #  define SEEK_END        2       /* Set file pointer to EOF plus "offset" */\r
140 #endif\r
141 \r
142 #endif /* _ZCONF_H */\r
143 \r
144 #define ZLIB_VERSION "1.1.3"\r
145 \r
146 /* \r
147      The 'zlib' compression library provides in-memory compression and\r
148   decompression functions, including integrity checks of the uncompressed\r
149   data.  This version of the library supports only one compression method\r
150   (deflation) but other algorithms will be added later and will have the same\r
151   stream interface.\r
152 \r
153      Compression can be done in a single step if the buffers are large\r
154   enough (for example if an input file is mmap'ed), or can be done by\r
155   repeated calls of the compression function.  In the latter case, the\r
156   application must provide more input and/or consume the output\r
157   (providing more output space) before each call.\r
158 \r
159      The library also supports reading and writing files in gzip (.gz) format\r
160   with an interface similar to that of stdio.\r
161 \r
162      The library does not install any signal handler. The decoder checks\r
163   the consistency of the compressed data, so the library should never\r
164   crash even in case of corrupted input.\r
165 */\r
166 \r
167 /*\r
168    The application must update next_in and avail_in when avail_in has\r
169    dropped to zero. It must update next_out and avail_out when avail_out\r
170    has dropped to zero. The application must initialize zalloc, zfree and\r
171    opaque before calling the init function. All other fields are set by the\r
172    compression library and must not be updated by the application.\r
173 \r
174    The opaque value provided by the application will be passed as the first\r
175    parameter for calls of zalloc and zfree. This can be useful for custom\r
176    memory management. The compression library attaches no meaning to the\r
177    opaque value.\r
178 \r
179    zalloc must return Z_NULL if there is not enough memory for the object.\r
180    If zlib is used in a multi-threaded application, zalloc and zfree must be\r
181    thread safe.\r
182 \r
183    On 16-bit systems, the functions zalloc and zfree must be able to allocate\r
184    exactly 65536 bytes, but will not be required to allocate more than this\r
185    if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,\r
186    pointers returned by zalloc for objects of exactly 65536 bytes *must*\r
187    have their offset normalized to zero. The default allocation function\r
188    provided by this library ensures this (see zutil.c). To reduce memory\r
189    requirements and avoid any allocation of 64K objects, at the expense of\r
190    compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).\r
191 \r
192    The fields total_in and total_out can be used for statistics or\r
193    progress reports. After compression, total_in holds the total size of\r
194    the uncompressed data and may be saved for use in the decompressor\r
195    (particularly if the decompressor wants to decompress everything in\r
196    a single step).\r
197 */\r
198 \r
199                         /* constants */\r
200 \r
201 #define Z_NO_FLUSH      0\r
202 #define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */\r
203 #define Z_SYNC_FLUSH    2\r
204 #define Z_FULL_FLUSH    3\r
205 #define Z_FINISH        4\r
206 /* Allowed flush values; see deflate() below for details */\r
207 \r
208 #define Z_OK            0\r
209 #define Z_STREAM_END    1\r
210 #define Z_NEED_DICT     2\r
211 #define Z_ERRNO        (-1)\r
212 #define Z_STREAM_ERROR (-2)\r
213 #define Z_DATA_ERROR   (-3)\r
214 #define Z_MEM_ERROR    (-4)\r
215 #define Z_BUF_ERROR    (-5)\r
216 #define Z_VERSION_ERROR (-6)\r
217 /* Return codes for the compression/decompression functions. Negative\r
218  * values are errors, positive values are used for special but normal events.\r
219  */\r
220 \r
221 #define Z_NO_COMPRESSION         0\r
222 #define Z_BEST_SPEED             1\r
223 #define Z_BEST_COMPRESSION       9\r
224 #define Z_DEFAULT_COMPRESSION  (-1)\r
225 /* compression levels */\r
226 \r
227 #define Z_FILTERED            1\r
228 #define Z_HUFFMAN_ONLY        2\r
229 #define Z_DEFAULT_STRATEGY    0\r
230 /* compression strategy; see deflateInit2() below for details */\r
231 \r
232 #define Z_BINARY   0\r
233 #define Z_ASCII    1\r
234 #define Z_UNKNOWN  2\r
235 /* Possible values of the data_type field */\r
236 \r
237 #define Z_DEFLATED   8\r
238 /* The deflate compression method (the only one supported in this version) */\r
239 \r
240 #define Z_NULL  0  /* for initializing zalloc, zfree, opaque */\r
241 \r
242 #define zlib_version zlibVersion()\r
243 /* for compatibility with versions < 1.0.2 */\r
244 \r
245                         /* basic functions */\r
246 \r
247 const char * zlibVersion OF((void));\r
248 /* The application can compare zlibVersion and ZLIB_VERSION for consistency.\r
249    If the first character differs, the library code actually used is\r
250    not compatible with the zlib.h header file used by the application.\r
251    This check is automatically made by deflateInit and inflateInit.\r
252  */\r
253 \r
254 /* \r
255 int deflateInit OF((z_streamp strm, int level));\r
256 \r
257      Initializes the internal stream state for compression. The fields\r
258    zalloc, zfree and opaque must be initialized before by the caller.\r
259    If zalloc and zfree are set to Z_NULL, deflateInit updates them to\r
260    use default allocation functions.\r
261 \r
262      The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:\r
263    1 gives best speed, 9 gives best compression, 0 gives no compression at\r
264    all (the input data is simply copied a block at a time).\r
265    Z_DEFAULT_COMPRESSION requests a default compromise between speed and\r
266    compression (currently equivalent to level 6).\r
267 \r
268      deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not\r
269    enough memory, Z_STREAM_ERROR if level is not a valid compression level,\r
270    Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible\r
271    with the version assumed by the caller (ZLIB_VERSION).\r
272    msg is set to null if there is no error message.  deflateInit does not\r
273    perform any compression: this will be done by deflate().\r
274 */\r
275 \r
276 \r
277 int deflate OF((z_streamp strm, int flush));\r
278 /*\r
279     deflate compresses as much data as possible, and stops when the input\r
280   buffer becomes empty or the output buffer becomes full. It may introduce some\r
281   output latency (reading input without producing any output) except when\r
282   forced to flush.\r
283 \r
284     The detailed semantics are as follows. deflate performs one or both of the\r
285   following actions:\r
286 \r
287   - Compress more input starting at next_in and update next_in and avail_in\r
288     accordingly. If not all input can be processed (because there is not\r
289     enough room in the output buffer), next_in and avail_in are updated and\r
290     processing will resume at this point for the next call of deflate().\r
291 \r
292   - Provide more output starting at next_out and update next_out and avail_out\r
293     accordingly. This action is forced if the parameter flush is non zero.\r
294     Forcing flush frequently degrades the compression ratio, so this parameter\r
295     should be set only when necessary (in interactive applications).\r
296     Some output may be provided even if flush is not set.\r
297 \r
298   Before the call of deflate(), the application should ensure that at least\r
299   one of the actions is possible, by providing more input and/or consuming\r
300   more output, and updating avail_in or avail_out accordingly; avail_out\r
301   should never be zero before the call. The application can consume the\r
302   compressed output when it wants, for example when the output buffer is full\r
303   (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK\r
304   and with zero avail_out, it must be called again after making room in the\r
305   output buffer because there might be more output pending.\r
306 \r
307     If the parameter flush is set to Z_SYNC_FLUSH, all pending output is\r
308   flushed to the output buffer and the output is aligned on a byte boundary, so\r
309   that the decompressor can get all input data available so far. (In particular\r
310   avail_in is zero after the call if enough output space has been provided\r
311   before the call.)  Flushing may degrade compression for some compression\r
312   algorithms and so it should be used only when necessary.\r
313 \r
314     If flush is set to Z_FULL_FLUSH, all output is flushed as with\r
315   Z_SYNC_FLUSH, and the compression state is reset so that decompression can\r
316   restart from this point if previous compressed data has been damaged or if\r
317   random access is desired. Using Z_FULL_FLUSH too often can seriously degrade\r
318   the compression.\r
319 \r
320     If deflate returns with avail_out == 0, this function must be called again\r
321   with the same value of the flush parameter and more output space (updated\r
322   avail_out), until the flush is complete (deflate returns with non-zero\r
323   avail_out).\r
324 \r
325     If the parameter flush is set to Z_FINISH, pending input is processed,\r
326   pending output is flushed and deflate returns with Z_STREAM_END if there\r
327   was enough output space; if deflate returns with Z_OK, this function must be\r
328   called again with Z_FINISH and more output space (updated avail_out) but no\r
329   more input data, until it returns with Z_STREAM_END or an error. After\r
330   deflate has returned Z_STREAM_END, the only possible operations on the\r
331   stream are deflateReset or deflateEnd.\r
332   \r
333     Z_FINISH can be used immediately after deflateInit if all the compression\r
334   is to be done in a single step. In this case, avail_out must be at least\r
335   0.1% larger than avail_in plus 12 bytes.  If deflate does not return\r
336   Z_STREAM_END, then it must be called again as described above.\r
337 \r
338     deflate() sets strm->adler to the adler32 checksum of all input read\r
339   so (that is, total_in bytes).\r
340 \r
341     deflate() may update data_type if it can make a good guess about\r
342   the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered\r
343   binary. This field is only for information purposes and does not affect\r
344   the compression algorithm in any manner.\r
345 \r
346     deflate() returns Z_OK if some progress has been made (more input\r
347   processed or more output produced), Z_STREAM_END if all input has been\r
348   consumed and all output has been produced (only when flush is set to\r
349   Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example\r
350   if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible\r
351   (for example avail_in or avail_out was zero).\r
352 */\r
353 \r
354 \r
355 int deflateEnd OF((z_streamp strm));\r
356 /*\r
357      All dynamically allocated data structures for this stream are freed.\r
358    This function discards any unprocessed input and does not flush any\r
359    pending output.\r
360 \r
361      deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the\r
362    stream state was inconsistent, Z_DATA_ERROR if the stream was freed\r
363    prematurely (some input or output was discarded). In the error case,\r
364    msg may be set but then points to a static string (which must not be\r
365    deallocated).\r
366 */\r
367 \r
368 \r
369 /* \r
370 int inflateInit OF((z_streamp strm));\r
371 \r
372      Initializes the internal stream state for decompression. The fields\r
373    next_in, avail_in, zalloc, zfree and opaque must be initialized before by\r
374    the caller. If next_in is not Z_NULL and avail_in is large enough (the exact\r
375    value depends on the compression method), inflateInit determines the\r
376    compression method from the zlib header and allocates all data structures\r
377    accordingly; otherwise the allocation will be deferred to the first call of\r
378    inflate.  If zalloc and zfree are set to Z_NULL, inflateInit updates them to\r
379    use default allocation functions.\r
380 \r
381      inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough\r
382    memory, Z_VERSION_ERROR if the zlib library version is incompatible with the\r
383    version assumed by the caller.  msg is set to null if there is no error\r
384    message. inflateInit does not perform any decompression apart from reading\r
385    the zlib header if present: this will be done by inflate().  (So next_in and\r
386    avail_in may be modified, but next_out and avail_out are unchanged.)\r
387 */\r
388 \r
389 \r
390 int inflate OF((z_streamp strm, int flush));\r
391 /*\r
392     inflate decompresses as much data as possible, and stops when the input\r
393   buffer becomes empty or the output buffer becomes full. It may some\r
394   introduce some output latency (reading input without producing any output)\r
395   except when forced to flush.\r
396 \r
397   The detailed semantics are as follows. inflate performs one or both of the\r
398   following actions:\r
399 \r
400   - Decompress more input starting at next_in and update next_in and avail_in\r
401     accordingly. If not all input can be processed (because there is not\r
402     enough room in the output buffer), next_in is updated and processing\r
403     will resume at this point for the next call of inflate().\r
404 \r
405   - Provide more output starting at next_out and update next_out and avail_out\r
406     accordingly.  inflate() provides as much output as possible, until there\r
407     is no more input data or no more space in the output buffer (see below\r
408     about the flush parameter).\r
409 \r
410   Before the call of inflate(), the application should ensure that at least\r
411   one of the actions is possible, by providing more input and/or consuming\r
412   more output, and updating the next_* and avail_* values accordingly.\r
413   The application can consume the uncompressed output when it wants, for\r
414   example when the output buffer is full (avail_out == 0), or after each\r
415   call of inflate(). If inflate returns Z_OK and with zero avail_out, it\r
416   must be called again after making room in the output buffer because there\r
417   might be more output pending.\r
418 \r
419     If the parameter flush is set to Z_SYNC_FLUSH, inflate flushes as much\r
420   output as possible to the output buffer. The flushing behavior of inflate is\r
421   not specified for values of the flush parameter other than Z_SYNC_FLUSH\r
422   and Z_FINISH, but the current implementation actually flushes as much output\r
423   as possible anyway.\r
424 \r
425     inflate() should normally be called until it returns Z_STREAM_END or an\r
426   error. However if all decompression is to be performed in a single step\r
427   (a single call of inflate), the parameter flush should be set to\r
428   Z_FINISH. In this case all pending input is processed and all pending\r
429   output is flushed; avail_out must be large enough to hold all the\r
430   uncompressed data. (The size of the uncompressed data may have been saved\r
431   by the compressor for this purpose.) The next operation on this stream must\r
432   be inflateEnd to deallocate the decompression state. The use of Z_FINISH\r
433   is never required, but can be used to inform inflate that a faster routine\r
434   may be used for the single inflate() call.\r
435 \r
436      If a preset dictionary is needed at this point (see inflateSetDictionary\r
437   below), inflate sets strm-adler to the adler32 checksum of the\r
438   dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise \r
439   it sets strm->adler to the adler32 checksum of all output produced\r
440   so (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or\r
441   an error code as described below. At the end of the stream, inflate()\r
442   checks that its computed adler32 checksum is equal to that saved by the\r
443   compressor and returns Z_STREAM_END only if the checksum is correct.\r
444 \r
445     inflate() returns Z_OK if some progress has been made (more input processed\r
446   or more output produced), Z_STREAM_END if the end of the compressed data has\r
447   been reached and all uncompressed output has been produced, Z_NEED_DICT if a\r
448   preset dictionary is needed at this point, Z_DATA_ERROR if the input data was\r
449   corrupted (input stream not conforming to the zlib format or incorrect\r
450   adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent\r
451   (for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not\r
452   enough memory, Z_BUF_ERROR if no progress is possible or if there was not\r
453   enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR\r
454   case, the application may then call inflateSync to look for a good\r
455   compression block.\r
456 */\r
457 \r
458 \r
459 int inflateEnd OF((z_streamp strm));\r
460 /*\r
461      All dynamically allocated data structures for this stream are freed.\r
462    This function discards any unprocessed input and does not flush any\r
463    pending output.\r
464 \r
465      inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state\r
466    was inconsistent. In the error case, msg may be set but then points to a\r
467    static string (which must not be deallocated).\r
468 */\r
469 \r
470                         /* Advanced functions */\r
471 \r
472 /*\r
473     The following functions are needed only in some special applications.\r
474 */\r
475 \r
476 /*   \r
477 int deflateInit2 OF((z_streamp strm,\r
478                                      int  level,\r
479                                      int  method,\r
480                                      int  windowBits,\r
481                                      int  memLevel,\r
482                                      int  strategy));\r
483 \r
484      This is another version of deflateInit with more compression options. The\r
485    fields next_in, zalloc, zfree and opaque must be initialized before by\r
486    the caller.\r
487 \r
488      The method parameter is the compression method. It must be Z_DEFLATED in\r
489    this version of the library.\r
490 \r
491      The windowBits parameter is the base two logarithm of the window size\r
492    (the size of the history buffer).  It should be in the range 8..15 for this\r
493    version of the library. Larger values of this parameter result in better\r
494    compression at the expense of memory usage. The default value is 15 if\r
495    deflateInit is used instead.\r
496 \r
497      The memLevel parameter specifies how much memory should be allocated\r
498    for the internal compression state. memLevel=1 uses minimum memory but\r
499    is slow and reduces compression ratio; memLevel=9 uses maximum memory\r
500    for optimal speed. The default value is 8. See zconf.h for total memory\r
501    usage as a function of windowBits and memLevel.\r
502 \r
503      The strategy parameter is used to tune the compression algorithm. Use the\r
504    value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a\r
505    filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no\r
506    string match).  Filtered data consists mostly of small values with a\r
507    somewhat random distribution. In this case, the compression algorithm is\r
508    tuned to compress them better. The effect of Z_FILTERED is to force more\r
509    Huffman coding and less string matching; it is somewhat intermediate\r
510    between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects\r
511    the compression ratio but not the correctness of the compressed output even\r
512    if it is not set appropriately.\r
513 \r
514       deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough\r
515    memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid\r
516    method). msg is set to null if there is no error message.  deflateInit2 does\r
517    not perform any compression: this will be done by deflate().\r
518 */\r
519                             \r
520 int deflateSetDictionary OF((z_streamp strm,\r
521                                              const Byte *dictionary,\r
522                                              uInt  dictLength));\r
523 /*\r
524      Initializes the compression dictionary from the given byte sequence\r
525    without producing any compressed output. This function must be called\r
526    immediately after deflateInit, deflateInit2 or deflateReset, before any\r
527    call of deflate. The compressor and decompressor must use exactly the same\r
528    dictionary (see inflateSetDictionary).\r
529 \r
530      The dictionary should consist of strings (byte sequences) that are likely\r
531    to be encountered later in the data to be compressed, with the most commonly\r
532    used strings preferably put towards the end of the dictionary. Using a\r
533    dictionary is most useful when the data to be compressed is short and can be\r
534    predicted with good accuracy; the data can then be compressed better than\r
535    with the default empty dictionary.\r
536 \r
537      Depending on the size of the compression data structures selected by\r
538    deflateInit or deflateInit2, a part of the dictionary may in effect be\r
539    discarded, for example if the dictionary is larger than the window size in\r
540    deflate or deflate2. Thus the strings most likely to be useful should be\r
541    put at the end of the dictionary, not at the front.\r
542 \r
543      Upon return of this function, strm->adler is set to the Adler32 value\r
544    of the dictionary; the decompressor may later use this value to determine\r
545    which dictionary has been used by the compressor. (The Adler32 value\r
546    applies to the whole dictionary even if only a subset of the dictionary is\r
547    actually used by the compressor.)\r
548 \r
549      deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a\r
550    parameter is invalid (such as NULL dictionary) or the stream state is\r
551    inconsistent (for example if deflate has already been called for this stream\r
552    or if the compression method is bsort). deflateSetDictionary does not\r
553    perform any compression: this will be done by deflate().\r
554 */\r
555 \r
556 int deflateCopy OF((z_streamp dest,\r
557                                     z_streamp source));\r
558 /*\r
559      Sets the destination stream as a complete copy of the source stream.\r
560 \r
561      This function can be useful when several compression strategies will be\r
562    tried, for example when there are several ways of pre-processing the input\r
563    data with a filter. The streams that will be discarded should then be freed\r
564    by calling deflateEnd.  Note that deflateCopy duplicates the internal\r
565    compression state which can be quite large, so this strategy is slow and\r
566    can consume lots of memory.\r
567 \r
568      deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not\r
569    enough memory, Z_STREAM_ERROR if the source stream state was inconsistent\r
570    (such as zalloc being NULL). msg is left unchanged in both source and\r
571    destination.\r
572 */\r
573 \r
574 int deflateReset OF((z_streamp strm));\r
575 /*\r
576      This function is equivalent to deflateEnd followed by deflateInit,\r
577    but does not free and reallocate all the internal compression state.\r
578    The stream will keep the same compression level and any other attributes\r
579    that may have been set by deflateInit2.\r
580 \r
581       deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source\r
582    stream state was inconsistent (such as zalloc or state being NULL).\r
583 */\r
584 \r
585 int deflateParams OF((z_streamp strm,\r
586                                       int level,\r
587                                       int strategy));\r
588 /*\r
589      Dynamically update the compression level and compression strategy.  The\r
590    interpretation of level and strategy is as in deflateInit2.  This can be\r
591    used to switch between compression and straight copy of the input data, or\r
592    to switch to a different kind of input data requiring a different\r
593    strategy. If the compression level is changed, the input available so far\r
594    is compressed with the old level (and may be flushed); the new level will\r
595    take effect only at the next call of deflate().\r
596 \r
597      Before the call of deflateParams, the stream state must be set as for\r
598    a call of deflate(), since the currently available input may have to\r
599    be compressed and flushed. In particular, strm->avail_out must be non-zero.\r
600 \r
601      deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source\r
602    stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR\r
603    if strm->avail_out was zero.\r
604 */\r
605 \r
606 /*   \r
607 int inflateInit2 OF((z_streamp strm,\r
608                                      int  windowBits));\r
609 \r
610      This is another version of inflateInit with an extra parameter. The\r
611    fields next_in, avail_in, zalloc, zfree and opaque must be initialized\r
612    before by the caller.\r
613 \r
614      The windowBits parameter is the base two logarithm of the maximum window\r
615    size (the size of the history buffer).  It should be in the range 8..15 for\r
616    this version of the library. The default value is 15 if inflateInit is used\r
617    instead. If a compressed stream with a larger window size is given as\r
618    input, inflate() will return with the error code Z_DATA_ERROR instead of\r
619    trying to allocate a larger window.\r
620 \r
621       inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough\r
622    memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative\r
623    memLevel). msg is set to null if there is no error message.  inflateInit2\r
624    does not perform any decompression apart from reading the zlib header if\r
625    present: this will be done by inflate(). (So next_in and avail_in may be\r
626    modified, but next_out and avail_out are unchanged.)\r
627 */\r
628 \r
629 int inflateSetDictionary OF((z_streamp strm,\r
630                                              const Byte *dictionary,\r
631                                              uInt  dictLength));\r
632 /*\r
633      Initializes the decompression dictionary from the given uncompressed byte\r
634    sequence. This function must be called immediately after a call of inflate\r
635    if this call returned Z_NEED_DICT. The dictionary chosen by the compressor\r
636    can be determined from the Adler32 value returned by this call of\r
637    inflate. The compressor and decompressor must use exactly the same\r
638    dictionary (see deflateSetDictionary).\r
639 \r
640      inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a\r
641    parameter is invalid (such as NULL dictionary) or the stream state is\r
642    inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the\r
643    expected one (incorrect Adler32 value). inflateSetDictionary does not\r
644    perform any decompression: this will be done by subsequent calls of\r
645    inflate().\r
646 */\r
647 \r
648 int inflateSync OF((z_streamp strm));\r
649 /* \r
650     Skips invalid compressed data until a full flush point (see above the\r
651   description of deflate with Z_FULL_FLUSH) can be found, or until all\r
652   available input is skipped. No output is provided.\r
653 \r
654     inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR\r
655   if no more input was provided, Z_DATA_ERROR if no flush point has been found,\r
656   or Z_STREAM_ERROR if the stream structure was inconsistent. In the success\r
657   case, the application may save the current current value of total_in which\r
658   indicates where valid compressed data was found. In the error case, the\r
659   application may repeatedly call inflateSync, providing more input each time,\r
660   until success or end of the input data.\r
661 */\r
662 \r
663 int inflateReset OF((z_streamp strm));\r
664 /*\r
665      This function is equivalent to inflateEnd followed by inflateInit,\r
666    but does not free and reallocate all the internal decompression state.\r
667    The stream will keep attributes that may have been set by inflateInit2.\r
668 \r
669       inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source\r
670    stream state was inconsistent (such as zalloc or state being NULL).\r
671 */\r
672 \r
673 \r
674                         /* utility functions */\r
675 \r
676 /*\r
677      The following utility functions are implemented on top of the\r
678    basic stream-oriented functions. To simplify the interface, some\r
679    default options are assumed (compression level and memory usage,\r
680    standard memory allocation functions). The source code of these\r
681    utility functions can easily be modified if you need special options.\r
682 */\r
683 \r
684 int compress OF((Byte *dest,   uLong *destLen,\r
685                                  const Byte *source, uLong sourceLen));\r
686 /*\r
687      Compresses the source buffer into the destination buffer.  sourceLen is\r
688    the byte length of the source buffer. Upon entry, destLen is the total\r
689    size of the destination buffer, which must be at least 0.1% larger than\r
690    sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the\r
691    compressed buffer.\r
692      This function can be used to compress a whole file at once if the\r
693    input file is mmap'ed.\r
694      compress returns Z_OK if success, Z_MEM_ERROR if there was not\r
695    enough memory, Z_BUF_ERROR if there was not enough room in the output\r
696    buffer.\r
697 */\r
698 \r
699 int compress2 OF((Byte *dest,   uLong *destLen,\r
700                                   const Byte *source, uLong sourceLen,\r
701                                   int level));\r
702 /*\r
703      Compresses the source buffer into the destination buffer. The level\r
704    parameter has the same meaning as in deflateInit.  sourceLen is the byte\r
705    length of the source buffer. Upon entry, destLen is the total size of the\r
706    destination buffer, which must be at least 0.1% larger than sourceLen plus\r
707    12 bytes. Upon exit, destLen is the actual size of the compressed buffer.\r
708 \r
709      compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough\r
710    memory, Z_BUF_ERROR if there was not enough room in the output buffer,\r
711    Z_STREAM_ERROR if the level parameter is invalid.\r
712 */\r
713 \r
714 int uncompress OF((Byte *dest,   uLong *destLen,\r
715                                    const Byte *source, uLong sourceLen));\r
716 /*\r
717      Decompresses the source buffer into the destination buffer.  sourceLen is\r
718    the byte length of the source buffer. Upon entry, destLen is the total\r
719    size of the destination buffer, which must be large enough to hold the\r
720    entire uncompressed data. (The size of the uncompressed data must have\r
721    been saved previously by the compressor and transmitted to the decompressor\r
722    by some mechanism outside the scope of this compression library.)\r
723    Upon exit, destLen is the actual size of the compressed buffer.\r
724      This function can be used to decompress a whole file at once if the\r
725    input file is mmap'ed.\r
726 \r
727      uncompress returns Z_OK if success, Z_MEM_ERROR if there was not\r
728    enough memory, Z_BUF_ERROR if there was not enough room in the output\r
729    buffer, or Z_DATA_ERROR if the input data was corrupted.\r
730 */\r
731 \r
732 \r
733 typedef voidp gzFile;\r
734 \r
735 gzFile gzopen  OF((const char *path, const char *mode));\r
736 /*\r
737      Opens a gzip (.gz) file for reading or writing. The mode parameter\r
738    is as in fopen ("rb" or "wb") but can also include a compression level\r
739    ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for\r
740    Huffman only compression as in "wb1h". (See the description\r
741    of deflateInit2 for more information about the strategy parameter.)\r
742 \r
743      gzopen can be used to read a file which is not in gzip format; in this\r
744    case gzread will directly read from the file without decompression.\r
745 \r
746      gzopen returns NULL if the file could not be opened or if there was\r
747    insufficient memory to allocate the (de)compression state; errno\r
748    can be checked to distinguish the two cases (if errno is zero, the\r
749    zlib error is Z_MEM_ERROR).  */\r
750 \r
751 gzFile gzdopen  OF((int fd, const char *mode));\r
752 /*\r
753      gzdopen() associates a gzFile with the file descriptor fd.  File\r
754    descriptors are obtained from calls like open, dup, creat, pipe or\r
755    fileno (in the file has been previously opened with fopen).\r
756    The mode parameter is as in gzopen.\r
757      The next call of gzclose on the returned gzFile will also close the\r
758    file descriptor fd, just like fclose(fdopen(fd), mode) closes the file\r
759    descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).\r
760      gzdopen returns NULL if there was insufficient memory to allocate\r
761    the (de)compression state.\r
762 */\r
763 \r
764 int gzsetparams OF((gzFile file, int level, int strategy));\r
765 /*\r
766      Dynamically update the compression level or strategy. See the description\r
767    of deflateInit2 for the meaning of these parameters.\r
768      gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not\r
769    opened for writing.\r
770 */\r
771 \r
772 int    gzread  OF((gzFile file, voidp buf, unsigned len));\r
773 /*\r
774      Reads the given number of uncompressed bytes from the compressed file.\r
775    If the input file was not in gzip format, gzread copies the given number\r
776    of bytes into the buffer.\r
777      gzread returns the number of uncompressed bytes actually read (0 for\r
778    end of file, -1 for error). */\r
779 \r
780 int    gzwrite OF((gzFile file, \r
781                                    const voidp buf, unsigned len));\r
782 /*\r
783      Writes the given number of uncompressed bytes into the compressed file.\r
784    gzwrite returns the number of uncompressed bytes actually written\r
785    (0 in case of error).\r
786 */\r
787 \r
788 int    gzprintf OF((gzFile file, const char *format, ...));\r
789 /*\r
790      Converts, formats, and writes the args to the compressed file under\r
791    control of the format string, as in fprintf. gzprintf returns the number of\r
792    uncompressed bytes actually written (0 in case of error).\r
793 */\r
794 \r
795 int gzputs OF((gzFile file, const char *s));\r
796 /*\r
797       Writes the given null-terminated string to the compressed file, excluding\r
798    the terminating null character.\r
799       gzputs returns the number of characters written, or -1 in case of error.\r
800 */\r
801 \r
802 char * gzgets OF((gzFile file, char *buf, int len));\r
803 /*\r
804       Reads bytes from the compressed file until len-1 characters are read, or\r
805    a newline character is read and transferred to buf, or an end-of-file\r
806    condition is encountered.  The string is then terminated with a null\r
807    character.\r
808       gzgets returns buf, or Z_NULL in case of error.\r
809 */\r
810 \r
811 int    gzputc OF((gzFile file, int c));\r
812 /*\r
813       Writes c, converted to an unsigned char, into the compressed file.\r
814    gzputc returns the value that was written, or -1 in case of error.\r
815 */\r
816 \r
817 int    gzgetc OF((gzFile file));\r
818 /*\r
819       Reads one byte from the compressed file. gzgetc returns this byte\r
820    or -1 in case of end of file or error.\r
821 */\r
822 \r
823 int    gzflush OF((gzFile file, int flush));\r
824 /*\r
825      Flushes all pending output into the compressed file. The parameter\r
826    flush is as in the deflate() function. The return value is the zlib\r
827    error number (see function gzerror below). gzflush returns Z_OK if\r
828    the flush parameter is Z_FINISH and all output could be flushed.\r
829      gzflush should be called only when strictly necessary because it can\r
830    degrade compression.\r
831 */\r
832 \r
833 long gzseek OF((gzFile file,\r
834                                       long offset, int whence));\r
835 /* \r
836       Sets the starting position for the next gzread or gzwrite on the\r
837    given compressed file. The offset represents a number of bytes in the\r
838    uncompressed data stream. The whence parameter is defined as in lseek(2);\r
839    the value SEEK_END is not supported.\r
840      If the file is opened for reading, this function is emulated but can be\r
841    extremely slow. If the file is opened for writing, only forward seeks are\r
842    supported; gzseek then compresses a sequence of zeroes up to the new\r
843    starting position.\r
844 \r
845       gzseek returns the resulting offset location as measured in bytes from\r
846    the beginning of the uncompressed stream, or -1 in case of error, in\r
847    particular if the file is opened for writing and the new starting position\r
848    would be before the current position.\r
849 */\r
850 \r
851 int    gzrewind OF((gzFile file));\r
852 /*\r
853      Rewinds the given file. This function is supported only for reading.\r
854 \r
855    gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)\r
856 */\r
857 \r
858 long    gztell OF((gzFile file));\r
859 /*\r
860      Returns the starting position for the next gzread or gzwrite on the\r
861    given compressed file. This position represents a number of bytes in the\r
862    uncompressed data stream.\r
863 \r
864    gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)\r
865 */\r
866 \r
867 int gzeof OF((gzFile file));\r
868 /*\r
869      Returns 1 when EOF has previously been detected reading the given\r
870    input stream, otherwise zero.\r
871 */\r
872 \r
873 int    gzclose OF((gzFile file));\r
874 /*\r
875      Flushes all pending output if necessary, closes the compressed file\r
876    and deallocates all the (de)compression state. The return value is the zlib\r
877    error number (see function gzerror below).\r
878 */\r
879 \r
880 const char * gzerror OF((gzFile file, int *errnum));\r
881 /*\r
882      Returns the error message for the last error which occurred on the\r
883    given compressed file. errnum is set to zlib error number. If an\r
884    error occurred in the file system and not in the compression library,\r
885    errnum is set to Z_ERRNO and the application may consult errno\r
886    to get the exact error code.\r
887 */\r
888 \r
889                         /* checksum functions */\r
890 \r
891 /*\r
892      These functions are not related to compression but are exported\r
893    anyway because they might be useful in applications using the\r
894    compression library.\r
895 */\r
896 \r
897 uLong adler32 OF((uLong adler, const Byte *buf, uInt len));\r
898 \r
899 /*\r
900      Update a running Adler-32 checksum with the bytes buf[0..len-1] and\r
901    return the updated checksum. If buf is NULL, this function returns\r
902    the required initial value for the checksum.\r
903    An Adler-32 checksum is almost as reliable as a CRC32 but can be computed\r
904    much faster. Usage example:\r
905 \r
906      uLong adler = adler32(0L, Z_NULL, 0);\r
907 \r
908      while (read_buffer(buffer, length) != EOF) {\r
909        adler = adler32(adler, buffer, length);\r
910      }\r
911      if (adler != original_adler) error();\r
912 */\r
913 \r
914 uLong crc32   OF((uLong crc, const Byte *buf, uInt len));\r
915 /*\r
916      Update a running crc with the bytes buf[0..len-1] and return the updated\r
917    crc. If buf is NULL, this function returns the required initial value\r
918    for the crc. Pre- and post-conditioning (one's complement) is performed\r
919    within this function so it shouldn't be done by the application.\r
920    Usage example:\r
921 \r
922      uLong crc = crc32(0L, Z_NULL, 0);\r
923 \r
924      while (read_buffer(buffer, length) != EOF) {\r
925        crc = crc32(crc, buffer, length);\r
926      }\r
927      if (crc != original_crc) error();\r
928 */\r
929 \r
930 // private stuff to not include cmdlib.h\r
931 /*\r
932 ============================================================================\r
933 \r
934                                         BYTE ORDER FUNCTIONS\r
935 \r
936 ============================================================================\r
937 */\r
938 \r
939 #ifdef _SGI_SOURCE\r
940 #define __BIG_ENDIAN__\r
941 #endif\r
942 \r
943 #ifdef __BIG_ENDIAN__\r
944 \r
945 short   __LittleShort (short l)\r
946 {\r
947         byte    b1,b2;\r
948 \r
949         b1 = l&255;\r
950         b2 = (l>>8)&255;\r
951 \r
952         return (b1<<8) + b2;\r
953 }\r
954 \r
955 short   __BigShort (short l)\r
956 {\r
957         return l;\r
958 }\r
959 \r
960 \r
961 int    __LittleLong (int l)\r
962 {\r
963         byte    b1,b2,b3,b4;\r
964 \r
965         b1 = l&255;\r
966         b2 = (l>>8)&255;\r
967         b3 = (l>>16)&255;\r
968         b4 = (l>>24)&255;\r
969 \r
970         return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;\r
971 }\r
972 \r
973 int    __BigLong (int l)\r
974 {\r
975         return l;\r
976 }\r
977 \r
978 \r
979 float   __LittleFloat (float l)\r
980 {\r
981         union {byte b[4]; float f;} in, out;\r
982         \r
983         in.f = l;\r
984         out.b[0] = in.b[3];\r
985         out.b[1] = in.b[2];\r
986         out.b[2] = in.b[1];\r
987         out.b[3] = in.b[0];\r
988         \r
989         return out.f;\r
990 }\r
991 \r
992 float   __BigFloat (float l)\r
993 {\r
994         return l;\r
995 }\r
996 \r
997 \r
998 #else\r
999 \r
1000 \r
1001 short   __BigShort (short l)\r
1002 {\r
1003         byte    b1,b2;\r
1004 \r
1005         b1 = l&255;\r
1006         b2 = (l>>8)&255;\r
1007 \r
1008         return (b1<<8) + b2;\r
1009 }\r
1010 \r
1011 short   __LittleShort (short l)\r
1012 {\r
1013         return l;\r
1014 }\r
1015 \r
1016 \r
1017 int    __BigLong (int l)\r
1018 {\r
1019         byte    b1,b2,b3,b4;\r
1020 \r
1021         b1 = l&255;\r
1022         b2 = (l>>8)&255;\r
1023         b3 = (l>>16)&255;\r
1024         b4 = (l>>24)&255;\r
1025 \r
1026         return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;\r
1027 }\r
1028 \r
1029 int    __LittleLong (int l)\r
1030 {\r
1031         return l;\r
1032 }\r
1033 \r
1034 float   __BigFloat (float l)\r
1035 {\r
1036         union {byte b[4]; float f;} in, out;\r
1037         \r
1038         in.f = l;\r
1039         out.b[0] = in.b[3];\r
1040         out.b[1] = in.b[2];\r
1041         out.b[2] = in.b[1];\r
1042         out.b[3] = in.b[0];\r
1043         \r
1044         return out.f;\r
1045 }\r
1046 \r
1047 float   __LittleFloat (float l)\r
1048 {\r
1049         return l;\r
1050 }\r
1051 \r
1052 \r
1053 \r
1054 #endif\r
1055 \r
1056 \r
1057 \r
1058 \r
1059                         /* various hacks, don't look :) */\r
1060 \r
1061 /* deflateInit and inflateInit are macros to allow checking the zlib version\r
1062  * and the compiler's view of z_stream:\r
1063  */\r
1064 int deflateInit_ OF((z_streamp strm, int level,\r
1065                                      const char *version, int stream_size));\r
1066 int inflateInit_ OF((z_streamp strm,\r
1067                                      const char *version, int stream_size));\r
1068 int deflateInit2_ OF((z_streamp strm, int  level, int  method,\r
1069                                       int windowBits, int memLevel,\r
1070                                       int strategy, const char *version,\r
1071                                       int stream_size));\r
1072 int inflateInit2_ OF((z_streamp strm, int  windowBits,\r
1073                                       const char *version, int stream_size));\r
1074 #define deflateInit(strm, level) \\r
1075         deflateInit_((strm), (level),       ZLIB_VERSION, sizeof(z_stream))\r
1076 #define inflateInit(strm) \\r
1077         inflateInit_((strm),                ZLIB_VERSION, sizeof(z_stream))\r
1078 #define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \\r
1079         deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\\r
1080                       (strategy),           ZLIB_VERSION, sizeof(z_stream))\r
1081 #define inflateInit2(strm, windowBits) \\r
1082         inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))\r
1083 \r
1084 \r
1085 const char   * zError           OF((int err));\r
1086 int            inflateSyncPoint OF((z_streamp z));\r
1087 const uLong * get_crc_table    OF((void));\r
1088 \r
1089 typedef unsigned char  uch;\r
1090 typedef unsigned short ush;\r
1091 typedef unsigned long  ulg;\r
1092 \r
1093 extern const char *z_errmsg[10]; /* indexed by 2-zlib_error */\r
1094 /* (size given to avoid silly warnings with Visual C++) */\r
1095 \r
1096 #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]\r
1097 \r
1098 #define ERR_RETURN(strm,err) \\r
1099   return (strm->msg = (char*)ERR_MSG(err), (err))\r
1100 /* To be used only when the state is known to be valid */\r
1101 \r
1102         /* common constants */\r
1103 \r
1104 #ifndef DEF_WBITS\r
1105 #  define DEF_WBITS MAX_WBITS\r
1106 #endif\r
1107 /* default windowBits for decompression. MAX_WBITS is for compression only */\r
1108 \r
1109 #if MAX_MEM_LEVEL >= 8\r
1110 #  define DEF_MEM_LEVEL 8\r
1111 #else\r
1112 #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL\r
1113 #endif\r
1114 /* default memLevel */\r
1115 \r
1116 #define STORED_BLOCK 0\r
1117 #define STATIC_TREES 1\r
1118 #define DYN_TREES    2\r
1119 /* The three kinds of block type */\r
1120 \r
1121 #define MIN_MATCH  3\r
1122 #define MAX_MATCH  258\r
1123 /* The minimum and maximum match lengths */\r
1124 \r
1125 #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */\r
1126 \r
1127         /* target dependencies */\r
1128 \r
1129         /* Common defaults */\r
1130 \r
1131 #ifndef OS_CODE\r
1132 #  define OS_CODE  0x03  /* assume Unix */\r
1133 #endif\r
1134 \r
1135 #ifndef F_OPEN\r
1136 #  define F_OPEN(name, mode) fopen((name), (mode))\r
1137 #endif\r
1138 \r
1139          /* functions */\r
1140 \r
1141 #ifdef HAVE_STRERROR\r
1142    extern char *strerror OF((int));\r
1143 #  define zstrerror(errnum) strerror(errnum)\r
1144 #else\r
1145 #  define zstrerror(errnum) ""\r
1146 #endif\r
1147 \r
1148 #define zmemcpy memcpy\r
1149 #define zmemcmp memcmp\r
1150 #define zmemzero(dest, len) memset(dest, 0, len)\r
1151 \r
1152 /* Diagnostic functions */\r
1153 #ifdef _ZIP_DEBUG_\r
1154    int z_verbose = 0;\r
1155 #  define Assert(cond,msg) assert(cond);\r
1156    //{if(!(cond)) Sys_Error(msg);}\r
1157 #  define Trace(x) {if (z_verbose>=0) Sys_Error x ;}\r
1158 #  define Tracev(x) {if (z_verbose>0) Sys_Error x ;}\r
1159 #  define Tracevv(x) {if (z_verbose>1) Sys_Error x ;}\r
1160 #  define Tracec(c,x) {if (z_verbose>0 && (c)) Sys_Error x ;}\r
1161 #  define Tracecv(c,x) {if (z_verbose>1 && (c)) Sys_Error x ;}\r
1162 #else\r
1163 #  define Assert(cond,msg)\r
1164 #  define Trace(x)\r
1165 #  define Tracev(x)\r
1166 #  define Tracevv(x)\r
1167 #  define Tracec(c,x)\r
1168 #  define Tracecv(c,x)\r
1169 #endif\r
1170 \r
1171 \r
1172 typedef uLong (*check_func) OF((uLong check, const Byte *buf, uInt len));\r
1173 voidp zcalloc OF((voidp opaque, unsigned items, unsigned size));\r
1174 void   zcfree  OF((voidp opaque, voidp ptr));\r
1175 \r
1176 #define ZALLOC(strm, items, size) \\r
1177            (*((strm)->zalloc))((strm)->opaque, (items), (size))\r
1178 #define ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidp)(addr))\r
1179 #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}\r
1180 \r
1181 \r
1182 #if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES) && \\r
1183                       !defined(CASESENSITIVITYDEFAULT_NO)\r
1184 #define CASESENSITIVITYDEFAULT_NO\r
1185 #endif\r
1186 \r
1187 \r
1188 #ifndef UNZ_BUFSIZE\r
1189 #define UNZ_BUFSIZE (65536)\r
1190 #endif\r
1191 \r
1192 #ifndef UNZ_MAXFILENAMEINZIP\r
1193 #define UNZ_MAXFILENAMEINZIP (256)\r
1194 #endif\r
1195 \r
1196 #ifndef ALLOC\r
1197 # define ALLOC(size) (malloc(size))\r
1198 #endif\r
1199 #ifndef TRYFREE\r
1200 # define TRYFREE(p) {if (p) free(p);}\r
1201 #endif\r
1202 \r
1203 #define SIZECENTRALDIRITEM (0x2e)\r
1204 #define SIZEZIPLOCALHEADER (0x1e)\r
1205 \r
1206 \r
1207 \r
1208 /* ===========================================================================\r
1209      Read a byte from a gz_stream; update next_in and avail_in. Return EOF\r
1210    for end of file.\r
1211    IN assertion: the stream s has been sucessfully opened for reading.\r
1212 */\r
1213 \r
1214 /*\r
1215 static int unzlocal_getByte(FILE *fin,int *pi)\r
1216 {\r
1217     unsigned char c;\r
1218         int err = fread(&c, 1, 1, fin);\r
1219     if (err==1)\r
1220     {\r
1221         *pi = (int)c;\r
1222         return UNZ_OK;\r
1223     }\r
1224     else\r
1225     {\r
1226         if (ferror(fin)) \r
1227             return UNZ_ERRNO;\r
1228         else\r
1229             return UNZ_EOF;\r
1230     }\r
1231 }\r
1232 */\r
1233 \r
1234 /* ===========================================================================\r
1235    Reads a long in LSB order from the given gz_stream. Sets \r
1236 */\r
1237 static int unzlocal_getShort (FILE* fin, uLong *pX)\r
1238 {\r
1239         short   v;\r
1240 \r
1241         fread( &v, sizeof(v), 1, fin );\r
1242 \r
1243         *pX = __LittleShort( v);\r
1244         return UNZ_OK;\r
1245 \r
1246 /*\r
1247     uLong x ;\r
1248     int i;\r
1249     int err;\r
1250 \r
1251     err = unzlocal_getByte(fin,&i);\r
1252     x = (uLong)i;\r
1253     \r
1254     if (err==UNZ_OK)\r
1255         err = unzlocal_getByte(fin,&i);\r
1256     x += ((uLong)i)<<8;\r
1257    \r
1258     if (err==UNZ_OK)\r
1259         *pX = x;\r
1260     else\r
1261         *pX = 0;\r
1262     return err;\r
1263 */\r
1264 }\r
1265 \r
1266 static int unzlocal_getLong (FILE *fin, uLong *pX)\r
1267 {\r
1268         int             v;\r
1269 \r
1270         fread( &v, sizeof(v), 1, fin );\r
1271 \r
1272         *pX = __LittleLong( v);\r
1273         return UNZ_OK;\r
1274 \r
1275 /*\r
1276     uLong x ;\r
1277     int i;\r
1278     int err;\r
1279 \r
1280     err = unzlocal_getByte(fin,&i);\r
1281     x = (uLong)i;\r
1282     \r
1283     if (err==UNZ_OK)\r
1284         err = unzlocal_getByte(fin,&i);\r
1285     x += ((uLong)i)<<8;\r
1286 \r
1287     if (err==UNZ_OK)\r
1288         err = unzlocal_getByte(fin,&i);\r
1289     x += ((uLong)i)<<16;\r
1290 \r
1291     if (err==UNZ_OK)\r
1292         err = unzlocal_getByte(fin,&i);\r
1293     x += ((uLong)i)<<24;\r
1294    \r
1295     if (err==UNZ_OK)\r
1296         *pX = x;\r
1297     else\r
1298         *pX = 0;\r
1299     return err;\r
1300 */\r
1301 }\r
1302 \r
1303 \r
1304 /* My own strcmpi / strcasecmp */\r
1305 static int strcmpcasenosensitive_internal (const char* fileName1,const char* fileName2)\r
1306 {\r
1307         for (;;)\r
1308         {\r
1309                 char c1=*(fileName1++);\r
1310                 char c2=*(fileName2++);\r
1311                 if ((c1>='a') && (c1<='z'))\r
1312                         c1 -= 0x20;\r
1313                 if ((c2>='a') && (c2<='z'))\r
1314                         c2 -= 0x20;\r
1315                 if (c1=='\0')\r
1316                         return ((c2=='\0') ? 0 : -1);\r
1317                 if (c2=='\0')\r
1318                         return 1;\r
1319                 if (c1<c2)\r
1320                         return -1;\r
1321                 if (c1>c2)\r
1322                         return 1;\r
1323         }\r
1324 }\r
1325 \r
1326 \r
1327 #ifdef  CASESENSITIVITYDEFAULT_NO\r
1328 #define CASESENSITIVITYDEFAULTVALUE 2\r
1329 #else\r
1330 #define CASESENSITIVITYDEFAULTVALUE 1\r
1331 #endif\r
1332 \r
1333 #ifndef STRCMPCASENOSENTIVEFUNCTION\r
1334 #define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal\r
1335 #endif\r
1336 \r
1337 /* \r
1338    Compare two filename (fileName1,fileName2).\r
1339    If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp)\r
1340    If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi\r
1341                                                                 or strcasecmp)\r
1342    If iCaseSenisivity = 0, case sensitivity is defaut of your operating system\r
1343         (like 1 on Unix, 2 on Windows)\r
1344 \r
1345 */\r
1346 extern int unzStringFileNameCompare (const char* fileName1,const char* fileName2,int iCaseSensitivity)\r
1347 {\r
1348         if (iCaseSensitivity==0)\r
1349                 iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE;\r
1350 \r
1351         if (iCaseSensitivity==1)\r
1352                 return strcmp(fileName1,fileName2);\r
1353 \r
1354         return STRCMPCASENOSENTIVEFUNCTION(fileName1,fileName2);\r
1355\r
1356 \r
1357 #define BUFREADCOMMENT (0x400)\r
1358 \r
1359 /*\r
1360   Locate the Central directory of a zipfile (at the end, just before\r
1361     the global comment)\r
1362 */\r
1363 static uLong unzlocal_SearchCentralDir(FILE *fin)\r
1364 {\r
1365         unsigned char* buf;\r
1366         uLong uSizeFile;\r
1367         uLong uBackRead;\r
1368         uLong uMaxBack=0xffff; /* maximum size of global comment */\r
1369         uLong uPosFound=0;\r
1370         \r
1371         if (fseek(fin,0,SEEK_END) != 0)\r
1372                 return 0;\r
1373 \r
1374 \r
1375         uSizeFile = ftell( fin );\r
1376         \r
1377         if (uMaxBack>uSizeFile)\r
1378                 uMaxBack = uSizeFile;\r
1379 \r
1380         buf = (unsigned char*)malloc(BUFREADCOMMENT+4);\r
1381         if (buf==NULL)\r
1382                 return 0;\r
1383 \r
1384         uBackRead = 4;\r
1385         while (uBackRead<uMaxBack)\r
1386         {\r
1387                 uLong uReadSize,uReadPos ;\r
1388                 int i;\r
1389                 if (uBackRead+BUFREADCOMMENT>uMaxBack) \r
1390                         uBackRead = uMaxBack;\r
1391                 else\r
1392                         uBackRead+=BUFREADCOMMENT;\r
1393                 uReadPos = uSizeFile-uBackRead ;\r
1394                 \r
1395                 uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? \r
1396                      (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);\r
1397                 if (fseek(fin,uReadPos,SEEK_SET)!=0)\r
1398                         break;\r
1399 \r
1400                 if (fread(buf,(uInt)uReadSize,1,fin)!=1)\r
1401                         break;\r
1402 \r
1403                 for (i=(int)uReadSize-3; (i--)>0;)\r
1404                         if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && \r
1405                                 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))\r
1406                         {\r
1407                                 uPosFound = uReadPos+i;\r
1408                                 break;\r
1409                         }\r
1410 \r
1411                 if (uPosFound!=0)\r
1412                         break;\r
1413         }\r
1414         free(buf);\r
1415         return uPosFound;\r
1416 }\r
1417 \r
1418 extern unzFile unzReOpen (const char* path, unzFile file)\r
1419 {\r
1420         unz_s *s;\r
1421         FILE * fin;\r
1422 \r
1423     fin=fopen(path,"rb");\r
1424         if (fin==NULL)\r
1425                 return NULL;\r
1426 \r
1427         s=(unz_s*)malloc(sizeof(unz_s));\r
1428         memcpy(s, (unz_s*)file, sizeof(unz_s));\r
1429 \r
1430         s->file = fin;\r
1431         return (unzFile)s;      \r
1432 }\r
1433 \r
1434 /*\r
1435   Open a Zip file. path contain the full pathname (by example,\r
1436      on a Windows NT computer "c:\\test\\zlib109.zip" or on an Unix computer\r
1437          "zlib/zlib109.zip".\r
1438          If the zipfile cannot be opened (file don't exist or in not valid), the\r
1439            return value is NULL.\r
1440      Else, the return value is a unzFile Handle, usable with other function\r
1441            of this unzip package.\r
1442 */\r
1443 extern unzFile unzOpen (const char* path)\r
1444 {\r
1445         unz_s us;\r
1446         unz_s *s;\r
1447         uLong central_pos,uL;\r
1448         FILE * fin ;\r
1449 \r
1450         uLong number_disk;          /* number of the current dist, used for \r
1451                                                                    spaning ZIP, unsupported, always 0*/\r
1452         uLong number_disk_with_CD;  /* number the the disk with central dir, used\r
1453                                                                    for spaning ZIP, unsupported, always 0*/\r
1454         uLong number_entry_CD;      /* total number of entries in\r
1455                                        the central dir \r
1456                                        (same than number_entry on nospan) */\r
1457 \r
1458         int err=UNZ_OK;\r
1459 \r
1460     fin=fopen(path,"rb");\r
1461         if (fin==NULL)\r
1462                 return NULL;\r
1463 \r
1464         central_pos = unzlocal_SearchCentralDir(fin);\r
1465         if (central_pos==0)\r
1466                 err=UNZ_ERRNO;\r
1467 \r
1468         if (fseek(fin,central_pos,SEEK_SET)!=0)\r
1469                 err=UNZ_ERRNO;\r
1470 \r
1471         /* the signature, already checked */\r
1472         if (unzlocal_getLong(fin,&uL)!=UNZ_OK)\r
1473                 err=UNZ_ERRNO;\r
1474 \r
1475         /* number of this disk */\r
1476         if (unzlocal_getShort(fin,&number_disk)!=UNZ_OK)\r
1477                 err=UNZ_ERRNO;\r
1478 \r
1479         /* number of the disk with the start of the central directory */\r
1480         if (unzlocal_getShort(fin,&number_disk_with_CD)!=UNZ_OK)\r
1481                 err=UNZ_ERRNO;\r
1482 \r
1483         /* total number of entries in the central dir on this disk */\r
1484         if (unzlocal_getShort(fin,&us.gi.number_entry)!=UNZ_OK)\r
1485                 err=UNZ_ERRNO;\r
1486 \r
1487         /* total number of entries in the central dir */\r
1488         if (unzlocal_getShort(fin,&number_entry_CD)!=UNZ_OK)\r
1489                 err=UNZ_ERRNO;\r
1490 \r
1491         if ((number_entry_CD!=us.gi.number_entry) ||\r
1492                 (number_disk_with_CD!=0) ||\r
1493                 (number_disk!=0))\r
1494                 err=UNZ_BADZIPFILE;\r
1495 \r
1496         /* size of the central directory */\r
1497         if (unzlocal_getLong(fin,&us.size_central_dir)!=UNZ_OK)\r
1498                 err=UNZ_ERRNO;\r
1499 \r
1500         /* offset of start of central directory with respect to the \r
1501               starting disk number */\r
1502         if (unzlocal_getLong(fin,&us.offset_central_dir)!=UNZ_OK)\r
1503                 err=UNZ_ERRNO;\r
1504 \r
1505         /* zipfile comment length */\r
1506         if (unzlocal_getShort(fin,&us.gi.size_comment)!=UNZ_OK)\r
1507                 err=UNZ_ERRNO;\r
1508 \r
1509         if ((central_pos<us.offset_central_dir+us.size_central_dir) && \r
1510                 (err==UNZ_OK))\r
1511                 err=UNZ_BADZIPFILE;\r
1512 \r
1513         if (err!=UNZ_OK)\r
1514         {\r
1515                 fclose(fin);\r
1516                 return NULL;\r
1517         }\r
1518 \r
1519         us.file=fin;\r
1520         us.byte_before_the_zipfile = central_pos -\r
1521                                     (us.offset_central_dir+us.size_central_dir);\r
1522         us.central_pos = central_pos;\r
1523     us.pfile_in_zip_read = NULL;\r
1524         \r
1525 \r
1526         s=(unz_s*)malloc(sizeof(unz_s));\r
1527         *s=us;\r
1528 //      unzGoToFirstFile((unzFile)s);   \r
1529         return (unzFile)s;      \r
1530 }\r
1531 \r
1532 \r
1533 /*\r
1534   Close a ZipFile opened with unzipOpen.\r
1535   If there is files inside the .Zip opened with unzipOpenCurrentFile (see later),\r
1536     these files MUST be closed with unzipCloseCurrentFile before call unzipClose.\r
1537   return UNZ_OK if there is no problem. */\r
1538 extern int unzClose (unzFile file)\r
1539 {\r
1540         unz_s* s;\r
1541         if (file==NULL)\r
1542                 return UNZ_PARAMERROR;\r
1543         s=(unz_s*)file;\r
1544 \r
1545     if (s->pfile_in_zip_read!=NULL)\r
1546         unzCloseCurrentFile(file);\r
1547 \r
1548         fclose(s->file);\r
1549         free(s);\r
1550         return UNZ_OK;\r
1551 }\r
1552 \r
1553 \r
1554 /*\r
1555   Write info about the ZipFile in the *pglobal_info structure.\r
1556   No preparation of the structure is needed\r
1557   return UNZ_OK if there is no problem. */\r
1558 extern int unzGetGlobalInfo (unzFile file,unz_global_info *pglobal_info)\r
1559 {\r
1560         unz_s* s;\r
1561         if (file==NULL)\r
1562                 return UNZ_PARAMERROR;\r
1563         s=(unz_s*)file;\r
1564         *pglobal_info=s->gi;\r
1565         return UNZ_OK;\r
1566 }\r
1567 \r
1568 \r
1569 /*\r
1570    Translate date/time from Dos format to tm_unz (readable more easilty)\r
1571 */\r
1572 static void unzlocal_DosDateToTmuDate (uLong ulDosDate, tm_unz* ptm)\r
1573 {\r
1574     uLong uDate;\r
1575     uDate = (uLong)(ulDosDate>>16);\r
1576     ptm->tm_mday = (uInt)(uDate&0x1f) ;\r
1577     ptm->tm_mon =  (uInt)((((uDate)&0x1E0)/0x20)-1) ;\r
1578     ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ;\r
1579 \r
1580     ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800);\r
1581     ptm->tm_min =  (uInt) ((ulDosDate&0x7E0)/0x20) ;\r
1582     ptm->tm_sec =  (uInt) (2*(ulDosDate&0x1f)) ;\r
1583 }\r
1584 \r
1585 /*\r
1586   Get Info about the current file in the zipfile, with internal only info\r
1587 */\r
1588 static int unzlocal_GetCurrentFileInfoInternal (unzFile file,\r
1589                                                   unz_file_info *pfile_info,\r
1590                                                   unz_file_info_internal \r
1591                                                   *pfile_info_internal,\r
1592                                                   char *szFileName,\r
1593                                                                                                   uLong fileNameBufferSize,\r
1594                                                   void *extraField,\r
1595                                                                                                   uLong extraFieldBufferSize,\r
1596                                                   char *szComment,\r
1597                                                                                                   uLong commentBufferSize)\r
1598 {\r
1599         unz_s* s;\r
1600         unz_file_info file_info;\r
1601         unz_file_info_internal file_info_internal;\r
1602         int err=UNZ_OK;\r
1603         uLong uMagic;\r
1604         long lSeek=0;\r
1605 \r
1606         if (file==NULL)\r
1607                 return UNZ_PARAMERROR;\r
1608         s=(unz_s*)file;\r
1609         if (fseek(s->file,s->pos_in_central_dir+s->byte_before_the_zipfile,SEEK_SET)!=0)\r
1610                 err=UNZ_ERRNO;\r
1611 \r
1612 \r
1613         /* we check the magic */\r
1614         if (err==UNZ_OK)\r
1615                 if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)\r
1616                         err=UNZ_ERRNO;\r
1617                 else if (uMagic!=0x02014b50)\r
1618                         err=UNZ_BADZIPFILE;\r
1619 \r
1620         if (unzlocal_getShort(s->file,&file_info.version) != UNZ_OK)\r
1621                 err=UNZ_ERRNO;\r
1622 \r
1623         if (unzlocal_getShort(s->file,&file_info.version_needed) != UNZ_OK)\r
1624                 err=UNZ_ERRNO;\r
1625 \r
1626         if (unzlocal_getShort(s->file,&file_info.flag) != UNZ_OK)\r
1627                 err=UNZ_ERRNO;\r
1628 \r
1629         if (unzlocal_getShort(s->file,&file_info.compression_method) != UNZ_OK)\r
1630                 err=UNZ_ERRNO;\r
1631 \r
1632         if (unzlocal_getLong(s->file,&file_info.dosDate) != UNZ_OK)\r
1633                 err=UNZ_ERRNO;\r
1634 \r
1635     unzlocal_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date);\r
1636 \r
1637         if (unzlocal_getLong(s->file,&file_info.crc) != UNZ_OK)\r
1638                 err=UNZ_ERRNO;\r
1639 \r
1640         if (unzlocal_getLong(s->file,&file_info.compressed_size) != UNZ_OK)\r
1641                 err=UNZ_ERRNO;\r
1642 \r
1643         if (unzlocal_getLong(s->file,&file_info.uncompressed_size) != UNZ_OK)\r
1644                 err=UNZ_ERRNO;\r
1645 \r
1646         if (unzlocal_getShort(s->file,&file_info.size_filename) != UNZ_OK)\r
1647                 err=UNZ_ERRNO;\r
1648 \r
1649         if (unzlocal_getShort(s->file,&file_info.size_file_extra) != UNZ_OK)\r
1650                 err=UNZ_ERRNO;\r
1651 \r
1652         if (unzlocal_getShort(s->file,&file_info.size_file_comment) != UNZ_OK)\r
1653                 err=UNZ_ERRNO;\r
1654 \r
1655         if (unzlocal_getShort(s->file,&file_info.disk_num_start) != UNZ_OK)\r
1656                 err=UNZ_ERRNO;\r
1657 \r
1658         if (unzlocal_getShort(s->file,&file_info.internal_fa) != UNZ_OK)\r
1659                 err=UNZ_ERRNO;\r
1660 \r
1661         if (unzlocal_getLong(s->file,&file_info.external_fa) != UNZ_OK)\r
1662                 err=UNZ_ERRNO;\r
1663 \r
1664         if (unzlocal_getLong(s->file,&file_info_internal.offset_curfile) != UNZ_OK)\r
1665                 err=UNZ_ERRNO;\r
1666 \r
1667         lSeek+=file_info.size_filename;\r
1668         if ((err==UNZ_OK) && (szFileName!=NULL))\r
1669         {\r
1670                 uLong uSizeRead ;\r
1671                 if (file_info.size_filename<fileNameBufferSize)\r
1672                 {\r
1673                         *(szFileName+file_info.size_filename)='\0';\r
1674                         uSizeRead = file_info.size_filename;\r
1675                 }\r
1676                 else\r
1677                         uSizeRead = fileNameBufferSize;\r
1678 \r
1679                 if ((file_info.size_filename>0) && (fileNameBufferSize>0))\r
1680                         if (fread(szFileName,(uInt)uSizeRead,1,s->file)!=1)\r
1681                                 err=UNZ_ERRNO;\r
1682                 lSeek -= uSizeRead;\r
1683         }\r
1684 \r
1685         \r
1686         if ((err==UNZ_OK) && (extraField!=NULL))\r
1687         {\r
1688                 uLong uSizeRead ;\r
1689                 if (file_info.size_file_extra<extraFieldBufferSize)\r
1690                         uSizeRead = file_info.size_file_extra;\r
1691                 else\r
1692                         uSizeRead = extraFieldBufferSize;\r
1693 \r
1694                 if (lSeek!=0)\r
1695                         if (fseek(s->file,lSeek,SEEK_CUR)==0)\r
1696                                 lSeek=0;\r
1697                         else\r
1698                                 err=UNZ_ERRNO;\r
1699                 if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0))\r
1700                         if (fread(extraField,(uInt)uSizeRead,1,s->file)!=1)\r
1701                                 err=UNZ_ERRNO;\r
1702                 lSeek += file_info.size_file_extra - uSizeRead;\r
1703         }\r
1704         else\r
1705                 lSeek+=file_info.size_file_extra; \r
1706 \r
1707         \r
1708         if ((err==UNZ_OK) && (szComment!=NULL))\r
1709         {\r
1710                 uLong uSizeRead ;\r
1711                 if (file_info.size_file_comment<commentBufferSize)\r
1712                 {\r
1713                         *(szComment+file_info.size_file_comment)='\0';\r
1714                         uSizeRead = file_info.size_file_comment;\r
1715                 }\r
1716                 else\r
1717                         uSizeRead = commentBufferSize;\r
1718 \r
1719                 if (lSeek!=0)\r
1720                         if (fseek(s->file,lSeek,SEEK_CUR)==0)\r
1721                                 lSeek=0;\r
1722                         else\r
1723                                 err=UNZ_ERRNO;\r
1724                 if ((file_info.size_file_comment>0) && (commentBufferSize>0))\r
1725                         if (fread(szComment,(uInt)uSizeRead,1,s->file)!=1)\r
1726                                 err=UNZ_ERRNO;\r
1727                 lSeek+=file_info.size_file_comment - uSizeRead;\r
1728         }\r
1729         else\r
1730                 lSeek+=file_info.size_file_comment;\r
1731 \r
1732         if ((err==UNZ_OK) && (pfile_info!=NULL))\r
1733                 *pfile_info=file_info;\r
1734 \r
1735         if ((err==UNZ_OK) && (pfile_info_internal!=NULL))\r
1736                 *pfile_info_internal=file_info_internal;\r
1737 \r
1738         return err;\r
1739 }\r
1740 \r
1741 \r
1742 \r
1743 /*\r
1744   Write info about the ZipFile in the *pglobal_info structure.\r
1745   No preparation of the structure is needed\r
1746   return UNZ_OK if there is no problem.\r
1747 */\r
1748 extern int unzGetCurrentFileInfo (      unzFile file, unz_file_info *pfile_info,\r
1749                                                                         char *szFileName, uLong fileNameBufferSize,\r
1750                                                                         void *extraField, uLong extraFieldBufferSize,\r
1751                                                                         char *szComment, uLong commentBufferSize)\r
1752 {\r
1753         return unzlocal_GetCurrentFileInfoInternal(file,pfile_info,NULL,\r
1754                                                                                                 szFileName,fileNameBufferSize,\r
1755                                                                                                 extraField,extraFieldBufferSize,\r
1756                                                                                                 szComment,commentBufferSize);\r
1757 }\r
1758 \r
1759 /*\r
1760   Set the current file of the zipfile to the first file.\r
1761   return UNZ_OK if there is no problem\r
1762 */\r
1763 extern int unzGoToFirstFile (unzFile file)\r
1764 {\r
1765         int err=UNZ_OK;\r
1766         unz_s* s;\r
1767         if (file==NULL)\r
1768                 return UNZ_PARAMERROR;\r
1769         s=(unz_s*)file;\r
1770         s->pos_in_central_dir=s->offset_central_dir;\r
1771         s->num_file=0;\r
1772         err=unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,\r
1773                                                                                          &s->cur_file_info_internal,\r
1774                                                                                          NULL,0,NULL,0,NULL,0);\r
1775         s->current_file_ok = (err == UNZ_OK);\r
1776         return err;\r
1777 }\r
1778 \r
1779 \r
1780 /*\r
1781   Set the current file of the zipfile to the next file.\r
1782   return UNZ_OK if there is no problem\r
1783   return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.\r
1784 */\r
1785 extern int unzGoToNextFile (unzFile file)\r
1786 {\r
1787         unz_s* s;       \r
1788         int err;\r
1789 \r
1790         if (file==NULL)\r
1791                 return UNZ_PARAMERROR;\r
1792         s=(unz_s*)file;\r
1793         if (!s->current_file_ok)\r
1794                 return UNZ_END_OF_LIST_OF_FILE;\r
1795         if (s->num_file+1==s->gi.number_entry)\r
1796                 return UNZ_END_OF_LIST_OF_FILE;\r
1797 \r
1798         s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename +\r
1799                         s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ;\r
1800         s->num_file++;\r
1801         err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,\r
1802                                                                                            &s->cur_file_info_internal,\r
1803                                                                                            NULL,0,NULL,0,NULL,0);\r
1804         s->current_file_ok = (err == UNZ_OK);\r
1805         return err;\r
1806 }\r
1807 \r
1808 \r
1809 /*\r
1810   Try locate the file szFileName in the zipfile.\r
1811   For the iCaseSensitivity signification, see unzipStringFileNameCompare\r
1812 \r
1813   return value :\r
1814   UNZ_OK if the file is found. It becomes the current file.\r
1815   UNZ_END_OF_LIST_OF_FILE if the file is not found\r
1816 */\r
1817 extern int unzLocateFile (unzFile file, const char *szFileName, int iCaseSensitivity)\r
1818 {\r
1819         unz_s* s;       \r
1820         int err;\r
1821 \r
1822         \r
1823         uLong num_fileSaved;\r
1824         uLong pos_in_central_dirSaved;\r
1825 \r
1826 \r
1827         if (file==NULL)\r
1828                 return UNZ_PARAMERROR;\r
1829 \r
1830     if (strlen(szFileName)>=UNZ_MAXFILENAMEINZIP)\r
1831         return UNZ_PARAMERROR;\r
1832 \r
1833         s=(unz_s*)file;\r
1834         if (!s->current_file_ok)\r
1835                 return UNZ_END_OF_LIST_OF_FILE;\r
1836 \r
1837         num_fileSaved = s->num_file;\r
1838         pos_in_central_dirSaved = s->pos_in_central_dir;\r
1839 \r
1840         err = unzGoToFirstFile(file);\r
1841 \r
1842         while (err == UNZ_OK)\r
1843         {\r
1844                 char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1];\r
1845                 unzGetCurrentFileInfo(file,NULL,\r
1846                                                                 szCurrentFileName,sizeof(szCurrentFileName)-1,\r
1847                                                                 NULL,0,NULL,0);\r
1848                 if (unzStringFileNameCompare(szCurrentFileName,\r
1849                                                                                 szFileName,iCaseSensitivity)==0)\r
1850                         return UNZ_OK;\r
1851                 err = unzGoToNextFile(file);\r
1852         }\r
1853 \r
1854         s->num_file = num_fileSaved ;\r
1855         s->pos_in_central_dir = pos_in_central_dirSaved ;\r
1856         return err;\r
1857 }\r
1858 \r
1859 \r
1860 /*\r
1861   Read the static header of the current zipfile\r
1862   Check the coherency of the static header and info in the end of central\r
1863         directory about this file\r
1864   store in *piSizeVar the size of extra info in static header\r
1865         (filename and size of extra field data)\r
1866 */\r
1867 static int unzlocal_CheckCurrentFileCoherencyHeader (unz_s* s, uInt* piSizeVar,\r
1868                                                                                                         uLong *poffset_local_extrafield,\r
1869                                                                                                         uInt *psize_local_extrafield)\r
1870 {\r
1871         uLong uMagic,uData,uFlags;\r
1872         uLong size_filename;\r
1873         uLong size_extra_field;\r
1874         int err=UNZ_OK;\r
1875 \r
1876         *piSizeVar = 0;\r
1877         *poffset_local_extrafield = 0;\r
1878         *psize_local_extrafield = 0;\r
1879 \r
1880         if (fseek(s->file,s->cur_file_info_internal.offset_curfile +\r
1881                                                                 s->byte_before_the_zipfile,SEEK_SET)!=0)\r
1882                 return UNZ_ERRNO;\r
1883 \r
1884 \r
1885         if (err==UNZ_OK)\r
1886                 if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)\r
1887                         err=UNZ_ERRNO;\r
1888                 else if (uMagic!=0x04034b50)\r
1889                         err=UNZ_BADZIPFILE;\r
1890 \r
1891         if (unzlocal_getShort(s->file,&uData) != UNZ_OK)\r
1892                 err=UNZ_ERRNO;\r
1893 /*\r
1894         else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion))\r
1895                 err=UNZ_BADZIPFILE;\r
1896 */\r
1897         if (unzlocal_getShort(s->file,&uFlags) != UNZ_OK)\r
1898                 err=UNZ_ERRNO;\r
1899 \r
1900         if (unzlocal_getShort(s->file,&uData) != UNZ_OK)\r
1901                 err=UNZ_ERRNO;\r
1902         else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method))\r
1903                 err=UNZ_BADZIPFILE;\r
1904 \r
1905     if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) &&\r
1906                          (s->cur_file_info.compression_method!=Z_DEFLATED))\r
1907         err=UNZ_BADZIPFILE;\r
1908 \r
1909         if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* date/time */\r
1910                 err=UNZ_ERRNO;\r
1911 \r
1912         if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* crc */\r
1913                 err=UNZ_ERRNO;\r
1914         else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) &&\r
1915                                       ((uFlags & 8)==0))\r
1916                 err=UNZ_BADZIPFILE;\r
1917 \r
1918         if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* size compr */\r
1919                 err=UNZ_ERRNO;\r
1920         else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) &&\r
1921                                                           ((uFlags & 8)==0))\r
1922                 err=UNZ_BADZIPFILE;\r
1923 \r
1924         if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* size uncompr */\r
1925                 err=UNZ_ERRNO;\r
1926         else if ((err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) && \r
1927                                                           ((uFlags & 8)==0))\r
1928                 err=UNZ_BADZIPFILE;\r
1929 \r
1930 \r
1931         if (unzlocal_getShort(s->file,&size_filename) != UNZ_OK)\r
1932                 err=UNZ_ERRNO;\r
1933         else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename))\r
1934                 err=UNZ_BADZIPFILE;\r
1935 \r
1936         *piSizeVar += (uInt)size_filename;\r
1937 \r
1938         if (unzlocal_getShort(s->file,&size_extra_field) != UNZ_OK)\r
1939                 err=UNZ_ERRNO;\r
1940         *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile +\r
1941                                                                         SIZEZIPLOCALHEADER + size_filename;\r
1942         *psize_local_extrafield = (uInt)size_extra_field;\r
1943 \r
1944         *piSizeVar += (uInt)size_extra_field;\r
1945 \r
1946         return err;\r
1947 }\r
1948                                                                                                 \r
1949 /*\r
1950   Open for reading data the current file in the zipfile.\r
1951   If there is no error and the file is opened, the return value is UNZ_OK.\r
1952 */\r
1953 extern int unzOpenCurrentFile (unzFile file)\r
1954 {\r
1955         int err=UNZ_OK;\r
1956         int Store;\r
1957         uInt iSizeVar;\r
1958         unz_s* s;\r
1959         file_in_zip_read_info_s* pfile_in_zip_read_info;\r
1960         uLong offset_local_extrafield;  /* offset of the static extra field */\r
1961         uInt  size_local_extrafield;    /* size of the static extra field */\r
1962 \r
1963         if (file==NULL)\r
1964                 return UNZ_PARAMERROR;\r
1965         s=(unz_s*)file;\r
1966         if (!s->current_file_ok)\r
1967                 return UNZ_PARAMERROR;\r
1968 \r
1969     if (s->pfile_in_zip_read != NULL)\r
1970         unzCloseCurrentFile(file);\r
1971 \r
1972         if (unzlocal_CheckCurrentFileCoherencyHeader(s,&iSizeVar,\r
1973                                 &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK)\r
1974                 return UNZ_BADZIPFILE;\r
1975 \r
1976         pfile_in_zip_read_info = (file_in_zip_read_info_s*)\r
1977                                                                             malloc(sizeof(file_in_zip_read_info_s));\r
1978         if (pfile_in_zip_read_info==NULL)\r
1979                 return UNZ_INTERNALERROR;\r
1980 \r
1981         pfile_in_zip_read_info->read_buffer=(char*)malloc(UNZ_BUFSIZE);\r
1982         pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;\r
1983         pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;\r
1984         pfile_in_zip_read_info->pos_local_extrafield=0;\r
1985 \r
1986         if (pfile_in_zip_read_info->read_buffer==NULL)\r
1987         {\r
1988                 free(pfile_in_zip_read_info);\r
1989                 return UNZ_INTERNALERROR;\r
1990         }\r
1991 \r
1992         pfile_in_zip_read_info->stream_initialised=0;\r
1993         \r
1994         if ((s->cur_file_info.compression_method!=0) &&\r
1995         (s->cur_file_info.compression_method!=Z_DEFLATED))\r
1996                 err=UNZ_BADZIPFILE;\r
1997         Store = s->cur_file_info.compression_method==0;\r
1998 \r
1999         pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc;\r
2000         pfile_in_zip_read_info->crc32=0;\r
2001         pfile_in_zip_read_info->compression_method =\r
2002             s->cur_file_info.compression_method;\r
2003         pfile_in_zip_read_info->file=s->file;\r
2004         pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile;\r
2005 \r
2006     pfile_in_zip_read_info->stream.total_out = 0;\r
2007 \r
2008         if (!Store)\r
2009         {\r
2010           pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;\r
2011           pfile_in_zip_read_info->stream.zfree = (free_func)0;\r
2012           pfile_in_zip_read_info->stream.opaque = (voidp)0; \r
2013       \r
2014           err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS);\r
2015           if (err == Z_OK)\r
2016             pfile_in_zip_read_info->stream_initialised=1;\r
2017         /* windowBits is passed < 0 to tell that there is no zlib header.\r
2018          * Note that in this case inflate *requires* an extra "dummy" byte\r
2019          * after the compressed stream in order to complete decompression and\r
2020          * return Z_STREAM_END. \r
2021          * In unzip, i don't wait absolutely Z_STREAM_END because I known the \r
2022          * size of both compressed and uncompressed data\r
2023          */\r
2024         }\r
2025         pfile_in_zip_read_info->rest_read_compressed = \r
2026             s->cur_file_info.compressed_size ;\r
2027         pfile_in_zip_read_info->rest_read_uncompressed = \r
2028             s->cur_file_info.uncompressed_size ;\r
2029 \r
2030         \r
2031         pfile_in_zip_read_info->pos_in_zipfile = \r
2032             s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + \r
2033                           iSizeVar;\r
2034         \r
2035         pfile_in_zip_read_info->stream.avail_in = (uInt)0;\r
2036 \r
2037 \r
2038         s->pfile_in_zip_read = pfile_in_zip_read_info;\r
2039     return UNZ_OK;\r
2040 }\r
2041 \r
2042 \r
2043 /*\r
2044   Read bytes from the current file.\r
2045   buf contain buffer where data must be copied\r
2046   len the size of buf.\r
2047 \r
2048   return the number of byte copied if somes bytes are copied\r
2049   return 0 if the end of file was reached\r
2050   return <0 with error code if there is an error\r
2051     (UNZ_ERRNO for IO error, or zLib error for uncompress error)\r
2052 */\r
2053 extern int unzReadCurrentFile  (unzFile file, void *buf, unsigned len)\r
2054 {\r
2055         int err=UNZ_OK;\r
2056         uInt iRead = 0;\r
2057         unz_s* s;\r
2058         file_in_zip_read_info_s* pfile_in_zip_read_info;\r
2059         if (file==NULL)\r
2060                 return UNZ_PARAMERROR;\r
2061         s=(unz_s*)file;\r
2062     pfile_in_zip_read_info=s->pfile_in_zip_read;\r
2063 \r
2064         if (pfile_in_zip_read_info==NULL)\r
2065                 return UNZ_PARAMERROR;\r
2066 \r
2067 \r
2068         if ((pfile_in_zip_read_info->read_buffer == NULL))\r
2069                 return UNZ_END_OF_LIST_OF_FILE;\r
2070         if (len==0)\r
2071                 return 0;\r
2072 \r
2073         pfile_in_zip_read_info->stream.next_out = (Byte*)buf;\r
2074 \r
2075         pfile_in_zip_read_info->stream.avail_out = (uInt)len;\r
2076         \r
2077         if (len>pfile_in_zip_read_info->rest_read_uncompressed)\r
2078                 pfile_in_zip_read_info->stream.avail_out = \r
2079                   (uInt)pfile_in_zip_read_info->rest_read_uncompressed;\r
2080 \r
2081         while (pfile_in_zip_read_info->stream.avail_out>0)\r
2082         {\r
2083                 if ((pfile_in_zip_read_info->stream.avail_in==0) &&\r
2084             (pfile_in_zip_read_info->rest_read_compressed>0))\r
2085                 {\r
2086                         uInt uReadThis = UNZ_BUFSIZE;\r
2087                         if (pfile_in_zip_read_info->rest_read_compressed<uReadThis)\r
2088                                 uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed;\r
2089                         if (uReadThis == 0)\r
2090                                 return UNZ_EOF;\r
2091                         if (s->cur_file_info.compressed_size == pfile_in_zip_read_info->rest_read_compressed)\r
2092                                 if (fseek(pfile_in_zip_read_info->file,\r
2093                                                   pfile_in_zip_read_info->pos_in_zipfile + \r
2094                                                          pfile_in_zip_read_info->byte_before_the_zipfile,SEEK_SET)!=0)\r
2095                                         return UNZ_ERRNO;\r
2096                         if (fread(pfile_in_zip_read_info->read_buffer,uReadThis,1,\r
2097                          pfile_in_zip_read_info->file)!=1)\r
2098                                 return UNZ_ERRNO;\r
2099                         pfile_in_zip_read_info->pos_in_zipfile += uReadThis;\r
2100 \r
2101                         pfile_in_zip_read_info->rest_read_compressed-=uReadThis;\r
2102                         \r
2103                         pfile_in_zip_read_info->stream.next_in = \r
2104                 (Byte*)pfile_in_zip_read_info->read_buffer;\r
2105                         pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis;\r
2106                 }\r
2107 \r
2108                 if (pfile_in_zip_read_info->compression_method==0)\r
2109                 {\r
2110                         uInt uDoCopy,i ;\r
2111                         if (pfile_in_zip_read_info->stream.avail_out < \r
2112                             pfile_in_zip_read_info->stream.avail_in)\r
2113                                 uDoCopy = pfile_in_zip_read_info->stream.avail_out ;\r
2114                         else\r
2115                                 uDoCopy = pfile_in_zip_read_info->stream.avail_in ;\r
2116                                 \r
2117                         for (i=0;i<uDoCopy;i++)\r
2118                                 *(pfile_in_zip_read_info->stream.next_out+i) =\r
2119                         *(pfile_in_zip_read_info->stream.next_in+i);\r
2120                                         \r
2121                         pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,\r
2122                                                                 pfile_in_zip_read_info->stream.next_out,\r
2123                                                                 uDoCopy);\r
2124                         pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy;\r
2125                         pfile_in_zip_read_info->stream.avail_in -= uDoCopy;\r
2126                         pfile_in_zip_read_info->stream.avail_out -= uDoCopy;\r
2127                         pfile_in_zip_read_info->stream.next_out += uDoCopy;\r
2128                         pfile_in_zip_read_info->stream.next_in += uDoCopy;\r
2129             pfile_in_zip_read_info->stream.total_out += uDoCopy;\r
2130                         iRead += uDoCopy;\r
2131                 }\r
2132                 else\r
2133                 {\r
2134                         uLong uTotalOutBefore,uTotalOutAfter;\r
2135                         const Byte *bufBefore;\r
2136                         uLong uOutThis;\r
2137                         int flush=Z_SYNC_FLUSH;\r
2138 \r
2139                         uTotalOutBefore = pfile_in_zip_read_info->stream.total_out;\r
2140                         bufBefore = pfile_in_zip_read_info->stream.next_out;\r
2141 \r
2142                         /*\r
2143                         if ((pfile_in_zip_read_info->rest_read_uncompressed ==\r
2144                                  pfile_in_zip_read_info->stream.avail_out) &&\r
2145                                 (pfile_in_zip_read_info->rest_read_compressed == 0))\r
2146                                 flush = Z_FINISH;\r
2147                         */\r
2148                         err=inflate(&pfile_in_zip_read_info->stream,flush);\r
2149 \r
2150                         uTotalOutAfter = pfile_in_zip_read_info->stream.total_out;\r
2151                         uOutThis = uTotalOutAfter-uTotalOutBefore;\r
2152                         \r
2153                         pfile_in_zip_read_info->crc32 = \r
2154                 crc32(pfile_in_zip_read_info->crc32,bufBefore,\r
2155                         (uInt)(uOutThis));\r
2156 \r
2157                         pfile_in_zip_read_info->rest_read_uncompressed -=\r
2158                 uOutThis;\r
2159 \r
2160                         iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);\r
2161             \r
2162                         if (err==Z_STREAM_END)\r
2163                                 return (iRead==0) ? UNZ_EOF : iRead;\r
2164                         if (err!=Z_OK) \r
2165                                 break;\r
2166                 }\r
2167         }\r
2168 \r
2169         if (err==Z_OK)\r
2170                 return iRead;\r
2171         return err;\r
2172 }\r
2173 \r
2174 \r
2175 /*\r
2176   Give the current position in uncompressed data\r
2177 */\r
2178 extern long unztell (unzFile file)\r
2179 {\r
2180         unz_s* s;\r
2181         file_in_zip_read_info_s* pfile_in_zip_read_info;\r
2182         if (file==NULL)\r
2183                 return UNZ_PARAMERROR;\r
2184         s=(unz_s*)file;\r
2185     pfile_in_zip_read_info=s->pfile_in_zip_read;\r
2186 \r
2187         if (pfile_in_zip_read_info==NULL)\r
2188                 return UNZ_PARAMERROR;\r
2189 \r
2190         return (long)pfile_in_zip_read_info->stream.total_out;\r
2191 }\r
2192 \r
2193 \r
2194 /*\r
2195   return 1 if the end of file was reached, 0 elsewhere \r
2196 */\r
2197 extern int unzeof (unzFile file)\r
2198 {\r
2199         unz_s* s;\r
2200         file_in_zip_read_info_s* pfile_in_zip_read_info;\r
2201         if (file==NULL)\r
2202                 return UNZ_PARAMERROR;\r
2203         s=(unz_s*)file;\r
2204     pfile_in_zip_read_info=s->pfile_in_zip_read;\r
2205 \r
2206         if (pfile_in_zip_read_info==NULL)\r
2207                 return UNZ_PARAMERROR;\r
2208         \r
2209         if (pfile_in_zip_read_info->rest_read_uncompressed == 0)\r
2210                 return 1;\r
2211         else\r
2212                 return 0;\r
2213 }\r
2214 \r
2215 \r
2216 \r
2217 /*\r
2218   Read extra field from the current file (opened by unzOpenCurrentFile)\r
2219   This is the static-header version of the extra field (sometimes, there is\r
2220     more info in the static-header version than in the central-header)\r
2221 \r
2222   if buf==NULL, it return the size of the static extra field that can be read\r
2223 \r
2224   if buf!=NULL, len is the size of the buffer, the extra header is copied in\r
2225         buf.\r
2226   the return value is the number of bytes copied in buf, or (if <0) \r
2227         the error code\r
2228 */\r
2229 extern int unzGetLocalExtrafield (unzFile file,void *buf,unsigned len)\r
2230 {\r
2231         unz_s* s;\r
2232         file_in_zip_read_info_s* pfile_in_zip_read_info;\r
2233         uInt read_now;\r
2234         uLong size_to_read;\r
2235 \r
2236         if (file==NULL)\r
2237                 return UNZ_PARAMERROR;\r
2238         s=(unz_s*)file;\r
2239     pfile_in_zip_read_info=s->pfile_in_zip_read;\r
2240 \r
2241         if (pfile_in_zip_read_info==NULL)\r
2242                 return UNZ_PARAMERROR;\r
2243 \r
2244         size_to_read = (pfile_in_zip_read_info->size_local_extrafield - \r
2245                                 pfile_in_zip_read_info->pos_local_extrafield);\r
2246 \r
2247         if (buf==NULL)\r
2248                 return (int)size_to_read;\r
2249         \r
2250         if (len>size_to_read)\r
2251                 read_now = (uInt)size_to_read;\r
2252         else\r
2253                 read_now = (uInt)len ;\r
2254 \r
2255         if (read_now==0)\r
2256                 return 0;\r
2257         \r
2258         if (fseek(pfile_in_zip_read_info->file,\r
2259               pfile_in_zip_read_info->offset_local_extrafield + \r
2260                           pfile_in_zip_read_info->pos_local_extrafield,SEEK_SET)!=0)\r
2261                 return UNZ_ERRNO;\r
2262 \r
2263         if (fread(buf,(uInt)size_to_read,1,pfile_in_zip_read_info->file)!=1)\r
2264                 return UNZ_ERRNO;\r
2265 \r
2266         return (int)read_now;\r
2267 }\r
2268 \r
2269 /*\r
2270   Close the file in zip opened with unzipOpenCurrentFile\r
2271   Return UNZ_CRCERROR if all the file was read but the CRC is not good\r
2272 */\r
2273 extern int unzCloseCurrentFile (unzFile file)\r
2274 {\r
2275         int err=UNZ_OK;\r
2276 \r
2277         unz_s* s;\r
2278         file_in_zip_read_info_s* pfile_in_zip_read_info;\r
2279         if (file==NULL)\r
2280                 return UNZ_PARAMERROR;\r
2281         s=(unz_s*)file;\r
2282     pfile_in_zip_read_info=s->pfile_in_zip_read;\r
2283 \r
2284         if (pfile_in_zip_read_info==NULL)\r
2285                 return UNZ_PARAMERROR;\r
2286 \r
2287 \r
2288         if (pfile_in_zip_read_info->rest_read_uncompressed == 0)\r
2289         {\r
2290                 if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait)\r
2291                         err=UNZ_CRCERROR;\r
2292         }\r
2293 \r
2294 \r
2295         free(pfile_in_zip_read_info->read_buffer);\r
2296         pfile_in_zip_read_info->read_buffer = NULL;\r
2297         if (pfile_in_zip_read_info->stream_initialised)\r
2298                 inflateEnd(&pfile_in_zip_read_info->stream);\r
2299 \r
2300         pfile_in_zip_read_info->stream_initialised = 0;\r
2301         free(pfile_in_zip_read_info);\r
2302 \r
2303     s->pfile_in_zip_read=NULL;\r
2304 \r
2305         return err;\r
2306 }\r
2307 \r
2308 \r
2309 /*\r
2310   Get the global comment string of the ZipFile, in the szComment buffer.\r
2311   uSizeBuf is the size of the szComment buffer.\r
2312   return the number of byte copied or an error code <0\r
2313 */\r
2314 extern int unzGetGlobalComment (unzFile file, char *szComment, uLong uSizeBuf)\r
2315 {\r
2316         unz_s* s;\r
2317         uLong uReadThis ;\r
2318         if (file==NULL)\r
2319                 return UNZ_PARAMERROR;\r
2320         s=(unz_s*)file;\r
2321 \r
2322         uReadThis = uSizeBuf;\r
2323         if (uReadThis>s->gi.size_comment)\r
2324                 uReadThis = s->gi.size_comment;\r
2325 \r
2326         if (fseek(s->file,s->central_pos+22,SEEK_SET)!=0)\r
2327                 return UNZ_ERRNO;\r
2328 \r
2329         if (uReadThis>0)\r
2330     {\r
2331       *szComment='\0';\r
2332           if (fread(szComment,(uInt)uReadThis,1,s->file)!=1)\r
2333                 return UNZ_ERRNO;\r
2334     }\r
2335 \r
2336         if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment))\r
2337                 *(szComment+s->gi.size_comment)='\0';\r
2338         return (int)uReadThis;\r
2339 }\r
2340 \r
2341 /* crc32.c -- compute the CRC-32 of a data stream\r
2342  * Copyright (C) 1995-1998 Mark Adler\r
2343  * For conditions of distribution and use, see copyright notice in zlib.h \r
2344  */\r
2345 \r
2346 #ifdef DYNAMIC_CRC_TABLE\r
2347 \r
2348 static int crc_table_empty = 1;\r
2349 static uLong crc_table[256];\r
2350 static void make_crc_table OF((void));\r
2351 \r
2352 /*\r
2353   Generate a table for a byte-wise 32-bit CRC calculation on the polynomial:\r
2354   x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.\r
2355 \r
2356   Polynomials over GF(2) are represented in binary, one bit per coefficient,\r
2357   with the lowest powers in the most significant bit.  Then adding polynomials\r
2358   is just exclusive-or, and multiplying a polynomial by x is a right shift by\r
2359   one.  If we call the above polynomial p, and represent a byte as the\r
2360   polynomial q, also with the lowest power in the most significant bit (so the\r
2361   byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,\r
2362   where a mod b means the remainder after dividing a by b.\r
2363 \r
2364   This calculation is done using the shift-register method of multiplying and\r
2365   taking the remainder.  The register is initialized to zero, and for each\r
2366   incoming bit, x^32 is added mod p to the register if the bit is a one (where\r
2367   x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by\r
2368   x (which is shifting right by one and adding x^32 mod p if the bit shifted\r
2369   out is a one).  We start with the highest power (least significant bit) of\r
2370   q and repeat for all eight bits of q.\r
2371 \r
2372   The table is simply the CRC of all possible eight bit values.  This is all\r
2373   the information needed to generate CRC's on data a byte at a time for all\r
2374   combinations of CRC register values and incoming bytes.\r
2375 */\r
2376 static void make_crc_table()\r
2377 {\r
2378   uLong c;\r
2379   int n, k;\r
2380   uLong poly;            /* polynomial exclusive-or pattern */\r
2381   /* terms of polynomial defining this crc (except x^32): */\r
2382   static const Byte p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};\r
2383 \r
2384   /* make exclusive-or pattern from polynomial (0xedb88320L) */\r
2385   poly = 0L;\r
2386   for (n = 0; n < sizeof(p)/sizeof(Byte); n++)\r
2387     poly |= 1L << (31 - p[n]);\r
2388  \r
2389   for (n = 0; n < 256; n++)\r
2390   {\r
2391     c = (uLong)n;\r
2392     for (k = 0; k < 8; k++)\r
2393       c = c & 1 ? poly ^ (c >> 1) : c >> 1;\r
2394     crc_table[n] = c;\r
2395   }\r
2396   crc_table_empty = 0;\r
2397 }\r
2398 #else\r
2399 /* ========================================================================\r
2400  * Table of CRC-32's of all single-byte values (made by make_crc_table)\r
2401  */\r
2402 static const uLong crc_table[256] = {\r
2403   0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,\r
2404   0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,\r
2405   0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,\r
2406   0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,\r
2407   0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,\r
2408   0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,\r
2409   0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,\r
2410   0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,\r
2411   0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,\r
2412   0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,\r
2413   0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,\r
2414   0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,\r
2415   0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,\r
2416   0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,\r
2417   0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,\r
2418   0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,\r
2419   0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,\r
2420   0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,\r
2421   0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,\r
2422   0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,\r
2423   0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,\r
2424   0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,\r
2425   0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,\r
2426   0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,\r
2427   0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,\r
2428   0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,\r
2429   0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,\r
2430   0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,\r
2431   0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,\r
2432   0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,\r
2433   0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,\r
2434   0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,\r
2435   0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,\r
2436   0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,\r
2437   0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,\r
2438   0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,\r
2439   0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,\r
2440   0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,\r
2441   0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,\r
2442   0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,\r
2443   0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,\r
2444   0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,\r
2445   0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,\r
2446   0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,\r
2447   0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,\r
2448   0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,\r
2449   0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,\r
2450   0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,\r
2451   0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,\r
2452   0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,\r
2453   0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,\r
2454   0x2d02ef8dL\r
2455 };\r
2456 #endif\r
2457 \r
2458 /* =========================================================================\r
2459  * This function can be used by asm versions of crc32()\r
2460  */\r
2461 const uLong * get_crc_table()\r
2462 {\r
2463 #ifdef DYNAMIC_CRC_TABLE\r
2464   if (crc_table_empty) make_crc_table();\r
2465 #endif\r
2466   return (const uLong *)crc_table;\r
2467 }\r
2468 \r
2469 /* ========================================================================= */\r
2470 #define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);\r
2471 #define DO2(buf)  DO1(buf); DO1(buf);\r
2472 #define DO4(buf)  DO2(buf); DO2(buf);\r
2473 #define DO8(buf)  DO4(buf); DO4(buf);\r
2474 \r
2475 /* ========================================================================= */\r
2476 uLong crc32(uLong crc, const Byte *buf, uInt len)\r
2477 {\r
2478     if (buf == Z_NULL) return 0L;\r
2479 #ifdef DYNAMIC_CRC_TABLE\r
2480     if (crc_table_empty)\r
2481       make_crc_table();\r
2482 #endif\r
2483     crc = crc ^ 0xffffffffL;\r
2484     while (len >= 8)\r
2485     {\r
2486       DO8(buf);\r
2487       len -= 8;\r
2488     }\r
2489     if (len) do {\r
2490       DO1(buf);\r
2491     } while (--len);\r
2492     return crc ^ 0xffffffffL;\r
2493 }\r
2494 \r
2495 /* infblock.h -- header to use infblock.c\r
2496  * Copyright (C) 1995-1998 Mark Adler\r
2497  * For conditions of distribution and use, see copyright notice in zlib.h \r
2498  */\r
2499 \r
2500 /* WARNING: this file should *not* be used by applications. It is\r
2501    part of the implementation of the compression library and is\r
2502    subject to change. Applications should only use zlib.h.\r
2503  */\r
2504 \r
2505 struct inflate_blocks_state;\r
2506 typedef struct inflate_blocks_state inflate_blocks_statef;\r
2507 \r
2508 extern inflate_blocks_statef * inflate_blocks_new OF((\r
2509     z_streamp z,\r
2510     check_func c,               /* check function */\r
2511     uInt w));                   /* window size */\r
2512 \r
2513 extern int inflate_blocks OF((\r
2514     inflate_blocks_statef *,\r
2515     z_streamp ,\r
2516     int));                      /* initial return code */\r
2517 \r
2518 extern void inflate_blocks_reset OF((\r
2519     inflate_blocks_statef *,\r
2520     z_streamp ,\r
2521     uLong *));                  /* check value on output */\r
2522 \r
2523 extern int inflate_blocks_free OF((\r
2524     inflate_blocks_statef *,\r
2525     z_streamp));\r
2526 \r
2527 extern void inflate_set_dictionary OF((\r
2528     inflate_blocks_statef *s,\r
2529     const Byte *d,  /* dictionary */\r
2530     uInt  n));       /* dictionary length */\r
2531 \r
2532 extern int inflate_blocks_sync_point OF((\r
2533     inflate_blocks_statef *s));\r
2534 \r
2535 /* simplify the use of the inflate_huft type with some defines */\r
2536 #define exop word.what.Exop\r
2537 #define bits word.what.Bits\r
2538 \r
2539 /* Table for deflate from PKZIP's appnote.txt. */\r
2540 static const uInt border[] = { /* Order of the bit length code lengths */\r
2541         16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};\r
2542 \r
2543 /* inftrees.h -- header to use inftrees.c\r
2544  * Copyright (C) 1995-1998 Mark Adler\r
2545  * For conditions of distribution and use, see copyright notice in zlib.h \r
2546  */\r
2547 \r
2548 /* WARNING: this file should *not* be used by applications. It is\r
2549    part of the implementation of the compression library and is\r
2550    subject to change. Applications should only use zlib.h.\r
2551  */\r
2552 \r
2553 /* Huffman code lookup table entry--this entry is four bytes for machines\r
2554    that have 16-bit pointers (e.g. PC's in the small or medium model). */\r
2555 \r
2556 typedef struct inflate_huft_s inflate_huft;\r
2557 \r
2558 struct inflate_huft_s {\r
2559   union {\r
2560     struct {\r
2561       Byte Exop;        /* number of extra bits or operation */\r
2562       Byte Bits;        /* number of bits in this code or subcode */\r
2563     } what;\r
2564     uInt pad;           /* pad structure to a power of 2 (4 bytes for */\r
2565   } word;               /*  16-bit, 8 bytes for 32-bit int's) */\r
2566   uInt base;            /* literal, length base, distance base,\r
2567                            or table offset */\r
2568 };\r
2569 \r
2570 /* Maximum size of dynamic tree.  The maximum found in a long but non-\r
2571    exhaustive search was 1004 huft structures (850 for length/literals\r
2572    and 154 for distances, the latter actually the result of an\r
2573    exhaustive search).  The actual maximum is not known, but the\r
2574    value below is more than safe. */\r
2575 #define MANY 1440\r
2576 \r
2577 extern int inflate_trees_bits OF((\r
2578     uInt *,                    /* 19 code lengths */\r
2579     uInt *,                    /* bits tree desired/actual depth */\r
2580     inflate_huft * *,       /* bits tree result */\r
2581     inflate_huft *,             /* space for trees */\r
2582     z_streamp));                /* for messages */\r
2583 \r
2584 extern int inflate_trees_dynamic OF((\r
2585     uInt,                       /* number of literal/length codes */\r
2586     uInt,                       /* number of distance codes */\r
2587     uInt *,                    /* that many (total) code lengths */\r
2588     uInt *,                    /* literal desired/actual bit depth */\r
2589     uInt *,                    /* distance desired/actual bit depth */\r
2590     inflate_huft * *,       /* literal/length tree result */\r
2591     inflate_huft * *,       /* distance tree result */\r
2592     inflate_huft *,             /* space for trees */\r
2593     z_streamp));                /* for messages */\r
2594 \r
2595 extern int inflate_trees_fixed OF((\r
2596     uInt *,                    /* literal desired/actual bit depth */\r
2597     uInt *,                    /* distance desired/actual bit depth */\r
2598     inflate_huft * *,       /* literal/length tree result */\r
2599     inflate_huft * *,       /* distance tree result */\r
2600     z_streamp));                /* for memory allocation */\r
2601 \r
2602 \r
2603 /* infcodes.h -- header to use infcodes.c\r
2604  * Copyright (C) 1995-1998 Mark Adler\r
2605  * For conditions of distribution and use, see copyright notice in zlib.h \r
2606  */\r
2607 \r
2608 /* WARNING: this file should *not* be used by applications. It is\r
2609    part of the implementation of the compression library and is\r
2610    subject to change. Applications should only use zlib.h.\r
2611  */\r
2612 \r
2613 struct inflate_codes_state;\r
2614 typedef struct inflate_codes_state inflate_codes_statef;\r
2615 \r
2616 extern inflate_codes_statef *inflate_codes_new OF((\r
2617     uInt, uInt,\r
2618     inflate_huft *, inflate_huft *,\r
2619     z_streamp ));\r
2620 \r
2621 extern int inflate_codes OF((\r
2622     inflate_blocks_statef *,\r
2623     z_streamp ,\r
2624     int));\r
2625 \r
2626 extern void inflate_codes_free OF((\r
2627     inflate_codes_statef *,\r
2628     z_streamp ));\r
2629 \r
2630 /* infutil.h -- types and macros common to blocks and codes\r
2631  * Copyright (C) 1995-1998 Mark Adler\r
2632  * For conditions of distribution and use, see copyright notice in zlib.h \r
2633  */\r
2634 \r
2635 /* WARNING: this file should *not* be used by applications. It is\r
2636    part of the implementation of the compression library and is\r
2637    subject to change. Applications should only use zlib.h.\r
2638  */\r
2639 \r
2640 #ifndef _INFUTIL_H\r
2641 #define _INFUTIL_H\r
2642 \r
2643 typedef enum {\r
2644       TYPE,     /* get type bits (3, including end bit) */\r
2645       LENS,     /* get lengths for stored */\r
2646       STORED,   /* processing stored block */\r
2647       TABLE,    /* get table lengths */\r
2648       BTREE,    /* get bit lengths tree for a dynamic block */\r
2649       DTREE,    /* get length, distance trees for a dynamic block */\r
2650       CODES,    /* processing fixed or dynamic block */\r
2651       DRY,      /* output remaining window bytes */\r
2652       DONE,     /* finished last block, done */\r
2653       BAD}      /* got a data error--stuck here */\r
2654 inflate_block_mode;\r
2655 \r
2656 /* inflate blocks semi-private state */\r
2657 struct inflate_blocks_state {\r
2658 \r
2659   /* mode */\r
2660   inflate_block_mode  mode;     /* current inflate_block mode */\r
2661 \r
2662   /* mode dependent information */\r
2663   union {\r
2664     uInt left;          /* if STORED, bytes left to copy */\r
2665     struct {\r
2666       uInt table;               /* table lengths (14 bits) */\r
2667       uInt index;               /* index into blens (or border) */\r
2668       uInt *blens;             /* bit lengths of codes */\r
2669       uInt bb;                  /* bit length tree depth */\r
2670       inflate_huft *tb;         /* bit length decoding tree */\r
2671     } trees;            /* if DTREE, decoding info for trees */\r
2672     struct {\r
2673       inflate_codes_statef \r
2674          *codes;\r
2675     } decode;           /* if CODES, current state */\r
2676   } sub;                /* submode */\r
2677   uInt last;            /* true if this block is the last block */\r
2678 \r
2679   /* mode independent information */\r
2680   uInt bitk;            /* bits in bit buffer */\r
2681   uLong bitb;           /* bit buffer */\r
2682   inflate_huft *hufts;  /* single malloc for tree space */\r
2683   Byte *window;        /* sliding window */\r
2684   Byte *end;           /* one byte after sliding window */\r
2685   Byte *read;          /* window read pointer */\r
2686   Byte *write;         /* window write pointer */\r
2687   check_func checkfn;   /* check function */\r
2688   uLong check;          /* check on output */\r
2689 \r
2690 };\r
2691 \r
2692 \r
2693 /* defines for inflate input/output */\r
2694 /*   update pointers and return */\r
2695 #define UPDBITS {s->bitb=b;s->bitk=k;}\r
2696 #define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;}\r
2697 #define UPDOUT {s->write=q;}\r
2698 #define UPDATE {UPDBITS UPDIN UPDOUT}\r
2699 #define LEAVE {UPDATE return inflate_flush(s,z,r);}\r
2700 /*   get bytes and bits */\r
2701 #define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}\r
2702 #define NEEDBYTE {if(n)r=Z_OK;else LEAVE}\r
2703 #define NEXTBYTE (n--,*p++)\r
2704 #define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}\r
2705 #define DUMPBITS(j) {b>>=(j);k-=(j);}\r
2706 /*   output bytes */\r
2707 #define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)\r
2708 #define LOADOUT {q=s->write;m=(uInt)WAVAIL;}\r
2709 #define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}\r
2710 #define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}\r
2711 #define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}\r
2712 #define OUTBYTE(a) {*q++=(Byte)(a);m--;}\r
2713 /*   load static pointers */\r
2714 #define LOAD {LOADIN LOADOUT}\r
2715 \r
2716 /* masks for lower bits (size given to avoid silly warnings with Visual C++) */\r
2717 extern uInt inflate_mask[17];\r
2718 \r
2719 /* copy as much as possible from the sliding window to the output area */\r
2720 extern int inflate_flush OF((\r
2721     inflate_blocks_statef *,\r
2722     z_streamp ,\r
2723     int));\r
2724 \r
2725 #endif\r
2726 \r
2727                                                                 \r
2728 /*\r
2729    Notes beyond the 1.93a appnote.txt:\r
2730 \r
2731    1. Distance pointers never point before the beginning of the output\r
2732       stream.\r
2733    2. Distance pointers can point back across blocks, up to 32k away.\r
2734    3. There is an implied maximum of 7 bits for the bit length table and\r
2735       15 bits for the actual data.\r
2736    4. If only one code exists, then it is encoded using one bit.  (Zero\r
2737       would be more efficient, but perhaps a little confusing.)  If two\r
2738       codes exist, they are coded using one bit each (0 and 1).\r
2739    5. There is no way of sending zero distance codes--a dummy must be\r
2740       sent if there are none.  (History: a pre 2.0 version of PKZIP would\r
2741       store blocks with no distance codes, but this was discovered to be\r
2742       too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow\r
2743       zero distance codes, which is sent as one code of zero bits in\r
2744       length.\r
2745    6. There are up to 286 literal/length codes.  Code 256 represents the\r
2746       end-of-block.  Note however that the static length tree defines\r
2747       288 codes just to fill out the Huffman codes.  Codes 286 and 287\r
2748       cannot be used though, since there is no length base or extra bits\r
2749       defined for them.  Similarily, there are up to 30 distance codes.\r
2750       However, static trees define 32 codes (all 5 bits) to fill out the\r
2751       Huffman codes, but the last two had better not show up in the data.\r
2752    7. Unzip can check dynamic Huffman blocks for complete code sets.\r
2753       The exception is that a single code would not be complete (see #4).\r
2754    8. The five bits following the block type is really the number of\r
2755       literal codes sent minus 257.\r
2756    9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits\r
2757       (1+6+6).  Therefore, to output three times the length, you output\r
2758       three codes (1+1+1), whereas to output four times the same length,\r
2759       you only need two codes (1+3).  Hmm.\r
2760   10. In the tree reconstruction algorithm, Code = Code + Increment\r
2761       only if BitLength(i) is not zero.  (Pretty obvious.)\r
2762   11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)\r
2763   12. Note: length code 284 can represent 227-258, but length code 285\r
2764       really is 258.  The last length deserves its own, short code\r
2765       since it gets used a lot in very redundant files.  The length\r
2766       258 is special since 258 - 3 (the min match length) is 255.\r
2767   13. The literal/length and distance code bit lengths are read as a\r
2768       single stream of lengths.  It is possible (and advantageous) for\r
2769       a repeat code (16, 17, or 18) to go across the boundary between\r
2770       the two sets of lengths.\r
2771  */\r
2772 \r
2773 \r
2774 void inflate_blocks_reset(inflate_blocks_statef *s, z_streamp z, uLong *c)\r
2775 {\r
2776   if (c != Z_NULL)\r
2777     *c = s->check;\r
2778   if (s->mode == BTREE || s->mode == DTREE)\r
2779     ZFREE(z, s->sub.trees.blens);\r
2780   if (s->mode == CODES)\r
2781     inflate_codes_free(s->sub.decode.codes, z);\r
2782   s->mode = TYPE;\r
2783   s->bitk = 0;\r
2784   s->bitb = 0;\r
2785   s->read = s->write = s->window;\r
2786   if (s->checkfn != Z_NULL)\r
2787     z->adler = s->check = (*s->checkfn)(0L, (const Byte *)Z_NULL, 0);\r
2788   Tracev(("inflate:   blocks reset\n"));\r
2789 }\r
2790 \r
2791 \r
2792 inflate_blocks_statef *inflate_blocks_new(z_streamp z, check_func c, uInt w)\r
2793 {\r
2794   inflate_blocks_statef *s;\r
2795 \r
2796   if ((s = (inflate_blocks_statef *)ZALLOC\r
2797        (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)\r
2798     return s;\r
2799   if ((s->hufts =\r
2800        (inflate_huft *)ZALLOC(z, sizeof(inflate_huft), MANY)) == Z_NULL)\r
2801   {\r
2802     ZFREE(z, s);\r
2803     return Z_NULL;\r
2804   }\r
2805   if ((s->window = (Byte *)ZALLOC(z, 1, w)) == Z_NULL)\r
2806   {\r
2807     ZFREE(z, s->hufts);\r
2808     ZFREE(z, s);\r
2809     return Z_NULL;\r
2810   }\r
2811   s->end = s->window + w;\r
2812   s->checkfn = c;\r
2813   s->mode = TYPE;\r
2814   Tracev(("inflate:   blocks allocated\n"));\r
2815   inflate_blocks_reset(s, z, Z_NULL);\r
2816   return s;\r
2817 }\r
2818 \r
2819 \r
2820 int inflate_blocks(inflate_blocks_statef *s, z_streamp z, int r)\r
2821 {\r
2822   uInt t;               /* temporary storage */\r
2823   uLong b;              /* bit buffer */\r
2824   uInt k;               /* bits in bit buffer */\r
2825   Byte *p;             /* input data pointer */\r
2826   uInt n;               /* bytes available there */\r
2827   Byte *q;             /* output window write pointer */\r
2828   uInt m;               /* bytes to end of window or read pointer */\r
2829 \r
2830   /* copy input/output information to locals (UPDATE macro restores) */\r
2831   LOAD\r
2832 \r
2833   /* process input based on current state */\r
2834   while (1) switch (s->mode)\r
2835   {\r
2836     case TYPE:\r
2837       NEEDBITS(3)\r
2838       t = (uInt)b & 7;\r
2839       s->last = t & 1;\r
2840       switch (t >> 1)\r
2841       {\r
2842         case 0:                         /* stored */\r
2843           Tracev(("inflate:     stored block%s\n",\r
2844                  s->last ? " (last)" : ""));\r
2845           DUMPBITS(3)\r
2846           t = k & 7;                    /* go to byte boundary */\r
2847           DUMPBITS(t)\r
2848           s->mode = LENS;               /* get length of stored block */\r
2849           break;\r
2850         case 1:                         /* fixed */\r
2851           Tracev(("inflate:     fixed codes block%s\n",\r
2852                  s->last ? " (last)" : ""));\r
2853           {\r
2854             uInt bl, bd;\r
2855             inflate_huft *tl, *td;\r
2856 \r
2857             inflate_trees_fixed(&bl, &bd, &tl, &td, z);\r
2858             s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);\r
2859             if (s->sub.decode.codes == Z_NULL)\r
2860             {\r
2861               r = Z_MEM_ERROR;\r
2862               LEAVE\r
2863             }\r
2864           }\r
2865           DUMPBITS(3)\r
2866           s->mode = CODES;\r
2867           break;\r
2868         case 2:                         /* dynamic */\r
2869           Tracev(("inflate:     dynamic codes block%s\n",\r
2870                  s->last ? " (last)" : ""));\r
2871           DUMPBITS(3)\r
2872           s->mode = TABLE;\r
2873           break;\r
2874         case 3:                         /* illegal */\r
2875           DUMPBITS(3)\r
2876           s->mode = BAD;\r
2877           z->msg = (char*)"invalid block type";\r
2878           r = Z_DATA_ERROR;\r
2879           LEAVE\r
2880       }\r
2881       break;\r
2882     case LENS:\r
2883       NEEDBITS(32)\r
2884       if ((((~b) >> 16) & 0xffff) != (b & 0xffff))\r
2885       {\r
2886         s->mode = BAD;\r
2887         z->msg = (char*)"invalid stored block lengths";\r
2888         r = Z_DATA_ERROR;\r
2889         LEAVE\r
2890       }\r
2891       s->sub.left = (uInt)b & 0xffff;\r
2892       b = k = 0;                      /* dump bits */\r
2893       Tracev(("inflate:       stored length %u\n", s->sub.left));\r
2894       s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);\r
2895       break;\r
2896     case STORED:\r
2897       if (n == 0)\r
2898         LEAVE\r
2899       NEEDOUT\r
2900       t = s->sub.left;\r
2901       if (t > n) t = n;\r
2902       if (t > m) t = m;\r
2903       zmemcpy(q, p, t);\r
2904       p += t;  n -= t;\r
2905       q += t;  m -= t;\r
2906       if ((s->sub.left -= t) != 0)\r
2907         break;\r
2908       Tracev(("inflate:       stored end, %lu total out\n",\r
2909               z->total_out + (q >= s->read ? q - s->read :\r
2910               (s->end - s->read) + (q - s->window))));\r
2911       s->mode = s->last ? DRY : TYPE;\r
2912       break;\r
2913     case TABLE:\r
2914       NEEDBITS(14)\r
2915       s->sub.trees.table = t = (uInt)b & 0x3fff;\r
2916 #ifndef PKZIP_BUG_WORKAROUND\r
2917       if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)\r
2918       {\r
2919         s->mode = BAD;\r
2920         z->msg = (char*)"too many length or distance symbols";\r
2921         r = Z_DATA_ERROR;\r
2922         LEAVE\r
2923       }\r
2924 #endif\r
2925       t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);\r
2926       if ((s->sub.trees.blens = (uInt*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)\r
2927       {\r
2928         r = Z_MEM_ERROR;\r
2929         LEAVE\r
2930       }\r
2931       DUMPBITS(14)\r
2932       s->sub.trees.index = 0;\r
2933       Tracev(("inflate:       table sizes ok\n"));\r
2934       s->mode = BTREE;\r
2935     case BTREE:\r
2936       while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))\r
2937       {\r
2938         NEEDBITS(3)\r
2939         s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;\r
2940         DUMPBITS(3)\r
2941       }\r
2942       while (s->sub.trees.index < 19)\r
2943         s->sub.trees.blens[border[s->sub.trees.index++]] = 0;\r
2944       s->sub.trees.bb = 7;\r
2945       t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,\r
2946                              &s->sub.trees.tb, s->hufts, z);\r
2947       if (t != Z_OK)\r
2948       {\r
2949         ZFREE(z, s->sub.trees.blens);\r
2950         r = t;\r
2951         if (r == Z_DATA_ERROR)\r
2952           s->mode = BAD;\r
2953         LEAVE\r
2954       }\r
2955       s->sub.trees.index = 0;\r
2956       Tracev(("inflate:       bits tree ok\n"));\r
2957       s->mode = DTREE;\r
2958     case DTREE:\r
2959       while (t = s->sub.trees.table,\r
2960              s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))\r
2961       {\r
2962         inflate_huft *h;\r
2963         uInt i, j, c;\r
2964 \r
2965         t = s->sub.trees.bb;\r
2966         NEEDBITS(t)\r
2967         h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);\r
2968         t = h->bits;\r
2969         c = h->base;\r
2970         if (c < 16)\r
2971         {\r
2972           DUMPBITS(t)\r
2973           s->sub.trees.blens[s->sub.trees.index++] = c;\r
2974         }\r
2975         else /* c == 16..18 */\r
2976         {\r
2977           i = c == 18 ? 7 : c - 14;\r
2978           j = c == 18 ? 11 : 3;\r
2979           NEEDBITS(t + i)\r
2980           DUMPBITS(t)\r
2981           j += (uInt)b & inflate_mask[i];\r
2982           DUMPBITS(i)\r
2983           i = s->sub.trees.index;\r
2984           t = s->sub.trees.table;\r
2985           if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||\r
2986               (c == 16 && i < 1))\r
2987           {\r
2988             ZFREE(z, s->sub.trees.blens);\r
2989             s->mode = BAD;\r
2990             z->msg = (char*)"invalid bit length repeat";\r
2991             r = Z_DATA_ERROR;\r
2992             LEAVE\r
2993           }\r
2994           c = c == 16 ? s->sub.trees.blens[i - 1] : 0;\r
2995           do {\r
2996             s->sub.trees.blens[i++] = c;\r
2997           } while (--j);\r
2998           s->sub.trees.index = i;\r
2999         }\r
3000       }\r
3001       s->sub.trees.tb = Z_NULL;\r
3002       {\r
3003         uInt bl, bd;\r
3004         inflate_huft *tl, *td;\r
3005         inflate_codes_statef *c;\r
3006 \r
3007         bl = 9;         /* must be <= 9 for lookahead assumptions */\r
3008         bd = 6;         /* must be <= 9 for lookahead assumptions */\r
3009         t = s->sub.trees.table;\r
3010         t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),\r
3011                                   s->sub.trees.blens, &bl, &bd, &tl, &td,\r
3012                                   s->hufts, z);\r
3013         ZFREE(z, s->sub.trees.blens);\r
3014         if (t != Z_OK)\r
3015         {\r
3016           if (t == (uInt)Z_DATA_ERROR)\r
3017             s->mode = BAD;\r
3018           r = t;\r
3019           LEAVE\r
3020         }\r
3021         Tracev(("inflate:       trees ok\n"));\r
3022         if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)\r
3023         {\r
3024           r = Z_MEM_ERROR;\r
3025           LEAVE\r
3026         }\r
3027         s->sub.decode.codes = c;\r
3028       }\r
3029       s->mode = CODES;\r
3030     case CODES:\r
3031       UPDATE\r
3032       if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)\r
3033         return inflate_flush(s, z, r);\r
3034       r = Z_OK;\r
3035       inflate_codes_free(s->sub.decode.codes, z);\r
3036       LOAD\r
3037       Tracev(("inflate:       codes end, %lu total out\n",\r
3038               z->total_out + (q >= s->read ? q - s->read :\r
3039               (s->end - s->read) + (q - s->window))));\r
3040       if (!s->last)\r
3041       {\r
3042         s->mode = TYPE;\r
3043         break;\r
3044       }\r
3045       s->mode = DRY;\r
3046     case DRY:\r
3047       FLUSH\r
3048       if (s->read != s->write)\r
3049         LEAVE\r
3050       s->mode = DONE;\r
3051     case DONE:\r
3052       r = Z_STREAM_END;\r
3053       LEAVE\r
3054     case BAD:\r
3055       r = Z_DATA_ERROR;\r
3056       LEAVE\r
3057     default:\r
3058       r = Z_STREAM_ERROR;\r
3059       LEAVE\r
3060   }\r
3061 }\r
3062 \r
3063 \r
3064 int inflate_blocks_free(inflate_blocks_statef *s, z_streamp z)\r
3065 {\r
3066   inflate_blocks_reset(s, z, Z_NULL);\r
3067   ZFREE(z, s->window);\r
3068   ZFREE(z, s->hufts);\r
3069   ZFREE(z, s);\r
3070   Tracev(("inflate:   blocks freed\n"));\r
3071   return Z_OK;\r
3072 }\r
3073 \r
3074 \r
3075 void inflate_set_dictionary(inflate_blocks_statef *s, const Byte *d, uInt n)\r
3076 {\r
3077   zmemcpy(s->window, d, n);\r
3078   s->read = s->write = s->window + n;\r
3079 }\r
3080 \r
3081 \r
3082 /* Returns true if inflate is currently at the end of a block generated\r
3083  * by Z_SYNC_FLUSH or Z_FULL_FLUSH. \r
3084  * IN assertion: s != Z_NULL\r
3085  */\r
3086 int inflate_blocks_sync_point(inflate_blocks_statef *s)\r
3087 {\r
3088   return s->mode == LENS;\r
3089 }\r
3090 \r
3091 /* And'ing with mask[n] masks the lower n bits */\r
3092 uInt inflate_mask[17] = {\r
3093     0x0000,\r
3094     0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,\r
3095     0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff\r
3096 };\r
3097 \r
3098 /* copy as much as possible from the sliding window to the output area */\r
3099 int inflate_flush(inflate_blocks_statef *s, z_streamp z, int r)\r
3100 {\r
3101   uInt n;\r
3102   Byte *p;\r
3103   Byte *q;\r
3104 \r
3105   /* static copies of source and destination pointers */\r
3106   p = z->next_out;\r
3107   q = s->read;\r
3108 \r
3109   /* compute number of bytes to copy as as end of window */\r
3110   n = (uInt)((q <= s->write ? s->write : s->end) - q);\r
3111   if (n > z->avail_out) n = z->avail_out;\r
3112   if (n && r == Z_BUF_ERROR) r = Z_OK;\r
3113 \r
3114   /* update counters */\r
3115   z->avail_out -= n;\r
3116   z->total_out += n;\r
3117 \r
3118   /* update check information */\r
3119   if (s->checkfn != Z_NULL)\r
3120     z->adler = s->check = (*s->checkfn)(s->check, q, n);\r
3121 \r
3122   /* copy as as end of window */\r
3123   zmemcpy(p, q, n);\r
3124   p += n;\r
3125   q += n;\r
3126 \r
3127   /* see if more to copy at beginning of window */\r
3128   if (q == s->end)\r
3129   {\r
3130     /* wrap pointers */\r
3131     q = s->window;\r
3132     if (s->write == s->end)\r
3133       s->write = s->window;\r
3134 \r
3135     /* compute bytes to copy */\r
3136     n = (uInt)(s->write - q);\r
3137     if (n > z->avail_out) n = z->avail_out;\r
3138     if (n && r == Z_BUF_ERROR) r = Z_OK;\r
3139 \r
3140     /* update counters */\r
3141     z->avail_out -= n;\r
3142     z->total_out += n;\r
3143 \r
3144     /* update check information */\r
3145     if (s->checkfn != Z_NULL)\r
3146       z->adler = s->check = (*s->checkfn)(s->check, q, n);\r
3147 \r
3148     /* copy */\r
3149     zmemcpy(p, q, n);\r
3150     p += n;\r
3151     q += n;\r
3152   }\r
3153 \r
3154   /* update pointers */\r
3155   z->next_out = p;\r
3156   s->read = q;\r
3157 \r
3158   /* done */\r
3159   return r;\r
3160 }\r
3161 \r
3162 /* inftrees.c -- generate Huffman trees for efficient decoding\r
3163  * Copyright (C) 1995-1998 Mark Adler\r
3164  * For conditions of distribution and use, see copyright notice in zlib.h \r
3165  */\r
3166 \r
3167 const char inflate_copyright[] =\r
3168    " inflate 1.1.3 Copyright 1995-1998 Mark Adler ";\r
3169 /*\r
3170   If you use the zlib library in a product, an acknowledgment is welcome\r
3171   in the documentation of your product. If for some reason you cannot\r
3172   include such an acknowledgment, I would appreciate that you keep this\r
3173   copyright string in the executable of your product.\r
3174  */\r
3175 \r
3176 /* simplify the use of the inflate_huft type with some defines */\r
3177 #define exop word.what.Exop\r
3178 #define bits word.what.Bits\r
3179 \r
3180 \r
3181 static int huft_build OF((\r
3182     uInt *,                             /* code lengths in bits */\r
3183     uInt,               /* number of codes */\r
3184     uInt,               /* number of "simple" codes */\r
3185     const uInt *,               /* list of base values for non-simple codes */\r
3186     const uInt *,               /* list of extra bits for non-simple codes */\r
3187     inflate_huft **,    /* result: starting table */\r
3188     uInt *,                             /* maximum lookup bits (returns actual) */\r
3189     inflate_huft *,     /* space for trees */\r
3190     uInt *,             /* hufts used in space */\r
3191     uInt * ));                  /* space for values */\r
3192 \r
3193 /* Tables for deflate from PKZIP's appnote.txt. */\r
3194 static const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */\r
3195         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,\r
3196         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};\r
3197         /* see note #13 above about 258 */\r
3198 static const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */\r
3199         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,\r
3200         3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */\r
3201 static const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */\r
3202         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,\r
3203         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,\r
3204         8193, 12289, 16385, 24577};\r
3205 static const uInt cpdext[30] = { /* Extra bits for distance codes */\r
3206         0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,\r
3207         7, 7, 8, 8, 9, 9, 10, 10, 11, 11,\r
3208         12, 12, 13, 13};\r
3209 \r
3210 /*\r
3211    Huffman code decoding is performed using a multi-level table lookup.\r
3212    The fastest way to decode is to simply build a lookup table whose\r
3213    size is determined by the longest code.  However, the time it takes\r
3214    to build this table can also be a factor if the data being decoded\r
3215    is not very long.  The most common codes are necessarily the\r
3216    shortest codes, so those codes dominate the decoding time, and hence\r
3217    the speed.  The idea is you can have a shorter table that decodes the\r
3218    shorter, more probable codes, and then point to subsidiary tables for\r
3219    the longer codes.  The time it costs to decode the longer codes is\r
3220    then traded against the time it takes to make longer tables.\r
3221 \r
3222    This results of this trade are in the variables lbits and dbits\r
3223    below.  lbits is the number of bits the first level table for literal/\r
3224    length codes can decode in one step, and dbits is the same thing for\r
3225    the distance codes.  Subsequent tables are also less than or equal to\r
3226    those sizes.  These values may be adjusted either when all of the\r
3227    codes are shorter than that, in which case the longest code length in\r
3228    bits is used, or when the shortest code is *longer* than the requested\r
3229    table size, in which case the length of the shortest code in bits is\r
3230    used.\r
3231 \r
3232    There are two different values for the two tables, since they code a\r
3233    different number of possibilities each.  The literal/length table\r
3234    codes 286 possible values, or in a flat code, a little over eight\r
3235    bits.  The distance table codes 30 possible values, or a little less\r
3236    than five bits, flat.  The optimum values for speed end up being\r
3237    about one bit more than those, so lbits is 8+1 and dbits is 5+1.\r
3238    The optimum values may differ though from machine to machine, and\r
3239    possibly even between compilers.  Your mileage may vary.\r
3240  */\r
3241 \r
3242 \r
3243 /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */\r
3244 #define BMAX 15         /* maximum bit length of any code */\r
3245 \r
3246 static int huft_build(uInt *b, uInt n, uInt s, const uInt *d, const uInt *e, inflate_huft ** t, uInt *m, inflate_huft *hp, uInt *hn, uInt *v)\r
3247 //uInt *b;               /* code lengths in bits (all assumed <= BMAX) */\r
3248 //uInt n;                 /* number of codes (assumed <= 288) */\r
3249 //uInt s;                 /* number of simple-valued codes (0..s-1) */\r
3250 //const uInt *d;         /* list of base values for non-simple codes */\r
3251 //const uInt *e;         /* list of extra bits for non-simple codes */\r
3252 //inflate_huft ** t;            /* result: starting table */\r
3253 //uInt *m;               /* maximum lookup bits, returns actual */\r
3254 //inflate_huft *hp;       /* space for trees */\r
3255 //uInt *hn;               /* hufts used in space */\r
3256 //uInt *v;               /* working area: values in order of bit length */\r
3257 /* Given a list of code lengths and a maximum table size, make a set of\r
3258    tables to decode that set of codes.  Return Z_OK on success, Z_BUF_ERROR\r
3259    if the given code set is incomplete (the tables are still built in this\r
3260    case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of\r
3261    lengths), or Z_MEM_ERROR if not enough memory. */\r
3262 {\r
3263 \r
3264   uInt a;                       /* counter for codes of length k */\r
3265   uInt c[BMAX+1];               /* bit length count table */\r
3266   uInt f;                       /* i repeats in table every f entries */\r
3267   int g;                        /* maximum code length */\r
3268   int h;                        /* table level */\r
3269   register uInt i;              /* counter, current code */\r
3270   register uInt j;              /* counter */\r
3271   register int k;               /* number of bits in current code */\r
3272   int l;                        /* bits per table (returned in m) */\r
3273   uInt mask;                    /* (1 << w) - 1, to avoid cc -O bug on HP */\r
3274   register uInt *p;            /* pointer into c[], b[], or v[] */\r
3275   inflate_huft *q;              /* points to current table */\r
3276   struct inflate_huft_s r;      /* table entry for structure assignment */\r
3277   inflate_huft *u[BMAX];        /* table stack */\r
3278   register int w;               /* bits before this table == (l * h) */\r
3279   uInt x[BMAX+1];               /* bit offsets, then code stack */\r
3280   uInt *xp;                    /* pointer into x */\r
3281   int y;                        /* number of dummy codes added */\r
3282   uInt z;                       /* number of entries in current table */\r
3283 \r
3284 \r
3285   /* Generate counts for each bit length */\r
3286   p = c;\r
3287 #define C0 *p++ = 0;\r
3288 #define C2 C0 C0 C0 C0\r
3289 #define C4 C2 C2 C2 C2\r
3290   C4                            /* clear c[]--assume BMAX+1 is 16 */\r
3291   p = b;  i = n;\r
3292   do {\r
3293     c[*p++]++;                  /* assume all entries <= BMAX */\r
3294   } while (--i);\r
3295   if (c[0] == n)                /* null input--all zero length codes */\r
3296   {\r
3297     *t = (inflate_huft *)Z_NULL;\r
3298     *m = 0;\r
3299     return Z_OK;\r
3300   }\r
3301 \r
3302 \r
3303   /* Find minimum and maximum length, bound *m by those */\r
3304   l = *m;\r
3305   for (j = 1; j <= BMAX; j++)\r
3306     if (c[j])\r
3307       break;\r
3308   k = j;                        /* minimum code length */\r
3309   if ((uInt)l < j)\r
3310     l = j;\r
3311   for (i = BMAX; i; i--)\r
3312     if (c[i])\r
3313       break;\r
3314   g = i;                        /* maximum code length */\r
3315   if ((uInt)l > i)\r
3316     l = i;\r
3317   *m = l;\r
3318 \r
3319 \r
3320   /* Adjust last length count to fill out codes, if needed */\r
3321   for (y = 1 << j; j < i; j++, y <<= 1)\r
3322     if ((y -= c[j]) < 0)\r
3323       return Z_DATA_ERROR;\r
3324   if ((y -= c[i]) < 0)\r
3325     return Z_DATA_ERROR;\r
3326   c[i] += y;\r
3327 \r
3328 \r
3329   /* Generate starting offsets into the value table for each length */\r
3330   x[1] = j = 0;\r
3331   p = c + 1;  xp = x + 2;\r
3332   while (--i) {                 /* note that i == g from above */\r
3333     *xp++ = (j += *p++);\r
3334   }\r
3335 \r
3336 \r
3337   /* Make a table of values in order of bit lengths */\r
3338   p = b;  i = 0;\r
3339   do {\r
3340     if ((j = *p++) != 0)\r
3341       v[x[j]++] = i;\r
3342   } while (++i < n);\r
3343   n = x[g];                     /* set n to length of v */\r
3344 \r
3345 \r
3346   /* Generate the Huffman codes and for each, make the table entries */\r
3347   x[0] = i = 0;                 /* first Huffman code is zero */\r
3348   p = v;                        /* grab values in bit order */\r
3349   h = -1;                       /* no tables yet--level -1 */\r
3350   w = -l;                       /* bits decoded == (l * h) */\r
3351   u[0] = (inflate_huft *)Z_NULL;        /* just to keep compilers happy */\r
3352   q = (inflate_huft *)Z_NULL;   /* ditto */\r
3353   z = 0;                        /* ditto */\r
3354 \r
3355   /* go through the bit lengths (k already is bits in shortest code) */\r
3356   for (; k <= g; k++)\r
3357   {\r
3358     a = c[k];\r
3359     while (a--)\r
3360     {\r
3361       /* here i is the Huffman code of length k bits for value *p */\r
3362       /* make tables up to required level */\r
3363       while (k > w + l)\r
3364       {\r
3365         h++;\r
3366         w += l;                 /* previous table always l bits */\r
3367 \r
3368         /* compute minimum size table less than or equal to l bits */\r
3369         z = g - w;\r
3370         z = z > (uInt)l ? l : z;        /* table size upper limit */\r
3371         if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */\r
3372         {                       /* too few codes for k-w bit table */\r
3373           f -= a + 1;           /* deduct codes from patterns left */\r
3374           xp = c + k;\r
3375           if (j < z)\r
3376             while (++j < z)     /* try smaller tables up to z bits */\r
3377             {\r
3378               if ((f <<= 1) <= *++xp)\r
3379                 break;          /* enough codes to use up j bits */\r
3380               f -= *xp;         /* else deduct codes from patterns */\r
3381             }\r
3382         }\r
3383         z = 1 << j;             /* table entries for j-bit table */\r
3384 \r
3385         /* allocate new table */\r
3386         if (*hn + z > MANY)     /* (note: doesn't matter for fixed) */\r
3387           return Z_MEM_ERROR;   /* not enough memory */\r
3388         u[h] = q = hp + *hn;\r
3389         *hn += z;\r
3390 \r
3391         /* connect to last table, if there is one */\r
3392         if (h)\r
3393         {\r
3394           x[h] = i;             /* save pattern for backing up */\r
3395           r.bits = (Byte)l;     /* bits to dump before this table */\r
3396           r.exop = (Byte)j;     /* bits in this table */\r
3397           j = i >> (w - l);\r
3398           r.base = (uInt)(q - u[h-1] - j);   /* offset to this table */\r
3399           u[h-1][j] = r;        /* connect to last table */\r
3400         }\r
3401         else\r
3402           *t = q;               /* first table is returned result */\r
3403       }\r
3404 \r
3405       /* set up table entry in r */\r
3406       r.bits = (Byte)(k - w);\r
3407       if (p >= v + n)\r
3408         r.exop = 128 + 64;      /* out of values--invalid code */\r
3409       else if (*p < s)\r
3410       {\r
3411         r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);     /* 256 is end-of-block */\r
3412         r.base = *p++;          /* simple code is just the value */\r
3413       }\r
3414       else\r
3415       {\r
3416         r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */\r
3417         r.base = d[*p++ - s];\r
3418       }\r
3419 \r
3420       /* fill code-like entries with r */\r
3421       f = 1 << (k - w);\r
3422       for (j = i >> w; j < z; j += f)\r
3423         q[j] = r;\r
3424 \r
3425       /* backwards increment the k-bit code i */\r
3426       for (j = 1 << (k - 1); i & j; j >>= 1)\r
3427         i ^= j;\r
3428       i ^= j;\r
3429 \r
3430       /* backup over finished tables */\r
3431       mask = (1 << w) - 1;      /* needed on HP, cc -O bug */\r
3432       while ((i & mask) != x[h])\r
3433       {\r
3434         h--;                    /* don't need to update q */\r
3435         w -= l;\r
3436         mask = (1 << w) - 1;\r
3437       }\r
3438     }\r
3439   }\r
3440 \r
3441 \r
3442   /* Return Z_BUF_ERROR if we were given an incomplete table */\r
3443   return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;\r
3444 }\r
3445 \r
3446 \r
3447 int inflate_trees_bits(uInt *c, uInt *bb, inflate_huft * *tb, inflate_huft *hp, z_streamp z)\r
3448 //uInt *c;               /* 19 code lengths */\r
3449 //uInt *bb;              /* bits tree desired/actual depth */\r
3450 //inflate_huft * *tb; /* bits tree result */\r
3451 //inflate_huft *hp;       /* space for trees */\r
3452 //z_streamp z;            /* for messages */\r
3453 {\r
3454   int r;\r
3455   uInt hn = 0;          /* hufts used in space */\r
3456   uInt *v;             /* work area for huft_build */\r
3457 \r
3458   if ((v = (uInt*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)\r
3459     return Z_MEM_ERROR;\r
3460   r = huft_build(c, 19, 19, (uInt*)Z_NULL, (uInt*)Z_NULL,\r
3461                  tb, bb, hp, &hn, v);\r
3462   if (r == Z_DATA_ERROR)\r
3463     z->msg = (char*)"oversubscribed dynamic bit lengths tree";\r
3464   else if (r == Z_BUF_ERROR || *bb == 0)\r
3465   {\r
3466     z->msg = (char*)"incomplete dynamic bit lengths tree";\r
3467     r = Z_DATA_ERROR;\r
3468   }\r
3469   ZFREE(z, v);\r
3470   return r;\r
3471 }\r
3472 \r
3473 \r
3474 int inflate_trees_dynamic(uInt nl, uInt nd, uInt *c, uInt *bl, uInt *bd, inflate_huft * *tl, inflate_huft * *td, inflate_huft *hp, z_streamp z)\r
3475 //uInt nl;                /* number of literal/length codes */\r
3476 //uInt nd;                /* number of distance codes */\r
3477 //uInt *c;               /* that many (total) code lengths */\r
3478 //uInt *bl;              /* literal desired/actual bit depth */\r
3479 //uInt *bd;              /* distance desired/actual bit depth */\r
3480 //inflate_huft * *tl; /* literal/length tree result */\r
3481 //inflate_huft * *td; /* distance tree result */\r
3482 //inflate_huft *hp;       /* space for trees */\r
3483 //z_streamp z;            /* for messages */\r
3484 {\r
3485   int r;\r
3486   uInt hn = 0;          /* hufts used in space */\r
3487   uInt *v;             /* work area for huft_build */\r
3488 \r
3489   /* allocate work area */\r
3490   if ((v = (uInt*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)\r
3491     return Z_MEM_ERROR;\r
3492 \r
3493   /* build literal/length tree */\r
3494   r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);\r
3495   if (r != Z_OK || *bl == 0)\r
3496   {\r
3497     if (r == Z_DATA_ERROR)\r
3498       z->msg = (char*)"oversubscribed literal/length tree";\r
3499     else if (r != Z_MEM_ERROR)\r
3500     {\r
3501       z->msg = (char*)"incomplete literal/length tree";\r
3502       r = Z_DATA_ERROR;\r
3503     }\r
3504     ZFREE(z, v);\r
3505     return r;\r
3506   }\r
3507 \r
3508   /* build distance tree */\r
3509   r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);\r
3510   if (r != Z_OK || (*bd == 0 && nl > 257))\r
3511   {\r
3512     if (r == Z_DATA_ERROR)\r
3513       z->msg = (char*)"oversubscribed distance tree";\r
3514     else if (r == Z_BUF_ERROR) {\r
3515 #ifdef PKZIP_BUG_WORKAROUND\r
3516       r = Z_OK;\r
3517     }\r
3518 #else\r
3519       z->msg = (char*)"incomplete distance tree";\r
3520       r = Z_DATA_ERROR;\r
3521     }\r
3522     else if (r != Z_MEM_ERROR)\r
3523     {\r
3524       z->msg = (char*)"empty distance tree with lengths";\r
3525       r = Z_DATA_ERROR;\r
3526     }\r
3527     ZFREE(z, v);\r
3528     return r;\r
3529 #endif\r
3530   }\r
3531 \r
3532   /* done */\r
3533   ZFREE(z, v);\r
3534   return Z_OK;\r
3535 }\r
3536 \r
3537 /* inffixed.h -- table for decoding fixed codes\r
3538  * Generated automatically by the maketree.c program\r
3539  */\r
3540 \r
3541 /* WARNING: this file should *not* be used by applications. It is\r
3542    part of the implementation of the compression library and is\r
3543    subject to change. Applications should only use zlib.h.\r
3544  */\r
3545 \r
3546 static uInt fixed_bl = 9;\r
3547 static uInt fixed_bd = 5;\r
3548 static inflate_huft fixed_tl[] = {\r
3549     {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},\r
3550     {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},192},\r
3551     {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},160},\r
3552     {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},224},\r
3553     {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},144},\r
3554     {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},208},\r
3555     {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},176},\r
3556     {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},240},\r
3557     {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},\r
3558     {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},200},\r
3559     {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},168},\r
3560     {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},232},\r
3561     {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},152},\r
3562     {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},216},\r
3563     {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},184},\r
3564     {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},248},\r
3565     {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},\r
3566     {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},196},\r
3567     {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},164},\r
3568     {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},228},\r
3569     {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},148},\r
3570     {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},212},\r
3571     {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},180},\r
3572     {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},244},\r
3573     {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},\r
3574     {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},204},\r
3575     {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},172},\r
3576     {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},236},\r
3577     {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},156},\r
3578     {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},220},\r
3579     {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},188},\r
3580     {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},252},\r
3581     {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},\r
3582     {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},194},\r
3583     {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},162},\r
3584     {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},226},\r
3585     {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},146},\r
3586     {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},210},\r
3587     {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},178},\r
3588     {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},242},\r
3589     {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},\r
3590     {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},202},\r
3591     {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},170},\r
3592     {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},234},\r
3593     {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},154},\r
3594     {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},218},\r
3595     {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},186},\r
3596     {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},250},\r
3597     {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},\r
3598     {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},198},\r
3599     {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},166},\r
3600     {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},230},\r
3601     {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},150},\r
3602     {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},214},\r
3603     {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},182},\r
3604     {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},246},\r
3605     {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},\r
3606     {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},206},\r
3607     {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},174},\r
3608     {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},238},\r
3609     {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},158},\r
3610     {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},222},\r
3611     {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},190},\r
3612     {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},254},\r
3613     {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},\r
3614     {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},193},\r
3615     {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},161},\r
3616     {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},225},\r
3617     {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},145},\r
3618     {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},209},\r
3619     {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},177},\r
3620     {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},241},\r
3621     {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},\r
3622     {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},201},\r
3623     {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},169},\r
3624     {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},233},\r
3625     {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},153},\r
3626     {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},217},\r
3627     {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},185},\r
3628     {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},249},\r
3629     {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},\r
3630     {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},197},\r
3631     {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},165},\r
3632     {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},229},\r
3633     {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},149},\r
3634     {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},213},\r
3635     {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},181},\r
3636     {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},245},\r
3637     {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},\r
3638     {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},205},\r
3639     {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},173},\r
3640     {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},237},\r
3641     {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},157},\r
3642     {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},221},\r
3643     {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},189},\r
3644     {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},253},\r
3645     {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},\r
3646     {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},195},\r
3647     {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},163},\r
3648     {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},227},\r
3649     {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},147},\r
3650     {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},211},\r
3651     {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},179},\r
3652     {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},243},\r
3653     {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},\r
3654     {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},203},\r
3655     {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},171},\r
3656     {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},235},\r
3657     {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},155},\r
3658     {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},219},\r
3659     {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},187},\r
3660     {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},251},\r
3661     {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},\r
3662     {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},199},\r
3663     {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},167},\r
3664     {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},231},\r
3665     {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},151},\r
3666     {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},215},\r
3667     {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},183},\r
3668     {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},247},\r
3669     {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},\r
3670     {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},207},\r
3671     {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},175},\r
3672     {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},239},\r
3673     {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},159},\r
3674     {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},223},\r
3675     {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},191},\r
3676     {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},255}\r
3677   };\r
3678 static inflate_huft fixed_td[] = {\r
3679     {{{80,5}},1}, {{{87,5}},257}, {{{83,5}},17}, {{{91,5}},4097},\r
3680     {{{81,5}},5}, {{{89,5}},1025}, {{{85,5}},65}, {{{93,5}},16385},\r
3681     {{{80,5}},3}, {{{88,5}},513}, {{{84,5}},33}, {{{92,5}},8193},\r
3682     {{{82,5}},9}, {{{90,5}},2049}, {{{86,5}},129}, {{{192,5}},24577},\r
3683     {{{80,5}},2}, {{{87,5}},385}, {{{83,5}},25}, {{{91,5}},6145},\r
3684     {{{81,5}},7}, {{{89,5}},1537}, {{{85,5}},97}, {{{93,5}},24577},\r
3685     {{{80,5}},4}, {{{88,5}},769}, {{{84,5}},49}, {{{92,5}},12289},\r
3686     {{{82,5}},13}, {{{90,5}},3073}, {{{86,5}},193}, {{{192,5}},24577}\r
3687   };\r
3688 \r
3689 int inflate_trees_fixed(uInt *bl, uInt *bd, inflate_huft * *tl, inflate_huft * *td, z_streamp z)\r
3690 //uInt *bl;               /* literal desired/actual bit depth */\r
3691 //uInt *bd;               /* distance desired/actual bit depth */\r
3692 //inflate_huft * *tl;  /* literal/length tree result */\r
3693 //inflate_huft * *td;  /* distance tree result */\r
3694 //z_streamp z;             /* for memory allocation */\r
3695 {\r
3696   *bl = fixed_bl;\r
3697   *bd = fixed_bd;\r
3698   *tl = fixed_tl;\r
3699   *td = fixed_td;\r
3700   return Z_OK;\r
3701 }\r
3702 \r
3703 /* simplify the use of the inflate_huft type with some defines */\r
3704 #define exop word.what.Exop\r
3705 #define bits word.what.Bits\r
3706 \r
3707 /* macros for bit input with no checking and for returning unused bytes */\r
3708 #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}\r
3709 #define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;}\r
3710 \r
3711 /* Called with number of bytes left to write in window at least 258\r
3712    (the maximum string length) and number of input bytes available\r
3713    at least ten.  The ten bytes are six bytes for the longest length/\r
3714    distance pair plus four bytes for overloading the bit buffer. */\r
3715 \r
3716 int inflate_fast(uInt bl, uInt bd, inflate_huft *tl, inflate_huft *td, inflate_blocks_statef *s, z_streamp z)\r
3717 {\r
3718   inflate_huft *t;      /* temporary pointer */\r
3719   uInt e;               /* extra bits or operation */\r
3720   uLong b;              /* bit buffer */\r
3721   uInt k;               /* bits in bit buffer */\r
3722   Byte *p;             /* input data pointer */\r
3723   uInt n;               /* bytes available there */\r
3724   Byte *q;             /* output window write pointer */\r
3725   uInt m;               /* bytes to end of window or read pointer */\r
3726   uInt ml;              /* mask for literal/length tree */\r
3727   uInt md;              /* mask for distance tree */\r
3728   uInt c;               /* bytes to copy */\r
3729   uInt d;               /* distance back to copy from */\r
3730   Byte *r;             /* copy source pointer */\r
3731 \r
3732   /* load input, output, bit values */\r
3733   LOAD\r
3734 \r
3735   /* initialize masks */\r
3736   ml = inflate_mask[bl];\r
3737   md = inflate_mask[bd];\r
3738 \r
3739   /* do until not enough input or output space for fast loop */\r
3740   do {                          /* assume called with m >= 258 && n >= 10 */\r
3741     /* get literal/length code */\r
3742     GRABBITS(20)                /* max bits for literal/length code */\r
3743     if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)\r
3744     {\r
3745       DUMPBITS(t->bits)\r
3746       Tracevv((t->base >= 0x20 && t->base < 0x7f ?\r
3747                 "inflate:         * literal '%c'\n" :\r
3748                 "inflate:         * literal 0x%02x\n", t->base));\r
3749       *q++ = (Byte)t->base;\r
3750       m--;\r
3751       continue;\r
3752     }\r
3753     do {\r
3754       DUMPBITS(t->bits)\r
3755       if (e & 16)\r
3756       {\r
3757         /* get extra bits for length */\r
3758         e &= 15;\r
3759         c = t->base + ((uInt)b & inflate_mask[e]);\r
3760         DUMPBITS(e)\r
3761         Tracevv(("inflate:         * length %u\n", c));\r
3762 \r
3763         /* decode distance base of block to copy */\r
3764         GRABBITS(15);           /* max bits for distance code */\r
3765         e = (t = td + ((uInt)b & md))->exop;\r
3766         do {\r
3767           DUMPBITS(t->bits)\r
3768           if (e & 16)\r
3769           {\r
3770             /* get extra bits to add to distance base */\r
3771             e &= 15;\r
3772             GRABBITS(e)         /* get extra bits (up to 13) */\r
3773             d = t->base + ((uInt)b & inflate_mask[e]);\r
3774             DUMPBITS(e)\r
3775             Tracevv(("inflate:         * distance %u\n", d));\r
3776 \r
3777             /* do the copy */\r
3778             m -= c;\r
3779             if ((uInt)(q - s->window) >= d)     /* offset before dest */\r
3780             {                                   /*  just copy */\r
3781               r = q - d;\r
3782               *q++ = *r++;  c--;        /* minimum count is three, */\r
3783               *q++ = *r++;  c--;        /*  so unroll loop a little */\r
3784             }\r
3785             else                        /* else offset after destination */\r
3786             {\r
3787               e = d - (uInt)(q - s->window); /* bytes from offset to end */\r
3788               r = s->end - e;           /* pointer to offset */\r
3789               if (c > e)                /* if source crosses, */\r
3790               {\r
3791                 c -= e;                 /* copy to end of window */\r
3792                 do {\r
3793                   *q++ = *r++;\r
3794                 } while (--e);\r
3795                 r = s->window;          /* copy rest from start of window */\r
3796               }\r
3797             }\r
3798             do {                        /* copy all or what's left */\r
3799               *q++ = *r++;\r
3800             } while (--c);\r
3801             break;\r
3802           }\r
3803           else if ((e & 64) == 0)\r
3804           {\r
3805             t += t->base;\r
3806             e = (t += ((uInt)b & inflate_mask[e]))->exop;\r
3807           }\r
3808           else\r
3809           {\r
3810             z->msg = (char*)"invalid distance code";\r
3811             UNGRAB\r
3812             UPDATE\r
3813             return Z_DATA_ERROR;\r
3814           }\r
3815         } while (1);\r
3816         break;\r
3817       }\r
3818       if ((e & 64) == 0)\r
3819       {\r
3820         t += t->base;\r
3821         if ((e = (t += ((uInt)b & inflate_mask[e]))->exop) == 0)\r
3822         {\r
3823           DUMPBITS(t->bits)\r
3824           Tracevv((t->base >= 0x20 && t->base < 0x7f ?\r
3825                     "inflate:         * literal '%c'\n" :\r
3826                     "inflate:         * literal 0x%02x\n", t->base));\r
3827           *q++ = (Byte)t->base;\r
3828           m--;\r
3829           break;\r
3830         }\r
3831       }\r
3832       else if (e & 32)\r
3833       {\r
3834         Tracevv(("inflate:         * end of block\n"));\r
3835         UNGRAB\r
3836         UPDATE\r
3837         return Z_STREAM_END;\r
3838       }\r
3839       else\r
3840       {\r
3841         z->msg = (char*)"invalid literal/length code";\r
3842         UNGRAB\r
3843         UPDATE\r
3844         return Z_DATA_ERROR;\r
3845       }\r
3846     } while (1);\r
3847   } while (m >= 258 && n >= 10);\r
3848 \r
3849   /* not enough input or output--restore pointers and return */\r
3850   UNGRAB\r
3851   UPDATE\r
3852   return Z_OK;\r
3853 }\r
3854 \r
3855 /* infcodes.c -- process literals and length/distance pairs\r
3856  * Copyright (C) 1995-1998 Mark Adler\r
3857  * For conditions of distribution and use, see copyright notice in zlib.h \r
3858  */\r
3859 \r
3860 /* simplify the use of the inflate_huft type with some defines */\r
3861 #define exop word.what.Exop\r
3862 #define bits word.what.Bits\r
3863 \r
3864 typedef enum {        /* waiting for "i:"=input, "o:"=output, "x:"=nothing */\r
3865       START,    /* x: set up for LEN */\r
3866       LEN,      /* i: get length/literal/eob next */\r
3867       LENEXT,   /* i: getting length extra (have base) */\r
3868       DIST,     /* i: get distance next */\r
3869       DISTEXT,  /* i: getting distance extra */\r
3870       COPY,     /* o: copying bytes in window, waiting for space */\r
3871       LIT,      /* o: got literal, waiting for output space */\r
3872       WASH,     /* o: got eob, possibly still output waiting */\r
3873       END,      /* x: got eob and all data flushed */\r
3874       BADCODE}  /* x: got error */\r
3875 inflate_codes_mode;\r
3876 \r
3877 /* inflate codes private state */\r
3878 struct inflate_codes_state {\r
3879 \r
3880   /* mode */\r
3881   inflate_codes_mode mode;      /* current inflate_codes mode */\r
3882 \r
3883   /* mode dependent information */\r
3884   uInt len;\r
3885   union {\r
3886     struct {\r
3887       inflate_huft *tree;       /* pointer into tree */\r
3888       uInt need;                /* bits needed */\r
3889     } code;             /* if LEN or DIST, where in tree */\r
3890     uInt lit;           /* if LIT, literal */\r
3891     struct {\r
3892       uInt get;                 /* bits to get for extra */\r
3893       uInt dist;                /* distance back to copy from */\r
3894     } copy;             /* if EXT or COPY, where and how much */\r
3895   } sub;                /* submode */\r
3896 \r
3897   /* mode independent information */\r
3898   Byte lbits;           /* ltree bits decoded per branch */\r
3899   Byte dbits;           /* dtree bits decoder per branch */\r
3900   inflate_huft *ltree;          /* literal/length/eob tree */\r
3901   inflate_huft *dtree;          /* distance tree */\r
3902 \r
3903 };\r
3904 \r
3905 \r
3906 inflate_codes_statef *inflate_codes_new(uInt bl, uInt bd, inflate_huft *tl, inflate_huft *td, z_streamp z)\r
3907 {\r
3908   inflate_codes_statef *c;\r
3909 \r
3910   if ((c = (inflate_codes_statef *)\r
3911        ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)\r
3912   {\r
3913     c->mode = START;\r
3914     c->lbits = (Byte)bl;\r
3915     c->dbits = (Byte)bd;\r
3916     c->ltree = tl;\r
3917     c->dtree = td;\r
3918     Tracev(("inflate:       codes new\n"));\r
3919   }\r
3920   return c;\r
3921 }\r
3922 \r
3923 \r
3924 int inflate_codes(inflate_blocks_statef *s, z_streamp z, int r)\r
3925 {\r
3926   uInt j;               /* temporary storage */\r
3927   inflate_huft *t;      /* temporary pointer */\r
3928   uInt e;               /* extra bits or operation */\r
3929   uLong b;              /* bit buffer */\r
3930   uInt k;               /* bits in bit buffer */\r
3931   Byte *p;             /* input data pointer */\r
3932   uInt n;               /* bytes available there */\r
3933   Byte *q;             /* output window write pointer */\r
3934   uInt m;               /* bytes to end of window or read pointer */\r
3935   Byte *f;             /* pointer to copy strings from */\r
3936   inflate_codes_statef *c = s->sub.decode.codes;  /* codes state */\r
3937 \r
3938   /* copy input/output information to locals (UPDATE macro restores) */\r
3939   LOAD\r
3940 \r
3941   /* process input and output based on current state */\r
3942   while (1) switch (c->mode)\r
3943   {             /* waiting for "i:"=input, "o:"=output, "x:"=nothing */\r
3944     case START:         /* x: set up for LEN */\r
3945 #ifndef SLOW\r
3946       if (m >= 258 && n >= 10)\r
3947       {\r
3948         UPDATE\r
3949         r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);\r
3950         LOAD\r
3951         if (r != Z_OK)\r
3952         {\r
3953           c->mode = r == Z_STREAM_END ? WASH : BADCODE;\r
3954           break;\r
3955         }\r
3956       }\r
3957 #endif /* !SLOW */\r
3958       c->sub.code.need = c->lbits;\r
3959       c->sub.code.tree = c->ltree;\r
3960       c->mode = LEN;\r
3961     case LEN:           /* i: get length/literal/eob next */\r
3962       j = c->sub.code.need;\r
3963       NEEDBITS(j)\r
3964       t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);\r
3965       DUMPBITS(t->bits)\r
3966       e = (uInt)(t->exop);\r
3967       if (e == 0)               /* literal */\r
3968       {\r
3969         c->sub.lit = t->base;\r
3970         Tracevv((t->base >= 0x20 && t->base < 0x7f ?\r
3971                  "inflate:         literal '%c'\n" :\r
3972                  "inflate:         literal 0x%02x\n", t->base));\r
3973         c->mode = LIT;\r
3974         break;\r
3975       }\r
3976       if (e & 16)               /* length */\r
3977       {\r
3978         c->sub.copy.get = e & 15;\r
3979         c->len = t->base;\r
3980         c->mode = LENEXT;\r
3981         break;\r
3982       }\r
3983       if ((e & 64) == 0)        /* next table */\r
3984       {\r
3985         c->sub.code.need = e;\r
3986         c->sub.code.tree = t + t->base;\r
3987         break;\r
3988       }\r
3989       if (e & 32)               /* end of block */\r
3990       {\r
3991         Tracevv(("inflate:         end of block\n"));\r
3992         c->mode = WASH;\r
3993         break;\r
3994       }\r
3995       c->mode = BADCODE;        /* invalid code */\r
3996       z->msg = (char*)"invalid literal/length code";\r
3997       r = Z_DATA_ERROR;\r
3998       LEAVE\r
3999     case LENEXT:        /* i: getting length extra (have base) */\r
4000       j = c->sub.copy.get;\r
4001       NEEDBITS(j)\r
4002       c->len += (uInt)b & inflate_mask[j];\r
4003       DUMPBITS(j)\r
4004       c->sub.code.need = c->dbits;\r
4005       c->sub.code.tree = c->dtree;\r
4006       Tracevv(("inflate:         length %u\n", c->len));\r
4007       c->mode = DIST;\r
4008     case DIST:          /* i: get distance next */\r
4009       j = c->sub.code.need;\r
4010       NEEDBITS(j)\r
4011       t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);\r
4012       DUMPBITS(t->bits)\r
4013       e = (uInt)(t->exop);\r
4014       if (e & 16)               /* distance */\r
4015       {\r
4016         c->sub.copy.get = e & 15;\r
4017         c->sub.copy.dist = t->base;\r
4018         c->mode = DISTEXT;\r
4019         break;\r
4020       }\r
4021       if ((e & 64) == 0)        /* next table */\r
4022       {\r
4023         c->sub.code.need = e;\r
4024         c->sub.code.tree = t + t->base;\r
4025         break;\r
4026       }\r
4027       c->mode = BADCODE;        /* invalid code */\r
4028       z->msg = (char*)"invalid distance code";\r
4029       r = Z_DATA_ERROR;\r
4030       LEAVE\r
4031     case DISTEXT:       /* i: getting distance extra */\r
4032       j = c->sub.copy.get;\r
4033       NEEDBITS(j)\r
4034       c->sub.copy.dist += (uInt)b & inflate_mask[j];\r
4035       DUMPBITS(j)\r
4036       Tracevv(("inflate:         distance %u\n", c->sub.copy.dist));\r
4037       c->mode = COPY;\r
4038     case COPY:          /* o: copying bytes in window, waiting for space */\r
4039 #ifndef __TURBOC__ /* Turbo C bug for following expression */\r
4040       f = (uInt)(q - s->window) < c->sub.copy.dist ?\r
4041           s->end - (c->sub.copy.dist - (q - s->window)) :\r
4042           q - c->sub.copy.dist;\r
4043 #else\r
4044       f = q - c->sub.copy.dist;\r
4045       if ((uInt)(q - s->window) < c->sub.copy.dist)\r
4046         f = s->end - (c->sub.copy.dist - (uInt)(q - s->window));\r
4047 #endif\r
4048       while (c->len)\r
4049       {\r
4050         NEEDOUT\r
4051         OUTBYTE(*f++)\r
4052         if (f == s->end)\r
4053           f = s->window;\r
4054         c->len--;\r
4055       }\r
4056       c->mode = START;\r
4057       break;\r
4058     case LIT:           /* o: got literal, waiting for output space */\r
4059       NEEDOUT\r
4060       OUTBYTE(c->sub.lit)\r
4061       c->mode = START;\r
4062       break;\r
4063     case WASH:          /* o: got eob, possibly more output */\r
4064       if (k > 7)        /* return unused byte, if any */\r
4065       {\r
4066         Assert(k < 16, "inflate_codes grabbed too many bytes")\r
4067         k -= 8;\r
4068         n++;\r
4069         p--;            /* can always return one */\r
4070       }\r
4071       FLUSH\r
4072       if (s->read != s->write)\r
4073         LEAVE\r
4074       c->mode = END;\r
4075     case END:\r
4076       r = Z_STREAM_END;\r
4077       LEAVE\r
4078     case BADCODE:       /* x: got error */\r
4079       r = Z_DATA_ERROR;\r
4080       LEAVE\r
4081     default:\r
4082       r = Z_STREAM_ERROR;\r
4083       LEAVE\r
4084   }\r
4085 #ifdef NEED_DUMMY_RETURN\r
4086   return Z_STREAM_ERROR;  /* Some dumb compilers complain without this */\r
4087 #endif\r
4088 }\r
4089 \r
4090 \r
4091 void inflate_codes_free(inflate_codes_statef *c, z_streamp z)\r
4092 {\r
4093   ZFREE(z, c);\r
4094   Tracev(("inflate:       codes free\n"));\r
4095 }\r
4096 \r
4097 /* adler32.c -- compute the Adler-32 checksum of a data stream\r
4098  * Copyright (C) 1995-1998 Mark Adler\r
4099  * For conditions of distribution and use, see copyright notice in zlib.h \r
4100  */\r
4101 \r
4102 #define BASE 65521L /* largest prime smaller than 65536 */\r
4103 #define NMAX 5552\r
4104 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */\r
4105 \r
4106 #undef DO1\r
4107 #undef DO2\r
4108 #undef DO4\r
4109 #undef DO8\r
4110 \r
4111 #define DO1(buf,i)  {s1 += buf[i]; s2 += s1;}\r
4112 #define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);\r
4113 #define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);\r
4114 #define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);\r
4115 #define DO16(buf)   DO8(buf,0); DO8(buf,8);\r
4116 \r
4117 /* ========================================================================= */\r
4118 uLong adler32(uLong adler, const Byte *buf, uInt len)\r
4119 {\r
4120     unsigned long s1 = adler & 0xffff;\r
4121     unsigned long s2 = (adler >> 16) & 0xffff;\r
4122     int k;\r
4123 \r
4124     if (buf == Z_NULL) return 1L;\r
4125 \r
4126     while (len > 0) {\r
4127         k = len < NMAX ? len : NMAX;\r
4128         len -= k;\r
4129         while (k >= 16) {\r
4130             DO16(buf);\r
4131             buf += 16;\r
4132             k -= 16;\r
4133         }\r
4134         if (k != 0) do {\r
4135             s1 += *buf++;\r
4136             s2 += s1;\r
4137         } while (--k);\r
4138         s1 %= BASE;\r
4139         s2 %= BASE;\r
4140     }\r
4141     return (s2 << 16) | s1;\r
4142 }\r
4143 \r
4144 /* infblock.h -- header to use infblock.c\r
4145  * Copyright (C) 1995-1998 Mark Adler\r
4146  * For conditions of distribution and use, see copyright notice in zlib.h \r
4147  */\r
4148 \r
4149 /* WARNING: this file should *not* be used by applications. It is\r
4150    part of the implementation of the compression library and is\r
4151    subject to change. Applications should only use zlib.h.\r
4152  */\r
4153 \r
4154 extern inflate_blocks_statef * inflate_blocks_new OF((\r
4155     z_streamp z,\r
4156     check_func c,               /* check function */\r
4157     uInt w));                   /* window size */\r
4158 \r
4159 extern int inflate_blocks OF((\r
4160     inflate_blocks_statef *,\r
4161     z_streamp ,\r
4162     int));                      /* initial return code */\r
4163 \r
4164 extern void inflate_blocks_reset OF((\r
4165     inflate_blocks_statef *,\r
4166     z_streamp ,\r
4167     uLong *));                  /* check value on output */\r
4168 \r
4169 extern int inflate_blocks_free OF((\r
4170     inflate_blocks_statef *,\r
4171     z_streamp));\r
4172 \r
4173 extern void inflate_set_dictionary OF((\r
4174     inflate_blocks_statef *s,\r
4175     const Byte *d,  /* dictionary */\r
4176     uInt  n));       /* dictionary length */\r
4177 \r
4178 extern int inflate_blocks_sync_point OF((\r
4179     inflate_blocks_statef *s));\r
4180 \r
4181 typedef enum {\r
4182       imMETHOD,   /* waiting for method byte */\r
4183       imFLAG,     /* waiting for flag byte */\r
4184       imDICT4,    /* four dictionary check bytes to go */\r
4185       imDICT3,    /* three dictionary check bytes to go */\r
4186       imDICT2,    /* two dictionary check bytes to go */\r
4187       imDICT1,    /* one dictionary check byte to go */\r
4188       imDICT0,    /* waiting for inflateSetDictionary */\r
4189       imBLOCKS,   /* decompressing blocks */\r
4190       imCHECK4,   /* four check bytes to go */\r
4191       imCHECK3,   /* three check bytes to go */\r
4192       imCHECK2,   /* two check bytes to go */\r
4193       imCHECK1,   /* one check byte to go */\r
4194       imDONE,     /* finished check, done */\r
4195       imBAD}      /* got an error--stay here */\r
4196 inflate_mode;\r
4197 \r
4198 /* inflate private state */\r
4199 struct internal_state {\r
4200 \r
4201   /* mode */\r
4202   inflate_mode  mode;   /* current inflate mode */\r
4203 \r
4204   /* mode dependent information */\r
4205   union {\r
4206     uInt method;        /* if FLAGS, method byte */\r
4207     struct {\r
4208       uLong was;                /* computed check value */\r
4209       uLong need;               /* stream check value */\r
4210     } check;            /* if CHECK, check values to compare */\r
4211     uInt marker;        /* if BAD, inflateSync's marker bytes count */\r
4212   } sub;        /* submode */\r
4213 \r
4214   /* mode independent information */\r
4215   int  nowrap;          /* flag for no wrapper */\r
4216   uInt wbits;           /* log2(window size)  (8..15, defaults to 15) */\r
4217   inflate_blocks_statef \r
4218     *blocks;            /* current inflate_blocks state */\r
4219 \r
4220 };\r
4221 \r
4222 \r
4223 int inflateReset(z_streamp z)\r
4224 {\r
4225   if (z == Z_NULL || z->state == Z_NULL)\r
4226     return Z_STREAM_ERROR;\r
4227   z->total_in = z->total_out = 0;\r
4228   z->msg = Z_NULL;\r
4229   z->state->mode = z->state->nowrap ? imBLOCKS : imMETHOD;\r
4230   inflate_blocks_reset(z->state->blocks, z, Z_NULL);\r
4231   Tracev(("inflate: reset\n"));\r
4232   return Z_OK;\r
4233 }\r
4234 \r
4235 \r
4236 int inflateEnd(z_streamp z)\r
4237 {\r
4238   if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)\r
4239     return Z_STREAM_ERROR;\r
4240   if (z->state->blocks != Z_NULL)\r
4241     inflate_blocks_free(z->state->blocks, z);\r
4242   ZFREE(z, z->state);\r
4243   z->state = Z_NULL;\r
4244   Tracev(("inflate: end\n"));\r
4245   return Z_OK;\r
4246 }\r
4247 \r
4248 \r
4249 \r
4250 int inflateInit2_(z_streamp z, int w, const char *version, int stream_size)\r
4251 {\r
4252   if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||\r
4253       stream_size != sizeof(z_stream))\r
4254       return Z_VERSION_ERROR;\r
4255 \r
4256   /* initialize state */\r
4257   if (z == Z_NULL)\r
4258     return Z_STREAM_ERROR;\r
4259   z->msg = Z_NULL;\r
4260   if (z->zalloc == Z_NULL)\r
4261   {\r
4262     z->zalloc = (void *(*)(void *, unsigned, unsigned))zcalloc;\r
4263     z->opaque = (voidp)0;\r
4264   }\r
4265   if (z->zfree == Z_NULL) z->zfree = (void (*)(void *, void *))zcfree;\r
4266   if ((z->state = (struct internal_state *)\r
4267        ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)\r
4268     return Z_MEM_ERROR;\r
4269   z->state->blocks = Z_NULL;\r
4270 \r
4271   /* handle undocumented nowrap option (no zlib header or check) */\r
4272   z->state->nowrap = 0;\r
4273   if (w < 0)\r
4274   {\r
4275     w = - w;\r
4276     z->state->nowrap = 1;\r
4277   }\r
4278 \r
4279   /* set window size */\r
4280   if (w < 8 || w > 15)\r
4281   {\r
4282     inflateEnd(z);\r
4283     return Z_STREAM_ERROR;\r
4284   }\r
4285   z->state->wbits = (uInt)w;\r
4286 \r
4287   /* create inflate_blocks state */\r
4288   if ((z->state->blocks =\r
4289       inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))\r
4290       == Z_NULL)\r
4291   {\r
4292     inflateEnd(z);\r
4293     return Z_MEM_ERROR;\r
4294   }\r
4295   Tracev(("inflate: allocated\n"));\r
4296 \r
4297   /* reset state */\r
4298   inflateReset(z);\r
4299   return Z_OK;\r
4300 }\r
4301 \r
4302 \r
4303 int inflateInit_(z_streamp z, const char *version, int stream_size)\r
4304 {\r
4305   return inflateInit2_(z, DEF_WBITS, version, stream_size);\r
4306 }\r
4307 \r
4308 \r
4309 #define iNEEDBYTE {if(z->avail_in==0)return r;r=f;}\r
4310 #define iNEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)\r
4311 \r
4312 int inflate(z_streamp z, int f)\r
4313 {\r
4314   int r;\r
4315   uInt b;\r
4316 \r
4317   if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)\r
4318     return Z_STREAM_ERROR;\r
4319   f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;\r
4320   r = Z_BUF_ERROR;\r
4321   while (1) switch (z->state->mode)\r
4322   {\r
4323     case imMETHOD:\r
4324       iNEEDBYTE\r
4325       if (((z->state->sub.method = iNEXTBYTE) & 0xf) != Z_DEFLATED)\r
4326       {\r
4327         z->state->mode = imBAD;\r
4328         z->msg = (char*)"unknown compression method";\r
4329         z->state->sub.marker = 5;       /* can't try inflateSync */\r
4330         break;\r
4331       }\r
4332       if ((z->state->sub.method >> 4) + 8 > z->state->wbits)\r
4333       {\r
4334         z->state->mode = imBAD;\r
4335         z->msg = (char*)"invalid window size";\r
4336         z->state->sub.marker = 5;       /* can't try inflateSync */\r
4337         break;\r
4338       }\r
4339       z->state->mode = imFLAG;\r
4340     case imFLAG:\r
4341       iNEEDBYTE\r
4342       b = iNEXTBYTE;\r
4343       if (((z->state->sub.method << 8) + b) % 31)\r
4344       {\r
4345         z->state->mode = imBAD;\r
4346         z->msg = (char*)"incorrect header check";\r
4347         z->state->sub.marker = 5;       /* can't try inflateSync */\r
4348         break;\r
4349       }\r
4350       Tracev(("inflate: zlib header ok\n"));\r
4351       if (!(b & PRESET_DICT))\r
4352       {\r
4353         z->state->mode = imBLOCKS;\r
4354         break;\r
4355       }\r
4356       z->state->mode = imDICT4;\r
4357     case imDICT4:\r
4358       iNEEDBYTE\r
4359       z->state->sub.check.need = (uLong)iNEXTBYTE << 24;\r
4360       z->state->mode = imDICT3;\r
4361     case imDICT3:\r
4362       iNEEDBYTE\r
4363       z->state->sub.check.need += (uLong)iNEXTBYTE << 16;\r
4364       z->state->mode = imDICT2;\r
4365     case imDICT2:\r
4366       iNEEDBYTE\r
4367       z->state->sub.check.need += (uLong)iNEXTBYTE << 8;\r
4368       z->state->mode = imDICT1;\r
4369     case imDICT1:\r
4370       iNEEDBYTE\r
4371       z->state->sub.check.need += (uLong)iNEXTBYTE;\r
4372       z->adler = z->state->sub.check.need;\r
4373       z->state->mode = imDICT0;\r
4374       return Z_NEED_DICT;\r
4375     case imDICT0:\r
4376       z->state->mode = imBAD;\r
4377       z->msg = (char*)"need dictionary";\r
4378       z->state->sub.marker = 0;       /* can try inflateSync */\r
4379       return Z_STREAM_ERROR;\r
4380     case imBLOCKS:\r
4381       r = inflate_blocks(z->state->blocks, z, r);\r
4382       if (r == Z_DATA_ERROR)\r
4383       {\r
4384         z->state->mode = imBAD;\r
4385         z->state->sub.marker = 0;       /* can try inflateSync */\r
4386         break;\r
4387       }\r
4388       if (r == Z_OK)\r
4389         r = f;\r
4390       if (r != Z_STREAM_END)\r
4391         return r;\r
4392       r = f;\r
4393       inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);\r
4394       if (z->state->nowrap)\r
4395       {\r
4396         z->state->mode = imDONE;\r
4397         break;\r
4398       }\r
4399       z->state->mode = imCHECK4;\r
4400     case imCHECK4:\r
4401       iNEEDBYTE\r
4402       z->state->sub.check.need = (uLong)iNEXTBYTE << 24;\r
4403       z->state->mode = imCHECK3;\r
4404     case imCHECK3:\r
4405       iNEEDBYTE\r
4406       z->state->sub.check.need += (uLong)iNEXTBYTE << 16;\r
4407       z->state->mode = imCHECK2;\r
4408     case imCHECK2:\r
4409       iNEEDBYTE\r
4410       z->state->sub.check.need += (uLong)iNEXTBYTE << 8;\r
4411       z->state->mode = imCHECK1;\r
4412     case imCHECK1:\r
4413       iNEEDBYTE\r
4414       z->state->sub.check.need += (uLong)iNEXTBYTE;\r
4415 \r
4416       if (z->state->sub.check.was != z->state->sub.check.need)\r
4417       {\r
4418         z->state->mode = imBAD;\r
4419         z->msg = (char*)"incorrect data check";\r
4420         z->state->sub.marker = 5;       /* can't try inflateSync */\r
4421         break;\r
4422       }\r
4423       Tracev(("inflate: zlib check ok\n"));\r
4424       z->state->mode = imDONE;\r
4425     case imDONE:\r
4426       return Z_STREAM_END;\r
4427     case imBAD:\r
4428       return Z_DATA_ERROR;\r
4429     default:\r
4430       return Z_STREAM_ERROR;\r
4431   }\r
4432 #ifdef NEED_DUMMY_RETURN\r
4433   return Z_STREAM_ERROR;  /* Some dumb compilers complain without this */\r
4434 #endif\r
4435 }\r
4436 \r
4437 \r
4438 int inflateSetDictionary(z_streamp z, const Byte *dictionary, uInt dictLength)\r
4439 {\r
4440   uInt length = dictLength;\r
4441 \r
4442   if (z == Z_NULL || z->state == Z_NULL || z->state->mode != imDICT0)\r
4443     return Z_STREAM_ERROR;\r
4444 \r
4445   if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;\r
4446   z->adler = 1L;\r
4447 \r
4448   if (length >= ((uInt)1<<z->state->wbits))\r
4449   {\r
4450     length = (1<<z->state->wbits)-1;\r
4451     dictionary += dictLength - length;\r
4452   }\r
4453   inflate_set_dictionary(z->state->blocks, dictionary, length);\r
4454   z->state->mode = imBLOCKS;\r
4455   return Z_OK;\r
4456 }\r
4457 \r
4458 \r
4459 int inflateSync(z_streamp z)\r
4460 {\r
4461   uInt n;       /* number of bytes to look at */\r
4462   Byte *p;     /* pointer to bytes */\r
4463   uInt m;       /* number of marker bytes found in a row */\r
4464   uLong r, w;   /* temporaries to save total_in and total_out */\r
4465 \r
4466   /* set up */\r
4467   if (z == Z_NULL || z->state == Z_NULL)\r
4468     return Z_STREAM_ERROR;\r
4469   if (z->state->mode != imBAD)\r
4470   {\r
4471     z->state->mode = imBAD;\r
4472     z->state->sub.marker = 0;\r
4473   }\r
4474   if ((n = z->avail_in) == 0)\r
4475     return Z_BUF_ERROR;\r
4476   p = z->next_in;\r
4477   m = z->state->sub.marker;\r
4478 \r
4479   /* search */\r
4480   while (n && m < 4)\r
4481   {\r
4482     static const Byte mark[4] = {0, 0, 0xff, 0xff};\r
4483     if (*p == mark[m])\r
4484       m++;\r
4485     else if (*p)\r
4486       m = 0;\r
4487     else\r
4488       m = 4 - m;\r
4489     p++, n--;\r
4490   }\r
4491 \r
4492   /* restore */\r
4493   z->total_in += p - z->next_in;\r
4494   z->next_in = p;\r
4495   z->avail_in = n;\r
4496   z->state->sub.marker = m;\r
4497 \r
4498   /* return no joy or set up to restart on a new block */\r
4499   if (m != 4)\r
4500     return Z_DATA_ERROR;\r
4501   r = z->total_in;  w = z->total_out;\r
4502   inflateReset(z);\r
4503   z->total_in = r;  z->total_out = w;\r
4504   z->state->mode = imBLOCKS;\r
4505   return Z_OK;\r
4506 }\r
4507 \r
4508 \r
4509 /* Returns true if inflate is currently at the end of a block generated\r
4510  * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP\r
4511  * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH\r
4512  * but removes the length bytes of the resulting empty stored block. When\r
4513  * decompressing, PPP checks that at the end of input packet, inflate is\r
4514  * waiting for these length bytes.\r
4515  */\r
4516 int inflateSyncPoint(z_streamp z)\r
4517 {\r
4518   if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)\r
4519     return Z_STREAM_ERROR;\r
4520   return inflate_blocks_sync_point(z->state->blocks);\r
4521 }\r
4522 \r
4523 voidp zcalloc (voidp opaque, unsigned items, unsigned size)\r
4524 {\r
4525     if (opaque) items += size - size; /* make compiler happy */\r
4526     return (voidp)malloc(items*size);\r
4527 }\r
4528 \r
4529 void  zcfree (voidp opaque, voidp ptr)\r
4530 {\r
4531     free(ptr);\r
4532     if (opaque) return; /* make compiler happy */\r
4533 }\r
4534 \r