Guest User

Untitled

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