istereo

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

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