vrshoot

annotate libs/zlib/infback.c @ 0:b2f14e535253

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