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