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