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