#pragma prototyped
/*
* zip deflate decoder
*/
/* 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.
[The history has been moved to the file ChangeLog.]
*/
/*
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/
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 are compressible, then either the fixed or dynamic methods
are used. In the dynamic method, the compressed data are preceded by
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 multi-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.
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.
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 "zip.h"
#include "huff.h"
#define STORED_BLOCK 0
/* Save to local */
#define BITS_LOCAL \
/* Restore to state */
#define BITS_GLOBAL \
struct State_s
{
int method;
int last;
};
static int
{
ssize_t r;
return 0;
{
return 0;
}
}
/* 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. */
/* Tables for deflate from PKZIP's appnote.txt. */
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. */
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 */
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};
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};
/* inflate (decompress) the codes in a deflated (compressed) block.
Return an error code or zero if it all goes ok. */
static uch*
{
Huff_t* t; /* pointer to table entry */
ulg l;
ulg w;
ulg d;
if (p == e)
return p;
/* inflate the coded data until end of block */
for (;;)
{
x = t->e;
while (x > 16)
{
if (x == 99)
return 0;
DUMPBITS(t->b);
x -= 16;
t = t->v.t + GETBITS(x);
x = t->e;
}
DUMPBITS(t->b);
if (x == 16)
{
/* literal */
w &= WINDOW - 1;
if (p >= e)
{
return p;
}
}
else if (x == 15)
break;
else
{
/* get length of block to copy */
l = t->v.n + GETBITS(x);
DUMPBITS(x);
/* decode distance of block to copy */
x = t->e;
while (x > 16)
{
if (x == 99)
return 0;
DUMPBITS(t->b);
x -= 16;
t = t->v.t + GETBITS(x);
x = t->e;
}
DUMPBITS(t->b);
d = w - t->v.n - GETBITS(x);
DUMPBITS(x);
/* do the copy */
while (l--)
{
d &= WINDOW - 1;
w &= WINDOW - 1;
if (p >= e)
{
return p;
}
}
}
}
return p;
}
/* "decompress" an inflated type 0 (stored) block. */
static uch*
{
ulg l;
ulg w;
/* go to byte boundary */
l = bit_len & 7;
DUMPBITS(l);
/* get the length and its complement */
l = GETBITS(16);
DUMPBITS(16);
{
return 0;
}
DUMPBITS(16);
/* read and output the compressed data */
for (;;)
{
if (!l--)
{
break;
}
w &= WINDOW - 1;
DUMPBITS(8);
if (p >= e)
{
break;
}
}
return p;
}
/* decompress an inflated type 1 (fixed Huffman codes) block. We should
either replace this with a custom state, or at least precompute the
Huffman tables. */
static int
{
/* if first time, set up tables for fixed blocks */
{
int i; /* temporary variable */
/* 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;
/* make a complete, but wrong code set */
for (; i < 288; i++)
l[i] = 8;
return -1;
/* distance table -- make an incomplete code set */
for (i = 0; i < 30; i++)
l[i] = 5;
return -1;
}
return 0;
}
/* decompress an inflated type 2 (dynamic Huffman codes) block. */
static int
{
int i; /* temporary variables */
ulg j;
ulg l; /* last length */
ulg n; /* number of lengths to get */
#ifdef PKZIP_BUG_WORKAROUND
#else
#endif
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
/* read in table lengths */
DUMPBITS(5);
DUMPBITS(5);
DUMPBITS(4);
#ifdef PKZIP_BUG_WORKAROUND
#else
#endif
{
/* bad lengths */
return -1;
}
/* read in bit-length-code lengths */
for (j = 0; j < nb; j++)
{
DUMPBITS(3);
}
for (; j < 19; j++)
/* build decoding table for trees--single level, 7 bit lookup */
bl = 7;
{
/* incomplete code set */
return -1;
}
/* read in literal and distance code lengths */
i = l = 0;
while ((ulg)i < n)
{
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 */
{
DUMPBITS(2);
if ((ulg)i + j > n)
{
return -1;
}
while (j--)
ll[i++] = l;
}
else if (j == 17) /* 3 to 10 zero length codes */
{
DUMPBITS(3);
if ((ulg)i + j > n)
{
return -1;
}
while (j--)
ll[i++] = 0;
l = 0;
}
else /* j == 18: 11 to 138 0 length codes */
{
DUMPBITS(7);
if ((ulg)i + j > n)
{
return -1;
}
while (j--)
ll[i++] = 0;
l = 0;
}
}
/* free decoding table for trees */
if (bl == 0) /* no literals or lengths */
i = 1;
if (i)
{
/* incomplete code set */
(*state->codex->disc->errorf)(NiL, state->codex->disc, 2, "%s: incomplete literal tree", state->codex->meth->name);
return -1;
}
{
/* lengths but no distances */
(*state->codex->disc->errorf)(NiL, state->codex->disc, 2, "%s: incomplete distance tree", state->codex->meth->name);
return -1;
}
if (i == 1)
{
#ifdef PKZIP_BUG_WORKAROUND
i = 0;
#else
(*state->codex->disc->errorf)(NiL, state->codex->disc, 2, "%s: incomplete distance tree", state->codex->meth->name);
#endif
}
if (i)
return -1;
return 0;
}
static int
{
return -1;
{
return -1;
}
return 0;
}
static int
{
if (!state)
return -1;
return 0;
}
static int
{
return 0;
}
static ssize_t
{
register uch* q;
ulg l;
ulg w;
ulg d;
{
{
while (l && p < e)
{
l--;
d &= WINDOW - 1;
w &= WINDOW - 1;
}
}
else
{
while (l && p < e)
{
l--;
w &= WINDOW - 1;
DUMPBITS(8);
}
if (!l)
}
}
while (p < e)
{
{
{
break;
}
{
l = bit_len & 7;
DUMPBITS(l);
}
/* read in last block bit */
if (GETBITS(1))
DUMPBITS(1);
/* read in block type */
DUMPBITS(2);
}
{
case STORED_BLOCK:
q = inflate_stored(state, p, e);
break;
case STATIC_TREES:
break;
case DYNAMIC_TREES:
break;
default:
q = 0;
break;
}
if (!q)
{
break;
return -1;
}
p = q;
}
}
{
"deflate",
"zip deflate compression (PKZIP method 8). Option is level"
" { 6:faster ... 9:better }.",
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
};