home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume44 / unzip / part09 < prev    next >
Internet Message Format  |  1994-09-19  |  71KB

  1. From: zip-bugs@wkuvx1.wku.edu (Info-ZIP group)
  2. Newsgroups: comp.sources.misc
  3. Subject: v44i074:  unzip - Info-ZIP portable UnZip, version 5.12, Part09/20
  4. Date: 18 Sep 1994 23:15:32 -0500
  5. Organization: Sterling Software
  6. Sender: kent@sparky.sterling.com
  7. Approved: kent@sparky.sterling.com
  8. Message-ID: <35j394$qoc@sparky.sterling.com>
  9. X-Md4-Signature: 2460b5521dcd2142b00bedcf80f343dc
  10.  
  11. Submitted-by: zip-bugs@wkuvx1.wku.edu (Info-ZIP group)
  12. Posting-number: Volume 44, Issue 74
  13. Archive-name: unzip/part09
  14. Environment: UNIX, VMS, OS/2, MS-DOS, MACINTOSH, WIN-NT, LINUX, MINIX, COHERENT, AMIGA?, ATARI TOS, SGI, DEC, Cray, Convex, Amdahl, Sun
  15. Supersedes: unzip50: Volume 31, Issue 104-117
  16.  
  17. #! /bin/sh
  18. # This is a shell archive.  Remove anything before this line, then feed it
  19. # into a shell via "sh file" or similar.  To overwrite existing files,
  20. # type "sh file -c".
  21. # Contents:  unzip-5.12/explode.c unzip-5.12/inflate.c
  22. # Wrapped by kent@sparky on Sat Sep 17 23:33:40 1994
  23. PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin:$PATH ; export PATH
  24. echo If this archive is complete, you will see the following message:
  25. echo '          "shar: End of archive 9 (of 20)."'
  26. if test -f 'unzip-5.12/explode.c' -a "${1}" != "-c" ; then 
  27.   echo shar: Will not clobber existing file \"'unzip-5.12/explode.c'\"
  28. else
  29.   echo shar: Extracting \"'unzip-5.12/explode.c'\" \(28523 characters\)
  30.   sed "s/^X//" >'unzip-5.12/explode.c' <<'END_OF_FILE'
  31. X/* explode.c -- put in the public domain by Mark Adler
  32. X   version c13, 25 August 1994 */
  33. X
  34. X
  35. X/* You can do whatever you like with this source file, though I would
  36. X   prefer that if you modify it and redistribute it that you include
  37. X   comments to that effect with your name and the date.  Thank you.
  38. X
  39. X   History:
  40. X   vers    date          who           what
  41. X   ----  ---------  --------------  ------------------------------------
  42. X    c1   30 Mar 92  M. Adler        explode that uses huft_build from inflate
  43. X                                    (this gives over a 70% speed improvement
  44. X                                    over the original unimplode.c, which
  45. X                                    decoded a bit at a time)
  46. X    c2    4 Apr 92  M. Adler        fixed bug for file sizes a multiple of 32k.
  47. X    c3   10 Apr 92  M. Adler        added a little memory tracking if DEBUG
  48. X    c4   11 Apr 92  M. Adler        added NOMEMCPY do kill use of memcpy()
  49. X    c5   21 Apr 92  M. Adler        added the WSIZE #define to allow reducing
  50. X                                    the 32K window size for specialized
  51. X                                    applications.
  52. X    c6   31 May 92  M. Adler        added typecasts to eliminate some warnings
  53. X    c7   27 Jun 92  G. Roelofs      added more typecasts.
  54. X    c8   17 Oct 92  G. Roelofs      changed ULONG/UWORD/byte to ulg/ush/uch.
  55. X    c9   19 Jul 93  J. Bush         added more typecasts (to return values);
  56. X                                    made l[256] array static for Amiga.
  57. X    c10   8 Oct 93  G. Roelofs      added used_csize for diagnostics; added
  58. X                                    buf and unshrink arguments to flush();
  59. X                                    undef'd various macros at end for Turbo C;
  60. X                                    removed NEXTBYTE macro (now in unzip.h)
  61. X                                    and bytebuf variable (not used); changed
  62. X                                    memset() to memzero().
  63. X    c11   9 Jan 94  M. Adler        fixed incorrect used_csize calculation.
  64. X    c12   9 Apr 94  G. Roelofs      fixed split comments on preprocessor lines
  65. X                                    to avoid bug in Encore compiler.
  66. X    c13  25 Aug 94  M. Adler        fixed distance-length comment (orig c9 fix)
  67. X */
  68. X
  69. X
  70. X/*
  71. X   Explode imploded (PKZIP method 6 compressed) data.  This compression
  72. X   method searches for as much of the current string of bytes (up to a length
  73. X   of ~320) in the previous 4K or 8K bytes.  If it doesn't find any matches
  74. X   (of at least length 2 or 3), it codes the next byte.  Otherwise, it codes
  75. X   the length of the matched string and its distance backwards from the
  76. X   current position.  Single bytes ("literals") are preceded by a one (a
  77. X   single bit) and are either uncoded (the eight bits go directly into the
  78. X   compressed stream for a total of nine bits) or Huffman coded with a
  79. X   supplied literal code tree.  If literals are coded, then the minimum match
  80. X   length is three, otherwise it is two.
  81. X   
  82. X   There are therefore four kinds of imploded streams: 8K search with coded
  83. X   literals (min match = 3), 4K search with coded literals (min match = 3),
  84. X   8K with uncoded literals (min match = 2), and 4K with uncoded literals
  85. X   (min match = 2).  The kind of stream is identified in two bits of a
  86. X   general purpose bit flag that is outside of the compressed stream.
  87. X   
  88. X   Distance-length pairs for matched strings are preceded by a zero bit (to
  89. X   distinguish them from literals) and are always coded.  The distance comes
  90. X   first and is either the low six (4K) or low seven (8K) bits of the
  91. X   distance (uncoded), followed by the high six bits of the distance coded.
  92. X   Then the length is six bits coded (0..63 + min match length), and if the
  93. X   maximum such length is coded, then it's followed by another eight bits
  94. X   (uncoded) to be added to the coded length.  This gives a match length
  95. X   range of 2..320 or 3..321 bytes.
  96. X
  97. X   The literal, length, and distance codes are all represented in a slightly
  98. X   compressed form themselves.  What is sent are the lengths of the codes for
  99. X   each value, which is sufficient to construct the codes.  Each byte of the
  100. X   code representation is the code length (the low four bits representing
  101. X   1..16), and the number of values sequentially with that length (the high
  102. X   four bits also representing 1..16).  There are 256 literal code values (if
  103. X   literals are coded), 64 length code values, and 64 distance code values,
  104. X   in that order at the beginning of the compressed stream.  Each set of code
  105. X   values is preceded (redundantly) with a byte indicating how many bytes are
  106. X   in the code description that follows, in the range 1..256.
  107. X
  108. X   The codes themselves are decoded using tables made by huft_build() from
  109. X   the bit lengths.  That routine and its comments are in the inflate.c
  110. X   module.
  111. X */
  112. X
  113. X#include "unzip.h"      /* must supply slide[] (uch) array and NEXTBYTE macro */
  114. X
  115. X#ifndef WSIZE
  116. X#  define WSIZE 0x8000  /* window size--must be a power of two, and */
  117. X#endif                  /* at least 8K for zip's implode method */
  118. X
  119. X
  120. Xstruct huft {
  121. X  uch e;                /* number of extra bits or operation */
  122. X  uch b;                /* number of bits in this code or subcode */
  123. X  union {
  124. X    ush n;              /* literal, length base, or distance base */
  125. X    struct huft *t;     /* pointer to next level of table */
  126. X  } v;
  127. X};
  128. X
  129. X/* Function prototypes */
  130. X/* routines from inflate.c */
  131. Xextern unsigned hufts;
  132. Xint huft_build OF((unsigned *, unsigned, unsigned, ush *, ush *,
  133. X                   struct huft **, int *));
  134. Xint huft_free OF((struct huft *));
  135. X
  136. X/* routines here */
  137. Xint get_tree OF((unsigned *, unsigned));
  138. Xint explode_lit8 OF((struct huft *, struct huft *, struct huft *,
  139. X                     int, int, int));
  140. Xint explode_lit4 OF((struct huft *, struct huft *, struct huft *,
  141. X                     int, int, int));
  142. Xint explode_nolit8 OF((struct huft *, struct huft *, int, int));
  143. Xint explode_nolit4 OF((struct huft *, struct huft *, int, int));
  144. Xint explode OF((void));
  145. X
  146. X
  147. X/* The implode algorithm uses a sliding 4K or 8K byte window on the
  148. X   uncompressed stream to find repeated byte strings.  This is implemented
  149. X   here as a circular buffer.  The index is updated simply by incrementing
  150. X   and then and'ing with 0x0fff (4K-1) or 0x1fff (8K-1).  Here, the 32K
  151. X   buffer of inflate is used, and it works just as well to always have
  152. X   a 32K circular buffer, so the index is anded with 0x7fff.  This is
  153. X   done to allow the window to also be used as the output buffer. */
  154. X/* This must be supplied in an external module useable like "uch slide[8192];"
  155. X   or "uch *slide;", where the latter would be malloc'ed.  In unzip, slide[]
  156. X   is actually a 32K area for use by inflate, which uses a 32K sliding window.
  157. X */
  158. X
  159. X
  160. X/* Tables for length and distance */
  161. Xush cplen2[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
  162. X        18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
  163. X        35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
  164. X        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65};
  165. Xush cplen3[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
  166. X        19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
  167. X        36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
  168. X        53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66};
  169. Xush extra[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  170. X        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  171. X        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  172. X        8};
  173. Xush cpdist4[] = {1, 65, 129, 193, 257, 321, 385, 449, 513, 577, 641, 705,
  174. X        769, 833, 897, 961, 1025, 1089, 1153, 1217, 1281, 1345, 1409, 1473,
  175. X        1537, 1601, 1665, 1729, 1793, 1857, 1921, 1985, 2049, 2113, 2177,
  176. X        2241, 2305, 2369, 2433, 2497, 2561, 2625, 2689, 2753, 2817, 2881,
  177. X        2945, 3009, 3073, 3137, 3201, 3265, 3329, 3393, 3457, 3521, 3585,
  178. X        3649, 3713, 3777, 3841, 3905, 3969, 4033};
  179. Xush cpdist8[] = {1, 129, 257, 385, 513, 641, 769, 897, 1025, 1153, 1281,
  180. X        1409, 1537, 1665, 1793, 1921, 2049, 2177, 2305, 2433, 2561, 2689,
  181. X        2817, 2945, 3073, 3201, 3329, 3457, 3585, 3713, 3841, 3969, 4097,
  182. X        4225, 4353, 4481, 4609, 4737, 4865, 4993, 5121, 5249, 5377, 5505,
  183. X        5633, 5761, 5889, 6017, 6145, 6273, 6401, 6529, 6657, 6785, 6913,
  184. X        7041, 7169, 7297, 7425, 7553, 7681, 7809, 7937, 8065};
  185. X
  186. X
  187. X/* Macros for inflate() bit peeking and grabbing.
  188. X   The usage is:
  189. X   
  190. X        NEEDBITS(j)
  191. X        x = b & mask_bits[j];
  192. X        DUMPBITS(j)
  193. X
  194. X   where NEEDBITS makes sure that b has at least j bits in it, and
  195. X   DUMPBITS removes the bits from b.  The macros use the variable k
  196. X   for the number of bits in b.  Normally, b and k are register
  197. X   variables for speed.
  198. X */
  199. X
  200. X#define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE)<<k;k+=8;}}
  201. X#define DUMPBITS(n) {b>>=(n);k-=(n);}
  202. X
  203. X
  204. X
  205. Xint get_tree(l, n)
  206. Xunsigned *l;            /* bit lengths */
  207. Xunsigned n;             /* number expected */
  208. X/* Get the bit lengths for a code representation from the compressed
  209. X   stream.  If get_tree() returns 4, then there is an error in the data.
  210. X   Otherwise zero is returned. */
  211. X{
  212. X  unsigned i;           /* bytes remaining in list */
  213. X  unsigned k;           /* lengths entered */
  214. X  unsigned j;           /* number of codes */
  215. X  unsigned b;           /* bit length for those codes */ 
  216. X
  217. X
  218. X  /* get bit lengths */
  219. X  i = NEXTBYTE + 1;                     /* length/count pairs to read */
  220. X  k = 0;                                /* next code */
  221. X  do {
  222. X    b = ((j = NEXTBYTE) & 0xf) + 1;     /* bits in code (1..16) */
  223. X    j = ((j & 0xf0) >> 4) + 1;          /* codes with those bits (1..16) */
  224. X    if (k + j > n)
  225. X      return 4;                         /* don't overflow l[] */
  226. X    do {
  227. X      l[k++] = b;
  228. X    } while (--j);
  229. X  } while (--i);
  230. X  return k != n ? 4 : 0;                /* should have read n of them */
  231. X}
  232. X
  233. X
  234. X
  235. Xint explode_lit8(tb, tl, td, bb, bl, bd)
  236. Xstruct huft *tb, *tl, *td;      /* literal, length, and distance tables */
  237. Xint bb, bl, bd;                 /* number of bits decoded by those */
  238. X/* Decompress the imploded data using coded literals and an 8K sliding
  239. X   window. */
  240. X{
  241. X  long s;               /* bytes to decompress */
  242. X  register unsigned e;  /* table entry flag/number of extra bits */
  243. X  unsigned n, d;        /* length and index for copy */
  244. X  unsigned w;           /* current window position */
  245. X  struct huft *t;       /* pointer to table entry */
  246. X  unsigned mb, ml, md;  /* masks for bb, bl, and bd bits */
  247. X  register ulg b;       /* bit buffer */
  248. X  register unsigned k;  /* number of bits in bit buffer */
  249. X  unsigned u;           /* true if unflushed */
  250. X
  251. X
  252. X  /* explode the coded data */
  253. X  b = k = w = 0;                /* initialize bit buffer, window */
  254. X  u = 1;                        /* buffer unflushed */
  255. X  mb = mask_bits[bb];           /* precompute masks for speed */
  256. X  ml = mask_bits[bl];
  257. X  md = mask_bits[bd];
  258. X  s = ucsize;
  259. X  while (s > 0)                 /* do until ucsize bytes uncompressed */
  260. X  {
  261. X    NEEDBITS(1)
  262. X    if (b & 1)                  /* then literal--decode it */
  263. X    {
  264. X      DUMPBITS(1)
  265. X      s--;
  266. X      NEEDBITS((unsigned)bb)    /* get coded literal */
  267. X      if ((e = (t = tb + ((~(unsigned)b) & mb))->e) > 16)
  268. X        do {
  269. X          if (e == 99)
  270. X            return 1;
  271. X          DUMPBITS(t->b)
  272. X          e -= 16;
  273. X          NEEDBITS(e)
  274. X        } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  275. X      DUMPBITS(t->b)
  276. X      slide[w++] = (uch)t->v.n;
  277. X      if (w == WSIZE)
  278. X      {
  279. X        flush(slide, w, 0);
  280. X        w = u = 0;
  281. X      }
  282. X    }
  283. X    else                        /* else distance/length */
  284. X    {
  285. X      DUMPBITS(1)
  286. X      NEEDBITS(7)               /* get distance low bits */
  287. X      d = (unsigned)b & 0x7f;
  288. X      DUMPBITS(7)
  289. X      NEEDBITS((unsigned)bd)    /* get coded distance high bits */
  290. X      if ((e = (t = td + ((~(unsigned)b) & md))->e) > 16)
  291. X        do {
  292. X          if (e == 99)
  293. X            return 1;
  294. X          DUMPBITS(t->b)
  295. X          e -= 16;
  296. X          NEEDBITS(e)
  297. X        } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  298. X      DUMPBITS(t->b)
  299. X      d = w - d - t->v.n;       /* construct offset */
  300. X      NEEDBITS((unsigned)bl)    /* get coded length */
  301. X      if ((e = (t = tl + ((~(unsigned)b) & ml))->e) > 16)
  302. X        do {
  303. X          if (e == 99)
  304. X            return 1;
  305. X          DUMPBITS(t->b)
  306. X          e -= 16;
  307. X          NEEDBITS(e)
  308. X        } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  309. X      DUMPBITS(t->b)
  310. X      n = t->v.n;
  311. X      if (e)                    /* get length extra bits */
  312. X      {
  313. X        NEEDBITS(8)
  314. X        n += (unsigned)b & 0xff;
  315. X        DUMPBITS(8)
  316. X      }
  317. X
  318. X      /* do the copy */
  319. X      s -= n;
  320. X      do {
  321. X        n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
  322. X        if (u && w <= d)
  323. X        {
  324. X          memzero(slide + w, e);
  325. X          w += e;
  326. X          d += e;
  327. X        }
  328. X        else
  329. X#ifndef NOMEMCPY
  330. X          if (w - d >= e)       /* (this test assumes unsigned comparison) */
  331. X          {
  332. X            memcpy(slide + w, slide + d, e);
  333. X            w += e;
  334. X            d += e;
  335. X          }
  336. X          else                  /* do it slow to avoid memcpy() overlap */
  337. X#endif /* !NOMEMCPY */
  338. X            do {
  339. X              slide[w++] = slide[d++];
  340. X            } while (--e);
  341. X        if (w == WSIZE)
  342. X        {
  343. X          flush(slide, w, 0);
  344. X          w = u = 0;
  345. X        }
  346. X      } while (n);
  347. X    }
  348. X  }
  349. X
  350. X  /* flush out slide */
  351. X  flush(slide, w, 0);
  352. X  if (csize + (k >> 3))   /* should have read csize bytes, but sometimes */
  353. X  {                       /* read one too many:  k>>3 compensates */
  354. X    used_csize = lrec.csize - csize - (k >> 3);
  355. X    return 5;
  356. X  }
  357. X  return 0;
  358. X}
  359. X
  360. X
  361. X
  362. Xint explode_lit4(tb, tl, td, bb, bl, bd)
  363. Xstruct huft *tb, *tl, *td;      /* literal, length, and distance tables */
  364. Xint bb, bl, bd;                 /* number of bits decoded by those */
  365. X/* Decompress the imploded data using coded literals and a 4K sliding
  366. X   window. */
  367. X{
  368. X  long s;               /* bytes to decompress */
  369. X  register unsigned e;  /* table entry flag/number of extra bits */
  370. X  unsigned n, d;        /* length and index for copy */
  371. X  unsigned w;           /* current window position */
  372. X  struct huft *t;       /* pointer to table entry */
  373. X  unsigned mb, ml, md;  /* masks for bb, bl, and bd bits */
  374. X  register ulg b;       /* bit buffer */
  375. X  register unsigned k;  /* number of bits in bit buffer */
  376. X  unsigned u;           /* true if unflushed */
  377. X
  378. X
  379. X  /* explode the coded data */
  380. X  b = k = w = 0;                /* initialize bit buffer, window */
  381. X  u = 1;                        /* buffer unflushed */
  382. X  mb = mask_bits[bb];           /* precompute masks for speed */
  383. X  ml = mask_bits[bl];
  384. X  md = mask_bits[bd];
  385. X  s = ucsize;
  386. X  while (s > 0)                 /* do until ucsize bytes uncompressed */
  387. X  {
  388. X    NEEDBITS(1)
  389. X    if (b & 1)                  /* then literal--decode it */
  390. X    {
  391. X      DUMPBITS(1)
  392. X      s--;
  393. X      NEEDBITS((unsigned)bb)    /* get coded literal */
  394. X      if ((e = (t = tb + ((~(unsigned)b) & mb))->e) > 16)
  395. X        do {
  396. X          if (e == 99)
  397. X            return 1;
  398. X          DUMPBITS(t->b)
  399. X          e -= 16;
  400. X          NEEDBITS(e)
  401. X        } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  402. X      DUMPBITS(t->b)
  403. X      slide[w++] = (uch)t->v.n;
  404. X      if (w == WSIZE)
  405. X      {
  406. X        flush(slide, w, 0);
  407. X        w = u = 0;
  408. X      }
  409. X    }
  410. X    else                        /* else distance/length */
  411. X    {
  412. X      DUMPBITS(1)
  413. X      NEEDBITS(6)               /* get distance low bits */
  414. X      d = (unsigned)b & 0x3f;
  415. X      DUMPBITS(6)
  416. X      NEEDBITS((unsigned)bd)    /* get coded distance high bits */
  417. X      if ((e = (t = td + ((~(unsigned)b) & md))->e) > 16)
  418. X        do {
  419. X          if (e == 99)
  420. X            return 1;
  421. X          DUMPBITS(t->b)
  422. X          e -= 16;
  423. X          NEEDBITS(e)
  424. X        } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  425. X      DUMPBITS(t->b)
  426. X      d = w - d - t->v.n;       /* construct offset */
  427. X      NEEDBITS((unsigned)bl)    /* get coded length */
  428. X      if ((e = (t = tl + ((~(unsigned)b) & ml))->e) > 16)
  429. X        do {
  430. X          if (e == 99)
  431. X            return 1;
  432. X          DUMPBITS(t->b)
  433. X          e -= 16;
  434. X          NEEDBITS(e)
  435. X        } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  436. X      DUMPBITS(t->b)
  437. X      n = t->v.n;
  438. X      if (e)                    /* get length extra bits */
  439. X      {
  440. X        NEEDBITS(8)
  441. X        n += (unsigned)b & 0xff;
  442. X        DUMPBITS(8)
  443. X      }
  444. X
  445. X      /* do the copy */
  446. X      s -= n;
  447. X      do {
  448. X        n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
  449. X        if (u && w <= d)
  450. X        {
  451. X          memzero(slide + w, e);
  452. X          w += e;
  453. X          d += e;
  454. X        }
  455. X        else
  456. X#ifndef NOMEMCPY
  457. X          if (w - d >= e)       /* (this test assumes unsigned comparison) */
  458. X          {
  459. X            memcpy(slide + w, slide + d, e);
  460. X            w += e;
  461. X            d += e;
  462. X          }
  463. X          else                  /* do it slow to avoid memcpy() overlap */
  464. X#endif /* !NOMEMCPY */
  465. X            do {
  466. X              slide[w++] = slide[d++];
  467. X            } while (--e);
  468. X        if (w == WSIZE)
  469. X        {
  470. X          flush(slide, w, 0);
  471. X          w = u = 0;
  472. X        }
  473. X      } while (n);
  474. X    }
  475. X  }
  476. X
  477. X  /* flush out slide */
  478. X  flush(slide, w, 0);
  479. X  if (csize + (k >> 3))   /* should have read csize bytes, but sometimes */
  480. X  {                       /* read one too many:  k>>3 compensates */
  481. X    used_csize = lrec.csize - csize - (k >> 3);
  482. X    return 5;
  483. X  }
  484. X  return 0;
  485. X}
  486. X
  487. X
  488. X
  489. Xint explode_nolit8(tl, td, bl, bd)
  490. Xstruct huft *tl, *td;   /* length and distance decoder tables */
  491. Xint bl, bd;             /* number of bits decoded by tl[] and td[] */
  492. X/* Decompress the imploded data using uncoded literals and an 8K sliding
  493. X   window. */
  494. X{
  495. X  long s;               /* bytes to decompress */
  496. X  register unsigned e;  /* table entry flag/number of extra bits */
  497. X  unsigned n, d;        /* length and index for copy */
  498. X  unsigned w;           /* current window position */
  499. X  struct huft *t;       /* pointer to table entry */
  500. X  unsigned ml, md;      /* masks for bl and bd bits */
  501. X  register ulg b;       /* bit buffer */
  502. X  register unsigned k;  /* number of bits in bit buffer */
  503. X  unsigned u;           /* true if unflushed */
  504. X
  505. X
  506. X  /* explode the coded data */
  507. X  b = k = w = 0;                /* initialize bit buffer, window */
  508. X  u = 1;                        /* buffer unflushed */
  509. X  ml = mask_bits[bl];           /* precompute masks for speed */
  510. X  md = mask_bits[bd];
  511. X  s = ucsize;
  512. X  while (s > 0)                 /* do until ucsize bytes uncompressed */
  513. X  {
  514. X    NEEDBITS(1)
  515. X    if (b & 1)                  /* then literal--get eight bits */
  516. X    {
  517. X      DUMPBITS(1)
  518. X      s--;
  519. X      NEEDBITS(8)
  520. X      slide[w++] = (uch)b;
  521. X      if (w == WSIZE)
  522. X      {
  523. X        flush(slide, w, 0);
  524. X        w = u = 0;
  525. X      }
  526. X      DUMPBITS(8)
  527. X    }
  528. X    else                        /* else distance/length */
  529. X    {
  530. X      DUMPBITS(1)
  531. X      NEEDBITS(7)               /* get distance low bits */
  532. X      d = (unsigned)b & 0x7f;
  533. X      DUMPBITS(7)
  534. X      NEEDBITS((unsigned)bd)    /* get coded distance high bits */
  535. X      if ((e = (t = td + ((~(unsigned)b) & md))->e) > 16)
  536. X        do {
  537. X          if (e == 99)
  538. X            return 1;
  539. X          DUMPBITS(t->b)
  540. X          e -= 16;
  541. X          NEEDBITS(e)
  542. X        } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  543. X      DUMPBITS(t->b)
  544. X      d = w - d - t->v.n;       /* construct offset */
  545. X      NEEDBITS((unsigned)bl)    /* get coded length */
  546. X      if ((e = (t = tl + ((~(unsigned)b) & ml))->e) > 16)
  547. X        do {
  548. X          if (e == 99)
  549. X            return 1;
  550. X          DUMPBITS(t->b)
  551. X          e -= 16;
  552. X          NEEDBITS(e)
  553. X        } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  554. X      DUMPBITS(t->b)
  555. X      n = t->v.n;
  556. X      if (e)                    /* get length extra bits */
  557. X      {
  558. X        NEEDBITS(8)
  559. X        n += (unsigned)b & 0xff;
  560. X        DUMPBITS(8)
  561. X      }
  562. X
  563. X      /* do the copy */
  564. X      s -= n;
  565. X      do {
  566. X        n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
  567. X        if (u && w <= d)
  568. X        {
  569. X          memzero(slide + w, e);
  570. X          w += e;
  571. X          d += e;
  572. X        }
  573. X        else
  574. X#ifndef NOMEMCPY
  575. X          if (w - d >= e)       /* (this test assumes unsigned comparison) */
  576. X          {
  577. X            memcpy(slide + w, slide + d, e);
  578. X            w += e;
  579. X            d += e;
  580. X          }
  581. X          else                  /* do it slow to avoid memcpy() overlap */
  582. X#endif /* !NOMEMCPY */
  583. X            do {
  584. X              slide[w++] = slide[d++];
  585. X            } while (--e);
  586. X        if (w == WSIZE)
  587. X        {
  588. X          flush(slide, w, 0);
  589. X          w = u = 0;
  590. X        }
  591. X      } while (n);
  592. X    }
  593. X  }
  594. X
  595. X  /* flush out slide */
  596. X  flush(slide, w, 0);
  597. X  if (csize + (k >> 3))   /* should have read csize bytes, but sometimes */
  598. X  {                       /* read one too many:  k>>3 compensates */
  599. X    used_csize = lrec.csize - csize - (k >> 3);
  600. X    return 5;
  601. X  }
  602. X  return 0;
  603. X}
  604. X
  605. X
  606. X
  607. Xint explode_nolit4(tl, td, bl, bd)
  608. Xstruct huft *tl, *td;   /* length and distance decoder tables */
  609. Xint bl, bd;             /* number of bits decoded by tl[] and td[] */
  610. X/* Decompress the imploded data using uncoded literals and a 4K sliding
  611. X   window. */
  612. X{
  613. X  long s;               /* bytes to decompress */
  614. X  register unsigned e;  /* table entry flag/number of extra bits */
  615. X  unsigned n, d;        /* length and index for copy */
  616. X  unsigned w;           /* current window position */
  617. X  struct huft *t;       /* pointer to table entry */
  618. X  unsigned ml, md;      /* masks for bl and bd bits */
  619. X  register ulg b;       /* bit buffer */
  620. X  register unsigned k;  /* number of bits in bit buffer */
  621. X  unsigned u;           /* true if unflushed */
  622. X
  623. X
  624. X  /* explode the coded data */
  625. X  b = k = w = 0;                /* initialize bit buffer, window */
  626. X  u = 1;                        /* buffer unflushed */
  627. X  ml = mask_bits[bl];           /* precompute masks for speed */
  628. X  md = mask_bits[bd];
  629. X  s = ucsize;
  630. X  while (s > 0)                 /* do until ucsize bytes uncompressed */
  631. X  {
  632. X    NEEDBITS(1)
  633. X    if (b & 1)                  /* then literal--get eight bits */
  634. X    {
  635. X      DUMPBITS(1)
  636. X      s--;
  637. X      NEEDBITS(8)
  638. X      slide[w++] = (uch)b;
  639. X      if (w == WSIZE)
  640. X      {
  641. X        flush(slide, w, 0);
  642. X        w = u = 0;
  643. X      }
  644. X      DUMPBITS(8)
  645. X    }
  646. X    else                        /* else distance/length */
  647. X    {
  648. X      DUMPBITS(1)
  649. X      NEEDBITS(6)               /* get distance low bits */
  650. X      d = (unsigned)b & 0x3f;
  651. X      DUMPBITS(6)
  652. X      NEEDBITS((unsigned)bd)    /* get coded distance high bits */
  653. X      if ((e = (t = td + ((~(unsigned)b) & md))->e) > 16)
  654. X        do {
  655. X          if (e == 99)
  656. X            return 1;
  657. X          DUMPBITS(t->b)
  658. X          e -= 16;
  659. X          NEEDBITS(e)
  660. X        } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  661. X      DUMPBITS(t->b)
  662. X      d = w - d - t->v.n;       /* construct offset */
  663. X      NEEDBITS((unsigned)bl)    /* get coded length */
  664. X      if ((e = (t = tl + ((~(unsigned)b) & ml))->e) > 16)
  665. X        do {
  666. X          if (e == 99)
  667. X            return 1;
  668. X          DUMPBITS(t->b)
  669. X          e -= 16;
  670. X          NEEDBITS(e)
  671. X        } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
  672. X      DUMPBITS(t->b)
  673. X      n = t->v.n;
  674. X      if (e)                    /* get length extra bits */
  675. X      {
  676. X        NEEDBITS(8)
  677. X        n += (unsigned)b & 0xff;
  678. X        DUMPBITS(8)
  679. X      }
  680. X
  681. X      /* do the copy */
  682. X      s -= n;
  683. X      do {
  684. X        n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
  685. X        if (u && w <= d)
  686. X        {
  687. X          memzero(slide + w, e);
  688. X          w += e;
  689. X          d += e;
  690. X        }
  691. X        else
  692. X#ifndef NOMEMCPY
  693. X          if (w - d >= e)       /* (this test assumes unsigned comparison) */
  694. X          {
  695. X            memcpy(slide + w, slide + d, e);
  696. X            w += e;
  697. X            d += e;
  698. X          }
  699. X          else                  /* do it slow to avoid memcpy() overlap */
  700. X#endif /* !NOMEMCPY */
  701. X            do {
  702. X              slide[w++] = slide[d++];
  703. X            } while (--e);
  704. X        if (w == WSIZE)
  705. X        {
  706. X          flush(slide, w, 0);
  707. X          w = u = 0;
  708. X        }
  709. X      } while (n);
  710. X    }
  711. X  }
  712. X
  713. X  /* flush out slide */
  714. X  flush(slide, w, 0);
  715. X  if (csize + (k >> 3))   /* should have read csize bytes, but sometimes */
  716. X  {                       /* read one too many:  k>>3 compensates */
  717. X    used_csize = lrec.csize - csize - (k >> 3);
  718. X    return 5;
  719. X  }
  720. X  return 0;
  721. X}
  722. X
  723. X
  724. X
  725. Xint explode()
  726. X/* Explode an imploded compressed stream.  Based on the general purpose
  727. X   bit flag, decide on coded or uncoded literals, and an 8K or 4K sliding
  728. X   window.  Construct the literal (if any), length, and distance codes and
  729. X   the tables needed to decode them (using huft_build() from inflate.c),
  730. X   and call the appropriate routine for the type of data in the remainder
  731. X   of the stream.  The four routines are nearly identical, differing only
  732. X   in whether the literal is decoded or simply read in, and in how many
  733. X   bits are read in, uncoded, for the low distance bits. */
  734. X{
  735. X  unsigned r;           /* return codes */
  736. X  struct huft *tb;      /* literal code table */
  737. X  struct huft *tl;      /* length code table */
  738. X  struct huft *td;      /* distance code table */
  739. X  int bb;               /* bits for tb */
  740. X  int bl;               /* bits for tl */
  741. X  int bd;               /* bits for td */
  742. X  static unsigned l[256]; /* bit lengths for codes */
  743. X
  744. X
  745. X  /* Tune base table sizes.  Note: I thought that to truly optimize speed,
  746. X     I would have to select different bl, bd, and bb values for different
  747. X     compressed file sizes.  I was suprised to find out the the values of
  748. X     7, 7, and 9 worked best over a very wide range of sizes, except that
  749. X     bd = 8 worked marginally better for large compressed sizes. */
  750. X  bl = 7;
  751. X  bd = csize > 200000L ? 8 : 7;
  752. X
  753. X
  754. X  /* With literal tree--minimum match length is 3 */
  755. X  hufts = 0;                    /* initialize huft's malloc'ed */
  756. X  if (lrec.general_purpose_bit_flag & 4)
  757. X  {
  758. X    bb = 9;                     /* base table size for literals */
  759. X    if ((r = get_tree(l, 256)) != 0)
  760. X      return (int)r;
  761. X    if ((r = huft_build(l, 256, 256, NULL, NULL, &tb, &bb)) != 0)
  762. X    {
  763. X      if (r == 1)
  764. X        huft_free(tb);
  765. X      return (int)r;
  766. X    }
  767. X    if ((r = get_tree(l, 64)) != 0)
  768. X      return (int)r;
  769. X    if ((r = huft_build(l, 64, 0, cplen3, extra, &tl, &bl)) != 0)
  770. X    {
  771. X      if (r == 1)
  772. X        huft_free(tl);
  773. X      huft_free(tb);
  774. X      return (int)r;
  775. X    }
  776. X    if ((r = get_tree(l, 64)) != 0)
  777. X      return (int)r;
  778. X    if (lrec.general_purpose_bit_flag & 2)      /* true if 8K */
  779. X    {
  780. X      if ((r = huft_build(l, 64, 0, cpdist8, extra, &td, &bd)) != 0)
  781. X      {
  782. X        if (r == 1)
  783. X          huft_free(td);
  784. X        huft_free(tl);
  785. X        huft_free(tb);
  786. X        return (int)r;
  787. X      }
  788. X      r = explode_lit8(tb, tl, td, bb, bl, bd);
  789. X    }
  790. X    else                                        /* else 4K */
  791. X    {
  792. X      if ((r = huft_build(l, 64, 0, cpdist4, extra, &td, &bd)) != 0)
  793. X      {
  794. X        if (r == 1)
  795. X          huft_free(td);
  796. X        huft_free(tl);
  797. X        huft_free(tb);
  798. X        return (int)r;
  799. X      }
  800. X      r = explode_lit4(tb, tl, td, bb, bl, bd);
  801. X    }
  802. X    huft_free(td);
  803. X    huft_free(tl);
  804. X    huft_free(tb);
  805. X  }
  806. X  else
  807. X
  808. X
  809. X  /* No literal tree--minimum match length is 2 */
  810. X  {
  811. X    if ((r = get_tree(l, 64)) != 0)
  812. X      return (int)r;
  813. X    if ((r = huft_build(l, 64, 0, cplen2, extra, &tl, &bl)) != 0)
  814. X    {
  815. X      if (r == 1)
  816. X        huft_free(tl);
  817. X      return (int)r;
  818. X    }
  819. X    if ((r = get_tree(l, 64)) != 0)
  820. X      return (int)r;
  821. X    if (lrec.general_purpose_bit_flag & 2)      /* true if 8K */
  822. X    {
  823. X      if ((r = huft_build(l, 64, 0, cpdist8, extra, &td, &bd)) != 0)
  824. X      {
  825. X        if (r == 1)
  826. X          huft_free(td);
  827. X        huft_free(tl);
  828. X        return (int)r;
  829. X      }
  830. X      r = explode_nolit8(tl, td, bl, bd);
  831. X    }
  832. X    else                                        /* else 4K */
  833. X    {
  834. X      if ((r = huft_build(l, 64, 0, cpdist4, extra, &td, &bd)) != 0)
  835. X      {
  836. X        if (r == 1)
  837. X          huft_free(td);
  838. X        huft_free(tl);
  839. X        return (int)r;
  840. X      }
  841. X      r = explode_nolit4(tl, td, bl, bd);
  842. X    }
  843. X    huft_free(td);
  844. X    huft_free(tl);
  845. X  }
  846. X#ifdef DEBUG
  847. X  fprintf(stderr, "<%u > ", hufts);
  848. X#endif /* DEBUG */
  849. X  return (int)r;
  850. X}
  851. X
  852. X/* so explode.c and inflate.c can be compiled together into one object: */
  853. X#undef NEXTBYTE
  854. X#undef NEEDBITS
  855. X#undef DUMPBITS
  856. END_OF_FILE
  857.   if test 28523 -ne `wc -c <'unzip-5.12/explode.c'`; then
  858.     echo shar: \"'unzip-5.12/explode.c'\" unpacked with wrong size!
  859.   fi
  860.   # end of 'unzip-5.12/explode.c'
  861. fi
  862. if test -f 'unzip-5.12/inflate.c' -a "${1}" != "-c" ; then 
  863.   echo shar: Will not clobber existing file \"'unzip-5.12/inflate.c'\"
  864. else
  865.   echo shar: Extracting \"'unzip-5.12/inflate.c'\" \(38275 characters\)
  866.   sed "s/^X//" >'unzip-5.12/inflate.c' <<'END_OF_FILE'
  867. X/* inflate.c -- put in the public domain by Mark Adler
  868. X   version c14o, 23 August 1994 */
  869. X
  870. X
  871. X/* You can do whatever you like with this source file, though I would
  872. X   prefer that if you modify it and redistribute it that you include
  873. X   comments to that effect with your name and the date.  Thank you.
  874. X
  875. X   History:
  876. X   vers    date          who           what
  877. X   ----  ---------  --------------  ------------------------------------
  878. X    a    ~~ Feb 92  M. Adler        used full (large, one-step) lookup table
  879. X    b1   21 Mar 92  M. Adler        first version with partial lookup tables
  880. X    b2   21 Mar 92  M. Adler        fixed bug in fixed-code blocks
  881. X    b3   22 Mar 92  M. Adler        sped up match copies, cleaned up some
  882. X    b4   25 Mar 92  M. Adler        added prototypes; removed window[] (now
  883. X                                    is the responsibility of unzip.h--also
  884. X                                    changed name to slide[]), so needs diffs
  885. X                                    for unzip.c and unzip.h (this allows
  886. X                                    compiling in the small model on MSDOS);
  887. X                                    fixed cast of q in huft_build();
  888. X    b5   26 Mar 92  M. Adler        got rid of unintended macro recursion.
  889. X    b6   27 Mar 92  M. Adler        got rid of nextbyte() routine.  fixed
  890. X                                    bug in inflate_fixed().
  891. X    c1   30 Mar 92  M. Adler        removed lbits, dbits environment variables.
  892. X                                    changed BMAX to 16 for explode.  Removed
  893. X                                    OUTB usage, and replaced it with flush()--
  894. X                                    this was a 20% speed improvement!  Added
  895. X                                    an explode.c (to replace unimplod.c) that
  896. X                                    uses the huft routines here.  Removed
  897. X                                    register union.
  898. X    c2    4 Apr 92  M. Adler        fixed bug for file sizes a multiple of 32k.
  899. X    c3   10 Apr 92  M. Adler        reduced memory of code tables made by
  900. X                                    huft_build significantly (factor of two to
  901. X                                    three).
  902. X    c4   15 Apr 92  M. Adler        added NOMEMCPY do kill use of memcpy().
  903. X                                    worked around a Turbo C optimization bug.
  904. X    c5   21 Apr 92  M. Adler        added the WSIZE #define to allow reducing
  905. X                                    the 32K window size for specialized
  906. X                                    applications.
  907. X    c6   31 May 92  M. Adler        added some typecasts to eliminate warnings
  908. X    c7   27 Jun 92  G. Roelofs      added some more typecasts (444:  MSC bug).
  909. X    c8    5 Oct 92  J-l. Gailly     added ifdef'd code to deal with PKZIP bug.
  910. X    c9    9 Oct 92  M. Adler        removed a memory error message (~line 416).
  911. X    c10  17 Oct 92  G. Roelofs      changed ULONG/UWORD/byte to ulg/ush/uch,
  912. X                                    removed old inflate, renamed inflate_entry
  913. X                                    to inflate, added Mark's fix to a comment.
  914. X   c10.5 14 Dec 92  M. Adler        fix up error messages for incomplete trees.
  915. X    c11   2 Jan 93  M. Adler        fixed bug in detection of incomplete
  916. X                                    tables, and removed assumption that EOB is
  917. X                                    the longest code (bad assumption).
  918. X    c12   3 Jan 93  M. Adler        make tables for fixed blocks only once.
  919. X    c13   5 Jan 93  M. Adler        allow all zero length codes (pkzip 2.04c
  920. X                                    outputs one zero length code for an empty
  921. X                                    distance tree).
  922. X    c14  12 Mar 93  M. Adler        made inflate.c standalone with the
  923. X                                    introduction of inflate.h.
  924. X   c14b  16 Jul 93  G. Roelofs      added (unsigned) typecast to w at 470.
  925. X   c14c  19 Jul 93  J. Bush         changed v[N_MAX], l[288], ll[28x+3x] arrays
  926. X                                    to static for Amiga.
  927. X   c14d  13 Aug 93  J-l. Gailly     de-complicatified Mark's c[*p++]++ thing.
  928. X   c14e   8 Oct 93  G. Roelofs      changed memset() to memzero().
  929. X   c14f  22 Oct 93  G. Roelofs      renamed quietflg to qflag; made Trace()
  930. X                                    conditional; added inflate_free().
  931. X   c14g  28 Oct 93  G. Roelofs      changed l/(lx+1) macro to pointer (Cray bug)
  932. X   c14h   7 Dec 93  C. Ghisler      huft_build() optimizations.
  933. X   c14i   9 Jan 94  A. Verheijen    set fixed_t{d,l} to NULL after freeing;
  934. X                    G. Roelofs      check NEXTBYTE macro for EOF.
  935. X   c14j  23 Jan 94  G. Roelofs      removed Ghisler "optimizations"; ifdef'd
  936. X                                    EOF check.
  937. X   c14k  27 Feb 94  G. Roelofs      added some typecasts to avoid warnings.
  938. X   c14l   9 Apr 94  G. Roelofs      fixed split comments on preprocessor lines
  939. X                                    to avoid bug in Encore compiler.
  940. X   c14m   7 Jul 94  P. Kienitz      modified to allow assembler version of
  941. X                                    inflate_codes() (define ASM_INFLATECODES)
  942. X   c14n  22 Jul 94  G. Roelofs      changed fprintf to FPRINTF for DLL versions
  943. X   c14o  23 Aug 94  C. Spieler      added a newline to a debug statement;
  944. X                    G. Roelofs      added another typecast to avoid MSC warning
  945. X */
  946. X
  947. X
  948. X/*
  949. X   Inflate deflated (PKZIP's method 8 compressed) data.  The compression
  950. X   method searches for as much of the current string of bytes (up to a
  951. X   length of 258) in the previous 32K bytes.  If it doesn't find any
  952. X   matches (of at least length 3), it codes the next byte.  Otherwise, it
  953. X   codes the length of the matched string and its distance backwards from
  954. X   the current position.  There is a single Huffman code that codes both
  955. X   single bytes (called "literals") and match lengths.  A second Huffman
  956. X   code codes the distance information, which follows a length code.  Each
  957. X   length or distance code actually represents a base value and a number
  958. X   of "extra" (sometimes zero) bits to get to add to the base value.  At
  959. X   the end of each deflated block is a special end-of-block (EOB) literal/
  960. X   length code.  The decoding process is basically: get a literal/length
  961. X   code; if EOB then done; if a literal, emit the decoded byte; if a
  962. X   length then get the distance and emit the referred-to bytes from the
  963. X   sliding window of previously emitted data.
  964. X
  965. X   There are (currently) three kinds of inflate blocks: stored, fixed, and
  966. X   dynamic.  The compressor outputs a chunk of data at a time and decides
  967. X   which method to use on a chunk-by-chunk basis.  A chunk might typically
  968. X   be 32K to 64K, uncompressed.  If the chunk is uncompressible, then the
  969. X   "stored" method is used.  In this case, the bytes are simply stored as
  970. X   is, eight bits per byte, with none of the above coding.  The bytes are
  971. X   preceded by a count, since there is no longer an EOB code.
  972. X
  973. X   If the data is compressible, then either the fixed or dynamic methods
  974. X   are used.  In the dynamic method, the compressed data is preceded by
  975. X   an encoding of the literal/length and distance Huffman codes that are
  976. X   to be used to decode this block.  The representation is itself Huffman
  977. X   coded, and so is preceded by a description of that code.  These code
  978. X   descriptions take up a little space, and so for small blocks, there is
  979. X   a predefined set of codes, called the fixed codes.  The fixed method is
  980. X   used if the block ends up smaller that way (usually for quite small
  981. X   chunks); otherwise the dynamic method is used.  In the latter case, the
  982. X   codes are customized to the probabilities in the current block and so
  983. X   can code it much better than the pre-determined fixed codes can.
  984. X   The Huffman codes themselves are decoded using a mutli-level table
  985. X   lookup, in order to maximize the speed of decoding plus the speed of
  986. X   building the decoding tables.  See the comments below that precede the
  987. X   lbits and dbits tuning parameters.
  988. X */
  989. X
  990. X
  991. X/*
  992. X   Notes beyond the 1.93a appnote.txt:
  993. X
  994. X   1. Distance pointers never point before the beginning of the output
  995. X      stream.
  996. X   2. Distance pointers can point back across blocks, up to 32k away.
  997. X   3. There is an implied maximum of 7 bits for the bit length table and
  998. X      15 bits for the actual data.
  999. X   4. If only one code exists, then it is encoded using one bit.  (Zero
  1000. X      would be more efficient, but perhaps a little confusing.)  If two
  1001. X      codes exist, they are coded using one bit each (0 and 1).
  1002. X   5. There is no way of sending zero distance codes--a dummy must be
  1003. X      sent if there are none.  (History: a pre 2.0 version of PKZIP would
  1004. X      store blocks with no distance codes, but this was discovered to be
  1005. X      too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
  1006. X      zero distance codes, which is sent as one code of zero bits in
  1007. X      length.
  1008. X   6. There are up to 286 literal/length codes.  Code 256 represents the
  1009. X      end-of-block.  Note however that the static length tree defines
  1010. X      288 codes just to fill out the Huffman codes.  Codes 286 and 287
  1011. X      cannot be used though, since there is no length base or extra bits
  1012. X      defined for them.  Similarily, there are up to 30 distance codes.
  1013. X      However, static trees define 32 codes (all 5 bits) to fill out the
  1014. X      Huffman codes, but the last two had better not show up in the data.
  1015. X   7. Unzip can check dynamic Huffman blocks for complete code sets.
  1016. X      The exception is that a single code would not be complete (see #4).
  1017. X   8. The five bits following the block type is really the number of
  1018. X      literal codes sent minus 257.
  1019. X   9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
  1020. X      (1+6+6).  Therefore, to output three times the length, you output
  1021. X      three codes (1+1+1), whereas to output four times the same length,
  1022. X      you only need two codes (1+3).  Hmm.
  1023. X  10. In the tree reconstruction algorithm, Code = Code + Increment
  1024. X      only if BitLength(i) is not zero.  (Pretty obvious.)
  1025. X  11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
  1026. X  12. Note: length code 284 can represent 227-258, but length code 285
  1027. X      really is 258.  The last length deserves its own, short code
  1028. X      since it gets used a lot in very redundant files.  The length
  1029. X      258 is special since 258 - 3 (the min match length) is 255.
  1030. X  13. The literal/length and distance code bit lengths are read as a
  1031. X      single stream of lengths.  It is possible (and advantageous) for
  1032. X      a repeat code (16, 17, or 18) to go across the boundary between
  1033. X      the two sets of lengths.
  1034. X */
  1035. X
  1036. X
  1037. X#define PKZIP_BUG_WORKAROUND    /* PKZIP 1.93a problem--live with it */
  1038. X
  1039. X/*
  1040. X    inflate.h must supply the uch slide[WSIZE] array and the NEXTBYTE,
  1041. X    FLUSH() and memzero macros.  If the window size is not 32K, it
  1042. X    should also define WSIZE.  If INFMOD is defined, it can include
  1043. X    compiled functions to support the NEXTBYTE and/or FLUSH() macros.
  1044. X    There are defaults for NEXTBYTE and FLUSH() below for use as
  1045. X    examples of what those functions need to do.  Normally, you would
  1046. X    also want FLUSH() to compute a crc on the data.  inflate.h also
  1047. X    needs to provide these typedefs:
  1048. X
  1049. X        typedef unsigned char uch;
  1050. X        typedef unsigned short ush;
  1051. X        typedef unsigned long ulg;
  1052. X
  1053. X    This module uses the external functions malloc() and free() (and
  1054. X    probably memset() or bzero() in the memzero() macro).  Their
  1055. X    prototypes are normally found in <string.h> and <stdlib.h>.
  1056. X */
  1057. X#define INFMOD          /* tell inflate.h to include code to be compiled */
  1058. X#include "inflate.h"
  1059. X
  1060. X#ifndef WSIZE           /* default is 32K */
  1061. X#  define WSIZE 0x8000  /* window size--must be a power of two, and at least */
  1062. X#endif                  /* 32K for zip's deflate method */
  1063. X
  1064. X#ifndef NEXTBYTE        /* default is to simply get a byte from stdin */
  1065. X#  define NEXTBYTE getchar()
  1066. X#endif
  1067. X
  1068. X#ifndef FPRINTF
  1069. X#  define FPRINTF fprintf
  1070. X#endif
  1071. X
  1072. X#ifndef FLUSH           /* default is to simply write the buffer to stdout */
  1073. X#  define FLUSH(n) fwrite(slide, 1, n, stdout)  /* return value not used */
  1074. X#endif
  1075. X/* Warning: the fwrite above might not work on 16-bit compilers, since
  1076. X   0x8000 might be interpreted as -32,768 by the library function. */
  1077. X
  1078. X#ifndef Trace
  1079. X#  ifdef DEBUG
  1080. X#    define Trace(x) fprintf x
  1081. X#  else
  1082. X#    define Trace(x)
  1083. X#  endif
  1084. X#endif
  1085. X
  1086. X
  1087. X/* Huffman code lookup table entry--this entry is four bytes for machines
  1088. X   that have 16-bit pointers (e.g. PC's in the small or medium model).
  1089. X   Valid extra bits are 0..13.  e == 15 is EOB (end of block), e == 16
  1090. X   means that v is a literal, 16 < e < 32 means that v is a pointer to
  1091. X   the next table, which codes e - 16 bits, and lastly e == 99 indicates
  1092. X   an unused code.  If a code with e == 99 is looked up, this implies an
  1093. X   error in the data. */
  1094. Xstruct huft {
  1095. X  uch e;                /* number of extra bits or operation */
  1096. X  uch b;                /* number of bits in this code or subcode */
  1097. X  union {
  1098. X    ush n;              /* literal, length base, or distance base */
  1099. X    struct huft *t;     /* pointer to next level of table */
  1100. X  } v;
  1101. X};
  1102. X
  1103. X
  1104. X/* Function prototypes */
  1105. X#ifndef OF
  1106. X#  ifdef __STDC__
  1107. X#    define OF(a) a
  1108. X#  else /* !__STDC__ */
  1109. X#    define OF(a) ()
  1110. X#  endif /* ?__STDC__ */
  1111. X#endif
  1112. Xint huft_build OF((unsigned *, unsigned, unsigned, ush *, ush *,
  1113. X                   struct huft **, int *));
  1114. Xint huft_free OF((struct huft *));
  1115. Xint inflate_codes OF((struct huft *, struct huft *, int, int));
  1116. Xint inflate_stored OF((void));
  1117. Xint inflate_fixed OF((void));
  1118. Xint inflate_dynamic OF((void));
  1119. Xint inflate_block OF((int *));
  1120. Xint inflate OF((void));
  1121. Xint inflate_free OF((void));
  1122. X
  1123. X
  1124. X/* The inflate algorithm uses a sliding 32K byte window on the uncompressed
  1125. X   stream to find repeated byte strings.  This is implemented here as a
  1126. X   circular buffer.  The index is updated simply by incrementing and then
  1127. X   and'ing with 0x7fff (32K-1). */
  1128. X/* It is left to other modules to supply the 32K area.  It is assumed
  1129. X   to be usable as if it were declared "uch slide[32768];" or as just
  1130. X   "uch *slide;" and then malloc'ed in the latter case.  The definition
  1131. X   must be in unzip.h, included above. */
  1132. Xunsigned wp;            /* current position in slide */
  1133. X
  1134. X
  1135. X/* Tables for deflate from PKZIP's appnote.txt. */
  1136. Xstatic unsigned border[] = {    /* Order of the bit length code lengths */
  1137. X        16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  1138. Xstatic ush cplens[] = {         /* Copy lengths for literal codes 257..285 */
  1139. X        3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  1140. X        35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
  1141. X        /* note: see note #13 above about the 258 in this list. */
  1142. Xstatic ush cplext[] = {         /* Extra bits for literal codes 257..285 */
  1143. X        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  1144. X        3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
  1145. Xstatic ush cpdist[] = {         /* Copy offsets for distance codes 0..29 */
  1146. X        1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  1147. X        257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  1148. X        8193, 12289, 16385, 24577};
  1149. Xstatic ush cpdext[] = {         /* Extra bits for distance codes */
  1150. X        0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  1151. X        7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  1152. X        12, 12, 13, 13};
  1153. X
  1154. X/* And'ing with mask[n] masks the lower n bits */
  1155. Xush mask[] = {
  1156. X    0x0000,
  1157. X    0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
  1158. X    0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
  1159. X};
  1160. X
  1161. X
  1162. X/* Macros for inflate() bit peeking and grabbing.
  1163. X   The usage is:
  1164. X   
  1165. X        NEEDBITS(j)
  1166. X        x = b & mask[j];
  1167. X        DUMPBITS(j)
  1168. X
  1169. X   where NEEDBITS makes sure that b has at least j bits in it, and
  1170. X   DUMPBITS removes the bits from b.  The macros use the variable k
  1171. X   for the number of bits in b.  Normally, b and k are register
  1172. X   variables for speed, and are initialized at the begining of a
  1173. X   routine that uses these macros from a global bit buffer and count.
  1174. X
  1175. X   In order to not ask for more bits than there are in the compressed
  1176. X   stream, the Huffman tables are constructed to only ask for just
  1177. X   enough bits to make up the end-of-block code (value 256).  Then no
  1178. X   bytes need to be "returned" to the buffer at the end of the last
  1179. X   block.  See the huft_build() routine.
  1180. X */
  1181. X
  1182. Xulg bb;                         /* bit buffer */
  1183. Xunsigned bk;                    /* bits in bit buffer */
  1184. X
  1185. X#ifndef CHECK_EOF
  1186. X#  define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE)<<k;k+=8;}}
  1187. X#else
  1188. X#  define NEEDBITS(n) {while(k<(n)){int c=NEXTBYTE;if(c==EOF)return 1;\
  1189. X    b|=((ulg)c)<<k;k+=8;}}
  1190. X#endif                      /* Piet Plomp:  change "return 1" to "break" */
  1191. X
  1192. X#define DUMPBITS(n) {b>>=(n);k-=(n);}
  1193. X
  1194. X
  1195. X/*
  1196. X   Huffman code decoding is performed using a multi-level table lookup.
  1197. X   The fastest way to decode is to simply build a lookup table whose
  1198. X   size is determined by the longest code.  However, the time it takes
  1199. X   to build this table can also be a factor if the data being decoded
  1200. X   is not very long.  The most common codes are necessarily the
  1201. X   shortest codes, so those codes dominate the decoding time, and hence
  1202. X   the speed.  The idea is you can have a shorter table that decodes the
  1203. X   shorter, more probable codes, and then point to subsidiary tables for
  1204. X   the longer codes.  The time it costs to decode the longer codes is
  1205. X   then traded against the time it takes to make longer tables.
  1206. X
  1207. X   This results of this trade are in the variables lbits and dbits
  1208. X   below.  lbits is the number of bits the first level table for literal/
  1209. X   length codes can decode in one step, and dbits is the same thing for
  1210. X   the distance codes.  Subsequent tables are also less than or equal to
  1211. X   those sizes.  These values may be adjusted either when all of the
  1212. X   codes are shorter than that, in which case the longest code length in
  1213. X   bits is used, or when the shortest code is *longer* than the requested
  1214. X   table size, in which case the length of the shortest code in bits is
  1215. X   used.
  1216. X
  1217. X   There are two different values for the two tables, since they code a
  1218. X   different number of possibilities each.  The literal/length table
  1219. X   codes 286 possible values, or in a flat code, a little over eight
  1220. X   bits.  The distance table codes 30 possible values, or a little less
  1221. X   than five bits, flat.  The optimum values for speed end up being
  1222. X   about one bit more than those, so lbits is 8+1 and dbits is 5+1.
  1223. X   The optimum values may differ though from machine to machine, and
  1224. X   possibly even between compilers.  Your mileage may vary.
  1225. X */
  1226. X
  1227. X
  1228. Xint lbits = 9;          /* bits in base literal/length lookup table */
  1229. Xint dbits = 6;          /* bits in base distance lookup table */
  1230. X
  1231. X
  1232. X/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
  1233. X#define BMAX 16         /* maximum bit length of any code (16 for explode) */
  1234. X#define N_MAX 288       /* maximum number of codes in any set */
  1235. X
  1236. X
  1237. Xunsigned hufts;         /* track memory usage */
  1238. X
  1239. X
  1240. Xint huft_build(b, n, s, d, e, t, m)
  1241. Xunsigned *b;            /* code lengths in bits (all assumed <= BMAX) */
  1242. Xunsigned n;             /* number of codes (assumed <= N_MAX) */
  1243. Xunsigned s;             /* number of simple-valued codes (0..s-1) */
  1244. Xush *d;                 /* list of base values for non-simple codes */
  1245. Xush *e;                 /* list of extra bits for non-simple codes */
  1246. Xstruct huft **t;        /* result: starting table */
  1247. Xint *m;                 /* maximum lookup bits, returns actual */
  1248. X/* Given a list of code lengths and a maximum table size, make a set of
  1249. X   tables to decode that set of codes.  Return zero on success, one if
  1250. X   the given code set is incomplete (the tables are still built in this
  1251. X   case), two if the input is invalid (all zero length codes or an
  1252. X   oversubscribed set of lengths), and three if not enough memory.
  1253. X   The code with value 256 is special, and the tables are constructed
  1254. X   so that no bits beyond that code are fetched when that code is
  1255. X   decoded. */
  1256. X{
  1257. X  unsigned a;                   /* counter for codes of length k */
  1258. X  unsigned c[BMAX+1];           /* bit length count table */
  1259. X  unsigned el;                  /* length of EOB code (value 256) */
  1260. X  unsigned f;                   /* i repeats in table every f entries */
  1261. X  int g;                        /* maximum code length */
  1262. X  int h;                        /* table level */
  1263. X  register unsigned i;          /* counter, current code */
  1264. X  register unsigned j;          /* counter */
  1265. X  register int k;               /* number of bits in current code */
  1266. X  int lx[BMAX+1];               /* memory for l[-1..BMAX-1] */
  1267. X  int *l = lx+1;                /* stack of bits per table */
  1268. X  register unsigned *p;         /* pointer into c[], b[], or v[] */
  1269. X  register struct huft *q;      /* points to current table */
  1270. X  struct huft r;                /* table entry for structure assignment */
  1271. X  struct huft *u[BMAX];         /* table stack */
  1272. X  static unsigned v[N_MAX];     /* values in order of bit length */
  1273. X  register int w;               /* bits before this table == (l * h) */
  1274. X  unsigned x[BMAX+1];           /* bit offsets, then code stack */
  1275. X  unsigned *xp;                 /* pointer into x */
  1276. X  int y;                        /* number of dummy codes added */
  1277. X  unsigned z;                   /* number of entries in current table */
  1278. X
  1279. X
  1280. X  /* Generate counts for each bit length */
  1281. X  el = n > 256 ? b[256] : BMAX; /* set length of EOB code, if any */
  1282. X  memzero((char *)c, sizeof(c));
  1283. X  p = b;  i = n;
  1284. X  do {
  1285. X    c[*p]++; p++;               /* assume all entries <= BMAX */
  1286. X  } while (--i);
  1287. X  if (c[0] == n)                /* null input--all zero length codes */
  1288. X  {
  1289. X    *t = (struct huft *)NULL;
  1290. X    *m = 0;
  1291. X    return 0;
  1292. X  }
  1293. X
  1294. X
  1295. X  /* Find minimum and maximum length, bound *m by those */
  1296. X  for (j = 1; j <= BMAX; j++)
  1297. X    if (c[j])
  1298. X      break;
  1299. X  k = j;                        /* minimum code length */
  1300. X  if ((unsigned)*m < j)
  1301. X    *m = j;
  1302. X  for (i = BMAX; i; i--)
  1303. X    if (c[i])
  1304. X      break;
  1305. X  g = i;                        /* maximum code length */
  1306. X  if ((unsigned)*m > i)
  1307. X    *m = i;
  1308. X
  1309. X
  1310. X  /* Adjust last length count to fill out codes, if needed */
  1311. X  for (y = 1 << j; j < i; j++, y <<= 1)
  1312. X    if ((y -= c[j]) < 0)
  1313. X      return 2;                 /* bad input: more codes than bits */
  1314. X  if ((y -= c[i]) < 0)
  1315. X    return 2;
  1316. X  c[i] += y;
  1317. X
  1318. X
  1319. X  /* Generate starting offsets into the value table for each length */
  1320. X  x[1] = j = 0;
  1321. X  p = c + 1;  xp = x + 2;
  1322. X  while (--i) {                 /* note that i == g from above */
  1323. X    *xp++ = (j += *p++);
  1324. X  }
  1325. X
  1326. X
  1327. X  /* Make a table of values in order of bit lengths */
  1328. X  p = b;  i = 0;
  1329. X  do {
  1330. X    if ((j = *p++) != 0)
  1331. X      v[x[j]++] = i;
  1332. X  } while (++i < n);
  1333. X
  1334. X
  1335. X  /* Generate the Huffman codes and for each, make the table entries */
  1336. X  x[0] = i = 0;                 /* first Huffman code is zero */
  1337. X  p = v;                        /* grab values in bit order */
  1338. X  h = -1;                       /* no tables yet--level -1 */
  1339. X  w = l[-1] = 0;                /* no bits decoded yet */
  1340. X  u[0] = (struct huft *)NULL;   /* just to keep compilers happy */
  1341. X  q = (struct huft *)NULL;      /* ditto */
  1342. X  z = 0;                        /* ditto */
  1343. X
  1344. X  /* go through the bit lengths (k already is bits in shortest code) */
  1345. X  for (; k <= g; k++)
  1346. X  {
  1347. X    a = c[k];
  1348. X    while (a--)
  1349. X    {
  1350. X      /* here i is the Huffman code of length k bits for value *p */
  1351. X      /* make tables up to required level */
  1352. X      while (k > w + l[h])
  1353. X      {
  1354. X        w += l[h++];            /* add bits already decoded */
  1355. X
  1356. X        /* compute minimum size table less than or equal to *m bits */
  1357. X        z = (z = g - w) > (unsigned)*m ? *m : z;        /* upper limit */
  1358. X        if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
  1359. X        {                       /* too few codes for k-w bit table */
  1360. X          f -= a + 1;           /* deduct codes from patterns left */
  1361. X          xp = c + k;
  1362. X          while (++j < z)       /* try smaller tables up to z bits */
  1363. X          {
  1364. X            if ((f <<= 1) <= *++xp)
  1365. X              break;            /* enough codes to use up j bits */
  1366. X            f -= *xp;           /* else deduct codes from patterns */
  1367. X          }
  1368. X        }
  1369. X        if ((unsigned)w + j > el && (unsigned)w < el)
  1370. X          j = el - w;           /* make EOB code end at table */
  1371. X        z = 1 << j;             /* table entries for j-bit table */
  1372. X        l[h] = j;               /* set table size in stack */
  1373. X
  1374. X        /* allocate and link in new table */
  1375. X        if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) ==
  1376. X            (struct huft *)NULL)
  1377. X        {
  1378. X          if (h)
  1379. X            huft_free(u[0]);
  1380. X          return 3;             /* not enough memory */
  1381. X        }
  1382. X        hufts += z + 1;         /* track memory usage */
  1383. X        *t = q + 1;             /* link to list for huft_free() */
  1384. X        *(t = &(q->v.t)) = (struct huft *)NULL;
  1385. X        u[h] = ++q;             /* table starts after link */
  1386. X
  1387. X        /* connect to last table, if there is one */
  1388. X        if (h)
  1389. X        {
  1390. X          x[h] = i;             /* save pattern for backing up */
  1391. X          r.b = (uch)l[h-1];    /* bits to dump before this table */
  1392. X          r.e = (uch)(16 + j);  /* bits in this table */
  1393. X          r.v.t = q;            /* pointer to this table */
  1394. X          j = (i & ((1 << w) - 1)) >> (w - l[h-1]);
  1395. X          u[h-1][j] = r;        /* connect to last table */
  1396. X        }
  1397. X      }
  1398. X
  1399. X      /* set up table entry in r */
  1400. X      r.b = (uch)(k - w);
  1401. X      if (p >= v + n)
  1402. X        r.e = 99;               /* out of values--invalid code */
  1403. X      else if (*p < s)
  1404. X      {
  1405. X        r.e = (uch)(*p < 256 ? 16 : 15);    /* 256 is end-of-block code */
  1406. X        r.v.n = *p++;           /* simple code is just the value */
  1407. X      }
  1408. X      else
  1409. X      {
  1410. X        r.e = (uch)e[*p - s];   /* non-simple--look up in lists */
  1411. X        r.v.n = d[*p++ - s];
  1412. X      }
  1413. X
  1414. X      /* fill code-like entries with r */
  1415. X      f = 1 << (k - w);
  1416. X      for (j = i >> w; j < z; j += f)
  1417. X        q[j] = r;
  1418. X
  1419. X      /* backwards increment the k-bit code i */
  1420. X      for (j = 1 << (k - 1); i & j; j >>= 1)
  1421. X        i ^= j;
  1422. X      i ^= j;
  1423. X
  1424. X      /* backup over finished tables */
  1425. X      while ((i & ((1 << w) - 1)) != x[h])
  1426. X        w -= l[--h];            /* don't need to update q */
  1427. X    }
  1428. X  }
  1429. X
  1430. X
  1431. X  /* return actual size of base table */
  1432. X  *m = l[0];
  1433. X
  1434. X
  1435. X  /* Return true (1) if we were given an incomplete table */
  1436. X  return y != 0 && g != 1;
  1437. X}
  1438. X
  1439. X
  1440. X
  1441. Xint huft_free(t)
  1442. Xstruct huft *t;         /* table to free */
  1443. X/* Free the malloc'ed tables built by huft_build(), which makes a linked
  1444. X   list of the tables it made, with the links in a dummy first entry of
  1445. X   each table. */
  1446. X{
  1447. X  register struct huft *p, *q;
  1448. X
  1449. X
  1450. X  /* Go through linked list, freeing from the malloced (t[-1]) address. */
  1451. X  p = t;
  1452. X  while (p != (struct huft *)NULL)
  1453. X  {
  1454. X    q = (--p)->v.t;
  1455. X    free(p);
  1456. X    p = q;
  1457. X  } 
  1458. X  return 0;
  1459. X}
  1460. X
  1461. X
  1462. X
  1463. X#ifdef ASM_INFLATECODES
  1464. X#  define inflate_codes(tl,td,bl,bd)  flate_codes(tl,td,bl,bd,(uch *)slide)
  1465. X   int flate_codes OF((struct huft *, struct huft *, int, int, uch *));
  1466. X
  1467. X#else
  1468. X
  1469. Xint inflate_codes(tl, td, bl, bd)
  1470. Xstruct huft *tl, *td;   /* literal/length and distance decoder tables */
  1471. Xint bl, bd;             /* number of bits decoded by tl[] and td[] */
  1472. X/* inflate (decompress) the codes in a deflated (compressed) block.
  1473. X   Return an error code or zero if it all goes ok. */
  1474. X{
  1475. X  register unsigned e;  /* table entry flag/number of extra bits */
  1476. X  unsigned n, d;        /* length and index for copy */
  1477. X  unsigned w;           /* current window position */
  1478. X  struct huft *t;       /* pointer to table entry */
  1479. X  unsigned ml, md;      /* masks for bl and bd bits */
  1480. X  register ulg b;       /* bit buffer */
  1481. X  register unsigned k;  /* number of bits in bit buffer */
  1482. X
  1483. X
  1484. X  /* make local copies of globals */
  1485. X  b = bb;                       /* initialize bit buffer */
  1486. X  k = bk;
  1487. X  w = wp;                       /* initialize window position */
  1488. X
  1489. X
  1490. X  /* inflate the coded data */
  1491. X  ml = mask[bl];           /* precompute masks for speed */
  1492. X  md = mask[bd];
  1493. X  while (1)                     /* do until end of block */
  1494. X  {
  1495. X    NEEDBITS((unsigned)bl)
  1496. X    if ((e = (t = tl + ((unsigned)b & ml))->e) > 16)
  1497. X      do {
  1498. X        if (e == 99)
  1499. X          return 1;
  1500. X        DUMPBITS(t->b)
  1501. X        e -= 16;
  1502. X        NEEDBITS(e)
  1503. X      } while ((e = (t = t->v.t + ((unsigned)b & mask[e]))->e) > 16);
  1504. X    DUMPBITS(t->b)
  1505. X    if (e == 16)                /* then it's a literal */
  1506. X    {
  1507. X      slide[w++] = (uch)t->v.n;
  1508. X      if (w == WSIZE)
  1509. X      {
  1510. X        FLUSH(w);
  1511. X        w = 0;
  1512. X      }
  1513. X    }
  1514. X    else                        /* it's an EOB or a length */
  1515. X    {
  1516. X      /* exit if end of block */
  1517. X      if (e == 15)
  1518. X        break;
  1519. X
  1520. X      /* get length of block to copy */
  1521. X      NEEDBITS(e)
  1522. X      n = t->v.n + ((unsigned)b & mask[e]);
  1523. X      DUMPBITS(e);
  1524. X
  1525. X      /* decode distance of block to copy */
  1526. X      NEEDBITS((unsigned)bd)
  1527. X      if ((e = (t = td + ((unsigned)b & md))->e) > 16)
  1528. X        do {
  1529. X          if (e == 99)
  1530. X            return 1;
  1531. X          DUMPBITS(t->b)
  1532. X          e -= 16;
  1533. X          NEEDBITS(e)
  1534. X        } while ((e = (t = t->v.t + ((unsigned)b & mask[e]))->e) > 16);
  1535. X      DUMPBITS(t->b)
  1536. X      NEEDBITS(e)
  1537. X      d = w - t->v.n - ((unsigned)b & mask[e]);
  1538. X      DUMPBITS(e)
  1539. X
  1540. X      /* do the copy */
  1541. X      do {
  1542. X        n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
  1543. X#ifndef NOMEMCPY
  1544. X        if (w - d >= e)         /* (this test assumes unsigned comparison) */
  1545. X        {
  1546. X          memcpy(slide + w, slide + d, e);
  1547. X          w += e;
  1548. X          d += e;
  1549. X        }
  1550. X        else                      /* do it slow to avoid memcpy() overlap */
  1551. X#endif /* !NOMEMCPY */
  1552. X          do {
  1553. X            slide[w++] = slide[d++];
  1554. X          } while (--e);
  1555. X        if (w == WSIZE)
  1556. X        {
  1557. X          FLUSH(w);
  1558. X          w = 0;
  1559. X        }
  1560. X      } while (n);
  1561. X    }
  1562. X  }
  1563. X
  1564. X
  1565. X  /* restore the globals from the locals */
  1566. X  wp = w;                       /* restore global window pointer */
  1567. X  bb = b;                       /* restore global bit buffer */
  1568. X  bk = k;
  1569. X
  1570. X
  1571. X  /* done */
  1572. X  return 0;
  1573. X}
  1574. X
  1575. X#endif /* ASM_INFLATECODES */
  1576. X
  1577. X
  1578. X
  1579. Xint inflate_stored()
  1580. X/* "decompress" an inflated type 0 (stored) block. */
  1581. X{
  1582. X  unsigned n;           /* number of bytes in block */
  1583. X  unsigned w;           /* current window position */
  1584. X  register ulg b;       /* bit buffer */
  1585. X  register unsigned k;  /* number of bits in bit buffer */
  1586. X
  1587. X
  1588. X  /* make local copies of globals */
  1589. X  Trace((stderr, "\nstored block"));
  1590. X  b = bb;                       /* initialize bit buffer */
  1591. X  k = bk;
  1592. X  w = wp;                       /* initialize window position */
  1593. X
  1594. X
  1595. X  /* go to byte boundary */
  1596. X  n = k & 7;
  1597. X  DUMPBITS(n);
  1598. X
  1599. X
  1600. X  /* get the length and its complement */
  1601. X  NEEDBITS(16)
  1602. X  n = ((unsigned)b & 0xffff);
  1603. X  DUMPBITS(16)
  1604. X  NEEDBITS(16)
  1605. X  if (n != (unsigned)((~b) & 0xffff))
  1606. X    return 1;                   /* error in compressed data */
  1607. X  DUMPBITS(16)
  1608. X
  1609. X
  1610. X  /* read and output the compressed data */
  1611. X  while (n--)
  1612. X  {
  1613. X    NEEDBITS(8)
  1614. X    slide[w++] = (uch)b;
  1615. X    if (w == WSIZE)
  1616. X    {
  1617. X      FLUSH(w);
  1618. X      w = 0;
  1619. X    }
  1620. X    DUMPBITS(8)
  1621. X  }
  1622. X
  1623. X
  1624. X  /* restore the globals from the locals */
  1625. X  wp = w;                       /* restore global window pointer */
  1626. X  bb = b;                       /* restore global bit buffer */
  1627. X  bk = k;
  1628. X  return 0;
  1629. X}
  1630. X
  1631. X
  1632. X/* Globals for literal tables (built once) */
  1633. Xstruct huft *fixed_tl = (struct huft *)NULL;
  1634. Xstruct huft *fixed_td;
  1635. Xint fixed_bl, fixed_bd;
  1636. X
  1637. Xint inflate_fixed()
  1638. X/* decompress an inflated type 1 (fixed Huffman codes) block.  We should
  1639. X   either replace this with a custom decoder, or at least precompute the
  1640. X   Huffman tables. */
  1641. X{
  1642. X  /* if first time, set up tables for fixed blocks */
  1643. X  Trace((stderr, "\nliteral block"));
  1644. X  if (fixed_tl == (struct huft *)NULL)
  1645. X  {
  1646. X    int i;                /* temporary variable */
  1647. X    static unsigned l[288]; /* length list for huft_build */
  1648. X
  1649. X    /* literal table */
  1650. X    for (i = 0; i < 144; i++)
  1651. X      l[i] = 8;
  1652. X    for (; i < 256; i++)
  1653. X      l[i] = 9;
  1654. X    for (; i < 280; i++)
  1655. X      l[i] = 7;
  1656. X    for (; i < 288; i++)          /* make a complete, but wrong code set */
  1657. X      l[i] = 8;
  1658. X    fixed_bl = 7;
  1659. X    if ((i = huft_build(l, 288, 257, cplens, cplext,
  1660. X                        &fixed_tl, &fixed_bl)) != 0)
  1661. X    {
  1662. X      fixed_tl = (struct huft *)NULL;
  1663. X      return i;
  1664. X    }
  1665. X
  1666. X    /* distance table */
  1667. X    for (i = 0; i < 30; i++)      /* make an incomplete code set */
  1668. X      l[i] = 5;
  1669. X    fixed_bd = 5;
  1670. X    if ((i = huft_build(l, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd)) > 1)
  1671. X    {
  1672. X      huft_free(fixed_tl);
  1673. X      fixed_tl = (struct huft *)NULL;
  1674. X      return i;
  1675. X    }
  1676. X  }
  1677. X
  1678. X
  1679. X  /* decompress until an end-of-block code */
  1680. X  return inflate_codes(fixed_tl, fixed_td, fixed_bl, fixed_bd) != 0;
  1681. X}
  1682. X
  1683. X
  1684. X
  1685. Xint inflate_dynamic()
  1686. X/* decompress an inflated type 2 (dynamic Huffman codes) block. */
  1687. X{
  1688. X  int i;                /* temporary variables */
  1689. X  unsigned j;
  1690. X  unsigned l;           /* last length */
  1691. X  unsigned m;           /* mask for bit lengths table */
  1692. X  unsigned n;           /* number of lengths to get */
  1693. X  struct huft *tl;      /* literal/length code table */
  1694. X  struct huft *td;      /* distance code table */
  1695. X  int bl;               /* lookup bits for tl */
  1696. X  int bd;               /* lookup bits for td */
  1697. X  unsigned nb;          /* number of bit length codes */
  1698. X  unsigned nl;          /* number of literal/length codes */
  1699. X  unsigned nd;          /* number of distance codes */
  1700. X#ifdef PKZIP_BUG_WORKAROUND
  1701. X  static unsigned ll[288+32]; /* literal/length and distance code lengths */
  1702. X#else
  1703. X  static unsigned ll[286+30]; /* literal/length and distance code lengths */
  1704. X#endif
  1705. X  register ulg b;       /* bit buffer */
  1706. X  register unsigned k;  /* number of bits in bit buffer */
  1707. X
  1708. X
  1709. X  /* make local bit buffer */
  1710. X  Trace((stderr, "\ndynamic block"));
  1711. X  b = bb;
  1712. X  k = bk;
  1713. X
  1714. X
  1715. X  /* read in table lengths */
  1716. X  NEEDBITS(5)
  1717. X  nl = 257 + ((unsigned)b & 0x1f);      /* number of literal/length codes */
  1718. X  DUMPBITS(5)
  1719. X  NEEDBITS(5)
  1720. X  nd = 1 + ((unsigned)b & 0x1f);        /* number of distance codes */
  1721. X  DUMPBITS(5)
  1722. X  NEEDBITS(4)
  1723. X  nb = 4 + ((unsigned)b & 0xf);         /* number of bit length codes */
  1724. X  DUMPBITS(4)
  1725. X#ifdef PKZIP_BUG_WORKAROUND
  1726. X  if (nl > 288 || nd > 32)
  1727. X#else
  1728. X  if (nl > 286 || nd > 30)
  1729. X#endif
  1730. X    return 1;                   /* bad lengths */
  1731. X
  1732. X
  1733. X  /* read in bit-length-code lengths */
  1734. X  for (j = 0; j < nb; j++)
  1735. X  {
  1736. X    NEEDBITS(3)
  1737. X    ll[border[j]] = (unsigned)b & 7;
  1738. X    DUMPBITS(3)
  1739. X  }
  1740. X  for (; j < 19; j++)
  1741. X    ll[border[j]] = 0;
  1742. X
  1743. X
  1744. X  /* build decoding table for trees--single level, 7 bit lookup */
  1745. X  bl = 7;
  1746. X  if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
  1747. X  {
  1748. X    if (i == 1)
  1749. X      huft_free(tl);
  1750. X    return i;                   /* incomplete code set */
  1751. X  }
  1752. X
  1753. X
  1754. X  /* read in literal and distance code lengths */
  1755. X  n = nl + nd;
  1756. X  m = mask[bl];
  1757. X  i = l = 0;
  1758. X  while ((unsigned)i < n)
  1759. X  {
  1760. X    NEEDBITS((unsigned)bl)
  1761. X    j = (td = tl + ((unsigned)b & m))->b;
  1762. X    DUMPBITS(j)
  1763. X    j = td->v.n;
  1764. X    if (j < 16)                 /* length of code in bits (0..15) */
  1765. X      ll[i++] = l = j;          /* save last length in l */
  1766. X    else if (j == 16)           /* repeat last length 3 to 6 times */
  1767. X    {
  1768. X      NEEDBITS(2)
  1769. X      j = 3 + ((unsigned)b & 3);
  1770. X      DUMPBITS(2)
  1771. X      if ((unsigned)i + j > n)
  1772. X        return 1;
  1773. X      while (j--)
  1774. X        ll[i++] = l;
  1775. X    }
  1776. X    else if (j == 17)           /* 3 to 10 zero length codes */
  1777. X    {
  1778. X      NEEDBITS(3)
  1779. X      j = 3 + ((unsigned)b & 7);
  1780. X      DUMPBITS(3)
  1781. X      if ((unsigned)i + j > n)
  1782. X        return 1;
  1783. X      while (j--)
  1784. X        ll[i++] = 0;
  1785. X      l = 0;
  1786. X    }
  1787. X    else                        /* j == 18: 11 to 138 zero length codes */
  1788. X    {
  1789. X      NEEDBITS(7)
  1790. X      j = 11 + ((unsigned)b & 0x7f);
  1791. X      DUMPBITS(7)
  1792. X      if ((unsigned)i + j > n)
  1793. X        return 1;
  1794. X      while (j--)
  1795. X        ll[i++] = 0;
  1796. X      l = 0;
  1797. X    }
  1798. X  }
  1799. X
  1800. X
  1801. X  /* free decoding table for trees */
  1802. X  huft_free(tl);
  1803. X
  1804. X
  1805. X  /* restore the global bit buffer */
  1806. X  bb = b;
  1807. X  bk = k;
  1808. X
  1809. X
  1810. X  /* build the decoding tables for literal/length and distance codes */
  1811. X  bl = lbits;
  1812. X  if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
  1813. X  {
  1814. X    if (i == 1 && !qflag) {
  1815. X      FPRINTF(stderr, "(incomplete l-tree)  ");
  1816. X      huft_free(tl);
  1817. X    }
  1818. X    return i;                   /* incomplete code set */
  1819. X  }
  1820. X  bd = dbits;
  1821. X  if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
  1822. X  {
  1823. X    if (i == 1 && !qflag) {
  1824. X      FPRINTF(stderr, "(incomplete d-tree)  ");
  1825. X#ifdef PKZIP_BUG_WORKAROUND
  1826. X      i = 0;
  1827. X    }
  1828. X#else
  1829. X      huft_free(td);
  1830. X    }
  1831. X    huft_free(tl);
  1832. X    return i;                   /* incomplete code set */
  1833. X#endif
  1834. X  }
  1835. X
  1836. X
  1837. X  /* decompress until an end-of-block code */
  1838. X  if (inflate_codes(tl, td, bl, bd))
  1839. X    return 1;
  1840. X
  1841. X
  1842. X  /* free the decoding tables, return */
  1843. X  huft_free(tl);
  1844. X  huft_free(td);
  1845. X  return 0;
  1846. X}
  1847. X
  1848. X
  1849. X
  1850. Xint inflate_block(e)
  1851. Xint *e;                 /* last block flag */
  1852. X/* decompress an inflated block */
  1853. X{
  1854. X  unsigned t;           /* block type */
  1855. X  register ulg b;       /* bit buffer */
  1856. X  register unsigned k;  /* number of bits in bit buffer */
  1857. X
  1858. X
  1859. X  /* make local bit buffer */
  1860. X  b = bb;
  1861. X  k = bk;
  1862. X
  1863. X
  1864. X  /* read in last block bit */
  1865. X  NEEDBITS(1)
  1866. X  *e = (int)b & 1;
  1867. X  DUMPBITS(1)
  1868. X
  1869. X
  1870. X  /* read in block type */
  1871. X  NEEDBITS(2)
  1872. X  t = (unsigned)b & 3;
  1873. X  DUMPBITS(2)
  1874. X
  1875. X
  1876. X  /* restore the global bit buffer */
  1877. X  bb = b;
  1878. X  bk = k;
  1879. X
  1880. X
  1881. X  /* inflate that block type */
  1882. X  if (t == 2)
  1883. X    return inflate_dynamic();
  1884. X  if (t == 0)
  1885. X    return inflate_stored();
  1886. X  if (t == 1)
  1887. X    return inflate_fixed();
  1888. X
  1889. X
  1890. X  /* bad block type */
  1891. X  return 2;
  1892. X}
  1893. X
  1894. X
  1895. X
  1896. Xint inflate()
  1897. X/* decompress an inflated entry */
  1898. X{
  1899. X  int e;                /* last block flag */
  1900. X  int r;                /* result code */
  1901. X  unsigned h;           /* maximum struct huft's malloc'ed */
  1902. X
  1903. X
  1904. X  /* initialize window, bit buffer */
  1905. X  wp = 0;
  1906. X  bk = 0;
  1907. X  bb = 0;
  1908. X
  1909. X
  1910. X  /* decompress until the last block */
  1911. X  h = 0;
  1912. X  do {
  1913. X    hufts = 0;
  1914. X    if ((r = inflate_block(&e)) != 0)
  1915. X      return r;
  1916. X    if (hufts > h)
  1917. X      h = hufts;
  1918. X  } while (!e);
  1919. X
  1920. X
  1921. X  /* flush out slide */
  1922. X  FLUSH(wp);
  1923. X
  1924. X
  1925. X  /* return success */
  1926. X  Trace((stderr, "\n%u bytes in Huffman tables (%d/entry)\n",
  1927. X         h * sizeof(struct huft), sizeof(struct huft)));
  1928. X  return 0;
  1929. X}
  1930. X
  1931. X
  1932. X
  1933. Xint inflate_free()
  1934. X{
  1935. X  if (fixed_tl != (struct huft *)NULL)
  1936. X  {
  1937. X    huft_free(fixed_td);
  1938. X    huft_free(fixed_tl);
  1939. X    fixed_td = fixed_tl = (struct huft *)NULL;
  1940. X  }
  1941. X  return 0;
  1942. X}
  1943. END_OF_FILE
  1944.   if test 38275 -ne `wc -c <'unzip-5.12/inflate.c'`; then
  1945.     echo shar: \"'unzip-5.12/inflate.c'\" unpacked with wrong size!
  1946.   fi
  1947.   # end of 'unzip-5.12/inflate.c'
  1948. fi
  1949. echo shar: End of archive 9 \(of 20\).
  1950. cp /dev/null ark9isdone
  1951. MISSING=""
  1952. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ; do
  1953.     if test ! -f ark${I}isdone ; then
  1954.     MISSING="${MISSING} ${I}"
  1955.     fi
  1956. done
  1957. if test "${MISSING}" = "" ; then
  1958.     echo You have unpacked all 20 archives.
  1959.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1960. else
  1961.     echo You still must unpack the following archives:
  1962.     echo "        " ${MISSING}
  1963. fi
  1964. exit 0
  1965. exit 0 # Just in case...
  1966.