istereo

annotate libs/zlib/inflate.c @ 26:862a3329a8f0

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