home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume44
/
unzip
/
part09
< prev
next >
Wrap
Internet Message Format
|
1994-09-19
|
71KB
From: zip-bugs@wkuvx1.wku.edu (Info-ZIP group)
Newsgroups: comp.sources.misc
Subject: v44i074: unzip - Info-ZIP portable UnZip, version 5.12, Part09/20
Date: 18 Sep 1994 23:15:32 -0500
Organization: Sterling Software
Sender: kent@sparky.sterling.com
Approved: kent@sparky.sterling.com
Message-ID: <35j394$qoc@sparky.sterling.com>
X-Md4-Signature: 2460b5521dcd2142b00bedcf80f343dc
Submitted-by: zip-bugs@wkuvx1.wku.edu (Info-ZIP group)
Posting-number: Volume 44, Issue 74
Archive-name: unzip/part09
Environment: UNIX, VMS, OS/2, MS-DOS, MACINTOSH, WIN-NT, LINUX, MINIX, COHERENT, AMIGA?, ATARI TOS, SGI, DEC, Cray, Convex, Amdahl, Sun
Supersedes: unzip50: Volume 31, Issue 104-117
#! /bin/sh
# This is a shell archive. Remove anything before this line, then feed it
# into a shell via "sh file" or similar. To overwrite existing files,
# type "sh file -c".
# Contents: unzip-5.12/explode.c unzip-5.12/inflate.c
# Wrapped by kent@sparky on Sat Sep 17 23:33:40 1994
PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin:$PATH ; export PATH
echo If this archive is complete, you will see the following message:
echo ' "shar: End of archive 9 (of 20)."'
if test -f 'unzip-5.12/explode.c' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'unzip-5.12/explode.c'\"
else
echo shar: Extracting \"'unzip-5.12/explode.c'\" \(28523 characters\)
sed "s/^X//" >'unzip-5.12/explode.c' <<'END_OF_FILE'
X/* explode.c -- put in the public domain by Mark Adler
X version c13, 25 August 1994 */
X
X
X/* You can do whatever you like with this source file, though I would
X prefer that if you modify it and redistribute it that you include
X comments to that effect with your name and the date. Thank you.
X
X History:
X vers date who what
X ---- --------- -------------- ------------------------------------
X c1 30 Mar 92 M. Adler explode that uses huft_build from inflate
X (this gives over a 70% speed improvement
X over the original unimplode.c, which
X decoded a bit at a time)
X c2 4 Apr 92 M. Adler fixed bug for file sizes a multiple of 32k.
X c3 10 Apr 92 M. Adler added a little memory tracking if DEBUG
X c4 11 Apr 92 M. Adler added NOMEMCPY do kill use of memcpy()
X c5 21 Apr 92 M. Adler added the WSIZE #define to allow reducing
X the 32K window size for specialized
X applications.
X c6 31 May 92 M. Adler added typecasts to eliminate some warnings
X c7 27 Jun 92 G. Roelofs added more typecasts.
X c8 17 Oct 92 G. Roelofs changed ULONG/UWORD/byte to ulg/ush/uch.
X c9 19 Jul 93 J. Bush added more typecasts (to return values);
X made l[256] array static for Amiga.
X c10 8 Oct 93 G. Roelofs added used_csize for diagnostics; added
X buf and unshrink arguments to flush();
X undef'd various macros at end for Turbo C;
X removed NEXTBYTE macro (now in unzip.h)
X and bytebuf variable (not used); changed
X memset() to memzero().
X c11 9 Jan 94 M. Adler fixed incorrect used_csize calculation.
X c12 9 Apr 94 G. Roelofs fixed split comments on preprocessor lines
X to avoid bug in Encore compiler.
X c13 25 Aug 94 M. Adler fixed distance-length comment (orig c9 fix)
X */
X
X
X/*
X Explode imploded (PKZIP method 6 compressed) data. This compression
X method searches for as much of the current string of bytes (up to a length
X of ~320) in the previous 4K or 8K bytes. If it doesn't find any matches
X (of at least length 2 or 3), it codes the next byte. Otherwise, it codes
X the length of the matched string and its distance backwards from the
X current position. Single bytes ("literals") are preceded by a one (a
X single bit) and are either uncoded (the eight bits go directly into the
X compressed stream for a total of nine bits) or Huffman coded with a
X supplied literal code tree. If literals are coded, then the minimum match
X length is three, otherwise it is two.
X
X There are therefore four kinds of imploded streams: 8K search with coded
X literals (min match = 3), 4K search with coded literals (min match = 3),
X 8K with uncoded literals (min match = 2), and 4K with uncoded literals
X (min match = 2). The kind of stream is identified in two bits of a
X general purpose bit flag that is outside of the compressed stream.
X
X Distance-length pairs for matched strings are preceded by a zero bit (to
X distinguish them from literals) and are always coded. The distance comes
X first and is either the low six (4K) or low seven (8K) bits of the
X distance (uncoded), followed by the high six bits of the distance coded.
X Then the length is six bits coded (0..63 + min match length), and if the
X maximum such length is coded, then it's followed by another eight bits
X (uncoded) to be added to the coded length. This gives a match length
X range of 2..320 or 3..321 bytes.
X
X The literal, length, and distance codes are all represented in a slightly
X compressed form themselves. What is sent are the lengths of the codes for
X each value, which is sufficient to construct the codes. Each byte of the
X code representation is the code length (the low four bits representing
X 1..16), and the number of values sequentially with that length (the high
X four bits also representing 1..16). There are 256 literal code values (if
X literals are coded), 64 length code values, and 64 distance code values,
X in that order at the beginning of the compressed stream. Each set of code
X values is preceded (redundantly) with a byte indicating how many bytes are
X in the code description that follows, in the range 1..256.
X
X The codes themselves are decoded using tables made by huft_build() from
X the bit lengths. That routine and its comments are in the inflate.c
X module.
X */
X
X#include "unzip.h" /* must supply slide[] (uch) array and NEXTBYTE macro */
X
X#ifndef WSIZE
X# define WSIZE 0x8000 /* window size--must be a power of two, and */
X#endif /* at least 8K for zip's implode method */
X
X
Xstruct huft {
X uch e; /* number of extra bits or operation */
X uch b; /* number of bits in this code or subcode */
X union {
X ush n; /* literal, length base, or distance base */
X struct huft *t; /* pointer to next level of table */
X } v;
X};
X
X/* Function prototypes */
X/* routines from inflate.c */
Xextern unsigned hufts;
Xint huft_build OF((unsigned *, unsigned, unsigned, ush *, ush *,
X struct huft **, int *));
Xint huft_free OF((struct huft *));
X
X/* routines here */
Xint get_tree OF((unsigned *, unsigned));
Xint explode_lit8 OF((struct huft *, struct huft *, struct huft *,
X int, int, int));
Xint explode_lit4 OF((struct huft *, struct huft *, struct huft *,
X int, int, int));
Xint explode_nolit8 OF((struct huft *, struct huft *, int, int));
Xint explode_nolit4 OF((struct huft *, struct huft *, int, int));
Xint explode OF((void));
X
X
X/* The implode algorithm uses a sliding 4K or 8K byte window on the
X uncompressed stream to find repeated byte strings. This is implemented
X here as a circular buffer. The index is updated simply by incrementing
X and then and'ing with 0x0fff (4K-1) or 0x1fff (8K-1). Here, the 32K
X buffer of inflate is used, and it works just as well to always have
X a 32K circular buffer, so the index is anded with 0x7fff. This is
X done to allow the window to also be used as the output buffer. */
X/* This must be supplied in an external module useable like "uch slide[8192];"
X or "uch *slide;", where the latter would be malloc'ed. In unzip, slide[]
X is actually a 32K area for use by inflate, which uses a 32K sliding window.
X */
X
X
X/* Tables for length and distance */
Xush cplen2[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
X 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
X 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
X 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65};
Xush cplen3[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
X 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
X 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
X 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66};
Xush extra[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
X 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
X 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
X 8};
Xush cpdist4[] = {1, 65, 129, 193, 257, 321, 385, 449, 513, 577, 641, 705,
X 769, 833, 897, 961, 1025, 1089, 1153, 1217, 1281, 1345, 1409, 1473,
X 1537, 1601, 1665, 1729, 1793, 1857, 1921, 1985, 2049, 2113, 2177,
X 2241, 2305, 2369, 2433, 2497, 2561, 2625, 2689, 2753, 2817, 2881,
X 2945, 3009, 3073, 3137, 3201, 3265, 3329, 3393, 3457, 3521, 3585,
X 3649, 3713, 3777, 3841, 3905, 3969, 4033};
Xush cpdist8[] = {1, 129, 257, 385, 513, 641, 769, 897, 1025, 1153, 1281,
X 1409, 1537, 1665, 1793, 1921, 2049, 2177, 2305, 2433, 2561, 2689,
X 2817, 2945, 3073, 3201, 3329, 3457, 3585, 3713, 3841, 3969, 4097,
X 4225, 4353, 4481, 4609, 4737, 4865, 4993, 5121, 5249, 5377, 5505,
X 5633, 5761, 5889, 6017, 6145, 6273, 6401, 6529, 6657, 6785, 6913,
X 7041, 7169, 7297, 7425, 7553, 7681, 7809, 7937, 8065};
X
X
X/* Macros for inflate() bit peeking and grabbing.
X The usage is:
X
X NEEDBITS(j)
X x = b & mask_bits[j];
X DUMPBITS(j)
X
X where NEEDBITS makes sure that b has at least j bits in it, and
X DUMPBITS removes the bits from b. The macros use the variable k
X for the number of bits in b. Normally, b and k are register
X variables for speed.
X */
X
X#define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE)<<k;k+=8;}}
X#define DUMPBITS(n) {b>>=(n);k-=(n);}
X
X
X
Xint get_tree(l, n)
Xunsigned *l; /* bit lengths */
Xunsigned n; /* number expected */
X/* Get the bit lengths for a code representation from the compressed
X stream. If get_tree() returns 4, then there is an error in the data.
X Otherwise zero is returned. */
X{
X unsigned i; /* bytes remaining in list */
X unsigned k; /* lengths entered */
X unsigned j; /* number of codes */
X unsigned b; /* bit length for those codes */
X
X
X /* get bit lengths */
X i = NEXTBYTE + 1; /* length/count pairs to read */
X k = 0; /* next code */
X do {
X b = ((j = NEXTBYTE) & 0xf) + 1; /* bits in code (1..16) */
X j = ((j & 0xf0) >> 4) + 1; /* codes with those bits (1..16) */
X if (k + j > n)
X return 4; /* don't overflow l[] */
X do {
X l[k++] = b;
X } while (--j);
X } while (--i);
X return k != n ? 4 : 0; /* should have read n of them */
X}
X
X
X
Xint explode_lit8(tb, tl, td, bb, bl, bd)
Xstruct huft *tb, *tl, *td; /* literal, length, and distance tables */
Xint bb, bl, bd; /* number of bits decoded by those */
X/* Decompress the imploded data using coded literals and an 8K sliding
X window. */
X{
X long s; /* bytes to decompress */
X register unsigned e; /* table entry flag/number of extra bits */
X unsigned n, d; /* length and index for copy */
X unsigned w; /* current window position */
X struct huft *t; /* pointer to table entry */
X unsigned mb, ml, md; /* masks for bb, bl, and bd bits */
X register ulg b; /* bit buffer */
X register unsigned k; /* number of bits in bit buffer */
X unsigned u; /* true if unflushed */
X
X
X /* explode the coded data */
X b = k = w = 0; /* initialize bit buffer, window */
X u = 1; /* buffer unflushed */
X mb = mask_bits[bb]; /* precompute masks for speed */
X ml = mask_bits[bl];
X md = mask_bits[bd];
X s = ucsize;
X while (s > 0) /* do until ucsize bytes uncompressed */
X {
X NEEDBITS(1)
X if (b & 1) /* then literal--decode it */
X {
X DUMPBITS(1)
X s--;
X NEEDBITS((unsigned)bb) /* get coded literal */
X if ((e = (t = tb + ((~(unsigned)b) & mb))->e) > 16)
X do {
X if (e == 99)
X return 1;
X DUMPBITS(t->b)
X e -= 16;
X NEEDBITS(e)
X } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
X DUMPBITS(t->b)
X slide[w++] = (uch)t->v.n;
X if (w == WSIZE)
X {
X flush(slide, w, 0);
X w = u = 0;
X }
X }
X else /* else distance/length */
X {
X DUMPBITS(1)
X NEEDBITS(7) /* get distance low bits */
X d = (unsigned)b & 0x7f;
X DUMPBITS(7)
X NEEDBITS((unsigned)bd) /* get coded distance high bits */
X if ((e = (t = td + ((~(unsigned)b) & md))->e) > 16)
X do {
X if (e == 99)
X return 1;
X DUMPBITS(t->b)
X e -= 16;
X NEEDBITS(e)
X } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
X DUMPBITS(t->b)
X d = w - d - t->v.n; /* construct offset */
X NEEDBITS((unsigned)bl) /* get coded length */
X if ((e = (t = tl + ((~(unsigned)b) & ml))->e) > 16)
X do {
X if (e == 99)
X return 1;
X DUMPBITS(t->b)
X e -= 16;
X NEEDBITS(e)
X } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
X DUMPBITS(t->b)
X n = t->v.n;
X if (e) /* get length extra bits */
X {
X NEEDBITS(8)
X n += (unsigned)b & 0xff;
X DUMPBITS(8)
X }
X
X /* do the copy */
X s -= n;
X do {
X n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
X if (u && w <= d)
X {
X memzero(slide + w, e);
X w += e;
X d += e;
X }
X else
X#ifndef NOMEMCPY
X if (w - d >= e) /* (this test assumes unsigned comparison) */
X {
X memcpy(slide + w, slide + d, e);
X w += e;
X d += e;
X }
X else /* do it slow to avoid memcpy() overlap */
X#endif /* !NOMEMCPY */
X do {
X slide[w++] = slide[d++];
X } while (--e);
X if (w == WSIZE)
X {
X flush(slide, w, 0);
X w = u = 0;
X }
X } while (n);
X }
X }
X
X /* flush out slide */
X flush(slide, w, 0);
X if (csize + (k >> 3)) /* should have read csize bytes, but sometimes */
X { /* read one too many: k>>3 compensates */
X used_csize = lrec.csize - csize - (k >> 3);
X return 5;
X }
X return 0;
X}
X
X
X
Xint explode_lit4(tb, tl, td, bb, bl, bd)
Xstruct huft *tb, *tl, *td; /* literal, length, and distance tables */
Xint bb, bl, bd; /* number of bits decoded by those */
X/* Decompress the imploded data using coded literals and a 4K sliding
X window. */
X{
X long s; /* bytes to decompress */
X register unsigned e; /* table entry flag/number of extra bits */
X unsigned n, d; /* length and index for copy */
X unsigned w; /* current window position */
X struct huft *t; /* pointer to table entry */
X unsigned mb, ml, md; /* masks for bb, bl, and bd bits */
X register ulg b; /* bit buffer */
X register unsigned k; /* number of bits in bit buffer */
X unsigned u; /* true if unflushed */
X
X
X /* explode the coded data */
X b = k = w = 0; /* initialize bit buffer, window */
X u = 1; /* buffer unflushed */
X mb = mask_bits[bb]; /* precompute masks for speed */
X ml = mask_bits[bl];
X md = mask_bits[bd];
X s = ucsize;
X while (s > 0) /* do until ucsize bytes uncompressed */
X {
X NEEDBITS(1)
X if (b & 1) /* then literal--decode it */
X {
X DUMPBITS(1)
X s--;
X NEEDBITS((unsigned)bb) /* get coded literal */
X if ((e = (t = tb + ((~(unsigned)b) & mb))->e) > 16)
X do {
X if (e == 99)
X return 1;
X DUMPBITS(t->b)
X e -= 16;
X NEEDBITS(e)
X } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
X DUMPBITS(t->b)
X slide[w++] = (uch)t->v.n;
X if (w == WSIZE)
X {
X flush(slide, w, 0);
X w = u = 0;
X }
X }
X else /* else distance/length */
X {
X DUMPBITS(1)
X NEEDBITS(6) /* get distance low bits */
X d = (unsigned)b & 0x3f;
X DUMPBITS(6)
X NEEDBITS((unsigned)bd) /* get coded distance high bits */
X if ((e = (t = td + ((~(unsigned)b) & md))->e) > 16)
X do {
X if (e == 99)
X return 1;
X DUMPBITS(t->b)
X e -= 16;
X NEEDBITS(e)
X } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
X DUMPBITS(t->b)
X d = w - d - t->v.n; /* construct offset */
X NEEDBITS((unsigned)bl) /* get coded length */
X if ((e = (t = tl + ((~(unsigned)b) & ml))->e) > 16)
X do {
X if (e == 99)
X return 1;
X DUMPBITS(t->b)
X e -= 16;
X NEEDBITS(e)
X } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
X DUMPBITS(t->b)
X n = t->v.n;
X if (e) /* get length extra bits */
X {
X NEEDBITS(8)
X n += (unsigned)b & 0xff;
X DUMPBITS(8)
X }
X
X /* do the copy */
X s -= n;
X do {
X n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
X if (u && w <= d)
X {
X memzero(slide + w, e);
X w += e;
X d += e;
X }
X else
X#ifndef NOMEMCPY
X if (w - d >= e) /* (this test assumes unsigned comparison) */
X {
X memcpy(slide + w, slide + d, e);
X w += e;
X d += e;
X }
X else /* do it slow to avoid memcpy() overlap */
X#endif /* !NOMEMCPY */
X do {
X slide[w++] = slide[d++];
X } while (--e);
X if (w == WSIZE)
X {
X flush(slide, w, 0);
X w = u = 0;
X }
X } while (n);
X }
X }
X
X /* flush out slide */
X flush(slide, w, 0);
X if (csize + (k >> 3)) /* should have read csize bytes, but sometimes */
X { /* read one too many: k>>3 compensates */
X used_csize = lrec.csize - csize - (k >> 3);
X return 5;
X }
X return 0;
X}
X
X
X
Xint explode_nolit8(tl, td, bl, bd)
Xstruct huft *tl, *td; /* length and distance decoder tables */
Xint bl, bd; /* number of bits decoded by tl[] and td[] */
X/* Decompress the imploded data using uncoded literals and an 8K sliding
X window. */
X{
X long s; /* bytes to decompress */
X register unsigned e; /* table entry flag/number of extra bits */
X unsigned n, d; /* length and index for copy */
X unsigned w; /* current window position */
X struct huft *t; /* pointer to table entry */
X unsigned ml, md; /* masks for bl and bd bits */
X register ulg b; /* bit buffer */
X register unsigned k; /* number of bits in bit buffer */
X unsigned u; /* true if unflushed */
X
X
X /* explode the coded data */
X b = k = w = 0; /* initialize bit buffer, window */
X u = 1; /* buffer unflushed */
X ml = mask_bits[bl]; /* precompute masks for speed */
X md = mask_bits[bd];
X s = ucsize;
X while (s > 0) /* do until ucsize bytes uncompressed */
X {
X NEEDBITS(1)
X if (b & 1) /* then literal--get eight bits */
X {
X DUMPBITS(1)
X s--;
X NEEDBITS(8)
X slide[w++] = (uch)b;
X if (w == WSIZE)
X {
X flush(slide, w, 0);
X w = u = 0;
X }
X DUMPBITS(8)
X }
X else /* else distance/length */
X {
X DUMPBITS(1)
X NEEDBITS(7) /* get distance low bits */
X d = (unsigned)b & 0x7f;
X DUMPBITS(7)
X NEEDBITS((unsigned)bd) /* get coded distance high bits */
X if ((e = (t = td + ((~(unsigned)b) & md))->e) > 16)
X do {
X if (e == 99)
X return 1;
X DUMPBITS(t->b)
X e -= 16;
X NEEDBITS(e)
X } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
X DUMPBITS(t->b)
X d = w - d - t->v.n; /* construct offset */
X NEEDBITS((unsigned)bl) /* get coded length */
X if ((e = (t = tl + ((~(unsigned)b) & ml))->e) > 16)
X do {
X if (e == 99)
X return 1;
X DUMPBITS(t->b)
X e -= 16;
X NEEDBITS(e)
X } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
X DUMPBITS(t->b)
X n = t->v.n;
X if (e) /* get length extra bits */
X {
X NEEDBITS(8)
X n += (unsigned)b & 0xff;
X DUMPBITS(8)
X }
X
X /* do the copy */
X s -= n;
X do {
X n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
X if (u && w <= d)
X {
X memzero(slide + w, e);
X w += e;
X d += e;
X }
X else
X#ifndef NOMEMCPY
X if (w - d >= e) /* (this test assumes unsigned comparison) */
X {
X memcpy(slide + w, slide + d, e);
X w += e;
X d += e;
X }
X else /* do it slow to avoid memcpy() overlap */
X#endif /* !NOMEMCPY */
X do {
X slide[w++] = slide[d++];
X } while (--e);
X if (w == WSIZE)
X {
X flush(slide, w, 0);
X w = u = 0;
X }
X } while (n);
X }
X }
X
X /* flush out slide */
X flush(slide, w, 0);
X if (csize + (k >> 3)) /* should have read csize bytes, but sometimes */
X { /* read one too many: k>>3 compensates */
X used_csize = lrec.csize - csize - (k >> 3);
X return 5;
X }
X return 0;
X}
X
X
X
Xint explode_nolit4(tl, td, bl, bd)
Xstruct huft *tl, *td; /* length and distance decoder tables */
Xint bl, bd; /* number of bits decoded by tl[] and td[] */
X/* Decompress the imploded data using uncoded literals and a 4K sliding
X window. */
X{
X long s; /* bytes to decompress */
X register unsigned e; /* table entry flag/number of extra bits */
X unsigned n, d; /* length and index for copy */
X unsigned w; /* current window position */
X struct huft *t; /* pointer to table entry */
X unsigned ml, md; /* masks for bl and bd bits */
X register ulg b; /* bit buffer */
X register unsigned k; /* number of bits in bit buffer */
X unsigned u; /* true if unflushed */
X
X
X /* explode the coded data */
X b = k = w = 0; /* initialize bit buffer, window */
X u = 1; /* buffer unflushed */
X ml = mask_bits[bl]; /* precompute masks for speed */
X md = mask_bits[bd];
X s = ucsize;
X while (s > 0) /* do until ucsize bytes uncompressed */
X {
X NEEDBITS(1)
X if (b & 1) /* then literal--get eight bits */
X {
X DUMPBITS(1)
X s--;
X NEEDBITS(8)
X slide[w++] = (uch)b;
X if (w == WSIZE)
X {
X flush(slide, w, 0);
X w = u = 0;
X }
X DUMPBITS(8)
X }
X else /* else distance/length */
X {
X DUMPBITS(1)
X NEEDBITS(6) /* get distance low bits */
X d = (unsigned)b & 0x3f;
X DUMPBITS(6)
X NEEDBITS((unsigned)bd) /* get coded distance high bits */
X if ((e = (t = td + ((~(unsigned)b) & md))->e) > 16)
X do {
X if (e == 99)
X return 1;
X DUMPBITS(t->b)
X e -= 16;
X NEEDBITS(e)
X } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
X DUMPBITS(t->b)
X d = w - d - t->v.n; /* construct offset */
X NEEDBITS((unsigned)bl) /* get coded length */
X if ((e = (t = tl + ((~(unsigned)b) & ml))->e) > 16)
X do {
X if (e == 99)
X return 1;
X DUMPBITS(t->b)
X e -= 16;
X NEEDBITS(e)
X } while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16);
X DUMPBITS(t->b)
X n = t->v.n;
X if (e) /* get length extra bits */
X {
X NEEDBITS(8)
X n += (unsigned)b & 0xff;
X DUMPBITS(8)
X }
X
X /* do the copy */
X s -= n;
X do {
X n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
X if (u && w <= d)
X {
X memzero(slide + w, e);
X w += e;
X d += e;
X }
X else
X#ifndef NOMEMCPY
X if (w - d >= e) /* (this test assumes unsigned comparison) */
X {
X memcpy(slide + w, slide + d, e);
X w += e;
X d += e;
X }
X else /* do it slow to avoid memcpy() overlap */
X#endif /* !NOMEMCPY */
X do {
X slide[w++] = slide[d++];
X } while (--e);
X if (w == WSIZE)
X {
X flush(slide, w, 0);
X w = u = 0;
X }
X } while (n);
X }
X }
X
X /* flush out slide */
X flush(slide, w, 0);
X if (csize + (k >> 3)) /* should have read csize bytes, but sometimes */
X { /* read one too many: k>>3 compensates */
X used_csize = lrec.csize - csize - (k >> 3);
X return 5;
X }
X return 0;
X}
X
X
X
Xint explode()
X/* Explode an imploded compressed stream. Based on the general purpose
X bit flag, decide on coded or uncoded literals, and an 8K or 4K sliding
X window. Construct the literal (if any), length, and distance codes and
X the tables needed to decode them (using huft_build() from inflate.c),
X and call the appropriate routine for the type of data in the remainder
X of the stream. The four routines are nearly identical, differing only
X in whether the literal is decoded or simply read in, and in how many
X bits are read in, uncoded, for the low distance bits. */
X{
X unsigned r; /* return codes */
X struct huft *tb; /* literal code table */
X struct huft *tl; /* length code table */
X struct huft *td; /* distance code table */
X int bb; /* bits for tb */
X int bl; /* bits for tl */
X int bd; /* bits for td */
X static unsigned l[256]; /* bit lengths for codes */
X
X
X /* Tune base table sizes. Note: I thought that to truly optimize speed,
X I would have to select different bl, bd, and bb values for different
X compressed file sizes. I was suprised to find out the the values of
X 7, 7, and 9 worked best over a very wide range of sizes, except that
X bd = 8 worked marginally better for large compressed sizes. */
X bl = 7;
X bd = csize > 200000L ? 8 : 7;
X
X
X /* With literal tree--minimum match length is 3 */
X hufts = 0; /* initialize huft's malloc'ed */
X if (lrec.general_purpose_bit_flag & 4)
X {
X bb = 9; /* base table size for literals */
X if ((r = get_tree(l, 256)) != 0)
X return (int)r;
X if ((r = huft_build(l, 256, 256, NULL, NULL, &tb, &bb)) != 0)
X {
X if (r == 1)
X huft_free(tb);
X return (int)r;
X }
X if ((r = get_tree(l, 64)) != 0)
X return (int)r;
X if ((r = huft_build(l, 64, 0, cplen3, extra, &tl, &bl)) != 0)
X {
X if (r == 1)
X huft_free(tl);
X huft_free(tb);
X return (int)r;
X }
X if ((r = get_tree(l, 64)) != 0)
X return (int)r;
X if (lrec.general_purpose_bit_flag & 2) /* true if 8K */
X {
X if ((r = huft_build(l, 64, 0, cpdist8, extra, &td, &bd)) != 0)
X {
X if (r == 1)
X huft_free(td);
X huft_free(tl);
X huft_free(tb);
X return (int)r;
X }
X r = explode_lit8(tb, tl, td, bb, bl, bd);
X }
X else /* else 4K */
X {
X if ((r = huft_build(l, 64, 0, cpdist4, extra, &td, &bd)) != 0)
X {
X if (r == 1)
X huft_free(td);
X huft_free(tl);
X huft_free(tb);
X return (int)r;
X }
X r = explode_lit4(tb, tl, td, bb, bl, bd);
X }
X huft_free(td);
X huft_free(tl);
X huft_free(tb);
X }
X else
X
X
X /* No literal tree--minimum match length is 2 */
X {
X if ((r = get_tree(l, 64)) != 0)
X return (int)r;
X if ((r = huft_build(l, 64, 0, cplen2, extra, &tl, &bl)) != 0)
X {
X if (r == 1)
X huft_free(tl);
X return (int)r;
X }
X if ((r = get_tree(l, 64)) != 0)
X return (int)r;
X if (lrec.general_purpose_bit_flag & 2) /* true if 8K */
X {
X if ((r = huft_build(l, 64, 0, cpdist8, extra, &td, &bd)) != 0)
X {
X if (r == 1)
X huft_free(td);
X huft_free(tl);
X return (int)r;
X }
X r = explode_nolit8(tl, td, bl, bd);
X }
X else /* else 4K */
X {
X if ((r = huft_build(l, 64, 0, cpdist4, extra, &td, &bd)) != 0)
X {
X if (r == 1)
X huft_free(td);
X huft_free(tl);
X return (int)r;
X }
X r = explode_nolit4(tl, td, bl, bd);
X }
X huft_free(td);
X huft_free(tl);
X }
X#ifdef DEBUG
X fprintf(stderr, "<%u > ", hufts);
X#endif /* DEBUG */
X return (int)r;
X}
X
X/* so explode.c and inflate.c can be compiled together into one object: */
X#undef NEXTBYTE
X#undef NEEDBITS
X#undef DUMPBITS
END_OF_FILE
if test 28523 -ne `wc -c <'unzip-5.12/explode.c'`; then
echo shar: \"'unzip-5.12/explode.c'\" unpacked with wrong size!
fi
# end of 'unzip-5.12/explode.c'
fi
if test -f 'unzip-5.12/inflate.c' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'unzip-5.12/inflate.c'\"
else
echo shar: Extracting \"'unzip-5.12/inflate.c'\" \(38275 characters\)
sed "s/^X//" >'unzip-5.12/inflate.c' <<'END_OF_FILE'
X/* inflate.c -- put in the public domain by Mark Adler
X version c14o, 23 August 1994 */
X
X
X/* You can do whatever you like with this source file, though I would
X prefer that if you modify it and redistribute it that you include
X comments to that effect with your name and the date. Thank you.
X
X History:
X vers date who what
X ---- --------- -------------- ------------------------------------
X a ~~ Feb 92 M. Adler used full (large, one-step) lookup table
X b1 21 Mar 92 M. Adler first version with partial lookup tables
X b2 21 Mar 92 M. Adler fixed bug in fixed-code blocks
X b3 22 Mar 92 M. Adler sped up match copies, cleaned up some
X b4 25 Mar 92 M. Adler added prototypes; removed window[] (now
X is the responsibility of unzip.h--also
X changed name to slide[]), so needs diffs
X for unzip.c and unzip.h (this allows
X compiling in the small model on MSDOS);
X fixed cast of q in huft_build();
X b5 26 Mar 92 M. Adler got rid of unintended macro recursion.
X b6 27 Mar 92 M. Adler got rid of nextbyte() routine. fixed
X bug in inflate_fixed().
X c1 30 Mar 92 M. Adler removed lbits, dbits environment variables.
X changed BMAX to 16 for explode. Removed
X OUTB usage, and replaced it with flush()--
X this was a 20% speed improvement! Added
X an explode.c (to replace unimplod.c) that
X uses the huft routines here. Removed
X register union.
X c2 4 Apr 92 M. Adler fixed bug for file sizes a multiple of 32k.
X c3 10 Apr 92 M. Adler reduced memory of code tables made by
X huft_build significantly (factor of two to
X three).
X c4 15 Apr 92 M. Adler added NOMEMCPY do kill use of memcpy().
X worked around a Turbo C optimization bug.
X c5 21 Apr 92 M. Adler added the WSIZE #define to allow reducing
X the 32K window size for specialized
X applications.
X c6 31 May 92 M. Adler added some typecasts to eliminate warnings
X c7 27 Jun 92 G. Roelofs added some more typecasts (444: MSC bug).
X c8 5 Oct 92 J-l. Gailly added ifdef'd code to deal with PKZIP bug.
X c9 9 Oct 92 M. Adler removed a memory error message (~line 416).
X c10 17 Oct 92 G. Roelofs changed ULONG/UWORD/byte to ulg/ush/uch,
X removed old inflate, renamed inflate_entry
X to inflate, added Mark's fix to a comment.
X c10.5 14 Dec 92 M. Adler fix up error messages for incomplete trees.
X c11 2 Jan 93 M. Adler fixed bug in detection of incomplete
X tables, and removed assumption that EOB is
X the longest code (bad assumption).
X c12 3 Jan 93 M. Adler make tables for fixed blocks only once.
X c13 5 Jan 93 M. Adler allow all zero length codes (pkzip 2.04c
X outputs one zero length code for an empty
X distance tree).
X c14 12 Mar 93 M. Adler made inflate.c standalone with the
X introduction of inflate.h.
X c14b 16 Jul 93 G. Roelofs added (unsigned) typecast to w at 470.
X c14c 19 Jul 93 J. Bush changed v[N_MAX], l[288], ll[28x+3x] arrays
X to static for Amiga.
X c14d 13 Aug 93 J-l. Gailly de-complicatified Mark's c[*p++]++ thing.
X c14e 8 Oct 93 G. Roelofs changed memset() to memzero().
X c14f 22 Oct 93 G. Roelofs renamed quietflg to qflag; made Trace()
X conditional; added inflate_free().
X c14g 28 Oct 93 G. Roelofs changed l/(lx+1) macro to pointer (Cray bug)
X c14h 7 Dec 93 C. Ghisler huft_build() optimizations.
X c14i 9 Jan 94 A. Verheijen set fixed_t{d,l} to NULL after freeing;
X G. Roelofs check NEXTBYTE macro for EOF.
X c14j 23 Jan 94 G. Roelofs removed Ghisler "optimizations"; ifdef'd
X EOF check.
X c14k 27 Feb 94 G. Roelofs added some typecasts to avoid warnings.
X c14l 9 Apr 94 G. Roelofs fixed split comments on preprocessor lines
X to avoid bug in Encore compiler.
X c14m 7 Jul 94 P. Kienitz modified to allow assembler version of
X inflate_codes() (define ASM_INFLATECODES)
X c14n 22 Jul 94 G. Roelofs changed fprintf to FPRINTF for DLL versions
X c14o 23 Aug 94 C. Spieler added a newline to a debug statement;
X G. Roelofs added another typecast to avoid MSC warning
X */
X
X
X/*
X Inflate deflated (PKZIP's method 8 compressed) data. The compression
X method searches for as much of the current string of bytes (up to a
X length of 258) in the previous 32K bytes. If it doesn't find any
X matches (of at least length 3), it codes the next byte. Otherwise, it
X codes the length of the matched string and its distance backwards from
X the current position. There is a single Huffman code that codes both
X single bytes (called "literals") and match lengths. A second Huffman
X code codes the distance information, which follows a length code. Each
X length or distance code actually represents a base value and a number
X of "extra" (sometimes zero) bits to get to add to the base value. At
X the end of each deflated block is a special end-of-block (EOB) literal/
X length code. The decoding process is basically: get a literal/length
X code; if EOB then done; if a literal, emit the decoded byte; if a
X length then get the distance and emit the referred-to bytes from the
X sliding window of previously emitted data.
X
X There are (currently) three kinds of inflate blocks: stored, fixed, and
X dynamic. The compressor outputs a chunk of data at a time and decides
X which method to use on a chunk-by-chunk basis. A chunk might typically
X be 32K to 64K, uncompressed. If the chunk is uncompressible, then the
X "stored" method is used. In this case, the bytes are simply stored as
X is, eight bits per byte, with none of the above coding. The bytes are
X preceded by a count, since there is no longer an EOB code.
X
X If the data is compressible, then either the fixed or dynamic methods
X are used. In the dynamic method, the compressed data is preceded by
X an encoding of the literal/length and distance Huffman codes that are
X to be used to decode this block. The representation is itself Huffman
X coded, and so is preceded by a description of that code. These code
X descriptions take up a little space, and so for small blocks, there is
X a predefined set of codes, called the fixed codes. The fixed method is
X used if the block ends up smaller that way (usually for quite small
X chunks); otherwise the dynamic method is used. In the latter case, the
X codes are customized to the probabilities in the current block and so
X can code it much better than the pre-determined fixed codes can.
X
X The Huffman codes themselves are decoded using a mutli-level table
X lookup, in order to maximize the speed of decoding plus the speed of
X building the decoding tables. See the comments below that precede the
X lbits and dbits tuning parameters.
X */
X
X
X/*
X Notes beyond the 1.93a appnote.txt:
X
X 1. Distance pointers never point before the beginning of the output
X stream.
X 2. Distance pointers can point back across blocks, up to 32k away.
X 3. There is an implied maximum of 7 bits for the bit length table and
X 15 bits for the actual data.
X 4. If only one code exists, then it is encoded using one bit. (Zero
X would be more efficient, but perhaps a little confusing.) If two
X codes exist, they are coded using one bit each (0 and 1).
X 5. There is no way of sending zero distance codes--a dummy must be
X sent if there are none. (History: a pre 2.0 version of PKZIP would
X store blocks with no distance codes, but this was discovered to be
X too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
X zero distance codes, which is sent as one code of zero bits in
X length.
X 6. There are up to 286 literal/length codes. Code 256 represents the
X end-of-block. Note however that the static length tree defines
X 288 codes just to fill out the Huffman codes. Codes 286 and 287
X cannot be used though, since there is no length base or extra bits
X defined for them. Similarily, there are up to 30 distance codes.
X However, static trees define 32 codes (all 5 bits) to fill out the
X Huffman codes, but the last two had better not show up in the data.
X 7. Unzip can check dynamic Huffman blocks for complete code sets.
X The exception is that a single code would not be complete (see #4).
X 8. The five bits following the block type is really the number of
X literal codes sent minus 257.
X 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
X (1+6+6). Therefore, to output three times the length, you output
X three codes (1+1+1), whereas to output four times the same length,
X you only need two codes (1+3). Hmm.
X 10. In the tree reconstruction algorithm, Code = Code + Increment
X only if BitLength(i) is not zero. (Pretty obvious.)
X 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
X 12. Note: length code 284 can represent 227-258, but length code 285
X really is 258. The last length deserves its own, short code
X since it gets used a lot in very redundant files. The length
X 258 is special since 258 - 3 (the min match length) is 255.
X 13. The literal/length and distance code bit lengths are read as a
X single stream of lengths. It is possible (and advantageous) for
X a repeat code (16, 17, or 18) to go across the boundary between
X the two sets of lengths.
X */
X
X
X#define PKZIP_BUG_WORKAROUND /* PKZIP 1.93a problem--live with it */
X
X/*
X inflate.h must supply the uch slide[WSIZE] array and the NEXTBYTE,
X FLUSH() and memzero macros. If the window size is not 32K, it
X should also define WSIZE. If INFMOD is defined, it can include
X compiled functions to support the NEXTBYTE and/or FLUSH() macros.
X There are defaults for NEXTBYTE and FLUSH() below for use as
X examples of what those functions need to do. Normally, you would
X also want FLUSH() to compute a crc on the data. inflate.h also
X needs to provide these typedefs:
X
X typedef unsigned char uch;
X typedef unsigned short ush;
X typedef unsigned long ulg;
X
X This module uses the external functions malloc() and free() (and
X probably memset() or bzero() in the memzero() macro). Their
X prototypes are normally found in <string.h> and <stdlib.h>.
X */
X#define INFMOD /* tell inflate.h to include code to be compiled */
X#include "inflate.h"
X
X#ifndef WSIZE /* default is 32K */
X# define WSIZE 0x8000 /* window size--must be a power of two, and at least */
X#endif /* 32K for zip's deflate method */
X
X#ifndef NEXTBYTE /* default is to simply get a byte from stdin */
X# define NEXTBYTE getchar()
X#endif
X
X#ifndef FPRINTF
X# define FPRINTF fprintf
X#endif
X
X#ifndef FLUSH /* default is to simply write the buffer to stdout */
X# define FLUSH(n) fwrite(slide, 1, n, stdout) /* return value not used */
X#endif
X/* Warning: the fwrite above might not work on 16-bit compilers, since
X 0x8000 might be interpreted as -32,768 by the library function. */
X
X#ifndef Trace
X# ifdef DEBUG
X# define Trace(x) fprintf x
X# else
X# define Trace(x)
X# endif
X#endif
X
X
X/* Huffman code lookup table entry--this entry is four bytes for machines
X that have 16-bit pointers (e.g. PC's in the small or medium model).
X Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16
X means that v is a literal, 16 < e < 32 means that v is a pointer to
X the next table, which codes e - 16 bits, and lastly e == 99 indicates
X an unused code. If a code with e == 99 is looked up, this implies an
X error in the data. */
Xstruct huft {
X uch e; /* number of extra bits or operation */
X uch b; /* number of bits in this code or subcode */
X union {
X ush n; /* literal, length base, or distance base */
X struct huft *t; /* pointer to next level of table */
X } v;
X};
X
X
X/* Function prototypes */
X#ifndef OF
X# ifdef __STDC__
X# define OF(a) a
X# else /* !__STDC__ */
X# define OF(a) ()
X# endif /* ?__STDC__ */
X#endif
Xint huft_build OF((unsigned *, unsigned, unsigned, ush *, ush *,
X struct huft **, int *));
Xint huft_free OF((struct huft *));
Xint inflate_codes OF((struct huft *, struct huft *, int, int));
Xint inflate_stored OF((void));
Xint inflate_fixed OF((void));
Xint inflate_dynamic OF((void));
Xint inflate_block OF((int *));
Xint inflate OF((void));
Xint inflate_free OF((void));
X
X
X/* The inflate algorithm uses a sliding 32K byte window on the uncompressed
X stream to find repeated byte strings. This is implemented here as a
X circular buffer. The index is updated simply by incrementing and then
X and'ing with 0x7fff (32K-1). */
X/* It is left to other modules to supply the 32K area. It is assumed
X to be usable as if it were declared "uch slide[32768];" or as just
X "uch *slide;" and then malloc'ed in the latter case. The definition
X must be in unzip.h, included above. */
Xunsigned wp; /* current position in slide */
X
X
X/* Tables for deflate from PKZIP's appnote.txt. */
Xstatic unsigned border[] = { /* Order of the bit length code lengths */
X 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
Xstatic ush cplens[] = { /* Copy lengths for literal codes 257..285 */
X 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
X 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
X /* note: see note #13 above about the 258 in this list. */
Xstatic ush cplext[] = { /* Extra bits for literal codes 257..285 */
X 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
X 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
Xstatic ush cpdist[] = { /* Copy offsets for distance codes 0..29 */
X 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
X 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
X 8193, 12289, 16385, 24577};
Xstatic ush cpdext[] = { /* Extra bits for distance codes */
X 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
X 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
X 12, 12, 13, 13};
X
X/* And'ing with mask[n] masks the lower n bits */
Xush mask[] = {
X 0x0000,
X 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
X 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
X};
X
X
X/* Macros for inflate() bit peeking and grabbing.
X The usage is:
X
X NEEDBITS(j)
X x = b & mask[j];
X DUMPBITS(j)
X
X where NEEDBITS makes sure that b has at least j bits in it, and
X DUMPBITS removes the bits from b. The macros use the variable k
X for the number of bits in b. Normally, b and k are register
X variables for speed, and are initialized at the begining of a
X routine that uses these macros from a global bit buffer and count.
X
X In order to not ask for more bits than there are in the compressed
X stream, the Huffman tables are constructed to only ask for just
X enough bits to make up the end-of-block code (value 256). Then no
X bytes need to be "returned" to the buffer at the end of the last
X block. See the huft_build() routine.
X */
X
Xulg bb; /* bit buffer */
Xunsigned bk; /* bits in bit buffer */
X
X#ifndef CHECK_EOF
X# define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE)<<k;k+=8;}}
X#else
X# define NEEDBITS(n) {while(k<(n)){int c=NEXTBYTE;if(c==EOF)return 1;\
X b|=((ulg)c)<<k;k+=8;}}
X#endif /* Piet Plomp: change "return 1" to "break" */
X
X#define DUMPBITS(n) {b>>=(n);k-=(n);}
X
X
X/*
X Huffman code decoding is performed using a multi-level table lookup.
X The fastest way to decode is to simply build a lookup table whose
X size is determined by the longest code. However, the time it takes
X to build this table can also be a factor if the data being decoded
X is not very long. The most common codes are necessarily the
X shortest codes, so those codes dominate the decoding time, and hence
X the speed. The idea is you can have a shorter table that decodes the
X shorter, more probable codes, and then point to subsidiary tables for
X the longer codes. The time it costs to decode the longer codes is
X then traded against the time it takes to make longer tables.
X
X This results of this trade are in the variables lbits and dbits
X below. lbits is the number of bits the first level table for literal/
X length codes can decode in one step, and dbits is the same thing for
X the distance codes. Subsequent tables are also less than or equal to
X those sizes. These values may be adjusted either when all of the
X codes are shorter than that, in which case the longest code length in
X bits is used, or when the shortest code is *longer* than the requested
X table size, in which case the length of the shortest code in bits is
X used.
X
X There are two different values for the two tables, since they code a
X different number of possibilities each. The literal/length table
X codes 286 possible values, or in a flat code, a little over eight
X bits. The distance table codes 30 possible values, or a little less
X than five bits, flat. The optimum values for speed end up being
X about one bit more than those, so lbits is 8+1 and dbits is 5+1.
X The optimum values may differ though from machine to machine, and
X possibly even between compilers. Your mileage may vary.
X */
X
X
Xint lbits = 9; /* bits in base literal/length lookup table */
Xint dbits = 6; /* bits in base distance lookup table */
X
X
X/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
X#define BMAX 16 /* maximum bit length of any code (16 for explode) */
X#define N_MAX 288 /* maximum number of codes in any set */
X
X
Xunsigned hufts; /* track memory usage */
X
X
Xint huft_build(b, n, s, d, e, t, m)
Xunsigned *b; /* code lengths in bits (all assumed <= BMAX) */
Xunsigned n; /* number of codes (assumed <= N_MAX) */
Xunsigned s; /* number of simple-valued codes (0..s-1) */
Xush *d; /* list of base values for non-simple codes */
Xush *e; /* list of extra bits for non-simple codes */
Xstruct huft **t; /* result: starting table */
Xint *m; /* maximum lookup bits, returns actual */
X/* Given a list of code lengths and a maximum table size, make a set of
X tables to decode that set of codes. Return zero on success, one if
X the given code set is incomplete (the tables are still built in this
X case), two if the input is invalid (all zero length codes or an
X oversubscribed set of lengths), and three if not enough memory.
X The code with value 256 is special, and the tables are constructed
X so that no bits beyond that code are fetched when that code is
X decoded. */
X{
X unsigned a; /* counter for codes of length k */
X unsigned c[BMAX+1]; /* bit length count table */
X unsigned el; /* length of EOB code (value 256) */
X unsigned f; /* i repeats in table every f entries */
X int g; /* maximum code length */
X int h; /* table level */
X register unsigned i; /* counter, current code */
X register unsigned j; /* counter */
X register int k; /* number of bits in current code */
X int lx[BMAX+1]; /* memory for l[-1..BMAX-1] */
X int *l = lx+1; /* stack of bits per table */
X register unsigned *p; /* pointer into c[], b[], or v[] */
X register struct huft *q; /* points to current table */
X struct huft r; /* table entry for structure assignment */
X struct huft *u[BMAX]; /* table stack */
X static unsigned v[N_MAX]; /* values in order of bit length */
X register int w; /* bits before this table == (l * h) */
X unsigned x[BMAX+1]; /* bit offsets, then code stack */
X unsigned *xp; /* pointer into x */
X int y; /* number of dummy codes added */
X unsigned z; /* number of entries in current table */
X
X
X /* Generate counts for each bit length */
X el = n > 256 ? b[256] : BMAX; /* set length of EOB code, if any */
X memzero((char *)c, sizeof(c));
X p = b; i = n;
X do {
X c[*p]++; p++; /* assume all entries <= BMAX */
X } while (--i);
X if (c[0] == n) /* null input--all zero length codes */
X {
X *t = (struct huft *)NULL;
X *m = 0;
X return 0;
X }
X
X
X /* Find minimum and maximum length, bound *m by those */
X for (j = 1; j <= BMAX; j++)
X if (c[j])
X break;
X k = j; /* minimum code length */
X if ((unsigned)*m < j)
X *m = j;
X for (i = BMAX; i; i--)
X if (c[i])
X break;
X g = i; /* maximum code length */
X if ((unsigned)*m > i)
X *m = i;
X
X
X /* Adjust last length count to fill out codes, if needed */
X for (y = 1 << j; j < i; j++, y <<= 1)
X if ((y -= c[j]) < 0)
X return 2; /* bad input: more codes than bits */
X if ((y -= c[i]) < 0)
X return 2;
X c[i] += y;
X
X
X /* Generate starting offsets into the value table for each length */
X x[1] = j = 0;
X p = c + 1; xp = x + 2;
X while (--i) { /* note that i == g from above */
X *xp++ = (j += *p++);
X }
X
X
X /* Make a table of values in order of bit lengths */
X p = b; i = 0;
X do {
X if ((j = *p++) != 0)
X v[x[j]++] = i;
X } while (++i < n);
X
X
X /* Generate the Huffman codes and for each, make the table entries */
X x[0] = i = 0; /* first Huffman code is zero */
X p = v; /* grab values in bit order */
X h = -1; /* no tables yet--level -1 */
X w = l[-1] = 0; /* no bits decoded yet */
X u[0] = (struct huft *)NULL; /* just to keep compilers happy */
X q = (struct huft *)NULL; /* ditto */
X z = 0; /* ditto */
X
X /* go through the bit lengths (k already is bits in shortest code) */
X for (; k <= g; k++)
X {
X a = c[k];
X while (a--)
X {
X /* here i is the Huffman code of length k bits for value *p */
X /* make tables up to required level */
X while (k > w + l[h])
X {
X w += l[h++]; /* add bits already decoded */
X
X /* compute minimum size table less than or equal to *m bits */
X z = (z = g - w) > (unsigned)*m ? *m : z; /* upper limit */
X if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
X { /* too few codes for k-w bit table */
X f -= a + 1; /* deduct codes from patterns left */
X xp = c + k;
X while (++j < z) /* try smaller tables up to z bits */
X {
X if ((f <<= 1) <= *++xp)
X break; /* enough codes to use up j bits */
X f -= *xp; /* else deduct codes from patterns */
X }
X }
X if ((unsigned)w + j > el && (unsigned)w < el)
X j = el - w; /* make EOB code end at table */
X z = 1 << j; /* table entries for j-bit table */
X l[h] = j; /* set table size in stack */
X
X /* allocate and link in new table */
X if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) ==
X (struct huft *)NULL)
X {
X if (h)
X huft_free(u[0]);
X return 3; /* not enough memory */
X }
X hufts += z + 1; /* track memory usage */
X *t = q + 1; /* link to list for huft_free() */
X *(t = &(q->v.t)) = (struct huft *)NULL;
X u[h] = ++q; /* table starts after link */
X
X /* connect to last table, if there is one */
X if (h)
X {
X x[h] = i; /* save pattern for backing up */
X r.b = (uch)l[h-1]; /* bits to dump before this table */
X r.e = (uch)(16 + j); /* bits in this table */
X r.v.t = q; /* pointer to this table */
X j = (i & ((1 << w) - 1)) >> (w - l[h-1]);
X u[h-1][j] = r; /* connect to last table */
X }
X }
X
X /* set up table entry in r */
X r.b = (uch)(k - w);
X if (p >= v + n)
X r.e = 99; /* out of values--invalid code */
X else if (*p < s)
X {
X r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
X r.v.n = *p++; /* simple code is just the value */
X }
X else
X {
X r.e = (uch)e[*p - s]; /* non-simple--look up in lists */
X r.v.n = d[*p++ - s];
X }
X
X /* fill code-like entries with r */
X f = 1 << (k - w);
X for (j = i >> w; j < z; j += f)
X q[j] = r;
X
X /* backwards increment the k-bit code i */
X for (j = 1 << (k - 1); i & j; j >>= 1)
X i ^= j;
X i ^= j;
X
X /* backup over finished tables */
X while ((i & ((1 << w) - 1)) != x[h])
X w -= l[--h]; /* don't need to update q */
X }
X }
X
X
X /* return actual size of base table */
X *m = l[0];
X
X
X /* Return true (1) if we were given an incomplete table */
X return y != 0 && g != 1;
X}
X
X
X
Xint huft_free(t)
Xstruct huft *t; /* table to free */
X/* Free the malloc'ed tables built by huft_build(), which makes a linked
X list of the tables it made, with the links in a dummy first entry of
X each table. */
X{
X register struct huft *p, *q;
X
X
X /* Go through linked list, freeing from the malloced (t[-1]) address. */
X p = t;
X while (p != (struct huft *)NULL)
X {
X q = (--p)->v.t;
X free(p);
X p = q;
X }
X return 0;
X}
X
X
X
X#ifdef ASM_INFLATECODES
X# define inflate_codes(tl,td,bl,bd) flate_codes(tl,td,bl,bd,(uch *)slide)
X int flate_codes OF((struct huft *, struct huft *, int, int, uch *));
X
X#else
X
Xint inflate_codes(tl, td, bl, bd)
Xstruct huft *tl, *td; /* literal/length and distance decoder tables */
Xint bl, bd; /* number of bits decoded by tl[] and td[] */
X/* inflate (decompress) the codes in a deflated (compressed) block.
X Return an error code or zero if it all goes ok. */
X{
X register unsigned e; /* table entry flag/number of extra bits */
X unsigned n, d; /* length and index for copy */
X unsigned w; /* current window position */
X struct huft *t; /* pointer to table entry */
X unsigned ml, md; /* masks for bl and bd bits */
X register ulg b; /* bit buffer */
X register unsigned k; /* number of bits in bit buffer */
X
X
X /* make local copies of globals */
X b = bb; /* initialize bit buffer */
X k = bk;
X w = wp; /* initialize window position */
X
X
X /* inflate the coded data */
X ml = mask[bl]; /* precompute masks for speed */
X md = mask[bd];
X while (1) /* do until end of block */
X {
X NEEDBITS((unsigned)bl)
X if ((e = (t = tl + ((unsigned)b & ml))->e) > 16)
X do {
X if (e == 99)
X return 1;
X DUMPBITS(t->b)
X e -= 16;
X NEEDBITS(e)
X } while ((e = (t = t->v.t + ((unsigned)b & mask[e]))->e) > 16);
X DUMPBITS(t->b)
X if (e == 16) /* then it's a literal */
X {
X slide[w++] = (uch)t->v.n;
X if (w == WSIZE)
X {
X FLUSH(w);
X w = 0;
X }
X }
X else /* it's an EOB or a length */
X {
X /* exit if end of block */
X if (e == 15)
X break;
X
X /* get length of block to copy */
X NEEDBITS(e)
X n = t->v.n + ((unsigned)b & mask[e]);
X DUMPBITS(e);
X
X /* decode distance of block to copy */
X NEEDBITS((unsigned)bd)
X if ((e = (t = td + ((unsigned)b & md))->e) > 16)
X do {
X if (e == 99)
X return 1;
X DUMPBITS(t->b)
X e -= 16;
X NEEDBITS(e)
X } while ((e = (t = t->v.t + ((unsigned)b & mask[e]))->e) > 16);
X DUMPBITS(t->b)
X NEEDBITS(e)
X d = w - t->v.n - ((unsigned)b & mask[e]);
X DUMPBITS(e)
X
X /* do the copy */
X do {
X n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
X#ifndef NOMEMCPY
X if (w - d >= e) /* (this test assumes unsigned comparison) */
X {
X memcpy(slide + w, slide + d, e);
X w += e;
X d += e;
X }
X else /* do it slow to avoid memcpy() overlap */
X#endif /* !NOMEMCPY */
X do {
X slide[w++] = slide[d++];
X } while (--e);
X if (w == WSIZE)
X {
X FLUSH(w);
X w = 0;
X }
X } while (n);
X }
X }
X
X
X /* restore the globals from the locals */
X wp = w; /* restore global window pointer */
X bb = b; /* restore global bit buffer */
X bk = k;
X
X
X /* done */
X return 0;
X}
X
X#endif /* ASM_INFLATECODES */
X
X
X
Xint inflate_stored()
X/* "decompress" an inflated type 0 (stored) block. */
X{
X unsigned n; /* number of bytes in block */
X unsigned w; /* current window position */
X register ulg b; /* bit buffer */
X register unsigned k; /* number of bits in bit buffer */
X
X
X /* make local copies of globals */
X Trace((stderr, "\nstored block"));
X b = bb; /* initialize bit buffer */
X k = bk;
X w = wp; /* initialize window position */
X
X
X /* go to byte boundary */
X n = k & 7;
X DUMPBITS(n);
X
X
X /* get the length and its complement */
X NEEDBITS(16)
X n = ((unsigned)b & 0xffff);
X DUMPBITS(16)
X NEEDBITS(16)
X if (n != (unsigned)((~b) & 0xffff))
X return 1; /* error in compressed data */
X DUMPBITS(16)
X
X
X /* read and output the compressed data */
X while (n--)
X {
X NEEDBITS(8)
X slide[w++] = (uch)b;
X if (w == WSIZE)
X {
X FLUSH(w);
X w = 0;
X }
X DUMPBITS(8)
X }
X
X
X /* restore the globals from the locals */
X wp = w; /* restore global window pointer */
X bb = b; /* restore global bit buffer */
X bk = k;
X return 0;
X}
X
X
X/* Globals for literal tables (built once) */
Xstruct huft *fixed_tl = (struct huft *)NULL;
Xstruct huft *fixed_td;
Xint fixed_bl, fixed_bd;
X
Xint inflate_fixed()
X/* decompress an inflated type 1 (fixed Huffman codes) block. We should
X either replace this with a custom decoder, or at least precompute the
X Huffman tables. */
X{
X /* if first time, set up tables for fixed blocks */
X Trace((stderr, "\nliteral block"));
X if (fixed_tl == (struct huft *)NULL)
X {
X int i; /* temporary variable */
X static unsigned l[288]; /* length list for huft_build */
X
X /* literal table */
X for (i = 0; i < 144; i++)
X l[i] = 8;
X for (; i < 256; i++)
X l[i] = 9;
X for (; i < 280; i++)
X l[i] = 7;
X for (; i < 288; i++) /* make a complete, but wrong code set */
X l[i] = 8;
X fixed_bl = 7;
X if ((i = huft_build(l, 288, 257, cplens, cplext,
X &fixed_tl, &fixed_bl)) != 0)
X {
X fixed_tl = (struct huft *)NULL;
X return i;
X }
X
X /* distance table */
X for (i = 0; i < 30; i++) /* make an incomplete code set */
X l[i] = 5;
X fixed_bd = 5;
X if ((i = huft_build(l, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd)) > 1)
X {
X huft_free(fixed_tl);
X fixed_tl = (struct huft *)NULL;
X return i;
X }
X }
X
X
X /* decompress until an end-of-block code */
X return inflate_codes(fixed_tl, fixed_td, fixed_bl, fixed_bd) != 0;
X}
X
X
X
Xint inflate_dynamic()
X/* decompress an inflated type 2 (dynamic Huffman codes) block. */
X{
X int i; /* temporary variables */
X unsigned j;
X unsigned l; /* last length */
X unsigned m; /* mask for bit lengths table */
X unsigned n; /* number of lengths to get */
X struct huft *tl; /* literal/length code table */
X struct huft *td; /* distance code table */
X int bl; /* lookup bits for tl */
X int bd; /* lookup bits for td */
X unsigned nb; /* number of bit length codes */
X unsigned nl; /* number of literal/length codes */
X unsigned nd; /* number of distance codes */
X#ifdef PKZIP_BUG_WORKAROUND
X static unsigned ll[288+32]; /* literal/length and distance code lengths */
X#else
X static unsigned ll[286+30]; /* literal/length and distance code lengths */
X#endif
X register ulg b; /* bit buffer */
X register unsigned k; /* number of bits in bit buffer */
X
X
X /* make local bit buffer */
X Trace((stderr, "\ndynamic block"));
X b = bb;
X k = bk;
X
X
X /* read in table lengths */
X NEEDBITS(5)
X nl = 257 + ((unsigned)b & 0x1f); /* number of literal/length codes */
X DUMPBITS(5)
X NEEDBITS(5)
X nd = 1 + ((unsigned)b & 0x1f); /* number of distance codes */
X DUMPBITS(5)
X NEEDBITS(4)
X nb = 4 + ((unsigned)b & 0xf); /* number of bit length codes */
X DUMPBITS(4)
X#ifdef PKZIP_BUG_WORKAROUND
X if (nl > 288 || nd > 32)
X#else
X if (nl > 286 || nd > 30)
X#endif
X return 1; /* bad lengths */
X
X
X /* read in bit-length-code lengths */
X for (j = 0; j < nb; j++)
X {
X NEEDBITS(3)
X ll[border[j]] = (unsigned)b & 7;
X DUMPBITS(3)
X }
X for (; j < 19; j++)
X ll[border[j]] = 0;
X
X
X /* build decoding table for trees--single level, 7 bit lookup */
X bl = 7;
X if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
X {
X if (i == 1)
X huft_free(tl);
X return i; /* incomplete code set */
X }
X
X
X /* read in literal and distance code lengths */
X n = nl + nd;
X m = mask[bl];
X i = l = 0;
X while ((unsigned)i < n)
X {
X NEEDBITS((unsigned)bl)
X j = (td = tl + ((unsigned)b & m))->b;
X DUMPBITS(j)
X j = td->v.n;
X if (j < 16) /* length of code in bits (0..15) */
X ll[i++] = l = j; /* save last length in l */
X else if (j == 16) /* repeat last length 3 to 6 times */
X {
X NEEDBITS(2)
X j = 3 + ((unsigned)b & 3);
X DUMPBITS(2)
X if ((unsigned)i + j > n)
X return 1;
X while (j--)
X ll[i++] = l;
X }
X else if (j == 17) /* 3 to 10 zero length codes */
X {
X NEEDBITS(3)
X j = 3 + ((unsigned)b & 7);
X DUMPBITS(3)
X if ((unsigned)i + j > n)
X return 1;
X while (j--)
X ll[i++] = 0;
X l = 0;
X }
X else /* j == 18: 11 to 138 zero length codes */
X {
X NEEDBITS(7)
X j = 11 + ((unsigned)b & 0x7f);
X DUMPBITS(7)
X if ((unsigned)i + j > n)
X return 1;
X while (j--)
X ll[i++] = 0;
X l = 0;
X }
X }
X
X
X /* free decoding table for trees */
X huft_free(tl);
X
X
X /* restore the global bit buffer */
X bb = b;
X bk = k;
X
X
X /* build the decoding tables for literal/length and distance codes */
X bl = lbits;
X if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
X {
X if (i == 1 && !qflag) {
X FPRINTF(stderr, "(incomplete l-tree) ");
X huft_free(tl);
X }
X return i; /* incomplete code set */
X }
X bd = dbits;
X if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
X {
X if (i == 1 && !qflag) {
X FPRINTF(stderr, "(incomplete d-tree) ");
X#ifdef PKZIP_BUG_WORKAROUND
X i = 0;
X }
X#else
X huft_free(td);
X }
X huft_free(tl);
X return i; /* incomplete code set */
X#endif
X }
X
X
X /* decompress until an end-of-block code */
X if (inflate_codes(tl, td, bl, bd))
X return 1;
X
X
X /* free the decoding tables, return */
X huft_free(tl);
X huft_free(td);
X return 0;
X}
X
X
X
Xint inflate_block(e)
Xint *e; /* last block flag */
X/* decompress an inflated block */
X{
X unsigned t; /* block type */
X register ulg b; /* bit buffer */
X register unsigned k; /* number of bits in bit buffer */
X
X
X /* make local bit buffer */
X b = bb;
X k = bk;
X
X
X /* read in last block bit */
X NEEDBITS(1)
X *e = (int)b & 1;
X DUMPBITS(1)
X
X
X /* read in block type */
X NEEDBITS(2)
X t = (unsigned)b & 3;
X DUMPBITS(2)
X
X
X /* restore the global bit buffer */
X bb = b;
X bk = k;
X
X
X /* inflate that block type */
X if (t == 2)
X return inflate_dynamic();
X if (t == 0)
X return inflate_stored();
X if (t == 1)
X return inflate_fixed();
X
X
X /* bad block type */
X return 2;
X}
X
X
X
Xint inflate()
X/* decompress an inflated entry */
X{
X int e; /* last block flag */
X int r; /* result code */
X unsigned h; /* maximum struct huft's malloc'ed */
X
X
X /* initialize window, bit buffer */
X wp = 0;
X bk = 0;
X bb = 0;
X
X
X /* decompress until the last block */
X h = 0;
X do {
X hufts = 0;
X if ((r = inflate_block(&e)) != 0)
X return r;
X if (hufts > h)
X h = hufts;
X } while (!e);
X
X
X /* flush out slide */
X FLUSH(wp);
X
X
X /* return success */
X Trace((stderr, "\n%u bytes in Huffman tables (%d/entry)\n",
X h * sizeof(struct huft), sizeof(struct huft)));
X return 0;
X}
X
X
X
Xint inflate_free()
X{
X if (fixed_tl != (struct huft *)NULL)
X {
X huft_free(fixed_td);
X huft_free(fixed_tl);
X fixed_td = fixed_tl = (struct huft *)NULL;
X }
X return 0;
X}
END_OF_FILE
if test 38275 -ne `wc -c <'unzip-5.12/inflate.c'`; then
echo shar: \"'unzip-5.12/inflate.c'\" unpacked with wrong size!
fi
# end of 'unzip-5.12/inflate.c'
fi
echo shar: End of archive 9 \(of 20\).
cp /dev/null ark9isdone
MISSING=""
for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ; do
if test ! -f ark${I}isdone ; then
MISSING="${MISSING} ${I}"
fi
done
if test "${MISSING}" = "" ; then
echo You have unpacked all 20 archives.
rm -f ark[1-9]isdone ark[1-9][0-9]isdone
else
echo You still must unpack the following archives:
echo " " ${MISSING}
fi
exit 0
exit 0 # Just in case...