Counter Strike : Global Offensive Source Code
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.

4525 lines
145 KiB

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