/* infblock.c -- interpret and process block types to last block
* Copyright (C) 1995-2002 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "zutil.h"
#include "infblock.h"
#include "inftrees.h"
#include "infcodes.h"
#include "infutil.h"
/* simplify the use of the inflate_huft type with some defines */
/* Table for deflate from PKZIP's appnote.txt. */
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
/*
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.
*/
void inflate_blocks_reset(s, z, c)
z_streamp z;
uLongf *c;
{
if (c != Z_NULL)
*c = s->check;
s->bitk = 0;
s->bitb = 0;
}
z_streamp z;
check_func c;
uInt w;
{
if ((s = (inflate_blocks_statef *)ZALLOC
return s;
if ((s->hufts =
{
ZFREE(z, s);
return Z_NULL;
}
{
ZFREE(z, s);
return Z_NULL;
}
s->checkfn = c;
inflate_blocks_reset(s, z, Z_NULL);
return s;
}
int inflate_blocks(s, z, r)
z_streamp z;
int r;
{
uInt t; /* temporary storage */
uLong b; /* bit buffer */
uInt k; /* bits in bit buffer */
Bytef *p; /* input data pointer */
uInt n; /* bytes available there */
Bytef *q; /* output window write pointer */
uInt m; /* bytes to end of window or read pointer */
/* process input based on current state */
while (1) switch (s->mode)
{
case TYPE:
NEEDBITS(3)
t = (uInt)b & 7;
s->last = t & 1;
switch (t >> 1)
{
case 0: /* stored */
DUMPBITS(3)
t = k & 7; /* go to byte boundary */
DUMPBITS(t)
break;
case 1: /* fixed */
{
{
r = Z_MEM_ERROR;
}
}
DUMPBITS(3)
break;
case 2: /* dynamic */
DUMPBITS(3)
break;
case 3: /* illegal */
DUMPBITS(3)
z->msg = (char*)"invalid block type";
r = Z_DATA_ERROR;
}
break;
case LENS:
NEEDBITS(32)
if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
{
z->msg = (char*)"invalid stored block lengths";
r = Z_DATA_ERROR;
}
b = k = 0; /* dump bits */
break;
case STORED:
if (n == 0)
if (t > n) t = n;
if (t > m) t = m;
zmemcpy(q, p, t);
p += t; n -= t;
q += t; m -= t;
break;
break;
case TABLE:
NEEDBITS(14)
#ifndef PKZIP_BUG_WORKAROUND
if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
{
z->msg = (char*)"too many length or distance symbols";
r = Z_DATA_ERROR;
}
#endif
t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
{
r = Z_MEM_ERROR;
}
DUMPBITS(14)
case BTREE:
{
NEEDBITS(3)
DUMPBITS(3)
}
if (t != Z_OK)
{
r = t;
if (r == Z_DATA_ERROR)
{
}
}
case DTREE:
{
inflate_huft *h;
uInt i, j, c;
NEEDBITS(t)
t = h->bits;
c = h->base;
if (c < 16)
{
DUMPBITS(t)
}
else /* c == 16..18 */
{
i = c == 18 ? 7 : c - 14;
j = c == 18 ? 11 : 3;
NEEDBITS(t + i)
DUMPBITS(t)
j += (uInt)b & inflate_mask[i];
DUMPBITS(i)
if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
(c == 16 && i < 1))
{
z->msg = (char*)"invalid bit length repeat";
r = Z_DATA_ERROR;
}
do {
} while (--j);
}
}
{
s->hufts, z);
if (t != Z_OK)
{
if (t == (uInt)Z_DATA_ERROR)
{
}
r = t;
}
{
r = Z_MEM_ERROR;
}
}
case CODES:
if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
return inflate_flush(s, z, r);
r = Z_OK;
if (!s->last)
{
break;
}
case DRY:
case DONE:
r = Z_STREAM_END;
case BAD:
r = Z_DATA_ERROR;
default:
r = Z_STREAM_ERROR;
}
}
int inflate_blocks_free(s, z)
z_streamp z;
{
inflate_blocks_reset(s, z, Z_NULL);
ZFREE(z, s);
return Z_OK;
}
void inflate_set_dictionary(s, d, n)
const Bytef *d;
uInt n;
{
}
/* Returns true if inflate is currently at the end of a block generated
* by Z_SYNC_FLUSH or Z_FULL_FLUSH.
* IN assertion: s != Z_NULL
*/
int inflate_blocks_sync_point(s)
{
}