1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/compressionlibs/ziplib/src/zlib/inflate.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,1453 @@
1.4 +/* Portions Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 + * All rights reserved.
1.6 + */
1.7 +
1.8 +/* inflate.cpp -- zlib decompression
1.9 + * Copyright (C) 1995-2005 Mark Adler
1.10 + * For conditions of distribution and use, see copyright notice in zlib.h
1.11 + */
1.12 +
1.13 +/*
1.14 + * Change history:
1.15 + *
1.16 + * 1.2.beta0 24 Nov 2002
1.17 + * - First version -- complete rewrite of inflate to simplify code, avoid
1.18 + * creation of window when not needed, minimize use of window when it is
1.19 + * needed, make inffast.c even faster, implement gzip decoding, and to
1.20 + * improve code readability and style over the previous zlib inflate code
1.21 + *
1.22 + * 1.2.beta1 25 Nov 2002
1.23 + * - Use pointers for available input and output checking in inffast.c
1.24 + * - Remove input and output counters in inffast.c
1.25 + * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
1.26 + * - Remove unnecessary second byte pull from length extra in inffast.c
1.27 + * - Unroll direct copy to three copies per loop in inffast.c
1.28 + *
1.29 + * 1.2.beta2 4 Dec 2002
1.30 + * - Change external routine names to reduce potential conflicts
1.31 + * - Correct filename to inffixed.h for fixed tables in inflate.c
1.32 + * - Make hbuf[] unsigned char to match parameter type in inflate.c
1.33 + * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
1.34 + * to avoid negation problem on Alphas (64 bit) in inflate.c
1.35 + *
1.36 + * 1.2.beta3 22 Dec 2002
1.37 + * - Add comments on state->bits assertion in inffast.c
1.38 + * - Add comments on op field in inftrees.h
1.39 + * - Fix bug in reuse of allocated window after inflateReset()
1.40 + * - Remove bit fields--back to byte structure for speed
1.41 + * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
1.42 + * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
1.43 + * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
1.44 + * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
1.45 + * - Use local copies of stream next and avail values, as well as local bit
1.46 + * buffer and bit count in inflate()--for speed when inflate_fast() not used
1.47 + *
1.48 + * 1.2.beta4 1 Jan 2003
1.49 + * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
1.50 + * - Move a comment on output buffer sizes from inffast.c to inflate.c
1.51 + * - Add comments in inffast.c to introduce the inflate_fast() routine
1.52 + * - Rearrange window copies in inflate_fast() for speed and simplification
1.53 + * - Unroll last copy for window match in inflate_fast()
1.54 + * - Use local copies of window variables in inflate_fast() for speed
1.55 + * - Pull out common write == 0 case for speed in inflate_fast()
1.56 + * - Make op and len in inflate_fast() unsigned for consistency
1.57 + * - Add FAR to lcode and dcode declarations in inflate_fast()
1.58 + * - Simplified bad distance check in inflate_fast()
1.59 + * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
1.60 + * source file infback.c to provide a call-back interface to inflate for
1.61 + * programs like gzip and unzip -- uses window as output buffer to avoid
1.62 + * window copying
1.63 + *
1.64 + * 1.2.beta5 1 Jan 2003
1.65 + * - Improved inflateBack() interface to allow the caller to provide initial
1.66 + * input in strm.
1.67 + * - Fixed stored blocks bug in inflateBack()
1.68 + *
1.69 + * 1.2.beta6 4 Jan 2003
1.70 + * - Added comments in inffast.c on effectiveness of POSTINC
1.71 + * - Typecasting all around to reduce compiler warnings
1.72 + * - Changed loops from while (1) or do {} while (1) to for (;;), again to
1.73 + * make compilers happy
1.74 + * - Changed type of window in inflateBackInit() to unsigned char *
1.75 + *
1.76 + * 1.2.beta7 27 Jan 2003
1.77 + * - Changed many types to unsigned or unsigned short to avoid warnings
1.78 + * - Added inflateCopy() function
1.79 + *
1.80 + * 1.2.0 9 Mar 2003
1.81 + * - Changed inflateBack() interface to provide separate opaque descriptors
1.82 + * for the in() and out() functions
1.83 + * - Changed inflateBack() argument and in_func typedef to swap the length
1.84 + * and buffer address return values for the input function
1.85 + * - Check next_in and next_out for Z_NULL on entry to inflate()
1.86 + *
1.87 + * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
1.88 + */
1.89 +
1.90 +#include "zutil.h"
1.91 +#include "inftrees.h"
1.92 +#include "inflate.h"
1.93 +#include "inffast.h"
1.94 +
1.95 +#ifdef MAKEFIXED
1.96 +# ifndef BUILDFIXED
1.97 +# define BUILDFIXED
1.98 +# endif
1.99 +#endif
1.100 +
1.101 +/* function prototypes */
1.102 +local void fixedtables OF((struct inflate_state FAR *state));
1.103 +local int updatewindow OF((z_streamp strm, unsigned out));
1.104 +#ifdef BUILDFIXED
1.105 + void makefixed OF((void));
1.106 +#endif
1.107 +local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
1.108 + unsigned len));
1.109 +
1.110 +#ifdef __SYMBIAN32__
1.111 +EXPORT_C int inflateReset_r (z_streamp strm)
1.112 +#else
1.113 +int ZEXPORT inflateReset(strm)
1.114 +z_streamp strm;
1.115 +#endif //__SYMBIAN32__
1.116 +{
1.117 + struct inflate_state FAR *state;
1.118 +
1.119 + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1.120 + state = (struct inflate_state FAR *)strm->state;
1.121 + strm->total_in = strm->total_out = state->total = 0;
1.122 + strm->msg = Z_NULL;
1.123 + strm->adler = 1; /* to support ill-conceived Java test suite */
1.124 + state->mode = HEAD;
1.125 + state->last = 0;
1.126 + state->havedict = 0;
1.127 + state->dmax = 32768U;
1.128 + state->head = Z_NULL;
1.129 + state->wsize = 0;
1.130 + state->whave = 0;
1.131 + state->write = 0;
1.132 + state->hold = 0;
1.133 + state->bits = 0;
1.134 + state->lencode = state->distcode = state->next = state->codes;
1.135 + Tracev((stderr, "inflate: reset\n"));
1.136 + return Z_OK;
1.137 +}
1.138 +
1.139 +
1.140 +#ifdef __SYMBIAN32__
1.141 +EXPORT_C int inflatePrime_r(z_streamp strm, int bits, int value)
1.142 +#else
1.143 +int ZEXPORT inflatePrime(strm, bits, value)
1.144 +z_streamp strm;
1.145 +int bits;
1.146 +int value;
1.147 +#endif //__SYMBIAN32__
1.148 +{
1.149 + struct inflate_state FAR *state;
1.150 +
1.151 + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1.152 + state = (struct inflate_state FAR *)strm->state;
1.153 + if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
1.154 + value &= (1L << bits) - 1;
1.155 + state->hold += value << state->bits;
1.156 + state->bits += bits;
1.157 + return Z_OK;
1.158 +}
1.159 +
1.160 +#ifdef __SYMBIAN32__
1.161 +EXPORT_C int inflateInit2__r(z_streamp strm, int windowBits,const char * version,int stream_size)
1.162 +#else
1.163 +int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
1.164 +z_streamp strm;
1.165 +int windowBits;
1.166 +const char *version;
1.167 +int stream_size;
1.168 +#endif //__SYMBIAN32__
1.169 +{
1.170 + struct inflate_state FAR *state;
1.171 +
1.172 + if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
1.173 + stream_size != (int)(sizeof(z_stream)))
1.174 + return Z_VERSION_ERROR;
1.175 + if (strm == Z_NULL) return Z_STREAM_ERROR;
1.176 + strm->msg = Z_NULL; /* in case we return an error */
1.177 + if (strm->zalloc == (alloc_func)0) {
1.178 + strm->zalloc = zcalloc;
1.179 + strm->opaque = (voidpf)0;
1.180 + }
1.181 + if (strm->zfree == (free_func)0) strm->zfree = zcfree;
1.182 + state = (struct inflate_state FAR *)
1.183 + ZALLOC(strm, 1, sizeof(struct inflate_state));
1.184 + if (state == Z_NULL) return Z_MEM_ERROR;
1.185 + Tracev((stderr, "inflate: allocated\n"));
1.186 + strm->state = (struct internal_state FAR *)state;
1.187 + if (windowBits < 0) {
1.188 + state->wrap = 0;
1.189 + windowBits = -windowBits;
1.190 + }
1.191 + else {
1.192 + state->wrap = (windowBits >> 4) + 1;
1.193 +#ifdef GUNZIP
1.194 + if (windowBits < 48) windowBits &= 15;
1.195 +#endif
1.196 + }
1.197 + if (windowBits < 8 || windowBits > 15) {
1.198 + ZFREE(strm, state);
1.199 + strm->state = Z_NULL;
1.200 + return Z_STREAM_ERROR;
1.201 + }
1.202 + state->wbits = (unsigned)windowBits;
1.203 + state->window = Z_NULL;
1.204 + return inflateReset_r (strm);
1.205 +}
1.206 +
1.207 +#ifdef __SYMBIAN32__
1.208 +EXPORT_C int inflateInit__r (z_streamp strm,const char * version,int stream_size)
1.209 +#else
1.210 +int ZEXPORT inflateInit_(strm, version, stream_size)
1.211 +z_streamp strm;
1.212 +const char *version;
1.213 +int stream_size;
1.214 +#endif //__SYMBIAN32__
1.215 +{
1.216 + return inflateInit2__r(strm, DEF_WBITS, version, stream_size);
1.217 +}
1.218 +
1.219 +
1.220 +/*
1.221 + Return state with length and distance decoding tables and index sizes set to
1.222 + fixed code decoding. Normally this returns fixed tables from inffixed.h.
1.223 + If BUILDFIXED is defined, then instead this routine builds the tables the
1.224 + first time it's called, and returns those tables the first time and
1.225 + thereafter. This reduces the size of the code by about 2K bytes, in
1.226 + exchange for a little execution time. However, BUILDFIXED should not be
1.227 + used for threaded applications, since the rewriting of the tables and virgin
1.228 + may not be thread-safe.
1.229 + */
1.230 +
1.231 +#ifdef __SYMBIAN32__
1.232 +local void fixedtables(struct inflate_state FAR * state)
1.233 +#else
1.234 +local void fixedtables(state)
1.235 +struct inflate_state FAR *state;
1.236 +#endif //__SYMBIAN32__
1.237 +{
1.238 +#ifdef BUILDFIXED
1.239 + static int virgin = 1;
1.240 + static code *lenfix, *distfix;
1.241 + static code fixed[544];
1.242 +
1.243 + /* build fixed huffman tables if first call (may not be thread safe) */
1.244 + if (virgin) {
1.245 + unsigned sym, bits;
1.246 + static code *next;
1.247 +
1.248 + /* literal/length table */
1.249 + sym = 0;
1.250 + while (sym < 144) state->lens[sym++] = 8;
1.251 + while (sym < 256) state->lens[sym++] = 9;
1.252 + while (sym < 280) state->lens[sym++] = 7;
1.253 + while (sym < 288) state->lens[sym++] = 8;
1.254 + next = fixed;
1.255 + lenfix = next;
1.256 + bits = 9;
1.257 + inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
1.258 +
1.259 + /* distance table */
1.260 + sym = 0;
1.261 + while (sym < 32) state->lens[sym++] = 5;
1.262 + distfix = next;
1.263 + bits = 5;
1.264 + inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
1.265 +
1.266 + /* do this just once */
1.267 + virgin = 0;
1.268 + }
1.269 +#else /* !BUILDFIXED */
1.270 +# include "inffixed.h"
1.271 +#endif /* BUILDFIXED */
1.272 + state->lencode = lenfix;
1.273 + state->lenbits = 9;
1.274 + state->distcode = distfix;
1.275 + state->distbits = 5;
1.276 +}
1.277 +
1.278 +#ifndef SYMBIAN_EZLIB_DEVICE
1.279 +
1.280 +#ifdef MAKEFIXED
1.281 +#include <stdio.h>
1.282 +
1.283 +/*
1.284 + Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
1.285 + defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
1.286 + those tables to stdout, which would be piped to inffixed.h. A small program
1.287 + can simply call makefixed to do this:
1.288 +
1.289 + void makefixed(void);
1.290 +
1.291 + int main(void)
1.292 + {
1.293 + makefixed();
1.294 + return 0;
1.295 + }
1.296 +
1.297 + Then that can be linked with zlib built with MAKEFIXED defined and run:
1.298 +
1.299 + a.out > inffixed.h
1.300 + */
1.301 +void makefixed()
1.302 +{
1.303 + unsigned low, size;
1.304 + struct inflate_state state;
1.305 +
1.306 + fixedtables(&state);
1.307 + puts(" /* inffixed.h -- table for decoding fixed codes");
1.308 + puts(" * Generated automatically by makefixed().");
1.309 + puts(" */");
1.310 + puts("");
1.311 + puts(" /* WARNING: this file should *not* be used by applications.");
1.312 + puts(" It is part of the implementation of this library and is");
1.313 + puts(" subject to change. Applications should only use zlib.h.");
1.314 + puts(" */");
1.315 + puts("");
1.316 + size = 1U << 9;
1.317 + printf(" static const code lenfix[%u] = {", size);
1.318 + low = 0;
1.319 + for (;;) {
1.320 + if ((low % 7) == 0) printf("\n ");
1.321 + printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits,
1.322 + state.lencode[low].val);
1.323 + if (++low == size) break;
1.324 + putchar(',');
1.325 + }
1.326 + puts("\n };");
1.327 + size = 1U << 5;
1.328 + printf("\n static const code distfix[%u] = {", size);
1.329 + low = 0;
1.330 + for (;;) {
1.331 + if ((low % 6) == 0) printf("\n ");
1.332 + printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
1.333 + state.distcode[low].val);
1.334 + if (++low == size) break;
1.335 + putchar(',');
1.336 + }
1.337 + puts("\n };");
1.338 +}
1.339 +#endif /* MAKEFIXED */
1.340 +
1.341 +#endif //SYMBIAN_EZLIB_DEVICE
1.342 +
1.343 +/*
1.344 + Update the window with the last wsize (normally 32K) bytes written before
1.345 + returning. If window does not exist yet, create it. This is only called
1.346 + when a window is already in use, or when output has been written during this
1.347 + inflate call, but the end of the deflate stream has not been reached yet.
1.348 + It is also called to create a window for dictionary data when a dictionary
1.349 + is loaded.
1.350 +
1.351 + Providing output buffers larger than 32K to inflate() should provide a speed
1.352 + advantage, since only the last 32K of output is copied to the sliding window
1.353 + upon return from inflate(), and since all distances after the first 32K of
1.354 + output will fall in the output data, making match copies simpler and faster.
1.355 + The advantage may be dependent on the size of the processor's data caches.
1.356 + */
1.357 +
1.358 +#ifdef __SYMBIAN32__
1.359 +local int updatewindow(z_streamp strm,unsigned out)
1.360 +#else
1.361 +local int updatewindow(strm, out)
1.362 +z_streamp strm;
1.363 +unsigned out;
1.364 +#endif //__SYMBIAN32__
1.365 +{
1.366 + struct inflate_state FAR *state;
1.367 + unsigned copy, dist;
1.368 +
1.369 + state = (struct inflate_state FAR *)strm->state;
1.370 +
1.371 + /* if it hasn't been done already, allocate space for the window */
1.372 + if (state->window == Z_NULL) {
1.373 + state->window = (unsigned char FAR *)
1.374 + ZALLOC(strm, 1U << state->wbits,
1.375 + sizeof(unsigned char));
1.376 + if (state->window == Z_NULL) return 1;
1.377 + }
1.378 +
1.379 + /* if window not in use yet, initialize */
1.380 + if (state->wsize == 0) {
1.381 + state->wsize = 1U << state->wbits;
1.382 + state->write = 0;
1.383 + state->whave = 0;
1.384 + }
1.385 +
1.386 + /* copy state->wsize or less output bytes into the circular window */
1.387 + copy = out - strm->avail_out;
1.388 + if (copy >= state->wsize) {
1.389 + zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
1.390 + state->write = 0;
1.391 + state->whave = state->wsize;
1.392 + }
1.393 + else {
1.394 + dist = state->wsize - state->write;
1.395 + if (dist > copy) dist = copy;
1.396 + zmemcpy(state->window + state->write, strm->next_out - copy, dist);
1.397 + copy -= dist;
1.398 + if (copy) {
1.399 + zmemcpy(state->window, strm->next_out - copy, copy);
1.400 + state->write = copy;
1.401 + state->whave = state->wsize;
1.402 + }
1.403 + else {
1.404 + state->write += dist;
1.405 + if (state->write == state->wsize) state->write = 0;
1.406 + if (state->whave < state->wsize) state->whave += dist;
1.407 + }
1.408 + }
1.409 + return 0;
1.410 +}
1.411 +
1.412 +
1.413 +
1.414 +/* Macros for inflate(): */
1.415 +
1.416 +/* check function to use adler32() for zlib or crc32() for gzip */
1.417 +#ifdef GUNZIP
1.418 +# define UPDATE(check, buf, len) \
1.419 + (state->flags ? crc32_r(check, buf, len) : adler32_r(check, buf, len))
1.420 +#else
1.421 +# define UPDATE(check, buf, len) adler32_r(check, buf, len)
1.422 +#endif
1.423 +
1.424 +/* check macros for header crc */
1.425 +#ifdef GUNZIP
1.426 +# define CRC2(check, word) \
1.427 + do { \
1.428 + hbuf[0] = (unsigned char)(word); \
1.429 + hbuf[1] = (unsigned char)((word) >> 8); \
1.430 + check = crc32_r(check, hbuf, 2); \
1.431 + } while (0)
1.432 +
1.433 +# define CRC4(check, word) \
1.434 + do { \
1.435 + hbuf[0] = (unsigned char)(word); \
1.436 + hbuf[1] = (unsigned char)((word) >> 8); \
1.437 + hbuf[2] = (unsigned char)((word) >> 16); \
1.438 + hbuf[3] = (unsigned char)((word) >> 24); \
1.439 + check = crc32_r(check, hbuf, 4); \
1.440 + } while (0)
1.441 +#endif
1.442 +
1.443 +/* Load registers with state in inflate() for speed */
1.444 +#define LOAD() \
1.445 + do { \
1.446 + put = strm->next_out; \
1.447 + left = strm->avail_out; \
1.448 + next = strm->next_in; \
1.449 + have = strm->avail_in; \
1.450 + hold = state->hold; \
1.451 + bits = state->bits; \
1.452 + } while (0)
1.453 +
1.454 +/* Restore state from registers in inflate() */
1.455 +#define RESTORE() \
1.456 + do { \
1.457 + strm->next_out = put; \
1.458 + strm->avail_out = left; \
1.459 + strm->next_in = next; \
1.460 + strm->avail_in = have; \
1.461 + state->hold = hold; \
1.462 + state->bits = bits; \
1.463 + } while (0)
1.464 +
1.465 +/* Clear the input bit accumulator */
1.466 +#define INITBITS() \
1.467 + do { \
1.468 + hold = 0; \
1.469 + bits = 0; \
1.470 + } while (0)
1.471 +
1.472 +/* Get a byte of input into the bit accumulator, or return from inflate()
1.473 + if there is no input available. */
1.474 +#define PULLBYTE() \
1.475 + do { \
1.476 + if (have == 0) goto inf_leave; \
1.477 + have--; \
1.478 + hold += (unsigned long)(*next++) << bits; \
1.479 + bits += 8; \
1.480 + } while (0)
1.481 +
1.482 +/* Assure that there are at least n bits in the bit accumulator. If there is
1.483 + not enough available input to do that, then return from inflate(). */
1.484 +#define NEEDBITS(n) \
1.485 + do { \
1.486 + while (bits < (unsigned)(n)) \
1.487 + PULLBYTE(); \
1.488 + } while (0)
1.489 +
1.490 +/* Return the low n bits of the bit accumulator (n < 16) */
1.491 +#define BITS(n) \
1.492 + ((unsigned)hold & ((1U << (n)) - 1))
1.493 +
1.494 +/* Remove n bits from the bit accumulator */
1.495 +#define DROPBITS(n) \
1.496 + do { \
1.497 + hold >>= (n); \
1.498 + bits -= (unsigned)(n); \
1.499 + } while (0)
1.500 +
1.501 +/* Remove zero to seven bits as needed to go to a byte boundary */
1.502 +#define BYTEBITS() \
1.503 + do { \
1.504 + hold >>= bits & 7; \
1.505 + bits -= bits & 7; \
1.506 + } while (0)
1.507 +
1.508 +/* Reverse the bytes in a 32-bit value */
1.509 +#define REVERSE(q) \
1.510 + ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
1.511 + (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
1.512 +
1.513 +
1.514 +/*
1.515 + inflate() uses a state machine to process as much input data and generate as
1.516 + much output data as possible before returning. The state machine is
1.517 + structured roughly as follows:
1.518 +
1.519 + for (;;) switch (state) {
1.520 + ...
1.521 + case STATEn:
1.522 + if (not enough input data or output space to make progress)
1.523 + return;
1.524 + ... make progress ...
1.525 + state = STATEm;
1.526 + break;
1.527 + ...
1.528 + }
1.529 +
1.530 + so when inflate() is called again, the same case is attempted again, and
1.531 + if the appropriate resources are provided, the machine proceeds to the
1.532 + next state. The NEEDBITS() macro is usually the way the state evaluates
1.533 + whether it can proceed or should return. NEEDBITS() does the return if
1.534 + the requested bits are not available. The typical use of the BITS macros
1.535 + is:
1.536 +
1.537 + NEEDBITS(n);
1.538 + ... do something with BITS(n) ...
1.539 + DROPBITS(n);
1.540 +
1.541 + where NEEDBITS(n) either returns from inflate() if there isn't enough
1.542 + input left to load n bits into the accumulator, or it continues. BITS(n)
1.543 + gives the low n bits in the accumulator. When done, DROPBITS(n) drops
1.544 + the low n bits off the accumulator. INITBITS() clears the accumulator
1.545 + and sets the number of available bits to zero. BYTEBITS() discards just
1.546 + enough bits to put the accumulator on a byte boundary. After BYTEBITS()
1.547 + and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
1.548 +
1.549 + NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
1.550 + if there is no input available. The decoding of variable length codes uses
1.551 + PULLBYTE() directly in order to pull just enough bytes to decode the next
1.552 + code, and no more.
1.553 +
1.554 + Some states loop until they get enough input, making sure that enough
1.555 + state information is maintained to continue the loop where it left off
1.556 + if NEEDBITS() returns in the loop. For example, want, need, and keep
1.557 + would all have to actually be part of the saved state in case NEEDBITS()
1.558 + returns:
1.559 +
1.560 + case STATEw:
1.561 + while (want < need) {
1.562 + NEEDBITS(n);
1.563 + keep[want++] = BITS(n);
1.564 + DROPBITS(n);
1.565 + }
1.566 + state = STATEx;
1.567 + case STATEx:
1.568 +
1.569 + As shown above, if the next state is also the next case, then the break
1.570 + is omitted.
1.571 +
1.572 + A state may also return if there is not enough output space available to
1.573 + complete that state. Those states are copying stored data, writing a
1.574 + literal byte, and copying a matching string.
1.575 +
1.576 + When returning, a "goto inf_leave" is used to update the total counters,
1.577 + update the check value, and determine whether any progress has been made
1.578 + during that inflate() call in order to return the proper return code.
1.579 + Progress is defined as a change in either strm->avail_in or strm->avail_out.
1.580 + When there is a window, goto inf_leave will update the window with the last
1.581 + output written. If a goto inf_leave occurs in the middle of decompression
1.582 + and there is no window currently, goto inf_leave will create one and copy
1.583 + output to the window for the next call of inflate().
1.584 +
1.585 + In this implementation, the flush parameter of inflate() only affects the
1.586 + return code (per zlib.h). inflate() always writes as much as possible to
1.587 + strm->next_out, given the space available and the provided input--the effect
1.588 + documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
1.589 + the allocation of and copying into a sliding window until necessary, which
1.590 + provides the effect documented in zlib.h for Z_FINISH when the entire input
1.591 + stream available. So the only thing the flush parameter actually does is:
1.592 + when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
1.593 + will return Z_BUF_ERROR if it has not reached the end of the stream.
1.594 + */
1.595 +#ifdef __SYMBIAN32__
1.596 +EXPORT_C int inflate_r (z_streamp strm,int flush)
1.597 +#else
1.598 +int ZEXPORT inflate(strm, flush)
1.599 +z_streamp strm;
1.600 +int flush;
1.601 +#endif //__SYMBIAN32__
1.602 +{
1.603 + struct inflate_state FAR *state;
1.604 + unsigned char FAR *next; /* next input */
1.605 + unsigned char FAR *put; /* next output */
1.606 + unsigned have, left; /* available input and output */
1.607 + unsigned long hold; /* bit buffer */
1.608 + unsigned bits; /* bits in bit buffer */
1.609 + unsigned in, out; /* save starting available input and output */
1.610 + unsigned copy; /* number of stored or match bytes to copy */
1.611 + unsigned char FAR *from; /* where to copy match bytes from */
1.612 +
1.613 +/* Need to replace "this" variable with "current" as "this" is a reserved
1.614 + * keyword in C++ which is prefectly fine for a c code. As this file
1.615 + * has been changed to C++ "this" needs to be changed.
1.616 + */
1.617 +# define this current
1.618 + code this; /* current decoding table entry */
1.619 + code last; /* parent table entry */
1.620 + unsigned len; /* length to copy for repeats, bits to drop */
1.621 + int ret; /* return code */
1.622 +#ifdef GUNZIP
1.623 + unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
1.624 +#endif
1.625 + static const unsigned short order[19] = /* permutation of code lengths */
1.626 + {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
1.627 +
1.628 + if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
1.629 + (strm->next_in == Z_NULL && strm->avail_in != 0))
1.630 + return Z_STREAM_ERROR;
1.631 +
1.632 + state = (struct inflate_state FAR *)strm->state;
1.633 + if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
1.634 + LOAD();
1.635 + in = have;
1.636 + out = left;
1.637 + ret = Z_OK;
1.638 + for (;;)
1.639 + switch (state->mode) {
1.640 + case HEAD:
1.641 + if (state->wrap == 0) {
1.642 + state->mode = TYPEDO;
1.643 + break;
1.644 + }
1.645 + NEEDBITS(16);
1.646 +#ifdef GUNZIP
1.647 + if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
1.648 + state->check = crc32_r(0L, Z_NULL, 0);
1.649 + CRC2(state->check, hold);
1.650 + INITBITS();
1.651 + state->mode = FLAGS;
1.652 + break;
1.653 + }
1.654 + state->flags = 0; /* expect zlib header */
1.655 + if (state->head != Z_NULL)
1.656 + state->head->done = -1;
1.657 + if (!(state->wrap & 1) || /* check if zlib header allowed */
1.658 +#else
1.659 + if (
1.660 +#endif
1.661 + ((BITS(8) << 8) + (hold >> 8)) % 31) {
1.662 + strm->msg = (char *)"incorrect header check";
1.663 + state->mode = BAD;
1.664 + break;
1.665 + }
1.666 + if (BITS(4) != Z_DEFLATED) {
1.667 + strm->msg = (char *)"unknown compression method";
1.668 + state->mode = BAD;
1.669 + break;
1.670 + }
1.671 + DROPBITS(4);
1.672 + len = BITS(4) + 8;
1.673 + if (len > state->wbits) {
1.674 + strm->msg = (char *)"invalid window size";
1.675 + state->mode = BAD;
1.676 + break;
1.677 + }
1.678 + state->dmax = 1U << len;
1.679 + Tracev((stderr, "inflate: zlib header ok\n"));
1.680 + strm->adler = state->check = adler32_r(0L, Z_NULL, 0);
1.681 + state->mode = hold & 0x200 ? DICTID : TYPE;
1.682 + INITBITS();
1.683 + break;
1.684 +#ifdef GUNZIP
1.685 + case FLAGS:
1.686 + NEEDBITS(16);
1.687 + state->flags = (int)(hold);
1.688 + if ((state->flags & 0xff) != Z_DEFLATED) {
1.689 + strm->msg = (char *)"unknown compression method";
1.690 + state->mode = BAD;
1.691 + break;
1.692 + }
1.693 + if (state->flags & 0xe000) {
1.694 + strm->msg = (char *)"unknown header flags set";
1.695 + state->mode = BAD;
1.696 + break;
1.697 + }
1.698 + if (state->head != Z_NULL)
1.699 + state->head->text = (int)((hold >> 8) & 1);
1.700 + if (state->flags & 0x0200) CRC2(state->check, hold);
1.701 + INITBITS();
1.702 + state->mode = TIME;
1.703 + case TIME:
1.704 + NEEDBITS(32);
1.705 + if (state->head != Z_NULL)
1.706 + state->head->time = hold;
1.707 + if (state->flags & 0x0200) CRC4(state->check, hold);
1.708 + INITBITS();
1.709 + state->mode = OS;
1.710 + case OS:
1.711 + NEEDBITS(16);
1.712 + if (state->head != Z_NULL) {
1.713 + state->head->xflags = (int)(hold & 0xff);
1.714 + state->head->os = (int)(hold >> 8);
1.715 + }
1.716 + if (state->flags & 0x0200) CRC2(state->check, hold);
1.717 + INITBITS();
1.718 + state->mode = EXLEN;
1.719 + case EXLEN:
1.720 + if (state->flags & 0x0400) {
1.721 + NEEDBITS(16);
1.722 + state->length = (unsigned)(hold);
1.723 + if (state->head != Z_NULL)
1.724 + state->head->extra_len = (unsigned)hold;
1.725 + if (state->flags & 0x0200) CRC2(state->check, hold);
1.726 + INITBITS();
1.727 + }
1.728 + else if (state->head != Z_NULL)
1.729 + state->head->extra = Z_NULL;
1.730 + state->mode = EXTRA;
1.731 + case EXTRA:
1.732 + if (state->flags & 0x0400) {
1.733 + copy = state->length;
1.734 + if (copy > have) copy = have;
1.735 + if (copy) {
1.736 + if (state->head != Z_NULL &&
1.737 + state->head->extra != Z_NULL) {
1.738 + len = state->head->extra_len - state->length;
1.739 + // Added ignore here as next cannot be NULL
1.740 + // a jump to inf_leave would occur first
1.741 + // coverity [var_deref_model]
1.742 + zmemcpy(state->head->extra + len, next,
1.743 + len + copy > state->head->extra_max ?
1.744 + state->head->extra_max - len : copy);
1.745 + }
1.746 + if (state->flags & 0x0200)
1.747 + state->check = crc32_r(state->check, next, copy);
1.748 + have -= copy;
1.749 + next += copy;
1.750 + state->length -= copy;
1.751 + }
1.752 + if (state->length) goto inf_leave;
1.753 + }
1.754 + state->length = 0;
1.755 + state->mode = NAME;
1.756 + case NAME:
1.757 + if (state->flags & 0x0800) {
1.758 + if (have == 0) goto inf_leave;
1.759 + copy = 0;
1.760 + do {
1.761 + len = (unsigned)(next[copy++]);
1.762 + if (state->head != Z_NULL &&
1.763 + state->head->name != Z_NULL &&
1.764 + state->length < state->head->name_max)
1.765 + state->head->name[state->length++] = len;
1.766 + } while (len && copy < have);
1.767 + if (state->flags & 0x0200)
1.768 + state->check = crc32_r(state->check, next, copy);
1.769 + have -= copy;
1.770 + next += copy;
1.771 + if (len) goto inf_leave;
1.772 + }
1.773 + else if (state->head != Z_NULL)
1.774 + state->head->name = Z_NULL;
1.775 + state->length = 0;
1.776 + state->mode = COMMENT;
1.777 + case COMMENT:
1.778 + if (state->flags & 0x1000) {
1.779 + if (have == 0) goto inf_leave;
1.780 + copy = 0;
1.781 + do {
1.782 + len = (unsigned)(next[copy++]);
1.783 + if (state->head != Z_NULL &&
1.784 + state->head->comment != Z_NULL &&
1.785 + state->length < state->head->comm_max)
1.786 + state->head->comment[state->length++] = len;
1.787 + } while (len && copy < have);
1.788 + if (state->flags & 0x0200)
1.789 + state->check = crc32_r(state->check, next, copy);
1.790 + have -= copy;
1.791 + next += copy;
1.792 + if (len) goto inf_leave;
1.793 + }
1.794 + else if (state->head != Z_NULL)
1.795 + state->head->comment = Z_NULL;
1.796 + state->mode = HCRC;
1.797 + case HCRC:
1.798 + if (state->flags & 0x0200) {
1.799 + NEEDBITS(16);
1.800 + if (hold != (state->check & 0xffff)) {
1.801 + strm->msg = (char *)"header crc mismatch";
1.802 + state->mode = BAD;
1.803 + break;
1.804 + }
1.805 + INITBITS();
1.806 + }
1.807 + if (state->head != Z_NULL) {
1.808 + state->head->hcrc = (int)((state->flags >> 9) & 1);
1.809 + state->head->done = 1;
1.810 + }
1.811 + strm->adler = state->check = crc32_r(0L, Z_NULL, 0);
1.812 + state->mode = TYPE;
1.813 + break;
1.814 +#endif
1.815 + case DICTID:
1.816 + NEEDBITS(32);
1.817 + strm->adler = state->check = REVERSE(hold);
1.818 + INITBITS();
1.819 + state->mode = DICT;
1.820 + case DICT:
1.821 + if (state->havedict == 0) {
1.822 + RESTORE();
1.823 + return Z_NEED_DICT;
1.824 + }
1.825 + strm->adler = state->check = adler32_r(0L, Z_NULL, 0);
1.826 + state->mode = TYPE;
1.827 + case TYPE:
1.828 + if (flush == Z_BLOCK) goto inf_leave;
1.829 + case TYPEDO:
1.830 + if (state->last) {
1.831 + BYTEBITS();
1.832 + state->mode = CHECK;
1.833 + break;
1.834 + }
1.835 + NEEDBITS(3);
1.836 + state->last = BITS(1);
1.837 + DROPBITS(1);
1.838 + switch (BITS(2)) {
1.839 + case 0: /* stored block */
1.840 + Tracev((stderr, "inflate: stored block%s\n",
1.841 + state->last ? " (last)" : ""));
1.842 + state->mode = STORED;
1.843 + break;
1.844 + case 1: /* fixed block */
1.845 + fixedtables(state);
1.846 + Tracev((stderr, "inflate: fixed codes block%s\n",
1.847 + state->last ? " (last)" : ""));
1.848 + state->mode = LEN; /* decode codes */
1.849 + break;
1.850 + case 2: /* dynamic block */
1.851 + Tracev((stderr, "inflate: dynamic codes block%s\n",
1.852 + state->last ? " (last)" : ""));
1.853 + state->mode = TABLE;
1.854 + break;
1.855 + case 3:
1.856 + strm->msg = (char *)"invalid block type";
1.857 + state->mode = BAD;
1.858 + }
1.859 + DROPBITS(2);
1.860 + break;
1.861 + case STORED:
1.862 + BYTEBITS(); /* go to byte boundary */
1.863 + NEEDBITS(32);
1.864 + if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
1.865 + strm->msg = (char *)"invalid stored block lengths";
1.866 + state->mode = BAD;
1.867 + break;
1.868 + }
1.869 + state->length = (unsigned)hold & 0xffff;
1.870 + Tracev((stderr, "inflate: stored length %u\n",
1.871 + state->length));
1.872 + INITBITS();
1.873 + state->mode = COPY;
1.874 + case COPY:
1.875 + copy = state->length;
1.876 + if (copy) {
1.877 + if (copy > have) copy = have;
1.878 + if (copy > left) copy = left;
1.879 + if (copy == 0) goto inf_leave;
1.880 + zmemcpy(put, next, copy);
1.881 + have -= copy;
1.882 + next += copy;
1.883 + left -= copy;
1.884 + put += copy;
1.885 + state->length -= copy;
1.886 + break;
1.887 + }
1.888 + Tracev((stderr, "inflate: stored end\n"));
1.889 + state->mode = TYPE;
1.890 + break;
1.891 + case TABLE:
1.892 + NEEDBITS(14);
1.893 + state->nlen = BITS(5) + 257;
1.894 + DROPBITS(5);
1.895 + state->ndist = BITS(5) + 1;
1.896 + DROPBITS(5);
1.897 + state->ncode = BITS(4) + 4;
1.898 + DROPBITS(4);
1.899 +#ifndef PKZIP_BUG_WORKAROUND
1.900 + if (state->nlen > 286 || state->ndist > 30) {
1.901 + strm->msg = (char *)"too many length or distance symbols";
1.902 + state->mode = BAD;
1.903 + break;
1.904 + }
1.905 +#endif
1.906 + Tracev((stderr, "inflate: table sizes ok\n"));
1.907 + state->have = 0;
1.908 + state->mode = LENLENS;
1.909 + case LENLENS:
1.910 + while (state->have < state->ncode) {
1.911 + NEEDBITS(3);
1.912 + state->lens[order[state->have++]] = (unsigned short)BITS(3);
1.913 + DROPBITS(3);
1.914 + }
1.915 + while (state->have < 19)
1.916 + state->lens[order[state->have++]] = 0;
1.917 + state->next = state->codes;
1.918 + state->lencode = (code const FAR *)(state->next);
1.919 + state->lenbits = 7;
1.920 + ret = inflate_table(CODES, state->lens, 19, &(state->next),
1.921 + &(state->lenbits), state->work);
1.922 + if (ret) {
1.923 + strm->msg = (char *)"invalid code lengths set";
1.924 + state->mode = BAD;
1.925 + break;
1.926 + }
1.927 + Tracev((stderr, "inflate: code lengths ok\n"));
1.928 + state->have = 0;
1.929 + state->mode = CODELENS;
1.930 + case CODELENS:
1.931 + while (state->have < state->nlen + state->ndist) {
1.932 + for (;;) {
1.933 + this = state->lencode[BITS(state->lenbits)];
1.934 + if ((unsigned)(this.bits) <= bits) break;
1.935 + PULLBYTE();
1.936 + }
1.937 + if (this.val < 16) {
1.938 + NEEDBITS(this.bits);
1.939 + DROPBITS(this.bits);
1.940 + state->lens[state->have++] = this.val;
1.941 + }
1.942 + else {
1.943 + if (this.val == 16) {
1.944 + NEEDBITS(this.bits + 2);
1.945 + DROPBITS(this.bits);
1.946 + if (state->have == 0) {
1.947 + strm->msg = (char *)"invalid bit length repeat";
1.948 + state->mode = BAD;
1.949 + break;
1.950 + }
1.951 + len = state->lens[state->have - 1];
1.952 + copy = 3 + BITS(2);
1.953 + DROPBITS(2);
1.954 + }
1.955 + else if (this.val == 17) {
1.956 + NEEDBITS(this.bits + 3);
1.957 + DROPBITS(this.bits);
1.958 + len = 0;
1.959 + copy = 3 + BITS(3);
1.960 + DROPBITS(3);
1.961 + }
1.962 + else {
1.963 + NEEDBITS(this.bits + 7);
1.964 + DROPBITS(this.bits);
1.965 + len = 0;
1.966 + copy = 11 + BITS(7);
1.967 + DROPBITS(7);
1.968 + }
1.969 + if (state->have + copy > state->nlen + state->ndist) {
1.970 + strm->msg = (char *)"invalid bit length repeat";
1.971 + state->mode = BAD;
1.972 + break;
1.973 + }
1.974 + while (copy--)
1.975 + state->lens[state->have++] = (unsigned short)len;
1.976 + }
1.977 + }
1.978 +
1.979 + /* handle error breaks in while */
1.980 + if (state->mode == BAD) break;
1.981 +
1.982 + /* build code tables */
1.983 + state->next = state->codes;
1.984 + state->lencode = (code const FAR *)(state->next);
1.985 + state->lenbits = 9;
1.986 + ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
1.987 + &(state->lenbits), state->work);
1.988 + if (ret) {
1.989 + strm->msg = (char *)"invalid literal/lengths set";
1.990 + state->mode = BAD;
1.991 + break;
1.992 + }
1.993 + state->distcode = (code const FAR *)(state->next);
1.994 + state->distbits = 6;
1.995 + ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
1.996 + &(state->next), &(state->distbits), state->work);
1.997 + if (ret) {
1.998 + strm->msg = (char *)"invalid distances set";
1.999 + state->mode = BAD;
1.1000 + break;
1.1001 + }
1.1002 + Tracev((stderr, "inflate: codes ok\n"));
1.1003 + state->mode = LEN;
1.1004 + case LEN:
1.1005 + if (have >= 6 && left >= 258) {
1.1006 + RESTORE();
1.1007 + inflate_fast(strm, out);
1.1008 + LOAD();
1.1009 + break;
1.1010 + }
1.1011 + for (;;) {
1.1012 + this = state->lencode[BITS(state->lenbits)];
1.1013 + if ((unsigned)(this.bits) <= bits) break;
1.1014 + PULLBYTE();
1.1015 + }
1.1016 + if (this.op && (this.op & 0xf0) == 0) {
1.1017 + last = this;
1.1018 + for (;;) {
1.1019 + this = state->lencode[last.val +
1.1020 + (BITS(last.bits + last.op) >> last.bits)];
1.1021 + if ((unsigned)(last.bits + this.bits) <= bits) break;
1.1022 + PULLBYTE();
1.1023 + }
1.1024 + DROPBITS(last.bits);
1.1025 + }
1.1026 + DROPBITS(this.bits);
1.1027 + state->length = (unsigned)this.val;
1.1028 + if ((int)(this.op) == 0) {
1.1029 + Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
1.1030 + "inflate: literal '%c'\n" :
1.1031 + "inflate: literal 0x%02x\n", this.val));
1.1032 + state->mode = LIT;
1.1033 + break;
1.1034 + }
1.1035 + if (this.op & 32) {
1.1036 + Tracevv((stderr, "inflate: end of block\n"));
1.1037 + state->mode = TYPE;
1.1038 + break;
1.1039 + }
1.1040 + if (this.op & 64) {
1.1041 + strm->msg = (char *)"invalid literal/length code";
1.1042 + state->mode = BAD;
1.1043 + break;
1.1044 + }
1.1045 + state->extra = (unsigned)(this.op) & 15;
1.1046 + state->mode = LENEXT;
1.1047 + case LENEXT:
1.1048 + if (state->extra) {
1.1049 + NEEDBITS(state->extra);
1.1050 + state->length += BITS(state->extra);
1.1051 + DROPBITS(state->extra);
1.1052 + }
1.1053 + Tracevv((stderr, "inflate: length %u\n", state->length));
1.1054 + state->mode = DIST;
1.1055 + case DIST:
1.1056 + for (;;) {
1.1057 + this = state->distcode[BITS(state->distbits)];
1.1058 + if ((unsigned)(this.bits) <= bits) break;
1.1059 + PULLBYTE();
1.1060 + }
1.1061 + if ((this.op & 0xf0) == 0) {
1.1062 + last = this;
1.1063 + for (;;) {
1.1064 + this = state->distcode[last.val +
1.1065 + (BITS(last.bits + last.op) >> last.bits)];
1.1066 + if ((unsigned)(last.bits + this.bits) <= bits) break;
1.1067 + PULLBYTE();
1.1068 + }
1.1069 + DROPBITS(last.bits);
1.1070 + }
1.1071 + DROPBITS(this.bits);
1.1072 + if (this.op & 64) {
1.1073 + strm->msg = (char *)"invalid distance code";
1.1074 + state->mode = BAD;
1.1075 + break;
1.1076 + }
1.1077 + state->offset = (unsigned)this.val;
1.1078 + state->extra = (unsigned)(this.op) & 15;
1.1079 + state->mode = DISTEXT;
1.1080 + case DISTEXT:
1.1081 + if (state->extra) {
1.1082 + NEEDBITS(state->extra);
1.1083 + state->offset += BITS(state->extra);
1.1084 + DROPBITS(state->extra);
1.1085 + }
1.1086 +#ifdef INFLATE_STRICT
1.1087 + if (state->offset > state->dmax) {
1.1088 + strm->msg = (char *)"invalid distance too far back";
1.1089 + state->mode = BAD;
1.1090 + break;
1.1091 + }
1.1092 +#endif
1.1093 + if (state->offset > state->whave + out - left) {
1.1094 + strm->msg = (char *)"invalid distance too far back";
1.1095 + state->mode = BAD;
1.1096 + break;
1.1097 + }
1.1098 + Tracevv((stderr, "inflate: distance %u\n", state->offset));
1.1099 + state->mode = MATCH;
1.1100 + case MATCH:
1.1101 + if (left == 0) goto inf_leave;
1.1102 + copy = out - left;
1.1103 + if (state->offset > copy) { /* copy from window */
1.1104 + copy = state->offset - copy;
1.1105 + if (copy > state->write) {
1.1106 + copy -= state->write;
1.1107 + from = state->window + (state->wsize - copy);
1.1108 + }
1.1109 + else
1.1110 + from = state->window + (state->write - copy);
1.1111 + if (copy > state->length) copy = state->length;
1.1112 + }
1.1113 + else { /* copy from output */
1.1114 + from = put - state->offset;
1.1115 + copy = state->length;
1.1116 + }
1.1117 + if (copy > left) copy = left;
1.1118 + left -= copy;
1.1119 + state->length -= copy;
1.1120 + do {
1.1121 + *put++ = *from++;
1.1122 + } while (--copy);
1.1123 + if (state->length == 0) state->mode = LEN;
1.1124 + break;
1.1125 + case LIT:
1.1126 + if (left == 0) goto inf_leave;
1.1127 + *put++ = (unsigned char)(state->length);
1.1128 + left--;
1.1129 + state->mode = LEN;
1.1130 + break;
1.1131 + case CHECK:
1.1132 + if (state->wrap) {
1.1133 + NEEDBITS(32);
1.1134 + out -= left;
1.1135 + strm->total_out += out;
1.1136 + state->total += out;
1.1137 + if (out)
1.1138 + strm->adler = state->check =
1.1139 + UPDATE(state->check, put - out, out);
1.1140 + out = left;
1.1141 + if ((
1.1142 +#ifdef GUNZIP
1.1143 + state->flags ? hold :
1.1144 +#endif
1.1145 + REVERSE(hold)) != state->check) {
1.1146 + strm->msg = (char *)"incorrect data check";
1.1147 + state->mode = BAD;
1.1148 + break;
1.1149 + }
1.1150 + INITBITS();
1.1151 + Tracev((stderr, "inflate: check matches trailer\n"));
1.1152 + }
1.1153 +#ifdef GUNZIP
1.1154 + state->mode = LENGTH;
1.1155 + case LENGTH:
1.1156 + if (state->wrap && state->flags) {
1.1157 + NEEDBITS(32);
1.1158 + if (hold != (state->total & 0xffffffffUL)) {
1.1159 + strm->msg = (char *)"incorrect length check";
1.1160 + state->mode = BAD;
1.1161 + break;
1.1162 + }
1.1163 + INITBITS();
1.1164 + Tracev((stderr, "inflate: length matches trailer\n"));
1.1165 + }
1.1166 +#endif
1.1167 + state->mode = DONE;
1.1168 + case DONE:
1.1169 + ret = Z_STREAM_END;
1.1170 + goto inf_leave;
1.1171 + case BAD:
1.1172 + ret = Z_DATA_ERROR;
1.1173 + goto inf_leave;
1.1174 + case MEM:
1.1175 + return Z_MEM_ERROR;
1.1176 + case SYNC:
1.1177 + default:
1.1178 + return Z_STREAM_ERROR;
1.1179 + }
1.1180 +
1.1181 + /*
1.1182 + Return from inflate(), updating the total counts and the check value.
1.1183 + If there was no progress during the inflate() call, return a buffer
1.1184 + error. Call updatewindow() to create and/or update the window state.
1.1185 + Note: a memory error from inflate() is non-recoverable.
1.1186 + */
1.1187 + inf_leave:
1.1188 + RESTORE();
1.1189 + if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
1.1190 + if (updatewindow(strm, out)) {
1.1191 + state->mode = MEM;
1.1192 + return Z_MEM_ERROR;
1.1193 + }
1.1194 + in -= strm->avail_in;
1.1195 + out -= strm->avail_out;
1.1196 + strm->total_in += in;
1.1197 + strm->total_out += out;
1.1198 + state->total += out;
1.1199 + if (state->wrap && out)
1.1200 + strm->adler = state->check =
1.1201 + UPDATE(state->check, strm->next_out - out, out);
1.1202 + strm->data_type = state->bits + (state->last ? 64 : 0) +
1.1203 + (state->mode == TYPE ? 128 : 0);
1.1204 + if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1.1205 + ret = Z_BUF_ERROR;
1.1206 + return ret;
1.1207 +}
1.1208 +#ifdef __SYMBIAN32__
1.1209 +EXPORT_C int inflateEnd_r (z_streamp strm)
1.1210 +#else
1.1211 +int ZEXPORT inflateEnd(strm)
1.1212 +z_streamp strm;
1.1213 +#endif //__SYMBIAN32__
1.1214 +{
1.1215 + struct inflate_state FAR *state;
1.1216 + if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
1.1217 + return Z_STREAM_ERROR;
1.1218 + state = (struct inflate_state FAR *)strm->state;
1.1219 + if (state->window != Z_NULL) ZFREE(strm, state->window);
1.1220 + ZFREE(strm, strm->state);
1.1221 + strm->state = Z_NULL;
1.1222 + Tracev((stderr, "inflate: end\n"));
1.1223 + return Z_OK;
1.1224 +}
1.1225 +#ifdef __SYMBIAN32__
1.1226 +EXPORT_C int inflateSetDictionary_r (z_streamp strm,const Bytef * dictionary,uInt dictLength)
1.1227 +#else
1.1228 +int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
1.1229 +z_streamp strm;
1.1230 +const Bytef *dictionary;
1.1231 +uInt dictLength;
1.1232 +#endif //__SYMBIAN32__
1.1233 +{
1.1234 + struct inflate_state FAR *state;
1.1235 + unsigned long id;
1.1236 +
1.1237 + /* check state */
1.1238 + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1.1239 + state = (struct inflate_state FAR *)strm->state;
1.1240 + if (state->wrap != 0 && state->mode != DICT)
1.1241 + return Z_STREAM_ERROR;
1.1242 +
1.1243 + /* check for correct dictionary id */
1.1244 + if (state->mode == DICT) {
1.1245 + id = adler32_r(0L, Z_NULL, 0);
1.1246 + id = adler32_r(id, dictionary, dictLength);
1.1247 + if (id != state->check)
1.1248 + return Z_DATA_ERROR;
1.1249 + }
1.1250 +
1.1251 + /* copy dictionary to window */
1.1252 + if (updatewindow(strm, strm->avail_out)) {
1.1253 + state->mode = MEM;
1.1254 + return Z_MEM_ERROR;
1.1255 + }
1.1256 + if (dictLength > state->wsize) {
1.1257 + zmemcpy(state->window, dictionary + dictLength - state->wsize,
1.1258 + state->wsize);
1.1259 + state->whave = state->wsize;
1.1260 + }
1.1261 + else {
1.1262 + zmemcpy(state->window + state->wsize - dictLength, dictionary,
1.1263 + dictLength);
1.1264 + state->whave = dictLength;
1.1265 + }
1.1266 + state->havedict = 1;
1.1267 + Tracev((stderr, "inflate: dictionary set\n"));
1.1268 + return Z_OK;
1.1269 +}
1.1270 +
1.1271 +
1.1272 +#ifdef __SYMBIAN32__
1.1273 +EXPORT_C int inflateGetHeader_r(z_streamp strm, gz_headerp head)
1.1274 +#else
1.1275 +int ZEXPORT inflateGetHeader(strm, head)
1.1276 +z_streamp strm;
1.1277 +gz_headerp head;
1.1278 +#endif //__SYMBIAN32__
1.1279 +{
1.1280 + struct inflate_state FAR *state;
1.1281 +
1.1282 + /* check state */
1.1283 + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1.1284 + state = (struct inflate_state FAR *)strm->state;
1.1285 + if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
1.1286 +
1.1287 + /* save header structure */
1.1288 + state->head = head;
1.1289 + head->done = 0;
1.1290 + return Z_OK;
1.1291 +}
1.1292 +
1.1293 +/*
1.1294 + Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
1.1295 + or when out of input. When called, *have is the number of pattern bytes
1.1296 + found in order so far, in 0..3. On return *have is updated to the new
1.1297 + state. If on return *have equals four, then the pattern was found and the
1.1298 + return value is how many bytes were read including the last byte of the
1.1299 + pattern. If *have is less than four, then the pattern has not been found
1.1300 + yet and the return value is len. In the latter case, syncsearch() can be
1.1301 + called again with more data and the *have state. *have is initialized to
1.1302 + zero for the first call.
1.1303 + */
1.1304 +
1.1305 +#ifdef __SYMBIAN32__
1.1306 +local unsigned syncsearch(unsigned FAR * have,unsigned char FAR * buf,unsigned len)
1.1307 +#else
1.1308 +local unsigned syncsearch(have, buf, len)
1.1309 +unsigned FAR *have;
1.1310 +unsigned char FAR *buf;
1.1311 +unsigned len;
1.1312 +#endif //__SYMBIAN32__
1.1313 +{
1.1314 + unsigned got;
1.1315 + unsigned next;
1.1316 +
1.1317 + got = *have;
1.1318 + next = 0;
1.1319 + while (next < len && got < 4) {
1.1320 + if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1.1321 + got++;
1.1322 + else if (buf[next])
1.1323 + got = 0;
1.1324 + else
1.1325 + got = 4 - got;
1.1326 + next++;
1.1327 + }
1.1328 + *have = got;
1.1329 + return next;
1.1330 +}
1.1331 +
1.1332 +
1.1333 +#ifdef __SYMBIAN32__
1.1334 +EXPORT_C int inflateSync_r (z_streamp strm)
1.1335 +#else
1.1336 +int ZEXPORT inflateSync(strm)
1.1337 +z_streamp strm;
1.1338 +#endif //__SYMBIAN32__
1.1339 +{
1.1340 + unsigned len; /* number of bytes to look at or looked at */
1.1341 + unsigned long in, out; /* temporary to save total_in and total_out */
1.1342 + unsigned char buf[4]; /* to restore bit buffer to byte string */
1.1343 + struct inflate_state FAR *state;
1.1344 +
1.1345 + /* check parameters */
1.1346 + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1.1347 + state = (struct inflate_state FAR *)strm->state;
1.1348 + if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
1.1349 +
1.1350 + /* if first time, start search in bit buffer */
1.1351 + if (state->mode != SYNC) {
1.1352 + state->mode = SYNC;
1.1353 + state->hold <<= state->bits & 7;
1.1354 + state->bits -= state->bits & 7;
1.1355 + len = 0;
1.1356 + while (state->bits >= 8) {
1.1357 + buf[len++] = (unsigned char)(state->hold);
1.1358 + state->hold >>= 8;
1.1359 + state->bits -= 8;
1.1360 + }
1.1361 + state->have = 0;
1.1362 + syncsearch(&(state->have), buf, len);
1.1363 + }
1.1364 +
1.1365 + /* search available input */
1.1366 + len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1.1367 + strm->avail_in -= len;
1.1368 + strm->next_in += len;
1.1369 + strm->total_in += len;
1.1370 +
1.1371 + /* return no joy or set up to restart inflate() on a new block */
1.1372 + if (state->have != 4) return Z_DATA_ERROR;
1.1373 + in = strm->total_in; out = strm->total_out;
1.1374 + inflateReset_r(strm);
1.1375 + strm->total_in = in; strm->total_out = out;
1.1376 + state->mode = TYPE;
1.1377 + return Z_OK;
1.1378 +}
1.1379 +
1.1380 +/*
1.1381 + Returns true if inflate is currently at the end of a block generated by
1.1382 + Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1.1383 + implementation to provide an additional safety check. PPP uses
1.1384 + Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1.1385 + block. When decompressing, PPP checks that at the end of input packet,
1.1386 + inflate is waiting for these length bytes.
1.1387 + */
1.1388 +#ifdef __SYMBIAN32__
1.1389 +EXPORT_C int inflateSyncPoint_r (z_streamp strm)
1.1390 +#else
1.1391 +int ZEXPORT inflateSyncPoint(strm)
1.1392 +z_streamp strm;
1.1393 +#endif //__SYMBIAN32__
1.1394 +{
1.1395 + struct inflate_state FAR *state;
1.1396 +
1.1397 + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1.1398 + state = (struct inflate_state FAR *)strm->state;
1.1399 + return state->mode == STORED && state->bits == 0;
1.1400 +}
1.1401 +
1.1402 +
1.1403 +#ifdef __SYMBIAN32__
1.1404 +EXPORT_C int inflateCopy_r(z_streamp dest, z_streamp source)
1.1405 +#else
1.1406 +int ZEXPORT inflateCopy(dest, source)
1.1407 +z_streamp dest;
1.1408 +z_streamp source;
1.1409 +#endif //__SYMBIAN32__
1.1410 +{
1.1411 + struct inflate_state FAR *state;
1.1412 + struct inflate_state FAR *copy;
1.1413 + unsigned char FAR *window;
1.1414 + unsigned wsize;
1.1415 +
1.1416 + /* check input */
1.1417 + if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
1.1418 + source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
1.1419 + return Z_STREAM_ERROR;
1.1420 + state = (struct inflate_state FAR *)source->state;
1.1421 +
1.1422 + /* allocate space */
1.1423 + copy = (struct inflate_state FAR *)
1.1424 + ZALLOC(source, 1, sizeof(struct inflate_state));
1.1425 + if (copy == Z_NULL) return Z_MEM_ERROR;
1.1426 + window = Z_NULL;
1.1427 + if (state->window != Z_NULL) {
1.1428 + window = (unsigned char FAR *)
1.1429 + ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
1.1430 + if (window == Z_NULL) {
1.1431 + ZFREE(source, copy);
1.1432 + return Z_MEM_ERROR;
1.1433 + }
1.1434 + }
1.1435 +
1.1436 + /* copy state */
1.1437 + zmemcpy(dest, source, sizeof(z_stream));
1.1438 + zmemcpy(copy, state, sizeof(struct inflate_state));
1.1439 + if (state->lencode >= state->codes &&
1.1440 + state->lencode <= state->codes + ENOUGH - 1) {
1.1441 + copy->lencode = copy->codes + (state->lencode - state->codes);
1.1442 + copy->distcode = copy->codes + (state->distcode - state->codes);
1.1443 + }
1.1444 + copy->next = copy->codes + (state->next - state->codes);
1.1445 + if (window != Z_NULL) {
1.1446 + wsize = 1U << state->wbits;
1.1447 + zmemcpy(window, state->window, wsize);
1.1448 + }
1.1449 + copy->window = window;
1.1450 + dest->state = (struct internal_state FAR *)copy;
1.1451 + return Z_OK;
1.1452 +}
1.1453 +
1.1454 +
1.1455 +
1.1456 +