| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "util.h" |
| 7 |
#include "inflate_tree.h" |
| 8 |
#include "inflate.h" |
| 9 |
#include "inflate64.h" |
| 10 |
|
| 11 |
|
| 12 |
local int inflate9StateCheck OF((z_streamp strm)); |
| 13 |
local void fixedtables OF((struct inflate_state FAR *state)); |
| 14 |
local int updatewindow OF((z_streamp strm, const unsigned char FAR *end, |
| 15 |
unsigned copy)); |
| 16 |
|
| 17 |
local int inflate9StateCheck(strm) |
| 18 |
z_streamp strm; |
| 19 |
{ |
| 20 |
struct inflate_state FAR *state; |
| 21 |
if (strm == Z_NULL || |
| 22 |
strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) |
| 23 |
return 1; |
| 24 |
state = (struct inflate_state FAR *)strm->state; |
| 25 |
if (state == Z_NULL || state->strm != strm || |
| 26 |
state->mode < TYPE || state->mode > MEM) |
| 27 |
return 1; |
| 28 |
return 0; |
| 29 |
} |
| 30 |
|
| 31 |
int ZEXPORT inflate9ResetKeep(strm) |
| 32 |
z_streamp strm; |
| 33 |
{ |
| 34 |
struct inflate_state FAR *state; |
| 35 |
|
| 36 |
if (inflate9StateCheck(strm)) return Z_STREAM_ERROR; |
| 37 |
state = (struct inflate_state FAR *)strm->state; |
| 38 |
strm->total_in = strm->total_out = state->total = 0; |
| 39 |
strm->msg = Z_NULL; |
| 40 |
state->mode = TYPE; |
| 41 |
state->last = 0; |
| 42 |
state->hold = 0; |
| 43 |
state->bits = 0; |
| 44 |
state->lencode = state->distcode = state->next = state->codes; |
| 45 |
Tracev((stderr, "inflate: reset\n")); |
| 46 |
return Z_OK; |
| 47 |
} |
| 48 |
|
| 49 |
int ZEXPORT inflate9Reset(strm) |
| 50 |
z_streamp strm; |
| 51 |
{ |
| 52 |
struct inflate_state FAR *state; |
| 53 |
|
| 54 |
if (inflate9StateCheck(strm)) return Z_STREAM_ERROR; |
| 55 |
state = (struct inflate_state FAR *)strm->state; |
| 56 |
state->wsize = 0; |
| 57 |
state->whave = 0; |
| 58 |
state->wnext = 0; |
| 59 |
return inflate9ResetKeep(strm); |
| 60 |
} |
| 61 |
|
| 62 |
int ZEXPORT inflate9Reset2(strm) |
| 63 |
z_streamp strm; |
| 64 |
{ |
| 65 |
struct inflate_state FAR *state; |
| 66 |
|
| 67 |
|
| 68 |
if (inflate9StateCheck(strm)) return Z_STREAM_ERROR; |
| 69 |
state = (struct inflate_state FAR *)strm->state; |
| 70 |
|
| 71 |
if (state->window != Z_NULL) { |
| 72 |
ZFREE(strm, state->window); |
| 73 |
state->window = Z_NULL; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
state->wrap = 0; |
| 78 |
state->wbits = WBITS64; |
| 79 |
return inflate9Reset(strm); |
| 80 |
} |
| 81 |
|
| 82 |
int ZEXPORT inflate9Init2(strm) |
| 83 |
z_streamp strm; |
| 84 |
{ |
| 85 |
int ret; |
| 86 |
struct inflate_state FAR *state; |
| 87 |
if (strm == Z_NULL) return Z_STREAM_ERROR; |
| 88 |
strm->msg = Z_NULL; |
| 89 |
if (strm->zalloc == (alloc_func)0) { |
| 90 |
return Z_STREAM_ERROR; |
| 91 |
} |
| 92 |
if (strm->zfree == (free_func)0) |
| 93 |
return Z_STREAM_ERROR; |
| 94 |
state = (struct inflate_state FAR *) |
| 95 |
ZALLOC(strm, 1, sizeof(struct inflate_state)); |
| 96 |
if (state == Z_NULL) return Z_MEM_ERROR; |
| 97 |
Tracev((stderr, "inflate: allocated\n")); |
| 98 |
strm->state = (struct internal_state FAR *)state; |
| 99 |
state->strm = strm; |
| 100 |
state->window = Z_NULL; |
| 101 |
state->mode = TYPE; |
| 102 |
ret = inflate9Reset2(strm); |
| 103 |
if (ret != Z_OK) { |
| 104 |
ZFREE(strm, state); |
| 105 |
strm->state = Z_NULL; |
| 106 |
} |
| 107 |
return ret; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
local void fixedtables(state) |
| 121 |
struct inflate_state FAR *state; |
| 122 |
{ |
| 123 |
#ifdef BUILDFIXED |
| 124 |
static int virgin = 1; |
| 125 |
static code *lenfix, *distfix; |
| 126 |
static code fixed[544]; |
| 127 |
|
| 128 |
|
| 129 |
if (virgin) { |
| 130 |
gen_static_table9(state, &fixed, distfix, lenfix); |
| 131 |
|
| 132 |
virgin = 0; |
| 133 |
} |
| 134 |
#else |
| 135 |
# include "inflate_fixed9.h" |
| 136 |
|
| 137 |
#endif |
| 138 |
state->lencode = lenfix; |
| 139 |
state->lenbits = 9; |
| 140 |
state->distcode = distfix; |
| 141 |
state->distbits = 5; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
local int updatewindow(strm, end, copy) |
| 159 |
z_streamp strm; |
| 160 |
const Byte FAR *end; |
| 161 |
unsigned copy; |
| 162 |
{ |
| 163 |
struct inflate_state FAR *state; |
| 164 |
unsigned dist; |
| 165 |
|
| 166 |
state = (struct inflate_state FAR *)strm->state; |
| 167 |
|
| 168 |
|
| 169 |
if (state->window == Z_NULL) { |
| 170 |
state->window = (unsigned char FAR *) |
| 171 |
ZALLOC(strm, 1U << state->wbits, |
| 172 |
sizeof(unsigned char)); |
| 173 |
if (state->window == Z_NULL) return 1; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
if (state->wsize == 0) { |
| 178 |
state->wsize = 1U << state->wbits; |
| 179 |
state->wnext = 0; |
| 180 |
state->whave = 0; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
if (copy >= state->wsize) { |
| 185 |
zmemcpy(state->window, end - state->wsize, state->wsize); |
| 186 |
state->wnext = 0; |
| 187 |
state->whave = state->wsize; |
| 188 |
} |
| 189 |
else { |
| 190 |
dist = state->wsize - state->wnext; |
| 191 |
if (dist > copy) dist = copy; |
| 192 |
zmemcpy(state->window + state->wnext, end - copy, dist); |
| 193 |
copy -= dist; |
| 194 |
if (copy) { |
| 195 |
zmemcpy(state->window, end - copy, copy); |
| 196 |
state->wnext = copy; |
| 197 |
state->whave = state->wsize; |
| 198 |
} |
| 199 |
else { |
| 200 |
state->wnext += dist; |
| 201 |
if (state->wnext == state->wsize) state->wnext = 0; |
| 202 |
if (state->whave < state->wsize) state->whave += dist; |
| 203 |
} |
| 204 |
} |
| 205 |
return 0; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
#define LOAD() \ |
| 210 |
do { \ |
| 211 |
put = strm->next_out; \ |
| 212 |
left = strm->avail_out; \ |
| 213 |
next = strm->next_in; \ |
| 214 |
have = strm->avail_in; \ |
| 215 |
hold = state->hold; \ |
| 216 |
bits = state->bits; \ |
| 217 |
} while (0) |
| 218 |
|
| 219 |
|
| 220 |
#define RESTORE() \ |
| 221 |
do { \ |
| 222 |
strm->next_out = put; \ |
| 223 |
strm->avail_out = left; \ |
| 224 |
strm->next_in = next; \ |
| 225 |
strm->avail_in = have; \ |
| 226 |
state->hold = hold; \ |
| 227 |
state->bits = bits; \ |
| 228 |
} while (0) |
| 229 |
|
| 230 |
|
| 231 |
#define INITBITS() \ |
| 232 |
do { \ |
| 233 |
hold = 0; \ |
| 234 |
bits = 0; \ |
| 235 |
} while (0) |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
#define PULLBYTE() \ |
| 240 |
do { \ |
| 241 |
if (have == 0) goto inf_leave; \ |
| 242 |
have--; \ |
| 243 |
hold += (unsigned long)(*next++) << bits; \ |
| 244 |
bits += 8; \ |
| 245 |
} while (0) |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
#define NEEDBITS(n) \ |
| 250 |
do { \ |
| 251 |
while (bits < (unsigned)(n)) \ |
| 252 |
PULLBYTE(); \ |
| 253 |
} while (0) |
| 254 |
|
| 255 |
|
| 256 |
#define BITS(n) \ |
| 257 |
((unsigned)hold & ((1U << (n)) - 1)) |
| 258 |
|
| 259 |
|
| 260 |
#define DROPBITS(n) \ |
| 261 |
do { \ |
| 262 |
hold >>= (n); \ |
| 263 |
bits -= (unsigned)(n); \ |
| 264 |
} while (0) |
| 265 |
|
| 266 |
|
| 267 |
#define BYTEBITS() \ |
| 268 |
do { \ |
| 269 |
hold >>= bits & 7; \ |
| 270 |
bits -= bits & 7; \ |
| 271 |
} while (0) |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
int ZEXPORT inflate9(strm, flush) |
| 359 |
z_streamp strm; |
| 360 |
int flush; |
| 361 |
{ |
| 362 |
struct inflate_state FAR *state; |
| 363 |
z_const unsigned char FAR *next; |
| 364 |
unsigned char FAR *put; |
| 365 |
unsigned have; |
| 366 |
unsigned long left; |
| 367 |
unsigned long hold; |
| 368 |
unsigned bits; |
| 369 |
unsigned in; |
| 370 |
unsigned long out; |
| 371 |
unsigned long copy; |
| 372 |
unsigned char FAR *from; |
| 373 |
code here; |
| 374 |
code last; |
| 375 |
unsigned len; |
| 376 |
int ret; |
| 377 |
static const unsigned short order[19] = |
| 378 |
{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; |
| 379 |
|
| 380 |
if (inflate9StateCheck(strm) || strm->next_out == Z_NULL || |
| 381 |
(strm->next_in == Z_NULL && strm->avail_in != 0)) |
| 382 |
return Z_STREAM_ERROR; |
| 383 |
|
| 384 |
state = (struct inflate_state FAR *)strm->state; |
| 385 |
LOAD(); |
| 386 |
in = have; |
| 387 |
out = left; |
| 388 |
ret = Z_OK; |
| 389 |
for (;;) |
| 390 |
switch (state->mode) { |
| 391 |
case TYPE: |
| 392 |
|
| 393 |
case TYPED0: |
| 394 |
if (state->last) { |
| 395 |
BYTEBITS(); |
| 396 |
state->mode = DONE; |
| 397 |
break; |
| 398 |
} |
| 399 |
NEEDBITS(3); |
| 400 |
state->last = BITS(1); |
| 401 |
DROPBITS(1); |
| 402 |
switch (BITS(2)) { |
| 403 |
case 0: |
| 404 |
Tracev((stderr, "inflate: stored block%s\n", |
| 405 |
state->last ? " (last)" : "")); |
| 406 |
state->mode = STORED; |
| 407 |
break; |
| 408 |
case 1: |
| 409 |
fixedtables(state); |
| 410 |
Tracev((stderr, "inflate: fixed codes block%s\n", |
| 411 |
state->last ? " (last)" : "")); |
| 412 |
state->mode = LEN; |
| 413 |
break; |
| 414 |
case 2: |
| 415 |
Tracev((stderr, "inflate: dynamic codes block%s\n", |
| 416 |
state->last ? " (last)" : "")); |
| 417 |
state->mode = TABLE; |
| 418 |
break; |
| 419 |
case 3: |
| 420 |
strm->msg = (char *)"invalid block type"; |
| 421 |
state->mode = BAD; |
| 422 |
} |
| 423 |
DROPBITS(2); |
| 424 |
break; |
| 425 |
case STORED: |
| 426 |
BYTEBITS(); |
| 427 |
NEEDBITS(32); |
| 428 |
if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { |
| 429 |
strm->msg = (char *)"invalid stored block lengths"; |
| 430 |
state->mode = BAD; |
| 431 |
break; |
| 432 |
} |
| 433 |
state->length = (unsigned)hold & 0xffff; |
| 434 |
Tracev((stderr, "inflate: stored length %u\n", |
| 435 |
state->length)); |
| 436 |
INITBITS(); |
| 437 |
state->mode = COPY_; |
| 438 |
|
| 439 |
case COPY_: |
| 440 |
state->mode = COPY; |
| 441 |
|
| 442 |
case COPY: |
| 443 |
copy = state->length; |
| 444 |
if (copy) { |
| 445 |
if (copy > have) copy = have; |
| 446 |
if (copy > left) copy = left; |
| 447 |
if (copy == 0) goto inf_leave; |
| 448 |
zmemcpy(put, next, copy); |
| 449 |
have -= copy; |
| 450 |
next += copy; |
| 451 |
left -= copy; |
| 452 |
put += copy; |
| 453 |
state->length -= copy; |
| 454 |
break; |
| 455 |
} |
| 456 |
Tracev((stderr, "inflate: stored end\n")); |
| 457 |
state->mode = TYPE; |
| 458 |
break; |
| 459 |
case TABLE: |
| 460 |
NEEDBITS(14); |
| 461 |
state->nlen = BITS(5) + 257; |
| 462 |
DROPBITS(5); |
| 463 |
state->ndist = BITS(5) + 1; |
| 464 |
DROPBITS(5); |
| 465 |
state->ncode = BITS(4) + 4; |
| 466 |
DROPBITS(4); |
| 467 |
if (state->nlen > 288 || state->ndist > 33) { |
| 468 |
strm->msg = (char *)"too many length symbols"; |
| 469 |
state->mode = BAD; |
| 470 |
break; |
| 471 |
} |
| 472 |
Tracev((stderr, "inflate: table sizes ok\n")); |
| 473 |
state->have = 0; |
| 474 |
state->mode = LENLENS; |
| 475 |
|
| 476 |
case LENLENS: |
| 477 |
while (state->have < state->ncode) { |
| 478 |
NEEDBITS(3); |
| 479 |
state->lens[order[state->have++]] = (unsigned short)BITS(3); |
| 480 |
DROPBITS(3); |
| 481 |
} |
| 482 |
while (state->have < 19) |
| 483 |
state->lens[order[state->have++]] = 0; |
| 484 |
state->next = state->codes; |
| 485 |
state->lencode = (const code FAR *)(state->next); |
| 486 |
state->lenbits = 7; |
| 487 |
ret = inflate_table9(CODES, state->lens, 19, &(state->next), |
| 488 |
&(state->lenbits), state->work); |
| 489 |
if (ret) { |
| 490 |
strm->msg = (char *)"invalid code lengths set"; |
| 491 |
state->mode = BAD; |
| 492 |
break; |
| 493 |
} |
| 494 |
Tracev((stderr, "inflate: code lengths ok\n")); |
| 495 |
state->have = 0; |
| 496 |
state->mode = CODELENS; |
| 497 |
|
| 498 |
case CODELENS: |
| 499 |
while (state->have < state->nlen + state->ndist) { |
| 500 |
for (;;) { |
| 501 |
here = state->lencode[BITS(state->lenbits)]; |
| 502 |
if ((unsigned)(here.bits) <= bits) break; |
| 503 |
PULLBYTE(); |
| 504 |
} |
| 505 |
if (here.val < 16) { |
| 506 |
DROPBITS(here.bits); |
| 507 |
state->lens[state->have++] = here.val; |
| 508 |
} |
| 509 |
else { |
| 510 |
if (here.val == 16) { |
| 511 |
NEEDBITS(here.bits + 2); |
| 512 |
DROPBITS(here.bits); |
| 513 |
if (state->have == 0) { |
| 514 |
strm->msg = (char *)"invalid bit length repeat"; |
| 515 |
state->mode = BAD; |
| 516 |
break; |
| 517 |
} |
| 518 |
len = (unsigned)(state->lens[state->have - 1]); |
| 519 |
copy = 3 + BITS(2); |
| 520 |
DROPBITS(2); |
| 521 |
} |
| 522 |
else if (here.val == 17) { |
| 523 |
NEEDBITS(here.bits + 3); |
| 524 |
DROPBITS(here.bits); |
| 525 |
len = 0; |
| 526 |
copy = 3 + BITS(3); |
| 527 |
DROPBITS(3); |
| 528 |
} |
| 529 |
else { |
| 530 |
NEEDBITS(here.bits + 7); |
| 531 |
DROPBITS(here.bits); |
| 532 |
len = 0; |
| 533 |
copy = 11 + BITS(7); |
| 534 |
DROPBITS(7); |
| 535 |
} |
| 536 |
if (state->have + copy > state->nlen + state->ndist) { |
| 537 |
strm->msg = (char *)"invalid bit length repeat"; |
| 538 |
state->mode = BAD; |
| 539 |
break; |
| 540 |
} |
| 541 |
while (copy--) |
| 542 |
state->lens[state->have++] = (unsigned short)len; |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
if (state->mode == BAD) break; |
| 548 |
|
| 549 |
|
| 550 |
if (state->lens[256] == 0) { |
| 551 |
strm->msg = (char *)"invalid code -- missing end-of-block"; |
| 552 |
state->mode = BAD; |
| 553 |
break; |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
state->next = state->codes; |
| 560 |
state->lencode = (const code FAR *)(state->next); |
| 561 |
state->lenbits = 9; |
| 562 |
ret = inflate_table9(LENS, state->lens, state->nlen, &(state->next), |
| 563 |
&(state->lenbits), state->work); |
| 564 |
if (ret) { |
| 565 |
strm->msg = (char *)"invalid literal/lengths set"; |
| 566 |
state->mode = BAD; |
| 567 |
break; |
| 568 |
} |
| 569 |
state->distcode = (const code FAR *)(state->next); |
| 570 |
state->distbits = 6; |
| 571 |
ret = inflate_table9(DISTS, state->lens + state->nlen, state->ndist, |
| 572 |
&(state->next), &(state->distbits), state->work); |
| 573 |
if (ret) { |
| 574 |
strm->msg = (char *)"invalid distances set"; |
| 575 |
state->mode = BAD; |
| 576 |
break; |
| 577 |
} |
| 578 |
Tracev((stderr, "inflate: codes ok\n")); |
| 579 |
state->mode = LEN; |
| 580 |
|
| 581 |
case LEN_: |
| 582 |
state->mode = LEN; |
| 583 |
|
| 584 |
case LEN: |
| 585 |
state->back = 0; |
| 586 |
for (;;) { |
| 587 |
here = state->lencode[BITS(state->lenbits)]; |
| 588 |
if ((unsigned)(here.bits) <= bits) break; |
| 589 |
PULLBYTE(); |
| 590 |
} |
| 591 |
if (here.op && (here.op & 0xf0) == 0) { |
| 592 |
last = here; |
| 593 |
for (;;) { |
| 594 |
here = state->lencode[last.val + |
| 595 |
(BITS(last.bits + last.op) >> last.bits)]; |
| 596 |
if ((unsigned)(last.bits + here.bits) <= bits) break; |
| 597 |
PULLBYTE(); |
| 598 |
} |
| 599 |
DROPBITS(last.bits); |
| 600 |
state->back += last.bits; |
| 601 |
} |
| 602 |
DROPBITS(here.bits); |
| 603 |
state->back += here.bits; |
| 604 |
state->length = (unsigned)here.val; |
| 605 |
|
| 606 |
if ((int)(here.op) == 0) { |
| 607 |
Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? |
| 608 |
"inflate: literal '%c'\n" : |
| 609 |
"inflate: literal 0x%02x\n", here.val)); |
| 610 |
state->mode = LIT; |
| 611 |
break; |
| 612 |
} |
| 613 |
|
| 614 |
if (here.op & 32) { |
| 615 |
Tracevv((stderr, "inflate: end of block\n")); |
| 616 |
state->back = -1; |
| 617 |
state->mode = TYPE; |
| 618 |
break; |
| 619 |
} |
| 620 |
|
| 621 |
if (here.op & 64) { |
| 622 |
strm->msg = (char *)"invalid literal/length code"; |
| 623 |
state->mode = BAD; |
| 624 |
break; |
| 625 |
} |
| 626 |
state->extra = (unsigned)(here.op) & 31; |
| 627 |
state->mode = LENEXT; |
| 628 |
|
| 629 |
case LENEXT: |
| 630 |
|
| 631 |
if (state->extra) { |
| 632 |
NEEDBITS(state->extra); |
| 633 |
state->length += BITS(state->extra); |
| 634 |
DROPBITS(state->extra); |
| 635 |
state->back += state->extra; |
| 636 |
} |
| 637 |
Tracevv((stderr, "inflate: length %u\n", state->length)); |
| 638 |
state->was = state->length; |
| 639 |
state->mode = DIST; |
| 640 |
|
| 641 |
case DIST: |
| 642 |
|
| 643 |
for (;;) { |
| 644 |
here = state->distcode[BITS(state->distbits)]; |
| 645 |
if ((unsigned)(here.bits) <= bits) break; |
| 646 |
PULLBYTE(); |
| 647 |
} |
| 648 |
if ((here.op & 0xf0) == 0) { |
| 649 |
last = here; |
| 650 |
for (;;) { |
| 651 |
here = state->distcode[last.val + |
| 652 |
(BITS(last.bits + last.op) >> last.bits)]; |
| 653 |
if ((unsigned)(last.bits + here.bits) <= bits) break; |
| 654 |
PULLBYTE(); |
| 655 |
} |
| 656 |
DROPBITS(last.bits); |
| 657 |
state->back += last.bits; |
| 658 |
} |
| 659 |
DROPBITS(here.bits); |
| 660 |
state->back += here.bits; |
| 661 |
if (here.op & 64) { |
| 662 |
strm->msg = (char *)"invalid distance code"; |
| 663 |
state->mode = BAD; |
| 664 |
break; |
| 665 |
} |
| 666 |
state->offset = (unsigned)here.val; |
| 667 |
|
| 668 |
state->extra = (unsigned)(here.op) & 31; |
| 669 |
state->mode = DISTEXT; |
| 670 |
|
| 671 |
case DISTEXT: |
| 672 |
if (state->extra) { |
| 673 |
NEEDBITS(state->extra); |
| 674 |
state->offset += BITS(state->extra); |
| 675 |
DROPBITS(state->extra); |
| 676 |
state->back += state->extra; |
| 677 |
} |
| 678 |
Tracevv((stderr, "inflate: distance %u\n", state->offset)); |
| 679 |
state->mode = MATCH; |
| 680 |
|
| 681 |
case MATCH: |
| 682 |
if (left == 0) goto inf_leave; |
| 683 |
copy = out - left; |
| 684 |
if (state->offset > copy) { |
| 685 |
if (state->window == NULL) { |
| 686 |
strm->msg = (char *)"try to copy from null window buffer!"; |
| 687 |
state->mode = BAD; |
| 688 |
break; |
| 689 |
} |
| 690 |
copy = state->offset - copy; |
| 691 |
if (copy > state->whave) { |
| 692 |
if (state->sane) { |
| 693 |
strm->msg = (char *)"invalid distance too far back"; |
| 694 |
state->mode = BAD; |
| 695 |
break; |
| 696 |
} |
| 697 |
} |
| 698 |
if (copy > state->wnext) { |
| 699 |
copy -= state->wnext; |
| 700 |
from = state->window + (state->wsize - copy); |
| 701 |
} else { |
| 702 |
from = state->window + (state->wnext - copy); |
| 703 |
} |
| 704 |
if (copy > state->length) copy = state->length; |
| 705 |
} else { |
| 706 |
from = put - state->offset; |
| 707 |
copy = state->length; |
| 708 |
} |
| 709 |
if (copy > left) copy = left; |
| 710 |
left -= copy; |
| 711 |
state->length -= copy; |
| 712 |
do { |
| 713 |
*put++ = *from++; |
| 714 |
} while (--copy); |
| 715 |
if (state->length == 0) state->mode = LEN; |
| 716 |
break; |
| 717 |
case LIT: |
| 718 |
if (left == 0) goto inf_leave; |
| 719 |
*put++ = (unsigned char)(state->length); |
| 720 |
left--; |
| 721 |
state->mode = LEN; |
| 722 |
break; |
| 723 |
case DONE: |
| 724 |
ret = Z_STREAM_END; |
| 725 |
goto inf_leave; |
| 726 |
case BAD: |
| 727 |
ret = Z_DATA_ERROR; |
| 728 |
goto inf_leave; |
| 729 |
case MEM: |
| 730 |
return Z_MEM_ERROR; |
| 731 |
default: |
| 732 |
return Z_STREAM_ERROR; |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
inf_leave: |
| 742 |
RESTORE(); |
| 743 |
if (state->wsize || (out != strm->avail_out && state->mode < BAD && |
| 744 |
(state->mode < CHECK || flush != Z_FINISH))) |
| 745 |
if (updatewindow(strm, strm->next_out, out - strm->avail_out)) { |
| 746 |
state->mode = MEM; |
| 747 |
return Z_MEM_ERROR; |
| 748 |
} |
| 749 |
in -= strm->avail_in; |
| 750 |
out -= strm->avail_out; |
| 751 |
strm->total_in += in; |
| 752 |
strm->total_out += out; |
| 753 |
state->total += out; |
| 754 |
strm->data_type = (int)state->bits + (state->last ? 64 : 0) + |
| 755 |
(state->mode == TYPE ? 128 : 0) + |
| 756 |
(state->mode == LEN_ || state->mode == COPY_ ? 256 : 0); |
| 757 |
if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) |
| 758 |
ret = Z_BUF_ERROR; |
| 759 |
return ret; |
| 760 |
} |
| 761 |
|
| 762 |
int ZEXPORT inflate9End(strm) |
| 763 |
z_streamp strm; |
| 764 |
{ |
| 765 |
struct inflate_state FAR *state; |
| 766 |
if (inflate9StateCheck(strm)) |
| 767 |
return Z_STREAM_ERROR; |
| 768 |
state = (struct inflate_state FAR *)strm->state; |
| 769 |
if (state->window != Z_NULL) ZFREE(strm, state->window); |
| 770 |
ZFREE(strm, strm->state); |
| 771 |
strm->state = Z_NULL; |
| 772 |
Tracev((stderr, "inflate: end\n")); |
| 773 |
return Z_OK; |
| 774 |
} |