1
/* infcodes.c -- process literals and length/distance pairs
2
* Copyright (C) 1995-2002 Mark Adler
3
* For conditions of distribution and use, see copyright notice in zlib.h
12
/* simplify the use of the inflate_huft type with some defines */
13
#define exop word.what.Exop
14
#define bits word.what.Bits
16
typedef enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
17
START, /* x: set up for LEN */
18
LEN, /* i: get length/literal/eob next */
19
LENEXT, /* i: getting length extra (have base) */
20
DIST, /* i: get distance next */
21
DISTEXT, /* i: getting distance extra */
22
COPY, /* o: copying bytes in window, waiting for space */
23
LIT, /* o: got literal, waiting for output space */
24
WASH, /* o: got eob, possibly still output waiting */
25
END, /* x: got eob and all data flushed */
26
BADCODE} /* x: got error */
29
/* inflate codes private state */
30
struct inflate_codes_state {
33
inflate_codes_mode mode; /* current inflate_codes mode */
35
/* mode dependent information */
39
inflate_huft *tree; /* pointer into tree */
40
uInt need; /* bits needed */
41
} code; /* if LEN or DIST, where in tree */
42
uInt lit; /* if LIT, literal */
44
uInt get; /* bits to get for extra */
45
uInt dist; /* distance back to copy from */
46
} copy; /* if EXT or COPY, where and how much */
49
/* mode independent information */
50
Byte lbits; /* ltree bits decoded per branch */
51
Byte dbits; /* dtree bits decoder per branch */
52
inflate_huft *ltree; /* literal/length/eob tree */
53
inflate_huft *dtree; /* distance tree */
58
local inflate_codes_statef *inflate_codes_new( /* bl, bd, tl, td, z) */
61
inflate_huft *td, /* need separate declaration for Borland C++ */
64
inflate_codes_statef *c;
66
if ((c = (inflate_codes_statef *)
67
ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
74
Tracev((stderr, "inflate: codes new\n"));
80
local int inflate_codes( /* s, z, r) */
81
inflate_blocks_statef *s,
85
uInt j; /* temporary storage */
86
inflate_huft *t; /* temporary pointer */
87
uInt e; /* extra bits or operation */
88
uLong b; /* bit buffer */
89
uInt k; /* bits in bit buffer */
90
Bytef *p; /* input data pointer */
91
uInt n; /* bytes available there */
92
Bytef *q; /* output window write pointer */
93
uInt m; /* bytes to end of window or read pointer */
94
Bytef *f; /* pointer to copy strings from */
95
inflate_codes_statef *c = s->sub.decode.codes; /* codes state */
97
/* copy input/output information to locals (UPDATE macro restores) */
100
/* process input and output based on current state */
101
while (1) switch (c->mode)
102
{ /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
103
case START: /* x: set up for LEN */
105
if (m >= 258 && n >= 10)
108
r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
112
c->mode = r == Z_STREAM_END ? WASH : BADCODE;
117
c->sub.code.need = c->lbits;
118
c->sub.code.tree = c->ltree;
120
case LEN: /* i: get length/literal/eob next */
121
j = c->sub.code.need;
123
t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
126
if (e == 0) /* literal */
128
c->sub.lit = t->base;
129
Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
130
"inflate: literal '%c'\n" :
131
"inflate: literal 0x%02x\n", t->base));
135
if (e & 16) /* length */
137
c->sub.copy.get = e & 15;
142
if ((e & 64) == 0) /* next table */
144
c->sub.code.need = e;
145
c->sub.code.tree = t + t->base;
148
if (e & 32) /* end of block */
150
Tracevv((stderr, "inflate: end of block\n"));
154
c->mode = BADCODE; /* invalid code */
155
z->msg = (char*)"invalid literal/length code";
158
case LENEXT: /* i: getting length extra (have base) */
161
c->len += (uInt)b & inflate_mask[j];
163
c->sub.code.need = c->dbits;
164
c->sub.code.tree = c->dtree;
165
Tracevv((stderr, "inflate: length %u\n", c->len));
167
case DIST: /* i: get distance next */
168
j = c->sub.code.need;
170
t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
173
if (e & 16) /* distance */
175
c->sub.copy.get = e & 15;
176
c->sub.copy.dist = t->base;
180
if ((e & 64) == 0) /* next table */
182
c->sub.code.need = e;
183
c->sub.code.tree = t + t->base;
186
c->mode = BADCODE; /* invalid code */
187
z->msg = (char*)"invalid distance code";
190
case DISTEXT: /* i: getting distance extra */
193
c->sub.copy.dist += (uInt)b & inflate_mask[j];
195
Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist));
197
case COPY: /* o: copying bytes in window, waiting for space */
198
f = q - c->sub.copy.dist;
199
while (f < s->window) /* modulo window size-"while" instead */
200
f += s->end - s->window; /* of "if" handles invalid distances */
211
case LIT: /* o: got literal, waiting for output space */
216
case WASH: /* o: got eob, possibly more output */
217
if (k > 7) /* return unused byte, if any */
219
Assert(k < 16, "inflate_codes grabbed too many bytes")
222
p--; /* can always return one */
225
if (s->read != s->write)
231
case BADCODE: /* x: got error */
238
#ifdef NEED_DUMMY_RETURN
239
return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
244
local void inflate_codes_free( /* c, z) */
245
inflate_codes_statef *c,
249
Tracev((stderr, "inflate: codes free\n"));