dbf-halloween2015

annotate libs/zlib/inflate.c @ 3:c37fe5d8a4ed

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