]> git.saurik.com Git - apple/security.git/blob - SecurityServer/MacYarrow/zlib/infblock.c
Security-30.1.tar.gz
[apple/security.git] / SecurityServer / MacYarrow / zlib / infblock.c
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 /* infblock.c -- interpret and process block types to last block
20 * Copyright (C) 1995-1998 Mark Adler
21 * For conditions of distribution and use, see copyright notice in zlib.h
22 */
23
24 #include "zutil.h"
25 #include "infblock.h"
26 #include "inftrees.h"
27 #include "infcodes.h"
28 #include "infutil.h"
29
30 struct inflate_codes_state {int dummy;}; /* for buggy compilers */
31
32 /* simplify the use of the inflate_huft type with some defines */
33 #define exop word.what.Exop
34 #define bits word.what.Bits
35
36 /* Table for deflate from PKZIP's appnote.txt. */
37 local const uInt border[] = { /* Order of the bit length code lengths */
38 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
39
40 /*
41 Notes beyond the 1.93a appnote.txt:
42
43 1. Distance pointers never point before the beginning of the output
44 stream.
45 2. Distance pointers can point back across blocks, up to 32k away.
46 3. There is an implied maximum of 7 bits for the bit length table and
47 15 bits for the actual data.
48 4. If only one code exists, then it is encoded using one bit. (Zero
49 would be more efficient, but perhaps a little confusing.) If two
50 codes exist, they are coded using one bit each (0 and 1).
51 5. There is no way of sending zero distance codes--a dummy must be
52 sent if there are none. (History: a pre 2.0 version of PKZIP would
53 store blocks with no distance codes, but this was discovered to be
54 too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
55 zero distance codes, which is sent as one code of zero bits in
56 length.
57 6. There are up to 286 literal/length codes. Code 256 represents the
58 end-of-block. Note however that the static length tree defines
59 288 codes just to fill out the Huffman codes. Codes 286 and 287
60 cannot be used though, since there is no length base or extra bits
61 defined for them. Similarily, there are up to 30 distance codes.
62 However, static trees define 32 codes (all 5 bits) to fill out the
63 Huffman codes, but the last two had better not show up in the data.
64 7. Unzip can check dynamic Huffman blocks for complete code sets.
65 The exception is that a single code would not be complete (see #4).
66 8. The five bits following the block type is really the number of
67 literal codes sent minus 257.
68 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
69 (1+6+6). Therefore, to output three times the length, you output
70 three codes (1+1+1), whereas to output four times the same length,
71 you only need two codes (1+3). Hmm.
72 10. In the tree reconstruction algorithm, Code = Code + Increment
73 only if BitLength(i) is not zero. (Pretty obvious.)
74 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
75 12. Note: length code 284 can represent 227-258, but length code 285
76 really is 258. The last length deserves its own, short code
77 since it gets used a lot in very redundant files. The length
78 258 is special since 258 - 3 (the min match length) is 255.
79 13. The literal/length and distance code bit lengths are read as a
80 single stream of lengths. It is possible (and advantageous) for
81 a repeat code (16, 17, or 18) to go across the boundary between
82 the two sets of lengths.
83 */
84
85
86 void inflate_blocks_reset(s, z, c)
87 inflate_blocks_statef *s;
88 z_streamp z;
89 uLongf *c;
90 {
91 if (c != Z_NULL)
92 *c = s->check;
93 if (s->mode == BTREE || s->mode == DTREE)
94 ZFREE(z, s->sub.trees.blens);
95 if (s->mode == CODES)
96 inflate_codes_free(s->sub.decode.codes, z);
97 s->mode = TYPE;
98 s->bitk = 0;
99 s->bitb = 0;
100 s->read = s->write = s->window;
101 if (s->checkfn != Z_NULL)
102 z->adler = s->check = (*s->checkfn)(0L, (const Bytef *)Z_NULL, 0);
103 Tracev((stderr, "inflate: blocks reset\n"));
104 }
105
106
107 inflate_blocks_statef *inflate_blocks_new(z, c, w)
108 z_streamp z;
109 check_func c;
110 uInt w;
111 {
112 inflate_blocks_statef *s;
113
114 if ((s = (inflate_blocks_statef *)ZALLOC
115 (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
116 return s;
117 if ((s->hufts =
118 (inflate_huft *)ZALLOC(z, sizeof(inflate_huft), MANY)) == Z_NULL)
119 {
120 ZFREE(z, s);
121 return Z_NULL;
122 }
123 if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
124 {
125 ZFREE(z, s->hufts);
126 ZFREE(z, s);
127 return Z_NULL;
128 }
129 s->end = s->window + w;
130 s->checkfn = c;
131 s->mode = TYPE;
132 Tracev((stderr, "inflate: blocks allocated\n"));
133 inflate_blocks_reset(s, z, Z_NULL);
134 return s;
135 }
136
137
138 int inflate_blocks(s, z, r)
139 inflate_blocks_statef *s;
140 z_streamp z;
141 int r;
142 {
143 uInt t; /* temporary storage */
144 uLong b; /* bit buffer */
145 uInt k; /* bits in bit buffer */
146 Bytef *p; /* input data pointer */
147 uInt n; /* bytes available there */
148 Bytef *q; /* output window write pointer */
149 uInt m; /* bytes to end of window or read pointer */
150
151 /* copy input/output information to locals (UPDATE macro restores) */
152 LOAD
153
154 /* process input based on current state */
155 while (1) switch (s->mode)
156 {
157 case TYPE:
158 NEEDBITS(3)
159 t = (uInt)b & 7;
160 s->last = t & 1;
161 switch (t >> 1)
162 {
163 case 0: /* stored */
164 Tracev((stderr, "inflate: stored block%s\n",
165 s->last ? " (last)" : ""));
166 DUMPBITS(3)
167 t = k & 7; /* go to byte boundary */
168 DUMPBITS(t)
169 s->mode = LENS; /* get length of stored block */
170 break;
171 case 1: /* fixed */
172 Tracev((stderr, "inflate: fixed codes block%s\n",
173 s->last ? " (last)" : ""));
174 {
175 uInt bl, bd;
176 inflate_huft *tl, *td;
177
178 inflate_trees_fixed(&bl, &bd, &tl, &td, z);
179 s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
180 if (s->sub.decode.codes == Z_NULL)
181 {
182 r = Z_MEM_ERROR;
183 LEAVE
184 }
185 }
186 DUMPBITS(3)
187 s->mode = CODES;
188 break;
189 case 2: /* dynamic */
190 Tracev((stderr, "inflate: dynamic codes block%s\n",
191 s->last ? " (last)" : ""));
192 DUMPBITS(3)
193 s->mode = TABLE;
194 break;
195 case 3: /* illegal */
196 DUMPBITS(3)
197 s->mode = BAD;
198 z->msg = (char*)"invalid block type";
199 r = Z_DATA_ERROR;
200 LEAVE
201 }
202 break;
203 case LENS:
204 NEEDBITS(32)
205 if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
206 {
207 s->mode = BAD;
208 z->msg = (char*)"invalid stored block lengths";
209 r = Z_DATA_ERROR;
210 LEAVE
211 }
212 s->sub.left = (uInt)b & 0xffff;
213 b = k = 0; /* dump bits */
214 Tracev((stderr, "inflate: stored length %u\n", s->sub.left));
215 s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
216 break;
217 case STORED:
218 if (n == 0)
219 LEAVE
220 NEEDOUT
221 t = s->sub.left;
222 if (t > n) t = n;
223 if (t > m) t = m;
224 zmemcpy(q, p, t);
225 p += t; n -= t;
226 q += t; m -= t;
227 if ((s->sub.left -= t) != 0)
228 break;
229 Tracev((stderr, "inflate: stored end, %lu total out\n",
230 z->total_out + (q >= s->read ? q - s->read :
231 (s->end - s->read) + (q - s->window))));
232 s->mode = s->last ? DRY : TYPE;
233 break;
234 case TABLE:
235 NEEDBITS(14)
236 s->sub.trees.table = t = (uInt)b & 0x3fff;
237 #ifndef PKZIP_BUG_WORKAROUND
238 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
239 {
240 s->mode = BAD;
241 z->msg = (char*)"too many length or distance symbols";
242 r = Z_DATA_ERROR;
243 LEAVE
244 }
245 #endif
246 t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
247 if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
248 {
249 r = Z_MEM_ERROR;
250 LEAVE
251 }
252 DUMPBITS(14)
253 s->sub.trees.index = 0;
254 Tracev((stderr, "inflate: table sizes ok\n"));
255 s->mode = BTREE;
256 case BTREE:
257 while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
258 {
259 NEEDBITS(3)
260 s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
261 DUMPBITS(3)
262 }
263 while (s->sub.trees.index < 19)
264 s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
265 s->sub.trees.bb = 7;
266 t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
267 &s->sub.trees.tb, s->hufts, z);
268 if (t != Z_OK)
269 {
270 ZFREE(z, s->sub.trees.blens);
271 r = t;
272 if (r == Z_DATA_ERROR)
273 s->mode = BAD;
274 LEAVE
275 }
276 s->sub.trees.index = 0;
277 Tracev((stderr, "inflate: bits tree ok\n"));
278 s->mode = DTREE;
279 case DTREE:
280 while (t = s->sub.trees.table,
281 s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
282 {
283 inflate_huft *h;
284 uInt i, j, c;
285
286 t = s->sub.trees.bb;
287 NEEDBITS(t)
288 h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
289 t = h->bits;
290 c = h->base;
291 if (c < 16)
292 {
293 DUMPBITS(t)
294 s->sub.trees.blens[s->sub.trees.index++] = c;
295 }
296 else /* c == 16..18 */
297 {
298 i = c == 18 ? 7 : c - 14;
299 j = c == 18 ? 11 : 3;
300 NEEDBITS(t + i)
301 DUMPBITS(t)
302 j += (uInt)b & inflate_mask[i];
303 DUMPBITS(i)
304 i = s->sub.trees.index;
305 t = s->sub.trees.table;
306 if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
307 (c == 16 && i < 1))
308 {
309 ZFREE(z, s->sub.trees.blens);
310 s->mode = BAD;
311 z->msg = (char*)"invalid bit length repeat";
312 r = Z_DATA_ERROR;
313 LEAVE
314 }
315 c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
316 do {
317 s->sub.trees.blens[i++] = c;
318 } while (--j);
319 s->sub.trees.index = i;
320 }
321 }
322 s->sub.trees.tb = Z_NULL;
323 {
324 uInt bl, bd;
325 inflate_huft *tl, *td;
326 inflate_codes_statef *c;
327
328 bl = 9; /* must be <= 9 for lookahead assumptions */
329 bd = 6; /* must be <= 9 for lookahead assumptions */
330 t = s->sub.trees.table;
331 t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
332 s->sub.trees.blens, &bl, &bd, &tl, &td,
333 s->hufts, z);
334 ZFREE(z, s->sub.trees.blens);
335 if (t != Z_OK)
336 {
337 if (t == (uInt)Z_DATA_ERROR)
338 s->mode = BAD;
339 r = t;
340 LEAVE
341 }
342 Tracev((stderr, "inflate: trees ok\n"));
343 if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
344 {
345 r = Z_MEM_ERROR;
346 LEAVE
347 }
348 s->sub.decode.codes = c;
349 }
350 s->mode = CODES;
351 case CODES:
352 UPDATE
353 if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
354 return inflate_flush(s, z, r);
355 r = Z_OK;
356 inflate_codes_free(s->sub.decode.codes, z);
357 LOAD
358 Tracev((stderr, "inflate: codes end, %lu total out\n",
359 z->total_out + (q >= s->read ? q - s->read :
360 (s->end - s->read) + (q - s->window))));
361 if (!s->last)
362 {
363 s->mode = TYPE;
364 break;
365 }
366 s->mode = DRY;
367 case DRY:
368 FLUSH
369 if (s->read != s->write)
370 LEAVE
371 s->mode = DONE;
372 case DONE:
373 r = Z_STREAM_END;
374 LEAVE
375 case BAD:
376 r = Z_DATA_ERROR;
377 LEAVE
378 default:
379 r = Z_STREAM_ERROR;
380 LEAVE
381 }
382 }
383
384
385 int inflate_blocks_free(s, z)
386 inflate_blocks_statef *s;
387 z_streamp z;
388 {
389 inflate_blocks_reset(s, z, Z_NULL);
390 ZFREE(z, s->window);
391 ZFREE(z, s->hufts);
392 ZFREE(z, s);
393 Tracev((stderr, "inflate: blocks freed\n"));
394 return Z_OK;
395 }
396
397
398 void inflate_set_dictionary(s, d, n)
399 inflate_blocks_statef *s;
400 const Bytef *d;
401 uInt n;
402 {
403 zmemcpy(s->window, d, n);
404 s->read = s->write = s->window + n;
405 }
406
407
408 /* Returns true if inflate is currently at the end of a block generated
409 * by Z_SYNC_FLUSH or Z_FULL_FLUSH.
410 * IN assertion: s != Z_NULL
411 */
412 int inflate_blocks_sync_point(s)
413 inflate_blocks_statef *s;
414 {
415 return s->mode == LENS;
416 }