Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1776 lines
62KB

  1. /*
  2. Copyright (c) 1990-2008 Info-ZIP. All rights reserved.
  3. See the accompanying file LICENSE, version 2007-Mar-04 or later
  4. (the contents of which are also included in unzip.h) for terms of use.
  5. If, for some reason, all these files are missing, the Info-ZIP license
  6. also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
  7. */
  8. /* inflate.c -- by Mark Adler
  9. version c17e, 30 Mar 2007 */
  10. /* Copyright history:
  11. - Starting with UnZip 5.41 of 16-April-2000, this source file
  12. is covered by the Info-Zip LICENSE cited above.
  13. - Prior versions of this source file, found in UnZip source packages
  14. up to UnZip 5.40, were put in the public domain.
  15. The original copyright note by Mark Adler was:
  16. "You can do whatever you like with this source file,
  17. though I would prefer that if you modify it and
  18. redistribute it that you include comments to that effect
  19. with your name and the date. Thank you."
  20. History:
  21. vers date who what
  22. ---- --------- -------------- ------------------------------------
  23. a ~~ Feb 92 M. Adler used full (large, one-step) lookup table
  24. b1 21 Mar 92 M. Adler first version with partial lookup tables
  25. b2 21 Mar 92 M. Adler fixed bug in fixed-code blocks
  26. b3 22 Mar 92 M. Adler sped up match copies, cleaned up some
  27. b4 25 Mar 92 M. Adler added prototypes; removed window[] (now
  28. is the responsibility of unzip.h--also
  29. changed name to slide[]), so needs diffs
  30. for unzip.c and unzip.h (this allows
  31. compiling in the small model on MSDOS);
  32. fixed cast of q in huft_build();
  33. b5 26 Mar 92 M. Adler got rid of unintended macro recursion.
  34. b6 27 Mar 92 M. Adler got rid of nextbyte() routine. fixed
  35. bug in inflate_fixed().
  36. c1 30 Mar 92 M. Adler removed lbits, dbits environment variables.
  37. changed BMAX to 16 for explode. Removed
  38. OUTB usage, and replaced it with flush()--
  39. this was a 20% speed improvement! Added
  40. an explode.c (to replace unimplod.c) that
  41. uses the huft routines here. Removed
  42. register union.
  43. c2 4 Apr 92 M. Adler fixed bug for file sizes a multiple of 32k.
  44. c3 10 Apr 92 M. Adler reduced memory of code tables made by
  45. huft_build significantly (factor of two to
  46. three).
  47. c4 15 Apr 92 M. Adler added NOMEMCPY do kill use of memcpy().
  48. worked around a Turbo C optimization bug.
  49. c5 21 Apr 92 M. Adler added the WSIZE #define to allow reducing
  50. the 32K window size for specialized
  51. applications.
  52. c6 31 May 92 M. Adler added some typecasts to eliminate warnings
  53. c7 27 Jun 92 G. Roelofs added some more typecasts (444: MSC bug).
  54. c8 5 Oct 92 J-l. Gailly added ifdef'd code to deal with PKZIP bug.
  55. c9 9 Oct 92 M. Adler removed a memory error message (~line 416).
  56. c10 17 Oct 92 G. Roelofs changed ULONG/UWORD/byte to ulg/ush/uch,
  57. removed old inflate, renamed inflate_entry
  58. to inflate, added Mark's fix to a comment.
  59. c10.5 14 Dec 92 M. Adler fix up error messages for incomplete trees.
  60. c11 2 Jan 93 M. Adler fixed bug in detection of incomplete
  61. tables, and removed assumption that EOB is
  62. the longest code (bad assumption).
  63. c12 3 Jan 93 M. Adler make tables for fixed blocks only once.
  64. c13 5 Jan 93 M. Adler allow all zero length codes (pkzip 2.04c
  65. outputs one zero length code for an empty
  66. distance tree).
  67. c14 12 Mar 93 M. Adler made inflate.c standalone with the
  68. introduction of inflate.h.
  69. c14b 16 Jul 93 G. Roelofs added (unsigned) typecast to w at 470.
  70. c14c 19 Jul 93 J. Bush changed v[N_MAX], l[288], ll[28x+3x] arrays
  71. to static for Amiga.
  72. c14d 13 Aug 93 J-l. Gailly de-complicatified Mark's c[*p++]++ thing.
  73. c14e 8 Oct 93 G. Roelofs changed memset() to memzero().
  74. c14f 22 Oct 93 G. Roelofs renamed quietflg to qflag; made Trace()
  75. conditional; added inflate_free().
  76. c14g 28 Oct 93 G. Roelofs changed l/(lx+1) macro to pointer (Cray bug)
  77. c14h 7 Dec 93 C. Ghisler huft_build() optimizations.
  78. c14i 9 Jan 94 A. Verheijen set fixed_t{d,l} to NULL after freeing;
  79. G. Roelofs check NEXTBYTE macro for EOF.
  80. c14j 23 Jan 94 G. Roelofs removed Ghisler "optimizations"; ifdef'd
  81. EOF check.
  82. c14k 27 Feb 94 G. Roelofs added some typecasts to avoid warnings.
  83. c14l 9 Apr 94 G. Roelofs fixed split comments on preprocessor lines
  84. to avoid bug in Encore compiler.
  85. c14m 7 Jul 94 P. Kienitz modified to allow assembler version of
  86. inflate_codes() (define ASM_INFLATECODES)
  87. c14n 22 Jul 94 G. Roelofs changed fprintf to macro for DLL versions
  88. c14o 23 Aug 94 C. Spieler added a newline to a debug statement;
  89. G. Roelofs added another typecast to avoid MSC warning
  90. c14p 4 Oct 94 G. Roelofs added (voidp *) cast to free() argument
  91. c14q 30 Oct 94 G. Roelofs changed fprintf macro to MESSAGE()
  92. c14r 1 Nov 94 G. Roelofs fixed possible redefinition of CHECK_EOF
  93. c14s 7 May 95 S. Maxwell OS/2 DLL globals stuff incorporated;
  94. P. Kienitz "fixed" ASM_INFLATECODES macro/prototype
  95. c14t 18 Aug 95 G. Roelofs added UZinflate() to use zlib functions;
  96. changed voidp to zvoid; moved huft_build()
  97. and huft_free() to end of file
  98. c14u 1 Oct 95 G. Roelofs moved G into definition of MESSAGE macro
  99. c14v 8 Nov 95 P. Kienitz changed ASM_INFLATECODES to use a regular
  100. call with __G__ instead of a macro
  101. c15 3 Aug 96 M. Adler fixed bomb-bug on random input data (Adobe)
  102. c15b 24 Aug 96 M. Adler more fixes for random input data
  103. c15c 28 Mar 97 G. Roelofs changed USE_ZLIB fatal exit code from
  104. PK_MEM2 to PK_MEM3
  105. c16 20 Apr 97 J. Altman added memzero(v[]) in huft_build()
  106. c16b 29 Mar 98 C. Spieler modified DLL code for slide redirection
  107. c16c 04 Apr 99 C. Spieler fixed memory leaks when processing gets
  108. stopped because of input data errors
  109. c16d 05 Jul 99 C. Spieler take care of FLUSH() return values and
  110. stop processing in case of errors
  111. c17 31 Dec 00 C. Spieler added preliminary support for Deflate64
  112. c17a 04 Feb 01 C. Spieler complete integration of Deflate64 support
  113. c17b 16 Feb 02 C. Spieler changed type of "extra bits" arrays and
  114. corresponding huft_build() parameter e from
  115. ush into uch, to save space
  116. c17c 9 Mar 02 C. Spieler fixed NEEDBITS() "read beyond EOF" problem
  117. with CHECK_EOF enabled
  118. c17d 23 Jul 05 C. Spieler fixed memory leaks in inflate_dynamic()
  119. when processing invalid compressed literal/
  120. distance table data
  121. c17e 30 Mar 07 C. Spieler in inflate_dynamic(), initialize tl and td
  122. to prevent freeing unallocated huft tables
  123. when processing invalid compressed data and
  124. hitting premature EOF, do not reuse td as
  125. temp work ptr during tables decoding
  126. */
  127. /*
  128. Inflate deflated (PKZIP's method 8 compressed) data. The compression
  129. method searches for as much of the current string of bytes (up to a
  130. length of 258) in the previous 32K bytes. If it doesn't find any
  131. matches (of at least length 3), it codes the next byte. Otherwise, it
  132. codes the length of the matched string and its distance backwards from
  133. the current position. There is a single Huffman code that codes both
  134. single bytes (called "literals") and match lengths. A second Huffman
  135. code codes the distance information, which follows a length code. Each
  136. length or distance code actually represents a base value and a number
  137. of "extra" (sometimes zero) bits to get to add to the base value. At
  138. the end of each deflated block is a special end-of-block (EOB) literal/
  139. length code. The decoding process is basically: get a literal/length
  140. code; if EOB then done; if a literal, emit the decoded byte; if a
  141. length then get the distance and emit the referred-to bytes from the
  142. sliding window of previously emitted data.
  143. There are (currently) three kinds of inflate blocks: stored, fixed, and
  144. dynamic. The compressor outputs a chunk of data at a time and decides
  145. which method to use on a chunk-by-chunk basis. A chunk might typically
  146. be 32K to 64K, uncompressed. If the chunk is uncompressible, then the
  147. "stored" method is used. In this case, the bytes are simply stored as
  148. is, eight bits per byte, with none of the above coding. The bytes are
  149. preceded by a count, since there is no longer an EOB code.
  150. If the data are compressible, then either the fixed or dynamic methods
  151. are used. In the dynamic method, the compressed data are preceded by
  152. an encoding of the literal/length and distance Huffman codes that are
  153. to be used to decode this block. The representation is itself Huffman
  154. coded, and so is preceded by a description of that code. These code
  155. descriptions take up a little space, and so for small blocks, there is
  156. a predefined set of codes, called the fixed codes. The fixed method is
  157. used if the block ends up smaller that way (usually for quite small
  158. chunks); otherwise the dynamic method is used. In the latter case, the
  159. codes are customized to the probabilities in the current block and so
  160. can code it much better than the pre-determined fixed codes can.
  161. The Huffman codes themselves are decoded using a multi-level table
  162. lookup, in order to maximize the speed of decoding plus the speed of
  163. building the decoding tables. See the comments below that precede the
  164. lbits and dbits tuning parameters.
  165. GRR: return values(?)
  166. 0 OK
  167. 1 incomplete table
  168. 2 bad input
  169. 3 not enough memory
  170. the following return codes are passed through from FLUSH() errors
  171. 50 (PK_DISK) "overflow of output space"
  172. 80 (IZ_CTRLC) "canceled by user's request"
  173. */
  174. /*
  175. Notes beyond the 1.93a appnote.txt:
  176. 1. Distance pointers never point before the beginning of the output
  177. stream.
  178. 2. Distance pointers can point back across blocks, up to 32k away.
  179. 3. There is an implied maximum of 7 bits for the bit length table and
  180. 15 bits for the actual data.
  181. 4. If only one code exists, then it is encoded using one bit. (Zero
  182. would be more efficient, but perhaps a little confusing.) If two
  183. codes exist, they are coded using one bit each (0 and 1).
  184. 5. There is no way of sending zero distance codes--a dummy must be
  185. sent if there are none. (History: a pre 2.0 version of PKZIP would
  186. store blocks with no distance codes, but this was discovered to be
  187. too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
  188. zero distance codes, which is sent as one code of zero bits in
  189. length.
  190. 6. There are up to 286 literal/length codes. Code 256 represents the
  191. end-of-block. Note however that the static length tree defines
  192. 288 codes just to fill out the Huffman codes. Codes 286 and 287
  193. cannot be used though, since there is no length base or extra bits
  194. defined for them. Similarily, there are up to 30 distance codes.
  195. However, static trees define 32 codes (all 5 bits) to fill out the
  196. Huffman codes, but the last two had better not show up in the data.
  197. 7. Unzip can check dynamic Huffman blocks for complete code sets.
  198. The exception is that a single code would not be complete (see #4).
  199. 8. The five bits following the block type is really the number of
  200. literal codes sent minus 257.
  201. 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
  202. (1+6+6). Therefore, to output three times the length, you output
  203. three codes (1+1+1), whereas to output four times the same length,
  204. you only need two codes (1+3). Hmm.
  205. 10. In the tree reconstruction algorithm, Code = Code + Increment
  206. only if BitLength(i) is not zero. (Pretty obvious.)
  207. 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
  208. 12. Note: length code 284 can represent 227-258, but length code 285
  209. really is 258. The last length deserves its own, short code
  210. since it gets used a lot in very redundant files. The length
  211. 258 is special since 258 - 3 (the min match length) is 255.
  212. 13. The literal/length and distance code bit lengths are read as a
  213. single stream of lengths. It is possible (and advantageous) for
  214. a repeat code (16, 17, or 18) to go across the boundary between
  215. the two sets of lengths.
  216. 14. The Deflate64 (PKZIP method 9) variant of the compression algorithm
  217. differs from "classic" deflate in the following 3 aspect:
  218. a) The size of the sliding history window is expanded to 64 kByte.
  219. b) The previously unused distance codes #30 and #31 code distances
  220. from 32769 to 49152 and 49153 to 65536. Both codes take 14 bits
  221. of extra data to determine the exact position in their 16 kByte
  222. range.
  223. c) The last lit/length code #285 gets a different meaning. Instead
  224. of coding a fixed maximum match length of 258, it is used as a
  225. "generic" match length code, capable of coding any length from
  226. 3 (min match length + 0) to 65538 (min match length + 65535).
  227. This means that the length code #285 takes 16 bits (!) of uncoded
  228. extra data, added to a fixed min length of 3.
  229. Changes a) and b) would have been transparent for valid deflated
  230. data, but change c) requires to switch decoder configurations between
  231. Deflate and Deflate64 modes.
  232. */
  233. #define PKZIP_BUG_WORKAROUND /* PKZIP 1.93a problem--live with it */
  234. /*
  235. inflate.h must supply the uch slide[WSIZE] array, the zvoid typedef
  236. (void if (void *) is accepted, else char) and the NEXTBYTE,
  237. FLUSH() and memzero macros. If the window size is not 32K, it
  238. should also define WSIZE. If INFMOD is defined, it can include
  239. compiled functions to support the NEXTBYTE and/or FLUSH() macros.
  240. There are defaults for NEXTBYTE and FLUSH() below for use as
  241. examples of what those functions need to do. Normally, you would
  242. also want FLUSH() to compute a crc on the data. inflate.h also
  243. needs to provide these typedefs:
  244. typedef unsigned char uch;
  245. typedef unsigned short ush;
  246. typedef unsigned long ulg;
  247. This module uses the external functions malloc() and free() (and
  248. probably memset() or bzero() in the memzero() macro). Their
  249. prototypes are normally found in <string.h> and <stdlib.h>.
  250. */
  251. #define __INFLATE_C /* identifies this source module */
  252. /* #define DEBUG */
  253. #define INFMOD /* tell inflate.h to include code to be compiled */
  254. #include "inflate.h"
  255. /* marker for "unused" huft code, and corresponding check macro */
  256. #define INVALID_CODE 99
  257. #define IS_INVALID_CODE(c) ((c) == INVALID_CODE)
  258. #ifndef WSIZE /* default is 32K resp. 64K */
  259. # ifdef USE_DEFLATE64
  260. # define WSIZE 65536L /* window size--must be a power of two, and */
  261. # else /* at least 64K for PKZip's deflate64 method */
  262. # define WSIZE 0x8000 /* window size--must be a power of two, and */
  263. # endif /* at least 32K for zip's deflate method */
  264. #endif
  265. /* some buffer counters must be capable of holding 64k for Deflate64 */
  266. #if (defined(USE_DEFLATE64) && defined(INT_16BIT))
  267. # define UINT_D64 ulg
  268. #else
  269. # define UINT_D64 unsigned
  270. #endif
  271. #if (defined(DLL) && !defined(NO_SLIDE_REDIR))
  272. # define wsize G._wsize /* wsize is a variable */
  273. #else
  274. # define wsize WSIZE /* wsize is a constant */
  275. #endif
  276. #ifndef NEXTBYTE /* default is to simply get a byte from stdin */
  277. # define NEXTBYTE getchar()
  278. #endif
  279. #ifndef MESSAGE /* only used twice, for fixed strings--NOT general-purpose */
  280. # define MESSAGE(str,len,flag) fprintf(stderr,(char *)(str))
  281. #endif
  282. #ifndef FLUSH /* default is to simply write the buffer to stdout */
  283. # define FLUSH(n) \
  284. (((extent)fwrite(redirSlide, 1, (extent)(n), stdout) == (extent)(n)) ? \
  285. 0 : PKDISK)
  286. #endif
  287. /* Warning: the fwrite above might not work on 16-bit compilers, since
  288. 0x8000 might be interpreted as -32,768 by the library function. When
  289. support for Deflate64 is enabled, the window size is 64K and the
  290. simple fwrite statement is definitely broken for 16-bit compilers. */
  291. #ifndef Trace
  292. # ifdef DEBUG
  293. # define Trace(x) fprintf x
  294. # else
  295. # define Trace(x)
  296. # endif
  297. #endif
  298. /*---------------------------------------------------------------------------*/
  299. #ifdef USE_ZLIB
  300. /* Beginning with zlib version 1.2.0, a new inflate callback interface is
  301. provided that allows tighter integration of the zlib inflate service
  302. into unzip's extraction framework.
  303. The advantages are:
  304. - uses the windows buffer supplied by the unzip code; this saves one
  305. copy process between zlib's internal decompression buffer and unzip's
  306. post-decompression output buffer and improves performance.
  307. - does not pull in unused checksum code (adler32).
  308. The preprocessor flag NO_ZLIBCALLBCK can be set to force usage of the
  309. old zlib 1.1.x interface, for testing purpose.
  310. */
  311. #ifdef USE_ZLIB_INFLATCB
  312. # undef USE_ZLIB_INFLATCB
  313. #endif
  314. #if (defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1200 && !defined(NO_ZLIBCALLBCK))
  315. # define USE_ZLIB_INFLATCB 1
  316. #else
  317. # define USE_ZLIB_INFLATCB 0
  318. #endif
  319. /* Check for incompatible combinations of zlib and Deflate64 support. */
  320. #if defined(USE_DEFLATE64)
  321. # if !USE_ZLIB_INFLATCB
  322. #error Deflate64 is incompatible with traditional (pre-1.2.x) zlib interface!
  323. # else
  324. /* The Deflate64 callback function in the framework of zlib 1.2.x requires
  325. the inclusion of the unsupported infback9 header file:
  326. */
  327. # include "infback9.h"
  328. # endif
  329. #endif /* USE_DEFLATE64 */
  330. #if USE_ZLIB_INFLATCB
  331. static unsigned zlib_inCB OF((void FAR *pG, unsigned char FAR * FAR * pInbuf));
  332. static int zlib_outCB OF((void FAR *pG, unsigned char FAR *outbuf,
  333. unsigned outcnt));
  334. static unsigned zlib_inCB(pG, pInbuf)
  335. void FAR *pG;
  336. unsigned char FAR * FAR * pInbuf;
  337. {
  338. *pInbuf = G.inbuf;
  339. return fillinbuf(__G);
  340. }
  341. static int zlib_outCB(pG, outbuf, outcnt)
  342. void FAR *pG;
  343. unsigned char FAR *outbuf;
  344. unsigned outcnt;
  345. {
  346. #ifdef FUNZIP
  347. return flush(__G__ (ulg)(outcnt));
  348. #else
  349. return ((G.mem_mode) ? memflush(__G__ outbuf, (ulg)(outcnt))
  350. : flush(__G__ outbuf, (ulg)(outcnt), 0));
  351. #endif
  352. }
  353. #endif /* USE_ZLIB_INFLATCB */
  354. /*
  355. GRR: return values for both original inflate() and UZinflate()
  356. 0 OK
  357. 1 incomplete table(?)
  358. 2 bad input
  359. 3 not enough memory
  360. */
  361. /**************************/
  362. /* Function UZinflate() */
  363. /**************************/
  364. int UZinflate(__G__ is_defl64)
  365. __GDEF
  366. int is_defl64;
  367. /* decompress an inflated entry using the zlib routines */
  368. {
  369. int retval = 0; /* return code: 0 = "no error" */
  370. int err=Z_OK;
  371. #if USE_ZLIB_INFLATCB
  372. #if (defined(DLL) && !defined(NO_SLIDE_REDIR))
  373. if (G.redirect_slide)
  374. wsize = G.redirect_size, redirSlide = G.redirect_buffer;
  375. else
  376. wsize = WSIZE, redirSlide = slide;
  377. #endif
  378. if (!G.inflInit) {
  379. /* local buffer for efficiency */
  380. ZCONST char *zlib_RtVersion = zlibVersion();
  381. /* only need to test this stuff once */
  382. if ((zlib_RtVersion[0] != ZLIB_VERSION[0]) ||
  383. (zlib_RtVersion[2] != ZLIB_VERSION[2])) {
  384. Info(slide, 0x21, ((char *)slide,
  385. "error: incompatible zlib version (expected %s, found %s)\n",
  386. ZLIB_VERSION, zlib_RtVersion));
  387. return 3;
  388. } else if (strcmp(zlib_RtVersion, ZLIB_VERSION) != 0)
  389. Info(slide, 0x21, ((char *)slide,
  390. "warning: different zlib version (expected %s, using %s)\n",
  391. ZLIB_VERSION, zlib_RtVersion));
  392. G.dstrm.zalloc = (alloc_func)Z_NULL;
  393. G.dstrm.zfree = (free_func)Z_NULL;
  394. G.inflInit = 1;
  395. }
  396. #ifdef USE_DEFLATE64
  397. if (is_defl64)
  398. {
  399. Trace((stderr, "initializing inflate9()\n"));
  400. err = inflateBack9Init(&G.dstrm, redirSlide);
  401. if (err == Z_MEM_ERROR)
  402. return 3;
  403. else if (err != Z_OK) {
  404. Trace((stderr, "oops! (inflateBack9Init() err = %d)\n", err));
  405. return 2;
  406. }
  407. G.dstrm.next_in = G.inptr;
  408. G.dstrm.avail_in = G.incnt;
  409. err = inflateBack9(&G.dstrm, zlib_inCB, &G, zlib_outCB, &G);
  410. if (err != Z_STREAM_END) {
  411. if (err == Z_DATA_ERROR || err == Z_STREAM_ERROR) {
  412. Trace((stderr, "oops! (inflateBack9() err = %d)\n", err));
  413. retval = 2;
  414. } else if (err == Z_MEM_ERROR) {
  415. retval = 3;
  416. } else if (err == Z_BUF_ERROR) {
  417. Trace((stderr, "oops! (inflateBack9() err = %d)\n", err));
  418. if (G.dstrm.next_in == Z_NULL) {
  419. /* input failure */
  420. Trace((stderr, " inflateBack9() input failure\n"));
  421. retval = 2;
  422. } else {
  423. /* output write failure */
  424. retval = (G.disk_full != 0 ? PK_DISK : IZ_CTRLC);
  425. }
  426. } else {
  427. Trace((stderr, "oops! (inflateBack9() err = %d)\n", err));
  428. retval = 2;
  429. }
  430. }
  431. if (G.dstrm.next_in != NULL) {
  432. G.inptr = (uch *)G.dstrm.next_in;
  433. G.incnt = G.dstrm.avail_in;
  434. }
  435. err = inflateBack9End(&G.dstrm);
  436. if (err != Z_OK) {
  437. Trace((stderr, "oops! (inflateBack9End() err = %d)\n", err));
  438. if (retval == 0)
  439. retval = 2;
  440. }
  441. }
  442. else
  443. #endif /* USE_DEFLATE64 */
  444. {
  445. /* For the callback interface, inflate initialization has to
  446. be called before each decompression call.
  447. */
  448. {
  449. unsigned i;
  450. int windowBits;
  451. /* windowBits = log2(wsize) */
  452. for (i = (unsigned)wsize, windowBits = 0;
  453. !(i & 1); i >>= 1, ++windowBits);
  454. if ((unsigned)windowBits > (unsigned)15)
  455. windowBits = 15;
  456. else if (windowBits < 8)
  457. windowBits = 8;
  458. Trace((stderr, "initializing inflate()\n"));
  459. err = inflateBackInit(&G.dstrm, windowBits, redirSlide);
  460. if (err == Z_MEM_ERROR)
  461. return 3;
  462. else if (err != Z_OK) {
  463. Trace((stderr, "oops! (inflateBackInit() err = %d)\n", err));
  464. return 2;
  465. }
  466. }
  467. G.dstrm.next_in = G.inptr;
  468. G.dstrm.avail_in = G.incnt;
  469. err = inflateBack(&G.dstrm, zlib_inCB, &G, zlib_outCB, &G);
  470. if (err != Z_STREAM_END) {
  471. if (err == Z_DATA_ERROR || err == Z_STREAM_ERROR) {
  472. Trace((stderr, "oops! (inflateBack() err = %d)\n", err));
  473. retval = 2;
  474. } else if (err == Z_MEM_ERROR) {
  475. retval = 3;
  476. } else if (err == Z_BUF_ERROR) {
  477. Trace((stderr, "oops! (inflateBack() err = %d)\n", err));
  478. if (G.dstrm.next_in == Z_NULL) {
  479. /* input failure */
  480. Trace((stderr, " inflateBack() input failure\n"));
  481. retval = 2;
  482. } else {
  483. /* output write failure */
  484. retval = (G.disk_full != 0 ? PK_DISK : IZ_CTRLC);
  485. }
  486. } else {
  487. Trace((stderr, "oops! (inflateBack() err = %d)\n", err));
  488. retval = 2;
  489. }
  490. }
  491. if (G.dstrm.next_in != NULL) {
  492. G.inptr = (uch *)G.dstrm.next_in;
  493. G.incnt = G.dstrm.avail_in;
  494. }
  495. err = inflateBackEnd(&G.dstrm);
  496. if (err != Z_OK) {
  497. Trace((stderr, "oops! (inflateBackEnd() err = %d)\n", err));
  498. if (retval == 0)
  499. retval = 2;
  500. }
  501. }
  502. #else /* !USE_ZLIB_INFLATCB */
  503. int repeated_buf_err;
  504. #if (defined(DLL) && !defined(NO_SLIDE_REDIR))
  505. if (G.redirect_slide)
  506. wsize = G.redirect_size, redirSlide = G.redirect_buffer;
  507. else
  508. wsize = WSIZE, redirSlide = slide;
  509. #endif
  510. G.dstrm.next_out = redirSlide;
  511. G.dstrm.avail_out = wsize;
  512. G.dstrm.next_in = G.inptr;
  513. G.dstrm.avail_in = G.incnt;
  514. if (!G.inflInit) {
  515. unsigned i;
  516. int windowBits;
  517. /* local buffer for efficiency */
  518. ZCONST char *zlib_RtVersion = zlibVersion();
  519. /* only need to test this stuff once */
  520. if (zlib_RtVersion[0] != ZLIB_VERSION[0]) {
  521. Info(slide, 0x21, ((char *)slide,
  522. "error: incompatible zlib version (expected %s, found %s)\n",
  523. ZLIB_VERSION, zlib_RtVersion));
  524. return 3;
  525. } else if (strcmp(zlib_RtVersion, ZLIB_VERSION) != 0)
  526. Info(slide, 0x21, ((char *)slide,
  527. "warning: different zlib version (expected %s, using %s)\n",
  528. ZLIB_VERSION, zlib_RtVersion));
  529. /* windowBits = log2(wsize) */
  530. for (i = (unsigned)wsize, windowBits = 0;
  531. !(i & 1); i >>= 1, ++windowBits);
  532. if ((unsigned)windowBits > (unsigned)15)
  533. windowBits = 15;
  534. else if (windowBits < 8)
  535. windowBits = 8;
  536. G.dstrm.zalloc = (alloc_func)Z_NULL;
  537. G.dstrm.zfree = (free_func)Z_NULL;
  538. Trace((stderr, "initializing inflate()\n"));
  539. err = inflateInit2(&G.dstrm, -windowBits);
  540. if (err == Z_MEM_ERROR)
  541. return 3;
  542. else if (err != Z_OK)
  543. Trace((stderr, "oops! (inflateInit2() err = %d)\n", err));
  544. G.inflInit = 1;
  545. }
  546. #ifdef FUNZIP
  547. while (err != Z_STREAM_END) {
  548. #else /* !FUNZIP */
  549. while (G.csize > 0) {
  550. Trace((stderr, "first loop: G.csize = %ld\n", G.csize));
  551. #endif /* ?FUNZIP */
  552. while (G.dstrm.avail_out > 0) {
  553. err = inflate(&G.dstrm, Z_PARTIAL_FLUSH);
  554. if (err == Z_DATA_ERROR) {
  555. retval = 2; goto uzinflate_cleanup_exit;
  556. } else if (err == Z_MEM_ERROR) {
  557. retval = 3; goto uzinflate_cleanup_exit;
  558. } else if (err != Z_OK && err != Z_STREAM_END)
  559. Trace((stderr, "oops! (inflate(first loop) err = %d)\n", err));
  560. #ifdef FUNZIP
  561. if (err == Z_STREAM_END) /* "END-of-entry-condition" ? */
  562. #else /* !FUNZIP */
  563. if (G.csize <= 0L) /* "END-of-entry-condition" ? */
  564. #endif /* ?FUNZIP */
  565. break;
  566. if (G.dstrm.avail_in == 0) {
  567. if (fillinbuf(__G) == 0) {
  568. /* no "END-condition" yet, but no more data */
  569. retval = 2; goto uzinflate_cleanup_exit;
  570. }
  571. G.dstrm.next_in = G.inptr;
  572. G.dstrm.avail_in = G.incnt;
  573. }
  574. Trace((stderr, " avail_in = %u\n", G.dstrm.avail_in));
  575. }
  576. /* flush slide[] */
  577. if ((retval = FLUSH(wsize - G.dstrm.avail_out)) != 0)
  578. goto uzinflate_cleanup_exit;
  579. Trace((stderr, "inside loop: flushing %ld bytes (ptr diff = %ld)\n",
  580. (long)(wsize - G.dstrm.avail_out),
  581. (long)(G.dstrm.next_out-(Bytef *)redirSlide)));
  582. G.dstrm.next_out = redirSlide;
  583. G.dstrm.avail_out = wsize;
  584. }
  585. /* no more input, so loop until we have all output */
  586. Trace((stderr, "beginning final loop: err = %d\n", err));
  587. repeated_buf_err = FALSE;
  588. while (err != Z_STREAM_END) {
  589. err = inflate(&G.dstrm, Z_PARTIAL_FLUSH);
  590. if (err == Z_DATA_ERROR) {
  591. retval = 2; goto uzinflate_cleanup_exit;
  592. } else if (err == Z_MEM_ERROR) {
  593. retval = 3; goto uzinflate_cleanup_exit;
  594. } else if (err == Z_BUF_ERROR) { /* DEBUG */
  595. #ifdef FUNZIP
  596. Trace((stderr,
  597. "zlib inflate() did not detect stream end\n"));
  598. #else
  599. Trace((stderr,
  600. "zlib inflate() did not detect stream end (%s, %s)\n",
  601. G.zipfn, G.filename));
  602. #endif
  603. if ((!repeated_buf_err) && (G.dstrm.avail_in == 0)) {
  604. /* when detecting this problem for the first time,
  605. try to provide one fake byte beyond "EOF"... */
  606. G.dstrm.next_in = "";
  607. G.dstrm.avail_in = 1;
  608. repeated_buf_err = TRUE;
  609. } else
  610. break;
  611. } else if (err != Z_OK && err != Z_STREAM_END) {
  612. Trace((stderr, "oops! (inflate(final loop) err = %d)\n", err));
  613. DESTROYGLOBALS();
  614. EXIT(PK_MEM3);
  615. }
  616. /* final flush of slide[] */
  617. if ((retval = FLUSH(wsize - G.dstrm.avail_out)) != 0)
  618. goto uzinflate_cleanup_exit;
  619. Trace((stderr, "final loop: flushing %ld bytes (ptr diff = %ld)\n",
  620. (long)(wsize - G.dstrm.avail_out),
  621. (long)(G.dstrm.next_out-(Bytef *)redirSlide)));
  622. G.dstrm.next_out = redirSlide;
  623. G.dstrm.avail_out = wsize;
  624. }
  625. Trace((stderr, "total in = %lu, total out = %lu\n", G.dstrm.total_in,
  626. G.dstrm.total_out));
  627. G.inptr = (uch *)G.dstrm.next_in;
  628. G.incnt = (G.inbuf + INBUFSIZ) - G.inptr; /* reset for other routines */
  629. uzinflate_cleanup_exit:
  630. err = inflateReset(&G.dstrm);
  631. if (err != Z_OK)
  632. Trace((stderr, "oops! (inflateReset() err = %d)\n", err));
  633. #endif /* ?USE_ZLIB_INFLATCB */
  634. return retval;
  635. }
  636. /*---------------------------------------------------------------------------*/
  637. #else /* !USE_ZLIB */
  638. /* Function prototypes */
  639. #ifndef OF
  640. # ifdef __STDC__
  641. # define OF(a) a
  642. # else
  643. # define OF(a) ()
  644. # endif
  645. #endif /* !OF */
  646. int inflate_codes OF((__GPRO__ struct huft *tl, struct huft *td,
  647. unsigned bl, unsigned bd));
  648. static int inflate_stored OF((__GPRO));
  649. static int inflate_fixed OF((__GPRO));
  650. static int inflate_dynamic OF((__GPRO));
  651. static int inflate_block OF((__GPRO__ int *e));
  652. /* The inflate algorithm uses a sliding 32K byte window on the uncompressed
  653. stream to find repeated byte strings. This is implemented here as a
  654. circular buffer. The index is updated simply by incrementing and then
  655. and'ing with 0x7fff (32K-1). */
  656. /* It is left to other modules to supply the 32K area. It is assumed
  657. to be usable as if it were declared "uch slide[32768];" or as just
  658. "uch *slide;" and then malloc'ed in the latter case. The definition
  659. must be in unzip.h, included above. */
  660. /* unsigned wp; moved to globals.h */ /* current position in slide */
  661. /* Tables for deflate from PKZIP's appnote.txt. */
  662. /* - Order of the bit length code lengths */
  663. static ZCONST unsigned border[] = {
  664. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  665. /* - Copy lengths for literal codes 257..285 */
  666. #ifdef USE_DEFLATE64
  667. static ZCONST ush cplens64[] = {
  668. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  669. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 3, 0, 0};
  670. /* For Deflate64, the code 285 is defined differently. */
  671. #else
  672. # define cplens32 cplens
  673. #endif
  674. static ZCONST ush cplens32[] = {
  675. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  676. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
  677. /* note: see note #13 above about the 258 in this list. */
  678. /* - Extra bits for literal codes 257..285 */
  679. #ifdef USE_DEFLATE64
  680. static ZCONST uch cplext64[] = {
  681. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  682. 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 16, INVALID_CODE, INVALID_CODE};
  683. #else
  684. # define cplext32 cplext
  685. #endif
  686. static ZCONST uch cplext32[] = {
  687. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  688. 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, INVALID_CODE, INVALID_CODE};
  689. /* - Copy offsets for distance codes 0..29 (0..31 for Deflate64) */
  690. static ZCONST ush cpdist[] = {
  691. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  692. 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  693. #if (defined(USE_DEFLATE64) || defined(PKZIP_BUG_WORKAROUND))
  694. 8193, 12289, 16385, 24577, 32769, 49153};
  695. #else
  696. 8193, 12289, 16385, 24577};
  697. #endif
  698. /* - Extra bits for distance codes 0..29 (0..31 for Deflate64) */
  699. #ifdef USE_DEFLATE64
  700. static ZCONST uch cpdext64[] = {
  701. 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  702. 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  703. 12, 12, 13, 13, 14, 14};
  704. #else
  705. # define cpdext32 cpdext
  706. #endif
  707. static ZCONST uch cpdext32[] = {
  708. 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  709. 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  710. #ifdef PKZIP_BUG_WORKAROUND
  711. 12, 12, 13, 13, INVALID_CODE, INVALID_CODE};
  712. #else
  713. 12, 12, 13, 13};
  714. #endif
  715. #ifdef PKZIP_BUG_WORKAROUND
  716. # define MAXLITLENS 288
  717. #else
  718. # define MAXLITLENS 286
  719. #endif
  720. #if (defined(USE_DEFLATE64) || defined(PKZIP_BUG_WORKAROUND))
  721. # define MAXDISTS 32
  722. #else
  723. # define MAXDISTS 30
  724. #endif
  725. /* moved to consts.h (included in unzip.c), resp. funzip.c */
  726. #if 0
  727. /* And'ing with mask_bits[n] masks the lower n bits */
  728. ZCONST unsigned near mask_bits[17] = {
  729. 0x0000,
  730. 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
  731. 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
  732. };
  733. #endif /* 0 */
  734. /* Macros for inflate() bit peeking and grabbing.
  735. The usage is:
  736. NEEDBITS(j)
  737. x = b & mask_bits[j];
  738. DUMPBITS(j)
  739. where NEEDBITS makes sure that b has at least j bits in it, and
  740. DUMPBITS removes the bits from b. The macros use the variable k
  741. for the number of bits in b. Normally, b and k are register
  742. variables for speed and are initialized at the beginning of a
  743. routine that uses these macros from a global bit buffer and count.
  744. In order to not ask for more bits than there are in the compressed
  745. stream, the Huffman tables are constructed to only ask for just
  746. enough bits to make up the end-of-block code (value 256). Then no
  747. bytes need to be "returned" to the buffer at the end of the last
  748. block. See the huft_build() routine.
  749. Actually, the precautions mentioned above are not sufficient to
  750. prevent fetches of bits beyound the end of the last block in every
  751. case. When the last code fetched before the end-of-block code was
  752. a very short distance code (shorter than "distance-prefetch-bits" -
  753. "end-of-block code bits"), this last distance code fetch already
  754. exausts the available data. To prevent failure of extraction in this
  755. case, the "read beyond EOF" check delays the raise of the "invalid
  756. data" error until an actual overflow of "used data" is detected.
  757. This error condition is only fulfilled when the "number of available
  758. bits" counter k is found to be negative in the NEEDBITS() macro.
  759. An alternate fix for that problem adjusts the size of the distance code
  760. base table so that it does not exceed the length of the end-of-block code
  761. plus the minimum length of a distance code. This alternate fix can be
  762. enabled by defining the preprocessor symbol FIX_PAST_EOB_BY_TABLEADJUST.
  763. */
  764. /* These have been moved to globals.h */
  765. #if 0
  766. ulg bb; /* bit buffer */
  767. unsigned bk; /* bits in bit buffer */
  768. #endif
  769. #ifndef CHECK_EOF
  770. # define CHECK_EOF /* default as of 5.13/5.2 */
  771. #endif
  772. #ifndef CHECK_EOF
  773. # define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE)<<k;k+=8;}}
  774. #else
  775. # ifdef FIX_PAST_EOB_BY_TABLEADJUST
  776. # define NEEDBITS(n) {while(k<(n)){int c=NEXTBYTE;\
  777. if(c==EOF){retval=1;goto cleanup_and_exit;}\
  778. b|=((ulg)c)<<k;k+=8;}}
  779. # else
  780. # define NEEDBITS(n) {while((int)k<(int)(n)){int c=NEXTBYTE;\
  781. if(c==EOF){if((int)k>=0)break;retval=1;goto cleanup_and_exit;}\
  782. b|=((ulg)c)<<k;k+=8;}}
  783. # endif
  784. #endif
  785. #define DUMPBITS(n) {b>>=(n);k-=(n);}
  786. /*
  787. Huffman code decoding is performed using a multi-level table lookup.
  788. The fastest way to decode is to simply build a lookup table whose
  789. size is determined by the longest code. However, the time it takes
  790. to build this table can also be a factor if the data being decoded
  791. are not very long. The most common codes are necessarily the
  792. shortest codes, so those codes dominate the decoding time, and hence
  793. the speed. The idea is you can have a shorter table that decodes the
  794. shorter, more probable codes, and then point to subsidiary tables for
  795. the longer codes. The time it costs to decode the longer codes is
  796. then traded against the time it takes to make longer tables.
  797. This results of this trade are in the variables lbits and dbits
  798. below. lbits is the number of bits the first level table for literal/
  799. length codes can decode in one step, and dbits is the same thing for
  800. the distance codes. Subsequent tables are also less than or equal to
  801. those sizes. These values may be adjusted either when all of the
  802. codes are shorter than that, in which case the longest code length in
  803. bits is used, or when the shortest code is *longer* than the requested
  804. table size, in which case the length of the shortest code in bits is
  805. used.
  806. There are two different values for the two tables, since they code a
  807. different number of possibilities each. The literal/length table
  808. codes 286 possible values, or in a flat code, a little over eight
  809. bits. The distance table codes 30 possible values, or a little less
  810. than five bits, flat. The optimum values for speed end up being
  811. about one bit more than those, so lbits is 8+1 and dbits is 5+1.
  812. The optimum values may differ though from machine to machine, and
  813. possibly even between compilers. Your mileage may vary.
  814. */
  815. /* bits in base literal/length lookup table */
  816. static ZCONST unsigned lbits = 9;
  817. /* bits in base distance lookup table */
  818. static ZCONST unsigned dbits = 6;
  819. #ifndef ASM_INFLATECODES
  820. int inflate_codes(__G__ tl, td, bl, bd)
  821. __GDEF
  822. struct huft *tl, *td; /* literal/length and distance decoder tables */
  823. unsigned bl, bd; /* number of bits decoded by tl[] and td[] */
  824. /* inflate (decompress) the codes in a deflated (compressed) block.
  825. Return an error code or zero if it all goes ok. */
  826. {
  827. register unsigned e; /* table entry flag/number of extra bits */
  828. unsigned d; /* index for copy */
  829. UINT_D64 n; /* length for copy (deflate64: might be 64k+2) */
  830. UINT_D64 w; /* current window position (deflate64: up to 64k) */
  831. struct huft *t; /* pointer to table entry */
  832. unsigned ml, md; /* masks for bl and bd bits */
  833. register ulg b; /* bit buffer */
  834. register unsigned k; /* number of bits in bit buffer */
  835. int retval = 0; /* error code returned: initialized to "no error" */
  836. /* make local copies of globals */
  837. b = G.bb; /* initialize bit buffer */
  838. k = G.bk;
  839. w = G.wp; /* initialize window position */
  840. /* inflate the coded data */
  841. ml = mask_bits[bl]; /* precompute masks for speed */
  842. md = mask_bits[bd];
  843. while (1) /* do until end of block */
  844. {
  845. NEEDBITS(bl)
  846. t = tl + ((unsigned)b & ml);
  847. while (1) {
  848. DUMPBITS(t->b)
  849. if ((e = t->e) == 32) /* then it's a literal */
  850. {
  851. redirSlide[w++] = (uch)t->v.n;
  852. if (w == wsize)
  853. {
  854. if ((retval = FLUSH(w)) != 0) goto cleanup_and_exit;
  855. w = 0;
  856. }
  857. break;
  858. }
  859. if (e < 31) /* then it's a length */
  860. {
  861. /* get length of block to copy */
  862. NEEDBITS(e)
  863. n = t->v.n + ((unsigned)b & mask_bits[e]);
  864. DUMPBITS(e)
  865. /* decode distance of block to copy */
  866. NEEDBITS(bd)
  867. t = td + ((unsigned)b & md);
  868. while (1) {
  869. DUMPBITS(t->b)
  870. if ((e = t->e) < 32)
  871. break;
  872. if (IS_INVALID_CODE(e))
  873. return 1;
  874. e &= 31;
  875. NEEDBITS(e)
  876. t = t->v.t + ((unsigned)b & mask_bits[e]);
  877. }
  878. NEEDBITS(e)
  879. d = (unsigned)w - t->v.n - ((unsigned)b & mask_bits[e]);
  880. DUMPBITS(e)
  881. /* do the copy */
  882. do {
  883. #if (defined(DLL) && !defined(NO_SLIDE_REDIR))
  884. if (G.redirect_slide) {
  885. /* &= w/ wsize unnecessary & wrong if redirect */
  886. if ((UINT_D64)d >= wsize)
  887. return 1; /* invalid compressed data */
  888. e = (unsigned)(wsize - (d > (unsigned)w ? (UINT_D64)d : w));
  889. }
  890. else
  891. #endif
  892. e = (unsigned)(wsize -
  893. ((d &= (unsigned)(wsize-1)) > (unsigned)w ?
  894. (UINT_D64)d : w));
  895. if ((UINT_D64)e > n) e = (unsigned)n;
  896. n -= e;
  897. #ifndef NOMEMCPY
  898. if ((unsigned)w - d >= e)
  899. /* (this test assumes unsigned comparison) */
  900. {
  901. memcpy(redirSlide + (unsigned)w, redirSlide + d, e);
  902. w += e;
  903. d += e;
  904. }
  905. else /* do it slowly to avoid memcpy() overlap */
  906. #endif /* !NOMEMCPY */
  907. do {
  908. redirSlide[w++] = redirSlide[d++];
  909. } while (--e);
  910. if (w == wsize)
  911. {
  912. if ((retval = FLUSH(w)) != 0) goto cleanup_and_exit;
  913. w = 0;
  914. }
  915. } while (n);
  916. break;
  917. }
  918. if (e == 31) /* it's the EOB signal */
  919. {
  920. /* sorry for this goto, but we have to exit two loops at once */
  921. goto cleanup_decode;
  922. }
  923. if (IS_INVALID_CODE(e))
  924. return 1;
  925. e &= 31;
  926. NEEDBITS(e)
  927. t = t->v.t + ((unsigned)b & mask_bits[e]);
  928. }
  929. }
  930. cleanup_decode:
  931. /* restore the globals from the locals */
  932. G.wp = (unsigned)w; /* restore global window pointer */
  933. G.bb = b; /* restore global bit buffer */
  934. G.bk = k;
  935. cleanup_and_exit:
  936. /* done */
  937. return retval;
  938. }
  939. #endif /* ASM_INFLATECODES */
  940. static int inflate_stored(__G)
  941. __GDEF
  942. /* "decompress" an inflated type 0 (stored) block. */
  943. {
  944. UINT_D64 w; /* current window position (deflate64: up to 64k!) */
  945. unsigned n; /* number of bytes in block */
  946. register ulg b; /* bit buffer */
  947. register unsigned k; /* number of bits in bit buffer */
  948. int retval = 0; /* error code returned: initialized to "no error" */
  949. /* make local copies of globals */
  950. Trace((stderr, "\nstored block"));
  951. b = G.bb; /* initialize bit buffer */
  952. k = G.bk;
  953. w = G.wp; /* initialize window position */
  954. /* go to byte boundary */
  955. n = k & 7;
  956. DUMPBITS(n);
  957. /* get the length and its complement */
  958. NEEDBITS(16)
  959. n = ((unsigned)b & 0xffff);
  960. DUMPBITS(16)
  961. NEEDBITS(16)
  962. if (n != (unsigned)((~b) & 0xffff))
  963. return 1; /* error in compressed data */
  964. DUMPBITS(16)
  965. /* read and output the compressed data */
  966. while (n--)
  967. {
  968. NEEDBITS(8)
  969. redirSlide[w++] = (uch)b;
  970. if (w == wsize)
  971. {
  972. if ((retval = FLUSH(w)) != 0) goto cleanup_and_exit;
  973. w = 0;
  974. }
  975. DUMPBITS(8)
  976. }
  977. /* restore the globals from the locals */
  978. G.wp = (unsigned)w; /* restore global window pointer */
  979. G.bb = b; /* restore global bit buffer */
  980. G.bk = k;
  981. cleanup_and_exit:
  982. return retval;
  983. }
  984. /* Globals for literal tables (built once) */
  985. /* Moved to globals.h */
  986. #if 0
  987. struct huft *fixed_tl = (struct huft *)NULL;
  988. struct huft *fixed_td;
  989. int fixed_bl, fixed_bd;
  990. #endif
  991. static int inflate_fixed(__G)
  992. __GDEF
  993. /* decompress an inflated type 1 (fixed Huffman codes) block. We should
  994. either replace this with a custom decoder, or at least precompute the
  995. Huffman tables. */
  996. {
  997. /* if first time, set up tables for fixed blocks */
  998. Trace((stderr, "\nliteral block"));
  999. if (G.fixed_tl == (struct huft *)NULL)
  1000. {
  1001. int i; /* temporary variable */
  1002. unsigned l[288]; /* length list for huft_build */
  1003. /* literal table */
  1004. for (i = 0; i < 144; i++)
  1005. l[i] = 8;
  1006. for (; i < 256; i++)
  1007. l[i] = 9;
  1008. for (; i < 280; i++)
  1009. l[i] = 7;
  1010. for (; i < 288; i++) /* make a complete, but wrong code set */
  1011. l[i] = 8;
  1012. G.fixed_bl = 7;
  1013. #ifdef USE_DEFLATE64
  1014. if ((i = huft_build(__G__ l, 288, 257, G.cplens, G.cplext,
  1015. &G.fixed_tl, &G.fixed_bl)) != 0)
  1016. #else
  1017. if ((i = huft_build(__G__ l, 288, 257, cplens, cplext,
  1018. &G.fixed_tl, &G.fixed_bl)) != 0)
  1019. #endif
  1020. {
  1021. G.fixed_tl = (struct huft *)NULL;
  1022. return i;
  1023. }
  1024. /* distance table */
  1025. for (i = 0; i < MAXDISTS; i++) /* make an incomplete code set */
  1026. l[i] = 5;
  1027. G.fixed_bd = 5;
  1028. #ifdef USE_DEFLATE64
  1029. if ((i = huft_build(__G__ l, MAXDISTS, 0, cpdist, G.cpdext,
  1030. &G.fixed_td, &G.fixed_bd)) > 1)
  1031. #else
  1032. if ((i = huft_build(__G__ l, MAXDISTS, 0, cpdist, cpdext,
  1033. &G.fixed_td, &G.fixed_bd)) > 1)
  1034. #endif
  1035. {
  1036. huft_free(G.fixed_tl);
  1037. G.fixed_td = G.fixed_tl = (struct huft *)NULL;
  1038. return i;
  1039. }
  1040. }
  1041. /* decompress until an end-of-block code */
  1042. return inflate_codes(__G__ G.fixed_tl, G.fixed_td,
  1043. G.fixed_bl, G.fixed_bd);
  1044. }
  1045. static int inflate_dynamic(__G)
  1046. __GDEF
  1047. /* decompress an inflated type 2 (dynamic Huffman codes) block. */
  1048. {
  1049. unsigned i; /* temporary variables */
  1050. unsigned j;
  1051. unsigned l; /* last length */
  1052. unsigned m; /* mask for bit lengths table */
  1053. unsigned n; /* number of lengths to get */
  1054. struct huft *tl = (struct huft *)NULL; /* literal/length code table */
  1055. struct huft *td = (struct huft *)NULL; /* distance code table */
  1056. struct huft *th; /* temp huft table pointer used in tables decoding */
  1057. unsigned bl; /* lookup bits for tl */
  1058. unsigned bd; /* lookup bits for td */
  1059. unsigned nb; /* number of bit length codes */
  1060. unsigned nl; /* number of literal/length codes */
  1061. unsigned nd; /* number of distance codes */
  1062. unsigned ll[MAXLITLENS+MAXDISTS]; /* lit./length and distance code lengths */
  1063. register ulg b; /* bit buffer */
  1064. register unsigned k; /* number of bits in bit buffer */
  1065. int retval = 0; /* error code returned: initialized to "no error" */
  1066. /* make local bit buffer */
  1067. Trace((stderr, "\ndynamic block"));
  1068. b = G.bb;
  1069. k = G.bk;
  1070. /* read in table lengths */
  1071. NEEDBITS(5)
  1072. nl = 257 + ((unsigned)b & 0x1f); /* number of literal/length codes */
  1073. DUMPBITS(5)
  1074. NEEDBITS(5)
  1075. nd = 1 + ((unsigned)b & 0x1f); /* number of distance codes */
  1076. DUMPBITS(5)
  1077. NEEDBITS(4)
  1078. nb = 4 + ((unsigned)b & 0xf); /* number of bit length codes */
  1079. DUMPBITS(4)
  1080. if (nl > MAXLITLENS || nd > MAXDISTS)
  1081. return 1; /* bad lengths */
  1082. /* read in bit-length-code lengths */
  1083. for (j = 0; j < nb; j++)
  1084. {
  1085. NEEDBITS(3)
  1086. ll[border[j]] = (unsigned)b & 7;
  1087. DUMPBITS(3)
  1088. }
  1089. for (; j < 19; j++)
  1090. ll[border[j]] = 0;
  1091. /* build decoding table for trees--single level, 7 bit lookup */
  1092. bl = 7;
  1093. retval = huft_build(__G__ ll, 19, 19, NULL, NULL, &tl, &bl);
  1094. if (bl == 0) /* no bit lengths */
  1095. retval = 1;
  1096. if (retval)
  1097. {
  1098. if (retval == 1)
  1099. huft_free(tl);
  1100. return retval; /* incomplete code set */
  1101. }
  1102. /* read in literal and distance code lengths */
  1103. n = nl + nd;
  1104. m = mask_bits[bl];
  1105. i = l = 0;
  1106. while (i < n)
  1107. {
  1108. NEEDBITS(bl)
  1109. j = (th = tl + ((unsigned)b & m))->b;
  1110. DUMPBITS(j)
  1111. j = th->v.n;
  1112. if (j < 16) /* length of code in bits (0..15) */
  1113. ll[i++] = l = j; /* save last length in l */
  1114. else if (j == 16) /* repeat last length 3 to 6 times */
  1115. {
  1116. NEEDBITS(2)
  1117. j = 3 + ((unsigned)b & 3);
  1118. DUMPBITS(2)
  1119. if ((unsigned)i + j > n) {
  1120. huft_free(tl);
  1121. return 1;
  1122. }
  1123. while (j--)
  1124. ll[i++] = l;
  1125. }
  1126. else if (j == 17) /* 3 to 10 zero length codes */
  1127. {
  1128. NEEDBITS(3)
  1129. j = 3 + ((unsigned)b & 7);
  1130. DUMPBITS(3)
  1131. if ((unsigned)i + j > n) {
  1132. huft_free(tl);
  1133. return 1;
  1134. }
  1135. while (j--)
  1136. ll[i++] = 0;
  1137. l = 0;
  1138. }
  1139. else /* j == 18: 11 to 138 zero length codes */
  1140. {
  1141. NEEDBITS(7)
  1142. j = 11 + ((unsigned)b & 0x7f);
  1143. DUMPBITS(7)
  1144. if ((unsigned)i + j > n) {
  1145. huft_free(tl);
  1146. return 1;
  1147. }
  1148. while (j--)
  1149. ll[i++] = 0;
  1150. l = 0;
  1151. }
  1152. }
  1153. /* free decoding table for trees */
  1154. huft_free(tl);
  1155. /* restore the global bit buffer */
  1156. G.bb = b;
  1157. G.bk = k;
  1158. /* build the decoding tables for literal/length and distance codes */
  1159. bl = lbits;
  1160. #ifdef USE_DEFLATE64
  1161. retval = huft_build(__G__ ll, nl, 257, G.cplens, G.cplext, &tl, &bl);
  1162. #else
  1163. retval = huft_build(__G__ ll, nl, 257, cplens, cplext, &tl, &bl);
  1164. #endif
  1165. if (bl == 0) /* no literals or lengths */
  1166. retval = 1;
  1167. if (retval)
  1168. {
  1169. if (retval == 1) {
  1170. if (!uO.qflag)
  1171. MESSAGE((uch *)"(incomplete l-tree) ", 21L, 1);
  1172. huft_free(tl);
  1173. }
  1174. return retval; /* incomplete code set */
  1175. }
  1176. #ifdef FIX_PAST_EOB_BY_TABLEADJUST
  1177. /* Adjust the requested distance base table size so that a distance code
  1178. fetch never tries to get bits behind an immediatly following end-of-block
  1179. code. */
  1180. bd = (dbits <= bl+1 ? dbits : bl+1);
  1181. #else
  1182. bd = dbits;
  1183. #endif
  1184. #ifdef USE_DEFLATE64
  1185. retval = huft_build(__G__ ll + nl, nd, 0, cpdist, G.cpdext, &td, &bd);
  1186. #else
  1187. retval = huft_build(__G__ ll + nl, nd, 0, cpdist, cpdext, &td, &bd);
  1188. #endif
  1189. #ifdef PKZIP_BUG_WORKAROUND
  1190. if (retval == 1)
  1191. retval = 0;
  1192. #endif
  1193. if (bd == 0 && nl > 257) /* lengths but no distances */
  1194. retval = 1;
  1195. if (retval)
  1196. {
  1197. if (retval == 1) {
  1198. if (!uO.qflag)
  1199. MESSAGE((uch *)"(incomplete d-tree) ", 21L, 1);
  1200. huft_free(td);
  1201. }
  1202. huft_free(tl);
  1203. return retval;
  1204. }
  1205. /* decompress until an end-of-block code */
  1206. retval = inflate_codes(__G__ tl, td, bl, bd);
  1207. cleanup_and_exit:
  1208. /* free the decoding tables, return */
  1209. if (tl != (struct huft *)NULL)
  1210. huft_free(tl);
  1211. if (td != (struct huft *)NULL)
  1212. huft_free(td);
  1213. return retval;
  1214. }
  1215. static int inflate_block(__G__ e)
  1216. __GDEF
  1217. int *e; /* last block flag */
  1218. /* decompress an inflated block */
  1219. {
  1220. unsigned t; /* block type */
  1221. register ulg b; /* bit buffer */
  1222. register unsigned k; /* number of bits in bit buffer */
  1223. int retval = 0; /* error code returned: initialized to "no error" */
  1224. /* make local bit buffer */
  1225. b = G.bb;
  1226. k = G.bk;
  1227. /* read in last block bit */
  1228. NEEDBITS(1)
  1229. *e = (int)b & 1;
  1230. DUMPBITS(1)
  1231. /* read in block type */
  1232. NEEDBITS(2)
  1233. t = (unsigned)b & 3;
  1234. DUMPBITS(2)
  1235. /* restore the global bit buffer */
  1236. G.bb = b;
  1237. G.bk = k;
  1238. /* inflate that block type */
  1239. if (t == 2)
  1240. return inflate_dynamic(__G);
  1241. if (t == 0)
  1242. return inflate_stored(__G);
  1243. if (t == 1)
  1244. return inflate_fixed(__G);
  1245. /* bad block type */
  1246. retval = 2;
  1247. cleanup_and_exit:
  1248. return retval;
  1249. }
  1250. int inflate(__G__ is_defl64)
  1251. __GDEF
  1252. int is_defl64;
  1253. /* decompress an inflated entry */
  1254. {
  1255. int e; /* last block flag */
  1256. int r; /* result code */
  1257. #ifdef DEBUG
  1258. unsigned h = 0; /* maximum struct huft's malloc'ed */
  1259. #endif
  1260. #if (defined(DLL) && !defined(NO_SLIDE_REDIR))
  1261. if (G.redirect_slide)
  1262. wsize = G.redirect_size, redirSlide = G.redirect_buffer;
  1263. else
  1264. wsize = WSIZE, redirSlide = slide; /* how they're #defined if !DLL */
  1265. #endif
  1266. /* initialize window, bit buffer */
  1267. G.wp = 0;
  1268. G.bk = 0;
  1269. G.bb = 0;
  1270. #ifdef USE_DEFLATE64
  1271. if (is_defl64) {
  1272. G.cplens = cplens64;
  1273. G.cplext = cplext64;
  1274. G.cpdext = cpdext64;
  1275. G.fixed_tl = G.fixed_tl64;
  1276. G.fixed_bl = G.fixed_bl64;
  1277. G.fixed_td = G.fixed_td64;
  1278. G.fixed_bd = G.fixed_bd64;
  1279. } else {
  1280. G.cplens = cplens32;
  1281. G.cplext = cplext32;
  1282. G.cpdext = cpdext32;
  1283. G.fixed_tl = G.fixed_tl32;
  1284. G.fixed_bl = G.fixed_bl32;
  1285. G.fixed_td = G.fixed_td32;
  1286. G.fixed_bd = G.fixed_bd32;
  1287. }
  1288. #else /* !USE_DEFLATE64 */
  1289. if (is_defl64) {
  1290. /* This should not happen unless UnZip is built from object files
  1291. * compiled with inconsistent option setting. Handle this by
  1292. * returning with "bad input" error code.
  1293. */
  1294. Trace((stderr, "\nThis inflate() cannot handle Deflate64!\n"));
  1295. return 2;
  1296. }
  1297. #endif /* ?USE_DEFLATE64 */
  1298. /* decompress until the last block */
  1299. do {
  1300. #ifdef DEBUG
  1301. G.hufts = 0;
  1302. #endif
  1303. if ((r = inflate_block(__G__ &e)) != 0)
  1304. return r;
  1305. #ifdef DEBUG
  1306. if (G.hufts > h)
  1307. h = G.hufts;
  1308. #endif
  1309. } while (!e);
  1310. Trace((stderr, "\n%u bytes in Huffman tables (%u/entry)\n",
  1311. h * (unsigned)sizeof(struct huft), (unsigned)sizeof(struct huft)));
  1312. #ifdef USE_DEFLATE64
  1313. if (is_defl64) {
  1314. G.fixed_tl64 = G.fixed_tl;
  1315. G.fixed_bl64 = G.fixed_bl;
  1316. G.fixed_td64 = G.fixed_td;
  1317. G.fixed_bd64 = G.fixed_bd;
  1318. } else {
  1319. G.fixed_tl32 = G.fixed_tl;
  1320. G.fixed_bl32 = G.fixed_bl;
  1321. G.fixed_td32 = G.fixed_td;
  1322. G.fixed_bd32 = G.fixed_bd;
  1323. }
  1324. #endif
  1325. /* flush out redirSlide and return (success, unless final FLUSH failed) */
  1326. return (FLUSH(G.wp));
  1327. }
  1328. int inflate_free(__G)
  1329. __GDEF
  1330. {
  1331. if (G.fixed_tl != (struct huft *)NULL)
  1332. {
  1333. huft_free(G.fixed_td);
  1334. huft_free(G.fixed_tl);
  1335. G.fixed_td = G.fixed_tl = (struct huft *)NULL;
  1336. }
  1337. return 0;
  1338. }
  1339. #endif /* ?USE_ZLIB */
  1340. /*
  1341. * GRR: moved huft_build() and huft_free() down here; used by explode()
  1342. * and fUnZip regardless of whether USE_ZLIB defined or not
  1343. */
  1344. /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
  1345. #define BMAX 16 /* maximum bit length of any code (16 for explode) */
  1346. #define N_MAX 288 /* maximum number of codes in any set */
  1347. int huft_build(__G__ b, n, s, d, e, t, m)
  1348. __GDEF
  1349. ZCONST unsigned *b; /* code lengths in bits (all assumed <= BMAX) */
  1350. unsigned n; /* number of codes (assumed <= N_MAX) */
  1351. unsigned s; /* number of simple-valued codes (0..s-1) */
  1352. ZCONST ush *d; /* list of base values for non-simple codes */
  1353. ZCONST uch *e; /* list of extra bits for non-simple codes */
  1354. struct huft **t; /* result: starting table */
  1355. unsigned *m; /* maximum lookup bits, returns actual */
  1356. /* Given a list of code lengths and a maximum table size, make a set of
  1357. tables to decode that set of codes. Return zero on success, one if
  1358. the given code set is incomplete (the tables are still built in this
  1359. case), two if the input is invalid (all zero length codes or an
  1360. oversubscribed set of lengths), and three if not enough memory.
  1361. The code with value 256 is special, and the tables are constructed
  1362. so that no bits beyond that code are fetched when that code is
  1363. decoded. */
  1364. {
  1365. unsigned a; /* counter for codes of length k */
  1366. unsigned c[BMAX+1]; /* bit length count table */
  1367. unsigned el; /* length of EOB code (value 256) */
  1368. unsigned f; /* i repeats in table every f entries */
  1369. int g; /* maximum code length */
  1370. int h; /* table level */
  1371. register unsigned i; /* counter, current code */
  1372. register unsigned j; /* counter */
  1373. register int k; /* number of bits in current code */
  1374. int lx[BMAX+1]; /* memory for l[-1..BMAX-1] */
  1375. int *l = lx+1; /* stack of bits per table */
  1376. register unsigned *p; /* pointer into c[], b[], or v[] */
  1377. register struct huft *q; /* points to current table */
  1378. struct huft r; /* table entry for structure assignment */
  1379. struct huft *u[BMAX]; /* table stack */
  1380. unsigned v[N_MAX]; /* values in order of bit length */
  1381. register int w; /* bits before this table == (l * h) */
  1382. unsigned x[BMAX+1]; /* bit offsets, then code stack */
  1383. unsigned *xp; /* pointer into x */
  1384. int y; /* number of dummy codes added */
  1385. unsigned z; /* number of entries in current table */
  1386. /* Generate counts for each bit length */
  1387. el = n > 256 ? b[256] : BMAX; /* set length of EOB code, if any */
  1388. memzero((char *)c, sizeof(c));
  1389. p = (unsigned *)b; i = n;
  1390. do {
  1391. c[*p]++; p++; /* assume all entries <= BMAX */
  1392. } while (--i);
  1393. if (c[0] == n) /* null input--all zero length codes */
  1394. {
  1395. *t = (struct huft *)NULL;
  1396. *m = 0;
  1397. return 0;
  1398. }
  1399. /* Find minimum and maximum length, bound *m by those */
  1400. for (j = 1; j <= BMAX; j++)
  1401. if (c[j])
  1402. break;
  1403. k = j; /* minimum code length */
  1404. if (*m < j)
  1405. *m = j;
  1406. for (i = BMAX; i; i--)
  1407. if (c[i])
  1408. break;
  1409. g = i; /* maximum code length */
  1410. if (*m > i)
  1411. *m = i;
  1412. /* Adjust last length count to fill out codes, if needed */
  1413. for (y = 1 << j; j < i; j++, y <<= 1)
  1414. if ((y -= c[j]) < 0)
  1415. return 2; /* bad input: more codes than bits */
  1416. if ((y -= c[i]) < 0)
  1417. return 2;
  1418. c[i] += y;
  1419. /* Generate starting offsets into the value table for each length */
  1420. x[1] = j = 0;
  1421. p = c + 1; xp = x + 2;
  1422. while (--i) { /* note that i == g from above */
  1423. *xp++ = (j += *p++);
  1424. }
  1425. /* Make a table of values in order of bit lengths */
  1426. memzero((char *)v, sizeof(v));
  1427. p = (unsigned *)b; i = 0;
  1428. do {
  1429. if ((j = *p++) != 0)
  1430. v[x[j]++] = i;
  1431. } while (++i < n);
  1432. n = x[g]; /* set n to length of v */
  1433. /* Generate the Huffman codes and for each, make the table entries */
  1434. x[0] = i = 0; /* first Huffman code is zero */
  1435. p = v; /* grab values in bit order */
  1436. h = -1; /* no tables yet--level -1 */
  1437. w = l[-1] = 0; /* no bits decoded yet */
  1438. u[0] = (struct huft *)NULL; /* just to keep compilers happy */
  1439. q = (struct huft *)NULL; /* ditto */
  1440. z = 0; /* ditto */
  1441. /* go through the bit lengths (k already is bits in shortest code) */
  1442. for (; k <= g; k++)
  1443. {
  1444. a = c[k];
  1445. while (a--)
  1446. {
  1447. /* here i is the Huffman code of length k bits for value *p */
  1448. /* make tables up to required level */
  1449. while (k > w + l[h])
  1450. {
  1451. w += l[h++]; /* add bits already decoded */
  1452. /* compute minimum size table less than or equal to *m bits */
  1453. z = (z = g - w) > *m ? *m : z; /* upper limit */
  1454. if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
  1455. { /* too few codes for k-w bit table */
  1456. f -= a + 1; /* deduct codes from patterns left */
  1457. xp = c + k;
  1458. while (++j < z) /* try smaller tables up to z bits */
  1459. {
  1460. if ((f <<= 1) <= *++xp)
  1461. break; /* enough codes to use up j bits */
  1462. f -= *xp; /* else deduct codes from patterns */
  1463. }
  1464. }
  1465. if ((unsigned)w + j > el && (unsigned)w < el)
  1466. j = el - w; /* make EOB code end at table */
  1467. z = 1 << j; /* table entries for j-bit table */
  1468. l[h] = j; /* set table size in stack */
  1469. /* allocate and link in new table */
  1470. if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) ==
  1471. (struct huft *)NULL)
  1472. {
  1473. if (h)
  1474. huft_free(u[0]);
  1475. return 3; /* not enough memory */
  1476. }
  1477. #ifdef DEBUG
  1478. G.hufts += z + 1; /* track memory usage */
  1479. #endif
  1480. *t = q + 1; /* link to list for huft_free() */
  1481. *(t = &(q->v.t)) = (struct huft *)NULL;
  1482. u[h] = ++q; /* table starts after link */
  1483. /* connect to last table, if there is one */
  1484. if (h)
  1485. {
  1486. x[h] = i; /* save pattern for backing up */
  1487. r.b = (uch)l[h-1]; /* bits to dump before this table */
  1488. r.e = (uch)(32 + j); /* bits in this table */
  1489. r.v.t = q; /* pointer to this table */
  1490. j = (i & ((1 << w) - 1)) >> (w - l[h-1]);
  1491. u[h-1][j] = r; /* connect to last table */
  1492. }
  1493. }
  1494. /* set up table entry in r */
  1495. r.b = (uch)(k - w);
  1496. if (p >= v + n)
  1497. r.e = INVALID_CODE; /* out of values--invalid code */
  1498. else if (*p < s)
  1499. {
  1500. r.e = (uch)(*p < 256 ? 32 : 31); /* 256 is end-of-block code */
  1501. r.v.n = (ush)*p++; /* simple code is just the value */
  1502. }
  1503. else
  1504. {
  1505. r.e = e[*p - s]; /* non-simple--look up in lists */
  1506. r.v.n = d[*p++ - s];
  1507. }
  1508. /* fill code-like entries with r */
  1509. f = 1 << (k - w);
  1510. for (j = i >> w; j < z; j += f)
  1511. q[j] = r;
  1512. /* backwards increment the k-bit code i */
  1513. for (j = 1 << (k - 1); i & j; j >>= 1)
  1514. i ^= j;
  1515. i ^= j;
  1516. /* backup over finished tables */
  1517. while ((i & ((1 << w) - 1)) != x[h])
  1518. w -= l[--h]; /* don't need to update q */
  1519. }
  1520. }
  1521. /* return actual size of base table */
  1522. *m = l[0];
  1523. /* Return true (1) if we were given an incomplete table */
  1524. return y != 0 && g != 1;
  1525. }
  1526. int huft_free(t)
  1527. struct huft *t; /* table to free */
  1528. /* Free the malloc'ed tables built by huft_build(), which makes a linked
  1529. list of the tables it made, with the links in a dummy first entry of
  1530. each table. */
  1531. {
  1532. register struct huft *p, *q;
  1533. /* Go through linked list, freeing from the malloced (t[-1]) address. */
  1534. p = t;
  1535. while (p != (struct huft *)NULL)
  1536. {
  1537. q = (--p)->v.t;
  1538. free((zvoid *)p);
  1539. p = q;
  1540. }
  1541. return 0;
  1542. }