zlib/infblock.c

402 lines
12 KiB
C
Raw Normal View History

2011-09-10 01:36:31 -04:00
/* infblock.c -- interpret and process block types to last block
2011-09-10 02:17:17 -04:00
* Copyright (C) 1995-1998 Mark Adler
2011-09-10 01:36:31 -04:00
* 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"
struct inflate_codes_state {int dummy;}; /* for buggy compilers */
/* Table for deflate from PKZIP's appnote.txt. */
2011-09-10 02:17:17 -04:00
local const uInt border[] = { /* Order of the bit length code lengths */
2011-09-10 01:36:31 -04:00
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.
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.
*/
2011-09-10 02:06:52 -04:00
void inflate_blocks_reset(s, z, c)
2011-09-10 02:09:18 -04:00
inflate_blocks_statef *s;
2011-09-10 02:17:02 -04:00
z_streamp z;
2011-09-10 02:09:18 -04:00
uLongf *c;
2011-09-10 02:06:52 -04:00
{
2011-09-10 02:17:33 -04:00
if (c != Z_NULL)
2011-09-10 02:06:52 -04:00
*c = s->check;
if (s->mode == BTREE || s->mode == DTREE)
ZFREE(z, s->sub.trees.blens);
if (s->mode == CODES)
2011-09-10 02:08:28 -04:00
inflate_codes_free(s->sub.decode.codes, z);
2011-09-10 02:06:52 -04:00
s->mode = TYPE;
s->bitk = 0;
s->bitb = 0;
s->read = s->write = s->window;
if (s->checkfn != Z_NULL)
2011-09-10 02:17:33 -04:00
z->adler = s->check = (*s->checkfn)(0L, (const Bytef *)Z_NULL, 0);
2011-09-10 02:18:57 -04:00
Tracev((stderr, "inflate: blocks reset\n"));
2011-09-10 02:06:52 -04:00
}
2011-09-10 02:09:18 -04:00
inflate_blocks_statef *inflate_blocks_new(z, c, w)
2011-09-10 02:17:02 -04:00
z_streamp z;
2011-09-10 01:52:17 -04:00
check_func c;
uInt w;
2011-09-10 01:36:31 -04:00
{
2011-09-10 02:09:18 -04:00
inflate_blocks_statef *s;
2011-09-10 01:36:31 -04:00
2011-09-10 02:09:18 -04:00
if ((s = (inflate_blocks_statef *)ZALLOC
2011-09-10 01:36:31 -04:00
(z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
return s;
2011-09-10 02:19:21 -04:00
if ((s->hufts =
(inflate_huft *)ZALLOC(z, sizeof(inflate_huft), MANY)) == Z_NULL)
{
ZFREE(z, s);
return Z_NULL;
}
2011-09-10 02:09:18 -04:00
if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
2011-09-10 01:36:31 -04:00
{
2011-09-10 02:19:21 -04:00
ZFREE(z, s->hufts);
2011-09-10 01:36:31 -04:00
ZFREE(z, s);
return Z_NULL;
}
2011-09-10 01:52:17 -04:00
s->end = s->window + w;
s->checkfn = c;
2011-09-10 02:06:52 -04:00
s->mode = TYPE;
2011-09-10 02:18:57 -04:00
Tracev((stderr, "inflate: blocks allocated\n"));
2011-09-10 02:17:33 -04:00
inflate_blocks_reset(s, z, Z_NULL);
2011-09-10 01:36:31 -04:00
return s;
}
int inflate_blocks(s, z, r)
2011-09-10 02:09:18 -04:00
inflate_blocks_statef *s;
2011-09-10 02:17:02 -04:00
z_streamp z;
2011-09-10 01:36:31 -04:00
int r;
{
2011-09-10 02:08:07 -04:00
uInt t; /* temporary storage */
uLong b; /* bit buffer */
uInt k; /* bits in bit buffer */
2011-09-10 02:09:18 -04:00
Bytef *p; /* input data pointer */
2011-09-10 02:08:07 -04:00
uInt n; /* bytes available there */
2011-09-10 02:09:18 -04:00
Bytef *q; /* output window write pointer */
2011-09-10 02:08:07 -04:00
uInt m; /* bytes to end of window or read pointer */
2011-09-10 01:36:31 -04:00
/* copy input/output information to locals (UPDATE macro restores) */
LOAD
/* 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)
{
2011-09-10 02:08:07 -04:00
case 0: /* stored */
2011-09-10 02:18:57 -04:00
Tracev((stderr, "inflate: stored block%s\n",
2011-09-10 02:08:07 -04:00
s->last ? " (last)" : ""));
DUMPBITS(3)
t = k & 7; /* go to byte boundary */
DUMPBITS(t)
s->mode = LENS; /* get length of stored block */
break;
case 1: /* fixed */
2011-09-10 02:18:57 -04:00
Tracev((stderr, "inflate: fixed codes block%s\n",
2011-09-10 02:08:07 -04:00
s->last ? " (last)" : ""));
{
uInt bl, bd;
inflate_huft *tl, *td;
2011-09-10 01:36:31 -04:00
2011-09-10 02:19:21 -04:00
inflate_trees_fixed(&bl, &bd, &tl, &td, z);
2011-09-10 02:08:28 -04:00
s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
if (s->sub.decode.codes == Z_NULL)
2011-09-10 02:08:07 -04:00
{
r = Z_MEM_ERROR;
LEAVE
}
}
DUMPBITS(3)
s->mode = CODES;
break;
case 2: /* dynamic */
2011-09-10 02:18:57 -04:00
Tracev((stderr, "inflate: dynamic codes block%s\n",
2011-09-10 02:08:07 -04:00
s->last ? " (last)" : ""));
DUMPBITS(3)
s->mode = TABLE;
break;
case 3: /* illegal */
DUMPBITS(3)
s->mode = BAD;
2011-09-10 02:14:39 -04:00
z->msg = (char*)"invalid block type";
2011-09-10 02:08:07 -04:00
r = Z_DATA_ERROR;
LEAVE
2011-09-10 01:36:31 -04:00
}
break;
case LENS:
NEEDBITS(32)
2011-09-10 02:11:37 -04:00
if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
2011-09-10 01:36:31 -04:00
{
2011-09-10 02:06:52 -04:00
s->mode = BAD;
2011-09-10 02:14:39 -04:00
z->msg = (char*)"invalid stored block lengths";
2011-09-10 02:08:07 -04:00
r = Z_DATA_ERROR;
LEAVE
2011-09-10 01:36:31 -04:00
}
s->sub.left = (uInt)b & 0xffff;
2011-09-10 02:08:28 -04:00
b = k = 0; /* dump bits */
2011-09-10 02:06:52 -04:00
Tracev((stderr, "inflate: stored length %u\n", s->sub.left));
2011-09-10 02:14:39 -04:00
s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
2011-09-10 01:36:31 -04:00
break;
case STORED:
2011-09-10 02:08:07 -04:00
if (n == 0)
LEAVE
NEEDOUT
t = s->sub.left;
if (t > n) t = n;
if (t > m) t = m;
zmemcpy(q, p, t);
p += t; n -= t;
q += t; m -= t;
if ((s->sub.left -= t) != 0)
break;
2011-09-10 02:06:52 -04:00
Tracev((stderr, "inflate: stored end, %lu total out\n",
2011-09-10 02:08:07 -04:00
z->total_out + (q >= s->read ? q - s->read :
(s->end - s->read) + (q - s->window))));
2011-09-10 01:36:31 -04:00
s->mode = s->last ? DRY : TYPE;
break;
case TABLE:
NEEDBITS(14)
s->sub.trees.table = t = (uInt)b & 0x3fff;
#ifndef PKZIP_BUG_WORKAROUND
if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
{
2011-09-10 02:06:52 -04:00
s->mode = BAD;
2011-09-10 02:14:39 -04:00
z->msg = (char*)"too many length or distance symbols";
2011-09-10 02:08:07 -04:00
r = Z_DATA_ERROR;
LEAVE
2011-09-10 01:36:31 -04:00
}
#endif
t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
2011-09-10 02:09:18 -04:00
if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
2011-09-10 01:36:31 -04:00
{
r = Z_MEM_ERROR;
2011-09-10 02:08:07 -04:00
LEAVE
2011-09-10 01:36:31 -04:00
}
DUMPBITS(14)
s->sub.trees.index = 0;
2011-09-10 02:06:52 -04:00
Tracev((stderr, "inflate: table sizes ok\n"));
2011-09-10 01:36:31 -04:00
s->mode = BTREE;
case BTREE:
while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
{
NEEDBITS(3)
2011-09-10 02:08:07 -04:00
s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
DUMPBITS(3)
2011-09-10 01:36:31 -04:00
}
while (s->sub.trees.index < 19)
s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
s->sub.trees.bb = 7;
t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
2011-09-10 02:19:21 -04:00
&s->sub.trees.tb, s->hufts, z);
2011-09-10 01:36:31 -04:00
if (t != Z_OK)
{
2011-09-10 02:17:17 -04:00
ZFREE(z, s->sub.trees.blens);
2011-09-10 01:36:31 -04:00
r = t;
2011-09-10 02:08:07 -04:00
if (r == Z_DATA_ERROR)
s->mode = BAD;
LEAVE
2011-09-10 01:36:31 -04:00
}
s->sub.trees.index = 0;
2011-09-10 02:06:52 -04:00
Tracev((stderr, "inflate: bits tree ok\n"));
2011-09-10 01:36:31 -04:00
s->mode = DTREE;
case DTREE:
while (t = s->sub.trees.table,
2011-09-10 02:08:07 -04:00
s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
2011-09-10 01:36:31 -04:00
{
inflate_huft *h;
2011-09-10 02:08:07 -04:00
uInt i, j, c;
2011-09-10 01:36:31 -04:00
t = s->sub.trees.bb;
NEEDBITS(t)
2011-09-10 02:08:07 -04:00
h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
t = h->word.what.Bits;
c = h->more.Base;
if (c < 16)
{
DUMPBITS(t)
s->sub.trees.blens[s->sub.trees.index++] = c;
}
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)
i = s->sub.trees.index;
t = s->sub.trees.table;
if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
(c == 16 && i < 1))
{
2011-09-10 02:17:17 -04:00
ZFREE(z, s->sub.trees.blens);
2011-09-10 02:08:07 -04:00
s->mode = BAD;
2011-09-10 02:14:39 -04:00
z->msg = (char*)"invalid bit length repeat";
2011-09-10 02:08:07 -04:00
r = Z_DATA_ERROR;
LEAVE
}
c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
do {
s->sub.trees.blens[i++] = c;
} while (--j);
s->sub.trees.index = i;
}
2011-09-10 01:36:31 -04:00
}
s->sub.trees.tb = Z_NULL;
{
2011-09-10 02:08:07 -04:00
uInt bl, bd;
inflate_huft *tl, *td;
2011-09-10 02:09:18 -04:00
inflate_codes_statef *c;
2011-09-10 01:36:31 -04:00
2011-09-10 02:09:18 -04:00
bl = 9; /* must be <= 9 for lookahead assumptions */
bd = 6; /* must be <= 9 for lookahead assumptions */
2011-09-10 02:08:07 -04:00
t = s->sub.trees.table;
t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
2011-09-10 02:19:21 -04:00
s->sub.trees.blens, &bl, &bd, &tl, &td,
s->hufts, z);
2011-09-10 02:17:17 -04:00
ZFREE(z, s->sub.trees.blens);
2011-09-10 02:08:07 -04:00
if (t != Z_OK)
{
if (t == (uInt)Z_DATA_ERROR)
s->mode = BAD;
r = t;
LEAVE
}
2011-09-10 02:19:21 -04:00
Tracev((stderr, "inflate: trees ok\n"));
2011-09-10 02:08:07 -04:00
if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
{
r = Z_MEM_ERROR;
LEAVE
}
2011-09-10 02:08:28 -04:00
s->sub.decode.codes = c;
2011-09-10 01:36:31 -04:00
}
s->mode = CODES;
case CODES:
UPDATE
if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
2011-09-10 02:08:07 -04:00
return inflate_flush(s, z, r);
2011-09-10 01:36:31 -04:00
r = Z_OK;
2011-09-10 02:08:28 -04:00
inflate_codes_free(s->sub.decode.codes, z);
2011-09-10 01:36:31 -04:00
LOAD
2011-09-10 02:06:52 -04:00
Tracev((stderr, "inflate: codes end, %lu total out\n",
2011-09-10 02:08:07 -04:00
z->total_out + (q >= s->read ? q - s->read :
(s->end - s->read) + (q - s->window))));
2011-09-10 02:03:14 -04:00
if (!s->last)
{
s->mode = TYPE;
2011-09-10 02:08:07 -04:00
break;
2011-09-10 02:03:14 -04:00
}
if (k > 7) /* return unused byte, if any */
{
Assert(k < 16, "inflate_codes grabbed too many bytes")
k -= 8;
2011-09-10 02:08:07 -04:00
n++;
p--; /* can always return one */
2011-09-10 02:03:14 -04:00
}
s->mode = DRY;
2011-09-10 01:36:31 -04:00
case DRY:
FLUSH
if (s->read != s->write)
LEAVE
s->mode = DONE;
case DONE:
r = Z_STREAM_END;
LEAVE
2011-09-10 02:06:52 -04:00
case BAD:
2011-09-10 01:36:31 -04:00
r = Z_DATA_ERROR;
LEAVE
default:
r = Z_STREAM_ERROR;
LEAVE
}
}
2011-09-10 02:17:33 -04:00
int inflate_blocks_free(s, z)
2011-09-10 02:09:18 -04:00
inflate_blocks_statef *s;
2011-09-10 02:17:02 -04:00
z_streamp z;
2011-09-10 01:36:31 -04:00
{
2011-09-10 02:17:33 -04:00
inflate_blocks_reset(s, z, Z_NULL);
2011-09-10 01:36:31 -04:00
ZFREE(z, s->window);
2011-09-10 02:19:21 -04:00
ZFREE(z, s->hufts);
2011-09-10 01:36:31 -04:00
ZFREE(z, s);
2011-09-10 02:18:57 -04:00
Tracev((stderr, "inflate: blocks freed\n"));
2011-09-10 01:36:31 -04:00
return Z_OK;
}
2011-09-10 02:14:39 -04:00
void inflate_set_dictionary(s, d, n)
inflate_blocks_statef *s;
const Bytef *d;
uInt n;
{
2011-09-10 02:19:21 -04:00
zmemcpy(s->window, d, n);
2011-09-10 02:14:39 -04:00
s->read = s->write = s->window + n;
}
2011-09-10 02:17:33 -04:00
/* 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)
inflate_blocks_statef *s;
{
return s->mode == LENS;
}