Debian Codegraph @

src/lib/inflate.c

raw symbols SCIP
1 /* inflate.c -- zlib deflate64 decompression
2 * Copyright (C) 1995-2022 Mark Adler
3 * Copyright (C) 2022 Hiroshi Miura
4 */
5
6 #include "util.h"
7 #include "inflate_tree.h"
8 #include "inflate.h"
9 #include "inflate64.h"
10
11 /* function prototypes */
12 local int inflate9StateCheck OF((z_streamp strm));
13 local void fixedtables OF((struct inflate_state FAR *state));
14 local int updatewindow OF((z_streamp strm, const unsigned char FAR *end,
15 unsigned copy));
16
17 local int inflate9StateCheck(strm)
18 z_streamp strm;
19 {
20 struct inflate_state FAR *state;
21 if (strm == Z_NULL ||
22 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
23 return 1;
24 state = (struct inflate_state FAR *)strm->state;
25 if (state == Z_NULL || state->strm != strm ||
26 state->mode < TYPE || state->mode > MEM)
27 return 1;
28 return 0;
29 }
30
31 int ZEXPORT inflate9ResetKeep(strm)
32 z_streamp strm;
33 {
34 struct inflate_state FAR *state;
35
36 if (inflate9StateCheck(strm)) return Z_STREAM_ERROR;
37 state = (struct inflate_state FAR *)strm->state;
38 strm->total_in = strm->total_out = state->total = 0;
39 strm->msg = Z_NULL;
40 state->mode = TYPE;
41 state->last = 0;
42 state->hold = 0;
43 state->bits = 0;
44 state->lencode = state->distcode = state->next = state->codes;
45 Tracev((stderr, "inflate: reset\n"));
46 return Z_OK;
47 }
48
49 int ZEXPORT inflate9Reset(strm)
50 z_streamp strm;
51 {
52 struct inflate_state FAR *state;
53
54 if (inflate9StateCheck(strm)) return Z_STREAM_ERROR;
55 state = (struct inflate_state FAR *)strm->state;
56 state->wsize = 0;
57 state->whave = 0;
58 state->wnext = 0;
59 return inflate9ResetKeep(strm);
60 }
61
62 int ZEXPORT inflate9Reset2(strm)
63 z_streamp strm;
64 {
65 struct inflate_state FAR *state;
66
67 /* get the state */
68 if (inflate9StateCheck(strm)) return Z_STREAM_ERROR;
69 state = (struct inflate_state FAR *)strm->state;
70
71 if (state->window != Z_NULL) {
72 ZFREE(strm, state->window);
73 state->window = Z_NULL;
74 }
75
76 /* update state and reset the rest of it */
77 state->wrap = 0;
78 state->wbits = WBITS64;
79 return inflate9Reset(strm);
80 }
81
82 int ZEXPORT inflate9Init2(strm)
83 z_streamp strm;
84 {
85 int ret;
86 struct inflate_state FAR *state;
87 if (strm == Z_NULL) return Z_STREAM_ERROR;
88 strm->msg = Z_NULL; /* in case we return an error */
89 if (strm->zalloc == (alloc_func)0) {
90 return Z_STREAM_ERROR;
91 }
92 if (strm->zfree == (free_func)0)
93 return Z_STREAM_ERROR;
94 state = (struct inflate_state FAR *)
95 ZALLOC(strm, 1, sizeof(struct inflate_state));
96 if (state == Z_NULL) return Z_MEM_ERROR;
97 Tracev((stderr, "inflate: allocated\n"));
98 strm->state = (struct internal_state FAR *)state;
99 state->strm = strm;
100 state->window = Z_NULL;
101 state->mode = TYPE;
102 ret = inflate9Reset2(strm);
103 if (ret != Z_OK) {
104 ZFREE(strm, state);
105 strm->state = Z_NULL;
106 }
107 return ret;
108 }
109
110 /*
111 Return state with length and distance decoding tables and index sizes set to
112 fixed code decoding. Normally this returns fixed tables from inffixed.h.
113 If BUILDFIXED is defined, then instead this routine builds the tables the
114 first time it's called, and returns those tables the first time and
115 thereafter. This reduces the size of the code by about 2K bytes, in
116 exchange for a little execution time. However, BUILDFIXED should not be
117 used for threaded applications, since the rewriting of the tables and virgin
118 may not be thread-safe.
119 */
120 local void fixedtables(state)
121 struct inflate_state FAR *state;
122 {
123 #ifdef BUILDFIXED
124 static int virgin = 1;
125 static code *lenfix, *distfix;
126 static code fixed[544];
127
128 /* build fixed huffman tables if first call (may not be thread safe) */
129 if (virgin) {
130 gen_static_table9(state, &fixed, distfix, lenfix);
131 /* do this just once */
132 virgin = 0;
133 }
134 #else /* !BUILDFIXED */
135 # include "inflate_fixed9.h"
136
137 #endif /* BUILDFIXED */
138 state->lencode = lenfix;
139 state->lenbits = 9;
140 state->distcode = distfix;
141 state->distbits = 5;
142 }
143
144 /*
145 Update the window with the last wsize (normally 32K) bytes written before
146 returning. If window does not exist yet, create it. This is only called
147 when a window is already in use, or when output has been written during this
148 inflate call, but the end of the deflate stream has not been reached yet.
149 It is also called to create a window for dictionary data when a dictionary
150 is loaded.
151
152 Providing output buffers larger than 32K to inflate() should provide a speed
153 advantage, since only the last 32K of output is copied to the sliding window
154 upon return from inflate(), and since all distances after the first 32K of
155 output will fall in the output data, making match copies simpler and faster.
156 The advantage may be dependent on the size of the processor's data caches.
157 */
158 local int updatewindow(strm, end, copy)
159 z_streamp strm;
160 const Byte FAR *end;
161 unsigned copy;
162 {
163 struct inflate_state FAR *state;
164 unsigned dist;
165
166 state = (struct inflate_state FAR *)strm->state;
167
168 /* if it hasn't been done already, allocate space for the window */
169 if (state->window == Z_NULL) {
170 state->window = (unsigned char FAR *)
171 ZALLOC(strm, 1U << state->wbits,
172 sizeof(unsigned char));
173 if (state->window == Z_NULL) return 1;
174 }
175
176 /* if window not in use yet, initialize */
177 if (state->wsize == 0) {
178 state->wsize = 1U << state->wbits;
179 state->wnext = 0;
180 state->whave = 0;
181 }
182
183 /* copy state->wsize or less output bytes into the circular window */
184 if (copy >= state->wsize) {
185 zmemcpy(state->window, end - state->wsize, state->wsize);
186 state->wnext = 0;
187 state->whave = state->wsize;
188 }
189 else {
190 dist = state->wsize - state->wnext;
191 if (dist > copy) dist = copy;
192 zmemcpy(state->window + state->wnext, end - copy, dist);
193 copy -= dist;
194 if (copy) {
195 zmemcpy(state->window, end - copy, copy);
196 state->wnext = copy;
197 state->whave = state->wsize;
198 }
199 else {
200 state->wnext += dist;
201 if (state->wnext == state->wsize) state->wnext = 0;
202 if (state->whave < state->wsize) state->whave += dist;
203 }
204 }
205 return 0;
206 }
207
208 /* Load registers with state in inflate() for speed */
209 #define LOAD() \
210 do { \
211 put = strm->next_out; \
212 left = strm->avail_out; \
213 next = strm->next_in; \
214 have = strm->avail_in; \
215 hold = state->hold; \
216 bits = state->bits; \
217 } while (0)
218
219 /* Restore state from registers in inflate() */
220 #define RESTORE() \
221 do { \
222 strm->next_out = put; \
223 strm->avail_out = left; \
224 strm->next_in = next; \
225 strm->avail_in = have; \
226 state->hold = hold; \
227 state->bits = bits; \
228 } while (0)
229
230 /* Clear the input bit accumulator */
231 #define INITBITS() \
232 do { \
233 hold = 0; \
234 bits = 0; \
235 } while (0)
236
237 /* Get a byte of input into the bit accumulator, or return from inflate()
238 if there is no input available. */
239 #define PULLBYTE() \
240 do { \
241 if (have == 0) goto inf_leave; \
242 have--; \
243 hold += (unsigned long)(*next++) << bits; \
244 bits += 8; \
245 } while (0)
246
247 /* Assure that there are at least n bits in the bit accumulator. If there is
248 not enough available input to do that, then return from inflate(). */
249 #define NEEDBITS(n) \
250 do { \
251 while (bits < (unsigned)(n)) \
252 PULLBYTE(); \
253 } while (0)
254
255 /* Return the low n bits of the bit accumulator (n < 16) */
256 #define BITS(n) \
257 ((unsigned)hold & ((1U << (n)) - 1))
258
259 /* Remove n bits from the bit accumulator */
260 #define DROPBITS(n) \
261 do { \
262 hold >>= (n); \
263 bits -= (unsigned)(n); \
264 } while (0)
265
266 /* Remove zero to seven bits as needed to go to a byte boundary */
267 #define BYTEBITS() \
268 do { \
269 hold >>= bits & 7; \
270 bits -= bits & 7; \
271 } while (0)
272
273 /*
274 *
275 * Original explanation of inflate()
276 ****************************************************
277 inflate() uses a state machine to process as much input data and generate as
278 much output data as possible before returning. The state machine is
279 structured roughly as follows:
280
281 for (;;) switch (state) {
282 ...
283 case STATEn:
284 if (not enough input data or output space to make progress)
285 return;
286 ... make progress ...
287 state = STATEm;
288 break;
289 ...
290 }
291
292 so when inflate() is called again, the same case is attempted again, and
293 if the appropriate resources are provided, the machine proceeds to the
294 next state. The NEEDBITS() macro is usually the way the state evaluates
295 whether it can proceed or should return. NEEDBITS() does the return if
296 the requested bits are not available. The typical use of the BITS macros
297 is:
298
299 NEEDBITS(n);
300 ... do something with BITS(n) ...
301 DROPBITS(n);
302
303 where NEEDBITS(n) either returns from inflate() if there isn't enough
304 input left to load n bits into the accumulator, or it continues. BITS(n)
305 gives the low n bits in the accumulator. When done, DROPBITS(n) drops
306 the low n bits off the accumulator. INITBITS() clears the accumulator
307 and sets the number of available bits to zero. BYTEBITS() discards just
308 enough bits to put the accumulator on a byte boundary. After BYTEBITS()
309 and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
310
311 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
312 if there is no input available. The decoding of variable length codes uses
313 PULLBYTE() directly in order to pull just enough bytes to decode the next
314 code, and no more.
315
316 Some states loop until they get enough input, making sure that enough
317 state information is maintained to continue the loop where it left off
318 if NEEDBITS() returns in the loop. For example, want, need, and keep
319 would all have to actually be part of the saved state in case NEEDBITS()
320 returns:
321
322 case STATEw:
323 while (want < need) {
324 NEEDBITS(n);
325 keep[want++] = BITS(n);
326 DROPBITS(n);
327 }
328 state = STATEx;
329 case STATEx:
330
331 As shown above, if the next state is also the next case, then the break
332 is omitted.
333
334 A state may also return if there is not enough output space available to
335 complete that state. Those states are copying stored data, writing a
336 literal byte, and copying a matching string.
337
338 When returning, a "goto inf_leave" is used to update the total counters,
339 update the check value, and determine whether any progress has been made
340 during that inflate() call in order to return the proper return code.
341 Progress is defined as a change in either strm->avail_in or strm->avail_out.
342 When there is a window, goto inf_leave will update the window with the last
343 output written. If a goto inf_leave occurs in the middle of decompression
344 and there is no window currently, goto inf_leave will create one and copy
345 output to the window for the next call of inflate().
346
347 In this implementation, the flush parameter of inflate() only affects the
348 return code (per zlib.h). inflate() always writes as much as possible to
349 strm->next_out, given the space available and the provided input--the effect
350 documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
351 the allocation of and copying into a sliding window until necessary, which
352 provides the effect documented in zlib.h for Z_FINISH when the entire input
353 stream available. So the only thing the flush parameter actually does is:
354 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
355 will return Z_BUF_ERROR if it has not reached the end of the stream.
356 */
357
358 int ZEXPORT inflate9(strm, flush)
359 z_streamp strm;
360 int flush;
361 {
362 struct inflate_state FAR *state;
363 z_const unsigned char FAR *next; /* next input */
364 unsigned char FAR *put; /* next output */
365 unsigned have; /* available input */
366 unsigned long left; /* available output */
367 unsigned long hold; /* bit buffer */
368 unsigned bits; /* bits in bit buffer */
369 unsigned in; /* save starting available input */
370 unsigned long out; /* save starting available output */
371 unsigned long copy; /* number of stored or match bytes to copy */
372 unsigned char FAR *from; /* where to copy match bytes from */
373 code here; /* current decoding table entry */
374 code last; /* parent table entry */
375 unsigned len; /* length to copy for repeats, bits to drop */
376 int ret; /* return code */
377 static const unsigned short order[19] = /* permutation of code lengths */
378 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
379
380 if (inflate9StateCheck(strm) || strm->next_out == Z_NULL ||
381 (strm->next_in == Z_NULL && strm->avail_in != 0))
382 return Z_STREAM_ERROR;
383
384 state = (struct inflate_state FAR *)strm->state;
385 LOAD();
386 in = have;
387 out = left;
388 ret = Z_OK;
389 for (;;)
390 switch (state->mode) {
391 case TYPE:
392 /* fallthrough */
393 case TYPED0:
394 if (state->last) {
395 BYTEBITS();
396 state->mode = DONE; /* no check */
397 break;
398 }
399 NEEDBITS(3);
400 state->last = BITS(1);
401 DROPBITS(1);
402 switch (BITS(2)) {
403 case 0: /* stored block */
404 Tracev((stderr, "inflate: stored block%s\n",
405 state->last ? " (last)" : ""));
406 state->mode = STORED;
407 break;
408 case 1: /* fixed block */
409 fixedtables(state);
410 Tracev((stderr, "inflate: fixed codes block%s\n",
411 state->last ? " (last)" : ""));
412 state->mode = LEN; /* decode codes */
413 break;
414 case 2: /* dynamic block */
415 Tracev((stderr, "inflate: dynamic codes block%s\n",
416 state->last ? " (last)" : ""));
417 state->mode = TABLE;
418 break;
419 case 3:
420 strm->msg = (char *)"invalid block type";
421 state->mode = BAD;
422 }
423 DROPBITS(2);
424 break;
425 case STORED:
426 BYTEBITS(); /* go to byte boundary */
427 NEEDBITS(32);
428 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
429 strm->msg = (char *)"invalid stored block lengths";
430 state->mode = BAD;
431 break;
432 }
433 state->length = (unsigned)hold & 0xffff;
434 Tracev((stderr, "inflate: stored length %u\n",
435 state->length));
436 INITBITS();
437 state->mode = COPY_;
438 /* fallthrough */
439 case COPY_:
440 state->mode = COPY;
441 /* fallthrough */
442 case COPY:
443 copy = state->length;
444 if (copy) {
445 if (copy > have) copy = have;
446 if (copy > left) copy = left;
447 if (copy == 0) goto inf_leave;
448 zmemcpy(put, next, copy);
449 have -= copy;
450 next += copy;
451 left -= copy;
452 put += copy;
453 state->length -= copy;
454 break;
455 }
456 Tracev((stderr, "inflate: stored end\n"));
457 state->mode = TYPE;
458 break;
459 case TABLE:
460 NEEDBITS(14);
461 state->nlen = BITS(5) + 257;
462 DROPBITS(5);
463 state->ndist = BITS(5) + 1;
464 DROPBITS(5);
465 state->ncode = BITS(4) + 4;
466 DROPBITS(4);
467 if (state->nlen > 288 || state->ndist > 33) { // (state->nlen > 286 || state->ndist > 30)
468 strm->msg = (char *)"too many length symbols";
469 state->mode = BAD;
470 break;
471 }
472 Tracev((stderr, "inflate: table sizes ok\n"));
473 state->have = 0;
474 state->mode = LENLENS;
475 /* fallthrough */
476 case LENLENS:
477 while (state->have < state->ncode) {
478 NEEDBITS(3);
479 state->lens[order[state->have++]] = (unsigned short)BITS(3);
480 DROPBITS(3);
481 }
482 while (state->have < 19)
483 state->lens[order[state->have++]] = 0;
484 state->next = state->codes;
485 state->lencode = (const code FAR *)(state->next);
486 state->lenbits = 7;
487 ret = inflate_table9(CODES, state->lens, 19, &(state->next),
488 &(state->lenbits), state->work);
489 if (ret) {
490 strm->msg = (char *)"invalid code lengths set";
491 state->mode = BAD;
492 break;
493 }
494 Tracev((stderr, "inflate: code lengths ok\n"));
495 state->have = 0;
496 state->mode = CODELENS;
497 /* fallthrough */
498 case CODELENS:
499 while (state->have < state->nlen + state->ndist) {
500 for (;;) {
501 here = state->lencode[BITS(state->lenbits)];
502 if ((unsigned)(here.bits) <= bits) break;
503 PULLBYTE();
504 }
505 if (here.val < 16) {
506 DROPBITS(here.bits);
507 state->lens[state->have++] = here.val;
508 }
509 else {
510 if (here.val == 16) {
511 NEEDBITS(here.bits + 2);
512 DROPBITS(here.bits);
513 if (state->have == 0) {
514 strm->msg = (char *)"invalid bit length repeat";
515 state->mode = BAD;
516 break;
517 }
518 len = (unsigned)(state->lens[state->have - 1]);
519 copy = 3 + BITS(2);
520 DROPBITS(2);
521 }
522 else if (here.val == 17) {
523 NEEDBITS(here.bits + 3);
524 DROPBITS(here.bits);
525 len = 0;
526 copy = 3 + BITS(3);
527 DROPBITS(3);
528 }
529 else {
530 NEEDBITS(here.bits + 7);
531 DROPBITS(here.bits);
532 len = 0;
533 copy = 11 + BITS(7);
534 DROPBITS(7);
535 }
536 if (state->have + copy > state->nlen + state->ndist) {
537 strm->msg = (char *)"invalid bit length repeat";
538 state->mode = BAD;
539 break;
540 }
541 while (copy--)
542 state->lens[state->have++] = (unsigned short)len;
543 }
544 }
545
546 /* handle error breaks in while */
547 if (state->mode == BAD) break;
548
549 /* check for end-of-block code (better have one) */
550 if (state->lens[256] == 0) {
551 strm->msg = (char *)"invalid code -- missing end-of-block";
552 state->mode = BAD;
553 break;
554 }
555
556 /* build code tables -- note: do not change the lenbits or distbits
557 values here (9 and 6) without reading the comments in inftrees.h
558 concerning the ENOUGH constants, which depend on those values */
559 state->next = state->codes;
560 state->lencode = (const code FAR *)(state->next);
561 state->lenbits = 9;
562 ret = inflate_table9(LENS, state->lens, state->nlen, &(state->next),
563 &(state->lenbits), state->work);
564 if (ret) {
565 strm->msg = (char *)"invalid literal/lengths set";
566 state->mode = BAD;
567 break;
568 }
569 state->distcode = (const code FAR *)(state->next);
570 state->distbits = 6;
571 ret = inflate_table9(DISTS, state->lens + state->nlen, state->ndist,
572 &(state->next), &(state->distbits), state->work);
573 if (ret) {
574 strm->msg = (char *)"invalid distances set";
575 state->mode = BAD;
576 break;
577 }
578 Tracev((stderr, "inflate: codes ok\n"));
579 state->mode = LEN;
580 /* fallthrough */
581 case LEN_:
582 state->mode = LEN;
583 /* fallthrough */
584 case LEN:
585 state->back = 0;
586 for (;;) {
587 here = state->lencode[BITS(state->lenbits)];
588 if ((unsigned)(here.bits) <= bits) break;
589 PULLBYTE();
590 }
591 if (here.op && (here.op & 0xf0) == 0) {
592 last = here;
593 for (;;) {
594 here = state->lencode[last.val +
595 (BITS(last.bits + last.op) >> last.bits)];
596 if ((unsigned)(last.bits + here.bits) <= bits) break;
597 PULLBYTE();
598 }
599 DROPBITS(last.bits);
600 state->back += last.bits;
601 }
602 DROPBITS(here.bits);
603 state->back += here.bits;
604 state->length = (unsigned)here.val;
605 /* process literal */
606 if ((int)(here.op) == 0) {
607 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
608 "inflate: literal '%c'\n" :
609 "inflate: literal 0x%02x\n", here.val));
610 state->mode = LIT;
611 break;
612 }
613 /* process end of block */
614 if (here.op & 32) {
615 Tracevv((stderr, "inflate: end of block\n"));
616 state->back = -1;
617 state->mode = TYPE;
618 break;
619 }
620 /* invalid code */
621 if (here.op & 64) {
622 strm->msg = (char *)"invalid literal/length code";
623 state->mode = BAD;
624 break;
625 }
626 state->extra = (unsigned)(here.op) & 31; // use 31 instead of 15
627 state->mode = LENEXT;
628 /* fallthrough */
629 case LENEXT:
630 /* length code */
631 if (state->extra) {
632 NEEDBITS(state->extra);
633 state->length += BITS(state->extra);
634 DROPBITS(state->extra);
635 state->back += state->extra;
636 }
637 Tracevv((stderr, "inflate: length %u\n", state->length));
638 state->was = state->length;
639 state->mode = DIST;
640 /* fallthrough */
641 case DIST:
642 /* get distance code */
643 for (;;) {
644 here = state->distcode[BITS(state->distbits)];
645 if ((unsigned)(here.bits) <= bits) break;
646 PULLBYTE();
647 }
648 if ((here.op & 0xf0) == 0) { // XXX 0xe0 ?
649 last = here;
650 for (;;) {
651 here = state->distcode[last.val +
652 (BITS(last.bits + last.op) >> last.bits)];
653 if ((unsigned)(last.bits + here.bits) <= bits) break;
654 PULLBYTE();
655 }
656 DROPBITS(last.bits);
657 state->back += last.bits;
658 }
659 DROPBITS(here.bits);
660 state->back += here.bits;
661 if (here.op & 64) {
662 strm->msg = (char *)"invalid distance code";
663 state->mode = BAD;
664 break;
665 }
666 state->offset = (unsigned)here.val;
667 /* get distance extra bits, if any */
668 state->extra = (unsigned)(here.op) & 31; // XXX 31 instead of 15
669 state->mode = DISTEXT;
670 /* fallthrough */
671 case DISTEXT:
672 if (state->extra) {
673 NEEDBITS(state->extra);
674 state->offset += BITS(state->extra);
675 DROPBITS(state->extra);
676 state->back += state->extra;
677 }
678 Tracevv((stderr, "inflate: distance %u\n", state->offset));
679 state->mode = MATCH;
680 /* fallthrough */
681 case MATCH:
682 if (left == 0) goto inf_leave;
683 copy = out - left;
684 if (state->offset > copy) { /* copy from window */
685 if (state->window == NULL) {
686 strm->msg = (char *)"try to copy from null window buffer!";
687 state->mode = BAD;
688 break;
689 }
690 copy = state->offset - copy;
691 if (copy > state->whave) {
692 if (state->sane) {
693 strm->msg = (char *)"invalid distance too far back";
694 state->mode = BAD;
695 break;
696 }
697 }
698 if (copy > state->wnext) {
699 copy -= state->wnext;
700 from = state->window + (state->wsize - copy);
701 } else {
702 from = state->window + (state->wnext - copy);
703 }
704 if (copy > state->length) copy = state->length;
705 } else { // copy from output
706 from = put - state->offset;
707 copy = state->length;
708 }
709 if (copy > left) copy = left;
710 left -= copy;
711 state->length -= copy;
712 do {
713 *put++ = *from++;
714 } while (--copy);
715 if (state->length == 0) state->mode = LEN;
716 break;
717 case LIT:
718 if (left == 0) goto inf_leave;
719 *put++ = (unsigned char)(state->length);
720 left--;
721 state->mode = LEN;
722 break;
723 case DONE:
724 ret = Z_STREAM_END;
725 goto inf_leave;
726 case BAD:
727 ret = Z_DATA_ERROR;
728 goto inf_leave;
729 case MEM:
730 return Z_MEM_ERROR;
731 default:
732 return Z_STREAM_ERROR;
733 }
734
735 /*
736 Return from inflate(), updating the total counts and the check value.
737 If there was no progress during the inflate() call, return a buffer
738 error. Call updatewindow() to create and/or update the window state.
739 Note: a memory error from inflate() is non-recoverable.
740 */
741 inf_leave:
742 RESTORE();
743 if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
744 (state->mode < CHECK || flush != Z_FINISH)))
745 if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
746 state->mode = MEM;
747 return Z_MEM_ERROR;
748 }
749 in -= strm->avail_in;
750 out -= strm->avail_out;
751 strm->total_in += in;
752 strm->total_out += out;
753 state->total += out;
754 strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
755 (state->mode == TYPE ? 128 : 0) +
756 (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
757 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
758 ret = Z_BUF_ERROR;
759 return ret;
760 }
761
762 int ZEXPORT inflate9End(strm)
763 z_streamp strm;
764 {
765 struct inflate_state FAR *state;
766 if (inflate9StateCheck(strm))
767 return Z_STREAM_ERROR;
768 state = (struct inflate_state FAR *)strm->state;
769 if (state->window != Z_NULL) ZFREE(strm, state->window);
770 ZFREE(strm, strm->state);
771 strm->state = Z_NULL;
772 Tracev((stderr, "inflate: end\n"));
773 return Z_OK;
774 }