1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 * |
|
4 * This code is free software; you can redistribute it and/or modify it |
|
5 * under the terms of the GNU General Public License version 2 only, as |
|
6 * published by the Free Software Foundation. Oracle designates this |
|
7 * particular file as subject to the "Classpath" exception as provided |
|
8 * by Oracle in the LICENSE file that accompanied this code. |
|
9 * |
|
10 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 * version 2 for more details (a copy is included in the LICENSE file that |
|
14 * accompanied this code). |
|
15 * |
|
16 * You should have received a copy of the GNU General Public License version |
|
17 * 2 along with this work; if not, write to the Free Software Foundation, |
|
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 * |
|
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
21 * or visit www.oracle.com if you need additional information or have any |
|
22 * questions. |
|
23 */ |
|
24 |
|
25 /* inflate.c -- zlib decompression |
|
26 * Copyright (C) 1995-2010 Mark Adler |
|
27 * For conditions of distribution and use, see copyright notice in zlib.h |
|
28 */ |
|
29 |
|
30 /* |
|
31 * Change history: |
|
32 * |
|
33 * 1.2.beta0 24 Nov 2002 |
|
34 * - First version -- complete rewrite of inflate to simplify code, avoid |
|
35 * creation of window when not needed, minimize use of window when it is |
|
36 * needed, make inffast.c even faster, implement gzip decoding, and to |
|
37 * improve code readability and style over the previous zlib inflate code |
|
38 * |
|
39 * 1.2.beta1 25 Nov 2002 |
|
40 * - Use pointers for available input and output checking in inffast.c |
|
41 * - Remove input and output counters in inffast.c |
|
42 * - Change inffast.c entry and loop from avail_in >= 7 to >= 6 |
|
43 * - Remove unnecessary second byte pull from length extra in inffast.c |
|
44 * - Unroll direct copy to three copies per loop in inffast.c |
|
45 * |
|
46 * 1.2.beta2 4 Dec 2002 |
|
47 * - Change external routine names to reduce potential conflicts |
|
48 * - Correct filename to inffixed.h for fixed tables in inflate.c |
|
49 * - Make hbuf[] unsigned char to match parameter type in inflate.c |
|
50 * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset) |
|
51 * to avoid negation problem on Alphas (64 bit) in inflate.c |
|
52 * |
|
53 * 1.2.beta3 22 Dec 2002 |
|
54 * - Add comments on state->bits assertion in inffast.c |
|
55 * - Add comments on op field in inftrees.h |
|
56 * - Fix bug in reuse of allocated window after inflateReset() |
|
57 * - Remove bit fields--back to byte structure for speed |
|
58 * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths |
|
59 * - Change post-increments to pre-increments in inflate_fast(), PPC biased? |
|
60 * - Add compile time option, POSTINC, to use post-increments instead (Intel?) |
|
61 * - Make MATCH copy in inflate() much faster for when inflate_fast() not used |
|
62 * - Use local copies of stream next and avail values, as well as local bit |
|
63 * buffer and bit count in inflate()--for speed when inflate_fast() not used |
|
64 * |
|
65 * 1.2.beta4 1 Jan 2003 |
|
66 * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings |
|
67 * - Move a comment on output buffer sizes from inffast.c to inflate.c |
|
68 * - Add comments in inffast.c to introduce the inflate_fast() routine |
|
69 * - Rearrange window copies in inflate_fast() for speed and simplification |
|
70 * - Unroll last copy for window match in inflate_fast() |
|
71 * - Use local copies of window variables in inflate_fast() for speed |
|
72 * - Pull out common wnext == 0 case for speed in inflate_fast() |
|
73 * - Make op and len in inflate_fast() unsigned for consistency |
|
74 * - Add FAR to lcode and dcode declarations in inflate_fast() |
|
75 * - Simplified bad distance check in inflate_fast() |
|
76 * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new |
|
77 * source file infback.c to provide a call-back interface to inflate for |
|
78 * programs like gzip and unzip -- uses window as output buffer to avoid |
|
79 * window copying |
|
80 * |
|
81 * 1.2.beta5 1 Jan 2003 |
|
82 * - Improved inflateBack() interface to allow the caller to provide initial |
|
83 * input in strm. |
|
84 * - Fixed stored blocks bug in inflateBack() |
|
85 * |
|
86 * 1.2.beta6 4 Jan 2003 |
|
87 * - Added comments in inffast.c on effectiveness of POSTINC |
|
88 * - Typecasting all around to reduce compiler warnings |
|
89 * - Changed loops from while (1) or do {} while (1) to for (;;), again to |
|
90 * make compilers happy |
|
91 * - Changed type of window in inflateBackInit() to unsigned char * |
|
92 * |
|
93 * 1.2.beta7 27 Jan 2003 |
|
94 * - Changed many types to unsigned or unsigned short to avoid warnings |
|
95 * - Added inflateCopy() function |
|
96 * |
|
97 * 1.2.0 9 Mar 2003 |
|
98 * - Changed inflateBack() interface to provide separate opaque descriptors |
|
99 * for the in() and out() functions |
|
100 * - Changed inflateBack() argument and in_func typedef to swap the length |
|
101 * and buffer address return values for the input function |
|
102 * - Check next_in and next_out for Z_NULL on entry to inflate() |
|
103 * |
|
104 * The history for versions after 1.2.0 are in ChangeLog in zlib distribution. |
|
105 */ |
|
106 |
|
107 #include "zutil.h" |
|
108 #include "inftrees.h" |
|
109 #include "inflate.h" |
|
110 #include "inffast.h" |
|
111 |
|
112 #ifdef MAKEFIXED |
|
113 # ifndef BUILDFIXED |
|
114 # define BUILDFIXED |
|
115 # endif |
|
116 #endif |
|
117 |
|
118 /* function prototypes */ |
|
119 local void fixedtables OF((struct inflate_state FAR *state)); |
|
120 local int updatewindow OF((z_streamp strm, unsigned out)); |
|
121 #ifdef BUILDFIXED |
|
122 void makefixed OF((void)); |
|
123 #endif |
|
124 local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf, |
|
125 unsigned len)); |
|
126 |
|
127 int ZEXPORT inflateReset(strm) |
|
128 z_streamp strm; |
|
129 { |
|
130 struct inflate_state FAR *state; |
|
131 |
|
132 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
|
133 state = (struct inflate_state FAR *)strm->state; |
|
134 strm->total_in = strm->total_out = state->total = 0; |
|
135 strm->msg = Z_NULL; |
|
136 strm->adler = 1; /* to support ill-conceived Java test suite */ |
|
137 state->mode = HEAD; |
|
138 state->last = 0; |
|
139 state->havedict = 0; |
|
140 state->dmax = 32768U; |
|
141 state->head = Z_NULL; |
|
142 state->wsize = 0; |
|
143 state->whave = 0; |
|
144 state->wnext = 0; |
|
145 state->hold = 0; |
|
146 state->bits = 0; |
|
147 state->lencode = state->distcode = state->next = state->codes; |
|
148 state->sane = 1; |
|
149 state->back = -1; |
|
150 Tracev((stderr, "inflate: reset\n")); |
|
151 return Z_OK; |
|
152 } |
|
153 |
|
154 int ZEXPORT inflateReset2(strm, windowBits) |
|
155 z_streamp strm; |
|
156 int windowBits; |
|
157 { |
|
158 int wrap; |
|
159 struct inflate_state FAR *state; |
|
160 |
|
161 /* get the state */ |
|
162 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
|
163 state = (struct inflate_state FAR *)strm->state; |
|
164 |
|
165 /* extract wrap request from windowBits parameter */ |
|
166 if (windowBits < 0) { |
|
167 wrap = 0; |
|
168 windowBits = -windowBits; |
|
169 } |
|
170 else { |
|
171 wrap = (windowBits >> 4) + 1; |
|
172 #ifdef GUNZIP |
|
173 if (windowBits < 48) |
|
174 windowBits &= 15; |
|
175 #endif |
|
176 } |
|
177 |
|
178 /* set number of window bits, free window if different */ |
|
179 if (windowBits && (windowBits < 8 || windowBits > 15)) |
|
180 return Z_STREAM_ERROR; |
|
181 if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) { |
|
182 ZFREE(strm, state->window); |
|
183 state->window = Z_NULL; |
|
184 } |
|
185 |
|
186 /* update state and reset the rest of it */ |
|
187 state->wrap = wrap; |
|
188 state->wbits = (unsigned)windowBits; |
|
189 return inflateReset(strm); |
|
190 } |
|
191 |
|
192 int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size) |
|
193 z_streamp strm; |
|
194 int windowBits; |
|
195 const char *version; |
|
196 int stream_size; |
|
197 { |
|
198 int ret; |
|
199 struct inflate_state FAR *state; |
|
200 |
|
201 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || |
|
202 stream_size != (int)(sizeof(z_stream))) |
|
203 return Z_VERSION_ERROR; |
|
204 if (strm == Z_NULL) return Z_STREAM_ERROR; |
|
205 strm->msg = Z_NULL; /* in case we return an error */ |
|
206 if (strm->zalloc == (alloc_func)0) { |
|
207 strm->zalloc = zcalloc; |
|
208 strm->opaque = (voidpf)0; |
|
209 } |
|
210 if (strm->zfree == (free_func)0) strm->zfree = zcfree; |
|
211 state = (struct inflate_state FAR *) |
|
212 ZALLOC(strm, 1, sizeof(struct inflate_state)); |
|
213 if (state == Z_NULL) return Z_MEM_ERROR; |
|
214 Tracev((stderr, "inflate: allocated\n")); |
|
215 strm->state = (struct internal_state FAR *)state; |
|
216 state->window = Z_NULL; |
|
217 ret = inflateReset2(strm, windowBits); |
|
218 if (ret != Z_OK) { |
|
219 ZFREE(strm, state); |
|
220 strm->state = Z_NULL; |
|
221 } |
|
222 return ret; |
|
223 } |
|
224 |
|
225 int ZEXPORT inflateInit_(strm, version, stream_size) |
|
226 z_streamp strm; |
|
227 const char *version; |
|
228 int stream_size; |
|
229 { |
|
230 return inflateInit2_(strm, DEF_WBITS, version, stream_size); |
|
231 } |
|
232 |
|
233 int ZEXPORT inflatePrime(strm, bits, value) |
|
234 z_streamp strm; |
|
235 int bits; |
|
236 int value; |
|
237 { |
|
238 struct inflate_state FAR *state; |
|
239 |
|
240 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
|
241 state = (struct inflate_state FAR *)strm->state; |
|
242 if (bits < 0) { |
|
243 state->hold = 0; |
|
244 state->bits = 0; |
|
245 return Z_OK; |
|
246 } |
|
247 if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR; |
|
248 value &= (1L << bits) - 1; |
|
249 state->hold += value << state->bits; |
|
250 state->bits += bits; |
|
251 return Z_OK; |
|
252 } |
|
253 |
|
254 /* |
|
255 Return state with length and distance decoding tables and index sizes set to |
|
256 fixed code decoding. Normally this returns fixed tables from inffixed.h. |
|
257 If BUILDFIXED is defined, then instead this routine builds the tables the |
|
258 first time it's called, and returns those tables the first time and |
|
259 thereafter. This reduces the size of the code by about 2K bytes, in |
|
260 exchange for a little execution time. However, BUILDFIXED should not be |
|
261 used for threaded applications, since the rewriting of the tables and virgin |
|
262 may not be thread-safe. |
|
263 */ |
|
264 local void fixedtables(state) |
|
265 struct inflate_state FAR *state; |
|
266 { |
|
267 #ifdef BUILDFIXED |
|
268 static int virgin = 1; |
|
269 static code *lenfix, *distfix; |
|
270 static code fixed[544]; |
|
271 |
|
272 /* build fixed huffman tables if first call (may not be thread safe) */ |
|
273 if (virgin) { |
|
274 unsigned sym, bits; |
|
275 static code *next; |
|
276 |
|
277 /* literal/length table */ |
|
278 sym = 0; |
|
279 while (sym < 144) state->lens[sym++] = 8; |
|
280 while (sym < 256) state->lens[sym++] = 9; |
|
281 while (sym < 280) state->lens[sym++] = 7; |
|
282 while (sym < 288) state->lens[sym++] = 8; |
|
283 next = fixed; |
|
284 lenfix = next; |
|
285 bits = 9; |
|
286 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work); |
|
287 |
|
288 /* distance table */ |
|
289 sym = 0; |
|
290 while (sym < 32) state->lens[sym++] = 5; |
|
291 distfix = next; |
|
292 bits = 5; |
|
293 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work); |
|
294 |
|
295 /* do this just once */ |
|
296 virgin = 0; |
|
297 } |
|
298 #else /* !BUILDFIXED */ |
|
299 # include "inffixed.h" |
|
300 #endif /* BUILDFIXED */ |
|
301 state->lencode = lenfix; |
|
302 state->lenbits = 9; |
|
303 state->distcode = distfix; |
|
304 state->distbits = 5; |
|
305 } |
|
306 |
|
307 #ifdef MAKEFIXED |
|
308 #include <stdio.h> |
|
309 |
|
310 /* |
|
311 Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also |
|
312 defines BUILDFIXED, so the tables are built on the fly. makefixed() writes |
|
313 those tables to stdout, which would be piped to inffixed.h. A small program |
|
314 can simply call makefixed to do this: |
|
315 |
|
316 void makefixed(void); |
|
317 |
|
318 int main(void) |
|
319 { |
|
320 makefixed(); |
|
321 return 0; |
|
322 } |
|
323 |
|
324 Then that can be linked with zlib built with MAKEFIXED defined and run: |
|
325 |
|
326 a.out > inffixed.h |
|
327 */ |
|
328 void makefixed() |
|
329 { |
|
330 unsigned low, size; |
|
331 struct inflate_state state; |
|
332 |
|
333 fixedtables(&state); |
|
334 puts(" /* inffixed.h -- table for decoding fixed codes"); |
|
335 puts(" * Generated automatically by makefixed()."); |
|
336 puts(" */"); |
|
337 puts(""); |
|
338 puts(" /* WARNING: this file should *not* be used by applications."); |
|
339 puts(" It is part of the implementation of this library and is"); |
|
340 puts(" subject to change. Applications should only use zlib.h."); |
|
341 puts(" */"); |
|
342 puts(""); |
|
343 size = 1U << 9; |
|
344 printf(" static const code lenfix[%u] = {", size); |
|
345 low = 0; |
|
346 for (;;) { |
|
347 if ((low % 7) == 0) printf("\n "); |
|
348 printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits, |
|
349 state.lencode[low].val); |
|
350 if (++low == size) break; |
|
351 putchar(','); |
|
352 } |
|
353 puts("\n };"); |
|
354 size = 1U << 5; |
|
355 printf("\n static const code distfix[%u] = {", size); |
|
356 low = 0; |
|
357 for (;;) { |
|
358 if ((low % 6) == 0) printf("\n "); |
|
359 printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits, |
|
360 state.distcode[low].val); |
|
361 if (++low == size) break; |
|
362 putchar(','); |
|
363 } |
|
364 puts("\n };"); |
|
365 } |
|
366 #endif /* MAKEFIXED */ |
|
367 |
|
368 /* |
|
369 Update the window with the last wsize (normally 32K) bytes written before |
|
370 returning. If window does not exist yet, create it. This is only called |
|
371 when a window is already in use, or when output has been written during this |
|
372 inflate call, but the end of the deflate stream has not been reached yet. |
|
373 It is also called to create a window for dictionary data when a dictionary |
|
374 is loaded. |
|
375 |
|
376 Providing output buffers larger than 32K to inflate() should provide a speed |
|
377 advantage, since only the last 32K of output is copied to the sliding window |
|
378 upon return from inflate(), and since all distances after the first 32K of |
|
379 output will fall in the output data, making match copies simpler and faster. |
|
380 The advantage may be dependent on the size of the processor's data caches. |
|
381 */ |
|
382 local int updatewindow(strm, out) |
|
383 z_streamp strm; |
|
384 unsigned out; |
|
385 { |
|
386 struct inflate_state FAR *state; |
|
387 unsigned copy, dist; |
|
388 |
|
389 state = (struct inflate_state FAR *)strm->state; |
|
390 |
|
391 /* if it hasn't been done already, allocate space for the window */ |
|
392 if (state->window == Z_NULL) { |
|
393 state->window = (unsigned char FAR *) |
|
394 ZALLOC(strm, 1U << state->wbits, |
|
395 sizeof(unsigned char)); |
|
396 if (state->window == Z_NULL) return 1; |
|
397 } |
|
398 |
|
399 /* if window not in use yet, initialize */ |
|
400 if (state->wsize == 0) { |
|
401 state->wsize = 1U << state->wbits; |
|
402 state->wnext = 0; |
|
403 state->whave = 0; |
|
404 } |
|
405 |
|
406 /* copy state->wsize or less output bytes into the circular window */ |
|
407 copy = out - strm->avail_out; |
|
408 if (copy >= state->wsize) { |
|
409 zmemcpy(state->window, strm->next_out - state->wsize, state->wsize); |
|
410 state->wnext = 0; |
|
411 state->whave = state->wsize; |
|
412 } |
|
413 else { |
|
414 dist = state->wsize - state->wnext; |
|
415 if (dist > copy) dist = copy; |
|
416 zmemcpy(state->window + state->wnext, strm->next_out - copy, dist); |
|
417 copy -= dist; |
|
418 if (copy) { |
|
419 zmemcpy(state->window, strm->next_out - copy, copy); |
|
420 state->wnext = copy; |
|
421 state->whave = state->wsize; |
|
422 } |
|
423 else { |
|
424 state->wnext += dist; |
|
425 if (state->wnext == state->wsize) state->wnext = 0; |
|
426 if (state->whave < state->wsize) state->whave += dist; |
|
427 } |
|
428 } |
|
429 return 0; |
|
430 } |
|
431 |
|
432 /* Macros for inflate(): */ |
|
433 |
|
434 /* check function to use adler32() for zlib or crc32() for gzip */ |
|
435 #ifdef GUNZIP |
|
436 # define UPDATE(check, buf, len) \ |
|
437 (state->flags ? crc32(check, buf, len) : adler32(check, buf, len)) |
|
438 #else |
|
439 # define UPDATE(check, buf, len) adler32(check, buf, len) |
|
440 #endif |
|
441 |
|
442 /* check macros for header crc */ |
|
443 #ifdef GUNZIP |
|
444 # define CRC2(check, word) \ |
|
445 do { \ |
|
446 hbuf[0] = (unsigned char)(word); \ |
|
447 hbuf[1] = (unsigned char)((word) >> 8); \ |
|
448 check = crc32(check, hbuf, 2); \ |
|
449 } while (0) |
|
450 |
|
451 # define CRC4(check, word) \ |
|
452 do { \ |
|
453 hbuf[0] = (unsigned char)(word); \ |
|
454 hbuf[1] = (unsigned char)((word) >> 8); \ |
|
455 hbuf[2] = (unsigned char)((word) >> 16); \ |
|
456 hbuf[3] = (unsigned char)((word) >> 24); \ |
|
457 check = crc32(check, hbuf, 4); \ |
|
458 } while (0) |
|
459 #endif |
|
460 |
|
461 /* Load registers with state in inflate() for speed */ |
|
462 #define LOAD() \ |
|
463 do { \ |
|
464 put = strm->next_out; \ |
|
465 left = strm->avail_out; \ |
|
466 next = strm->next_in; \ |
|
467 have = strm->avail_in; \ |
|
468 hold = state->hold; \ |
|
469 bits = state->bits; \ |
|
470 } while (0) |
|
471 |
|
472 /* Restore state from registers in inflate() */ |
|
473 #define RESTORE() \ |
|
474 do { \ |
|
475 strm->next_out = put; \ |
|
476 strm->avail_out = left; \ |
|
477 strm->next_in = next; \ |
|
478 strm->avail_in = have; \ |
|
479 state->hold = hold; \ |
|
480 state->bits = bits; \ |
|
481 } while (0) |
|
482 |
|
483 /* Clear the input bit accumulator */ |
|
484 #define INITBITS() \ |
|
485 do { \ |
|
486 hold = 0; \ |
|
487 bits = 0; \ |
|
488 } while (0) |
|
489 |
|
490 /* Get a byte of input into the bit accumulator, or return from inflate() |
|
491 if there is no input available. */ |
|
492 #define PULLBYTE() \ |
|
493 do { \ |
|
494 if (have == 0) goto inf_leave; \ |
|
495 have--; \ |
|
496 hold += (unsigned long)(*next++) << bits; \ |
|
497 bits += 8; \ |
|
498 } while (0) |
|
499 |
|
500 /* Assure that there are at least n bits in the bit accumulator. If there is |
|
501 not enough available input to do that, then return from inflate(). */ |
|
502 #define NEEDBITS(n) \ |
|
503 do { \ |
|
504 while (bits < (unsigned)(n)) \ |
|
505 PULLBYTE(); \ |
|
506 } while (0) |
|
507 |
|
508 /* Return the low n bits of the bit accumulator (n < 16) */ |
|
509 #define BITS(n) \ |
|
510 ((unsigned)hold & ((1U << (n)) - 1)) |
|
511 |
|
512 /* Remove n bits from the bit accumulator */ |
|
513 #define DROPBITS(n) \ |
|
514 do { \ |
|
515 hold >>= (n); \ |
|
516 bits -= (unsigned)(n); \ |
|
517 } while (0) |
|
518 |
|
519 /* Remove zero to seven bits as needed to go to a byte boundary */ |
|
520 #define BYTEBITS() \ |
|
521 do { \ |
|
522 hold >>= bits & 7; \ |
|
523 bits -= bits & 7; \ |
|
524 } while (0) |
|
525 |
|
526 /* Reverse the bytes in a 32-bit value */ |
|
527 #define REVERSE(q) \ |
|
528 ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \ |
|
529 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24)) |
|
530 |
|
531 /* |
|
532 inflate() uses a state machine to process as much input data and generate as |
|
533 much output data as possible before returning. The state machine is |
|
534 structured roughly as follows: |
|
535 |
|
536 for (;;) switch (state) { |
|
537 ... |
|
538 case STATEn: |
|
539 if (not enough input data or output space to make progress) |
|
540 return; |
|
541 ... make progress ... |
|
542 state = STATEm; |
|
543 break; |
|
544 ... |
|
545 } |
|
546 |
|
547 so when inflate() is called again, the same case is attempted again, and |
|
548 if the appropriate resources are provided, the machine proceeds to the |
|
549 next state. The NEEDBITS() macro is usually the way the state evaluates |
|
550 whether it can proceed or should return. NEEDBITS() does the return if |
|
551 the requested bits are not available. The typical use of the BITS macros |
|
552 is: |
|
553 |
|
554 NEEDBITS(n); |
|
555 ... do something with BITS(n) ... |
|
556 DROPBITS(n); |
|
557 |
|
558 where NEEDBITS(n) either returns from inflate() if there isn't enough |
|
559 input left to load n bits into the accumulator, or it continues. BITS(n) |
|
560 gives the low n bits in the accumulator. When done, DROPBITS(n) drops |
|
561 the low n bits off the accumulator. INITBITS() clears the accumulator |
|
562 and sets the number of available bits to zero. BYTEBITS() discards just |
|
563 enough bits to put the accumulator on a byte boundary. After BYTEBITS() |
|
564 and a NEEDBITS(8), then BITS(8) would return the next byte in the stream. |
|
565 |
|
566 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return |
|
567 if there is no input available. The decoding of variable length codes uses |
|
568 PULLBYTE() directly in order to pull just enough bytes to decode the next |
|
569 code, and no more. |
|
570 |
|
571 Some states loop until they get enough input, making sure that enough |
|
572 state information is maintained to continue the loop where it left off |
|
573 if NEEDBITS() returns in the loop. For example, want, need, and keep |
|
574 would all have to actually be part of the saved state in case NEEDBITS() |
|
575 returns: |
|
576 |
|
577 case STATEw: |
|
578 while (want < need) { |
|
579 NEEDBITS(n); |
|
580 keep[want++] = BITS(n); |
|
581 DROPBITS(n); |
|
582 } |
|
583 state = STATEx; |
|
584 case STATEx: |
|
585 |
|
586 As shown above, if the next state is also the next case, then the break |
|
587 is omitted. |
|
588 |
|
589 A state may also return if there is not enough output space available to |
|
590 complete that state. Those states are copying stored data, writing a |
|
591 literal byte, and copying a matching string. |
|
592 |
|
593 When returning, a "goto inf_leave" is used to update the total counters, |
|
594 update the check value, and determine whether any progress has been made |
|
595 during that inflate() call in order to return the proper return code. |
|
596 Progress is defined as a change in either strm->avail_in or strm->avail_out. |
|
597 When there is a window, goto inf_leave will update the window with the last |
|
598 output written. If a goto inf_leave occurs in the middle of decompression |
|
599 and there is no window currently, goto inf_leave will create one and copy |
|
600 output to the window for the next call of inflate(). |
|
601 |
|
602 In this implementation, the flush parameter of inflate() only affects the |
|
603 return code (per zlib.h). inflate() always writes as much as possible to |
|
604 strm->next_out, given the space available and the provided input--the effect |
|
605 documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers |
|
606 the allocation of and copying into a sliding window until necessary, which |
|
607 provides the effect documented in zlib.h for Z_FINISH when the entire input |
|
608 stream available. So the only thing the flush parameter actually does is: |
|
609 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it |
|
610 will return Z_BUF_ERROR if it has not reached the end of the stream. |
|
611 */ |
|
612 |
|
613 int ZEXPORT inflate(strm, flush) |
|
614 z_streamp strm; |
|
615 int flush; |
|
616 { |
|
617 struct inflate_state FAR *state; |
|
618 unsigned char FAR *next; /* next input */ |
|
619 unsigned char FAR *put; /* next output */ |
|
620 unsigned have, left; /* available input and output */ |
|
621 unsigned long hold; /* bit buffer */ |
|
622 unsigned bits; /* bits in bit buffer */ |
|
623 unsigned in, out; /* save starting available input and output */ |
|
624 unsigned copy; /* number of stored or match bytes to copy */ |
|
625 unsigned char FAR *from; /* where to copy match bytes from */ |
|
626 code here; /* current decoding table entry */ |
|
627 code last; /* parent table entry */ |
|
628 unsigned len; /* length to copy for repeats, bits to drop */ |
|
629 int ret; /* return code */ |
|
630 #ifdef GUNZIP |
|
631 unsigned char hbuf[4]; /* buffer for gzip header crc calculation */ |
|
632 #endif |
|
633 static const unsigned short order[19] = /* permutation of code lengths */ |
|
634 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; |
|
635 |
|
636 if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL || |
|
637 (strm->next_in == Z_NULL && strm->avail_in != 0)) |
|
638 return Z_STREAM_ERROR; |
|
639 |
|
640 state = (struct inflate_state FAR *)strm->state; |
|
641 if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */ |
|
642 LOAD(); |
|
643 in = have; |
|
644 out = left; |
|
645 ret = Z_OK; |
|
646 for (;;) |
|
647 switch (state->mode) { |
|
648 case HEAD: |
|
649 if (state->wrap == 0) { |
|
650 state->mode = TYPEDO; |
|
651 break; |
|
652 } |
|
653 NEEDBITS(16); |
|
654 #ifdef GUNZIP |
|
655 if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */ |
|
656 state->check = crc32(0L, Z_NULL, 0); |
|
657 CRC2(state->check, hold); |
|
658 INITBITS(); |
|
659 state->mode = FLAGS; |
|
660 break; |
|
661 } |
|
662 state->flags = 0; /* expect zlib header */ |
|
663 if (state->head != Z_NULL) |
|
664 state->head->done = -1; |
|
665 if (!(state->wrap & 1) || /* check if zlib header allowed */ |
|
666 #else |
|
667 if ( |
|
668 #endif |
|
669 ((BITS(8) << 8) + (hold >> 8)) % 31) { |
|
670 strm->msg = (char *)"incorrect header check"; |
|
671 state->mode = BAD; |
|
672 break; |
|
673 } |
|
674 if (BITS(4) != Z_DEFLATED) { |
|
675 strm->msg = (char *)"unknown compression method"; |
|
676 state->mode = BAD; |
|
677 break; |
|
678 } |
|
679 DROPBITS(4); |
|
680 len = BITS(4) + 8; |
|
681 if (state->wbits == 0) |
|
682 state->wbits = len; |
|
683 else if (len > state->wbits) { |
|
684 strm->msg = (char *)"invalid window size"; |
|
685 state->mode = BAD; |
|
686 break; |
|
687 } |
|
688 state->dmax = 1U << len; |
|
689 Tracev((stderr, "inflate: zlib header ok\n")); |
|
690 strm->adler = state->check = adler32(0L, Z_NULL, 0); |
|
691 state->mode = hold & 0x200 ? DICTID : TYPE; |
|
692 INITBITS(); |
|
693 break; |
|
694 #ifdef GUNZIP |
|
695 case FLAGS: |
|
696 NEEDBITS(16); |
|
697 state->flags = (int)(hold); |
|
698 if ((state->flags & 0xff) != Z_DEFLATED) { |
|
699 strm->msg = (char *)"unknown compression method"; |
|
700 state->mode = BAD; |
|
701 break; |
|
702 } |
|
703 if (state->flags & 0xe000) { |
|
704 strm->msg = (char *)"unknown header flags set"; |
|
705 state->mode = BAD; |
|
706 break; |
|
707 } |
|
708 if (state->head != Z_NULL) |
|
709 state->head->text = (int)((hold >> 8) & 1); |
|
710 if (state->flags & 0x0200) CRC2(state->check, hold); |
|
711 INITBITS(); |
|
712 state->mode = TIME; |
|
713 case TIME: |
|
714 NEEDBITS(32); |
|
715 if (state->head != Z_NULL) |
|
716 state->head->time = hold; |
|
717 if (state->flags & 0x0200) CRC4(state->check, hold); |
|
718 INITBITS(); |
|
719 state->mode = OS; |
|
720 case OS: |
|
721 NEEDBITS(16); |
|
722 if (state->head != Z_NULL) { |
|
723 state->head->xflags = (int)(hold & 0xff); |
|
724 state->head->os = (int)(hold >> 8); |
|
725 } |
|
726 if (state->flags & 0x0200) CRC2(state->check, hold); |
|
727 INITBITS(); |
|
728 state->mode = EXLEN; |
|
729 case EXLEN: |
|
730 if (state->flags & 0x0400) { |
|
731 NEEDBITS(16); |
|
732 state->length = (unsigned)(hold); |
|
733 if (state->head != Z_NULL) |
|
734 state->head->extra_len = (unsigned)hold; |
|
735 if (state->flags & 0x0200) CRC2(state->check, hold); |
|
736 INITBITS(); |
|
737 } |
|
738 else if (state->head != Z_NULL) |
|
739 state->head->extra = Z_NULL; |
|
740 state->mode = EXTRA; |
|
741 case EXTRA: |
|
742 if (state->flags & 0x0400) { |
|
743 copy = state->length; |
|
744 if (copy > have) copy = have; |
|
745 if (copy) { |
|
746 if (state->head != Z_NULL && |
|
747 state->head->extra != Z_NULL) { |
|
748 len = state->head->extra_len - state->length; |
|
749 zmemcpy(state->head->extra + len, next, |
|
750 len + copy > state->head->extra_max ? |
|
751 state->head->extra_max - len : copy); |
|
752 } |
|
753 if (state->flags & 0x0200) |
|
754 state->check = crc32(state->check, next, copy); |
|
755 have -= copy; |
|
756 next += copy; |
|
757 state->length -= copy; |
|
758 } |
|
759 if (state->length) goto inf_leave; |
|
760 } |
|
761 state->length = 0; |
|
762 state->mode = NAME; |
|
763 case NAME: |
|
764 if (state->flags & 0x0800) { |
|
765 if (have == 0) goto inf_leave; |
|
766 copy = 0; |
|
767 do { |
|
768 len = (unsigned)(next[copy++]); |
|
769 if (state->head != Z_NULL && |
|
770 state->head->name != Z_NULL && |
|
771 state->length < state->head->name_max) |
|
772 state->head->name[state->length++] = len; |
|
773 } while (len && copy < have); |
|
774 if (state->flags & 0x0200) |
|
775 state->check = crc32(state->check, next, copy); |
|
776 have -= copy; |
|
777 next += copy; |
|
778 if (len) goto inf_leave; |
|
779 } |
|
780 else if (state->head != Z_NULL) |
|
781 state->head->name = Z_NULL; |
|
782 state->length = 0; |
|
783 state->mode = COMMENT; |
|
784 case COMMENT: |
|
785 if (state->flags & 0x1000) { |
|
786 if (have == 0) goto inf_leave; |
|
787 copy = 0; |
|
788 do { |
|
789 len = (unsigned)(next[copy++]); |
|
790 if (state->head != Z_NULL && |
|
791 state->head->comment != Z_NULL && |
|
792 state->length < state->head->comm_max) |
|
793 state->head->comment[state->length++] = len; |
|
794 } while (len && copy < have); |
|
795 if (state->flags & 0x0200) |
|
796 state->check = crc32(state->check, next, copy); |
|
797 have -= copy; |
|
798 next += copy; |
|
799 if (len) goto inf_leave; |
|
800 } |
|
801 else if (state->head != Z_NULL) |
|
802 state->head->comment = Z_NULL; |
|
803 state->mode = HCRC; |
|
804 case HCRC: |
|
805 if (state->flags & 0x0200) { |
|
806 NEEDBITS(16); |
|
807 if (hold != (state->check & 0xffff)) { |
|
808 strm->msg = (char *)"header crc mismatch"; |
|
809 state->mode = BAD; |
|
810 break; |
|
811 } |
|
812 INITBITS(); |
|
813 } |
|
814 if (state->head != Z_NULL) { |
|
815 state->head->hcrc = (int)((state->flags >> 9) & 1); |
|
816 state->head->done = 1; |
|
817 } |
|
818 strm->adler = state->check = crc32(0L, Z_NULL, 0); |
|
819 state->mode = TYPE; |
|
820 break; |
|
821 #endif |
|
822 case DICTID: |
|
823 NEEDBITS(32); |
|
824 strm->adler = state->check = REVERSE(hold); |
|
825 INITBITS(); |
|
826 state->mode = DICT; |
|
827 case DICT: |
|
828 if (state->havedict == 0) { |
|
829 RESTORE(); |
|
830 return Z_NEED_DICT; |
|
831 } |
|
832 strm->adler = state->check = adler32(0L, Z_NULL, 0); |
|
833 state->mode = TYPE; |
|
834 case TYPE: |
|
835 if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave; |
|
836 case TYPEDO: |
|
837 if (state->last) { |
|
838 BYTEBITS(); |
|
839 state->mode = CHECK; |
|
840 break; |
|
841 } |
|
842 NEEDBITS(3); |
|
843 state->last = BITS(1); |
|
844 DROPBITS(1); |
|
845 switch (BITS(2)) { |
|
846 case 0: /* stored block */ |
|
847 Tracev((stderr, "inflate: stored block%s\n", |
|
848 state->last ? " (last)" : "")); |
|
849 state->mode = STORED; |
|
850 break; |
|
851 case 1: /* fixed block */ |
|
852 fixedtables(state); |
|
853 Tracev((stderr, "inflate: fixed codes block%s\n", |
|
854 state->last ? " (last)" : "")); |
|
855 state->mode = LEN_; /* decode codes */ |
|
856 if (flush == Z_TREES) { |
|
857 DROPBITS(2); |
|
858 goto inf_leave; |
|
859 } |
|
860 break; |
|
861 case 2: /* dynamic block */ |
|
862 Tracev((stderr, "inflate: dynamic codes block%s\n", |
|
863 state->last ? " (last)" : "")); |
|
864 state->mode = TABLE; |
|
865 break; |
|
866 case 3: |
|
867 strm->msg = (char *)"invalid block type"; |
|
868 state->mode = BAD; |
|
869 } |
|
870 DROPBITS(2); |
|
871 break; |
|
872 case STORED: |
|
873 BYTEBITS(); /* go to byte boundary */ |
|
874 NEEDBITS(32); |
|
875 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { |
|
876 strm->msg = (char *)"invalid stored block lengths"; |
|
877 state->mode = BAD; |
|
878 break; |
|
879 } |
|
880 state->length = (unsigned)hold & 0xffff; |
|
881 Tracev((stderr, "inflate: stored length %u\n", |
|
882 state->length)); |
|
883 INITBITS(); |
|
884 state->mode = COPY_; |
|
885 if (flush == Z_TREES) goto inf_leave; |
|
886 case COPY_: |
|
887 state->mode = COPY; |
|
888 case COPY: |
|
889 copy = state->length; |
|
890 if (copy) { |
|
891 if (copy > have) copy = have; |
|
892 if (copy > left) copy = left; |
|
893 if (copy == 0) goto inf_leave; |
|
894 zmemcpy(put, next, copy); |
|
895 have -= copy; |
|
896 next += copy; |
|
897 left -= copy; |
|
898 put += copy; |
|
899 state->length -= copy; |
|
900 break; |
|
901 } |
|
902 Tracev((stderr, "inflate: stored end\n")); |
|
903 state->mode = TYPE; |
|
904 break; |
|
905 case TABLE: |
|
906 NEEDBITS(14); |
|
907 state->nlen = BITS(5) + 257; |
|
908 DROPBITS(5); |
|
909 state->ndist = BITS(5) + 1; |
|
910 DROPBITS(5); |
|
911 state->ncode = BITS(4) + 4; |
|
912 DROPBITS(4); |
|
913 #ifndef PKZIP_BUG_WORKAROUND |
|
914 if (state->nlen > 286 || state->ndist > 30) { |
|
915 strm->msg = (char *)"too many length or distance symbols"; |
|
916 state->mode = BAD; |
|
917 break; |
|
918 } |
|
919 #endif |
|
920 Tracev((stderr, "inflate: table sizes ok\n")); |
|
921 state->have = 0; |
|
922 state->mode = LENLENS; |
|
923 case LENLENS: |
|
924 while (state->have < state->ncode) { |
|
925 NEEDBITS(3); |
|
926 state->lens[order[state->have++]] = (unsigned short)BITS(3); |
|
927 DROPBITS(3); |
|
928 } |
|
929 while (state->have < 19) |
|
930 state->lens[order[state->have++]] = 0; |
|
931 state->next = state->codes; |
|
932 state->lencode = (code const FAR *)(state->next); |
|
933 state->lenbits = 7; |
|
934 ret = inflate_table(CODES, state->lens, 19, &(state->next), |
|
935 &(state->lenbits), state->work); |
|
936 if (ret) { |
|
937 strm->msg = (char *)"invalid code lengths set"; |
|
938 state->mode = BAD; |
|
939 break; |
|
940 } |
|
941 Tracev((stderr, "inflate: code lengths ok\n")); |
|
942 state->have = 0; |
|
943 state->mode = CODELENS; |
|
944 case CODELENS: |
|
945 while (state->have < state->nlen + state->ndist) { |
|
946 for (;;) { |
|
947 here = state->lencode[BITS(state->lenbits)]; |
|
948 if ((unsigned)(here.bits) <= bits) break; |
|
949 PULLBYTE(); |
|
950 } |
|
951 if (here.val < 16) { |
|
952 NEEDBITS(here.bits); |
|
953 DROPBITS(here.bits); |
|
954 state->lens[state->have++] = here.val; |
|
955 } |
|
956 else { |
|
957 if (here.val == 16) { |
|
958 NEEDBITS(here.bits + 2); |
|
959 DROPBITS(here.bits); |
|
960 if (state->have == 0) { |
|
961 strm->msg = (char *)"invalid bit length repeat"; |
|
962 state->mode = BAD; |
|
963 break; |
|
964 } |
|
965 len = state->lens[state->have - 1]; |
|
966 copy = 3 + BITS(2); |
|
967 DROPBITS(2); |
|
968 } |
|
969 else if (here.val == 17) { |
|
970 NEEDBITS(here.bits + 3); |
|
971 DROPBITS(here.bits); |
|
972 len = 0; |
|
973 copy = 3 + BITS(3); |
|
974 DROPBITS(3); |
|
975 } |
|
976 else { |
|
977 NEEDBITS(here.bits + 7); |
|
978 DROPBITS(here.bits); |
|
979 len = 0; |
|
980 copy = 11 + BITS(7); |
|
981 DROPBITS(7); |
|
982 } |
|
983 if (state->have + copy > state->nlen + state->ndist) { |
|
984 strm->msg = (char *)"invalid bit length repeat"; |
|
985 state->mode = BAD; |
|
986 break; |
|
987 } |
|
988 while (copy--) |
|
989 state->lens[state->have++] = (unsigned short)len; |
|
990 } |
|
991 } |
|
992 |
|
993 /* handle error breaks in while */ |
|
994 if (state->mode == BAD) break; |
|
995 |
|
996 /* check for end-of-block code (better have one) */ |
|
997 if (state->lens[256] == 0) { |
|
998 strm->msg = (char *)"invalid code -- missing end-of-block"; |
|
999 state->mode = BAD; |
|
1000 break; |
|
1001 } |
|
1002 |
|
1003 /* build code tables -- note: do not change the lenbits or distbits |
|
1004 values here (9 and 6) without reading the comments in inftrees.h |
|
1005 concerning the ENOUGH constants, which depend on those values */ |
|
1006 state->next = state->codes; |
|
1007 state->lencode = (code const FAR *)(state->next); |
|
1008 state->lenbits = 9; |
|
1009 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), |
|
1010 &(state->lenbits), state->work); |
|
1011 if (ret) { |
|
1012 strm->msg = (char *)"invalid literal/lengths set"; |
|
1013 state->mode = BAD; |
|
1014 break; |
|
1015 } |
|
1016 state->distcode = (code const FAR *)(state->next); |
|
1017 state->distbits = 6; |
|
1018 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, |
|
1019 &(state->next), &(state->distbits), state->work); |
|
1020 if (ret) { |
|
1021 strm->msg = (char *)"invalid distances set"; |
|
1022 state->mode = BAD; |
|
1023 break; |
|
1024 } |
|
1025 Tracev((stderr, "inflate: codes ok\n")); |
|
1026 state->mode = LEN_; |
|
1027 if (flush == Z_TREES) goto inf_leave; |
|
1028 case LEN_: |
|
1029 state->mode = LEN; |
|
1030 case LEN: |
|
1031 if (have >= 6 && left >= 258) { |
|
1032 RESTORE(); |
|
1033 inflate_fast(strm, out); |
|
1034 LOAD(); |
|
1035 if (state->mode == TYPE) |
|
1036 state->back = -1; |
|
1037 break; |
|
1038 } |
|
1039 state->back = 0; |
|
1040 for (;;) { |
|
1041 here = state->lencode[BITS(state->lenbits)]; |
|
1042 if ((unsigned)(here.bits) <= bits) break; |
|
1043 PULLBYTE(); |
|
1044 } |
|
1045 if (here.op && (here.op & 0xf0) == 0) { |
|
1046 last = here; |
|
1047 for (;;) { |
|
1048 here = state->lencode[last.val + |
|
1049 (BITS(last.bits + last.op) >> last.bits)]; |
|
1050 if ((unsigned)(last.bits + here.bits) <= bits) break; |
|
1051 PULLBYTE(); |
|
1052 } |
|
1053 DROPBITS(last.bits); |
|
1054 state->back += last.bits; |
|
1055 } |
|
1056 DROPBITS(here.bits); |
|
1057 state->back += here.bits; |
|
1058 state->length = (unsigned)here.val; |
|
1059 if ((int)(here.op) == 0) { |
|
1060 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? |
|
1061 "inflate: literal '%c'\n" : |
|
1062 "inflate: literal 0x%02x\n", here.val)); |
|
1063 state->mode = LIT; |
|
1064 break; |
|
1065 } |
|
1066 if (here.op & 32) { |
|
1067 Tracevv((stderr, "inflate: end of block\n")); |
|
1068 state->back = -1; |
|
1069 state->mode = TYPE; |
|
1070 break; |
|
1071 } |
|
1072 if (here.op & 64) { |
|
1073 strm->msg = (char *)"invalid literal/length code"; |
|
1074 state->mode = BAD; |
|
1075 break; |
|
1076 } |
|
1077 state->extra = (unsigned)(here.op) & 15; |
|
1078 state->mode = LENEXT; |
|
1079 case LENEXT: |
|
1080 if (state->extra) { |
|
1081 NEEDBITS(state->extra); |
|
1082 state->length += BITS(state->extra); |
|
1083 DROPBITS(state->extra); |
|
1084 state->back += state->extra; |
|
1085 } |
|
1086 Tracevv((stderr, "inflate: length %u\n", state->length)); |
|
1087 state->was = state->length; |
|
1088 state->mode = DIST; |
|
1089 case DIST: |
|
1090 for (;;) { |
|
1091 here = state->distcode[BITS(state->distbits)]; |
|
1092 if ((unsigned)(here.bits) <= bits) break; |
|
1093 PULLBYTE(); |
|
1094 } |
|
1095 if ((here.op & 0xf0) == 0) { |
|
1096 last = here; |
|
1097 for (;;) { |
|
1098 here = state->distcode[last.val + |
|
1099 (BITS(last.bits + last.op) >> last.bits)]; |
|
1100 if ((unsigned)(last.bits + here.bits) <= bits) break; |
|
1101 PULLBYTE(); |
|
1102 } |
|
1103 DROPBITS(last.bits); |
|
1104 state->back += last.bits; |
|
1105 } |
|
1106 DROPBITS(here.bits); |
|
1107 state->back += here.bits; |
|
1108 if (here.op & 64) { |
|
1109 strm->msg = (char *)"invalid distance code"; |
|
1110 state->mode = BAD; |
|
1111 break; |
|
1112 } |
|
1113 state->offset = (unsigned)here.val; |
|
1114 state->extra = (unsigned)(here.op) & 15; |
|
1115 state->mode = DISTEXT; |
|
1116 case DISTEXT: |
|
1117 if (state->extra) { |
|
1118 NEEDBITS(state->extra); |
|
1119 state->offset += BITS(state->extra); |
|
1120 DROPBITS(state->extra); |
|
1121 state->back += state->extra; |
|
1122 } |
|
1123 #ifdef INFLATE_STRICT |
|
1124 if (state->offset > state->dmax) { |
|
1125 strm->msg = (char *)"invalid distance too far back"; |
|
1126 state->mode = BAD; |
|
1127 break; |
|
1128 } |
|
1129 #endif |
|
1130 Tracevv((stderr, "inflate: distance %u\n", state->offset)); |
|
1131 state->mode = MATCH; |
|
1132 case MATCH: |
|
1133 if (left == 0) goto inf_leave; |
|
1134 copy = out - left; |
|
1135 if (state->offset > copy) { /* copy from window */ |
|
1136 copy = state->offset - copy; |
|
1137 if (copy > state->whave) { |
|
1138 if (state->sane) { |
|
1139 strm->msg = (char *)"invalid distance too far back"; |
|
1140 state->mode = BAD; |
|
1141 break; |
|
1142 } |
|
1143 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR |
|
1144 Trace((stderr, "inflate.c too far\n")); |
|
1145 copy -= state->whave; |
|
1146 if (copy > state->length) copy = state->length; |
|
1147 if (copy > left) copy = left; |
|
1148 left -= copy; |
|
1149 state->length -= copy; |
|
1150 do { |
|
1151 *put++ = 0; |
|
1152 } while (--copy); |
|
1153 if (state->length == 0) state->mode = LEN; |
|
1154 break; |
|
1155 #endif |
|
1156 } |
|
1157 if (copy > state->wnext) { |
|
1158 copy -= state->wnext; |
|
1159 from = state->window + (state->wsize - copy); |
|
1160 } |
|
1161 else |
|
1162 from = state->window + (state->wnext - copy); |
|
1163 if (copy > state->length) copy = state->length; |
|
1164 } |
|
1165 else { /* copy from output */ |
|
1166 from = put - state->offset; |
|
1167 copy = state->length; |
|
1168 } |
|
1169 if (copy > left) copy = left; |
|
1170 left -= copy; |
|
1171 state->length -= copy; |
|
1172 do { |
|
1173 *put++ = *from++; |
|
1174 } while (--copy); |
|
1175 if (state->length == 0) state->mode = LEN; |
|
1176 break; |
|
1177 case LIT: |
|
1178 if (left == 0) goto inf_leave; |
|
1179 *put++ = (unsigned char)(state->length); |
|
1180 left--; |
|
1181 state->mode = LEN; |
|
1182 break; |
|
1183 case CHECK: |
|
1184 if (state->wrap) { |
|
1185 NEEDBITS(32); |
|
1186 out -= left; |
|
1187 strm->total_out += out; |
|
1188 state->total += out; |
|
1189 if (out) |
|
1190 strm->adler = state->check = |
|
1191 UPDATE(state->check, put - out, out); |
|
1192 out = left; |
|
1193 if (( |
|
1194 #ifdef GUNZIP |
|
1195 state->flags ? hold : |
|
1196 #endif |
|
1197 REVERSE(hold)) != state->check) { |
|
1198 strm->msg = (char *)"incorrect data check"; |
|
1199 state->mode = BAD; |
|
1200 break; |
|
1201 } |
|
1202 INITBITS(); |
|
1203 Tracev((stderr, "inflate: check matches trailer\n")); |
|
1204 } |
|
1205 #ifdef GUNZIP |
|
1206 state->mode = LENGTH; |
|
1207 case LENGTH: |
|
1208 if (state->wrap && state->flags) { |
|
1209 NEEDBITS(32); |
|
1210 if (hold != (state->total & 0xffffffffUL)) { |
|
1211 strm->msg = (char *)"incorrect length check"; |
|
1212 state->mode = BAD; |
|
1213 break; |
|
1214 } |
|
1215 INITBITS(); |
|
1216 Tracev((stderr, "inflate: length matches trailer\n")); |
|
1217 } |
|
1218 #endif |
|
1219 state->mode = DONE; |
|
1220 case DONE: |
|
1221 ret = Z_STREAM_END; |
|
1222 goto inf_leave; |
|
1223 case BAD: |
|
1224 ret = Z_DATA_ERROR; |
|
1225 goto inf_leave; |
|
1226 case MEM: |
|
1227 return Z_MEM_ERROR; |
|
1228 case SYNC: |
|
1229 default: |
|
1230 return Z_STREAM_ERROR; |
|
1231 } |
|
1232 |
|
1233 /* |
|
1234 Return from inflate(), updating the total counts and the check value. |
|
1235 If there was no progress during the inflate() call, return a buffer |
|
1236 error. Call updatewindow() to create and/or update the window state. |
|
1237 Note: a memory error from inflate() is non-recoverable. |
|
1238 */ |
|
1239 inf_leave: |
|
1240 RESTORE(); |
|
1241 if (state->wsize || (state->mode < CHECK && out != strm->avail_out)) |
|
1242 if (updatewindow(strm, out)) { |
|
1243 state->mode = MEM; |
|
1244 return Z_MEM_ERROR; |
|
1245 } |
|
1246 in -= strm->avail_in; |
|
1247 out -= strm->avail_out; |
|
1248 strm->total_in += in; |
|
1249 strm->total_out += out; |
|
1250 state->total += out; |
|
1251 if (state->wrap && out) |
|
1252 strm->adler = state->check = |
|
1253 UPDATE(state->check, strm->next_out - out, out); |
|
1254 strm->data_type = state->bits + (state->last ? 64 : 0) + |
|
1255 (state->mode == TYPE ? 128 : 0) + |
|
1256 (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0); |
|
1257 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) |
|
1258 ret = Z_BUF_ERROR; |
|
1259 return ret; |
|
1260 } |
|
1261 |
|
1262 int ZEXPORT inflateEnd(strm) |
|
1263 z_streamp strm; |
|
1264 { |
|
1265 struct inflate_state FAR *state; |
|
1266 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) |
|
1267 return Z_STREAM_ERROR; |
|
1268 state = (struct inflate_state FAR *)strm->state; |
|
1269 if (state->window != Z_NULL) ZFREE(strm, state->window); |
|
1270 ZFREE(strm, strm->state); |
|
1271 strm->state = Z_NULL; |
|
1272 Tracev((stderr, "inflate: end\n")); |
|
1273 return Z_OK; |
|
1274 } |
|
1275 |
|
1276 int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength) |
|
1277 z_streamp strm; |
|
1278 const Bytef *dictionary; |
|
1279 uInt dictLength; |
|
1280 { |
|
1281 struct inflate_state FAR *state; |
|
1282 unsigned long id; |
|
1283 |
|
1284 /* check state */ |
|
1285 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
|
1286 state = (struct inflate_state FAR *)strm->state; |
|
1287 if (state->wrap != 0 && state->mode != DICT) |
|
1288 return Z_STREAM_ERROR; |
|
1289 |
|
1290 /* check for correct dictionary id */ |
|
1291 if (state->mode == DICT) { |
|
1292 id = adler32(0L, Z_NULL, 0); |
|
1293 id = adler32(id, dictionary, dictLength); |
|
1294 if (id != state->check) |
|
1295 return Z_DATA_ERROR; |
|
1296 } |
|
1297 |
|
1298 /* copy dictionary to window */ |
|
1299 if (updatewindow(strm, strm->avail_out)) { |
|
1300 state->mode = MEM; |
|
1301 return Z_MEM_ERROR; |
|
1302 } |
|
1303 if (dictLength > state->wsize) { |
|
1304 zmemcpy(state->window, dictionary + dictLength - state->wsize, |
|
1305 state->wsize); |
|
1306 state->whave = state->wsize; |
|
1307 } |
|
1308 else { |
|
1309 zmemcpy(state->window + state->wsize - dictLength, dictionary, |
|
1310 dictLength); |
|
1311 state->whave = dictLength; |
|
1312 } |
|
1313 state->havedict = 1; |
|
1314 Tracev((stderr, "inflate: dictionary set\n")); |
|
1315 return Z_OK; |
|
1316 } |
|
1317 |
|
1318 int ZEXPORT inflateGetHeader(strm, head) |
|
1319 z_streamp strm; |
|
1320 gz_headerp head; |
|
1321 { |
|
1322 struct inflate_state FAR *state; |
|
1323 |
|
1324 /* check state */ |
|
1325 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
|
1326 state = (struct inflate_state FAR *)strm->state; |
|
1327 if ((state->wrap & 2) == 0) return Z_STREAM_ERROR; |
|
1328 |
|
1329 /* save header structure */ |
|
1330 state->head = head; |
|
1331 head->done = 0; |
|
1332 return Z_OK; |
|
1333 } |
|
1334 |
|
1335 /* |
|
1336 Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found |
|
1337 or when out of input. When called, *have is the number of pattern bytes |
|
1338 found in order so far, in 0..3. On return *have is updated to the new |
|
1339 state. If on return *have equals four, then the pattern was found and the |
|
1340 return value is how many bytes were read including the last byte of the |
|
1341 pattern. If *have is less than four, then the pattern has not been found |
|
1342 yet and the return value is len. In the latter case, syncsearch() can be |
|
1343 called again with more data and the *have state. *have is initialized to |
|
1344 zero for the first call. |
|
1345 */ |
|
1346 local unsigned syncsearch(have, buf, len) |
|
1347 unsigned FAR *have; |
|
1348 unsigned char FAR *buf; |
|
1349 unsigned len; |
|
1350 { |
|
1351 unsigned got; |
|
1352 unsigned next; |
|
1353 |
|
1354 got = *have; |
|
1355 next = 0; |
|
1356 while (next < len && got < 4) { |
|
1357 if ((int)(buf[next]) == (got < 2 ? 0 : 0xff)) |
|
1358 got++; |
|
1359 else if (buf[next]) |
|
1360 got = 0; |
|
1361 else |
|
1362 got = 4 - got; |
|
1363 next++; |
|
1364 } |
|
1365 *have = got; |
|
1366 return next; |
|
1367 } |
|
1368 |
|
1369 int ZEXPORT inflateSync(strm) |
|
1370 z_streamp strm; |
|
1371 { |
|
1372 unsigned len; /* number of bytes to look at or looked at */ |
|
1373 unsigned long in, out; /* temporary to save total_in and total_out */ |
|
1374 unsigned char buf[4]; /* to restore bit buffer to byte string */ |
|
1375 struct inflate_state FAR *state; |
|
1376 |
|
1377 /* check parameters */ |
|
1378 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
|
1379 state = (struct inflate_state FAR *)strm->state; |
|
1380 if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR; |
|
1381 |
|
1382 /* if first time, start search in bit buffer */ |
|
1383 if (state->mode != SYNC) { |
|
1384 state->mode = SYNC; |
|
1385 state->hold <<= state->bits & 7; |
|
1386 state->bits -= state->bits & 7; |
|
1387 len = 0; |
|
1388 while (state->bits >= 8) { |
|
1389 buf[len++] = (unsigned char)(state->hold); |
|
1390 state->hold >>= 8; |
|
1391 state->bits -= 8; |
|
1392 } |
|
1393 state->have = 0; |
|
1394 syncsearch(&(state->have), buf, len); |
|
1395 } |
|
1396 |
|
1397 /* search available input */ |
|
1398 len = syncsearch(&(state->have), strm->next_in, strm->avail_in); |
|
1399 strm->avail_in -= len; |
|
1400 strm->next_in += len; |
|
1401 strm->total_in += len; |
|
1402 |
|
1403 /* return no joy or set up to restart inflate() on a new block */ |
|
1404 if (state->have != 4) return Z_DATA_ERROR; |
|
1405 in = strm->total_in; out = strm->total_out; |
|
1406 inflateReset(strm); |
|
1407 strm->total_in = in; strm->total_out = out; |
|
1408 state->mode = TYPE; |
|
1409 return Z_OK; |
|
1410 } |
|
1411 |
|
1412 /* |
|
1413 Returns true if inflate is currently at the end of a block generated by |
|
1414 Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP |
|
1415 implementation to provide an additional safety check. PPP uses |
|
1416 Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored |
|
1417 block. When decompressing, PPP checks that at the end of input packet, |
|
1418 inflate is waiting for these length bytes. |
|
1419 */ |
|
1420 int ZEXPORT inflateSyncPoint(strm) |
|
1421 z_streamp strm; |
|
1422 { |
|
1423 struct inflate_state FAR *state; |
|
1424 |
|
1425 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
|
1426 state = (struct inflate_state FAR *)strm->state; |
|
1427 return state->mode == STORED && state->bits == 0; |
|
1428 } |
|
1429 |
|
1430 int ZEXPORT inflateCopy(dest, source) |
|
1431 z_streamp dest; |
|
1432 z_streamp source; |
|
1433 { |
|
1434 struct inflate_state FAR *state; |
|
1435 struct inflate_state FAR *copy; |
|
1436 unsigned char FAR *window; |
|
1437 unsigned wsize; |
|
1438 |
|
1439 /* check input */ |
|
1440 if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL || |
|
1441 source->zalloc == (alloc_func)0 || source->zfree == (free_func)0) |
|
1442 return Z_STREAM_ERROR; |
|
1443 state = (struct inflate_state FAR *)source->state; |
|
1444 |
|
1445 /* allocate space */ |
|
1446 copy = (struct inflate_state FAR *) |
|
1447 ZALLOC(source, 1, sizeof(struct inflate_state)); |
|
1448 if (copy == Z_NULL) return Z_MEM_ERROR; |
|
1449 window = Z_NULL; |
|
1450 if (state->window != Z_NULL) { |
|
1451 window = (unsigned char FAR *) |
|
1452 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); |
|
1453 if (window == Z_NULL) { |
|
1454 ZFREE(source, copy); |
|
1455 return Z_MEM_ERROR; |
|
1456 } |
|
1457 } |
|
1458 |
|
1459 /* copy state */ |
|
1460 zmemcpy(dest, source, sizeof(z_stream)); |
|
1461 zmemcpy(copy, state, sizeof(struct inflate_state)); |
|
1462 if (state->lencode >= state->codes && |
|
1463 state->lencode <= state->codes + ENOUGH - 1) { |
|
1464 copy->lencode = copy->codes + (state->lencode - state->codes); |
|
1465 copy->distcode = copy->codes + (state->distcode - state->codes); |
|
1466 } |
|
1467 copy->next = copy->codes + (state->next - state->codes); |
|
1468 if (window != Z_NULL) { |
|
1469 wsize = 1U << state->wbits; |
|
1470 zmemcpy(window, state->window, wsize); |
|
1471 } |
|
1472 copy->window = window; |
|
1473 dest->state = (struct internal_state FAR *)copy; |
|
1474 return Z_OK; |
|
1475 } |
|
1476 |
|
1477 int ZEXPORT inflateUndermine(strm, subvert) |
|
1478 z_streamp strm; |
|
1479 int subvert; |
|
1480 { |
|
1481 struct inflate_state FAR *state; |
|
1482 |
|
1483 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
|
1484 state = (struct inflate_state FAR *)strm->state; |
|
1485 state->sane = !subvert; |
|
1486 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR |
|
1487 return Z_OK; |
|
1488 #else |
|
1489 state->sane = 1; |
|
1490 return Z_DATA_ERROR; |
|
1491 #endif |
|
1492 } |
|
1493 |
|
1494 long ZEXPORT inflateMark(strm) |
|
1495 z_streamp strm; |
|
1496 { |
|
1497 struct inflate_state FAR *state; |
|
1498 |
|
1499 if (strm == Z_NULL || strm->state == Z_NULL) return -1L << 16; |
|
1500 state = (struct inflate_state FAR *)strm->state; |
|
1501 return ((long)(state->back) << 16) + |
|
1502 (state->mode == COPY ? state->length : |
|
1503 (state->mode == MATCH ? state->was - state->length : 0)); |
|
1504 } |
|