File: /usr/src/linux/fs/cramfs/inflate/infblock.c
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 "zutil.h"
7 #include "infblock.h"
8 #include "inftrees.h"
9 #include "infcodes.h"
10 #include "infutil.h"
11
12 struct inflate_codes_state {int dummy;}; /* for buggy compilers */
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 local 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 cramfs_inflate_blocks_reset(s, z, c)
69 inflate_blocks_statef *s;
70 z_streamp z;
71 uLongf *c;
72 {
73 if (c != Z_NULL)
74 *c = s->check;
75 if (s->mode == CODES)
76 cramfs_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
85
86 inflate_blocks_statef *cramfs_inflate_blocks_new(z, c, w)
87 z_streamp z;
88 check_func c;
89 uInt w;
90 {
91 inflate_blocks_statef *s;
92 static struct inflate_blocks_state working_blocks_state;
93 static inflate_huft working_hufts[MANY];
94 static unsigned char working_window[1 << MAX_WBITS];
95
96 s = &working_blocks_state;
97 s->hufts = working_hufts;
98 s->window = working_window;
99 s->end = s->window + w;
100 s->checkfn = c;
101 s->mode = TYPE;
102 cramfs_inflate_blocks_reset(s, z, Z_NULL);
103 return s;
104 }
105
106
107 int cramfs_inflate_blocks(s, z, r)
108 inflate_blocks_statef *s;
109 z_streamp z;
110 int r;
111 {
112 uInt t; /* temporary storage */
113 uLong b; /* bit buffer */
114 uInt k; /* bits in bit buffer */
115 Bytef *p; /* input data pointer */
116 uInt n; /* bytes available there */
117 Bytef *q; /* output window write pointer */
118 uInt m; /* bytes to end of window or read pointer */
119
120 /* copy input/output information to locals (UPDATE macro restores) */
121 LOAD
122
123 /* process input based on current state */
124 while (1) switch (s->mode)
125 {
126 case TYPE:
127 NEEDBITS(3)
128 t = (uInt)b & 7;
129 s->last = t & 1;
130 switch (t >> 1)
131 {
132 case 0: /* stored */
133 DUMPBITS(3)
134 t = k & 7; /* go to byte boundary */
135 DUMPBITS(t)
136 s->mode = LENS; /* get length of stored block */
137 break;
138 case 1: /* fixed */
139 {
140 uInt bl, bd;
141 inflate_huft *tl, *td;
142
143 cramfs_inflate_trees_fixed(&bl, &bd, &tl, &td, z);
144 s->sub.decode.codes = cramfs_inflate_codes_new(bl, bd, tl, td, z);
145 if (s->sub.decode.codes == Z_NULL)
146 {
147 r = Z_MEM_ERROR;
148 LEAVE
149 }
150 }
151 DUMPBITS(3)
152 s->mode = CODES;
153 break;
154 case 2: /* dynamic */
155 DUMPBITS(3)
156 s->mode = TABLE;
157 break;
158 case 3: /* illegal */
159 DUMPBITS(3)
160 s->mode = BAD;
161 z->msg = (char*)"invalid block type";
162 r = Z_DATA_ERROR;
163 LEAVE
164 }
165 break;
166 case LENS:
167 NEEDBITS(32)
168 if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
169 {
170 s->mode = BAD;
171 z->msg = (char*)"invalid stored block lengths";
172 r = Z_DATA_ERROR;
173 LEAVE
174 }
175 s->sub.left = (uInt)b & 0xffff;
176 b = k = 0; /* dump bits */
177 s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
178 break;
179 case STORED:
180 if (n == 0)
181 LEAVE
182 NEEDOUT
183 t = s->sub.left;
184 if (t > n) t = n;
185 if (t > m) t = m;
186 memcpy(q, p, t);
187 p += t; n -= t;
188 q += t; m -= t;
189 if ((s->sub.left -= t) != 0)
190 break;
191 s->mode = s->last ? DRY : TYPE;
192 break;
193 case TABLE:
194 NEEDBITS(14)
195 s->sub.trees.table = t = (uInt)b & 0x3fff;
196 #ifndef PKZIP_BUG_WORKAROUND
197 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
198 {
199 s->mode = BAD;
200 z->msg = (char*)"too many length or distance symbols";
201 r = Z_DATA_ERROR;
202 LEAVE
203 }
204 #endif
205 {
206 static unsigned int working_blens [258 + 0x1f + 0x1f];
207 s->sub.trees.blens = working_blens;
208 }
209 DUMPBITS(14)
210 s->sub.trees.index = 0;
211 s->mode = BTREE;
212 case BTREE:
213 while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
214 {
215 NEEDBITS(3)
216 s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
217 DUMPBITS(3)
218 }
219 while (s->sub.trees.index < 19)
220 s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
221 s->sub.trees.bb = 7;
222 t = cramfs_inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
223 &s->sub.trees.tb, s->hufts, z);
224 if (t != Z_OK)
225 {
226 r = t;
227 if (r == Z_DATA_ERROR)
228 s->mode = BAD;
229 LEAVE
230 }
231 s->sub.trees.index = 0;
232 s->mode = DTREE;
233 case DTREE:
234 while (t = s->sub.trees.table,
235 s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
236 {
237 inflate_huft *h;
238 uInt i, j, c;
239
240 t = s->sub.trees.bb;
241 NEEDBITS(t)
242 h = s->sub.trees.tb + ((uInt)b & cramfs_inflate_mask[t]);
243 t = h->bits;
244 c = h->base;
245 if (c < 16)
246 {
247 DUMPBITS(t)
248 s->sub.trees.blens[s->sub.trees.index++] = c;
249 }
250 else /* c == 16..18 */
251 {
252 i = c == 18 ? 7 : c - 14;
253 j = c == 18 ? 11 : 3;
254 NEEDBITS(t + i)
255 DUMPBITS(t)
256 j += (uInt)b & cramfs_inflate_mask[i];
257 DUMPBITS(i)
258 i = s->sub.trees.index;
259 t = s->sub.trees.table;
260 if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
261 (c == 16 && i < 1))
262 {
263 s->mode = BAD;
264 z->msg = (char*)"invalid bit length repeat";
265 r = Z_DATA_ERROR;
266 LEAVE
267 }
268 c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
269 do {
270 s->sub.trees.blens[i++] = c;
271 } while (--j);
272 s->sub.trees.index = i;
273 }
274 }
275 s->sub.trees.tb = Z_NULL;
276 {
277 uInt bl, bd;
278 inflate_huft *tl, *td;
279 inflate_codes_statef *c;
280
281 bl = 9; /* must be <= 9 for lookahead assumptions */
282 bd = 6; /* must be <= 9 for lookahead assumptions */
283 t = s->sub.trees.table;
284 t = cramfs_inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
285 s->sub.trees.blens, &bl, &bd, &tl, &td,
286 s->hufts, z);
287 if (t != Z_OK)
288 {
289 if (t == (uInt)Z_DATA_ERROR)
290 s->mode = BAD;
291 r = t;
292 LEAVE
293 }
294 if ((c = cramfs_inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
295 {
296 r = Z_MEM_ERROR;
297 LEAVE
298 }
299 s->sub.decode.codes = c;
300 }
301 s->mode = CODES;
302 case CODES:
303 UPDATE
304 if ((r = cramfs_inflate_codes(s, z, r)) != Z_STREAM_END)
305 return cramfs_inflate_flush(s, z, r);
306 r = Z_OK;
307 cramfs_inflate_codes_free(s->sub.decode.codes, z);
308 LOAD
309 if (!s->last)
310 {
311 s->mode = TYPE;
312 break;
313 }
314 s->mode = DRY;
315 case DRY:
316 FLUSH
317 if (s->read != s->write)
318 LEAVE
319 s->mode = DONE;
320 case DONE:
321 r = Z_STREAM_END;
322 LEAVE
323 case BAD:
324 r = Z_DATA_ERROR;
325 LEAVE
326 default:
327 r = Z_STREAM_ERROR;
328 LEAVE
329 }
330 }
331
332
333 int cramfs_inflate_blocks_free(s, z)
334 inflate_blocks_statef *s;
335 z_streamp z;
336 {
337 cramfs_inflate_blocks_reset(s, z, Z_NULL);
338 return Z_OK;
339 }
340
341
342 void cramfs_inflate_set_dictionary(s, d, n)
343 inflate_blocks_statef *s;
344 const Bytef *d;
345 uInt n;
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 != Z_NULL
355 */
356 int inflate_blocks_sync_point(s)
357 inflate_blocks_statef *s;
358 {
359 return s->mode == LENS;
360 }
361