annotate src/zlib-1.2.7/inflate.c @ 23:619f715526df sv_v2.1

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