1
2
3
4
5
6#include <linux/zutil.h>
7#include "infblock.h"
8#include "inftrees.h"
9#include "infcodes.h"
10#include "infutil.h"
11
12struct inflate_codes_state;
13
14
15#define exop word.what.Exop
16#define bits word.what.Bits
17
18
19local const uInt border[] = {
20 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68void zlib_inflate_blocks_reset(s, z, c)
69inflate_blocks_statef *s;
70z_streamp z;
71uLongf *c;
72{
73 if (c != Z_NULL)
74 *c = s->check;
75 if (s->mode == CODES)
76 zlib_inflate_codes_free(s->sub.decode.codes, z);
77 s->mode = TYPE;
78 s->bitk = 0;
79 s->bitb = 0;
80 s->read = s->write = s->window;
81 if (s->checkfn != Z_NULL)
82 z->adler = s->check = (*s->checkfn)(0L, (const Bytef *)Z_NULL, 0);
83}
84
85inflate_blocks_statef *zlib_inflate_blocks_new(z, c, w)
86z_streamp z;
87check_func c;
88uInt w;
89{
90 inflate_blocks_statef *s;
91
92 s = &WS(z)->working_blocks_state;
93 s->hufts = WS(z)->working_hufts;
94 s->window = WS(z)->working_window;
95 s->end = s->window + w;
96 s->checkfn = c;
97 s->mode = TYPE;
98 zlib_inflate_blocks_reset(s, z, Z_NULL);
99 return s;
100}
101
102
103int zlib_inflate_blocks(s, z, r)
104inflate_blocks_statef *s;
105z_streamp z;
106int r;
107{
108 uInt t;
109 uLong b;
110 uInt k;
111 Bytef *p;
112 uInt n;
113 Bytef *q;
114 uInt m;
115
116
117 LOAD
118
119
120 while (1) switch (s->mode)
121 {
122 case TYPE:
123 NEEDBITS(3)
124 t = (uInt)b & 7;
125 s->last = t & 1;
126 switch (t >> 1)
127 {
128 case 0:
129 DUMPBITS(3)
130 t = k & 7;
131 DUMPBITS(t)
132 s->mode = LENS;
133 break;
134 case 1:
135 {
136 uInt bl, bd;
137 inflate_huft *tl, *td;
138
139 zlib_inflate_trees_fixed(&bl, &bd, &tl, &td, z);
140 s->sub.decode.codes = zlib_inflate_codes_new(bl, bd, tl, td, z);
141 if (s->sub.decode.codes == Z_NULL)
142 {
143 r = Z_MEM_ERROR;
144 LEAVE
145 }
146 }
147 DUMPBITS(3)
148 s->mode = CODES;
149 break;
150 case 2:
151 DUMPBITS(3)
152 s->mode = TABLE;
153 break;
154 case 3:
155 DUMPBITS(3)
156 s->mode = B_BAD;
157 z->msg = (char*)"invalid block type";
158 r = Z_DATA_ERROR;
159 LEAVE
160 }
161 break;
162 case LENS:
163 NEEDBITS(32)
164 if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
165 {
166 s->mode = B_BAD;
167 z->msg = (char*)"invalid stored block lengths";
168 r = Z_DATA_ERROR;
169 LEAVE
170 }
171 s->sub.left = (uInt)b & 0xffff;
172 b = k = 0;
173 s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
174 break;
175 case STORED:
176 if (n == 0)
177 LEAVE
178 NEEDOUT
179 t = s->sub.left;
180 if (t > n) t = n;
181 if (t > m) t = m;
182 memcpy(q, p, t);
183 p += t; n -= t;
184 q += t; m -= t;
185 if ((s->sub.left -= t) != 0)
186 break;
187 s->mode = s->last ? DRY : TYPE;
188 break;
189 case TABLE:
190 NEEDBITS(14)
191 s->sub.trees.table = t = (uInt)b & 0x3fff;
192#ifndef PKZIP_BUG_WORKAROUND
193 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
194 {
195 s->mode = B_BAD;
196 z->msg = (char*)"too many length or distance symbols";
197 r = Z_DATA_ERROR;
198 LEAVE
199 }
200#endif
201 {
202 s->sub.trees.blens = WS(z)->working_blens;
203 }
204 DUMPBITS(14)
205 s->sub.trees.index = 0;
206 s->mode = BTREE;
207 case BTREE:
208 while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
209 {
210 NEEDBITS(3)
211 s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
212 DUMPBITS(3)
213 }
214 while (s->sub.trees.index < 19)
215 s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
216 s->sub.trees.bb = 7;
217 t = zlib_inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
218 &s->sub.trees.tb, s->hufts, z);
219 if (t != Z_OK)
220 {
221 r = t;
222 if (r == Z_DATA_ERROR)
223 s->mode = B_BAD;
224 LEAVE
225 }
226 s->sub.trees.index = 0;
227 s->mode = DTREE;
228 case DTREE:
229 while (t = s->sub.trees.table,
230 s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
231 {
232 inflate_huft *h;
233 uInt i, j, c;
234
235 t = s->sub.trees.bb;
236 NEEDBITS(t)
237 h = s->sub.trees.tb + ((uInt)b & zlib_inflate_mask[t]);
238 t = h->bits;
239 c = h->base;
240 if (c < 16)
241 {
242 DUMPBITS(t)
243 s->sub.trees.blens[s->sub.trees.index++] = c;
244 }
245 else
246 {
247 i = c == 18 ? 7 : c - 14;
248 j = c == 18 ? 11 : 3;
249 NEEDBITS(t + i)
250 DUMPBITS(t)
251 j += (uInt)b & zlib_inflate_mask[i];
252 DUMPBITS(i)
253 i = s->sub.trees.index;
254 t = s->sub.trees.table;
255 if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
256 (c == 16 && i < 1))
257 {
258 s->mode = B_BAD;
259 z->msg = (char*)"invalid bit length repeat";
260 r = Z_DATA_ERROR;
261 LEAVE
262 }
263 c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
264 do {
265 s->sub.trees.blens[i++] = c;
266 } while (--j);
267 s->sub.trees.index = i;
268 }
269 }
270 s->sub.trees.tb = Z_NULL;
271 {
272 uInt bl, bd;
273 inflate_huft *tl, *td;
274 inflate_codes_statef *c;
275
276 bl = 9;
277 bd = 6;
278 t = s->sub.trees.table;
279 t = zlib_inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
280 s->sub.trees.blens, &bl, &bd, &tl, &td,
281 s->hufts, z);
282 if (t != Z_OK)
283 {
284 if (t == (uInt)Z_DATA_ERROR)
285 s->mode = B_BAD;
286 r = t;
287 LEAVE
288 }
289 if ((c = zlib_inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
290 {
291 r = Z_MEM_ERROR;
292 LEAVE
293 }
294 s->sub.decode.codes = c;
295 }
296 s->mode = CODES;
297 case CODES:
298 UPDATE
299 if ((r = zlib_inflate_codes(s, z, r)) != Z_STREAM_END)
300 return zlib_inflate_flush(s, z, r);
301 r = Z_OK;
302 zlib_inflate_codes_free(s->sub.decode.codes, z);
303 LOAD
304 if (!s->last)
305 {
306 s->mode = TYPE;
307 break;
308 }
309 s->mode = DRY;
310 case DRY:
311 FLUSH
312 if (s->read != s->write)
313 LEAVE
314 s->mode = B_DONE;
315 case B_DONE:
316 r = Z_STREAM_END;
317 LEAVE
318 case B_BAD:
319 r = Z_DATA_ERROR;
320 LEAVE
321 default:
322 r = Z_STREAM_ERROR;
323 LEAVE
324 }
325}
326
327
328int zlib_inflate_blocks_free(s, z)
329inflate_blocks_statef *s;
330z_streamp z;
331{
332 zlib_inflate_blocks_reset(s, z, Z_NULL);
333 return Z_OK;
334}
335
336
337void zlib_inflate_set_dictionary(s, d, n)
338inflate_blocks_statef *s;
339const Bytef *d;
340uInt n;
341{
342 memcpy(s->window, d, n);
343 s->read = s->write = s->window + n;
344}
345
346
347
348
349
350
351int zlib_inflate_blocks_sync_point(s)
352inflate_blocks_statef *s;
353{
354 return s->mode == LENS;
355}
356