Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

unzip.cpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * name:        unzip.c
00003  *
00004  * desc:        IO on .zip files using portions of zlib 
00005  *
00006  * $Archive: /source/code/qcommon/unzip.c $
00007  * $Author: ttimo $ 
00008  * $Revision: 1.1.1.3 $
00009  * $Modtime: 10/19/99 3:59p $
00010  * $Date: 2000/01/11 16:37:27 $
00011  *
00012  *****************************************************************************/
00013 
00014 #include <stdio.h>
00015 #include <string.h>
00016 #include <windows.h>
00017 #include "unzip.h"
00018 //#include "cmdlib.h"
00019 
00020 /* unzip.h -- IO for uncompress .zip files using zlib 
00021    Version 0.15 beta, Mar 19th, 1998,
00022 
00023    Copyright (C) 1998 Gilles Vollant
00024 
00025    This unzip package allow extract file from .ZIP file, compatible with PKZip 2.04g
00026      WinZip, InfoZip tools and compatible.
00027    Encryption and multi volume ZipFile (span) are not supported.
00028    Old compressions used by old PKZip 1.x are not supported
00029 
00030    THIS IS AN ALPHA VERSION. AT THIS STAGE OF DEVELOPPEMENT, SOMES API OR STRUCTURE
00031    CAN CHANGE IN FUTURE VERSION !!
00032    I WAIT FEEDBACK at mail info@winimage.com
00033    Visit also http://www.winimage.com/zLibDll/unzip.htm for evolution
00034 
00035    Condition of use and distribution are the same than zlib :
00036 
00037   This software is provided 'as-is', without any express or implied
00038   warranty.  In no event will the authors be held liable for any damages
00039   arising from the use of this software.
00040 
00041   Permission is granted to anyone to use this software for any purpose,
00042   including commercial applications, and to alter it and redistribute it
00043   freely, subject to the following restrictions:
00044 
00045   1. The origin of this software must not be misrepresented; you must not
00046      claim that you wrote the original software. If you use this software
00047      in a product, an acknowledgment in the product documentation would be
00048      appreciated but is not required.
00049   2. Altered source versions must be plainly marked as such, and must not be
00050      misrepresented as being the original software.
00051   3. This notice may not be removed or altered from any source distribution.
00052 
00053 
00054 */
00055 /* for more info about .ZIP format, see 
00056       ftp://ftp.cdrom.com/pub/infozip/doc/appnote-970311-iz.zip
00057    PkWare has also a specification at :
00058       ftp://ftp.pkware.com/probdesc.zip */
00059 
00060 /* zlib.h -- interface of the 'zlib' general purpose compression library
00061   version 1.1.3, July 9th, 1998
00062 
00063   Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
00064 
00065   This software is provided 'as-is', without any express or implied
00066   warranty.  In no event will the authors be held liable for any damages
00067   arising from the use of this software.
00068 
00069   Permission is granted to anyone to use this software for any purpose,
00070   including commercial applications, and to alter it and redistribute it
00071   freely, subject to the following restrictions:
00072 
00073   1. The origin of this software must not be misrepresented; you must not
00074      claim that you wrote the original software. If you use this software
00075      in a product, an acknowledgment in the product documentation would be
00076      appreciated but is not required.
00077   2. Altered source versions must be plainly marked as such, and must not be
00078      misrepresented as being the original software.
00079   3. This notice may not be removed or altered from any source distribution.
00080 
00081   Jean-loup Gailly        Mark Adler
00082   jloup@gzip.org          madler@alumni.caltech.edu
00083 
00084 
00085   The data format used by the zlib library is described by RFCs (Request for
00086   Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
00087   (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
00088 */
00089 
00090 /* zconf.h -- configuration of the zlib compression library
00091  * Copyright (C) 1995-1998 Jean-loup Gailly.
00092  * For conditions of distribution and use, see copyright notice in zlib.h 
00093  */
00094 
00095 /* @(#) $Id: unzip.cpp,v 1.1.1.3 2000/01/11 16:37:27 ttimo Exp $ */
00096 
00097 #ifndef _ZCONF_H
00098 #define _ZCONF_H
00099 
00100 /* Maximum value for memLevel in deflateInit2 */
00101 #ifndef MAX_MEM_LEVEL
00102 #  ifdef MAXSEG_64K
00103 #    define MAX_MEM_LEVEL 8
00104 #  else
00105 #    define MAX_MEM_LEVEL 9
00106 #  endif
00107 #endif
00108 
00109 /* Maximum value for windowBits in deflateInit2 and inflateInit2.
00110  * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
00111  * created by gzip. (Files created by minigzip can still be extracted by
00112  * gzip.)
00113  */
00114 #ifndef MAX_WBITS
00115 #  define MAX_WBITS   15 /* 32K LZ77 window */
00116 #endif
00117 
00118 /* The memory requirements for deflate are (in bytes):
00119             (1 << (windowBits+2)) +  (1 << (memLevel+9))
00120  that is: 128K for windowBits=15  +  128K for memLevel = 8  (default values)
00121  plus a few kilobytes for small objects. For example, if you want to reduce
00122  the default memory requirements from 256K to 128K, compile with
00123      make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
00124  Of course this will generally degrade compression (there's no free lunch).
00125 
00126    The memory requirements for inflate are (in bytes) 1 << windowBits
00127  that is, 32K for windowBits=15 (default value) plus a few kilobytes
00128  for small objects.
00129 */
00130 
00131                         /* Type declarations */
00132 
00133 #ifndef OF /* function prototypes */
00134 #define OF(args)  args
00135 #endif
00136 
00137 typedef unsigned char  Byte;  /* 8 bits */
00138 typedef unsigned int   uInt;  /* 16 bits or more */
00139 typedef unsigned long  uLong; /* 32 bits or more */
00140 typedef Byte    *voidp;
00141 
00142 #ifndef SEEK_SET
00143 #  define SEEK_SET        0       /* Seek from beginning of file.  */
00144 #  define SEEK_CUR        1       /* Seek from current position.  */
00145 #  define SEEK_END        2       /* Set file pointer to EOF plus "offset" */
00146 #endif
00147 
00148 #endif /* _ZCONF_H */
00149 
00150 #define ZLIB_VERSION "1.1.3"
00151 
00152 /* 
00153      The 'zlib' compression library provides in-memory compression and
00154   decompression functions, including integrity checks of the uncompressed
00155   data.  This version of the library supports only one compression method
00156   (deflation) but other algorithms will be added later and will have the same
00157   stream interface.
00158 
00159      Compression can be done in a single step if the buffers are large
00160   enough (for example if an input file is mmap'ed), or can be done by
00161   repeated calls of the compression function.  In the latter case, the
00162   application must provide more input and/or consume the output
00163   (providing more output space) before each call.
00164 
00165      The library also supports reading and writing files in gzip (.gz) format
00166   with an interface similar to that of stdio.
00167 
00168      The library does not install any signal handler. The decoder checks
00169   the consistency of the compressed data, so the library should never
00170   crash even in case of corrupted input.
00171 */
00172 
00173 /*
00174    The application must update next_in and avail_in when avail_in has
00175    dropped to zero. It must update next_out and avail_out when avail_out
00176    has dropped to zero. The application must initialize zalloc, zfree and
00177    opaque before calling the init function. All other fields are set by the
00178    compression library and must not be updated by the application.
00179 
00180    The opaque value provided by the application will be passed as the first
00181    parameter for calls of zalloc and zfree. This can be useful for custom
00182    memory management. The compression library attaches no meaning to the
00183    opaque value.
00184 
00185    zalloc must return Z_NULL if there is not enough memory for the object.
00186    If zlib is used in a multi-threaded application, zalloc and zfree must be
00187    thread safe.
00188 
00189    On 16-bit systems, the functions zalloc and zfree must be able to allocate
00190    exactly 65536 bytes, but will not be required to allocate more than this
00191    if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
00192    pointers returned by zalloc for objects of exactly 65536 bytes *must*
00193    have their offset normalized to zero. The default allocation function
00194    provided by this library ensures this (see zutil.c). To reduce memory
00195    requirements and avoid any allocation of 64K objects, at the expense of
00196    compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
00197 
00198    The fields total_in and total_out can be used for statistics or
00199    progress reports. After compression, total_in holds the total size of
00200    the uncompressed data and may be saved for use in the decompressor
00201    (particularly if the decompressor wants to decompress everything in
00202    a single step).
00203 */
00204 
00205                         /* constants */
00206 
00207 #define Z_NO_FLUSH      0
00208 #define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
00209 #define Z_SYNC_FLUSH    2
00210 #define Z_FULL_FLUSH    3
00211 #define Z_FINISH        4
00212 /* Allowed flush values; see deflate() below for details */
00213 
00214 #define Z_OK            0
00215 #define Z_STREAM_END    1
00216 #define Z_NEED_DICT     2
00217 #define Z_ERRNO        (-1)
00218 #define Z_STREAM_ERROR (-2)
00219 #define Z_DATA_ERROR   (-3)
00220 #define Z_MEM_ERROR    (-4)
00221 #define Z_BUF_ERROR    (-5)
00222 #define Z_VERSION_ERROR (-6)
00223 /* Return codes for the compression/decompression functions. Negative
00224  * values are errors, positive values are used for special but normal events.
00225  */
00226 
00227 #define Z_NO_COMPRESSION         0
00228 #define Z_BEST_SPEED             1
00229 #define Z_BEST_COMPRESSION       9
00230 #define Z_DEFAULT_COMPRESSION  (-1)
00231 /* compression levels */
00232 
00233 #define Z_FILTERED            1
00234 #define Z_HUFFMAN_ONLY        2
00235 #define Z_DEFAULT_STRATEGY    0
00236 /* compression strategy; see deflateInit2() below for details */
00237 
00238 #define Z_BINARY   0
00239 #define Z_ASCII    1
00240 #define Z_UNKNOWN  2
00241 /* Possible values of the data_type field */
00242 
00243 #define Z_DEFLATED   8
00244 /* The deflate compression method (the only one supported in this version) */
00245 
00246 #define Z_NULL  0  /* for initializing zalloc, zfree, opaque */
00247 
00248 #define zlib_version zlibVersion()
00249 /* for compatibility with versions < 1.0.2 */
00250 
00251                         /* basic functions */
00252 
00253 const char * zlibVersion OF((void));
00254 /* The application can compare zlibVersion and ZLIB_VERSION for consistency.
00255    If the first character differs, the library code actually used is
00256    not compatible with the zlib.h header file used by the application.
00257    This check is automatically made by deflateInit and inflateInit.
00258  */
00259 
00260 /* 
00261 int deflateInit OF((z_streamp strm, int level));
00262 
00263      Initializes the internal stream state for compression. The fields
00264    zalloc, zfree and opaque must be initialized before by the caller.
00265    If zalloc and zfree are set to Z_NULL, deflateInit updates them to
00266    use default allocation functions.
00267 
00268      The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
00269    1 gives best speed, 9 gives best compression, 0 gives no compression at
00270    all (the input data is simply copied a block at a time).
00271    Z_DEFAULT_COMPRESSION requests a default compromise between speed and
00272    compression (currently equivalent to level 6).
00273 
00274      deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
00275    enough memory, Z_STREAM_ERROR if level is not a valid compression level,
00276    Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
00277    with the version assumed by the caller (ZLIB_VERSION).
00278    msg is set to null if there is no error message.  deflateInit does not
00279    perform any compression: this will be done by deflate().
00280 */
00281 
00282 
00283 int deflate OF((z_streamp strm, int flush));
00284 /*
00285     deflate compresses as much data as possible, and stops when the input
00286   buffer becomes empty or the output buffer becomes full. It may introduce some
00287   output latency (reading input without producing any output) except when
00288   forced to flush.
00289 
00290     The detailed semantics are as follows. deflate performs one or both of the
00291   following actions:
00292 
00293   - Compress more input starting at next_in and update next_in and avail_in
00294     accordingly. If not all input can be processed (because there is not
00295     enough room in the output buffer), next_in and avail_in are updated and
00296     processing will resume at this point for the next call of deflate().
00297 
00298   - Provide more output starting at next_out and update next_out and avail_out
00299     accordingly. This action is forced if the parameter flush is non zero.
00300     Forcing flush frequently degrades the compression ratio, so this parameter
00301     should be set only when necessary (in interactive applications).
00302     Some output may be provided even if flush is not set.
00303 
00304   Before the call of deflate(), the application should ensure that at least
00305   one of the actions is possible, by providing more input and/or consuming
00306   more output, and updating avail_in or avail_out accordingly; avail_out
00307   should never be zero before the call. The application can consume the
00308   compressed output when it wants, for example when the output buffer is full
00309   (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
00310   and with zero avail_out, it must be called again after making room in the
00311   output buffer because there might be more output pending.
00312 
00313     If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
00314   flushed to the output buffer and the output is aligned on a byte boundary, so
00315   that the decompressor can get all input data available so far. (In particular
00316   avail_in is zero after the call if enough output space has been provided
00317   before the call.)  Flushing may degrade compression for some compression
00318   algorithms and so it should be used only when necessary.
00319 
00320     If flush is set to Z_FULL_FLUSH, all output is flushed as with
00321   Z_SYNC_FLUSH, and the compression state is reset so that decompression can
00322   restart from this point if previous compressed data has been damaged or if
00323   random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
00324   the compression.
00325 
00326     If deflate returns with avail_out == 0, this function must be called again
00327   with the same value of the flush parameter and more output space (updated
00328   avail_out), until the flush is complete (deflate returns with non-zero
00329   avail_out).
00330 
00331     If the parameter flush is set to Z_FINISH, pending input is processed,
00332   pending output is flushed and deflate returns with Z_STREAM_END if there
00333   was enough output space; if deflate returns with Z_OK, this function must be
00334   called again with Z_FINISH and more output space (updated avail_out) but no
00335   more input data, until it returns with Z_STREAM_END or an error. After
00336   deflate has returned Z_STREAM_END, the only possible operations on the
00337   stream are deflateReset or deflateEnd.
00338   
00339     Z_FINISH can be used immediately after deflateInit if all the compression
00340   is to be done in a single step. In this case, avail_out must be at least
00341   0.1% larger than avail_in plus 12 bytes.  If deflate does not return
00342   Z_STREAM_END, then it must be called again as described above.
00343 
00344     deflate() sets strm->adler to the adler32 checksum of all input read
00345   so (that is, total_in bytes).
00346 
00347     deflate() may update data_type if it can make a good guess about
00348   the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered
00349   binary. This field is only for information purposes and does not affect
00350   the compression algorithm in any manner.
00351 
00352     deflate() returns Z_OK if some progress has been made (more input
00353   processed or more output produced), Z_STREAM_END if all input has been
00354   consumed and all output has been produced (only when flush is set to
00355   Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
00356   if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
00357   (for example avail_in or avail_out was zero).
00358 */
00359 
00360 
00361 int deflateEnd OF((z_streamp strm));
00362 /*
00363      All dynamically allocated data structures for this stream are freed.
00364    This function discards any unprocessed input and does not flush any
00365    pending output.
00366 
00367      deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
00368    stream state was inconsistent, Z_DATA_ERROR if the stream was freed
00369    prematurely (some input or output was discarded). In the error case,
00370    msg may be set but then points to a static string (which must not be
00371    deallocated).
00372 */
00373 
00374 
00375 /* 
00376 int inflateInit OF((z_streamp strm));
00377 
00378      Initializes the internal stream state for decompression. The fields
00379    next_in, avail_in, zalloc, zfree and opaque must be initialized before by
00380    the caller. If next_in is not Z_NULL and avail_in is large enough (the exact
00381    value depends on the compression method), inflateInit determines the
00382    compression method from the zlib header and allocates all data structures
00383    accordingly; otherwise the allocation will be deferred to the first call of
00384    inflate.  If zalloc and zfree are set to Z_NULL, inflateInit updates them to
00385    use default allocation functions.
00386 
00387      inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
00388    memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
00389    version assumed by the caller.  msg is set to null if there is no error
00390    message. inflateInit does not perform any decompression apart from reading
00391    the zlib header if present: this will be done by inflate().  (So next_in and
00392    avail_in may be modified, but next_out and avail_out are unchanged.)
00393 */
00394 
00395 
00396 int inflate OF((z_streamp strm, int flush));
00397 /*
00398     inflate decompresses as much data as possible, and stops when the input
00399   buffer becomes empty or the output buffer becomes full. It may some
00400   introduce some output latency (reading input without producing any output)
00401   except when forced to flush.
00402 
00403   The detailed semantics are as follows. inflate performs one or both of the
00404   following actions:
00405 
00406   - Decompress more input starting at next_in and update next_in and avail_in
00407     accordingly. If not all input can be processed (because there is not
00408     enough room in the output buffer), next_in is updated and processing
00409     will resume at this point for the next call of inflate().
00410 
00411   - Provide more output starting at next_out and update next_out and avail_out
00412     accordingly.  inflate() provides as much output as possible, until there
00413     is no more input data or no more space in the output buffer (see below
00414     about the flush parameter).
00415 
00416   Before the call of inflate(), the application should ensure that at least
00417   one of the actions is possible, by providing more input and/or consuming
00418   more output, and updating the next_* and avail_* values accordingly.
00419   The application can consume the uncompressed output when it wants, for
00420   example when the output buffer is full (avail_out == 0), or after each
00421   call of inflate(). If inflate returns Z_OK and with zero avail_out, it
00422   must be called again after making room in the output buffer because there
00423   might be more output pending.
00424 
00425     If the parameter flush is set to Z_SYNC_FLUSH, inflate flushes as much
00426   output as possible to the output buffer. The flushing behavior of inflate is
00427   not specified for values of the flush parameter other than Z_SYNC_FLUSH
00428   and Z_FINISH, but the current implementation actually flushes as much output
00429   as possible anyway.
00430 
00431     inflate() should normally be called until it returns Z_STREAM_END or an
00432   error. However if all decompression is to be performed in a single step
00433   (a single call of inflate), the parameter flush should be set to
00434   Z_FINISH. In this case all pending input is processed and all pending
00435   output is flushed; avail_out must be large enough to hold all the
00436   uncompressed data. (The size of the uncompressed data may have been saved
00437   by the compressor for this purpose.) The next operation on this stream must
00438   be inflateEnd to deallocate the decompression state. The use of Z_FINISH
00439   is never required, but can be used to inform inflate that a faster routine
00440   may be used for the single inflate() call.
00441 
00442      If a preset dictionary is needed at this point (see inflateSetDictionary
00443   below), inflate sets strm-adler to the adler32 checksum of the
00444   dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise 
00445   it sets strm->adler to the adler32 checksum of all output produced
00446   so (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or
00447   an error code as described below. At the end of the stream, inflate()
00448   checks that its computed adler32 checksum is equal to that saved by the
00449   compressor and returns Z_STREAM_END only if the checksum is correct.
00450 
00451     inflate() returns Z_OK if some progress has been made (more input processed
00452   or more output produced), Z_STREAM_END if the end of the compressed data has
00453   been reached and all uncompressed output has been produced, Z_NEED_DICT if a
00454   preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
00455   corrupted (input stream not conforming to the zlib format or incorrect
00456   adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent
00457   (for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not
00458   enough memory, Z_BUF_ERROR if no progress is possible or if there was not
00459   enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR
00460   case, the application may then call inflateSync to look for a good
00461   compression block.
00462 */
00463 
00464 
00465 int inflateEnd OF((z_streamp strm));
00466 /*
00467      All dynamically allocated data structures for this stream are freed.
00468    This function discards any unprocessed input and does not flush any
00469    pending output.
00470 
00471      inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
00472    was inconsistent. In the error case, msg may be set but then points to a
00473    static string (which must not be deallocated).
00474 */
00475 
00476                         /* Advanced functions */
00477 
00478 /*
00479     The following functions are needed only in some special applications.
00480 */
00481 
00482 /*   
00483 int deflateInit2 OF((z_streamp strm,
00484                                      int  level,
00485                                      int  method,
00486                                      int  windowBits,
00487                                      int  memLevel,
00488                                      int  strategy));
00489 
00490      This is another version of deflateInit with more compression options. The
00491    fields next_in, zalloc, zfree and opaque must be initialized before by
00492    the caller.
00493 
00494      The method parameter is the compression method. It must be Z_DEFLATED in
00495    this version of the library.
00496 
00497      The windowBits parameter is the base two logarithm of the window size
00498    (the size of the history buffer).  It should be in the range 8..15 for this
00499    version of the library. Larger values of this parameter result in better
00500    compression at the expense of memory usage. The default value is 15 if
00501    deflateInit is used instead.
00502 
00503      The memLevel parameter specifies how much memory should be allocated
00504    for the internal compression state. memLevel=1 uses minimum memory but
00505    is slow and reduces compression ratio; memLevel=9 uses maximum memory
00506    for optimal speed. The default value is 8. See zconf.h for total memory
00507    usage as a function of windowBits and memLevel.
00508 
00509      The strategy parameter is used to tune the compression algorithm. Use the
00510    value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
00511    filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no
00512    string match).  Filtered data consists mostly of small values with a
00513    somewhat random distribution. In this case, the compression algorithm is
00514    tuned to compress them better. The effect of Z_FILTERED is to force more
00515    Huffman coding and less string matching; it is somewhat intermediate
00516    between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects
00517    the compression ratio but not the correctness of the compressed output even
00518    if it is not set appropriately.
00519 
00520       deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
00521    memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
00522    method). msg is set to null if there is no error message.  deflateInit2 does
00523    not perform any compression: this will be done by deflate().
00524 */
00525                             
00526 int deflateSetDictionary OF((z_streamp strm,
00527                                              const Byte *dictionary,
00528                                              uInt  dictLength));
00529 /*
00530      Initializes the compression dictionary from the given byte sequence
00531    without producing any compressed output. This function must be called
00532    immediately after deflateInit, deflateInit2 or deflateReset, before any
00533    call of deflate. The compressor and decompressor must use exactly the same
00534    dictionary (see inflateSetDictionary).
00535 
00536      The dictionary should consist of strings (byte sequences) that are likely
00537    to be encountered later in the data to be compressed, with the most commonly
00538    used strings preferably put towards the end of the dictionary. Using a
00539    dictionary is most useful when the data to be compressed is short and can be
00540    predicted with good accuracy; the data can then be compressed better than
00541    with the default empty dictionary.
00542 
00543      Depending on the size of the compression data structures selected by
00544    deflateInit or deflateInit2, a part of the dictionary may in effect be
00545    discarded, for example if the dictionary is larger than the window size in
00546    deflate or deflate2. Thus the strings most likely to be useful should be
00547    put at the end of the dictionary, not at the front.
00548 
00549      Upon return of this function, strm->adler is set to the Adler32 value
00550    of the dictionary; the decompressor may later use this value to determine
00551    which dictionary has been used by the compressor. (The Adler32 value
00552    applies to the whole dictionary even if only a subset of the dictionary is
00553    actually used by the compressor.)
00554 
00555      deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
00556    parameter is invalid (such as NULL dictionary) or the stream state is
00557    inconsistent (for example if deflate has already been called for this stream
00558    or if the compression method is bsort). deflateSetDictionary does not
00559    perform any compression: this will be done by deflate().
00560 */
00561 
00562 int deflateCopy OF((z_streamp dest,
00563                                     z_streamp source));
00564 /*
00565      Sets the destination stream as a complete copy of the source stream.
00566 
00567      This function can be useful when several compression strategies will be
00568    tried, for example when there are several ways of pre-processing the input
00569    data with a filter. The streams that will be discarded should then be freed
00570    by calling deflateEnd.  Note that deflateCopy duplicates the internal
00571    compression state which can be quite large, so this strategy is slow and
00572    can consume lots of memory.
00573 
00574      deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
00575    enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
00576    (such as zalloc being NULL). msg is left unchanged in both source and
00577    destination.
00578 */
00579 
00580 int deflateReset OF((z_streamp strm));
00581 /*
00582      This function is equivalent to deflateEnd followed by deflateInit,
00583    but does not free and reallocate all the internal compression state.
00584    The stream will keep the same compression level and any other attributes
00585    that may have been set by deflateInit2.
00586 
00587       deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
00588    stream state was inconsistent (such as zalloc or state being NULL).
00589 */
00590 
00591 int deflateParams OF((z_streamp strm,
00592                       int level,
00593                       int strategy));
00594 /*
00595      Dynamically update the compression level and compression strategy.  The
00596    interpretation of level and strategy is as in deflateInit2.  This can be
00597    used to switch between compression and straight copy of the input data, or
00598    to switch to a different kind of input data requiring a different
00599    strategy. If the compression level is changed, the input available so far
00600    is compressed with the old level (and may be flushed); the new level will
00601    take effect only at the next call of deflate().
00602 
00603      Before the call of deflateParams, the stream state must be set as for
00604    a call of deflate(), since the currently available input may have to
00605    be compressed and flushed. In particular, strm->avail_out must be non-zero.
00606 
00607      deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
00608    stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
00609    if strm->avail_out was zero.
00610 */
00611 
00612 /*   
00613 int inflateInit2 OF((z_streamp strm,
00614                                      int  windowBits));
00615 
00616      This is another version of inflateInit with an extra parameter. The
00617    fields next_in, avail_in, zalloc, zfree and opaque must be initialized
00618    before by the caller.
00619 
00620      The windowBits parameter is the base two logarithm of the maximum window
00621    size (the size of the history buffer).  It should be in the range 8..15 for
00622    this version of the library. The default value is 15 if inflateInit is used
00623    instead. If a compressed stream with a larger window size is given as
00624    input, inflate() will return with the error code Z_DATA_ERROR instead of
00625    trying to allocate a larger window.
00626 
00627       inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
00628    memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative
00629    memLevel). msg is set to null if there is no error message.  inflateInit2
00630    does not perform any decompression apart from reading the zlib header if
00631    present: this will be done by inflate(). (So next_in and avail_in may be
00632    modified, but next_out and avail_out are unchanged.)
00633 */
00634 
00635 int inflateSetDictionary OF((z_streamp strm,
00636                                              const Byte *dictionary,
00637                                              uInt  dictLength));
00638 /*
00639      Initializes the decompression dictionary from the given uncompressed byte
00640    sequence. This function must be called immediately after a call of inflate
00641    if this call returned Z_NEED_DICT. The dictionary chosen by the compressor
00642    can be determined from the Adler32 value returned by this call of
00643    inflate. The compressor and decompressor must use exactly the same
00644    dictionary (see deflateSetDictionary).
00645 
00646      inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
00647    parameter is invalid (such as NULL dictionary) or the stream state is
00648    inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
00649    expected one (incorrect Adler32 value). inflateSetDictionary does not
00650    perform any decompression: this will be done by subsequent calls of
00651    inflate().
00652 */
00653 
00654 int inflateSync OF((z_streamp strm));
00655 /* 
00656     Skips invalid compressed data until a full flush point (see above the
00657   description of deflate with Z_FULL_FLUSH) can be found, or until all
00658   available input is skipped. No output is provided.
00659 
00660     inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
00661   if no more input was provided, Z_DATA_ERROR if no flush point has been found,
00662   or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
00663   case, the application may save the current current value of total_in which
00664   indicates where valid compressed data was found. In the error case, the
00665   application may repeatedly call inflateSync, providing more input each time,
00666   until success or end of the input data.
00667 */
00668 
00669 int inflateReset OF((z_streamp strm));
00670 /*
00671      This function is equivalent to inflateEnd followed by inflateInit,
00672    but does not free and reallocate all the internal decompression state.
00673    The stream will keep attributes that may have been set by inflateInit2.
00674 
00675       inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
00676    stream state was inconsistent (such as zalloc or state being NULL).
00677 */
00678 
00679 
00680                         /* utility functions */
00681 
00682 /*
00683      The following utility functions are implemented on top of the
00684    basic stream-oriented functions. To simplify the interface, some
00685    default options are assumed (compression level and memory usage,
00686    standard memory allocation functions). The source code of these
00687    utility functions can easily be modified if you need special options.
00688 */
00689 
00690 int compress OF((Byte *dest,   uLong *destLen,
00691                                  const Byte *source, uLong sourceLen));
00692 /*
00693      Compresses the source buffer into the destination buffer.  sourceLen is
00694    the byte length of the source buffer. Upon entry, destLen is the total
00695    size of the destination buffer, which must be at least 0.1% larger than
00696    sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the
00697    compressed buffer.
00698      This function can be used to compress a whole file at once if the
00699    input file is mmap'ed.
00700      compress returns Z_OK if success, Z_MEM_ERROR if there was not
00701    enough memory, Z_BUF_ERROR if there was not enough room in the output
00702    buffer.
00703 */
00704 
00705 int compress2 OF((Byte *dest,   uLong *destLen,
00706                                   const Byte *source, uLong sourceLen,
00707                                   int level));
00708 /*
00709      Compresses the source buffer into the destination buffer. The level
00710    parameter has the same meaning as in deflateInit.  sourceLen is the byte
00711    length of the source buffer. Upon entry, destLen is the total size of the
00712    destination buffer, which must be at least 0.1% larger than sourceLen plus
00713    12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
00714 
00715      compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
00716    memory, Z_BUF_ERROR if there was not enough room in the output buffer,
00717    Z_STREAM_ERROR if the level parameter is invalid.
00718 */
00719 
00720 int uncompress OF((Byte *dest,   uLong *destLen,
00721                                    const Byte *source, uLong sourceLen));
00722 /*
00723      Decompresses the source buffer into the destination buffer.  sourceLen is
00724    the byte length of the source buffer. Upon entry, destLen is the total
00725    size of the destination buffer, which must be large enough to hold the
00726    entire uncompressed data. (The size of the uncompressed data must have
00727    been saved previously by the compressor and transmitted to the decompressor
00728    by some mechanism outside the scope of this compression library.)
00729    Upon exit, destLen is the actual size of the compressed buffer.
00730      This function can be used to decompress a whole file at once if the
00731    input file is mmap'ed.
00732 
00733      uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
00734    enough memory, Z_BUF_ERROR if there was not enough room in the output
00735    buffer, or Z_DATA_ERROR if the input data was corrupted.
00736 */
00737 
00738 
00739 typedef voidp gzFile;
00740 
00741 gzFile gzopen  OF((const char *path, const char *mode));
00742 /*
00743      Opens a gzip (.gz) file for reading or writing. The mode parameter
00744    is as in fopen ("rb" or "wb") but can also include a compression level
00745    ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
00746    Huffman only compression as in "wb1h". (See the description
00747    of deflateInit2 for more information about the strategy parameter.)
00748 
00749      gzopen can be used to read a file which is not in gzip format; in this
00750    case gzread will directly read from the file without decompression.
00751 
00752      gzopen returns NULL if the file could not be opened or if there was
00753    insufficient memory to allocate the (de)compression state; errno
00754    can be checked to distinguish the two cases (if errno is zero, the
00755    zlib error is Z_MEM_ERROR).  */
00756 
00757 gzFile gzdopen  OF((int fd, const char *mode));
00758 /*
00759      gzdopen() associates a gzFile with the file descriptor fd.  File
00760    descriptors are obtained from calls like open, dup, creat, pipe or
00761    fileno (in the file has been previously opened with fopen).
00762    The mode parameter is as in gzopen.
00763      The next call of gzclose on the returned gzFile will also close the
00764    file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
00765    descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
00766      gzdopen returns NULL if there was insufficient memory to allocate
00767    the (de)compression state.
00768 */
00769 
00770 int gzsetparams OF((gzFile file, int level, int strategy));
00771 /*
00772      Dynamically update the compression level or strategy. See the description
00773    of deflateInit2 for the meaning of these parameters.
00774      gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
00775    opened for writing.
00776 */
00777 
00778 int    gzread  OF((gzFile file, voidp buf, unsigned len));
00779 /*
00780      Reads the given number of uncompressed bytes from the compressed file.
00781    If the input file was not in gzip format, gzread copies the given number
00782    of bytes into the buffer.
00783      gzread returns the number of uncompressed bytes actually read (0 for
00784    end of file, -1 for error). */
00785 
00786 int    gzwrite OF((gzFile file, 
00787                    const voidp buf, unsigned len));
00788 /*
00789      Writes the given number of uncompressed bytes into the compressed file.
00790    gzwrite returns the number of uncompressed bytes actually written
00791    (0 in case of error).
00792 */
00793 
00794 int    gzprintf OF((gzFile file, const char *format, ...));
00795 /*
00796      Converts, formats, and writes the args to the compressed file under
00797    control of the format string, as in fprintf. gzprintf returns the number of
00798    uncompressed bytes actually written (0 in case of error).
00799 */
00800 
00801 int gzputs OF((gzFile file, const char *s));
00802 /*
00803       Writes the given null-terminated string to the compressed file, excluding
00804    the terminating null character.
00805       gzputs returns the number of characters written, or -1 in case of error.
00806 */
00807 
00808 char * gzgets OF((gzFile file, char *buf, int len));
00809 /*
00810       Reads bytes from the compressed file until len-1 characters are read, or
00811    a newline character is read and transferred to buf, or an end-of-file
00812    condition is encountered.  The string is then terminated with a null
00813    character.
00814       gzgets returns buf, or Z_NULL in case of error.
00815 */
00816 
00817 int    gzputc OF((gzFile file, int c));
00818 /*
00819       Writes c, converted to an unsigned char, into the compressed file.
00820    gzputc returns the value that was written, or -1 in case of error.
00821 */
00822 
00823 int    gzgetc OF((gzFile file));
00824 /*
00825       Reads one byte from the compressed file. gzgetc returns this byte
00826    or -1 in case of end of file or error.
00827 */
00828 
00829 int    gzflush OF((gzFile file, int flush));
00830 /*
00831      Flushes all pending output into the compressed file. The parameter
00832    flush is as in the deflate() function. The return value is the zlib
00833    error number (see function gzerror below). gzflush returns Z_OK if
00834    the flush parameter is Z_FINISH and all output could be flushed.
00835      gzflush should be called only when strictly necessary because it can
00836    degrade compression.
00837 */
00838 
00839 long gzseek OF((gzFile file,
00840                       long offset, int whence));
00841 /* 
00842       Sets the starting position for the next gzread or gzwrite on the
00843    given compressed file. The offset represents a number of bytes in the
00844    uncompressed data stream. The whence parameter is defined as in lseek(2);
00845    the value SEEK_END is not supported.
00846      If the file is opened for reading, this function is emulated but can be
00847    extremely slow. If the file is opened for writing, only forward seeks are
00848    supported; gzseek then compresses a sequence of zeroes up to the new
00849    starting position.
00850 
00851       gzseek returns the resulting offset location as measured in bytes from
00852    the beginning of the uncompressed stream, or -1 in case of error, in
00853    particular if the file is opened for writing and the new starting position
00854    would be before the current position.
00855 */
00856 
00857 int    gzrewind OF((gzFile file));
00858 /*
00859      Rewinds the given file. This function is supported only for reading.
00860 
00861    gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
00862 */
00863 
00864 long    gztell OF((gzFile file));
00865 /*
00866      Returns the starting position for the next gzread or gzwrite on the
00867    given compressed file. This position represents a number of bytes in the
00868    uncompressed data stream.
00869 
00870    gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
00871 */
00872 
00873 int gzeof OF((gzFile file));
00874 /*
00875      Returns 1 when EOF has previously been detected reading the given
00876    input stream, otherwise zero.
00877 */
00878 
00879 int    gzclose OF((gzFile file));
00880 /*
00881      Flushes all pending output if necessary, closes the compressed file
00882    and deallocates all the (de)compression state. The return value is the zlib
00883    error number (see function gzerror below).
00884 */
00885 
00886 const char * gzerror OF((gzFile file, int *errnum));
00887 /*
00888      Returns the error message for the last error which occurred on the
00889    given compressed file. errnum is set to zlib error number. If an
00890    error occurred in the file system and not in the compression library,
00891    errnum is set to Z_ERRNO and the application may consult errno
00892    to get the exact error code.
00893 */
00894 
00895                         /* checksum functions */
00896 
00897 /*
00898      These functions are not related to compression but are exported
00899    anyway because they might be useful in applications using the
00900    compression library.
00901 */
00902 
00903 uLong adler32 OF((uLong adler, const Byte *buf, uInt len));
00904 
00905 /*
00906      Update a running Adler-32 checksum with the bytes buf[0..len-1] and
00907    return the updated checksum. If buf is NULL, this function returns
00908    the required initial value for the checksum.
00909    An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
00910    much faster. Usage example:
00911 
00912      uLong adler = adler32(0L, Z_NULL, 0);
00913 
00914      while (read_buffer(buffer, length) != EOF) {
00915        adler = adler32(adler, buffer, length);
00916      }
00917      if (adler != original_adler) error();
00918 */
00919 
00920 uLong crc32   OF((uLong crc, const Byte *buf, uInt len));
00921 /*
00922      Update a running crc with the bytes buf[0..len-1] and return the updated
00923    crc. If buf is NULL, this function returns the required initial value
00924    for the crc. Pre- and post-conditioning (one's complement) is performed
00925    within this function so it shouldn't be done by the application.
00926    Usage example:
00927 
00928      uLong crc = crc32(0L, Z_NULL, 0);
00929 
00930      while (read_buffer(buffer, length) != EOF) {
00931        crc = crc32(crc, buffer, length);
00932      }
00933      if (crc != original_crc) error();
00934 */
00935 
00936 // private stuff to not include cmdlib.h
00937 /*
00938 ============================================================================
00939 
00940                     BYTE ORDER FUNCTIONS
00941 
00942 ============================================================================
00943 */
00944 
00945 #ifdef _SGI_SOURCE
00946 #define __BIG_ENDIAN__
00947 #endif
00948 
00949 #ifdef __BIG_ENDIAN__
00950 
00951 short   __LittleShort (short l)
00952 {
00953     byte    b1,b2;
00954 
00955     b1 = l&255;
00956     b2 = (l>>8)&255;
00957 
00958     return (b1<<8) + b2;
00959 }
00960 
00961 short   __BigShort (short l)
00962 {
00963     return l;
00964 }
00965 
00966 
00967 int    __LittleLong (int l)
00968 {
00969     byte    b1,b2,b3,b4;
00970 
00971     b1 = l&255;
00972     b2 = (l>>8)&255;
00973     b3 = (l>>16)&255;
00974     b4 = (l>>24)&255;
00975 
00976     return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
00977 }
00978 
00979 int    __BigLong (int l)
00980 {
00981     return l;
00982 }
00983 
00984 
00985 float   __LittleFloat (float l)
00986 {
00987     union {byte b[4]; float f;} in, out;
00988     
00989     in.f = l;
00990     out.b[0] = in.b[3];
00991     out.b[1] = in.b[2];
00992     out.b[2] = in.b[1];
00993     out.b[3] = in.b[0];
00994     
00995     return out.f;
00996 }
00997 
00998 float   __BigFloat (float l)
00999 {
01000     return l;
01001 }
01002 
01003 
01004 #else
01005 
01006 
01007 short   __BigShort (short l)
01008 {
01009     byte    b1,b2;
01010 
01011     b1 = l&255;
01012     b2 = (l>>8)&255;
01013 
01014     return (b1<<8) + b2;
01015 }
01016 
01017 short   __LittleShort (short l)
01018 {
01019     return l;
01020 }
01021 
01022 
01023 int    __BigLong (int l)
01024 {
01025     byte    b1,b2,b3,b4;
01026 
01027     b1 = l&255;
01028     b2 = (l>>8)&255;
01029     b3 = (l>>16)&255;
01030     b4 = (l>>24)&255;
01031 
01032     return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
01033 }
01034 
01035 int    __LittleLong (int l)
01036 {
01037     return l;
01038 }
01039 
01040 float   __BigFloat (float l)
01041 {
01042     union {byte b[4]; float f;} in, out;
01043     
01044     in.f = l;
01045     out.b[0] = in.b[3];
01046     out.b[1] = in.b[2];
01047     out.b[2] = in.b[1];
01048     out.b[3] = in.b[0];
01049     
01050     return out.f;
01051 }
01052 
01053 float   __LittleFloat (float l)
01054 {
01055     return l;
01056 }
01057 
01058 
01059 
01060 #endif
01061 
01062 
01063 
01064 
01065                         /* various hacks, don't look :) */
01066 
01067 /* deflateInit and inflateInit are macros to allow checking the zlib version
01068  * and the compiler's view of z_stream:
01069  */
01070 int deflateInit_ OF((z_streamp strm, int level,
01071                                      const char *version, int stream_size));
01072 int inflateInit_ OF((z_streamp strm,
01073                                      const char *version, int stream_size));
01074 int deflateInit2_ OF((z_streamp strm, int  level, int  method,
01075                                       int windowBits, int memLevel,
01076                                       int strategy, const char *version,
01077                                       int stream_size));
01078 int inflateInit2_ OF((z_streamp strm, int  windowBits,
01079                                       const char *version, int stream_size));
01080 #define deflateInit(strm, level) \
01081         deflateInit_((strm), (level),       ZLIB_VERSION, sizeof(z_stream))
01082 #define inflateInit(strm) \
01083         inflateInit_((strm),                ZLIB_VERSION, sizeof(z_stream))
01084 #define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
01085         deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
01086                       (strategy),           ZLIB_VERSION, sizeof(z_stream))
01087 #define inflateInit2(strm, windowBits) \
01088         inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
01089 
01090 
01091 const char   * zError           OF((int err));
01092 int            inflateSyncPoint OF((z_streamp z));
01093 const uLong * get_crc_table    OF((void));
01094 
01095 typedef unsigned char  uch;
01096 typedef unsigned short ush;
01097 typedef unsigned long  ulg;
01098 
01099 extern const char *z_errmsg[10]; /* indexed by 2-zlib_error */
01100 /* (size given to avoid silly warnings with Visual C++) */
01101 
01102 #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
01103 
01104 #define ERR_RETURN(strm,err) \
01105   return (strm->msg = (char*)ERR_MSG(err), (err))
01106 /* To be used only when the state is known to be valid */
01107 
01108         /* common constants */
01109 
01110 #ifndef DEF_WBITS
01111 #  define DEF_WBITS MAX_WBITS
01112 #endif
01113 /* default windowBits for decompression. MAX_WBITS is for compression only */
01114 
01115 #if MAX_MEM_LEVEL >= 8
01116 #  define DEF_MEM_LEVEL 8
01117 #else
01118 #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
01119 #endif
01120 /* default memLevel */
01121 
01122 #define STORED_BLOCK 0
01123 #define STATIC_TREES 1
01124 #define DYN_TREES    2
01125 /* The three kinds of block type */
01126 
01127 #define MIN_MATCH  3
01128 #define MAX_MATCH  258
01129 /* The minimum and maximum match lengths */
01130 
01131 #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
01132 
01133         /* target dependencies */
01134 
01135         /* Common defaults */
01136 
01137 #ifndef OS_CODE
01138 #  define OS_CODE  0x03  /* assume Unix */
01139 #endif
01140 
01141 #ifndef F_OPEN
01142 #  define F_OPEN(name, mode) fopen((name), (mode))
01143 #endif
01144 
01145          /* functions */
01146 
01147 #ifdef HAVE_STRERROR
01148    extern char *strerror OF((int));
01149 #  define zstrerror(errnum) strerror(errnum)
01150 #else
01151 #  define zstrerror(errnum) ""
01152 #endif
01153 
01154 #define zmemcpy memcpy
01155 #define zmemcmp memcmp
01156 #define zmemzero(dest, len) memset(dest, 0, len)
01157 
01158 /* Diagnostic functions */
01159 #ifdef _ZIP_DEBUG_
01160    int z_verbose = 0;
01161 #  define Assert(cond,msg) assert(cond);
01162    //{if(!(cond)) Sys_Error(msg);}
01163 #  define Trace(x) {if (z_verbose>=0) Sys_Error x ;}
01164 #  define Tracev(x) {if (z_verbose>0) Sys_Error x ;}
01165 #  define Tracevv(x) {if (z_verbose>1) Sys_Error x ;}
01166 #  define Tracec(c,x) {if (z_verbose>0 && (c)) Sys_Error x ;}
01167 #  define Tracecv(c,x) {if (z_verbose>1 && (c)) Sys_Error x ;}
01168 #else
01169 #  define Assert(cond,msg)
01170 #  define Trace(x)
01171 #  define Tracev(x)
01172 #  define Tracevv(x)
01173 #  define Tracec(c,x)
01174 #  define Tracecv(c,x)
01175 #endif
01176 
01177 
01178 typedef uLong (*check_func) OF((uLong check, const Byte *buf, uInt len));
01179 voidp zcalloc OF((voidp opaque, unsigned items, unsigned size));
01180 void   zcfree  OF((voidp opaque, voidp ptr));
01181 
01182 #define ZALLOC(strm, items, size) \
01183            (*((strm)->zalloc))((strm)->opaque, (items), (size))
01184 #define ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidp)(addr))
01185 #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
01186 
01187 
01188 #if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES) && \
01189                       !defined(CASESENSITIVITYDEFAULT_NO)
01190 #define CASESENSITIVITYDEFAULT_NO
01191 #endif
01192 
01193 
01194 #ifndef UNZ_BUFSIZE
01195 #define UNZ_BUFSIZE (65536)
01196 #endif
01197 
01198 #ifndef UNZ_MAXFILENAMEINZIP
01199 #define UNZ_MAXFILENAMEINZIP (256)
01200 #endif
01201 
01202 #ifndef ALLOC
01203 # define ALLOC(size) (malloc(size))
01204 #endif
01205 #ifndef TRYFREE
01206 # define TRYFREE(p) {if (p) free(p);}
01207 #endif
01208 
01209 #define SIZECENTRALDIRITEM (0x2e)
01210 #define SIZEZIPLOCALHEADER (0x1e)
01211 
01212 
01213 
01214 /* ===========================================================================
01215      Read a byte from a gz_stream; update next_in and avail_in. Return EOF
01216    for end of file.
01217    IN assertion: the stream s has been sucessfully opened for reading.
01218 */
01219 
01220 /*
01221 static int unzlocal_getByte(FILE *fin,int *pi)
01222 {
01223     unsigned char c;
01224     int err = fread(&c, 1, 1, fin);
01225     if (err==1)
01226     {
01227         *pi = (int)c;
01228         return UNZ_OK;
01229     }
01230     else
01231     {
01232         if (ferror(fin)) 
01233             return UNZ_ERRNO;
01234         else
01235             return UNZ_EOF;
01236     }
01237 }
01238 */
01239 
01240 /* ===========================================================================
01241    Reads a long in LSB order from the given gz_stream. Sets 
01242 */
01243 static int unzlocal_getShort (FILE* fin, uLong *pX)
01244 {
01245     short   v;
01246 
01247     fread( &v, sizeof(v), 1, fin );
01248 
01249     *pX = __LittleShort( v);
01250     return UNZ_OK;
01251 
01252 /*
01253     uLong x ;
01254     int i;
01255     int err;
01256 
01257     err = unzlocal_getByte(fin,&i);
01258     x = (uLong)i;
01259     
01260     if (err==UNZ_OK)
01261         err = unzlocal_getByte(fin,&i);
01262     x += ((uLong)i)<<8;
01263    
01264     if (err==UNZ_OK)
01265         *pX = x;
01266     else
01267         *pX = 0;
01268     return err;
01269 */
01270 }
01271 
01272 static int unzlocal_getLong (FILE *fin, uLong *pX)
01273 {
01274     int     v;
01275 
01276     fread( &v, sizeof(v), 1, fin );
01277 
01278     *pX = __LittleLong( v);
01279     return UNZ_OK;
01280 
01281 /*
01282     uLong x ;
01283     int i;
01284     int err;
01285 
01286     err = unzlocal_getByte(fin,&i);
01287     x = (uLong)i;
01288     
01289     if (err==UNZ_OK)
01290         err = unzlocal_getByte(fin,&i);
01291     x += ((uLong)i)<<8;
01292 
01293     if (err==UNZ_OK)
01294         err = unzlocal_getByte(fin,&i);
01295     x += ((uLong)i)<<16;
01296 
01297     if (err==UNZ_OK)
01298         err = unzlocal_getByte(fin,&i);
01299     x += ((uLong)i)<<24;
01300    
01301     if (err==UNZ_OK)
01302         *pX = x;
01303     else
01304         *pX = 0;
01305     return err;
01306 */
01307 }
01308 
01309 
01310 /* My own strcmpi / strcasecmp */
01311 static int strcmpcasenosensitive_internal (const char* fileName1,const char* fileName2)
01312 {
01313     for (;;)
01314     {
01315         char c1=*(fileName1++);
01316         char c2=*(fileName2++);
01317         if ((c1>='a') && (c1<='z'))
01318             c1 -= 0x20;
01319         if ((c2>='a') && (c2<='z'))
01320             c2 -= 0x20;
01321         if (c1=='\0')
01322             return ((c2=='\0') ? 0 : -1);
01323         if (c2=='\0')
01324             return 1;
01325         if (c1<c2)
01326             return -1;
01327         if (c1>c2)
01328             return 1;
01329     }
01330 }
01331 
01332 
01333 #ifdef  CASESENSITIVITYDEFAULT_NO
01334 #define CASESENSITIVITYDEFAULTVALUE 2
01335 #else
01336 #define CASESENSITIVITYDEFAULTVALUE 1
01337 #endif
01338 
01339 #ifndef STRCMPCASENOSENTIVEFUNCTION
01340 #define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal
01341 #endif
01342 
01343 /* 
01344    Compare two filename (fileName1,fileName2).
01345    If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp)
01346    If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi
01347                                                                 or strcasecmp)
01348    If iCaseSenisivity = 0, case sensitivity is defaut of your operating system
01349         (like 1 on Unix, 2 on Windows)
01350 
01351 */
01352 extern int unzStringFileNameCompare (const char* fileName1,const char* fileName2,int iCaseSensitivity)
01353 {
01354     if (iCaseSensitivity==0)
01355         iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE;
01356 
01357     if (iCaseSensitivity==1)
01358         return strcmp(fileName1,fileName2);
01359 
01360     return STRCMPCASENOSENTIVEFUNCTION(fileName1,fileName2);
01361 } 
01362 
01363 #define BUFREADCOMMENT (0x400)
01364 
01365 /*
01366   Locate the Central directory of a zipfile (at the end, just before
01367     the global comment)
01368 */
01369 static uLong unzlocal_SearchCentralDir(FILE *fin)
01370 {
01371     unsigned char* buf;
01372     uLong uSizeFile;
01373     uLong uBackRead;
01374     uLong uMaxBack=0xffff; /* maximum size of global comment */
01375     uLong uPosFound=0;
01376     
01377     if (fseek(fin,0,SEEK_END) != 0)
01378         return 0;
01379 
01380 
01381     uSizeFile = ftell( fin );
01382     
01383     if (uMaxBack>uSizeFile)
01384         uMaxBack = uSizeFile;
01385 
01386     buf = (unsigned char*)malloc(BUFREADCOMMENT+4);
01387     if (buf==NULL)
01388         return 0;
01389 
01390     uBackRead = 4;
01391     while (uBackRead<uMaxBack)
01392     {
01393         uLong uReadSize,uReadPos ;
01394         int i;
01395         if (uBackRead+BUFREADCOMMENT>uMaxBack) 
01396             uBackRead = uMaxBack;
01397         else
01398             uBackRead+=BUFREADCOMMENT;
01399         uReadPos = uSizeFile-uBackRead ;
01400         
01401         uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? 
01402                      (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);
01403         if (fseek(fin,uReadPos,SEEK_SET)!=0)
01404             break;
01405 
01406         if (fread(buf,(uInt)uReadSize,1,fin)!=1)
01407             break;
01408 
01409                 for (i=(int)uReadSize-3; (i--)>0;)
01410             if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && 
01411                 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
01412             {
01413                 uPosFound = uReadPos+i;
01414                 break;
01415             }
01416 
01417         if (uPosFound!=0)
01418             break;
01419     }
01420     free(buf);
01421     return uPosFound;
01422 }
01423 
01424 extern unzFile unzReOpen (const char* path, unzFile file)
01425 {
01426     unz_s *s;
01427     FILE * fin;
01428 
01429     fin=fopen(path,"rb");
01430     if (fin==NULL)
01431         return NULL;
01432 
01433     s=(unz_s*)malloc(sizeof(unz_s));
01434     memcpy(s, (unz_s*)file, sizeof(unz_s));
01435 
01436     s->file = fin;
01437     return (unzFile)s;  
01438 }
01439 
01440 /*
01441   Open a Zip file. path contain the full pathname (by example,
01442      on a Windows NT computer "c:\\test\\zlib109.zip" or on an Unix computer
01443      "zlib/zlib109.zip".
01444      If the zipfile cannot be opened (file don't exist or in not valid), the
01445        return value is NULL.
01446      Else, the return value is a unzFile Handle, usable with other function
01447        of this unzip package.
01448 */
01449 extern unzFile unzOpen (const char* path)
01450 {
01451     unz_s us;
01452     unz_s *s;
01453     uLong central_pos,uL;
01454     FILE * fin ;
01455 
01456     uLong number_disk;          /* number of the current dist, used for 
01457                                    spaning ZIP, unsupported, always 0*/
01458     uLong number_disk_with_CD;  /* number the the disk with central dir, used
01459                                    for spaning ZIP, unsupported, always 0*/
01460     uLong number_entry_CD;      /* total number of entries in
01461                                    the central dir 
01462                                    (same than number_entry on nospan) */
01463 
01464     int err=UNZ_OK;
01465 
01466     fin=fopen(path,"rb");
01467     if (fin==NULL)
01468         return NULL;
01469 
01470     central_pos = unzlocal_SearchCentralDir(fin);
01471     if (central_pos==0)
01472         err=UNZ_ERRNO;
01473 
01474     if (fseek(fin,central_pos,SEEK_SET)!=0)
01475         err=UNZ_ERRNO;
01476 
01477     /* the signature, already checked */
01478     if (unzlocal_getLong(fin,&uL)!=UNZ_OK)
01479         err=UNZ_ERRNO;
01480 
01481     /* number of this disk */
01482     if (unzlocal_getShort(fin,&number_disk)!=UNZ_OK)
01483         err=UNZ_ERRNO;
01484 
01485     /* number of the disk with the start of the central directory */
01486     if (unzlocal_getShort(fin,&number_disk_with_CD)!=UNZ_OK)
01487         err=UNZ_ERRNO;
01488 
01489     /* total number of entries in the central dir on this disk */
01490     if (unzlocal_getShort(fin,&us.gi.number_entry)!=UNZ_OK)
01491         err=UNZ_ERRNO;
01492 
01493     /* total number of entries in the central dir */
01494     if (unzlocal_getShort(fin,&number_entry_CD)!=UNZ_OK)
01495         err=UNZ_ERRNO;
01496 
01497     if ((number_entry_CD!=us.gi.number_entry) ||
01498         (number_disk_with_CD!=0) ||
01499         (number_disk!=0))
01500         err=UNZ_BADZIPFILE;
01501 
01502     /* size of the central directory */
01503     if (unzlocal_getLong(fin,&us.size_central_dir)!=UNZ_OK)
01504         err=UNZ_ERRNO;
01505 
01506     /* offset of start of central directory with respect to the 
01507           starting disk number */
01508     if (unzlocal_getLong(fin,&us.offset_central_dir)!=UNZ_OK)
01509         err=UNZ_ERRNO;
01510 
01511     /* zipfile comment length */
01512     if (unzlocal_getShort(fin,&us.gi.size_comment)!=UNZ_OK)
01513         err=UNZ_ERRNO;
01514 
01515     if ((central_pos<us.offset_central_dir+us.size_central_dir) && 
01516         (err==UNZ_OK))
01517         err=UNZ_BADZIPFILE;
01518 
01519     if (err!=UNZ_OK)
01520     {
01521         fclose(fin);
01522         return NULL;
01523     }
01524 
01525     us.file=fin;
01526     us.byte_before_the_zipfile = central_pos -
01527                             (us.offset_central_dir+us.size_central_dir);
01528     us.central_pos = central_pos;
01529     us.pfile_in_zip_read = NULL;
01530     
01531 
01532     s=(unz_s*)malloc(sizeof(unz_s));
01533     *s=us;
01534 //  unzGoToFirstFile((unzFile)s);   
01535     return (unzFile)s;  
01536 }
01537 
01538 
01539 /*
01540   Close a ZipFile opened with unzipOpen.
01541   If there is files inside the .Zip opened with unzipOpenCurrentFile (see later),
01542     these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
01543   return UNZ_OK if there is no problem. */
01544 extern int unzClose (unzFile file)
01545 {
01546     unz_s* s;
01547     if (file==NULL)
01548         return UNZ_PARAMERROR;
01549     s=(unz_s*)file;
01550 
01551     if (s->pfile_in_zip_read!=NULL)
01552         unzCloseCurrentFile(file);
01553 
01554     fclose(s->file);
01555     free(s);
01556     return UNZ_OK;
01557 }
01558 
01559 
01560 /*
01561   Write info about the ZipFile in the *pglobal_info structure.
01562   No preparation of the structure is needed
01563   return UNZ_OK if there is no problem. */
01564 extern int unzGetGlobalInfo (unzFile file,unz_global_info *pglobal_info)
01565 {
01566     unz_s* s;
01567     if (file==NULL)
01568         return UNZ_PARAMERROR;
01569     s=(unz_s*)file;
01570     *pglobal_info=s->gi;
01571     return UNZ_OK;
01572 }
01573 
01574 
01575 /*
01576    Translate date/time from Dos format to tm_unz (readable more easilty)
01577 */
01578 static void unzlocal_DosDateToTmuDate (uLong ulDosDate, tm_unz* ptm)
01579 {
01580     uLong uDate;
01581     uDate = (uLong)(ulDosDate>>16);
01582     ptm->tm_mday = (uInt)(uDate&0x1f) ;
01583     ptm->tm_mon =  (uInt)((((uDate)&0x1E0)/0x20)-1) ;
01584     ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ;
01585 
01586     ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800);
01587     ptm->tm_min =  (uInt) ((ulDosDate&0x7E0)/0x20) ;
01588     ptm->tm_sec =  (uInt) (2*(ulDosDate&0x1f)) ;
01589 }
01590 
01591 /*
01592   Get Info about the current file in the zipfile, with internal only info
01593 */
01594 static int unzlocal_GetCurrentFileInfoInternal (unzFile file,
01595                                                   unz_file_info *pfile_info,
01596                                                   unz_file_info_internal 
01597                                                   *pfile_info_internal,
01598                                                   char *szFileName,
01599                                                   uLong fileNameBufferSize,
01600                                                   void *extraField,
01601                                                   uLong extraFieldBufferSize,
01602                                                   char *szComment,
01603                                                   uLong commentBufferSize)
01604 {
01605     unz_s* s;
01606     unz_file_info file_info;
01607     unz_file_info_internal file_info_internal;
01608     int err=UNZ_OK;
01609     uLong uMagic;
01610     long lSeek=0;
01611 
01612     if (file==NULL)
01613         return UNZ_PARAMERROR;
01614     s=(unz_s*)file;
01615     if (fseek(s->file,s->pos_in_central_dir+s->byte_before_the_zipfile,SEEK_SET)!=0)
01616         err=UNZ_ERRNO;
01617 
01618 
01619     /* we check the magic */
01620     if (err==UNZ_OK)
01621         if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)
01622             err=UNZ_ERRNO;
01623         else if (uMagic!=0x02014b50)
01624             err=UNZ_BADZIPFILE;
01625 
01626     if (unzlocal_getShort(s->file,&file_info.version) != UNZ_OK)
01627         err=UNZ_ERRNO;
01628 
01629     if (unzlocal_getShort(s->file,&file_info.version_needed) != UNZ_OK)
01630         err=UNZ_ERRNO;
01631 
01632     if (unzlocal_getShort(s->file,&file_info.flag) != UNZ_OK)
01633         err=UNZ_ERRNO;
01634 
01635     if (unzlocal_getShort(s->file,&file_info.compression_method) != UNZ_OK)
01636         err=UNZ_ERRNO;
01637 
01638     if (unzlocal_getLong(s->file,&file_info.dosDate) != UNZ_OK)
01639         err=UNZ_ERRNO;
01640 
01641     unzlocal_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date);
01642 
01643     if (unzlocal_getLong(s->file,&file_info.crc) != UNZ_OK)
01644         err=UNZ_ERRNO;
01645 
01646     if (unzlocal_getLong(s->file,&file_info.compressed_size) != UNZ_OK)
01647         err=UNZ_ERRNO;
01648 
01649     if (unzlocal_getLong(s->file,&file_info.uncompressed_size) != UNZ_OK)
01650         err=UNZ_ERRNO;
01651 
01652     if (unzlocal_getShort(s->file,&file_info.size_filename) != UNZ_OK)
01653         err=UNZ_ERRNO;
01654 
01655     if (unzlocal_getShort(s->file,&file_info.size_file_extra) != UNZ_OK)
01656         err=UNZ_ERRNO;
01657 
01658     if (unzlocal_getShort(s->file,&file_info.size_file_comment) != UNZ_OK)
01659         err=UNZ_ERRNO;
01660 
01661     if (unzlocal_getShort(s->file,&file_info.disk_num_start) != UNZ_OK)
01662         err=UNZ_ERRNO;
01663 
01664     if (unzlocal_getShort(s->file,&file_info.internal_fa) != UNZ_OK)
01665         err=UNZ_ERRNO;
01666 
01667     if (unzlocal_getLong(s->file,&file_info.external_fa) != UNZ_OK)
01668         err=UNZ_ERRNO;
01669 
01670     if (unzlocal_getLong(s->file,&file_info_internal.offset_curfile) != UNZ_OK)
01671         err=UNZ_ERRNO;
01672 
01673     lSeek+=file_info.size_filename;
01674     if ((err==UNZ_OK) && (szFileName!=NULL))
01675     {
01676         uLong uSizeRead ;
01677         if (file_info.size_filename<fileNameBufferSize)
01678         {
01679             *(szFileName+file_info.size_filename)='\0';
01680             uSizeRead = file_info.size_filename;
01681         }
01682         else
01683             uSizeRead = fileNameBufferSize;
01684 
01685         if ((file_info.size_filename>0) && (fileNameBufferSize>0))
01686             if (fread(szFileName,(uInt)uSizeRead,1,s->file)!=1)
01687                 err=UNZ_ERRNO;
01688         lSeek -= uSizeRead;
01689     }
01690 
01691     
01692     if ((err==UNZ_OK) && (extraField!=NULL))
01693     {
01694         uLong uSizeRead ;
01695         if (file_info.size_file_extra<extraFieldBufferSize)
01696             uSizeRead = file_info.size_file_extra;
01697         else
01698             uSizeRead = extraFieldBufferSize;
01699 
01700         if (lSeek!=0)
01701             if (fseek(s->file,lSeek,SEEK_CUR)==0)
01702                 lSeek=0;
01703             else
01704                 err=UNZ_ERRNO;
01705         if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0))
01706             if (fread(extraField,(uInt)uSizeRead,1,s->file)!=1)
01707                 err=UNZ_ERRNO;
01708         lSeek += file_info.size_file_extra - uSizeRead;
01709     }
01710     else
01711         lSeek+=file_info.size_file_extra; 
01712 
01713     
01714     if ((err==UNZ_OK) && (szComment!=NULL))
01715     {
01716         uLong uSizeRead ;
01717         if (file_info.size_file_comment<commentBufferSize)
01718         {
01719             *(szComment+file_info.size_file_comment)='\0';
01720             uSizeRead = file_info.size_file_comment;
01721         }
01722         else
01723             uSizeRead = commentBufferSize;
01724 
01725         if (lSeek!=0)
01726             if (fseek(s->file,lSeek,SEEK_CUR)==0)
01727                 lSeek=0;
01728             else
01729                 err=UNZ_ERRNO;
01730         if ((file_info.size_file_comment>0) && (commentBufferSize>0))
01731             if (fread(szComment,(uInt)uSizeRead,1,s->file)!=1)
01732                 err=UNZ_ERRNO;
01733         lSeek+=file_info.size_file_comment - uSizeRead;
01734     }
01735     else
01736         lSeek+=file_info.size_file_comment;
01737 
01738     if ((err==UNZ_OK) && (pfile_info!=NULL))
01739         *pfile_info=file_info;
01740 
01741     if ((err==UNZ_OK) && (pfile_info_internal!=NULL))
01742         *pfile_info_internal=file_info_internal;
01743 
01744     return err;
01745 }
01746 
01747 
01748 
01749 /*
01750   Write info about the ZipFile in the *pglobal_info structure.
01751   No preparation of the structure is needed
01752   return UNZ_OK if there is no problem.
01753 */
01754 extern int unzGetCurrentFileInfo (  unzFile file, unz_file_info *pfile_info,
01755                                     char *szFileName, uLong fileNameBufferSize,
01756                                     void *extraField, uLong extraFieldBufferSize,
01757                                     char *szComment, uLong commentBufferSize)
01758 {
01759     return unzlocal_GetCurrentFileInfoInternal(file,pfile_info,NULL,
01760                                                 szFileName,fileNameBufferSize,
01761                                                 extraField,extraFieldBufferSize,
01762                                                 szComment,commentBufferSize);
01763 }
01764 
01765 /*
01766   Set the current file of the zipfile to the first file.
01767   return UNZ_OK if there is no problem
01768 */
01769 extern int unzGoToFirstFile (unzFile file)
01770 {
01771     int err=UNZ_OK;
01772     unz_s* s;
01773     if (file==NULL)
01774         return UNZ_PARAMERROR;
01775     s=(unz_s*)file;
01776     s->pos_in_central_dir=s->offset_central_dir;
01777     s->num_file=0;
01778     err=unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
01779                                              &s->cur_file_info_internal,
01780                                              NULL,0,NULL,0,NULL,0);
01781     s->current_file_ok = (err == UNZ_OK);
01782     return err;
01783 }
01784 
01785 
01786 /*
01787   Set the current file of the zipfile to the next file.
01788   return UNZ_OK if there is no problem
01789   return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
01790 */
01791 extern int unzGoToNextFile (unzFile file)
01792 {
01793     unz_s* s;   
01794     int err;
01795 
01796     if (file==NULL)
01797         return UNZ_PARAMERROR;
01798     s=(unz_s*)file;
01799     if (!s->current_file_ok)
01800         return UNZ_END_OF_LIST_OF_FILE;
01801     if (s->num_file+1==s->gi.number_entry)
01802         return UNZ_END_OF_LIST_OF_FILE;
01803 
01804     s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename +
01805             s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ;
01806     s->num_file++;
01807     err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
01808                                                &s->cur_file_info_internal,
01809                                                NULL,0,NULL,0,NULL,0);
01810     s->current_file_ok = (err == UNZ_OK);
01811     return err;
01812 }
01813 
01814 
01815 /*
01816   Try locate the file szFileName in the zipfile.
01817   For the iCaseSensitivity signification, see unzipStringFileNameCompare
01818 
01819   return value :
01820   UNZ_OK if the file is found. It becomes the current file.
01821   UNZ_END_OF_LIST_OF_FILE if the file is not found
01822 */
01823 extern int unzLocateFile (unzFile file, const char *szFileName, int iCaseSensitivity)
01824 {
01825     unz_s* s;   
01826     int err;
01827 
01828     
01829     uLong num_fileSaved;
01830     uLong pos_in_central_dirSaved;
01831 
01832 
01833     if (file==NULL)
01834         return UNZ_PARAMERROR;
01835 
01836     if (strlen(szFileName)>=UNZ_MAXFILENAMEINZIP)
01837         return UNZ_PARAMERROR;
01838 
01839     s=(unz_s*)file;
01840     if (!s->current_file_ok)
01841         return UNZ_END_OF_LIST_OF_FILE;
01842 
01843     num_fileSaved = s->num_file;
01844     pos_in_central_dirSaved = s->pos_in_central_dir;
01845 
01846     err = unzGoToFirstFile(file);
01847 
01848     while (err == UNZ_OK)
01849     {
01850         char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1];
01851         unzGetCurrentFileInfo(file,NULL,
01852                                 szCurrentFileName,sizeof(szCurrentFileName)-1,
01853                                 NULL,0,NULL,0);
01854         if (unzStringFileNameCompare(szCurrentFileName,
01855                                         szFileName,iCaseSensitivity)==0)
01856             return UNZ_OK;
01857         err = unzGoToNextFile(file);
01858     }
01859 
01860     s->num_file = num_fileSaved ;
01861     s->pos_in_central_dir = pos_in_central_dirSaved ;
01862     return err;
01863 }
01864 
01865 
01866 /*
01867   Read the static header of the current zipfile
01868   Check the coherency of the static header and info in the end of central
01869         directory about this file
01870   store in *piSizeVar the size of extra info in static header
01871         (filename and size of extra field data)
01872 */
01873 static int unzlocal_CheckCurrentFileCoherencyHeader (unz_s* s, uInt* piSizeVar,
01874                                                     uLong *poffset_local_extrafield,
01875                                                     uInt *psize_local_extrafield)
01876 {
01877     uLong uMagic,uData,uFlags;
01878     uLong size_filename;
01879     uLong size_extra_field;
01880     int err=UNZ_OK;
01881 
01882     *piSizeVar = 0;
01883     *poffset_local_extrafield = 0;
01884     *psize_local_extrafield = 0;
01885 
01886     if (fseek(s->file,s->cur_file_info_internal.offset_curfile +
01887                                 s->byte_before_the_zipfile,SEEK_SET)!=0)
01888         return UNZ_ERRNO;
01889 
01890 
01891     if (err==UNZ_OK)
01892         if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)
01893             err=UNZ_ERRNO;
01894         else if (uMagic!=0x04034b50)
01895             err=UNZ_BADZIPFILE;
01896 
01897     if (unzlocal_getShort(s->file,&uData) != UNZ_OK)
01898         err=UNZ_ERRNO;
01899 /*
01900     else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion))
01901         err=UNZ_BADZIPFILE;
01902 */
01903     if (unzlocal_getShort(s->file,&uFlags) != UNZ_OK)
01904         err=UNZ_ERRNO;
01905 
01906     if (unzlocal_getShort(s->file,&uData) != UNZ_OK)
01907         err=UNZ_ERRNO;
01908     else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method))
01909         err=UNZ_BADZIPFILE;
01910 
01911     if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) &&
01912                          (s->cur_file_info.compression_method!=Z_DEFLATED))
01913         err=UNZ_BADZIPFILE;
01914 
01915     if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* date/time */
01916         err=UNZ_ERRNO;
01917 
01918     if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* crc */
01919         err=UNZ_ERRNO;
01920     else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) &&
01921                               ((uFlags & 8)==0))
01922         err=UNZ_BADZIPFILE;
01923 
01924     if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* size compr */
01925         err=UNZ_ERRNO;
01926     else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) &&
01927                               ((uFlags & 8)==0))
01928         err=UNZ_BADZIPFILE;
01929 
01930     if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* size uncompr */
01931         err=UNZ_ERRNO;
01932     else if ((err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) && 
01933                               ((uFlags & 8)==0))
01934         err=UNZ_BADZIPFILE;
01935 
01936 
01937     if (unzlocal_getShort(s->file,&size_filename) != UNZ_OK)
01938         err=UNZ_ERRNO;
01939     else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename))
01940         err=UNZ_BADZIPFILE;
01941 
01942     *piSizeVar += (uInt)size_filename;
01943 
01944     if (unzlocal_getShort(s->file,&size_extra_field) != UNZ_OK)
01945         err=UNZ_ERRNO;
01946     *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile +
01947                                     SIZEZIPLOCALHEADER + size_filename;
01948     *psize_local_extrafield = (uInt)size_extra_field;
01949 
01950     *piSizeVar += (uInt)size_extra_field;
01951 
01952     return err;
01953 }
01954                                                 
01955 /*
01956   Open for reading data the current file in the zipfile.
01957   If there is no error and the file is opened, the return value is UNZ_OK.
01958 */
01959 extern int unzOpenCurrentFile (unzFile file)
01960 {
01961     int err=UNZ_OK;
01962     int Store;
01963     uInt iSizeVar;
01964     unz_s* s;
01965     file_in_zip_read_info_s* pfile_in_zip_read_info;
01966     uLong offset_local_extrafield;  /* offset of the static extra field */
01967     uInt  size_local_extrafield;    /* size of the static extra field */
01968 
01969     if (file==NULL)
01970         return UNZ_PARAMERROR;
01971     s=(unz_s*)file;
01972     if (!s->current_file_ok)
01973         return UNZ_PARAMERROR;
01974 
01975     if (s->pfile_in_zip_read != NULL)
01976         unzCloseCurrentFile(file);
01977 
01978     if (unzlocal_CheckCurrentFileCoherencyHeader(s,&iSizeVar,
01979                 &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK)
01980         return UNZ_BADZIPFILE;
01981 
01982     pfile_in_zip_read_info = (file_in_zip_read_info_s*)
01983                                         malloc(sizeof(file_in_zip_read_info_s));
01984     if (pfile_in_zip_read_info==NULL)
01985         return UNZ_INTERNALERROR;
01986 
01987     pfile_in_zip_read_info->read_buffer=(char*)malloc(UNZ_BUFSIZE);
01988     pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;
01989     pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;
01990     pfile_in_zip_read_info->pos_local_extrafield=0;
01991 
01992     if (pfile_in_zip_read_info->read_buffer==NULL)
01993     {
01994         free(pfile_in_zip_read_info);
01995         return UNZ_INTERNALERROR;
01996     }
01997 
01998     pfile_in_zip_read_info->stream_initialised=0;
01999     
02000     if ((s->cur_file_info.compression_method!=0) &&
02001         (s->cur_file_info.compression_method!=Z_DEFLATED))
02002         err=UNZ_BADZIPFILE;
02003     Store = s->cur_file_info.compression_method==0;
02004 
02005     pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc;
02006     pfile_in_zip_read_info->crc32=0;
02007     pfile_in_zip_read_info->compression_method =
02008             s->cur_file_info.compression_method;
02009     pfile_in_zip_read_info->file=s->file;
02010     pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile;
02011 
02012     pfile_in_zip_read_info->stream.total_out = 0;
02013 
02014     if (!Store)
02015     {
02016       pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
02017       pfile_in_zip_read_info->stream.zfree = (free_func)0;
02018       pfile_in_zip_read_info->stream.opaque = (voidp)0; 
02019       
02020       err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS);
02021       if (err == Z_OK)
02022         pfile_in_zip_read_info->stream_initialised=1;
02023         /* windowBits is passed < 0 to tell that there is no zlib header.
02024          * Note that in this case inflate *requires* an extra "dummy" byte
02025          * after the compressed stream in order to complete decompression and
02026          * return Z_STREAM_END. 
02027          * In unzip, i don't wait absolutely Z_STREAM_END because I known the 
02028          * size of both compressed and uncompressed data
02029          */
02030     }
02031     pfile_in_zip_read_info->rest_read_compressed = 
02032             s->cur_file_info.compressed_size ;
02033     pfile_in_zip_read_info->rest_read_uncompressed = 
02034             s->cur_file_info.uncompressed_size ;
02035 
02036     
02037     pfile_in_zip_read_info->pos_in_zipfile = 
02038             s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + 
02039               iSizeVar;
02040     
02041     pfile_in_zip_read_info->stream.avail_in = (uInt)0;
02042 
02043 
02044     s->pfile_in_zip_read = pfile_in_zip_read_info;
02045     return UNZ_OK;
02046 }
02047 
02048 
02049 /*
02050   Read bytes from the current file.
02051   buf contain buffer where data must be copied
02052   len the size of buf.
02053 
02054   return the number of byte copied if somes bytes are copied
02055   return 0 if the end of file was reached
02056   return <0 with error code if there is an error
02057     (UNZ_ERRNO for IO error, or zLib error for uncompress error)
02058 */
02059 extern int unzReadCurrentFile  (unzFile file, void *buf, unsigned len)
02060 {
02061     int err=UNZ_OK;
02062     uInt iRead = 0;
02063     unz_s* s;
02064     file_in_zip_read_info_s* pfile_in_zip_read_info;
02065     if (file==NULL)
02066         return UNZ_PARAMERROR;
02067     s=(unz_s*)file;
02068     pfile_in_zip_read_info=s->pfile_in_zip_read;
02069 
02070     if (pfile_in_zip_read_info==NULL)
02071         return UNZ_PARAMERROR;
02072 
02073 
02074     if ((pfile_in_zip_read_info->read_buffer == NULL))
02075         return UNZ_END_OF_LIST_OF_FILE;
02076     if (len==0)
02077         return 0;
02078 
02079     pfile_in_zip_read_info->stream.next_out = (Byte*)buf;
02080 
02081     pfile_in_zip_read_info->stream.avail_out = (uInt)len;
02082     
02083     if (len>pfile_in_zip_read_info->rest_read_uncompressed)
02084         pfile_in_zip_read_info->stream.avail_out = 
02085           (uInt)pfile_in_zip_read_info->rest_read_uncompressed;
02086 
02087     while (pfile_in_zip_read_info->stream.avail_out>0)
02088     {
02089         if ((pfile_in_zip_read_info->stream.avail_in==0) &&
02090             (pfile_in_zip_read_info->rest_read_compressed>0))
02091         {
02092             uInt uReadThis = UNZ_BUFSIZE;
02093             if (pfile_in_zip_read_info->rest_read_compressed<uReadThis)
02094                 uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed;
02095             if (uReadThis == 0)
02096                 return UNZ_EOF;
02097             if (s->cur_file_info.compressed_size == pfile_in_zip_read_info->rest_read_compressed)
02098                 if (fseek(pfile_in_zip_read_info->file,
02099                           pfile_in_zip_read_info->pos_in_zipfile + 
02100                              pfile_in_zip_read_info->byte_before_the_zipfile,SEEK_SET)!=0)
02101                     return UNZ_ERRNO;
02102             if (fread(pfile_in_zip_read_info->read_buffer,uReadThis,1,
02103                          pfile_in_zip_read_info->file)!=1)
02104                 return UNZ_ERRNO;
02105             pfile_in_zip_read_info->pos_in_zipfile += uReadThis;
02106 
02107             pfile_in_zip_read_info->rest_read_compressed-=uReadThis;
02108             
02109             pfile_in_zip_read_info->stream.next_in = 
02110                 (Byte*)pfile_in_zip_read_info->read_buffer;
02111             pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis;
02112         }
02113 
02114         if (pfile_in_zip_read_info->compression_method==0)
02115         {
02116             uInt uDoCopy,i ;
02117             if (pfile_in_zip_read_info->stream.avail_out < 
02118                             pfile_in_zip_read_info->stream.avail_in)
02119                 uDoCopy = pfile_in_zip_read_info->stream.avail_out ;
02120             else
02121                 uDoCopy = pfile_in_zip_read_info->stream.avail_in ;
02122                 
02123             for (i=0;i<uDoCopy;i++)
02124                 *(pfile_in_zip_read_info->stream.next_out+i) =
02125                         *(pfile_in_zip_read_info->stream.next_in+i);
02126                     
02127             pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,
02128                                 pfile_in_zip_read_info->stream.next_out,
02129                                 uDoCopy);
02130             pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy;
02131             pfile_in_zip_read_info->stream.avail_in -= uDoCopy;
02132             pfile_in_zip_read_info->stream.avail_out -= uDoCopy;
02133             pfile_in_zip_read_info->stream.next_out += uDoCopy;
02134             pfile_in_zip_read_info->stream.next_in += uDoCopy;
02135             pfile_in_zip_read_info->stream.total_out += uDoCopy;
02136             iRead += uDoCopy;
02137         }
02138         else
02139         {
02140             uLong uTotalOutBefore,uTotalOutAfter;
02141             const Byte *bufBefore;
02142             uLong uOutThis;
02143             int flush=Z_SYNC_FLUSH;
02144 
02145             uTotalOutBefore = pfile_in_zip_read_info->stream.total_out;
02146             bufBefore = pfile_in_zip_read_info->stream.next_out;
02147 
02148             /*
02149             if ((pfile_in_zip_read_info->rest_read_uncompressed ==
02150                      pfile_in_zip_read_info->stream.avail_out) &&
02151                 (pfile_in_zip_read_info->rest_read_compressed == 0))
02152                 flush = Z_FINISH;
02153             */
02154             err=inflate(&pfile_in_zip_read_info->stream,flush);
02155 
02156             uTotalOutAfter = pfile_in_zip_read_info->stream.total_out;
02157             uOutThis = uTotalOutAfter-uTotalOutBefore;
02158             
02159             pfile_in_zip_read_info->crc32 = 
02160                 crc32(pfile_in_zip_read_info->crc32,bufBefore,
02161                         (uInt)(uOutThis));
02162 
02163             pfile_in_zip_read_info->rest_read_uncompressed -=
02164                 uOutThis;
02165 
02166             iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
02167             
02168             if (err==Z_STREAM_END)
02169                 return (iRead==0) ? UNZ_EOF : iRead;
02170             if (err!=Z_OK) 
02171                 break;
02172         }
02173     }
02174 
02175     if (err==Z_OK)
02176         return iRead;
02177     return err;
02178 }
02179 
02180 
02181 /*
02182   Give the current position in uncompressed data
02183 */
02184 extern long unztell (unzFile file)
02185 {
02186     unz_s* s;
02187     file_in_zip_read_info_s* pfile_in_zip_read_info;
02188     if (file==NULL)
02189         return UNZ_PARAMERROR;
02190     s=(unz_s*)file;
02191     pfile_in_zip_read_info=s->pfile_in_zip_read;
02192 
02193     if (pfile_in_zip_read_info==NULL)
02194         return UNZ_PARAMERROR;
02195 
02196     return (long)pfile_in_zip_read_info->stream.total_out;
02197 }
02198 
02199 
02200 /*
02201   return 1 if the end of file was reached, 0 elsewhere 
02202 */
02203 extern int unzeof (unzFile file)
02204 {
02205     unz_s* s;
02206     file_in_zip_read_info_s* pfile_in_zip_read_info;
02207     if (file==NULL)
02208         return UNZ_PARAMERROR;
02209     s=(unz_s*)file;
02210     pfile_in_zip_read_info=s->pfile_in_zip_read;
02211 
02212     if (pfile_in_zip_read_info==NULL)
02213         return UNZ_PARAMERROR;
02214     
02215     if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
02216         return 1;
02217     else
02218         return 0;
02219 }
02220 
02221 
02222 
02223 /*
02224   Read extra field from the current file (opened by unzOpenCurrentFile)
02225   This is the static-header version of the extra field (sometimes, there is
02226     more info in the static-header version than in the central-header)
02227 
02228   if buf==NULL, it return the size of the static extra field that can be read
02229 
02230   if buf!=NULL, len is the size of the buffer, the extra header is copied in
02231     buf.
02232   the return value is the number of bytes copied in buf, or (if <0) 
02233     the error code
02234 */
02235 extern int unzGetLocalExtrafield (unzFile file,void *buf,unsigned len)
02236 {
02237     unz_s* s;
02238     file_in_zip_read_info_s* pfile_in_zip_read_info;
02239     uInt read_now;
02240     uLong size_to_read;
02241 
02242     if (file==NULL)
02243         return UNZ_PARAMERROR;
02244     s=(unz_s*)file;
02245     pfile_in_zip_read_info=s->pfile_in_zip_read;
02246 
02247     if (pfile_in_zip_read_info==NULL)
02248         return UNZ_PARAMERROR;
02249 
02250     size_to_read = (pfile_in_zip_read_info->size_local_extrafield - 
02251                 pfile_in_zip_read_info->pos_local_extrafield);
02252 
02253     if (buf==NULL)
02254         return (int)size_to_read;
02255     
02256     if (len>size_to_read)
02257         read_now = (uInt)size_to_read;
02258     else
02259         read_now = (uInt)len ;
02260 
02261     if (read_now==0)
02262         return 0;
02263     
02264     if (fseek(pfile_in_zip_read_info->file,
02265               pfile_in_zip_read_info->offset_local_extrafield + 
02266               pfile_in_zip_read_info->pos_local_extrafield,SEEK_SET)!=0)
02267         return UNZ_ERRNO;
02268 
02269     if (fread(buf,(uInt)size_to_read,1,pfile_in_zip_read_info->file)!=1)
02270         return UNZ_ERRNO;
02271 
02272     return (int)read_now;
02273 }
02274 
02275 /*
02276   Close the file in zip opened with unzipOpenCurrentFile
02277   Return UNZ_CRCERROR if all the file was read but the CRC is not good
02278 */
02279 extern int unzCloseCurrentFile (unzFile file)
02280 {
02281     int err=UNZ_OK;
02282 
02283     unz_s* s;
02284     file_in_zip_read_info_s* pfile_in_zip_read_info;
02285     if (file==NULL)
02286         return UNZ_PARAMERROR;
02287     s=(unz_s*)file;
02288     pfile_in_zip_read_info=s->pfile_in_zip_read;
02289 
02290     if (pfile_in_zip_read_info==NULL)
02291         return UNZ_PARAMERROR;
02292 
02293 
02294     if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
02295     {
02296         if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait)
02297             err=UNZ_CRCERROR;
02298     }
02299 
02300 
02301     free(pfile_in_zip_read_info->read_buffer);
02302     pfile_in_zip_read_info->read_buffer = NULL;
02303     if (pfile_in_zip_read_info->stream_initialised)
02304         inflateEnd(&pfile_in_zip_read_info->stream);
02305 
02306     pfile_in_zip_read_info->stream_initialised = 0;
02307     free(pfile_in_zip_read_info);
02308 
02309     s->pfile_in_zip_read=NULL;
02310 
02311     return err;
02312 }
02313 
02314 
02315 /*
02316   Get the global comment string of the ZipFile, in the szComment buffer.
02317   uSizeBuf is the size of the szComment buffer.
02318   return the number of byte copied or an error code <0
02319 */
02320 extern int unzGetGlobalComment (unzFile file, char *szComment, uLong uSizeBuf)
02321 {
02322     unz_s* s;
02323     uLong uReadThis ;
02324     if (file==NULL)
02325         return UNZ_PARAMERROR;
02326     s=(unz_s*)file;
02327 
02328     uReadThis = uSizeBuf;
02329     if (uReadThis>s->gi.size_comment)
02330         uReadThis = s->gi.size_comment;
02331 
02332     if (fseek(s->file,s->central_pos+22,SEEK_SET)!=0)
02333         return UNZ_ERRNO;
02334 
02335     if (uReadThis>0)
02336     {
02337       *szComment='\0';
02338       if (fread(szComment,(uInt)uReadThis,1,s->file)!=1)
02339         return UNZ_ERRNO;
02340     }
02341 
02342     if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment))
02343         *(szComment+s->gi.size_comment)='\0';
02344     return (int)uReadThis;
02345 }
02346 
02347 /* crc32.c -- compute the CRC-32 of a data stream
02348  * Copyright (C) 1995-1998 Mark Adler
02349  * For conditions of distribution and use, see copyright notice in zlib.h 
02350  */
02351 
02352 /* @(#) $Id: unzip.cpp,v 1.1.1.3 2000/01/11 16:37:27 ttimo Exp $ */
02353 
02354 #ifdef DYNAMIC_CRC_TABLE
02355 
02356 static int crc_table_empty = 1;
02357 static uLong crc_table[256];
02358 static void make_crc_table OF((void));
02359 
02360 /*
02361   Generate a table for a byte-wise 32-bit CRC calculation on the polynomial:
02362   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.
02363 
02364   Polynomials over GF(2) are represented in binary, one bit per coefficient,
02365   with the lowest powers in the most significant bit.  Then adding polynomials
02366   is just exclusive-or, and multiplying a polynomial by x is a right shift by
02367   one.  If we call the above polynomial p, and represent a byte as the
02368   polynomial q, also with the lowest power in the most significant bit (so the
02369   byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
02370   where a mod b means the remainder after dividing a by b.
02371 
02372   This calculation is done using the shift-register method of multiplying and
02373   taking the remainder.  The register is initialized to zero, and for each
02374   incoming bit, x^32 is added mod p to the register if the bit is a one (where
02375   x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
02376   x (which is shifting right by one and adding x^32 mod p if the bit shifted
02377   out is a one).  We start with the highest power (least significant bit) of
02378   q and repeat for all eight bits of q.
02379 
02380   The table is simply the CRC of all possible eight bit values.  This is all
02381   the information needed to generate CRC's on data a byte at a time for all
02382   combinations of CRC register values and incoming bytes.
02383 */
02384 static void make_crc_table()
02385 {
02386   uLong c;
02387   int n, k;
02388   uLong poly;            /* polynomial exclusive-or pattern */
02389   /* terms of polynomial defining this crc (except x^32): */
02390   static const Byte p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
02391 
02392   /* make exclusive-or pattern from polynomial (0xedb88320L) */
02393   poly = 0L;
02394   for (n = 0; n < sizeof(p)/sizeof(Byte); n++)
02395     poly |= 1L << (31 - p[n]);
02396  
02397   for (n = 0; n < 256; n++)
02398   {
02399     c = (uLong)n;
02400     for (k = 0; k < 8; k++)
02401       c = c & 1 ? poly ^ (c >> 1) : c >> 1;
02402     crc_table[n] = c;
02403   }
02404   crc_table_empty = 0;
02405 }
02406 #else
02407 /* ========================================================================
02408  * Table of CRC-32's of all single-byte values (made by make_crc_table)
02409  */
02410 static const uLong crc_table[256] = {
02411   0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
02412   0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
02413   0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
02414   0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
02415   0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
02416   0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
02417   0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
02418   0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
02419   0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
02420   0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
02421   0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
02422   0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
02423   0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
02424   0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
02425   0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
02426   0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
02427   0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
02428   0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
02429   0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
02430   0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
02431   0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
02432   0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
02433   0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
02434   0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
02435   0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
02436   0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
02437   0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
02438   0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
02439   0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
02440   0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
02441   0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
02442   0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
02443   0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
02444   0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
02445   0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
02446   0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
02447   0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
02448   0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
02449   0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
02450   0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
02451   0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
02452   0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
02453   0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
02454   0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
02455   0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
02456   0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
02457   0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
02458   0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
02459   0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
02460   0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
02461   0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
02462   0x2d02ef8dL
02463 };
02464 #endif
02465 
02466 /* =========================================================================
02467  * This function can be used by asm versions of crc32()
02468  */
02469 const uLong * get_crc_table()
02470 {
02471 #ifdef DYNAMIC_CRC_TABLE
02472   if (crc_table_empty) make_crc_table();
02473 #endif
02474   return (const uLong *)crc_table;
02475 }
02476 
02477 /* ========================================================================= */
02478 #define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
02479 #define DO2(buf)  DO1(buf); DO1(buf);
02480 #define DO4(buf)  DO2(buf); DO2(buf);
02481 #define DO8(buf)  DO4(buf); DO4(buf);
02482 
02483 /* ========================================================================= */
02484 uLong crc32(uLong crc, const Byte *buf, uInt len)
02485 {
02486     if (buf == Z_NULL) return 0L;
02487 #ifdef DYNAMIC_CRC_TABLE
02488     if (crc_table_empty)
02489       make_crc_table();
02490 #endif
02491     crc = crc ^ 0xffffffffL;
02492     while (len >= 8)
02493     {
02494       DO8(buf);
02495       len -= 8;
02496     }
02497     if (len) do {
02498       DO1(buf);
02499     } while (--len);
02500     return crc ^ 0xffffffffL;
02501 }
02502 
02503 /* infblock.h -- header to use infblock.c
02504  * Copyright (C) 1995-1998 Mark Adler
02505  * For conditions of distribution and use, see copyright notice in zlib.h 
02506  */
02507 
02508 /* WARNING: this file should *not* be used by applications. It is
02509    part of the implementation of the compression library and is
02510    subject to change. Applications should only use zlib.h.
02511  */
02512 
02513 struct inflate_blocks_state;
02514 typedef struct inflate_blocks_state inflate_blocks_statef;
02515 
02516 extern inflate_blocks_statef * inflate_blocks_new OF((
02517     z_streamp z,
02518     check_func c,               /* check function */
02519     uInt w));                   /* window size */
02520 
02521 extern int inflate_blocks OF((
02522     inflate_blocks_statef *,
02523     z_streamp ,
02524     int));                      /* initial return code */
02525 
02526 extern void inflate_blocks_reset OF((
02527     inflate_blocks_statef *,
02528     z_streamp ,
02529     uLong *));                  /* check value on output */
02530 
02531 extern int inflate_blocks_free OF((
02532     inflate_blocks_statef *,
02533     z_streamp));
02534 
02535 extern void inflate_set_dictionary OF((
02536     inflate_blocks_statef *s,
02537     const Byte *d,  /* dictionary */
02538     uInt  n));       /* dictionary length */
02539 
02540 extern int inflate_blocks_sync_point OF((
02541     inflate_blocks_statef *s));
02542 
02543 /* simplify the use of the inflate_huft type with some defines */
02544 #define exop word.what.Exop
02545 #define bits word.what.Bits
02546 
02547 /* Table for deflate from PKZIP's appnote.txt. */
02548 static const uInt border[] = { /* Order of the bit length code lengths */
02549         16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
02550 
02551 /* inftrees.h -- header to use inftrees.c
02552  * Copyright (C) 1995-1998 Mark Adler
02553  * For conditions of distribution and use, see copyright notice in zlib.h 
02554  */
02555 
02556 /* WARNING: this file should *not* be used by applications. It is
02557    part of the implementation of the compression library and is
02558    subject to change. Applications should only use zlib.h.
02559  */
02560 
02561 /* Huffman code lookup table entry--this entry is four bytes for machines
02562    that have 16-bit pointers (e.g. PC's in the small or medium model). */
02563 
02564 typedef struct inflate_huft_s inflate_huft;
02565 
02566 struct inflate_huft_s {
02567   union {
02568     struct {
02569       Byte Exop;        /* number of extra bits or operation */
02570       Byte Bits;        /* number of bits in this code or subcode */
02571     } what;
02572     uInt pad;           /* pad structure to a power of 2 (4 bytes for */
02573   } word;               /*  16-bit, 8 bytes for 32-bit int's) */
02574   uInt base;            /* literal, length base, distance base,
02575                            or table offset */
02576 };
02577 
02578 /* Maximum size of dynamic tree.  The maximum found in a long but non-
02579    exhaustive search was 1004 huft structures (850 for length/literals
02580    and 154 for distances, the latter actually the result of an
02581    exhaustive search).  The actual maximum is not known, but the
02582    value below is more than safe. */
02583 #define MANY 1440
02584 
02585 extern int inflate_trees_bits OF((
02586     uInt *,                    /* 19 code lengths */
02587     uInt *,                    /* bits tree desired/actual depth */
02588     inflate_huft * *,       /* bits tree result */
02589     inflate_huft *,             /* space for trees */
02590     z_streamp));                /* for messages */
02591 
02592 extern int inflate_trees_dynamic OF((
02593     uInt,                       /* number of literal/length codes */
02594     uInt,                       /* number of distance codes */
02595     uInt *,                    /* that many (total) code lengths */
02596     uInt *,                    /* literal desired/actual bit depth */
02597     uInt *,                    /* distance desired/actual bit depth */
02598     inflate_huft * *,       /* literal/length tree result */
02599     inflate_huft * *,       /* distance tree result */
02600     inflate_huft *,             /* space for trees */
02601     z_streamp));                /* for messages */
02602 
02603 extern int inflate_trees_fixed OF((
02604     uInt *,                    /* literal desired/actual bit depth */
02605     uInt *,                    /* distance desired/actual bit depth */
02606     inflate_huft * *,       /* literal/length tree result */
02607     inflate_huft * *,       /* distance tree result */
02608     z_streamp));                /* for memory allocation */
02609 
02610 
02611 /* infcodes.h -- header to use infcodes.c
02612  * Copyright (C) 1995-1998 Mark Adler
02613  * For conditions of distribution and use, see copyright notice in zlib.h 
02614  */
02615 
02616 /* WARNING: this file should *not* be used by applications. It is
02617    part of the implementation of the compression library and is
02618    subject to change. Applications should only use zlib.h.
02619  */
02620 
02621 struct inflate_codes_state;
02622 typedef struct inflate_codes_state inflate_codes_statef;
02623 
02624 extern inflate_codes_statef *inflate_codes_new OF((
02625     uInt, uInt,
02626     inflate_huft *, inflate_huft *,
02627     z_streamp ));
02628 
02629 extern int inflate_codes OF((
02630     inflate_blocks_statef *,
02631     z_streamp ,
02632     int));
02633 
02634 extern void inflate_codes_free OF((
02635     inflate_codes_statef *,
02636     z_streamp ));
02637 
02638 /* infutil.h -- types and macros common to blocks and codes
02639  * Copyright (C) 1995-1998 Mark Adler
02640  * For conditions of distribution and use, see copyright notice in zlib.h 
02641  */
02642 
02643 /* WARNING: this file should *not* be used by applications. It is
02644    part of the implementation of the compression library and is
02645    subject to change. Applications should only use zlib.h.
02646  */
02647 
02648 #ifndef _INFUTIL_H
02649 #define _INFUTIL_H
02650 
02651 typedef enum {
02652       TYPE,     /* get type bits (3, including end bit) */
02653       LENS,     /* get lengths for stored */
02654       STORED,   /* processing stored block */
02655       TABLE,    /* get table lengths */
02656       BTREE,    /* get bit lengths tree for a dynamic block */
02657       DTREE,    /* get length, distance trees for a dynamic block */
02658       CODES,    /* processing fixed or dynamic block */
02659       DRY,      /* output remaining window bytes */
02660       DONE,     /* finished last block, done */
02661       BAD}      /* got a data error--stuck here */
02662 inflate_block_mode;
02663 
02664 /* inflate blocks semi-private state */
02665 struct inflate_blocks_state {
02666 
02667   /* mode */
02668   inflate_block_mode  mode;     /* current inflate_block mode */
02669 
02670   /* mode dependent information */
02671   union {
02672     uInt left;          /* if STORED, bytes left to copy */
02673     struct {
02674       uInt table;               /* table lengths (14 bits) */
02675       uInt index;               /* index into blens (or border) */
02676       uInt *blens;             /* bit lengths of codes */
02677       uInt bb;                  /* bit length tree depth */
02678       inflate_huft *tb;         /* bit length decoding tree */
02679     } trees;            /* if DTREE, decoding info for trees */
02680     struct {
02681       inflate_codes_statef 
02682          *codes;
02683     } decode;           /* if CODES, current state */
02684   } sub;                /* submode */
02685   uInt last;            /* true if this block is the last block */
02686 
02687   /* mode independent information */
02688   uInt bitk;            /* bits in bit buffer */
02689   uLong bitb;           /* bit buffer */
02690   inflate_huft *hufts;  /* single malloc for tree space */
02691   Byte *window;        /* sliding window */
02692   Byte *end;           /* one byte after sliding window */
02693   Byte *read;          /* window read pointer */
02694   Byte *write;         /* window write pointer */
02695   check_func checkfn;   /* check function */
02696   uLong check;          /* check on output */
02697 
02698 };
02699 
02700 
02701 /* defines for inflate input/output */
02702 /*   update pointers and return */
02703 #define UPDBITS {s->bitb=b;s->bitk=k;}
02704 #define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;}
02705 #define UPDOUT {s->write=q;}
02706 #define UPDATE {UPDBITS UPDIN UPDOUT}
02707 #define LEAVE {UPDATE return inflate_flush(s,z,r);}
02708 /*   get bytes and bits */
02709 #define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
02710 #define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
02711 #define NEXTBYTE (n--,*p++)
02712 #define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
02713 #define DUMPBITS(j) {b>>=(j);k-=(j);}
02714 /*   output bytes */
02715 #define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
02716 #define LOADOUT {q=s->write;m=(uInt)WAVAIL;}
02717 #define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}
02718 #define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
02719 #define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}
02720 #define OUTBYTE(a) {*q++=(Byte)(a);m--;}
02721 /*   load static pointers */
02722 #define LOAD {LOADIN LOADOUT}
02723 
02724 /* masks for lower bits (size given to avoid silly warnings with Visual C++) */
02725 extern uInt inflate_mask[17];
02726 
02727 /* copy as much as possible from the sliding window to the output area */
02728 extern int inflate_flush OF((
02729     inflate_blocks_statef *,
02730     z_streamp ,
02731     int));
02732 
02733 #endif
02734 
02735                                 
02736 /*
02737    Notes beyond the 1.93a appnote.txt:
02738 
02739    1. Distance pointers never point before the beginning of the output
02740       stream.
02741    2. Distance pointers can point back across blocks, up to 32k away.
02742    3. There is an implied maximum of 7 bits for the bit length table and
02743       15 bits for the actual data.
02744    4. If only one code exists, then it is encoded using one bit.  (Zero
02745       would be more efficient, but perhaps a little confusing.)  If two
02746       codes exist, they are coded using one bit each (0 and 1).
02747    5. There is no way of sending zero distance codes--a dummy must be
02748       sent if there are none.  (History: a pre 2.0 version of PKZIP would
02749       store blocks with no distance codes, but this was discovered to be
02750       too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
02751       zero distance codes, which is sent as one code of zero bits in
02752       length.
02753    6. There are up to 286 literal/length codes.  Code 256 represents the
02754       end-of-block.  Note however that the static length tree defines
02755       288 codes just to fill out the Huffman codes.  Codes 286 and 287
02756       cannot be used though, since there is no length base or extra bits
02757       defined for them.  Similarily, there are up to 30 distance codes.
02758       However, static trees define 32 codes (all 5 bits) to fill out the
02759       Huffman codes, but the last two had better not show up in the data.
02760    7. Unzip can check dynamic Huffman blocks for complete code sets.
02761       The exception is that a single code would not be complete (see #4).
02762    8. The five bits following the block type is really the number of
02763       literal codes sent minus 257.
02764    9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
02765       (1+6+6).  Therefore, to output three times the length, you output
02766       three codes (1+1+1), whereas to output four times the same length,
02767       you only need two codes (1+3).  Hmm.
02768   10. In the tree reconstruction algorithm, Code = Code + Increment
02769       only if BitLength(i) is not zero.  (Pretty obvious.)
02770   11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
02771   12. Note: length code 284 can represent 227-258, but length code 285
02772       really is 258.  The last length deserves its own, short code
02773       since it gets used a lot in very redundant files.  The length
02774       258 is special since 258 - 3 (the min match length) is 255.
02775   13. The literal/length and distance code bit lengths are read as a
02776       single stream of lengths.  It is possible (and advantageous) for
02777       a repeat code (16, 17, or 18) to go across the boundary between
02778       the two sets of lengths.
02779  */
02780 
02781 
02782 void inflate_blocks_reset(inflate_blocks_statef *s, z_streamp z, uLong *c)
02783 {
02784   if (c != Z_NULL)
02785     *c = s->check;
02786   if (s->mode == BTREE || s->mode == DTREE)
02787     ZFREE(z, s->sub.trees.blens);
02788   if (s->mode == CODES)
02789     inflate_codes_free(s->sub.decode.codes, z);
02790   s->mode = TYPE;
02791   s->bitk = 0;
02792   s->bitb = 0;
02793   s->read = s->write = s->window;
02794   if (s->checkfn != Z_NULL)
02795     z->adler = s->check = (*s->checkfn)(0L, (const Byte *)Z_NULL, 0);
02796   Tracev(("inflate:   blocks reset\n"));
02797 }
02798 
02799 
02800 inflate_blocks_statef *inflate_blocks_new(z_streamp z, check_func c, uInt w)
02801 {
02802   inflate_blocks_statef *s;
02803 
02804   if ((s = (inflate_blocks_statef *)ZALLOC
02805        (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
02806     return s;
02807   if ((s->hufts =
02808        (inflate_huft *)ZALLOC(z, sizeof(inflate_huft), MANY)) == Z_NULL)
02809   {
02810     ZFREE(z, s);
02811     return Z_NULL;
02812   }
02813   if ((s->window = (Byte *)ZALLOC(z, 1, w)) == Z_NULL)
02814   {
02815     ZFREE(z, s->hufts);
02816     ZFREE(z, s);
02817     return Z_NULL;
02818   }
02819   s->end = s->window + w;
02820   s->checkfn = c;
02821   s->mode = TYPE;
02822   Tracev(("inflate:   blocks allocated\n"));
02823   inflate_blocks_reset(s, z, Z_NULL);
02824   return s;
02825 }
02826 
02827 
02828 int inflate_blocks(inflate_blocks_statef *s, z_streamp z, int r)
02829 {
02830   uInt t;               /* temporary storage */
02831   uLong b;              /* bit buffer */
02832   uInt k;               /* bits in bit buffer */
02833   Byte *p;             /* input data pointer */
02834   uInt n;               /* bytes available there */
02835   Byte *q;             /* output window write pointer */
02836   uInt m;               /* bytes to end of window or read pointer */
02837 
02838   /* copy input/output information to locals (UPDATE macro restores) */
02839   LOAD
02840 
02841   /* process input based on current state */
02842   while (1) switch (s->mode)
02843   {
02844     case TYPE:
02845       NEEDBITS(3)
02846       t = (uInt)b & 7;
02847       s->last = t & 1;
02848       switch (t >> 1)
02849       {
02850         case 0:                         /* stored */
02851           Tracev(("inflate:     stored block%s\n",
02852                  s->last ? " (last)" : ""));
02853           DUMPBITS(3)
02854           t = k & 7;                    /* go to byte boundary */
02855           DUMPBITS(t)
02856           s->mode = LENS;               /* get length of stored block */
02857           break;
02858         case 1:                         /* fixed */
02859           Tracev(("inflate:     fixed codes block%s\n",
02860                  s->last ? " (last)" : ""));
02861           {
02862             uInt bl, bd;
02863             inflate_huft *tl, *td;
02864 
02865             inflate_trees_fixed(&bl, &bd, &tl, &td, z);
02866             s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
02867             if (s->sub.decode.codes == Z_NULL)
02868             {
02869               r = Z_MEM_ERROR;
02870               LEAVE
02871             }
02872           }
02873           DUMPBITS(3)
02874           s->mode = CODES;
02875           break;
02876         case 2:                         /* dynamic */
02877           Tracev(("inflate:     dynamic codes block%s\n",
02878                  s->last ? " (last)" : ""));
02879           DUMPBITS(3)
02880           s->mode = TABLE;
02881           break;
02882         case 3:                         /* illegal */
02883           DUMPBITS(3)
02884           s->mode = BAD;
02885           z->msg = (char*)"invalid block type";
02886           r = Z_DATA_ERROR;
02887           LEAVE
02888       }
02889       break;
02890     case LENS:
02891       NEEDBITS(32)
02892       if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
02893       {
02894         s->mode = BAD;
02895         z->msg = (char*)"invalid stored block lengths";
02896         r = Z_DATA_ERROR;
02897         LEAVE
02898       }
02899       s->sub.left = (uInt)b & 0xffff;
02900       b = k = 0;                      /* dump bits */
02901       Tracev(("inflate:       stored length %u\n", s->sub.left));
02902       s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
02903       break;
02904     case STORED:
02905       if (n == 0)
02906         LEAVE
02907       NEEDOUT
02908       t = s->sub.left;
02909       if (t > n) t = n;
02910       if (t > m) t = m;
02911       zmemcpy(q, p, t);
02912       p += t;  n -= t;
02913       q += t;  m -= t;
02914       if ((s->sub.left -= t) != 0)
02915         break;
02916       Tracev(("inflate:       stored end, %lu total out\n",
02917               z->total_out + (q >= s->read ? q - s->read :
02918               (s->end - s->read) + (q - s->window))));
02919       s->mode = s->last ? DRY : TYPE;
02920       break;
02921     case TABLE:
02922       NEEDBITS(14)
02923       s->sub.trees.table = t = (uInt)b & 0x3fff;
02924 #ifndef PKZIP_BUG_WORKAROUND
02925       if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
02926       {
02927         s->mode = BAD;
02928         z->msg = (char*)"too many length or distance symbols";
02929         r = Z_DATA_ERROR;
02930         LEAVE
02931       }
02932 #endif
02933       t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
02934       if ((s->sub.trees.blens = (uInt*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
02935       {
02936         r = Z_MEM_ERROR;
02937         LEAVE
02938       }
02939       DUMPBITS(14)
02940       s->sub.trees.index = 0;
02941       Tracev(("inflate:       table sizes ok\n"));
02942       s->mode = BTREE;
02943     case BTREE:
02944       while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
02945       {
02946         NEEDBITS(3)
02947         s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
02948         DUMPBITS(3)
02949       }
02950       while (s->sub.trees.index < 19)
02951         s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
02952       s->sub.trees.bb = 7;
02953       t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
02954                              &s->sub.trees.tb, s->hufts, z);
02955       if (t != Z_OK)
02956       {
02957         ZFREE(z, s->sub.trees.blens);
02958         r = t;
02959         if (r == Z_DATA_ERROR)
02960           s->mode = BAD;
02961         LEAVE
02962       }
02963       s->sub.trees.index = 0;
02964       Tracev(("inflate:       bits tree ok\n"));
02965       s->mode = DTREE;
02966     case DTREE:
02967       while (t = s->sub.trees.table,
02968              s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
02969       {
02970         inflate_huft *h;
02971         uInt i, j, c;
02972 
02973         t = s->sub.trees.bb;
02974         NEEDBITS(t)
02975         h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
02976         t = h->bits;
02977         c = h->base;
02978         if (c < 16)
02979         {
02980           DUMPBITS(t)
02981           s->sub.trees.blens[s->sub.trees.index++] = c;
02982         }
02983         else /* c == 16..18 */
02984         {
02985           i = c == 18 ? 7 : c - 14;
02986           j = c == 18 ? 11 : 3;
02987           NEEDBITS(t + i)
02988           DUMPBITS(t)
02989           j += (uInt)b & inflate_mask[i];
02990           DUMPBITS(i)
02991           i = s->sub.trees.index;
02992           t = s->sub.trees.table;
02993           if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
02994               (c == 16 && i < 1))
02995           {
02996             ZFREE(z, s->sub.trees.blens);
02997             s->mode = BAD;
02998             z->msg = (char*)"invalid bit length repeat";
02999             r = Z_DATA_ERROR;
03000             LEAVE
03001           }
03002           c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
03003           do {
03004             s->sub.trees.blens[i++] = c;
03005           } while (--j);
03006           s->sub.trees.index = i;
03007         }
03008       }
03009       s->sub.trees.tb = Z_NULL;
03010       {
03011         uInt bl, bd;
03012         inflate_huft *tl, *td;
03013         inflate_codes_statef *c;
03014 
03015         bl = 9;         /* must be <= 9 for lookahead assumptions */
03016         bd = 6;         /* must be <= 9 for lookahead assumptions */
03017         t = s->sub.trees.table;
03018         t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
03019                                   s->sub.trees.blens, &bl, &bd, &tl, &td,
03020                                   s->hufts, z);
03021         ZFREE(z, s->sub.trees.blens);
03022         if (t != Z_OK)
03023         {
03024           if (t == (uInt)Z_DATA_ERROR)
03025             s->mode = BAD;
03026           r = t;
03027           LEAVE
03028         }
03029         Tracev(("inflate:       trees ok\n"));
03030         if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
03031         {
03032           r = Z_MEM_ERROR;
03033           LEAVE
03034         }
03035         s->sub.decode.codes = c;
03036       }
03037       s->mode = CODES;
03038     case CODES:
03039       UPDATE
03040       if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
03041         return inflate_flush(s, z, r);
03042       r = Z_OK;
03043       inflate_codes_free(s->sub.decode.codes, z);
03044       LOAD
03045       Tracev(("inflate:       codes end, %lu total out\n",
03046               z->total_out + (q >= s->read ? q - s->read :
03047               (s->end - s->read) + (q - s->window))));
03048       if (!s->last)
03049       {
03050         s->mode = TYPE;
03051         break;
03052       }
03053       s->mode = DRY;
03054     case DRY:
03055       FLUSH
03056       if (s->read != s->write)
03057         LEAVE
03058       s->mode = DONE;
03059     case DONE:
03060       r = Z_STREAM_END;
03061       LEAVE
03062     case BAD:
03063       r = Z_DATA_ERROR;
03064       LEAVE
03065     default:
03066       r = Z_STREAM_ERROR;
03067       LEAVE
03068   }
03069 }
03070 
03071 
03072 int inflate_blocks_free(inflate_blocks_statef *s, z_streamp z)
03073 {
03074   inflate_blocks_reset(s, z, Z_NULL);
03075   ZFREE(z, s->window);
03076   ZFREE(z, s->hufts);
03077   ZFREE(z, s);
03078   Tracev(("inflate:   blocks freed\n"));
03079   return Z_OK;
03080 }
03081 
03082 
03083 void inflate_set_dictionary(inflate_blocks_statef *s, const Byte *d, uInt n)
03084 {
03085   zmemcpy(s->window, d, n);
03086   s->read = s->write = s->window + n;
03087 }
03088 
03089 
03090 /* Returns true if inflate is currently at the end of a block generated
03091  * by Z_SYNC_FLUSH or Z_FULL_FLUSH. 
03092  * IN assertion: s != Z_NULL
03093  */
03094 int inflate_blocks_sync_point(inflate_blocks_statef *s)
03095 {
03096   return s->mode == LENS;
03097 }
03098 
03099 
03100 /* And'ing with mask[n] masks the lower n bits */
03101 uInt inflate_mask[17] = {
03102     0x0000,
03103     0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
03104     0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
03105 };
03106 
03107 
03108 /* copy as much as possible from the sliding window to the output area */
03109 int inflate_flush(inflate_blocks_statef *s, z_streamp z, int r)
03110 {
03111   uInt n;
03112   Byte *p;
03113   Byte *q;
03114 
03115   /* static copies of source and destination pointers */
03116   p = z->next_out;
03117   q = s->read;
03118 
03119   /* compute number of bytes to copy as as end of window */
03120   n = (uInt)((q <= s->write ? s->write : s->end) - q);
03121   if (n > z->avail_out) n = z->avail_out;
03122   if (n && r == Z_BUF_ERROR) r = Z_OK;
03123 
03124   /* update counters */
03125   z->avail_out -= n;
03126   z->total_out += n;
03127 
03128   /* update check information */
03129   if (s->checkfn != Z_NULL)
03130     z->adler = s->check = (*s->checkfn)(s->check, q, n);
03131 
03132   /* copy as as end of window */
03133   zmemcpy(p, q, n);
03134   p += n;
03135   q += n;
03136 
03137   /* see if more to copy at beginning of window */
03138   if (q == s->end)
03139   {
03140     /* wrap pointers */
03141     q = s->window;
03142     if (s->write == s->end)
03143       s->write = s->window;
03144 
03145     /* compute bytes to copy */
03146     n = (uInt)(s->write - q);
03147     if (n > z->avail_out) n = z->avail_out;
03148     if (n && r == Z_BUF_ERROR) r = Z_OK;
03149 
03150     /* update counters */
03151     z->avail_out -= n;
03152     z->total_out += n;
03153 
03154     /* update check information */
03155     if (s->checkfn != Z_NULL)
03156       z->adler = s->check = (*s->checkfn)(s->check, q, n);
03157 
03158     /* copy */
03159     zmemcpy(p, q, n);
03160     p += n;
03161     q += n;
03162   }
03163 
03164   /* update pointers */
03165   z->next_out = p;
03166   s->read = q;
03167 
03168   /* done */
03169   return r;
03170 }
03171 
03172 /* inftrees.c -- generate Huffman trees for efficient decoding
03173  * Copyright (C) 1995-1998 Mark Adler
03174  * For conditions of distribution and use, see copyright notice in zlib.h 
03175  */
03176 
03177 const char inflate_copyright[] =
03178    " inflate 1.1.3 Copyright 1995-1998 Mark Adler ";
03179 /*
03180   If you use the zlib library in a product, an acknowledgment is welcome
03181   in the documentation of your product. If for some reason you cannot
03182   include such an acknowledgment, I would appreciate that you keep this
03183   copyright string in the executable of your product.
03184  */
03185 
03186 /* simplify the use of the inflate_huft type with some defines */
03187 #define exop word.what.Exop
03188 #define bits word.what.Bits
03189 
03190 
03191 static int huft_build OF((
03192     uInt *,             /* code lengths in bits */
03193     uInt,               /* number of codes */
03194     uInt,               /* number of "simple" codes */
03195     const uInt *,       /* list of base values for non-simple codes */
03196     const uInt *,       /* list of extra bits for non-simple codes */
03197     inflate_huft **,    /* result: starting table */
03198     uInt *,             /* maximum lookup bits (returns actual) */
03199     inflate_huft *,     /* space for trees */
03200     uInt *,             /* hufts used in space */
03201     uInt * ));          /* space for values */
03202 
03203 /* Tables for deflate from PKZIP's appnote.txt. */
03204 static const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
03205         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
03206         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
03207         /* see note #13 above about 258 */
03208 static const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
03209         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
03210         3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
03211 static const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
03212         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
03213         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
03214         8193, 12289, 16385, 24577};
03215 static const uInt cpdext[30] = { /* Extra bits for distance codes */
03216         0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
03217         7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
03218         12, 12, 13, 13};
03219 
03220 /*
03221    Huffman code decoding is performed using a multi-level table lookup.
03222    The fastest way to decode is to simply build a lookup table whose
03223    size is determined by the longest code.  However, the time it takes
03224    to build this table can also be a factor if the data being decoded
03225    is not very long.  The most common codes are necessarily the
03226    shortest codes, so those codes dominate the decoding time, and hence
03227    the speed.  The idea is you can have a shorter table that decodes the
03228    shorter, more probable codes, and then point to subsidiary tables for
03229    the longer codes.  The time it costs to decode the longer codes is
03230    then traded against the time it takes to make longer tables.
03231 
03232    This results of this trade are in the variables lbits and dbits
03233    below.  lbits is the number of bits the first level table for literal/
03234    length codes can decode in one step, and dbits is the same thing for
03235    the distance codes.  Subsequent tables are also less than or equal to
03236    those sizes.  These values may be adjusted either when all of the
03237    codes are shorter than that, in which case the longest code length in
03238    bits is used, or when the shortest code is *longer* than the requested
03239    table size, in which case the length of the shortest code in bits is
03240    used.
03241 
03242    There are two different values for the two tables, since they code a
03243    different number of possibilities each.  The literal/length table
03244    codes 286 possible values, or in a flat code, a little over eight
03245    bits.  The distance table codes 30 possible values, or a little less
03246    than five bits, flat.  The optimum values for speed end up being
03247    about one bit more than those, so lbits is 8+1 and dbits is 5+1.
03248    The optimum values may differ though from machine to machine, and
03249    possibly even between compilers.  Your mileage may vary.
03250  */
03251 
03252 
03253 /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
03254 #define BMAX 15         /* maximum bit length of any code */
03255 
03256 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)
03257 //uInt *b;               /* code lengths in bits (all assumed <= BMAX) */
03258 //uInt n;                 /* number of codes (assumed <= 288) */
03259 //uInt s;                 /* number of simple-valued codes (0..s-1) */
03260 //const uInt *d;         /* list of base values for non-simple codes */
03261 //const uInt *e;         /* list of extra bits for non-simple codes */
03262 //inflate_huft ** t;        /* result: starting table */
03263 //uInt *m;               /* maximum lookup bits, returns actual */
03264 //inflate_huft *hp;       /* space for trees */
03265 //uInt *hn;               /* hufts used in space */
03266 //uInt *v;               /* working area: values in order of bit length */
03267 /* Given a list of code lengths and a maximum table size, make a set of
03268    tables to decode that set of codes.  Return Z_OK on success, Z_BUF_ERROR
03269    if the given code set is incomplete (the tables are still built in this
03270    case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
03271    lengths), or Z_MEM_ERROR if not enough memory. */
03272 {
03273 
03274   uInt a;                       /* counter for codes of length k */
03275   uInt c[BMAX+1];               /* bit length count table */
03276   uInt f;                       /* i repeats in table every f entries */
03277   int g;                        /* maximum code length */
03278   int h;                        /* table level */
03279   register uInt i;              /* counter, current code */
03280   register uInt j;              /* counter */
03281   register int k;               /* number of bits in current code */
03282   int l;                        /* bits per table (returned in m) */
03283   uInt mask;                    /* (1 << w) - 1, to avoid cc -O bug on HP */
03284   register uInt *p;            /* pointer into c[], b[], or v[] */
03285   inflate_huft *q;              /* points to current table */
03286   struct inflate_huft_s r;      /* table entry for structure assignment */
03287   inflate_huft *u[BMAX];        /* table stack */
03288   register int w;               /* bits before this table == (l * h) */
03289   uInt x[BMAX+1];               /* bit offsets, then code stack */
03290   uInt *xp;                    /* pointer into x */
03291   int y;                        /* number of dummy codes added */
03292   uInt z;                       /* number of entries in current table */
03293 
03294 
03295   /* Generate counts for each bit length */
03296   p = c;
03297 #define C0 *p++ = 0;
03298 #define C2 C0 C0 C0 C0
03299 #define C4 C2 C2 C2 C2
03300   C4                            /* clear c[]--assume BMAX+1 is 16 */
03301   p = b;  i = n;
03302   do {
03303     c[*p++]++;                  /* assume all entries <= BMAX */
03304   } while (--i);
03305   if (c[0] == n)                /* null input--all zero length codes */
03306   {
03307     *t = (inflate_huft *)Z_NULL;
03308     *m = 0;
03309     return Z_OK;
03310   }
03311 
03312 
03313   /* Find minimum and maximum length, bound *m by those */
03314   l = *m;
03315   for (j = 1; j <= BMAX; j++)
03316     if (c[j])
03317       break;
03318   k = j;                        /* minimum code length */
03319   if ((uInt)l < j)
03320     l = j;
03321   for (i = BMAX; i; i--)
03322     if (c[i])
03323       break;
03324   g = i;                        /* maximum code length */
03325   if ((uInt)l > i)
03326     l = i;
03327   *m = l;
03328 
03329 
03330   /* Adjust last length count to fill out codes, if needed */
03331   for (y = 1 << j; j < i; j++, y <<= 1)
03332     if ((y -= c[j]) < 0)
03333       return Z_DATA_ERROR;
03334   if ((y -= c[i]) < 0)
03335     return Z_DATA_ERROR;
03336   c[i] += y;
03337 
03338 
03339   /* Generate starting offsets into the value table for each length */
03340   x[1] = j = 0;
03341   p = c + 1;  xp = x + 2;
03342   while (--i) {                 /* note that i == g from above */
03343     *xp++ = (j += *p++);
03344   }
03345 
03346 
03347   /* Make a table of values in order of bit lengths */
03348   p = b;  i = 0;
03349   do {
03350     if ((j = *p++) != 0)
03351       v[x[j]++] = i;
03352   } while (++i < n);
03353   n = x[g];                     /* set n to length of v */
03354 
03355 
03356   /* Generate the Huffman codes and for each, make the table entries */
03357   x[0] = i = 0;                 /* first Huffman code is zero */
03358   p = v;                        /* grab values in bit order */
03359   h = -1;                       /* no tables yet--level -1 */
03360   w = -l;                       /* bits decoded == (l * h) */
03361   u[0] = (inflate_huft *)Z_NULL;        /* just to keep compilers happy */
03362   q = (inflate_huft *)Z_NULL;   /* ditto */
03363   z = 0;                        /* ditto */
03364 
03365   /* go through the bit lengths (k already is bits in shortest code) */
03366   for (; k <= g; k++)
03367   {
03368     a = c[k];
03369     while (a--)
03370     {
03371       /* here i is the Huffman code of length k bits for value *p */
03372       /* make tables up to required level */
03373       while (k > w + l)
03374       {
03375         h++;
03376         w += l;                 /* previous table always l bits */
03377 
03378         /* compute minimum size table less than or equal to l bits */
03379         z = g - w;
03380         z = z > (uInt)l ? l : z;        /* table size upper limit */
03381         if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
03382         {                       /* too few codes for k-w bit table */
03383           f -= a + 1;           /* deduct codes from patterns left */
03384           xp = c + k;
03385           if (j < z)
03386             while (++j < z)     /* try smaller tables up to z bits */
03387             {
03388               if ((f <<= 1) <= *++xp)
03389                 break;          /* enough codes to use up j bits */
03390               f -= *xp;         /* else deduct codes from patterns */
03391             }
03392         }
03393         z = 1 << j;             /* table entries for j-bit table */
03394 
03395         /* allocate new table */
03396         if (*hn + z > MANY)     /* (note: doesn't matter for fixed) */
03397           return Z_MEM_ERROR;   /* not enough memory */
03398         u[h] = q = hp + *hn;
03399         *hn += z;
03400 
03401         /* connect to last table, if there is one */
03402         if (h)
03403         {
03404           x[h] = i;             /* save pattern for backing up */
03405           r.bits = (Byte)l;     /* bits to dump before this table */
03406           r.exop = (Byte)j;     /* bits in this table */
03407           j = i >> (w - l);
03408           r.base = (uInt)(q - u[h-1] - j);   /* offset to this table */
03409           u[h-1][j] = r;        /* connect to last table */
03410         }
03411         else
03412           *t = q;               /* first table is returned result */
03413       }
03414 
03415       /* set up table entry in r */
03416       r.bits = (Byte)(k - w);
03417       if (p >= v + n)
03418         r.exop = 128 + 64;      /* out of values--invalid code */
03419       else if (*p < s)
03420       {
03421         r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);     /* 256 is end-of-block */
03422         r.base = *p++;          /* simple code is just the value */
03423       }
03424       else
03425       {
03426         r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
03427         r.base = d[*p++ - s];
03428       }
03429 
03430       /* fill code-like entries with r */
03431       f = 1 << (k - w);
03432       for (j = i >> w; j < z; j += f)
03433         q[j] = r;
03434 
03435       /* backwards increment the k-bit code i */
03436       for (j = 1 << (k - 1); i & j; j >>= 1)
03437         i ^= j;
03438       i ^= j;
03439 
03440       /* backup over finished tables */
03441       mask = (1 << w) - 1;      /* needed on HP, cc -O bug */
03442       while ((i & mask) != x[h])
03443       {
03444         h--;                    /* don't need to update q */
03445         w -= l;
03446         mask = (1 << w) - 1;
03447       }
03448     }
03449   }
03450 
03451 
03452   /* Return Z_BUF_ERROR if we were given an incomplete table */
03453   return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
03454 }
03455 
03456 
03457 int inflate_trees_bits(uInt *c, uInt *bb, inflate_huft * *tb, inflate_huft *hp, z_streamp z)
03458 //uInt *c;               /* 19 code lengths */
03459 //uInt *bb;              /* bits tree desired/actual depth */
03460 //inflate_huft * *tb; /* bits tree result */
03461 //inflate_huft *hp;       /* space for trees */
03462 //z_streamp z;            /* for messages */
03463 {
03464   int r;
03465   uInt hn = 0;          /* hufts used in space */
03466   uInt *v;             /* work area for huft_build */
03467 
03468   if ((v = (uInt*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)
03469     return Z_MEM_ERROR;
03470   r = huft_build(c, 19, 19, (uInt*)Z_NULL, (uInt*)Z_NULL,
03471                  tb, bb, hp, &hn, v);
03472   if (r == Z_DATA_ERROR)
03473     z->msg = (char*)"oversubscribed dynamic bit lengths tree";
03474   else if (r == Z_BUF_ERROR || *bb == 0)
03475   {
03476     z->msg = (char*)"incomplete dynamic bit lengths tree";
03477     r = Z_DATA_ERROR;
03478   }
03479   ZFREE(z, v);
03480   return r;
03481 }
03482 
03483 
03484 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)
03485 //uInt nl;                /* number of literal/length codes */
03486 //uInt nd;                /* number of distance codes */
03487 //uInt *c;               /* that many (total) code lengths */
03488 //uInt *bl;              /* literal desired/actual bit depth */
03489 //uInt *bd;              /* distance desired/actual bit depth */
03490 //inflate_huft * *tl; /* literal/length tree result */
03491 //inflate_huft * *td; /* distance tree result */
03492 //inflate_huft *hp;       /* space for trees */
03493 //z_streamp z;            /* for messages */
03494 {
03495   int r;
03496   uInt hn = 0;          /* hufts used in space */
03497   uInt *v;             /* work area for huft_build */
03498 
03499   /* allocate work area */
03500   if ((v = (uInt*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
03501     return Z_MEM_ERROR;
03502 
03503   /* build literal/length tree */
03504   r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
03505   if (r != Z_OK || *bl == 0)
03506   {
03507     if (r == Z_DATA_ERROR)
03508       z->msg = (char*)"oversubscribed literal/length tree";
03509     else if (r != Z_MEM_ERROR)
03510     {
03511       z->msg = (char*)"incomplete literal/length tree";
03512       r = Z_DATA_ERROR;
03513     }
03514     ZFREE(z, v);
03515     return r;
03516   }
03517 
03518   /* build distance tree */
03519   r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
03520   if (r != Z_OK || (*bd == 0 && nl > 257))
03521   {
03522     if (r == Z_DATA_ERROR)
03523       z->msg = (char*)"oversubscribed distance tree";
03524     else if (r == Z_BUF_ERROR) {
03525 #ifdef PKZIP_BUG_WORKAROUND
03526       r = Z_OK;
03527     }
03528 #else
03529       z->msg = (char*)"incomplete distance tree";
03530       r = Z_DATA_ERROR;
03531     }
03532     else if (r != Z_MEM_ERROR)
03533     {
03534       z->msg = (char*)"empty distance tree with lengths";
03535       r = Z_DATA_ERROR;
03536     }
03537     ZFREE(z, v);
03538     return r;
03539 #endif
03540   }
03541 
03542   /* done */
03543   ZFREE(z, v);
03544   return Z_OK;
03545 }
03546 
03547 /* inffixed.h -- table for decoding fixed codes
03548  * Generated automatically by the maketree.c program
03549  */
03550 
03551 /* WARNING: this file should *not* be used by applications. It is
03552    part of the implementation of the compression library and is
03553    subject to change. Applications should only use zlib.h.
03554  */
03555 
03556 static uInt fixed_bl = 9;
03557 static uInt fixed_bd = 5;
03558 static inflate_huft fixed_tl[] = {
03559     {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
03560     {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},192},
03561     {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},160},
03562     {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},224},
03563     {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},144},
03564     {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},208},
03565     {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},176},
03566     {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},240},
03567     {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
03568     {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},200},
03569     {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},168},
03570     {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},232},
03571     {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},152},
03572     {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},216},
03573     {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},184},
03574     {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},248},
03575     {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
03576     {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},196},
03577     {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},164},
03578     {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},228},
03579     {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},148},
03580     {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},212},
03581     {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},180},
03582     {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},244},
03583     {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
03584     {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},204},
03585     {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},172},
03586     {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},236},
03587     {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},156},
03588     {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},220},
03589     {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},188},
03590     {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},252},
03591     {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
03592     {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},194},
03593     {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},162},
03594     {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},226},
03595     {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},146},
03596     {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},210},
03597     {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},178},
03598     {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},242},
03599     {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
03600     {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},202},
03601     {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},170},
03602     {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},234},
03603     {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},154},
03604     {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},218},
03605     {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},186},
03606     {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},250},
03607     {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
03608     {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},198},
03609     {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},166},
03610     {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},230},
03611     {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},150},
03612     {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},214},
03613     {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},182},
03614     {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},246},
03615     {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
03616     {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},206},
03617     {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},174},
03618     {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},238},
03619     {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},158},
03620     {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},222},
03621     {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},190},
03622     {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},254},
03623     {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
03624     {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},193},
03625     {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},161},
03626     {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},225},
03627     {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},145},
03628     {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},209},
03629     {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},177},
03630     {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},241},
03631     {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
03632     {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},201},
03633     {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},169},
03634     {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},233},
03635     {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},153},
03636     {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},217},
03637     {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},185},
03638     {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},249},
03639     {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
03640     {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},197},
03641     {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},165},
03642     {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},229},
03643     {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},149},
03644     {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},213},
03645     {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},181},
03646     {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},245},
03647     {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
03648     {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},205},
03649     {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},173},
03650     {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},237},
03651     {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},157},
03652     {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},221},
03653     {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},189},
03654     {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},253},
03655     {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
03656     {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},195},
03657     {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},163},
03658     {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},227},
03659     {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},147},
03660     {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},211},
03661     {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},179},
03662     {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},243},
03663     {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
03664     {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},203},
03665     {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},171},
03666     {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},235},
03667     {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},155},
03668     {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},219},
03669     {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},187},
03670     {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},251},
03671     {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
03672     {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},199},
03673     {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},167},
03674     {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},231},
03675     {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},151},
03676     {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},215},
03677     {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},183},
03678     {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},247},
03679     {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
03680     {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},207},
03681     {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},175},
03682     {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},239},
03683     {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},159},
03684     {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},223},
03685     {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},191},
03686     {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},255}
03687   };
03688 static inflate_huft fixed_td[] = {
03689     {{{80,5}},1}, {{{87,5}},257}, {{{83,5}},17}, {{{91,5}},4097},
03690     {{{81,5}},5}, {{{89,5}},1025}, {{{85,5}},65}, {{{93,5}},16385},
03691     {{{80,5}},3}, {{{88,5}},513}, {{{84,5}},33}, {{{92,5}},8193},
03692     {{{82,5}},9}, {{{90,5}},2049}, {{{86,5}},129}, {{{192,5}},24577},
03693     {{{80,5}},2}, {{{87,5}},385}, {{{83,5}},25}, {{{91,5}},6145},
03694     {{{81,5}},7}, {{{89,5}},1537}, {{{85,5}},97}, {{{93,5}},24577},
03695     {{{80,5}},4}, {{{88,5}},769}, {{{84,5}},49}, {{{92,5}},12289},
03696     {{{82,5}},13}, {{{90,5}},3073}, {{{86,5}},193}, {{{192,5}},24577}
03697   };
03698 
03699 int inflate_trees_fixed(uInt *bl, uInt *bd, inflate_huft * *tl, inflate_huft * *td, z_streamp z)
03700 //uInt *bl;               /* literal desired/actual bit depth */
03701 //uInt *bd;               /* distance desired/actual bit depth */
03702 //inflate_huft * *tl;  /* literal/length tree result */
03703 //inflate_huft * *td;  /* distance tree result */
03704 //z_streamp z;             /* for memory allocation */
03705 {
03706   *bl = fixed_bl;
03707   *bd = fixed_bd;
03708   *tl = fixed_tl;
03709   *td = fixed_td;
03710   return Z_OK;
03711 }
03712 
03713 /* simplify the use of the inflate_huft type with some defines */
03714 #define exop word.what.Exop
03715 #define bits word.what.Bits
03716 
03717 /* macros for bit input with no checking and for returning unused bytes */
03718 #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
03719 #define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;}
03720 
03721 /* Called with number of bytes left to write in window at least 258
03722    (the maximum string length) and number of input bytes available
03723    at least ten.  The ten bytes are six bytes for the longest length/
03724    distance pair plus four bytes for overloading the bit buffer. */
03725 
03726 int inflate_fast(uInt bl, uInt bd, inflate_huft *tl, inflate_huft *td, inflate_blocks_statef *s, z_streamp z)
03727 {
03728   inflate_huft *t;      /* temporary pointer */
03729   uInt e;               /* extra bits or operation */
03730   uLong b;              /* bit buffer */
03731   uInt k;               /* bits in bit buffer */
03732   Byte *p;             /* input data pointer */
03733   uInt n;               /* bytes available there */
03734   Byte *q;             /* output window write pointer */
03735   uInt m;               /* bytes to end of window or read pointer */
03736   uInt ml;              /* mask for literal/length tree */
03737   uInt md;              /* mask for distance tree */
03738   uInt c;               /* bytes to copy */
03739   uInt d;               /* distance back to copy from */
03740   Byte *r;             /* copy source pointer */
03741 
03742   /* load input, output, bit values */
03743   LOAD
03744 
03745   /* initialize masks */
03746   ml = inflate_mask[bl];
03747   md = inflate_mask[bd];
03748 
03749   /* do until not enough input or output space for fast loop */
03750   do {                          /* assume called with m >= 258 && n >= 10 */
03751     /* get literal/length code */
03752     GRABBITS(20)                /* max bits for literal/length code */
03753     if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
03754     {
03755       DUMPBITS(t->bits)
03756       Tracevv((t->base >= 0x20 && t->base < 0x7f ?
03757                 "inflate:         * literal '%c'\n" :
03758                 "inflate:         * literal 0x%02x\n", t->base));
03759       *q++ = (Byte)t->base;
03760       m--;
03761       continue;
03762     }
03763     do {
03764       DUMPBITS(t->bits)
03765       if (e & 16)
03766       {
03767         /* get extra bits for length */
03768         e &= 15;
03769         c = t->base + ((uInt)b & inflate_mask[e]);
03770         DUMPBITS(e)
03771         Tracevv(("inflate:         * length %u\n", c));
03772 
03773         /* decode distance base of block to copy */
03774         GRABBITS(15);           /* max bits for distance code */
03775         e = (t = td + ((uInt)b & md))->exop;
03776         do {
03777           DUMPBITS(t->bits)
03778           if (e & 16)
03779           {
03780             /* get extra bits to add to distance base */
03781             e &= 15;
03782             GRABBITS(e)         /* get extra bits (up to 13) */
03783             d = t->base + ((uInt)b & inflate_mask[e]);
03784             DUMPBITS(e)
03785             Tracevv(("inflate:         * distance %u\n", d));
03786 
03787             /* do the copy */
03788             m -= c;
03789             if ((uInt)(q - s->window) >= d)     /* offset before dest */
03790             {                                   /*  just copy */
03791               r = q - d;
03792               *q++ = *r++;  c--;        /* minimum count is three, */
03793               *q++ = *r++;  c--;        /*  so unroll loop a little */
03794             }
03795             else                        /* else offset after destination */
03796             {
03797               e = d - (uInt)(q - s->window); /* bytes from offset to end */
03798               r = s->end - e;           /* pointer to offset */
03799               if (c > e)                /* if source crosses, */
03800               {
03801                 c -= e;                 /* copy to end of window */
03802                 do {
03803                   *q++ = *r++;
03804                 } while (--e);
03805                 r = s->window;          /* copy rest from start of window */
03806               }
03807             }
03808             do {                        /* copy all or what's left */
03809               *q++ = *r++;
03810             } while (--c);
03811             break;
03812           }
03813           else if ((e & 64) == 0)
03814           {
03815             t += t->base;
03816             e = (t += ((uInt)b & inflate_mask[e]))->exop;
03817           }
03818           else
03819           {
03820             z->msg = (char*)"invalid distance code";
03821             UNGRAB
03822             UPDATE
03823             return Z_DATA_ERROR;
03824           }
03825         } while (1);
03826         break;
03827       }
03828       if ((e & 64) == 0)
03829       {
03830         t += t->base;
03831         if ((e = (t += ((uInt)b & inflate_mask[e]))->exop) == 0)
03832         {
03833           DUMPBITS(t->bits)
03834           Tracevv((t->base >= 0x20 && t->base < 0x7f ?
03835                     "inflate:         * literal '%c'\n" :
03836                     "inflate:         * literal 0x%02x\n", t->base));
03837           *q++ = (Byte)t->base;
03838           m--;
03839           break;
03840         }
03841       }
03842       else if (e & 32)
03843       {
03844         Tracevv(("inflate:         * end of block\n"));
03845         UNGRAB
03846         UPDATE
03847         return Z_STREAM_END;
03848       }
03849       else
03850       {
03851         z->msg = (char*)"invalid literal/length code";
03852         UNGRAB
03853         UPDATE
03854         return Z_DATA_ERROR;
03855       }
03856     } while (1);
03857   } while (m >= 258 && n >= 10);
03858 
03859   /* not enough input or output--restore pointers and return */
03860   UNGRAB
03861   UPDATE
03862   return Z_OK;
03863 }
03864 
03865 /* infcodes.c -- process literals and length/distance pairs
03866  * Copyright (C) 1995-1998 Mark Adler
03867  * For conditions of distribution and use, see copyright notice in zlib.h 
03868  */
03869 
03870 /* simplify the use of the inflate_huft type with some defines */
03871 #define exop word.what.Exop
03872 #define bits word.what.Bits
03873 
03874 typedef enum {        /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
03875       START,    /* x: set up for LEN */
03876       LEN,      /* i: get length/literal/eob next */
03877       LENEXT,   /* i: getting length extra (have base) */
03878       DIST,     /* i: get distance next */
03879       DISTEXT,  /* i: getting distance extra */
03880       COPY,     /* o: copying bytes in window, waiting for space */
03881       LIT,      /* o: got literal, waiting for output space */
03882       WASH,     /* o: got eob, possibly still output waiting */
03883       END,      /* x: got eob and all data flushed */
03884       BADCODE}  /* x: got error */
03885 inflate_codes_mode;
03886 
03887 /* inflate codes private state */
03888 struct inflate_codes_state {
03889 
03890   /* mode */
03891   inflate_codes_mode mode;      /* current inflate_codes mode */
03892 
03893   /* mode dependent information */
03894   uInt len;
03895   union {
03896     struct {
03897       inflate_huft *tree;       /* pointer into tree */
03898       uInt need;                /* bits needed */
03899     } code;             /* if LEN or DIST, where in tree */
03900     uInt lit;           /* if LIT, literal */
03901     struct {
03902       uInt get;                 /* bits to get for extra */
03903       uInt dist;                /* distance back to copy from */
03904     } copy;             /* if EXT or COPY, where and how much */
03905   } sub;                /* submode */
03906 
03907   /* mode independent information */
03908   Byte lbits;           /* ltree bits decoded per branch */
03909   Byte dbits;           /* dtree bits decoder per branch */
03910   inflate_huft *ltree;          /* literal/length/eob tree */
03911   inflate_huft *dtree;          /* distance tree */
03912 
03913 };
03914 
03915 
03916 inflate_codes_statef *inflate_codes_new(uInt bl, uInt bd, inflate_huft *tl, inflate_huft *td, z_streamp z)
03917 {
03918   inflate_codes_statef *c;
03919 
03920   if ((c = (inflate_codes_statef *)
03921        ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
03922   {
03923     c->mode = START;
03924     c->lbits = (Byte)bl;
03925     c->dbits = (Byte)bd;
03926     c->ltree = tl;
03927     c->dtree = td;
03928     Tracev(("inflate:       codes new\n"));
03929   }
03930   return c;
03931 }
03932 
03933 
03934 int inflate_codes(inflate_blocks_statef *s, z_streamp z, int r)
03935 {
03936   uInt j;               /* temporary storage */
03937   inflate_huft *t;      /* temporary pointer */
03938   uInt e;               /* extra bits or operation */
03939   uLong b;              /* bit buffer */
03940   uInt k;               /* bits in bit buffer */
03941   Byte *p;             /* input data pointer */
03942   uInt n;               /* bytes available there */
03943   Byte *q;             /* output window write pointer */
03944   uInt m;               /* bytes to end of window or read pointer */
03945   Byte *f;             /* pointer to copy strings from */
03946   inflate_codes_statef *c = s->sub.decode.codes;  /* codes state */
03947 
03948   /* copy input/output information to locals (UPDATE macro restores) */
03949   LOAD
03950 
03951   /* process input and output based on current state */
03952   while (1) switch (c->mode)
03953   {             /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
03954     case START:         /* x: set up for LEN */
03955 #ifndef SLOW
03956       if (m >= 258 && n >= 10)
03957       {
03958         UPDATE
03959         r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
03960         LOAD
03961         if (r != Z_OK)
03962         {
03963           c->mode = r == Z_STREAM_END ? WASH : BADCODE;
03964           break;
03965         }
03966       }
03967 #endif /* !SLOW */
03968       c->sub.code.need = c->lbits;
03969       c->sub.code.tree = c->ltree;
03970       c->mode = LEN;
03971     case LEN:           /* i: get length/literal/eob next */
03972       j = c->sub.code.need;
03973       NEEDBITS(j)
03974       t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
03975       DUMPBITS(t->bits)
03976       e = (uInt)(t->exop);
03977       if (e == 0)               /* literal */
03978       {
03979         c->sub.lit = t->base;
03980         Tracevv((t->base >= 0x20 && t->base < 0x7f ?
03981                  "inflate:         literal '%c'\n" :
03982                  "inflate:         literal 0x%02x\n", t->base));
03983         c->mode = LIT;
03984         break;
03985       }
03986       if (e & 16)               /* length */
03987       {
03988         c->sub.copy.get = e & 15;
03989         c->len = t->base;
03990         c->mode = LENEXT;
03991         break;
03992       }
03993       if ((e & 64) == 0)        /* next table */
03994       {
03995         c->sub.code.need = e;
03996         c->sub.code.tree = t + t->base;
03997         break;
03998       }
03999       if (e & 32)               /* end of block */
04000       {
04001         Tracevv(("inflate:         end of block\n"));
04002         c->mode = WASH;
04003         break;
04004       }
04005       c->mode = BADCODE;        /* invalid code */
04006       z->msg = (char*)"invalid literal/length code";
04007       r = Z_DATA_ERROR;
04008       LEAVE
04009     case LENEXT:        /* i: getting length extra (have base) */
04010       j = c->sub.copy.get;
04011       NEEDBITS(j)
04012       c->len += (uInt)b & inflate_mask[j];
04013       DUMPBITS(j)
04014       c->sub.code.need = c->dbits;
04015       c->sub.code.tree = c->dtree;
04016       Tracevv(("inflate:         length %u\n", c->len));
04017       c->mode = DIST;
04018     case DIST:          /* i: get distance next */
04019       j = c->sub.code.need;
04020       NEEDBITS(j)
04021       t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
04022       DUMPBITS(t->bits)
04023       e = (uInt)(t->exop);
04024       if (e & 16)               /* distance */
04025       {
04026         c->sub.copy.get = e & 15;
04027         c->sub.copy.dist = t->base;
04028         c->mode = DISTEXT;
04029         break;
04030       }
04031       if ((e & 64) == 0)        /* next table */
04032       {
04033         c->sub.code.need = e;
04034         c->sub.code.tree = t + t->base;
04035         break;
04036       }
04037       c->mode = BADCODE;        /* invalid code */
04038       z->msg = (char*)"invalid distance code";
04039       r = Z_DATA_ERROR;
04040       LEAVE
04041     case DISTEXT:       /* i: getting distance extra */
04042       j = c->sub.copy.get;
04043       NEEDBITS(j)
04044       c->sub.copy.dist += (uInt)b & inflate_mask[j];
04045       DUMPBITS(j)
04046       Tracevv(("inflate:         distance %u\n", c->sub.copy.dist));
04047       c->mode = COPY;
04048     case COPY:          /* o: copying bytes in window, waiting for space */
04049 #ifndef __TURBOC__ /* Turbo C bug for following expression */
04050       f = (uInt)(q - s->window) < c->sub.copy.dist ?
04051           s->end - (c->sub.copy.dist - (q - s->window)) :
04052           q - c->sub.copy.dist;
04053 #else
04054       f = q - c->sub.copy.dist;
04055       if ((uInt)(q - s->window) < c->sub.copy.dist)
04056         f = s->end - (c->sub.copy.dist - (uInt)(q - s->window));
04057 #endif
04058       while (c->len)
04059       {
04060         NEEDOUT
04061         OUTBYTE(*f++)
04062         if (f == s->end)
04063           f = s->window;
04064         c->len--;
04065       }
04066       c->mode = START;
04067       break;
04068     case LIT:           /* o: got literal, waiting for output space */
04069       NEEDOUT
04070       OUTBYTE(c->sub.lit)
04071       c->mode = START;
04072       break;
04073     case WASH:          /* o: got eob, possibly more output */
04074       if (k > 7)        /* return unused byte, if any */
04075       {
04076         Assert(k < 16, "inflate_codes grabbed too many bytes")
04077         k -= 8;
04078         n++;
04079         p--;            /* can always return one */
04080       }
04081       FLUSH
04082       if (s->read != s->write)
04083         LEAVE
04084       c->mode = END;
04085     case END:
04086       r = Z_STREAM_END;
04087       LEAVE
04088     case BADCODE:       /* x: got error */
04089       r = Z_DATA_ERROR;
04090       LEAVE
04091     default:
04092       r = Z_STREAM_ERROR;
04093       LEAVE
04094   }
04095 #ifdef NEED_DUMMY_RETURN
04096   return Z_STREAM_ERROR;  /* Some dumb compilers complain without this */
04097 #endif
04098 }
04099 
04100 
04101 void inflate_codes_free(inflate_codes_statef *c, z_streamp z)
04102 {
04103   ZFREE(z, c);
04104   Tracev(("inflate:       codes free\n"));
04105 }
04106 
04107 /* adler32.c -- compute the Adler-32 checksum of a data stream
04108  * Copyright (C) 1995-1998 Mark Adler
04109  * For conditions of distribution and use, see copyright notice in zlib.h 
04110  */
04111 
04112 #define BASE 65521L /* largest prime smaller than 65536 */
04113 #define NMAX 5552
04114 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
04115 
04116 #undef DO1
04117 #undef DO2
04118 #undef DO4
04119 #undef DO8
04120 
04121 #define DO1(buf,i)  {s1 += buf[i]; s2 += s1;}
04122 #define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
04123 #define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
04124 #define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
04125 #define DO16(buf)   DO8(buf,0); DO8(buf,8);
04126 
04127 /* ========================================================================= */
04128 uLong adler32(uLong adler, const Byte *buf, uInt len)
04129 {
04130     unsigned long s1 = adler & 0xffff;
04131     unsigned long s2 = (adler >> 16) & 0xffff;
04132     int k;
04133 
04134     if (buf == Z_NULL) return 1L;
04135 
04136     while (len > 0) {
04137         k = len < NMAX ? len : NMAX;
04138         len -= k;
04139         while (k >= 16) {
04140             DO16(buf);
04141         buf += 16;
04142             k -= 16;
04143         }
04144         if (k != 0) do {
04145             s1 += *buf++;
04146         s2 += s1;
04147         } while (--k);
04148         s1 %= BASE;
04149         s2 %= BASE;
04150     }
04151     return (s2 << 16) | s1;
04152 }
04153 
04154 /* @(#) $Id: unzip.cpp,v 1.1.1.3 2000/01/11 16:37:27 ttimo Exp $ */
04155 
04156 /* infblock.h -- header to use infblock.c
04157  * Copyright (C) 1995-1998 Mark Adler
04158  * For conditions of distribution and use, see copyright notice in zlib.h 
04159  */
04160 
04161 /* WARNING: this file should *not* be used by applications. It is
04162    part of the implementation of the compression library and is
04163    subject to change. Applications should only use zlib.h.
04164  */
04165 
04166 extern inflate_blocks_statef * inflate_blocks_new OF((
04167     z_streamp z,
04168     check_func c,               /* check function */
04169     uInt w));                   /* window size */
04170 
04171 extern int inflate_blocks OF((
04172     inflate_blocks_statef *,
04173     z_streamp ,
04174     int));                      /* initial return code */
04175 
04176 extern void inflate_blocks_reset OF((
04177     inflate_blocks_statef *,
04178     z_streamp ,
04179     uLong *));                  /* check value on output */
04180 
04181 extern int inflate_blocks_free OF((
04182     inflate_blocks_statef *,
04183     z_streamp));
04184 
04185 extern void inflate_set_dictionary OF((
04186     inflate_blocks_statef *s,
04187     const Byte *d,  /* dictionary */
04188     uInt  n));       /* dictionary length */
04189 
04190 extern int inflate_blocks_sync_point OF((
04191     inflate_blocks_statef *s));
04192 
04193 typedef enum {
04194       imMETHOD,   /* waiting for method byte */
04195       imFLAG,     /* waiting for flag byte */
04196       imDICT4,    /* four dictionary check bytes to go */
04197       imDICT3,    /* three dictionary check bytes to go */
04198       imDICT2,    /* two dictionary check bytes to go */
04199       imDICT1,    /* one dictionary check byte to go */
04200       imDICT0,    /* waiting for inflateSetDictionary */
04201       imBLOCKS,   /* decompressing blocks */
04202       imCHECK4,   /* four check bytes to go */
04203       imCHECK3,   /* three check bytes to go */
04204       imCHECK2,   /* two check bytes to go */
04205       imCHECK1,   /* one check byte to go */
04206       imDONE,     /* finished check, done */
04207       imBAD}      /* got an error--stay here */
04208 inflate_mode;
04209 
04210 /* inflate private state */
04211 struct internal_state {
04212 
04213   /* mode */
04214   inflate_mode  mode;   /* current inflate mode */
04215 
04216   /* mode dependent information */
04217   union {
04218     uInt method;        /* if FLAGS, method byte */
04219     struct {
04220       uLong was;                /* computed check value */
04221       uLong need;               /* stream check value */
04222     } check;            /* if CHECK, check values to compare */
04223     uInt marker;        /* if BAD, inflateSync's marker bytes count */
04224   } sub;        /* submode */
04225 
04226   /* mode independent information */
04227   int  nowrap;          /* flag for no wrapper */
04228   uInt wbits;           /* log2(window size)  (8..15, defaults to 15) */
04229   inflate_blocks_statef 
04230     *blocks;            /* current inflate_blocks state */
04231 
04232 };
04233 
04234 
04235 int inflateReset(z_streamp z)
04236 {
04237   if (z == Z_NULL || z->state == Z_NULL)
04238     return Z_STREAM_ERROR;
04239   z->total_in = z->total_out = 0;
04240   z->msg = Z_NULL;
04241   z->state->mode = z->state->nowrap ? imBLOCKS : imMETHOD;
04242   inflate_blocks_reset(z->state->blocks, z, Z_NULL);
04243   Tracev(("inflate: reset\n"));
04244   return Z_OK;
04245 }
04246 
04247 
04248 int inflateEnd(z_streamp z)
04249 {
04250   if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
04251     return Z_STREAM_ERROR;
04252   if (z->state->blocks != Z_NULL)
04253     inflate_blocks_free(z->state->blocks, z);
04254   ZFREE(z, z->state);
04255   z->state = Z_NULL;
04256   Tracev(("inflate: end\n"));
04257   return Z_OK;
04258 }
04259 
04260 
04261 
04262 int inflateInit2_(z_streamp z, int w, const char *version, int stream_size)
04263 {
04264   if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
04265       stream_size != sizeof(z_stream))
04266       return Z_VERSION_ERROR;
04267 
04268   /* initialize state */
04269   if (z == Z_NULL)
04270     return Z_STREAM_ERROR;
04271   z->msg = Z_NULL;
04272   if (z->zalloc == Z_NULL)
04273   {
04274     z->zalloc = (void *(*)(void *, unsigned, unsigned))zcalloc;
04275     z->opaque = (voidp)0;
04276   }
04277   if (z->zfree == Z_NULL) z->zfree = (void (*)(void *, void *))zcfree;
04278   if ((z->state = (struct internal_state *)
04279        ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
04280     return Z_MEM_ERROR;
04281   z->state->blocks = Z_NULL;
04282 
04283   /* handle undocumented nowrap option (no zlib header or check) */
04284   z->state->nowrap = 0;
04285   if (w < 0)
04286   {
04287     w = - w;
04288     z->state->nowrap = 1;
04289   }
04290 
04291   /* set window size */
04292   if (w < 8 || w > 15)
04293   {
04294     inflateEnd(z);
04295     return Z_STREAM_ERROR;
04296   }
04297   z->state->wbits = (uInt)w;
04298 
04299   /* create inflate_blocks state */
04300   if ((z->state->blocks =
04301       inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
04302       == Z_NULL)
04303   {
04304     inflateEnd(z);
04305     return Z_MEM_ERROR;
04306   }
04307   Tracev(("inflate: allocated\n"));
04308 
04309   /* reset state */
04310   inflateReset(z);
04311   return Z_OK;
04312 }
04313 
04314 
04315 int inflateInit_(z_streamp z, const char *version, int stream_size)
04316 {
04317   return inflateInit2_(z, DEF_WBITS, version, stream_size);
04318 }
04319 
04320 
04321 #define iNEEDBYTE {if(z->avail_in==0)return r;r=f;}
04322 #define iNEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
04323 
04324 int inflate(z_streamp z, int f)
04325 {
04326   int r;
04327   uInt b;
04328 
04329   if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
04330     return Z_STREAM_ERROR;
04331   f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
04332   r = Z_BUF_ERROR;
04333   while (1) switch (z->state->mode)
04334   {
04335     case imMETHOD:
04336       iNEEDBYTE
04337       if (((z->state->sub.method = iNEXTBYTE) & 0xf) != Z_DEFLATED)
04338       {
04339         z->state->mode = imBAD;
04340         z->msg = (char*)"unknown compression method";
04341         z->state->sub.marker = 5;       /* can't try inflateSync */
04342         break;
04343       }
04344       if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
04345       {
04346         z->state->mode = imBAD;
04347         z->msg = (char*)"invalid window size";
04348         z->state->sub.marker = 5;       /* can't try inflateSync */
04349         break;
04350       }
04351       z->state->mode = imFLAG;
04352     case imFLAG:
04353       iNEEDBYTE
04354       b = iNEXTBYTE;
04355       if (((z->state->sub.method << 8) + b) % 31)
04356       {
04357         z->state->mode = imBAD;
04358         z->msg = (char*)"incorrect header check";
04359         z->state->sub.marker = 5;       /* can't try inflateSync */
04360         break;
04361       }
04362       Tracev(("inflate: zlib header ok\n"));
04363       if (!(b & PRESET_DICT))
04364       {
04365         z->state->mode = imBLOCKS;
04366         break;
04367       }
04368       z->state->mode = imDICT4;
04369     case imDICT4:
04370       iNEEDBYTE
04371       z->state->sub.check.need = (uLong)iNEXTBYTE << 24;
04372       z->state->mode = imDICT3;
04373     case imDICT3:
04374       iNEEDBYTE
04375       z->state->sub.check.need += (uLong)iNEXTBYTE << 16;
04376       z->state->mode = imDICT2;
04377     case imDICT2:
04378       iNEEDBYTE
04379       z->state->sub.check.need += (uLong)iNEXTBYTE << 8;
04380       z->state->mode = imDICT1;
04381     case imDICT1:
04382       iNEEDBYTE
04383       z->state->sub.check.need += (uLong)iNEXTBYTE;
04384       z->adler = z->state->sub.check.need;
04385       z->state->mode = imDICT0;
04386       return Z_NEED_DICT;
04387     case imDICT0:
04388       z->state->mode = imBAD;
04389       z->msg = (char*)"need dictionary";
04390       z->state->sub.marker = 0;       /* can try inflateSync */
04391       return Z_STREAM_ERROR;
04392     case imBLOCKS:
04393       r = inflate_blocks(z->state->blocks, z, r);
04394       if (r == Z_DATA_ERROR)
04395       {
04396         z->state->mode = imBAD;
04397         z->state->sub.marker = 0;       /* can try inflateSync */
04398         break;
04399       }
04400       if (r == Z_OK)
04401         r = f;
04402       if (r != Z_STREAM_END)
04403         return r;
04404       r = f;
04405       inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
04406       if (z->state->nowrap)
04407       {
04408         z->state->mode = imDONE;
04409         break;
04410       }
04411       z->state->mode = imCHECK4;
04412     case imCHECK4:
04413       iNEEDBYTE
04414       z->state->sub.check.need = (uLong)iNEXTBYTE << 24;
04415       z->state->mode = imCHECK3;
04416     case imCHECK3:
04417       iNEEDBYTE
04418       z->state->sub.check.need += (uLong)iNEXTBYTE << 16;
04419       z->state->mode = imCHECK2;
04420     case imCHECK2:
04421       iNEEDBYTE
04422       z->state->sub.check.need += (uLong)iNEXTBYTE << 8;
04423       z->state->mode = imCHECK1;
04424     case imCHECK1:
04425       iNEEDBYTE
04426       z->state->sub.check.need += (uLong)iNEXTBYTE;
04427 
04428       if (z->state->sub.check.was != z->state->sub.check.need)
04429       {
04430         z->state->mode = imBAD;
04431         z->msg = (char*)"incorrect data check";
04432         z->state->sub.marker = 5;       /* can't try inflateSync */
04433         break;
04434       }
04435       Tracev(("inflate: zlib check ok\n"));
04436       z->state->mode = imDONE;
04437     case imDONE:
04438       return Z_STREAM_END;
04439     case imBAD:
04440       return Z_DATA_ERROR;
04441     default:
04442       return Z_STREAM_ERROR;
04443   }
04444 #ifdef NEED_DUMMY_RETURN
04445   return Z_STREAM_ERROR;  /* Some dumb compilers complain without this */
04446 #endif
04447 }
04448 
04449 
04450 int inflateSetDictionary(z_streamp z, const Byte *dictionary, uInt dictLength)
04451 {
04452   uInt length = dictLength;
04453 
04454   if (z == Z_NULL || z->state == Z_NULL || z->state->mode != imDICT0)
04455     return Z_STREAM_ERROR;
04456 
04457   if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
04458   z->adler = 1L;
04459 
04460   if (length >= ((uInt)1<<z->state->wbits))
04461   {
04462     length = (1<<z->state->wbits)-1;
04463     dictionary += dictLength - length;
04464   }
04465   inflate_set_dictionary(z->state->blocks, dictionary, length);
04466   z->state->mode = imBLOCKS;
04467   return Z_OK;
04468 }
04469 
04470 
04471 int inflateSync(z_streamp z)
04472 {
04473   uInt n;       /* number of bytes to look at */
04474   Byte *p;     /* pointer to bytes */
04475   uInt m;       /* number of marker bytes found in a row */
04476   uLong r, w;   /* temporaries to save total_in and total_out */
04477 
04478   /* set up */
04479   if (z == Z_NULL || z->state == Z_NULL)
04480     return Z_STREAM_ERROR;
04481   if (z->state->mode != imBAD)
04482   {
04483     z->state->mode = imBAD;
04484     z->state->sub.marker = 0;
04485   }
04486   if ((n = z->avail_in) == 0)
04487     return Z_BUF_ERROR;
04488   p = z->next_in;
04489   m = z->state->sub.marker;
04490 
04491   /* search */
04492   while (n && m < 4)
04493   {
04494     static const Byte mark[4] = {0, 0, 0xff, 0xff};
04495     if (*p == mark[m])
04496       m++;
04497     else if (*p)
04498       m = 0;
04499     else
04500       m = 4 - m;
04501     p++, n--;
04502   }
04503 
04504   /* restore */
04505   z->total_in += p - z->next_in;
04506   z->next_in = p;
04507   z->avail_in = n;
04508   z->state->sub.marker = m;
04509 
04510   /* return no joy or set up to restart on a new block */
04511   if (m != 4)
04512     return Z_DATA_ERROR;
04513   r = z->total_in;  w = z->total_out;
04514   inflateReset(z);
04515   z->total_in = r;  z->total_out = w;
04516   z->state->mode = imBLOCKS;
04517   return Z_OK;
04518 }
04519 
04520 
04521 /* Returns true if inflate is currently at the end of a block generated
04522  * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
04523  * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
04524  * but removes the length bytes of the resulting empty stored block. When
04525  * decompressing, PPP checks that at the end of input packet, inflate is
04526  * waiting for these length bytes.
04527  */
04528 int inflateSyncPoint(z_streamp z)
04529 {
04530   if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
04531     return Z_STREAM_ERROR;
04532   return inflate_blocks_sync_point(z->state->blocks);
04533 }
04534 
04535 voidp zcalloc (voidp opaque, unsigned items, unsigned size)
04536 {
04537     if (opaque) items += size - size; /* make compiler happy */
04538     return (voidp)malloc(items*size);
04539 }
04540 
04541 void  zcfree (voidp opaque, voidp ptr)
04542 {
04543     free(ptr);
04544     if (opaque) return; /* make compiler happy */
04545 }
04546 

Generated on Thu Aug 25 12:38:19 2005 for Quake III Arena by  doxygen 1.3.9.1