| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#include "zbuild.h" |
| 14 |
#include "zutil.h" |
| 15 |
#include "inftrees.h" |
| 16 |
#include "inflate.h" |
| 17 |
#include "inffast.h" |
| 18 |
#include "inflate_p.h" |
| 19 |
#include "functable.h" |
| 20 |
|
| 21 |
|
| 22 |
#ifdef ZLIB_COMPAT |
| 23 |
# undef inflateBackInit |
| 24 |
#endif |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
int32_t ZNG_CONDEXPORT PREFIX(inflateBackInit)(PREFIX3(stream) *strm, int32_t windowBits, uint8_t *window) { |
| 36 |
struct inflate_state *state; |
| 37 |
|
| 38 |
if (strm == NULL || window == NULL || windowBits < 8 || windowBits > 15) |
| 39 |
return Z_STREAM_ERROR; |
| 40 |
strm->msg = NULL; |
| 41 |
if (strm->zalloc == NULL) { |
| 42 |
strm->zalloc = zng_calloc; |
| 43 |
strm->opaque = NULL; |
| 44 |
} |
| 45 |
if (strm->zfree == NULL) |
| 46 |
strm->zfree = zng_cfree; |
| 47 |
state = ZALLOC_INFLATE_STATE(strm); |
| 48 |
if (state == NULL) |
| 49 |
return Z_MEM_ERROR; |
| 50 |
Tracev((stderr, "inflate: allocated\n")); |
| 51 |
strm->state = (struct internal_state *)state; |
| 52 |
state->dmax = 32768U; |
| 53 |
state->wbits = (unsigned int)windowBits; |
| 54 |
state->wsize = 1U << windowBits; |
| 55 |
state->window = window; |
| 56 |
state->wnext = 0; |
| 57 |
state->whave = 0; |
| 58 |
state->sane = 1; |
| 59 |
state->chunksize = functable.chunksize(); |
| 60 |
return Z_OK; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
int32_t Z_EXPORT PREFIX(inflateBackInit_)(PREFIX3(stream) *strm, int32_t windowBits, uint8_t *window, |
| 65 |
const char *version, int32_t stream_size) { |
| 66 |
if (CHECK_VER_STSIZE(version, stream_size)) |
| 67 |
return Z_VERSION_ERROR; |
| 68 |
return PREFIX(inflateBackInit)(strm, windowBits, window); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
#define PULL() \ |
| 79 |
do { \ |
| 80 |
if (have == 0) { \ |
| 81 |
have = in(in_desc, &next); \ |
| 82 |
if (have == 0) { \ |
| 83 |
next = NULL; \ |
| 84 |
ret = Z_BUF_ERROR; \ |
| 85 |
goto inf_leave; \ |
| 86 |
} \ |
| 87 |
} \ |
| 88 |
} while (0) |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
#define PULLBYTE() \ |
| 93 |
do { \ |
| 94 |
PULL(); \ |
| 95 |
have--; \ |
| 96 |
hold += ((unsigned)(*next++) << bits); \ |
| 97 |
bits += 8; \ |
| 98 |
} while (0) |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
#define ROOM() \ |
| 104 |
do { \ |
| 105 |
if (left == 0) { \ |
| 106 |
put = state->window; \ |
| 107 |
left = state->wsize; \ |
| 108 |
state->whave = left; \ |
| 109 |
if (out(out_desc, put, left)) { \ |
| 110 |
ret = Z_BUF_ERROR; \ |
| 111 |
goto inf_leave; \ |
| 112 |
} \ |
| 113 |
} \ |
| 114 |
} while (0) |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
int32_t Z_EXPORT PREFIX(inflateBack)(PREFIX3(stream) *strm, in_func in, void *in_desc, out_func out, void *out_desc) { |
| 144 |
struct inflate_state *state; |
| 145 |
z_const unsigned char *next; |
| 146 |
unsigned char *put; |
| 147 |
unsigned have, left; |
| 148 |
uint32_t hold; |
| 149 |
unsigned bits; |
| 150 |
unsigned copy; |
| 151 |
unsigned char *from; |
| 152 |
code here; |
| 153 |
code last; |
| 154 |
unsigned len; |
| 155 |
int32_t ret; |
| 156 |
static const uint16_t order[19] = |
| 157 |
{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; |
| 158 |
|
| 159 |
|
| 160 |
if (strm == NULL || strm->state == NULL) |
| 161 |
return Z_STREAM_ERROR; |
| 162 |
state = (struct inflate_state *)strm->state; |
| 163 |
|
| 164 |
|
| 165 |
strm->msg = NULL; |
| 166 |
state->mode = TYPE; |
| 167 |
state->last = 0; |
| 168 |
state->whave = 0; |
| 169 |
next = strm->next_in; |
| 170 |
have = next != NULL ? strm->avail_in : 0; |
| 171 |
hold = 0; |
| 172 |
bits = 0; |
| 173 |
put = state->window; |
| 174 |
left = state->wsize; |
| 175 |
|
| 176 |
|
| 177 |
for (;;) |
| 178 |
switch (state->mode) { |
| 179 |
case TYPE: |
| 180 |
|
| 181 |
if (state->last) { |
| 182 |
BYTEBITS(); |
| 183 |
state->mode = DONE; |
| 184 |
break; |
| 185 |
} |
| 186 |
NEEDBITS(3); |
| 187 |
state->last = BITS(1); |
| 188 |
DROPBITS(1); |
| 189 |
switch (BITS(2)) { |
| 190 |
case 0: |
| 191 |
Tracev((stderr, "inflate: stored block%s\n", state->last ? " (last)" : "")); |
| 192 |
state->mode = STORED; |
| 193 |
break; |
| 194 |
case 1: |
| 195 |
fixedtables(state); |
| 196 |
Tracev((stderr, "inflate: fixed codes block%s\n", state->last ? " (last)" : "")); |
| 197 |
state->mode = LEN; |
| 198 |
break; |
| 199 |
case 2: |
| 200 |
Tracev((stderr, "inflate: dynamic codes block%s\n", state->last ? " (last)" : "")); |
| 201 |
state->mode = TABLE; |
| 202 |
break; |
| 203 |
case 3: |
| 204 |
SET_BAD("invalid block type"); |
| 205 |
} |
| 206 |
DROPBITS(2); |
| 207 |
break; |
| 208 |
|
| 209 |
case STORED: |
| 210 |
|
| 211 |
BYTEBITS(); |
| 212 |
NEEDBITS(32); |
| 213 |
if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { |
| 214 |
SET_BAD("invalid stored block lengths"); |
| 215 |
break; |
| 216 |
} |
| 217 |
state->length = (uint16_t)hold; |
| 218 |
Tracev((stderr, "inflate: stored length %u\n", state->length)); |
| 219 |
INITBITS(); |
| 220 |
|
| 221 |
|
| 222 |
while (state->length != 0) { |
| 223 |
copy = state->length; |
| 224 |
PULL(); |
| 225 |
ROOM(); |
| 226 |
copy = MIN(copy, have); |
| 227 |
copy = MIN(copy, left); |
| 228 |
memcpy(put, next, copy); |
| 229 |
have -= copy; |
| 230 |
next += copy; |
| 231 |
left -= copy; |
| 232 |
put += copy; |
| 233 |
state->length -= copy; |
| 234 |
} |
| 235 |
Tracev((stderr, "inflate: stored end\n")); |
| 236 |
state->mode = TYPE; |
| 237 |
break; |
| 238 |
|
| 239 |
case TABLE: |
| 240 |
|
| 241 |
NEEDBITS(14); |
| 242 |
state->nlen = BITS(5) + 257; |
| 243 |
DROPBITS(5); |
| 244 |
state->ndist = BITS(5) + 1; |
| 245 |
DROPBITS(5); |
| 246 |
state->ncode = BITS(4) + 4; |
| 247 |
DROPBITS(4); |
| 248 |
#ifndef PKZIP_BUG_WORKAROUND |
| 249 |
if (state->nlen > 286 || state->ndist > 30) { |
| 250 |
SET_BAD("too many length or distance symbols"); |
| 251 |
break; |
| 252 |
} |
| 253 |
#endif |
| 254 |
Tracev((stderr, "inflate: table sizes ok\n")); |
| 255 |
state->have = 0; |
| 256 |
|
| 257 |
|
| 258 |
while (state->have < state->ncode) { |
| 259 |
NEEDBITS(3); |
| 260 |
state->lens[order[state->have++]] = (uint16_t)BITS(3); |
| 261 |
DROPBITS(3); |
| 262 |
} |
| 263 |
while (state->have < 19) |
| 264 |
state->lens[order[state->have++]] = 0; |
| 265 |
state->next = state->codes; |
| 266 |
state->lencode = (const code *)(state->next); |
| 267 |
state->lenbits = 7; |
| 268 |
ret = zng_inflate_table(CODES, state->lens, 19, &(state->next), &(state->lenbits), state->work); |
| 269 |
if (ret) { |
| 270 |
SET_BAD("invalid code lengths set"); |
| 271 |
break; |
| 272 |
} |
| 273 |
Tracev((stderr, "inflate: code lengths ok\n")); |
| 274 |
state->have = 0; |
| 275 |
|
| 276 |
|
| 277 |
while (state->have < state->nlen + state->ndist) { |
| 278 |
for (;;) { |
| 279 |
here = state->lencode[BITS(state->lenbits)]; |
| 280 |
if (here.bits <= bits) break; |
| 281 |
PULLBYTE(); |
| 282 |
} |
| 283 |
if (here.val < 16) { |
| 284 |
DROPBITS(here.bits); |
| 285 |
state->lens[state->have++] = here.val; |
| 286 |
} else { |
| 287 |
if (here.val == 16) { |
| 288 |
NEEDBITS(here.bits + 2); |
| 289 |
DROPBITS(here.bits); |
| 290 |
if (state->have == 0) { |
| 291 |
SET_BAD("invalid bit length repeat"); |
| 292 |
break; |
| 293 |
} |
| 294 |
len = state->lens[state->have - 1]; |
| 295 |
copy = 3 + BITS(2); |
| 296 |
DROPBITS(2); |
| 297 |
} else if (here.val == 17) { |
| 298 |
NEEDBITS(here.bits + 3); |
| 299 |
DROPBITS(here.bits); |
| 300 |
len = 0; |
| 301 |
copy = 3 + BITS(3); |
| 302 |
DROPBITS(3); |
| 303 |
} else { |
| 304 |
NEEDBITS(here.bits + 7); |
| 305 |
DROPBITS(here.bits); |
| 306 |
len = 0; |
| 307 |
copy = 11 + BITS(7); |
| 308 |
DROPBITS(7); |
| 309 |
} |
| 310 |
if (state->have + copy > state->nlen + state->ndist) { |
| 311 |
SET_BAD("invalid bit length repeat"); |
| 312 |
break; |
| 313 |
} |
| 314 |
while (copy) { |
| 315 |
--copy; |
| 316 |
state->lens[state->have++] = (uint16_t)len; |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
if (state->mode == BAD) |
| 323 |
break; |
| 324 |
|
| 325 |
|
| 326 |
if (state->lens[256] == 0) { |
| 327 |
SET_BAD("invalid code -- missing end-of-block"); |
| 328 |
break; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
state->next = state->codes; |
| 335 |
state->lencode = (const code *)(state->next); |
| 336 |
state->lenbits = 10; |
| 337 |
ret = zng_inflate_table(LENS, state->lens, state->nlen, &(state->next), &(state->lenbits), state->work); |
| 338 |
if (ret) { |
| 339 |
SET_BAD("invalid literal/lengths set"); |
| 340 |
break; |
| 341 |
} |
| 342 |
state->distcode = (const code *)(state->next); |
| 343 |
state->distbits = 9; |
| 344 |
ret = zng_inflate_table(DISTS, state->lens + state->nlen, state->ndist, |
| 345 |
&(state->next), &(state->distbits), state->work); |
| 346 |
if (ret) { |
| 347 |
SET_BAD("invalid distances set"); |
| 348 |
break; |
| 349 |
} |
| 350 |
Tracev((stderr, "inflate: codes ok\n")); |
| 351 |
state->mode = LEN; |
| 352 |
|
| 353 |
case LEN: |
| 354 |
|
| 355 |
if (have >= INFLATE_FAST_MIN_HAVE && |
| 356 |
left >= INFLATE_FAST_MIN_LEFT) { |
| 357 |
RESTORE(); |
| 358 |
if (state->whave < state->wsize) |
| 359 |
state->whave = state->wsize - left; |
| 360 |
zng_inflate_fast(strm, state->wsize); |
| 361 |
LOAD(); |
| 362 |
break; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
for (;;) { |
| 367 |
here = state->lencode[BITS(state->lenbits)]; |
| 368 |
if (here.bits <= bits) |
| 369 |
break; |
| 370 |
PULLBYTE(); |
| 371 |
} |
| 372 |
if (here.op && (here.op & 0xf0) == 0) { |
| 373 |
last = here; |
| 374 |
for (;;) { |
| 375 |
here = state->lencode[last.val + (BITS(last.bits + last.op) >> last.bits)]; |
| 376 |
if ((unsigned)last.bits + (unsigned)here.bits <= bits) |
| 377 |
break; |
| 378 |
PULLBYTE(); |
| 379 |
} |
| 380 |
DROPBITS(last.bits); |
| 381 |
} |
| 382 |
DROPBITS(here.bits); |
| 383 |
state->length = here.val; |
| 384 |
|
| 385 |
|
| 386 |
if ((int)(here.op) == 0) { |
| 387 |
Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? |
| 388 |
"inflate: literal '%c'\n" : |
| 389 |
"inflate: literal 0x%02x\n", here.val)); |
| 390 |
ROOM(); |
| 391 |
*put++ = (unsigned char)(state->length); |
| 392 |
left--; |
| 393 |
state->mode = LEN; |
| 394 |
break; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
if (here.op & 32) { |
| 399 |
Tracevv((stderr, "inflate: end of block\n")); |
| 400 |
state->mode = TYPE; |
| 401 |
break; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
if (here.op & 64) { |
| 406 |
SET_BAD("invalid literal/length code"); |
| 407 |
break; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
state->extra = (here.op & 15); |
| 412 |
if (state->extra) { |
| 413 |
NEEDBITS(state->extra); |
| 414 |
state->length += BITS(state->extra); |
| 415 |
DROPBITS(state->extra); |
| 416 |
} |
| 417 |
Tracevv((stderr, "inflate: length %u\n", state->length)); |
| 418 |
|
| 419 |
|
| 420 |
for (;;) { |
| 421 |
here = state->distcode[BITS(state->distbits)]; |
| 422 |
if (here.bits <= bits) |
| 423 |
break; |
| 424 |
PULLBYTE(); |
| 425 |
} |
| 426 |
if ((here.op & 0xf0) == 0) { |
| 427 |
last = here; |
| 428 |
for (;;) { |
| 429 |
here = state->distcode[last.val + (BITS(last.bits + last.op) >> last.bits)]; |
| 430 |
if ((unsigned)last.bits + (unsigned)here.bits <= bits) |
| 431 |
break; |
| 432 |
PULLBYTE(); |
| 433 |
} |
| 434 |
DROPBITS(last.bits); |
| 435 |
} |
| 436 |
DROPBITS(here.bits); |
| 437 |
if (here.op & 64) { |
| 438 |
SET_BAD("invalid distance code"); |
| 439 |
break; |
| 440 |
} |
| 441 |
state->offset = here.val; |
| 442 |
state->extra = (here.op & 15); |
| 443 |
|
| 444 |
|
| 445 |
if (state->extra) { |
| 446 |
NEEDBITS(state->extra); |
| 447 |
state->offset += BITS(state->extra); |
| 448 |
DROPBITS(state->extra); |
| 449 |
} |
| 450 |
#ifdef INFLATE_STRICT |
| 451 |
if (state->offset > state->wsize - (state->whave < state->wsize ? left : 0)) { |
| 452 |
SET_BAD("invalid distance too far back"); |
| 453 |
break; |
| 454 |
} |
| 455 |
#endif |
| 456 |
Tracevv((stderr, "inflate: distance %u\n", state->offset)); |
| 457 |
|
| 458 |
|
| 459 |
do { |
| 460 |
ROOM(); |
| 461 |
copy = state->wsize - state->offset; |
| 462 |
if (copy < left) { |
| 463 |
from = put + copy; |
| 464 |
copy = left - copy; |
| 465 |
} else { |
| 466 |
from = put - state->offset; |
| 467 |
copy = left; |
| 468 |
} |
| 469 |
copy = MIN(copy, state->length); |
| 470 |
state->length -= copy; |
| 471 |
left -= copy; |
| 472 |
do { |
| 473 |
*put++ = *from++; |
| 474 |
} while (--copy); |
| 475 |
} while (state->length != 0); |
| 476 |
break; |
| 477 |
|
| 478 |
case DONE: |
| 479 |
|
| 480 |
ret = Z_STREAM_END; |
| 481 |
if (left < state->wsize) { |
| 482 |
if (out(out_desc, state->window, state->wsize - left)) |
| 483 |
ret = Z_BUF_ERROR; |
| 484 |
} |
| 485 |
goto inf_leave; |
| 486 |
|
| 487 |
case BAD: |
| 488 |
ret = Z_DATA_ERROR; |
| 489 |
goto inf_leave; |
| 490 |
|
| 491 |
default: |
| 492 |
ret = Z_STREAM_ERROR; |
| 493 |
goto inf_leave; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
inf_leave: |
| 498 |
strm->next_in = next; |
| 499 |
strm->avail_in = have; |
| 500 |
return ret; |
| 501 |
} |
| 502 |
|
| 503 |
int32_t Z_EXPORT PREFIX(inflateBackEnd)(PREFIX3(stream) *strm) { |
| 504 |
if (strm == NULL || strm->state == NULL || strm->zfree == NULL) |
| 505 |
return Z_STREAM_ERROR; |
| 506 |
ZFREE_STATE(strm, strm->state); |
| 507 |
strm->state = NULL; |
| 508 |
Tracev((stderr, "inflate: end\n")); |
| 509 |
return Z_OK; |
| 510 |
} |