Lines Matching +full:- +full:state

1 /* infback.c -- inflate using a call-back interface
2 * Copyright (C) 1995-2022 Mark Adler
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
22 windowBits is in the range 8..15, and window is a user-supplied
28 struct inflate_state FAR *state; in inflateBackInit_() local
36 strm->msg = Z_NULL; /* in case we return an error */ in inflateBackInit_()
37 if (strm->zalloc == (alloc_func)0) { in inflateBackInit_()
41 strm->zalloc = zcalloc; in inflateBackInit_()
42 strm->opaque = (voidpf)0; in inflateBackInit_()
45 if (strm->zfree == (free_func)0) in inflateBackInit_()
49 strm->zfree = zcfree; in inflateBackInit_()
51 state = (struct inflate_state FAR *)ZALLOC(strm, 1, in inflateBackInit_()
53 if (state == Z_NULL) return Z_MEM_ERROR; in inflateBackInit_()
55 strm->state = (struct internal_state FAR *)state; in inflateBackInit_()
56 state->dmax = 32768U; in inflateBackInit_()
57 state->wbits = (uInt)windowBits; in inflateBackInit_()
58 state->wsize = 1U << windowBits; in inflateBackInit_()
59 state->window = window; in inflateBackInit_()
60 state->wnext = 0; in inflateBackInit_()
61 state->whave = 0; in inflateBackInit_()
62 state->sane = 1; in inflateBackInit_()
67 Return state with length and distance decoding tables and index sizes set to
74 may not be thread-safe.
76 local void fixedtables(struct inflate_state FAR *state) { in fixedtables() argument
89 while (sym < 144) state->lens[sym++] = 8; in fixedtables()
90 while (sym < 256) state->lens[sym++] = 9; in fixedtables()
91 while (sym < 280) state->lens[sym++] = 7; in fixedtables()
92 while (sym < 288) state->lens[sym++] = 8; in fixedtables()
96 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work); in fixedtables()
100 while (sym < 32) state->lens[sym++] = 5; in fixedtables()
103 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work); in fixedtables()
111 state->lencode = lenfix; in fixedtables()
112 state->lenbits = 9; in fixedtables()
113 state->distcode = distfix; in fixedtables()
114 state->distbits = 5; in fixedtables()
119 /* Load returned state from inflate_fast() */
122 put = strm->next_out; \
123 left = strm->avail_out; \
124 next = strm->next_in; \
125 have = strm->avail_in; \
126 hold = state->hold; \
127 bits = state->bits; \
130 /* Set state from registers for inflate_fast() */
133 strm->next_out = put; \
134 strm->avail_out = left; \
135 strm->next_in = next; \
136 strm->avail_in = have; \
137 state->hold = hold; \
138 state->bits = bits; \
167 have--; \
183 ((unsigned)hold & ((1U << (n)) - 1))
189 bits -= (unsigned)(n); \
196 bits -= bits & 7; \
205 put = state->window; \
206 left = state->wsize; \
207 state->whave = left; \
220 in() and out() are the call-back input and output functions. When
233 in() should return zero on failure. out() should return non-zero on
235 Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
238 error, or Z_MEM_ERROR if it could not allocate memory for the state.
240 are not correct, i.e. strm is Z_NULL or the state was not initialized.
244 struct inflate_state FAR *state; in inflateBack() local
259 /* Check that the strm exists and that the state was initialized */ in inflateBack()
260 if (strm == Z_NULL || strm->state == Z_NULL) in inflateBack()
262 state = (struct inflate_state FAR *)strm->state; in inflateBack()
264 /* Reset the state */ in inflateBack()
265 strm->msg = Z_NULL; in inflateBack()
266 state->mode = TYPE; in inflateBack()
267 state->last = 0; in inflateBack()
268 state->whave = 0; in inflateBack()
269 next = strm->next_in; in inflateBack()
270 have = next != Z_NULL ? strm->avail_in : 0; in inflateBack()
273 put = state->window; in inflateBack()
274 left = state->wsize; in inflateBack()
278 switch (state->mode) { in inflateBack()
281 if (state->last) { in inflateBack()
283 state->mode = DONE; in inflateBack()
287 state->last = BITS(1); in inflateBack()
292 state->last ? " (last)" : "")); in inflateBack()
293 state->mode = STORED; in inflateBack()
296 fixedtables(state); in inflateBack()
298 state->last ? " (last)" : "")); in inflateBack()
299 state->mode = LEN; /* decode codes */ in inflateBack()
303 state->last ? " (last)" : "")); in inflateBack()
304 state->mode = TABLE; in inflateBack()
307 strm->msg = (char *)"invalid block type"; in inflateBack()
308 state->mode = BAD; in inflateBack()
318 strm->msg = (char *)"invalid stored block lengths"; in inflateBack()
319 state->mode = BAD; in inflateBack()
322 state->length = (unsigned)hold & 0xffff; in inflateBack()
324 state->length)); in inflateBack()
328 while (state->length != 0) { in inflateBack()
329 copy = state->length; in inflateBack()
335 have -= copy; in inflateBack()
337 left -= copy; in inflateBack()
339 state->length -= copy; in inflateBack()
342 state->mode = TYPE; in inflateBack()
348 state->nlen = BITS(5) + 257; in inflateBack()
350 state->ndist = BITS(5) + 1; in inflateBack()
352 state->ncode = BITS(4) + 4; in inflateBack()
355 if (state->nlen > 286 || state->ndist > 30) { in inflateBack()
356 strm->msg = (char *)"too many length or distance symbols"; in inflateBack()
357 state->mode = BAD; in inflateBack()
364 state->have = 0; in inflateBack()
365 while (state->have < state->ncode) { in inflateBack()
367 state->lens[order[state->have++]] = (unsigned short)BITS(3); in inflateBack()
370 while (state->have < 19) in inflateBack()
371 state->lens[order[state->have++]] = 0; in inflateBack()
372 state->next = state->codes; in inflateBack()
373 state->lencode = (code const FAR *)(state->next); in inflateBack()
374 state->lenbits = 7; in inflateBack()
375 ret = inflate_table(CODES, state->lens, 19, &(state->next), in inflateBack()
376 &(state->lenbits), state->work); in inflateBack()
378 strm->msg = (char *)"invalid code lengths set"; in inflateBack()
379 state->mode = BAD; in inflateBack()
385 state->have = 0; in inflateBack()
386 while (state->have < state->nlen + state->ndist) { in inflateBack()
388 here = state->lencode[BITS(state->lenbits)]; in inflateBack()
394 state->lens[state->have++] = here.val; in inflateBack()
400 if (state->have == 0) { in inflateBack()
401 strm->msg = (char *)"invalid bit length repeat"; in inflateBack()
402 state->mode = BAD; in inflateBack()
405 len = (unsigned)(state->lens[state->have - 1]); in inflateBack()
423 if (state->have + copy > state->nlen + state->ndist) { in inflateBack()
424 strm->msg = (char *)"invalid bit length repeat"; in inflateBack()
425 state->mode = BAD; in inflateBack()
428 while (copy--) in inflateBack()
429 state->lens[state->have++] = (unsigned short)len; in inflateBack()
434 if (state->mode == BAD) break; in inflateBack()
436 /* check for end-of-block code (better have one) */ in inflateBack()
437 if (state->lens[256] == 0) { in inflateBack()
438 strm->msg = (char *)"invalid code -- missing end-of-block"; in inflateBack()
439 state->mode = BAD; in inflateBack()
443 /* build code tables -- note: do not change the lenbits or distbits in inflateBack()
446 state->next = state->codes; in inflateBack()
447 state->lencode = (code const FAR *)(state->next); in inflateBack()
448 state->lenbits = 9; in inflateBack()
449 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), in inflateBack()
450 &(state->lenbits), state->work); in inflateBack()
452 strm->msg = (char *)"invalid literal/lengths set"; in inflateBack()
453 state->mode = BAD; in inflateBack()
456 state->distcode = (code const FAR *)(state->next); in inflateBack()
457 state->distbits = 6; in inflateBack()
458 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, in inflateBack()
459 &(state->next), &(state->distbits), state->work); in inflateBack()
461 strm->msg = (char *)"invalid distances set"; in inflateBack()
462 state->mode = BAD; in inflateBack()
466 state->mode = LEN; in inflateBack()
473 if (state->whave < state->wsize) in inflateBack()
474 state->whave = state->wsize - left; in inflateBack()
475 inflate_fast(strm, state->wsize); in inflateBack()
480 /* get a literal, length, or end-of-block code */ in inflateBack()
482 here = state->lencode[BITS(state->lenbits)]; in inflateBack()
489 here = state->lencode[last.val + in inflateBack()
497 state->length = (unsigned)here.val; in inflateBack()
505 *put++ = (unsigned char)(state->length); in inflateBack()
506 left--; in inflateBack()
507 state->mode = LEN; in inflateBack()
514 state->mode = TYPE; in inflateBack()
520 strm->msg = (char *)"invalid literal/length code"; in inflateBack()
521 state->mode = BAD; in inflateBack()
525 /* length code -- get extra bits, if any */ in inflateBack()
526 state->extra = (unsigned)(here.op) & 15; in inflateBack()
527 if (state->extra != 0) { in inflateBack()
528 NEEDBITS(state->extra); in inflateBack()
529 state->length += BITS(state->extra); in inflateBack()
530 DROPBITS(state->extra); in inflateBack()
532 Tracevv((stderr, "inflate: length %u\n", state->length)); in inflateBack()
536 here = state->distcode[BITS(state->distbits)]; in inflateBack()
543 here = state->distcode[last.val + in inflateBack()
552 strm->msg = (char *)"invalid distance code"; in inflateBack()
553 state->mode = BAD; in inflateBack()
556 state->offset = (unsigned)here.val; in inflateBack()
559 state->extra = (unsigned)(here.op) & 15; in inflateBack()
560 if (state->extra != 0) { in inflateBack()
561 NEEDBITS(state->extra); in inflateBack()
562 state->offset += BITS(state->extra); in inflateBack()
563 DROPBITS(state->extra); in inflateBack()
565 if (state->offset > state->wsize - (state->whave < state->wsize ? in inflateBack()
567 strm->msg = (char *)"invalid distance too far back"; in inflateBack()
568 state->mode = BAD; in inflateBack()
571 Tracevv((stderr, "inflate: distance %u\n", state->offset)); in inflateBack()
576 copy = state->wsize - state->offset; in inflateBack()
579 copy = left - copy; in inflateBack()
582 from = put - state->offset; in inflateBack()
585 if (copy > state->length) copy = state->length; in inflateBack()
586 state->length -= copy; in inflateBack()
587 left -= copy; in inflateBack()
590 } while (--copy); in inflateBack()
591 } while (state->length != 0); in inflateBack()
611 if (left < state->wsize) { in inflateBack()
612 if (out(out_desc, state->window, state->wsize - left) && in inflateBack()
616 strm->next_in = next; in inflateBack()
617 strm->avail_in = have; in inflateBack()
622 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) in inflateBackEnd()
624 ZFREE(strm, strm->state); in inflateBackEnd()
625 strm->state = Z_NULL; in inflateBackEnd()