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