Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4488 lines
144 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // XUnzip.cpp Version 1.1
  9. //
  10. // Authors: Mark Adler et al. (see below)
  11. //
  12. // Modified by: Lucian Wischik
  13. // [email protected]
  14. //
  15. // Version 1.0 - Turned C files into just a single CPP file
  16. // - Made them compile cleanly as C++ files
  17. // - Gave them simpler APIs
  18. // - Added the ability to zip/unzip directly in memory without
  19. // any intermediate files
  20. //
  21. // Modified by: Hans Dietrich
  22. // [email protected]
  23. //
  24. // Version 1.1: - Added Unicode support to CreateZip() and ZipAdd()
  25. // - Changed file names to avoid conflicts with Lucian's files
  26. //
  27. ///////////////////////////////////////////////////////////////////////////////
  28. //
  29. // Lucian Wischik's comments:
  30. // --------------------------
  31. // THIS FILE is almost entirely based upon code by Info-ZIP.
  32. // It has been modified by Lucian Wischik.
  33. // The original code may be found at http://www.info-zip.org
  34. // The original copyright text follows.
  35. //
  36. ///////////////////////////////////////////////////////////////////////////////
  37. //
  38. // Original authors' comments:
  39. // ---------------------------
  40. // This is version 2002-Feb-16 of the Info-ZIP copyright and license. The
  41. // definitive version of this document should be available at
  42. // ftp://ftp.info-zip.org/pub/infozip/license.html indefinitely.
  43. //
  44. // Copyright (c) 1990-2002 Info-ZIP. All rights reserved.
  45. //
  46. // For the purposes of this copyright and license, "Info-ZIP" is defined as
  47. // the following set of individuals:
  48. //
  49. // Mark Adler, John Bush, Karl Davis, Harald Denker, Jean-Michel Dubois,
  50. // Jean-loup Gailly, Hunter Goatley, Ian Gorman, Chris Herborth, Dirk Haase,
  51. // Greg Hartwig, Robert Heath, Jonathan Hudson, Paul Kienitz,
  52. // David Kirschbaum, Johnny Lee, Onno van der Linden, Igor Mandrichenko,
  53. // Steve P. Miller, Sergio Monesi, Keith Owens, George Petrov, Greg Roelofs,
  54. // Kai Uwe Rommel, Steve Salisbury, Dave Smith, Christian Spieler,
  55. // Antoine Verheijen, Paul von Behren, Rich Wales, Mike White
  56. //
  57. // This software is provided "as is", without warranty of any kind, express
  58. // or implied. In no event shall Info-ZIP or its contributors be held liable
  59. // for any direct, indirect, incidental, special or consequential damages
  60. // arising out of the use of or inability to use this software.
  61. //
  62. // Permission is granted to anyone to use this software for any purpose,
  63. // including commercial applications, and to alter it and redistribute it
  64. // freely, subject to the following restrictions:
  65. //
  66. // 1. Redistributions of source code must retain the above copyright notice,
  67. // definition, disclaimer, and this list of conditions.
  68. //
  69. // 2. Redistributions in binary form (compiled executables) must reproduce
  70. // the above copyright notice, definition, disclaimer, and this list of
  71. // conditions in documentation and/or other materials provided with the
  72. // distribution. The sole exception to this condition is redistribution
  73. // of a standard UnZipSFX binary as part of a self-extracting archive;
  74. // that is permitted without inclusion of this license, as long as the
  75. // normal UnZipSFX banner has not been removed from the binary or disabled.
  76. //
  77. // 3. Altered versions--including, but not limited to, ports to new
  78. // operating systems, existing ports with new graphical interfaces, and
  79. // dynamic, shared, or static library versions--must be plainly marked
  80. // as such and must not be misrepresented as being the original source.
  81. // Such altered versions also must not be misrepresented as being
  82. // Info-ZIP releases--including, but not limited to, labeling of the
  83. // altered versions with the names "Info-ZIP" (or any variation thereof,
  84. // including, but not limited to, different capitalizations),
  85. // "Pocket UnZip", "WiZ" or "MacZip" without the explicit permission of
  86. // Info-ZIP. Such altered versions are further prohibited from
  87. // misrepresentative use of the Zip-Bugs or Info-ZIP e-mail addresses or
  88. // of the Info-ZIP URL(s).
  89. //
  90. // 4. Info-ZIP retains the right to use the names "Info-ZIP", "Zip", "UnZip",
  91. // "UnZipSFX", "WiZ", "Pocket UnZip", "Pocket Zip", and "MacZip" for its
  92. // own source and binary releases.
  93. //
  94. ///////////////////////////////////////////////////////////////////////////////
  95. #if defined( WIN32 ) && !defined( _X360 )
  96. #define STRICT
  97. #define WIN32_LEAN_AND_MEAN
  98. #include <windows.h>
  99. #include <tchar.h>
  100. #elif defined(POSIX)
  101. #include <fcntl.h>
  102. #include <sys/stat.h>
  103. #include <sys/time.h>
  104. #include <unistd.h>
  105. #endif
  106. #include <time.h>
  107. #include <stdio.h>
  108. #include <stdlib.h>
  109. #include <string.h>
  110. #include "zip/XUnzip.h"
  111. #if defined(POSIX)
  112. #define _tcslen strlen
  113. #define _tcscpy strcpy
  114. #define _tcscat strcat
  115. #define _tcsstr strstr
  116. #if !defined( _T )
  117. #define _T( arg ) arg
  118. #endif
  119. #define INVALID_HANDLE_VALUE (void*)-1
  120. #define CloseHandle( arg ) close( (int) arg )
  121. #define ZeroMemory( ptr, size ) memset( ptr, 0, size )
  122. #define FILE_CURRENT SEEK_CUR
  123. #define FILE_BEGIN SEEK_SET
  124. #define FILE_END SEEK_END
  125. #define CreateDirectory( dir, ign ) mkdir( dir, S_IRWXU | S_IRWXG | S_IRWXO )
  126. #define SetFilePointer( handle, pos, ign, dir ) lseek( (int) handle, pos, dir )
  127. bool ReadFile( void *handle, void *outbuf, unsigned int toread, unsigned int *nread, void *ignored )
  128. {
  129. *nread = read( (int) handle, outbuf, toread );
  130. return *nread == toread;
  131. }
  132. bool WriteFile( void *handle, void *buf, unsigned int towrite, unsigned int *written, void *ignored )
  133. {
  134. *written = write( (int) handle, buf, towrite );
  135. return *written == towrite;
  136. }
  137. #define FILE_ATTRIBUTE_NORMAL S_IFREG
  138. #define FILE_ATTRIBUTE_DIRECTORY S_IFDIR
  139. #define FILE_ATTRIBUTE_ARCHIVE 0
  140. #define FILE_ATTRIBUTE_HIDDEN 0
  141. #define FILE_ATTRIBUTE_READONLY 0
  142. #define FILE_ATTRIBUTE_SYSTEM 0
  143. typedef unsigned char BYTE;
  144. #endif // POSIX
  145. #if defined( _X360 )
  146. #include "xbox/xbox_win32stubs.h"
  147. #endif
  148. // THIS FILE is almost entirely based upon code by Jean-loup Gailly
  149. // and Mark Adler. It has been modified by Lucian Wischik.
  150. // The original code may be found at http://www.gzip.org/zlib/
  151. // The original copyright text follows.
  152. //
  153. //
  154. //
  155. // zlib.h -- interface of the 'zlib' general purpose compression library
  156. // version 1.1.3, July 9th, 1998
  157. //
  158. // Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
  159. //
  160. // This software is provided 'as-is', without any express or implied
  161. // warranty. In no event will the authors be held liable for any damages
  162. // arising from the use of this software.
  163. //
  164. // Permission is granted to anyone to use this software for any purpose,
  165. // including commercial applications, and to alter it and redistribute it
  166. // freely, subject to the following restrictions:
  167. //
  168. // 1. The origin of this software must not be misrepresented; you must not
  169. // claim that you wrote the original software. If you use this software
  170. // in a product, an acknowledgment in the product documentation would be
  171. // appreciated but is not required.
  172. // 2. Altered source versions must be plainly marked as such, and must not be
  173. // misrepresented as being the original software.
  174. // 3. This notice may not be removed or altered from any source distribution.
  175. //
  176. // Jean-loup Gailly Mark Adler
  177. // [email protected] [email protected]
  178. //
  179. //
  180. // The data format used by the zlib library is described by RFCs (Request for
  181. // Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
  182. // (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
  183. //
  184. //
  185. // The 'zlib' compression library provides in-memory compression and
  186. // decompression functions, including integrity checks of the uncompressed
  187. // data. This version of the library supports only one compression method
  188. // (deflation) but other algorithms will be added later and will have the same
  189. // stream interface.
  190. //
  191. // Compression can be done in a single step if the buffers are large
  192. // enough (for example if an input file is mmap'ed), or can be done by
  193. // repeated calls of the compression function. In the latter case, the
  194. // application must provide more input and/or consume the output
  195. // (providing more output space) before each call.
  196. //
  197. // The library also supports reading and writing files in gzip (.gz) format
  198. // with an interface similar to that of stdio.
  199. //
  200. // The library does not install any signal handler. The decoder checks
  201. // the consistency of the compressed data, so the library should never
  202. // crash even in case of corrupted input.
  203. //
  204. // for more info about .ZIP format, see ftp://ftp.cdrom.com/pub/infozip/doc/appnote-970311-iz.zip
  205. // PkWare has also a specification at ftp://ftp.pkware.com/probdesc.zip
  206. #define zmalloc(len) malloc(len)
  207. #define zfree(p) free(p)
  208. /*
  209. void *zmalloc(unsigned int len)
  210. { char *buf = new char[len+32];
  211. for (int i=0; i<16; i++)
  212. { buf[i]=i;
  213. buf[len+31-i]=i;
  214. }
  215. *((unsigned int*)buf) = len;
  216. char c[1000]; wsprintf(c,"malloc 0x%lx - %lu",buf+16,len);
  217. OutputDebugString(c);
  218. return buf+16;
  219. }
  220. void zfree(void *buf)
  221. { char c[1000]; wsprintf(c,"free 0x%lx",buf);
  222. OutputDebugString(c);
  223. char *p = ((char*)buf)-16;
  224. unsigned int len = *((unsigned int*)p);
  225. bool blown=false;
  226. for (int i=0; i<16; i++)
  227. { char lo = p[i];
  228. char hi = p[len+31-i];
  229. if (hi!=i || (lo!=i && i>4)) blown=true;
  230. }
  231. if (blown)
  232. { OutputDebugString("BLOWN!!!");
  233. }
  234. delete[] p;
  235. }
  236. */
  237. #pragma warning(disable : 4702) // unreachable code
  238. typedef struct tm_unz_s
  239. { unsigned int tm_sec; // seconds after the minute - [0,59]
  240. unsigned int tm_min; // minutes after the hour - [0,59]
  241. unsigned int tm_hour; // hours since midnight - [0,23]
  242. unsigned int tm_mday; // day of the month - [1,31]
  243. unsigned int tm_mon; // months since January - [0,11]
  244. unsigned int tm_year; // years - [1980..2044]
  245. } tm_unz;
  246. // unz_global_info structure contain global data about the ZIPfile
  247. typedef struct unz_global_info_s
  248. { unsigned long number_entry; // total number of entries in the central dir on this disk
  249. unsigned long size_comment; // size of the global comment of the zipfile
  250. } unz_global_info;
  251. // unz_file_info contain information about a file in the zipfile
  252. typedef struct unz_file_info_s
  253. { unsigned long version; // version made by 2 bytes
  254. unsigned long version_needed; // version needed to extract 2 bytes
  255. unsigned long flag; // general purpose bit flag 2 bytes
  256. unsigned long compression_method; // compression method 2 bytes
  257. unsigned long dosDate; // last mod file date in Dos fmt 4 bytes
  258. unsigned long crc; // crc-32 4 bytes
  259. unsigned long compressed_size; // compressed size 4 bytes
  260. unsigned long uncompressed_size; // uncompressed size 4 bytes
  261. unsigned long size_filename; // filename length 2 bytes
  262. unsigned long size_file_extra; // extra field length 2 bytes
  263. unsigned long size_file_comment; // file comment length 2 bytes
  264. unsigned long disk_num_start; // disk number start 2 bytes
  265. unsigned long internal_fa; // internal file attributes 2 bytes
  266. unsigned long external_fa; // external file attributes 4 bytes
  267. tm_unz tmu_date;
  268. } unz_file_info;
  269. #define UNZ_OK (0)
  270. #define UNZ_END_OF_LIST_OF_FILE (-100)
  271. #define UNZ_ERRNO (Z_ERRNO)
  272. #define UNZ_EOF (0)
  273. #define UNZ_PARAMERROR (-102)
  274. #define UNZ_BADZIPFILE (-103)
  275. #define UNZ_INTERNALERROR (-104)
  276. #define UNZ_CRCERROR (-105)
  277. #define ZLIB_VERSION "1.1.3"
  278. // Allowed flush values; see deflate() for details
  279. #define Z_NO_FLUSH 0
  280. #define Z_SYNC_FLUSH 2
  281. #define Z_FULL_FLUSH 3
  282. #define Z_FINISH 4
  283. // compression levels
  284. #define Z_NO_COMPRESSION 0
  285. #define Z_BEST_SPEED 1
  286. #define Z_BEST_COMPRESSION 9
  287. #define Z_DEFAULT_COMPRESSION (-1)
  288. // compression strategy; see deflateInit2() for details
  289. #define Z_FILTERED 1
  290. #define Z_HUFFMAN_ONLY 2
  291. #define Z_DEFAULT_STRATEGY 0
  292. // Possible values of the data_type field
  293. #define Z_BINARY 0
  294. #define Z_ASCII 1
  295. #define Z_UNKNOWN 2
  296. // The deflate compression method (the only one supported in this version)
  297. #define Z_DEFLATED 8
  298. // for initializing zalloc, zfree, opaque
  299. #define Z_NULL 0
  300. // case sensitivity when searching for filenames
  301. #define CASE_SENSITIVE 1
  302. #define CASE_INSENSITIVE 2
  303. // Return codes for the compression/decompression functions. Negative
  304. // values are errors, positive values are used for special but normal events.
  305. #define Z_OK 0
  306. #define Z_STREAM_END 1
  307. #define Z_NEED_DICT 2
  308. #define Z_ERRNO (-1)
  309. #define Z_STREAM_ERROR (-2)
  310. #define Z_DATA_ERROR (-3)
  311. #define Z_MEM_ERROR (-4)
  312. #define Z_BUF_ERROR (-5)
  313. #define Z_VERSION_ERROR (-6)
  314. // Basic data types
  315. typedef unsigned char Byte; // 8 bits
  316. typedef unsigned int uInt; // 16 bits or more
  317. typedef unsigned long uLong; // 32 bits or more
  318. typedef void *voidpf;
  319. typedef void *voidp;
  320. typedef long z_off_t;
  321. typedef voidpf (*alloc_func) (voidpf opaque, uInt items, uInt size);
  322. typedef void (*free_func) (voidpf opaque, voidpf address);
  323. struct internal_state;
  324. typedef struct z_stream_s {
  325. Byte *next_in; // next input byte
  326. uInt avail_in; // number of bytes available at next_in
  327. uLong total_in; // total nb of input bytes read so far
  328. Byte *next_out; // next output byte should be put there
  329. uInt avail_out; // remaining free space at next_out
  330. uLong total_out; // total nb of bytes output so far
  331. char *msg; // last error message, NULL if no error
  332. struct internal_state *state; // not visible by applications
  333. alloc_func zalloc; // used to allocate the internal state
  334. free_func zfree; // used to free the internal state
  335. voidpf opaque; // private data object passed to zalloc and zfree
  336. int data_type; // best guess about the data type: ascii or binary
  337. uLong adler; // adler32 value of the uncompressed data
  338. uLong reserved; // reserved for future use
  339. } z_stream;
  340. typedef z_stream *z_streamp;
  341. // The application must update next_in and avail_in when avail_in has
  342. // dropped to zero. It must update next_out and avail_out when avail_out
  343. // has dropped to zero. The application must initialize zalloc, zfree and
  344. // opaque before calling the init function. All other fields are set by the
  345. // compression library and must not be updated by the application.
  346. //
  347. // The opaque value provided by the application will be passed as the first
  348. // parameter for calls of zalloc and zfree. This can be useful for custom
  349. // memory management. The compression library attaches no meaning to the
  350. // opaque value.
  351. //
  352. // zalloc must return Z_NULL if there is not enough memory for the object.
  353. // If zlib is used in a multi-threaded application, zalloc and zfree must be
  354. // thread safe.
  355. //
  356. // The fields total_in and total_out can be used for statistics or
  357. // progress reports. After compression, total_in holds the total size of
  358. // the uncompressed data and may be saved for use in the decompressor
  359. // (particularly if the decompressor wants to decompress everything in
  360. // a single step).
  361. //
  362. // basic functions
  363. const char *zlibVersion ();
  364. // The application can compare zlibVersion and ZLIB_VERSION for consistency.
  365. // If the first character differs, the library code actually used is
  366. // not compatible with the zlib.h header file used by the application.
  367. // This check is automatically made by inflateInit.
  368. int inflate (z_streamp strm, int flush);
  369. //
  370. // inflate decompresses as much data as possible, and stops when the input
  371. // buffer becomes empty or the output buffer becomes full. It may some
  372. // introduce some output latency (reading input without producing any output)
  373. // except when forced to flush.
  374. //
  375. // The detailed semantics are as follows. inflate performs one or both of the
  376. // following actions:
  377. //
  378. // - Decompress more input starting at next_in and update next_in and avail_in
  379. // accordingly. If not all input can be processed (because there is not
  380. // enough room in the output buffer), next_in is updated and processing
  381. // will resume at this point for the next call of inflate().
  382. //
  383. // - Provide more output starting at next_out and update next_out and avail_out
  384. // accordingly. inflate() provides as much output as possible, until there
  385. // is no more input data or no more space in the output buffer (see below
  386. // about the flush parameter).
  387. //
  388. // Before the call of inflate(), the application should ensure that at least
  389. // one of the actions is possible, by providing more input and/or consuming
  390. // more output, and updating the next_* and avail_* values accordingly.
  391. // The application can consume the uncompressed output when it wants, for
  392. // example when the output buffer is full (avail_out == 0), or after each
  393. // call of inflate(). If inflate returns Z_OK and with zero avail_out, it
  394. // must be called again after making room in the output buffer because there
  395. // might be more output pending.
  396. //
  397. // If the parameter flush is set to Z_SYNC_FLUSH, inflate flushes as much
  398. // output as possible to the output buffer. The flushing behavior of inflate is
  399. // not specified for values of the flush parameter other than Z_SYNC_FLUSH
  400. // and Z_FINISH, but the current implementation actually flushes as much output
  401. // as possible anyway.
  402. //
  403. // inflate() should normally be called until it returns Z_STREAM_END or an
  404. // error. However if all decompression is to be performed in a single step
  405. // (a single call of inflate), the parameter flush should be set to
  406. // Z_FINISH. In this case all pending input is processed and all pending
  407. // output is flushed; avail_out must be large enough to hold all the
  408. // uncompressed data. (The size of the uncompressed data may have been saved
  409. // by the compressor for this purpose.) The next operation on this stream must
  410. // be inflateEnd to deallocate the decompression state. The use of Z_FINISH
  411. // is never required, but can be used to inform inflate that a faster routine
  412. // may be used for the single inflate() call.
  413. //
  414. // If a preset dictionary is needed at this point (see inflateSetDictionary
  415. // below), inflate sets strm-adler to the adler32 checksum of the
  416. // dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise
  417. // it sets strm->adler to the adler32 checksum of all output produced
  418. // so far (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or
  419. // an error code as described below. At the end of the stream, inflate()
  420. // checks that its computed adler32 checksum is equal to that saved by the
  421. // compressor and returns Z_STREAM_END only if the checksum is correct.
  422. //
  423. // inflate() returns Z_OK if some progress has been made (more input processed
  424. // or more output produced), Z_STREAM_END if the end of the compressed data has
  425. // been reached and all uncompressed output has been produced, Z_NEED_DICT if a
  426. // preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
  427. // corrupted (input stream not conforming to the zlib format or incorrect
  428. // adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent
  429. // (for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not
  430. // enough memory, Z_BUF_ERROR if no progress is possible or if there was not
  431. // enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR
  432. // case, the application may then call inflateSync to look for a good
  433. // compression block.
  434. //
  435. int inflateEnd (z_streamp strm);
  436. //
  437. // All dynamically allocated data structures for this stream are freed.
  438. // This function discards any unprocessed input and does not flush any
  439. // pending output.
  440. //
  441. // inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
  442. // was inconsistent. In the error case, msg may be set but then points to a
  443. // static string (which must not be deallocated).
  444. // Advanced functions
  445. // The following functions are needed only in some special applications.
  446. int inflateSetDictionary (z_streamp strm,
  447. const Byte *dictionary,
  448. uInt dictLength);
  449. //
  450. // Initializes the decompression dictionary from the given uncompressed byte
  451. // sequence. This function must be called immediately after a call of inflate
  452. // if this call returned Z_NEED_DICT. The dictionary chosen by the compressor
  453. // can be determined from the Adler32 value returned by this call of
  454. // inflate. The compressor and decompressor must use exactly the same
  455. // dictionary.
  456. //
  457. // inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
  458. // parameter is invalid (such as NULL dictionary) or the stream state is
  459. // inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
  460. // expected one (incorrect Adler32 value). inflateSetDictionary does not
  461. // perform any decompression: this will be done by subsequent calls of
  462. // inflate().
  463. int inflateSync (z_streamp strm);
  464. //
  465. // Skips invalid compressed data until a full flush point can be found, or until all
  466. // available input is skipped. No output is provided.
  467. //
  468. // inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
  469. // if no more input was provided, Z_DATA_ERROR if no flush point has been found,
  470. // or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
  471. // case, the application may save the current current value of total_in which
  472. // indicates where valid compressed data was found. In the error case, the
  473. // application may repeatedly call inflateSync, providing more input each time,
  474. // until success or end of the input data.
  475. int inflateReset (z_streamp strm);
  476. // This function is equivalent to inflateEnd followed by inflateInit,
  477. // but does not free and reallocate all the internal decompression state.
  478. // The stream will keep attributes that may have been set by inflateInit2.
  479. //
  480. // inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
  481. // stream state was inconsistent (such as zalloc or state being NULL).
  482. //
  483. // checksum functions
  484. // These functions are not related to compression but are exported
  485. // anyway because they might be useful in applications using the
  486. // compression library.
  487. uLong adler32 (uLong adler, const Byte *buf, uInt len);
  488. // Update a running Adler-32 checksum with the bytes buf[0..len-1] and
  489. // return the updated checksum. If buf is NULL, this function returns
  490. // the required initial value for the checksum.
  491. // An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
  492. // much faster. Usage example:
  493. //
  494. // uLong adler = adler32(0L, Z_NULL, 0);
  495. //
  496. // while (read_buffer(buffer, length) != EOF) {
  497. // adler = adler32(adler, buffer, length);
  498. // }
  499. // if (adler != original_adler) error();
  500. uLong ucrc32 (uLong crc, const Byte *buf, uInt len);
  501. // Update a running crc with the bytes buf[0..len-1] and return the updated
  502. // crc. If buf is NULL, this function returns the required initial value
  503. // for the crc. Pre- and post-conditioning (one's complement) is performed
  504. // within this function so it shouldn't be done by the application.
  505. // Usage example:
  506. //
  507. // uLong crc = crc32(0L, Z_NULL, 0);
  508. //
  509. // while (read_buffer(buffer, length) != EOF) {
  510. // crc = crc32(crc, buffer, length);
  511. // }
  512. // if (crc != original_crc) error();
  513. const char *zError (int err);
  514. int inflateSyncPoint (z_streamp z);
  515. const uLong *get_crc_table (void);
  516. typedef unsigned char uch;
  517. typedef uch uchf;
  518. typedef unsigned short ush;
  519. typedef ush ushf;
  520. typedef unsigned long ulg;
  521. const char * const z_errmsg[10] = { // indexed by 2-zlib_error
  522. "need dictionary", // Z_NEED_DICT 2
  523. "stream end", // Z_STREAM_END 1
  524. "", // Z_OK 0
  525. "file error", // Z_ERRNO (-1)
  526. "stream error", // Z_STREAM_ERROR (-2)
  527. "data error", // Z_DATA_ERROR (-3)
  528. "insufficient memory", // Z_MEM_ERROR (-4)
  529. "buffer error", // Z_BUF_ERROR (-5)
  530. "incompatible version",// Z_VERSION_ERROR (-6)
  531. ""};
  532. #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
  533. #define ERR_RETURN(strm,err) \
  534. return (strm->msg = (char*)ERR_MSG(err), (err))
  535. // To be used only when the state is known to be valid
  536. // common constants
  537. #define STORED_BLOCK 0
  538. #define STATIC_TREES 1
  539. #define DYN_TREES 2
  540. // The three kinds of block type
  541. #define MIN_MATCH 3
  542. #define MAX_MATCH 258
  543. // The minimum and maximum match lengths
  544. #define PRESET_DICT 0x20 // preset dictionary flag in zlib header
  545. // target dependencies
  546. #define OS_CODE 0x0b // Window 95 & Windows NT
  547. // functions
  548. #define zmemzero(dest, len) memset(dest, 0, len)
  549. // Diagnostic functions
  550. #undef Assert
  551. #undef Trace
  552. #undef Tracev
  553. #undef Tracevv
  554. #undef Tracec
  555. #undef Tracecv
  556. #ifdef DEBUG
  557. int z_verbose = 0;
  558. void z_error (char *m) {fprintf(stderr, "%s\n", m); exit(1);}
  559. # define Assert(cond,msg) {if(!(cond)) z_error(msg);}
  560. # define Trace(x) {if (z_verbose>=0) fprintf x ;}
  561. # define Tracev(x) {if (z_verbose>0) fprintf x ;}
  562. # define Tracevv(x) {if (z_verbose>1) fprintf x ;}
  563. # define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
  564. # define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
  565. #else
  566. # define Assert(cond,msg)
  567. # define Trace(x)
  568. # define Tracev(x)
  569. # define Tracevv(x)
  570. # define Tracec(c,x)
  571. # define Tracecv(c,x)
  572. #endif
  573. typedef uLong (*check_func) (uLong check, const Byte *buf, uInt len);
  574. voidpf zcalloc (voidpf opaque, unsigned items, unsigned size);
  575. void zcfree (voidpf opaque, voidpf ptr);
  576. #define ZALLOC(strm, items, size) \
  577. (*((strm)->zalloc))((strm)->opaque, (items), (size))
  578. #define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
  579. //void ZFREE(z_streamp strm,voidpf addr)
  580. //{ *((strm)->zfree))((strm)->opaque, addr);
  581. //}
  582. #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
  583. // Huffman code lookup table entry--this entry is four bytes for machines
  584. // that have 16-bit pointers (e.g. PC's in the small or medium model).
  585. typedef struct inflate_huft_s inflate_huft;
  586. struct inflate_huft_s {
  587. union {
  588. struct {
  589. Byte Exop; // number of extra bits or operation
  590. Byte Bits; // number of bits in this code or subcode
  591. } what;
  592. uInt pad; // pad structure to a power of 2 (4 bytes for
  593. } word; // 16-bit, 8 bytes for 32-bit int's)
  594. uInt base; // literal, length base, distance base, or table offset
  595. };
  596. // Maximum size of dynamic tree. The maximum found in a long but non-
  597. // exhaustive search was 1004 huft structures (850 for length/literals
  598. // and 154 for distances, the latter actually the result of an
  599. // exhaustive search). The actual maximum is not known, but the
  600. // value below is more than safe.
  601. #define MANY 1440
  602. int inflate_trees_bits (
  603. uInt *, // 19 code lengths
  604. uInt *, // bits tree desired/actual depth
  605. inflate_huft * *, // bits tree result
  606. inflate_huft *, // space for trees
  607. z_streamp); // for messages
  608. int inflate_trees_dynamic (
  609. uInt, // number of literal/length codes
  610. uInt, // number of distance codes
  611. uInt *, // that many (total) code lengths
  612. uInt *, // literal desired/actual bit depth
  613. uInt *, // distance desired/actual bit depth
  614. inflate_huft * *, // literal/length tree result
  615. inflate_huft * *, // distance tree result
  616. inflate_huft *, // space for trees
  617. z_streamp); // for messages
  618. int inflate_trees_fixed (
  619. uInt *, // literal desired/actual bit depth
  620. uInt *, // distance desired/actual bit depth
  621. const inflate_huft * *, // literal/length tree result
  622. const inflate_huft * *, // distance tree result
  623. z_streamp); // for memory allocation
  624. struct inflate_blocks_state;
  625. typedef struct inflate_blocks_state inflate_blocks_statef;
  626. inflate_blocks_statef * inflate_blocks_new (
  627. z_streamp z,
  628. check_func c, // check function
  629. uInt w); // window size
  630. int inflate_blocks (
  631. inflate_blocks_statef *,
  632. z_streamp ,
  633. int); // initial return code
  634. void inflate_blocks_reset (
  635. inflate_blocks_statef *,
  636. z_streamp ,
  637. uLong *); // check value on output
  638. int inflate_blocks_free (
  639. inflate_blocks_statef *,
  640. z_streamp);
  641. void inflate_set_dictionary (
  642. inflate_blocks_statef *s,
  643. const Byte *d, // dictionary
  644. uInt n); // dictionary length
  645. int inflate_blocks_sync_point (
  646. inflate_blocks_statef *s);
  647. struct inflate_codes_state;
  648. typedef struct inflate_codes_state inflate_codes_statef;
  649. inflate_codes_statef *inflate_codes_new (
  650. uInt, uInt,
  651. const inflate_huft *, const inflate_huft *,
  652. z_streamp );
  653. int inflate_codes (
  654. inflate_blocks_statef *,
  655. z_streamp ,
  656. int);
  657. void inflate_codes_free (
  658. inflate_codes_statef *,
  659. z_streamp );
  660. typedef enum {
  661. IBM_TYPE, // get type bits (3, including end bit)
  662. IBM_LENS, // get lengths for stored
  663. IBM_STORED, // processing stored block
  664. IBM_TABLE, // get table lengths
  665. IBM_BTREE, // get bit lengths tree for a dynamic block
  666. IBM_DTREE, // get length, distance trees for a dynamic block
  667. IBM_CODES, // processing fixed or dynamic block
  668. IBM_DRY, // output remaining window bytes
  669. IBM_DONE, // finished last block, done
  670. IBM_BAD} // got a data error--stuck here
  671. inflate_block_mode;
  672. // inflate blocks semi-private state
  673. struct inflate_blocks_state {
  674. // mode
  675. inflate_block_mode mode; // current inflate_block mode
  676. // mode dependent information
  677. union {
  678. uInt left; // if STORED, bytes left to copy
  679. struct {
  680. uInt table; // table lengths (14 bits)
  681. uInt index; // index into blens (or border)
  682. uInt *blens; // bit lengths of codes
  683. uInt bb; // bit length tree depth
  684. inflate_huft *tb; // bit length decoding tree
  685. } trees; // if DTREE, decoding info for trees
  686. struct {
  687. inflate_codes_statef
  688. *codes;
  689. } decode; // if CODES, current state
  690. } sub; // submode
  691. uInt last; // true if this block is the last block
  692. // mode independent information
  693. uInt bitk; // bits in bit buffer
  694. uLong bitb; // bit buffer
  695. inflate_huft *hufts; // single malloc for tree space
  696. Byte *window; // sliding window
  697. Byte *end; // one byte after sliding window
  698. Byte *read; // window read pointer
  699. Byte *write; // window write pointer
  700. check_func checkfn; // check function
  701. uLong check; // check on output
  702. };
  703. // defines for inflate input/output
  704. // update pointers and return
  705. #define UPDBITS {s->bitb=b;s->bitk=k;}
  706. #define UPDIN {z->avail_in=n;z->total_in+=(uLong)(p-z->next_in);z->next_in=p;}
  707. #define UPDOUT {s->write=q;}
  708. #define UPDATE {UPDBITS UPDIN UPDOUT}
  709. #define LEAVE {UPDATE return inflate_flush(s,z,r);}
  710. // get bytes and bits
  711. #define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
  712. #define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
  713. #define NEXTBYTE (n--,*p++)
  714. #define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
  715. #define DUMPBITS(j) {b>>=(j);k-=(j);}
  716. // output bytes
  717. #define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
  718. #define LOADOUT {q=s->write;m=(uInt)WAVAIL;m;}
  719. #define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}
  720. #define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
  721. #define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}
  722. #define OUTBYTE(a) {*q++=(Byte)(a);m--;}
  723. // load local pointers
  724. #define LOAD {LOADIN LOADOUT}
  725. // masks for lower bits (size given to avoid silly warnings with Visual C++)
  726. // And'ing with mask[n] masks the lower n bits
  727. const uInt inflate_mask[17] = {
  728. 0x0000,
  729. 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
  730. 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
  731. };
  732. // copy as much as possible from the sliding window to the output area
  733. int inflate_flush (inflate_blocks_statef *, z_streamp, int);
  734. int inflate_fast (uInt, uInt, const inflate_huft *, const inflate_huft *, inflate_blocks_statef *, z_streamp );
  735. const uInt fixed_bl = 9;
  736. const uInt fixed_bd = 5;
  737. const inflate_huft fixed_tl[] = {
  738. {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
  739. {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},192},
  740. {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},160},
  741. {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},224},
  742. {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},144},
  743. {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},208},
  744. {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},176},
  745. {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},240},
  746. {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
  747. {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},200},
  748. {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},168},
  749. {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},232},
  750. {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},152},
  751. {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},216},
  752. {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},184},
  753. {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},248},
  754. {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
  755. {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},196},
  756. {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},164},
  757. {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},228},
  758. {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},148},
  759. {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},212},
  760. {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},180},
  761. {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},244},
  762. {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
  763. {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},204},
  764. {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},172},
  765. {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},236},
  766. {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},156},
  767. {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},220},
  768. {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},188},
  769. {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},252},
  770. {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
  771. {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},194},
  772. {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},162},
  773. {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},226},
  774. {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},146},
  775. {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},210},
  776. {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},178},
  777. {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},242},
  778. {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
  779. {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},202},
  780. {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},170},
  781. {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},234},
  782. {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},154},
  783. {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},218},
  784. {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},186},
  785. {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},250},
  786. {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
  787. {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},198},
  788. {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},166},
  789. {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},230},
  790. {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},150},
  791. {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},214},
  792. {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},182},
  793. {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},246},
  794. {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
  795. {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},206},
  796. {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},174},
  797. {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},238},
  798. {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},158},
  799. {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},222},
  800. {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},190},
  801. {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},254},
  802. {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
  803. {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},193},
  804. {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},161},
  805. {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},225},
  806. {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},145},
  807. {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},209},
  808. {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},177},
  809. {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},241},
  810. {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
  811. {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},201},
  812. {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},169},
  813. {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},233},
  814. {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},153},
  815. {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},217},
  816. {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},185},
  817. {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},249},
  818. {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
  819. {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},197},
  820. {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},165},
  821. {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},229},
  822. {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},149},
  823. {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},213},
  824. {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},181},
  825. {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},245},
  826. {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
  827. {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},205},
  828. {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},173},
  829. {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},237},
  830. {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},157},
  831. {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},221},
  832. {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},189},
  833. {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},253},
  834. {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
  835. {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},195},
  836. {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},163},
  837. {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},227},
  838. {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},147},
  839. {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},211},
  840. {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},179},
  841. {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},243},
  842. {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
  843. {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},203},
  844. {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},171},
  845. {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},235},
  846. {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},155},
  847. {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},219},
  848. {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},187},
  849. {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},251},
  850. {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
  851. {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},199},
  852. {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},167},
  853. {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},231},
  854. {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},151},
  855. {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},215},
  856. {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},183},
  857. {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},247},
  858. {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
  859. {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},207},
  860. {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},175},
  861. {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},239},
  862. {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},159},
  863. {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},223},
  864. {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},191},
  865. {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},255}
  866. };
  867. const inflate_huft fixed_td[] = {
  868. {{{80,5}},1}, {{{87,5}},257}, {{{83,5}},17}, {{{91,5}},4097},
  869. {{{81,5}},5}, {{{89,5}},1025}, {{{85,5}},65}, {{{93,5}},16385},
  870. {{{80,5}},3}, {{{88,5}},513}, {{{84,5}},33}, {{{92,5}},8193},
  871. {{{82,5}},9}, {{{90,5}},2049}, {{{86,5}},129}, {{{192,5}},24577},
  872. {{{80,5}},2}, {{{87,5}},385}, {{{83,5}},25}, {{{91,5}},6145},
  873. {{{81,5}},7}, {{{89,5}},1537}, {{{85,5}},97}, {{{93,5}},24577},
  874. {{{80,5}},4}, {{{88,5}},769}, {{{84,5}},49}, {{{92,5}},12289},
  875. {{{82,5}},13}, {{{90,5}},3073}, {{{86,5}},193}, {{{192,5}},24577}
  876. };
  877. // copy as much as possible from the sliding window to the output area
  878. int inflate_flush(inflate_blocks_statef *s,z_streamp z,int r)
  879. {
  880. uInt n;
  881. Byte *p;
  882. Byte *q;
  883. // local copies of source and destination pointers
  884. p = z->next_out;
  885. q = s->read;
  886. // compute number of bytes to copy as far as end of window
  887. n = (uInt)((q <= s->write ? s->write : s->end) - q);
  888. if (n > z->avail_out) n = z->avail_out;
  889. if (n && r == Z_BUF_ERROR) r = Z_OK;
  890. // update counters
  891. z->avail_out -= n;
  892. z->total_out += n;
  893. // update check information
  894. if (s->checkfn != Z_NULL)
  895. z->adler = s->check = (*s->checkfn)(s->check, q, n);
  896. // copy as far as end of window
  897. if (n!=0) // check for n!=0 to avoid waking up CodeGuard
  898. { memcpy(p, q, n);
  899. p += n;
  900. q += n;
  901. }
  902. // see if more to copy at beginning of window
  903. if (q == s->end)
  904. {
  905. // wrap pointers
  906. q = s->window;
  907. if (s->write == s->end)
  908. s->write = s->window;
  909. // compute bytes to copy
  910. n = (uInt)(s->write - q);
  911. if (n > z->avail_out) n = z->avail_out;
  912. if (n && r == Z_BUF_ERROR) r = Z_OK;
  913. // update counters
  914. z->avail_out -= n;
  915. z->total_out += n;
  916. // update check information
  917. if (s->checkfn != Z_NULL)
  918. z->adler = s->check = (*s->checkfn)(s->check, q, n);
  919. // copy
  920. memcpy(p, q, n);
  921. p += n;
  922. q += n;
  923. }
  924. // update pointers
  925. z->next_out = p;
  926. s->read = q;
  927. // done
  928. return r;
  929. }
  930. // simplify the use of the inflate_huft type with some defines
  931. #define exop word.what.Exop
  932. #define bits word.what.Bits
  933. typedef enum { // waiting for "i:"=input, "o:"=output, "x:"=nothing
  934. START, // x: set up for LEN
  935. LEN, // i: get length/literal/eob next
  936. LENEXT, // i: getting length extra (have base)
  937. DIST, // i: get distance next
  938. DISTEXT, // i: getting distance extra
  939. COPY, // o: copying bytes in window, waiting for space
  940. LIT, // o: got literal, waiting for output space
  941. WASH, // o: got eob, possibly still output waiting
  942. END, // x: got eob and all data flushed
  943. BADCODE} // x: got error
  944. inflate_codes_mode;
  945. // inflate codes private state
  946. struct inflate_codes_state {
  947. // mode
  948. inflate_codes_mode mode; // current inflate_codes mode
  949. // mode dependent information
  950. uInt len;
  951. union {
  952. struct {
  953. const inflate_huft *tree; // pointer into tree
  954. uInt need; // bits needed
  955. } code; // if LEN or DIST, where in tree
  956. uInt lit; // if LIT, literal
  957. struct {
  958. uInt get; // bits to get for extra
  959. uInt dist; // distance back to copy from
  960. } copy; // if EXT or COPY, where and how much
  961. } sub; // submode
  962. // mode independent information
  963. Byte lbits; // ltree bits decoded per branch
  964. Byte dbits; // dtree bits decoder per branch
  965. const inflate_huft *ltree; // literal/length/eob tree
  966. const inflate_huft *dtree; // distance tree
  967. };
  968. inflate_codes_statef *inflate_codes_new(
  969. uInt bl, uInt bd,
  970. const inflate_huft *tl,
  971. const inflate_huft *td, // need separate declaration for Borland C++
  972. z_streamp z)
  973. {
  974. inflate_codes_statef *c;
  975. if ((c = (inflate_codes_statef *)
  976. ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
  977. {
  978. c->mode = START;
  979. c->lbits = (Byte)bl;
  980. c->dbits = (Byte)bd;
  981. c->ltree = tl;
  982. c->dtree = td;
  983. Tracev((stderr, "inflate: codes new\n"));
  984. }
  985. return c;
  986. }
  987. int inflate_codes(inflate_blocks_statef *s, z_streamp z, int r)
  988. {
  989. uInt j; // temporary storage
  990. const inflate_huft *t; // temporary pointer
  991. uInt e; // extra bits or operation
  992. uLong b; // bit buffer
  993. uInt k; // bits in bit buffer
  994. Byte *p; // input data pointer
  995. uInt n; // bytes available there
  996. Byte *q; // output window write pointer
  997. uInt m; // bytes to end of window or read pointer
  998. Byte *f; // pointer to copy strings from
  999. inflate_codes_statef *c = s->sub.decode.codes; // codes state
  1000. // copy input/output information to locals (UPDATE macro restores)
  1001. LOAD
  1002. // process input and output based on current state
  1003. for(;;) switch (c->mode)
  1004. { // waiting for "i:"=input, "o:"=output, "x:"=nothing
  1005. case START: // x: set up for LEN
  1006. #ifndef SLOW
  1007. if (m >= 258 && n >= 10)
  1008. {
  1009. UPDATE
  1010. r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
  1011. LOAD
  1012. if (r != Z_OK)
  1013. {
  1014. c->mode = r == Z_STREAM_END ? WASH : BADCODE;
  1015. break;
  1016. }
  1017. }
  1018. #endif // !SLOW
  1019. c->sub.code.need = c->lbits;
  1020. c->sub.code.tree = c->ltree;
  1021. c->mode = LEN;
  1022. case LEN: // i: get length/literal/eob next
  1023. j = c->sub.code.need;
  1024. NEEDBITS(j)
  1025. t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
  1026. DUMPBITS(t->bits)
  1027. e = (uInt)(t->exop);
  1028. if (e == 0) // literal
  1029. {
  1030. c->sub.lit = t->base;
  1031. Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
  1032. "inflate: literal '%c'\n" :
  1033. "inflate: literal 0x%02x\n", t->base));
  1034. c->mode = LIT;
  1035. break;
  1036. }
  1037. if (e & 16) // length
  1038. {
  1039. c->sub.copy.get = e & 15;
  1040. c->len = t->base;
  1041. c->mode = LENEXT;
  1042. break;
  1043. }
  1044. if ((e & 64) == 0) // next table
  1045. {
  1046. c->sub.code.need = e;
  1047. c->sub.code.tree = t + t->base;
  1048. break;
  1049. }
  1050. if (e & 32) // end of block
  1051. {
  1052. Tracevv((stderr, "inflate: end of block\n"));
  1053. c->mode = WASH;
  1054. break;
  1055. }
  1056. c->mode = BADCODE; // invalid code
  1057. z->msg = (char*)"invalid literal/length code";
  1058. r = Z_DATA_ERROR;
  1059. LEAVE
  1060. case LENEXT: // i: getting length extra (have base)
  1061. j = c->sub.copy.get;
  1062. NEEDBITS(j)
  1063. c->len += (uInt)b & inflate_mask[j];
  1064. DUMPBITS(j)
  1065. c->sub.code.need = c->dbits;
  1066. c->sub.code.tree = c->dtree;
  1067. Tracevv((stderr, "inflate: length %u\n", c->len));
  1068. c->mode = DIST;
  1069. case DIST: // i: get distance next
  1070. j = c->sub.code.need;
  1071. NEEDBITS(j)
  1072. t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
  1073. DUMPBITS(t->bits)
  1074. e = (uInt)(t->exop);
  1075. if (e & 16) // distance
  1076. {
  1077. c->sub.copy.get = e & 15;
  1078. c->sub.copy.dist = t->base;
  1079. c->mode = DISTEXT;
  1080. break;
  1081. }
  1082. if ((e & 64) == 0) // next table
  1083. {
  1084. c->sub.code.need = e;
  1085. c->sub.code.tree = t + t->base;
  1086. break;
  1087. }
  1088. c->mode = BADCODE; // invalid code
  1089. z->msg = (char*)"invalid distance code";
  1090. r = Z_DATA_ERROR;
  1091. LEAVE
  1092. case DISTEXT: // i: getting distance extra
  1093. j = c->sub.copy.get;
  1094. NEEDBITS(j)
  1095. c->sub.copy.dist += (uInt)b & inflate_mask[j];
  1096. DUMPBITS(j)
  1097. Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist));
  1098. c->mode = COPY;
  1099. case COPY: // o: copying bytes in window, waiting for space
  1100. f = (uInt)(q - s->window) < c->sub.copy.dist ?
  1101. s->end - (c->sub.copy.dist - (q - s->window)) :
  1102. q - c->sub.copy.dist;
  1103. while (c->len)
  1104. {
  1105. NEEDOUT
  1106. OUTBYTE(*f++)
  1107. if (f == s->end)
  1108. f = s->window;
  1109. c->len--;
  1110. }
  1111. c->mode = START;
  1112. break;
  1113. case LIT: // o: got literal, waiting for output space
  1114. NEEDOUT
  1115. OUTBYTE(c->sub.lit)
  1116. c->mode = START;
  1117. break;
  1118. case WASH: // o: got eob, possibly more output
  1119. if (k > 7) // return unused byte, if any
  1120. {
  1121. Assert(k < 16, "inflate_codes grabbed too many bytes")
  1122. k -= 8;
  1123. n++;
  1124. p--; // can always return one
  1125. }
  1126. FLUSH
  1127. if (s->read != s->write)
  1128. LEAVE
  1129. c->mode = END;
  1130. case END:
  1131. r = Z_STREAM_END;
  1132. LEAVE
  1133. case BADCODE: // x: got error
  1134. r = Z_DATA_ERROR;
  1135. LEAVE
  1136. default:
  1137. r = Z_STREAM_ERROR;
  1138. LEAVE
  1139. }
  1140. }
  1141. void inflate_codes_free(inflate_codes_statef *c,z_streamp z)
  1142. { ZFREE(z, c);
  1143. Tracev((stderr, "inflate: codes free\n"));
  1144. }
  1145. // infblock.c -- interpret and process block types to last block
  1146. // Copyright (C) 1995-1998 Mark Adler
  1147. // For conditions of distribution and use, see copyright notice in zlib.h
  1148. //struct inflate_codes_state {int dummy;}; // for buggy compilers
  1149. // Table for deflate from PKZIP's appnote.txt.
  1150. const uInt border[] = { // Order of the bit length code lengths
  1151. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  1152. //
  1153. // Notes beyond the 1.93a appnote.txt:
  1154. //
  1155. // 1. Distance pointers never point before the beginning of the output stream.
  1156. // 2. Distance pointers can point back across blocks, up to 32k away.
  1157. // 3. There is an implied maximum of 7 bits for the bit length table and
  1158. // 15 bits for the actual data.
  1159. // 4. If only one code exists, then it is encoded using one bit. (Zero
  1160. // would be more efficient, but perhaps a little confusing.) If two
  1161. // codes exist, they are coded using one bit each (0 and 1).
  1162. // 5. There is no way of sending zero distance codes--a dummy must be
  1163. // sent if there are none. (History: a pre 2.0 version of PKZIP would
  1164. // store blocks with no distance codes, but this was discovered to be
  1165. // too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
  1166. // zero distance codes, which is sent as one code of zero bits in
  1167. // length.
  1168. // 6. There are up to 286 literal/length codes. Code 256 represents the
  1169. // end-of-block. Note however that the static length tree defines
  1170. // 288 codes just to fill out the Huffman codes. Codes 286 and 287
  1171. // cannot be used though, since there is no length base or extra bits
  1172. // defined for them. Similarily, there are up to 30 distance codes.
  1173. // However, static trees define 32 codes (all 5 bits) to fill out the
  1174. // Huffman codes, but the last two had better not show up in the data.
  1175. // 7. Unzip can check dynamic Huffman blocks for complete code sets.
  1176. // The exception is that a single code would not be complete (see #4).
  1177. // 8. The five bits following the block type is really the number of
  1178. // literal codes sent minus 257.
  1179. // 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
  1180. // (1+6+6). Therefore, to output three times the length, you output
  1181. // three codes (1+1+1), whereas to output four times the same length,
  1182. // you only need two codes (1+3). Hmm.
  1183. //10. In the tree reconstruction algorithm, Code = Code + Increment
  1184. // only if BitLength(i) is not zero. (Pretty obvious.)
  1185. //11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
  1186. //12. Note: length code 284 can represent 227-258, but length code 285
  1187. // really is 258. The last length deserves its own, short code
  1188. // since it gets used a lot in very redundant files. The length
  1189. // 258 is special since 258 - 3 (the min match length) is 255.
  1190. //13. The literal/length and distance code bit lengths are read as a
  1191. // single stream of lengths. It is possible (and advantageous) for
  1192. // a repeat code (16, 17, or 18) to go across the boundary between
  1193. // the two sets of lengths.
  1194. void inflate_blocks_reset(inflate_blocks_statef *s, z_streamp z, uLong *c)
  1195. {
  1196. if (c != Z_NULL)
  1197. *c = s->check;
  1198. if (s->mode == IBM_BTREE || s->mode == IBM_DTREE)
  1199. ZFREE(z, s->sub.trees.blens);
  1200. if (s->mode == IBM_CODES)
  1201. inflate_codes_free(s->sub.decode.codes, z);
  1202. s->mode = IBM_TYPE;
  1203. s->bitk = 0;
  1204. s->bitb = 0;
  1205. s->read = s->write = s->window;
  1206. if (s->checkfn != Z_NULL)
  1207. z->adler = s->check = (*s->checkfn)(0L, (const Byte *)Z_NULL, 0);
  1208. Tracev((stderr, "inflate: blocks reset\n"));
  1209. }
  1210. inflate_blocks_statef *inflate_blocks_new(z_streamp z, check_func c, uInt w)
  1211. {
  1212. inflate_blocks_statef *s;
  1213. if ((s = (inflate_blocks_statef *)ZALLOC
  1214. (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
  1215. return s;
  1216. if ((s->hufts =
  1217. (inflate_huft *)ZALLOC(z, sizeof(inflate_huft), MANY)) == Z_NULL)
  1218. {
  1219. ZFREE(z, s);
  1220. return Z_NULL;
  1221. }
  1222. if ((s->window = (Byte *)ZALLOC(z, 1, w)) == Z_NULL)
  1223. {
  1224. ZFREE(z, s->hufts);
  1225. ZFREE(z, s);
  1226. return Z_NULL;
  1227. }
  1228. s->end = s->window + w;
  1229. s->checkfn = c;
  1230. s->mode = IBM_TYPE;
  1231. Tracev((stderr, "inflate: blocks allocated\n"));
  1232. inflate_blocks_reset(s, z, Z_NULL);
  1233. return s;
  1234. }
  1235. int inflate_blocks(inflate_blocks_statef *s, z_streamp z, int r)
  1236. {
  1237. uInt t; // temporary storage
  1238. uLong b; // bit buffer
  1239. uInt k; // bits in bit buffer
  1240. Byte *p; // input data pointer
  1241. uInt n; // bytes available there
  1242. Byte *q; // output window write pointer
  1243. uInt m; // bytes to end of window or read pointer
  1244. // copy input/output information to locals (UPDATE macro restores)
  1245. LOAD
  1246. // process input based on current state
  1247. for(;;) switch (s->mode)
  1248. {
  1249. case IBM_TYPE:
  1250. NEEDBITS(3)
  1251. t = (uInt)b & 7;
  1252. s->last = t & 1;
  1253. switch (t >> 1)
  1254. {
  1255. case 0: // stored
  1256. Tracev((stderr, "inflate: stored block%s\n",
  1257. s->last ? " (last)" : ""));
  1258. DUMPBITS(3)
  1259. t = k & 7; // go to byte boundary
  1260. DUMPBITS(t)
  1261. s->mode = IBM_LENS; // get length of stored block
  1262. break;
  1263. case 1: // fixed
  1264. Tracev((stderr, "inflate: fixed codes block%s\n",
  1265. s->last ? " (last)" : ""));
  1266. {
  1267. uInt bl, bd;
  1268. const inflate_huft *tl, *td;
  1269. inflate_trees_fixed(&bl, &bd, &tl, &td, z);
  1270. s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
  1271. if (s->sub.decode.codes == Z_NULL)
  1272. {
  1273. r = Z_MEM_ERROR;
  1274. LEAVE
  1275. }
  1276. }
  1277. DUMPBITS(3)
  1278. s->mode = IBM_CODES;
  1279. break;
  1280. case 2: // dynamic
  1281. Tracev((stderr, "inflate: dynamic codes block%s\n",
  1282. s->last ? " (last)" : ""));
  1283. DUMPBITS(3)
  1284. s->mode = IBM_TABLE;
  1285. break;
  1286. case 3: // illegal
  1287. DUMPBITS(3)
  1288. s->mode = IBM_BAD;
  1289. z->msg = (char*)"invalid block type";
  1290. r = Z_DATA_ERROR;
  1291. LEAVE
  1292. }
  1293. break;
  1294. case IBM_LENS:
  1295. NEEDBITS(32)
  1296. if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
  1297. {
  1298. s->mode = IBM_BAD;
  1299. z->msg = (char*)"invalid stored block lengths";
  1300. r = Z_DATA_ERROR;
  1301. LEAVE
  1302. }
  1303. s->sub.left = (uInt)b & 0xffff;
  1304. b = k = 0; // dump bits
  1305. Tracev((stderr, "inflate: stored length %u\n", s->sub.left));
  1306. s->mode = s->sub.left ? IBM_STORED : (s->last ? IBM_DRY : IBM_TYPE);
  1307. break;
  1308. case IBM_STORED:
  1309. if (n == 0)
  1310. LEAVE
  1311. NEEDOUT
  1312. t = s->sub.left;
  1313. if (t > n) t = n;
  1314. if (t > m) t = m;
  1315. memcpy(q, p, t);
  1316. p += t; n -= t;
  1317. q += t; m -= t;
  1318. if ((s->sub.left -= t) != 0)
  1319. break;
  1320. Tracev((stderr, "inflate: stored end, %lu total out\n",
  1321. z->total_out + (q >= s->read ? q - s->read :
  1322. (s->end - s->read) + (q - s->window))));
  1323. s->mode = s->last ? IBM_DRY : IBM_TYPE;
  1324. break;
  1325. case IBM_TABLE:
  1326. NEEDBITS(14)
  1327. s->sub.trees.table = t = (uInt)b & 0x3fff;
  1328. // remove this section to workaround bug in pkzip
  1329. if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
  1330. {
  1331. s->mode = IBM_BAD;
  1332. z->msg = (char*)"too many length or distance symbols";
  1333. r = Z_DATA_ERROR;
  1334. LEAVE
  1335. }
  1336. // end remove
  1337. t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
  1338. if ((s->sub.trees.blens = (uInt*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
  1339. {
  1340. r = Z_MEM_ERROR;
  1341. LEAVE
  1342. }
  1343. DUMPBITS(14)
  1344. s->sub.trees.index = 0;
  1345. Tracev((stderr, "inflate: table sizes ok\n"));
  1346. s->mode = IBM_BTREE;
  1347. case IBM_BTREE:
  1348. while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
  1349. {
  1350. NEEDBITS(3)
  1351. s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
  1352. DUMPBITS(3)
  1353. }
  1354. while (s->sub.trees.index < 19)
  1355. s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
  1356. s->sub.trees.bb = 7;
  1357. t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
  1358. &s->sub.trees.tb, s->hufts, z);
  1359. if (t != Z_OK)
  1360. {
  1361. ZFREE(z, s->sub.trees.blens);
  1362. r = t;
  1363. if (r == Z_DATA_ERROR)
  1364. s->mode = IBM_BAD;
  1365. LEAVE
  1366. }
  1367. s->sub.trees.index = 0;
  1368. Tracev((stderr, "inflate: bits tree ok\n"));
  1369. s->mode = IBM_DTREE;
  1370. case IBM_DTREE:
  1371. while (t = s->sub.trees.table,
  1372. s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
  1373. {
  1374. inflate_huft *h;
  1375. uInt i, j, c;
  1376. t = s->sub.trees.bb;
  1377. NEEDBITS(t)
  1378. h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
  1379. t = h->bits;
  1380. c = h->base;
  1381. if (c < 16)
  1382. {
  1383. DUMPBITS(t)
  1384. s->sub.trees.blens[s->sub.trees.index++] = c;
  1385. }
  1386. else // c == 16..18
  1387. {
  1388. i = c == 18 ? 7 : c - 14;
  1389. j = c == 18 ? 11 : 3;
  1390. NEEDBITS(t + i)
  1391. DUMPBITS(t)
  1392. j += (uInt)b & inflate_mask[i];
  1393. DUMPBITS(i)
  1394. i = s->sub.trees.index;
  1395. t = s->sub.trees.table;
  1396. if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
  1397. (c == 16 && i < 1))
  1398. {
  1399. ZFREE(z, s->sub.trees.blens);
  1400. s->mode = IBM_BAD;
  1401. z->msg = (char*)"invalid bit length repeat";
  1402. r = Z_DATA_ERROR;
  1403. LEAVE
  1404. }
  1405. c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
  1406. do {
  1407. s->sub.trees.blens[i++] = c;
  1408. } while (--j);
  1409. s->sub.trees.index = i;
  1410. }
  1411. }
  1412. s->sub.trees.tb = Z_NULL;
  1413. {
  1414. uInt bl, bd;
  1415. inflate_huft *tl, *td;
  1416. inflate_codes_statef *c;
  1417. bl = 9; // must be <= 9 for lookahead assumptions
  1418. bd = 6; // must be <= 9 for lookahead assumptions
  1419. t = s->sub.trees.table;
  1420. t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
  1421. s->sub.trees.blens, &bl, &bd, &tl, &td,
  1422. s->hufts, z);
  1423. ZFREE(z, s->sub.trees.blens);
  1424. if (t != Z_OK)
  1425. {
  1426. if (t == (uInt)Z_DATA_ERROR)
  1427. s->mode = IBM_BAD;
  1428. r = t;
  1429. LEAVE
  1430. }
  1431. Tracev((stderr, "inflate: trees ok\n"));
  1432. if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
  1433. {
  1434. r = Z_MEM_ERROR;
  1435. LEAVE
  1436. }
  1437. s->sub.decode.codes = c;
  1438. }
  1439. s->mode = IBM_CODES;
  1440. case IBM_CODES:
  1441. UPDATE
  1442. if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
  1443. return inflate_flush(s, z, r);
  1444. r = Z_OK;
  1445. inflate_codes_free(s->sub.decode.codes, z);
  1446. LOAD
  1447. Tracev((stderr, "inflate: codes end, %lu total out\n",
  1448. z->total_out + (q >= s->read ? q - s->read :
  1449. (s->end - s->read) + (q - s->window))));
  1450. if (!s->last)
  1451. {
  1452. s->mode = IBM_TYPE;
  1453. break;
  1454. }
  1455. s->mode = IBM_DRY;
  1456. case IBM_DRY:
  1457. FLUSH
  1458. if (s->read != s->write)
  1459. LEAVE
  1460. s->mode = IBM_DONE;
  1461. case IBM_DONE:
  1462. r = Z_STREAM_END;
  1463. LEAVE
  1464. case IBM_BAD:
  1465. r = Z_DATA_ERROR;
  1466. LEAVE
  1467. default:
  1468. r = Z_STREAM_ERROR;
  1469. LEAVE
  1470. }
  1471. }
  1472. int inflate_blocks_free(inflate_blocks_statef *s, z_streamp z)
  1473. {
  1474. inflate_blocks_reset(s, z, Z_NULL);
  1475. ZFREE(z, s->window);
  1476. ZFREE(z, s->hufts);
  1477. ZFREE(z, s);
  1478. Tracev((stderr, "inflate: blocks freed\n"));
  1479. return Z_OK;
  1480. }
  1481. // inftrees.c -- generate Huffman trees for efficient decoding
  1482. // Copyright (C) 1995-1998 Mark Adler
  1483. // For conditions of distribution and use, see copyright notice in zlib.h
  1484. //
  1485. extern const char inflate_copyright_XUnzip[] =
  1486. " inflate 1.1.3 Copyright 1995-1998 Mark Adler ";
  1487. // If you use the zlib library in a product, an acknowledgment is welcome
  1488. // in the documentation of your product. If for some reason you cannot
  1489. // include such an acknowledgment, I would appreciate that you keep this
  1490. // copyright string in the executable of your product.
  1491. int huft_build (
  1492. uInt *, // code lengths in bits
  1493. uInt, // number of codes
  1494. uInt, // number of "simple" codes
  1495. const uInt *, // list of base values for non-simple codes
  1496. const uInt *, // list of extra bits for non-simple codes
  1497. inflate_huft **,// result: starting table
  1498. uInt *, // maximum lookup bits (returns actual)
  1499. inflate_huft *, // space for trees
  1500. uInt *, // hufts used in space
  1501. uInt * ); // space for values
  1502. // Tables for deflate from PKZIP's appnote.txt.
  1503. const uInt cplens[31] = { // Copy lengths for literal codes 257..285
  1504. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  1505. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
  1506. // see note #13 above about 258
  1507. const uInt cplext[31] = { // Extra bits for literal codes 257..285
  1508. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  1509. 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; // 112==invalid
  1510. const uInt cpdist[30] = { // Copy offsets for distance codes 0..29
  1511. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  1512. 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  1513. 8193, 12289, 16385, 24577};
  1514. const uInt cpdext[30] = { // Extra bits for distance codes
  1515. 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  1516. 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  1517. 12, 12, 13, 13};
  1518. //
  1519. // Huffman code decoding is performed using a multi-level table lookup.
  1520. // The fastest way to decode is to simply build a lookup table whose
  1521. // size is determined by the longest code. However, the time it takes
  1522. // to build this table can also be a factor if the data being decoded
  1523. // is not very long. The most common codes are necessarily the
  1524. // shortest codes, so those codes dominate the decoding time, and hence
  1525. // the speed. The idea is you can have a shorter table that decodes the
  1526. // shorter, more probable codes, and then point to subsidiary tables for
  1527. // the longer codes. The time it costs to decode the longer codes is
  1528. // then traded against the time it takes to make longer tables.
  1529. //
  1530. // This results of this trade are in the variables lbits and dbits
  1531. // below. lbits is the number of bits the first level table for literal/
  1532. // length codes can decode in one step, and dbits is the same thing for
  1533. // the distance codes. Subsequent tables are also less than or equal to
  1534. // those sizes. These values may be adjusted either when all of the
  1535. // codes are shorter than that, in which case the longest code length in
  1536. // bits is used, or when the shortest code is *longer* than the requested
  1537. // table size, in which case the length of the shortest code in bits is
  1538. // used.
  1539. //
  1540. // There are two different values for the two tables, since they code a
  1541. // different number of possibilities each. The literal/length table
  1542. // codes 286 possible values, or in a flat code, a little over eight
  1543. // bits. The distance table codes 30 possible values, or a little less
  1544. // than five bits, flat. The optimum values for speed end up being
  1545. // about one bit more than those, so lbits is 8+1 and dbits is 5+1.
  1546. // The optimum values may differ though from machine to machine, and
  1547. // possibly even between compilers. Your mileage may vary.
  1548. //
  1549. // If BMAX needs to be larger than 16, then h and x[] should be uLong.
  1550. #define BMAX 15 // maximum bit length of any code
  1551. int huft_build(
  1552. uInt *b, // code lengths in bits (all assumed <= BMAX)
  1553. uInt n, // number of codes (assumed <= 288)
  1554. uInt s, // number of simple-valued codes (0..s-1)
  1555. const uInt *d, // list of base values for non-simple codes
  1556. const uInt *e, // list of extra bits for non-simple codes
  1557. inflate_huft * *t, // result: starting table
  1558. uInt *m, // maximum lookup bits, returns actual
  1559. inflate_huft *hp, // space for trees
  1560. uInt *hn, // hufts used in space
  1561. uInt *v) // working area: values in order of bit length
  1562. // Given a list of code lengths and a maximum table size, make a set of
  1563. // tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
  1564. // if the given code set is incomplete (the tables are still built in this
  1565. // case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
  1566. // lengths), or Z_MEM_ERROR if not enough memory.
  1567. {
  1568. uInt a; // counter for codes of length k
  1569. uInt c[BMAX+1]; // bit length count table
  1570. uInt f; // i repeats in table every f entries
  1571. int g; // maximum code length
  1572. int h; // table level
  1573. uInt i; // counter, current code
  1574. uInt j; // counter
  1575. int k; // number of bits in current code
  1576. int l; // bits per table (returned in m)
  1577. uInt mask; // (1 << w) - 1, to avoid cc -O bug on HP
  1578. uInt *p; // pointer into c[], b[], or v[]
  1579. inflate_huft *q; // points to current table
  1580. struct inflate_huft_s r; // table entry for structure assignment
  1581. inflate_huft *u[BMAX]; // table stack
  1582. int w; // bits before this table == (l * h)
  1583. uInt x[BMAX+1]; // bit offsets, then code stack
  1584. uInt *xp; // pointer into x
  1585. int y; // number of dummy codes added
  1586. uInt z; // number of entries in current table
  1587. // Generate counts for each bit length
  1588. p = c;
  1589. #define C0 *p++ = 0;
  1590. #define C2 C0 C0 C0 C0
  1591. #define C4 C2 C2 C2 C2
  1592. C4; p; // clear c[]--assume BMAX+1 is 16
  1593. p = b; i = n;
  1594. do {
  1595. c[*p++]++; // assume all entries <= BMAX
  1596. } while (--i);
  1597. if (c[0] == n) // null input--all zero length codes
  1598. {
  1599. *t = (inflate_huft *)Z_NULL;
  1600. *m = 0;
  1601. return Z_OK;
  1602. }
  1603. // Find minimum and maximum length, bound *m by those
  1604. l = *m;
  1605. for (j = 1; j <= BMAX; j++)
  1606. if (c[j])
  1607. break;
  1608. k = j; // minimum code length
  1609. if ((uInt)l < j)
  1610. l = j;
  1611. for (i = BMAX; i; i--)
  1612. if (c[i])
  1613. break;
  1614. g = i; // maximum code length
  1615. if ((uInt)l > i)
  1616. l = i;
  1617. *m = l;
  1618. // Adjust last length count to fill out codes, if needed
  1619. for (y = 1 << j; j < i; j++, y <<= 1)
  1620. if ((y -= c[j]) < 0)
  1621. return Z_DATA_ERROR;
  1622. if ((y -= c[i]) < 0)
  1623. return Z_DATA_ERROR;
  1624. c[i] += y;
  1625. // Generate starting offsets into the value table for each length
  1626. x[1] = j = 0;
  1627. p = c + 1; xp = x + 2;
  1628. while (--i) { // note that i == g from above
  1629. *xp++ = (j += *p++);
  1630. }
  1631. // Make a table of values in order of bit lengths
  1632. p = b; i = 0;
  1633. do {
  1634. if ((j = *p++) != 0)
  1635. v[x[j]++] = i;
  1636. } while (++i < n);
  1637. n = x[g]; // set n to length of v
  1638. // Generate the Huffman codes and for each, make the table entries
  1639. x[0] = i = 0; // first Huffman code is zero
  1640. p = v; // grab values in bit order
  1641. h = -1; // no tables yet--level -1
  1642. w = -l; // bits decoded == (l * h)
  1643. u[0] = (inflate_huft *)Z_NULL; // just to keep compilers happy
  1644. q = (inflate_huft *)Z_NULL; // ditto
  1645. z = 0; // ditto
  1646. // go through the bit lengths (k already is bits in shortest code)
  1647. for (; k <= g; k++)
  1648. {
  1649. a = c[k];
  1650. while (a--)
  1651. {
  1652. // here i is the Huffman code of length k bits for value *p
  1653. // make tables up to required level
  1654. while (k > w + l)
  1655. {
  1656. h++;
  1657. w += l; // previous table always l bits
  1658. // compute minimum size table less than or equal to l bits
  1659. z = g - w;
  1660. z = z > (uInt)l ? l : z; // table size upper limit
  1661. if ((f = 1 << (j = k - w)) > a + 1) // try a k-w bit table
  1662. { // too few codes for k-w bit table
  1663. f -= a + 1; // deduct codes from patterns left
  1664. xp = c + k;
  1665. if (j < z)
  1666. while (++j < z) // try smaller tables up to z bits
  1667. {
  1668. if ((f <<= 1) <= *++xp)
  1669. break; // enough codes to use up j bits
  1670. f -= *xp; // else deduct codes from patterns
  1671. }
  1672. }
  1673. z = 1 << j; // table entries for j-bit table
  1674. // allocate new table
  1675. if (*hn + z > MANY) // (note: doesn't matter for fixed)
  1676. return Z_MEM_ERROR; // not enough memory
  1677. u[h] = q = hp + *hn;
  1678. *hn += z;
  1679. // connect to last table, if there is one
  1680. if (h)
  1681. {
  1682. x[h] = i; // save pattern for backing up
  1683. r.bits = (Byte)l; // bits to dump before this table
  1684. r.exop = (Byte)j; // bits in this table
  1685. j = i >> (w - l);
  1686. r.base = (uInt)(q - u[h-1] - j); // offset to this table
  1687. u[h-1][j] = r; // connect to last table
  1688. }
  1689. else
  1690. *t = q; // first table is returned result
  1691. }
  1692. // set up table entry in r
  1693. r.bits = (Byte)(k - w);
  1694. if (p >= v + n)
  1695. r.exop = 128 + 64; // out of values--invalid code
  1696. else if (*p < s)
  1697. {
  1698. r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); // 256 is end-of-block
  1699. r.base = *p++; // simple code is just the value
  1700. }
  1701. else
  1702. {
  1703. r.exop = (Byte)(e[*p - s] + 16 + 64);// non-simple--look up in lists
  1704. r.base = d[*p++ - s];
  1705. }
  1706. // fill code-like entries with r
  1707. f = 1 << (k - w);
  1708. for (j = i >> w; j < z; j += f)
  1709. q[j] = r;
  1710. // backwards increment the k-bit code i
  1711. for (j = 1 << (k - 1); i & j; j >>= 1)
  1712. i ^= j;
  1713. i ^= j;
  1714. // backup over finished tables
  1715. mask = (1 << w) - 1; // needed on HP, cc -O bug
  1716. while ((i & mask) != x[h])
  1717. {
  1718. h--; // don't need to update q
  1719. w -= l;
  1720. mask = (1 << w) - 1;
  1721. }
  1722. }
  1723. }
  1724. // Return Z_BUF_ERROR if we were given an incomplete table
  1725. return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
  1726. }
  1727. int inflate_trees_bits(
  1728. uInt *c, // 19 code lengths
  1729. uInt *bb, // bits tree desired/actual depth
  1730. inflate_huft * *tb, // bits tree result
  1731. inflate_huft *hp, // space for trees
  1732. z_streamp z) // for messages
  1733. {
  1734. int r;
  1735. uInt hn = 0; // hufts used in space
  1736. uInt *v; // work area for huft_build
  1737. if ((v = (uInt*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)
  1738. return Z_MEM_ERROR;
  1739. r = huft_build(c, 19, 19, (uInt*)Z_NULL, (uInt*)Z_NULL,
  1740. tb, bb, hp, &hn, v);
  1741. if (r == Z_DATA_ERROR)
  1742. z->msg = (char*)"oversubscribed dynamic bit lengths tree";
  1743. else if (r == Z_BUF_ERROR || *bb == 0)
  1744. {
  1745. z->msg = (char*)"incomplete dynamic bit lengths tree";
  1746. r = Z_DATA_ERROR;
  1747. }
  1748. ZFREE(z, v);
  1749. return r;
  1750. }
  1751. int inflate_trees_dynamic(
  1752. uInt nl, // number of literal/length codes
  1753. uInt nd, // number of distance codes
  1754. uInt *c, // that many (total) code lengths
  1755. uInt *bl, // literal desired/actual bit depth
  1756. uInt *bd, // distance desired/actual bit depth
  1757. inflate_huft * *tl, // literal/length tree result
  1758. inflate_huft * *td, // distance tree result
  1759. inflate_huft *hp, // space for trees
  1760. z_streamp z) // for messages
  1761. {
  1762. int r;
  1763. uInt hn = 0; // hufts used in space
  1764. uInt *v; // work area for huft_build
  1765. // allocate work area
  1766. if ((v = (uInt*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
  1767. return Z_MEM_ERROR;
  1768. // build literal/length tree
  1769. r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
  1770. if (r != Z_OK || *bl == 0)
  1771. {
  1772. if (r == Z_DATA_ERROR)
  1773. z->msg = (char*)"oversubscribed literal/length tree";
  1774. else if (r != Z_MEM_ERROR)
  1775. {
  1776. z->msg = (char*)"incomplete literal/length tree";
  1777. r = Z_DATA_ERROR;
  1778. }
  1779. ZFREE(z, v);
  1780. return r;
  1781. }
  1782. // build distance tree
  1783. r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
  1784. if (r != Z_OK || (*bd == 0 && nl > 257))
  1785. {
  1786. if (r == Z_DATA_ERROR)
  1787. z->msg = (char*)"oversubscribed distance tree";
  1788. else if (r == Z_BUF_ERROR) {
  1789. z->msg = (char*)"incomplete distance tree";
  1790. r = Z_DATA_ERROR;
  1791. }
  1792. else if (r != Z_MEM_ERROR)
  1793. {
  1794. z->msg = (char*)"empty distance tree with lengths";
  1795. r = Z_DATA_ERROR;
  1796. }
  1797. ZFREE(z, v);
  1798. return r;
  1799. }
  1800. // done
  1801. ZFREE(z, v);
  1802. return Z_OK;
  1803. }
  1804. int inflate_trees_fixed(
  1805. uInt *bl, // literal desired/actual bit depth
  1806. uInt *bd, // distance desired/actual bit depth
  1807. const inflate_huft * * tl, // literal/length tree result
  1808. const inflate_huft * *td, // distance tree result
  1809. z_streamp ) // for memory allocation
  1810. {
  1811. *bl = fixed_bl;
  1812. *bd = fixed_bd;
  1813. *tl = fixed_tl;
  1814. *td = fixed_td;
  1815. return Z_OK;
  1816. }
  1817. // inffast.c -- process literals and length/distance pairs fast
  1818. // Copyright (C) 1995-1998 Mark Adler
  1819. // For conditions of distribution and use, see copyright notice in zlib.h
  1820. //
  1821. //struct inflate_codes_state {int dummy;}; // for buggy compilers
  1822. // macros for bit input with no checking and for returning unused bytes
  1823. #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
  1824. #define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;}
  1825. // Called with number of bytes left to write in window at least 258
  1826. // (the maximum string length) and number of input bytes available
  1827. // at least ten. The ten bytes are six bytes for the longest length/
  1828. // distance pair plus four bytes for overloading the bit buffer.
  1829. int inflate_fast(
  1830. uInt bl, uInt bd,
  1831. const inflate_huft *tl,
  1832. const inflate_huft *td, // need separate declaration for Borland C++
  1833. inflate_blocks_statef *s,
  1834. z_streamp z)
  1835. {
  1836. const inflate_huft *t; // temporary pointer
  1837. uInt e; // extra bits or operation
  1838. uLong b; // bit buffer
  1839. uInt k; // bits in bit buffer
  1840. Byte *p; // input data pointer
  1841. uInt n; // bytes available there
  1842. Byte *q; // output window write pointer
  1843. uInt m; // bytes to end of window or read pointer
  1844. uInt ml; // mask for literal/length tree
  1845. uInt md; // mask for distance tree
  1846. uInt c; // bytes to copy
  1847. uInt d; // distance back to copy from
  1848. Byte *r; // copy source pointer
  1849. // load input, output, bit values
  1850. LOAD
  1851. // initialize masks
  1852. ml = inflate_mask[bl];
  1853. md = inflate_mask[bd];
  1854. // do until not enough input or output space for fast loop
  1855. do { // assume called with m >= 258 && n >= 10
  1856. // get literal/length code
  1857. GRABBITS(20) // max bits for literal/length code
  1858. if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
  1859. {
  1860. DUMPBITS(t->bits)
  1861. Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
  1862. "inflate: * literal '%c'\n" :
  1863. "inflate: * literal 0x%02x\n", t->base));
  1864. *q++ = (Byte)t->base;
  1865. m--;
  1866. continue;
  1867. }
  1868. for (;;) {
  1869. DUMPBITS(t->bits)
  1870. if (e & 16)
  1871. {
  1872. // get extra bits for length
  1873. e &= 15;
  1874. c = t->base + ((uInt)b & inflate_mask[e]);
  1875. DUMPBITS(e)
  1876. Tracevv((stderr, "inflate: * length %u\n", c));
  1877. // decode distance base of block to copy
  1878. GRABBITS(15); // max bits for distance code
  1879. e = (t = td + ((uInt)b & md))->exop;
  1880. for (;;) {
  1881. DUMPBITS(t->bits)
  1882. if (e & 16)
  1883. {
  1884. // get extra bits to add to distance base
  1885. e &= 15;
  1886. GRABBITS(e) // get extra bits (up to 13)
  1887. d = t->base + ((uInt)b & inflate_mask[e]);
  1888. DUMPBITS(e)
  1889. Tracevv((stderr, "inflate: * distance %u\n", d));
  1890. // do the copy
  1891. m -= c;
  1892. if ((uInt)(q - s->window) >= d) // offset before dest
  1893. { // just copy
  1894. r = q - d;
  1895. *q++ = *r++; c--; // minimum count is three,
  1896. *q++ = *r++; c--; // so unroll loop a little
  1897. }
  1898. else // else offset after destination
  1899. {
  1900. e = d - (uInt)(q - s->window); // bytes from offset to end
  1901. r = s->end - e; // pointer to offset
  1902. if (c > e) // if source crosses,
  1903. {
  1904. c -= e; // copy to end of window
  1905. do {
  1906. *q++ = *r++;
  1907. } while (--e);
  1908. r = s->window; // copy rest from start of window
  1909. }
  1910. }
  1911. do { // copy all or what's left
  1912. *q++ = *r++;
  1913. } while (--c);
  1914. break;
  1915. }
  1916. else if ((e & 64) == 0)
  1917. {
  1918. t += t->base;
  1919. e = (t += ((uInt)b & inflate_mask[e]))->exop;
  1920. }
  1921. else
  1922. {
  1923. z->msg = (char*)"invalid distance code";
  1924. UNGRAB
  1925. UPDATE
  1926. return Z_DATA_ERROR;
  1927. }
  1928. };
  1929. break;
  1930. }
  1931. if ((e & 64) == 0)
  1932. {
  1933. t += t->base;
  1934. if ((e = (t += ((uInt)b & inflate_mask[e]))->exop) == 0)
  1935. {
  1936. DUMPBITS(t->bits)
  1937. Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
  1938. "inflate: * literal '%c'\n" :
  1939. "inflate: * literal 0x%02x\n", t->base));
  1940. *q++ = (Byte)t->base;
  1941. m--;
  1942. break;
  1943. }
  1944. }
  1945. else if (e & 32)
  1946. {
  1947. Tracevv((stderr, "inflate: * end of block\n"));
  1948. UNGRAB
  1949. UPDATE
  1950. return Z_STREAM_END;
  1951. }
  1952. else
  1953. {
  1954. z->msg = (char*)"invalid literal/length code";
  1955. UNGRAB
  1956. UPDATE
  1957. return Z_DATA_ERROR;
  1958. }
  1959. };
  1960. } while (m >= 258 && n >= 10);
  1961. // not enough input or output--restore pointers and return
  1962. UNGRAB
  1963. UPDATE
  1964. return Z_OK;
  1965. }
  1966. // crc32.c -- compute the CRC-32 of a data stream
  1967. // Copyright (C) 1995-1998 Mark Adler
  1968. // For conditions of distribution and use, see copyright notice in zlib.h
  1969. // @(#) $Id$
  1970. // Table of CRC-32's of all single-byte values (made by make_crc_table)
  1971. const uLong crc_table[256] = {
  1972. 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
  1973. 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
  1974. 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
  1975. 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
  1976. 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
  1977. 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
  1978. 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
  1979. 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
  1980. 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
  1981. 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
  1982. 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
  1983. 0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
  1984. 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
  1985. 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
  1986. 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
  1987. 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
  1988. 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
  1989. 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
  1990. 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
  1991. 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
  1992. 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
  1993. 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
  1994. 0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
  1995. 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
  1996. 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
  1997. 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
  1998. 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
  1999. 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
  2000. 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
  2001. 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
  2002. 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
  2003. 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
  2004. 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
  2005. 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
  2006. 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
  2007. 0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
  2008. 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
  2009. 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
  2010. 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
  2011. 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
  2012. 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
  2013. 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
  2014. 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
  2015. 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
  2016. 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
  2017. 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
  2018. 0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
  2019. 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
  2020. 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
  2021. 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
  2022. 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
  2023. 0x2d02ef8dL
  2024. };
  2025. const uLong * get_crc_table()
  2026. { return (const uLong *)crc_table;
  2027. }
  2028. #define CRC_DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
  2029. #define CRC_DO2(buf) CRC_DO1(buf); CRC_DO1(buf);
  2030. #define CRC_DO4(buf) CRC_DO2(buf); CRC_DO2(buf);
  2031. #define CRC_DO8(buf) CRC_DO4(buf); CRC_DO4(buf);
  2032. uLong ucrc32(uLong crc, const Byte *buf, uInt len)
  2033. { if (buf == Z_NULL) return 0L;
  2034. crc = crc ^ 0xffffffffL;
  2035. while (len >= 8) {CRC_DO8(buf); len -= 8;}
  2036. if (len) do {CRC_DO1(buf);} while (--len);
  2037. return crc ^ 0xffffffffL;
  2038. }
  2039. // adler32.c -- compute the Adler-32 checksum of a data stream
  2040. // Copyright (C) 1995-1998 Mark Adler
  2041. // For conditions of distribution and use, see copyright notice in zlib.h
  2042. // @(#) $Id$
  2043. #define BASE 65521L // largest prime smaller than 65536
  2044. #define NMAX 5552
  2045. // NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
  2046. #define AD_DO1(buf,i) {s1 += buf[i]; s2 += s1;}
  2047. #define AD_DO2(buf,i) AD_DO1(buf,i); AD_DO1(buf,i+1);
  2048. #define AD_DO4(buf,i) AD_DO2(buf,i); AD_DO2(buf,i+2);
  2049. #define AD_DO8(buf,i) AD_DO4(buf,i); AD_DO4(buf,i+4);
  2050. #define AD_DO16(buf) AD_DO8(buf,0); AD_DO8(buf,8);
  2051. // =========================================================================
  2052. uLong adler32(uLong adler, const Byte *buf, uInt len)
  2053. {
  2054. unsigned long s1 = adler & 0xffff;
  2055. unsigned long s2 = (adler >> 16) & 0xffff;
  2056. int k;
  2057. if (buf == Z_NULL) return 1L;
  2058. while (len > 0) {
  2059. k = len < NMAX ? len : NMAX;
  2060. len -= k;
  2061. while (k >= 16) {
  2062. AD_DO16(buf);
  2063. buf += 16;
  2064. k -= 16;
  2065. }
  2066. if (k != 0) do {
  2067. s1 += *buf++;
  2068. s2 += s1;
  2069. } while (--k);
  2070. s1 %= BASE;
  2071. s2 %= BASE;
  2072. }
  2073. return (s2 << 16) | s1;
  2074. }
  2075. // zutil.c -- target dependent utility functions for the compression library
  2076. // Copyright (C) 1995-1998 Jean-loup Gailly.
  2077. // For conditions of distribution and use, see copyright notice in zlib.h
  2078. // @(#) $Id$
  2079. const char * zlibVersion()
  2080. {
  2081. return ZLIB_VERSION;
  2082. }
  2083. // exported to allow conversion of error code to string for compress() and
  2084. // uncompress()
  2085. const char * zError(int err)
  2086. { return ERR_MSG(err);
  2087. }
  2088. voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
  2089. {
  2090. if (opaque) items += size - size; // make compiler happy
  2091. return (voidpf)calloc(items, size);
  2092. }
  2093. void zcfree (voidpf opaque, voidpf ptr)
  2094. {
  2095. zfree(ptr);
  2096. if (opaque) return; // make compiler happy
  2097. }
  2098. // inflate.c -- zlib interface to inflate modules
  2099. // Copyright (C) 1995-1998 Mark Adler
  2100. // For conditions of distribution and use, see copyright notice in zlib.h
  2101. //struct inflate_blocks_state {int dummy;}; // for buggy compilers
  2102. typedef enum {
  2103. IM_METHOD, // waiting for method byte
  2104. IM_FLAG, // waiting for flag byte
  2105. IM_DICT4, // four dictionary check bytes to go
  2106. IM_DICT3, // three dictionary check bytes to go
  2107. IM_DICT2, // two dictionary check bytes to go
  2108. IM_DICT1, // one dictionary check byte to go
  2109. IM_DICT0, // waiting for inflateSetDictionary
  2110. IM_BLOCKS, // decompressing blocks
  2111. IM_CHECK4, // four check bytes to go
  2112. IM_CHECK3, // three check bytes to go
  2113. IM_CHECK2, // two check bytes to go
  2114. IM_CHECK1, // one check byte to go
  2115. IM_DONE, // finished check, done
  2116. IM_BAD} // got an error--stay here
  2117. inflate_mode;
  2118. // inflate private state
  2119. struct internal_state {
  2120. // mode
  2121. inflate_mode mode; // current inflate mode
  2122. // mode dependent information
  2123. union {
  2124. uInt method; // if IM_FLAGS, method byte
  2125. struct {
  2126. uLong was; // computed check value
  2127. uLong need; // stream check value
  2128. } check; // if CHECK, check values to compare
  2129. uInt marker; // if IM_BAD, inflateSync's marker bytes count
  2130. } sub; // submode
  2131. // mode independent information
  2132. int nowrap; // flag for no wrapper
  2133. uInt wbits; // log2(window size) (8..15, defaults to 15)
  2134. inflate_blocks_statef
  2135. *blocks; // current inflate_blocks state
  2136. };
  2137. int inflateReset(z_streamp z)
  2138. {
  2139. if (z == Z_NULL || z->state == Z_NULL)
  2140. return Z_STREAM_ERROR;
  2141. z->total_in = z->total_out = 0;
  2142. z->msg = Z_NULL;
  2143. z->state->mode = z->state->nowrap ? IM_BLOCKS : IM_METHOD;
  2144. inflate_blocks_reset(z->state->blocks, z, Z_NULL);
  2145. Tracev((stderr, "inflate: reset\n"));
  2146. return Z_OK;
  2147. }
  2148. int inflateEnd(z_streamp z)
  2149. {
  2150. if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
  2151. return Z_STREAM_ERROR;
  2152. if (z->state->blocks != Z_NULL)
  2153. inflate_blocks_free(z->state->blocks, z);
  2154. ZFREE(z, z->state);
  2155. z->state = Z_NULL;
  2156. Tracev((stderr, "inflate: end\n"));
  2157. return Z_OK;
  2158. }
  2159. int inflateInit2(z_streamp z)
  2160. { const char *version = ZLIB_VERSION; int stream_size = sizeof(z_stream);
  2161. if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || stream_size != sizeof(z_stream)) return Z_VERSION_ERROR;
  2162. int w = -15; // MAX_WBITS: 32K LZ77 window.
  2163. // Warning: reducing MAX_WBITS makes minigzip unable to extract .gz files created by gzip.
  2164. // The memory requirements for deflate are (in bytes):
  2165. // (1 << (windowBits+2)) + (1 << (memLevel+9))
  2166. // that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
  2167. // plus a few kilobytes for small objects. For example, if you want to reduce
  2168. // the default memory requirements from 256K to 128K, compile with
  2169. // make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
  2170. // Of course this will generally degrade compression (there's no free lunch).
  2171. //
  2172. // The memory requirements for inflate are (in bytes) 1 << windowBits
  2173. // that is, 32K for windowBits=15 (default value) plus a few kilobytes
  2174. // for small objects.
  2175. // initialize state
  2176. if (z == Z_NULL) return Z_STREAM_ERROR;
  2177. z->msg = Z_NULL;
  2178. if (z->zalloc == Z_NULL)
  2179. {
  2180. z->zalloc = zcalloc;
  2181. z->opaque = (voidpf)0;
  2182. }
  2183. if (z->zfree == Z_NULL) z->zfree = zcfree;
  2184. if ((z->state = (struct internal_state *)
  2185. ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
  2186. return Z_MEM_ERROR;
  2187. z->state->blocks = Z_NULL;
  2188. // handle undocumented nowrap option (no zlib header or check)
  2189. z->state->nowrap = 0;
  2190. if (w < 0)
  2191. {
  2192. w = - w;
  2193. z->state->nowrap = 1;
  2194. }
  2195. // set window size
  2196. if (w < 8 || w > 15)
  2197. {
  2198. inflateEnd(z);
  2199. return Z_STREAM_ERROR;
  2200. }
  2201. z->state->wbits = (uInt)w;
  2202. // create inflate_blocks state
  2203. if ((z->state->blocks =
  2204. inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
  2205. == Z_NULL)
  2206. {
  2207. inflateEnd(z);
  2208. return Z_MEM_ERROR;
  2209. }
  2210. Tracev((stderr, "inflate: allocated\n"));
  2211. // reset state
  2212. inflateReset(z);
  2213. return Z_OK;
  2214. }
  2215. #define IM_NEEDBYTE {if(z->avail_in==0)return r;r=f;}
  2216. #define IM_NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
  2217. int inflate(z_streamp z, int f)
  2218. {
  2219. int r;
  2220. uInt b;
  2221. if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
  2222. return Z_STREAM_ERROR;
  2223. f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
  2224. r = Z_BUF_ERROR;
  2225. for (;;) switch (z->state->mode)
  2226. {
  2227. case IM_METHOD:
  2228. IM_NEEDBYTE
  2229. if (((z->state->sub.method = IM_NEXTBYTE) & 0xf) != Z_DEFLATED)
  2230. {
  2231. z->state->mode = IM_BAD;
  2232. z->msg = (char*)"unknown compression method";
  2233. z->state->sub.marker = 5; // can't try inflateSync
  2234. break;
  2235. }
  2236. if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
  2237. {
  2238. z->state->mode = IM_BAD;
  2239. z->msg = (char*)"invalid window size";
  2240. z->state->sub.marker = 5; // can't try inflateSync
  2241. break;
  2242. }
  2243. z->state->mode = IM_FLAG;
  2244. case IM_FLAG:
  2245. IM_NEEDBYTE
  2246. b = IM_NEXTBYTE;
  2247. if (((z->state->sub.method << 8) + b) % 31)
  2248. {
  2249. z->state->mode = IM_BAD;
  2250. z->msg = (char*)"incorrect header check";
  2251. z->state->sub.marker = 5; // can't try inflateSync
  2252. break;
  2253. }
  2254. Tracev((stderr, "inflate: zlib header ok\n"));
  2255. if (!(b & PRESET_DICT))
  2256. {
  2257. z->state->mode = IM_BLOCKS;
  2258. break;
  2259. }
  2260. z->state->mode = IM_DICT4;
  2261. case IM_DICT4:
  2262. IM_NEEDBYTE
  2263. z->state->sub.check.need = (uLong)IM_NEXTBYTE << 24;
  2264. z->state->mode = IM_DICT3;
  2265. case IM_DICT3:
  2266. IM_NEEDBYTE
  2267. z->state->sub.check.need += (uLong)IM_NEXTBYTE << 16;
  2268. z->state->mode = IM_DICT2;
  2269. case IM_DICT2:
  2270. IM_NEEDBYTE
  2271. z->state->sub.check.need += (uLong)IM_NEXTBYTE << 8;
  2272. z->state->mode = IM_DICT1;
  2273. case IM_DICT1:
  2274. IM_NEEDBYTE; r;
  2275. z->state->sub.check.need += (uLong)IM_NEXTBYTE;
  2276. z->adler = z->state->sub.check.need;
  2277. z->state->mode = IM_DICT0;
  2278. return Z_NEED_DICT;
  2279. case IM_DICT0:
  2280. z->state->mode = IM_BAD;
  2281. z->msg = (char*)"need dictionary";
  2282. z->state->sub.marker = 0; // can try inflateSync
  2283. return Z_STREAM_ERROR;
  2284. case IM_BLOCKS:
  2285. r = inflate_blocks(z->state->blocks, z, r);
  2286. if (r == Z_DATA_ERROR)
  2287. {
  2288. z->state->mode = IM_BAD;
  2289. z->state->sub.marker = 0; // can try inflateSync
  2290. break;
  2291. }
  2292. if (r == Z_OK)
  2293. r = f;
  2294. if (r != Z_STREAM_END)
  2295. return r;
  2296. r = f;
  2297. inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
  2298. if (z->state->nowrap)
  2299. {
  2300. z->state->mode = IM_DONE;
  2301. break;
  2302. }
  2303. z->state->mode = IM_CHECK4;
  2304. case IM_CHECK4:
  2305. IM_NEEDBYTE
  2306. z->state->sub.check.need = (uLong)IM_NEXTBYTE << 24;
  2307. z->state->mode = IM_CHECK3;
  2308. case IM_CHECK3:
  2309. IM_NEEDBYTE
  2310. z->state->sub.check.need += (uLong)IM_NEXTBYTE << 16;
  2311. z->state->mode = IM_CHECK2;
  2312. case IM_CHECK2:
  2313. IM_NEEDBYTE
  2314. z->state->sub.check.need += (uLong)IM_NEXTBYTE << 8;
  2315. z->state->mode = IM_CHECK1;
  2316. case IM_CHECK1:
  2317. IM_NEEDBYTE
  2318. z->state->sub.check.need += (uLong)IM_NEXTBYTE;
  2319. if (z->state->sub.check.was != z->state->sub.check.need)
  2320. {
  2321. z->state->mode = IM_BAD;
  2322. z->msg = (char*)"incorrect data check";
  2323. z->state->sub.marker = 5; // can't try inflateSync
  2324. break;
  2325. }
  2326. Tracev((stderr, "inflate: zlib check ok\n"));
  2327. z->state->mode = IM_DONE;
  2328. case IM_DONE:
  2329. return Z_STREAM_END;
  2330. case IM_BAD:
  2331. return Z_DATA_ERROR;
  2332. default:
  2333. return Z_STREAM_ERROR;
  2334. }
  2335. }
  2336. #ifdef _UNICODE
  2337. static int GetAnsiFileName(LPCWSTR name, char * buf, int nBufSize)
  2338. {
  2339. memset(buf, 0, nBufSize);
  2340. int n = WideCharToMultiByte(CP_ACP, // code page
  2341. 0, // performance and mapping flags
  2342. name, // wide-character string
  2343. -1, // number of chars in string
  2344. buf, // buffer for new string
  2345. nBufSize, // size of buffer
  2346. NULL, // default for unmappable chars
  2347. NULL); // set when default char used
  2348. return n;
  2349. }
  2350. static int GetUnicodeFileName(const char * name, LPWSTR buf, int nBufSize)
  2351. {
  2352. memset(buf, 0, nBufSize*sizeof(TCHAR));
  2353. int n = MultiByteToWideChar(CP_ACP, // code page
  2354. 0, // character-type options
  2355. name, // string to map
  2356. -1, // number of bytes in string
  2357. buf, // wide-character buffer
  2358. nBufSize); // size of buffer
  2359. return n;
  2360. }
  2361. #endif
  2362. // unzip.c -- IO on .zip files using zlib
  2363. // Version 0.15 beta, Mar 19th, 1998,
  2364. // Read unzip.h for more info
  2365. #define UNZ_BUFSIZE (16384)
  2366. #define UNZ_MAXFILENAMEINZIP (256)
  2367. #define SIZECENTRALDIRITEM (0x2e)
  2368. #define SIZEZIPLOCALHEADER (0x1e)
  2369. const char unz_copyright[] = " unzip 0.15 Copyright 1998 Gilles Vollant ";
  2370. // unz_file_info_interntal contain internal info about a file in zipfile
  2371. typedef struct unz_file_info_internal_s
  2372. {
  2373. uLong offset_curfile;// relative offset of local header 4 bytes
  2374. } unz_file_info_internal;
  2375. typedef struct
  2376. { bool is_handle; // either a handle or memory
  2377. bool canseek;
  2378. // for handles:
  2379. HANDLE h; bool herr; unsigned long initial_offset;
  2380. // for memory:
  2381. void *buf; unsigned int len,pos; // if it's a memory block
  2382. } LUFILE;
  2383. LUFILE *lufopen(void *z,unsigned int len,DWORD flags,ZRESULT *err)
  2384. {
  2385. if (flags!=ZIP_HANDLE && flags!=ZIP_FILENAME && flags!=ZIP_MEMORY)
  2386. {
  2387. *err=ZR_ARGS;
  2388. return NULL;
  2389. }
  2390. //
  2391. HANDLE h=0; bool canseek=false; *err=ZR_OK;
  2392. if (flags==ZIP_HANDLE||flags==ZIP_FILENAME)
  2393. {
  2394. if (flags==ZIP_HANDLE)
  2395. {
  2396. HANDLE hf = z;
  2397. bool res;
  2398. #ifdef _WIN32
  2399. res = DuplicateHandle(GetCurrentProcess(),hf,GetCurrentProcess(),&h,0,FALSE,DUPLICATE_SAME_ACCESS) == TRUE;
  2400. #else
  2401. h = (void*) dup( (int)hf );
  2402. res = (int) dup >= 0;
  2403. #endif
  2404. if (!res)
  2405. {
  2406. *err=ZR_NODUPH;
  2407. return NULL;
  2408. }
  2409. }
  2410. else
  2411. {
  2412. #ifdef _WIN32
  2413. h = CreateFile((const TCHAR *)z, GENERIC_READ, FILE_SHARE_READ,
  2414. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  2415. #else
  2416. h = (void*) open( (const TCHAR *)z, O_RDONLY );
  2417. #endif
  2418. if (h == INVALID_HANDLE_VALUE)
  2419. {
  2420. *err = ZR_NOFILE;
  2421. return NULL;
  2422. }
  2423. }
  2424. #ifdef _WIN32
  2425. DWORD type = GetFileType(h);
  2426. canseek = (type==FILE_TYPE_DISK);
  2427. #else
  2428. struct stat buf;
  2429. fstat( (int)h, &buf );
  2430. canseek = buf.st_mode & S_IFREG;
  2431. #endif
  2432. }
  2433. LUFILE *lf = new LUFILE;
  2434. if (flags==ZIP_HANDLE||flags==ZIP_FILENAME)
  2435. {
  2436. lf->is_handle=true;
  2437. lf->canseek=canseek;
  2438. lf->h=h; lf->herr=false;
  2439. lf->initial_offset=0;
  2440. if (canseek)
  2441. {
  2442. lf->initial_offset = SetFilePointer(h,0,NULL,FILE_CURRENT);
  2443. }
  2444. }
  2445. else
  2446. {
  2447. lf->is_handle=false;
  2448. lf->canseek=true;
  2449. lf->buf=z;
  2450. lf->len=len;
  2451. lf->pos=0;
  2452. lf->initial_offset=0;
  2453. }
  2454. *err=ZR_OK;
  2455. return lf;
  2456. }
  2457. int lufclose(LUFILE *stream)
  2458. { if (stream==NULL) return EOF;
  2459. if (stream->is_handle) CloseHandle(stream->h);
  2460. delete stream;
  2461. return 0;
  2462. }
  2463. int luferror(LUFILE *stream)
  2464. { if (stream->is_handle && stream->herr) return 1;
  2465. else return 0;
  2466. }
  2467. long int luftell(LUFILE *stream)
  2468. { if (stream->is_handle && stream->canseek) return SetFilePointer(stream->h,0,NULL,FILE_CURRENT)-stream->initial_offset;
  2469. else if (stream->is_handle) return 0;
  2470. else return stream->pos;
  2471. }
  2472. int lufseek(LUFILE *stream, long offset, int whence)
  2473. { if (stream->is_handle && stream->canseek)
  2474. { if (whence==SEEK_SET) SetFilePointer(stream->h,stream->initial_offset+offset,0,FILE_BEGIN);
  2475. else if (whence==SEEK_CUR) SetFilePointer(stream->h,offset,NULL,FILE_CURRENT);
  2476. else if (whence==SEEK_END) SetFilePointer(stream->h,offset,NULL,FILE_END);
  2477. else return 19; // EINVAL
  2478. return 0;
  2479. }
  2480. else if (stream->is_handle) return 29; // ESPIPE
  2481. else
  2482. { if (whence==SEEK_SET) stream->pos=offset;
  2483. else if (whence==SEEK_CUR) stream->pos+=offset;
  2484. else if (whence==SEEK_END) stream->pos=stream->len+offset;
  2485. return 0;
  2486. }
  2487. }
  2488. size_t lufread(void *ptr,size_t size,size_t n,LUFILE *stream)
  2489. { unsigned int toread = (unsigned int)(size*n);
  2490. if (stream->is_handle)
  2491. { DWORD red; BOOL res = ReadFile(stream->h,ptr,toread,&red,NULL);
  2492. if (!res) stream->herr=true;
  2493. return red/size;
  2494. }
  2495. if (stream->pos+toread > stream->len) toread = stream->len-stream->pos;
  2496. memcpy(ptr, (char*)stream->buf + stream->pos, toread); DWORD red = toread;
  2497. stream->pos += red;
  2498. return red/size;
  2499. }
  2500. // file_in_zip_read_info_s contain internal information about a file in zipfile,
  2501. // when reading and decompress it
  2502. typedef struct
  2503. {
  2504. char *read_buffer; // internal buffer for compressed data
  2505. z_stream stream; // zLib stream structure for inflate
  2506. uLong pos_in_zipfile; // position in byte on the zipfile, for fseek
  2507. uLong stream_initialised; // flag set if stream structure is initialised
  2508. uLong offset_local_extrafield;// offset of the local extra field
  2509. uInt size_local_extrafield;// size of the local extra field
  2510. uLong pos_local_extrafield; // position in the local extra field in read
  2511. uLong crc32; // crc32 of all data uncompressed
  2512. uLong crc32_wait; // crc32 we must obtain after decompress all
  2513. uLong rest_read_compressed; // number of byte to be decompressed
  2514. uLong rest_read_uncompressed;//number of byte to be obtained after decomp
  2515. LUFILE* file; // io structore of the zipfile
  2516. uLong compression_method; // compression method (0==store)
  2517. uLong byte_before_the_zipfile;// byte before the zipfile, (>0 for sfx)
  2518. } file_in_zip_read_info_s;
  2519. // unz_s contain internal information about the zipfile
  2520. typedef struct
  2521. {
  2522. LUFILE* file; // io structore of the zipfile
  2523. unz_global_info gi; // public global information
  2524. uLong byte_before_the_zipfile;// byte before the zipfile, (>0 for sfx)
  2525. uLong num_file; // number of the current file in the zipfile
  2526. uLong pos_in_central_dir; // pos of the current file in the central dir
  2527. uLong current_file_ok; // flag about the usability of the current file
  2528. uLong central_pos; // position of the beginning of the central dir
  2529. uLong size_central_dir; // size of the central directory
  2530. uLong offset_central_dir; // offset of start of central directory with respect to the starting disk number
  2531. unz_file_info cur_file_info; // public info about the current file in zip
  2532. unz_file_info_internal cur_file_info_internal; // private info about it
  2533. file_in_zip_read_info_s* pfile_in_zip_read; // structure about the current file if we are decompressing it
  2534. } unz_s, *unzFile;
  2535. int unzStringFileNameCompare (const char* fileName1,const char* fileName2,int iCaseSensitivity);
  2536. // Compare two filename (fileName1,fileName2).
  2537. z_off_t unztell (unzFile file);
  2538. // Give the current position in uncompressed data
  2539. int unzeof (unzFile file);
  2540. // return 1 if the end of file was reached, 0 elsewhere
  2541. int unzGetLocalExtrafield (unzFile file, voidp buf, unsigned len);
  2542. // Read extra field from the current file (opened by unzOpenCurrentFile)
  2543. // This is the local-header version of the extra field (sometimes, there is
  2544. // more info in the local-header version than in the central-header)
  2545. //
  2546. // if buf==NULL, it return the size of the local extra field
  2547. //
  2548. // if buf!=NULL, len is the size of the buffer, the extra header is copied in
  2549. // buf.
  2550. // the return value is the number of bytes copied in buf, or (if <0)
  2551. // the error code
  2552. // ===========================================================================
  2553. // Read a byte from a gz_stream; update next_in and avail_in. Return EOF
  2554. // for end of file.
  2555. // IN assertion: the stream s has been sucessfully opened for reading.
  2556. int unzlocal_getByte(LUFILE *fin,int *pi)
  2557. { unsigned char c;
  2558. int err = (int)lufread(&c, 1, 1, fin);
  2559. if (err==1)
  2560. { *pi = (int)c;
  2561. return UNZ_OK;
  2562. }
  2563. else
  2564. { if (luferror(fin)) return UNZ_ERRNO;
  2565. else return UNZ_EOF;
  2566. }
  2567. }
  2568. // ===========================================================================
  2569. // Reads a long in LSB order from the given gz_stream. Sets
  2570. int unzlocal_getShort (LUFILE *fin,uLong *pX)
  2571. {
  2572. uLong x ;
  2573. int i;
  2574. int err;
  2575. err = unzlocal_getByte(fin,&i);
  2576. x = (uLong)i;
  2577. if (err==UNZ_OK)
  2578. err = unzlocal_getByte(fin,&i);
  2579. x += ((uLong)i)<<8;
  2580. if (err==UNZ_OK)
  2581. *pX = x;
  2582. else
  2583. *pX = 0;
  2584. return err;
  2585. }
  2586. int unzlocal_getLong (LUFILE *fin,uLong *pX)
  2587. {
  2588. uLong x ;
  2589. int i;
  2590. int err;
  2591. err = unzlocal_getByte(fin,&i);
  2592. x = (uLong)i;
  2593. if (err==UNZ_OK)
  2594. err = unzlocal_getByte(fin,&i);
  2595. x += ((uLong)i)<<8;
  2596. if (err==UNZ_OK)
  2597. err = unzlocal_getByte(fin,&i);
  2598. x += ((uLong)i)<<16;
  2599. if (err==UNZ_OK)
  2600. err = unzlocal_getByte(fin,&i);
  2601. x += ((uLong)i)<<24;
  2602. if (err==UNZ_OK)
  2603. *pX = x;
  2604. else
  2605. *pX = 0;
  2606. return err;
  2607. }
  2608. // My own strcmpi / strcasecmp
  2609. int strcmpcasenosensitive_internal (const char* fileName1,const char *fileName2)
  2610. {
  2611. for (;;)
  2612. {
  2613. char c1=*(fileName1++);
  2614. char c2=*(fileName2++);
  2615. if ((c1>='a') && (c1<='z'))
  2616. c1 -= (char)0x20;
  2617. if ((c2>='a') && (c2<='z'))
  2618. c2 -= (char)0x20;
  2619. if (c1=='\0')
  2620. return ((c2=='\0') ? 0 : -1);
  2621. if (c2=='\0')
  2622. return 1;
  2623. if (c1<c2)
  2624. return -1;
  2625. if (c1>c2)
  2626. return 1;
  2627. }
  2628. }
  2629. //
  2630. // Compare two filename (fileName1,fileName2).
  2631. // If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp)
  2632. // If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi or strcasecmp)
  2633. //
  2634. int unzStringFileNameCompare (const char*fileName1,const char*fileName2,int iCaseSensitivity)
  2635. { if (iCaseSensitivity==1) return strcmp(fileName1,fileName2);
  2636. else return strcmpcasenosensitive_internal(fileName1,fileName2);
  2637. }
  2638. #define BUFREADCOMMENT (0x400)
  2639. // Locate the Central directory of a zipfile (at the end, just before
  2640. // the global comment)
  2641. uLong unzlocal_SearchCentralDir(LUFILE *fin)
  2642. { if (lufseek(fin,0,SEEK_END) != 0) return 0;
  2643. uLong uSizeFile = luftell(fin);
  2644. uLong uMaxBack=0xffff; // maximum size of global comment
  2645. if (uMaxBack>uSizeFile) uMaxBack = uSizeFile;
  2646. unsigned char *buf = (unsigned char*)zmalloc(BUFREADCOMMENT+4);
  2647. if (buf==NULL) return 0;
  2648. uLong uPosFound=0;
  2649. uLong uBackRead = 4;
  2650. while (uBackRead<uMaxBack)
  2651. { uLong uReadSize,uReadPos ;
  2652. int i;
  2653. if (uBackRead+BUFREADCOMMENT>uMaxBack) uBackRead = uMaxBack;
  2654. else uBackRead+=BUFREADCOMMENT;
  2655. uReadPos = uSizeFile-uBackRead ;
  2656. uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);
  2657. if (lufseek(fin,uReadPos,SEEK_SET)!=0) break;
  2658. if (lufread(buf,(uInt)uReadSize,1,fin)!=1) break;
  2659. for (i=(int)uReadSize-3; (i--)>0;)
  2660. { if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
  2661. { uPosFound = uReadPos+i; break;
  2662. }
  2663. }
  2664. if (uPosFound!=0) break;
  2665. }
  2666. if (buf) zfree(buf);
  2667. return uPosFound;
  2668. }
  2669. int unzGoToFirstFile (unzFile file);
  2670. int unzCloseCurrentFile (unzFile file);
  2671. // Open a Zip file.
  2672. // If the zipfile cannot be opened (file don't exist or in not valid), return NULL.
  2673. // Otherwise, the return value is a unzFile Handle, usable with other unzip functions
  2674. unzFile unzOpenInternal(LUFILE *fin)
  2675. { if (fin==NULL) return NULL;
  2676. if (unz_copyright[0]!=' ') {lufclose(fin); return NULL;}
  2677. int err=UNZ_OK;
  2678. unz_s us;
  2679. uLong central_pos,uL;
  2680. central_pos = unzlocal_SearchCentralDir(fin);
  2681. if (central_pos==0) err=UNZ_ERRNO;
  2682. if (lufseek(fin,central_pos,SEEK_SET)!=0) err=UNZ_ERRNO;
  2683. // the signature, already checked
  2684. if (unzlocal_getLong(fin,&uL)!=UNZ_OK) err=UNZ_ERRNO;
  2685. // number of this disk
  2686. uLong number_disk; // number of the current dist, used for spanning ZIP, unsupported, always 0
  2687. if (unzlocal_getShort(fin,&number_disk)!=UNZ_OK) err=UNZ_ERRNO;
  2688. // number of the disk with the start of the central directory
  2689. uLong number_disk_with_CD; // number the the disk with central dir, used for spaning ZIP, unsupported, always 0
  2690. if (unzlocal_getShort(fin,&number_disk_with_CD)!=UNZ_OK) err=UNZ_ERRNO;
  2691. // total number of entries in the central dir on this disk
  2692. if (unzlocal_getShort(fin,&us.gi.number_entry)!=UNZ_OK) err=UNZ_ERRNO;
  2693. // total number of entries in the central dir
  2694. uLong number_entry_CD; // total number of entries in the central dir (same than number_entry on nospan)
  2695. if (unzlocal_getShort(fin,&number_entry_CD)!=UNZ_OK) err=UNZ_ERRNO;
  2696. if ((number_entry_CD!=us.gi.number_entry) || (number_disk_with_CD!=0) || (number_disk!=0)) err=UNZ_BADZIPFILE;
  2697. // size of the central directory
  2698. if (unzlocal_getLong(fin,&us.size_central_dir)!=UNZ_OK) err=UNZ_ERRNO;
  2699. // offset of start of central directory with respect to the starting disk number
  2700. if (unzlocal_getLong(fin,&us.offset_central_dir)!=UNZ_OK) err=UNZ_ERRNO;
  2701. // zipfile comment length
  2702. if (unzlocal_getShort(fin,&us.gi.size_comment)!=UNZ_OK) err=UNZ_ERRNO;
  2703. if ((central_pos+fin->initial_offset<us.offset_central_dir+us.size_central_dir) && (err==UNZ_OK)) err=UNZ_BADZIPFILE;
  2704. if (err!=UNZ_OK) {lufclose(fin);return NULL;}
  2705. us.file=fin;
  2706. us.byte_before_the_zipfile = central_pos+fin->initial_offset - (us.offset_central_dir+us.size_central_dir);
  2707. us.central_pos = central_pos;
  2708. us.pfile_in_zip_read = NULL;
  2709. fin->initial_offset = 0; // since the zipfile itself is expected to handle this
  2710. unz_s *s = (unz_s*)zmalloc(sizeof(unz_s));
  2711. *s=us;
  2712. unzGoToFirstFile((unzFile)s);
  2713. return (unzFile)s;
  2714. }
  2715. // Close a ZipFile opened with unzipOpen.
  2716. // If there is files inside the .Zip opened with unzipOpenCurrentFile (see later),
  2717. // these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
  2718. // return UNZ_OK if there is no problem.
  2719. int unzClose (unzFile file)
  2720. {
  2721. unz_s* s;
  2722. if (file==NULL)
  2723. return UNZ_PARAMERROR;
  2724. s=(unz_s*)file;
  2725. if (s->pfile_in_zip_read!=NULL)
  2726. unzCloseCurrentFile(file);
  2727. lufclose(s->file);
  2728. if (s) zfree(s); // unused s=0;
  2729. return UNZ_OK;
  2730. }
  2731. // Write info about the ZipFile in the *pglobal_info structure.
  2732. // No preparation of the structure is needed
  2733. // return UNZ_OK if there is no problem.
  2734. int unzGetGlobalInfo (unzFile file,unz_global_info *pglobal_info)
  2735. {
  2736. unz_s* s;
  2737. if (file==NULL)
  2738. return UNZ_PARAMERROR;
  2739. s=(unz_s*)file;
  2740. *pglobal_info=s->gi;
  2741. return UNZ_OK;
  2742. }
  2743. // Translate date/time from Dos format to tm_unz (readable more easilty)
  2744. void unzlocal_DosDateToTmuDate (uLong ulDosDate, tm_unz* ptm)
  2745. {
  2746. uLong uDate;
  2747. uDate = (uLong)(ulDosDate>>16);
  2748. ptm->tm_mday = (uInt)(uDate&0x1f) ;
  2749. ptm->tm_mon = (uInt)((((uDate)&0x1E0)/0x20)-1) ;
  2750. ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ;
  2751. ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800);
  2752. ptm->tm_min = (uInt) ((ulDosDate&0x7E0)/0x20) ;
  2753. ptm->tm_sec = (uInt) (2*(ulDosDate&0x1f)) ;
  2754. }
  2755. // Get Info about the current file in the zipfile, with internal only info
  2756. int unzlocal_GetCurrentFileInfoInternal (unzFile file,
  2757. unz_file_info *pfile_info,
  2758. unz_file_info_internal
  2759. *pfile_info_internal,
  2760. char *szFileName,
  2761. uLong fileNameBufferSize,
  2762. void *extraField,
  2763. uLong extraFieldBufferSize,
  2764. char *szComment,
  2765. uLong commentBufferSize);
  2766. int unzlocal_GetCurrentFileInfoInternal (unzFile file, unz_file_info *pfile_info,
  2767. unz_file_info_internal *pfile_info_internal, char *szFileName,
  2768. uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize,
  2769. char *szComment, uLong commentBufferSize)
  2770. {
  2771. unz_s* s;
  2772. unz_file_info file_info;
  2773. unz_file_info_internal file_info_internal;
  2774. int err=UNZ_OK;
  2775. uLong uMagic;
  2776. long lSeek=0;
  2777. if (file==NULL)
  2778. return UNZ_PARAMERROR;
  2779. s=(unz_s*)file;
  2780. if (lufseek(s->file,s->pos_in_central_dir+s->byte_before_the_zipfile,SEEK_SET)!=0)
  2781. err=UNZ_ERRNO;
  2782. // we check the magic
  2783. if (err==UNZ_OK)
  2784. {
  2785. if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)
  2786. err=UNZ_ERRNO;
  2787. else if (uMagic!=0x02014b50)
  2788. err=UNZ_BADZIPFILE;
  2789. }
  2790. if (unzlocal_getShort(s->file,&file_info.version) != UNZ_OK)
  2791. err=UNZ_ERRNO;
  2792. if (unzlocal_getShort(s->file,&file_info.version_needed) != UNZ_OK)
  2793. err=UNZ_ERRNO;
  2794. if (unzlocal_getShort(s->file,&file_info.flag) != UNZ_OK)
  2795. err=UNZ_ERRNO;
  2796. if (unzlocal_getShort(s->file,&file_info.compression_method) != UNZ_OK)
  2797. err=UNZ_ERRNO;
  2798. if (unzlocal_getLong(s->file,&file_info.dosDate) != UNZ_OK)
  2799. err=UNZ_ERRNO;
  2800. unzlocal_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date);
  2801. if (unzlocal_getLong(s->file,&file_info.crc) != UNZ_OK)
  2802. err=UNZ_ERRNO;
  2803. if (unzlocal_getLong(s->file,&file_info.compressed_size) != UNZ_OK)
  2804. err=UNZ_ERRNO;
  2805. if (unzlocal_getLong(s->file,&file_info.uncompressed_size) != UNZ_OK)
  2806. err=UNZ_ERRNO;
  2807. if (unzlocal_getShort(s->file,&file_info.size_filename) != UNZ_OK)
  2808. err=UNZ_ERRNO;
  2809. if (unzlocal_getShort(s->file,&file_info.size_file_extra) != UNZ_OK)
  2810. err=UNZ_ERRNO;
  2811. if (unzlocal_getShort(s->file,&file_info.size_file_comment) != UNZ_OK)
  2812. err=UNZ_ERRNO;
  2813. if (unzlocal_getShort(s->file,&file_info.disk_num_start) != UNZ_OK)
  2814. err=UNZ_ERRNO;
  2815. if (unzlocal_getShort(s->file,&file_info.internal_fa) != UNZ_OK)
  2816. err=UNZ_ERRNO;
  2817. if (unzlocal_getLong(s->file,&file_info.external_fa) != UNZ_OK)
  2818. err=UNZ_ERRNO;
  2819. if (unzlocal_getLong(s->file,&file_info_internal.offset_curfile) != UNZ_OK)
  2820. err=UNZ_ERRNO;
  2821. lSeek+=file_info.size_filename;
  2822. if ((err==UNZ_OK) && (szFileName!=NULL))
  2823. {
  2824. uLong uSizeRead ;
  2825. if (file_info.size_filename<fileNameBufferSize)
  2826. {
  2827. *(szFileName+file_info.size_filename)='\0';
  2828. uSizeRead = file_info.size_filename;
  2829. }
  2830. else
  2831. uSizeRead = fileNameBufferSize;
  2832. if ((file_info.size_filename>0) && (fileNameBufferSize>0))
  2833. if (lufread(szFileName,(uInt)uSizeRead,1,s->file)!=1)
  2834. err=UNZ_ERRNO;
  2835. lSeek -= uSizeRead;
  2836. }
  2837. if ((err==UNZ_OK) && (extraField!=NULL))
  2838. {
  2839. uLong uSizeRead ;
  2840. if (file_info.size_file_extra<extraFieldBufferSize)
  2841. uSizeRead = file_info.size_file_extra;
  2842. else
  2843. uSizeRead = extraFieldBufferSize;
  2844. if (lSeek!=0)
  2845. {
  2846. if (lufseek(s->file,lSeek,SEEK_CUR)==0)
  2847. lSeek=0;
  2848. else
  2849. err=UNZ_ERRNO;
  2850. }
  2851. if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0))
  2852. if (lufread(extraField,(uInt)uSizeRead,1,s->file)!=1)
  2853. err=UNZ_ERRNO;
  2854. lSeek += file_info.size_file_extra - uSizeRead;
  2855. }
  2856. else
  2857. lSeek+=file_info.size_file_extra;
  2858. if ((err==UNZ_OK) && (szComment!=NULL))
  2859. {
  2860. uLong uSizeRead ;
  2861. if (file_info.size_file_comment<commentBufferSize)
  2862. {
  2863. *(szComment+file_info.size_file_comment)='\0';
  2864. uSizeRead = file_info.size_file_comment;
  2865. }
  2866. else
  2867. uSizeRead = commentBufferSize;
  2868. if (lSeek!=0)
  2869. {
  2870. if (lufseek(s->file,lSeek,SEEK_CUR)==0)
  2871. {} // unused lSeek=0;
  2872. else
  2873. err=UNZ_ERRNO;
  2874. }
  2875. if ((file_info.size_file_comment>0) && (commentBufferSize>0))
  2876. if (lufread(szComment,(uInt)uSizeRead,1,s->file)!=1)
  2877. err=UNZ_ERRNO;
  2878. //unused lSeek+=file_info.size_file_comment - uSizeRead;
  2879. }
  2880. else {} //unused lSeek+=file_info.size_file_comment;
  2881. if ((err==UNZ_OK) && (pfile_info!=NULL))
  2882. *pfile_info=file_info;
  2883. if ((err==UNZ_OK) && (pfile_info_internal!=NULL))
  2884. *pfile_info_internal=file_info_internal;
  2885. return err;
  2886. }
  2887. // Write info about the ZipFile in the *pglobal_info structure.
  2888. // No preparation of the structure is needed
  2889. // return UNZ_OK if there is no problem.
  2890. int unzGetCurrentFileInfo (unzFile file, unz_file_info *pfile_info,
  2891. char *szFileName, uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize,
  2892. char *szComment, uLong commentBufferSize)
  2893. { return unzlocal_GetCurrentFileInfoInternal(file,pfile_info,NULL,szFileName,fileNameBufferSize,
  2894. extraField,extraFieldBufferSize, szComment,commentBufferSize);
  2895. }
  2896. // Set the current file of the zipfile to the first file.
  2897. // return UNZ_OK if there is no problem
  2898. int unzGoToFirstFile (unzFile file)
  2899. {
  2900. int err;
  2901. unz_s* s;
  2902. if (file==NULL) return UNZ_PARAMERROR;
  2903. s=(unz_s*)file;
  2904. s->pos_in_central_dir=s->offset_central_dir;
  2905. s->num_file=0;
  2906. err=unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
  2907. &s->cur_file_info_internal,
  2908. NULL,0,NULL,0,NULL,0);
  2909. s->current_file_ok = (err == UNZ_OK);
  2910. return err;
  2911. }
  2912. // Set the current file of the zipfile to the next file.
  2913. // return UNZ_OK if there is no problem
  2914. // return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
  2915. int unzGoToNextFile (unzFile file)
  2916. {
  2917. unz_s* s;
  2918. int err;
  2919. if (file==NULL)
  2920. return UNZ_PARAMERROR;
  2921. s=(unz_s*)file;
  2922. if (!s->current_file_ok)
  2923. return UNZ_END_OF_LIST_OF_FILE;
  2924. if (s->num_file+1==s->gi.number_entry)
  2925. return UNZ_END_OF_LIST_OF_FILE;
  2926. s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename +
  2927. s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ;
  2928. s->num_file++;
  2929. err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
  2930. &s->cur_file_info_internal,
  2931. NULL,0,NULL,0,NULL,0);
  2932. s->current_file_ok = (err == UNZ_OK);
  2933. return err;
  2934. }
  2935. // Try locate the file szFileName in the zipfile.
  2936. // For the iCaseSensitivity signification, see unzStringFileNameCompare
  2937. // return value :
  2938. // UNZ_OK if the file is found. It becomes the current file.
  2939. // UNZ_END_OF_LIST_OF_FILE if the file is not found
  2940. int unzLocateFile (unzFile file, const TCHAR *szFileName, int iCaseSensitivity)
  2941. {
  2942. unz_s* s;
  2943. int err;
  2944. uLong num_fileSaved;
  2945. uLong pos_in_central_dirSaved;
  2946. if (file==NULL)
  2947. return UNZ_PARAMERROR;
  2948. if (_tcslen(szFileName)>=UNZ_MAXFILENAMEINZIP)
  2949. return UNZ_PARAMERROR;
  2950. char szFileNameA[MAX_PATH];
  2951. #ifdef _UNICODE
  2952. GetAnsiFileName(szFileName, szFileNameA, MAX_PATH-1);
  2953. #else
  2954. strcpy(szFileNameA, szFileName);
  2955. #endif
  2956. s=(unz_s*)file;
  2957. if (!s->current_file_ok)
  2958. return UNZ_END_OF_LIST_OF_FILE;
  2959. num_fileSaved = s->num_file;
  2960. pos_in_central_dirSaved = s->pos_in_central_dir;
  2961. err = unzGoToFirstFile(file);
  2962. while (err == UNZ_OK)
  2963. {
  2964. char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1];
  2965. unzGetCurrentFileInfo(file,NULL,
  2966. szCurrentFileName,sizeof(szCurrentFileName)-1,
  2967. NULL,0,NULL,0);
  2968. if (unzStringFileNameCompare(szCurrentFileName,szFileNameA,iCaseSensitivity)==0)
  2969. return UNZ_OK;
  2970. err = unzGoToNextFile(file);
  2971. }
  2972. s->num_file = num_fileSaved ;
  2973. s->pos_in_central_dir = pos_in_central_dirSaved ;
  2974. return err;
  2975. }
  2976. // Read the local header of the current zipfile
  2977. // Check the coherency of the local header and info in the end of central
  2978. // directory about this file
  2979. // store in *piSizeVar the size of extra info in local header
  2980. // (filename and size of extra field data)
  2981. int unzlocal_CheckCurrentFileCoherencyHeader (unz_s *s,uInt *piSizeVar,
  2982. uLong *poffset_local_extrafield, uInt *psize_local_extrafield)
  2983. {
  2984. uLong uMagic,uData,uFlags;
  2985. uLong size_filename;
  2986. uLong size_extra_field;
  2987. int err=UNZ_OK;
  2988. *piSizeVar = 0;
  2989. *poffset_local_extrafield = 0;
  2990. *psize_local_extrafield = 0;
  2991. if (lufseek(s->file,s->cur_file_info_internal.offset_curfile + s->byte_before_the_zipfile,SEEK_SET)!=0)
  2992. return UNZ_ERRNO;
  2993. if (err==UNZ_OK)
  2994. {
  2995. if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)
  2996. err=UNZ_ERRNO;
  2997. else if (uMagic!=0x04034b50)
  2998. err=UNZ_BADZIPFILE;
  2999. }
  3000. if (unzlocal_getShort(s->file,&uData) != UNZ_OK)
  3001. err=UNZ_ERRNO;
  3002. // else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion))
  3003. // err=UNZ_BADZIPFILE;
  3004. if (unzlocal_getShort(s->file,&uFlags) != UNZ_OK)
  3005. err=UNZ_ERRNO;
  3006. if (unzlocal_getShort(s->file,&uData) != UNZ_OK)
  3007. err=UNZ_ERRNO;
  3008. else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method))
  3009. err=UNZ_BADZIPFILE;
  3010. if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) &&
  3011. (s->cur_file_info.compression_method!=Z_DEFLATED))
  3012. err=UNZ_BADZIPFILE;
  3013. if (unzlocal_getLong(s->file,&uData) != UNZ_OK) // date/time
  3014. err=UNZ_ERRNO;
  3015. if (unzlocal_getLong(s->file,&uData) != UNZ_OK) // crc
  3016. err=UNZ_ERRNO;
  3017. else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) &&
  3018. ((uFlags & 8)==0))
  3019. err=UNZ_BADZIPFILE;
  3020. if (unzlocal_getLong(s->file,&uData) != UNZ_OK) // size compr
  3021. err=UNZ_ERRNO;
  3022. else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) &&
  3023. ((uFlags & 8)==0))
  3024. err=UNZ_BADZIPFILE;
  3025. if (unzlocal_getLong(s->file,&uData) != UNZ_OK) // size uncompr
  3026. err=UNZ_ERRNO;
  3027. else if ((err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) &&
  3028. ((uFlags & 8)==0))
  3029. err=UNZ_BADZIPFILE;
  3030. if (unzlocal_getShort(s->file,&size_filename) != UNZ_OK)
  3031. err=UNZ_ERRNO;
  3032. else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename))
  3033. err=UNZ_BADZIPFILE;
  3034. *piSizeVar += (uInt)size_filename;
  3035. if (unzlocal_getShort(s->file,&size_extra_field) != UNZ_OK)
  3036. err=UNZ_ERRNO;
  3037. *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile +
  3038. SIZEZIPLOCALHEADER + size_filename;
  3039. *psize_local_extrafield = (uInt)size_extra_field;
  3040. *piSizeVar += (uInt)size_extra_field;
  3041. return err;
  3042. }
  3043. // Open for reading data the current file in the zipfile.
  3044. // If there is no error and the file is opened, the return value is UNZ_OK.
  3045. int unzOpenCurrentFile (unzFile file)
  3046. {
  3047. int err;
  3048. int Store;
  3049. uInt iSizeVar;
  3050. unz_s* s;
  3051. file_in_zip_read_info_s* pfile_in_zip_read_info;
  3052. uLong offset_local_extrafield; // offset of the local extra field
  3053. uInt size_local_extrafield; // size of the local extra field
  3054. if (file==NULL)
  3055. return UNZ_PARAMERROR;
  3056. s=(unz_s*)file;
  3057. if (!s->current_file_ok)
  3058. return UNZ_PARAMERROR;
  3059. if (s->pfile_in_zip_read != NULL)
  3060. unzCloseCurrentFile(file);
  3061. if (unzlocal_CheckCurrentFileCoherencyHeader(s,&iSizeVar,
  3062. &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK)
  3063. return UNZ_BADZIPFILE;
  3064. pfile_in_zip_read_info = (file_in_zip_read_info_s*)zmalloc(sizeof(file_in_zip_read_info_s));
  3065. if (pfile_in_zip_read_info==NULL)
  3066. return UNZ_INTERNALERROR;
  3067. pfile_in_zip_read_info->read_buffer=(char*)zmalloc(UNZ_BUFSIZE);
  3068. pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;
  3069. pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;
  3070. pfile_in_zip_read_info->pos_local_extrafield=0;
  3071. if (pfile_in_zip_read_info->read_buffer==NULL)
  3072. {
  3073. if (pfile_in_zip_read_info!=0) zfree(pfile_in_zip_read_info); //unused pfile_in_zip_read_info=0;
  3074. return UNZ_INTERNALERROR;
  3075. }
  3076. pfile_in_zip_read_info->stream_initialised=0;
  3077. if ((s->cur_file_info.compression_method!=0) && (s->cur_file_info.compression_method!=Z_DEFLATED))
  3078. { // unused err=UNZ_BADZIPFILE;
  3079. }
  3080. Store = s->cur_file_info.compression_method==0;
  3081. pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc;
  3082. pfile_in_zip_read_info->crc32=0;
  3083. pfile_in_zip_read_info->compression_method =
  3084. s->cur_file_info.compression_method;
  3085. pfile_in_zip_read_info->file=s->file;
  3086. pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile;
  3087. pfile_in_zip_read_info->stream.total_out = 0;
  3088. if (!Store)
  3089. {
  3090. pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
  3091. pfile_in_zip_read_info->stream.zfree = (free_func)0;
  3092. pfile_in_zip_read_info->stream.opaque = (voidpf)0;
  3093. err=inflateInit2(&pfile_in_zip_read_info->stream);
  3094. if (err == Z_OK)
  3095. pfile_in_zip_read_info->stream_initialised=1;
  3096. // windowBits is passed < 0 to tell that there is no zlib header.
  3097. // Note that in this case inflate *requires* an extra "dummy" byte
  3098. // after the compressed stream in order to complete decompression and
  3099. // return Z_STREAM_END.
  3100. // In unzip, i don't wait absolutely Z_STREAM_END because I known the
  3101. // size of both compressed and uncompressed data
  3102. }
  3103. pfile_in_zip_read_info->rest_read_compressed =
  3104. s->cur_file_info.compressed_size ;
  3105. pfile_in_zip_read_info->rest_read_uncompressed =
  3106. s->cur_file_info.uncompressed_size ;
  3107. pfile_in_zip_read_info->pos_in_zipfile =
  3108. s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER +
  3109. iSizeVar;
  3110. pfile_in_zip_read_info->stream.avail_in = (uInt)0;
  3111. s->pfile_in_zip_read = pfile_in_zip_read_info;
  3112. return UNZ_OK;
  3113. }
  3114. // Read bytes from the current file.
  3115. // buf contain buffer where data must be copied
  3116. // len the size of buf.
  3117. // return the number of byte copied if somes bytes are copied
  3118. // return 0 if the end of file was reached
  3119. // return <0 with error code if there is an error
  3120. // (UNZ_ERRNO for IO error, or zLib error for uncompress error)
  3121. int unzReadCurrentFile (unzFile file, voidp buf, unsigned len)
  3122. { int err=UNZ_OK;
  3123. uInt iRead = 0;
  3124. unz_s *s = (unz_s*)file;
  3125. if (s==NULL) return UNZ_PARAMERROR;
  3126. file_in_zip_read_info_s* pfile_in_zip_read_info = s->pfile_in_zip_read;
  3127. if (pfile_in_zip_read_info==NULL) return UNZ_PARAMERROR;
  3128. if (pfile_in_zip_read_info->read_buffer == NULL) return UNZ_END_OF_LIST_OF_FILE;
  3129. if (len==0) return 0;
  3130. pfile_in_zip_read_info->stream.next_out = (Byte*)buf;
  3131. pfile_in_zip_read_info->stream.avail_out = (uInt)len;
  3132. if (len>pfile_in_zip_read_info->rest_read_uncompressed)
  3133. { pfile_in_zip_read_info->stream.avail_out = (uInt)pfile_in_zip_read_info->rest_read_uncompressed;
  3134. }
  3135. while (pfile_in_zip_read_info->stream.avail_out>0)
  3136. { if ((pfile_in_zip_read_info->stream.avail_in==0) && (pfile_in_zip_read_info->rest_read_compressed>0))
  3137. { uInt uReadThis = UNZ_BUFSIZE;
  3138. if (pfile_in_zip_read_info->rest_read_compressed<uReadThis) uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed;
  3139. if (uReadThis == 0) return UNZ_EOF;
  3140. if (lufseek(pfile_in_zip_read_info->file, pfile_in_zip_read_info->pos_in_zipfile + pfile_in_zip_read_info->byte_before_the_zipfile,SEEK_SET)!=0) return UNZ_ERRNO;
  3141. if (lufread(pfile_in_zip_read_info->read_buffer,uReadThis,1,pfile_in_zip_read_info->file)!=1) return UNZ_ERRNO;
  3142. pfile_in_zip_read_info->pos_in_zipfile += uReadThis;
  3143. pfile_in_zip_read_info->rest_read_compressed-=uReadThis;
  3144. pfile_in_zip_read_info->stream.next_in = (Byte*)pfile_in_zip_read_info->read_buffer;
  3145. pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis;
  3146. }
  3147. if (pfile_in_zip_read_info->compression_method==0)
  3148. { uInt uDoCopy,i ;
  3149. if (pfile_in_zip_read_info->stream.avail_out < pfile_in_zip_read_info->stream.avail_in)
  3150. { uDoCopy = pfile_in_zip_read_info->stream.avail_out ;
  3151. }
  3152. else
  3153. { uDoCopy = pfile_in_zip_read_info->stream.avail_in ;
  3154. }
  3155. for (i=0;i<uDoCopy;i++)
  3156. { *(pfile_in_zip_read_info->stream.next_out+i) = *(pfile_in_zip_read_info->stream.next_in+i);
  3157. }
  3158. pfile_in_zip_read_info->crc32 = ucrc32(pfile_in_zip_read_info->crc32,pfile_in_zip_read_info->stream.next_out,uDoCopy);
  3159. pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy;
  3160. pfile_in_zip_read_info->stream.avail_in -= uDoCopy;
  3161. pfile_in_zip_read_info->stream.avail_out -= uDoCopy;
  3162. pfile_in_zip_read_info->stream.next_out += uDoCopy;
  3163. pfile_in_zip_read_info->stream.next_in += uDoCopy;
  3164. pfile_in_zip_read_info->stream.total_out += uDoCopy;
  3165. iRead += uDoCopy;
  3166. }
  3167. else
  3168. { uLong uTotalOutBefore,uTotalOutAfter;
  3169. const Byte *bufBefore;
  3170. uLong uOutThis;
  3171. int flush=Z_SYNC_FLUSH;
  3172. uTotalOutBefore = pfile_in_zip_read_info->stream.total_out;
  3173. bufBefore = pfile_in_zip_read_info->stream.next_out;
  3174. err=inflate(&pfile_in_zip_read_info->stream,flush);
  3175. uTotalOutAfter = pfile_in_zip_read_info->stream.total_out;
  3176. uOutThis = uTotalOutAfter-uTotalOutBefore;
  3177. pfile_in_zip_read_info->crc32 = ucrc32(pfile_in_zip_read_info->crc32,bufBefore,(uInt)(uOutThis));
  3178. pfile_in_zip_read_info->rest_read_uncompressed -= uOutThis;
  3179. iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
  3180. if (err==Z_STREAM_END) return (iRead==0) ? UNZ_EOF : iRead;
  3181. if (err!=Z_OK) break;
  3182. }
  3183. }
  3184. if (err==Z_OK) return iRead;
  3185. return err;
  3186. }
  3187. // Give the current position in uncompressed data
  3188. z_off_t unztell (unzFile file)
  3189. {
  3190. unz_s* s;
  3191. file_in_zip_read_info_s* pfile_in_zip_read_info;
  3192. if (file==NULL)
  3193. return UNZ_PARAMERROR;
  3194. s=(unz_s*)file;
  3195. pfile_in_zip_read_info=s->pfile_in_zip_read;
  3196. if (pfile_in_zip_read_info==NULL)
  3197. return UNZ_PARAMERROR;
  3198. return (z_off_t)pfile_in_zip_read_info->stream.total_out;
  3199. }
  3200. // return 1 if the end of file was reached, 0 elsewhere
  3201. int unzeof (unzFile file)
  3202. {
  3203. unz_s* s;
  3204. file_in_zip_read_info_s* pfile_in_zip_read_info;
  3205. if (file==NULL)
  3206. return UNZ_PARAMERROR;
  3207. s=(unz_s*)file;
  3208. pfile_in_zip_read_info=s->pfile_in_zip_read;
  3209. if (pfile_in_zip_read_info==NULL)
  3210. return UNZ_PARAMERROR;
  3211. if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
  3212. return 1;
  3213. else
  3214. return 0;
  3215. }
  3216. // Read extra field from the current file (opened by unzOpenCurrentFile)
  3217. // This is the local-header version of the extra field (sometimes, there is
  3218. // more info in the local-header version than in the central-header)
  3219. // if buf==NULL, it return the size of the local extra field that can be read
  3220. // if buf!=NULL, len is the size of the buffer, the extra header is copied in buf.
  3221. // the return value is the number of bytes copied in buf, or (if <0) the error code
  3222. int unzGetLocalExtrafield (unzFile file,voidp buf,unsigned len)
  3223. {
  3224. unz_s* s;
  3225. file_in_zip_read_info_s* pfile_in_zip_read_info;
  3226. uInt read_now;
  3227. uLong size_to_read;
  3228. if (file==NULL)
  3229. return UNZ_PARAMERROR;
  3230. s=(unz_s*)file;
  3231. pfile_in_zip_read_info=s->pfile_in_zip_read;
  3232. if (pfile_in_zip_read_info==NULL)
  3233. return UNZ_PARAMERROR;
  3234. size_to_read = (pfile_in_zip_read_info->size_local_extrafield -
  3235. pfile_in_zip_read_info->pos_local_extrafield);
  3236. if (buf==NULL)
  3237. return (int)size_to_read;
  3238. if (len>size_to_read)
  3239. read_now = (uInt)size_to_read;
  3240. else
  3241. read_now = (uInt)len ;
  3242. if (read_now==0)
  3243. return 0;
  3244. if (lufseek(pfile_in_zip_read_info->file, pfile_in_zip_read_info->offset_local_extrafield + pfile_in_zip_read_info->pos_local_extrafield,SEEK_SET)!=0)
  3245. return UNZ_ERRNO;
  3246. if (lufread(buf,(uInt)size_to_read,1,pfile_in_zip_read_info->file)!=1)
  3247. return UNZ_ERRNO;
  3248. return (int)read_now;
  3249. }
  3250. // Close the file in zip opened with unzipOpenCurrentFile
  3251. // Return UNZ_CRCERROR if all the file was read but the CRC is not good
  3252. int unzCloseCurrentFile (unzFile file)
  3253. {
  3254. int err=UNZ_OK;
  3255. unz_s* s;
  3256. file_in_zip_read_info_s* pfile_in_zip_read_info;
  3257. if (file==NULL)
  3258. return UNZ_PARAMERROR;
  3259. s=(unz_s*)file;
  3260. pfile_in_zip_read_info=s->pfile_in_zip_read;
  3261. if (pfile_in_zip_read_info==NULL)
  3262. return UNZ_PARAMERROR;
  3263. if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
  3264. {
  3265. if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait)
  3266. err=UNZ_CRCERROR;
  3267. }
  3268. if (pfile_in_zip_read_info->read_buffer!=0)
  3269. { void *buf = pfile_in_zip_read_info->read_buffer;
  3270. zfree(buf);
  3271. pfile_in_zip_read_info->read_buffer=0;
  3272. }
  3273. pfile_in_zip_read_info->read_buffer = NULL;
  3274. if (pfile_in_zip_read_info->stream_initialised)
  3275. inflateEnd(&pfile_in_zip_read_info->stream);
  3276. pfile_in_zip_read_info->stream_initialised = 0;
  3277. if (pfile_in_zip_read_info!=0) zfree(pfile_in_zip_read_info); // unused pfile_in_zip_read_info=0;
  3278. s->pfile_in_zip_read=NULL;
  3279. return err;
  3280. }
  3281. // Get the global comment string of the ZipFile, in the szComment buffer.
  3282. // uSizeBuf is the size of the szComment buffer.
  3283. // return the number of byte copied or an error code <0
  3284. int unzGetGlobalComment (unzFile file, char *szComment, uLong uSizeBuf)
  3285. { //int err=UNZ_OK;
  3286. unz_s* s;
  3287. uLong uReadThis ;
  3288. if (file==NULL) return UNZ_PARAMERROR;
  3289. s=(unz_s*)file;
  3290. uReadThis = uSizeBuf;
  3291. if (uReadThis>s->gi.size_comment) uReadThis = s->gi.size_comment;
  3292. if (lufseek(s->file,s->central_pos+22,SEEK_SET)!=0) return UNZ_ERRNO;
  3293. if (uReadThis>0)
  3294. { *szComment='\0';
  3295. if (lufread(szComment,(uInt)uReadThis,1,s->file)!=1) return UNZ_ERRNO;
  3296. }
  3297. if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment)) *(szComment+s->gi.size_comment)='\0';
  3298. return (int)uReadThis;
  3299. }
  3300. int unzOpenCurrentFile (unzFile file);
  3301. int unzReadCurrentFile (unzFile file, void *buf, unsigned len);
  3302. int unzCloseCurrentFile (unzFile file);
  3303. #ifdef _WIN32
  3304. FILETIME timet2filetime(const time_t timer)
  3305. { struct tm *tm = gmtime(&timer);
  3306. SYSTEMTIME st;
  3307. st.wYear = (WORD)(tm->tm_year+1900);
  3308. st.wMonth = (WORD)(tm->tm_mon+1);
  3309. st.wDay = (WORD)(tm->tm_mday);
  3310. st.wHour = (WORD)(tm->tm_hour);
  3311. st.wMinute = (WORD)(tm->tm_min);
  3312. st.wSecond = (WORD)(tm->tm_sec);
  3313. st.wMilliseconds=0;
  3314. FILETIME ft;
  3315. SystemTimeToFileTime(&st,&ft);
  3316. return ft;
  3317. }
  3318. #endif
  3319. ///////////////////////////////////////////////////////////////////////////////
  3320. ///////////////////////////////////////////////////////////////////////////////
  3321. ///////////////////////////////////////////////////////////////////////////////
  3322. class TUnzip
  3323. { public:
  3324. TUnzip() : uf(0), currentfile(-1), czei(-1) {}
  3325. unzFile uf; int currentfile; ZIPENTRY cze; int czei;
  3326. TCHAR rootdir[MAX_PATH];
  3327. ZRESULT Open(void *z,unsigned int len,DWORD flags);
  3328. ZRESULT Get(int index,ZIPENTRY *ze);
  3329. ZRESULT Find(const TCHAR *name,bool ic,int *index,ZIPENTRY *ze);
  3330. ZRESULT Unzip(int index,void *dst,unsigned int len,DWORD flags);
  3331. ZRESULT Close();
  3332. };
  3333. ZRESULT TUnzip::Open(void *z,unsigned int len,DWORD flags)
  3334. {
  3335. if (uf!=0 || currentfile!=-1)
  3336. return ZR_NOTINITED;
  3337. #ifdef _WIN32
  3338. GetCurrentDirectory(MAX_PATH,rootdir);
  3339. _tcscat(rootdir,_T("\\"));
  3340. if (flags==ZIP_HANDLE)
  3341. {
  3342. DWORD type = GetFileType(z);
  3343. if (type!=FILE_TYPE_DISK)
  3344. return ZR_SEEK;
  3345. }
  3346. #endif
  3347. ZRESULT e;
  3348. LUFILE *f = lufopen(z,len,flags,&e);
  3349. if (f==NULL)
  3350. return e;
  3351. uf = unzOpenInternal(f);
  3352. return uf ? ZR_OK : ZR_CORRUPT;
  3353. }
  3354. ZRESULT TUnzip::Get(int index,ZIPENTRY *ze)
  3355. { if (index<-1 || index>=(int)uf->gi.number_entry)
  3356. return ZR_ARGS;
  3357. if (currentfile!=-1)
  3358. unzCloseCurrentFile(uf);
  3359. currentfile=-1;
  3360. if (index==czei && index!=-1) {memcpy(ze,&cze,sizeof(ZIPENTRY)); return ZR_OK;}
  3361. if (index==-1)
  3362. { ze->index = uf->gi.number_entry;
  3363. ze->name[0]=0;
  3364. ze->attr=0;
  3365. #ifdef _WIN32
  3366. ze->atime.dwLowDateTime=0; ze->atime.dwHighDateTime=0;
  3367. ze->ctime.dwLowDateTime=0; ze->ctime.dwHighDateTime=0;
  3368. ze->mtime.dwLowDateTime=0; ze->mtime.dwHighDateTime=0;
  3369. #else
  3370. ze->atime = 0;
  3371. ze->ctime = 0;
  3372. ze->mtime = 0;
  3373. #endif
  3374. ze->comp_size=0;
  3375. ze->unc_size=0;
  3376. return ZR_OK;
  3377. }
  3378. if (index<(int)uf->num_file) unzGoToFirstFile(uf);
  3379. while ((int)uf->num_file<index) unzGoToNextFile(uf);
  3380. unz_file_info ufi;
  3381. char fn[MAX_PATH];
  3382. unzGetCurrentFileInfo(uf,&ufi,fn,MAX_PATH,NULL,0,NULL,0);
  3383. // now get the extra header. We do this ourselves, instead of
  3384. // calling unzOpenCurrentFile &c., to avoid allocating more than necessary.
  3385. unsigned int extralen,iSizeVar; unsigned long offset;
  3386. int res = unzlocal_CheckCurrentFileCoherencyHeader(uf,&iSizeVar,&offset,&extralen);
  3387. if (res!=UNZ_OK) return ZR_CORRUPT;
  3388. if (lufseek(uf->file,offset,SEEK_SET)!=0) return ZR_READ;
  3389. char *extra = new char[extralen];
  3390. if (lufread(extra,1,(uInt)extralen,uf->file)!=extralen) {delete[] extra; return ZR_READ;}
  3391. //
  3392. ze->index=uf->num_file;
  3393. strcpy(ze->name,fn);
  3394. // zip has an 'attribute' 32bit value. Its lower half is windows stuff
  3395. // its upper half is standard unix attr.
  3396. unsigned long a = ufi.external_fa;
  3397. bool uisdir = (a&0x40000000)!=0;
  3398. //bool uwriteable= (a&0x08000000)!=0;
  3399. bool uwriteable= (a&0x00800000)!=0; // ***hd***
  3400. //bool ureadable= (a&0x01000000)!=0;
  3401. //bool uexecutable=(a&0x00400000)!=0;
  3402. bool wreadonly= (a&0x00000001)!=0;
  3403. bool whidden= (a&0x00000002)!=0;
  3404. bool wsystem= (a&0x00000004)!=0;
  3405. bool wisdir= (a&0x00000010)!=0;
  3406. bool warchive= (a&0x00000020)!=0;
  3407. ze->attr=FILE_ATTRIBUTE_NORMAL;
  3408. if (uisdir || wisdir) ze->attr |= FILE_ATTRIBUTE_DIRECTORY;
  3409. if (warchive) ze->attr|=FILE_ATTRIBUTE_ARCHIVE;
  3410. if (whidden) ze->attr|=FILE_ATTRIBUTE_HIDDEN;
  3411. if (!uwriteable||wreadonly) ze->attr|=FILE_ATTRIBUTE_READONLY;
  3412. if (wsystem) ze->attr|=FILE_ATTRIBUTE_SYSTEM;
  3413. ze->comp_size = ufi.compressed_size;
  3414. ze->unc_size = ufi.uncompressed_size;
  3415. //
  3416. #ifdef _WIN32
  3417. WORD dostime = (WORD)(ufi.dosDate&0xFFFF);
  3418. WORD dosdate = (WORD)((ufi.dosDate>>16)&0xFFFF);
  3419. FILETIME ft;
  3420. DosDateTimeToFileTime(dosdate,dostime,&ft);
  3421. ze->atime=ft; ze->ctime=ft; ze->mtime=ft;
  3422. #else
  3423. ze->atime=ufi.dosDate; ze->ctime=ufi.dosDate; ze->mtime=ufi.dosDate;
  3424. #endif
  3425. // the zip will always have at least that dostime. But if it also has
  3426. // an extra header, then we'll instead get the info from that.
  3427. unsigned int epos=0;
  3428. while (epos+4<extralen)
  3429. { char etype[3]; etype[0]=extra[epos+0]; etype[1]=extra[epos+1]; etype[2]=0;
  3430. int size = extra[epos+2];
  3431. if (strcmp(etype,"UT")!=0) {epos += 4+size; continue;}
  3432. int flags = extra[epos+4];
  3433. bool hasmtime = (flags&1)!=0;
  3434. bool hasatime = (flags&2)!=0;
  3435. bool hasctime = (flags&4)!=0;
  3436. epos+=5;
  3437. if (hasmtime)
  3438. { time_t mtime = *(time_t*)(extra+epos); epos+=4;
  3439. #ifdef _WIN32
  3440. ze->mtime = timet2filetime(mtime);
  3441. #else
  3442. ze->mtime = mtime;
  3443. #endif
  3444. }
  3445. if (hasatime)
  3446. { time_t atime = *(time_t*)(extra+epos); epos+=4;
  3447. #ifdef _WIN32
  3448. ze->atime = timet2filetime(atime);
  3449. #else
  3450. ze->atime = atime;
  3451. #endif
  3452. }
  3453. if (hasctime)
  3454. { time_t ctime = *(time_t*)(extra+epos);
  3455. #ifdef _WIN32
  3456. ze->ctime = timet2filetime(ctime);
  3457. #else
  3458. ze->ctime = ctime;
  3459. #endif
  3460. }
  3461. break;
  3462. }
  3463. //
  3464. if (extra!=0) delete[] extra;
  3465. memcpy(&cze,ze,sizeof(ZIPENTRY)); czei=index;
  3466. return ZR_OK;
  3467. }
  3468. ZRESULT TUnzip::Find(const TCHAR *name, bool ic, int *index, ZIPENTRY *ze)
  3469. {
  3470. int res = unzLocateFile(uf,name,ic?CASE_INSENSITIVE:CASE_SENSITIVE);
  3471. if (res!=UNZ_OK)
  3472. {
  3473. if (index!=0)
  3474. *index=-1;
  3475. if (ze!=NULL)
  3476. {
  3477. ZeroMemory(ze,sizeof(ZIPENTRY)); ze->index=-1;
  3478. }
  3479. return ZR_NOTFOUND;
  3480. }
  3481. if (currentfile!=-1)
  3482. unzCloseCurrentFile(uf);
  3483. currentfile=-1;
  3484. int i = (int)uf->num_file;
  3485. if (index!=NULL)
  3486. *index=i;
  3487. if (ze!=NULL)
  3488. {
  3489. ZRESULT zres = Get(i,ze);
  3490. if (zres!=ZR_OK)
  3491. return zres;
  3492. }
  3493. return ZR_OK;
  3494. }
  3495. void EnsureDirectory(const TCHAR *rootdir, const TCHAR *dir)
  3496. {
  3497. if (dir==NULL || dir[0] == _T('\0'))
  3498. return;
  3499. TCHAR cd[MAX_PATH];
  3500. _tcscpy(cd,rootdir);
  3501. _tcscat(cd,dir);
  3502. for ( unsigned int iCD = 0; iCD < _tcslen( cd ); iCD++ )
  3503. {
  3504. if ( cd[ iCD ] == _T( '/' ) || cd[ iCD ] == _T( '\\' ) )
  3505. {
  3506. cd[ iCD ] = 0;
  3507. CreateDirectory(cd,NULL);
  3508. cd[ iCD ] = _T( '\\' );
  3509. }
  3510. }
  3511. CreateDirectory(cd,NULL);
  3512. }
  3513. ZRESULT TUnzip::Unzip(int index,void *dst,unsigned int len,DWORD flags)
  3514. {
  3515. if (flags!=ZIP_MEMORY && flags!=ZIP_FILENAME && flags!=ZIP_HANDLE)
  3516. return ZR_ARGS;
  3517. if (flags==ZIP_MEMORY)
  3518. {
  3519. if (index!=currentfile)
  3520. {
  3521. if (currentfile!=-1)
  3522. unzCloseCurrentFile(uf);
  3523. currentfile=-1;
  3524. if (index>=(int)uf->gi.number_entry)
  3525. return ZR_ARGS;
  3526. if (index<(int)uf->num_file)
  3527. unzGoToFirstFile(uf);
  3528. while ((int)uf->num_file<index)
  3529. unzGoToNextFile(uf);
  3530. unzOpenCurrentFile(uf);
  3531. currentfile=index;
  3532. }
  3533. int res = unzReadCurrentFile(uf,dst,len);
  3534. if (res>0)
  3535. return ZR_MORE;
  3536. unzCloseCurrentFile(uf);
  3537. currentfile=-1;
  3538. if (res==0)
  3539. return ZR_OK;
  3540. else
  3541. return ZR_FLATE;
  3542. }
  3543. // otherwise we're writing to a handle or a file
  3544. if (currentfile!=-1)
  3545. unzCloseCurrentFile(uf);
  3546. currentfile=-1;
  3547. if (index >= (int)uf->gi.number_entry)
  3548. return ZR_ARGS;
  3549. if (index < (int)uf->num_file)
  3550. unzGoToFirstFile(uf);
  3551. while ((int)uf->num_file<index)
  3552. unzGoToNextFile(uf);
  3553. ZIPENTRY ze;
  3554. Get(index,&ze);
  3555. // zipentry=directory is handled specially
  3556. if ((ze.attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
  3557. {
  3558. if (flags==ZIP_HANDLE)
  3559. return ZR_OK; // don't do anything
  3560. #ifdef _UNICODE
  3561. TCHAR uname[MAX_PATH];
  3562. GetUnicodeFileName(ze.name, uname, MAX_PATH-1);
  3563. EnsureDirectory(rootdir, uname);
  3564. #else
  3565. EnsureDirectory(rootdir, ze.name);
  3566. #endif
  3567. return ZR_OK;
  3568. }
  3569. // otherwise, we write the zipentry to a file/handle
  3570. HANDLE h;
  3571. if (flags==ZIP_HANDLE)
  3572. h=dst;
  3573. else
  3574. {
  3575. const TCHAR *name = (const TCHAR *)dst;
  3576. const TCHAR *c = name;
  3577. while (*c)
  3578. {
  3579. if (*c == _T('/') || *c == _T('\\'))
  3580. name = c + 1;
  3581. c++;
  3582. }
  3583. // if it's a relative filename, ensure directories. We do this as a service
  3584. // to the caller so they can just unzip straight unto ze.name.
  3585. if (name != (const TCHAR *)dst)
  3586. {
  3587. TCHAR dir[MAX_PATH];
  3588. _tcscpy(dir,(const TCHAR*)dst);
  3589. dir[name-(const TCHAR*)dst-1] = _T('\0');
  3590. bool isabsolute = (dir[0]==_T('/') || dir[0]==_T('\\') || dir[1]==_T(':'));
  3591. isabsolute |= (_tcsstr(dir,_T("../"))!=0) | (_tcsstr(dir,_T("..\\"))!=0);
  3592. if (!isabsolute)
  3593. EnsureDirectory(rootdir,dir);
  3594. }
  3595. #ifdef _WIN32
  3596. h = ::CreateFile((const TCHAR*)dst, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
  3597. ze.attr, NULL);
  3598. #else
  3599. h = (void*) open( (const TCHAR*)dst, O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO );
  3600. #endif
  3601. }
  3602. if (h == INVALID_HANDLE_VALUE)
  3603. return ZR_NOFILE;
  3604. unzOpenCurrentFile(uf);
  3605. BYTE buf[16384];
  3606. bool haderr=false;
  3607. for (;;)
  3608. {
  3609. int res = unzReadCurrentFile(uf,buf,16384);
  3610. if (res<0)
  3611. {
  3612. haderr=true;
  3613. break;
  3614. }
  3615. if (res==0)
  3616. break;
  3617. DWORD writ;
  3618. BOOL bres = WriteFile(h,buf,res,&writ,NULL);
  3619. if (!bres)
  3620. {
  3621. haderr=true;
  3622. break;
  3623. }
  3624. }
  3625. bool settime=false;
  3626. #ifdef _WIN32
  3627. DWORD type = GetFileType(h);
  3628. if (type==FILE_TYPE_DISK && !haderr)
  3629. settime=true;
  3630. #else
  3631. struct stat sbuf;
  3632. fstat( (int)h, &sbuf );
  3633. settime = ( sbuf.st_mode & S_IFREG );
  3634. #endif
  3635. if (settime)
  3636. {
  3637. #ifdef _WIN32
  3638. SetFileTime(h,&ze.ctime,&ze.atime,&ze.mtime);
  3639. #else
  3640. struct timeval tv[2];
  3641. tv[0].tv_sec = ze.atime;
  3642. tv[0].tv_usec = 0;
  3643. tv[1].tv_sec = ze.mtime;
  3644. tv[1].tv_usec = 0;
  3645. futimes( (int)h, tv );
  3646. #endif
  3647. }
  3648. if (flags!=ZIP_HANDLE)
  3649. CloseHandle(h);
  3650. unzCloseCurrentFile(uf);
  3651. if (haderr)
  3652. return ZR_WRITE;
  3653. return ZR_OK;
  3654. }
  3655. ZRESULT TUnzip::Close()
  3656. { if (currentfile!=-1) unzCloseCurrentFile(uf); currentfile=-1;
  3657. if (uf!=0) unzClose(uf); uf=0;
  3658. return ZR_OK;
  3659. }
  3660. ZRESULT lasterrorU=ZR_OK;
  3661. unsigned int FormatZipMessageU(ZRESULT code, char *buf,unsigned int len)
  3662. { if (code==ZR_RECENT) code=lasterrorU;
  3663. const char *msg="unknown zip result code";
  3664. switch (code)
  3665. { case ZR_OK: msg="Success"; break;
  3666. case ZR_NODUPH: msg="Culdn't duplicate handle"; break;
  3667. case ZR_NOFILE: msg="Couldn't create/open file"; break;
  3668. case ZR_NOALLOC: msg="Failed to allocate memory"; break;
  3669. case ZR_WRITE: msg="Error writing to file"; break;
  3670. case ZR_NOTFOUND: msg="File not found in the zipfile"; break;
  3671. case ZR_MORE: msg="Still more data to unzip"; break;
  3672. case ZR_CORRUPT: msg="Zipfile is corrupt or not a zipfile"; break;
  3673. case ZR_READ: msg="Error reading file"; break;
  3674. case ZR_ARGS: msg="Caller: faulty arguments"; break;
  3675. case ZR_PARTIALUNZ: msg="Caller: the file had already been partially unzipped"; break;
  3676. case ZR_NOTMMAP: msg="Caller: can only get memory of a memory zipfile"; break;
  3677. case ZR_MEMSIZE: msg="Caller: not enough space allocated for memory zipfile"; break;
  3678. case ZR_FAILED: msg="Caller: there was a previous error"; break;
  3679. case ZR_ENDED: msg="Caller: additions to the zip have already been ended"; break;
  3680. case ZR_ZMODE: msg="Caller: mixing creation and opening of zip"; break;
  3681. case ZR_NOTINITED: msg="Zip-bug: internal initialisation not completed"; break;
  3682. case ZR_SEEK: msg="Zip-bug: trying to seek the unseekable"; break;
  3683. case ZR_MISSIZE: msg="Zip-bug: the anticipated size turned out wrong"; break;
  3684. case ZR_NOCHANGE: msg="Zip-bug: tried to change mind, but not allowed"; break;
  3685. case ZR_FLATE: msg="Zip-bug: an internal error during flation"; break;
  3686. }
  3687. unsigned int mlen=(unsigned int)strlen(msg);
  3688. if (buf==0 || len==0) return mlen;
  3689. unsigned int n=mlen; if (n+1>len) n=len-1;
  3690. memcpy(buf,msg,n); buf[n]=0;
  3691. return mlen;
  3692. }
  3693. typedef struct
  3694. { DWORD flag;
  3695. TUnzip *unz;
  3696. } TUnzipHandleData;
  3697. HZIP OpenZipU(void *z,unsigned int len,DWORD flags)
  3698. {
  3699. TUnzip *unz = new TUnzip();
  3700. lasterrorU = unz->Open(z,len,flags);
  3701. if (lasterrorU!=ZR_OK)
  3702. {
  3703. delete unz;
  3704. return 0;
  3705. }
  3706. TUnzipHandleData *han = new TUnzipHandleData;
  3707. han->flag=1;
  3708. han->unz=unz;
  3709. return (HZIP)han;
  3710. }
  3711. ZRESULT GetZipItemA(HZIP hz, int index, ZIPENTRY *ze)
  3712. {
  3713. if (hz==0)
  3714. {
  3715. lasterrorU=ZR_ARGS;
  3716. return ZR_ARGS;
  3717. }
  3718. TUnzipHandleData *han = (TUnzipHandleData*)hz;
  3719. if (han->flag!=1)
  3720. {
  3721. lasterrorU=ZR_ZMODE;
  3722. return ZR_ZMODE;
  3723. }
  3724. TUnzip *unz = han->unz;
  3725. lasterrorU = unz->Get(index,ze);
  3726. return lasterrorU;
  3727. }
  3728. ZRESULT GetZipItemW(HZIP hz, int index, ZIPENTRYW *zew)
  3729. {
  3730. if (hz==0)
  3731. {
  3732. lasterrorU=ZR_ARGS;
  3733. return ZR_ARGS;
  3734. }
  3735. TUnzipHandleData *han = (TUnzipHandleData*)hz;
  3736. if (han->flag!=1)
  3737. {
  3738. lasterrorU=ZR_ZMODE;
  3739. return ZR_ZMODE;
  3740. }
  3741. TUnzip *unz = han->unz;
  3742. ZIPENTRY ze;
  3743. lasterrorU = unz->Get(index,&ze);
  3744. if (lasterrorU == ZR_OK)
  3745. {
  3746. zew->index = ze.index;
  3747. zew->attr = ze.attr;
  3748. zew->atime = ze.atime;
  3749. zew->ctime = ze.ctime;
  3750. zew->mtime = ze.mtime;
  3751. zew->comp_size = ze.comp_size;
  3752. zew->unc_size = ze.unc_size;
  3753. #ifdef _UNICODE
  3754. GetUnicodeFileName(ze.name, zew->name, MAX_PATH-1);
  3755. #else
  3756. strcpy(zew->name, ze.name);
  3757. #endif
  3758. }
  3759. return lasterrorU;
  3760. }
  3761. ZRESULT FindZipItemA(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRY *ze)
  3762. {
  3763. if (hz==0)
  3764. {
  3765. lasterrorU=ZR_ARGS;
  3766. return ZR_ARGS;
  3767. }
  3768. TUnzipHandleData *han = (TUnzipHandleData*)hz;
  3769. if (han->flag!=1)
  3770. {
  3771. lasterrorU=ZR_ZMODE;
  3772. return ZR_ZMODE;
  3773. }
  3774. TUnzip *unz = han->unz;
  3775. lasterrorU = unz->Find(name,ic,index,ze);
  3776. return lasterrorU;
  3777. }
  3778. ZRESULT FindZipItemW(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRYW *zew)
  3779. {
  3780. if (hz==0)
  3781. {
  3782. lasterrorU=ZR_ARGS;
  3783. return ZR_ARGS;
  3784. }
  3785. TUnzipHandleData *han = (TUnzipHandleData*)hz;
  3786. if (han->flag!=1)
  3787. {
  3788. lasterrorU=ZR_ZMODE;
  3789. return ZR_ZMODE;
  3790. }
  3791. TUnzip *unz = han->unz;
  3792. ZIPENTRY ze;
  3793. lasterrorU = unz->Find(name,ic,index,&ze);
  3794. if (lasterrorU == ZR_OK)
  3795. {
  3796. zew->index = ze.index;
  3797. zew->attr = ze.attr;
  3798. zew->atime = ze.atime;
  3799. zew->ctime = ze.ctime;
  3800. zew->mtime = ze.mtime;
  3801. zew->comp_size = ze.comp_size;
  3802. zew->unc_size = ze.unc_size;
  3803. #ifdef _UNICODE
  3804. GetUnicodeFileName(ze.name, zew->name, MAX_PATH-1);
  3805. #else
  3806. strcpy(zew->name, ze.name);
  3807. #endif
  3808. }
  3809. return lasterrorU;
  3810. }
  3811. ZRESULT UnzipItem(HZIP hz, int index, void *dst, unsigned int len, DWORD flags)
  3812. {
  3813. if (hz==0)
  3814. {
  3815. lasterrorU=ZR_ARGS;
  3816. return ZR_ARGS;
  3817. }
  3818. TUnzipHandleData *han = (TUnzipHandleData*)hz;
  3819. if (han->flag!=1)
  3820. {
  3821. lasterrorU=ZR_ZMODE;
  3822. return ZR_ZMODE;
  3823. }
  3824. TUnzip *unz = han->unz;
  3825. lasterrorU = unz->Unzip(index,dst,len,flags);
  3826. return lasterrorU;
  3827. }
  3828. ZRESULT CloseZipU(HZIP hz)
  3829. { if (hz==0) {lasterrorU=ZR_ARGS;return ZR_ARGS;}
  3830. TUnzipHandleData *han = (TUnzipHandleData*)hz;
  3831. if (han->flag!=1) {lasterrorU=ZR_ZMODE;return ZR_ZMODE;}
  3832. TUnzip *unz = han->unz;
  3833. lasterrorU = unz->Close();
  3834. delete unz;
  3835. delete han;
  3836. return lasterrorU;
  3837. }
  3838. bool IsZipHandleU(HZIP hz)
  3839. { if (hz==0) return true;
  3840. TUnzipHandleData *han = (TUnzipHandleData*)hz;
  3841. return (han->flag==1);
  3842. }
  3843. bool SafeUnzipMemory( const void *pvZipped, int cubZipped, void *pvDest, int cubDest /* should be the exact expected unzipped size */ )
  3844. {
  3845. // unzip
  3846. HZIP hZip = OpenZip( (void *)pvZipped, cubZipped, ZIP_MEMORY );
  3847. // UnzipItem is returning ZR_MORE no matter what size buffer is passed in, we know the real size so just accept
  3848. int iRes = ZR_CORRUPT;
  3849. if ( hZip )
  3850. {
  3851. iRes = UnzipItem( hZip, 0, pvDest, cubDest, ZIP_MEMORY );
  3852. CloseZip( hZip );
  3853. }
  3854. // check for failure
  3855. if ( ZR_OK != iRes && ZR_MORE != iRes )
  3856. return false;
  3857. return true;
  3858. }