security/nss/lib/zlib/inflate.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial