Last active
March 16, 2016 00:47
-
-
Save jvilk/d1494ffb3d04df350c7d to your computer and use it in GitHub Desktop.
explode.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* explode.c -- Not copyrighted 1992 by Mark Adler | |
version c7, 27 June 1992 */ | |
/* You can do whatever you like with this source file, though I would | |
prefer that if you modify it and redistribute it that you include | |
comments to that effect with your name and the date. Thank you. | |
History: | |
vers date who what | |
---- --------- -------------- ------------------------------------ | |
c1 30 Mar 92 M. Adler explode that uses huft_build from inflate | |
(this gives over a 70% speed improvement | |
over the original unimplode.c, which | |
decoded a bit at a time) | |
c2 4 Apr 92 M. Adler fixed bug for file sizes a multiple of 32k. | |
c3 10 Apr 92 M. Adler added a little memory tracking if DEBUG | |
c4 11 Apr 92 M. Adler added NOMEMCPY do kill use of memcpy() | |
c5 21 Apr 92 M. Adler added the WSIZE #define to allow reducing | |
the 32K window size for specialized | |
applications. | |
c6 31 May 92 M. Adler added typecasts to eliminate some warnings | |
c7 27 Jun 92 G. Roelofs added more typecasts | |
*/ | |
/* | |
Explode imploded (PKZIP method 6 compressed) data. This compression | |
method searches for as much of the current string of bytes (up to a length | |
of ~320) in the previous 4K or 8K bytes. If it doesn't find any matches | |
(of at least length 2 or 3), it codes the next byte. Otherwise, it codes | |
the length of the matched string and its distance backwards from the | |
current position. Single bytes ("literals") are preceded by a one (a | |
single bit) and are either uncoded (the eight bits go directly into the | |
compressed stream for a total of nine bits) or Huffman coded with a | |
supplied literal code tree. If literals are coded, then the minimum match | |
length is three, otherwise it is two. | |
There are therefore four kinds of imploded streams: 8K search with coded | |
literals (min match = 3), 4K search with coded literals (min match = 3), | |
8K with uncoded literals (min match = 2), and 4K with uncoded literals | |
(min match = 2). The kind of stream is identified in two bits of a | |
general purpose bit flag that is outside of the compressed stream. | |
Distance-length pairs are always coded. Distance-length pairs for matched | |
strings are preceded by a zero bit (to distinguish them from literals) and | |
are always coded. The distance comes first and is either the low six (4K) | |
or low seven (8K) bits of the distance (uncoded), followed by the high six | |
bits of the distance coded. Then the length is six bits coded (0..63 + | |
min match length), and if the maximum such length is coded, then it's | |
followed by another eight bits (uncoded) to be added to the coded length. | |
This gives a match length range of 2..320 or 3..321 bytes. | |
The literal, length, and distance codes are all represented in a slightly | |
compressed form themselves. What is sent are the lengths of the codes for | |
each value, which is sufficient to construct the codes. Each byte of the | |
code representation is the code length (the low four bits representing | |
1..16), and the number of values sequentially with that length (the high | |
four bits also representing 1..16). There are 256 literal code values (if | |
literals are coded), 64 length code values, and 64 distance code values, | |
in that order at the beginning of the compressed stream. Each set of code | |
values is preceded (redundantly) with a byte indicating how many bytes are | |
in the code description that follows, in the range 1..256. | |
The codes themselves are decoded using tables made by huft_build() from | |
the bit lengths. That routine and its comments are in the inflate.c | |
module. | |
*/ | |
#include "unzip.h" /* this must supply the slide[] (byte) array */ | |
#ifndef WSIZE | |
# define WSIZE 0x8000 /* window size--must be a power of two, and at least | |
8K for zip's implode method */ | |
#endif /* !WSIZE */ | |
struct huft { | |
byte e; /* number of extra bits or operation */ | |
byte b; /* number of bits in this code or subcode */ | |
union { | |
UWORD n; /* literal, length base, or distance base */ | |
struct huft *t; /* pointer to next level of table */ | |
} v; | |
}; | |
/* Function prototypes */ | |
/* routines from inflate.c */ | |
extern unsigned hufts; | |
int huft_build OF((unsigned *, unsigned, unsigned, UWORD *, UWORD *, | |
struct huft **, int *)); | |
int huft_free OF((struct huft *)); | |
void flush OF((unsigned)); | |
/* routines here */ | |
int get_tree OF((unsigned *, unsigned)); | |
int explode_lit8 OF((struct huft *, struct huft *, struct huft *, | |
int, int, int)); | |
int explode_lit4 OF((struct huft *, struct huft *, struct huft *, | |
int, int, int)); | |
int explode_nolit8 OF((struct huft *, struct huft *, int, int)); | |
int explode_nolit4 OF((struct huft *, struct huft *, int, int)); | |
int explode OF((void)); | |
/* The implode algorithm uses a sliding 4K or 8K byte window on the | |
uncompressed stream to find repeated byte strings. This is implemented | |
here as a circular buffer. The index is updated simply by incrementing | |
and then and'ing with 0x0fff (4K-1) or 0x1fff (8K-1). Here, the 32K | |
buffer of inflate is used, and it works just as well to always have | |
a 32K circular buffer, so the index is anded with 0x7fff. This is | |
done to allow the window to also be used as the output buffer. */ | |
/* This must be supplied in an external module useable like "byte slide[8192];" | |
or "byte *slide;", where the latter would be malloc'ed. In unzip, slide[] | |
is actually a 32K area for use by inflate, which uses a 32K sliding window. | |
*/ | |
/* Tables for length and distance */ | |
UWORD cplen2[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, | |
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, | |
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, | |
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65}; | |
UWORD cplen3[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, | |
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, | |
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, | |
53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66}; | |
UWORD extra[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
8}; | |
UWORD cpdist4[] = {1, 65, 129, 193, 257, 321, 385, 449, 513, 577, 641, 705, | |
769, 833, 897, 961, 1025, 1089, 1153, 1217, 1281, 1345, 1409, 1473, | |
1537, 1601, 1665, 1729, 1793, 1857, 1921, 1985, 2049, 2113, 2177, | |
2241, 2305, 2369, 2433, 2497, 2561, 2625, 2689, 2753, 2817, 2881, | |
2945, 3009, 3073, 3137, 3201, 3265, 3329, 3393, 3457, 3521, 3585, | |
3649, 3713, 3777, 3841, 3905, 3969, 4033}; | |
UWORD cpdist8[] = {1, 129, 257, 385, 513, 641, 769, 897, 1025, 1153, 1281, | |
1409, 1537, 1665, 1793, 1921, 2049, 2177, 2305, 2433, 2561, 2689, | |
2817, 2945, 3073, 3201, 3329, 3457, 3585, 3713, 3841, 3969, 4097, | |
4225, 4353, 4481, 4609, 4737, 4865, 4993, 5121, 5249, 5377, 5505, | |
5633, 5761, 5889, 6017, 6145, 6273, 6401, 6529, 6657, 6785, 6913, | |
7041, 7169, 7297, 7425, 7553, 7681, 7809, 7937, 8065}; | |
/* Macros for inflate() bit peeking and grabbing. | |
The usage is: | |
NEEDBITS(j) | |
x = b & mask_bits[j]; | |
DUMPBITS(j) | |
where NEEDBITS makes sure that b has at least j bits in it, and | |
DUMPBITS removes the bits from b. The macros use the variable k | |
for the number of bits in b. Normally, b and k are register | |
variables for speed. | |
*/ | |
extern UWORD bytebuf; /* (use the one in inflate.c) */ | |
#define NEXTBYTE (ReadByte(&bytebuf), bytebuf) | |
#define NEEDBITS(n) {while(k<(n)){b|=((ULONG)NEXTBYTE)<<k;k+=8;}} | |
#define DUMPBITS(n) {b>>=(n);k-=(n);} | |
int get_tree(l, n) | |
unsigned *l; /* bit lengths */ | |
unsigned n; /* number expected */ | |
/* Get the bit lengths for a code representation from the compressed | |
stream. If get_tree() returns 4, then there is an error in the data. | |
Otherwise zero is returned. */ | |
{ | |
unsigned i; /* bytes remaining in list */ | |
unsigned k; /* lengths entered */ | |
unsigned j; /* number of codes */ | |
unsigned b; /* bit length for those codes */ | |
/* get bit lengths */ | |
ReadByte(&bytebuf); | |
i = bytebuf + 1; /* length/count pairs to read */ | |
k = 0; /* next code */ | |
do { | |
ReadByte(&bytebuf); | |
b = ((j = bytebuf) & 0xf) + 1; /* bits in code (1..16) */ | |
j = ((j & 0xf0) >> 4) + 1; /* codes with those bits (1..16) */ | |
if (k + j > n) | |
return 4; /* don't overflow l[] */ | |
do { | |
l[k++] = b; | |
} while (--j); | |
} while (--i); | |
return k != n ? 4 : 0; /* should have read n of them */ | |
} | |
int explode_lit8(tb, tl, td, bb, bl, bd) | |
struct huft *tb, *tl, *td; /* literal, length, and distance tables */ | |
int bb, bl, bd; /* number of bits decoded by those */ | |
/* Decompress the imploded data using coded literals and an 8K sliding | |
window. */ | |
{ | |
longint s; /* bytes to decompress */ | |
register unsigned e; /* table entry flag/number of extra bits */ | |
unsigned n, d; /* length and index for copy */ | |
unsigned w; /* current window position */ | |
struct huft *t; /* pointer to table entry */ | |
unsigned mb, ml, md; /* masks for bb, bl, and bd bits */ | |
register ULONG b; /* bit buffer */ | |
register unsigned k; /* number of bits in bit buffer */ | |
unsigned u; /* true if unflushed */ | |
/* explode the coded data */ | |
b = k = w = 0; /* initialize bit buffer, window */ | |
u = 1; /* buffer unflushed */ | |
mb = mask_bits[bb]; /* precompute masks for speed */ | |
ml = mask_bits[bl]; | |
md = mask_bits[bd]; | |
s = ucsize; | |
while (s > 0) /* do until ucsize bytes uncompressed */ | |
{ | |
NEEDBITS(1) | |
if (b & 1) /* then literal--decode it */ | |
{ | |
DUMPBITS(1) | |
s--; | |
NEEDBITS((unsigned)bb) /* get coded literal */ | |
if ((e = (t = tb + ((~(unsigned)b) & mb))->e) > 16) | |
do { | |
if (e == 99) | |
return 1; | |
DUMPBITS(t->b) | |
e -= 16; | |
NEEDBITS(e) | |
} while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16); | |
DUMPBITS(t->b) | |
slide[w++] = (byte)t->v.n; | |
if (w == WSIZE) | |
{ | |
flush(w); | |
w = u = 0; | |
} | |
} | |
else /* else distance/length */ | |
{ | |
DUMPBITS(1) | |
NEEDBITS(7) /* get distance low bits */ | |
d = (unsigned)b & 0x7f; | |
DUMPBITS(7) | |
NEEDBITS((unsigned)bd) /* get coded distance high bits */ | |
if ((e = (t = td + ((~(unsigned)b) & md))->e) > 16) | |
do { | |
if (e == 99) | |
return 1; | |
DUMPBITS(t->b) | |
e -= 16; | |
NEEDBITS(e) | |
} while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16); | |
DUMPBITS(t->b) | |
d = w - d - t->v.n; /* construct offset */ | |
NEEDBITS((unsigned)bl) /* get coded length */ | |
if ((e = (t = tl + ((~(unsigned)b) & ml))->e) > 16) | |
do { | |
if (e == 99) | |
return 1; | |
DUMPBITS(t->b) | |
e -= 16; | |
NEEDBITS(e) | |
} while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16); | |
DUMPBITS(t->b) | |
n = t->v.n; | |
if (e) /* get length extra bits */ | |
{ | |
NEEDBITS(8) | |
n += (unsigned)b & 0xff; | |
DUMPBITS(8) | |
} | |
/* do the copy */ | |
s -= n; | |
do { | |
n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e); | |
if (u && w <= d) | |
{ | |
memset(slide + w, 0, e); | |
w += e; | |
d += e; | |
} | |
else | |
#ifndef NOMEMCPY | |
if (w - d >= e) /* (this test assumes unsigned comparison) */ | |
{ | |
memcpy(slide + w, slide + d, e); | |
w += e; | |
d += e; | |
} | |
else /* do it slow to avoid memcpy() overlap */ | |
#endif /* !NOMEMCPY */ | |
do { | |
slide[w++] = slide[d++]; | |
} while (--e); | |
if (w == WSIZE) | |
{ | |
flush(w); | |
w = u = 0; | |
} | |
} while (n); | |
} | |
} | |
/* flush out slide */ | |
flush(w); | |
return csize ? 5 : 0; /* should have read csize bytes */ | |
} | |
int explode_lit4(tb, tl, td, bb, bl, bd) | |
struct huft *tb, *tl, *td; /* literal, length, and distance tables */ | |
int bb, bl, bd; /* number of bits decoded by those */ | |
/* Decompress the imploded data using coded literals and a 4K sliding | |
window. */ | |
{ | |
longint s; /* bytes to decompress */ | |
register unsigned e; /* table entry flag/number of extra bits */ | |
unsigned n, d; /* length and index for copy */ | |
unsigned w; /* current window position */ | |
struct huft *t; /* pointer to table entry */ | |
unsigned mb, ml, md; /* masks for bb, bl, and bd bits */ | |
register ULONG b; /* bit buffer */ | |
register unsigned k; /* number of bits in bit buffer */ | |
unsigned u; /* true if unflushed */ | |
/* explode the coded data */ | |
b = k = w = 0; /* initialize bit buffer, window */ | |
u = 1; /* buffer unflushed */ | |
mb = mask_bits[bb]; /* precompute masks for speed */ | |
ml = mask_bits[bl]; | |
md = mask_bits[bd]; | |
s = ucsize; | |
while (s > 0) /* do until ucsize bytes uncompressed */ | |
{ | |
NEEDBITS(1) | |
if (b & 1) /* then literal--decode it */ | |
{ | |
DUMPBITS(1) | |
s--; | |
NEEDBITS((unsigned)bb) /* get coded literal */ | |
if ((e = (t = tb + ((~(unsigned)b) & mb))->e) > 16) | |
do { | |
if (e == 99) | |
return 1; | |
DUMPBITS(t->b) | |
e -= 16; | |
NEEDBITS(e) | |
} while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16); | |
DUMPBITS(t->b) | |
slide[w++] = (byte)t->v.n; | |
if (w == WSIZE) | |
{ | |
flush(w); | |
w = u = 0; | |
} | |
} | |
else /* else distance/length */ | |
{ | |
DUMPBITS(1) | |
NEEDBITS(6) /* get distance low bits */ | |
d = (unsigned)b & 0x3f; | |
DUMPBITS(6) | |
NEEDBITS((unsigned)bd) /* get coded distance high bits */ | |
if ((e = (t = td + ((~(unsigned)b) & md))->e) > 16) | |
do { | |
if (e == 99) | |
return 1; | |
DUMPBITS(t->b) | |
e -= 16; | |
NEEDBITS(e) | |
} while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16); | |
DUMPBITS(t->b) | |
d = w - d - t->v.n; /* construct offset */ | |
NEEDBITS((unsigned)bl) /* get coded length */ | |
if ((e = (t = tl + ((~(unsigned)b) & ml))->e) > 16) | |
do { | |
if (e == 99) | |
return 1; | |
DUMPBITS(t->b) | |
e -= 16; | |
NEEDBITS(e) | |
} while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16); | |
DUMPBITS(t->b) | |
n = t->v.n; | |
if (e) /* get length extra bits */ | |
{ | |
NEEDBITS(8) | |
n += (unsigned)b & 0xff; | |
DUMPBITS(8) | |
} | |
/* do the copy */ | |
s -= n; | |
do { | |
n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e); | |
if (u && w <= d) | |
{ | |
memset(slide + w, 0, e); | |
w += e; | |
d += e; | |
} | |
else | |
#ifndef NOMEMCPY | |
if (w - d >= e) /* (this test assumes unsigned comparison) */ | |
{ | |
memcpy(slide + w, slide + d, e); | |
w += e; | |
d += e; | |
} | |
else /* do it slow to avoid memcpy() overlap */ | |
#endif /* !NOMEMCPY */ | |
do { | |
slide[w++] = slide[d++]; | |
} while (--e); | |
if (w == WSIZE) | |
{ | |
flush(w); | |
w = u = 0; | |
} | |
} while (n); | |
} | |
} | |
/* flush out slide */ | |
flush(w); | |
return csize ? 5 : 0; /* should have read csize bytes */ | |
} | |
int explode_nolit8(tl, td, bl, bd) | |
struct huft *tl, *td; /* length and distance decoder tables */ | |
int bl, bd; /* number of bits decoded by tl[] and td[] */ | |
/* Decompress the imploded data using uncoded literals and an 8K sliding | |
window. */ | |
{ | |
longint s; /* bytes to decompress */ | |
register unsigned e; /* table entry flag/number of extra bits */ | |
unsigned n, d; /* length and index for copy */ | |
unsigned w; /* current window position */ | |
struct huft *t; /* pointer to table entry */ | |
unsigned ml, md; /* masks for bl and bd bits */ | |
register ULONG b; /* bit buffer */ | |
register unsigned k; /* number of bits in bit buffer */ | |
unsigned u; /* true if unflushed */ | |
/* explode the coded data */ | |
b = k = w = 0; /* initialize bit buffer, window */ | |
u = 1; /* buffer unflushed */ | |
ml = mask_bits[bl]; /* precompute masks for speed */ | |
md = mask_bits[bd]; | |
s = ucsize; | |
while (s > 0) /* do until ucsize bytes uncompressed */ | |
{ | |
NEEDBITS(1) | |
if (b & 1) /* then literal--get eight bits */ | |
{ | |
DUMPBITS(1) | |
s--; | |
NEEDBITS(8) | |
slide[w++] = (byte)b; | |
if (w == WSIZE) | |
{ | |
flush(w); | |
w = u = 0; | |
} | |
DUMPBITS(8) | |
} | |
else /* else distance/length */ | |
{ | |
DUMPBITS(1) | |
NEEDBITS(7) /* get distance low bits */ | |
d = (unsigned)b & 0x7f; | |
DUMPBITS(7) | |
NEEDBITS((unsigned)bd) /* get coded distance high bits */ | |
if ((e = (t = td + ((~(unsigned)b) & md))->e) > 16) | |
do { | |
if (e == 99) | |
return 1; | |
DUMPBITS(t->b) | |
e -= 16; | |
NEEDBITS(e) | |
} while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16); | |
DUMPBITS(t->b) | |
d = w - d - t->v.n; /* construct offset */ | |
NEEDBITS((unsigned)bl) /* get coded length */ | |
if ((e = (t = tl + ((~(unsigned)b) & ml))->e) > 16) | |
do { | |
if (e == 99) | |
return 1; | |
DUMPBITS(t->b) | |
e -= 16; | |
NEEDBITS(e) | |
} while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16); | |
DUMPBITS(t->b) | |
n = t->v.n; | |
if (e) /* get length extra bits */ | |
{ | |
NEEDBITS(8) | |
n += (unsigned)b & 0xff; | |
DUMPBITS(8) | |
} | |
/* do the copy */ | |
s -= n; | |
do { | |
n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e); | |
if (u && w <= d) | |
{ | |
memset(slide + w, 0, e); | |
w += e; | |
d += e; | |
} | |
else | |
#ifndef NOMEMCPY | |
if (w - d >= e) /* (this test assumes unsigned comparison) */ | |
{ | |
memcpy(slide + w, slide + d, e); | |
w += e; | |
d += e; | |
} | |
else /* do it slow to avoid memcpy() overlap */ | |
#endif /* !NOMEMCPY */ | |
do { | |
slide[w++] = slide[d++]; | |
} while (--e); | |
if (w == WSIZE) | |
{ | |
flush(w); | |
w = u = 0; | |
} | |
} while (n); | |
} | |
} | |
/* flush out slide */ | |
flush(w); | |
return csize ? 5 : 0; /* should have read csize bytes */ | |
} | |
int explode_nolit4(tl, td, bl, bd) | |
struct huft *tl, *td; /* length and distance decoder tables */ | |
int bl, bd; /* number of bits decoded by tl[] and td[] */ | |
/* Decompress the imploded data using uncoded literals and a 4K sliding | |
window. */ | |
{ | |
longint s; /* bytes to decompress */ | |
register unsigned e; /* table entry flag/number of extra bits */ | |
unsigned n, d; /* length and index for copy */ | |
unsigned w; /* current window position */ | |
struct huft *t; /* pointer to table entry */ | |
unsigned ml, md; /* masks for bl and bd bits */ | |
register ULONG b; /* bit buffer */ | |
register unsigned k; /* number of bits in bit buffer */ | |
unsigned u; /* true if unflushed */ | |
/* explode the coded data */ | |
b = k = w = 0; /* initialize bit buffer, window */ | |
u = 1; /* buffer unflushed */ | |
ml = mask_bits[bl]; /* precompute masks for speed */ | |
md = mask_bits[bd]; | |
s = ucsize; | |
while (s > 0) /* do until ucsize bytes uncompressed */ | |
{ | |
NEEDBITS(1) | |
if (b & 1) /* then literal--get eight bits */ | |
{ | |
DUMPBITS(1) | |
s--; | |
NEEDBITS(8) | |
slide[w++] = (byte)b; | |
if (w == WSIZE) | |
{ | |
flush(w); | |
w = u = 0; | |
} | |
DUMPBITS(8) | |
} | |
else /* else distance/length */ | |
{ | |
DUMPBITS(1) | |
NEEDBITS(6) /* get distance low bits */ | |
d = (unsigned)b & 0x3f; | |
DUMPBITS(6) | |
NEEDBITS((unsigned)bd) /* get coded distance high bits */ | |
if ((e = (t = td + ((~(unsigned)b) & md))->e) > 16) | |
do { | |
if (e == 99) | |
return 1; | |
DUMPBITS(t->b) | |
e -= 16; | |
NEEDBITS(e) | |
} while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16); | |
DUMPBITS(t->b) | |
d = w - d - t->v.n; /* construct offset */ | |
NEEDBITS((unsigned)bl) /* get coded length */ | |
if ((e = (t = tl + ((~(unsigned)b) & ml))->e) > 16) | |
do { | |
if (e == 99) | |
return 1; | |
DUMPBITS(t->b) | |
e -= 16; | |
NEEDBITS(e) | |
} while ((e = (t = t->v.t + ((~(unsigned)b) & mask_bits[e]))->e) > 16); | |
DUMPBITS(t->b) | |
n = t->v.n; | |
if (e) /* get length extra bits */ | |
{ | |
NEEDBITS(8) | |
n += (unsigned)b & 0xff; | |
DUMPBITS(8) | |
} | |
/* do the copy */ | |
s -= n; | |
do { | |
n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e); | |
if (u && w <= d) | |
{ | |
memset(slide + w, 0, e); | |
w += e; | |
d += e; | |
} | |
else | |
#ifndef NOMEMCPY | |
if (w - d >= e) /* (this test assumes unsigned comparison) */ | |
{ | |
memcpy(slide + w, slide + d, e); | |
w += e; | |
d += e; | |
} | |
else /* do it slow to avoid memcpy() overlap */ | |
#endif /* !NOMEMCPY */ | |
do { | |
slide[w++] = slide[d++]; | |
} while (--e); | |
if (w == WSIZE) | |
{ | |
flush(w); | |
w = u = 0; | |
} | |
} while (n); | |
} | |
} | |
/* flush out slide */ | |
flush(w); | |
return csize ? 5 : 0; /* should have read csize bytes */ | |
} | |
int explode() | |
/* Explode an imploded compressed stream. Based on the general purpose | |
bit flag, decide on coded or uncoded literals, and an 8K or 4K sliding | |
window. Construct the literal (if any), length, and distance codes and | |
the tables needed to decode them (using huft_build() from inflate.c), | |
and call the appropriate routine for the type of data in the remainder | |
of the stream. The four routines are nearly identical, differing only | |
in whether the literal is decoded or simply read in, and in how many | |
bits are read in, uncoded, for the low distance bits. */ | |
{ | |
unsigned r; /* return codes */ | |
struct huft *tb; /* literal code table */ | |
struct huft *tl; /* length code table */ | |
struct huft *td; /* distance code table */ | |
int bb; /* bits for tb */ | |
int bl; /* bits for tl */ | |
int bd; /* bits for td */ | |
unsigned l[256]; /* bit lengths for codes */ | |
/* Tune base table sizes. Note: I thought that to truly optimize speed, | |
I would have to select different bl, bd, and bb values for different | |
compressed file sizes. I was suprised to find out the the values of | |
7, 7, and 9 worked best over a very wide range of sizes, except that | |
bd = 8 worked marginally better for large compressed sizes. */ | |
bl = 7; | |
bd = csize > 200000L ? 8 : 7; | |
/* With literal tree--minimum match length is 3 */ | |
hufts = 0; /* initialze huft's malloc'ed */ | |
if (lrec.general_purpose_bit_flag & 4) | |
{ | |
bb = 9; /* base table size for literals */ | |
if ((r = get_tree(l, 256)) != 0) | |
return r; | |
if ((r = huft_build(l, 256, 256, NULL, NULL, &tb, &bb)) != 0) | |
{ | |
if (r == 1) | |
huft_free(tb); | |
return r; | |
} | |
if ((r = get_tree(l, 64)) != 0) | |
return r; | |
if ((r = huft_build(l, 64, 0, cplen3, extra, &tl, &bl)) != 0) | |
{ | |
if (r == 1) | |
huft_free(tl); | |
huft_free(tb); | |
return r; | |
} | |
if ((r = get_tree(l, 64)) != 0) | |
return r; | |
if (lrec.general_purpose_bit_flag & 2) /* true if 8K */ | |
{ | |
if ((r = huft_build(l, 64, 0, cpdist8, extra, &td, &bd)) != 0) | |
{ | |
if (r == 1) | |
huft_free(td); | |
huft_free(tl); | |
huft_free(tb); | |
return r; | |
} | |
r = explode_lit8(tb, tl, td, bb, bl, bd); | |
} | |
else /* else 4K */ | |
{ | |
if ((r = huft_build(l, 64, 0, cpdist4, extra, &td, &bd)) != 0) | |
{ | |
if (r == 1) | |
huft_free(td); | |
huft_free(tl); | |
huft_free(tb); | |
return r; | |
} | |
r = explode_lit4(tb, tl, td, bb, bl, bd); | |
} | |
huft_free(td); | |
huft_free(tl); | |
huft_free(tb); | |
} | |
else | |
/* No literal tree--minimum match length is 2 */ | |
{ | |
if ((r = get_tree(l, 64)) != 0) | |
return r; | |
if ((r = huft_build(l, 64, 0, cplen2, extra, &tl, &bl)) != 0) | |
{ | |
if (r == 1) | |
huft_free(tl); | |
return r; | |
} | |
if ((r = get_tree(l, 64)) != 0) | |
return r; | |
if (lrec.general_purpose_bit_flag & 2) /* true if 8K */ | |
{ | |
if ((r = huft_build(l, 64, 0, cpdist8, extra, &td, &bd)) != 0) | |
{ | |
if (r == 1) | |
huft_free(td); | |
huft_free(tl); | |
return r; | |
} | |
r = explode_nolit8(tl, td, bl, bd); | |
} | |
else /* else 4K */ | |
{ | |
if ((r = huft_build(l, 64, 0, cpdist4, extra, &td, &bd)) != 0) | |
{ | |
if (r == 1) | |
huft_free(td); | |
huft_free(tl); | |
return r; | |
} | |
r = explode_nolit4(tl, td, bl, bd); | |
} | |
huft_free(td); | |
huft_free(tl); | |
} | |
#ifdef DEBUG | |
fprintf(stderr, "<%u > ", hufts); | |
#endif /* DEBUG */ | |
return r; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* inflate.c -- Not copyrighted 1992 by Mark Adler | |
version c10p1, 10 January 1993 */ | |
/* You can do whatever you like with this source file, though I would | |
prefer that if you modify it and redistribute it that you include | |
comments to that effect with your name and the date. Thank you. | |
History: | |
vers date who what | |
---- --------- -------------- ------------------------------------ | |
a ~~ Feb 92 M. Adler used full (large, one-step) lookup table | |
b1 21 Mar 92 M. Adler first version with partial lookup tables | |
b2 21 Mar 92 M. Adler fixed bug in fixed-code blocks | |
b3 22 Mar 92 M. Adler sped up match copies, cleaned up some | |
b4 25 Mar 92 M. Adler added prototypes; removed window[] (now | |
is the responsibility of unzip.h--also | |
changed name to slide[]), so needs diffs | |
for unzip.c and unzip.h (this allows | |
compiling in the small model on MSDOS); | |
fixed cast of q in huft_build(); | |
b5 26 Mar 92 M. Adler got rid of unintended macro recursion. | |
b6 27 Mar 92 M. Adler got rid of nextbyte() routine. fixed | |
bug in inflate_fixed(). | |
c1 30 Mar 92 M. Adler removed lbits, dbits environment variables. | |
changed BMAX to 16 for explode. Removed | |
OUTB usage, and replaced it with flush()-- | |
this was a 20% speed improvement! Added | |
an explode.c (to replace unimplode.c) that | |
uses the huft routines here. Removed | |
register union. | |
c2 4 Apr 92 M. Adler fixed bug for file sizes a multiple of 32k. | |
c3 10 Apr 92 M. Adler reduced memory of code tables made by | |
huft_build significantly (factor of two to | |
three). | |
c4 15 Apr 92 M. Adler added NOMEMCPY do kill use of memcpy(). | |
worked around a Turbo C optimization bug. | |
c5 21 Apr 92 M. Adler added the WSIZE #define to allow reducing | |
the 32K window size for specialized | |
applications. | |
c6 31 May 92 M. Adler added some typecasts to eliminate warnings | |
c7 27 Jun 92 G. Roelofs added some more typecasts (444: MSC bug). | |
c8 5 Oct 92 J-l. Gailly added ifdef'd code to deal with PKZIP bug. | |
c9 9 Oct 92 M. Adler removed a memory error message (~line 416). | |
c10 17 Oct 92 G. Roelofs changed ULONG/UWORD/byte to ulg/ush/uch, | |
removed old inflate, renamed inflate_entry | |
to inflate, added Mark's fix to a comment. | |
c10p1 10 Jan 93 G. Roelofs version c10 plus Mark's c13 patch: | |
[c13] M. Adler allow empty code sets in huft_build (the | |
new pkz204c.exe file has a null distance | |
tree for the file pkzip.exe) | |
*/ | |
/* | |
Inflate deflated (PKZIP's method 8 compressed) data. The compression | |
method searches for as much of the current string of bytes (up to a | |
length of 258) in the previous 32K bytes. If it doesn't find any | |
matches (of at least length 3), it codes the next byte. Otherwise, it | |
codes the length of the matched string and its distance backwards from | |
the current position. There is a single Huffman code that codes both | |
single bytes (called "literals") and match lengths. A second Huffman | |
code codes the distance information, which follows a length code. Each | |
length or distance code actually represents a base value and a number | |
of "extra" (sometimes zero) bits to get to add to the base value. At | |
the end of each deflated block is a special end-of-block (EOB) literal/ | |
length code. The decoding process is basically: get a literal/length | |
code; if EOB then done; if a literal, emit the decoded byte; if a | |
length then get the distance and emit the referred-to bytes from the | |
sliding window of previously emitted data. | |
There are (currently) three kinds of inflate blocks: stored, fixed, and | |
dynamic. The compressor outputs a chunk of data at a time, and decides | |
which method to use on a chunk-by-chunk basis. A chunk might typically | |
be 32K to 64K, uncompressed. If the chunk is uncompressible, then the | |
"stored" method is used. In this case, the bytes are simply stored as | |
is, eight bits per byte, with none of the above coding. The bytes are | |
preceded by a count, since there is no longer an EOB code. | |
If the data is compressible, then either the fixed or dynamic methods | |
are used. In the dynamic method, the compressed data is preceded by | |
an encoding of the literal/length and distance Huffman codes that are | |
to be used to decode this block. The representation is itself Huffman | |
coded, and so is preceded by a description of that code. These code | |
descriptions take up a little space, and so for small blocks, there is | |
a predefined set of codes, called the fixed codes. The fixed method is | |
used if the block ends up smaller that way (usually for quite small | |
chunks), otherwise the dynamic method is used. In the latter case, the | |
codes are customized to the probabilities in the current block, and so | |
can code it much better than the pre-determined fixed codes can. | |
The Huffman codes themselves are decoded using a mutli-level table | |
lookup, in order to maximize the speed of decoding plus the speed of | |
building the decoding tables. See the comments below that precede the | |
lbits and dbits tuning parameters. | |
*/ | |
/* | |
Notes beyond the 1.93a appnote.txt: | |
1. Distance pointers never point before the beginning of the output | |
stream. | |
2. Distance pointers can point back across blocks, up to 32k away. | |
3. There is an implied maximum of 7 bits for the bit length table and | |
15 bits for the actual data. | |
4. If only one code exists, then it is encoded using one bit. (Zero | |
would be more efficient, but perhaps a little confusing.) If two | |
codes exist, they are coded using one bit each (0 and 1). | |
5. There is no way of sending zero distance codes--a dummy must be | |
sent if there are none. (History: a pre 2.0 version of PKZIP would | |
store blocks with no distance codes, but this was discovered to be | |
too harsh a criterion.) Valid only for 1.93a. 2.04c does allow | |
zero distance codes, which is sent as one code of zero bits in | |
length. | |
6. There are up to 286 literal/length codes. Code 256 represents the | |
end-of-block. Note however that the static length tree defines | |
288 codes just to fill out the Huffman codes. Codes 286 and 287 | |
cannot be used though, since there is no length base or extra bits | |
defined for them. Similarily, there are up to 30 distance codes. | |
However, static trees define 32 codes (all 5 bits) to fill out the | |
Huffman codes, but the last two had better not show up in the data. | |
7. Unzip can check dynamic Huffman blocks for complete code sets. | |
The exception is that a single code would not be complete (see #4). | |
8. The five bits following the block type is really the number of | |
literal codes sent minus 257. | |
9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits | |
(1+6+6). Therefore, to output three times the length, you output | |
three codes (1+1+1), whereas to output four times the same length, | |
you only need two codes (1+3). Hmm. | |
10. In the tree reconstruction algorithm, Code = Code + Increment | |
only if BitLength(i) is not zero. (Pretty obvious.) | |
11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19) | |
12. Note: length code 284 can represent 227-258, but length code 285 | |
really is 258. The last length deserves its own, short code | |
since it gets used a lot in very redundant files. The length | |
258 is special since 258 - 3 (the min match length) is 255. | |
13. The literal/length and distance code bit lengths are read as a | |
single stream of lengths. It is possible (and advantageous) for | |
a repeat code (16, 17, or 18) to go across the boundary between | |
the two sets of lengths. | |
*/ | |
#include "unzip.h" /* this must supply the slide[] (uch) array */ | |
#ifndef WSIZE | |
# define WSIZE 0x8000 /* window size--must be a power of two, and at least | |
32K for zip's deflate method */ | |
#endif /* !WSIZE */ | |
#ifdef DEBUG | |
# define Trace(x) fprintf x | |
#else | |
# define Trace(x) | |
#endif | |
/* Huffman code lookup table entry--this entry is four bytes for machines | |
that have 16-bit pointers (e.g. PC's in the small or medium model). | |
Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16 | |
means that v is a literal, 16 < e < 32 means that v is a pointer to | |
the next table, which codes e - 16 bits, and lastly e == 99 indicates | |
an unused code. If a code with e == 99 is looked up, this implies an | |
error in the data. */ | |
struct huft { | |
uch e; /* number of extra bits or operation */ | |
uch b; /* number of bits in this code or subcode */ | |
union { | |
ush n; /* literal, length base, or distance base */ | |
struct huft *t; /* pointer to next level of table */ | |
} v; | |
}; | |
/* Function prototypes */ | |
int huft_build OF((unsigned *, unsigned, unsigned, ush *, ush *, | |
struct huft **, int *)); | |
int huft_free OF((struct huft *)); | |
void flush OF((unsigned)); | |
int inflate_codes OF((struct huft *, struct huft *, int, int)); | |
int inflate_stored OF((void)); | |
int inflate_fixed OF((void)); | |
int inflate_dynamic OF((void)); | |
int inflate_block OF((int *)); | |
int inflate OF((void)); | |
/* The inflate algorithm uses a sliding 32K byte window on the uncompressed | |
stream to find repeated byte strings. This is implemented here as a | |
circular buffer. The index is updated simply by incrementing and then | |
and'ing with 0x7fff (32K-1). */ | |
/* It is left to other modules to supply the 32K area. It is assumed | |
to be usable as if it were declared "uch slide[32768];" or as just | |
"uch *slide;" and then malloc'ed in the latter case. The definition | |
must be in unzip.h, included above. */ | |
unsigned wp; /* current position in slide */ | |
/* Tables for deflate from PKZIP's appnote.txt. */ | |
static unsigned border[] = { /* Order of the bit length code lengths */ | |
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; | |
static ush cplens[] = { /* Copy lengths for literal codes 257..285 */ | |
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, | |
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; | |
/* note: see note #13 above about the 258 in this list. */ | |
static ush cplext[] = { /* Extra bits for literal codes 257..285 */ | |
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, | |
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */ | |
static ush cpdist[] = { /* Copy offsets for distance codes 0..29 */ | |
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, | |
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, | |
8193, 12289, 16385, 24577}; | |
static ush cpdext[] = { /* Extra bits for distance codes */ | |
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, | |
7, 7, 8, 8, 9, 9, 10, 10, 11, 11, | |
12, 12, 13, 13}; | |
/* Macros for inflate() bit peeking and grabbing. | |
The usage is: | |
NEEDBITS(j) | |
x = b & mask_bits[j]; | |
DUMPBITS(j) | |
where NEEDBITS makes sure that b has at least j bits in it, and | |
DUMPBITS removes the bits from b. The macros use the variable k | |
for the number of bits in b. Normally, b and k are register | |
variables for speed, and are initialized at the begining of a | |
routine that uses these macros from a global bit buffer and count. | |
If we assume that EOB will be the longest code, then we will never | |
ask for bits with NEEDBITS that are beyond the end of the stream. | |
So, NEEDBITS should not read any more bytes than are needed to | |
meet the request. Then no bytes need to be "returned" to the buffer | |
at the end of the last block. | |
However, this assumption is not true for fixed blocks--the EOB code | |
is 7 bits, but the other literal/length codes can be 8 or 9 bits. | |
(The EOB code is shorter than other codes becuase fixed blocks are | |
generally short. So, while a block always has an EOB, many other | |
literal/length codes have a significantly lower probability of | |
showing up at all.) However, by making the first table have a | |
lookup of seven bits, the EOB code will be found in that first | |
lookup, and so will not require that too many bits be pulled from | |
the stream. | |
*/ | |
ulg bb; /* bit buffer */ | |
unsigned bk; /* bits in bit buffer */ | |
ush bytebuf; | |
#define NEXTBYTE (ReadByte(&bytebuf), bytebuf) | |
#define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE)<<k;k+=8;}} | |
#define DUMPBITS(n) {b>>=(n);k-=(n);} | |
/* | |
Huffman code decoding is performed using a multi-level table lookup. | |
The fastest way to decode is to simply build a lookup table whose | |
size is determined by the longest code. However, the time it takes | |
to build this table can also be a factor if the data being decoded | |
is not very long. The most common codes are necessarily the | |
shortest codes, so those codes dominate the decoding time, and hence | |
the speed. The idea is you can have a shorter table that decodes the | |
shorter, more probable codes, and then point to subsidiary tables for | |
the longer codes. The time it costs to decode the longer codes is | |
then traded against the time it takes to make longer tables. | |
This results of this trade are in the variables lbits and dbits | |
below. lbits is the number of bits the first level table for literal/ | |
length codes can decode in one step, and dbits is the same thing for | |
the distance codes. Subsequent tables are also less than or equal to | |
those sizes. These values may be adjusted either when all of the | |
codes are shorter than that, in which case the longest code length in | |
bits is used, or when the shortest code is *longer* than the requested | |
table size, in which case the length of the shortest code in bits is | |
used. | |
There are two different values for the two tables, since they code a | |
different number of possibilities each. The literal/length table | |
codes 286 possible values, or in a flat code, a little over eight | |
bits. The distance table codes 30 possible values, or a little less | |
than five bits, flat. The optimum values for speed end up being | |
about one bit more than those, so lbits is 8+1 and dbits is 5+1. | |
The optimum values may differ though from machine to machine, and | |
possibly even between compilers. Your mileage may vary. | |
*/ | |
int lbits = 9; /* bits in base literal/length lookup table */ | |
int dbits = 6; /* bits in base distance lookup table */ | |
/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ | |
#define BMAX 16 /* maximum bit length of any code (16 for explode) */ | |
#define N_MAX 288 /* maximum number of codes in any set */ | |
unsigned hufts; /* track memory usage */ | |
int huft_build(b, n, s, d, e, t, m) | |
unsigned *b; /* code lengths in bits (all assumed <= BMAX) */ | |
unsigned n; /* number of codes (assumed <= N_MAX) */ | |
unsigned s; /* number of simple-valued codes (0..s-1) */ | |
ush *d; /* list of base values for non-simple codes */ | |
ush *e; /* list of extra bits for non-simple codes */ | |
struct huft **t; /* result: starting table */ | |
int *m; /* maximum lookup bits, returns actual */ | |
/* Given a list of code lengths and a maximum table size, make a set of | |
tables to decode that set of codes. Return zero on success, one if | |
the given code set is incomplete (the tables are still built in this | |
case), two if the input is invalid (all zero length codes or an | |
oversubscribed set of lengths), and three if not enough memory. */ | |
{ | |
unsigned a; /* counter for codes of length k */ | |
unsigned c[BMAX+1]; /* bit length count table */ | |
unsigned f; /* i repeats in table every f entries */ | |
int g; /* maximum code length */ | |
int h; /* table level */ | |
register unsigned i; /* counter, current code */ | |
register unsigned j; /* counter */ | |
register int k; /* number of bits in current code */ | |
int l; /* bits per table (returned in m) */ | |
register unsigned *p; /* pointer into c[], b[], or v[] */ | |
register struct huft *q; /* points to current table */ | |
struct huft r; /* table entry for structure assignment */ | |
struct huft *u[BMAX]; /* table stack */ | |
unsigned v[N_MAX]; /* values in order of bit length */ | |
register int w; /* bits before this table == (l * h) */ | |
unsigned x[BMAX+1]; /* bit offsets, then code stack */ | |
unsigned *xp; /* pointer into x */ | |
int y; /* number of dummy codes added */ | |
unsigned z; /* number of entries in current table */ | |
/* Generate counts for each bit length */ | |
memset(c, 0, sizeof(c)); | |
p = b; i = n; | |
do { | |
c[*p++]++; /* assume all entries <= BMAX */ | |
} while (--i); | |
if (c[0] == n) /* null input--all zero length codes */ | |
{ | |
*t = (struct huft *)NULL; | |
*m = 0; | |
return 0; | |
} | |
/* Find minimum and maximum length, bound *m by those */ | |
l = *m; | |
for (j = 1; j <= BMAX; j++) | |
if (c[j]) | |
break; | |
k = j; /* minimum code length */ | |
if ((unsigned)l < j) | |
l = j; | |
for (i = BMAX; i; i--) | |
if (c[i]) | |
break; | |
g = i; /* maximum code length */ | |
if ((unsigned)l > i) | |
l = i; | |
*m = l; | |
/* Adjust last length count to fill out codes, if needed */ | |
for (y = 1 << j; j < i; j++, y <<= 1) | |
if ((y -= c[j]) < 0) | |
return 2; /* bad input: more codes than bits */ | |
if ((y -= c[i]) < 0) | |
return 2; | |
c[i] += y; | |
/* Generate starting offsets into the value table for each length */ | |
x[1] = j = 0; | |
p = c + 1; xp = x + 2; | |
while (--i) { /* note that i == g from above */ | |
*xp++ = (j += *p++); | |
} | |
/* Make a table of values in order of bit lengths */ | |
p = b; i = 0; | |
do { | |
if ((j = *p++) != 0) | |
v[x[j]++] = i; | |
} while (++i < n); | |
/* Generate the Huffman codes and for each, make the table entries */ | |
x[0] = i = 0; /* first Huffman code is zero */ | |
p = v; /* grab values in bit order */ | |
h = -1; /* no tables yet--level -1 */ | |
w = -l; /* bits decoded == (l * h) */ | |
u[0] = (struct huft *)NULL; /* just to keep compilers happy */ | |
q = (struct huft *)NULL; /* ditto */ | |
z = 0; /* ditto */ | |
/* go through the bit lengths (k already is bits in shortest code) */ | |
for (; k <= g; k++) | |
{ | |
a = c[k]; | |
while (a--) | |
{ | |
/* here i is the Huffman code of length k bits for value *p */ | |
/* make tables up to required level */ | |
while (k > w + l) | |
{ | |
h++; | |
w += l; /* previous table always l bits */ | |
/* compute minimum size table less than or equal to l bits */ | |
z = (z = g - w) > (unsigned)l ? l : z; /* upper limit on table size */ | |
if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */ | |
{ /* too few codes for k-w bit table */ | |
f -= a + 1; /* deduct codes from patterns left */ | |
xp = c + k; | |
while (++j < z) /* try smaller tables up to z bits */ | |
{ | |
if ((f <<= 1) <= *++xp) | |
break; /* enough codes to use up j bits */ | |
f -= *xp; /* else deduct codes from patterns */ | |
} | |
} | |
z = 1 << j; /* table entries for j-bit table */ | |
/* allocate and link in new table */ | |
if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) == | |
(struct huft *)NULL) | |
{ | |
if (h) | |
huft_free(u[0]); | |
return 3; /* not enough memory */ | |
} | |
hufts += z + 1; /* track memory usage */ | |
*t = q + 1; /* link to list for huft_free() */ | |
*(t = &(q->v.t)) = (struct huft *)NULL; | |
u[h] = ++q; /* table starts after link */ | |
/* connect to last table, if there is one */ | |
if (h) | |
{ | |
x[h] = i; /* save pattern for backing up */ | |
r.b = (uch)l; /* bits to dump before this table */ | |
r.e = (uch)(16 + j); /* bits in this table */ | |
r.v.t = q; /* pointer to this table */ | |
j = i >> (w - l); /* (get around Turbo C bug) */ | |
u[h-1][j] = r; /* connect to last table */ | |
} | |
} | |
/* set up table entry in r */ | |
r.b = (uch)(k - w); | |
if (p >= v + n) | |
r.e = 99; /* out of values--invalid code */ | |
else if (*p < s) | |
{ | |
r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */ | |
r.v.n = *p++; /* simple code is just the value */ | |
} | |
else | |
{ | |
r.e = (uch)e[*p - s]; /* non-simple--look up in lists */ | |
r.v.n = d[*p++ - s]; | |
} | |
/* fill code-like entries with r */ | |
f = 1 << (k - w); | |
for (j = i >> w; j < z; j += f) | |
q[j] = r; | |
/* backwards increment the k-bit code i */ | |
for (j = 1 << (k - 1); i & j; j >>= 1) | |
i ^= j; | |
i ^= j; | |
/* backup over finished tables */ | |
while ((i & ((1 << w) - 1)) != x[h]) | |
{ | |
h--; /* don't need to update q */ | |
w -= l; | |
} | |
} | |
} | |
/* Return true (1) if we were given an incomplete table */ | |
return y != 0 && g != 1; | |
} | |
int huft_free(t) | |
struct huft *t; /* table to free */ | |
/* Free the malloc'ed tables built by huft_build(), which makes a linked | |
list of the tables it made, with the links in a dummy first entry of | |
each table. */ | |
{ | |
register struct huft *p, *q; | |
/* Go through linked list, freeing from the malloced (t[-1]) address. */ | |
p = t; | |
while (p != (struct huft *)NULL) | |
{ | |
q = (--p)->v.t; | |
free(p); | |
p = q; | |
} | |
return 0; | |
} | |
void flush(w) | |
unsigned w; /* number of bytes to flush */ | |
/* Do the equivalent of OUTB for the bytes slide[0..w-1]. */ | |
{ | |
unsigned n; | |
uch *p; | |
p = slide; | |
while (w) | |
{ | |
n = (n = OUTBUFSIZ - outcnt) < w ? n : w; | |
memcpy(outptr, p, n); /* try to fill up buffer */ | |
outptr += n; | |
if ((outcnt += n) == OUTBUFSIZ) | |
FlushOutput(); /* if full, empty */ | |
p += n; | |
w -= n; | |
} | |
} | |
int inflate_codes(tl, td, bl, bd) | |
struct huft *tl, *td; /* literal/length and distance decoder tables */ | |
int bl, bd; /* number of bits decoded by tl[] and td[] */ | |
/* inflate (decompress) the codes in a deflated (compressed) block. | |
Return an error code or zero if it all goes ok. */ | |
{ | |
register unsigned e; /* table entry flag/number of extra bits */ | |
unsigned n, d; /* length and index for copy */ | |
unsigned w; /* current window position */ | |
struct huft *t; /* pointer to table entry */ | |
unsigned ml, md; /* masks for bl and bd bits */ | |
register ulg b; /* bit buffer */ | |
register unsigned k; /* number of bits in bit buffer */ | |
/* make local copies of globals */ | |
b = bb; /* initialize bit buffer */ | |
k = bk; | |
w = wp; /* initialize window position */ | |
/* inflate the coded data */ | |
ml = mask_bits[bl]; /* precompute masks for speed */ | |
md = mask_bits[bd]; | |
while (1) /* do until end of block */ | |
{ | |
NEEDBITS((unsigned)bl) | |
if ((e = (t = tl + ((unsigned)b & ml))->e) > 16) | |
do { | |
if (e == 99) | |
return 1; | |
DUMPBITS(t->b) | |
e -= 16; | |
NEEDBITS(e) | |
} while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16); | |
DUMPBITS(t->b) | |
if (e == 16) /* then it's a literal */ | |
{ | |
slide[w++] = (uch)t->v.n; | |
if (w == WSIZE) | |
{ | |
flush(w); | |
w = 0; | |
} | |
} | |
else /* it's an EOB or a length */ | |
{ | |
/* exit if end of block */ | |
if (e == 15) | |
break; | |
/* get length of block to copy */ | |
NEEDBITS(e) | |
n = t->v.n + ((unsigned)b & mask_bits[e]); | |
DUMPBITS(e); | |
/* decode distance of block to copy */ | |
NEEDBITS((unsigned)bd) | |
if ((e = (t = td + ((unsigned)b & md))->e) > 16) | |
do { | |
if (e == 99) | |
return 1; | |
DUMPBITS(t->b) | |
e -= 16; | |
NEEDBITS(e) | |
} while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16); | |
DUMPBITS(t->b) | |
NEEDBITS(e) | |
d = w - t->v.n - ((unsigned)b & mask_bits[e]); | |
DUMPBITS(e) | |
/* do the copy */ | |
do { | |
n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e); | |
#ifndef NOMEMCPY | |
if (w - d >= e) /* (this test assumes unsigned comparison) */ | |
{ | |
memcpy(slide + w, slide + d, e); | |
w += e; | |
d += e; | |
} | |
else /* do it slow to avoid memcpy() overlap */ | |
#endif /* !NOMEMCPY */ | |
do { | |
slide[w++] = slide[d++]; | |
} while (--e); | |
if (w == WSIZE) | |
{ | |
flush(w); | |
w = 0; | |
} | |
} while (n); | |
} | |
} | |
/* restore the globals from the locals */ | |
wp = w; /* restore global window pointer */ | |
bb = b; /* restore global bit buffer */ | |
bk = k; | |
/* done */ | |
return 0; | |
} | |
int inflate_stored() | |
/* "decompress" an inflated type 0 (stored) block. */ | |
{ | |
unsigned n; /* number of bytes in block */ | |
unsigned w; /* current window position */ | |
register ulg b; /* bit buffer */ | |
register unsigned k; /* number of bits in bit buffer */ | |
/* make local copies of globals */ | |
b = bb; /* initialize bit buffer */ | |
k = bk; | |
w = wp; /* initialize window position */ | |
/* go to byte boundary */ | |
n = k & 7; | |
DUMPBITS(n); | |
/* get the length and its complement */ | |
NEEDBITS(16) | |
n = ((unsigned)b & 0xffff); | |
DUMPBITS(16) | |
NEEDBITS(16) | |
if (n != (unsigned)((~b) & 0xffff)) | |
return 1; /* error in compressed data */ | |
DUMPBITS(16) | |
/* read and output the compressed data */ | |
while (n--) | |
{ | |
NEEDBITS(8) | |
slide[w++] = (uch)b; | |
if (w == WSIZE) | |
{ | |
flush(w); | |
w = 0; | |
} | |
DUMPBITS(8) | |
} | |
/* restore the globals from the locals */ | |
wp = w; /* restore global window pointer */ | |
bb = b; /* restore global bit buffer */ | |
bk = k; | |
return 0; | |
} | |
int inflate_fixed() | |
/* decompress an inflated type 1 (fixed Huffman codes) block. We should | |
either replace this with a custom decoder, or at least precompute the | |
Huffman tables. */ | |
{ | |
int i; /* temporary variable */ | |
struct huft *tl; /* literal/length code table */ | |
struct huft *td; /* distance code table */ | |
int bl; /* lookup bits for tl */ | |
int bd; /* lookup bits for td */ | |
unsigned l[288]; /* length list for huft_build */ | |
/* set up literal table */ | |
for (i = 0; i < 144; i++) | |
l[i] = 8; | |
for (; i < 256; i++) | |
l[i] = 9; | |
for (; i < 280; i++) | |
l[i] = 7; | |
for (; i < 288; i++) /* make a complete, but wrong code set */ | |
l[i] = 8; | |
bl = 7; | |
if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) | |
return i; | |
/* set up distance table */ | |
for (i = 0; i < 30; i++) /* make an incomplete code set */ | |
l[i] = 5; | |
bd = 5; | |
if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) | |
{ | |
huft_free(tl); | |
return i; | |
} | |
/* decompress until an end-of-block code */ | |
if (inflate_codes(tl, td, bl, bd)) | |
return 1; | |
/* free the decoding tables, return */ | |
huft_free(tl); | |
huft_free(td); | |
return 0; | |
} | |
int inflate_dynamic() | |
/* decompress an inflated type 2 (dynamic Huffman codes) block. */ | |
{ | |
int i; /* temporary variables */ | |
unsigned j; | |
unsigned l; /* last length */ | |
unsigned m; /* mask for bit lengths table */ | |
unsigned n; /* number of lengths to get */ | |
struct huft *tl; /* literal/length code table */ | |
struct huft *td; /* distance code table */ | |
int bl; /* lookup bits for tl */ | |
int bd; /* lookup bits for td */ | |
unsigned nb; /* number of bit length codes */ | |
unsigned nl; /* number of literal/length codes */ | |
unsigned nd; /* number of distance codes */ | |
#ifdef PKZIP_BUG_WORKAROUND | |
unsigned ll[288+32]; /* literal/length and distance code lengths */ | |
#else | |
unsigned ll[286+30]; /* literal/length and distance code lengths */ | |
#endif | |
register ulg b; /* bit buffer */ | |
register unsigned k; /* number of bits in bit buffer */ | |
/* make local bit buffer */ | |
b = bb; | |
k = bk; | |
/* read in table lengths */ | |
NEEDBITS(5) | |
nl = 257 + ((unsigned)b & 0x1f); /* number of literal/length codes */ | |
DUMPBITS(5) | |
NEEDBITS(5) | |
nd = 1 + ((unsigned)b & 0x1f); /* number of distance codes */ | |
DUMPBITS(5) | |
NEEDBITS(4) | |
nb = 4 + ((unsigned)b & 0xf); /* number of bit length codes */ | |
DUMPBITS(4) | |
#ifdef PKZIP_BUG_WORKAROUND | |
if (nl > 288 || nd > 32) | |
#else | |
if (nl > 286 || nd > 30) | |
#endif | |
return 1; /* bad lengths */ | |
/* read in bit-length-code lengths */ | |
for (j = 0; j < nb; j++) | |
{ | |
NEEDBITS(3) | |
ll[border[j]] = (unsigned)b & 7; | |
DUMPBITS(3) | |
} | |
for (; j < 19; j++) | |
ll[border[j]] = 0; | |
/* build decoding table for trees--single level, 7 bit lookup */ | |
bl = 7; | |
if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) | |
{ | |
if (i == 1) | |
huft_free(tl); | |
return i; /* incomplete code set */ | |
} | |
/* read in literal and distance code lengths */ | |
n = nl + nd; | |
m = mask_bits[bl]; | |
i = l = 0; | |
while ((unsigned)i < n) | |
{ | |
NEEDBITS((unsigned)bl) | |
j = (td = tl + ((unsigned)b & m))->b; | |
DUMPBITS(j) | |
j = td->v.n; | |
if (j < 16) /* length of code in bits (0..15) */ | |
ll[i++] = l = j; /* save last length in l */ | |
else if (j == 16) /* repeat last length 3 to 6 times */ | |
{ | |
NEEDBITS(2) | |
j = 3 + ((unsigned)b & 3); | |
DUMPBITS(2) | |
if ((unsigned)i + j > n) | |
return 1; | |
while (j--) | |
ll[i++] = l; | |
} | |
else if (j == 17) /* 3 to 10 zero length codes */ | |
{ | |
NEEDBITS(3) | |
j = 3 + ((unsigned)b & 7); | |
DUMPBITS(3) | |
if ((unsigned)i + j > n) | |
return 1; | |
while (j--) | |
ll[i++] = 0; | |
l = 0; | |
} | |
else /* j == 18: 11 to 138 zero length codes */ | |
{ | |
NEEDBITS(7) | |
j = 11 + ((unsigned)b & 0x7f); | |
DUMPBITS(7) | |
if ((unsigned)i + j > n) | |
return 1; | |
while (j--) | |
ll[i++] = 0; | |
l = 0; | |
} | |
} | |
/* free decoding table for trees */ | |
huft_free(tl); | |
/* restore the global bit buffer */ | |
bb = b; | |
bk = k; | |
/* build the decoding tables for literal/length and distance codes */ | |
bl = lbits; | |
if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) | |
{ | |
if (i == 1) { | |
fprintf(stderr, " incomplete literal tree\n"); | |
huft_free(tl); | |
} | |
return i; /* incomplete code set */ | |
} | |
bd = dbits; | |
if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) | |
{ | |
if (i == 1) { | |
fprintf(stderr, " incomplete distance tree\n"); | |
#ifdef PKZIP_BUG_WORKAROUND | |
i = 0; | |
} | |
#else | |
huft_free(td); | |
} | |
huft_free(tl); | |
return i; /* incomplete code set */ | |
#endif | |
} | |
/* decompress until an end-of-block code */ | |
if (inflate_codes(tl, td, bl, bd)) | |
return 1; | |
/* free the decoding tables, return */ | |
huft_free(tl); | |
huft_free(td); | |
return 0; | |
} | |
int inflate_block(e) | |
int *e; /* last block flag */ | |
/* decompress an inflated block */ | |
{ | |
unsigned t; /* block type */ | |
register ulg b; /* bit buffer */ | |
register unsigned k; /* number of bits in bit buffer */ | |
/* make local bit buffer */ | |
b = bb; | |
k = bk; | |
/* read in last block bit */ | |
NEEDBITS(1) | |
*e = (int)b & 1; | |
DUMPBITS(1) | |
/* read in block type */ | |
NEEDBITS(2) | |
t = (unsigned)b & 3; | |
DUMPBITS(2) | |
/* restore the global bit buffer */ | |
bb = b; | |
bk = k; | |
/* inflate that block type */ | |
if (t == 2) | |
return inflate_dynamic(); | |
if (t == 0) | |
return inflate_stored(); | |
if (t == 1) | |
return inflate_fixed(); | |
/* bad block type */ | |
return 2; | |
} | |
int inflate() | |
/* decompress an inflated entry */ | |
{ | |
int e; /* last block flag */ | |
int r; /* result code */ | |
unsigned h; /* maximum struct huft's malloc'ed */ | |
/* initialize window, bit buffer */ | |
wp = 0; | |
bk = 0; | |
bb = 0; | |
/* decompress until the last block */ | |
h = 0; | |
do { | |
hufts = 0; | |
if ((r = inflate_block(&e)) != 0) | |
{ | |
Trace((stderr, "\ninflate_block returned %d", r)); | |
return r; | |
} | |
if (hufts > h) | |
h = hufts; | |
} while (!e); | |
/* flush out slide */ | |
flush(wp); | |
/* return success */ | |
#ifdef DEBUG | |
fprintf(stderr, "<%u> ", h); | |
#endif /* DEBUG */ | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*--------------------------------------------------------------------------- | |
unzip.h | |
This header file is used by all of the unzip source files. Its contents | |
are divided into seven more-or-less separate sections: predefined macros, | |
OS-dependent includes, (mostly) OS-independent defines, typedefs, function | |
prototypes (or "prototypes," in the case of non-ANSI compilers), macros, | |
and global-variable declarations. | |
---------------------------------------------------------------------------*/ | |
/*****************************************/ | |
/* Predefined, Machine-specific Macros */ | |
/*****************************************/ | |
#if (defined(__GO32__) && defined(unix)) /* DOS extender */ | |
# undef unix | |
#endif | |
#if defined(unix) || defined(__convexc__) || defined(M_XENIX) | |
# ifndef UNIX | |
# define UNIX | |
# endif /* !UNIX */ | |
#endif /* unix || __convexc__ || M_XENIX */ | |
/* Much of the following is swiped from zip's tailor.h: */ | |
/* define MSDOS for Turbo C (unless OS/2) and Power C as well as Microsoft C */ | |
#ifdef __POWERC | |
# define __TURBOC__ | |
# define MSDOS | |
#endif /* __POWERC */ | |
#if (defined(__TURBOC__) && defined(__MSDOS__) && !defined(MSDOS)) | |
# define MSDOS | |
#endif | |
/* use prototypes and ANSI libraries if __STDC__, or Microsoft or Borland C, | |
* or Silicon Graphics, or Convex, or IBM C Set/2, or GNU gcc under emx, or | |
* or Watcom C, or Macintosh, or Windows NT. | |
*/ | |
#if (__STDC__ || defined(MSDOS) || defined(sgi) || defined(CONVEX)) | |
# ifndef PROTO | |
# define PROTO | |
# endif | |
# define MODERN | |
#endif | |
#if (defined(__IBMC__) || defined(__EMX__) || defined(__WATCOMC__)) | |
# ifndef PROTO | |
# define PROTO | |
# endif | |
# define MODERN | |
#endif | |
#if (defined(THINK_C) || defined(MPW) || defined(WIN32)) | |
# ifndef PROTO | |
# define PROTO | |
# endif | |
# define MODERN | |
#endif | |
/* turn off prototypes if requested */ | |
#if (defined(NOPROTO) && defined(PROTO)) | |
# undef PROTO | |
#endif | |
/* used to remove arguments in function prototypes for non-ANSI C */ | |
#ifdef PROTO | |
# define OF(a) a | |
#else /* !PROTO */ | |
# define OF(a) () | |
#endif /* ?PROTO */ | |
#if (defined(ultrix) || defined(bsd4_2) || defined(sun) || defined(pyr)) | |
# if (!defined(BSD) && !defined(__SYSTEM_FIVE) && !defined(SYSV)) | |
# define BSD | |
# endif /* !BSD && !__SYSTEM_FIVE && !SYSV */ | |
#endif /* ultrix || bsd4_2 || sun || pyr */ | |
#if (defined(CONVEX) || defined(CRAY) || defined(__SYSTEM_FIVE)) | |
# ifndef TERMIO | |
# define TERMIO | |
# endif /* !TERMIO */ | |
#endif /* CONVEX || CRAY || __SYSTEM_FIVE */ | |
#ifdef pyr /* Pyramid */ | |
# ifndef ZMEM | |
# define ZMEM | |
# endif /* !ZMEM */ | |
#endif /* pyr */ | |
#ifdef CRAY | |
# ifdef ZMEM | |
# undef ZMEM | |
# endif /* ZMEM */ | |
#endif /* CRAY */ | |
/* the i386 test below is to catch SCO Unix (which has redefinition | |
* warnings if param.h is included), but it probably doesn't hurt if | |
* other 386 Unixes get nailed, too...except now that 386BSD and BSDI | |
* exist. Sigh. <sys/param.h> is mostly included for "BSD", I think. | |
* [An alternate fix for SCO Unix is below.] | |
*/ | |
#if (defined(MINIX) || (defined(i386) && defined(unix))) | |
# define NO_PARAM_H | |
#endif /* MINIX || (i386 && unix) */ | |
/***************************/ | |
/* OS-Dependent Includes */ | |
/***************************/ | |
#ifndef MINIX /* Minix needs it after all the other includes (?) */ | |
# include <stdio.h> | |
#endif | |
#include <ctype.h> /* skip for VMS, to use tolower() function? */ | |
#include <errno.h> /* used in mapname() */ | |
#ifndef NO_ERRNO | |
# define DECLARE_ERRNO /* everybody except MSC 6.0, SCO cc, Watcom C/386 */ | |
#endif /* !NO_ERRNO */ | |
#ifdef VMS | |
# include <types.h> /* (placed up here instead of in VMS section below */ | |
# include <stat.h> /* because types.h is used in some other headers) */ | |
#else /* !VMS */ | |
# if !defined(THINK_C) && !defined(MPW) | |
# include <sys/types.h> /* off_t, time_t, dev_t, ... */ | |
# include <sys/stat.h> | |
# endif /* !THINK_C && !MPW */ | |
#endif /* ?VMS */ | |
#if defined(MODERN) || defined(__386BSD__) | |
# if (!defined(M_XENIX) && !(defined(__GNUC__) && defined(sun))) | |
# include <stddef.h> | |
# endif | |
# if (!defined(__GNUC__) && !defined(apollo)) /* both define __STDC__ */ | |
# include <stdlib.h> /* standard library prototypes, malloc(), etc. */ | |
# else | |
# ifdef __EMX__ | |
# include <stdlib.h> /* emx IS gcc but has stdlib.h */ | |
# endif | |
# endif | |
# ifndef __386BSD__ | |
# include <string.h> /* defines strcpy, strcmp, memcpy, etc. */ | |
# endif | |
typedef size_t extent; | |
typedef void voidp; | |
#else /* !MODERN */ | |
char *malloc(); | |
char *strchr(), *strrchr(); | |
long lseek(); | |
typedef unsigned int extent; | |
# define void int | |
typedef char voidp; | |
#endif /* ?MODERN */ | |
/* this include must be down here for SysV.4, for some reason... */ | |
#include <signal.h> /* used in unzip.c, file_io.c */ | |
/*--------------------------------------------------------------------------- | |
Next, a word from our Unix (mostly) sponsors: | |
---------------------------------------------------------------------------*/ | |
#ifdef UNIX | |
# ifdef AMIGA | |
# include <libraries/dos.h> | |
# else /* !AMIGA */ | |
# ifndef NO_PARAM_H | |
#if 0 /* [GRR: this is an alternate fix for SCO's redefinition bug] */ | |
# ifdef NGROUPS_MAX | |
# undef NGROUPS_MAX /* SCO bug: defined again in <param.h> */ | |
# endif /* NGROUPS_MAX */ | |
#endif /* 0 */ | |
# include <sys/param.h> /* conflict with <sys/types.h>, some systems? */ | |
# endif /* !NO_PARAM_H */ | |
# endif /* ?AMIGA */ | |
# ifndef BSIZE | |
# ifdef MINIX | |
# define BSIZE 1024 | |
# else /* !MINIX */ | |
# define BSIZE DEV_BSIZE /* assume common for all Unix systems */ | |
# endif /* ?MINIX */ | |
# endif | |
# ifndef BSD | |
# if (!defined(AMIGA) && !defined(MINIX)) | |
# define NO_MKDIR /* for mapname() */ | |
# endif /* !AMIGA && !MINIX */ | |
# include <time.h> | |
struct tm *gmtime(), *localtime(); | |
# else /* BSD */ | |
# include <sys/time.h> | |
# include <sys/timeb.h> | |
# ifdef _AIX | |
# include <time.h> | |
# endif | |
# endif | |
#else /* !UNIX */ | |
# define BSIZE 512 /* disk block size */ | |
#endif /* ?UNIX */ | |
#if (defined(V7) || defined(BSD)) | |
# define strchr index | |
# define strrchr rindex | |
#endif | |
/*--------------------------------------------------------------------------- | |
And now, our MS-DOS and OS/2 corner: | |
---------------------------------------------------------------------------*/ | |
#ifdef __TURBOC__ | |
# define DOS_OS2 | |
# include <sys/timeb.h> /* for structure ftime */ | |
# ifndef __BORLANDC__ /* there appears to be a bug (?) in Borland's */ | |
# include <mem.h> /* MEM.H related to __STDC__ and far poin- */ | |
# endif /* ters. (dpk) [mem.h included for memcpy] */ | |
# include <dos.h> /* for REGS macro (at least for Turbo C 2.0) */ | |
#else /* NOT Turbo C (or Power C)... */ | |
# ifdef MSDOS /* but still MS-DOS, so we'll assume it's */ | |
# ifndef MSC /* Microsoft's compiler and fake the ID, if */ | |
# define MSC /* necessary (it is in 5.0; apparently not */ | |
# endif /* in 5.1 and 6.0) */ | |
# include <dos.h> /* for _dos_setftime() */ | |
# endif | |
#endif | |
#if (defined(__IBMC__) && defined(__OS2__)) | |
# define DOS_OS2 | |
# define S_IFMT 0xF000 | |
# define timezone _timezone | |
#endif | |
#ifdef __WATCOMC__ | |
# define DOS_OS2 | |
# define __32BIT__ | |
# ifdef DECLARE_ERRNO | |
# undef DECLARE_ERRNO | |
# endif | |
# undef far | |
# define far | |
#endif | |
#ifdef __EMX__ | |
# define DOS_OS2 | |
# define __32BIT__ | |
# define far | |
#endif /* __EMX__ */ | |
#ifdef MSC /* defined for all versions of MSC now */ | |
# define DOS_OS2 /* Turbo C under DOS, MSC under DOS or OS/2 */ | |
# if (defined(_MSC_VER) && (_MSC_VER >= 600)) /* new with 5.1 or 6.0 ... */ | |
# undef DECLARE_ERRNO /* errno is now a function in a dynamic link */ | |
# endif /* library (or something)--incompatible with */ | |
#endif /* the usual "extern int errno" declaration */ | |
#ifdef DOS_OS2 /* defined for all MS-DOS and OS/2 compilers */ | |
# include <io.h> /* lseek(), open(), setftime(), dup(), creat() */ | |
# include <time.h> /* localtime() */ | |
#endif | |
#ifdef OS2 /* defined for all OS/2 compilers */ | |
# ifdef isupper | |
# undef isupper | |
# endif | |
# ifdef tolower | |
# undef tolower | |
# endif | |
# define isupper(x) IsUpperNLS((unsigned char)(x)) | |
# define tolower(x) ToLowerNLS((unsigned char)(x)) | |
#endif | |
#ifdef WIN32 | |
# include <io.h> /* read(), open(), etc. */ | |
# include <time.h> | |
# include <memory.h> | |
# include <direct.h> /* mkdir() */ | |
# ifdef FILE_IO_C | |
# include <fcntl.h> | |
# include <conio.h> | |
# include <sys\types.h> | |
# include <sys\utime.h> | |
# include <windows.h> | |
# define DOS_OS2 | |
# define getch() getchar() | |
# endif | |
#endif | |
/*--------------------------------------------------------------------------- | |
Followed by some VMS (mostly) stuff: | |
---------------------------------------------------------------------------*/ | |
#ifdef VMS | |
# include <time.h> /* the usual non-BSD time functions */ | |
# include <file.h> /* same things as fcntl.h has */ | |
# include <rms.h> | |
# define _MAX_PATH NAM$C_MAXRSS /* to define FILNAMSIZ below */ | |
# define UNIX /* can share most of same code from now on */ | |
# define RETURN return_VMS /* VMS interprets return codes incorrectly */ | |
#else /* !VMS */ | |
# ifndef THINK_C | |
# define RETURN return /* only used in main() */ | |
# else | |
# define RETURN(v) { int n;\ | |
n = (v);\ | |
fprintf(stderr, "\npress <return> to continue ");\ | |
while (getc(stdin) != '\n');\ | |
putc('\n', stderr);\ | |
InitCursor();\ | |
goto start;\ | |
} | |
# endif | |
# ifdef V7 | |
# define O_RDONLY 0 | |
# define O_WRONLY 1 | |
# define O_RDWR 2 | |
# else /* !V7 */ | |
# ifdef MTS | |
# include <sys/file.h> /* MTS uses this instead of fcntl.h */ | |
# include <timeb.h> | |
# include <time.h> | |
# else /* !MTS */ | |
# ifdef COHERENT /* Coherent 3.10/Mark Williams C */ | |
# include <sys/fcntl.h> | |
# define SHORT_NAMES | |
# define tzset settz | |
# else /* !COHERENT */ | |
# include <fcntl.h> /* O_BINARY for open() w/o CR/LF translation */ | |
# endif /* ?COHERENT */ | |
# endif /* ?MTS */ | |
# endif /* ?V7 */ | |
#endif /* ?VMS */ | |
#if (defined(MSDOS) || defined(VMS)) | |
# define DOS_VMS | |
#endif | |
/*--------------------------------------------------------------------------- | |
And some Mac stuff for good measure: | |
---------------------------------------------------------------------------*/ | |
#ifdef THINK_C | |
# define MACOS | |
# ifndef __STDC__ /* if Think C hasn't defined __STDC__ ... */ | |
# define __STDC__ 1 /* make sure it's defined: it needs it */ | |
# else /* __STDC__ defined */ | |
# if !__STDC__ /* sometimes __STDC__ is defined as 0; */ | |
# undef __STDC__ /* it needs to be 1 or required header */ | |
# define __STDC__ 1 /* files are not properly included. */ | |
# endif /* !__STDC__ */ | |
# endif /* ?defined(__STDC__) */ | |
#endif /* THINK_C */ | |
#ifdef MPW | |
# define MACOS | |
# include <Errors.h> | |
# include <Files.h> | |
# include <Memory.h> | |
# include <Quickdraw.h> | |
# include <ToolUtils.h> | |
# define CtoPstr c2pstr | |
# define PtoCstr p2cstr | |
# ifdef CR | |
# undef CR | |
# endif | |
#endif /* MPW */ | |
#ifdef MACOS | |
# define open(x,y) macopen(x,y, gnVRefNum, glDirID) | |
# define close macclose | |
# define read macread | |
# define write macwrite | |
# define lseek maclseek | |
# define creat(x,y) maccreat(x, gnVRefNum, glDirID, gostCreator, gostType) | |
# define stat(x,y) macstat(x,y,gnVRefNum, glDirID) | |
# ifndef isascii | |
# define isascii(c) ((unsigned char)(c) <= 0x3F) | |
# endif | |
# include "macstat.h" | |
typedef struct _ZipExtraHdr { | |
unsigned short header; /* 2 bytes */ | |
unsigned short data; /* 2 bytes */ | |
} ZIP_EXTRA_HEADER; | |
typedef struct _MacInfoMin { | |
unsigned short header; /* 2 bytes */ | |
unsigned short data; /* 2 bytes */ | |
unsigned long signature; /* 4 bytes */ | |
FInfo finfo; /* 16 bytes */ | |
unsigned long lCrDat; /* 4 bytes */ | |
unsigned long lMdDat; /* 4 bytes */ | |
unsigned long flags ; /* 4 bytes */ | |
unsigned long lDirID; /* 4 bytes */ | |
/*------------*/ | |
} MACINFOMIN; /* = 40 bytes for size of data */ | |
typedef struct _MacInfo { | |
unsigned short header; /* 2 bytes */ | |
unsigned short data; /* 2 bytes */ | |
unsigned long signature; /* 4 bytes */ | |
FInfo finfo; /* 16 bytes */ | |
unsigned long lCrDat; /* 4 bytes */ | |
unsigned long lMdDat; /* 4 bytes */ | |
unsigned long flags ; /* 4 bytes */ | |
unsigned long lDirID; /* 4 bytes */ | |
char rguchVolName[28]; /* 28 bytes */ | |
/*------------*/ | |
} MACINFO; /* = 68 bytes for size of data */ | |
#endif /* MACOS */ | |
/*--------------------------------------------------------------------------- | |
And finally, some random extra stuff: | |
---------------------------------------------------------------------------*/ | |
#ifdef MINIX | |
# include <stdio.h> | |
#endif | |
#ifdef SHORT_NAMES /* Mark Williams C, ...? */ | |
# define extract_or_test_files xtr_or_tst_files | |
# define extract_or_test_member xtr_or_tst_member | |
#endif | |
#ifdef MTS | |
# include <unix.h> /* Some important non-ANSI routines */ | |
# define mkdir(s,n) (-1) /* No "make directory" capability */ | |
# define EBCDIC /* Set EBCDIC conversion on */ | |
#endif | |
/*************/ | |
/* Defines */ | |
/*************/ | |
#ifndef WSIZE | |
# define WSIZE 0x8000 /* window size--must be a power of two, and */ | |
#endif /* !WSIZE */ /* at least 32K for zip's deflate method */ | |
#define DIR_BLKSIZ 64 /* number of directory entries per block | |
* (should fit in 4096 bytes, usually) */ | |
#ifndef INBUFSIZ | |
# define INBUFSIZ 2048 /* works for MS-DOS small model */ | |
#endif /* !INBUFSIZ */ | |
/* | |
* If <limits.h> exists on most systems, should include that, since it may | |
* define some or all of the following: NAME_MAX, PATH_MAX, _POSIX_NAME_MAX, | |
* _POSIX_PATH_MAX. | |
*/ | |
#ifdef DOS_OS2 | |
# include <limits.h> | |
#endif /* DOS_OS2 */ | |
#ifdef _MAX_PATH | |
# define FILNAMSIZ (_MAX_PATH) | |
#else /* !_MAX_PATH */ | |
# define FILNAMSIZ 1025 | |
#endif /* ?_MAX_PATH */ | |
#ifndef PATH_MAX | |
# ifdef MAXPATHLEN /* defined in <sys/param.h> some systems */ | |
# define PATH_MAX MAXPATHLEN | |
# else | |
# if FILENAME_MAX > 255 /* used like PATH_MAX on some systems */ | |
# define PATH_MAX FILENAME_MAX | |
# else | |
# define PATH_MAX (FILNAMSIZ - 1) | |
# endif | |
# endif /* ?MAXPATHLEN */ | |
#endif /* !PATH_MAX */ | |
#define OUTBUFSIZ INBUFSIZ | |
#define ZSUFX ".zip" | |
#define CENTRAL_HDR_SIG "\113\001\002" /* the infamous "PK" signature */ | |
#define LOCAL_HDR_SIG "\113\003\004" /* bytes, sans "P" (so unzip */ | |
#define END_CENTRAL_SIG "\113\005\006" /* executable not mistaken for */ | |
#define EXTD_LOCAL_SIG "\113\007\010" /* zipfile itself) */ | |
#define SKIP 0 /* choice of activities for do_string() */ | |
#define DISPLAY 1 | |
#define FILENAME 2 | |
#define EXTRA_FIELD 3 | |
#define DOES_NOT_EXIST -1 /* return values for check_for_newer() */ | |
#define EXISTS_AND_OLDER 0 | |
#define EXISTS_AND_NEWER 1 | |
#define DOS_OS2_FAT_ 0 /* version_made_by codes (central dir) */ | |
#define AMIGA_ 1 | |
#define VMS_ 2 /* make sure these are not defined on */ | |
#define UNIX_ 3 /* the respective systems!! (like, for */ | |
#define VM_CMS_ 4 /* instance, "VMS", or "UNIX": CFLAGS = */ | |
#define ATARI_ 5 /* -O -DUNIX) */ | |
#define OS2_HPFS_ 6 | |
#define MAC_ 7 | |
#define Z_SYSTEM_ 8 | |
#define CPM_ 9 | |
/* #define TOPS20_ 10? (TOPS20_ is to be defined in PKZIP 2.0...) */ | |
#define NUM_HOSTS 10 /* index of last system + 1 */ | |
#define STORED 0 /* compression methods */ | |
#define SHRUNK 1 | |
#define REDUCED1 2 | |
#define REDUCED2 3 | |
#define REDUCED3 4 | |
#define REDUCED4 5 | |
#define IMPLODED 6 | |
#define TOKENIZED 7 | |
#define DEFLATED 8 | |
#define NUM_METHODS 9 /* index of last method + 1 */ | |
/* don't forget to update list_files() appropriately if NUM_METHODS changes */ | |
#define DF_MDY 0 /* date format 10/26/91 (USA only) */ | |
#define DF_DMY 1 /* date format 26/10/91 (most of the world) */ | |
#define DF_YMD 2 /* date format 91/10/26 (a few countries) */ | |
#define UNZIP_VERSION 20 /* compatible with PKUNZIP 2.0 */ | |
#define VMS_VERSION 42 /* if OS-needed-to-extract is VMS: can do */ | |
/*--------------------------------------------------------------------------- | |
True sizes of the various headers, as defined by PKWare--so it is not | |
likely that these will ever change. But if they do, make sure both these | |
defines AND the typedefs below get updated accordingly. | |
---------------------------------------------------------------------------*/ | |
#define LREC_SIZE 26 /* lengths of local file headers, central */ | |
#define CREC_SIZE 42 /* directory headers, and the end-of- */ | |
#define ECREC_SIZE 18 /* central-dir record, respectively */ | |
#define MAX_BITS 13 /* used in unShrink() */ | |
#define HSIZE (1 << MAX_BITS) /* size of global work area */ | |
#define LF 10 /* '\n' on ASCII machines. Must be 10 due to EBCDIC */ | |
#define CR 13 /* '\r' on ASCII machines. Must be 13 due to EBCDIC */ | |
#define CTRLZ 26 /* DOS & OS/2 EOF marker (used in file_io.c, vms.c) */ | |
#ifdef EBCDIC | |
# define ascii_to_native(c) ebcdic[(c)] | |
# define NATIVE "EBCDIC" | |
#endif | |
#if MPW | |
# define FFLUSH putc('\n',stderr); | |
#else /* !MPW */ | |
# define FFLUSH fflush(stderr); | |
#endif /* ?MPW */ | |
#ifdef VMS | |
# define ENV_UNZIP "UNZIP_OPTS" /* name of environment variable */ | |
# define ENV_ZIPINFO "ZIPINFO_OPTS" | |
#else /* !VMS */ | |
# define ENV_UNZIP "UNZIP" | |
# define ENV_ZIPINFO "ZIPINFO" | |
#endif /* ?VMS */ | |
#ifdef CRYPT | |
# define PWLEN 80 | |
# define DECRYPT(b) (update_keys(t=((b)&0xff)^decrypt_byte()),t) | |
#endif /* CRYPT */ | |
#ifdef QQ /* Newtware version */ | |
# define QCOND (!quietflg) /* for no file comments with -vq or -vqq */ | |
#else /* (original) Bill Davidsen version */ | |
# define QCOND (which_hdr) /* no way to kill file comments with -v, -l */ | |
#endif | |
#ifndef TRUE | |
# define TRUE 1 /* sort of obvious */ | |
#endif | |
#ifndef FALSE | |
# define FALSE 0 | |
#endif | |
#ifndef SEEK_SET /* These should all be declared in stdio.h! But */ | |
# define SEEK_SET 0 /* since they're not (in many cases), do so here. */ | |
# define SEEK_CUR 1 | |
# define SEEK_END 2 | |
#endif | |
#ifndef S_IRUSR | |
# define S_IRWXU 00700 /* read, write, execute: owner */ | |
# define S_IRUSR 00400 /* read permission: owner */ | |
# define S_IWUSR 00200 /* write permission: owner */ | |
# define S_IXUSR 00100 /* execute permission: owner */ | |
# define S_IRWXG 00070 /* read, write, execute: group */ | |
# define S_IRGRP 00040 /* read permission: group */ | |
# define S_IWGRP 00020 /* write permission: group */ | |
# define S_IXGRP 00010 /* execute permission: group */ | |
# define S_IRWXO 00007 /* read, write, execute: other */ | |
# define S_IROTH 00004 /* read permission: other */ | |
# define S_IWOTH 00002 /* write permission: other */ | |
# define S_IXOTH 00001 /* execute permission: other */ | |
#endif /* !S_IRUSR */ | |
#ifdef ZIPINFO /* these are individually checked because SysV doesn't */ | |
# ifndef S_IFBLK /* have some of them, Microsoft C others, etc. */ | |
# define S_IFBLK 0060000 /* block special */ | |
# endif | |
# ifndef S_IFIFO /* in Borland C, not MSC */ | |
# define S_IFIFO 0010000 /* fifo */ | |
# endif | |
# ifndef S_IFLNK /* in BSD, not SysV */ | |
# define S_IFLNK 0120000 /* symbolic link */ | |
# endif | |
# ifndef S_IFSOCK /* in BSD, not SysV */ | |
# define S_IFSOCK 0140000 /* socket */ | |
# endif | |
# ifndef S_ISUID | |
# define S_ISUID 04000 /* set user id on execution */ | |
# endif | |
# ifndef S_ISGID | |
# define S_ISGID 02000 /* set group id on execution */ | |
# endif | |
# ifndef S_ISVTX | |
# define S_ISVTX 01000 /* directory permissions control */ | |
# endif | |
# ifndef S_ENFMT | |
# define S_ENFMT S_ISGID /* record locking enforcement flag */ | |
# endif | |
#endif /* ZIPINFO */ | |
/**************/ | |
/* Typedefs */ | |
/**************/ | |
#ifndef _BULL_SOURCE /* Bull has it defined somewhere already */ | |
typedef unsigned char byte; /* code assumes UNSIGNED bytes */ | |
#endif /* !_BULL_SOURCE */ | |
typedef char boolean; | |
typedef long longint; | |
typedef unsigned short UWORD; | |
typedef unsigned long ULONG; | |
typedef unsigned char uch; | |
typedef unsigned short ush; | |
typedef unsigned long ulg; | |
typedef struct min_info { | |
unsigned unix_attr; | |
unsigned dos_attr; | |
int hostnum; | |
longint offset; | |
ULONG compr_size; /* compressed size (needed if extended header) */ | |
ULONG crc; /* crc (needed if extended header) */ | |
unsigned encrypted : 1; /* file encrypted: decrypt before uncompressing */ | |
unsigned ExtLocHdr : 1; /* use time instead of CRC for decrypt check */ | |
unsigned text : 1; /* file is text or binary */ | |
unsigned lcflag : 1; /* convert filename to lowercase */ | |
} min_info; | |
typedef struct VMStimbuf { | |
char *revdate; /* (both correspond to Unix modtime/st_mtime) */ | |
char *credate; | |
} VMStimbuf; | |
/*--------------------------------------------------------------------------- | |
Zipfile layout declarations. If these headers ever change, make sure the | |
xxREC_SIZE defines (above) change with them! | |
---------------------------------------------------------------------------*/ | |
typedef byte local_byte_hdr[ LREC_SIZE ]; | |
# define L_VERSION_NEEDED_TO_EXTRACT_0 0 | |
# define L_VERSION_NEEDED_TO_EXTRACT_1 1 | |
# define L_GENERAL_PURPOSE_BIT_FLAG 2 | |
# define L_COMPRESSION_METHOD 4 | |
# define L_LAST_MOD_FILE_TIME 6 | |
# define L_LAST_MOD_FILE_DATE 8 | |
# define L_CRC32 10 | |
# define L_COMPRESSED_SIZE 14 | |
# define L_UNCOMPRESSED_SIZE 18 | |
# define L_FILENAME_LENGTH 22 | |
# define L_EXTRA_FIELD_LENGTH 24 | |
typedef byte cdir_byte_hdr[ CREC_SIZE ]; | |
# define C_VERSION_MADE_BY_0 0 | |
# define C_VERSION_MADE_BY_1 1 | |
# define C_VERSION_NEEDED_TO_EXTRACT_0 2 | |
# define C_VERSION_NEEDED_TO_EXTRACT_1 3 | |
# define C_GENERAL_PURPOSE_BIT_FLAG 4 | |
# define C_COMPRESSION_METHOD 6 | |
# define C_LAST_MOD_FILE_TIME 8 | |
# define C_LAST_MOD_FILE_DATE 10 | |
# define C_CRC32 12 | |
# define C_COMPRESSED_SIZE 16 | |
# define C_UNCOMPRESSED_SIZE 20 | |
# define C_FILENAME_LENGTH 24 | |
# define C_EXTRA_FIELD_LENGTH 26 | |
# define C_FILE_COMMENT_LENGTH 28 | |
# define C_DISK_NUMBER_START 30 | |
# define C_INTERNAL_FILE_ATTRIBUTES 32 | |
# define C_EXTERNAL_FILE_ATTRIBUTES 34 | |
# define C_RELATIVE_OFFSET_LOCAL_HEADER 38 | |
typedef byte ec_byte_rec[ ECREC_SIZE+4 ]; | |
/* define SIGNATURE 0 space-holder only */ | |
# define NUMBER_THIS_DISK 4 | |
# define NUM_DISK_WITH_START_CENTRAL_DIR 6 | |
# define NUM_ENTRIES_CENTRL_DIR_THS_DISK 8 | |
# define TOTAL_ENTRIES_CENTRAL_DIR 10 | |
# define SIZE_CENTRAL_DIRECTORY 12 | |
# define OFFSET_START_CENTRAL_DIRECTORY 16 | |
# define ZIPFILE_COMMENT_LENGTH 20 | |
typedef struct local_file_header { /* LOCAL */ | |
byte version_needed_to_extract[2]; | |
UWORD general_purpose_bit_flag; | |
UWORD compression_method; | |
UWORD last_mod_file_time; | |
UWORD last_mod_file_date; | |
ULONG crc32; | |
ULONG compressed_size; | |
ULONG uncompressed_size; | |
UWORD filename_length; | |
UWORD extra_field_length; | |
} local_file_hdr; | |
typedef struct central_directory_file_header { /* CENTRAL */ | |
byte version_made_by[2]; | |
byte version_needed_to_extract[2]; | |
UWORD general_purpose_bit_flag; | |
UWORD compression_method; | |
UWORD last_mod_file_time; | |
UWORD last_mod_file_date; | |
ULONG crc32; | |
ULONG compressed_size; | |
ULONG uncompressed_size; | |
UWORD filename_length; | |
UWORD extra_field_length; | |
UWORD file_comment_length; | |
UWORD disk_number_start; | |
UWORD internal_file_attributes; | |
ULONG external_file_attributes; | |
ULONG relative_offset_local_header; | |
} cdir_file_hdr; | |
typedef struct end_central_dir_record { /* END CENTRAL */ | |
UWORD number_this_disk; | |
UWORD num_disk_with_start_central_dir; | |
UWORD num_entries_centrl_dir_ths_disk; | |
UWORD total_entries_central_dir; | |
ULONG size_central_directory; | |
ULONG offset_start_central_directory; | |
UWORD zipfile_comment_length; | |
} ecdir_rec; | |
/*************************/ | |
/* Function Prototypes */ | |
/*************************/ | |
#ifndef __ | |
# define __ OF | |
#endif | |
/*--------------------------------------------------------------------------- | |
Functions in unzip.c and/or zipinfo.c: | |
---------------------------------------------------------------------------*/ | |
int usage __((int error)); | |
int process_zipfile __((void)); | |
int find_end_central_dir __((void)); | |
int process_end_central_dir __((void)); | |
int list_files __((void)); /* unzip.c */ | |
int process_cdir_file_hdr __((void)); /* unzip.c */ | |
int process_local_file_hdr __((void)); /* unzip.c */ | |
int process_central_dir __((void)); | |
int long_info __((void)); /* zipinfo.c */ | |
int short_info __((void)); /* zipinfo.c */ | |
char *zipinfo_time __((UWORD *datez, UWORD *timez)); | |
/*--------------------------------------------------------------------------- | |
Functions in extract.c: | |
---------------------------------------------------------------------------*/ | |
int extract_or_test_files __((void)); | |
/* static int store_info __((void)); */ | |
/* static int extract_or_test_member __((void)); */ | |
int memextract __((byte *, ULONG, byte *, ULONG)); | |
int FlushMemory __((void)); | |
int ReadMemoryByte __((UWORD *x)); | |
/*--------------------------------------------------------------------------- | |
Decompression functions: | |
---------------------------------------------------------------------------*/ | |
int explode __((void)); /* explode.c */ | |
int inflate __((void)); /* inflate.c */ | |
void unReduce __((void)); /* unreduce.c */ | |
/* static void LoadFollowers __((void)); * unreduce.c */ | |
void unShrink __((void)); /* unshrink.c */ | |
/* static void partial_clear __((void)); * unshrink.c */ | |
/*--------------------------------------------------------------------------- | |
Functions in file_io.c and crypt.c: | |
---------------------------------------------------------------------------*/ | |
int open_input_file __((void)); /* file_io.c */ | |
int readbuf __((char *buf, register unsigned len)); | |
int create_output_file __((void)); /* file_io.c, vms.c */ | |
int FillBitBuffer __((void)); /* file_io.c */ | |
int ReadByte __((UWORD *x)); /* file_io.c */ | |
int FlushOutput __((void)); /* file_io.c, vms.c */ | |
/* static int dos2unix __((unsigned char *, int)); * file_io.c */ | |
void set_file_time_and_close __((void)); /* file_io.c */ | |
void handler __((int signal)); /* file_io.c */ | |
int echo __((int opt)); /* file_io.c */ | |
void echoff __((int f)); /* file_io.c */ | |
void echon __((void)); /* file_io.c */ | |
char *getp __((char *, char *, int)); /* file_io.c */ | |
int decrypt_byte __((void)); /* crypt.c */ | |
void update_keys __((int)); /* crypt.c */ | |
void init_keys __((char *)); /* crypt.c */ | |
/*--------------------------------------------------------------------------- | |
Macintosh file_io functions: | |
---------------------------------------------------------------------------*/ | |
#ifdef MACOS | |
/* static int IsHFSDisk __((int)); */ | |
void macfstest __((int)); | |
int macmkdir __((char *, short, long)); | |
void ResolveMacVol __((short, short *, long *, StringPtr)); | |
short macopen __((char *, short, short, long)); | |
short maccreat __((char *, short, long, OSType, OSType)); | |
short macread __((short, char *, unsigned)); | |
short macwrite __((short, char *, unsigned)); | |
short macclose __((short)); | |
long maclseek __((short, long, short)); | |
#endif | |
/*--------------------------------------------------------------------------- | |
OS/2 file_io functions: | |
---------------------------------------------------------------------------*/ | |
void ChangeNameForFAT __((char *name)); /* os2unzip.c */ | |
int IsFileNameValid __((char *name)); /* os2unzip.c */ | |
int GetCountryInfo __((void)); /* os2unzip.c */ | |
long GetFileTime __((char *name)); /* os2unzip.c */ | |
void SetPathInfo __((char *path, UWORD moddate, UWORD modtime, int flags)); | |
int SetLongNameEA __((char *name, char *longname)); /* os2unzip.c */ | |
int IsEA __((void *extra_field)); /* os2unzip.c */ | |
ULONG SizeOfEAs __((void *extra_field)); /* os2unzip.c */ | |
void SetEAs __((char *path, void *eablock)); /* os2unzip.c */ | |
int IsUpperNLS __((int nChr)); /* os2unzip.c */ | |
int ToLowerNLS __((int nChr)); /* os2unzip.c */ | |
/*--------------------------------------------------------------------------- | |
VMS file_io functions: | |
---------------------------------------------------------------------------*/ | |
int check_format __((void)); /* vms.c */ | |
int find_vms_attrs __((void)); /* vms.c */ | |
int CloseOutputFile __((void)); /* vms.c */ | |
/* static byte *extract_block __((struct extra_block *, int *, byte *, int));*/ | |
/* static int _flush_blocks __((int final_flag)); * vms.c */ | |
/* static int _flush_records __((int final_flag)); * vms.c */ | |
/* static int WriteBuffer __((unsigned char *buf, int len)); * vms.c */ | |
/* static int WriteRecord __((unsigned char *rec, int len)); * vms.c */ | |
/* static void message __((int string, char *status)); * vms.c */ | |
int VMSmunch __((char *, int, char *)); /* VMSmunch.c */ | |
/*--------------------------------------------------------------------------- | |
Functions in match.c, mapname.c, misc.c, etc.: | |
---------------------------------------------------------------------------*/ | |
int match __((char *string, char *pattern)); /* match.c */ | |
int mapname __((int create_dirs)); /* mapname.c */ | |
void UpdateCRC __((register unsigned char *s, register int len)); | |
int do_string __((unsigned int len, int option)); /* misc.c */ | |
time_t dos_to_unix_time __((unsigned ddate, unsigned dtime)); /* misc.c */ | |
int check_for_newer __((char *filename)); /* misc.c */ | |
int dateformat __((void)); /* misc.c */ | |
UWORD makeword __((byte *b)); /* misc.c */ | |
ULONG makelong __((byte *sig)); /* misc.c */ | |
void return_VMS __((int zip_error)); /* misc.c */ | |
void envargs __((int *, char ***, char *)); /* envargs.c */ | |
#ifdef AMIGA | |
int utime __((char *file, time_t timep[])); /* utime.c */ | |
#endif /* AMIGA */ | |
#ifdef ZMEM /* these MUST be ifdef'd because of conflicts with the std def */ | |
char *memset __((register char *buf, register char init, | |
register unsigned int len)); /* misc.c */ | |
char *memcpy __((register char *dst, register char *src, | |
register unsigned int len)); /* misc.c */ | |
#endif /* ZMEM */ | |
/************/ | |
/* Macros */ | |
/************/ | |
#ifndef MAX | |
# define MAX(a,b) ((a) > (b) ? (a) : (b)) | |
#endif | |
#ifndef MIN | |
# define MIN(a,b) ((a) < (b) ? (a) : (b)) | |
#endif | |
#define LSEEK(abs_offset) {longint request=(abs_offset)+extra_bytes,\ | |
inbuf_offset=request%INBUFSIZ, bufstart=request-inbuf_offset;\ | |
if(request<0) {fprintf(stderr, SeekMsg, ReportMsg); return(3);}\ | |
else if(bufstart!=cur_zipfile_bufstart)\ | |
{cur_zipfile_bufstart=lseek(zipfd,bufstart,SEEK_SET);\ | |
if((incnt=read(zipfd,(char *)inbuf,INBUFSIZ))<=0) return(51);\ | |
inptr=inbuf+(int)inbuf_offset; incnt-=(int)inbuf_offset;} else\ | |
{incnt+=(inptr-inbuf)-(int)inbuf_offset; inptr=inbuf+(int)inbuf_offset;}} | |
/* | |
* Seek to the block boundary of the block which includes abs_offset, | |
* then read block into input buffer and set pointers appropriately. | |
* If block is already in the buffer, just set the pointers. This macro | |
* is used by process_end_central_dir (unzip.c) and do_string (misc.c). | |
* A slightly modified version is embedded within extract_or_test_files | |
* (unzip.c). ReadByte and readbuf (file_io.c) are compatible. | |
* | |
* macro LSEEK(abs_offset) | |
* ULONG abs_offset; | |
* { | |
* longint request = abs_offset + extra_bytes; | |
* longint inbuf_offset = request % INBUFSIZ; | |
* longint bufstart = request - inbuf_offset; | |
* | |
* if (request < 0) { | |
* fprintf(stderr, SeekMsg, ReportMsg); | |
* return(3); /-* 3: severe error in zipfile *-/ | |
* } else if (bufstart != cur_zipfile_bufstart) { | |
* cur_zipfile_bufstart = lseek(zipfd, bufstart, SEEK_SET); | |
* if ((incnt = read(zipfd,inbuf,INBUFSIZ)) <= 0) | |
* return(51); /-* 51: unexpected EOF *-/ | |
* inptr = inbuf + (int)inbuf_offset; | |
* incnt -= (int)inbuf_offset; | |
* } else { | |
* incnt += (inptr-inbuf) - (int)inbuf_offset; | |
* inptr = inbuf + (int)inbuf_offset; | |
* } | |
* } | |
* | |
*/ | |
#define SKIP_(length) if(length&&((error=do_string(length,SKIP))!=0))\ | |
{error_in_archive=error; if(error>1) return error;} | |
/* | |
* Skip a variable-length field, and report any errors. Used in zipinfo.c | |
* and unzip.c in several functions. | |
* | |
* macro SKIP_(length) | |
* UWORD length; | |
* { | |
* if (length && ((error = do_string(length, SKIP)) != 0)) { | |
* error_in_archive = error; /-* might be warning *-/ | |
* if (error > 1) /-* fatal *-/ | |
* return (error); | |
* } | |
* } | |
* | |
*/ | |
#define OUTB(intc) {*outptr++=(byte)(intc); if (++outcnt==OUTBUFSIZ)\ | |
FlushOutput();} | |
/* | |
* macro OUTB(intc) | |
* { | |
* *outptr++ = (byte)(intc); | |
* if (++outcnt == OUTBUFSIZ) | |
* FlushOutput(); | |
* } | |
* | |
*/ | |
#define READBIT(nbits,zdest) {if(nbits>bits_left) FillBitBuffer();\ | |
zdest=(int)((UWORD)bitbuf&mask_bits[nbits]);bitbuf>>=nbits;bits_left-=nbits;} | |
/* | |
* macro READBIT(nbits,zdest) | |
* { | |
* if (nbits > bits_left) | |
* FillBitBuffer(); | |
* zdest = (int)((UWORD)bitbuf & mask_bits[nbits]); | |
* bitbuf >>= nbits; | |
* bits_left -= nbits; | |
* } | |
* | |
*/ | |
#define PEEKBIT(nbits) (nbits>bits_left? (FillBitBuffer(),\ | |
(UWORD)bitbuf & mask_bits[nbits]) : (UWORD)bitbuf & mask_bits[nbits]) | |
#define NUKE_CRs(buf,len) {register int i,j; for (i=j=0; j<len;\ | |
(buf)[i++]=(buf)[j++]) if ((buf)[j]=='\r') ++j; len=i;} | |
/* | |
* Remove all the ASCII carriage returns from buffer buf (length len), | |
* shortening as necessary (note that len gets modified in the process, | |
* so it CANNOT be an expression). This macro is intended to be used | |
* BEFORE A_TO_N(); hence the check for CR instead of '\r'. NOTE: The | |
* if-test gets performed one time too many, but it doesn't matter. | |
* | |
* macro NUKE_CRs(buf,len) | |
* { | |
* register int i, j; | |
* | |
* for (i = j = 0; j < len; (buf)[i++] = (buf)[j++]) | |
* if ((buf)[j] == CR) | |
* ++j; | |
* len = i; | |
* } | |
* | |
*/ | |
#define TOLOWER(str1,str2) {char *ps1,*ps2; ps1=(str1)-1; ps2=(str2);\ | |
while(*++ps1) *ps2++=(char)(isupper((int)(*ps1))?tolower((int)(*ps1)):*ps1);\ | |
*ps2='\0';} | |
/* | |
* Copy the zero-terminated string in str1 into str2, converting any | |
* uppercase letters to lowercase as we go. str2 gets zero-terminated | |
* as well, of course. str1 and str2 may be the same character array. | |
* | |
* macro TOLOWER( str1, str2 ) | |
* { | |
* char *ps1, *ps2; | |
* | |
* ps1 = (str1) - 1; | |
* ps2 = (str2); | |
* while (*++ps1) | |
* *ps2++ = (char)(isupper((int)(*ps1))? tolower((int)(*ps1)) : *ps1); | |
* *ps2='\0'; | |
* } | |
* | |
* NOTES: This macro makes no assumptions about the characteristics of | |
* the tolower() function or macro (beyond its existence), nor does it | |
* make assumptions about the structure of the character set (i.e., it | |
* should work on EBCDIC machines, too). The fact that either or both | |
* of isupper() and tolower() may be macros has been taken into account; | |
* watch out for "side effects" (in the C sense) when modifying this | |
* macro. | |
*/ | |
#ifndef ascii_to_native | |
# define ascii_to_native(c) (c) | |
# define A_TO_N(str1) | |
#else | |
# ifndef NATIVE | |
# define NATIVE "native chars" | |
# endif | |
# define A_TO_N(str1) {register unsigned char *ps1;\ | |
for (ps1=str1; *ps1; ps1++) *ps1=ascii_to_native(*ps1);} | |
#endif | |
/* | |
* Translate the zero-terminated string in str1 from ASCII to the native | |
* character set. The translation is performed in-place and uses the | |
* ascii_to_native macro to translate each character. | |
* | |
* macro A_TO_N( str1 ) | |
* { | |
* register unsigned char *ps1; | |
* | |
* for (ps1 = str1; *ps1; ++ps1) | |
* *ps1 = ascii_to_native(*ps1); | |
* } | |
* | |
* NOTE: Using the ascii_to_native macro means that is it the only part of | |
* unzip which knows which translation table (if any) is actually in use | |
* to produce the native character set. This makes adding new character | |
* set translation tables easy, insofar as all that is needed is an | |
* appropriate ascii_to_native macro definition and the translation | |
* table itself. Currently, the only non-ASCII native character set | |
* implemented is EBCDIC, but this may not always be so. | |
*/ | |
/*************/ | |
/* Globals */ | |
/*************/ | |
extern int aflag; | |
/* extern int bflag; reserved */ | |
extern int cflag; | |
extern int fflag; | |
extern int jflag; | |
extern int overwrite_none; | |
extern int overwrite_all; | |
extern int force_flag; | |
extern int quietflg; | |
#ifdef DOS_OS2 | |
extern int sflag; | |
#endif | |
extern int tflag; | |
extern int uflag; | |
extern int V_flag; | |
#ifdef VMS | |
extern int secinf; | |
#endif | |
#ifdef MACOS | |
extern int hfsflag; | |
#endif | |
extern int process_all_files; | |
extern longint csize; | |
extern longint ucsize; | |
extern char *fnames[]; | |
extern char **fnv; | |
extern char sig[]; | |
extern char answerbuf[]; | |
extern min_info *pInfo; | |
extern char *key; | |
extern ULONG keys[]; | |
#ifdef MACOS | |
union work { | |
struct { | |
short *Prefix_of; /* (8193 * sizeof(short)) */ | |
byte *Suffix_of; | |
byte *Stack; | |
} shrink; | |
byte *Slide; | |
}; | |
#else | |
union work { | |
struct { | |
short Prefix_of[HSIZE + 2]; /* (8194 * sizeof(short)) */ | |
byte Suffix_of[HSIZE + 2]; /* also s-f length_nodes (smaller) */ | |
byte Stack[HSIZE + 2]; /* also s-f distance_nodes (smaller) */ | |
} shrink; | |
byte Slide[WSIZE]; | |
}; | |
#endif | |
extern union work area; | |
# define prefix_of area.shrink.Prefix_of | |
# define suffix_of area.shrink.Suffix_of | |
# define stack area.shrink.Stack | |
# define slide area.Slide | |
extern ULONG crc32val; | |
extern UWORD mask_bits[]; | |
extern byte *inbuf; | |
extern byte *inptr; | |
extern int incnt; | |
extern ULONG bitbuf; | |
extern int bits_left; | |
extern boolean zipeof; | |
extern int zipfd; | |
#ifdef MSWIN | |
extern char *zipfn; | |
#else | |
extern char zipfn[]; | |
#endif | |
extern longint extra_bytes; | |
extern longint cur_zipfile_bufstart; | |
extern byte *extra_field; | |
extern char local_hdr_sig[]; | |
extern char central_hdr_sig[]; | |
extern char end_central_sig[]; | |
extern local_file_hdr lrec; | |
extern cdir_file_hdr crec; | |
extern ecdir_rec ecrec; | |
extern struct stat statbuf; | |
extern byte *outbuf; | |
extern byte *outptr; | |
#ifdef MSWIN | |
extern byte __far *outout; | |
extern char *filename; | |
#else | |
extern byte *outout; | |
extern char filename[]; | |
#endif | |
extern longint outpos; | |
extern int outcnt; | |
extern int outfd; | |
extern int mem_mode; | |
extern int disk_full; | |
extern char *EndSigMsg; | |
extern char *CentSigMsg; | |
extern char *SeekMsg; | |
extern char *ReportMsg; | |
#ifdef DECLARE_ERRNO | |
extern int errno; | |
#endif | |
#ifdef EBCDIC | |
extern byte ebcdic[]; | |
#endif | |
#ifdef MACOS | |
extern short gnVRefNum; | |
extern long glDirID; | |
extern OSType gostCreator; | |
extern OSType gostType; | |
extern boolean fMacZipped; | |
extern boolean macflag; | |
extern short giCursor; | |
extern CursHandle rghCursor[]; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment