annotate lib/zlib/infcodes.c @ 703:a9c1fb7483c2

monster parser almost complete.
author Gloval
date Tue, 19 Mar 2013 01:33:35 +0400
parents 9c0607679772
children
rev   line source
0
Ritor1
parents:
diff changeset
1 /* infcodes.c -- process literals and length/distance pairs
Ritor1
parents:
diff changeset
2 * Copyright (C) 1995-1998 Mark Adler
Ritor1
parents:
diff changeset
3 * For conditions of distribution and use, see copyright notice in zlib.h
Ritor1
parents:
diff changeset
4 */
Ritor1
parents:
diff changeset
5
Ritor1
parents:
diff changeset
6 #include "zutil.h"
Ritor1
parents:
diff changeset
7 #include "inftrees.h"
Ritor1
parents:
diff changeset
8 #include "infblock.h"
Ritor1
parents:
diff changeset
9 #include "infcodes.h"
Ritor1
parents:
diff changeset
10 #include "infutil.h"
Ritor1
parents:
diff changeset
11 #include "inffast.h"
Ritor1
parents:
diff changeset
12
Ritor1
parents:
diff changeset
13 /* simplify the use of the inflate_huft type with some defines */
Ritor1
parents:
diff changeset
14 #define exop word.what.Exop
Ritor1
parents:
diff changeset
15 #define bits word.what.Bits
Ritor1
parents:
diff changeset
16
Ritor1
parents:
diff changeset
17 typedef enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
Ritor1
parents:
diff changeset
18 START, /* x: set up for LEN */
Ritor1
parents:
diff changeset
19 LEN, /* i: get length/literal/eob next */
Ritor1
parents:
diff changeset
20 LENEXT, /* i: getting length extra (have base) */
Ritor1
parents:
diff changeset
21 DIST, /* i: get distance next */
Ritor1
parents:
diff changeset
22 DISTEXT, /* i: getting distance extra */
Ritor1
parents:
diff changeset
23 COPY, /* o: copying bytes in window, waiting for space */
Ritor1
parents:
diff changeset
24 LIT, /* o: got literal, waiting for output space */
Ritor1
parents:
diff changeset
25 WASH, /* o: got eob, possibly still output waiting */
Ritor1
parents:
diff changeset
26 END, /* x: got eob and all data flushed */
Ritor1
parents:
diff changeset
27 BADCODE} /* x: got error */
Ritor1
parents:
diff changeset
28 inflate_codes_mode;
Ritor1
parents:
diff changeset
29
Ritor1
parents:
diff changeset
30 /* inflate codes private state */
Ritor1
parents:
diff changeset
31 struct inflate_codes_state {
Ritor1
parents:
diff changeset
32
Ritor1
parents:
diff changeset
33 /* mode */
Ritor1
parents:
diff changeset
34 inflate_codes_mode mode; /* current inflate_codes mode */
Ritor1
parents:
diff changeset
35
Ritor1
parents:
diff changeset
36 /* mode dependent information */
Ritor1
parents:
diff changeset
37 uInt len;
Ritor1
parents:
diff changeset
38 union {
Ritor1
parents:
diff changeset
39 struct {
Ritor1
parents:
diff changeset
40 inflate_huft *tree; /* pointer into tree */
Ritor1
parents:
diff changeset
41 uInt need; /* bits needed */
Ritor1
parents:
diff changeset
42 } code; /* if LEN or DIST, where in tree */
Ritor1
parents:
diff changeset
43 uInt lit; /* if LIT, literal */
Ritor1
parents:
diff changeset
44 struct {
Ritor1
parents:
diff changeset
45 uInt get; /* bits to get for extra */
Ritor1
parents:
diff changeset
46 uInt dist; /* distance back to copy from */
Ritor1
parents:
diff changeset
47 } copy; /* if EXT or COPY, where and how much */
Ritor1
parents:
diff changeset
48 } sub; /* submode */
Ritor1
parents:
diff changeset
49
Ritor1
parents:
diff changeset
50 /* mode independent information */
Ritor1
parents:
diff changeset
51 Byte lbits; /* ltree bits decoded per branch */
Ritor1
parents:
diff changeset
52 Byte dbits; /* dtree bits decoder per branch */
Ritor1
parents:
diff changeset
53 inflate_huft *ltree; /* literal/length/eob tree */
Ritor1
parents:
diff changeset
54 inflate_huft *dtree; /* distance tree */
Ritor1
parents:
diff changeset
55
Ritor1
parents:
diff changeset
56 };
Ritor1
parents:
diff changeset
57
Ritor1
parents:
diff changeset
58
Ritor1
parents:
diff changeset
59 inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
Ritor1
parents:
diff changeset
60 uInt bl, bd;
Ritor1
parents:
diff changeset
61 inflate_huft *tl;
Ritor1
parents:
diff changeset
62 inflate_huft *td; /* need separate declaration for Borland C++ */
Ritor1
parents:
diff changeset
63 z_streamp z;
Ritor1
parents:
diff changeset
64 {
Ritor1
parents:
diff changeset
65 inflate_codes_statef *c;
Ritor1
parents:
diff changeset
66
Ritor1
parents:
diff changeset
67 if ((c = (inflate_codes_statef *)
Ritor1
parents:
diff changeset
68 ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
Ritor1
parents:
diff changeset
69 {
Ritor1
parents:
diff changeset
70 c->mode = START;
Ritor1
parents:
diff changeset
71 c->lbits = (Byte)bl;
Ritor1
parents:
diff changeset
72 c->dbits = (Byte)bd;
Ritor1
parents:
diff changeset
73 c->ltree = tl;
Ritor1
parents:
diff changeset
74 c->dtree = td;
Ritor1
parents:
diff changeset
75 Tracev((stderr, "inflate: codes new\n"));
Ritor1
parents:
diff changeset
76 }
Ritor1
parents:
diff changeset
77 return c;
Ritor1
parents:
diff changeset
78 }
Ritor1
parents:
diff changeset
79
Ritor1
parents:
diff changeset
80
Ritor1
parents:
diff changeset
81 int inflate_codes(s, z, r)
Ritor1
parents:
diff changeset
82 inflate_blocks_statef *s;
Ritor1
parents:
diff changeset
83 z_streamp z;
Ritor1
parents:
diff changeset
84 int r;
Ritor1
parents:
diff changeset
85 {
Ritor1
parents:
diff changeset
86 uInt j; /* temporary storage */
Ritor1
parents:
diff changeset
87 inflate_huft *t; /* temporary pointer */
Ritor1
parents:
diff changeset
88 uInt e; /* extra bits or operation */
Ritor1
parents:
diff changeset
89 uLong b; /* bit buffer */
Ritor1
parents:
diff changeset
90 uInt k; /* bits in bit buffer */
Ritor1
parents:
diff changeset
91 Bytef *p; /* input data pointer */
Ritor1
parents:
diff changeset
92 uInt n; /* bytes available there */
Ritor1
parents:
diff changeset
93 Bytef *q; /* output window write pointer */
Ritor1
parents:
diff changeset
94 uInt m; /* bytes to end of window or read pointer */
Ritor1
parents:
diff changeset
95 Bytef *f; /* pointer to copy strings from */
Ritor1
parents:
diff changeset
96 inflate_codes_statef *c = s->sub.decode.codes; /* codes state */
Ritor1
parents:
diff changeset
97
Ritor1
parents:
diff changeset
98 /* copy input/output information to locals (UPDATE macro restores) */
Ritor1
parents:
diff changeset
99 LOAD
Ritor1
parents:
diff changeset
100
Ritor1
parents:
diff changeset
101 /* process input and output based on current state */
Ritor1
parents:
diff changeset
102 while (1) switch (c->mode)
Ritor1
parents:
diff changeset
103 { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
Ritor1
parents:
diff changeset
104 case START: /* x: set up for LEN */
Ritor1
parents:
diff changeset
105 #ifndef SLOW
Ritor1
parents:
diff changeset
106 if (m >= 258 && n >= 10)
Ritor1
parents:
diff changeset
107 {
Ritor1
parents:
diff changeset
108 UPDATE
Ritor1
parents:
diff changeset
109 r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
Ritor1
parents:
diff changeset
110 LOAD
Ritor1
parents:
diff changeset
111 if (r != Z_OK)
Ritor1
parents:
diff changeset
112 {
Ritor1
parents:
diff changeset
113 c->mode = r == Z_STREAM_END ? WASH : BADCODE;
Ritor1
parents:
diff changeset
114 break;
Ritor1
parents:
diff changeset
115 }
Ritor1
parents:
diff changeset
116 }
Ritor1
parents:
diff changeset
117 #endif /* !SLOW */
Ritor1
parents:
diff changeset
118 c->sub.code.need = c->lbits;
Ritor1
parents:
diff changeset
119 c->sub.code.tree = c->ltree;
Ritor1
parents:
diff changeset
120 c->mode = LEN;
Ritor1
parents:
diff changeset
121 case LEN: /* i: get length/literal/eob next */
Ritor1
parents:
diff changeset
122 j = c->sub.code.need;
Ritor1
parents:
diff changeset
123 NEEDBITS(j)
Ritor1
parents:
diff changeset
124 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
Ritor1
parents:
diff changeset
125 DUMPBITS(t->bits)
Ritor1
parents:
diff changeset
126 e = (uInt)(t->exop);
Ritor1
parents:
diff changeset
127 if (e == 0) /* literal */
Ritor1
parents:
diff changeset
128 {
Ritor1
parents:
diff changeset
129 c->sub.lit = t->base;
Ritor1
parents:
diff changeset
130 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
Ritor1
parents:
diff changeset
131 "inflate: literal '%c'\n" :
Ritor1
parents:
diff changeset
132 "inflate: literal 0x%02x\n", t->base));
Ritor1
parents:
diff changeset
133 c->mode = LIT;
Ritor1
parents:
diff changeset
134 break;
Ritor1
parents:
diff changeset
135 }
Ritor1
parents:
diff changeset
136 if (e & 16) /* length */
Ritor1
parents:
diff changeset
137 {
Ritor1
parents:
diff changeset
138 c->sub.copy.get = e & 15;
Ritor1
parents:
diff changeset
139 c->len = t->base;
Ritor1
parents:
diff changeset
140 c->mode = LENEXT;
Ritor1
parents:
diff changeset
141 break;
Ritor1
parents:
diff changeset
142 }
Ritor1
parents:
diff changeset
143 if ((e & 64) == 0) /* next table */
Ritor1
parents:
diff changeset
144 {
Ritor1
parents:
diff changeset
145 c->sub.code.need = e;
Ritor1
parents:
diff changeset
146 c->sub.code.tree = t + t->base;
Ritor1
parents:
diff changeset
147 break;
Ritor1
parents:
diff changeset
148 }
Ritor1
parents:
diff changeset
149 if (e & 32) /* end of block */
Ritor1
parents:
diff changeset
150 {
Ritor1
parents:
diff changeset
151 Tracevv((stderr, "inflate: end of block\n"));
Ritor1
parents:
diff changeset
152 c->mode = WASH;
Ritor1
parents:
diff changeset
153 break;
Ritor1
parents:
diff changeset
154 }
Ritor1
parents:
diff changeset
155 c->mode = BADCODE; /* invalid code */
Ritor1
parents:
diff changeset
156 z->msg = (char*)"invalid literal/length code";
Ritor1
parents:
diff changeset
157 r = Z_DATA_ERROR;
Ritor1
parents:
diff changeset
158 LEAVE
Ritor1
parents:
diff changeset
159 case LENEXT: /* i: getting length extra (have base) */
Ritor1
parents:
diff changeset
160 j = c->sub.copy.get;
Ritor1
parents:
diff changeset
161 NEEDBITS(j)
Ritor1
parents:
diff changeset
162 c->len += (uInt)b & inflate_mask[j];
Ritor1
parents:
diff changeset
163 DUMPBITS(j)
Ritor1
parents:
diff changeset
164 c->sub.code.need = c->dbits;
Ritor1
parents:
diff changeset
165 c->sub.code.tree = c->dtree;
Ritor1
parents:
diff changeset
166 Tracevv((stderr, "inflate: length %u\n", c->len));
Ritor1
parents:
diff changeset
167 c->mode = DIST;
Ritor1
parents:
diff changeset
168 case DIST: /* i: get distance next */
Ritor1
parents:
diff changeset
169 j = c->sub.code.need;
Ritor1
parents:
diff changeset
170 NEEDBITS(j)
Ritor1
parents:
diff changeset
171 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
Ritor1
parents:
diff changeset
172 DUMPBITS(t->bits)
Ritor1
parents:
diff changeset
173 e = (uInt)(t->exop);
Ritor1
parents:
diff changeset
174 if (e & 16) /* distance */
Ritor1
parents:
diff changeset
175 {
Ritor1
parents:
diff changeset
176 c->sub.copy.get = e & 15;
Ritor1
parents:
diff changeset
177 c->sub.copy.dist = t->base;
Ritor1
parents:
diff changeset
178 c->mode = DISTEXT;
Ritor1
parents:
diff changeset
179 break;
Ritor1
parents:
diff changeset
180 }
Ritor1
parents:
diff changeset
181 if ((e & 64) == 0) /* next table */
Ritor1
parents:
diff changeset
182 {
Ritor1
parents:
diff changeset
183 c->sub.code.need = e;
Ritor1
parents:
diff changeset
184 c->sub.code.tree = t + t->base;
Ritor1
parents:
diff changeset
185 break;
Ritor1
parents:
diff changeset
186 }
Ritor1
parents:
diff changeset
187 c->mode = BADCODE; /* invalid code */
Ritor1
parents:
diff changeset
188 z->msg = (char*)"invalid distance code";
Ritor1
parents:
diff changeset
189 r = Z_DATA_ERROR;
Ritor1
parents:
diff changeset
190 LEAVE
Ritor1
parents:
diff changeset
191 case DISTEXT: /* i: getting distance extra */
Ritor1
parents:
diff changeset
192 j = c->sub.copy.get;
Ritor1
parents:
diff changeset
193 NEEDBITS(j)
Ritor1
parents:
diff changeset
194 c->sub.copy.dist += (uInt)b & inflate_mask[j];
Ritor1
parents:
diff changeset
195 DUMPBITS(j)
Ritor1
parents:
diff changeset
196 Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist));
Ritor1
parents:
diff changeset
197 c->mode = COPY;
Ritor1
parents:
diff changeset
198 case COPY: /* o: copying bytes in window, waiting for space */
Ritor1
parents:
diff changeset
199 #ifndef __TURBOC__ /* Turbo C bug for following expression */
Ritor1
parents:
diff changeset
200 f = (uInt)(q - s->window) < c->sub.copy.dist ?
Ritor1
parents:
diff changeset
201 s->end - (c->sub.copy.dist - (q - s->window)) :
Ritor1
parents:
diff changeset
202 q - c->sub.copy.dist;
Ritor1
parents:
diff changeset
203 #else
Ritor1
parents:
diff changeset
204 f = q - c->sub.copy.dist;
Ritor1
parents:
diff changeset
205 if ((uInt)(q - s->window) < c->sub.copy.dist)
Ritor1
parents:
diff changeset
206 f = s->end - (c->sub.copy.dist - (uInt)(q - s->window));
Ritor1
parents:
diff changeset
207 #endif
Ritor1
parents:
diff changeset
208 while (c->len)
Ritor1
parents:
diff changeset
209 {
Ritor1
parents:
diff changeset
210 NEEDOUT
Ritor1
parents:
diff changeset
211 OUTBYTE(*f++)
Ritor1
parents:
diff changeset
212 if (f == s->end)
Ritor1
parents:
diff changeset
213 f = s->window;
Ritor1
parents:
diff changeset
214 c->len--;
Ritor1
parents:
diff changeset
215 }
Ritor1
parents:
diff changeset
216 c->mode = START;
Ritor1
parents:
diff changeset
217 break;
Ritor1
parents:
diff changeset
218 case LIT: /* o: got literal, waiting for output space */
Ritor1
parents:
diff changeset
219 NEEDOUT
Ritor1
parents:
diff changeset
220 OUTBYTE(c->sub.lit)
Ritor1
parents:
diff changeset
221 c->mode = START;
Ritor1
parents:
diff changeset
222 break;
Ritor1
parents:
diff changeset
223 case WASH: /* o: got eob, possibly more output */
Ritor1
parents:
diff changeset
224 if (k > 7) /* return unused byte, if any */
Ritor1
parents:
diff changeset
225 {
Ritor1
parents:
diff changeset
226 Assert(k < 16, "inflate_codes grabbed too many bytes")
Ritor1
parents:
diff changeset
227 k -= 8;
Ritor1
parents:
diff changeset
228 n++;
Ritor1
parents:
diff changeset
229 p--; /* can always return one */
Ritor1
parents:
diff changeset
230 }
Ritor1
parents:
diff changeset
231 FLUSH
Ritor1
parents:
diff changeset
232 if (s->read != s->write)
Ritor1
parents:
diff changeset
233 LEAVE
Ritor1
parents:
diff changeset
234 c->mode = END;
Ritor1
parents:
diff changeset
235 case END:
Ritor1
parents:
diff changeset
236 r = Z_STREAM_END;
Ritor1
parents:
diff changeset
237 LEAVE
Ritor1
parents:
diff changeset
238 case BADCODE: /* x: got error */
Ritor1
parents:
diff changeset
239 r = Z_DATA_ERROR;
Ritor1
parents:
diff changeset
240 LEAVE
Ritor1
parents:
diff changeset
241 default:
Ritor1
parents:
diff changeset
242 r = Z_STREAM_ERROR;
Ritor1
parents:
diff changeset
243 LEAVE
Ritor1
parents:
diff changeset
244 }
Ritor1
parents:
diff changeset
245 #ifdef NEED_DUMMY_RETURN
Ritor1
parents:
diff changeset
246 return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
Ritor1
parents:
diff changeset
247 #endif
Ritor1
parents:
diff changeset
248 }
Ritor1
parents:
diff changeset
249
Ritor1
parents:
diff changeset
250
Ritor1
parents:
diff changeset
251 void inflate_codes_free(c, z)
Ritor1
parents:
diff changeset
252 inflate_codes_statef *c;
Ritor1
parents:
diff changeset
253 z_streamp z;
Ritor1
parents:
diff changeset
254 {
Ritor1
parents:
diff changeset
255 ZFREE(z, c);
Ritor1
parents:
diff changeset
256 Tracev((stderr, "inflate: codes free\n"));
Ritor1
parents:
diff changeset
257 }