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