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

unzip.c

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: /MissionPack/code/qcommon/unzip.c $
00007  *
00008  *****************************************************************************/
00009 
00010 #include "../client/client.h"
00011 #include "unzip.h"
00012 
00013 /* unzip.h -- IO for uncompress .zip files using zlib 
00014    Version 0.15 beta, Mar 19th, 1998,
00015 
00016    Copyright (C) 1998 Gilles Vollant
00017 
00018    This unzip package allow extract file from .ZIP file, compatible with PKZip 2.04g
00019      WinZip, InfoZip tools and compatible.
00020    Encryption and multi volume ZipFile (span) are not supported.
00021    Old compressions used by old PKZip 1.x are not supported
00022 
00023    THIS IS AN ALPHA VERSION. AT THIS STAGE OF DEVELOPPEMENT, SOMES API OR STRUCTURE
00024    CAN CHANGE IN FUTURE VERSION !!
00025    I WAIT FEEDBACK at mail info@winimage.com
00026    Visit also http://www.winimage.com/zLibDll/unzip.htm for evolution
00027 
00028    Condition of use and distribution are the same than zlib :
00029 
00030   This software is provided 'as-is', without any express or implied
00031   warranty.  In no event will the authors be held liable for any damages
00032   arising from the use of this software.
00033 
00034   Permission is granted to anyone to use this software for any purpose,
00035   including commercial applications, and to alter it and redistribute it
00036   freely, subject to the following restrictions:
00037 
00038   1. The origin of this software must not be misrepresented; you must not
00039      claim that you wrote the original software. If you use this software
00040      in a product, an acknowledgment in the product documentation would be
00041      appreciated but is not required.
00042   2. Altered source versions must be plainly marked as such, and must not be
00043      misrepresented as being the original software.
00044   3. This notice may not be removed or altered from any source distribution.
00045 
00046 
00047 */
00048 /* for more info about .ZIP format, see 
00049       ftp://ftp.cdrom.com/pub/infozip/doc/appnote-970311-iz.zip
00050    PkWare has also a specification at :
00051       ftp://ftp.pkware.com/probdesc.zip */
00052 
00053 /* zlib.h -- interface of the 'zlib' general purpose compression library
00054   version 1.1.3, July 9th, 1998
00055 
00056   Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
00057 
00058   This software is provided 'as-is', without any express or implied
00059   warranty.  In no event will the authors be held liable for any damages
00060   arising from the use of this software.
00061 
00062   Permission is granted to anyone to use this software for any purpose,
00063   including commercial applications, and to alter it and redistribute it
00064   freely, subject to the following restrictions:
00065 
00066   1. The origin of this software must not be misrepresented; you must not
00067      claim that you wrote the original software. If you use this software
00068      in a product, an acknowledgment in the product documentation would be
00069      appreciated but is not required.
00070   2. Altered source versions must be plainly marked as such, and must not be
00071      misrepresented as being the original software.
00072   3. This notice may not be removed or altered from any source distribution.
00073 
00074   Jean-loup Gailly        Mark Adler
00075   jloup@gzip.org          madler@alumni.caltech.edu
00076 
00077 
00078   The data format used by the zlib library is described by RFCs (Request for
00079   Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
00080   (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
00081 */
00082 
00083 /* zconf.h -- configuration of the zlib compression library
00084  * Copyright (C) 1995-1998 Jean-loup Gailly.
00085  * For conditions of distribution and use, see copyright notice in zlib.h 
00086  */
00087 
00088 
00089 #ifndef _ZCONF_H
00090 #define _ZCONF_H
00091 
00092 /* Maximum value for memLevel in deflateInit2 */
00093 #ifndef MAX_MEM_LEVEL
00094 #  ifdef MAXSEG_64K
00095 #    define MAX_MEM_LEVEL 8
00096 #  else
00097 #    define MAX_MEM_LEVEL 9
00098 #  endif
00099 #endif
00100 
00101 /* Maximum value for windowBits in deflateInit2 and inflateInit2.
00102  * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
00103  * created by gzip. (Files created by minigzip can still be extracted by
00104  * gzip.)
00105  */
00106 #ifndef MAX_WBITS
00107 #  define MAX_WBITS   15 /* 32K LZ77 window */
00108 #endif
00109 
00110 /* The memory requirements for deflate are (in bytes):
00111             (1 << (windowBits+2)) +  (1 << (memLevel+9))
00112  that is: 128K for windowBits=15  +  128K for memLevel = 8  (default values)
00113  plus a few kilobytes for small objects. For example, if you want to reduce
00114  the default memory requirements from 256K to 128K, compile with
00115      make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
00116  Of course this will generally degrade compression (there's no free lunch).
00117 
00118    The memory requirements for inflate are (in bytes) 1 << windowBits
00119  that is, 32K for windowBits=15 (default value) plus a few kilobytes
00120  for small objects.
00121 */
00122 
00123                         /* Type declarations */
00124 
00125 #ifndef OF /* function prototypes */
00126 #define OF(args)  args
00127 #endif
00128 
00129 typedef unsigned char  Byte;  /* 8 bits */
00130 typedef unsigned int   uInt;  /* 16 bits or more */
00131 typedef unsigned long  uLong; /* 32 bits or more */
00132 typedef Byte    *voidp;
00133 
00134 #ifndef SEEK_SET
00135 #  define SEEK_SET        0       /* Seek from beginning of file.  */
00136 #  define SEEK_CUR        1       /* Seek from current position.  */
00137 #  define SEEK_END        2       /* Set file pointer to EOF plus "offset" */
00138 #endif
00139 
00140 #endif /* _ZCONF_H */
00141 
00142 #define ZLIB_VERSION "1.1.3"
00143 
00144 /* 
00145      The 'zlib' compression library provides in-memory compression and
00146   decompression functions, including integrity checks of the uncompressed
00147   data.  This version of the library supports only one compression method
00148   (deflation) but other algorithms will be added later and will have the same
00149   stream interface.
00150 
00151      Compression can be done in a single step if the buffers are large
00152   enough (for example if an input file is mmap'ed), or can be done by
00153   repeated calls of the compression function.  In the latter case, the
00154   application must provide more input and/or consume the output
00155   (providing more output space) before each call.
00156 
00157      The library also supports reading and writing files in gzip (.gz) format
00158   with an interface similar to that of stdio.
00159 
00160      The library does not install any signal handler. The decoder checks
00161   the consistency of the compressed data, so the library should never
00162   crash even in case of corrupted input.
00163 */
00164 
00165 /*
00166    The application must update next_in and avail_in when avail_in has
00167    dropped to zero. It must update next_out and avail_out when avail_out
00168    has dropped to zero. The application must initialize zalloc, zfree and
00169    opaque before calling the init function. All other fields are set by the
00170    compression library and must not be updated by the application.
00171 
00172    The opaque value provided by the application will be passed as the first
00173    parameter for calls of zalloc and zfree. This can be useful for custom
00174    memory management. The compression library attaches no meaning to the
00175    opaque value.
00176 
00177    zalloc must return Z_NULL if there is not enough memory for the object.
00178    If zlib is used in a multi-threaded application, zalloc and zfree must be
00179    thread safe.
00180 
00181    On 16-bit systems, the functions zalloc and zfree must be able to allocate
00182    exactly 65536 bytes, but will not be required to allocate more than this
00183    if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
00184    pointers returned by zalloc for objects of exactly 65536 bytes *must*
00185    have their offset normalized to zero. The default allocation function
00186    provided by this library ensures this (see zutil.c). To reduce memory
00187    requirements and avoid any allocation of 64K objects, at the expense of
00188    compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
00189 
00190    The fields total_in and total_out can be used for statistics or
00191    progress reports. After compression, total_in holds the total size of
00192    the uncompressed data and may be saved for use in the decompressor
00193    (particularly if the decompressor wants to decompress everything in
00194    a single step).
00195 */
00196 
00197                         /* constants */
00198 
00199 #define Z_NO_FLUSH      0
00200 #define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
00201 #define Z_SYNC_FLUSH    2
00202 #define Z_FULL_FLUSH    3
00203 #define Z_FINISH        4
00204 /* Allowed flush values; see deflate() below for details */
00205 
00206 #define Z_OK            0
00207 #define Z_STREAM_END    1
00208 #define Z_NEED_DICT     2
00209 #define Z_ERRNO        (-1)
00210 #define Z_STREAM_ERROR (-2)
00211 #define Z_DATA_ERROR   (-3)
00212 #define Z_MEM_ERROR    (-4)
00213 #define Z_BUF_ERROR    (-5)
00214 #define Z_VERSION_ERROR (-6)
00215 /* Return codes for the compression/decompression functions. Negative
00216  * values are errors, positive values are used for special but normal events.
00217  */
00218 
00219 #define Z_NO_COMPRESSION         0
00220 #define Z_BEST_SPEED             1
00221 #define Z_BEST_COMPRESSION       9
00222 #define Z_DEFAULT_COMPRESSION  (-1)
00223 /* compression levels */
00224 
00225 #define Z_FILTERED            1
00226 #define Z_HUFFMAN_ONLY        2
00227 #define Z_DEFAULT_STRATEGY    0
00228 /* compression strategy; see deflateInit2() below for details */
00229 
00230 #define Z_BINARY   0
00231 #define Z_ASCII    1
00232 #define Z_UNKNOWN  2
00233 /* Possible values of the data_type field */
00234 
00235 #define Z_DEFLATED   8
00236 /* The deflate compression method (the only one supported in this version) */
00237 
00238 #define Z_NULL  0  /* for initializing zalloc, zfree, opaque */
00239 
00240 #define zlib_version zlibVersion()
00241 /* for compatibility with versions < 1.0.2 */
00242 
00243                         /* basic functions */
00244 
00245 // static const char * zlibVersion OF((void));
00246 /* The application can compare zlibVersion and ZLIB_VERSION for consistency.
00247    If the first character differs, the library code actually used is
00248    not compatible with the zlib.h header file used by the application.
00249    This check is automatically made by deflateInit and inflateInit.
00250  */
00251 
00252 /* 
00253 int deflateInit OF((z_streamp strm, int level));
00254 
00255      Initializes the internal stream state for compression. The fields
00256    zalloc, zfree and opaque must be initialized before by the caller.
00257    If zalloc and zfree are set to Z_NULL, deflateInit updates them to
00258    use default allocation functions.
00259 
00260      The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
00261    1 gives best speed, 9 gives best compression, 0 gives no compression at
00262    all (the input data is simply copied a block at a time).
00263    Z_DEFAULT_COMPRESSION requests a default compromise between speed and
00264    compression (currently equivalent to level 6).
00265 
00266      deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
00267    enough memory, Z_STREAM_ERROR if level is not a valid compression level,
00268    Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
00269    with the version assumed by the caller (ZLIB_VERSION).
00270    msg is set to null if there is no error message.  deflateInit does not
00271    perform any compression: this will be done by deflate().
00272 */
00273 
00274 
00275 // static int deflate OF((z_streamp strm, int flush));
00276 /*
00277     deflate compresses as much data as possible, and stops when the input
00278   buffer becomes empty or the output buffer becomes full. It may introduce some
00279   output latency (reading input without producing any output) except when
00280   forced to flush.
00281 
00282     The detailed semantics are as follows. deflate performs one or both of the
00283   following actions:
00284 
00285   - Compress more input starting at next_in and update next_in and avail_in
00286     accordingly. If not all input can be processed (because there is not
00287     enough room in the output buffer), next_in and avail_in are updated and
00288     processing will resume at this point for the next call of deflate().
00289 
00290   - Provide more output starting at next_out and update next_out and avail_out
00291     accordingly. This action is forced if the parameter flush is non zero.
00292     Forcing flush frequently degrades the compression ratio, so this parameter
00293     should be set only when necessary (in interactive applications).
00294     Some output may be provided even if flush is not set.
00295 
00296   Before the call of deflate(), the application should ensure that at least
00297   one of the actions is possible, by providing more input and/or consuming
00298   more output, and updating avail_in or avail_out accordingly; avail_out
00299   should never be zero before the call. The application can consume the
00300   compressed output when it wants, for example when the output buffer is full
00301   (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
00302   and with zero avail_out, it must be called again after making room in the
00303   output buffer because there might be more output pending.
00304 
00305     If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
00306   flushed to the output buffer and the output is aligned on a byte boundary, so
00307   that the decompressor can get all input data available so far. (In particular
00308   avail_in is zero after the call if enough output space has been provided
00309   before the call.)  Flushing may degrade compression for some compression
00310   algorithms and so it should be used only when necessary.
00311 
00312     If flush is set to Z_FULL_FLUSH, all output is flushed as with
00313   Z_SYNC_FLUSH, and the compression state is reset so that decompression can
00314   restart from this point if previous compressed data has been damaged or if
00315   random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
00316   the compression.
00317 
00318     If deflate returns with avail_out == 0, this function must be called again
00319   with the same value of the flush parameter and more output space (updated
00320   avail_out), until the flush is complete (deflate returns with non-zero
00321   avail_out).
00322 
00323     If the parameter flush is set to Z_FINISH, pending input is processed,
00324   pending output is flushed and deflate returns with Z_STREAM_END if there
00325   was enough output space; if deflate returns with Z_OK, this function must be
00326   called again with Z_FINISH and more output space (updated avail_out) but no
00327   more input data, until it returns with Z_STREAM_END or an error. After
00328   deflate has returned Z_STREAM_END, the only possible operations on the
00329   stream are deflateReset or deflateEnd.
00330   
00331     Z_FINISH can be used immediately after deflateInit if all the compression
00332   is to be done in a single step. In this case, avail_out must be at least
00333   0.1% larger than avail_in plus 12 bytes.  If deflate does not return
00334   Z_STREAM_END, then it must be called again as described above.
00335 
00336     deflate() sets strm->adler to the adler32 checksum of all input read
00337   so (that is, total_in bytes).
00338 
00339     deflate() may update data_type if it can make a good guess about
00340   the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered
00341   binary. This field is only for information purposes and does not affect
00342   the compression algorithm in any manner.
00343 
00344     deflate() returns Z_OK if some progress has been made (more input
00345   processed or more output produced), Z_STREAM_END if all input has been
00346   consumed and all output has been produced (only when flush is set to
00347   Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
00348   if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
00349   (for example avail_in or avail_out was zero).
00350 */
00351 
00352 
00353 // static int deflateEnd OF((z_streamp strm));
00354 /*
00355      All dynamically allocated data structures for this stream are freed.
00356    This function discards any unprocessed input and does not flush any
00357    pending output.
00358 
00359      deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
00360    stream state was inconsistent, Z_DATA_ERROR if the stream was freed
00361    prematurely (some input or output was discarded). In the error case,
00362    msg may be set but then points to a static string (which must not be
00363    deallocated).
00364 */
00365 
00366 
00367 /* 
00368 int inflateInit OF((z_streamp strm));
00369 
00370      Initializes the internal stream state for decompression. The fields
00371    next_in, avail_in, zalloc, zfree and opaque must be initialized before by
00372    the caller. If next_in is not Z_NULL and avail_in is large enough (the exact
00373    value depends on the compression method), inflateInit determines the
00374    compression method from the zlib header and allocates all data structures
00375    accordingly; otherwise the allocation will be deferred to the first call of
00376    inflate.  If zalloc and zfree are set to Z_NULL, inflateInit updates them to
00377    use default allocation functions.
00378 
00379      inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
00380    memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
00381    version assumed by the caller.  msg is set to null if there is no error
00382    message. inflateInit does not perform any decompression apart from reading
00383    the zlib header if present: this will be done by inflate().  (So next_in and
00384    avail_in may be modified, but next_out and avail_out are unchanged.)
00385 */
00386 
00387 
00388 static int inflate OF((z_streamp strm, int flush));
00389 /*
00390     inflate decompresses as much data as possible, and stops when the input
00391   buffer becomes empty or the output buffer becomes full. It may some
00392   introduce some output latency (reading input without producing any output)
00393   except when forced to flush.
00394 
00395   The detailed semantics are as follows. inflate performs one or both of the
00396   following actions:
00397 
00398   - Decompress more input starting at next_in and update next_in and avail_in
00399     accordingly. If not all input can be processed (because there is not
00400     enough room in the output buffer), next_in is updated and processing
00401     will resume at this point for the next call of inflate().
00402 
00403   - Provide more output starting at next_out and update next_out and avail_out
00404     accordingly.  inflate() provides as much output as possible, until there
00405     is no more input data or no more space in the output buffer (see below
00406     about the flush parameter).
00407 
00408   Before the call of inflate(), the application should ensure that at least
00409   one of the actions is possible, by providing more input and/or consuming
00410   more output, and updating the next_* and avail_* values accordingly.
00411   The application can consume the uncompressed output when it wants, for
00412   example when the output buffer is full (avail_out == 0), or after each
00413   call of inflate(). If inflate returns Z_OK and with zero avail_out, it
00414   must be called again after making room in the output buffer because there
00415   might be more output pending.
00416 
00417     If the parameter flush is set to Z_SYNC_FLUSH, inflate flushes as much
00418   output as possible to the output buffer. The flushing behavior of inflate is
00419   not specified for values of the flush parameter other than Z_SYNC_FLUSH
00420   and Z_FINISH, but the current implementation actually flushes as much output
00421   as possible anyway.
00422 
00423     inflate() should normally be called until it returns Z_STREAM_END or an
00424   error. However if all decompression is to be performed in a single step
00425   (a single call of inflate), the parameter flush should be set to
00426   Z_FINISH. In this case all pending input is processed and all pending
00427   output is flushed; avail_out must be large enough to hold all the
00428   uncompressed data. (The size of the uncompressed data may have been saved
00429   by the compressor for this purpose.) The next operation on this stream must
00430   be inflateEnd to deallocate the decompression state. The use of Z_FINISH
00431   is never required, but can be used to inform inflate that a faster routine
00432   may be used for the single inflate() call.
00433 
00434      If a preset dictionary is needed at this point (see inflateSetDictionary
00435   below), inflate sets strm-adler to the adler32 checksum of the
00436   dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise 
00437   it sets strm->adler to the adler32 checksum of all output produced
00438   so (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or
00439   an error code as described below. At the end of the stream, inflate()
00440   checks that its computed adler32 checksum is equal to that saved by the
00441   compressor and returns Z_STREAM_END only if the checksum is correct.
00442 
00443     inflate() returns Z_OK if some progress has been made (more input processed
00444   or more output produced), Z_STREAM_END if the end of the compressed data has
00445   been reached and all uncompressed output has been produced, Z_NEED_DICT if a
00446   preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
00447   corrupted (input stream not conforming to the zlib format or incorrect
00448   adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent
00449   (for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not
00450   enough memory, Z_BUF_ERROR if no progress is possible or if there was not
00451   enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR
00452   case, the application may then call inflateSync to look for a good
00453   compression block.
00454 */
00455 
00456 
00457 static int inflateEnd OF((z_streamp strm));
00458 /*
00459      All dynamically allocated data structures for this stream are freed.
00460    This function discards any unprocessed input and does not flush any
00461    pending output.
00462 
00463      inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
00464    was inconsistent. In the error case, msg may be set but then points to a
00465    static string (which must not be deallocated).
00466 */
00467 
00468                         /* Advanced functions */
00469 
00470 /*
00471     The following functions are needed only in some special applications.
00472 */
00473 
00474 /*   
00475 int deflateInit2 OF((z_streamp strm,
00476                                      int  level,
00477                                      int  method,
00478                                      int  windowBits,
00479                                      int  memLevel,
00480                                      int  strategy));
00481 
00482      This is another version of deflateInit with more compression options. The
00483    fields next_in, zalloc, zfree and opaque must be initialized before by
00484    the caller.
00485 
00486      The method parameter is the compression method. It must be Z_DEFLATED in
00487    this version of the library.
00488 
00489      The windowBits parameter is the base two logarithm of the window size
00490    (the size of the history buffer).  It should be in the range 8..15 for this
00491    version of the library. Larger values of this parameter result in better
00492    compression at the expense of memory usage. The default value is 15 if
00493    deflateInit is used instead.
00494 
00495      The memLevel parameter specifies how much memory should be allocated
00496    for the internal compression state. memLevel=1 uses minimum memory but
00497    is slow and reduces compression ratio; memLevel=9 uses maximum memory
00498    for optimal speed. The default value is 8. See zconf.h for total memory
00499    usage as a function of windowBits and memLevel.
00500 
00501      The strategy parameter is used to tune the compression algorithm. Use the
00502    value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
00503    filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no
00504    string match).  Filtered data consists mostly of small values with a
00505    somewhat random distribution. In this case, the compression algorithm is
00506    tuned to compress them better. The effect of Z_FILTERED is to force more
00507    Huffman coding and less string matching; it is somewhat intermediate
00508    between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects
00509    the compression ratio but not the correctness of the compressed output even
00510    if it is not set appropriately.
00511 
00512       deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
00513    memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
00514    method). msg is set to null if there is no error message.  deflateInit2 does
00515    not perform any compression: this will be done by deflate().
00516 */
00517                             
00518 /*
00519 static int deflateSetDictionary OF((z_streamp strm,
00520                                              const Byte *dictionary,
00521                                              uInt  dictLength));
00522 */
00523 /*
00524      Initializes the compression dictionary from the given byte sequence
00525    without producing any compressed output. This function must be called
00526    immediately after deflateInit, deflateInit2 or deflateReset, before any
00527    call of deflate. The compressor and decompressor must use exactly the same
00528    dictionary (see inflateSetDictionary).
00529 
00530      The dictionary should consist of strings (byte sequences) that are likely
00531    to be encountered later in the data to be compressed, with the most commonly
00532    used strings preferably put towards the end of the dictionary. Using a
00533    dictionary is most useful when the data to be compressed is short and can be
00534    predicted with good accuracy; the data can then be compressed better than
00535    with the default empty dictionary.
00536 
00537      Depending on the size of the compression data structures selected by
00538    deflateInit or deflateInit2, a part of the dictionary may in effect be
00539    discarded, for example if the dictionary is larger than the window size in
00540    deflate or deflate2. Thus the strings most likely to be useful should be
00541    put at the end of the dictionary, not at the front.
00542 
00543      Upon return of this function, strm->adler is set to the Adler32 value
00544    of the dictionary; the decompressor may later use this value to determine
00545    which dictionary has been used by the compressor. (The Adler32 value
00546    applies to the whole dictionary even if only a subset of the dictionary is
00547    actually used by the compressor.)
00548 
00549      deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
00550    parameter is invalid (such as NULL dictionary) or the stream state is
00551    inconsistent (for example if deflate has already been called for this stream
00552    or if the compression method is bsort). deflateSetDictionary does not
00553    perform any compression: this will be done by deflate().
00554 */
00555 
00556 /*
00557 static int deflateCopy OF((z_streamp dest,
00558                                     z_streamp source));
00559 */
00560 /*
00561      Sets the destination stream as a complete copy of the source stream.
00562 
00563      This function can be useful when several compression strategies will be
00564    tried, for example when there are several ways of pre-processing the input
00565    data with a filter. The streams that will be discarded should then be freed
00566    by calling deflateEnd.  Note that deflateCopy duplicates the internal
00567    compression state which can be quite large, so this strategy is slow and
00568    can consume lots of memory.
00569 
00570      deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
00571    enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
00572    (such as zalloc being NULL). msg is left unchanged in both source and
00573    destination.
00574 */
00575 
00576 // static int deflateReset OF((z_streamp strm));
00577 /*
00578      This function is equivalent to deflateEnd followed by deflateInit,
00579    but does not free and reallocate all the internal compression state.
00580    The stream will keep the same compression level and any other attributes
00581    that may have been set by deflateInit2.
00582 
00583       deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
00584    stream state was inconsistent (such as zalloc or state being NULL).
00585 */
00586 
00587 /*
00588 static int deflateParams OF((z_streamp strm,
00589                       int level,
00590                       int strategy));
00591 */
00592 /*
00593      Dynamically update the compression level and compression strategy.  The
00594    interpretation of level and strategy is as in deflateInit2.  This can be
00595    used to switch between compression and straight copy of the input data, or
00596    to switch to a different kind of input data requiring a different
00597    strategy. If the compression level is changed, the input available so far
00598    is compressed with the old level (and may be flushed); the new level will
00599    take effect only at the next call of deflate().
00600 
00601      Before the call of deflateParams, the stream state must be set as for
00602    a call of deflate(), since the currently available input may have to
00603    be compressed and flushed. In particular, strm->avail_out must be non-zero.
00604 
00605      deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
00606    stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
00607    if strm->avail_out was zero.
00608 */
00609 
00610 /*   
00611 int inflateInit2 OF((z_streamp strm,
00612                                      int  windowBits));
00613 
00614      This is another version of inflateInit with an extra parameter. The
00615    fields next_in, avail_in, zalloc, zfree and opaque must be initialized
00616    before by the caller.
00617 
00618      The windowBits parameter is the base two logarithm of the maximum window
00619    size (the size of the history buffer).  It should be in the range 8..15 for
00620    this version of the library. The default value is 15 if inflateInit is used
00621    instead. If a compressed stream with a larger window size is given as
00622    input, inflate() will return with the error code Z_DATA_ERROR instead of
00623    trying to allocate a larger window.
00624 
00625       inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
00626    memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative
00627    memLevel). msg is set to null if there is no error message.  inflateInit2
00628    does not perform any decompression apart from reading the zlib header if
00629    present: this will be done by inflate(). (So next_in and avail_in may be
00630    modified, but next_out and avail_out are unchanged.)
00631 */
00632 
00633 /*
00634 static int inflateSetDictionary OF((z_streamp strm,
00635                                              const Byte *dictionary,
00636                                              uInt  dictLength));
00637 */
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 // static 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 static 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 /*
00691 static int compress OF((Byte *dest,   uLong *destLen,
00692                                  const Byte *source, uLong sourceLen));
00693 */
00694 /*
00695      Compresses the source buffer into the destination buffer.  sourceLen is
00696    the byte length of the source buffer. Upon entry, destLen is the total
00697    size of the destination buffer, which must be at least 0.1% larger than
00698    sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the
00699    compressed buffer.
00700      This function can be used to compress a whole file at once if the
00701    input file is mmap'ed.
00702      compress returns Z_OK if success, Z_MEM_ERROR if there was not
00703    enough memory, Z_BUF_ERROR if there was not enough room in the output
00704    buffer.
00705 */
00706 
00707 /*
00708 static int compress2 OF((Byte *dest,   uLong *destLen,
00709                                   const Byte *source, uLong sourceLen,
00710                                   int level));
00711 */
00712 /*
00713      Compresses the source buffer into the destination buffer. The level
00714    parameter has the same meaning as in deflateInit.  sourceLen is the byte
00715    length of the source buffer. Upon entry, destLen is the total size of the
00716    destination buffer, which must be at least 0.1% larger than sourceLen plus
00717    12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
00718 
00719      compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
00720    memory, Z_BUF_ERROR if there was not enough room in the output buffer,
00721    Z_STREAM_ERROR if the level parameter is invalid.
00722 */
00723 
00724 /*
00725 static int uncompress OF((Byte *dest,   uLong *destLen,
00726                                    const Byte *source, uLong sourceLen));
00727 */                                   
00728 /*
00729      Decompresses the source buffer into the destination buffer.  sourceLen is
00730    the byte length of the source buffer. Upon entry, destLen is the total
00731    size of the destination buffer, which must be large enough to hold the
00732    entire uncompressed data. (The size of the uncompressed data must have
00733    been saved previously by the compressor and transmitted to the decompressor
00734    by some mechanism outside the scope of this compression library.)
00735    Upon exit, destLen is the actual size of the compressed buffer.
00736      This function can be used to decompress a whole file at once if the
00737    input file is mmap'ed.
00738 
00739      uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
00740    enough memory, Z_BUF_ERROR if there was not enough room in the output
00741    buffer, or Z_DATA_ERROR if the input data was corrupted.
00742 */
00743 
00744 
00745 typedef voidp gzFile;
00746 
00747 gzFile gzopen  OF((const char *path, const char *mode));
00748 /*
00749      Opens a gzip (.gz) file for reading or writing. The mode parameter
00750    is as in fopen ("rb" or "wb") but can also include a compression level
00751    ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
00752    Huffman only compression as in "wb1h". (See the description
00753    of deflateInit2 for more information about the strategy parameter.)
00754 
00755      gzopen can be used to read a file which is not in gzip format; in this
00756    case gzread will directly read from the file without decompression.
00757 
00758      gzopen returns NULL if the file could not be opened or if there was
00759    insufficient memory to allocate the (de)compression state; errno
00760    can be checked to distinguish the two cases (if errno is zero, the
00761    zlib error is Z_MEM_ERROR).  */
00762 
00763 gzFile gzdopen  OF((int fd, const char *mode));
00764 /*
00765      gzdopen() associates a gzFile with the file descriptor fd.  File
00766    descriptors are obtained from calls like open, dup, creat, pipe or
00767    fileno (in the file has been previously opened with fopen).
00768    The mode parameter is as in gzopen.
00769      The next call of gzclose on the returned gzFile will also close the
00770    file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
00771    descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
00772      gzdopen returns NULL if there was insufficient memory to allocate
00773    the (de)compression state.
00774 */
00775 
00776 int gzsetparams OF((gzFile file, int level, int strategy));
00777 /*
00778      Dynamically update the compression level or strategy. See the description
00779    of deflateInit2 for the meaning of these parameters.
00780      gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
00781    opened for writing.
00782 */
00783 
00784 int    gzread  OF((gzFile file, voidp buf, unsigned len));
00785 /*
00786      Reads the given number of uncompressed bytes from the compressed file.
00787    If the input file was not in gzip format, gzread copies the given number
00788    of bytes into the buffer.
00789      gzread returns the number of uncompressed bytes actually read (0 for
00790    end of file, -1 for error). */
00791 
00792 int    gzwrite OF((gzFile file, 
00793                    const voidp buf, unsigned len));
00794 /*
00795      Writes the given number of uncompressed bytes into the compressed file.
00796    gzwrite returns the number of uncompressed bytes actually written
00797    (0 in case of error).
00798 */
00799 
00800 int    QDECL gzprintf OF((gzFile file, const char *format, ...));
00801 /*
00802      Converts, formats, and writes the args to the compressed file under
00803    control of the format string, as in fprintf. gzprintf returns the number of
00804    uncompressed bytes actually written (0 in case of error).
00805 */
00806 
00807 int gzputs OF((gzFile file, const char *s));
00808 /*
00809       Writes the given null-terminated string to the compressed file, excluding
00810    the terminating null character.
00811       gzputs returns the number of characters written, or -1 in case of error.
00812 */
00813 
00814 char * gzgets OF((gzFile file, char *buf, int len));
00815 /*
00816       Reads bytes from the compressed file until len-1 characters are read, or
00817    a newline character is read and transferred to buf, or an end-of-file
00818    condition is encountered.  The string is then terminated with a null
00819    character.
00820       gzgets returns buf, or Z_NULL in case of error.
00821 */
00822 
00823 int    gzputc OF((gzFile file, int c));
00824 /*
00825       Writes c, converted to an unsigned char, into the compressed file.
00826    gzputc returns the value that was written, or -1 in case of error.
00827 */
00828 
00829 int    gzgetc OF((gzFile file));
00830 /*
00831       Reads one byte from the compressed file. gzgetc returns this byte
00832    or -1 in case of end of file or error.
00833 */
00834 
00835 int    gzflush OF((gzFile file, int flush));
00836 /*
00837      Flushes all pending output into the compressed file. The parameter
00838    flush is as in the deflate() function. The return value is the zlib
00839    error number (see function gzerror below). gzflush returns Z_OK if
00840    the flush parameter is Z_FINISH and all output could be flushed.
00841      gzflush should be called only when strictly necessary because it can
00842    degrade compression.
00843 */
00844 
00845 long gzseek OF((gzFile file,
00846                       long offset, int whence));
00847 /* 
00848       Sets the starting position for the next gzread or gzwrite on the
00849    given compressed file. The offset represents a number of bytes in the
00850    uncompressed data stream. The whence parameter is defined as in lseek(2);
00851    the value SEEK_END is not supported.
00852      If the file is opened for reading, this function is emulated but can be
00853    extremely slow. If the file is opened for writing, only forward seeks are
00854    supported; gzseek then compresses a sequence of zeroes up to the new
00855    starting position.
00856 
00857       gzseek returns the resulting offset location as measured in bytes from
00858    the beginning of the uncompressed stream, or -1 in case of error, in
00859    particular if the file is opened for writing and the new starting position
00860    would be before the current position.
00861 */
00862 
00863 int    gzrewind OF((gzFile file));
00864 /*
00865      Rewinds the given file. This function is supported only for reading.
00866 
00867    gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
00868 */
00869 
00870 long    gztell OF((gzFile file));
00871 /*
00872      Returns the starting position for the next gzread or gzwrite on the
00873    given compressed file. This position represents a number of bytes in the
00874    uncompressed data stream.
00875 
00876    gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
00877 */
00878 
00879 int gzeof OF((gzFile file));
00880 /*
00881      Returns 1 when EOF has previously been detected reading the given
00882    input stream, otherwise zero.
00883 */
00884 
00885 int    gzclose OF((gzFile file));
00886 /*
00887      Flushes all pending output if necessary, closes the compressed file
00888    and deallocates all the (de)compression state. The return value is the zlib
00889    error number (see function gzerror below).
00890 */
00891 
00892 // static const char * gzerror OF((gzFile file, int *errnum));
00893 /*
00894      Returns the error message for the last error which occurred on the
00895    given compressed file. errnum is set to zlib error number. If an
00896    error occurred in the file system and not in the compression library,
00897    errnum is set to Z_ERRNO and the application may consult errno
00898    to get the exact error code.
00899 */
00900 
00901                         /* checksum functions */
00902 
00903 /*
00904      These functions are not related to compression but are exported
00905    anyway because they might be useful in applications using the
00906    compression library.
00907 */
00908 
00909 static uLong adler32 OF((uLong adler, const Byte *buf, uInt len));
00910 
00911 /*
00912      Update a running Adler-32 checksum with the bytes buf[0..len-1] and
00913    return the updated checksum. If buf is NULL, this function returns
00914    the required initial value for the checksum.
00915    An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
00916    much faster. Usage example:
00917 
00918      uLong adler = adler32(0L, Z_NULL, 0);
00919 
00920      while (read_buffer(buffer, length) != EOF) {
00921        adler = adler32(adler, buffer, length);
00922      }
00923      if (adler != original_adler) error();
00924 */
00925 
00926                         /* various hacks, don't look :) */
00927 
00928 /* deflateInit and inflateInit are macros to allow checking the zlib version
00929  * and the compiler's view of z_stream:
00930  */
00931 /*
00932 static int deflateInit_ OF((z_streamp strm, int level,
00933                                      const char *version, int stream_size));
00934 static int inflateInit_ OF((z_streamp strm,
00935                                      const char *version, int stream_size));
00936 static int deflateInit2_ OF((z_streamp strm, int  level, int  method,
00937                                       int windowBits, int memLevel,
00938                                       int strategy, const char *version,
00939                                       int stream_size));
00940 */
00941 static int inflateInit2_ OF((z_streamp strm, int  windowBits,
00942                                       const char *version, int stream_size));
00943 
00944 #define deflateInit(strm, level) \
00945         deflateInit_((strm), (level),       ZLIB_VERSION, sizeof(z_stream))
00946 #define inflateInit(strm) \
00947         inflateInit_((strm),                ZLIB_VERSION, sizeof(z_stream))
00948 #define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
00949         deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
00950                       (strategy),           ZLIB_VERSION, sizeof(z_stream))
00951 #define inflateInit2(strm, windowBits) \
00952         inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
00953 
00954 
00955 // static const char   * zError           OF((int err));
00956 // static int            inflateSyncPoint OF((z_streamp z));
00957 // static const uLong * get_crc_table    OF((void));
00958 
00959 typedef unsigned char  uch;
00960 typedef unsigned short ush;
00961 typedef unsigned long  ulg;
00962 
00963 // static const char *z_errmsg[10]; /* indexed by 2-zlib_error */
00964 /* (size given to avoid silly warnings with Visual C++) */
00965 
00966 #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
00967 
00968 #define ERR_RETURN(strm,err) \
00969   return (strm->msg = (char*)ERR_MSG(err), (err))
00970 /* To be used only when the state is known to be valid */
00971 
00972         /* common constants */
00973 
00974 #ifndef DEF_WBITS
00975 #  define DEF_WBITS MAX_WBITS
00976 #endif
00977 /* default windowBits for decompression. MAX_WBITS is for compression only */
00978 
00979 #if MAX_MEM_LEVEL >= 8
00980 #  define DEF_MEM_LEVEL 8
00981 #else
00982 #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
00983 #endif
00984 /* default memLevel */
00985 
00986 #define STORED_BLOCK 0
00987 #define STATIC_TREES 1
00988 #define DYN_TREES    2
00989 /* The three kinds of block type */
00990 
00991 #define MIN_MATCH  3
00992 #define MAX_MATCH  258
00993 /* The minimum and maximum match lengths */
00994 
00995 #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
00996 
00997         /* target dependencies */
00998 
00999         /* Common defaults */
01000 
01001 #ifndef OS_CODE
01002 #  define OS_CODE  0x03  /* assume Unix */
01003 #endif
01004 
01005 #ifndef F_OPEN
01006 #  define F_OPEN(name, mode) fopen((name), (mode))
01007 #endif
01008 
01009          /* functions */
01010 
01011 #ifdef HAVE_STRERROR
01012    extern char *strerror OF((int));
01013 #  define zstrerror(errnum) strerror(errnum)
01014 #else
01015 #  define zstrerror(errnum) ""
01016 #endif
01017 
01018 #define zmemcpy Com_Memcpy
01019 #define zmemcmp memcmp
01020 #define zmemzero(dest, len) Com_Memset(dest, 0, len)
01021 
01022 /* Diagnostic functions */
01023 #ifdef _ZIP_DEBUG_
01024    int z_verbose = 0;
01025 #  define Assert(cond,msg) assert(cond);
01026    //{if(!(cond)) Sys_Error(msg);}
01027 #  define Trace(x) {if (z_verbose>=0) Sys_Error x ;}
01028 #  define Tracev(x) {if (z_verbose>0) Sys_Error x ;}
01029 #  define Tracevv(x) {if (z_verbose>1) Sys_Error x ;}
01030 #  define Tracec(c,x) {if (z_verbose>0 && (c)) Sys_Error x ;}
01031 #  define Tracecv(c,x) {if (z_verbose>1 && (c)) Sys_Error x ;}
01032 #else
01033 #  define Assert(cond,msg)
01034 #  define Trace(x)
01035 #  define Tracev(x)
01036 #  define Tracevv(x)
01037 #  define Tracec(c,x)
01038 #  define Tracecv(c,x)
01039 #endif
01040 
01041 
01042 typedef uLong (*check_func) OF((uLong check, const Byte *buf, uInt len));
01043 static voidp zcalloc OF((voidp opaque, unsigned items, unsigned size));
01044 static void   zcfree  OF((voidp opaque, voidp ptr));
01045 
01046 #define ZALLOC(strm, items, size) \
01047            (*((strm)->zalloc))((strm)->opaque, (items), (size))
01048 #define ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidp)(addr))
01049 #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
01050 
01051 
01052 #if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES) && \
01053                       !defined(CASESENSITIVITYDEFAULT_NO)
01054 #define CASESENSITIVITYDEFAULT_NO
01055 #endif
01056 
01057 
01058 #ifndef UNZ_BUFSIZE
01059 #define UNZ_BUFSIZE (65536)
01060 #endif
01061 
01062 #ifndef UNZ_MAXFILENAMEINZIP
01063 #define UNZ_MAXFILENAMEINZIP (256)
01064 #endif
01065 
01066 #ifndef ALLOC
01067 # define ALLOC(size) (Z_Malloc(size))
01068 #endif
01069 #ifndef TRYFREE
01070 # define TRYFREE(p) {if (p) Z_Free(p);}
01071 #endif
01072 
01073 #define SIZECENTRALDIRITEM (0x2e)
01074 #define SIZEZIPLOCALHEADER (0x1e)
01075 
01076 
01077 
01078 /* ===========================================================================
01079      Read a byte from a gz_stream; update next_in and avail_in. Return EOF
01080    for end of file.
01081    IN assertion: the stream s has been sucessfully opened for reading.
01082 */
01083 
01084 /*
01085 static int unzlocal_getByte(FILE *fin,int *pi)
01086 {
01087     unsigned char c;
01088     int err = fread(&c, 1, 1, fin);
01089     if (err==1)
01090     {
01091         *pi = (int)c;
01092         return UNZ_OK;
01093     }
01094     else
01095     {
01096         if (ferror(fin)) 
01097             return UNZ_ERRNO;
01098         else
01099             return UNZ_EOF;
01100     }
01101 }
01102 */
01103 
01104 /* ===========================================================================
01105    Reads a long in LSB order from the given gz_stream. Sets 
01106 */
01107 static int unzlocal_getShort (FILE* fin, uLong *pX)
01108 {
01109     short   v;
01110 
01111     fread( &v, sizeof(v), 1, fin );
01112 
01113     *pX = LittleShort( v);
01114     return UNZ_OK;
01115 
01116 /*
01117     uLong x ;
01118     int i;
01119     int err;
01120 
01121     err = unzlocal_getByte(fin,&i);
01122     x = (uLong)i;
01123     
01124     if (err==UNZ_OK)
01125         err = unzlocal_getByte(fin,&i);
01126     x += ((uLong)i)<<8;
01127    
01128     if (err==UNZ_OK)
01129         *pX = x;
01130     else
01131         *pX = 0;
01132     return err;
01133 */
01134 }
01135 
01136 static int unzlocal_getLong (FILE *fin, uLong *pX)
01137 {
01138     int     v;
01139 
01140     fread( &v, sizeof(v), 1, fin );
01141 
01142     *pX = LittleLong( v);
01143     return UNZ_OK;
01144 
01145 /*
01146     uLong x ;
01147     int i;
01148     int err;
01149 
01150     err = unzlocal_getByte(fin,&i);
01151     x = (uLong)i;
01152     
01153     if (err==UNZ_OK)
01154         err = unzlocal_getByte(fin,&i);
01155     x += ((uLong)i)<<8;
01156 
01157     if (err==UNZ_OK)
01158         err = unzlocal_getByte(fin,&i);
01159     x += ((uLong)i)<<16;
01160 
01161     if (err==UNZ_OK)
01162         err = unzlocal_getByte(fin,&i);
01163     x += ((uLong)i)<<24;
01164    
01165     if (err==UNZ_OK)
01166         *pX = x;
01167     else
01168         *pX = 0;
01169     return err;
01170 */
01171 }
01172 
01173 
01174 /* My own strcmpi / strcasecmp */
01175 static int strcmpcasenosensitive_internal (const char* fileName1,const char* fileName2)
01176 {
01177     for (;;)
01178     {
01179         char c1=*(fileName1++);
01180         char c2=*(fileName2++);
01181         if ((c1>='a') && (c1<='z'))
01182             c1 -= 0x20;
01183         if ((c2>='a') && (c2<='z'))
01184             c2 -= 0x20;
01185         if (c1=='\0')
01186             return ((c2=='\0') ? 0 : -1);
01187         if (c2=='\0')
01188             return 1;
01189         if (c1<c2)
01190             return -1;
01191         if (c1>c2)
01192             return 1;
01193     }
01194 }
01195 
01196 
01197 #ifdef  CASESENSITIVITYDEFAULT_NO
01198 #define CASESENSITIVITYDEFAULTVALUE 2
01199 #else
01200 #define CASESENSITIVITYDEFAULTVALUE 1
01201 #endif
01202 
01203 #ifndef STRCMPCASENOSENTIVEFUNCTION
01204 #define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal
01205 #endif
01206 
01207 /* 
01208    Compare two filename (fileName1,fileName2).
01209    If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp)
01210    If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi
01211                                                                 or strcasecmp)
01212    If iCaseSenisivity = 0, case sensitivity is defaut of your operating system
01213         (like 1 on Unix, 2 on Windows)
01214 
01215 */
01216 extern  int unzStringFileNameCompare (const char* fileName1,const char* fileName2,int iCaseSensitivity)
01217 {
01218     if (iCaseSensitivity==0)
01219         iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE;
01220 
01221     if (iCaseSensitivity==1)
01222         return strcmp(fileName1,fileName2);
01223 
01224     return STRCMPCASENOSENTIVEFUNCTION(fileName1,fileName2);
01225 } 
01226 
01227 #define BUFREADCOMMENT (0x400)
01228 
01229 /*
01230   Locate the Central directory of a zipfile (at the end, just before
01231     the global comment)
01232 */
01233 extern uLong unzlocal_SearchCentralDir(FILE *fin)
01234 {
01235     unsigned char* buf;
01236     uLong uSizeFile;
01237     uLong uBackRead;
01238     uLong uMaxBack=0xffff; /* maximum size of global comment */
01239     uLong uPosFound=0;
01240     
01241     if (fseek(fin,0,SEEK_END) != 0)
01242         return 0;
01243 
01244 
01245     uSizeFile = ftell( fin );
01246     
01247     if (uMaxBack>uSizeFile)
01248         uMaxBack = uSizeFile;
01249 
01250     buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
01251     if (buf==NULL)
01252         return 0;
01253 
01254     uBackRead = 4;
01255     while (uBackRead<uMaxBack)
01256     {
01257         uLong uReadSize,uReadPos ;
01258         int i;
01259         if (uBackRead+BUFREADCOMMENT>uMaxBack) 
01260             uBackRead = uMaxBack;
01261         else
01262             uBackRead+=BUFREADCOMMENT;
01263         uReadPos = uSizeFile-uBackRead ;
01264         
01265         uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? 
01266                      (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);
01267         if (fseek(fin,uReadPos,SEEK_SET)!=0)
01268             break;
01269 
01270         if (fread(buf,(uInt)uReadSize,1,fin)!=1)
01271             break;
01272 
01273                 for (i=(int)uReadSize-3; (i--)>0;)
01274             if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && 
01275                 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
01276             {
01277                 uPosFound = uReadPos+i;
01278                 break;
01279             }
01280 
01281         if (uPosFound!=0)
01282             break;
01283     }
01284     TRYFREE(buf);
01285     return uPosFound;
01286 }
01287 
01288 extern unzFile unzReOpen (const char* path, unzFile file)
01289 {
01290     unz_s *s;
01291     FILE * fin;
01292 
01293     fin=fopen(path,"rb");
01294     if (fin==NULL)
01295         return NULL;
01296 
01297     s=(unz_s*)ALLOC(sizeof(unz_s));
01298     Com_Memcpy(s, (unz_s*)file, sizeof(unz_s));
01299 
01300     s->file = fin;
01301     return (unzFile)s;  
01302 }
01303 
01304 /*
01305   Open a Zip file. path contain the full pathname (by example,
01306      on a Windows NT computer "c:\\test\\zlib109.zip" or on an Unix computer
01307      "zlib/zlib109.zip".
01308      If the zipfile cannot be opened (file don't exist or in not valid), the
01309        return value is NULL.
01310      Else, the return value is a unzFile Handle, usable with other function
01311        of this unzip package.
01312 */
01313 extern unzFile unzOpen (const char* path)
01314 {
01315     unz_s us;
01316     unz_s *s;
01317     uLong central_pos,uL;
01318     FILE * fin ;
01319 
01320     uLong number_disk;          /* number of the current dist, used for 
01321                                    spaning ZIP, unsupported, always 0*/
01322     uLong number_disk_with_CD;  /* number the the disk with central dir, used
01323                                    for spaning ZIP, unsupported, always 0*/
01324     uLong number_entry_CD;      /* total number of entries in
01325                                    the central dir 
01326                                    (same than number_entry on nospan) */
01327 
01328     int err=UNZ_OK;
01329 
01330     fin=fopen(path,"rb");
01331     if (fin==NULL)
01332         return NULL;
01333 
01334     central_pos = unzlocal_SearchCentralDir(fin);
01335     if (central_pos==0)
01336         err=UNZ_ERRNO;
01337 
01338     if (fseek(fin,central_pos,SEEK_SET)!=0)
01339         err=UNZ_ERRNO;
01340 
01341     /* the signature, already checked */
01342     if (unzlocal_getLong(fin,&uL)!=UNZ_OK)
01343         err=UNZ_ERRNO;
01344 
01345     /* number of this disk */
01346     if (unzlocal_getShort(fin,&number_disk)!=UNZ_OK)
01347         err=UNZ_ERRNO;
01348 
01349     /* number of the disk with the start of the central directory */
01350     if (unzlocal_getShort(fin,&number_disk_with_CD)!=UNZ_OK)
01351         err=UNZ_ERRNO;
01352 
01353     /* total number of entries in the central dir on this disk */
01354     if (unzlocal_getShort(fin,&us.gi.number_entry)!=UNZ_OK)
01355         err=UNZ_ERRNO;
01356 
01357     /* total number of entries in the central dir */
01358     if (unzlocal_getShort(fin,&number_entry_CD)!=UNZ_OK)
01359         err=UNZ_ERRNO;
01360 
01361     if ((number_entry_CD!=us.gi.number_entry) ||
01362         (number_disk_with_CD!=0) ||
01363         (number_disk!=0))
01364         err=UNZ_BADZIPFILE;
01365 
01366     /* size of the central directory */
01367     if (unzlocal_getLong(fin,&us.size_central_dir)!=UNZ_OK)
01368         err=UNZ_ERRNO;
01369 
01370     /* offset of start of central directory with respect to the 
01371           starting disk number */
01372     if (unzlocal_getLong(fin,&us.offset_central_dir)!=UNZ_OK)
01373         err=UNZ_ERRNO;
01374 
01375     /* zipfile comment length */
01376     if (unzlocal_getShort(fin,&us.gi.size_comment)!=UNZ_OK)
01377         err=UNZ_ERRNO;
01378 
01379     if ((central_pos<us.offset_central_dir+us.size_central_dir) && 
01380         (err==UNZ_OK))
01381         err=UNZ_BADZIPFILE;
01382 
01383     if (err!=UNZ_OK)
01384     {
01385         fclose(fin);
01386         return NULL;
01387     }
01388 
01389     us.file=fin;
01390     us.byte_before_the_zipfile = central_pos -
01391                             (us.offset_central_dir+us.size_central_dir);
01392     us.central_pos = central_pos;
01393     us.pfile_in_zip_read = NULL;
01394     
01395 
01396     s=(unz_s*)ALLOC(sizeof(unz_s));
01397     *s=us;
01398 //  unzGoToFirstFile((unzFile)s);   
01399     return (unzFile)s;  
01400 }
01401 
01402 
01403 /*
01404   Close a ZipFile opened with unzipOpen.
01405   If there is files inside the .Zip opened with unzipOpenCurrentFile (see later),
01406     these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
01407   return UNZ_OK if there is no problem. */
01408 extern int unzClose (unzFile file)
01409 {
01410     unz_s* s;
01411     if (file==NULL)
01412         return UNZ_PARAMERROR;
01413     s=(unz_s*)file;
01414 
01415     if (s->pfile_in_zip_read!=NULL)
01416         unzCloseCurrentFile(file);
01417 
01418     fclose(s->file);
01419     TRYFREE(s);
01420     return UNZ_OK;
01421 }
01422 
01423 
01424 /*
01425   Write info about the ZipFile in the *pglobal_info structure.
01426   No preparation of the structure is needed
01427   return UNZ_OK if there is no problem. */
01428 extern int unzGetGlobalInfo (unzFile file,unz_global_info *pglobal_info)
01429 {
01430     unz_s* s;
01431     if (file==NULL)
01432         return UNZ_PARAMERROR;
01433     s=(unz_s*)file;
01434     *pglobal_info=s->gi;
01435     return UNZ_OK;
01436 }
01437 
01438 
01439 /*
01440    Translate date/time from Dos format to tm_unz (readable more easilty)
01441 */
01442 static void unzlocal_DosDateToTmuDate (uLong ulDosDate, tm_unz* ptm)
01443 {
01444     uLong uDate;
01445     uDate = (uLong)(ulDosDate>>16);
01446     ptm->tm_mday = (uInt)(uDate&0x1f) ;
01447     ptm->tm_mon =  (uInt)((((uDate)&0x1E0)/0x20)-1) ;
01448     ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ;
01449 
01450     ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800);
01451     ptm->tm_min =  (uInt) ((ulDosDate&0x7E0)/0x20) ;
01452     ptm->tm_sec =  (uInt) (2*(ulDosDate&0x1f)) ;
01453 }
01454 
01455 /*
01456   Get Info about the current file in the zipfile, with internal only info
01457 */
01458 static int unzlocal_GetCurrentFileInfoInternal (unzFile file,
01459                                                   unz_file_info *pfile_info,
01460                                                   unz_file_info_internal 
01461                                                   *pfile_info_internal,
01462                                                   char *szFileName,
01463                                                   uLong fileNameBufferSize,
01464                                                   void *extraField,
01465                                                   uLong extraFieldBufferSize,
01466                                                   char *szComment,
01467                                                   uLong commentBufferSize)
01468 {
01469     unz_s* s;
01470     unz_file_info file_info;
01471     unz_file_info_internal file_info_internal;
01472     int err=UNZ_OK;
01473     uLong uMagic;
01474     long lSeek=0;
01475 
01476     if (file==NULL)
01477         return UNZ_PARAMERROR;
01478     s=(unz_s*)file;
01479     if (fseek(s->file,s->pos_in_central_dir+s->byte_before_the_zipfile,SEEK_SET)!=0)
01480         err=UNZ_ERRNO;
01481 
01482 
01483     /* we check the magic */
01484     if (err==UNZ_OK) {
01485         if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)
01486             err=UNZ_ERRNO;
01487         else if (uMagic!=0x02014b50)
01488             err=UNZ_BADZIPFILE;
01489     }
01490     if (unzlocal_getShort(s->file,&file_info.version) != UNZ_OK)
01491         err=UNZ_ERRNO;
01492 
01493     if (unzlocal_getShort(s->file,&file_info.version_needed) != UNZ_OK)
01494         err=UNZ_ERRNO;
01495 
01496     if (unzlocal_getShort(s->file,&file_info.flag) != UNZ_OK)
01497         err=UNZ_ERRNO;
01498 
01499     if (unzlocal_getShort(s->file,&file_info.compression_method) != UNZ_OK)
01500         err=UNZ_ERRNO;
01501 
01502     if (unzlocal_getLong(s->file,&file_info.dosDate) != UNZ_OK)
01503         err=UNZ_ERRNO;
01504 
01505     unzlocal_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date);
01506 
01507     if (unzlocal_getLong(s->file,&file_info.crc) != UNZ_OK)
01508         err=UNZ_ERRNO;
01509 
01510     if (unzlocal_getLong(s->file,&file_info.compressed_size) != UNZ_OK)
01511         err=UNZ_ERRNO;
01512 
01513     if (unzlocal_getLong(s->file,&file_info.uncompressed_size) != UNZ_OK)
01514         err=UNZ_ERRNO;
01515 
01516     if (unzlocal_getShort(s->file,&file_info.size_filename) != UNZ_OK)
01517         err=UNZ_ERRNO;
01518 
01519     if (unzlocal_getShort(s->file,&file_info.