modules/zlib/src/infback.c

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial