annotate src/zlib-1.2.8/inflate.c @ 56:af97cad61ff0

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