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