1 /* infcodes.c -- process literals and length/distance pairs
2 * Copyright (C) 1995 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 base more.Base
14 #define next more.Next
15 #define exop word.what.Exop
16 #define bits word.what.Bits
18 /* inflate codes private state */
19 struct inflate_codes_state {
22 enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
23 START, /* x: set up for LEN */
24 LEN, /* i: get length/literal/eob next */
25 LENEXT, /* i: getting length extra (have base) */
26 DIST, /* i: get distance next */
27 DISTEXT, /* i: getting distance extra */
28 COPY, /* o: copying bytes in window, waiting for space */
29 LIT, /* o: got literal, waiting for output space */
30 WASH, /* o: got eob, possibly still output waiting */
31 END, /* x: got eob and all data flushed */
32 BAD} /* x: got error */
33 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 struct inflate_codes_state *inflate_codes_new(bl, bd, tl, td, z)
60 inflate_huft *tl, *td;
63 struct inflate_codes_state *c;
65 if ((c = (struct inflate_codes_state *)
66 ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
78 int inflate_codes(s, z, r)
79 struct inflate_blocks_state *s;
83 uInt j; /* temporary storage */
84 inflate_huft *t; /* temporary pointer */
85 int e; /* extra bits or operation */
86 uLong b; /* bit buffer */
87 uInt k; /* bits in bit buffer */
88 Byte *p; /* input data pointer */
89 uInt n; /* bytes available there */
90 Byte *q; /* output window write pointer */
91 uInt m; /* bytes to end of window or read pointer */
92 Byte *f; /* pointer to copy strings from */
93 struct inflate_codes_state *c = s->sub.codes; /* codes state */
95 /* copy input/output information to locals (UPDATE macro restores) */
98 /* process input and output based on current state */
99 while (1) switch (c->mode)
100 { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
101 case START: /* x: set up for LEN */
103 if (m >= 258 && n >= 10)
106 r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
110 c->mode = r == Z_STREAM_END ? WASH : BAD;
115 c->sub.code.need = c->lbits;
116 c->sub.code.tree = c->ltree;
118 case LEN: /* i: get length/literal/eob next */
119 j = c->sub.code.need;
121 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
123 if ((e = (int)(t->exop)) < 0)
125 if (e == -128) /* invalid code */
128 z->msg = "invalid literal/length code";
133 if (e & 64) /* end of block */
138 c->sub.code.need = e;
139 c->sub.code.tree = t->next;
142 if (e & 16) /* literal */
144 c->sub.lit = t->base;
151 case LENEXT: /* i: getting length extra (have base) */
154 c->len += (uInt)b & inflate_mask[j];
156 c->sub.code.need = c->dbits;
157 c->sub.code.tree = c->dtree;
159 case DIST: /* i: get distance next */
160 j = c->sub.code.need;
162 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
164 if ((e = (int)(t->exop)) < 0)
169 z->msg = "invalid distance code";
173 c->sub.code.need = -e;
174 c->sub.code.tree = t->next;
177 c->sub.copy.dist = t->base;
180 case DISTEXT: /* i: getting distance extra */
183 c->sub.copy.dist += (uInt)b & inflate_mask[j];
186 case COPY: /* o: copying bytes in window, waiting for space */
187 f = (uInt)(q - s->window) < c->sub.copy.dist ?
188 s->end - (c->sub.copy.dist - (q - s->window)) :
189 q - c->sub.copy.dist;
200 case LIT: /* o: got literal, waiting for output space */
205 case WASH: /* o: got eob, possibly more output */
207 if (s->read != s->write)
213 case BAD: /* x: got error */
223 void inflate_codes_free(c, z)
224 struct inflate_codes_state *c;
227 inflate_trees_free(c->dtree, z);
228 inflate_trees_free(c->ltree, z);