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