]>
Commit | Line | Data |
---|---|---|
7eb75bcc DM |
1 | /* infback.c -- inflate using a call-back interface\r |
2 | * Copyright (C) 1995-2011 Mark Adler\r | |
3 | * For conditions of distribution and use, see copyright notice in zlib.h\r | |
4 | */\r | |
5 | \r | |
6 | /*\r | |
7 | This code is largely copied from inflate.c. Normally either infback.o or\r | |
8 | inflate.o would be linked into an application--not both. The interface\r | |
9 | with inffast.c is retained so that optimized assembler-coded versions of\r | |
10 | inflate_fast() can be used with either inflate.c or infback.c.\r | |
11 | */\r | |
12 | \r | |
13 | #include "zutil.h"\r | |
14 | #include "inftrees.h"\r | |
15 | #include "inflate.h"\r | |
16 | #include "inffast.h"\r | |
17 | \r | |
18 | /* function prototypes */\r | |
19 | local void fixedtables OF((struct inflate_state FAR *state));\r | |
20 | \r | |
21 | /*\r | |
22 | strm provides memory allocation functions in zalloc and zfree, or\r | |
23 | Z_NULL to use the library memory allocation functions.\r | |
24 | \r | |
25 | windowBits is in the range 8..15, and window is a user-supplied\r | |
26 | window and output buffer that is 2**windowBits bytes.\r | |
27 | */\r | |
28 | int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size)\r | |
29 | z_streamp strm;\r | |
30 | int windowBits;\r | |
31 | unsigned char FAR *window;\r | |
32 | const char *version;\r | |
33 | int stream_size;\r | |
34 | {\r | |
35 | struct inflate_state FAR *state;\r | |
36 | \r | |
37 | if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||\r | |
38 | stream_size != (int)(sizeof(z_stream)))\r | |
39 | return Z_VERSION_ERROR;\r | |
40 | if (strm == Z_NULL || window == Z_NULL ||\r | |
41 | windowBits < 8 || windowBits > 15)\r | |
42 | return Z_STREAM_ERROR;\r | |
43 | strm->msg = Z_NULL; /* in case we return an error */\r | |
44 | if (strm->zalloc == (alloc_func)0) {\r | |
45 | #ifdef Z_SOLO\r | |
46 | return Z_STREAM_ERROR;\r | |
47 | #else\r | |
48 | strm->zalloc = zcalloc;\r | |
49 | strm->opaque = (voidpf)0;\r | |
50 | #endif\r | |
51 | }\r | |
52 | if (strm->zfree == (free_func)0)\r | |
53 | #ifdef Z_SOLO\r | |
54 | return Z_STREAM_ERROR;\r | |
55 | #else\r | |
56 | strm->zfree = zcfree;\r | |
57 | #endif\r | |
58 | state = (struct inflate_state FAR *)ZALLOC(strm, 1,\r | |
59 | sizeof(struct inflate_state));\r | |
60 | if (state == Z_NULL) return Z_MEM_ERROR;\r | |
61 | Tracev((stderr, "inflate: allocated\n"));\r | |
62 | strm->state = (struct internal_state FAR *)state;\r | |
63 | state->dmax = 32768U;\r | |
64 | state->wbits = windowBits;\r | |
65 | state->wsize = 1U << windowBits;\r | |
66 | state->window = window;\r | |
67 | state->wnext = 0;\r | |
68 | state->whave = 0;\r | |
69 | return Z_OK;\r | |
70 | }\r | |
71 | \r | |
72 | /*\r | |
73 | Return state with length and distance decoding tables and index sizes set to\r | |
74 | fixed code decoding. Normally this returns fixed tables from inffixed.h.\r | |
75 | If BUILDFIXED is defined, then instead this routine builds the tables the\r | |
76 | first time it's called, and returns those tables the first time and\r | |
77 | thereafter. This reduces the size of the code by about 2K bytes, in\r | |
78 | exchange for a little execution time. However, BUILDFIXED should not be\r | |
79 | used for threaded applications, since the rewriting of the tables and virgin\r | |
80 | may not be thread-safe.\r | |
81 | */\r | |
82 | local void fixedtables(state)\r | |
83 | struct inflate_state FAR *state;\r | |
84 | {\r | |
85 | #ifdef BUILDFIXED\r | |
86 | static int virgin = 1;\r | |
87 | static code *lenfix, *distfix;\r | |
88 | static code fixed[544];\r | |
89 | \r | |
90 | /* build fixed huffman tables if first call (may not be thread safe) */\r | |
91 | if (virgin) {\r | |
92 | unsigned sym, bits;\r | |
93 | static code *next;\r | |
94 | \r | |
95 | /* literal/length table */\r | |
96 | sym = 0;\r | |
97 | while (sym < 144) state->lens[sym++] = 8;\r | |
98 | while (sym < 256) state->lens[sym++] = 9;\r | |
99 | while (sym < 280) state->lens[sym++] = 7;\r | |
100 | while (sym < 288) state->lens[sym++] = 8;\r | |
101 | next = fixed;\r | |
102 | lenfix = next;\r | |
103 | bits = 9;\r | |
104 | inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);\r | |
105 | \r | |
106 | /* distance table */\r | |
107 | sym = 0;\r | |
108 | while (sym < 32) state->lens[sym++] = 5;\r | |
109 | distfix = next;\r | |
110 | bits = 5;\r | |
111 | inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);\r | |
112 | \r | |
113 | /* do this just once */\r | |
114 | virgin = 0;\r | |
115 | }\r | |
116 | #else /* !BUILDFIXED */\r | |
117 | # include "inffixed.h"\r | |
118 | #endif /* BUILDFIXED */\r | |
119 | state->lencode = lenfix;\r | |
120 | state->lenbits = 9;\r | |
121 | state->distcode = distfix;\r | |
122 | state->distbits = 5;\r | |
123 | }\r | |
124 | \r | |
125 | /* Macros for inflateBack(): */\r | |
126 | \r | |
127 | /* Load returned state from inflate_fast() */\r | |
128 | #define LOAD() \\r | |
129 | do { \\r | |
130 | put = strm->next_out; \\r | |
131 | left = strm->avail_out; \\r | |
132 | next = strm->next_in; \\r | |
133 | have = strm->avail_in; \\r | |
134 | hold = state->hold; \\r | |
135 | bits = state->bits; \\r | |
136 | } while (0)\r | |
137 | \r | |
138 | /* Set state from registers for inflate_fast() */\r | |
139 | #define RESTORE() \\r | |
140 | do { \\r | |
141 | strm->next_out = put; \\r | |
142 | strm->avail_out = left; \\r | |
143 | strm->next_in = next; \\r | |
144 | strm->avail_in = have; \\r | |
145 | state->hold = hold; \\r | |
146 | state->bits = bits; \\r | |
147 | } while (0)\r | |
148 | \r | |
149 | /* Clear the input bit accumulator */\r | |
150 | #define INITBITS() \\r | |
151 | do { \\r | |
152 | hold = 0; \\r | |
153 | bits = 0; \\r | |
154 | } while (0)\r | |
155 | \r | |
156 | /* Assure that some input is available. If input is requested, but denied,\r | |
157 | then return a Z_BUF_ERROR from inflateBack(). */\r | |
158 | #define PULL() \\r | |
159 | do { \\r | |
160 | if (have == 0) { \\r | |
161 | have = in(in_desc, &next); \\r | |
162 | if (have == 0) { \\r | |
163 | next = Z_NULL; \\r | |
164 | ret = Z_BUF_ERROR; \\r | |
165 | goto inf_leave; \\r | |
166 | } \\r | |
167 | } \\r | |
168 | } while (0)\r | |
169 | \r | |
170 | /* Get a byte of input into the bit accumulator, or return from inflateBack()\r | |
171 | with an error if there is no input available. */\r | |
172 | #define PULLBYTE() \\r | |
173 | do { \\r | |
174 | PULL(); \\r | |
175 | have--; \\r | |
176 | hold += (unsigned long)(*next++) << bits; \\r | |
177 | bits += 8; \\r | |
178 | } while (0)\r | |
179 | \r | |
180 | /* Assure that there are at least n bits in the bit accumulator. If there is\r | |
181 | not enough available input to do that, then return from inflateBack() with\r | |
182 | an error. */\r | |
183 | #define NEEDBITS(n) \\r | |
184 | do { \\r | |
185 | while (bits < (unsigned)(n)) \\r | |
186 | PULLBYTE(); \\r | |
187 | } while (0)\r | |
188 | \r | |
189 | /* Return the low n bits of the bit accumulator (n < 16) */\r | |
190 | #define BITS(n) \\r | |
191 | ((unsigned)hold & ((1U << (n)) - 1))\r | |
192 | \r | |
193 | /* Remove n bits from the bit accumulator */\r | |
194 | #define DROPBITS(n) \\r | |
195 | do { \\r | |
196 | hold >>= (n); \\r | |
197 | bits -= (unsigned)(n); \\r | |
198 | } while (0)\r | |
199 | \r | |
200 | /* Remove zero to seven bits as needed to go to a byte boundary */\r | |
201 | #define BYTEBITS() \\r | |
202 | do { \\r | |
203 | hold >>= bits & 7; \\r | |
204 | bits -= bits & 7; \\r | |
205 | } while (0)\r | |
206 | \r | |
207 | /* Assure that some output space is available, by writing out the window\r | |
208 | if it's full. If the write fails, return from inflateBack() with a\r | |
209 | Z_BUF_ERROR. */\r | |
210 | #define ROOM() \\r | |
211 | do { \\r | |
212 | if (left == 0) { \\r | |
213 | put = state->window; \\r | |
214 | left = state->wsize; \\r | |
215 | state->whave = left; \\r | |
216 | if (out(out_desc, put, left)) { \\r | |
217 | ret = Z_BUF_ERROR; \\r | |
218 | goto inf_leave; \\r | |
219 | } \\r | |
220 | } \\r | |
221 | } while (0)\r | |
222 | \r | |
223 | /*\r | |
224 | strm provides the memory allocation functions and window buffer on input,\r | |
225 | and provides information on the unused input on return. For Z_DATA_ERROR\r | |
226 | returns, strm will also provide an error message.\r | |
227 | \r | |
228 | in() and out() are the call-back input and output functions. When\r | |
229 | inflateBack() needs more input, it calls in(). When inflateBack() has\r | |
230 | filled the window with output, or when it completes with data in the\r | |
231 | window, it calls out() to write out the data. The application must not\r | |
232 | change the provided input until in() is called again or inflateBack()\r | |
233 | returns. The application must not change the window/output buffer until\r | |
234 | inflateBack() returns.\r | |
235 | \r | |
236 | in() and out() are called with a descriptor parameter provided in the\r | |
237 | inflateBack() call. This parameter can be a structure that provides the\r | |
238 | information required to do the read or write, as well as accumulated\r | |
239 | information on the input and output such as totals and check values.\r | |
240 | \r | |
241 | in() should return zero on failure. out() should return non-zero on\r | |
242 | failure. If either in() or out() fails, than inflateBack() returns a\r | |
243 | Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it\r | |
244 | was in() or out() that caused in the error. Otherwise, inflateBack()\r | |
245 | returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format\r | |
246 | error, or Z_MEM_ERROR if it could not allocate memory for the state.\r | |
247 | inflateBack() can also return Z_STREAM_ERROR if the input parameters\r | |
248 | are not correct, i.e. strm is Z_NULL or the state was not initialized.\r | |
249 | */\r | |
250 | int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)\r | |
251 | z_streamp strm;\r | |
252 | in_func in;\r | |
253 | void FAR *in_desc;\r | |
254 | out_func out;\r | |
255 | void FAR *out_desc;\r | |
256 | {\r | |
257 | struct inflate_state FAR *state;\r | |
258 | z_const unsigned char FAR *next; /* next input */\r | |
259 | unsigned char FAR *put; /* next output */\r | |
260 | unsigned have, left; /* available input and output */\r | |
261 | unsigned long hold; /* bit buffer */\r | |
262 | unsigned bits; /* bits in bit buffer */\r | |
263 | unsigned copy; /* number of stored or match bytes to copy */\r | |
264 | unsigned char FAR *from; /* where to copy match bytes from */\r | |
265 | code here; /* current decoding table entry */\r | |
266 | code last; /* parent table entry */\r | |
267 | unsigned len; /* length to copy for repeats, bits to drop */\r | |
268 | int ret; /* return code */\r | |
269 | static const unsigned short order[19] = /* permutation of code lengths */\r | |
270 | {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};\r | |
271 | \r | |
272 | /* Check that the strm exists and that the state was initialized */\r | |
273 | if (strm == Z_NULL || strm->state == Z_NULL)\r | |
274 | return Z_STREAM_ERROR;\r | |
275 | state = (struct inflate_state FAR *)strm->state;\r | |
276 | \r | |
277 | /* Reset the state */\r | |
278 | strm->msg = Z_NULL;\r | |
279 | state->mode = TYPE;\r | |
280 | state->last = 0;\r | |
281 | state->whave = 0;\r | |
282 | next = strm->next_in;\r | |
283 | have = next != Z_NULL ? strm->avail_in : 0;\r | |
284 | hold = 0;\r | |
285 | bits = 0;\r | |
286 | put = state->window;\r | |
287 | left = state->wsize;\r | |
288 | \r | |
289 | /* Inflate until end of block marked as last */\r | |
290 | for (;;)\r | |
291 | switch (state->mode) {\r | |
292 | case TYPE:\r | |
293 | /* determine and dispatch block type */\r | |
294 | if (state->last) {\r | |
295 | BYTEBITS();\r | |
296 | state->mode = DONE;\r | |
297 | break;\r | |
298 | }\r | |
299 | NEEDBITS(3);\r | |
300 | state->last = BITS(1);\r | |
301 | DROPBITS(1);\r | |
302 | switch (BITS(2)) {\r | |
303 | case 0: /* stored block */\r | |
304 | Tracev((stderr, "inflate: stored block%s\n",\r | |
305 | state->last ? " (last)" : ""));\r | |
306 | state->mode = STORED;\r | |
307 | break;\r | |
308 | case 1: /* fixed block */\r | |
309 | fixedtables(state);\r | |
310 | Tracev((stderr, "inflate: fixed codes block%s\n",\r | |
311 | state->last ? " (last)" : ""));\r | |
312 | state->mode = LEN; /* decode codes */\r | |
313 | break;\r | |
314 | case 2: /* dynamic block */\r | |
315 | Tracev((stderr, "inflate: dynamic codes block%s\n",\r | |
316 | state->last ? " (last)" : ""));\r | |
317 | state->mode = TABLE;\r | |
318 | break;\r | |
319 | case 3:\r | |
320 | strm->msg = (char *)"invalid block type";\r | |
321 | state->mode = BAD;\r | |
322 | }\r | |
323 | DROPBITS(2);\r | |
324 | break;\r | |
325 | \r | |
326 | case STORED:\r | |
327 | /* get and verify stored block length */\r | |
328 | BYTEBITS(); /* go to byte boundary */\r | |
329 | NEEDBITS(32);\r | |
330 | if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {\r | |
331 | strm->msg = (char *)"invalid stored block lengths";\r | |
332 | state->mode = BAD;\r | |
333 | break;\r | |
334 | }\r | |
335 | state->length = (unsigned)hold & 0xffff;\r | |
336 | Tracev((stderr, "inflate: stored length %u\n",\r | |
337 | state->length));\r | |
338 | INITBITS();\r | |
339 | \r | |
340 | /* copy stored block from input to output */\r | |
341 | while (state->length != 0) {\r | |
342 | copy = state->length;\r | |
343 | PULL();\r | |
344 | ROOM();\r | |
345 | if (copy > have) copy = have;\r | |
346 | if (copy > left) copy = left;\r | |
347 | zmemcpy(put, next, copy);\r | |
348 | have -= copy;\r | |
349 | next += copy;\r | |
350 | left -= copy;\r | |
351 | put += copy;\r | |
352 | state->length -= copy;\r | |
353 | }\r | |
354 | Tracev((stderr, "inflate: stored end\n"));\r | |
355 | state->mode = TYPE;\r | |
356 | break;\r | |
357 | \r | |
358 | case TABLE:\r | |
359 | /* get dynamic table entries descriptor */\r | |
360 | NEEDBITS(14);\r | |
361 | state->nlen = BITS(5) + 257;\r | |
362 | DROPBITS(5);\r | |
363 | state->ndist = BITS(5) + 1;\r | |
364 | DROPBITS(5);\r | |
365 | state->ncode = BITS(4) + 4;\r | |
366 | DROPBITS(4);\r | |
367 | #ifndef PKZIP_BUG_WORKAROUND\r | |
368 | if (state->nlen > 286 || state->ndist > 30) {\r | |
369 | strm->msg = (char *)"too many length or distance symbols";\r | |
370 | state->mode = BAD;\r | |
371 | break;\r | |
372 | }\r | |
373 | #endif\r | |
374 | Tracev((stderr, "inflate: table sizes ok\n"));\r | |
375 | \r | |
376 | /* get code length code lengths (not a typo) */\r | |
377 | state->have = 0;\r | |
378 | while (state->have < state->ncode) {\r | |
379 | NEEDBITS(3);\r | |
380 | state->lens[order[state->have++]] = (unsigned short)BITS(3);\r | |
381 | DROPBITS(3);\r | |
382 | }\r | |
383 | while (state->have < 19)\r | |
384 | state->lens[order[state->have++]] = 0;\r | |
385 | state->next = state->codes;\r | |
386 | state->lencode = (code const FAR *)(state->next);\r | |
387 | state->lenbits = 7;\r | |
388 | ret = inflate_table(CODES, state->lens, 19, &(state->next),\r | |
389 | &(state->lenbits), state->work);\r | |
390 | if (ret) {\r | |
391 | strm->msg = (char *)"invalid code lengths set";\r | |
392 | state->mode = BAD;\r | |
393 | break;\r | |
394 | }\r | |
395 | Tracev((stderr, "inflate: code lengths ok\n"));\r | |
396 | \r | |
397 | /* get length and distance code code lengths */\r | |
398 | state->have = 0;\r | |
399 | while (state->have < state->nlen + state->ndist) {\r | |
400 | for (;;) {\r | |
401 | here = state->lencode[BITS(state->lenbits)];\r | |
402 | if ((unsigned)(here.bits) <= bits) break;\r | |
403 | PULLBYTE();\r | |
404 | }\r | |
405 | if (here.val < 16) {\r | |
406 | DROPBITS(here.bits);\r | |
407 | state->lens[state->have++] = here.val;\r | |
408 | }\r | |
409 | else {\r | |
410 | if (here.val == 16) {\r | |
411 | NEEDBITS(here.bits + 2);\r | |
412 | DROPBITS(here.bits);\r | |
413 | if (state->have == 0) {\r | |
414 | strm->msg = (char *)"invalid bit length repeat";\r | |
415 | state->mode = BAD;\r | |
416 | break;\r | |
417 | }\r | |
418 | len = (unsigned)(state->lens[state->have - 1]);\r | |
419 | copy = 3 + BITS(2);\r | |
420 | DROPBITS(2);\r | |
421 | }\r | |
422 | else if (here.val == 17) {\r | |
423 | NEEDBITS(here.bits + 3);\r | |
424 | DROPBITS(here.bits);\r | |
425 | len = 0;\r | |
426 | copy = 3 + BITS(3);\r | |
427 | DROPBITS(3);\r | |
428 | }\r | |
429 | else {\r | |
430 | NEEDBITS(here.bits + 7);\r | |
431 | DROPBITS(here.bits);\r | |
432 | len = 0;\r | |
433 | copy = 11 + BITS(7);\r | |
434 | DROPBITS(7);\r | |
435 | }\r | |
436 | if (state->have + copy > state->nlen + state->ndist) {\r | |
437 | strm->msg = (char *)"invalid bit length repeat";\r | |
438 | state->mode = BAD;\r | |
439 | break;\r | |
440 | }\r | |
441 | while (copy--)\r | |
442 | state->lens[state->have++] = (unsigned short)len;\r | |
443 | }\r | |
444 | }\r | |
445 | \r | |
446 | /* handle error breaks in while */\r | |
447 | if (state->mode == BAD) break;\r | |
448 | \r | |
449 | /* check for end-of-block code (better have one) */\r | |
450 | if (state->lens[256] == 0) {\r | |
451 | strm->msg = (char *)"invalid code -- missing end-of-block";\r | |
452 | state->mode = BAD;\r | |
453 | break;\r | |
454 | }\r | |
455 | \r | |
456 | /* build code tables -- note: do not change the lenbits or distbits\r | |
457 | values here (9 and 6) without reading the comments in inftrees.h\r | |
458 | concerning the ENOUGH constants, which depend on those values */\r | |
459 | state->next = state->codes;\r | |
460 | state->lencode = (code const FAR *)(state->next);\r | |
461 | state->lenbits = 9;\r | |
462 | ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),\r | |
463 | &(state->lenbits), state->work);\r | |
464 | if (ret) {\r | |
465 | strm->msg = (char *)"invalid literal/lengths set";\r | |
466 | state->mode = BAD;\r | |
467 | break;\r | |
468 | }\r | |
469 | state->distcode = (code const FAR *)(state->next);\r | |
470 | state->distbits = 6;\r | |
471 | ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,\r | |
472 | &(state->next), &(state->distbits), state->work);\r | |
473 | if (ret) {\r | |
474 | strm->msg = (char *)"invalid distances set";\r | |
475 | state->mode = BAD;\r | |
476 | break;\r | |
477 | }\r | |
478 | Tracev((stderr, "inflate: codes ok\n"));\r | |
479 | state->mode = LEN;\r | |
480 | \r | |
481 | case LEN:\r | |
482 | /* use inflate_fast() if we have enough input and output */\r | |
483 | if (have >= 6 && left >= 258) {\r | |
484 | RESTORE();\r | |
485 | if (state->whave < state->wsize)\r | |
486 | state->whave = state->wsize - left;\r | |
487 | inflate_fast(strm, state->wsize);\r | |
488 | LOAD();\r | |
489 | break;\r | |
490 | }\r | |
491 | \r | |
492 | /* get a literal, length, or end-of-block code */\r | |
493 | for (;;) {\r | |
494 | here = state->lencode[BITS(state->lenbits)];\r | |
495 | if ((unsigned)(here.bits) <= bits) break;\r | |
496 | PULLBYTE();\r | |
497 | }\r | |
498 | if (here.op && (here.op & 0xf0) == 0) {\r | |
499 | last = here;\r | |
500 | for (;;) {\r | |
501 | here = state->lencode[last.val +\r | |
502 | (BITS(last.bits + last.op) >> last.bits)];\r | |
503 | if ((unsigned)(last.bits + here.bits) <= bits) break;\r | |
504 | PULLBYTE();\r | |
505 | }\r | |
506 | DROPBITS(last.bits);\r | |
507 | }\r | |
508 | DROPBITS(here.bits);\r | |
509 | state->length = (unsigned)here.val;\r | |
510 | \r | |
511 | /* process literal */\r | |
512 | if (here.op == 0) {\r | |
513 | Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?\r | |
514 | "inflate: literal '%c'\n" :\r | |
515 | "inflate: literal 0x%02x\n", here.val));\r | |
516 | ROOM();\r | |
517 | *put++ = (unsigned char)(state->length);\r | |
518 | left--;\r | |
519 | state->mode = LEN;\r | |
520 | break;\r | |
521 | }\r | |
522 | \r | |
523 | /* process end of block */\r | |
524 | if (here.op & 32) {\r | |
525 | Tracevv((stderr, "inflate: end of block\n"));\r | |
526 | state->mode = TYPE;\r | |
527 | break;\r | |
528 | }\r | |
529 | \r | |
530 | /* invalid code */\r | |
531 | if (here.op & 64) {\r | |
532 | strm->msg = (char *)"invalid literal/length code";\r | |
533 | state->mode = BAD;\r | |
534 | break;\r | |
535 | }\r | |
536 | \r | |
537 | /* length code -- get extra bits, if any */\r | |
538 | state->extra = (unsigned)(here.op) & 15;\r | |
539 | if (state->extra != 0) {\r | |
540 | NEEDBITS(state->extra);\r | |
541 | state->length += BITS(state->extra);\r | |
542 | DROPBITS(state->extra);\r | |
543 | }\r | |
544 | Tracevv((stderr, "inflate: length %u\n", state->length));\r | |
545 | \r | |
546 | /* get distance code */\r | |
547 | for (;;) {\r | |
548 | here = state->distcode[BITS(state->distbits)];\r | |
549 | if ((unsigned)(here.bits) <= bits) break;\r | |
550 | PULLBYTE();\r | |
551 | }\r | |
552 | if ((here.op & 0xf0) == 0) {\r | |
553 | last = here;\r | |
554 | for (;;) {\r | |
555 | here = state->distcode[last.val +\r | |
556 | (BITS(last.bits + last.op) >> last.bits)];\r | |
557 | if ((unsigned)(last.bits + here.bits) <= bits) break;\r | |
558 | PULLBYTE();\r | |
559 | }\r | |
560 | DROPBITS(last.bits);\r | |
561 | }\r | |
562 | DROPBITS(here.bits);\r | |
563 | if (here.op & 64) {\r | |
564 | strm->msg = (char *)"invalid distance code";\r | |
565 | state->mode = BAD;\r | |
566 | break;\r | |
567 | }\r | |
568 | state->offset = (unsigned)here.val;\r | |
569 | \r | |
570 | /* get distance extra bits, if any */\r | |
571 | state->extra = (unsigned)(here.op) & 15;\r | |
572 | if (state->extra != 0) {\r | |
573 | NEEDBITS(state->extra);\r | |
574 | state->offset += BITS(state->extra);\r | |
575 | DROPBITS(state->extra);\r | |
576 | }\r | |
577 | if (state->offset > state->wsize - (state->whave < state->wsize ?\r | |
578 | left : 0)) {\r | |
579 | strm->msg = (char *)"invalid distance too far back";\r | |
580 | state->mode = BAD;\r | |
581 | break;\r | |
582 | }\r | |
583 | Tracevv((stderr, "inflate: distance %u\n", state->offset));\r | |
584 | \r | |
585 | /* copy match from window to output */\r | |
586 | do {\r | |
587 | ROOM();\r | |
588 | copy = state->wsize - state->offset;\r | |
589 | if (copy < left) {\r | |
590 | from = put + copy;\r | |
591 | copy = left - copy;\r | |
592 | }\r | |
593 | else {\r | |
594 | from = put - state->offset;\r | |
595 | copy = left;\r | |
596 | }\r | |
597 | if (copy > state->length) copy = state->length;\r | |
598 | state->length -= copy;\r | |
599 | left -= copy;\r | |
600 | do {\r | |
601 | *put++ = *from++;\r | |
602 | } while (--copy);\r | |
603 | } while (state->length != 0);\r | |
604 | break;\r | |
605 | \r | |
606 | case DONE:\r | |
607 | /* inflate stream terminated properly -- write leftover output */\r | |
608 | ret = Z_STREAM_END;\r | |
609 | if (left < state->wsize) {\r | |
610 | if (out(out_desc, state->window, state->wsize - left))\r | |
611 | ret = Z_BUF_ERROR;\r | |
612 | }\r | |
613 | goto inf_leave;\r | |
614 | \r | |
615 | case BAD:\r | |
616 | ret = Z_DATA_ERROR;\r | |
617 | goto inf_leave;\r | |
618 | \r | |
619 | default: /* can't happen, but makes compilers happy */\r | |
620 | ret = Z_STREAM_ERROR;\r | |
621 | goto inf_leave;\r | |
622 | }\r | |
623 | \r | |
624 | /* Return unused input */\r | |
625 | inf_leave:\r | |
626 | strm->next_in = next;\r | |
627 | strm->avail_in = have;\r | |
628 | return ret;\r | |
629 | }\r | |
630 | \r | |
631 | int ZEXPORT inflateBackEnd(strm)\r | |
632 | z_streamp strm;\r | |
633 | {\r | |
634 | if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)\r | |
635 | return Z_STREAM_ERROR;\r | |
636 | ZFREE(strm, strm->state);\r | |
637 | strm->state = Z_NULL;\r | |
638 | Tracev((stderr, "inflate: end\n"));\r | |
639 | return Z_OK;\r | |
640 | }\r |