1
/* $Xorg: infcodes.c,v 1.3 2000/08/17 19:46:47 cpqbld Exp $ */
3
/* infcodes.c -- process literals and length/distance pairs
4
* Copyright (C) 1995-1998 Mark Adler
5
* For conditions of distribution and use, see copyright notice in zlib.h
15
/* simplify the use of the inflate_huft type with some defines */
16
#define base more.Base
17
#define next more.Next
18
#define exop word.what.Exop
19
#define bits word.what.Bits
21
typedef enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
22
START, /* x: set up for LEN */
23
LEN, /* i: get length/literal/eob next */
24
LENEXT, /* i: getting length extra (have base) */
25
DIST, /* i: get distance next */
26
DISTEXT, /* i: getting distance extra */
27
COPY, /* o: copying bytes in window, waiting for space */
28
LIT, /* o: got literal, waiting for output space */
29
WASH, /* o: got eob, possibly still output waiting */
30
END, /* x: got eob and all data flushed */
31
BADCODE} /* x: got error */
34
/* inflate codes private state */
35
struct inflate_codes_state {
38
inflate_codes_mode mode; /* current inflate_codes mode */
40
/* mode dependent information */
44
inflate_huft *tree; /* pointer into tree */
45
uInt need; /* bits needed */
46
} code; /* if LEN or DIST, where in tree */
47
uInt lit; /* if LIT, literal */
49
uInt get; /* bits to get for extra */
50
uInt dist; /* distance back to copy from */
51
} copy; /* if EXT or COPY, where and how much */
54
/* mode independent information */
55
Byte lbits; /* ltree bits decoded per branch */
56
Byte dbits; /* dtree bits decoder per branch */
57
inflate_huft *ltree; /* literal/length/eob tree */
58
inflate_huft *dtree; /* distance tree */
63
inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
66
inflate_huft *td; /* need separate declaration for Borland C++ */
69
inflate_codes_statef *c;
71
if ((c = (inflate_codes_statef *)
72
ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
79
Tracev((stderr, "inflate: codes new\n"));
85
int inflate_codes(s, z, r)
86
inflate_blocks_statef *s;
90
uInt j; /* temporary storage */
91
inflate_huft *t; /* temporary pointer */
92
uInt e; /* extra bits or operation */
93
uLong b; /* bit buffer */
94
uInt k; /* bits in bit buffer */
95
Bytef *p; /* input data pointer */
96
uInt n; /* bytes available there */
97
Bytef *q; /* output window write pointer */
98
uInt m; /* bytes to end of window or read pointer */
99
Bytef *f; /* pointer to copy strings from */
100
inflate_codes_statef *c = s->sub.decode.codes; /* codes state */
102
/* copy input/output information to locals (UPDATE macro restores) */
105
/* process input and output based on current state */
106
while (1) switch (c->mode)
107
{ /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
108
case START: /* x: set up for LEN */
110
if (m >= 258 && n >= 10)
113
r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
117
c->mode = r == Z_STREAM_END ? WASH : BADCODE;
122
c->sub.code.need = c->lbits;
123
c->sub.code.tree = c->ltree;
125
case LEN: /* i: get length/literal/eob next */
126
j = c->sub.code.need;
128
t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
131
if (e == 0) /* literal */
133
c->sub.lit = t->base;
134
Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
135
"inflate: literal '%c'\n" :
136
"inflate: literal 0x%02x\n", t->base));
140
if (e & 16) /* length */
142
c->sub.copy.get = e & 15;
147
if ((e & 64) == 0) /* next table */
149
c->sub.code.need = e;
150
c->sub.code.tree = t->next;
153
if (e & 32) /* end of block */
155
Tracevv((stderr, "inflate: end of block\n"));
159
c->mode = BADCODE; /* invalid code */
160
z->msg = (char*)"invalid literal/length code";
163
case LENEXT: /* i: getting length extra (have base) */
166
c->len += (uInt)b & inflate_mask[j];
168
c->sub.code.need = c->dbits;
169
c->sub.code.tree = c->dtree;
170
Tracevv((stderr, "inflate: length %u\n", c->len));
172
case DIST: /* i: get distance next */
173
j = c->sub.code.need;
175
t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
178
if (e & 16) /* distance */
180
c->sub.copy.get = e & 15;
181
c->sub.copy.dist = t->base;
185
if ((e & 64) == 0) /* next table */
187
c->sub.code.need = e;
188
c->sub.code.tree = t->next;
191
c->mode = BADCODE; /* invalid code */
192
z->msg = (char*)"invalid distance code";
195
case DISTEXT: /* i: getting distance extra */
198
c->sub.copy.dist += (uInt)b & inflate_mask[j];
200
Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist));
202
case COPY: /* o: copying bytes in window, waiting for space */
203
#ifndef __TURBOC__ /* Turbo C bug for following expression */
204
f = (uInt)(q - s->window) < c->sub.copy.dist ?
205
s->end - (c->sub.copy.dist - (q - s->window)) :
206
q - c->sub.copy.dist;
208
f = q - c->sub.copy.dist;
209
if ((uInt)(q - s->window) < c->sub.copy.dist)
210
f = s->end - (c->sub.copy.dist - (uInt)(q - s->window));
222
case LIT: /* o: got literal, waiting for output space */
227
case WASH: /* o: got eob, possibly more output */
229
if (s->read != s->write)
235
case BADCODE: /* x: got error */
242
#ifdef NEED_DUMMY_RETURN
243
return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
248
void inflate_codes_free(c, z)
249
inflate_codes_statef *c;
253
Tracev((stderr, "inflate: codes free\n"));