istereo2

annotate libs/zlib/inflate.c @ 15:7bd4264bf74a

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