annotate zlib/inflate.c @ 1965:3a46ff899fa6

More correct implementation of byte printing in builtin debugger. Fix GDB debugger to use helper in backend.c for reading bytes
author Michael Pavone <pavone@retrodev.com>
date Sun, 03 May 2020 23:28:42 -0700
parents 00d788dac91a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1530
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1 /* inflate.c -- zlib decompression
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
2 * Copyright (C) 1995-2016 Mark Adler
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3 * For conditions of distribution and use, see copyright notice in zlib.h
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4 */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
5
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
6 /*
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
7 * Change history:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
8 *
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
9 * 1.2.beta0 24 Nov 2002
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
10 * - First version -- complete rewrite of inflate to simplify code, avoid
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
11 * creation of window when not needed, minimize use of window when it is
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
12 * needed, make inffast.c even faster, implement gzip decoding, and to
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
13 * improve code readability and style over the previous zlib inflate code
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
14 *
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
15 * 1.2.beta1 25 Nov 2002
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
16 * - Use pointers for available input and output checking in inffast.c
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
17 * - Remove input and output counters in inffast.c
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
18 * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
19 * - Remove unnecessary second byte pull from length extra in inffast.c
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
20 * - Unroll direct copy to three copies per loop in inffast.c
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
21 *
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
22 * 1.2.beta2 4 Dec 2002
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
23 * - Change external routine names to reduce potential conflicts
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
24 * - Correct filename to inffixed.h for fixed tables in inflate.c
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
25 * - Make hbuf[] unsigned char to match parameter type in inflate.c
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
26 * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
27 * to avoid negation problem on Alphas (64 bit) in inflate.c
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
28 *
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
29 * 1.2.beta3 22 Dec 2002
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
30 * - Add comments on state->bits assertion in inffast.c
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
31 * - Add comments on op field in inftrees.h
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
32 * - Fix bug in reuse of allocated window after inflateReset()
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
33 * - Remove bit fields--back to byte structure for speed
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
34 * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
35 * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
36 * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
37 * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
38 * - Use local copies of stream next and avail values, as well as local bit
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
39 * buffer and bit count in inflate()--for speed when inflate_fast() not used
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
40 *
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
41 * 1.2.beta4 1 Jan 2003
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
42 * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
43 * - Move a comment on output buffer sizes from inffast.c to inflate.c
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
44 * - Add comments in inffast.c to introduce the inflate_fast() routine
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
45 * - Rearrange window copies in inflate_fast() for speed and simplification
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
46 * - Unroll last copy for window match in inflate_fast()
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
47 * - Use local copies of window variables in inflate_fast() for speed
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
48 * - Pull out common wnext == 0 case for speed in inflate_fast()
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
49 * - Make op and len in inflate_fast() unsigned for consistency
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
50 * - Add FAR to lcode and dcode declarations in inflate_fast()
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
51 * - Simplified bad distance check in inflate_fast()
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
52 * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
53 * source file infback.c to provide a call-back interface to inflate for
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
54 * programs like gzip and unzip -- uses window as output buffer to avoid
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
55 * window copying
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
56 *
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
57 * 1.2.beta5 1 Jan 2003
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
58 * - Improved inflateBack() interface to allow the caller to provide initial
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
59 * input in strm.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
60 * - Fixed stored blocks bug in inflateBack()
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
61 *
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
62 * 1.2.beta6 4 Jan 2003
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
63 * - Added comments in inffast.c on effectiveness of POSTINC
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
64 * - Typecasting all around to reduce compiler warnings
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
65 * - Changed loops from while (1) or do {} while (1) to for (;;), again to
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
66 * make compilers happy
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
67 * - Changed type of window in inflateBackInit() to unsigned char *
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
68 *
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
69 * 1.2.beta7 27 Jan 2003
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
70 * - Changed many types to unsigned or unsigned short to avoid warnings
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
71 * - Added inflateCopy() function
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
72 *
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
73 * 1.2.0 9 Mar 2003
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
74 * - Changed inflateBack() interface to provide separate opaque descriptors
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
75 * for the in() and out() functions
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
76 * - Changed inflateBack() argument and in_func typedef to swap the length
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
77 * and buffer address return values for the input function
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
78 * - Check next_in and next_out for Z_NULL on entry to inflate()
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
79 *
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
80 * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
81 */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
82
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
83 #include "zutil.h"
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
84 #include "inftrees.h"
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
85 #include "inflate.h"
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
86 #include "inffast.h"
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
87
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
88 #ifdef MAKEFIXED
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
89 # ifndef BUILDFIXED
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
90 # define BUILDFIXED
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
91 # endif
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
92 #endif
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
93
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
94 /* function prototypes */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
95 local int inflateStateCheck OF((z_streamp strm));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
96 local void fixedtables OF((struct inflate_state FAR *state));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
97 local int updatewindow OF((z_streamp strm, const unsigned char FAR *end,
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
98 unsigned copy));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
99 #ifdef BUILDFIXED
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
100 void makefixed OF((void));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
101 #endif
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
102 local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf,
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
103 unsigned len));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
104
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
105 local int inflateStateCheck(strm)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
106 z_streamp strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
107 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
108 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
109 if (strm == Z_NULL ||
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
110 strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
111 return 1;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
112 state = (struct inflate_state FAR *)strm->state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
113 if (state == Z_NULL || state->strm != strm ||
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
114 state->mode < HEAD || state->mode > SYNC)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
115 return 1;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
116 return 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
117 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
118
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
119 int ZEXPORT inflateResetKeep(strm)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
120 z_streamp strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
121 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
122 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
123
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
124 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
125 state = (struct inflate_state FAR *)strm->state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
126 strm->total_in = strm->total_out = state->total = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
127 strm->msg = Z_NULL;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
128 if (state->wrap) /* to support ill-conceived Java test suite */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
129 strm->adler = state->wrap & 1;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
130 state->mode = HEAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
131 state->last = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
132 state->havedict = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
133 state->dmax = 32768U;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
134 state->head = Z_NULL;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
135 state->hold = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
136 state->bits = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
137 state->lencode = state->distcode = state->next = state->codes;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
138 state->sane = 1;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
139 state->back = -1;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
140 Tracev((stderr, "inflate: reset\n"));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
141 return Z_OK;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
142 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
143
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
144 int ZEXPORT inflateReset(strm)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
145 z_streamp strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
146 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
147 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
148
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
149 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
150 state = (struct inflate_state FAR *)strm->state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
151 state->wsize = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
152 state->whave = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
153 state->wnext = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
154 return inflateResetKeep(strm);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
155 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
156
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
157 int ZEXPORT inflateReset2(strm, windowBits)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
158 z_streamp strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
159 int windowBits;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
160 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
161 int wrap;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
162 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
163
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
164 /* get the state */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
165 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
166 state = (struct inflate_state FAR *)strm->state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
167
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
168 /* extract wrap request from windowBits parameter */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
169 if (windowBits < 0) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
170 wrap = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
171 windowBits = -windowBits;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
172 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
173 else {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
174 wrap = (windowBits >> 4) + 5;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
175 #ifdef GUNZIP
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
176 if (windowBits < 48)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
177 windowBits &= 15;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
178 #endif
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
179 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
180
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
181 /* set number of window bits, free window if different */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
182 if (windowBits && (windowBits < 8 || windowBits > 15))
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
183 return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
184 if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
185 ZFREE(strm, state->window);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
186 state->window = Z_NULL;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
187 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
188
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
189 /* update state and reset the rest of it */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
190 state->wrap = wrap;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
191 state->wbits = (unsigned)windowBits;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
192 return inflateReset(strm);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
193 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
194
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
195 int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
196 z_streamp strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
197 int windowBits;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
198 const char *version;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
199 int stream_size;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
200 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
201 int ret;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
202 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
203
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
204 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
205 stream_size != (int)(sizeof(z_stream)))
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
206 return Z_VERSION_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
207 if (strm == Z_NULL) return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
208 strm->msg = Z_NULL; /* in case we return an error */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
209 if (strm->zalloc == (alloc_func)0) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
210 #ifdef Z_SOLO
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
211 return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
212 #else
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
213 strm->zalloc = zcalloc;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
214 strm->opaque = (voidpf)0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
215 #endif
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
216 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
217 if (strm->zfree == (free_func)0)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
218 #ifdef Z_SOLO
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
219 return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
220 #else
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
221 strm->zfree = zcfree;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
222 #endif
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
223 state = (struct inflate_state FAR *)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
224 ZALLOC(strm, 1, sizeof(struct inflate_state));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
225 if (state == Z_NULL) return Z_MEM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
226 Tracev((stderr, "inflate: allocated\n"));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
227 strm->state = (struct internal_state FAR *)state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
228 state->strm = strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
229 state->window = Z_NULL;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
230 state->mode = HEAD; /* to pass state test in inflateReset2() */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
231 ret = inflateReset2(strm, windowBits);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
232 if (ret != Z_OK) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
233 ZFREE(strm, state);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
234 strm->state = Z_NULL;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
235 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
236 return ret;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
237 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
238
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
239 int ZEXPORT inflateInit_(strm, version, stream_size)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
240 z_streamp strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
241 const char *version;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
242 int stream_size;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
243 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
244 return inflateInit2_(strm, DEF_WBITS, version, stream_size);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
245 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
246
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
247 int ZEXPORT inflatePrime(strm, bits, value)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
248 z_streamp strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
249 int bits;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
250 int value;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
251 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
252 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
253
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
254 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
255 state = (struct inflate_state FAR *)strm->state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
256 if (bits < 0) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
257 state->hold = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
258 state->bits = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
259 return Z_OK;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
260 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
261 if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
262 value &= (1L << bits) - 1;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
263 state->hold += (unsigned)value << state->bits;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
264 state->bits += (uInt)bits;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
265 return Z_OK;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
266 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
267
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
268 /*
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
269 Return state with length and distance decoding tables and index sizes set to
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
270 fixed code decoding. Normally this returns fixed tables from inffixed.h.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
271 If BUILDFIXED is defined, then instead this routine builds the tables the
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
272 first time it's called, and returns those tables the first time and
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
273 thereafter. This reduces the size of the code by about 2K bytes, in
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
274 exchange for a little execution time. However, BUILDFIXED should not be
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
275 used for threaded applications, since the rewriting of the tables and virgin
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
276 may not be thread-safe.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
277 */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
278 local void fixedtables(state)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
279 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
280 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
281 #ifdef BUILDFIXED
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
282 static int virgin = 1;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
283 static code *lenfix, *distfix;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
284 static code fixed[544];
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
285
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
286 /* build fixed huffman tables if first call (may not be thread safe) */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
287 if (virgin) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
288 unsigned sym, bits;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
289 static code *next;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
290
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
291 /* literal/length table */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
292 sym = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
293 while (sym < 144) state->lens[sym++] = 8;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
294 while (sym < 256) state->lens[sym++] = 9;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
295 while (sym < 280) state->lens[sym++] = 7;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
296 while (sym < 288) state->lens[sym++] = 8;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
297 next = fixed;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
298 lenfix = next;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
299 bits = 9;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
300 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
301
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
302 /* distance table */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
303 sym = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
304 while (sym < 32) state->lens[sym++] = 5;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
305 distfix = next;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
306 bits = 5;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
307 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
308
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
309 /* do this just once */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
310 virgin = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
311 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
312 #else /* !BUILDFIXED */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
313 # include "inffixed.h"
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
314 #endif /* BUILDFIXED */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
315 state->lencode = lenfix;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
316 state->lenbits = 9;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
317 state->distcode = distfix;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
318 state->distbits = 5;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
319 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
320
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
321 #ifdef MAKEFIXED
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
322 #include <stdio.h>
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
323
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
324 /*
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
325 Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
326 defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
327 those tables to stdout, which would be piped to inffixed.h. A small program
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
328 can simply call makefixed to do this:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
329
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
330 void makefixed(void);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
331
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
332 int main(void)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
333 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
334 makefixed();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
335 return 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
336 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
337
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
338 Then that can be linked with zlib built with MAKEFIXED defined and run:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
339
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
340 a.out > inffixed.h
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
341 */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
342 void makefixed()
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
343 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
344 unsigned low, size;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
345 struct inflate_state state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
346
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
347 fixedtables(&state);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
348 puts(" /* inffixed.h -- table for decoding fixed codes");
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
349 puts(" * Generated automatically by makefixed().");
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
350 puts(" */");
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
351 puts("");
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
352 puts(" /* WARNING: this file should *not* be used by applications.");
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
353 puts(" It is part of the implementation of this library and is");
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
354 puts(" subject to change. Applications should only use zlib.h.");
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
355 puts(" */");
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
356 puts("");
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
357 size = 1U << 9;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
358 printf(" static const code lenfix[%u] = {", size);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
359 low = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
360 for (;;) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
361 if ((low % 7) == 0) printf("\n ");
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
362 printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
363 state.lencode[low].bits, state.lencode[low].val);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
364 if (++low == size) break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
365 putchar(',');
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
366 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
367 puts("\n };");
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
368 size = 1U << 5;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
369 printf("\n static const code distfix[%u] = {", size);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
370 low = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
371 for (;;) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
372 if ((low % 6) == 0) printf("\n ");
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
373 printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
374 state.distcode[low].val);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
375 if (++low == size) break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
376 putchar(',');
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
377 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
378 puts("\n };");
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
379 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
380 #endif /* MAKEFIXED */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
381
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
382 /*
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
383 Update the window with the last wsize (normally 32K) bytes written before
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
384 returning. If window does not exist yet, create it. This is only called
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
385 when a window is already in use, or when output has been written during this
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
386 inflate call, but the end of the deflate stream has not been reached yet.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
387 It is also called to create a window for dictionary data when a dictionary
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
388 is loaded.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
389
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
390 Providing output buffers larger than 32K to inflate() should provide a speed
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
391 advantage, since only the last 32K of output is copied to the sliding window
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
392 upon return from inflate(), and since all distances after the first 32K of
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
393 output will fall in the output data, making match copies simpler and faster.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
394 The advantage may be dependent on the size of the processor's data caches.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
395 */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
396 local int updatewindow(strm, end, copy)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
397 z_streamp strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
398 const Bytef *end;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
399 unsigned copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
400 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
401 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
402 unsigned dist;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
403
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
404 state = (struct inflate_state FAR *)strm->state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
405
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
406 /* if it hasn't been done already, allocate space for the window */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
407 if (state->window == Z_NULL) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
408 state->window = (unsigned char FAR *)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
409 ZALLOC(strm, 1U << state->wbits,
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
410 sizeof(unsigned char));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
411 if (state->window == Z_NULL) return 1;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
412 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
413
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
414 /* if window not in use yet, initialize */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
415 if (state->wsize == 0) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
416 state->wsize = 1U << state->wbits;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
417 state->wnext = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
418 state->whave = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
419 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
420
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
421 /* copy state->wsize or less output bytes into the circular window */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
422 if (copy >= state->wsize) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
423 zmemcpy(state->window, end - state->wsize, state->wsize);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
424 state->wnext = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
425 state->whave = state->wsize;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
426 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
427 else {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
428 dist = state->wsize - state->wnext;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
429 if (dist > copy) dist = copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
430 zmemcpy(state->window + state->wnext, end - copy, dist);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
431 copy -= dist;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
432 if (copy) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
433 zmemcpy(state->window, end - copy, copy);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
434 state->wnext = copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
435 state->whave = state->wsize;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
436 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
437 else {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
438 state->wnext += dist;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
439 if (state->wnext == state->wsize) state->wnext = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
440 if (state->whave < state->wsize) state->whave += dist;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
441 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
442 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
443 return 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
444 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
445
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
446 /* Macros for inflate(): */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
447
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
448 /* check function to use adler32() for zlib or crc32() for gzip */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
449 #ifdef GUNZIP
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
450 # define UPDATE(check, buf, len) \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
451 (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
452 #else
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
453 # define UPDATE(check, buf, len) adler32(check, buf, len)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
454 #endif
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
455
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
456 /* check macros for header crc */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
457 #ifdef GUNZIP
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
458 # define CRC2(check, word) \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
459 do { \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
460 hbuf[0] = (unsigned char)(word); \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
461 hbuf[1] = (unsigned char)((word) >> 8); \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
462 check = crc32(check, hbuf, 2); \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
463 } while (0)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
464
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
465 # define CRC4(check, word) \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
466 do { \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
467 hbuf[0] = (unsigned char)(word); \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
468 hbuf[1] = (unsigned char)((word) >> 8); \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
469 hbuf[2] = (unsigned char)((word) >> 16); \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
470 hbuf[3] = (unsigned char)((word) >> 24); \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
471 check = crc32(check, hbuf, 4); \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
472 } while (0)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
473 #endif
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
474
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
475 /* Load registers with state in inflate() for speed */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
476 #define LOAD() \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
477 do { \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
478 put = strm->next_out; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
479 left = strm->avail_out; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
480 next = strm->next_in; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
481 have = strm->avail_in; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
482 hold = state->hold; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
483 bits = state->bits; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
484 } while (0)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
485
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
486 /* Restore state from registers in inflate() */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
487 #define RESTORE() \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
488 do { \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
489 strm->next_out = put; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
490 strm->avail_out = left; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
491 strm->next_in = next; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
492 strm->avail_in = have; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
493 state->hold = hold; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
494 state->bits = bits; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
495 } while (0)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
496
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
497 /* Clear the input bit accumulator */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
498 #define INITBITS() \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
499 do { \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
500 hold = 0; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
501 bits = 0; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
502 } while (0)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
503
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
504 /* Get a byte of input into the bit accumulator, or return from inflate()
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
505 if there is no input available. */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
506 #define PULLBYTE() \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
507 do { \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
508 if (have == 0) goto inf_leave; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
509 have--; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
510 hold += (unsigned long)(*next++) << bits; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
511 bits += 8; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
512 } while (0)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
513
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
514 /* Assure that there are at least n bits in the bit accumulator. If there is
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
515 not enough available input to do that, then return from inflate(). */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
516 #define NEEDBITS(n) \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
517 do { \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
518 while (bits < (unsigned)(n)) \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
519 PULLBYTE(); \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
520 } while (0)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
521
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
522 /* Return the low n bits of the bit accumulator (n < 16) */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
523 #define BITS(n) \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
524 ((unsigned)hold & ((1U << (n)) - 1))
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
525
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
526 /* Remove n bits from the bit accumulator */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
527 #define DROPBITS(n) \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
528 do { \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
529 hold >>= (n); \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
530 bits -= (unsigned)(n); \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
531 } while (0)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
532
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
533 /* Remove zero to seven bits as needed to go to a byte boundary */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
534 #define BYTEBITS() \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
535 do { \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
536 hold >>= bits & 7; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
537 bits -= bits & 7; \
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
538 } while (0)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
539
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
540 /*
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
541 inflate() uses a state machine to process as much input data and generate as
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
542 much output data as possible before returning. The state machine is
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
543 structured roughly as follows:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
544
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
545 for (;;) switch (state) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
546 ...
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
547 case STATEn:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
548 if (not enough input data or output space to make progress)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
549 return;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
550 ... make progress ...
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
551 state = STATEm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
552 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
553 ...
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
554 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
555
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
556 so when inflate() is called again, the same case is attempted again, and
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
557 if the appropriate resources are provided, the machine proceeds to the
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
558 next state. The NEEDBITS() macro is usually the way the state evaluates
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
559 whether it can proceed or should return. NEEDBITS() does the return if
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
560 the requested bits are not available. The typical use of the BITS macros
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
561 is:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
562
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
563 NEEDBITS(n);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
564 ... do something with BITS(n) ...
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
565 DROPBITS(n);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
566
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
567 where NEEDBITS(n) either returns from inflate() if there isn't enough
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
568 input left to load n bits into the accumulator, or it continues. BITS(n)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
569 gives the low n bits in the accumulator. When done, DROPBITS(n) drops
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
570 the low n bits off the accumulator. INITBITS() clears the accumulator
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
571 and sets the number of available bits to zero. BYTEBITS() discards just
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
572 enough bits to put the accumulator on a byte boundary. After BYTEBITS()
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
573 and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
574
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
575 NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
576 if there is no input available. The decoding of variable length codes uses
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
577 PULLBYTE() directly in order to pull just enough bytes to decode the next
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
578 code, and no more.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
579
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
580 Some states loop until they get enough input, making sure that enough
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
581 state information is maintained to continue the loop where it left off
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
582 if NEEDBITS() returns in the loop. For example, want, need, and keep
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
583 would all have to actually be part of the saved state in case NEEDBITS()
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
584 returns:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
585
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
586 case STATEw:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
587 while (want < need) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
588 NEEDBITS(n);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
589 keep[want++] = BITS(n);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
590 DROPBITS(n);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
591 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
592 state = STATEx;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
593 case STATEx:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
594
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
595 As shown above, if the next state is also the next case, then the break
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
596 is omitted.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
597
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
598 A state may also return if there is not enough output space available to
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
599 complete that state. Those states are copying stored data, writing a
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
600 literal byte, and copying a matching string.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
601
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
602 When returning, a "goto inf_leave" is used to update the total counters,
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
603 update the check value, and determine whether any progress has been made
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
604 during that inflate() call in order to return the proper return code.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
605 Progress is defined as a change in either strm->avail_in or strm->avail_out.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
606 When there is a window, goto inf_leave will update the window with the last
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
607 output written. If a goto inf_leave occurs in the middle of decompression
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
608 and there is no window currently, goto inf_leave will create one and copy
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
609 output to the window for the next call of inflate().
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
610
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
611 In this implementation, the flush parameter of inflate() only affects the
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
612 return code (per zlib.h). inflate() always writes as much as possible to
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
613 strm->next_out, given the space available and the provided input--the effect
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
614 documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
615 the allocation of and copying into a sliding window until necessary, which
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
616 provides the effect documented in zlib.h for Z_FINISH when the entire input
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
617 stream available. So the only thing the flush parameter actually does is:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
618 when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
619 will return Z_BUF_ERROR if it has not reached the end of the stream.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
620 */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
621
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
622 int ZEXPORT inflate(strm, flush)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
623 z_streamp strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
624 int flush;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
625 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
626 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
627 z_const unsigned char FAR *next; /* next input */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
628 unsigned char FAR *put; /* next output */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
629 unsigned have, left; /* available input and output */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
630 unsigned long hold; /* bit buffer */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
631 unsigned bits; /* bits in bit buffer */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
632 unsigned in, out; /* save starting available input and output */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
633 unsigned copy; /* number of stored or match bytes to copy */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
634 unsigned char FAR *from; /* where to copy match bytes from */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
635 code here; /* current decoding table entry */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
636 code last; /* parent table entry */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
637 unsigned len; /* length to copy for repeats, bits to drop */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
638 int ret; /* return code */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
639 #ifdef GUNZIP
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
640 unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
641 #endif
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
642 static const unsigned short order[19] = /* permutation of code lengths */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
643 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
644
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
645 if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
646 (strm->next_in == Z_NULL && strm->avail_in != 0))
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
647 return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
648
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
649 state = (struct inflate_state FAR *)strm->state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
650 if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
651 LOAD();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
652 in = have;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
653 out = left;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
654 ret = Z_OK;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
655 for (;;)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
656 switch (state->mode) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
657 case HEAD:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
658 if (state->wrap == 0) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
659 state->mode = TYPEDO;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
660 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
661 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
662 NEEDBITS(16);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
663 #ifdef GUNZIP
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
664 if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
665 if (state->wbits == 0)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
666 state->wbits = 15;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
667 state->check = crc32(0L, Z_NULL, 0);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
668 CRC2(state->check, hold);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
669 INITBITS();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
670 state->mode = FLAGS;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
671 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
672 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
673 state->flags = 0; /* expect zlib header */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
674 if (state->head != Z_NULL)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
675 state->head->done = -1;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
676 if (!(state->wrap & 1) || /* check if zlib header allowed */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
677 #else
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
678 if (
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
679 #endif
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
680 ((BITS(8) << 8) + (hold >> 8)) % 31) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
681 strm->msg = (char *)"incorrect header check";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
682 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
683 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
684 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
685 if (BITS(4) != Z_DEFLATED) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
686 strm->msg = (char *)"unknown compression method";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
687 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
688 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
689 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
690 DROPBITS(4);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
691 len = BITS(4) + 8;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
692 if (state->wbits == 0)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
693 state->wbits = len;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
694 if (len > 15 || len > state->wbits) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
695 strm->msg = (char *)"invalid window size";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
696 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
697 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
698 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
699 state->dmax = 1U << len;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
700 Tracev((stderr, "inflate: zlib header ok\n"));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
701 strm->adler = state->check = adler32(0L, Z_NULL, 0);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
702 state->mode = hold & 0x200 ? DICTID : TYPE;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
703 INITBITS();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
704 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
705 #ifdef GUNZIP
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
706 case FLAGS:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
707 NEEDBITS(16);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
708 state->flags = (int)(hold);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
709 if ((state->flags & 0xff) != Z_DEFLATED) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
710 strm->msg = (char *)"unknown compression method";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
711 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
712 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
713 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
714 if (state->flags & 0xe000) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
715 strm->msg = (char *)"unknown header flags set";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
716 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
717 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
718 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
719 if (state->head != Z_NULL)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
720 state->head->text = (int)((hold >> 8) & 1);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
721 if ((state->flags & 0x0200) && (state->wrap & 4))
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
722 CRC2(state->check, hold);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
723 INITBITS();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
724 state->mode = TIME;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
725 case TIME:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
726 NEEDBITS(32);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
727 if (state->head != Z_NULL)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
728 state->head->time = hold;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
729 if ((state->flags & 0x0200) && (state->wrap & 4))
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
730 CRC4(state->check, hold);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
731 INITBITS();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
732 state->mode = OS;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
733 case OS:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
734 NEEDBITS(16);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
735 if (state->head != Z_NULL) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
736 state->head->xflags = (int)(hold & 0xff);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
737 state->head->os = (int)(hold >> 8);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
738 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
739 if ((state->flags & 0x0200) && (state->wrap & 4))
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
740 CRC2(state->check, hold);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
741 INITBITS();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
742 state->mode = EXLEN;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
743 case EXLEN:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
744 if (state->flags & 0x0400) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
745 NEEDBITS(16);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
746 state->length = (unsigned)(hold);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
747 if (state->head != Z_NULL)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
748 state->head->extra_len = (unsigned)hold;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
749 if ((state->flags & 0x0200) && (state->wrap & 4))
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
750 CRC2(state->check, hold);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
751 INITBITS();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
752 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
753 else if (state->head != Z_NULL)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
754 state->head->extra = Z_NULL;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
755 state->mode = EXTRA;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
756 case EXTRA:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
757 if (state->flags & 0x0400) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
758 copy = state->length;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
759 if (copy > have) copy = have;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
760 if (copy) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
761 if (state->head != Z_NULL &&
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
762 state->head->extra != Z_NULL) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
763 len = state->head->extra_len - state->length;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
764 zmemcpy(state->head->extra + len, next,
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
765 len + copy > state->head->extra_max ?
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
766 state->head->extra_max - len : copy);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
767 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
768 if ((state->flags & 0x0200) && (state->wrap & 4))
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
769 state->check = crc32(state->check, next, copy);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
770 have -= copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
771 next += copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
772 state->length -= copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
773 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
774 if (state->length) goto inf_leave;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
775 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
776 state->length = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
777 state->mode = NAME;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
778 case NAME:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
779 if (state->flags & 0x0800) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
780 if (have == 0) goto inf_leave;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
781 copy = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
782 do {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
783 len = (unsigned)(next[copy++]);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
784 if (state->head != Z_NULL &&
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
785 state->head->name != Z_NULL &&
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
786 state->length < state->head->name_max)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
787 state->head->name[state->length++] = (Bytef)len;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
788 } while (len && copy < have);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
789 if ((state->flags & 0x0200) && (state->wrap & 4))
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
790 state->check = crc32(state->check, next, copy);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
791 have -= copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
792 next += copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
793 if (len) goto inf_leave;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
794 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
795 else if (state->head != Z_NULL)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
796 state->head->name = Z_NULL;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
797 state->length = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
798 state->mode = COMMENT;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
799 case COMMENT:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
800 if (state->flags & 0x1000) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
801 if (have == 0) goto inf_leave;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
802 copy = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
803 do {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
804 len = (unsigned)(next[copy++]);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
805 if (state->head != Z_NULL &&
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
806 state->head->comment != Z_NULL &&
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
807 state->length < state->head->comm_max)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
808 state->head->comment[state->length++] = (Bytef)len;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
809 } while (len && copy < have);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
810 if ((state->flags & 0x0200) && (state->wrap & 4))
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
811 state->check = crc32(state->check, next, copy);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
812 have -= copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
813 next += copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
814 if (len) goto inf_leave;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
815 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
816 else if (state->head != Z_NULL)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
817 state->head->comment = Z_NULL;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
818 state->mode = HCRC;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
819 case HCRC:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
820 if (state->flags & 0x0200) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
821 NEEDBITS(16);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
822 if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
823 strm->msg = (char *)"header crc mismatch";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
824 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
825 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
826 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
827 INITBITS();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
828 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
829 if (state->head != Z_NULL) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
830 state->head->hcrc = (int)((state->flags >> 9) & 1);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
831 state->head->done = 1;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
832 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
833 strm->adler = state->check = crc32(0L, Z_NULL, 0);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
834 state->mode = TYPE;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
835 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
836 #endif
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
837 case DICTID:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
838 NEEDBITS(32);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
839 strm->adler = state->check = ZSWAP32(hold);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
840 INITBITS();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
841 state->mode = DICT;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
842 case DICT:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
843 if (state->havedict == 0) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
844 RESTORE();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
845 return Z_NEED_DICT;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
846 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
847 strm->adler = state->check = adler32(0L, Z_NULL, 0);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
848 state->mode = TYPE;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
849 case TYPE:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
850 if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
851 case TYPEDO:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
852 if (state->last) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
853 BYTEBITS();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
854 state->mode = CHECK;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
855 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
856 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
857 NEEDBITS(3);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
858 state->last = BITS(1);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
859 DROPBITS(1);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
860 switch (BITS(2)) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
861 case 0: /* stored block */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
862 Tracev((stderr, "inflate: stored block%s\n",
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
863 state->last ? " (last)" : ""));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
864 state->mode = STORED;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
865 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
866 case 1: /* fixed block */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
867 fixedtables(state);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
868 Tracev((stderr, "inflate: fixed codes block%s\n",
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
869 state->last ? " (last)" : ""));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
870 state->mode = LEN_; /* decode codes */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
871 if (flush == Z_TREES) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
872 DROPBITS(2);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
873 goto inf_leave;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
874 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
875 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
876 case 2: /* dynamic block */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
877 Tracev((stderr, "inflate: dynamic codes block%s\n",
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
878 state->last ? " (last)" : ""));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
879 state->mode = TABLE;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
880 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
881 case 3:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
882 strm->msg = (char *)"invalid block type";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
883 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
884 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
885 DROPBITS(2);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
886 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
887 case STORED:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
888 BYTEBITS(); /* go to byte boundary */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
889 NEEDBITS(32);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
890 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
891 strm->msg = (char *)"invalid stored block lengths";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
892 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
893 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
894 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
895 state->length = (unsigned)hold & 0xffff;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
896 Tracev((stderr, "inflate: stored length %u\n",
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
897 state->length));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
898 INITBITS();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
899 state->mode = COPY_;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
900 if (flush == Z_TREES) goto inf_leave;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
901 case COPY_:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
902 state->mode = COPY;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
903 case COPY:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
904 copy = state->length;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
905 if (copy) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
906 if (copy > have) copy = have;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
907 if (copy > left) copy = left;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
908 if (copy == 0) goto inf_leave;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
909 zmemcpy(put, next, copy);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
910 have -= copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
911 next += copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
912 left -= copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
913 put += copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
914 state->length -= copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
915 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
916 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
917 Tracev((stderr, "inflate: stored end\n"));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
918 state->mode = TYPE;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
919 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
920 case TABLE:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
921 NEEDBITS(14);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
922 state->nlen = BITS(5) + 257;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
923 DROPBITS(5);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
924 state->ndist = BITS(5) + 1;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
925 DROPBITS(5);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
926 state->ncode = BITS(4) + 4;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
927 DROPBITS(4);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
928 #ifndef PKZIP_BUG_WORKAROUND
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
929 if (state->nlen > 286 || state->ndist > 30) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
930 strm->msg = (char *)"too many length or distance symbols";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
931 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
932 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
933 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
934 #endif
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
935 Tracev((stderr, "inflate: table sizes ok\n"));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
936 state->have = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
937 state->mode = LENLENS;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
938 case LENLENS:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
939 while (state->have < state->ncode) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
940 NEEDBITS(3);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
941 state->lens[order[state->have++]] = (unsigned short)BITS(3);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
942 DROPBITS(3);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
943 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
944 while (state->have < 19)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
945 state->lens[order[state->have++]] = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
946 state->next = state->codes;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
947 state->lencode = (const code FAR *)(state->next);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
948 state->lenbits = 7;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
949 ret = inflate_table(CODES, state->lens, 19, &(state->next),
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
950 &(state->lenbits), state->work);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
951 if (ret) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
952 strm->msg = (char *)"invalid code lengths set";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
953 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
954 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
955 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
956 Tracev((stderr, "inflate: code lengths ok\n"));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
957 state->have = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
958 state->mode = CODELENS;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
959 case CODELENS:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
960 while (state->have < state->nlen + state->ndist) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
961 for (;;) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
962 here = state->lencode[BITS(state->lenbits)];
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
963 if ((unsigned)(here.bits) <= bits) break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
964 PULLBYTE();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
965 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
966 if (here.val < 16) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
967 DROPBITS(here.bits);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
968 state->lens[state->have++] = here.val;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
969 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
970 else {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
971 if (here.val == 16) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
972 NEEDBITS(here.bits + 2);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
973 DROPBITS(here.bits);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
974 if (state->have == 0) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
975 strm->msg = (char *)"invalid bit length repeat";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
976 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
977 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
978 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
979 len = state->lens[state->have - 1];
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
980 copy = 3 + BITS(2);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
981 DROPBITS(2);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
982 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
983 else if (here.val == 17) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
984 NEEDBITS(here.bits + 3);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
985 DROPBITS(here.bits);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
986 len = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
987 copy = 3 + BITS(3);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
988 DROPBITS(3);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
989 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
990 else {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
991 NEEDBITS(here.bits + 7);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
992 DROPBITS(here.bits);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
993 len = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
994 copy = 11 + BITS(7);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
995 DROPBITS(7);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
996 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
997 if (state->have + copy > state->nlen + state->ndist) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
998 strm->msg = (char *)"invalid bit length repeat";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
999 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1000 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1001 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1002 while (copy--)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1003 state->lens[state->have++] = (unsigned short)len;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1004 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1005 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1006
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1007 /* handle error breaks in while */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1008 if (state->mode == BAD) break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1009
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1010 /* check for end-of-block code (better have one) */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1011 if (state->lens[256] == 0) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1012 strm->msg = (char *)"invalid code -- missing end-of-block";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1013 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1014 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1015 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1016
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1017 /* build code tables -- note: do not change the lenbits or distbits
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1018 values here (9 and 6) without reading the comments in inftrees.h
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1019 concerning the ENOUGH constants, which depend on those values */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1020 state->next = state->codes;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1021 state->lencode = (const code FAR *)(state->next);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1022 state->lenbits = 9;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1023 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1024 &(state->lenbits), state->work);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1025 if (ret) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1026 strm->msg = (char *)"invalid literal/lengths set";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1027 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1028 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1029 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1030 state->distcode = (const code FAR *)(state->next);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1031 state->distbits = 6;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1032 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1033 &(state->next), &(state->distbits), state->work);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1034 if (ret) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1035 strm->msg = (char *)"invalid distances set";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1036 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1037 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1038 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1039 Tracev((stderr, "inflate: codes ok\n"));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1040 state->mode = LEN_;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1041 if (flush == Z_TREES) goto inf_leave;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1042 case LEN_:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1043 state->mode = LEN;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1044 case LEN:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1045 if (have >= 6 && left >= 258) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1046 RESTORE();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1047 inflate_fast(strm, out);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1048 LOAD();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1049 if (state->mode == TYPE)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1050 state->back = -1;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1051 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1052 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1053 state->back = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1054 for (;;) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1055 here = state->lencode[BITS(state->lenbits)];
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1056 if ((unsigned)(here.bits) <= bits) break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1057 PULLBYTE();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1058 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1059 if (here.op && (here.op & 0xf0) == 0) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1060 last = here;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1061 for (;;) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1062 here = state->lencode[last.val +
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1063 (BITS(last.bits + last.op) >> last.bits)];
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1064 if ((unsigned)(last.bits + here.bits) <= bits) break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1065 PULLBYTE();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1066 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1067 DROPBITS(last.bits);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1068 state->back += last.bits;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1069 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1070 DROPBITS(here.bits);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1071 state->back += here.bits;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1072 state->length = (unsigned)here.val;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1073 if ((int)(here.op) == 0) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1074 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1075 "inflate: literal '%c'\n" :
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1076 "inflate: literal 0x%02x\n", here.val));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1077 state->mode = LIT;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1078 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1079 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1080 if (here.op & 32) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1081 Tracevv((stderr, "inflate: end of block\n"));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1082 state->back = -1;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1083 state->mode = TYPE;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1084 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1085 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1086 if (here.op & 64) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1087 strm->msg = (char *)"invalid literal/length code";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1088 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1089 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1090 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1091 state->extra = (unsigned)(here.op) & 15;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1092 state->mode = LENEXT;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1093 case LENEXT:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1094 if (state->extra) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1095 NEEDBITS(state->extra);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1096 state->length += BITS(state->extra);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1097 DROPBITS(state->extra);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1098 state->back += state->extra;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1099 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1100 Tracevv((stderr, "inflate: length %u\n", state->length));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1101 state->was = state->length;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1102 state->mode = DIST;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1103 case DIST:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1104 for (;;) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1105 here = state->distcode[BITS(state->distbits)];
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1106 if ((unsigned)(here.bits) <= bits) break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1107 PULLBYTE();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1108 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1109 if ((here.op & 0xf0) == 0) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1110 last = here;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1111 for (;;) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1112 here = state->distcode[last.val +
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1113 (BITS(last.bits + last.op) >> last.bits)];
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1114 if ((unsigned)(last.bits + here.bits) <= bits) break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1115 PULLBYTE();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1116 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1117 DROPBITS(last.bits);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1118 state->back += last.bits;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1119 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1120 DROPBITS(here.bits);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1121 state->back += here.bits;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1122 if (here.op & 64) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1123 strm->msg = (char *)"invalid distance code";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1124 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1125 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1126 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1127 state->offset = (unsigned)here.val;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1128 state->extra = (unsigned)(here.op) & 15;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1129 state->mode = DISTEXT;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1130 case DISTEXT:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1131 if (state->extra) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1132 NEEDBITS(state->extra);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1133 state->offset += BITS(state->extra);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1134 DROPBITS(state->extra);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1135 state->back += state->extra;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1136 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1137 #ifdef INFLATE_STRICT
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1138 if (state->offset > state->dmax) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1139 strm->msg = (char *)"invalid distance too far back";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1140 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1141 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1142 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1143 #endif
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1144 Tracevv((stderr, "inflate: distance %u\n", state->offset));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1145 state->mode = MATCH;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1146 case MATCH:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1147 if (left == 0) goto inf_leave;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1148 copy = out - left;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1149 if (state->offset > copy) { /* copy from window */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1150 copy = state->offset - copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1151 if (copy > state->whave) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1152 if (state->sane) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1153 strm->msg = (char *)"invalid distance too far back";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1154 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1155 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1156 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1157 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1158 Trace((stderr, "inflate.c too far\n"));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1159 copy -= state->whave;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1160 if (copy > state->length) copy = state->length;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1161 if (copy > left) copy = left;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1162 left -= copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1163 state->length -= copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1164 do {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1165 *put++ = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1166 } while (--copy);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1167 if (state->length == 0) state->mode = LEN;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1168 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1169 #endif
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1170 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1171 if (copy > state->wnext) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1172 copy -= state->wnext;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1173 from = state->window + (state->wsize - copy);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1174 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1175 else
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1176 from = state->window + (state->wnext - copy);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1177 if (copy > state->length) copy = state->length;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1178 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1179 else { /* copy from output */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1180 from = put - state->offset;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1181 copy = state->length;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1182 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1183 if (copy > left) copy = left;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1184 left -= copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1185 state->length -= copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1186 do {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1187 *put++ = *from++;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1188 } while (--copy);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1189 if (state->length == 0) state->mode = LEN;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1190 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1191 case LIT:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1192 if (left == 0) goto inf_leave;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1193 *put++ = (unsigned char)(state->length);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1194 left--;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1195 state->mode = LEN;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1196 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1197 case CHECK:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1198 if (state->wrap) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1199 NEEDBITS(32);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1200 out -= left;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1201 strm->total_out += out;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1202 state->total += out;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1203 if ((state->wrap & 4) && out)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1204 strm->adler = state->check =
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1205 UPDATE(state->check, put - out, out);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1206 out = left;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1207 if ((state->wrap & 4) && (
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1208 #ifdef GUNZIP
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1209 state->flags ? hold :
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1210 #endif
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1211 ZSWAP32(hold)) != state->check) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1212 strm->msg = (char *)"incorrect data check";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1213 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1214 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1215 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1216 INITBITS();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1217 Tracev((stderr, "inflate: check matches trailer\n"));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1218 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1219 #ifdef GUNZIP
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1220 state->mode = LENGTH;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1221 case LENGTH:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1222 if (state->wrap && state->flags) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1223 NEEDBITS(32);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1224 if (hold != (state->total & 0xffffffffUL)) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1225 strm->msg = (char *)"incorrect length check";
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1226 state->mode = BAD;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1227 break;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1228 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1229 INITBITS();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1230 Tracev((stderr, "inflate: length matches trailer\n"));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1231 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1232 #endif
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1233 state->mode = DONE;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1234 case DONE:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1235 ret = Z_STREAM_END;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1236 goto inf_leave;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1237 case BAD:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1238 ret = Z_DATA_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1239 goto inf_leave;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1240 case MEM:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1241 return Z_MEM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1242 case SYNC:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1243 default:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1244 return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1245 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1246
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1247 /*
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1248 Return from inflate(), updating the total counts and the check value.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1249 If there was no progress during the inflate() call, return a buffer
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1250 error. Call updatewindow() to create and/or update the window state.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1251 Note: a memory error from inflate() is non-recoverable.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1252 */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1253 inf_leave:
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1254 RESTORE();
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1255 if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1256 (state->mode < CHECK || flush != Z_FINISH)))
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1257 if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1258 state->mode = MEM;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1259 return Z_MEM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1260 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1261 in -= strm->avail_in;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1262 out -= strm->avail_out;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1263 strm->total_in += in;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1264 strm->total_out += out;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1265 state->total += out;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1266 if ((state->wrap & 4) && out)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1267 strm->adler = state->check =
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1268 UPDATE(state->check, strm->next_out - out, out);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1269 strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1270 (state->mode == TYPE ? 128 : 0) +
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1271 (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1272 if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1273 ret = Z_BUF_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1274 return ret;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1275 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1276
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1277 int ZEXPORT inflateEnd(strm)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1278 z_streamp strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1279 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1280 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1281 if (inflateStateCheck(strm))
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1282 return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1283 state = (struct inflate_state FAR *)strm->state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1284 if (state->window != Z_NULL) ZFREE(strm, state->window);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1285 ZFREE(strm, strm->state);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1286 strm->state = Z_NULL;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1287 Tracev((stderr, "inflate: end\n"));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1288 return Z_OK;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1289 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1290
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1291 int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1292 z_streamp strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1293 Bytef *dictionary;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1294 uInt *dictLength;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1295 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1296 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1297
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1298 /* check state */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1299 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1300 state = (struct inflate_state FAR *)strm->state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1301
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1302 /* copy dictionary */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1303 if (state->whave && dictionary != Z_NULL) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1304 zmemcpy(dictionary, state->window + state->wnext,
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1305 state->whave - state->wnext);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1306 zmemcpy(dictionary + state->whave - state->wnext,
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1307 state->window, state->wnext);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1308 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1309 if (dictLength != Z_NULL)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1310 *dictLength = state->whave;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1311 return Z_OK;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1312 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1313
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1314 int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1315 z_streamp strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1316 const Bytef *dictionary;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1317 uInt dictLength;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1318 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1319 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1320 unsigned long dictid;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1321 int ret;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1322
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1323 /* check state */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1324 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1325 state = (struct inflate_state FAR *)strm->state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1326 if (state->wrap != 0 && state->mode != DICT)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1327 return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1328
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1329 /* check for correct dictionary identifier */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1330 if (state->mode == DICT) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1331 dictid = adler32(0L, Z_NULL, 0);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1332 dictid = adler32(dictid, dictionary, dictLength);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1333 if (dictid != state->check)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1334 return Z_DATA_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1335 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1336
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1337 /* copy dictionary to window using updatewindow(), which will amend the
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1338 existing dictionary if appropriate */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1339 ret = updatewindow(strm, dictionary + dictLength, dictLength);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1340 if (ret) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1341 state->mode = MEM;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1342 return Z_MEM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1343 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1344 state->havedict = 1;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1345 Tracev((stderr, "inflate: dictionary set\n"));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1346 return Z_OK;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1347 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1348
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1349 int ZEXPORT inflateGetHeader(strm, head)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1350 z_streamp strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1351 gz_headerp head;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1352 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1353 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1354
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1355 /* check state */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1356 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1357 state = (struct inflate_state FAR *)strm->state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1358 if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1359
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1360 /* save header structure */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1361 state->head = head;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1362 head->done = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1363 return Z_OK;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1364 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1365
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1366 /*
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1367 Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1368 or when out of input. When called, *have is the number of pattern bytes
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1369 found in order so far, in 0..3. On return *have is updated to the new
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1370 state. If on return *have equals four, then the pattern was found and the
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1371 return value is how many bytes were read including the last byte of the
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1372 pattern. If *have is less than four, then the pattern has not been found
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1373 yet and the return value is len. In the latter case, syncsearch() can be
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1374 called again with more data and the *have state. *have is initialized to
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1375 zero for the first call.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1376 */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1377 local unsigned syncsearch(have, buf, len)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1378 unsigned FAR *have;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1379 const unsigned char FAR *buf;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1380 unsigned len;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1381 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1382 unsigned got;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1383 unsigned next;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1384
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1385 got = *have;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1386 next = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1387 while (next < len && got < 4) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1388 if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1389 got++;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1390 else if (buf[next])
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1391 got = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1392 else
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1393 got = 4 - got;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1394 next++;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1395 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1396 *have = got;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1397 return next;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1398 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1399
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1400 int ZEXPORT inflateSync(strm)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1401 z_streamp strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1402 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1403 unsigned len; /* number of bytes to look at or looked at */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1404 unsigned long in, out; /* temporary to save total_in and total_out */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1405 unsigned char buf[4]; /* to restore bit buffer to byte string */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1406 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1407
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1408 /* check parameters */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1409 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1410 state = (struct inflate_state FAR *)strm->state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1411 if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1412
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1413 /* if first time, start search in bit buffer */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1414 if (state->mode != SYNC) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1415 state->mode = SYNC;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1416 state->hold <<= state->bits & 7;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1417 state->bits -= state->bits & 7;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1418 len = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1419 while (state->bits >= 8) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1420 buf[len++] = (unsigned char)(state->hold);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1421 state->hold >>= 8;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1422 state->bits -= 8;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1423 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1424 state->have = 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1425 syncsearch(&(state->have), buf, len);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1426 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1427
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1428 /* search available input */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1429 len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1430 strm->avail_in -= len;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1431 strm->next_in += len;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1432 strm->total_in += len;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1433
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1434 /* return no joy or set up to restart inflate() on a new block */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1435 if (state->have != 4) return Z_DATA_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1436 in = strm->total_in; out = strm->total_out;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1437 inflateReset(strm);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1438 strm->total_in = in; strm->total_out = out;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1439 state->mode = TYPE;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1440 return Z_OK;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1441 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1442
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1443 /*
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1444 Returns true if inflate is currently at the end of a block generated by
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1445 Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1446 implementation to provide an additional safety check. PPP uses
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1447 Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1448 block. When decompressing, PPP checks that at the end of input packet,
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1449 inflate is waiting for these length bytes.
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1450 */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1451 int ZEXPORT inflateSyncPoint(strm)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1452 z_streamp strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1453 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1454 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1455
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1456 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1457 state = (struct inflate_state FAR *)strm->state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1458 return state->mode == STORED && state->bits == 0;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1459 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1460
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1461 int ZEXPORT inflateCopy(dest, source)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1462 z_streamp dest;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1463 z_streamp source;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1464 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1465 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1466 struct inflate_state FAR *copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1467 unsigned char FAR *window;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1468 unsigned wsize;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1469
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1470 /* check input */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1471 if (inflateStateCheck(source) || dest == Z_NULL)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1472 return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1473 state = (struct inflate_state FAR *)source->state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1474
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1475 /* allocate space */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1476 copy = (struct inflate_state FAR *)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1477 ZALLOC(source, 1, sizeof(struct inflate_state));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1478 if (copy == Z_NULL) return Z_MEM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1479 window = Z_NULL;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1480 if (state->window != Z_NULL) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1481 window = (unsigned char FAR *)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1482 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1483 if (window == Z_NULL) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1484 ZFREE(source, copy);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1485 return Z_MEM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1486 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1487 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1488
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1489 /* copy state */
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1490 zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1491 zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1492 copy->strm = dest;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1493 if (state->lencode >= state->codes &&
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1494 state->lencode <= state->codes + ENOUGH - 1) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1495 copy->lencode = copy->codes + (state->lencode - state->codes);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1496 copy->distcode = copy->codes + (state->distcode - state->codes);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1497 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1498 copy->next = copy->codes + (state->next - state->codes);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1499 if (window != Z_NULL) {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1500 wsize = 1U << state->wbits;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1501 zmemcpy(window, state->window, wsize);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1502 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1503 copy->window = window;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1504 dest->state = (struct internal_state FAR *)copy;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1505 return Z_OK;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1506 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1507
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1508 int ZEXPORT inflateUndermine(strm, subvert)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1509 z_streamp strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1510 int subvert;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1511 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1512 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1513
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1514 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1515 state = (struct inflate_state FAR *)strm->state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1516 #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1517 state->sane = !subvert;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1518 return Z_OK;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1519 #else
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1520 (void)subvert;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1521 state->sane = 1;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1522 return Z_DATA_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1523 #endif
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1524 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1525
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1526 int ZEXPORT inflateValidate(strm, check)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1527 z_streamp strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1528 int check;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1529 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1530 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1531
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1532 if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1533 state = (struct inflate_state FAR *)strm->state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1534 if (check)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1535 state->wrap |= 4;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1536 else
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1537 state->wrap &= ~4;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1538 return Z_OK;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1539 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1540
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1541 long ZEXPORT inflateMark(strm)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1542 z_streamp strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1543 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1544 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1545
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1546 if (inflateStateCheck(strm))
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1547 return -(1L << 16);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1548 state = (struct inflate_state FAR *)strm->state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1549 return (long)(((unsigned long)((long)state->back)) << 16) +
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1550 (state->mode == COPY ? state->length :
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1551 (state->mode == MATCH ? state->was - state->length : 0));
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1552 }
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1553
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1554 unsigned long ZEXPORT inflateCodesUsed(strm)
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1555 z_streamp strm;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1556 {
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1557 struct inflate_state FAR *state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1558 if (inflateStateCheck(strm)) return (unsigned long)-1;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1559 state = (struct inflate_state FAR *)strm->state;
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1560 return (unsigned long)(state->next - state->codes);
00d788dac91a Added support for reading gzipped ROMs
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1561 }