dbf-halloween2015

annotate libs/zlib/infback.c @ 4:4316c0c879e9

fixed RUN script for macosx
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 06:18:18 +0200
parents
children
rev   line source
nuclear@1 1 /* infback.c -- inflate using a call-back interface
nuclear@1 2 * Copyright (C) 1995-2005 Mark Adler
nuclear@1 3 * For conditions of distribution and use, see copyright notice in zlib.h
nuclear@1 4 */
nuclear@1 5
nuclear@1 6 /*
nuclear@1 7 This code is largely copied from inflate.c. Normally either infback.o or
nuclear@1 8 inflate.o would be linked into an application--not both. The interface
nuclear@1 9 with inffast.c is retained so that optimized assembler-coded versions of
nuclear@1 10 inflate_fast() can be used with either inflate.c or infback.c.
nuclear@1 11 */
nuclear@1 12
nuclear@1 13 #include "zutil.h"
nuclear@1 14 #include "inftrees.h"
nuclear@1 15 #include "inflate.h"
nuclear@1 16 #include "inffast.h"
nuclear@1 17
nuclear@1 18 /* function prototypes */
nuclear@1 19 local void fixedtables OF((struct inflate_state FAR *state));
nuclear@1 20
nuclear@1 21 /*
nuclear@1 22 strm provides memory allocation functions in zalloc and zfree, or
nuclear@1 23 Z_NULL to use the library memory allocation functions.
nuclear@1 24
nuclear@1 25 windowBits is in the range 8..15, and window is a user-supplied
nuclear@1 26 window and output buffer that is 2**windowBits bytes.
nuclear@1 27 */
nuclear@1 28 int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size)
nuclear@1 29 z_streamp strm;
nuclear@1 30 int windowBits;
nuclear@1 31 unsigned char FAR *window;
nuclear@1 32 const char *version;
nuclear@1 33 int stream_size;
nuclear@1 34 {
nuclear@1 35 struct inflate_state FAR *state;
nuclear@1 36
nuclear@1 37 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
nuclear@1 38 stream_size != (int)(sizeof(z_stream)))
nuclear@1 39 return Z_VERSION_ERROR;
nuclear@1 40 if (strm == Z_NULL || window == Z_NULL ||
nuclear@1 41 windowBits < 8 || windowBits > 15)
nuclear@1 42 return Z_STREAM_ERROR;
nuclear@1 43 strm->msg = Z_NULL; /* in case we return an error */
nuclear@1 44 if (strm->zalloc == (alloc_func)0) {
nuclear@1 45 strm->zalloc = zcalloc;
nuclear@1 46 strm->opaque = (voidpf)0;
nuclear@1 47 }
nuclear@1 48 if (strm->zfree == (free_func)0) strm->zfree = zcfree;
nuclear@1 49 state = (struct inflate_state FAR *)ZALLOC(strm, 1,
nuclear@1 50 sizeof(struct inflate_state));
nuclear@1 51 if (state == Z_NULL) return Z_MEM_ERROR;
nuclear@1 52 Tracev((stderr, "inflate: allocated\n"));
nuclear@1 53 strm->state = (struct internal_state FAR *)state;
nuclear@1 54 state->dmax = 32768U;
nuclear@1 55 state->wbits = windowBits;
nuclear@1 56 state->wsize = 1U << windowBits;
nuclear@1 57 state->window = window;
nuclear@1 58 state->write = 0;
nuclear@1 59 state->whave = 0;
nuclear@1 60 return Z_OK;
nuclear@1 61 }
nuclear@1 62
nuclear@1 63 /*
nuclear@1 64 Return state with length and distance decoding tables and index sizes set to
nuclear@1 65 fixed code decoding. Normally this returns fixed tables from inffixed.h.
nuclear@1 66 If BUILDFIXED is defined, then instead this routine builds the tables the
nuclear@1 67 first time it's called, and returns those tables the first time and
nuclear@1 68 thereafter. This reduces the size of the code by about 2K bytes, in
nuclear@1 69 exchange for a little execution time. However, BUILDFIXED should not be
nuclear@1 70 used for threaded applications, since the rewriting of the tables and virgin
nuclear@1 71 may not be thread-safe.
nuclear@1 72 */
nuclear@1 73 local void fixedtables(state)
nuclear@1 74 struct inflate_state FAR *state;
nuclear@1 75 {
nuclear@1 76 #ifdef BUILDFIXED
nuclear@1 77 static int virgin = 1;
nuclear@1 78 static code *lenfix, *distfix;
nuclear@1 79 static code fixed[544];
nuclear@1 80
nuclear@1 81 /* build fixed huffman tables if first call (may not be thread safe) */
nuclear@1 82 if (virgin) {
nuclear@1 83 unsigned sym, bits;
nuclear@1 84 static code *next;
nuclear@1 85
nuclear@1 86 /* literal/length table */
nuclear@1 87 sym = 0;
nuclear@1 88 while (sym < 144) state->lens[sym++] = 8;
nuclear@1 89 while (sym < 256) state->lens[sym++] = 9;
nuclear@1 90 while (sym < 280) state->lens[sym++] = 7;
nuclear@1 91 while (sym < 288) state->lens[sym++] = 8;
nuclear@1 92 next = fixed;
nuclear@1 93 lenfix = next;
nuclear@1 94 bits = 9;
nuclear@1 95 inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
nuclear@1 96
nuclear@1 97 /* distance table */
nuclear@1 98 sym = 0;
nuclear@1 99 while (sym < 32) state->lens[sym++] = 5;
nuclear@1 100 distfix = next;
nuclear@1 101 bits = 5;
nuclear@1 102 inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
nuclear@1 103
nuclear@1 104 /* do this just once */
nuclear@1 105 virgin = 0;
nuclear@1 106 }
nuclear@1 107 #else /* !BUILDFIXED */
nuclear@1 108 # include "inffixed.h"
nuclear@1 109 #endif /* BUILDFIXED */
nuclear@1 110 state->lencode = lenfix;
nuclear@1 111 state->lenbits = 9;
nuclear@1 112 state->distcode = distfix;
nuclear@1 113 state->distbits = 5;
nuclear@1 114 }
nuclear@1 115
nuclear@1 116 /* Macros for inflateBack(): */
nuclear@1 117
nuclear@1 118 /* Load returned state from inflate_fast() */
nuclear@1 119 #define LOAD() \
nuclear@1 120 do { \
nuclear@1 121 put = strm->next_out; \
nuclear@1 122 left = strm->avail_out; \
nuclear@1 123 next = strm->next_in; \
nuclear@1 124 have = strm->avail_in; \
nuclear@1 125 hold = state->hold; \
nuclear@1 126 bits = state->bits; \
nuclear@1 127 } while (0)
nuclear@1 128
nuclear@1 129 /* Set state from registers for inflate_fast() */
nuclear@1 130 #define RESTORE() \
nuclear@1 131 do { \
nuclear@1 132 strm->next_out = put; \
nuclear@1 133 strm->avail_out = left; \
nuclear@1 134 strm->next_in = next; \
nuclear@1 135 strm->avail_in = have; \
nuclear@1 136 state->hold = hold; \
nuclear@1 137 state->bits = bits; \
nuclear@1 138 } while (0)
nuclear@1 139
nuclear@1 140 /* Clear the input bit accumulator */
nuclear@1 141 #define INITBITS() \
nuclear@1 142 do { \
nuclear@1 143 hold = 0; \
nuclear@1 144 bits = 0; \
nuclear@1 145 } while (0)
nuclear@1 146
nuclear@1 147 /* Assure that some input is available. If input is requested, but denied,
nuclear@1 148 then return a Z_BUF_ERROR from inflateBack(). */
nuclear@1 149 #define PULL() \
nuclear@1 150 do { \
nuclear@1 151 if (have == 0) { \
nuclear@1 152 have = in(in_desc, &next); \
nuclear@1 153 if (have == 0) { \
nuclear@1 154 next = Z_NULL; \
nuclear@1 155 ret = Z_BUF_ERROR; \
nuclear@1 156 goto inf_leave; \
nuclear@1 157 } \
nuclear@1 158 } \
nuclear@1 159 } while (0)
nuclear@1 160
nuclear@1 161 /* Get a byte of input into the bit accumulator, or return from inflateBack()
nuclear@1 162 with an error if there is no input available. */
nuclear@1 163 #define PULLBYTE() \
nuclear@1 164 do { \
nuclear@1 165 PULL(); \
nuclear@1 166 have--; \
nuclear@1 167 hold += (unsigned long)(*next++) << bits; \
nuclear@1 168 bits += 8; \
nuclear@1 169 } while (0)
nuclear@1 170
nuclear@1 171 /* Assure that there are at least n bits in the bit accumulator. If there is
nuclear@1 172 not enough available input to do that, then return from inflateBack() with
nuclear@1 173 an error. */
nuclear@1 174 #define NEEDBITS(n) \
nuclear@1 175 do { \
nuclear@1 176 while (bits < (unsigned)(n)) \
nuclear@1 177 PULLBYTE(); \
nuclear@1 178 } while (0)
nuclear@1 179
nuclear@1 180 /* Return the low n bits of the bit accumulator (n < 16) */
nuclear@1 181 #define BITS(n) \
nuclear@1 182 ((unsigned)hold & ((1U << (n)) - 1))
nuclear@1 183
nuclear@1 184 /* Remove n bits from the bit accumulator */
nuclear@1 185 #define DROPBITS(n) \
nuclear@1 186 do { \
nuclear@1 187 hold >>= (n); \
nuclear@1 188 bits -= (unsigned)(n); \
nuclear@1 189 } while (0)
nuclear@1 190
nuclear@1 191 /* Remove zero to seven bits as needed to go to a byte boundary */
nuclear@1 192 #define BYTEBITS() \
nuclear@1 193 do { \
nuclear@1 194 hold >>= bits & 7; \
nuclear@1 195 bits -= bits & 7; \
nuclear@1 196 } while (0)
nuclear@1 197
nuclear@1 198 /* Assure that some output space is available, by writing out the window
nuclear@1 199 if it's full. If the write fails, return from inflateBack() with a
nuclear@1 200 Z_BUF_ERROR. */
nuclear@1 201 #define ROOM() \
nuclear@1 202 do { \
nuclear@1 203 if (left == 0) { \
nuclear@1 204 put = state->window; \
nuclear@1 205 left = state->wsize; \
nuclear@1 206 state->whave = left; \
nuclear@1 207 if (out(out_desc, put, left)) { \
nuclear@1 208 ret = Z_BUF_ERROR; \
nuclear@1 209 goto inf_leave; \
nuclear@1 210 } \
nuclear@1 211 } \
nuclear@1 212 } while (0)
nuclear@1 213
nuclear@1 214 /*
nuclear@1 215 strm provides the memory allocation functions and window buffer on input,
nuclear@1 216 and provides information on the unused input on return. For Z_DATA_ERROR
nuclear@1 217 returns, strm will also provide an error message.
nuclear@1 218
nuclear@1 219 in() and out() are the call-back input and output functions. When
nuclear@1 220 inflateBack() needs more input, it calls in(). When inflateBack() has
nuclear@1 221 filled the window with output, or when it completes with data in the
nuclear@1 222 window, it calls out() to write out the data. The application must not
nuclear@1 223 change the provided input until in() is called again or inflateBack()
nuclear@1 224 returns. The application must not change the window/output buffer until
nuclear@1 225 inflateBack() returns.
nuclear@1 226
nuclear@1 227 in() and out() are called with a descriptor parameter provided in the
nuclear@1 228 inflateBack() call. This parameter can be a structure that provides the
nuclear@1 229 information required to do the read or write, as well as accumulated
nuclear@1 230 information on the input and output such as totals and check values.
nuclear@1 231
nuclear@1 232 in() should return zero on failure. out() should return non-zero on
nuclear@1 233 failure. If either in() or out() fails, than inflateBack() returns a
nuclear@1 234 Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
nuclear@1 235 was in() or out() that caused in the error. Otherwise, inflateBack()
nuclear@1 236 returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
nuclear@1 237 error, or Z_MEM_ERROR if it could not allocate memory for the state.
nuclear@1 238 inflateBack() can also return Z_STREAM_ERROR if the input parameters
nuclear@1 239 are not correct, i.e. strm is Z_NULL or the state was not initialized.
nuclear@1 240 */
nuclear@1 241 int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)
nuclear@1 242 z_streamp strm;
nuclear@1 243 in_func in;
nuclear@1 244 void FAR *in_desc;
nuclear@1 245 out_func out;
nuclear@1 246 void FAR *out_desc;
nuclear@1 247 {
nuclear@1 248 struct inflate_state FAR *state;
nuclear@1 249 unsigned char FAR *next; /* next input */
nuclear@1 250 unsigned char FAR *put; /* next output */
nuclear@1 251 unsigned have, left; /* available input and output */
nuclear@1 252 unsigned long hold; /* bit buffer */
nuclear@1 253 unsigned bits; /* bits in bit buffer */
nuclear@1 254 unsigned copy; /* number of stored or match bytes to copy */
nuclear@1 255 unsigned char FAR *from; /* where to copy match bytes from */
nuclear@1 256 code this; /* current decoding table entry */
nuclear@1 257 code last; /* parent table entry */
nuclear@1 258 unsigned len; /* length to copy for repeats, bits to drop */
nuclear@1 259 int ret; /* return code */
nuclear@1 260 static const unsigned short order[19] = /* permutation of code lengths */
nuclear@1 261 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
nuclear@1 262
nuclear@1 263 /* Check that the strm exists and that the state was initialized */
nuclear@1 264 if (strm == Z_NULL || strm->state == Z_NULL)
nuclear@1 265 return Z_STREAM_ERROR;
nuclear@1 266 state = (struct inflate_state FAR *)strm->state;
nuclear@1 267
nuclear@1 268 /* Reset the state */
nuclear@1 269 strm->msg = Z_NULL;
nuclear@1 270 state->mode = TYPE;
nuclear@1 271 state->last = 0;
nuclear@1 272 state->whave = 0;
nuclear@1 273 next = strm->next_in;
nuclear@1 274 have = next != Z_NULL ? strm->avail_in : 0;
nuclear@1 275 hold = 0;
nuclear@1 276 bits = 0;
nuclear@1 277 put = state->window;
nuclear@1 278 left = state->wsize;
nuclear@1 279
nuclear@1 280 /* Inflate until end of block marked as last */
nuclear@1 281 for (;;)
nuclear@1 282 switch (state->mode) {
nuclear@1 283 case TYPE:
nuclear@1 284 /* determine and dispatch block type */
nuclear@1 285 if (state->last) {
nuclear@1 286 BYTEBITS();
nuclear@1 287 state->mode = DONE;
nuclear@1 288 break;
nuclear@1 289 }
nuclear@1 290 NEEDBITS(3);
nuclear@1 291 state->last = BITS(1);
nuclear@1 292 DROPBITS(1);
nuclear@1 293 switch (BITS(2)) {
nuclear@1 294 case 0: /* stored block */
nuclear@1 295 Tracev((stderr, "inflate: stored block%s\n",
nuclear@1 296 state->last ? " (last)" : ""));
nuclear@1 297 state->mode = STORED;
nuclear@1 298 break;
nuclear@1 299 case 1: /* fixed block */
nuclear@1 300 fixedtables(state);
nuclear@1 301 Tracev((stderr, "inflate: fixed codes block%s\n",
nuclear@1 302 state->last ? " (last)" : ""));
nuclear@1 303 state->mode = LEN; /* decode codes */
nuclear@1 304 break;
nuclear@1 305 case 2: /* dynamic block */
nuclear@1 306 Tracev((stderr, "inflate: dynamic codes block%s\n",
nuclear@1 307 state->last ? " (last)" : ""));
nuclear@1 308 state->mode = TABLE;
nuclear@1 309 break;
nuclear@1 310 case 3:
nuclear@1 311 strm->msg = (char *)"invalid block type";
nuclear@1 312 state->mode = BAD;
nuclear@1 313 }
nuclear@1 314 DROPBITS(2);
nuclear@1 315 break;
nuclear@1 316
nuclear@1 317 case STORED:
nuclear@1 318 /* get and verify stored block length */
nuclear@1 319 BYTEBITS(); /* go to byte boundary */
nuclear@1 320 NEEDBITS(32);
nuclear@1 321 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
nuclear@1 322 strm->msg = (char *)"invalid stored block lengths";
nuclear@1 323 state->mode = BAD;
nuclear@1 324 break;
nuclear@1 325 }
nuclear@1 326 state->length = (unsigned)hold & 0xffff;
nuclear@1 327 Tracev((stderr, "inflate: stored length %u\n",
nuclear@1 328 state->length));
nuclear@1 329 INITBITS();
nuclear@1 330
nuclear@1 331 /* copy stored block from input to output */
nuclear@1 332 while (state->length != 0) {
nuclear@1 333 copy = state->length;
nuclear@1 334 PULL();
nuclear@1 335 ROOM();
nuclear@1 336 if (copy > have) copy = have;
nuclear@1 337 if (copy > left) copy = left;
nuclear@1 338 zmemcpy(put, next, copy);
nuclear@1 339 have -= copy;
nuclear@1 340 next += copy;
nuclear@1 341 left -= copy;
nuclear@1 342 put += copy;
nuclear@1 343 state->length -= copy;
nuclear@1 344 }
nuclear@1 345 Tracev((stderr, "inflate: stored end\n"));
nuclear@1 346 state->mode = TYPE;
nuclear@1 347 break;
nuclear@1 348
nuclear@1 349 case TABLE:
nuclear@1 350 /* get dynamic table entries descriptor */
nuclear@1 351 NEEDBITS(14);
nuclear@1 352 state->nlen = BITS(5) + 257;
nuclear@1 353 DROPBITS(5);
nuclear@1 354 state->ndist = BITS(5) + 1;
nuclear@1 355 DROPBITS(5);
nuclear@1 356 state->ncode = BITS(4) + 4;
nuclear@1 357 DROPBITS(4);
nuclear@1 358 #ifndef PKZIP_BUG_WORKAROUND
nuclear@1 359 if (state->nlen > 286 || state->ndist > 30) {
nuclear@1 360 strm->msg = (char *)"too many length or distance symbols";
nuclear@1 361 state->mode = BAD;
nuclear@1 362 break;
nuclear@1 363 }
nuclear@1 364 #endif
nuclear@1 365 Tracev((stderr, "inflate: table sizes ok\n"));
nuclear@1 366
nuclear@1 367 /* get code length code lengths (not a typo) */
nuclear@1 368 state->have = 0;
nuclear@1 369 while (state->have < state->ncode) {
nuclear@1 370 NEEDBITS(3);
nuclear@1 371 state->lens[order[state->have++]] = (unsigned short)BITS(3);
nuclear@1 372 DROPBITS(3);
nuclear@1 373 }
nuclear@1 374 while (state->have < 19)
nuclear@1 375 state->lens[order[state->have++]] = 0;
nuclear@1 376 state->next = state->codes;
nuclear@1 377 state->lencode = (code const FAR *)(state->next);
nuclear@1 378 state->lenbits = 7;
nuclear@1 379 ret = inflate_table(CODES, state->lens, 19, &(state->next),
nuclear@1 380 &(state->lenbits), state->work);
nuclear@1 381 if (ret) {
nuclear@1 382 strm->msg = (char *)"invalid code lengths set";
nuclear@1 383 state->mode = BAD;
nuclear@1 384 break;
nuclear@1 385 }
nuclear@1 386 Tracev((stderr, "inflate: code lengths ok\n"));
nuclear@1 387
nuclear@1 388 /* get length and distance code code lengths */
nuclear@1 389 state->have = 0;
nuclear@1 390 while (state->have < state->nlen + state->ndist) {
nuclear@1 391 for (;;) {
nuclear@1 392 this = state->lencode[BITS(state->lenbits)];
nuclear@1 393 if ((unsigned)(this.bits) <= bits) break;
nuclear@1 394 PULLBYTE();
nuclear@1 395 }
nuclear@1 396 if (this.val < 16) {
nuclear@1 397 NEEDBITS(this.bits);
nuclear@1 398 DROPBITS(this.bits);
nuclear@1 399 state->lens[state->have++] = this.val;
nuclear@1 400 }
nuclear@1 401 else {
nuclear@1 402 if (this.val == 16) {
nuclear@1 403 NEEDBITS(this.bits + 2);
nuclear@1 404 DROPBITS(this.bits);
nuclear@1 405 if (state->have == 0) {
nuclear@1 406 strm->msg = (char *)"invalid bit length repeat";
nuclear@1 407 state->mode = BAD;
nuclear@1 408 break;
nuclear@1 409 }
nuclear@1 410 len = (unsigned)(state->lens[state->have - 1]);
nuclear@1 411 copy = 3 + BITS(2);
nuclear@1 412 DROPBITS(2);
nuclear@1 413 }
nuclear@1 414 else if (this.val == 17) {
nuclear@1 415 NEEDBITS(this.bits + 3);
nuclear@1 416 DROPBITS(this.bits);
nuclear@1 417 len = 0;
nuclear@1 418 copy = 3 + BITS(3);
nuclear@1 419 DROPBITS(3);
nuclear@1 420 }
nuclear@1 421 else {
nuclear@1 422 NEEDBITS(this.bits + 7);
nuclear@1 423 DROPBITS(this.bits);
nuclear@1 424 len = 0;
nuclear@1 425 copy = 11 + BITS(7);
nuclear@1 426 DROPBITS(7);
nuclear@1 427 }
nuclear@1 428 if (state->have + copy > state->nlen + state->ndist) {
nuclear@1 429 strm->msg = (char *)"invalid bit length repeat";
nuclear@1 430 state->mode = BAD;
nuclear@1 431 break;
nuclear@1 432 }
nuclear@1 433 while (copy--)
nuclear@1 434 state->lens[state->have++] = (unsigned short)len;
nuclear@1 435 }
nuclear@1 436 }
nuclear@1 437
nuclear@1 438 /* handle error breaks in while */
nuclear@1 439 if (state->mode == BAD) break;
nuclear@1 440
nuclear@1 441 /* build code tables */
nuclear@1 442 state->next = state->codes;
nuclear@1 443 state->lencode = (code const FAR *)(state->next);
nuclear@1 444 state->lenbits = 9;
nuclear@1 445 ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
nuclear@1 446 &(state->lenbits), state->work);
nuclear@1 447 if (ret) {
nuclear@1 448 strm->msg = (char *)"invalid literal/lengths set";
nuclear@1 449 state->mode = BAD;
nuclear@1 450 break;
nuclear@1 451 }
nuclear@1 452 state->distcode = (code const FAR *)(state->next);
nuclear@1 453 state->distbits = 6;
nuclear@1 454 ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
nuclear@1 455 &(state->next), &(state->distbits), state->work);
nuclear@1 456 if (ret) {
nuclear@1 457 strm->msg = (char *)"invalid distances set";
nuclear@1 458 state->mode = BAD;
nuclear@1 459 break;
nuclear@1 460 }
nuclear@1 461 Tracev((stderr, "inflate: codes ok\n"));
nuclear@1 462 state->mode = LEN;
nuclear@1 463
nuclear@1 464 case LEN:
nuclear@1 465 /* use inflate_fast() if we have enough input and output */
nuclear@1 466 if (have >= 6 && left >= 258) {
nuclear@1 467 RESTORE();
nuclear@1 468 if (state->whave < state->wsize)
nuclear@1 469 state->whave = state->wsize - left;
nuclear@1 470 inflate_fast(strm, state->wsize);
nuclear@1 471 LOAD();
nuclear@1 472 break;
nuclear@1 473 }
nuclear@1 474
nuclear@1 475 /* get a literal, length, or end-of-block code */
nuclear@1 476 for (;;) {
nuclear@1 477 this = state->lencode[BITS(state->lenbits)];
nuclear@1 478 if ((unsigned)(this.bits) <= bits) break;
nuclear@1 479 PULLBYTE();
nuclear@1 480 }
nuclear@1 481 if (this.op && (this.op & 0xf0) == 0) {
nuclear@1 482 last = this;
nuclear@1 483 for (;;) {
nuclear@1 484 this = state->lencode[last.val +
nuclear@1 485 (BITS(last.bits + last.op) >> last.bits)];
nuclear@1 486 if ((unsigned)(last.bits + this.bits) <= bits) break;
nuclear@1 487 PULLBYTE();
nuclear@1 488 }
nuclear@1 489 DROPBITS(last.bits);
nuclear@1 490 }
nuclear@1 491 DROPBITS(this.bits);
nuclear@1 492 state->length = (unsigned)this.val;
nuclear@1 493
nuclear@1 494 /* process literal */
nuclear@1 495 if (this.op == 0) {
nuclear@1 496 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
nuclear@1 497 "inflate: literal '%c'\n" :
nuclear@1 498 "inflate: literal 0x%02x\n", this.val));
nuclear@1 499 ROOM();
nuclear@1 500 *put++ = (unsigned char)(state->length);
nuclear@1 501 left--;
nuclear@1 502 state->mode = LEN;
nuclear@1 503 break;
nuclear@1 504 }
nuclear@1 505
nuclear@1 506 /* process end of block */
nuclear@1 507 if (this.op & 32) {
nuclear@1 508 Tracevv((stderr, "inflate: end of block\n"));
nuclear@1 509 state->mode = TYPE;
nuclear@1 510 break;
nuclear@1 511 }
nuclear@1 512
nuclear@1 513 /* invalid code */
nuclear@1 514 if (this.op & 64) {
nuclear@1 515 strm->msg = (char *)"invalid literal/length code";
nuclear@1 516 state->mode = BAD;
nuclear@1 517 break;
nuclear@1 518 }
nuclear@1 519
nuclear@1 520 /* length code -- get extra bits, if any */
nuclear@1 521 state->extra = (unsigned)(this.op) & 15;
nuclear@1 522 if (state->extra != 0) {
nuclear@1 523 NEEDBITS(state->extra);
nuclear@1 524 state->length += BITS(state->extra);
nuclear@1 525 DROPBITS(state->extra);
nuclear@1 526 }
nuclear@1 527 Tracevv((stderr, "inflate: length %u\n", state->length));
nuclear@1 528
nuclear@1 529 /* get distance code */
nuclear@1 530 for (;;) {
nuclear@1 531 this = state->distcode[BITS(state->distbits)];
nuclear@1 532 if ((unsigned)(this.bits) <= bits) break;
nuclear@1 533 PULLBYTE();
nuclear@1 534 }
nuclear@1 535 if ((this.op & 0xf0) == 0) {
nuclear@1 536 last = this;
nuclear@1 537 for (;;) {
nuclear@1 538 this = state->distcode[last.val +
nuclear@1 539 (BITS(last.bits + last.op) >> last.bits)];
nuclear@1 540 if ((unsigned)(last.bits + this.bits) <= bits) break;
nuclear@1 541 PULLBYTE();
nuclear@1 542 }
nuclear@1 543 DROPBITS(last.bits);
nuclear@1 544 }
nuclear@1 545 DROPBITS(this.bits);
nuclear@1 546 if (this.op & 64) {
nuclear@1 547 strm->msg = (char *)"invalid distance code";
nuclear@1 548 state->mode = BAD;
nuclear@1 549 break;
nuclear@1 550 }
nuclear@1 551 state->offset = (unsigned)this.val;
nuclear@1 552
nuclear@1 553 /* get distance extra bits, if any */
nuclear@1 554 state->extra = (unsigned)(this.op) & 15;
nuclear@1 555 if (state->extra != 0) {
nuclear@1 556 NEEDBITS(state->extra);
nuclear@1 557 state->offset += BITS(state->extra);
nuclear@1 558 DROPBITS(state->extra);
nuclear@1 559 }
nuclear@1 560 if (state->offset > state->wsize - (state->whave < state->wsize ?
nuclear@1 561 left : 0)) {
nuclear@1 562 strm->msg = (char *)"invalid distance too far back";
nuclear@1 563 state->mode = BAD;
nuclear@1 564 break;
nuclear@1 565 }
nuclear@1 566 Tracevv((stderr, "inflate: distance %u\n", state->offset));
nuclear@1 567
nuclear@1 568 /* copy match from window to output */
nuclear@1 569 do {
nuclear@1 570 ROOM();
nuclear@1 571 copy = state->wsize - state->offset;
nuclear@1 572 if (copy < left) {
nuclear@1 573 from = put + copy;
nuclear@1 574 copy = left - copy;
nuclear@1 575 }
nuclear@1 576 else {
nuclear@1 577 from = put - state->offset;
nuclear@1 578 copy = left;
nuclear@1 579 }
nuclear@1 580 if (copy > state->length) copy = state->length;
nuclear@1 581 state->length -= copy;
nuclear@1 582 left -= copy;
nuclear@1 583 do {
nuclear@1 584 *put++ = *from++;
nuclear@1 585 } while (--copy);
nuclear@1 586 } while (state->length != 0);
nuclear@1 587 break;
nuclear@1 588
nuclear@1 589 case DONE:
nuclear@1 590 /* inflate stream terminated properly -- write leftover output */
nuclear@1 591 ret = Z_STREAM_END;
nuclear@1 592 if (left < state->wsize) {
nuclear@1 593 if (out(out_desc, state->window, state->wsize - left))
nuclear@1 594 ret = Z_BUF_ERROR;
nuclear@1 595 }
nuclear@1 596 goto inf_leave;
nuclear@1 597
nuclear@1 598 case BAD:
nuclear@1 599 ret = Z_DATA_ERROR;
nuclear@1 600 goto inf_leave;
nuclear@1 601
nuclear@1 602 default: /* can't happen, but makes compilers happy */
nuclear@1 603 ret = Z_STREAM_ERROR;
nuclear@1 604 goto inf_leave;
nuclear@1 605 }
nuclear@1 606
nuclear@1 607 /* Return unused input */
nuclear@1 608 inf_leave:
nuclear@1 609 strm->next_in = next;
nuclear@1 610 strm->avail_in = have;
nuclear@1 611 return ret;
nuclear@1 612 }
nuclear@1 613
nuclear@1 614 int ZEXPORT inflateBackEnd(strm)
nuclear@1 615 z_streamp strm;
nuclear@1 616 {
nuclear@1 617 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
nuclear@1 618 return Z_STREAM_ERROR;
nuclear@1 619 ZFREE(strm, strm->state);
nuclear@1 620 strm->state = Z_NULL;
nuclear@1 621 Tracev((stderr, "inflate: end\n"));
nuclear@1 622 return Z_OK;
nuclear@1 623 }