vrshoot

annotate libs/zlib/inflate.c @ 2:334d17aed7de

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