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