comparison zlib/infback.c @ 2690:9ef72ee5c0b0

Update vendored zlib to 1.3.1
author Michael Pavone <pavone@retrodev.com>
date Sun, 15 Jun 2025 15:39:33 -0700
parents 00d788dac91a
children
comparison
equal deleted inserted replaced
2689:bd6e33de0972 2690:9ef72ee5c0b0
1 /* infback.c -- inflate using a call-back interface 1 /* infback.c -- inflate using a call-back interface
2 * Copyright (C) 1995-2016 Mark Adler 2 * Copyright (C) 1995-2022 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */ 4 */
5 5
6 /* 6 /*
7 This code is largely copied from inflate.c. Normally either infback.o or 7 This code is largely copied from inflate.c. Normally either infback.o or
13 #include "zutil.h" 13 #include "zutil.h"
14 #include "inftrees.h" 14 #include "inftrees.h"
15 #include "inflate.h" 15 #include "inflate.h"
16 #include "inffast.h" 16 #include "inffast.h"
17 17
18 /* function prototypes */
19 local void fixedtables OF((struct inflate_state FAR *state));
20
21 /* 18 /*
22 strm provides memory allocation functions in zalloc and zfree, or 19 strm provides memory allocation functions in zalloc and zfree, or
23 Z_NULL to use the library memory allocation functions. 20 Z_NULL to use the library memory allocation functions.
24 21
25 windowBits is in the range 8..15, and window is a user-supplied 22 windowBits is in the range 8..15, and window is a user-supplied
26 window and output buffer that is 2**windowBits bytes. 23 window and output buffer that is 2**windowBits bytes.
27 */ 24 */
28 int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size) 25 int ZEXPORT inflateBackInit_(z_streamp strm, int windowBits,
29 z_streamp strm; 26 unsigned char FAR *window, const char *version,
30 int windowBits; 27 int stream_size) {
31 unsigned char FAR *window;
32 const char *version;
33 int stream_size;
34 {
35 struct inflate_state FAR *state; 28 struct inflate_state FAR *state;
36 29
37 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || 30 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
38 stream_size != (int)(sizeof(z_stream))) 31 stream_size != (int)(sizeof(z_stream)))
39 return Z_VERSION_ERROR; 32 return Z_VERSION_ERROR;
64 state->wbits = (uInt)windowBits; 57 state->wbits = (uInt)windowBits;
65 state->wsize = 1U << windowBits; 58 state->wsize = 1U << windowBits;
66 state->window = window; 59 state->window = window;
67 state->wnext = 0; 60 state->wnext = 0;
68 state->whave = 0; 61 state->whave = 0;
62 state->sane = 1;
69 return Z_OK; 63 return Z_OK;
70 } 64 }
71 65
72 /* 66 /*
73 Return state with length and distance decoding tables and index sizes set to 67 Return state with length and distance decoding tables and index sizes set to
77 thereafter. This reduces the size of the code by about 2K bytes, in 71 thereafter. This reduces the size of the code by about 2K bytes, in
78 exchange for a little execution time. However, BUILDFIXED should not be 72 exchange for a little execution time. However, BUILDFIXED should not be
79 used for threaded applications, since the rewriting of the tables and virgin 73 used for threaded applications, since the rewriting of the tables and virgin
80 may not be thread-safe. 74 may not be thread-safe.
81 */ 75 */
82 local void fixedtables(state) 76 local void fixedtables(struct inflate_state FAR *state) {
83 struct inflate_state FAR *state;
84 {
85 #ifdef BUILDFIXED 77 #ifdef BUILDFIXED
86 static int virgin = 1; 78 static int virgin = 1;
87 static code *lenfix, *distfix; 79 static code *lenfix, *distfix;
88 static code fixed[544]; 80 static code fixed[544];
89 81
245 returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format 237 returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
246 error, or Z_MEM_ERROR if it could not allocate memory for the state. 238 error, or Z_MEM_ERROR if it could not allocate memory for the state.
247 inflateBack() can also return Z_STREAM_ERROR if the input parameters 239 inflateBack() can also return Z_STREAM_ERROR if the input parameters
248 are not correct, i.e. strm is Z_NULL or the state was not initialized. 240 are not correct, i.e. strm is Z_NULL or the state was not initialized.
249 */ 241 */
250 int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc) 242 int ZEXPORT inflateBack(z_streamp strm, in_func in, void FAR *in_desc,
251 z_streamp strm; 243 out_func out, void FAR *out_desc) {
252 in_func in;
253 void FAR *in_desc;
254 out_func out;
255 void FAR *out_desc;
256 {
257 struct inflate_state FAR *state; 244 struct inflate_state FAR *state;
258 z_const unsigned char FAR *next; /* next input */ 245 z_const unsigned char FAR *next; /* next input */
259 unsigned char FAR *put; /* next output */ 246 unsigned char FAR *put; /* next output */
260 unsigned have, left; /* available input and output */ 247 unsigned have, left; /* available input and output */
261 unsigned long hold; /* bit buffer */ 248 unsigned long hold; /* bit buffer */
475 state->mode = BAD; 462 state->mode = BAD;
476 break; 463 break;
477 } 464 }
478 Tracev((stderr, "inflate: codes ok\n")); 465 Tracev((stderr, "inflate: codes ok\n"));
479 state->mode = LEN; 466 state->mode = LEN;
467 /* fallthrough */
480 468
481 case LEN: 469 case LEN:
482 /* use inflate_fast() if we have enough input and output */ 470 /* use inflate_fast() if we have enough input and output */
483 if (have >= 6 && left >= 258) { 471 if (have >= 6 && left >= 258) {
484 RESTORE(); 472 RESTORE();
602 } while (--copy); 590 } while (--copy);
603 } while (state->length != 0); 591 } while (state->length != 0);
604 break; 592 break;
605 593
606 case DONE: 594 case DONE:
607 /* inflate stream terminated properly -- write leftover output */ 595 /* inflate stream terminated properly */
608 ret = Z_STREAM_END; 596 ret = Z_STREAM_END;
609 if (left < state->wsize) {
610 if (out(out_desc, state->window, state->wsize - left))
611 ret = Z_BUF_ERROR;
612 }
613 goto inf_leave; 597 goto inf_leave;
614 598
615 case BAD: 599 case BAD:
616 ret = Z_DATA_ERROR; 600 ret = Z_DATA_ERROR;
617 goto inf_leave; 601 goto inf_leave;
618 602
619 default: /* can't happen, but makes compilers happy */ 603 default:
604 /* can't happen, but makes compilers happy */
620 ret = Z_STREAM_ERROR; 605 ret = Z_STREAM_ERROR;
621 goto inf_leave; 606 goto inf_leave;
622 } 607 }
623 608
624 /* Return unused input */ 609 /* Write leftover output and return unused input */
625 inf_leave: 610 inf_leave:
611 if (left < state->wsize) {
612 if (out(out_desc, state->window, state->wsize - left) &&
613 ret == Z_STREAM_END)
614 ret = Z_BUF_ERROR;
615 }
626 strm->next_in = next; 616 strm->next_in = next;
627 strm->avail_in = have; 617 strm->avail_in = have;
628 return ret; 618 return ret;
629 } 619 }
630 620
631 int ZEXPORT inflateBackEnd(strm) 621 int ZEXPORT inflateBackEnd(z_streamp strm) {
632 z_streamp strm;
633 {
634 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) 622 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
635 return Z_STREAM_ERROR; 623 return Z_STREAM_ERROR;
636 ZFREE(strm, strm->state); 624 ZFREE(strm, strm->state);
637 strm->state = Z_NULL; 625 strm->state = Z_NULL;
638 Tracev((stderr, "inflate: end\n")); 626 Tracev((stderr, "inflate: end\n"));