home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / vmsnet / sources / 321 < prev    next >
Encoding:
Internet Message Format  |  1992-09-01  |  47.6 KB

  1. Path: sparky!uunet!wupost!zaphod.mps.ohio-state.edu!news.acns.nwu.edu!network.ucsd.edu!mvb.saic.com!vmsnet-sources
  2. From: goathunter@wkuvx1.bitnet
  3. Newsgroups: vmsnet.sources
  4. Subject: Zip v1.9 & UnZip v5.0, part 04/22
  5. Message-ID: <8009596@MVB.SAIC.COM>
  6. Date: Tue, 01 Sep 1992 22:49:25 GMT
  7. Organization: Western Kentucky University, Bowling Green, KY
  8. Lines: 1216
  9. Approved: Mark.Berryman@Mvb.Saic.Com
  10.  
  11. Submitted-by: goathunter@wkuvx1.bitnet
  12. Posting-number: Volume 3, Issue 126
  13. Archive-name: zip_unzip/part04
  14.  
  15. -+-+-+-+-+-+-+-+ START OF PART 4 -+-+-+-+-+-+-+-+
  16. X  0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
  17. X  0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
  18. X  0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
  19. X  0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
  20. X  0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
  21. X  0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
  22. X  0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
  23. X  0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
  24. X  0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
  25. X  0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
  26. X  0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
  27. X  0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
  28. X  0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
  29. X  0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
  30. X  0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
  31. X  0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
  32. X  0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
  33. X  0x2d02ef8dL
  34. X`7D;
  35. X
  36. X
  37. XULONG updcrc(s, n)
  38. Xbyte *s;                /* pointer to bytes to pump through */
  39. Xint n;                  /* number of bytes in s`5B`5D */
  40. X/* Run a set of bytes through the crc shift register.  If s is a NULL
  41. X   pointer, then initialize the crc shift register contents instead.
  42. X   Return the current crc in either case. */
  43. X`7B
  44. X  register ULONG c;       /* temporary variable */
  45. X
  46. X  static ULONG crc = 0xffffffffL; /* shift register contents */
  47. X
  48. X  if (s == NULL)
  49. X    c = 0xffffffffL;
  50. X  else
  51. X  `7B
  52. X    c = crc;
  53. X    while (n--)
  54. X      c = crc_32_tab`5B((int)c `5E (*s++)) & 0xff`5D `5E (c >> 8);
  55. X  `7D
  56. X  crc = c;
  57. X  return c `5E 0xffffffffL;       /* (instead of `7Ec for 64-bit machines) *
  58. V/
  59. X`7D
  60. X
  61. X
  62. Xvoid err(n, m)
  63. Xint n;
  64. Xchar *m;
  65. X/* Exit on error with a message and a code */
  66. X`7B
  67. X  fprintf(stderr, "funzip error: %s\n", m);
  68. X  exit(n);
  69. X`7D
  70. X
  71. X
  72. Xint ReadByte(b)
  73. XUWORD *b;
  74. X/* Used by inflate.c to get a byte (archaism from unzip) */
  75. X`7B
  76. X  register int c = getc(in);
  77. X
  78. X  if (c == EOF)
  79. X    return 0;
  80. X#ifdef CRYPT
  81. X  if (decrypt)
  82. X    update_keys(c `5E= decrypt_byte());
  83. X#endif /* CRYPT */
  84. X  *b = (UWORD)c;
  85. X  return 8;
  86. X`7D
  87. X
  88. X
  89. Xint FlushOutput()
  90. X/* Empty output buffer */
  91. X`7B
  92. X  if (outcnt)
  93. X  `7B
  94. X    updcrc(outbuf, outcnt);
  95. X    if (fwrite(outbuf, 1, outcnt, out) != outcnt)
  96. X      err(9, "out of space on stdout");
  97. X    outsiz += outcnt;
  98. X    outptr = outbuf;
  99. X    outcnt = 0;
  100. X  `7D
  101. X  return 0;
  102. X`7D
  103. X
  104. X
  105. Xvoid main(argc, argv)
  106. Xint argc;
  107. Xchar **argv;
  108. X/* Given a zip file on stdin, decompress the first entry to stdout. */
  109. X`7B
  110. X  byte h`5BLOCHDR`5D;               /* first local header */
  111. X
  112. X  /* if stdin not redirected, give the user help */
  113. X  if (isatty(0))
  114. X  `7B
  115. X    fprintf(stderr,
  116. X#ifdef CRYPT
  117. X      "usage: funzip `5Bpassword`5D < infile.zip > outfile\n");
  118. X#else /* !CRYPT */
  119. X      "usage: funzip < infile.zip > outfile\n");
  120. X#endif /* ?CRYPT */
  121. X    fprintf(stderr,
  122. X      "       extracts to stdout the first zip entry of stdin.\n");
  123. X    exit(3);
  124. X  `7D
  125. X
  126. X  /* prepare to be a binary filter */
  127. X  if ((outbuf = (byte *)malloc(OUTBUFSIZ)) == NULL)
  128. X    err(1, "out of memory");
  129. X  if ((in = fdopen(0, FOPR)) == NULL)
  130. X    err(2, "cannot find stdin");
  131. X  if ((out = fdopen(1, FOPW)) == NULL)
  132. X    err(2, "cannot write to stdout");
  133. X
  134. X  /* read local header, check validity, and skip name and extra fields */
  135. X  if (fread((char *)h, 1, LOCHDR, in) != LOCHDR `7C`7C LG(h) != LOCSIG)
  136. X    err(3, "input not a zip file or empty");
  137. X  if (SH(h + LOCHOW) != STORED && SH(h + LOCHOW) != DEFLATED)
  138. X    err(3, "first entry not deflated or stored--can't funzip");
  139. X  fseek(in, (long)(SH(h + LOCFIL)) + (long)(SH(h + LOCEXT)), 1);
  140. X
  141. X  /* if entry encrypted, decrypt and validate encryption header */
  142. X  if ((decrypt = h`5BLOCFLG`5D & CRPFLG) != 0)
  143. X#ifdef CRYPT
  144. X  `7B
  145. X    UWORD i, e;
  146. X
  147. X    if (argc < 2)
  148. X      err(3, "need password on command line for encrypted entry");
  149. X    init_keys(argv`5B1`5D);
  150. X    for (i = 0; i < 10; i++)
  151. X      ReadByte(&e);
  152. X    ReadByte(&e);
  153. X    ReadByte(&i);
  154. X    e += i << 8;
  155. X    if (e != (h`5BLOCFLG`5D & EXTFLG ? SH(h + LOCTIM) : SH(h + LOCCRC + 2)))
  156. X      err(3, "incorrect password for first entry");
  157. X  `7D
  158. X#else /* !CRYPT */
  159. X    err(3, "cannot decrypt entry (need to recompile funzip with crypt.c)");
  160. X#endif /* ?CRYPT */
  161. X
  162. X  /* prepare output buffer and crc */
  163. X  outptr = outbuf;
  164. X  outcnt = 0;
  165. X  outsiz = 0L;
  166. X  updcrc(NULL, 0);
  167. X
  168. X  /* decompress */
  169. X  if (h`5BLOCHOW`5D)
  170. X  `7B                             /* deflated entry */
  171. X    if (inflate_entry())
  172. X      err(4, "invalid compressed data or out of memory");
  173. X  `7D
  174. X  else
  175. X  `7B                             /* stored entry */
  176. X    register ULONG n;
  177. X
  178. X    n = LG(h + LOCLEN);
  179. X    if (n != LG(h + LOCSIZ))
  180. X      err(4, "invalid compressed data--length mismatch");
  181. X    while (n--)
  182. X      OUTB(getc(in));
  183. X  `7D
  184. X  FlushOutput();
  185. X  fflush(out);
  186. X
  187. X  /* if extended header, get it */
  188. X  if ((h`5BLOCFLG`5D & EXTFLG) &&
  189. X      fread((char *)h + LOCCRC - 4, 1, EXTHDR, in) != EXTHDR)
  190. X    err(3, "zip file ended prematurely");
  191. X
  192. X  /* validate decompression */
  193. X  if (LG(h + LOCCRC) != updcrc(outbuf, 0))
  194. X    err(4, "invalid compressed data--crc error");
  195. X  if (LG(h + LOCLEN) != outsiz)
  196. X    err(4, "invalid compressed data--length error");
  197. X
  198. X  /* check if there are more entries */
  199. X  if (fread((char *)h, 1, 4, in) == 4 && LG(h) == LOCSIG)
  200. X    fprintf(stderr,
  201. X      "funzip warning: zip file has more than one entry--rest ignored\n");
  202. X`7D
  203. $ CALL UNPACK [.UNZIP50]FUNZIP.C;1 1614356133
  204. $ create 'f'
  205. X
  206. XFUNZIP(1)                USER COMMANDS                  FUNZIP(1)
  207. X
  208. XNAME
  209. X     funzip - extract from a ZIP archive file as a filter
  210. X
  211. XSYNOPSIS
  212. X     `5B...`5D  `7C  funzip `5B password `5D  `7C  `5B...`5D
  213. X
  214. XARGUMENTS
  215. X     `5Bpassword`5D  Optional password to be used if ZIP  archive  is
  216. X                 encrypted.   Decryption  may not be supported at
  217. X                 some sites.  See DESCRIPTION for more details.
  218. X
  219. XDESCRIPTION
  220. X     FUnZip acts as a filter; that is,  it  assumes  that  a  ZIP
  221. X     archive  is being piped into standard input, and it extracts
  222. X     the first member from the archive to stdout.  Given the lim-
  223. X     itation  on  single-member extraction, FUnZip is most useful
  224. X     in conjunction with a secondary  archiver  program  such  as
  225. X     tar(1).   The  following  section includes an example illus-
  226. X     trating this usage in the case of disk backups to tape.
  227. X
  228. XEXAMPLES
  229. X     To use FUnZip to  extract  the  first  member  file  of  the
  230. X     archive test.zip and to pipe it into more(1):
  231. X
  232. X          funzip < test.zip `7C more
  233. X
  234. X     To use FUnZip to test the first member file of test.zip (any
  235. X     errors will be reported on standard error):
  236. X
  237. X          funzip < test.zip > /dev/null
  238. X
  239. X     To use Zip and FUnZip in place of  compress(1)  and  zcat(1)
  240. X     for tape backups:
  241. X
  242. X          tar cf - . `7C zip -7 `7C dd of=/dev/nrst0 obs=8k
  243. X
  244. X          dd if=/dev/nrst0 ibs=8k `7C funzip `7C tar xf -
  245. X
  246. X     (where, for example, nrst0 is a SCSI tape drive).
  247. X
  248. XLIMITATIONS
  249. X     There is presently no way to  extract  any  member  but  the
  250. X     first  from a ZIP archive.  This would be useful in the case
  251. X     where a ZIP archive is included within another archive.
  252. X
  253. X     An alternate mechanism for passing the  password  to  FUnZip
  254. X     would be preferable to putting it on the command line.
  255. X
  256. X     FUnZip would be useful under OS/2, too.
  257. X
  258. X     The functionality of FUnZip  should  be  included  in  UnZip
  259. X     directly (future release).
  260. X
  261. XInfo-ZIP          Last change: 19 Aug 92 (v1.3)                 1
  262. X
  263. XFUNZIP(1)                USER COMMANDS                  FUNZIP(1)
  264. X
  265. XSEE ALSO
  266. X     unzip(1),  zip(1),  zipcloak(1),   zipinfo(1),   zipnote(1),
  267. X     zipsplit(1)
  268. X
  269. XAUTHOR
  270. X     Mark Adler (Info-ZIP)
  271. X
  272. XInfo-ZIP          Last change: 19 Aug 92 (v1.3)                 2
  273. X
  274. $ CALL UNPACK [.UNZIP50]FUNZIP.DOC;1 1631257473
  275. $ create 'f'
  276. XUnZip version 5.0, 21 August 1992
  277. X
  278. XNew features, you betcha:
  279. X
  280. X - inflation `5BMark Adler`5D
  281. X - unimploding (exploding) rewritten--much faster, no copyright restrictions
  282. X    `5BMark Adler`5D
  283. X - updated makefiles for inflation and explosion `5BJean-loup Gailly, Glenn
  284. X    Andrews, Mark Adler, CN, ...`5D
  285. X - multiple password guessing `5BCN, Mark Adler`5D
  286. X - OS/2:  extended attributes code, including decompression `5BKai Uwe Romme
  287. Vl`5D
  288. X - MS-DOS:  Quick C makefile `5BRichard Seay`5D
  289. X - new public domain version of match.c to replace copyrighted SEA version
  290. X    `5BJ. Kercheval, David Kirschbaum`5D
  291. X - INBUFSIZ may be changed on compile line (e.g., -DINBUFSIZ=8192) `5BSteve
  292. X    Salisbury`5D
  293. X - Mac:  new extra-field resource fork stuff `5BJohnny Lee`5D
  294. X - VMS:  f*def.h header files included within VMSmunch.h for neatness `5BCN`
  295. V5D
  296. X - new zipinfo formats (well, slightly different), including the default,
  297. X    allowing longer filenames without wrapping `5BCN`5D
  298. X - special file perms (sticky bits, etc.) now restored properly (with excep-
  299. X    tion of compressed symlinks, as noted in BUGS) `5BMark Adler, CN`5D
  300. X - Linux support `5BHumberto Ortiz-Zuazaga`5D
  301. X - MS-DOS:  gcc support (DJ DeLorie's) `5BOnno van der Linden`5D
  302. X - UNZIP/ZIPINFO environment variable support `5BCN, Bill Davidsen`5D
  303. X - smarter zipinfo defaults, including optional header and totals lines `5BC
  304. VN`5D
  305. X - zipinfo OS/2 EA information `5BKai Uwe Rommel`5D
  306. X - Convex C-120, C-210 targets added `5BRafal Maszkowski`5D
  307. X - signal handler for restoring echo if `5EC during password, catch segmenta
  308. Vtion
  309. X    violations/bus errors `5BMark Adler, CN`5D
  310. X - VMS:  RMS services used for (nearly) everything, instead of QIO/ACP `5BIg
  311. Vor`5D
  312. X - MS-DOS:  Borland C++ makefile to replace individual unzip`7B_cr`7D, zipin
  313. Vfo`20
  314. X    ones `5BAlvin Koh, David Kirschbaum`5D
  315. X - MS-DOS:  new Turbo C project files and zipinfo batch file `5BCharles Scri
  316. Vpter`5D
  317. X - VMS:  added overwrite/create-new capability for existing files `5BIgor`5D
  318. X - Cray scc v3.0 target added `5BCN`5D
  319. X - Olivetti X/OS target added `5BFulvio Marino`5D
  320. X - OS/2:  makefile.os2 provided in lieu of OS/2 targets in Makefile `5BKai U
  321. Vwe`5D
  322. X - "all" and "install" targets added to main Makefile `5BCN, Mark, Alvin Koh
  323. V`5D
  324. X - MS-DOS:  massive Windows port (only patches to existing source included;
  325. X    also need stand-alone Windows sources from wunz12sr.zip) `5BRobert Heath
  326. V,
  327. X    Johnny Lee`5D
  328. X - OS/2:  added support for Watcom C/386 9.0, in addition to Microsoft C 6.0
  329. V,
  330. X    IBM C Set/2, and gcc/emx `5BKai Uwe`5D
  331. X - AT&T 6300+/SysV target added `5BPeter Mauzey`5D
  332. X - Windows NT port `5BDavid Feinleib`5D
  333. X - reprompt for overwrite instead of assuming "n" if user error `5BMark, Mik
  334. Ve
  335. X    Freeman`5D
  336. X - new (*very* new) funzip.c:  read zipfile from stdin, extract first member
  337. X    to stdout (now includes decryption, too) `5BMark`5D
  338. X - funzip man page, Where file `5BCN`5D
  339. X - 386BSD 0.1 support added `5BOnno van der Linden`5D
  340. X
  341. XBugs fixed:
  342. X
  343. X - MS-DOS:  incorrect use of FP_ macros in dateformat() fixed `5BSteve Salis
  344. Vbury`5D
  345. X - dateformat() only called once now `5BSteve Salisbury, CN`5D
  346. X - "#if defined(x) `7C`7C defined(y)" causes problems with some compilers (B
  347. Vorland
  348. X    C++ 3); globally replaced with "#if (defined(x) `7C`7C defined(y))" `5BA
  349. Vlvin Koh`5D
  350. X - "Deflating" message corrected to "Inflating" `5BJames Birdsall`5D
  351. X - GNU C prototype conflicts fixed (crypt.c, file_io.c) `5BJean-loup Gailly`
  352. V5D
  353. X - OS/2:  stderr needs fflush for IBM C Set/2 compiler (as did Amiga); added
  354. X    for all compilers except Mac Progammer's Workbench `5BCN, Jon Saxton`5D
  355. X - VMS:  multiple-dot filenames now converted properly `5BIgor Mandrichenko`
  356. V5D
  357. X - decryption version:  time.h now only included in unzip.h, not crypt.c
  358. X    (requires crypt.c "v1.5" or later) `5BCN, Jean-loup Gailly`5D
  359. X - changed use of obsolescent S_IEXEC to S_IXUSR `5BHumberto Ortiz-Zuazaga`5
  360. VD
  361. X - SCO optimization bug in unshrink fixed `5BOnno van der Linden`5D
  362. X - aviion target moved to _sysv section of makefile for password echoing
  363. X    `5BCN, Benjamin Goldsteen`5D
  364. X - less reliance on __STDC__; now using zip's MODERN and PROTO macros `5BCN,
  365. X    Mark Adler, ...`5D
  366. X - stdlib.h not included for buggy compilers which define __STDC__ anyway
  367. X    (GNU C, Apollo) `5BCN`5D
  368. X - MS-DOS:  multiple dots in filenames handled correctly `5BCN`5D
  369. X - corrected explode(), matche_...() prototypes `5Bvarious people`5D
  370. X - changed zipinfo targets in Makefile to use ln or copy rather than
  371. X    mv/rename for misc_.c `5BKjetil J`7B\o`7Drgenson`5D
  372. X - corrected (or attempted to correct) zipinfo project files for Borland
  373. X    (misc_.obj, -DZIPINFO) `5BCN, Jean-loup`5D
  374. X - updated BCC .mak files for unzip 5.0 `5BAlvin Koh`5D
  375. X - MS-DOS:  rewrote msc_dos target in Makefile to get back under DOS 128-
  376. X    character command-line limit (I hope) `5BCN`5D
  377. X - Unix:  use "unix" predefined macro instead of -DUNIX `5BCN`5D
  378. X - VMS:  revision dates once again set properly
  379. X - added typecasts, etc., to get rid of nearly all warnings `5BCN, Charles
  380. X    Scripter, Mark Adler`5D
  381. X - fixed "zipfile corrupt" bug for empty zipfiles `5BCN`5D
  382. X - OS/2, Unix:  high-order (European) characters allowed in filenames `5BKai
  383. V Uwe,
  384. X    CN`5D
  385. X - MS-DOS:  patches for TC 2.0 and BC++ 3.0 preprocessors and for TCC and BC
  386. VC
  387. X    themselves to handle Unix-style line endings (LF) correctly (in MSDOS su
  388. Vb-
  389. X    archive) `5BOnno van der Linden`5D
  390. X - added missing <signal.h> `5BIgor, Kai Uwe, Jean-loup, many others`5D
  391. X - modified sysv target and unzip.h to allow SysV-ish Suns to compile right
  392. X    `5BCN`5D
  393. X - removed some SysV.4 warnings `5BJean-loup`5D
  394. X - OS/2:  added comments in zipinfo and man page to forestall queries about
  395. X    "EA's stored not equal to what dir reports" `5BCN, Kai Uwe`5D
  396. X - patches for RS/6000 + AIX `5BTrevor Paquette`5D
  397. X - OS/2:  "smart" upper/lowercasing depending on national language code page
  398. X    `5BKai Uwe`5D
  399. X - updated VMS, MS-DOS, and AMIGA sub-archives for release `5BCN`5D
  400. X - fixed zi_gcc makefile target `5BCN, Bo Kullmar`5D
  401. X - fixed size_t/extent bug for non-ANSI compilers `5BCN`5D
  402. X - fixed zipinfo EOL bug `5BCN`5D
  403. X - updated OS2 sub-archive and main Makefile (OS/2 targets, crypt, etc.) for
  404. X    release `5BCN`5D
  405. X - added typecasts to get rid of remaining inflate/explode warnings `5BCN`5D
  406. X - MS-DOS:  updated BCC project files `5BAlvin Koh`5D
  407. X - Mac, MTS:  fixed work-area initialization bug and added missing isascii
  408. X    definition `5BAntoine Verheijen`5D
  409. X - Mac:  updated Think C resource file `5BAntoine`5D
  410. X - fixed (I hope) couple of conversion warnings in extract.c `5BCN, Charles`
  411. V5D
  412. X - updated NoGate error message (PAK 2.51) `5BCN, Keith Petersen`5D
  413. X - VMS:  corrected gcc XAB definition problem; cleaned up make_unzip_gcc.com
  414. X    `5BIgor, Mike Freeman, CN`5D
  415. X - MS-DOS:  fixed msc_dos target in Makefile `5BPiet W. Plomp`5D
  416. X - fixed sco_dos stack overflow bug and allowed zipinfo to be compiled as we
  417. Vll,
  418. X    provisionally (UNZIPS variable) `5BBill Davidsen, CN`5D
  419. X - OS/2:  added -DMSC to msc target in makefile.os2 (and msc_os2 in Makefile
  420. V),
  421. X    removed -Fb, changed some icc settings; removed references to main Makef
  422. Vile
  423. X    `5BKai Uwe, CN`5D
  424. X - OS/2:  simplified default unzip.h defines `5BKai Uwe`5D
  425. X - OS/2, MS-DOS:  reversed meaning of -s (now spaces allowed by default) `5B
  426. VCN,
  427. X    Kai Uwe`5D
  428. X - OS/2:  changed extra-field format to official PKWare format `5BKai Uwe`5D
  429. X - typecast all NULLs appropriately to avoid AIX warnings `5BCN, Trevor Paqu
  430. Vette,
  431. X    Christopher Tjon`5D
  432. X - changed zfwrite definition in crypt.c to match zip's `5BJean-loup`5D
  433. X - fixed some WinNT problems `5BDave Feinleib, CN`5D
  434. X - fixed SysV.4 warning regarding signal.h placement `5BJean-loup`5D
  435. X - fixed c120 target and renamed NMAX to N_MAX inflate.c to avoid Convex
  436. X    redefinition warning `5BCN, Rafal Maszkowski`5D
  437. X - fixed bad comment line in Makefile `5BMark, CN`5D
  438. X - fixed memcpy/byte-ordering problem in memextract() `5BCN, Kai Uwe`5D
  439. X - updated funzip (1.1) `5BMark`5D
  440. X - updated unzip/zipinfo error message to include Norwegian ZIPSPLIT as
  441. X    well as Atari STZIP `5BCN`5D
  442. X - fixed zi_gcc Makefile target; added fu_gcc target `5BCN`5D
  443. X - finished unzip and zipinfo man pages (I think) `5BCN`5D
  444. X - OS/2:  minor national language code fix `5BKai Uwe`5D
  445. X - initialized all unzip and zipinfo global flags to zero (buggy Sun compile
  446. Vr)
  447. X    `5BCN, Frank van der Linden`5D
  448. X - reformatted .man pages slightly; renamed to .doc `5BCN, Jean-loup`5D
  449. X - renamed Mac Think C makefiles and VMS command files so names will be uniq
  450. Vue
  451. X    if extracted under MS-DOS; renamed Turbo C zipinfo.bat to zi_make.bat `5
  452. VBCN,
  453. X    Jean-loup`5D
  454. X
  455. XVersion info extracted from WizUnZip patches to unzip.c:
  456. X
  457. X  /* History:
  458. X   * 01/27/92  1.0    Original.
  459. X   * 04/12/92  1.1    Added separate handle and buffer for outout.
  460. X   *                  Change outout's typing to byte _far *.
  461. X   *                  Apology: I did some of the wacky things I did
  462. X   *                  with buffers and pointers because I kept running`20
  463. X   *                  out of local memory in the Windows version.--rah.
  464. X   * 06/30/92  1.2    Cleaned up source code.
  465. X   *                  Added drag-drop, hiding status window,
  466. X   *                  faster unzipping, updated to Unzip 5.0 source
  467. X   */
  468. X
  469. XVersion info extracted from new match.c (formerly filmatch.c):
  470. X
  471. X   File: filmatch.c
  472. X   Author: J. Kercheval
  473. X   Created: Thu, 03/14/1991  22:22:01
  474. X   `5Bfrom WILDF114.ZIP, SIMTEL20`5D
  475. X
  476. X   EPSRevision History
  477. X
  478. X   J. Kercheval  Wed, 02/20/1991  22:29:01  Released to Public Domain
  479. X   J. Kercheval  Fri, 02/22/1991  15:29:01  fix '\' bugs (two :( of them)
  480. X   J. Kercheval  Sun, 03/10/1991  19:31:29  add error return to matche()
  481. X   J. Kercheval  Sun, 03/10/1991  20:11:11  add is_valid_pattern code
  482. X   J. Kercheval  Sun, 03/10/1991  20:37:11  beef up main()
  483. X   J. Kercheval  Tue, 03/12/1991  22:25:10  Released as V1.1 to Public Domai
  484. Vn
  485. X   J. Kercheval  Thu, 03/14/1991  22:22:25  remove '\' for DOS file parsing
  486. X   J. Kercheval  Thu, 03/28/1991  20:58:27  include filmatch.h
  487. X
  488. X   v1.2 Toad Hall Tweak, Apr 92
  489. X
  490. X - Adapting to Info-ZIP unzip (to replace SEA's match() code).
  491. X   Why?  To avoid any future copyright problems.  (Hopefully one day
  492. X   we'll have EVERYTHING built of public domain code.)
  493. X   An even greater hope: that this new code added functionality
  494. X   and didn't break *too* many old applications.
  495. X
  496. X - Incorporating FILMATCH.H code herein.
  497. X - Consolidated all test code at the end (#ifdef'ed out, of course)
  498. X   so we can throw it all away when we're done playing.
  499. X - Swapping *string and *pattern to match previous match() parms.
  500. X - `5BGRR:  renamed from "filmatch.c" to "match.c" to avoid changing
  501. X   all of the unzip makefiles`5D
  502. X
  503. X   From the previous match()'s comments:
  504. X
  505. X   "The match() routine recursively compares a string to a "pattern" (regula
  506. Vr
  507. X   expression), returning TRUE if a match is found or FALSE if not.
  508. X   This version is specifically for use with unzip.c:  as did the previous
  509. X   SEA's match(), it leaves the case (upper, lower, or mixed) of the string
  510. X   alone, but converts any uppercase characters in the pattern to lowercase
  511. X   if indicated by the global var pInfo->lcflag (which is to say, string is
  512. X   assumed to have been converted to lowercase already, if such was
  513. X   necessary)."
  514. X
  515. X   Since the above has approached or exceeded unintelligibility
  516. X   (why use small words when big ones will do?), scan down for "lcflag"
  517. X   and see what the code does.
  518. X
  519. X   David Kirschbaum
  520. X   Toad Hall
  521. X
  522. X   File: filmatch.h
  523. X   Author: J. Kercheval
  524. X   Created: Thu, 03/14/1991  22:24:34
  525. X
  526. X   EPSRevision History
  527. X
  528. X   J. Kercheval  Wed, 02/20/1991  22:28:37  Released to Public Domain
  529. X   J. Kercheval  Sun, 03/10/1991  18:02:56  add is_valid_pattern
  530. X   J. Kercheval  Sun, 03/10/1991  18:25:48  add error_type in is_valid_patte
  531. Vrn
  532. X   J. Kercheval  Sun, 03/10/1991  18:47:47  error return from matche()
  533. X   J. Kercheval  Tue, 03/12/1991  22:24:49  Released as V1.1 to Public Domai
  534. Vn
  535. X   J. Kercheval  Thu, 03/14/1991  22:25:00  remove '\' for DOS file matching
  536. X   J. Kercheval  Thu, 03/28/1991  21:03:59  add in PATTERN_ESC & MATCH_LITER
  537. VAL
  538. X
  539. X==================
  540. X
  541. XThese changes occurred in beta versions 5.0a to 5.0p.  This list may have`20
  542. Xleft out some bugfixes and even some features...the brain cell is going,`20
  543. Xfolks (as Mark would say).  Apologies, etc., but I did muh best....
  544. X
  545. XGreg Roelofs (a.k.a. Cave Newt)
  546. $ CALL UNPACK [.UNZIP50]HISTORY.500;1 708332292
  547. $ create 'f'
  548. X/* inflate.c -- Not copyrighted 1992 by Mark Adler
  549. X   version c7, 27 June 1992 */
  550. X
  551. X
  552. X/* You can do whatever you like with this source file, though I would
  553. X   prefer that if you modify it and redistribute it that you include
  554. X   comments to that effect with your name and the date.  Thank you.
  555. X
  556. X   History:
  557. X   vers    date          who           what
  558. X   ----  ---------  --------------  ------------------------------------
  559. X    a    `7E`7E Feb 92  M. Adler        used full (large, one-step) lookup t
  560. Vable
  561. X    b1   21 Mar 92  M. Adler        first version with partial lookup tables
  562. X    b2   21 Mar 92  M. Adler        fixed bug in fixed-code blocks
  563. X    b3   22 Mar 92  M. Adler        sped up match copies, cleaned up some
  564. X    b4   25 Mar 92  M. Adler        added prototypes; removed window`5B`5D (
  565. Vnow
  566. X                                    is the responsibility of unzip.h--also
  567. X                                    changed name to slide`5B`5D), so needs d
  568. Viffs
  569. X                                    for unzip.c and unzip.h (this allows
  570. X                                    compiling in the small model on MSDOS);
  571. X                                    fixed cast of q in huft_build();
  572. X    b5   26 Mar 92  M. Adler        got rid of unintended macro recursion.
  573. X    b6   27 Mar 92  M. Adler        got rid of nextbyte() routine.  fixed
  574. X                                    bug in inflate_fixed().
  575. X    c1   30 Mar 92  M. Adler        removed lbits, dbits environment variabl
  576. Ves.
  577. X                                    changed BMAX to 16 for explode.  Removed
  578. X                                    OUTB usage, and replaced it with flush()
  579. V--
  580. X                                    this was a 20% speed improvement!  Added
  581. X                                    an explode.c (to replace unimplode.c) th
  582. Vat
  583. X                                    uses the huft routines here.  Removed
  584. X                                    register union.
  585. X    c2    4 Apr 92  M. Adler        fixed bug for file sizes a multiple of 3
  586. V2k.
  587. X    c3   10 Apr 92  M. Adler        reduced memory of code tables made by
  588. X                                    huft_build significantly (factor of two
  589. V to
  590. X                                    three).
  591. X    c4   15 Apr 92  M. Adler        added NOMEMCPY do kill use of memcpy().
  592. X                                    worked around a Turbo C optimization bug
  593. V.
  594. X    c5   21 Apr 92  M. Adler        added the WSIZE #define to allow reducin
  595. Vg
  596. X                                    the 32K window size for specialized
  597. X                                    applications.
  598. X    c6   31 May 92  M. Adler        added some typecasts to eliminate warnin
  599. Vgs
  600. X    c7   27 Jun 92  G. Roelofs      added some more typecasts (439:  MSC bug
  601. V)
  602. X */
  603. X
  604. X
  605. X/*
  606. X   Inflate deflated (PKZIP's method 8 compressed) data.  The compression
  607. X   method searches for as much of the current string of bytes (up to a
  608. X   length of 258) in the previous 32K bytes.  If it doesn't find any
  609. X   matches (of at least length 3), it codes the next byte.  Otherwise, it
  610. X   codes the length of the matched string and its distance backwards from
  611. X   the current position.  There is a single Huffman code that codes both
  612. X   single bytes (called "literals") and match lengths.  A second Huffman
  613. X   code codes the distance information, which follows a length code.  Each
  614. X   length or distance code actually represents a base value and a number
  615. X   of "extra" (sometimes zero) bits to get to add to the base value.  At
  616. X   the end of each deflated block is a special end-of-block (EOB) literal/
  617. X   length code.  The decoding process is basically: get a literal/length
  618. X   code; if EOB then done; if a literal, emit the decoded byte; if a
  619. X   length then get the distance and emit the referred-to bytes from the
  620. X   sliding window of previously emitted data.
  621. X
  622. X   There are (currently) three kinds of inflate blocks: stored, fixed, and
  623. X   dynamic.  The compressor deals with some chunk of data at a time, and
  624. X   decides which method to use on a chunk-by-chunk basis.  A chunk might
  625. X   typically be 32K or 64K.  If the chunk is uncompressible, then the
  626. X   "stored" method is used.  In this case, the bytes are simply stored as
  627. X   is, eight bits per byte, with none of the above coding.  The bytes are
  628. X   preceded by a count, since there is no longer an EOB code.
  629. X
  630. X   If the data is compressible, then either the fixed or dynamic methods
  631. X   are used.  In the dynamic method, the compressed data is preceded by
  632. X   an encoding of the literal/length and distance Huffman codes that are
  633. X   to be used to decode this block.  The representation is itself Huffman
  634. X   coded, and so is preceded by a description of that code.  These code
  635. X   descriptions take up a little space, and so for small blocks, there is
  636. X   a predefined set of codes, called the fixed codes.  The fixed method is
  637. X   used if the block codes up smaller that way (usually for quite small
  638. X   chunks), otherwise the dynamic method is used.  In the latter case, the
  639. X   codes are customized to the probabilities in the current block, and so
  640. X   can code it much better than the pre-determined fixed codes.
  641. X`20
  642. X   The Huffman codes themselves are decoded using a mutli-level table
  643. X   lookup, in order to maximize the speed of decoding plus the speed of
  644. X   building the decoding tables.  See the comments below that precede the
  645. X   lbits and dbits tuning parameters.
  646. X */
  647. X
  648. X
  649. X/*
  650. X   Notes beyond the 1.93a appnote.txt:
  651. X
  652. X   1. Distance pointers never point before the beginning of the output
  653. X      stream.
  654. X   2. Distance pointers can point back across blocks, up to 32k away.
  655. X   3. There is an implied maximum of 7 bits for the bit length table and
  656. X      15 bits for the actual data.
  657. X   4. If only one code exists, then it is encoded using one bit.  (Zero
  658. X      would be more efficient, but perhaps a little confusing.)  If two
  659. X      codes exist, they are coded using one bit each (0 and 1).
  660. X   5. There is no way of sending zero distance codes--a dummy must be
  661. X      sent if there are none.  (History: a pre 2.0 version of PKZIP would
  662. X      store blocks with no distance codes, but this was discovered to be
  663. X      too harsh a criterion.)
  664. X   6. There are up to 286 literal/length codes.  Code 256 represents the
  665. X      end-of-block.  Note however that the static length tree defines
  666. X      288 codes just to fill out the Huffman codes.  Codes 286 and 287
  667. X      cannot be used though, since there is no length base or extra bits
  668. X      defined for them.  Similarily, there are up to 30 distance codes.
  669. X      However, static trees define 32 codes (all 5 bits) to fill out the
  670. X      Huffman codes, but the last two had better not show up in the data.
  671. X   7. Unzip can check dynamic Huffman blocks for complete code sets.
  672. X      The exception is that a single code would not be complete (see #4).
  673. X   8. The five bits following the block type is really the number of
  674. X      literal codes sent minus 257.
  675. X   9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
  676. X      (1+6+6).  Therefore, to output three times the length, you output
  677. X      three codes (1+1+1), whereas to output four times the same length,
  678. X      you only need two codes (1+3).  Hmm.
  679. X  10. In the tree reconstruction algorithm, Code = Code + Increment
  680. X      only if BitLength(i) is not zero.  (Pretty obvious.)
  681. X  11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
  682. X  12. Note: length code 284 can represent 227-258, but length code 285
  683. X      really is 258.  The last length deserves its own, short code
  684. X      since it gets used a lot in very redundant files.  The length
  685. X      258 is special since 258 - 3 (the min match length) is 255.
  686. X  13. The literal/length and distance code bit lengths are read as a
  687. X      single stream of lengths.  It is possible (and advantageous) for
  688. X      a repeat code (16, 17, or 18) to go across the boundary between
  689. X      the two sets of lengths.
  690. X */
  691. X
  692. X#include "unzip.h"      /* this must supply the slide`5B`5D (byte) array */
  693. X
  694. X#ifndef WSIZE
  695. X#  define WSIZE 0x8000  /* window size--must be a power of two, and at least
  696. X                           32K for zip's deflate method */
  697. X#endif /* !WSIZE */
  698. X
  699. X
  700. X/* Huffman code lookup table entry--this entry is four bytes for machines
  701. X   that have 16-bit pointers (e.g. PC's in the small or medium model).
  702. X   Valid extra bits are 0..13.  e == 15 is EOB (end of block), e == 16
  703. X   means that v is a literal, 16 < e < 32 means that v is a pointer to
  704. X   the next table, which codes e - 16 bits, and lastly e == 99 indicates
  705. X   an unused code.  If a code with e == 99 is looked up, this implies an
  706. X   error in the data. */
  707. Xstruct huft `7B
  708. X  byte e;               /* number of extra bits or operation */
  709. X  byte b;               /* number of bits in this code or subcode */
  710. X  union `7B
  711. X    UWORD n;            /* literal, length base, or distance base */
  712. X    struct huft *t;     /* pointer to next level of table */
  713. X  `7D v;
  714. X`7D;
  715. X
  716. X
  717. X/* Function prototypes */
  718. Xint huft_build OF((unsigned *, unsigned, unsigned, UWORD *, UWORD *,
  719. X                   struct huft **, int *));
  720. Xint huft_free OF((struct huft *));
  721. Xvoid flush OF((unsigned));
  722. Xint inflate_codes OF((struct huft *, struct huft *, int, int));
  723. Xint inflate_stored OF((void));
  724. Xint inflate_fixed OF((void));
  725. Xint inflate_dynamic OF((void));
  726. Xint inflate_block OF((int *));
  727. Xint inflate_entry OF((void));
  728. Xvoid inflate OF((void));
  729. X
  730. X
  731. X/* The inflate algorithm uses a sliding 32K byte window on the uncompressed
  732. X   stream to find repeated byte strings.  This is implemented here as a
  733. X   circular buffer.  The index is updated simply by incrementing and then
  734. X   and'ing with 0x7fff (32K-1). */
  735. X/* It is left to other modules to supply the 32K area.  It is assumed
  736. X   to be usable as if it were declared "byte slide`5B32768`5D;" or as just
  737. X   "byte *slide;" and then malloc'ed in the latter case.  The definition
  738. X   must be in unzip.h, included above. */
  739. Xunsigned wp;            /* current position in slide */
  740. X
  741. X
  742. X/* Tables for deflate from PKZIP's appnote.txt. */
  743. Xstatic unsigned border`5B`5D = `7B    /* Order of the bit length code length
  744. Vs */
  745. X        16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15`7D;
  746. Xstatic UWORD cplens`5B`5D = `7B       /* Copy lengths for literal codes 257.
  747. V.285 */
  748. X        3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  749. X        35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0`7D;
  750. X        /* note: see note #13 above about the 258 in this list. */
  751. Xstatic UWORD cplext`5B`5D = `7B       /* Extra bits for literal codes 257..2
  752. V85 */
  753. X        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  754. X        3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99`7D; /* 99==invalid */
  755. Xstatic UWORD cpdist`5B`5D = `7B       /* Copy offsets for distance codes 0..
  756. V29 */
  757. X        1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  758. X        257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  759. X        8193, 12289, 16385, 24577`7D;
  760. Xstatic UWORD cpdext`5B`5D = `7B       /* Extra bits for distance codes */
  761. X        0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  762. X        7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  763. X        12, 12, 13, 13`7D;
  764. X
  765. X
  766. X
  767. X/* Macros for inflate() bit peeking and grabbing.
  768. X   The usage is:
  769. X  `20
  770. X        NEEDBITS(j)
  771. X        x = b & mask_bits`5Bj`5D;
  772. X        DUMPBITS(j)
  773. X
  774. X   where NEEDBITS makes sure that b has at least j bits in it, and
  775. X   DUMPBITS removes the bits from b.  The macros use the variable k
  776. X   for the number of bits in b.  Normally, b and k are register
  777. X   variables for speed, and are initialized at the begining of a
  778. X   routine that uses these macros from a global bit buffer and count.
  779. X
  780. X   If we assume that EOB will be the longest code, then we will never
  781. X   ask for bits with NEEDBITS that are beyond the end of the stream.
  782. X   So, NEEDBITS should not read any more bytes than are needed to
  783. X   meet the request.  Then no bytes need to be "returned" to the buffer
  784. X   at the end of the last block.
  785. X
  786. X   However, this assumption is not true for fixed blocks--the EOB code
  787. X   is 7 bits, but the other literal/length codes can be 8 or 9 bits.
  788. X   (Why PK made the EOB code, which can only occur once in a block,
  789. X   the *shortest* code in the set, I'll never know.)  However, by
  790. X   making the first table have a lookup of seven bits, the EOB code
  791. X   will be found in that first lookup, and so will not require that too
  792. X   many bits be pulled from the stream.
  793. X */
  794. X
  795. XULONG bb;                       /* bit buffer */
  796. Xunsigned bk;                    /* bits in bit buffer */
  797. X
  798. XUWORD bytebuf;
  799. X#define NEXTBYTE    (ReadByte(&bytebuf), bytebuf)
  800. X#define NEEDBITS(n) `7Bwhile(k<(n))`7Bb`7C=((ULONG)NEXTBYTE)<<k;k+=8;`7D`7D
  801. X#define DUMPBITS(n) `7Bb>>=(n);k-=(n);`7D
  802. X
  803. X
  804. X/*
  805. X   Huffman code decoding is performed using a multi-level table lookup.
  806. X   The fastest way to decode is to simply build a lookup table whose
  807. X   size is determined by the longest code.  However, the time it takes
  808. X   to build this table can also be a factor if the data being decoded
  809. X   is not very long.  The most common codes are necessarily the
  810. X   shortest codes, so those codes dominate the decoding time, and hence
  811. X   the speed.  The idea is you can have a shorter table that decodes the
  812. X   shorter, more probable codes, and then point to subsidiary tables for
  813. X   the longer codes.  The time it costs to decode the longer codes is
  814. X   then traded against the time it takes to make longer tables.
  815. X
  816. X   This results of this trade are in the variables lbits and dbits
  817. X   below.  lbits is the number of bits the first level table for literal/
  818. X   length codes can decode in one step, and dbits is the same thing for
  819. X   the distance codes.  Subsequent tables are also less than or equal to
  820. X   those sizes.  These values may be adjusted either when all of the
  821. X   codes are shorter than that, in which case the longest code length in
  822. X   bits is used, or when the shortest code is *longer* than the requested
  823. X   table size, in which case the length of the shortest code in bits is
  824. X   used.
  825. X
  826. X   There are two different values for the two tables, since they code a
  827. X   different number of possibilities each.  The literal/length table
  828. X   codes 286 possible values, or in a flat code, a little over eight
  829. X   bits.  The distance table codes 30 possible values, or a little less
  830. X   than five bits, flat.  The optimum values for speed end up being
  831. X   about one bit more than those, so lbits is 8+1 and dbits is 5+1.
  832. X   The optimum values may differ though from machine to machine, and
  833. X   possibly even between compilers.  Your mileage may vary.
  834. X */
  835. X
  836. X
  837. Xint lbits = 9;          /* bits in base literal/length lookup table */
  838. Xint dbits = 6;          /* bits in base distance lookup table */
  839. X
  840. X
  841. X/* If BMAX needs to be larger than 16, then h and x`5B`5D should be ULONG. *
  842. V/
  843. X#define BMAX 16         /* maximum bit length of any code (16 for explode) *
  844. V/
  845. X#define N_MAX 288       /* maximum number of codes in any set */
  846. X
  847. X
  848. Xunsigned hufts;         /* track memory usage */
  849. X
  850. X
  851. Xint huft_build(b, n, s, d, e, t, m)
  852. Xunsigned *b;            /* code lengths in bits (all assumed <= BMAX) */
  853. Xunsigned n;             /* number of codes (assumed <= N_MAX) */
  854. Xunsigned s;             /* number of simple-valued codes (0..s-1) */
  855. XUWORD *d;               /* list of base values for non-simple codes */
  856. XUWORD *e;               /* list of extra bits for non-simple codes */
  857. Xstruct huft **t;        /* result: starting table */
  858. Xint *m;                 /* maximum lookup bits, returns actual */
  859. X/* Given a list of code lengths and a maximum table size, make a set of
  860. X   tables to decode that set of codes.  Return zero on success, one if
  861. X   the given code set is incomplete (the tables are still built in this
  862. X   case), two if the input is invalid (all zero length codes or an
  863. X   oversubscribed set of lengths), and three if not enough memory. */
  864. X`7B
  865. X  unsigned a;                   /* counter for codes of length k */
  866. X  unsigned c`5BBMAX+1`5D;           /* bit length count table */
  867. X  unsigned f;                   /* i repeats in table every f entries */
  868. X  int g;                        /* maximum code length */
  869. X  int h;                        /* table level */
  870. X  register unsigned i;          /* counter, current code */
  871. X  register unsigned j;          /* counter */
  872. X  register int k;               /* number of bits in current code */
  873. X  int l;                        /* bits per table (returned in m) */
  874. X  register unsigned *p;         /* pointer into c`5B`5D, b`5B`5D, or v`5B`5D
  875. V */
  876. X  register struct huft *q;      /* points to current table */
  877. X  struct huft r;                /* table entry for structure assignment */
  878. X  struct huft *u`5BBMAX`5D;         /* table stack */
  879. X  unsigned v`5BN_MAX`5D;            /* values in order of bit length */
  880. X  register int w;               /* bits before this table == (l * h) */
  881. X  unsigned x`5BBMAX+1`5D;           /* bit offsets, then code stack */
  882. X  unsigned *xp;                 /* pointer into x */
  883. X  int y;                        /* number of dummy codes added */
  884. X  unsigned z;                   /* number of entries in current table */
  885. X
  886. X
  887. X  /* Generate counts for each bit length */
  888. X  memset(c, 0, sizeof(c));
  889. X  p = b;  i = n;
  890. X  do `7B
  891. X    c`5B*p++`5D++;                  /* assume all entries <= BMAX */
  892. X  `7D while (--i);
  893. X  if (c`5B0`5D == n)
  894. X    return 2;                   /* bad input--all zero length codes */
  895. X
  896. X
  897. X  /* Find minimum and maximum length, bound *m by those */
  898. X  l = *m;
  899. X  for (j = 1; j <= BMAX; j++)
  900. X    if (c`5Bj`5D)
  901. X      break;
  902. X  k = j;                        /* minimum code length */
  903. X  if ((unsigned)l < j)
  904. X    l = j;
  905. X  for (i = BMAX; i; i--)
  906. X    if (c`5Bi`5D)
  907. X      break;
  908. X  g = i;                        /* maximum code length */
  909. X  if ((unsigned)l > i)
  910. X    l = i;
  911. X  *m = l;
  912. X
  913. X
  914. X  /* Adjust last length count to fill out codes, if needed */
  915. X  for (y = 1 << j; j < i; j++, y <<= 1)
  916. X    if ((y -= c`5Bj`5D) < 0)
  917. X      return 2;                 /* bad input: more codes than bits */
  918. X  if ((y -= c`5Bi`5D) < 0)
  919. X    return 2;
  920. X  c`5Bi`5D += y;
  921. X
  922. X
  923. X  /* Generate starting offsets into the value table for each length */
  924. X  x`5B1`5D = j = 0;
  925. X  p = c + 1;  xp = x + 2;
  926. X  while (--i) `7B                 /* note that i == g from above */
  927. X    *xp++ = (j += *p++);
  928. X  `7D
  929. X
  930. X
  931. X  /* Make a table of values in order of bit lengths */
  932. X  p = b;  i = 0;
  933. X  do `7B
  934. X    if ((j = *p++) != 0)
  935. X      v`5Bx`5Bj`5D++`5D = i;
  936. X  `7D while (++i < n);
  937. X
  938. X
  939. X  /* Generate the Huffman codes and for each, make the table entries */
  940. X  x`5B0`5D = i = 0;                 /* first Huffman code is zero */
  941. X  p = v;                        /* grab values in bit order */
  942. X  h = -1;                       /* no tables yet--level -1 */
  943. X  w = -l;                       /* bits decoded == (l * h) */
  944. X  u`5B0`5D = (struct huft *)NULL;   /* just to keep compilers happy */
  945. X  q = (struct huft *)NULL;      /* ditto */
  946. X  z = 0;                        /* ditto */
  947. X
  948. X  /* go through the bit lengths (k already is bits in shortest code) */
  949. X  for (; k <= g; k++)
  950. X  `7B
  951. X    a = c`5Bk`5D;
  952. X    while (a--)
  953. X    `7B
  954. X      /* here i is the Huffman code of length k bits for value *p */
  955. X      /* make tables up to required level */
  956. X      while (k > w + l)
  957. X      `7B
  958. X        h++;
  959. X        w += l;                 /* previous table always l bits */
  960. X
  961. X        /* compute minimum size table less than or equal to l bits */
  962. X        z = (z = g - w) > (unsigned)l ? l : z;  /* upper limit on table size
  963. V */
  964. X        if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
  965. X        `7B                       /* too few codes for k-w bit table */
  966. X          f -= a + 1;           /* deduct codes from patterns left */
  967. X          xp = c + k;
  968. X          while (++j < z)       /* try smaller tables up to z bits */
  969. X          `7B
  970. X            if ((f <<= 1) <= *++xp)
  971. X              break;            /* enough codes to use up j bits */
  972. X            f -= *xp;           /* else deduct codes from patterns */
  973. X          `7D
  974. X        `7D
  975. X        z = 1 << j;             /* table entries for j-bit table */
  976. X
  977. X        /* allocate and link in new table */
  978. X        if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) ==
  979. X            (struct huft *)NULL)
  980. X        `7B
  981. X          if (h)
  982. X            huft_free(u`5B0`5D);
  983. X          fprintf(stderr, "\n*** inflate out of memory *** ");
  984. X          return 3;             /* not enough memory */
  985. X        `7D
  986. X        hufts += z + 1;         /* track memory usage */
  987. X        *t = q + 1;             /* link to list for huft_free() */
  988. X        *(t = &(q->v.t)) = (struct huft *)NULL;
  989. X        u`5Bh`5D = ++q;             /* table starts after link */
  990. X
  991. X        /* connect to last table, if there is one */
  992. X        if (h)
  993. X        `7B
  994. X          x`5Bh`5D = i;             /* save pattern for backing up */
  995. X          r.b = (byte)l;        /* bits to dump before this table */
  996. X          r.e = (byte)(16 + j); /* bits in this table */
  997. X          r.v.t = q;            /* pointer to this table */
  998. X          j = i >> (w - l);     /* (get around Turbo C bug) */
  999. X          u`5Bh-1`5D`5Bj`5D = r;        /* connect to last table */
  1000. X        `7D
  1001. X      `7D
  1002. X
  1003. X      /* set up table entry in r */
  1004. X      r.b = (byte)(k - w);
  1005. X      if (p >= v + n)
  1006. X        r.e = 99;               /* out of values--invalid code */
  1007. X      else if (*p < s)
  1008. X      `7B
  1009. X        r.e = (byte)(*p < 256 ? 16 : 15);    /* 256 is end-of-block code */
  1010. X        r.v.n = *p++;           /* simple code is just the value */
  1011. X      `7D
  1012. X      else
  1013. X      `7B
  1014. X        r.e = (byte)e`5B*p - s`5D;  /* non-simple--look up in lists */
  1015. X        r.v.n = d`5B*p++ - s`5D;
  1016. X      `7D
  1017. X
  1018. X      /* fill code-like entries with r */
  1019. X      f = 1 << (k - w);
  1020. X      for (j = i >> w; j < z; j += f)
  1021. X        q`5Bj`5D = r;
  1022. X
  1023. X      /* backwards increment the k-bit code i */
  1024. X      for (j = 1 << (k - 1); i & j; j >>= 1)
  1025. X        i `5E= j;
  1026. X      i `5E= j;
  1027. X
  1028. X      /* backup over finished tables */
  1029. X      while ((i & ((1 << w) - 1)) != x`5Bh`5D)
  1030. X      `7B
  1031. X        h--;                    /* don't need to update q */
  1032. X        w -= l;
  1033. X      `7D
  1034. X    `7D
  1035. X  `7D
  1036. X
  1037. X
  1038. X  /* Return true (1) if we were given an incomplete table */
  1039. X  return y != 0 && n != 1;
  1040. X`7D
  1041. X
  1042. X
  1043. X
  1044. Xint huft_free(t)
  1045. Xstruct huft *t;         /* table to free */
  1046. X/* Free the malloc'ed tables built by huft_build(), which makes a linked
  1047. X   list of the tables it made, with the links in a dummy first entry of
  1048. X   each table. */
  1049. X`7B
  1050. X  register struct huft *p, *q;
  1051. X
  1052. X
  1053. X  /* Go through linked list, freeing from the malloced (t`5B-1`5D) address.
  1054. V */
  1055. X  p = t;
  1056. X  while (p != (struct huft *)NULL)
  1057. X  `7B
  1058. X    q = (--p)->v.t;
  1059. X    free(p);
  1060. X    p = q;
  1061. X  `7D`20
  1062. X  return 0;
  1063. X`7D
  1064. X
  1065. X
  1066. X
  1067. Xvoid flush(w)
  1068. Xunsigned w;             /* number of bytes to flush */
  1069. X/* Do the equivalent of OUTB for the bytes slide`5B0..w-1`5D. */
  1070. X`7B
  1071. X  unsigned n;
  1072. X  byte *p;
  1073. X
  1074. X  p = slide;
  1075. X  while (w)
  1076. X  `7B
  1077. X    n = (n = OUTBUFSIZ - outcnt) < w ? n : w;
  1078. X    memcpy(outptr, p, n);       /* try to fill up buffer */
  1079. X    outptr += n;
  1080. X    if ((outcnt += n) == OUTBUFSIZ)
  1081. X      FlushOutput();            /* if full, empty */
  1082. X    p += n;
  1083. X    w -= n;
  1084. X  `7D
  1085. X`7D
  1086. X
  1087. X
  1088. X
  1089. Xint inflate_codes(tl, td, bl, bd)
  1090. Xstruct huft *tl, *td;   /* literal/length and distance decoder tables */
  1091. Xint bl, bd;             /* number of bits decoded by tl`5B`5D and td`5B`5D *
  1092. V/
  1093. X/* inflate (decompress) the codes in a deflated (compressed) block.
  1094. X   Return an error code or zero if it all goes ok. */
  1095. X`7B
  1096. X  register unsigned e;  /* table entry flag/number of extra bits */
  1097. X  unsigned n, d;        /* length and index for copy */
  1098. X  unsigned w;           /* current window position */
  1099. X  struct huft *t;       /* pointer to table entry */
  1100. X  unsigned ml, md;      /* masks for bl and bd bits */
  1101. X  register ULONG b;     /* bit buffer */
  1102. X  register unsigned k;  /* number of bits in bit buffer */
  1103. X
  1104. X
  1105. X  /* make local copies of globals */
  1106. X  b = bb;                       /* initialize bit buffer */
  1107. X  k = bk;
  1108. X  w = wp;                       /* initialize window position */
  1109. X
  1110. X
  1111. X  /* inflate the coded data */
  1112. X  ml = mask_bits`5Bbl`5D;           /* precompute masks for speed */
  1113. X  md = mask_bits`5Bbd`5D;
  1114. X  while (1)                     /* do until end of block */
  1115. X  `7B
  1116. X    NEEDBITS((unsigned)bl)
  1117. X    if ((e = (t = tl + ((unsigned)b & ml))->e) > 16)
  1118. X      do `7B
  1119. X        if (e == 99)
  1120. X          return 1;
  1121. X        DUMPBITS(t->b)
  1122. X        e -= 16;
  1123. X        NEEDBITS(e)
  1124. X      `7D while ((e = (t = t->v.t + ((unsigned)b & mask_bits`5Be`5D))->e) >
  1125. V 16);
  1126. X    DUMPBITS(t->b)
  1127. X    if (e == 16)                /* then it's a literal */
  1128. X    `7B
  1129. X      slide`5Bw++`5D = (byte)t->v.n;
  1130. X      if (w == WSIZE)
  1131. X      `7B
  1132. X        flush(w);
  1133. X        w = 0;
  1134. X      `7D
  1135. X    `7D
  1136. X    else                        /* it's an EOB or a length */
  1137. X    `7B
  1138. X      /* exit if end of block */
  1139. X      if (e == 15)
  1140. X        break;
  1141. X
  1142. X      /* get length of block to copy */
  1143. X      NEEDBITS(e)
  1144. X      n = t->v.n + ((unsigned)b & mask_bits`5Be`5D);
  1145. X      DUMPBITS(e);
  1146. X
  1147. X      /* decode distance of block to copy */
  1148. X      NEEDBITS((unsigned)bd)
  1149. X      if ((e = (t = td + ((unsigned)b & md))->e) > 16)
  1150. X        do `7B
  1151. X          if (e == 99)
  1152. X            return 1;
  1153. X          DUMPBITS(t->b)
  1154. X          e -= 16;
  1155. X          NEEDBITS(e)
  1156. X        `7D while ((e = (t = t->v.t + ((unsigned)b & mask_bits`5Be`5D))->e)
  1157. V > 16);
  1158. X      DUMPBITS(t->b)
  1159. X      NEEDBITS(e)
  1160. X      d = w - t->v.n - ((unsigned)b & mask_bits`5Be`5D);
  1161. X      DUMPBITS(e)
  1162. X
  1163. X      /* do the copy */
  1164. X      do `7B
  1165. X        n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
  1166. X#ifndef NOMEMCPY
  1167. X        if (w - d >= e)         /* (this test assumes unsigned comparison) *
  1168. V/
  1169. X        `7B
  1170. X          memcpy(slide + w, slide + d, e);
  1171. X          w += e;
  1172. X          d += e;
  1173. X        `7D
  1174. X        else                      /* do it slow to avoid memcpy() overlap */
  1175. X#endif /* !NOMEMCPY */
  1176. X          do `7B
  1177. X            slide`5Bw++`5D = slide`5Bd++`5D;
  1178. X          `7D while (--e);
  1179. X        if (w == WSIZE)
  1180. X        `7B
  1181. X          flush(w);
  1182. X          w = 0;
  1183. X        `7D
  1184. X      `7D while (n);
  1185. X    `7D
  1186. X  `7D
  1187. X
  1188. X
  1189. X  /* restore the globals from the locals */
  1190. X  wp = w;                       /* restore global window pointer */
  1191. X  bb = b;                       /* restore global bit buffer */
  1192. X  bk = k;
  1193. X
  1194. X
  1195. X  /* done */
  1196. X  return 0;
  1197. X`7D
  1198. X
  1199. X
  1200. X
  1201. Xint inflate_stored()
  1202. X/* "decompress" an inflated type 0 (stored) block. */
  1203. X`7B
  1204. X  unsigned n;           /* number of bytes in block */
  1205. X  unsigned w;           /* current window position */
  1206. X  register ULONG b;     /* bit buffer */
  1207. X  register unsigned k;  /* number of bits in bit buffer */
  1208. X
  1209. X
  1210. X  /* make local copies of globals */
  1211. X  b = bb;                       /* initialize bit buffer */
  1212. X  k = bk;
  1213. X  w = wp;                       /* initialize window position */
  1214. X
  1215. X
  1216. X  /* go to byte boundary */
  1217. X  n = k & 7;
  1218. X  DUMPBITS(n);
  1219. X
  1220. X
  1221. X  /* get the length and its complement */
  1222. X  NEEDBITS(16)
  1223. X  n = ((unsigned)b & 0xffff);
  1224. X  DUMPBITS(16)
  1225. X  NEEDBITS(16)
  1226. +-+-+-+-+-+-+-+-  END  OF PART 4 +-+-+-+-+-+-+-+-
  1227.