1 /* infcodes.c -- process literals and length/distance pairs
2 * Copyright (C) 1995-1998 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
6 #include <linux/zutil.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 inflate_codes_statef *zlib_inflate_codes_new(bl, bd, tl, td, z)
20 inflate_huft *td; /* need separate declaration for Borland C++ */
23 inflate_codes_statef *c;
25 c = &WS(z)->working_state;
37 int zlib_inflate_codes(s, z, r)
38 inflate_blocks_statef *s;
42 uInt j; /* temporary storage */
43 inflate_huft *t; /* temporary pointer */
44 uInt e; /* extra bits or operation */
45 uLong b; /* bit buffer */
46 uInt k; /* bits in bit buffer */
47 Bytef *p; /* input data pointer */
48 uInt n; /* bytes available there */
49 Bytef *q; /* output window write pointer */
50 uInt m; /* bytes to end of window or read pointer */
51 Bytef *f; /* pointer to copy strings from */
52 inflate_codes_statef *c = s->sub.decode.codes; /* codes state */
54 /* copy input/output information to locals (UPDATE macro restores) */
57 /* process input and output based on current state */
58 while (1) switch (c->mode)
59 { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
60 case START: /* x: set up for LEN */
62 if (m >= 258 && n >= 10)
65 r = zlib_inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
69 c->mode = r == Z_STREAM_END ? WASH : BADCODE;
74 c->sub.code.need = c->lbits;
75 c->sub.code.tree = c->ltree;
77 case LEN: /* i: get length/literal/eob next */
80 t = c->sub.code.tree + ((uInt)b & zlib_inflate_mask[j]);
83 if (e == 0) /* literal */
89 if (e & 16) /* length */
91 c->sub.copy.get = e & 15;
96 if ((e & 64) == 0) /* next table */
99 c->sub.code.tree = t + t->base;
102 if (e & 32) /* end of block */
107 c->mode = BADCODE; /* invalid code */
108 z->msg = (char*)"invalid literal/length code";
111 case LENEXT: /* i: getting length extra (have base) */
114 c->len += (uInt)b & zlib_inflate_mask[j];
116 c->sub.code.need = c->dbits;
117 c->sub.code.tree = c->dtree;
119 case DIST: /* i: get distance next */
120 j = c->sub.code.need;
122 t = c->sub.code.tree + ((uInt)b & zlib_inflate_mask[j]);
125 if (e & 16) /* distance */
127 c->sub.copy.get = e & 15;
128 c->sub.copy.dist = t->base;
132 if ((e & 64) == 0) /* next table */
134 c->sub.code.need = e;
135 c->sub.code.tree = t + t->base;
138 c->mode = BADCODE; /* invalid code */
139 z->msg = (char*)"invalid distance code";
142 case DISTEXT: /* i: getting distance extra */
145 c->sub.copy.dist += (uInt)b & zlib_inflate_mask[j];
148 case COPY: /* o: copying bytes in window, waiting for space */
149 #ifndef __TURBOC__ /* Turbo C bug for following expression */
150 f = (uInt)(q - s->window) < c->sub.copy.dist ?
151 s->end - (c->sub.copy.dist - (q - s->window)) :
152 q - c->sub.copy.dist;
154 f = q - c->sub.copy.dist;
155 if ((uInt)(q - s->window) < c->sub.copy.dist)
156 f = s->end - (c->sub.copy.dist - (uInt)(q - s->window));
168 case LIT: /* o: got literal, waiting for output space */
173 case WASH: /* o: got eob, possibly more output */
174 if (k > 7) /* return unused byte, if any */
178 p--; /* can always return one */
181 if (s->read != s->write)
187 case BADCODE: /* x: got error */
194 #ifdef NEED_DUMMY_RETURN
195 return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
200 void zlib_inflate_codes_free(c, z)
201 inflate_codes_statef *c;