istereo2

annotate libs/zlib/infback.c @ 13:ea928c313344

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