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)