diff options
Diffstat (limited to 'lib/zlib_inflate')
-rw-r--r-- | lib/zlib_inflate/Makefile | 19 | ||||
-rw-r--r-- | lib/zlib_inflate/infblock.c | 361 | ||||
-rw-r--r-- | lib/zlib_inflate/infblock.h | 44 | ||||
-rw-r--r-- | lib/zlib_inflate/infcodes.c | 202 | ||||
-rw-r--r-- | lib/zlib_inflate/infcodes.h | 33 | ||||
-rw-r--r-- | lib/zlib_inflate/inffast.c | 176 | ||||
-rw-r--r-- | lib/zlib_inflate/inffast.h | 17 | ||||
-rw-r--r-- | lib/zlib_inflate/inflate.c | 248 | ||||
-rw-r--r-- | lib/zlib_inflate/inflate_syms.c | 22 | ||||
-rw-r--r-- | lib/zlib_inflate/inflate_sync.c | 148 | ||||
-rw-r--r-- | lib/zlib_inflate/inftrees.c | 412 | ||||
-rw-r--r-- | lib/zlib_inflate/inftrees.h | 64 | ||||
-rw-r--r-- | lib/zlib_inflate/infutil.c | 88 | ||||
-rw-r--r-- | lib/zlib_inflate/infutil.h | 197 |
14 files changed, 2031 insertions, 0 deletions
diff --git a/lib/zlib_inflate/Makefile b/lib/zlib_inflate/Makefile new file mode 100644 index 000000000000..221c139e0df1 --- /dev/null +++ b/lib/zlib_inflate/Makefile | |||
@@ -0,0 +1,19 @@ | |||
1 | # | ||
2 | # This is a modified version of zlib, which does all memory | ||
3 | # allocation ahead of time. | ||
4 | # | ||
5 | # This is only the decompression, see zlib_deflate for the | ||
6 | # the compression | ||
7 | # | ||
8 | # Decompression needs to be serialized for each memory | ||
9 | # allocation. | ||
10 | # | ||
11 | # (The upsides of the simplification is that you can't get in | ||
12 | # any nasty situations wrt memory management, and that the | ||
13 | # uncompression can be done without blocking on allocation). | ||
14 | # | ||
15 | |||
16 | obj-$(CONFIG_ZLIB_INFLATE) += zlib_inflate.o | ||
17 | |||
18 | zlib_inflate-objs := infblock.o infcodes.o inffast.o inflate.o \ | ||
19 | inflate_sync.o inftrees.o infutil.o inflate_syms.o | ||
diff --git a/lib/zlib_inflate/infblock.c b/lib/zlib_inflate/infblock.c new file mode 100644 index 000000000000..50f21ca4ef7f --- /dev/null +++ b/lib/zlib_inflate/infblock.c | |||
@@ -0,0 +1,361 @@ | |||
1 | /* infblock.c -- interpret and process block types to last block | ||
2 | * Copyright (C) 1995-1998 Mark Adler | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | #include <linux/zutil.h> | ||
7 | #include "infblock.h" | ||
8 | #include "inftrees.h" | ||
9 | #include "infcodes.h" | ||
10 | #include "infutil.h" | ||
11 | |||
12 | struct inflate_codes_state; | ||
13 | |||
14 | /* simplify the use of the inflate_huft type with some defines */ | ||
15 | #define exop word.what.Exop | ||
16 | #define bits word.what.Bits | ||
17 | |||
18 | /* Table for deflate from PKZIP's appnote.txt. */ | ||
19 | static const uInt border[] = { /* Order of the bit length code lengths */ | ||
20 | 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; | ||
21 | |||
22 | /* | ||
23 | Notes beyond the 1.93a appnote.txt: | ||
24 | |||
25 | 1. Distance pointers never point before the beginning of the output | ||
26 | stream. | ||
27 | 2. Distance pointers can point back across blocks, up to 32k away. | ||
28 | 3. There is an implied maximum of 7 bits for the bit length table and | ||
29 | 15 bits for the actual data. | ||
30 | 4. If only one code exists, then it is encoded using one bit. (Zero | ||
31 | would be more efficient, but perhaps a little confusing.) If two | ||
32 | codes exist, they are coded using one bit each (0 and 1). | ||
33 | 5. There is no way of sending zero distance codes--a dummy must be | ||
34 | sent if there are none. (History: a pre 2.0 version of PKZIP would | ||
35 | store blocks with no distance codes, but this was discovered to be | ||
36 | too harsh a criterion.) Valid only for 1.93a. 2.04c does allow | ||
37 | zero distance codes, which is sent as one code of zero bits in | ||
38 | length. | ||
39 | 6. There are up to 286 literal/length codes. Code 256 represents the | ||
40 | end-of-block. Note however that the static length tree defines | ||
41 | 288 codes just to fill out the Huffman codes. Codes 286 and 287 | ||
42 | cannot be used though, since there is no length base or extra bits | ||
43 | defined for them. Similarily, there are up to 30 distance codes. | ||
44 | However, static trees define 32 codes (all 5 bits) to fill out the | ||
45 | Huffman codes, but the last two had better not show up in the data. | ||
46 | 7. Unzip can check dynamic Huffman blocks for complete code sets. | ||
47 | The exception is that a single code would not be complete (see #4). | ||
48 | 8. The five bits following the block type is really the number of | ||
49 | literal codes sent minus 257. | ||
50 | 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits | ||
51 | (1+6+6). Therefore, to output three times the length, you output | ||
52 | three codes (1+1+1), whereas to output four times the same length, | ||
53 | you only need two codes (1+3). Hmm. | ||
54 | 10. In the tree reconstruction algorithm, Code = Code + Increment | ||
55 | only if BitLength(i) is not zero. (Pretty obvious.) | ||
56 | 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19) | ||
57 | 12. Note: length code 284 can represent 227-258, but length code 285 | ||
58 | really is 258. The last length deserves its own, short code | ||
59 | since it gets used a lot in very redundant files. The length | ||
60 | 258 is special since 258 - 3 (the min match length) is 255. | ||
61 | 13. The literal/length and distance code bit lengths are read as a | ||
62 | single stream of lengths. It is possible (and advantageous) for | ||
63 | a repeat code (16, 17, or 18) to go across the boundary between | ||
64 | the two sets of lengths. | ||
65 | */ | ||
66 | |||
67 | |||
68 | void zlib_inflate_blocks_reset( | ||
69 | inflate_blocks_statef *s, | ||
70 | z_streamp z, | ||
71 | uLong *c | ||
72 | ) | ||
73 | { | ||
74 | if (c != NULL) | ||
75 | *c = s->check; | ||
76 | if (s->mode == CODES) | ||
77 | zlib_inflate_codes_free(s->sub.decode.codes, z); | ||
78 | s->mode = TYPE; | ||
79 | s->bitk = 0; | ||
80 | s->bitb = 0; | ||
81 | s->read = s->write = s->window; | ||
82 | if (s->checkfn != NULL) | ||
83 | z->adler = s->check = (*s->checkfn)(0L, NULL, 0); | ||
84 | } | ||
85 | |||
86 | inflate_blocks_statef *zlib_inflate_blocks_new( | ||
87 | z_streamp z, | ||
88 | check_func c, | ||
89 | uInt w | ||
90 | ) | ||
91 | { | ||
92 | inflate_blocks_statef *s; | ||
93 | |||
94 | s = &WS(z)->working_blocks_state; | ||
95 | s->hufts = WS(z)->working_hufts; | ||
96 | s->window = WS(z)->working_window; | ||
97 | s->end = s->window + w; | ||
98 | s->checkfn = c; | ||
99 | s->mode = TYPE; | ||
100 | zlib_inflate_blocks_reset(s, z, NULL); | ||
101 | return s; | ||
102 | } | ||
103 | |||
104 | |||
105 | int zlib_inflate_blocks( | ||
106 | inflate_blocks_statef *s, | ||
107 | z_streamp z, | ||
108 | int r | ||
109 | ) | ||
110 | { | ||
111 | uInt t; /* temporary storage */ | ||
112 | uLong b; /* bit buffer */ | ||
113 | uInt k; /* bits in bit buffer */ | ||
114 | Byte *p; /* input data pointer */ | ||
115 | uInt n; /* bytes available there */ | ||
116 | Byte *q; /* output window write pointer */ | ||
117 | uInt m; /* bytes to end of window or read pointer */ | ||
118 | |||
119 | /* copy input/output information to locals (UPDATE macro restores) */ | ||
120 | LOAD | ||
121 | |||
122 | /* process input based on current state */ | ||
123 | while (1) switch (s->mode) | ||
124 | { | ||
125 | case TYPE: | ||
126 | NEEDBITS(3) | ||
127 | t = (uInt)b & 7; | ||
128 | s->last = t & 1; | ||
129 | switch (t >> 1) | ||
130 | { | ||
131 | case 0: /* stored */ | ||
132 | DUMPBITS(3) | ||
133 | t = k & 7; /* go to byte boundary */ | ||
134 | DUMPBITS(t) | ||
135 | s->mode = LENS; /* get length of stored block */ | ||
136 | break; | ||
137 | case 1: /* fixed */ | ||
138 | { | ||
139 | uInt bl, bd; | ||
140 | inflate_huft *tl, *td; | ||
141 | |||
142 | zlib_inflate_trees_fixed(&bl, &bd, &tl, &td, s->hufts, z); | ||
143 | s->sub.decode.codes = zlib_inflate_codes_new(bl, bd, tl, td, z); | ||
144 | if (s->sub.decode.codes == NULL) | ||
145 | { | ||
146 | r = Z_MEM_ERROR; | ||
147 | LEAVE | ||
148 | } | ||
149 | } | ||
150 | DUMPBITS(3) | ||
151 | s->mode = CODES; | ||
152 | break; | ||
153 | case 2: /* dynamic */ | ||
154 | DUMPBITS(3) | ||
155 | s->mode = TABLE; | ||
156 | break; | ||
157 | case 3: /* illegal */ | ||
158 | DUMPBITS(3) | ||
159 | s->mode = B_BAD; | ||
160 | z->msg = (char*)"invalid block type"; | ||
161 | r = Z_DATA_ERROR; | ||
162 | LEAVE | ||
163 | } | ||
164 | break; | ||
165 | case LENS: | ||
166 | NEEDBITS(32) | ||
167 | if ((((~b) >> 16) & 0xffff) != (b & 0xffff)) | ||
168 | { | ||
169 | s->mode = B_BAD; | ||
170 | z->msg = (char*)"invalid stored block lengths"; | ||
171 | r = Z_DATA_ERROR; | ||
172 | LEAVE | ||
173 | } | ||
174 | s->sub.left = (uInt)b & 0xffff; | ||
175 | b = k = 0; /* dump bits */ | ||
176 | s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE); | ||
177 | break; | ||
178 | case STORED: | ||
179 | if (n == 0) | ||
180 | LEAVE | ||
181 | NEEDOUT | ||
182 | t = s->sub.left; | ||
183 | if (t > n) t = n; | ||
184 | if (t > m) t = m; | ||
185 | memcpy(q, p, t); | ||
186 | p += t; n -= t; | ||
187 | q += t; m -= t; | ||
188 | if ((s->sub.left -= t) != 0) | ||
189 | break; | ||
190 | s->mode = s->last ? DRY : TYPE; | ||
191 | break; | ||
192 | case TABLE: | ||
193 | NEEDBITS(14) | ||
194 | s->sub.trees.table = t = (uInt)b & 0x3fff; | ||
195 | #ifndef PKZIP_BUG_WORKAROUND | ||
196 | if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29) | ||
197 | { | ||
198 | s->mode = B_BAD; | ||
199 | z->msg = (char*)"too many length or distance symbols"; | ||
200 | r = Z_DATA_ERROR; | ||
201 | LEAVE | ||
202 | } | ||
203 | #endif | ||
204 | { | ||
205 | s->sub.trees.blens = WS(z)->working_blens; | ||
206 | } | ||
207 | DUMPBITS(14) | ||
208 | s->sub.trees.index = 0; | ||
209 | s->mode = BTREE; | ||
210 | case BTREE: | ||
211 | while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10)) | ||
212 | { | ||
213 | NEEDBITS(3) | ||
214 | s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7; | ||
215 | DUMPBITS(3) | ||
216 | } | ||
217 | while (s->sub.trees.index < 19) | ||
218 | s->sub.trees.blens[border[s->sub.trees.index++]] = 0; | ||
219 | s->sub.trees.bb = 7; | ||
220 | t = zlib_inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb, | ||
221 | &s->sub.trees.tb, s->hufts, z); | ||
222 | if (t != Z_OK) | ||
223 | { | ||
224 | r = t; | ||
225 | if (r == Z_DATA_ERROR) | ||
226 | s->mode = B_BAD; | ||
227 | LEAVE | ||
228 | } | ||
229 | s->sub.trees.index = 0; | ||
230 | s->mode = DTREE; | ||
231 | case DTREE: | ||
232 | while (t = s->sub.trees.table, | ||
233 | s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f)) | ||
234 | { | ||
235 | inflate_huft *h; | ||
236 | uInt i, j, c; | ||
237 | |||
238 | t = s->sub.trees.bb; | ||
239 | NEEDBITS(t) | ||
240 | h = s->sub.trees.tb + ((uInt)b & zlib_inflate_mask[t]); | ||
241 | t = h->bits; | ||
242 | c = h->base; | ||
243 | if (c < 16) | ||
244 | { | ||
245 | DUMPBITS(t) | ||
246 | s->sub.trees.blens[s->sub.trees.index++] = c; | ||
247 | } | ||
248 | else /* c == 16..18 */ | ||
249 | { | ||
250 | i = c == 18 ? 7 : c - 14; | ||
251 | j = c == 18 ? 11 : 3; | ||
252 | NEEDBITS(t + i) | ||
253 | DUMPBITS(t) | ||
254 | j += (uInt)b & zlib_inflate_mask[i]; | ||
255 | DUMPBITS(i) | ||
256 | i = s->sub.trees.index; | ||
257 | t = s->sub.trees.table; | ||
258 | if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) || | ||
259 | (c == 16 && i < 1)) | ||
260 | { | ||
261 | s->mode = B_BAD; | ||
262 | z->msg = (char*)"invalid bit length repeat"; | ||
263 | r = Z_DATA_ERROR; | ||
264 | LEAVE | ||
265 | } | ||
266 | c = c == 16 ? s->sub.trees.blens[i - 1] : 0; | ||
267 | do { | ||
268 | s->sub.trees.blens[i++] = c; | ||
269 | } while (--j); | ||
270 | s->sub.trees.index = i; | ||
271 | } | ||
272 | } | ||
273 | s->sub.trees.tb = NULL; | ||
274 | { | ||
275 | uInt bl, bd; | ||
276 | inflate_huft *tl, *td; | ||
277 | inflate_codes_statef *c; | ||
278 | |||
279 | bl = 9; /* must be <= 9 for lookahead assumptions */ | ||
280 | bd = 6; /* must be <= 9 for lookahead assumptions */ | ||
281 | t = s->sub.trees.table; | ||
282 | t = zlib_inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f), | ||
283 | s->sub.trees.blens, &bl, &bd, &tl, &td, | ||
284 | s->hufts, z); | ||
285 | if (t != Z_OK) | ||
286 | { | ||
287 | if (t == (uInt)Z_DATA_ERROR) | ||
288 | s->mode = B_BAD; | ||
289 | r = t; | ||
290 | LEAVE | ||
291 | } | ||
292 | if ((c = zlib_inflate_codes_new(bl, bd, tl, td, z)) == NULL) | ||
293 | { | ||
294 | r = Z_MEM_ERROR; | ||
295 | LEAVE | ||
296 | } | ||
297 | s->sub.decode.codes = c; | ||
298 | } | ||
299 | s->mode = CODES; | ||
300 | case CODES: | ||
301 | UPDATE | ||
302 | if ((r = zlib_inflate_codes(s, z, r)) != Z_STREAM_END) | ||
303 | return zlib_inflate_flush(s, z, r); | ||
304 | r = Z_OK; | ||
305 | zlib_inflate_codes_free(s->sub.decode.codes, z); | ||
306 | LOAD | ||
307 | if (!s->last) | ||
308 | { | ||
309 | s->mode = TYPE; | ||
310 | break; | ||
311 | } | ||
312 | s->mode = DRY; | ||
313 | case DRY: | ||
314 | FLUSH | ||
315 | if (s->read != s->write) | ||
316 | LEAVE | ||
317 | s->mode = B_DONE; | ||
318 | case B_DONE: | ||
319 | r = Z_STREAM_END; | ||
320 | LEAVE | ||
321 | case B_BAD: | ||
322 | r = Z_DATA_ERROR; | ||
323 | LEAVE | ||
324 | default: | ||
325 | r = Z_STREAM_ERROR; | ||
326 | LEAVE | ||
327 | } | ||
328 | } | ||
329 | |||
330 | |||
331 | int zlib_inflate_blocks_free( | ||
332 | inflate_blocks_statef *s, | ||
333 | z_streamp z | ||
334 | ) | ||
335 | { | ||
336 | zlib_inflate_blocks_reset(s, z, NULL); | ||
337 | return Z_OK; | ||
338 | } | ||
339 | |||
340 | |||
341 | void zlib_inflate_set_dictionary( | ||
342 | inflate_blocks_statef *s, | ||
343 | const Byte *d, | ||
344 | uInt n | ||
345 | ) | ||
346 | { | ||
347 | memcpy(s->window, d, n); | ||
348 | s->read = s->write = s->window + n; | ||
349 | } | ||
350 | |||
351 | |||
352 | /* Returns true if inflate is currently at the end of a block generated | ||
353 | * by Z_SYNC_FLUSH or Z_FULL_FLUSH. | ||
354 | * IN assertion: s != NULL | ||
355 | */ | ||
356 | int zlib_inflate_blocks_sync_point( | ||
357 | inflate_blocks_statef *s | ||
358 | ) | ||
359 | { | ||
360 | return s->mode == LENS; | ||
361 | } | ||
diff --git a/lib/zlib_inflate/infblock.h b/lib/zlib_inflate/infblock.h new file mode 100644 index 000000000000..f5221ddf6054 --- /dev/null +++ b/lib/zlib_inflate/infblock.h | |||
@@ -0,0 +1,44 @@ | |||
1 | /* infblock.h -- header to use infblock.c | ||
2 | * Copyright (C) 1995-1998 Mark Adler | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | /* WARNING: this file should *not* be used by applications. It is | ||
7 | part of the implementation of the compression library and is | ||
8 | subject to change. Applications should only use zlib.h. | ||
9 | */ | ||
10 | |||
11 | #ifndef _INFBLOCK_H | ||
12 | #define _INFBLOCK_H | ||
13 | |||
14 | struct inflate_blocks_state; | ||
15 | typedef struct inflate_blocks_state inflate_blocks_statef; | ||
16 | |||
17 | extern inflate_blocks_statef * zlib_inflate_blocks_new ( | ||
18 | z_streamp z, | ||
19 | check_func c, /* check function */ | ||
20 | uInt w); /* window size */ | ||
21 | |||
22 | extern int zlib_inflate_blocks ( | ||
23 | inflate_blocks_statef *, | ||
24 | z_streamp , | ||
25 | int); /* initial return code */ | ||
26 | |||
27 | extern void zlib_inflate_blocks_reset ( | ||
28 | inflate_blocks_statef *, | ||
29 | z_streamp , | ||
30 | uLong *); /* check value on output */ | ||
31 | |||
32 | extern int zlib_inflate_blocks_free ( | ||
33 | inflate_blocks_statef *, | ||
34 | z_streamp); | ||
35 | |||
36 | extern void zlib_inflate_set_dictionary ( | ||
37 | inflate_blocks_statef *s, | ||
38 | const Byte *d, /* dictionary */ | ||
39 | uInt n); /* dictionary length */ | ||
40 | |||
41 | extern int zlib_inflate_blocks_sync_point ( | ||
42 | inflate_blocks_statef *s); | ||
43 | |||
44 | #endif /* _INFBLOCK_H */ | ||
diff --git a/lib/zlib_inflate/infcodes.c b/lib/zlib_inflate/infcodes.c new file mode 100644 index 000000000000..07cd7591cbb7 --- /dev/null +++ b/lib/zlib_inflate/infcodes.c | |||
@@ -0,0 +1,202 @@ | |||
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 | ||
4 | */ | ||
5 | |||
6 | #include <linux/zutil.h> | ||
7 | #include "inftrees.h" | ||
8 | #include "infblock.h" | ||
9 | #include "infcodes.h" | ||
10 | #include "infutil.h" | ||
11 | #include "inffast.h" | ||
12 | |||
13 | /* simplify the use of the inflate_huft type with some defines */ | ||
14 | #define exop word.what.Exop | ||
15 | #define bits word.what.Bits | ||
16 | |||
17 | inflate_codes_statef *zlib_inflate_codes_new( | ||
18 | uInt bl, | ||
19 | uInt bd, | ||
20 | inflate_huft *tl, | ||
21 | inflate_huft *td, /* need separate declaration for Borland C++ */ | ||
22 | z_streamp z | ||
23 | ) | ||
24 | { | ||
25 | inflate_codes_statef *c; | ||
26 | |||
27 | c = &WS(z)->working_state; | ||
28 | { | ||
29 | c->mode = START; | ||
30 | c->lbits = (Byte)bl; | ||
31 | c->dbits = (Byte)bd; | ||
32 | c->ltree = tl; | ||
33 | c->dtree = td; | ||
34 | } | ||
35 | return c; | ||
36 | } | ||
37 | |||
38 | |||
39 | int zlib_inflate_codes( | ||
40 | inflate_blocks_statef *s, | ||
41 | z_streamp z, | ||
42 | int r | ||
43 | ) | ||
44 | { | ||
45 | uInt j; /* temporary storage */ | ||
46 | inflate_huft *t; /* temporary pointer */ | ||
47 | uInt e; /* extra bits or operation */ | ||
48 | uLong b; /* bit buffer */ | ||
49 | uInt k; /* bits in bit buffer */ | ||
50 | Byte *p; /* input data pointer */ | ||
51 | uInt n; /* bytes available there */ | ||
52 | Byte *q; /* output window write pointer */ | ||
53 | uInt m; /* bytes to end of window or read pointer */ | ||
54 | Byte *f; /* pointer to copy strings from */ | ||
55 | inflate_codes_statef *c = s->sub.decode.codes; /* codes state */ | ||
56 | |||
57 | /* copy input/output information to locals (UPDATE macro restores) */ | ||
58 | LOAD | ||
59 | |||
60 | /* process input and output based on current state */ | ||
61 | while (1) switch (c->mode) | ||
62 | { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */ | ||
63 | case START: /* x: set up for LEN */ | ||
64 | #ifndef SLOW | ||
65 | if (m >= 258 && n >= 10) | ||
66 | { | ||
67 | UPDATE | ||
68 | r = zlib_inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z); | ||
69 | LOAD | ||
70 | if (r != Z_OK) | ||
71 | { | ||
72 | c->mode = r == Z_STREAM_END ? WASH : BADCODE; | ||
73 | break; | ||
74 | } | ||
75 | } | ||
76 | #endif /* !SLOW */ | ||
77 | c->sub.code.need = c->lbits; | ||
78 | c->sub.code.tree = c->ltree; | ||
79 | c->mode = LEN; | ||
80 | case LEN: /* i: get length/literal/eob next */ | ||
81 | j = c->sub.code.need; | ||
82 | NEEDBITS(j) | ||
83 | t = c->sub.code.tree + ((uInt)b & zlib_inflate_mask[j]); | ||
84 | DUMPBITS(t->bits) | ||
85 | e = (uInt)(t->exop); | ||
86 | if (e == 0) /* literal */ | ||
87 | { | ||
88 | c->sub.lit = t->base; | ||
89 | c->mode = LIT; | ||
90 | break; | ||
91 | } | ||
92 | if (e & 16) /* length */ | ||
93 | { | ||
94 | c->sub.copy.get = e & 15; | ||
95 | c->len = t->base; | ||
96 | c->mode = LENEXT; | ||
97 | break; | ||
98 | } | ||
99 | if ((e & 64) == 0) /* next table */ | ||
100 | { | ||
101 | c->sub.code.need = e; | ||
102 | c->sub.code.tree = t + t->base; | ||
103 | break; | ||
104 | } | ||
105 | if (e & 32) /* end of block */ | ||
106 | { | ||
107 | c->mode = WASH; | ||
108 | break; | ||
109 | } | ||
110 | c->mode = BADCODE; /* invalid code */ | ||
111 | z->msg = (char*)"invalid literal/length code"; | ||
112 | r = Z_DATA_ERROR; | ||
113 | LEAVE | ||
114 | case LENEXT: /* i: getting length extra (have base) */ | ||
115 | j = c->sub.copy.get; | ||
116 | NEEDBITS(j) | ||
117 | c->len += (uInt)b & zlib_inflate_mask[j]; | ||
118 | DUMPBITS(j) | ||
119 | c->sub.code.need = c->dbits; | ||
120 | c->sub.code.tree = c->dtree; | ||
121 | c->mode = DIST; | ||
122 | case DIST: /* i: get distance next */ | ||
123 | j = c->sub.code.need; | ||
124 | NEEDBITS(j) | ||
125 | t = c->sub.code.tree + ((uInt)b & zlib_inflate_mask[j]); | ||
126 | DUMPBITS(t->bits) | ||
127 | e = (uInt)(t->exop); | ||
128 | if (e & 16) /* distance */ | ||
129 | { | ||
130 | c->sub.copy.get = e & 15; | ||
131 | c->sub.copy.dist = t->base; | ||
132 | c->mode = DISTEXT; | ||
133 | break; | ||
134 | } | ||
135 | if ((e & 64) == 0) /* next table */ | ||
136 | { | ||
137 | c->sub.code.need = e; | ||
138 | c->sub.code.tree = t + t->base; | ||
139 | break; | ||
140 | } | ||
141 | c->mode = BADCODE; /* invalid code */ | ||
142 | z->msg = (char*)"invalid distance code"; | ||
143 | r = Z_DATA_ERROR; | ||
144 | LEAVE | ||
145 | case DISTEXT: /* i: getting distance extra */ | ||
146 | j = c->sub.copy.get; | ||
147 | NEEDBITS(j) | ||
148 | c->sub.copy.dist += (uInt)b & zlib_inflate_mask[j]; | ||
149 | DUMPBITS(j) | ||
150 | c->mode = COPY; | ||
151 | case COPY: /* o: copying bytes in window, waiting for space */ | ||
152 | f = q - c->sub.copy.dist; | ||
153 | while (f < s->window) /* modulo window size-"while" instead */ | ||
154 | f += s->end - s->window; /* of "if" handles invalid distances */ | ||
155 | while (c->len) | ||
156 | { | ||
157 | NEEDOUT | ||
158 | OUTBYTE(*f++) | ||
159 | if (f == s->end) | ||
160 | f = s->window; | ||
161 | c->len--; | ||
162 | } | ||
163 | c->mode = START; | ||
164 | break; | ||
165 | case LIT: /* o: got literal, waiting for output space */ | ||
166 | NEEDOUT | ||
167 | OUTBYTE(c->sub.lit) | ||
168 | c->mode = START; | ||
169 | break; | ||
170 | case WASH: /* o: got eob, possibly more output */ | ||
171 | if (k > 7) /* return unused byte, if any */ | ||
172 | { | ||
173 | k -= 8; | ||
174 | n++; | ||
175 | p--; /* can always return one */ | ||
176 | } | ||
177 | FLUSH | ||
178 | if (s->read != s->write) | ||
179 | LEAVE | ||
180 | c->mode = END; | ||
181 | case END: | ||
182 | r = Z_STREAM_END; | ||
183 | LEAVE | ||
184 | case BADCODE: /* x: got error */ | ||
185 | r = Z_DATA_ERROR; | ||
186 | LEAVE | ||
187 | default: | ||
188 | r = Z_STREAM_ERROR; | ||
189 | LEAVE | ||
190 | } | ||
191 | #ifdef NEED_DUMMY_RETURN | ||
192 | return Z_STREAM_ERROR; /* Some dumb compilers complain without this */ | ||
193 | #endif | ||
194 | } | ||
195 | |||
196 | |||
197 | void zlib_inflate_codes_free( | ||
198 | inflate_codes_statef *c, | ||
199 | z_streamp z | ||
200 | ) | ||
201 | { | ||
202 | } | ||
diff --git a/lib/zlib_inflate/infcodes.h b/lib/zlib_inflate/infcodes.h new file mode 100644 index 000000000000..5cff417523b0 --- /dev/null +++ b/lib/zlib_inflate/infcodes.h | |||
@@ -0,0 +1,33 @@ | |||
1 | /* infcodes.h -- header to use infcodes.c | ||
2 | * Copyright (C) 1995-1998 Mark Adler | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | /* WARNING: this file should *not* be used by applications. It is | ||
7 | part of the implementation of the compression library and is | ||
8 | subject to change. Applications should only use zlib.h. | ||
9 | */ | ||
10 | |||
11 | #ifndef _INFCODES_H | ||
12 | #define _INFCODES_H | ||
13 | |||
14 | #include "infblock.h" | ||
15 | |||
16 | struct inflate_codes_state; | ||
17 | typedef struct inflate_codes_state inflate_codes_statef; | ||
18 | |||
19 | extern inflate_codes_statef *zlib_inflate_codes_new ( | ||
20 | uInt, uInt, | ||
21 | inflate_huft *, inflate_huft *, | ||
22 | z_streamp ); | ||
23 | |||
24 | extern int zlib_inflate_codes ( | ||
25 | inflate_blocks_statef *, | ||
26 | z_streamp , | ||
27 | int); | ||
28 | |||
29 | extern void zlib_inflate_codes_free ( | ||
30 | inflate_codes_statef *, | ||
31 | z_streamp ); | ||
32 | |||
33 | #endif /* _INFCODES_H */ | ||
diff --git a/lib/zlib_inflate/inffast.c b/lib/zlib_inflate/inffast.c new file mode 100644 index 000000000000..0bd7623fc85a --- /dev/null +++ b/lib/zlib_inflate/inffast.c | |||
@@ -0,0 +1,176 @@ | |||
1 | /* inffast.c -- process literals and length/distance pairs fast | ||
2 | * Copyright (C) 1995-1998 Mark Adler | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | #include <linux/zutil.h> | ||
7 | #include "inftrees.h" | ||
8 | #include "infblock.h" | ||
9 | #include "infcodes.h" | ||
10 | #include "infutil.h" | ||
11 | #include "inffast.h" | ||
12 | |||
13 | struct inflate_codes_state; | ||
14 | |||
15 | /* simplify the use of the inflate_huft type with some defines */ | ||
16 | #define exop word.what.Exop | ||
17 | #define bits word.what.Bits | ||
18 | |||
19 | /* macros for bit input with no checking and for returning unused bytes */ | ||
20 | #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}} | ||
21 | #define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;} | ||
22 | |||
23 | /* Called with number of bytes left to write in window at least 258 | ||
24 | (the maximum string length) and number of input bytes available | ||
25 | at least ten. The ten bytes are six bytes for the longest length/ | ||
26 | distance pair plus four bytes for overloading the bit buffer. */ | ||
27 | |||
28 | int zlib_inflate_fast( | ||
29 | uInt bl, | ||
30 | uInt bd, | ||
31 | inflate_huft *tl, | ||
32 | inflate_huft *td, /* need separate declaration for Borland C++ */ | ||
33 | inflate_blocks_statef *s, | ||
34 | z_streamp z | ||
35 | ) | ||
36 | { | ||
37 | inflate_huft *t; /* temporary pointer */ | ||
38 | uInt e; /* extra bits or operation */ | ||
39 | uLong b; /* bit buffer */ | ||
40 | uInt k; /* bits in bit buffer */ | ||
41 | Byte *p; /* input data pointer */ | ||
42 | uInt n; /* bytes available there */ | ||
43 | Byte *q; /* output window write pointer */ | ||
44 | uInt m; /* bytes to end of window or read pointer */ | ||
45 | uInt ml; /* mask for literal/length tree */ | ||
46 | uInt md; /* mask for distance tree */ | ||
47 | uInt c; /* bytes to copy */ | ||
48 | uInt d; /* distance back to copy from */ | ||
49 | Byte *r; /* copy source pointer */ | ||
50 | |||
51 | /* load input, output, bit values */ | ||
52 | LOAD | ||
53 | |||
54 | /* initialize masks */ | ||
55 | ml = zlib_inflate_mask[bl]; | ||
56 | md = zlib_inflate_mask[bd]; | ||
57 | |||
58 | /* do until not enough input or output space for fast loop */ | ||
59 | do { /* assume called with m >= 258 && n >= 10 */ | ||
60 | /* get literal/length code */ | ||
61 | GRABBITS(20) /* max bits for literal/length code */ | ||
62 | if ((e = (t = tl + ((uInt)b & ml))->exop) == 0) | ||
63 | { | ||
64 | DUMPBITS(t->bits) | ||
65 | *q++ = (Byte)t->base; | ||
66 | m--; | ||
67 | continue; | ||
68 | } | ||
69 | do { | ||
70 | DUMPBITS(t->bits) | ||
71 | if (e & 16) | ||
72 | { | ||
73 | /* get extra bits for length */ | ||
74 | e &= 15; | ||
75 | c = t->base + ((uInt)b & zlib_inflate_mask[e]); | ||
76 | DUMPBITS(e) | ||
77 | |||
78 | /* decode distance base of block to copy */ | ||
79 | GRABBITS(15); /* max bits for distance code */ | ||
80 | e = (t = td + ((uInt)b & md))->exop; | ||
81 | do { | ||
82 | DUMPBITS(t->bits) | ||
83 | if (e & 16) | ||
84 | { | ||
85 | /* get extra bits to add to distance base */ | ||
86 | e &= 15; | ||
87 | GRABBITS(e) /* get extra bits (up to 13) */ | ||
88 | d = t->base + ((uInt)b & zlib_inflate_mask[e]); | ||
89 | DUMPBITS(e) | ||
90 | |||
91 | /* do the copy */ | ||
92 | m -= c; | ||
93 | r = q - d; | ||
94 | if (r < s->window) /* wrap if needed */ | ||
95 | { | ||
96 | do { | ||
97 | r += s->end - s->window; /* force pointer in window */ | ||
98 | } while (r < s->window); /* covers invalid distances */ | ||
99 | e = s->end - r; | ||
100 | if (c > e) | ||
101 | { | ||
102 | c -= e; /* wrapped copy */ | ||
103 | do { | ||
104 | *q++ = *r++; | ||
105 | } while (--e); | ||
106 | r = s->window; | ||
107 | do { | ||
108 | *q++ = *r++; | ||
109 | } while (--c); | ||
110 | } | ||
111 | else /* normal copy */ | ||
112 | { | ||
113 | *q++ = *r++; c--; | ||
114 | *q++ = *r++; c--; | ||
115 | do { | ||
116 | *q++ = *r++; | ||
117 | } while (--c); | ||
118 | } | ||
119 | } | ||
120 | else /* normal copy */ | ||
121 | { | ||
122 | *q++ = *r++; c--; | ||
123 | *q++ = *r++; c--; | ||
124 | do { | ||
125 | *q++ = *r++; | ||
126 | } while (--c); | ||
127 | } | ||
128 | break; | ||
129 | } | ||
130 | else if ((e & 64) == 0) | ||
131 | { | ||
132 | t += t->base; | ||
133 | e = (t += ((uInt)b & zlib_inflate_mask[e]))->exop; | ||
134 | } | ||
135 | else | ||
136 | { | ||
137 | z->msg = (char*)"invalid distance code"; | ||
138 | UNGRAB | ||
139 | UPDATE | ||
140 | return Z_DATA_ERROR; | ||
141 | } | ||
142 | } while (1); | ||
143 | break; | ||
144 | } | ||
145 | if ((e & 64) == 0) | ||
146 | { | ||
147 | t += t->base; | ||
148 | if ((e = (t += ((uInt)b & zlib_inflate_mask[e]))->exop) == 0) | ||
149 | { | ||
150 | DUMPBITS(t->bits) | ||
151 | *q++ = (Byte)t->base; | ||
152 | m--; | ||
153 | break; | ||
154 | } | ||
155 | } | ||
156 | else if (e & 32) | ||
157 | { | ||
158 | UNGRAB | ||
159 | UPDATE | ||
160 | return Z_STREAM_END; | ||
161 | } | ||
162 | else | ||
163 | { | ||
164 | z->msg = (char*)"invalid literal/length code"; | ||
165 | UNGRAB | ||
166 | UPDATE | ||
167 | return Z_DATA_ERROR; | ||
168 | } | ||
169 | } while (1); | ||
170 | } while (m >= 258 && n >= 10); | ||
171 | |||
172 | /* not enough input or output--restore pointers and return */ | ||
173 | UNGRAB | ||
174 | UPDATE | ||
175 | return Z_OK; | ||
176 | } | ||
diff --git a/lib/zlib_inflate/inffast.h b/lib/zlib_inflate/inffast.h new file mode 100644 index 000000000000..fc720f0fa7f5 --- /dev/null +++ b/lib/zlib_inflate/inffast.h | |||
@@ -0,0 +1,17 @@ | |||
1 | /* inffast.h -- header to use inffast.c | ||
2 | * Copyright (C) 1995-1998 Mark Adler | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | /* WARNING: this file should *not* be used by applications. It is | ||
7 | part of the implementation of the compression library and is | ||
8 | subject to change. Applications should only use zlib.h. | ||
9 | */ | ||
10 | |||
11 | extern int zlib_inflate_fast ( | ||
12 | uInt, | ||
13 | uInt, | ||
14 | inflate_huft *, | ||
15 | inflate_huft *, | ||
16 | inflate_blocks_statef *, | ||
17 | z_streamp ); | ||
diff --git a/lib/zlib_inflate/inflate.c b/lib/zlib_inflate/inflate.c new file mode 100644 index 000000000000..3d94cb90c1d3 --- /dev/null +++ b/lib/zlib_inflate/inflate.c | |||
@@ -0,0 +1,248 @@ | |||
1 | /* inflate.c -- zlib interface to inflate modules | ||
2 | * Copyright (C) 1995-1998 Mark Adler | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | #include <linux/module.h> | ||
7 | #include <linux/zutil.h> | ||
8 | #include "infblock.h" | ||
9 | #include "infutil.h" | ||
10 | |||
11 | int zlib_inflate_workspacesize(void) | ||
12 | { | ||
13 | return sizeof(struct inflate_workspace); | ||
14 | } | ||
15 | |||
16 | |||
17 | int zlib_inflateReset( | ||
18 | z_streamp z | ||
19 | ) | ||
20 | { | ||
21 | if (z == NULL || z->state == NULL || z->workspace == NULL) | ||
22 | return Z_STREAM_ERROR; | ||
23 | z->total_in = z->total_out = 0; | ||
24 | z->msg = NULL; | ||
25 | z->state->mode = z->state->nowrap ? BLOCKS : METHOD; | ||
26 | zlib_inflate_blocks_reset(z->state->blocks, z, NULL); | ||
27 | return Z_OK; | ||
28 | } | ||
29 | |||
30 | |||
31 | int zlib_inflateEnd( | ||
32 | z_streamp z | ||
33 | ) | ||
34 | { | ||
35 | if (z == NULL || z->state == NULL || z->workspace == NULL) | ||
36 | return Z_STREAM_ERROR; | ||
37 | if (z->state->blocks != NULL) | ||
38 | zlib_inflate_blocks_free(z->state->blocks, z); | ||
39 | z->state = NULL; | ||
40 | return Z_OK; | ||
41 | } | ||
42 | |||
43 | |||
44 | int zlib_inflateInit2_( | ||
45 | z_streamp z, | ||
46 | int w, | ||
47 | const char *version, | ||
48 | int stream_size | ||
49 | ) | ||
50 | { | ||
51 | if (version == NULL || version[0] != ZLIB_VERSION[0] || | ||
52 | stream_size != sizeof(z_stream) || z->workspace == NULL) | ||
53 | return Z_VERSION_ERROR; | ||
54 | |||
55 | /* initialize state */ | ||
56 | z->msg = NULL; | ||
57 | z->state = &WS(z)->internal_state; | ||
58 | z->state->blocks = NULL; | ||
59 | |||
60 | /* handle undocumented nowrap option (no zlib header or check) */ | ||
61 | z->state->nowrap = 0; | ||
62 | if (w < 0) | ||
63 | { | ||
64 | w = - w; | ||
65 | z->state->nowrap = 1; | ||
66 | } | ||
67 | |||
68 | /* set window size */ | ||
69 | if (w < 8 || w > 15) | ||
70 | { | ||
71 | zlib_inflateEnd(z); | ||
72 | return Z_STREAM_ERROR; | ||
73 | } | ||
74 | z->state->wbits = (uInt)w; | ||
75 | |||
76 | /* create inflate_blocks state */ | ||
77 | if ((z->state->blocks = | ||
78 | zlib_inflate_blocks_new(z, z->state->nowrap ? NULL : zlib_adler32, (uInt)1 << w)) | ||
79 | == NULL) | ||
80 | { | ||
81 | zlib_inflateEnd(z); | ||
82 | return Z_MEM_ERROR; | ||
83 | } | ||
84 | |||
85 | /* reset state */ | ||
86 | zlib_inflateReset(z); | ||
87 | return Z_OK; | ||
88 | } | ||
89 | |||
90 | |||
91 | /* | ||
92 | * At the end of a Deflate-compressed PPP packet, we expect to have seen | ||
93 | * a `stored' block type value but not the (zero) length bytes. | ||
94 | */ | ||
95 | static int zlib_inflate_packet_flush(inflate_blocks_statef *s) | ||
96 | { | ||
97 | if (s->mode != LENS) | ||
98 | return Z_DATA_ERROR; | ||
99 | s->mode = TYPE; | ||
100 | return Z_OK; | ||
101 | } | ||
102 | |||
103 | |||
104 | int zlib_inflateInit_( | ||
105 | z_streamp z, | ||
106 | const char *version, | ||
107 | int stream_size | ||
108 | ) | ||
109 | { | ||
110 | return zlib_inflateInit2_(z, DEF_WBITS, version, stream_size); | ||
111 | } | ||
112 | |||
113 | #undef NEEDBYTE | ||
114 | #undef NEXTBYTE | ||
115 | #define NEEDBYTE {if(z->avail_in==0)goto empty;r=trv;} | ||
116 | #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++) | ||
117 | |||
118 | int zlib_inflate( | ||
119 | z_streamp z, | ||
120 | int f | ||
121 | ) | ||
122 | { | ||
123 | int r, trv; | ||
124 | uInt b; | ||
125 | |||
126 | if (z == NULL || z->state == NULL || z->next_in == NULL) | ||
127 | return Z_STREAM_ERROR; | ||
128 | trv = f == Z_FINISH ? Z_BUF_ERROR : Z_OK; | ||
129 | r = Z_BUF_ERROR; | ||
130 | while (1) switch (z->state->mode) | ||
131 | { | ||
132 | case METHOD: | ||
133 | NEEDBYTE | ||
134 | if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED) | ||
135 | { | ||
136 | z->state->mode = I_BAD; | ||
137 | z->msg = (char*)"unknown compression method"; | ||
138 | z->state->sub.marker = 5; /* can't try inflateSync */ | ||
139 | break; | ||
140 | } | ||
141 | if ((z->state->sub.method >> 4) + 8 > z->state->wbits) | ||
142 | { | ||
143 | z->state->mode = I_BAD; | ||
144 | z->msg = (char*)"invalid window size"; | ||
145 | z->state->sub.marker = 5; /* can't try inflateSync */ | ||
146 | break; | ||
147 | } | ||
148 | z->state->mode = FLAG; | ||
149 | case FLAG: | ||
150 | NEEDBYTE | ||
151 | b = NEXTBYTE; | ||
152 | if (((z->state->sub.method << 8) + b) % 31) | ||
153 | { | ||
154 | z->state->mode = I_BAD; | ||
155 | z->msg = (char*)"incorrect header check"; | ||
156 | z->state->sub.marker = 5; /* can't try inflateSync */ | ||
157 | break; | ||
158 | } | ||
159 | if (!(b & PRESET_DICT)) | ||
160 | { | ||
161 | z->state->mode = BLOCKS; | ||
162 | break; | ||
163 | } | ||
164 | z->state->mode = DICT4; | ||
165 | case DICT4: | ||
166 | NEEDBYTE | ||
167 | z->state->sub.check.need = (uLong)NEXTBYTE << 24; | ||
168 | z->state->mode = DICT3; | ||
169 | case DICT3: | ||
170 | NEEDBYTE | ||
171 | z->state->sub.check.need += (uLong)NEXTBYTE << 16; | ||
172 | z->state->mode = DICT2; | ||
173 | case DICT2: | ||
174 | NEEDBYTE | ||
175 | z->state->sub.check.need += (uLong)NEXTBYTE << 8; | ||
176 | z->state->mode = DICT1; | ||
177 | case DICT1: | ||
178 | NEEDBYTE | ||
179 | z->state->sub.check.need += (uLong)NEXTBYTE; | ||
180 | z->adler = z->state->sub.check.need; | ||
181 | z->state->mode = DICT0; | ||
182 | return Z_NEED_DICT; | ||
183 | case DICT0: | ||
184 | z->state->mode = I_BAD; | ||
185 | z->msg = (char*)"need dictionary"; | ||
186 | z->state->sub.marker = 0; /* can try inflateSync */ | ||
187 | return Z_STREAM_ERROR; | ||
188 | case BLOCKS: | ||
189 | r = zlib_inflate_blocks(z->state->blocks, z, r); | ||
190 | if (f == Z_PACKET_FLUSH && z->avail_in == 0 && z->avail_out != 0) | ||
191 | r = zlib_inflate_packet_flush(z->state->blocks); | ||
192 | if (r == Z_DATA_ERROR) | ||
193 | { | ||
194 | z->state->mode = I_BAD; | ||
195 | z->state->sub.marker = 0; /* can try inflateSync */ | ||
196 | break; | ||
197 | } | ||
198 | if (r == Z_OK) | ||
199 | r = trv; | ||
200 | if (r != Z_STREAM_END) | ||
201 | return r; | ||
202 | r = trv; | ||
203 | zlib_inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was); | ||
204 | if (z->state->nowrap) | ||
205 | { | ||
206 | z->state->mode = I_DONE; | ||
207 | break; | ||
208 | } | ||
209 | z->state->mode = CHECK4; | ||
210 | case CHECK4: | ||
211 | NEEDBYTE | ||
212 | z->state->sub.check.need = (uLong)NEXTBYTE << 24; | ||
213 | z->state->mode = CHECK3; | ||
214 | case CHECK3: | ||
215 | NEEDBYTE | ||
216 | z->state->sub.check.need += (uLong)NEXTBYTE << 16; | ||
217 | z->state->mode = CHECK2; | ||
218 | case CHECK2: | ||
219 | NEEDBYTE | ||
220 | z->state->sub.check.need += (uLong)NEXTBYTE << 8; | ||
221 | z->state->mode = CHECK1; | ||
222 | case CHECK1: | ||
223 | NEEDBYTE | ||
224 | z->state->sub.check.need += (uLong)NEXTBYTE; | ||
225 | |||
226 | if (z->state->sub.check.was != z->state->sub.check.need) | ||
227 | { | ||
228 | z->state->mode = I_BAD; | ||
229 | z->msg = (char*)"incorrect data check"; | ||
230 | z->state->sub.marker = 5; /* can't try inflateSync */ | ||
231 | break; | ||
232 | } | ||
233 | z->state->mode = I_DONE; | ||
234 | case I_DONE: | ||
235 | return Z_STREAM_END; | ||
236 | case I_BAD: | ||
237 | return Z_DATA_ERROR; | ||
238 | default: | ||
239 | return Z_STREAM_ERROR; | ||
240 | } | ||
241 | empty: | ||
242 | if (f != Z_PACKET_FLUSH) | ||
243 | return r; | ||
244 | z->state->mode = I_BAD; | ||
245 | z->msg = (char *)"need more for packet flush"; | ||
246 | z->state->sub.marker = 0; /* can try inflateSync */ | ||
247 | return Z_DATA_ERROR; | ||
248 | } | ||
diff --git a/lib/zlib_inflate/inflate_syms.c b/lib/zlib_inflate/inflate_syms.c new file mode 100644 index 000000000000..aa1b08189121 --- /dev/null +++ b/lib/zlib_inflate/inflate_syms.c | |||
@@ -0,0 +1,22 @@ | |||
1 | /* | ||
2 | * linux/lib/zlib_inflate/inflate_syms.c | ||
3 | * | ||
4 | * Exported symbols for the inflate functionality. | ||
5 | * | ||
6 | */ | ||
7 | |||
8 | #include <linux/module.h> | ||
9 | #include <linux/init.h> | ||
10 | |||
11 | #include <linux/zlib.h> | ||
12 | |||
13 | EXPORT_SYMBOL(zlib_inflate_workspacesize); | ||
14 | EXPORT_SYMBOL(zlib_inflate); | ||
15 | EXPORT_SYMBOL(zlib_inflateInit_); | ||
16 | EXPORT_SYMBOL(zlib_inflateInit2_); | ||
17 | EXPORT_SYMBOL(zlib_inflateEnd); | ||
18 | EXPORT_SYMBOL(zlib_inflateSync); | ||
19 | EXPORT_SYMBOL(zlib_inflateReset); | ||
20 | EXPORT_SYMBOL(zlib_inflateSyncPoint); | ||
21 | EXPORT_SYMBOL(zlib_inflateIncomp); | ||
22 | MODULE_LICENSE("GPL"); | ||
diff --git a/lib/zlib_inflate/inflate_sync.c b/lib/zlib_inflate/inflate_sync.c new file mode 100644 index 000000000000..e07bdb21f55c --- /dev/null +++ b/lib/zlib_inflate/inflate_sync.c | |||
@@ -0,0 +1,148 @@ | |||
1 | /* inflate.c -- zlib interface to inflate modules | ||
2 | * Copyright (C) 1995-1998 Mark Adler | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | #include <linux/zutil.h> | ||
7 | #include "infblock.h" | ||
8 | #include "infutil.h" | ||
9 | |||
10 | int zlib_inflateSync( | ||
11 | z_streamp z | ||
12 | ) | ||
13 | { | ||
14 | uInt n; /* number of bytes to look at */ | ||
15 | Byte *p; /* pointer to bytes */ | ||
16 | uInt m; /* number of marker bytes found in a row */ | ||
17 | uLong r, w; /* temporaries to save total_in and total_out */ | ||
18 | |||
19 | /* set up */ | ||
20 | if (z == NULL || z->state == NULL) | ||
21 | return Z_STREAM_ERROR; | ||
22 | if (z->state->mode != I_BAD) | ||
23 | { | ||
24 | z->state->mode = I_BAD; | ||
25 | z->state->sub.marker = 0; | ||
26 | } | ||
27 | if ((n = z->avail_in) == 0) | ||
28 | return Z_BUF_ERROR; | ||
29 | p = z->next_in; | ||
30 | m = z->state->sub.marker; | ||
31 | |||
32 | /* search */ | ||
33 | while (n && m < 4) | ||
34 | { | ||
35 | static const Byte mark[4] = {0, 0, 0xff, 0xff}; | ||
36 | if (*p == mark[m]) | ||
37 | m++; | ||
38 | else if (*p) | ||
39 | m = 0; | ||
40 | else | ||
41 | m = 4 - m; | ||
42 | p++, n--; | ||
43 | } | ||
44 | |||
45 | /* restore */ | ||
46 | z->total_in += p - z->next_in; | ||
47 | z->next_in = p; | ||
48 | z->avail_in = n; | ||
49 | z->state->sub.marker = m; | ||
50 | |||
51 | /* return no joy or set up to restart on a new block */ | ||
52 | if (m != 4) | ||
53 | return Z_DATA_ERROR; | ||
54 | r = z->total_in; w = z->total_out; | ||
55 | zlib_inflateReset(z); | ||
56 | z->total_in = r; z->total_out = w; | ||
57 | z->state->mode = BLOCKS; | ||
58 | return Z_OK; | ||
59 | } | ||
60 | |||
61 | |||
62 | /* Returns true if inflate is currently at the end of a block generated | ||
63 | * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP | ||
64 | * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH | ||
65 | * but removes the length bytes of the resulting empty stored block. When | ||
66 | * decompressing, PPP checks that at the end of input packet, inflate is | ||
67 | * waiting for these length bytes. | ||
68 | */ | ||
69 | int zlib_inflateSyncPoint( | ||
70 | z_streamp z | ||
71 | ) | ||
72 | { | ||
73 | if (z == NULL || z->state == NULL || z->state->blocks == NULL) | ||
74 | return Z_STREAM_ERROR; | ||
75 | return zlib_inflate_blocks_sync_point(z->state->blocks); | ||
76 | } | ||
77 | |||
78 | /* | ||
79 | * This subroutine adds the data at next_in/avail_in to the output history | ||
80 | * without performing any output. The output buffer must be "caught up"; | ||
81 | * i.e. no pending output (hence s->read equals s->write), and the state must | ||
82 | * be BLOCKS (i.e. we should be willing to see the start of a series of | ||
83 | * BLOCKS). On exit, the output will also be caught up, and the checksum | ||
84 | * will have been updated if need be. | ||
85 | */ | ||
86 | static int zlib_inflate_addhistory(inflate_blocks_statef *s, | ||
87 | z_stream *z) | ||
88 | { | ||
89 | uLong b; /* bit buffer */ /* NOT USED HERE */ | ||
90 | uInt k; /* bits in bit buffer */ /* NOT USED HERE */ | ||
91 | uInt t; /* temporary storage */ | ||
92 | Byte *p; /* input data pointer */ | ||
93 | uInt n; /* bytes available there */ | ||
94 | Byte *q; /* output window write pointer */ | ||
95 | uInt m; /* bytes to end of window or read pointer */ | ||
96 | |||
97 | if (s->read != s->write) | ||
98 | return Z_STREAM_ERROR; | ||
99 | if (s->mode != TYPE) | ||
100 | return Z_DATA_ERROR; | ||
101 | |||
102 | /* we're ready to rock */ | ||
103 | LOAD | ||
104 | /* while there is input ready, copy to output buffer, moving | ||
105 | * pointers as needed. | ||
106 | */ | ||
107 | while (n) { | ||
108 | t = n; /* how many to do */ | ||
109 | /* is there room until end of buffer? */ | ||
110 | if (t > m) t = m; | ||
111 | /* update check information */ | ||
112 | if (s->checkfn != NULL) | ||
113 | s->check = (*s->checkfn)(s->check, q, t); | ||
114 | memcpy(q, p, t); | ||
115 | q += t; | ||
116 | p += t; | ||
117 | n -= t; | ||
118 | z->total_out += t; | ||
119 | s->read = q; /* drag read pointer forward */ | ||
120 | /* WWRAP */ /* expand WWRAP macro by hand to handle s->read */ | ||
121 | if (q == s->end) { | ||
122 | s->read = q = s->window; | ||
123 | m = WAVAIL; | ||
124 | } | ||
125 | } | ||
126 | UPDATE | ||
127 | return Z_OK; | ||
128 | } | ||
129 | |||
130 | |||
131 | /* | ||
132 | * This subroutine adds the data at next_in/avail_in to the output history | ||
133 | * without performing any output. The output buffer must be "caught up"; | ||
134 | * i.e. no pending output (hence s->read equals s->write), and the state must | ||
135 | * be BLOCKS (i.e. we should be willing to see the start of a series of | ||
136 | * BLOCKS). On exit, the output will also be caught up, and the checksum | ||
137 | * will have been updated if need be. | ||
138 | */ | ||
139 | |||
140 | int zlib_inflateIncomp( | ||
141 | z_stream *z | ||
142 | |||
143 | ) | ||
144 | { | ||
145 | if (z->state->mode != BLOCKS) | ||
146 | return Z_DATA_ERROR; | ||
147 | return zlib_inflate_addhistory(z->state->blocks, z); | ||
148 | } | ||
diff --git a/lib/zlib_inflate/inftrees.c b/lib/zlib_inflate/inftrees.c new file mode 100644 index 000000000000..874950ec4858 --- /dev/null +++ b/lib/zlib_inflate/inftrees.c | |||
@@ -0,0 +1,412 @@ | |||
1 | /* inftrees.c -- generate Huffman trees for efficient decoding | ||
2 | * Copyright (C) 1995-1998 Mark Adler | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | #include <linux/zutil.h> | ||
7 | #include "inftrees.h" | ||
8 | #include "infutil.h" | ||
9 | |||
10 | static const char inflate_copyright[] __attribute_used__ = | ||
11 | " inflate 1.1.3 Copyright 1995-1998 Mark Adler "; | ||
12 | /* | ||
13 | If you use the zlib library in a product, an acknowledgment is welcome | ||
14 | in the documentation of your product. If for some reason you cannot | ||
15 | include such an acknowledgment, I would appreciate that you keep this | ||
16 | copyright string in the executable of your product. | ||
17 | */ | ||
18 | struct internal_state; | ||
19 | |||
20 | /* simplify the use of the inflate_huft type with some defines */ | ||
21 | #define exop word.what.Exop | ||
22 | #define bits word.what.Bits | ||
23 | |||
24 | |||
25 | static int huft_build ( | ||
26 | uInt *, /* code lengths in bits */ | ||
27 | uInt, /* number of codes */ | ||
28 | uInt, /* number of "simple" codes */ | ||
29 | const uInt *, /* list of base values for non-simple codes */ | ||
30 | const uInt *, /* list of extra bits for non-simple codes */ | ||
31 | inflate_huft **, /* result: starting table */ | ||
32 | uInt *, /* maximum lookup bits (returns actual) */ | ||
33 | inflate_huft *, /* space for trees */ | ||
34 | uInt *, /* hufts used in space */ | ||
35 | uInt * ); /* space for values */ | ||
36 | |||
37 | /* Tables for deflate from PKZIP's appnote.txt. */ | ||
38 | static const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */ | ||
39 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, | ||
40 | 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; | ||
41 | /* see note #13 above about 258 */ | ||
42 | static const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */ | ||
43 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, | ||
44 | 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */ | ||
45 | static const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */ | ||
46 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, | ||
47 | 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, | ||
48 | 8193, 12289, 16385, 24577}; | ||
49 | static const uInt cpdext[30] = { /* Extra bits for distance codes */ | ||
50 | 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, | ||
51 | 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, | ||
52 | 12, 12, 13, 13}; | ||
53 | |||
54 | /* | ||
55 | Huffman code decoding is performed using a multi-level table lookup. | ||
56 | The fastest way to decode is to simply build a lookup table whose | ||
57 | size is determined by the longest code. However, the time it takes | ||
58 | to build this table can also be a factor if the data being decoded | ||
59 | is not very long. The most common codes are necessarily the | ||
60 | shortest codes, so those codes dominate the decoding time, and hence | ||
61 | the speed. The idea is you can have a shorter table that decodes the | ||
62 | shorter, more probable codes, and then point to subsidiary tables for | ||
63 | the longer codes. The time it costs to decode the longer codes is | ||
64 | then traded against the time it takes to make longer tables. | ||
65 | |||
66 | This results of this trade are in the variables lbits and dbits | ||
67 | below. lbits is the number of bits the first level table for literal/ | ||
68 | length codes can decode in one step, and dbits is the same thing for | ||
69 | the distance codes. Subsequent tables are also less than or equal to | ||
70 | those sizes. These values may be adjusted either when all of the | ||
71 | codes are shorter than that, in which case the longest code length in | ||
72 | bits is used, or when the shortest code is *longer* than the requested | ||
73 | table size, in which case the length of the shortest code in bits is | ||
74 | used. | ||
75 | |||
76 | There are two different values for the two tables, since they code a | ||
77 | different number of possibilities each. The literal/length table | ||
78 | codes 286 possible values, or in a flat code, a little over eight | ||
79 | bits. The distance table codes 30 possible values, or a little less | ||
80 | than five bits, flat. The optimum values for speed end up being | ||
81 | about one bit more than those, so lbits is 8+1 and dbits is 5+1. | ||
82 | The optimum values may differ though from machine to machine, and | ||
83 | possibly even between compilers. Your mileage may vary. | ||
84 | */ | ||
85 | |||
86 | |||
87 | /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */ | ||
88 | #define BMAX 15 /* maximum bit length of any code */ | ||
89 | |||
90 | static int huft_build( | ||
91 | uInt *b, /* code lengths in bits (all assumed <= BMAX) */ | ||
92 | uInt n, /* number of codes (assumed <= 288) */ | ||
93 | uInt s, /* number of simple-valued codes (0..s-1) */ | ||
94 | const uInt *d, /* list of base values for non-simple codes */ | ||
95 | const uInt *e, /* list of extra bits for non-simple codes */ | ||
96 | inflate_huft **t, /* result: starting table */ | ||
97 | uInt *m, /* maximum lookup bits, returns actual */ | ||
98 | inflate_huft *hp, /* space for trees */ | ||
99 | uInt *hn, /* hufts used in space */ | ||
100 | uInt *v /* working area: values in order of bit length */ | ||
101 | ) | ||
102 | /* Given a list of code lengths and a maximum table size, make a set of | ||
103 | tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR | ||
104 | if the given code set is incomplete (the tables are still built in this | ||
105 | case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of | ||
106 | lengths), or Z_MEM_ERROR if not enough memory. */ | ||
107 | { | ||
108 | |||
109 | uInt a; /* counter for codes of length k */ | ||
110 | uInt c[BMAX+1]; /* bit length count table */ | ||
111 | uInt f; /* i repeats in table every f entries */ | ||
112 | int g; /* maximum code length */ | ||
113 | int h; /* table level */ | ||
114 | register uInt i; /* counter, current code */ | ||
115 | register uInt j; /* counter */ | ||
116 | register int k; /* number of bits in current code */ | ||
117 | int l; /* bits per table (returned in m) */ | ||
118 | uInt mask; /* (1 << w) - 1, to avoid cc -O bug on HP */ | ||
119 | register uInt *p; /* pointer into c[], b[], or v[] */ | ||
120 | inflate_huft *q; /* points to current table */ | ||
121 | struct inflate_huft_s r; /* table entry for structure assignment */ | ||
122 | inflate_huft *u[BMAX]; /* table stack */ | ||
123 | register int w; /* bits before this table == (l * h) */ | ||
124 | uInt x[BMAX+1]; /* bit offsets, then code stack */ | ||
125 | uInt *xp; /* pointer into x */ | ||
126 | int y; /* number of dummy codes added */ | ||
127 | uInt z; /* number of entries in current table */ | ||
128 | |||
129 | |||
130 | /* Generate counts for each bit length */ | ||
131 | p = c; | ||
132 | #define C0 *p++ = 0; | ||
133 | #define C2 C0 C0 C0 C0 | ||
134 | #define C4 C2 C2 C2 C2 | ||
135 | C4 /* clear c[]--assume BMAX+1 is 16 */ | ||
136 | p = b; i = n; | ||
137 | do { | ||
138 | c[*p++]++; /* assume all entries <= BMAX */ | ||
139 | } while (--i); | ||
140 | if (c[0] == n) /* null input--all zero length codes */ | ||
141 | { | ||
142 | *t = NULL; | ||
143 | *m = 0; | ||
144 | return Z_OK; | ||
145 | } | ||
146 | |||
147 | |||
148 | /* Find minimum and maximum length, bound *m by those */ | ||
149 | l = *m; | ||
150 | for (j = 1; j <= BMAX; j++) | ||
151 | if (c[j]) | ||
152 | break; | ||
153 | k = j; /* minimum code length */ | ||
154 | if ((uInt)l < j) | ||
155 | l = j; | ||
156 | for (i = BMAX; i; i--) | ||
157 | if (c[i]) | ||
158 | break; | ||
159 | g = i; /* maximum code length */ | ||
160 | if ((uInt)l > i) | ||
161 | l = i; | ||
162 | *m = l; | ||
163 | |||
164 | |||
165 | /* Adjust last length count to fill out codes, if needed */ | ||
166 | for (y = 1 << j; j < i; j++, y <<= 1) | ||
167 | if ((y -= c[j]) < 0) | ||
168 | return Z_DATA_ERROR; | ||
169 | if ((y -= c[i]) < 0) | ||
170 | return Z_DATA_ERROR; | ||
171 | c[i] += y; | ||
172 | |||
173 | |||
174 | /* Generate starting offsets into the value table for each length */ | ||
175 | x[1] = j = 0; | ||
176 | p = c + 1; xp = x + 2; | ||
177 | while (--i) { /* note that i == g from above */ | ||
178 | *xp++ = (j += *p++); | ||
179 | } | ||
180 | |||
181 | |||
182 | /* Make a table of values in order of bit lengths */ | ||
183 | p = b; i = 0; | ||
184 | do { | ||
185 | if ((j = *p++) != 0) | ||
186 | v[x[j]++] = i; | ||
187 | } while (++i < n); | ||
188 | n = x[g]; /* set n to length of v */ | ||
189 | |||
190 | |||
191 | /* Generate the Huffman codes and for each, make the table entries */ | ||
192 | x[0] = i = 0; /* first Huffman code is zero */ | ||
193 | p = v; /* grab values in bit order */ | ||
194 | h = -1; /* no tables yet--level -1 */ | ||
195 | w = -l; /* bits decoded == (l * h) */ | ||
196 | u[0] = NULL; /* just to keep compilers happy */ | ||
197 | q = NULL; /* ditto */ | ||
198 | z = 0; /* ditto */ | ||
199 | |||
200 | /* go through the bit lengths (k already is bits in shortest code) */ | ||
201 | for (; k <= g; k++) | ||
202 | { | ||
203 | a = c[k]; | ||
204 | while (a--) | ||
205 | { | ||
206 | /* here i is the Huffman code of length k bits for value *p */ | ||
207 | /* make tables up to required level */ | ||
208 | while (k > w + l) | ||
209 | { | ||
210 | h++; | ||
211 | w += l; /* previous table always l bits */ | ||
212 | |||
213 | /* compute minimum size table less than or equal to l bits */ | ||
214 | z = g - w; | ||
215 | z = z > (uInt)l ? l : z; /* table size upper limit */ | ||
216 | if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */ | ||
217 | { /* too few codes for k-w bit table */ | ||
218 | f -= a + 1; /* deduct codes from patterns left */ | ||
219 | xp = c + k; | ||
220 | if (j < z) | ||
221 | while (++j < z) /* try smaller tables up to z bits */ | ||
222 | { | ||
223 | if ((f <<= 1) <= *++xp) | ||
224 | break; /* enough codes to use up j bits */ | ||
225 | f -= *xp; /* else deduct codes from patterns */ | ||
226 | } | ||
227 | } | ||
228 | z = 1 << j; /* table entries for j-bit table */ | ||
229 | |||
230 | /* allocate new table */ | ||
231 | if (*hn + z > MANY) /* (note: doesn't matter for fixed) */ | ||
232 | return Z_DATA_ERROR; /* overflow of MANY */ | ||
233 | u[h] = q = hp + *hn; | ||
234 | *hn += z; | ||
235 | |||
236 | /* connect to last table, if there is one */ | ||
237 | if (h) | ||
238 | { | ||
239 | x[h] = i; /* save pattern for backing up */ | ||
240 | r.bits = (Byte)l; /* bits to dump before this table */ | ||
241 | r.exop = (Byte)j; /* bits in this table */ | ||
242 | j = i >> (w - l); | ||
243 | r.base = (uInt)(q - u[h-1] - j); /* offset to this table */ | ||
244 | u[h-1][j] = r; /* connect to last table */ | ||
245 | } | ||
246 | else | ||
247 | *t = q; /* first table is returned result */ | ||
248 | } | ||
249 | |||
250 | /* set up table entry in r */ | ||
251 | r.bits = (Byte)(k - w); | ||
252 | if (p >= v + n) | ||
253 | r.exop = 128 + 64; /* out of values--invalid code */ | ||
254 | else if (*p < s) | ||
255 | { | ||
256 | r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); /* 256 is end-of-block */ | ||
257 | r.base = *p++; /* simple code is just the value */ | ||
258 | } | ||
259 | else | ||
260 | { | ||
261 | r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */ | ||
262 | r.base = d[*p++ - s]; | ||
263 | } | ||
264 | |||
265 | /* fill code-like entries with r */ | ||
266 | f = 1 << (k - w); | ||
267 | for (j = i >> w; j < z; j += f) | ||
268 | q[j] = r; | ||
269 | |||
270 | /* backwards increment the k-bit code i */ | ||
271 | for (j = 1 << (k - 1); i & j; j >>= 1) | ||
272 | i ^= j; | ||
273 | i ^= j; | ||
274 | |||
275 | /* backup over finished tables */ | ||
276 | mask = (1 << w) - 1; /* needed on HP, cc -O bug */ | ||
277 | while ((i & mask) != x[h]) | ||
278 | { | ||
279 | h--; /* don't need to update q */ | ||
280 | w -= l; | ||
281 | mask = (1 << w) - 1; | ||
282 | } | ||
283 | } | ||
284 | } | ||
285 | |||
286 | |||
287 | /* Return Z_BUF_ERROR if we were given an incomplete table */ | ||
288 | return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK; | ||
289 | } | ||
290 | |||
291 | |||
292 | int zlib_inflate_trees_bits( | ||
293 | uInt *c, /* 19 code lengths */ | ||
294 | uInt *bb, /* bits tree desired/actual depth */ | ||
295 | inflate_huft **tb, /* bits tree result */ | ||
296 | inflate_huft *hp, /* space for trees */ | ||
297 | z_streamp z /* for messages */ | ||
298 | ) | ||
299 | { | ||
300 | int r; | ||
301 | uInt hn = 0; /* hufts used in space */ | ||
302 | uInt *v; /* work area for huft_build */ | ||
303 | |||
304 | v = WS(z)->tree_work_area_1; | ||
305 | r = huft_build(c, 19, 19, NULL, NULL, tb, bb, hp, &hn, v); | ||
306 | if (r == Z_DATA_ERROR) | ||
307 | z->msg = (char*)"oversubscribed dynamic bit lengths tree"; | ||
308 | else if (r == Z_BUF_ERROR || *bb == 0) | ||
309 | { | ||
310 | z->msg = (char*)"incomplete dynamic bit lengths tree"; | ||
311 | r = Z_DATA_ERROR; | ||
312 | } | ||
313 | return r; | ||
314 | } | ||
315 | |||
316 | int zlib_inflate_trees_dynamic( | ||
317 | uInt nl, /* number of literal/length codes */ | ||
318 | uInt nd, /* number of distance codes */ | ||
319 | uInt *c, /* that many (total) code lengths */ | ||
320 | uInt *bl, /* literal desired/actual bit depth */ | ||
321 | uInt *bd, /* distance desired/actual bit depth */ | ||
322 | inflate_huft **tl, /* literal/length tree result */ | ||
323 | inflate_huft **td, /* distance tree result */ | ||
324 | inflate_huft *hp, /* space for trees */ | ||
325 | z_streamp z /* for messages */ | ||
326 | ) | ||
327 | { | ||
328 | int r; | ||
329 | uInt hn = 0; /* hufts used in space */ | ||
330 | uInt *v; /* work area for huft_build */ | ||
331 | |||
332 | /* allocate work area */ | ||
333 | v = WS(z)->tree_work_area_2; | ||
334 | |||
335 | /* build literal/length tree */ | ||
336 | r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v); | ||
337 | if (r != Z_OK || *bl == 0) | ||
338 | { | ||
339 | if (r == Z_DATA_ERROR) | ||
340 | z->msg = (char*)"oversubscribed literal/length tree"; | ||
341 | else if (r != Z_MEM_ERROR) | ||
342 | { | ||
343 | z->msg = (char*)"incomplete literal/length tree"; | ||
344 | r = Z_DATA_ERROR; | ||
345 | } | ||
346 | return r; | ||
347 | } | ||
348 | |||
349 | /* build distance tree */ | ||
350 | r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v); | ||
351 | if (r != Z_OK || (*bd == 0 && nl > 257)) | ||
352 | { | ||
353 | if (r == Z_DATA_ERROR) | ||
354 | z->msg = (char*)"oversubscribed distance tree"; | ||
355 | else if (r == Z_BUF_ERROR) { | ||
356 | #ifdef PKZIP_BUG_WORKAROUND | ||
357 | r = Z_OK; | ||
358 | } | ||
359 | #else | ||
360 | z->msg = (char*)"incomplete distance tree"; | ||
361 | r = Z_DATA_ERROR; | ||
362 | } | ||
363 | else if (r != Z_MEM_ERROR) | ||
364 | { | ||
365 | z->msg = (char*)"empty distance tree with lengths"; | ||
366 | r = Z_DATA_ERROR; | ||
367 | } | ||
368 | return r; | ||
369 | #endif | ||
370 | } | ||
371 | |||
372 | /* done */ | ||
373 | return Z_OK; | ||
374 | } | ||
375 | |||
376 | |||
377 | int zlib_inflate_trees_fixed( | ||
378 | uInt *bl, /* literal desired/actual bit depth */ | ||
379 | uInt *bd, /* distance desired/actual bit depth */ | ||
380 | inflate_huft **tl, /* literal/length tree result */ | ||
381 | inflate_huft **td, /* distance tree result */ | ||
382 | inflate_huft *hp, /* space for trees */ | ||
383 | z_streamp z /* for memory allocation */ | ||
384 | ) | ||
385 | { | ||
386 | int i; /* temporary variable */ | ||
387 | unsigned l[288]; /* length list for huft_build */ | ||
388 | uInt *v; /* work area for huft_build */ | ||
389 | |||
390 | /* set up literal table */ | ||
391 | for (i = 0; i < 144; i++) | ||
392 | l[i] = 8; | ||
393 | for (; i < 256; i++) | ||
394 | l[i] = 9; | ||
395 | for (; i < 280; i++) | ||
396 | l[i] = 7; | ||
397 | for (; i < 288; i++) /* make a complete, but wrong code set */ | ||
398 | l[i] = 8; | ||
399 | *bl = 9; | ||
400 | v = WS(z)->tree_work_area_1; | ||
401 | if ((i = huft_build(l, 288, 257, cplens, cplext, tl, bl, hp, &i, v)) != 0) | ||
402 | return i; | ||
403 | |||
404 | /* set up distance table */ | ||
405 | for (i = 0; i < 30; i++) /* make an incomplete code set */ | ||
406 | l[i] = 5; | ||
407 | *bd = 5; | ||
408 | if ((i = huft_build(l, 30, 0, cpdist, cpdext, td, bd, hp, &i, v)) > 1) | ||
409 | return i; | ||
410 | |||
411 | return Z_OK; | ||
412 | } | ||
diff --git a/lib/zlib_inflate/inftrees.h b/lib/zlib_inflate/inftrees.h new file mode 100644 index 000000000000..e37705adc008 --- /dev/null +++ b/lib/zlib_inflate/inftrees.h | |||
@@ -0,0 +1,64 @@ | |||
1 | /* inftrees.h -- header to use inftrees.c | ||
2 | * Copyright (C) 1995-1998 Mark Adler | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | /* WARNING: this file should *not* be used by applications. It is | ||
7 | part of the implementation of the compression library and is | ||
8 | subject to change. Applications should only use zlib.h. | ||
9 | */ | ||
10 | |||
11 | /* Huffman code lookup table entry--this entry is four bytes for machines | ||
12 | that have 16-bit pointers (e.g. PC's in the small or medium model). */ | ||
13 | |||
14 | #ifndef _INFTREES_H | ||
15 | #define _INFTREES_H | ||
16 | |||
17 | typedef struct inflate_huft_s inflate_huft; | ||
18 | |||
19 | struct inflate_huft_s { | ||
20 | union { | ||
21 | struct { | ||
22 | Byte Exop; /* number of extra bits or operation */ | ||
23 | Byte Bits; /* number of bits in this code or subcode */ | ||
24 | } what; | ||
25 | uInt pad; /* pad structure to a power of 2 (4 bytes for */ | ||
26 | } word; /* 16-bit, 8 bytes for 32-bit int's) */ | ||
27 | uInt base; /* literal, length base, distance base, | ||
28 | or table offset */ | ||
29 | }; | ||
30 | |||
31 | /* Maximum size of dynamic tree. The maximum found in a long but non- | ||
32 | exhaustive search was 1004 huft structures (850 for length/literals | ||
33 | and 154 for distances, the latter actually the result of an | ||
34 | exhaustive search). The actual maximum is not known, but the | ||
35 | value below is more than safe. */ | ||
36 | #define MANY 1440 | ||
37 | |||
38 | extern int zlib_inflate_trees_bits ( | ||
39 | uInt *, /* 19 code lengths */ | ||
40 | uInt *, /* bits tree desired/actual depth */ | ||
41 | inflate_huft **, /* bits tree result */ | ||
42 | inflate_huft *, /* space for trees */ | ||
43 | z_streamp); /* for messages */ | ||
44 | |||
45 | extern int zlib_inflate_trees_dynamic ( | ||
46 | uInt, /* number of literal/length codes */ | ||
47 | uInt, /* number of distance codes */ | ||
48 | uInt *, /* that many (total) code lengths */ | ||
49 | uInt *, /* literal desired/actual bit depth */ | ||
50 | uInt *, /* distance desired/actual bit depth */ | ||
51 | inflate_huft **, /* literal/length tree result */ | ||
52 | inflate_huft **, /* distance tree result */ | ||
53 | inflate_huft *, /* space for trees */ | ||
54 | z_streamp); /* for messages */ | ||
55 | |||
56 | extern int zlib_inflate_trees_fixed ( | ||
57 | uInt *, /* literal desired/actual bit depth */ | ||
58 | uInt *, /* distance desired/actual bit depth */ | ||
59 | inflate_huft **, /* literal/length tree result */ | ||
60 | inflate_huft **, /* distance tree result */ | ||
61 | inflate_huft *, /* space for trees */ | ||
62 | z_streamp); /* for memory allocation */ | ||
63 | |||
64 | #endif /* _INFTREES_H */ | ||
diff --git a/lib/zlib_inflate/infutil.c b/lib/zlib_inflate/infutil.c new file mode 100644 index 000000000000..00202b3438e1 --- /dev/null +++ b/lib/zlib_inflate/infutil.c | |||
@@ -0,0 +1,88 @@ | |||
1 | /* inflate_util.c -- data and routines common to blocks and codes | ||
2 | * Copyright (C) 1995-1998 Mark Adler | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | #include <linux/zutil.h> | ||
7 | #include "infblock.h" | ||
8 | #include "inftrees.h" | ||
9 | #include "infcodes.h" | ||
10 | #include "infutil.h" | ||
11 | |||
12 | struct inflate_codes_state; | ||
13 | |||
14 | /* And'ing with mask[n] masks the lower n bits */ | ||
15 | uInt zlib_inflate_mask[17] = { | ||
16 | 0x0000, | ||
17 | 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, | ||
18 | 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff | ||
19 | }; | ||
20 | |||
21 | |||
22 | /* copy as much as possible from the sliding window to the output area */ | ||
23 | int zlib_inflate_flush( | ||
24 | inflate_blocks_statef *s, | ||
25 | z_streamp z, | ||
26 | int r | ||
27 | ) | ||
28 | { | ||
29 | uInt n; | ||
30 | Byte *p; | ||
31 | Byte *q; | ||
32 | |||
33 | /* local copies of source and destination pointers */ | ||
34 | p = z->next_out; | ||
35 | q = s->read; | ||
36 | |||
37 | /* compute number of bytes to copy as far as end of window */ | ||
38 | n = (uInt)((q <= s->write ? s->write : s->end) - q); | ||
39 | if (n > z->avail_out) n = z->avail_out; | ||
40 | if (n && r == Z_BUF_ERROR) r = Z_OK; | ||
41 | |||
42 | /* update counters */ | ||
43 | z->avail_out -= n; | ||
44 | z->total_out += n; | ||
45 | |||
46 | /* update check information */ | ||
47 | if (s->checkfn != NULL) | ||
48 | z->adler = s->check = (*s->checkfn)(s->check, q, n); | ||
49 | |||
50 | /* copy as far as end of window */ | ||
51 | memcpy(p, q, n); | ||
52 | p += n; | ||
53 | q += n; | ||
54 | |||
55 | /* see if more to copy at beginning of window */ | ||
56 | if (q == s->end) | ||
57 | { | ||
58 | /* wrap pointers */ | ||
59 | q = s->window; | ||
60 | if (s->write == s->end) | ||
61 | s->write = s->window; | ||
62 | |||
63 | /* compute bytes to copy */ | ||
64 | n = (uInt)(s->write - q); | ||
65 | if (n > z->avail_out) n = z->avail_out; | ||
66 | if (n && r == Z_BUF_ERROR) r = Z_OK; | ||
67 | |||
68 | /* update counters */ | ||
69 | z->avail_out -= n; | ||
70 | z->total_out += n; | ||
71 | |||
72 | /* update check information */ | ||
73 | if (s->checkfn != NULL) | ||
74 | z->adler = s->check = (*s->checkfn)(s->check, q, n); | ||
75 | |||
76 | /* copy */ | ||
77 | memcpy(p, q, n); | ||
78 | p += n; | ||
79 | q += n; | ||
80 | } | ||
81 | |||
82 | /* update pointers */ | ||
83 | z->next_out = p; | ||
84 | s->read = q; | ||
85 | |||
86 | /* done */ | ||
87 | return r; | ||
88 | } | ||
diff --git a/lib/zlib_inflate/infutil.h b/lib/zlib_inflate/infutil.h new file mode 100644 index 000000000000..a15875fc5f72 --- /dev/null +++ b/lib/zlib_inflate/infutil.h | |||
@@ -0,0 +1,197 @@ | |||
1 | /* infutil.h -- types and macros common to blocks and codes | ||
2 | * Copyright (C) 1995-1998 Mark Adler | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | /* WARNING: this file should *not* be used by applications. It is | ||
7 | part of the implementation of the compression library and is | ||
8 | subject to change. Applications should only use zlib.h. | ||
9 | */ | ||
10 | |||
11 | #ifndef _INFUTIL_H | ||
12 | #define _INFUTIL_H | ||
13 | |||
14 | #include <linux/zconf.h> | ||
15 | #include "inftrees.h" | ||
16 | #include "infcodes.h" | ||
17 | |||
18 | typedef enum { | ||
19 | TYPE, /* get type bits (3, including end bit) */ | ||
20 | LENS, /* get lengths for stored */ | ||
21 | STORED, /* processing stored block */ | ||
22 | TABLE, /* get table lengths */ | ||
23 | BTREE, /* get bit lengths tree for a dynamic block */ | ||
24 | DTREE, /* get length, distance trees for a dynamic block */ | ||
25 | CODES, /* processing fixed or dynamic block */ | ||
26 | DRY, /* output remaining window bytes */ | ||
27 | B_DONE, /* finished last block, done */ | ||
28 | B_BAD} /* got a data error--stuck here */ | ||
29 | inflate_block_mode; | ||
30 | |||
31 | /* inflate blocks semi-private state */ | ||
32 | struct inflate_blocks_state { | ||
33 | |||
34 | /* mode */ | ||
35 | inflate_block_mode mode; /* current inflate_block mode */ | ||
36 | |||
37 | /* mode dependent information */ | ||
38 | union { | ||
39 | uInt left; /* if STORED, bytes left to copy */ | ||
40 | struct { | ||
41 | uInt table; /* table lengths (14 bits) */ | ||
42 | uInt index; /* index into blens (or border) */ | ||
43 | uInt *blens; /* bit lengths of codes */ | ||
44 | uInt bb; /* bit length tree depth */ | ||
45 | inflate_huft *tb; /* bit length decoding tree */ | ||
46 | } trees; /* if DTREE, decoding info for trees */ | ||
47 | struct { | ||
48 | inflate_codes_statef | ||
49 | *codes; | ||
50 | } decode; /* if CODES, current state */ | ||
51 | } sub; /* submode */ | ||
52 | uInt last; /* true if this block is the last block */ | ||
53 | |||
54 | /* mode independent information */ | ||
55 | uInt bitk; /* bits in bit buffer */ | ||
56 | uLong bitb; /* bit buffer */ | ||
57 | inflate_huft *hufts; /* single malloc for tree space */ | ||
58 | Byte *window; /* sliding window */ | ||
59 | Byte *end; /* one byte after sliding window */ | ||
60 | Byte *read; /* window read pointer */ | ||
61 | Byte *write; /* window write pointer */ | ||
62 | check_func checkfn; /* check function */ | ||
63 | uLong check; /* check on output */ | ||
64 | |||
65 | }; | ||
66 | |||
67 | |||
68 | /* defines for inflate input/output */ | ||
69 | /* update pointers and return */ | ||
70 | #define UPDBITS {s->bitb=b;s->bitk=k;} | ||
71 | #define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;} | ||
72 | #define UPDOUT {s->write=q;} | ||
73 | #define UPDATE {UPDBITS UPDIN UPDOUT} | ||
74 | #define LEAVE {UPDATE return zlib_inflate_flush(s,z,r);} | ||
75 | /* get bytes and bits */ | ||
76 | #define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;} | ||
77 | #define NEEDBYTE {if(n)r=Z_OK;else LEAVE} | ||
78 | #define NEXTBYTE (n--,*p++) | ||
79 | #define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}} | ||
80 | #define DUMPBITS(j) {b>>=(j);k-=(j);} | ||
81 | /* output bytes */ | ||
82 | #define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q) | ||
83 | #define LOADOUT {q=s->write;m=(uInt)WAVAIL;} | ||
84 | #define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}} | ||
85 | #define FLUSH {UPDOUT r=zlib_inflate_flush(s,z,r); LOADOUT} | ||
86 | #define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;} | ||
87 | #define OUTBYTE(a) {*q++=(Byte)(a);m--;} | ||
88 | /* load local pointers */ | ||
89 | #define LOAD {LOADIN LOADOUT} | ||
90 | |||
91 | /* masks for lower bits (size given to avoid silly warnings with Visual C++) */ | ||
92 | extern uInt zlib_inflate_mask[17]; | ||
93 | |||
94 | /* copy as much as possible from the sliding window to the output area */ | ||
95 | extern int zlib_inflate_flush ( | ||
96 | inflate_blocks_statef *, | ||
97 | z_streamp , | ||
98 | int); | ||
99 | |||
100 | /* inflate private state */ | ||
101 | typedef enum { | ||
102 | METHOD, /* waiting for method byte */ | ||
103 | FLAG, /* waiting for flag byte */ | ||
104 | DICT4, /* four dictionary check bytes to go */ | ||
105 | DICT3, /* three dictionary check bytes to go */ | ||
106 | DICT2, /* two dictionary check bytes to go */ | ||
107 | DICT1, /* one dictionary check byte to go */ | ||
108 | DICT0, /* waiting for inflateSetDictionary */ | ||
109 | BLOCKS, /* decompressing blocks */ | ||
110 | CHECK4, /* four check bytes to go */ | ||
111 | CHECK3, /* three check bytes to go */ | ||
112 | CHECK2, /* two check bytes to go */ | ||
113 | CHECK1, /* one check byte to go */ | ||
114 | I_DONE, /* finished check, done */ | ||
115 | I_BAD} /* got an error--stay here */ | ||
116 | inflate_mode; | ||
117 | |||
118 | struct internal_state { | ||
119 | |||
120 | /* mode */ | ||
121 | inflate_mode mode; /* current inflate mode */ | ||
122 | |||
123 | /* mode dependent information */ | ||
124 | union { | ||
125 | uInt method; /* if FLAGS, method byte */ | ||
126 | struct { | ||
127 | uLong was; /* computed check value */ | ||
128 | uLong need; /* stream check value */ | ||
129 | } check; /* if CHECK, check values to compare */ | ||
130 | uInt marker; /* if BAD, inflateSync's marker bytes count */ | ||
131 | } sub; /* submode */ | ||
132 | |||
133 | /* mode independent information */ | ||
134 | int nowrap; /* flag for no wrapper */ | ||
135 | uInt wbits; /* log2(window size) (8..15, defaults to 15) */ | ||
136 | inflate_blocks_statef | ||
137 | *blocks; /* current inflate_blocks state */ | ||
138 | |||
139 | }; | ||
140 | |||
141 | /* inflate codes private state */ | ||
142 | typedef enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */ | ||
143 | START, /* x: set up for LEN */ | ||
144 | LEN, /* i: get length/literal/eob next */ | ||
145 | LENEXT, /* i: getting length extra (have base) */ | ||
146 | DIST, /* i: get distance next */ | ||
147 | DISTEXT, /* i: getting distance extra */ | ||
148 | COPY, /* o: copying bytes in window, waiting for space */ | ||
149 | LIT, /* o: got literal, waiting for output space */ | ||
150 | WASH, /* o: got eob, possibly still output waiting */ | ||
151 | END, /* x: got eob and all data flushed */ | ||
152 | BADCODE} /* x: got error */ | ||
153 | inflate_codes_mode; | ||
154 | |||
155 | struct inflate_codes_state { | ||
156 | |||
157 | /* mode */ | ||
158 | inflate_codes_mode mode; /* current inflate_codes mode */ | ||
159 | |||
160 | /* mode dependent information */ | ||
161 | uInt len; | ||
162 | union { | ||
163 | struct { | ||
164 | inflate_huft *tree; /* pointer into tree */ | ||
165 | uInt need; /* bits needed */ | ||
166 | } code; /* if LEN or DIST, where in tree */ | ||
167 | uInt lit; /* if LIT, literal */ | ||
168 | struct { | ||
169 | uInt get; /* bits to get for extra */ | ||
170 | uInt dist; /* distance back to copy from */ | ||
171 | } copy; /* if EXT or COPY, where and how much */ | ||
172 | } sub; /* submode */ | ||
173 | |||
174 | /* mode independent information */ | ||
175 | Byte lbits; /* ltree bits decoded per branch */ | ||
176 | Byte dbits; /* dtree bits decoder per branch */ | ||
177 | inflate_huft *ltree; /* literal/length/eob tree */ | ||
178 | inflate_huft *dtree; /* distance tree */ | ||
179 | |||
180 | }; | ||
181 | |||
182 | /* memory allocation for inflation */ | ||
183 | |||
184 | struct inflate_workspace { | ||
185 | inflate_codes_statef working_state; | ||
186 | struct inflate_blocks_state working_blocks_state; | ||
187 | struct internal_state internal_state; | ||
188 | unsigned int tree_work_area_1[19]; | ||
189 | unsigned int tree_work_area_2[288]; | ||
190 | unsigned working_blens[258 + 0x1f + 0x1f]; | ||
191 | inflate_huft working_hufts[MANY]; | ||
192 | unsigned char working_window[1 << MAX_WBITS]; | ||
193 | }; | ||
194 | |||
195 | #define WS(z) ((struct inflate_workspace *)(z->workspace)) | ||
196 | |||
197 | #endif | ||