annotate src/zlib-1.2.7/infback.c @ 7:cd13f7cd9bc3

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