home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume31 / unzip50 / part04 < prev    next >
Encoding:
Text File  |  1992-08-22  |  58.5 KB  |  1,652 lines

  1. Newsgroups: comp.sources.misc
  2. From: zip-bugs@cs.ucla.edu (Info-ZIP group)
  3. Subject:  v31i107:  unzip50 - Info-ZIP portable UnZip, version 5.0, Part04/14
  4. Message-ID: <1992Aug24.025313.24292@sparky.imd.sterling.com>
  5. X-Md4-Signature: 87248553aaab76344f978a59780729b7
  6. Date: Mon, 24 Aug 1992 02:53:13 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: zip-bugs@cs.ucla.edu (Info-ZIP group)
  10. Posting-number: Volume 31, Issue 107
  11. Archive-name: unzip50/part04
  12. Supersedes: unzip: Volume 29, Issue 31-42
  13. Environment: UNIX, VMS, OS/2, MS-DOS, MACINTOSH, WIN-NT, LINUX, MINIX, COHERENT AMIGA?, !ATARI, symlink, SGI, DEC, Cray, Convex, Amdahl, Sun
  14.  
  15. #! /bin/sh
  16. # This is a shell archive.  Remove anything before this line, then feed it
  17. # into a shell via "sh file" or similar.  To overwrite existing files,
  18. # type "sh file -c".
  19. # The tool that generated this appeared in the comp.sources.unix newsgroup;
  20. # send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  21. # Contents:  Makefile inflate.c
  22. # Wrapped by kent@sparky on Sun Aug 23 21:09:32 1992
  23. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  24. echo If this archive is complete, you will see the following message:
  25. echo '          "shar: End of archive 4 (of 14)."'
  26. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  27.   echo shar: Will not clobber existing file \"'Makefile'\"
  28. else
  29.   echo shar: Extracting \"'Makefile'\" \(22919 characters\)
  30.   sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  31. X#==============================================================================
  32. X# Makefile for UnZip, ZipInfo & FUnZip:  Unix, OS/2, MS-DOS ("real" makes only)
  33. X# Version:  5.0 (inflate,explode)                                20 August 1992
  34. X#==============================================================================
  35. X#
  36. X#
  37. X# INSTRUCTIONS (such as they are):
  38. X#
  39. X# "make vax"    -- makes UnZip on a VAX 11-780 BSD 4.3 in current directory
  40. X#           (or a SysV VAX, or an 8600 running Ultrix, or...)
  41. X# "make"    -- uses environment variable SYSTEM to set the type
  42. X#           system to compile for.  This doesn't work for some
  43. X#           particularly brain-damaged versions of make (VAX BSD,
  44. X#           Gould, and SCO Unix are in this group).  If SYSTEM not
  45. X#           set, gives instructions on what to try instead.
  46. X# "make list"    -- lists all supported systems (targets), including related
  47. X#           utilities' targets
  48. X# "make wombat" -- Chokes and dies if you haven't added the specifics
  49. X#           for your Wombat 68000 (or whatever) to the systems list.
  50. X#
  51. X# CF are flags for the C compiler.  LF are flags for the loader.  LF2 are
  52. X# more flags for the loader, if they need to be at the end of the line
  53. X# instead of at the beginning (for example, some libraries).  LOCAL_UNZIP
  54. X# is an environment variable that can be used to add default C flags to
  55. X# your compile without editing the Makefile (e.g., -DDEBUG_STRUC, or -FPi87
  56. X# on PCs).
  57. X#
  58. X# My host (a VAX 11-780 running BSD 4.3) is hereafter referred to as "my host."
  59. X#
  60. X# My host's /usr/include/sys/param.h defines BSD for me.  You may have to add
  61. X# "-DBSD" to the list of CF for your system.
  62. X#
  63. X# Some versions of make do not define the macro "$(MAKE)" (my host did not).
  64. X# The makefile should now handle such systems correctly, more or less; the
  65. X# possible exception to this is if you've used a make command-line option
  66. X# (for example, the one which displays the commands which WOULD be executed,
  67. X# but doesn't actually execute them).  It probably needs some more tinkering.
  68. X# If things still don't work, use "make" instead of "$(MAKE)" in your system's
  69. X# makerule.  Or try adding the following line to your .login file:
  70. X#   setenv MAKE "make"
  71. X# (It didn't help on my host.)
  72. X#
  73. X# Memcpy and memset are provided for those systems that don't have them;
  74. X# they're found in misc.c and will be used if -DZMEM is included in the list
  75. X# of CF.  These days *almost* all systems have them (they're mandated by
  76. X# ANSI), but older systems might be lacking.  And at least one machine's
  77. X# version results in some serious performance degradation...
  78. X#
  79. X# Be sure to test your nice new UnZip; successful compilation does not always
  80. X# imply a working program.
  81. X
  82. X
  83. X#####################
  84. X# MACRO DEFINITIONS #
  85. X#####################
  86. X
  87. X# Defaults most systems use (use LOCAL_UNZIP in environment to add flags, 
  88. X# such as -DNOMEMCPY).
  89. X
  90. XCRYPTF =
  91. XCRYPTO =
  92. X# Uncomment next two lines for decryption version:
  93. X#CRYPTF = -DCRYPT
  94. X#CRYPTO = crypt$O
  95. X
  96. X# UnZip flags
  97. XCC = cc#    try using "gcc" target rather than changing this (if you do,
  98. XLD = cc#    you MUST change LD, too--else "unresolved symbol:  ___main")
  99. XLOC = $(LOCAL_UNZIP) $(CRYPTF)
  100. XCF = -O $(LOC)
  101. XLF = -o unzip
  102. XLF2 = -s
  103. X
  104. X# ZipInfo flags
  105. XZC = -DZIPINFO
  106. XZL = -o zipinfo
  107. XZL2 = -s
  108. X
  109. X# FUnZip flags
  110. XFC = # not used
  111. XFL = -o funzip
  112. XFL2 = -s
  113. X
  114. X# general-purpose stuff
  115. XLN = rm -f misc_.c; ln
  116. XRM = rm -f
  117. XE =
  118. XO = .o
  119. XSHELL = /bin/sh
  120. XINSTALL = cp#            probably can change this to 'install' if you have it
  121. XBINDIR = /usr/local/bin#   target directory - where to install executables
  122. X
  123. X# object files
  124. XOBJS1 = unzip$O $(CRYPTO) envargs$O explode$O extract$O file_io$O inflate$O
  125. XOBJS2 = mapname$O match$O misc$O unreduce$O unshrink$O
  126. XOBJS = $(OBJS1) $(OBJS2)
  127. XLOBJS = $(OBJS)
  128. XOS2_OBJS = $(OBJS:.o=.obj) os2unzip.obj
  129. XOBJZ = zipinfo$O envargs$O match$O misc_$O
  130. XOS2_OBJZ = $(OBJZ:.o=.obj) os2zinfo.obj
  131. XOBJF = funzip$O $(CRYPTO) inflate$O
  132. XOS2_OBJF = # not yet supported
  133. XUNZIPS = unzip$E # zipinfo$E funzip$E    # zipinfo, funzip not fully supported
  134. X#                    #  yet (next release)
  135. X
  136. X# list of supported systems/targets in this version
  137. XSYSTEMS1 = 386i 3Bx 7300 amdahl apollo aviion bsd bull c120 c210 coherent
  138. XSYSTEMS2 = convex cray cray_cc cray_v3 cyber_sgi dec dnix encore eta
  139. XSYSTEMS3 = gcc gcc_dos generic generic2 gould hk68 hp indigo linux
  140. XSYSTEMS4 = minix mips msc_dos next osf1 p_iris pyramid rs6000 rtaix
  141. XSYSTEMS5 = sco sco_dos sco_x286 sequent sgi stellar sun sysv sysv6300
  142. XSYSTEMS6 = tahoe ultrix vax wombat xos
  143. X
  144. XSYS_UTIL1 = zi_dos zi_gcc zi_indigo zipinfo fu_gcc funzip
  145. X# SYS_UTIL2 = ship ship_dos ship_sysv
  146. X
  147. X
  148. X####################
  149. X# DEFAULT HANDLING #
  150. X####################
  151. X
  152. X# The below will try to use your shell variable "SYSTEM" as the type system
  153. X# to use (e.g., if you type "make" with no parameters at the command line).
  154. X# The test for $(MAKE) is necessary for VAX BSD make (and Gould, apparently),
  155. X# as is the "goober" (else stupid makes see an "else ;" statement, which they
  156. X# don't like).  "goober" must then be made into a valid target for machines
  157. X# which DO define MAKE properly (and have SYSTEM set).  Quel kludge, non?
  158. X# And to top it all off, it appears that the VAX, at least, can't pick SYSTEM
  159. X# out of the environment either (which, I suppose, should not be surprising).
  160. X# [Btw, if the empty "goober" target causes someone else's make to barf, just
  161. X# add an "@echo > /dev/null" command (or whatever).  Works OK on the Amdahl
  162. X# and Crays, though.]
  163. X
  164. Xdefault:
  165. X    @if test -z "$(MAKE)"; then\
  166. X        if test -z "$(SYSTEM)";\
  167. X        then make help;\
  168. X        else make $(SYSTEM) MAKE="make";\
  169. X        fi;\
  170. X    else\
  171. X        if test -z "$(SYSTEM)";\
  172. X        then $(MAKE) help;\
  173. X        else $(MAKE) $(SYSTEM) goober;\
  174. X        fi;\
  175. X    fi
  176. X
  177. Xgoober:
  178. X
  179. Xhelp:
  180. X    @echo
  181. X    @echo\
  182. X "  If you're not sure about the characteristics of your system, try typing"
  183. X    @echo\
  184. X '  "make generic".  If the compiler barfs and says something unpleasant about'
  185. X    @echo\
  186. X '  "timezone redefined," try typing "make clean" followed by "make generic2".'
  187. X    @echo\
  188. X '  One of these actions should produce a working copy of unzip on most Unix'
  189. X    @echo\
  190. X '  systems.  If you know a bit more about the machine on which you work, you'
  191. X    @echo\
  192. X '  might try "make list" for a list of the specific systems supported herein.'
  193. X    @echo\
  194. X '  And as a last resort, feel free to read the numerous comments within the'
  195. X    @echo\
  196. X '  Makefile itself.  Note that to compile the decryption version of UnZip,'
  197. X    @echo\
  198. X '  you must obtain crypt.c separately, in addition to uncommenting two lines'
  199. X    @echo\
  200. X '  in Makefile (see the main Contents file for ftp and mail-server sites).'
  201. X    @echo\
  202. X '  Have an excruciatingly pleasant day.'
  203. X    @echo
  204. X
  205. Xlist:
  206. X    @echo
  207. X    @echo\
  208. X 'Type "make <system>", where <system> is one of the following:'
  209. X    @echo
  210. X    @echo  "    $(SYSTEMS1)"
  211. X    @echo  "    $(SYSTEMS2)"
  212. X    @echo  "    $(SYSTEMS3)"
  213. X    @echo  "    $(SYSTEMS4)"
  214. X    @echo  "    $(SYSTEMS5)"
  215. X    @echo  "    $(SYSTEMS6)"
  216. X    @echo
  217. X    @echo\
  218. X 'Otherwise set the shell variable SYSTEM to one of these and just type "make".'
  219. X    @echo\
  220. X 'Targets for related utilities (ZipInfo) include:'
  221. X    @echo
  222. X    @echo  "    $(SYS_UTIL1)"
  223. X#    @echo  "    $(SYS_UTIL2)"
  224. X    @echo
  225. X    @echo\
  226. X 'For further (very useful) information, please read the comments in Makefile.'
  227. X    @echo
  228. X
  229. X
  230. X###############################################
  231. X# BASIC COMPILE INSTRUCTIONS AND DEPENDENCIES #
  232. X###############################################
  233. X
  234. X.c$O :
  235. X    $(CC) -c $(CF) $*.c
  236. X
  237. Xunzips:        $(UNZIPS)
  238. X
  239. Xunzip$E:    $(OBJS)
  240. X    $(LD) $(LF) $(LOBJS) $(LF2)
  241. X
  242. Xcrypt$O:        crypt.c unzip.h zip.h    # may or may not be in distribution
  243. Xenvargs$O:      envargs.c unzip.h
  244. Xexplode$O:      explode.c unzip.h
  245. Xextract$O:      extract.c unzip.h
  246. Xfile_io$O:      file_io.c unzip.h
  247. Xfunzip$O:       funzip.c unzip.h
  248. Xinflate$O:      inflate.c unzip.h
  249. Xmapname$O:      mapname.c unzip.h
  250. Xmatch$O:        match.c unzip.h
  251. Xmisc$O:         misc.c unzip.h
  252. Xos2unzip$O:     os2unzip.c unzip.h    # for OS/2 only
  253. Xos2zinfo$O:     os2unzip.c unzip.h    # for OS/2 only
  254. Xunreduce$O:     unreduce.c unzip.h
  255. Xunshrink$O:     unshrink.c unzip.h
  256. Xunzip$O:        unzip.c unzip.h
  257. X
  258. Xall:    generic_msg generic zipinfo
  259. X
  260. Xgeneric_msg:
  261. X    @echo
  262. X    @echo\
  263. X '  Attempting "make generic" and "make zipinfo" now.  If this fails for some'
  264. X    @echo\
  265. X '  reason, type "make help" and/or "make list" for suggestions.'
  266. X    @echo
  267. X
  268. Xinstall:    $(UNZIPS)
  269. X    $(INSTALL) $(UNZIPS) $(BINDIR)
  270. X
  271. Xclean:
  272. X    rm -f $(OBJS) unzip$E $(OBJZ) zipinfo$E
  273. X
  274. X
  275. X################################
  276. X# INDIVIDUAL MACHINE MAKERULES #
  277. X################################
  278. X
  279. X# these are the makerules for various systems
  280. X# TABS ARE REQUIRED FOR MANY VERSIONS OF "MAKE"!
  281. X
  282. X
  283. X# ---------------------------------------------------------------------------
  284. X#   Generic targets (can't assume make utility groks "$(MAKE)")
  285. X# ---------------------------------------------------------------------------
  286. X
  287. Xgeneric:    unzip    # first try if unknown
  288. X
  289. Xgeneric2:        # second try if unknown:  hope make is called "make"...
  290. X    make unzip CF="$(CF) -DBSD"
  291. X
  292. X# ---------------------------------------------------------------------------
  293. X#   "Normal" group (both big- and little-endian, structure-padding or not):
  294. X# ---------------------------------------------------------------------------
  295. X
  296. X386i:        unzip    # sun386i, SunOS 4.0.2
  297. X3Bx:        unzip    # AT&T 3B2/1000-80; should work on any WE32XXX machine
  298. X7300:        unzip    # AT&T 7300 (M68000/SysV)
  299. Xapollo:        unzip    # Apollo Domain/OS machines
  300. Xbull:        unzip    # Bull DPX/2, BOS 2.00.45 (doesn't require -Xk switch)
  301. Xcoherent:    unzip    # Coherent 3.10, Mark Williams C
  302. Xcray_cc:    unzip    # Cray-2 and Y-MP, using default (possibly old) compiler
  303. Xdec:        unzip    # DEC 5820 (MIPS RISC), test version of Ultrix v4.0
  304. Xencore:        unzip    # Multimax
  305. Xeta:        unzip    # ETA-10P*, hybrid SysV with BSD 4.3 enhancements
  306. Xgould:        unzip    # Gould PN9000 running UTX/32 2.1Bu01
  307. Xhp:        unzip    # HP 9000 series (68020), 4.3BSD or HP-UX A.B3.10 Ver D
  308. Xhp_ux:        unzip    # (to match zip's makefile entry)
  309. Xmips:        unzip    # MIPS M120-5(?), SysV.3 [error in sys/param.h file?]
  310. Xpyramid:    unzip    # Pyramid 90X, prob. all, under >= OSx4.1, BSD universe
  311. Xrtaix:        unzip    # IBM RT 6150 under AIX 2.2.1
  312. Xsco:        unzip    # Xenix/386 (tested on 2.3.1); SCO Unix 3.2.0.
  313. Xstellar:    unzip    # gs-2000
  314. Xsun:        unzip    # Sun 3, 4; SunOS 4.x (SOME SYSTEMS ARE SYSTEM V!)
  315. Xtahoe:        unzip    # tahoe (CCI Power6/32), 4.3BSD
  316. Xultrix:        unzip    # VAXen, DEC 58x0 (MIPS guts), DECstation 2100; v4.x
  317. Xvax:        unzip    # general-purpose VAX target (not counting VMS)
  318. X
  319. X# ---------------------------------------------------------------------------
  320. X#   BSD group (for timezone structs [struct timeb]):
  321. X# ---------------------------------------------------------------------------
  322. X
  323. Xbsd:        _bsd    # generic BSD (BSD 4.2 & Ultrix handled in unzip.h)
  324. X
  325. X_bsd:
  326. X    $(MAKE) unzip CF="$(CF) -DBSD"
  327. X
  328. X# ---------------------------------------------------------------------------
  329. X#   SysV group (for extern long timezone and ioctl.h instead of sgtty.h):
  330. X# ---------------------------------------------------------------------------
  331. X
  332. Xamdahl:        _sysv    # Amdahl (IBM) mainframe, UTS (SysV) 1.2.4 and 2.0.1
  333. Xaviion:         _sysv    # Data General AViiONs, DG/UX 4.3x
  334. Xsgi:        _sysv    # Silicon Graphics Iris 4D, Irix SysV rel. 3.3.2
  335. Xsysv:        _sysv    # generic System V Unix
  336. Xxos:        _sysv    # Olivetti LSX-3005..3045, X/OS 2.3 and 2.4
  337. X
  338. X_sysv:
  339. X    $(MAKE) unzip CF="$(CF) -DSYSV -DTERMIO"
  340. X
  341. X# ---------------------------------------------------------------------------
  342. X#   "Unique" group (require non-standard options):
  343. X# ---------------------------------------------------------------------------
  344. X
  345. X# Apparently the C-120 has an optimization bug, and possibly another
  346. X# bug in the (SysV?) time routines which adds 11 years to the date.
  347. X# -DCONVEX not needed?  [RZM:  The remark above the C-120 entry 
  348. X# about a bug may not be true.  I think it is rather time procedures
  349. X# uncompatibility between unixes.] [GRR:  So is -O2 ok for c120?]
  350. X#
  351. Xc120:            # Convex C-120, OS 9.0, with non-vectorizing cc 4.0
  352. X    $(MAKE) unzip CF="-O1 $(LOC) -Dunix -DBSD"
  353. X
  354. Xc210:            # Convex C-210, OS 9.0, cc 4.0
  355. X    $(MAKE) unzip CF="-O2 $(LOC) -Dunix -DBSD"
  356. X
  357. X# Enclosed you'll find a context diff for the unzip41 makefile
  358. X# which enhances compilation on a convex.  The previous version
  359. X# probably worked great a couple of years ago, and would still do
  360. X# so if one compiles in our "backward compatible" pcc mode.   The
  361. X# following allows it to work better in a modern convexian environment.
  362. X# [This target results in the following error on various Convex's, 
  363. X# however:  "cc: Error on line 79 of file_io.c: 'ioctl' redeclared:
  364. X# incompatible types."]
  365. X#
  366. Xconvex:            # previous target was tested on C200/C400
  367. X    $(MAKE) unzip CF="$(CF) -Dunix -DCONVEX -ext" LF="$(LF) -ext"
  368. X
  369. X# Cray-2 and Y-MP, running Unicos 5.1 to 6.1 (SysV + BSD enhancements)
  370. X# and Standard (ANSI) C compiler 1.5, 2.0 or 3.0.
  371. Xcray:
  372. X    $(MAKE) unzip CC="scc" LD="scc"
  373. X
  374. X# Ditto, for Cray Standard C 3.0 or later.
  375. Xcray_v3:
  376. X    $(MAKE) unzip CC="scc" LD="scc" CF="$(CF) -h scalar3 -h vector3"
  377. X
  378. X# The unzip41 build on a Cyber 910/SGI running Irix v3.3.3 was successful
  379. X# with the following change to Makefile:
  380. Xcyber_sgi:
  381. X    $(MAKE) unzip CF="$(CF) -I/usr/include/bsd"\
  382. X     LF="-lbsd $(LF)"
  383. X
  384. X# The DIAB dnix 5.3 compiler does not define __STDC__ but understands
  385. X# prototypes, void, etc., anyway.  It also does not provide any predefined
  386. X# macros to detect this (aside from "unix" and the four file, line, time
  387. X# and date macros).  Thus we must define MODERN and PROTO by hand.
  388. X#
  389. Xdnix:        # 680X0, DIAB dnix 5.2/5.3 (a Swedish System V clone)
  390. X    $(MAKE) unzip CF="$(CF) -DPROTO -DMODERN"
  391. X
  392. X# Generic BSDish Unix gcc.  ``The -O2 only works with the latest version of
  393. X# gcc; you may have to use -O only for earlier versions.  I have no idea why
  394. X# -s causes this bug in gcc.''  [Bug:  "nm: unzip: no name list", "collect:
  395. X# /usr/bin/nm returned 1 exit status".]  If you don't have strip, don't
  396. X# worry about it (it just makes the executable smaller).
  397. X#
  398. Xgcc:
  399. X    $(MAKE) unzip CC=gcc LD=gcc CF="-O2 $(LOC)" LF2=""
  400. X    strip unzip
  401. X
  402. X# MS-DOS with D.J. Delorie's djgcc 1.06.  Note that go32 doesn't support
  403. X# dos function 0x38 (yet); to fix, append to line 400 of exphdlr.c (go32)
  404. X# the following:  "case 0x38:".
  405. X#
  406. Xgcc_dos:    # may need to add -Uunix to CF
  407. X    $(MAKE) unzip CC=gcc LD=gcc CF="-O2 -Wall $(LOC)"\
  408. X     LF="-s" LF2="-o unzip"
  409. X    aout2exe unzip
  410. X
  411. X# Heurikon HK68 (68010), UniPlus+ System V 5.0, Green Hills C-68000
  412. Xhk68:
  413. X    $(MAKE) unzip CC="gcc" LD="gcc" LF="-n $(LF)" \
  414. X    CF="-ga -X138 $(LOC) -Dlocaltime=localti -Dtimezone=timezon"
  415. X
  416. X# Rules needed to build the unzip program for an Iris Indigo running
  417. X# Irix Version 4.0.1
  418. Xindigo:
  419. X    $(MAKE) unzip CF="-cckr $(CF) -DTERMIO"
  420. X
  421. X# Linux is almost sysv but not quite
  422. Xlinux:                # Linux pre-0.96 with gcc 2.1
  423. X    $(MAKE) unzip CF="$(CF) -DTERMIO -DLINUX" CC=gcc LD=gcc
  424. X
  425. X# Minix 1.5 PC for the 386 with gcc or bcc
  426. Xminix:
  427. X    $(MAKE) unzip CC=gcc CF="$(CF) -DMINIX"
  428. X
  429. X# PCs (IBM-type), running MS-DOS, Microsoft C 6.0 and NMAKE.  Can't use
  430. X# SYSTEM environment variable:  "default" target is > 200 characters.
  431. X# "nmake msc_dos" works fine, aside from (possibly) an irrelevant message
  432. X# about the creation of a temporary file.  Environment variable LOCAL_UNZIP
  433. X# should be set via "SET LOCAL_UNZIP=-FPi87" if you use the 80x87 library;
  434. X# also add -G2 or -G3 if using a 286/386/486 system.
  435. X#
  436. X#msc_dos:
  437. X#    $(MAKE) unzip.exe\
  438. X#     CF="-Ox $(LOC) -nologo -G2" CC=cl LD=link E=.exe O=.obj\
  439. X#     LF="/noi/nol" LF2=",unzip;"
  440. X
  441. Xmsc_dos:    rsp
  442. X    $(MAKE) unzip.exe CF="-Ox $(LOC) -nologo" CC=cl LD=link E=.exe\
  443. X     O=.obj LOBJS="" LF="@rsp" LF2=""
  444. X    del rsp
  445. X
  446. Xrsp:
  447. X    echo $(OBJS1:.o=.obj)+ > rsp
  448. X    echo $(OBJS2:.o=.obj)/noi/e/st:0x1000; >> rsp
  449. X
  450. X# $(LOCAL_UNZIP):  math libraries and/or any other personal or debugging
  451. X#                  definitions:  e.g., SET LOCAL_UNZIP=-FPi87 -DDEBUG_STRUC
  452. X# $(NOD):  intended to be used as   SET NOD=-link /nod:slibcep   to allow the
  453. X#          use of default library names (slibce.lib) instead of protected-mode
  454. X#          names (slibcep.lib), but it fails:  MSC adds its own /nod qualifier,
  455. X#          and there seems to be no way to override this.  Typical...
  456. X#
  457. Xmsc_os2:        # 16-bit OS/2 (1.x) with MSC 6.00 (use makefile.os2)
  458. X    $(MAKE) -nologo unzip.exe zipinfo.exe CC=cl LD=cl E=.exe O=.obj\
  459. X     OBJS="$(OS2_OBJS)" OBJZ="$(OS2_OBJZ)"\
  460. X     CF="-nologo -AC -Ocegit -G2s -DOS2 -DMSC $(LOC)"\
  461. X     LF="-nologo -AC $(LOC) -Lp -F 2000"\
  462. X     LF2="unzip.def -o unzip.exe $(NOD)" LN="copy" RM="del"\
  463. X     ZL="-nologo -AC $(LOC) -Lp -Fb" ZL2="zipinfo.def -o zipinfo.exe"
  464. X
  465. X# NeXT 2.x: make the executable smaller.
  466. Xnext:            # 68030 BSD 4.3+Mach
  467. X    $(MAKE) unzip LF2="-object -s"
  468. X
  469. X# Rules to build the unzip program on a DecStation running DEC OSF/1 V1.0.
  470. X# This machine hasn't got ftime(3) in the standard C library.
  471. Xosf1:
  472. X    $(MAKE) unzip LF2="-lbsd"
  473. X
  474. X# I successfully compiled and tested the unzip program (v30) for the
  475. X# Silicon Graphics environment (Personal Iris 4D20/G with IRIX v3.2.2)
  476. Xp_iris:
  477. X    $(MAKE) unzip CF="$(CF) -I/usr/include/bsd -DBSD"\
  478. X     LF="-lbsd $(LF)"
  479. X
  480. X# I have finished porting unzip 3.0 to the Pyramid 90X under OSX4.1.
  481. X# The biggest problem was the default structure alignment yielding two
  482. X# extra bytes.  The compiler has the -q option to pack structures, and
  483. X# this was all that was needed.  To avoid needing ZMEMS we could compile
  484. X# in the AT&T universe, but it runs more slowly!
  485. X#
  486. X#UnZip 5.0f:  moved to regular targets as test
  487. X#pyramid:    # Pyramid 90X, probably all, under >= OSx4.1, BSD universe
  488. X#    make unzip CF="$(CF) -q"
  489. X
  490. X# IBM RS/6000 under AIX 3.2
  491. Xrs6000:
  492. X    $(MAKE) unzip CF="$(CF) -DBSD -D_BSD -DUNIX" LF="-lbsd $(LF)"
  493. X
  494. X# SCO cross compile from unix to DOS. Tested with Xenix/386 and OpenDeskTop.
  495. X# Should work with xenix/286 as well. (davidsen)  Note that you *must* remove
  496. X# the unix objects and executable before doing this!  (Piet Plomp:  gcc won't
  497. X# recognize the -M0 flag which forces 8086 code.)
  498. X#
  499. Xsco_dos:    # uncomment zipinfo in UNZIPS if desired
  500. X    $(MAKE) unzips CF="-O $(LOC) -DNO_ERRNO -dos -M0" LF="-dos -F 2000"\
  501. X     LF2="-o unzip.exe" ZL="-dos" ZL2="-o zipinfo.exe"
  502. X
  503. X# SCO Xenix/286 2.2.1
  504. Xsco_x286:
  505. X    $(MAKE) unzip CF="$(CF) -Ml2" LF="$(LF) -Ml2"
  506. X
  507. X# Sequent Symmetry is a 386 but needs -DZMEM
  508. X# This should also work on Balance but I can't test it just yet.
  509. Xsequent:    # Sequent w/Dynix
  510. X    $(MAKE) unzip CF="$(CF) -DBSD -DZMEM"
  511. X
  512. X# AT&T 6300+, running System V.? Unix:  out-of-memory error if don't use -Ml
  513. Xsysv6300:
  514. X    $(MAKE) unzip CF="$(CF) -Ml -DTERMIO" LF="$(LF) -Ml"
  515. X
  516. X# I didn't do this.  I swear.  No, really.
  517. Xwombat:        # Wombat 68000 (or whatever)
  518. X    @echo
  519. X    @echo  '    Ha ha!  Just kidding.'
  520. X    @echo
  521. X
  522. X
  523. X#####################
  524. X# ZIPINFO MAKERULES #
  525. X#####################
  526. X
  527. X# ZipInfo section:  less hand-holding here, but it should be pretty
  528. X# straightforward by now.
  529. X
  530. Xzipinfo$O:    zipinfo.c unzip.h
  531. X    $(CC) -c $(CF) zipinfo.c
  532. X
  533. Xmisc_$O:    misc.c unzip.h
  534. X    $(LN) misc.c misc_.c
  535. X    $(CC) -c $(CF) $(ZC) misc_.c
  536. X    $(RM) misc_.c
  537. X
  538. Xos2zinfo$O:    os2unzip.c unzip.h
  539. X    $(LN) os2unzip.c os2zinfo.c
  540. X    $(CC) -c $(CF) $(ZC) os2zinfo.c
  541. X    $(RM) os2zinfo.c
  542. X
  543. Xzipinfo$E:    $(OBJZ)
  544. X    $(LD) $(ZL) $(OBJZ) $(ZL2)
  545. X
  546. Xzi_gcc:            # GNU gcc under Unix (if no strip, don't worry)
  547. X    $(MAKE) zipinfo CC=gcc LD=gcc ZL2=""
  548. X    strip zipinfo
  549. X
  550. Xzi_indigo:        # SGI Iris Indigo
  551. X    $(MAKE) zipinfo CF="-cckr -O -DUNIX $(LOC)"
  552. X
  553. Xzi_dos:            # MSC 6.0 + nmake, MS-DOS
  554. X    $(MAKE) zipinfo.exe CF="-Ox -nologo $(LOC) -G2" CC=cl\
  555. X     LD=link E=.exe O=.obj ZL="/noi /nol" ZL2=",zipinfo;"\
  556. X     LN="copy" RM="DEL"
  557. X
  558. X
  559. X####################
  560. X# FUNZIP MAKERULES #
  561. X####################
  562. X
  563. X# FUnZip section:  FUnZip (Filter UnZip) is a last-minute addition to the
  564. X# UnZip suite and is still VERY raw.  Its purpose is to take a zipfile from 
  565. X# stdin and decompress the first entry to stdout.  Only non-encrypted, stored
  566. X# or deflated files are allowed at present.  FUnZip may be absorbed into
  567. X# regular UnZip in a subsequent release.  This target should work for some
  568. X# Unix systems but is not guaranteed to work for all (or even most).
  569. X
  570. Xfunzip$E:    $(OBJF)
  571. X    $(LD) $(FL) $(OBJF) $(FL2)
  572. X
  573. Xfu_gcc:            # GNU gcc under Unix (if no strip, don't worry)
  574. X    $(MAKE) funzip CC=gcc LD=gcc FL2=""
  575. X    strip funzip
  576. X
  577. X
  578. X################
  579. X# ATTRIBUTIONS #
  580. X################
  581. X
  582. X# Thanks to the following people for their help in testing and/or porting
  583. X# to various machines (and thanks to the many others who aren't listed
  584. X# here but should be):
  585. X#
  586. X#  (original Unix port:  Carl Mascott <cmascott@world.std.com>)
  587. X#  386i:    Richard Stephen <stephen@corp.telecom.co.nz>
  588. X#  3Bx:        Bob Kemp <hrrca!bobc@cbnewse.att.com>
  589. X#  7300:    Richard H. Gumpertz <rhg@cpsolv.CPS.COM>
  590. X#        Greg Roelofs <roelofs@amelia.nas.nasa.gov>
  591. X#  amdahl:    Kim DeVaughn <ked01@juts.ccc.amdahl.com>, Greg Roelofs
  592. X#  apollo:    Tim Geibelhaus
  593. X#  aviion:    Bruce Kahn <bkahn@archive.webo.dg.com>
  594. X#  bull:    Matt D'Errico <doc@magna.com>
  595. X#  c120:    Rafal Maszkowski <sgumk%pltumk11.bitnet>
  596. X#  coherent:    David Fenyes <dfenyes@thesis1.med.uth.tmc.edu>
  597. X#  convex:    Randy Wright <rwright@convex.com>
  598. X#  cray:    Greg Roelofs, Paul Borman <prb@cray.com>
  599. X#  cray_cc:    Greg Roelofs
  600. X#  cray_v3:    Greg Roelofs
  601. X#  cyber_sgi:    Clint Pulley <u001@cs910.cciw.ca>
  602. X#  dec:        "Moby" Dick O'Connor <djo7613@u.washington.edu>
  603. X#  dnix:    Bo Kullmar <bk@kullmar.se>
  604. X#  eta:        Greg Flint <afc@klaatu.cc.purdue.edu>
  605. X#  gcc:        Jean-loup Gailly <jloup@chorus.fr>
  606. X#  gcc_dos:    Onno van der Linden <linden@fwi.uva.nl>
  607. X#  gcc_os2:    Kai Uwe Rommel <rommel@informatik.tu-muenchen.de>
  608. X#  gould:    Onno van der Linden
  609. X#  hk68:    John Limpert <gronk!johnl@uunet.UU.NET>
  610. X#  hp:        Randy McCaskile <rmccask@seas.gwu.edu> (HP-UX)
  611. X#        Gershon Elber <gershon@cs.utah.edu> (HP BSD 4.3)
  612. X#  icc_os2:    Kai Uwe Rommel
  613. X#  indigo:    Kjetil Wiekhorst J|rgensen <jorgens@lise.unit.no>
  614. X#  linux:    Humberto Ortiz-Zuazaga <zuazaga@ucunix.san.uc.edu>
  615. X#  minix:    Kai Uwe Rommel (Minix 1.5)
  616. X#  mips:    Peter Jones <jones@mips1.uqam.ca>
  617. X#  msc_dos:    Greg Roelofs <roe2@ellis.uchicago.edu>
  618. X#        Piet W. Plomp <piet@icce.rug.nl>
  619. X#  msc_os2:    Wim Bonner <wbonner@yoda.eecs.wsu.edu>
  620. X#        Kai Uwe Rommel, Greg Roelofs
  621. X#  next:    Mark Adler <madler@piglet.caltech.edu>
  622. X#  osf1:    Kjetil Wiekhorst J{\o}rgensen
  623. X#  p_iris:    Valter V. Cavecchia <root@itnsg1.cineca.it>
  624. X#  pyramid:    James Dugal <jpd@usl.edu>
  625. X#  rs6000:    Filip Gieszczykiewicz <fmg@smi.med.pitt.edu>
  626. X#        Trevor Paquette <tpaquett@ita.lgc.com>
  627. X#  rtaix:    Erik-Jan Vens
  628. X#  sco:        Onno van der Linden (SCO Unix 3.2.0)
  629. X#           Bill Davidsen <davidsen@crdos1.crd.ge.com> (Xenix/386)
  630. X#  sco_dos:    Bill Davidsen, Piet W. Plomp
  631. X#  sco_x286:    Ricky Mobley <ddi1!lrark!rick@uunet.UU.NET>
  632. X#  sequent:    Phil Howard <phil@ux1.cso.uiuc.edu>
  633. X#  sgi:        Greg Roelofs (Iris 4D/380?)
  634. X#  sun:        Onno van der Linden (Sun 4), Greg Roelofs (Sun 3, 4)
  635. X#  sysv:    Greg Roelofs
  636. X#  sysv6300:    Peter Mauzey <ptm@mtdcc.att.com>
  637. X#  tahoe:    Mark Edwards <mce%sdcc10@ucsd.edu>
  638. X#  ultrix:    Greg Flint (VAX)
  639. X#        Michael Graff <explorer@iastate.edu> (DECstation 2100?)
  640. X#        Greg Roelofs (DEC 5810)
  641. X#        Alex A Sergejew <aas@brain.wph.uq.oz.au>
  642. X#  vax:        Forrest Gehrke <feg@dodger.att.com> (SysV)
  643. X#        David Kirschbaum <kirsch@usasoc.soc.mil> (BSD 4.3)
  644. X#        Jim Steiner <steiner@pica.army.mil> (8600+Ultrix)
  645. X#  wombat:    Joe Isuzu <joe@trustme.isuzu.com>
  646. X#  xos:        Fulvio Marino <fulvio@iconet.ico.olivetti.com>
  647. X#  zi_dos:    Greg Roelofs
  648. X#  zi_icc:    Kai Uwe Rommel
  649. X#  zi_os2:    Greg Roelofs, Kai Uwe Rommel
  650. X#  zipinfo:    Greg Roelofs
  651. END_OF_FILE
  652.   if test 22919 -ne `wc -c <'Makefile'`; then
  653.     echo shar: \"'Makefile'\" unpacked with wrong size!
  654.   fi
  655.   # end of 'Makefile'
  656. fi
  657. if test -f 'inflate.c' -a "${1}" != "-c" ; then 
  658.   echo shar: Will not clobber existing file \"'inflate.c'\"
  659. else
  660.   echo shar: Extracting \"'inflate.c'\" \(32972 characters\)
  661.   sed "s/^X//" >'inflate.c' <<'END_OF_FILE'
  662. X/* inflate.c -- Not copyrighted 1992 by Mark Adler
  663. X   version c7, 27 June 1992 */
  664. X
  665. X
  666. X/* You can do whatever you like with this source file, though I would
  667. X   prefer that if you modify it and redistribute it that you include
  668. X   comments to that effect with your name and the date.  Thank you.
  669. X
  670. X   History:
  671. X   vers    date          who           what
  672. X   ----  ---------  --------------  ------------------------------------
  673. X    a    ~~ Feb 92  M. Adler        used full (large, one-step) lookup table
  674. X    b1   21 Mar 92  M. Adler        first version with partial lookup tables
  675. X    b2   21 Mar 92  M. Adler        fixed bug in fixed-code blocks
  676. X    b3   22 Mar 92  M. Adler        sped up match copies, cleaned up some
  677. X    b4   25 Mar 92  M. Adler        added prototypes; removed window[] (now
  678. X                                    is the responsibility of unzip.h--also
  679. X                                    changed name to slide[]), so needs diffs
  680. X                                    for unzip.c and unzip.h (this allows
  681. X                                    compiling in the small model on MSDOS);
  682. X                                    fixed cast of q in huft_build();
  683. X    b5   26 Mar 92  M. Adler        got rid of unintended macro recursion.
  684. X    b6   27 Mar 92  M. Adler        got rid of nextbyte() routine.  fixed
  685. X                                    bug in inflate_fixed().
  686. X    c1   30 Mar 92  M. Adler        removed lbits, dbits environment variables.
  687. X                                    changed BMAX to 16 for explode.  Removed
  688. X                                    OUTB usage, and replaced it with flush()--
  689. X                                    this was a 20% speed improvement!  Added
  690. X                                    an explode.c (to replace unimplode.c) that
  691. X                                    uses the huft routines here.  Removed
  692. X                                    register union.
  693. X    c2    4 Apr 92  M. Adler        fixed bug for file sizes a multiple of 32k.
  694. X    c3   10 Apr 92  M. Adler        reduced memory of code tables made by
  695. X                                    huft_build significantly (factor of two to
  696. X                                    three).
  697. X    c4   15 Apr 92  M. Adler        added NOMEMCPY do kill use of memcpy().
  698. X                                    worked around a Turbo C optimization bug.
  699. X    c5   21 Apr 92  M. Adler        added the WSIZE #define to allow reducing
  700. X                                    the 32K window size for specialized
  701. X                                    applications.
  702. X    c6   31 May 92  M. Adler        added some typecasts to eliminate warnings
  703. X    c7   27 Jun 92  G. Roelofs      added some more typecasts (439:  MSC bug)
  704. X */
  705. X
  706. X
  707. X/*
  708. X   Inflate deflated (PKZIP's method 8 compressed) data.  The compression
  709. X   method searches for as much of the current string of bytes (up to a
  710. X   length of 258) in the previous 32K bytes.  If it doesn't find any
  711. X   matches (of at least length 3), it codes the next byte.  Otherwise, it
  712. X   codes the length of the matched string and its distance backwards from
  713. X   the current position.  There is a single Huffman code that codes both
  714. X   single bytes (called "literals") and match lengths.  A second Huffman
  715. X   code codes the distance information, which follows a length code.  Each
  716. X   length or distance code actually represents a base value and a number
  717. X   of "extra" (sometimes zero) bits to get to add to the base value.  At
  718. X   the end of each deflated block is a special end-of-block (EOB) literal/
  719. X   length code.  The decoding process is basically: get a literal/length
  720. X   code; if EOB then done; if a literal, emit the decoded byte; if a
  721. X   length then get the distance and emit the referred-to bytes from the
  722. X   sliding window of previously emitted data.
  723. X
  724. X   There are (currently) three kinds of inflate blocks: stored, fixed, and
  725. X   dynamic.  The compressor deals with some chunk of data at a time, and
  726. X   decides which method to use on a chunk-by-chunk basis.  A chunk might
  727. X   typically be 32K or 64K.  If the chunk is uncompressible, then the
  728. X   "stored" method is used.  In this case, the bytes are simply stored as
  729. X   is, eight bits per byte, with none of the above coding.  The bytes are
  730. X   preceded by a count, since there is no longer an EOB code.
  731. X
  732. X   If the data is compressible, then either the fixed or dynamic methods
  733. X   are used.  In the dynamic method, the compressed data is preceded by
  734. X   an encoding of the literal/length and distance Huffman codes that are
  735. X   to be used to decode this block.  The representation is itself Huffman
  736. X   coded, and so is preceded by a description of that code.  These code
  737. X   descriptions take up a little space, and so for small blocks, there is
  738. X   a predefined set of codes, called the fixed codes.  The fixed method is
  739. X   used if the block codes up smaller that way (usually for quite small
  740. X   chunks), otherwise the dynamic method is used.  In the latter case, the
  741. X   codes are customized to the probabilities in the current block, and so
  742. X   can code it much better than the pre-determined fixed codes.
  743. X   The Huffman codes themselves are decoded using a mutli-level table
  744. X   lookup, in order to maximize the speed of decoding plus the speed of
  745. X   building the decoding tables.  See the comments below that precede the
  746. X   lbits and dbits tuning parameters.
  747. X */
  748. X
  749. X
  750. X/*
  751. X   Notes beyond the 1.93a appnote.txt:
  752. X
  753. X   1. Distance pointers never point before the beginning of the output
  754. X      stream.
  755. X   2. Distance pointers can point back across blocks, up to 32k away.
  756. X   3. There is an implied maximum of 7 bits for the bit length table and
  757. X      15 bits for the actual data.
  758. X   4. If only one code exists, then it is encoded using one bit.  (Zero
  759. X      would be more efficient, but perhaps a little confusing.)  If two
  760. X      codes exist, they are coded using one bit each (0 and 1).
  761. X   5. There is no way of sending zero distance codes--a dummy must be
  762. X      sent if there are none.  (History: a pre 2.0 version of PKZIP would
  763. X      store blocks with no distance codes, but this was discovered to be
  764. X      too harsh a criterion.)
  765. X   6. There are up to 286 literal/length codes.  Code 256 represents the
  766. X      end-of-block.  Note however that the static length tree defines
  767. X      288 codes just to fill out the Huffman codes.  Codes 286 and 287
  768. X      cannot be used though, since there is no length base or extra bits
  769. X      defined for them.  Similarily, there are up to 30 distance codes.
  770. X      However, static trees define 32 codes (all 5 bits) to fill out the
  771. X      Huffman codes, but the last two had better not show up in the data.
  772. X   7. Unzip can check dynamic Huffman blocks for complete code sets.
  773. X      The exception is that a single code would not be complete (see #4).
  774. X   8. The five bits following the block type is really the number of
  775. X      literal codes sent minus 257.
  776. X   9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
  777. X      (1+6+6).  Therefore, to output three times the length, you output
  778. X      three codes (1+1+1), whereas to output four times the same length,
  779. X      you only need two codes (1+3).  Hmm.
  780. X  10. In the tree reconstruction algorithm, Code = Code + Increment
  781. X      only if BitLength(i) is not zero.  (Pretty obvious.)
  782. X  11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
  783. X  12. Note: length code 284 can represent 227-258, but length code 285
  784. X      really is 258.  The last length deserves its own, short code
  785. X      since it gets used a lot in very redundant files.  The length
  786. X      258 is special since 258 - 3 (the min match length) is 255.
  787. X  13. The literal/length and distance code bit lengths are read as a
  788. X      single stream of lengths.  It is possible (and advantageous) for
  789. X      a repeat code (16, 17, or 18) to go across the boundary between
  790. X      the two sets of lengths.
  791. X */
  792. X
  793. X#include "unzip.h"      /* this must supply the slide[] (byte) array */
  794. X
  795. X#ifndef WSIZE
  796. X#  define WSIZE 0x8000  /* window size--must be a power of two, and at least
  797. X                           32K for zip's deflate method */
  798. X#endif /* !WSIZE */
  799. X
  800. X
  801. X/* Huffman code lookup table entry--this entry is four bytes for machines
  802. X   that have 16-bit pointers (e.g. PC's in the small or medium model).
  803. X   Valid extra bits are 0..13.  e == 15 is EOB (end of block), e == 16
  804. X   means that v is a literal, 16 < e < 32 means that v is a pointer to
  805. X   the next table, which codes e - 16 bits, and lastly e == 99 indicates
  806. X   an unused code.  If a code with e == 99 is looked up, this implies an
  807. X   error in the data. */
  808. Xstruct huft {
  809. X  byte e;               /* number of extra bits or operation */
  810. X  byte b;               /* number of bits in this code or subcode */
  811. X  union {
  812. X    UWORD n;            /* literal, length base, or distance base */
  813. X    struct huft *t;     /* pointer to next level of table */
  814. X  } v;
  815. X};
  816. X
  817. X
  818. X/* Function prototypes */
  819. Xint huft_build OF((unsigned *, unsigned, unsigned, UWORD *, UWORD *,
  820. X                   struct huft **, int *));
  821. Xint huft_free OF((struct huft *));
  822. Xvoid flush OF((unsigned));
  823. Xint inflate_codes OF((struct huft *, struct huft *, int, int));
  824. Xint inflate_stored OF((void));
  825. Xint inflate_fixed OF((void));
  826. Xint inflate_dynamic OF((void));
  827. Xint inflate_block OF((int *));
  828. Xint inflate_entry OF((void));
  829. Xvoid inflate OF((void));
  830. X
  831. X
  832. X/* The inflate algorithm uses a sliding 32K byte window on the uncompressed
  833. X   stream to find repeated byte strings.  This is implemented here as a
  834. X   circular buffer.  The index is updated simply by incrementing and then
  835. X   and'ing with 0x7fff (32K-1). */
  836. X/* It is left to other modules to supply the 32K area.  It is assumed
  837. X   to be usable as if it were declared "byte slide[32768];" or as just
  838. X   "byte *slide;" and then malloc'ed in the latter case.  The definition
  839. X   must be in unzip.h, included above. */
  840. Xunsigned wp;            /* current position in slide */
  841. X
  842. X
  843. X/* Tables for deflate from PKZIP's appnote.txt. */
  844. Xstatic unsigned border[] = {    /* Order of the bit length code lengths */
  845. X        16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  846. Xstatic UWORD cplens[] = {       /* Copy lengths for literal codes 257..285 */
  847. X        3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  848. X        35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
  849. X        /* note: see note #13 above about the 258 in this list. */
  850. Xstatic UWORD cplext[] = {       /* Extra bits for literal codes 257..285 */
  851. X        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  852. X        3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
  853. Xstatic UWORD cpdist[] = {       /* Copy offsets for distance codes 0..29 */
  854. X        1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  855. X        257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  856. X        8193, 12289, 16385, 24577};
  857. Xstatic UWORD cpdext[] = {       /* Extra bits for distance codes */
  858. X        0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  859. X        7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  860. X        12, 12, 13, 13};
  861. X
  862. X
  863. X
  864. X/* Macros for inflate() bit peeking and grabbing.
  865. X   The usage is:
  866. X   
  867. X        NEEDBITS(j)
  868. X        x = b & mask_bits[j];
  869. X        DUMPBITS(j)
  870. X
  871. X   where NEEDBITS makes sure that b has at least j bits in it, and
  872. X   DUMPBITS removes the bits from b.  The macros use the variable k
  873. X   for the number of bits in b.  Normally, b and k are register
  874. X   variables for speed, and are initialized at the begining of a
  875. X   routine that uses these macros from a global bit buffer and count.
  876. X
  877. X   If we assume that EOB will be the longest code, then we will never
  878. X   ask for bits with NEEDBITS that are beyond the end of the stream.
  879. X   So, NEEDBITS should not read any more bytes than are needed to
  880. X   meet the request.  Then no bytes need to be "returned" to the buffer
  881. X   at the end of the last block.
  882. X
  883. X   However, this assumption is not true for fixed blocks--the EOB code
  884. X   is 7 bits, but the other literal/length codes can be 8 or 9 bits.
  885. X   (Why PK made the EOB code, which can only occur once in a block,
  886. X   the *shortest* code in the set, I'll never know.)  However, by
  887. X   making the first table have a lookup of seven bits, the EOB code
  888. X   will be found in that first lookup, and so will not require that too
  889. X   many bits be pulled from the stream.
  890. X */
  891. X
  892. XULONG bb;                       /* bit buffer */
  893. Xunsigned bk;                    /* bits in bit buffer */
  894. X
  895. XUWORD bytebuf;
  896. X#define NEXTBYTE    (ReadByte(&bytebuf), bytebuf)
  897. X#define NEEDBITS(n) {while(k<(n)){b|=((ULONG)NEXTBYTE)<<k;k+=8;}}
  898. X#define DUMPBITS(n) {b>>=(n);k-=(n);}
  899. X
  900. X
  901. X/*
  902. X   Huffman code decoding is performed using a multi-level table lookup.
  903. X   The fastest way to decode is to simply build a lookup table whose
  904. X   size is determined by the longest code.  However, the time it takes
  905. X   to build this table can also be a factor if the data being decoded
  906. X   is not very long.  The most common codes are necessarily the
  907. X   shortest codes, so those codes dominate the decoding time, and hence
  908. X   the speed.  The idea is you can have a shorter table that decodes the
  909. X   shorter, more probable codes, and then point to subsidiary tables for
  910. X   the longer codes.  The time it costs to decode the longer codes is
  911. X   then traded against the time it takes to make longer tables.
  912. X
  913. X   This results of this trade are in the variables lbits and dbits
  914. X   below.  lbits is the number of bits the first level table for literal/
  915. X   length codes can decode in one step, and dbits is the same thing for
  916. X   the distance codes.  Subsequent tables are also less than or equal to
  917. X   those sizes.  These values may be adjusted either when all of the
  918. X   codes are shorter than that, in which case the longest code length in
  919. X   bits is used, or when the shortest code is *longer* than the requested
  920. X   table size, in which case the length of the shortest code in bits is
  921. X   used.
  922. X
  923. X   There are two different values for the two tables, since they code a
  924. X   different number of possibilities each.  The literal/length table
  925. X   codes 286 possible values, or in a flat code, a little over eight
  926. X   bits.  The distance table codes 30 possible values, or a little less
  927. X   than five bits, flat.  The optimum values for speed end up being
  928. X   about one bit more than those, so lbits is 8+1 and dbits is 5+1.
  929. X   The optimum values may differ though from machine to machine, and
  930. X   possibly even between compilers.  Your mileage may vary.
  931. X */
  932. X
  933. X
  934. Xint lbits = 9;          /* bits in base literal/length lookup table */
  935. Xint dbits = 6;          /* bits in base distance lookup table */
  936. X
  937. X
  938. X/* If BMAX needs to be larger than 16, then h and x[] should be ULONG. */
  939. X#define BMAX 16         /* maximum bit length of any code (16 for explode) */
  940. X#define N_MAX 288       /* maximum number of codes in any set */
  941. X
  942. X
  943. Xunsigned hufts;         /* track memory usage */
  944. X
  945. X
  946. Xint huft_build(b, n, s, d, e, t, m)
  947. Xunsigned *b;            /* code lengths in bits (all assumed <= BMAX) */
  948. Xunsigned n;             /* number of codes (assumed <= N_MAX) */
  949. Xunsigned s;             /* number of simple-valued codes (0..s-1) */
  950. XUWORD *d;               /* list of base values for non-simple codes */
  951. XUWORD *e;               /* list of extra bits for non-simple codes */
  952. Xstruct huft **t;        /* result: starting table */
  953. Xint *m;                 /* maximum lookup bits, returns actual */
  954. X/* Given a list of code lengths and a maximum table size, make a set of
  955. X   tables to decode that set of codes.  Return zero on success, one if
  956. X   the given code set is incomplete (the tables are still built in this
  957. X   case), two if the input is invalid (all zero length codes or an
  958. X   oversubscribed set of lengths), and three if not enough memory. */
  959. X{
  960. X  unsigned a;                   /* counter for codes of length k */
  961. X  unsigned c[BMAX+1];           /* bit length count table */
  962. X  unsigned f;                   /* i repeats in table every f entries */
  963. X  int g;                        /* maximum code length */
  964. X  int h;                        /* table level */
  965. X  register unsigned i;          /* counter, current code */
  966. X  register unsigned j;          /* counter */
  967. X  register int k;               /* number of bits in current code */
  968. X  int l;                        /* bits per table (returned in m) */
  969. X  register unsigned *p;         /* pointer into c[], b[], or v[] */
  970. X  register struct huft *q;      /* points to current table */
  971. X  struct huft r;                /* table entry for structure assignment */
  972. X  struct huft *u[BMAX];         /* table stack */
  973. X  unsigned v[N_MAX];            /* values in order of bit length */
  974. X  register int w;               /* bits before this table == (l * h) */
  975. X  unsigned x[BMAX+1];           /* bit offsets, then code stack */
  976. X  unsigned *xp;                 /* pointer into x */
  977. X  int y;                        /* number of dummy codes added */
  978. X  unsigned z;                   /* number of entries in current table */
  979. X
  980. X
  981. X  /* Generate counts for each bit length */
  982. X  memset(c, 0, sizeof(c));
  983. X  p = b;  i = n;
  984. X  do {
  985. X    c[*p++]++;                  /* assume all entries <= BMAX */
  986. X  } while (--i);
  987. X  if (c[0] == n)
  988. X    return 2;                   /* bad input--all zero length codes */
  989. X
  990. X
  991. X  /* Find minimum and maximum length, bound *m by those */
  992. X  l = *m;
  993. X  for (j = 1; j <= BMAX; j++)
  994. X    if (c[j])
  995. X      break;
  996. X  k = j;                        /* minimum code length */
  997. X  if ((unsigned)l < j)
  998. X    l = j;
  999. X  for (i = BMAX; i; i--)
  1000. X    if (c[i])
  1001. X      break;
  1002. X  g = i;                        /* maximum code length */
  1003. X  if ((unsigned)l > i)
  1004. X    l = i;
  1005. X  *m = l;
  1006. X
  1007. X
  1008. X  /* Adjust last length count to fill out codes, if needed */
  1009. X  for (y = 1 << j; j < i; j++, y <<= 1)
  1010. X    if ((y -= c[j]) < 0)
  1011. X      return 2;                 /* bad input: more codes than bits */
  1012. X  if ((y -= c[i]) < 0)
  1013. X    return 2;
  1014. X  c[i] += y;
  1015. X
  1016. X
  1017. X  /* Generate starting offsets into the value table for each length */
  1018. X  x[1] = j = 0;
  1019. X  p = c + 1;  xp = x + 2;
  1020. X  while (--i) {                 /* note that i == g from above */
  1021. X    *xp++ = (j += *p++);
  1022. X  }
  1023. X
  1024. X
  1025. X  /* Make a table of values in order of bit lengths */
  1026. X  p = b;  i = 0;
  1027. X  do {
  1028. X    if ((j = *p++) != 0)
  1029. X      v[x[j]++] = i;
  1030. X  } while (++i < n);
  1031. X
  1032. X
  1033. X  /* Generate the Huffman codes and for each, make the table entries */
  1034. X  x[0] = i = 0;                 /* first Huffman code is zero */
  1035. X  p = v;                        /* grab values in bit order */
  1036. X  h = -1;                       /* no tables yet--level -1 */
  1037. X  w = -l;                       /* bits decoded == (l * h) */
  1038. X  u[0] = (struct huft *)NULL;   /* just to keep compilers happy */
  1039. X  q = (struct huft *)NULL;      /* ditto */
  1040. X  z = 0;                        /* ditto */
  1041. X
  1042. X  /* go through the bit lengths (k already is bits in shortest code) */
  1043. X  for (; k <= g; k++)
  1044. X  {
  1045. X    a = c[k];
  1046. X    while (a--)
  1047. X    {
  1048. X      /* here i is the Huffman code of length k bits for value *p */
  1049. X      /* make tables up to required level */
  1050. X      while (k > w + l)
  1051. X      {
  1052. X        h++;
  1053. X        w += l;                 /* previous table always l bits */
  1054. X
  1055. X        /* compute minimum size table less than or equal to l bits */
  1056. X        z = (z = g - w) > (unsigned)l ? l : z;  /* upper limit on table size */
  1057. X        if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
  1058. X        {                       /* too few codes for k-w bit table */
  1059. X          f -= a + 1;           /* deduct codes from patterns left */
  1060. X          xp = c + k;
  1061. X          while (++j < z)       /* try smaller tables up to z bits */
  1062. X          {
  1063. X            if ((f <<= 1) <= *++xp)
  1064. X              break;            /* enough codes to use up j bits */
  1065. X            f -= *xp;           /* else deduct codes from patterns */
  1066. X          }
  1067. X        }
  1068. X        z = 1 << j;             /* table entries for j-bit table */
  1069. X
  1070. X        /* allocate and link in new table */
  1071. X        if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) ==
  1072. X            (struct huft *)NULL)
  1073. X        {
  1074. X          if (h)
  1075. X            huft_free(u[0]);
  1076. X          fprintf(stderr, "\n*** inflate out of memory *** ");
  1077. X          return 3;             /* not enough memory */
  1078. X        }
  1079. X        hufts += z + 1;         /* track memory usage */
  1080. X        *t = q + 1;             /* link to list for huft_free() */
  1081. X        *(t = &(q->v.t)) = (struct huft *)NULL;
  1082. X        u[h] = ++q;             /* table starts after link */
  1083. X
  1084. X        /* connect to last table, if there is one */
  1085. X        if (h)
  1086. X        {
  1087. X          x[h] = i;             /* save pattern for backing up */
  1088. X          r.b = (byte)l;        /* bits to dump before this table */
  1089. X          r.e = (byte)(16 + j); /* bits in this table */
  1090. X          r.v.t = q;            /* pointer to this table */
  1091. X          j = i >> (w - l);     /* (get around Turbo C bug) */
  1092. X          u[h-1][j] = r;        /* connect to last table */
  1093. X        }
  1094. X      }
  1095. X
  1096. X      /* set up table entry in r */
  1097. X      r.b = (byte)(k - w);
  1098. X      if (p >= v + n)
  1099. X        r.e = 99;               /* out of values--invalid code */
  1100. X      else if (*p < s)
  1101. X      {
  1102. X        r.e = (byte)(*p < 256 ? 16 : 15);    /* 256 is end-of-block code */
  1103. X        r.v.n = *p++;           /* simple code is just the value */
  1104. X      }
  1105. X      else
  1106. X      {
  1107. X        r.e = (byte)e[*p - s];  /* non-simple--look up in lists */
  1108. X        r.v.n = d[*p++ - s];
  1109. X      }
  1110. X
  1111. X      /* fill code-like entries with r */
  1112. X      f = 1 << (k - w);
  1113. X      for (j = i >> w; j < z; j += f)
  1114. X        q[j] = r;
  1115. X
  1116. X      /* backwards increment the k-bit code i */
  1117. X      for (j = 1 << (k - 1); i & j; j >>= 1)
  1118. X        i ^= j;
  1119. X      i ^= j;
  1120. X
  1121. X      /* backup over finished tables */
  1122. X      while ((i & ((1 << w) - 1)) != x[h])
  1123. X      {
  1124. X        h--;                    /* don't need to update q */
  1125. X        w -= l;
  1126. X      }
  1127. X    }
  1128. X  }
  1129. X
  1130. X
  1131. X  /* Return true (1) if we were given an incomplete table */
  1132. X  return y != 0 && n != 1;
  1133. X}
  1134. X
  1135. X
  1136. X
  1137. Xint huft_free(t)
  1138. Xstruct huft *t;         /* table to free */
  1139. X/* Free the malloc'ed tables built by huft_build(), which makes a linked
  1140. X   list of the tables it made, with the links in a dummy first entry of
  1141. X   each table. */
  1142. X{
  1143. X  register struct huft *p, *q;
  1144. X
  1145. X
  1146. X  /* Go through linked list, freeing from the malloced (t[-1]) address. */
  1147. X  p = t;
  1148. X  while (p != (struct huft *)NULL)
  1149. X  {
  1150. X    q = (--p)->v.t;
  1151. X    free(p);
  1152. X    p = q;
  1153. X  } 
  1154. X  return 0;
  1155. X}
  1156. X
  1157. X
  1158. X
  1159. Xvoid flush(w)
  1160. Xunsigned w;             /* number of bytes to flush */
  1161. X/* Do the equivalent of OUTB for the bytes slide[0..w-1]. */
  1162. X{
  1163. X  unsigned n;
  1164. X  byte *p;
  1165. X
  1166. X  p = slide;
  1167. X  while (w)
  1168. X  {
  1169. X    n = (n = OUTBUFSIZ - outcnt) < w ? n : w;
  1170. X    memcpy(outptr, p, n);       /* try to fill up buffer */
  1171. X    outptr += n;
  1172. X    if ((outcnt += n) == OUTBUFSIZ)
  1173. X      FlushOutput();            /* if full, empty */
  1174. X    p += n;
  1175. X    w -= n;
  1176. X  }
  1177. X}
  1178. X
  1179. X
  1180. X
  1181. Xint inflate_codes(tl, td, bl, bd)
  1182. Xstruct huft *tl, *td;   /* literal/length and distance decoder tables */
  1183. Xint bl, bd;             /* number of bits decoded by tl[] and td[] */
  1184. X/* inflate (decompress) the codes in a deflated (compressed) block.
  1185. X   Return an error code or zero if it all goes ok. */
  1186. X{
  1187. X  register unsigned e;  /* table entry flag/number of extra bits */
  1188. X  unsigned n, d;        /* length and index for copy */
  1189. X  unsigned w;           /* current window position */
  1190. X  struct huft *t;       /* pointer to table entry */
  1191. X  unsigned ml, md;      /* masks for bl and bd bits */
  1192. X  register ULONG b;     /* bit buffer */
  1193. X  register unsigned k;  /* number of bits in bit buffer */
  1194. X
  1195. X
  1196. X  /* make local copies of globals */
  1197. X  b = bb;                       /* initialize bit buffer */
  1198. X  k = bk;
  1199. X  w = wp;                       /* initialize window position */
  1200. X
  1201. X
  1202. X  /* inflate the coded data */
  1203. X  ml = mask_bits[bl];           /* precompute masks for speed */
  1204. X  md = mask_bits[bd];
  1205. X  while (1)                     /* do until end of block */
  1206. X  {
  1207. X    NEEDBITS((unsigned)bl)
  1208. X    if ((e = (t = tl + ((unsigned)b & ml))->e) > 16)
  1209. X      do {
  1210. X        if (e == 99)
  1211. X          return 1;
  1212. X        DUMPBITS(t->b)
  1213. X        e -= 16;
  1214. X        NEEDBITS(e)
  1215. X      } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
  1216. X    DUMPBITS(t->b)
  1217. X    if (e == 16)                /* then it's a literal */
  1218. X    {
  1219. X      slide[w++] = (byte)t->v.n;
  1220. X      if (w == WSIZE)
  1221. X      {
  1222. X        flush(w);
  1223. X        w = 0;
  1224. X      }
  1225. X    }
  1226. X    else                        /* it's an EOB or a length */
  1227. X    {
  1228. X      /* exit if end of block */
  1229. X      if (e == 15)
  1230. X        break;
  1231. X
  1232. X      /* get length of block to copy */
  1233. X      NEEDBITS(e)
  1234. X      n = t->v.n + ((unsigned)b & mask_bits[e]);
  1235. X      DUMPBITS(e);
  1236. X
  1237. X      /* decode distance of block to copy */
  1238. X      NEEDBITS((unsigned)bd)
  1239. X      if ((e = (t = td + ((unsigned)b & md))->e) > 16)
  1240. X        do {
  1241. X          if (e == 99)
  1242. X            return 1;
  1243. X          DUMPBITS(t->b)
  1244. X          e -= 16;
  1245. X          NEEDBITS(e)
  1246. X        } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
  1247. X      DUMPBITS(t->b)
  1248. X      NEEDBITS(e)
  1249. X      d = w - t->v.n - ((unsigned)b & mask_bits[e]);
  1250. X      DUMPBITS(e)
  1251. X
  1252. X      /* do the copy */
  1253. X      do {
  1254. X        n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
  1255. X#ifndef NOMEMCPY
  1256. X        if (w - d >= e)         /* (this test assumes unsigned comparison) */
  1257. X        {
  1258. X          memcpy(slide + w, slide + d, e);
  1259. X          w += e;
  1260. X          d += e;
  1261. X        }
  1262. X        else                      /* do it slow to avoid memcpy() overlap */
  1263. X#endif /* !NOMEMCPY */
  1264. X          do {
  1265. X            slide[w++] = slide[d++];
  1266. X          } while (--e);
  1267. X        if (w == WSIZE)
  1268. X        {
  1269. X          flush(w);
  1270. X          w = 0;
  1271. X        }
  1272. X      } while (n);
  1273. X    }
  1274. X  }
  1275. X
  1276. X
  1277. X  /* restore the globals from the locals */
  1278. X  wp = w;                       /* restore global window pointer */
  1279. X  bb = b;                       /* restore global bit buffer */
  1280. X  bk = k;
  1281. X
  1282. X
  1283. X  /* done */
  1284. X  return 0;
  1285. X}
  1286. X
  1287. X
  1288. X
  1289. Xint inflate_stored()
  1290. X/* "decompress" an inflated type 0 (stored) block. */
  1291. X{
  1292. X  unsigned n;           /* number of bytes in block */
  1293. X  unsigned w;           /* current window position */
  1294. X  register ULONG b;     /* bit buffer */
  1295. X  register unsigned k;  /* number of bits in bit buffer */
  1296. X
  1297. X
  1298. X  /* make local copies of globals */
  1299. X  b = bb;                       /* initialize bit buffer */
  1300. X  k = bk;
  1301. X  w = wp;                       /* initialize window position */
  1302. X
  1303. X
  1304. X  /* go to byte boundary */
  1305. X  n = k & 7;
  1306. X  DUMPBITS(n);
  1307. X
  1308. X
  1309. X  /* get the length and its complement */
  1310. X  NEEDBITS(16)
  1311. X  n = ((unsigned)b & 0xffff);
  1312. X  DUMPBITS(16)
  1313. X  NEEDBITS(16)
  1314. X  if (n != (unsigned)((~b) & 0xffff))
  1315. X    return 1;                   /* error in compressed data */
  1316. X  DUMPBITS(16)
  1317. X
  1318. X
  1319. X  /* read and output the compressed data */
  1320. X  while (n--)
  1321. X  {
  1322. X    NEEDBITS(8)
  1323. X    slide[w++] = (byte)b;
  1324. X    if (w == WSIZE)
  1325. X    {
  1326. X      flush(w);
  1327. X      w = 0;
  1328. X    }
  1329. X    DUMPBITS(8)
  1330. X  }
  1331. X
  1332. X
  1333. X  /* restore the globals from the locals */
  1334. X  wp = w;                       /* restore global window pointer */
  1335. X  bb = b;                       /* restore global bit buffer */
  1336. X  bk = k;
  1337. X  return 0;
  1338. X}
  1339. X
  1340. X
  1341. X
  1342. Xint inflate_fixed()
  1343. X/* decompress an inflated type 1 (fixed Huffman codes) block.  We should
  1344. X   either replace this with a custom decoder, or at least precompute the
  1345. X   Huffman tables. */
  1346. X{
  1347. X  int i;                /* temporary variable */
  1348. X  struct huft *tl;      /* literal/length code table */
  1349. X  struct huft *td;      /* distance code table */
  1350. X  int bl;               /* lookup bits for tl */
  1351. X  int bd;               /* lookup bits for td */
  1352. X  unsigned l[288];      /* length list for huft_build */
  1353. X
  1354. X
  1355. X  /* set up literal table */
  1356. X  for (i = 0; i < 144; i++)
  1357. X    l[i] = 8;
  1358. X  for (; i < 256; i++)
  1359. X    l[i] = 9;
  1360. X  for (; i < 280; i++)
  1361. X    l[i] = 7;
  1362. X  for (; i < 288; i++)          /* make a complete, but wrong code set */
  1363. X    l[i] = 8;
  1364. X  bl = 7;
  1365. X  if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0)
  1366. X    return i;
  1367. X
  1368. X
  1369. X  /* set up distance table */
  1370. X  for (i = 0; i < 30; i++)      /* make an incomplete code set */
  1371. X    l[i] = 5;
  1372. X  bd = 5;
  1373. X  if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
  1374. X  {
  1375. X    huft_free(tl);
  1376. X    return i;
  1377. X  }
  1378. X
  1379. X
  1380. X  /* decompress until an end-of-block code */
  1381. X  if (inflate_codes(tl, td, bl, bd))
  1382. X    return 1;
  1383. X
  1384. X
  1385. X  /* free the decoding tables, return */
  1386. X  huft_free(tl);
  1387. X  huft_free(td);
  1388. X  return 0;
  1389. X}
  1390. X
  1391. X
  1392. X
  1393. Xint inflate_dynamic()
  1394. X/* decompress an inflated type 2 (dynamic Huffman codes) block. */
  1395. X{
  1396. X  int i;                /* temporary variables */
  1397. X  unsigned j;
  1398. X  unsigned l;           /* last length */
  1399. X  unsigned m;           /* mask for bit lengths table */
  1400. X  unsigned n;           /* number of lengths to get */
  1401. X  struct huft *tl;      /* literal/length code table */
  1402. X  struct huft *td;      /* distance code table */
  1403. X  int bl;               /* lookup bits for tl */
  1404. X  int bd;               /* lookup bits for td */
  1405. X  unsigned nb;          /* number of bit length codes */
  1406. X  unsigned nl;          /* number of literal/length codes */
  1407. X  unsigned nd;          /* number of distance codes */
  1408. X  unsigned ll[286+30];  /* literal/length and distance code lengths */
  1409. X  register ULONG b;     /* bit buffer */
  1410. X  register unsigned k;  /* number of bits in bit buffer */
  1411. X
  1412. X
  1413. X  /* make local bit buffer */
  1414. X  b = bb;
  1415. X  k = bk;
  1416. X
  1417. X
  1418. X  /* read in table lengths */
  1419. X  NEEDBITS(5)
  1420. X  nl = 257 + ((unsigned)b & 0x1f);      /* number of literal/length codes */
  1421. X  DUMPBITS(5)
  1422. X  NEEDBITS(5)
  1423. X  nd = 1 + ((unsigned)b & 0x1f);        /* number of distance codes */
  1424. X  DUMPBITS(5)
  1425. X  NEEDBITS(4)
  1426. X  nb = 4 + ((unsigned)b & 0xf);         /* number of bit length codes */
  1427. X  DUMPBITS(4)
  1428. X  if (nl > 286 || nd > 30)
  1429. X    return 1;                   /* bad lengths */
  1430. X
  1431. X
  1432. X  /* read in bit-length-code lengths */
  1433. X  for (j = 0; j < nb; j++)
  1434. X  {
  1435. X    NEEDBITS(3)
  1436. X    ll[border[j]] = (unsigned)b & 7;
  1437. X    DUMPBITS(3)
  1438. X  }
  1439. X  for (; j < 19; j++)
  1440. X    ll[border[j]] = 0;
  1441. X
  1442. X
  1443. X  /* build decoding table for trees--single level, 7 bit lookup */
  1444. X  bl = 7;
  1445. X  if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
  1446. X  {
  1447. X    if (i == 1)
  1448. X      huft_free(tl);
  1449. X    return i;                   /* incomplete code set */
  1450. X  }
  1451. X
  1452. X
  1453. X  /* read in literal and distance code lengths */
  1454. X  n = nl + nd;
  1455. X  m = mask_bits[bl];
  1456. X  i = l = 0;
  1457. X  while ((unsigned)i < n)
  1458. X  {
  1459. X    NEEDBITS((unsigned)bl)
  1460. X    j = (td = tl + ((unsigned)b & m))->b;
  1461. X    DUMPBITS(j)
  1462. X    j = td->v.n;
  1463. X    if (j < 16)                 /* length of code in bits (0..15) */
  1464. X      ll[i++] = l = j;          /* save last length in l */
  1465. X    else if (j == 16)           /* repeat last length 3 to 6 times */
  1466. X    {
  1467. X      NEEDBITS(2)
  1468. X      j = 3 + ((unsigned)b & 3);
  1469. X      DUMPBITS(2)
  1470. X      if ((unsigned)i + j > n)
  1471. X        return 1;
  1472. X      while (j--)
  1473. X        ll[i++] = l;
  1474. X    }
  1475. X    else if (j == 17)           /* 3 to 10 zero length codes */
  1476. X    {
  1477. X      NEEDBITS(3)
  1478. X      j = 3 + ((unsigned)b & 7);
  1479. X      DUMPBITS(3)
  1480. X      if ((unsigned)i + j > n)
  1481. X        return 1;
  1482. X      while (j--)
  1483. X        ll[i++] = 0;
  1484. X      l = 0;
  1485. X    }
  1486. X    else                        /* j == 18: 11 to 138 zero length codes */
  1487. X    {
  1488. X      NEEDBITS(7)
  1489. X      j = 11 + ((unsigned)b & 0x7f);
  1490. X      DUMPBITS(7)
  1491. X      if ((unsigned)i + j > n)
  1492. X        return 1;
  1493. X      while (j--)
  1494. X        ll[i++] = 0;
  1495. X      l = 0;
  1496. X    }
  1497. X  }
  1498. X
  1499. X
  1500. X  /* free decoding table for trees */
  1501. X  huft_free(tl);
  1502. X
  1503. X
  1504. X  /* restore the global bit buffer */
  1505. X  bb = b;
  1506. X  bk = k;
  1507. X
  1508. X
  1509. X  /* build the decoding tables for literal/length and distance codes */
  1510. X  bl = lbits;
  1511. X  if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
  1512. X  {
  1513. X    if (i == 1)
  1514. X      huft_free(tl);
  1515. X    return i;                   /* incomplete code set */
  1516. X  }
  1517. X  bd = dbits;
  1518. X  if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
  1519. X  {
  1520. X    if (i == 1)
  1521. X      huft_free(td);
  1522. X    huft_free(tl);
  1523. X    return i;                   /* incomplete code set */
  1524. X  }
  1525. X
  1526. X
  1527. X  /* decompress until an end-of-block code */
  1528. X  if (inflate_codes(tl, td, bl, bd))
  1529. X    return 1;
  1530. X
  1531. X
  1532. X  /* free the decoding tables, return */
  1533. X  huft_free(tl);
  1534. X  huft_free(td);
  1535. X  return 0;
  1536. X}
  1537. X
  1538. X
  1539. X
  1540. Xint inflate_block(e)
  1541. Xint *e;                 /* last block flag */
  1542. X/* decompress an inflated block */
  1543. X{
  1544. X  unsigned t;           /* block type */
  1545. X  register ULONG b;     /* bit buffer */
  1546. X  register unsigned k;  /* number of bits in bit buffer */
  1547. X
  1548. X
  1549. X  /* make local bit buffer */
  1550. X  b = bb;
  1551. X  k = bk;
  1552. X
  1553. X
  1554. X  /* read in last block bit */
  1555. X  NEEDBITS(1)
  1556. X  *e = (int)b & 1;
  1557. X  DUMPBITS(1)
  1558. X
  1559. X
  1560. X  /* read in block type */
  1561. X  NEEDBITS(2)
  1562. X  t = (unsigned)b & 3;
  1563. X  DUMPBITS(2)
  1564. X
  1565. X
  1566. X  /* restore the global bit buffer */
  1567. X  bb = b;
  1568. X  bk = k;
  1569. X
  1570. X
  1571. X  /* inflate that block type */
  1572. X  if (t == 2)
  1573. X    return inflate_dynamic();
  1574. X  if (t == 0)
  1575. X    return inflate_stored();
  1576. X  if (t == 1)
  1577. X    return inflate_fixed();
  1578. X
  1579. X
  1580. X  /* bad block type */
  1581. X  return 2;
  1582. X}
  1583. X
  1584. X
  1585. X
  1586. Xint inflate_entry()
  1587. X/* decompress an inflated entry */
  1588. X{
  1589. X  int e;                /* last block flag */
  1590. X  int r;                /* result code */
  1591. X  unsigned h;           /* maximum struct huft's malloc'ed */
  1592. X
  1593. X
  1594. X  /* initialize window, bit buffer */
  1595. X  wp = 0;
  1596. X  bk = 0;
  1597. X  bb = 0;
  1598. X
  1599. X
  1600. X  /* decompress until the last block */
  1601. X  h = 0;
  1602. X  do {
  1603. X    hufts = 0;
  1604. X    if ((r = inflate_block(&e)) != 0)
  1605. X      return r;
  1606. X    if (hufts > h)
  1607. X      h = hufts;
  1608. X  } while (!e);
  1609. X
  1610. X
  1611. X  /* flush out slide */
  1612. X  flush(wp);
  1613. X
  1614. X
  1615. X  /* return success */
  1616. X#ifdef DEBUG
  1617. X  fprintf(stderr, "<%u> ", h);
  1618. X#endif /* DEBUG */
  1619. X  return 0;
  1620. X}
  1621. X
  1622. X
  1623. Xvoid inflate()
  1624. X/* ignore the return code for now ... */
  1625. X{
  1626. X  inflate_entry();
  1627. X}
  1628. END_OF_FILE
  1629.   if test 32972 -ne `wc -c <'inflate.c'`; then
  1630.     echo shar: \"'inflate.c'\" unpacked with wrong size!
  1631.   fi
  1632.   # end of 'inflate.c'
  1633. fi
  1634. echo shar: End of archive 4 \(of 14\).
  1635. cp /dev/null ark4isdone
  1636. MISSING=""
  1637. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ; do
  1638.     if test ! -f ark${I}isdone ; then
  1639.     MISSING="${MISSING} ${I}"
  1640.     fi
  1641. done
  1642. if test "${MISSING}" = "" ; then
  1643.     echo You have unpacked all 14 archives.
  1644.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1645. else
  1646.     echo You still must unpack the following archives:
  1647.     echo "        " ${MISSING}
  1648. fi
  1649. exit 0
  1650. exit 0 # Just in case...
  1651.