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