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
13
/* simplify the use of the inflate_huft type with some defines */
14
#define exop word.what.Exop
15
#define bits word.what.Bits
17
typedef enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
18
START, /* x: set up for LEN */
19
LEN, /* i: get length/literal/eob next */
20
LENEXT, /* i: getting length extra (have base) */
21
DIST, /* i: get distance next */
22
DISTEXT, /* i: getting distance extra */
23
COPY, /* o: copying bytes in window, waiting for space */
24
LIT, /* o: got literal, waiting for output space */
25
WASH, /* o: got eob, possibly still output waiting */
26
END, /* x: got eob and all data flushed */
27
BADCODE} /* x: got error */
30
/* inflate codes private state */
31
struct inflate_codes_state {
34
inflate_codes_mode mode; /* current inflate_codes mode */
36
/* mode dependent information */
40
inflate_huft *tree; /* pointer into tree */
41
uInt need; /* bits needed */
42
} code; /* if LEN or DIST, where in tree */
43
uInt lit; /* if LIT, literal */
45
uInt get; /* bits to get for extra */
46
uInt dist; /* distance back to copy from */
47
} copy; /* if EXT or COPY, where and how much */
50
/* mode independent information */
51
Byte lbits; /* ltree bits decoded per branch */
52
Byte dbits; /* dtree bits decoder per branch */
53
inflate_huft *ltree; /* literal/length/eob tree */
54
inflate_huft *dtree; /* distance tree */
59
inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
62
inflate_huft *td; /* need separate declaration for Borland C++ */
65
inflate_codes_statef *c;
67
if ((c = (inflate_codes_statef *)
68
ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
75
Tracev((stderr, "inflate: codes new\n"));
81
int inflate_codes(s, z, r)
82
inflate_blocks_statef *s;
86
uInt j; /* temporary storage */
87
inflate_huft *t; /* temporary pointer */
88
uInt e; /* extra bits or operation */
89
uLong b; /* bit buffer */
90
uInt k; /* bits in bit buffer */
91
Bytef *p; /* input data pointer */
92
uInt n; /* bytes available there */
93
Bytef *q; /* output window write pointer */
94
uInt m; /* bytes to end of window or read pointer */
95
Bytef *f; /* pointer to copy strings from */
96
inflate_codes_statef *c = s->sub.decode.codes; /* codes state */
98
/* copy input/output information to locals (UPDATE macro restores) */
101
/* process input and output based on current state */
102
while (1) switch (c->mode)
103
{ /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
104
case START: /* x: set up for LEN */
106
if (m >= 258 && n >= 10)
109
r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
113
c->mode = r == Z_STREAM_END ? WASH : BADCODE;
118
c->sub.code.need = c->lbits;
119
c->sub.code.tree = c->ltree;
121
case LEN: /* i: get length/literal/eob next */
122
j = c->sub.code.need;
124
t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
127
if (e == 0) /* literal */
129
c->sub.lit = t->base;
130
Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
131
"inflate: literal '%c'\n" :
132
"inflate: literal 0x%02x\n", t->base));
136
if (e & 16) /* length */
138
c->sub.copy.get = e & 15;
143
if ((e & 64) == 0) /* next table */
145
c->sub.code.need = e;
146
c->sub.code.tree = t + t->base;
149
if (e & 32) /* end of block */
151
Tracevv((stderr, "inflate: end of block\n"));
155
c->mode = BADCODE; /* invalid code */
156
z->msg = (char*)"invalid literal/length code";
159
case LENEXT: /* i: getting length extra (have base) */
162
c->len += (uInt)b & inflate_mask[j];
164
c->sub.code.need = c->dbits;
165
c->sub.code.tree = c->dtree;
166
Tracevv((stderr, "inflate: length %u\n", c->len));
168
case DIST: /* i: get distance next */
169
j = c->sub.code.need;
171
t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
174
if (e & 16) /* distance */
176
c->sub.copy.get = e & 15;
177
c->sub.copy.dist = t->base;
181
if ((e & 64) == 0) /* next table */
183
c->sub.code.need = e;
184
c->sub.code.tree = t + t->base;
187
c->mode = BADCODE; /* invalid code */
188
z->msg = (char*)"invalid distance code";
191
case DISTEXT: /* i: getting distance extra */
194
c->sub.copy.dist += (uInt)b & inflate_mask[j];
196
Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist));
198
case COPY: /* o: copying bytes in window, waiting for space */
199
f = q - c->sub.copy.dist;
200
while (f < s->window) /* modulo window size-"while" instead */
201
f += s->end - s->window; /* of "if" handles invalid distances */
212
case LIT: /* o: got literal, waiting for output space */
217
case WASH: /* o: got eob, possibly more output */
218
if (k > 7) /* return unused byte, if any */
220
Assert(k < 16, "inflate_codes grabbed too many bytes")
223
p--; /* can always return one */
226
if (s->read != s->write)
232
case BADCODE: /* x: got error */
239
#ifdef NEED_DUMMY_RETURN
240
return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
245
void inflate_codes_free(c, z)
246
inflate_codes_statef *c;
250
Tracev((stderr, "inflate: codes free\n"));