annotate src/zlib-1.2.8/infback.c @ 43:5ea0608b923f

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