| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include "index_decoder.h" |
| 13 |
|
| 14 |
|
| 15 |
typedef struct { |
| 16 |
enum { |
| 17 |
SEQ_MAGIC_BYTES, |
| 18 |
SEQ_PADDING_SEEK, |
| 19 |
SEQ_PADDING_DECODE, |
| 20 |
SEQ_FOOTER, |
| 21 |
SEQ_INDEX_INIT, |
| 22 |
SEQ_INDEX_DECODE, |
| 23 |
SEQ_HEADER_DECODE, |
| 24 |
SEQ_HEADER_COMPARE, |
| 25 |
} sequence; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
uint64_t file_cur_pos; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
uint64_t file_target_pos; |
| 38 |
|
| 39 |
|
| 40 |
uint64_t file_size; |
| 41 |
|
| 42 |
|
| 43 |
lzma_next_coder index_decoder; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
lzma_vli index_remaining; |
| 48 |
|
| 49 |
|
| 50 |
lzma_index *this_index; |
| 51 |
|
| 52 |
|
| 53 |
lzma_vli stream_padding; |
| 54 |
|
| 55 |
|
| 56 |
lzma_index *combined_index; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
lzma_index **dest_index; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
uint64_t *external_seek_pos; |
| 65 |
|
| 66 |
|
| 67 |
uint64_t memlimit; |
| 68 |
|
| 69 |
|
| 70 |
lzma_stream_flags first_header_flags; |
| 71 |
|
| 72 |
|
| 73 |
lzma_stream_flags header_flags; |
| 74 |
|
| 75 |
|
| 76 |
lzma_stream_flags footer_flags; |
| 77 |
|
| 78 |
size_t temp_pos; |
| 79 |
size_t temp_size; |
| 80 |
uint8_t temp[8192]; |
| 81 |
|
| 82 |
} lzma_file_info_coder; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
static bool |
| 89 |
fill_temp(lzma_file_info_coder *coder, const uint8_t *restrict in, |
| 90 |
size_t *restrict in_pos, size_t in_size) |
| 91 |
{ |
| 92 |
coder->file_cur_pos += lzma_bufcpy(in, in_pos, in_size, |
| 93 |
coder->temp, &coder->temp_pos, coder->temp_size); |
| 94 |
return coder->temp_pos < coder->temp_size; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
static bool |
| 107 |
seek_to_pos(lzma_file_info_coder *coder, uint64_t target_pos, |
| 108 |
size_t in_start, size_t *in_pos, size_t in_size) |
| 109 |
{ |
| 110 |
|
| 111 |
|
| 112 |
assert(coder->file_size - coder->file_cur_pos >= in_size - *in_pos); |
| 113 |
|
| 114 |
const uint64_t pos_min = coder->file_cur_pos - (*in_pos - in_start); |
| 115 |
const uint64_t pos_max = coder->file_cur_pos + (in_size - *in_pos); |
| 116 |
|
| 117 |
bool external_seek_needed; |
| 118 |
|
| 119 |
if (target_pos >= pos_min && target_pos <= pos_max) { |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
*in_pos += (size_t)(target_pos - coder->file_cur_pos); |
| 125 |
external_seek_needed = false; |
| 126 |
} else { |
| 127 |
|
| 128 |
*coder->external_seek_pos = target_pos; |
| 129 |
external_seek_needed = true; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
*in_pos = in_size; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
coder->file_cur_pos = target_pos; |
| 143 |
|
| 144 |
return external_seek_needed; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
static lzma_ret |
| 158 |
reverse_seek(lzma_file_info_coder *coder, |
| 159 |
size_t in_start, size_t *in_pos, size_t in_size) |
| 160 |
{ |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
if (coder->file_target_pos < 2 * LZMA_STREAM_HEADER_SIZE) |
| 165 |
return LZMA_DATA_ERROR; |
| 166 |
|
| 167 |
coder->temp_pos = 0; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
if (coder->file_target_pos - LZMA_STREAM_HEADER_SIZE |
| 176 |
< sizeof(coder->temp)) |
| 177 |
coder->temp_size = (size_t)(coder->file_target_pos |
| 178 |
- LZMA_STREAM_HEADER_SIZE); |
| 179 |
else |
| 180 |
coder->temp_size = sizeof(coder->temp); |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
assert(coder->temp_size >= LZMA_STREAM_HEADER_SIZE); |
| 186 |
|
| 187 |
if (seek_to_pos(coder, coder->file_target_pos - coder->temp_size, |
| 188 |
in_start, in_pos, in_size)) |
| 189 |
return LZMA_SEEK_NEEDED; |
| 190 |
|
| 191 |
return LZMA_OK; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
static size_t |
| 197 |
get_padding_size(const uint8_t *buf, size_t buf_size) |
| 198 |
{ |
| 199 |
size_t padding = 0; |
| 200 |
while (buf_size > 0 && buf[--buf_size] == 0x00) |
| 201 |
++padding; |
| 202 |
|
| 203 |
return padding; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
static lzma_ret |
| 214 |
hide_format_error(lzma_ret ret) |
| 215 |
{ |
| 216 |
if (ret == LZMA_FORMAT_ERROR) |
| 217 |
ret = LZMA_DATA_ERROR; |
| 218 |
|
| 219 |
return ret; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
static lzma_ret |
| 227 |
decode_index(lzma_file_info_coder *coder, const lzma_allocator *allocator, |
| 228 |
const uint8_t *restrict in, size_t *restrict in_pos, |
| 229 |
size_t in_size, bool update_file_cur_pos) |
| 230 |
{ |
| 231 |
const size_t in_start = *in_pos; |
| 232 |
|
| 233 |
const lzma_ret ret = coder->index_decoder.code( |
| 234 |
coder->index_decoder.coder, |
| 235 |
allocator, in, in_pos, in_size, |
| 236 |
NULL, NULL, 0, LZMA_RUN); |
| 237 |
|
| 238 |
coder->index_remaining -= *in_pos - in_start; |
| 239 |
|
| 240 |
if (update_file_cur_pos) |
| 241 |
coder->file_cur_pos += *in_pos - in_start; |
| 242 |
|
| 243 |
return ret; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
static lzma_ret |
| 248 |
file_info_decode(void *coder_ptr, const lzma_allocator *allocator, |
| 249 |
const uint8_t *restrict in, size_t *restrict in_pos, |
| 250 |
size_t in_size, |
| 251 |
uint8_t *restrict out lzma_attribute((__unused__)), |
| 252 |
size_t *restrict out_pos lzma_attribute((__unused__)), |
| 253 |
size_t out_size lzma_attribute((__unused__)), |
| 254 |
lzma_action action lzma_attribute((__unused__))) |
| 255 |
{ |
| 256 |
lzma_file_info_coder *coder = coder_ptr; |
| 257 |
const size_t in_start = *in_pos; |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
assert(coder->file_size >= coder->file_cur_pos); |
| 262 |
if (coder->file_size - coder->file_cur_pos < in_size - in_start) |
| 263 |
in_size = in_start |
| 264 |
+ (size_t)(coder->file_size - coder->file_cur_pos); |
| 265 |
|
| 266 |
while (true) |
| 267 |
switch (coder->sequence) { |
| 268 |
case SEQ_MAGIC_BYTES: |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
if (coder->file_size < LZMA_STREAM_HEADER_SIZE) |
| 277 |
return LZMA_FORMAT_ERROR; |
| 278 |
|
| 279 |
|
| 280 |
if (fill_temp(coder, in, in_pos, in_size)) |
| 281 |
return LZMA_OK; |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
return_if_error(lzma_stream_header_decode( |
| 287 |
&coder->first_header_flags, coder->temp)); |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
if (coder->file_size > LZMA_VLI_MAX || (coder->file_size & 3)) |
| 296 |
return LZMA_DATA_ERROR; |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
coder->file_target_pos = coder->file_size; |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
case SEQ_PADDING_SEEK: |
| 305 |
coder->sequence = SEQ_PADDING_DECODE; |
| 306 |
return_if_error(reverse_seek( |
| 307 |
coder, in_start, in_pos, in_size)); |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
case SEQ_PADDING_DECODE: { |
| 312 |
|
| 313 |
|
| 314 |
if (fill_temp(coder, in, in_pos, in_size)) |
| 315 |
return LZMA_OK; |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
const size_t new_padding = get_padding_size( |
| 320 |
coder->temp, coder->temp_size); |
| 321 |
coder->stream_padding += new_padding; |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
coder->file_target_pos -= new_padding; |
| 328 |
|
| 329 |
if (new_padding == coder->temp_size) { |
| 330 |
|
| 331 |
|
| 332 |
coder->sequence = SEQ_PADDING_SEEK; |
| 333 |
break; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
if (coder->stream_padding & 3) |
| 338 |
return LZMA_DATA_ERROR; |
| 339 |
|
| 340 |
coder->sequence = SEQ_FOOTER; |
| 341 |
|
| 342 |
|
| 343 |
coder->temp_size -= new_padding; |
| 344 |
coder->temp_pos = coder->temp_size; |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
if (coder->temp_size < LZMA_STREAM_HEADER_SIZE) |
| 357 |
return_if_error(reverse_seek( |
| 358 |
coder, in_start, in_pos, in_size)); |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
case SEQ_FOOTER: |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
if (fill_temp(coder, in, in_pos, in_size)) |
| 368 |
return LZMA_OK; |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
coder->file_target_pos -= LZMA_STREAM_HEADER_SIZE; |
| 375 |
coder->temp_size -= LZMA_STREAM_HEADER_SIZE; |
| 376 |
|
| 377 |
|
| 378 |
return_if_error(hide_format_error(lzma_stream_footer_decode( |
| 379 |
&coder->footer_flags, |
| 380 |
coder->temp + coder->temp_size))); |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
if (coder->file_target_pos < coder->footer_flags.backward_size |
| 391 |
+ LZMA_STREAM_HEADER_SIZE) |
| 392 |
return LZMA_DATA_ERROR; |
| 393 |
|
| 394 |
|
| 395 |
coder->file_target_pos -= coder->footer_flags.backward_size; |
| 396 |
coder->sequence = SEQ_INDEX_INIT; |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
if (coder->temp_size >= coder->footer_flags.backward_size) { |
| 401 |
|
| 402 |
|
| 403 |
coder->temp_pos = coder->temp_size |
| 404 |
- coder->footer_flags.backward_size; |
| 405 |
} else { |
| 406 |
|
| 407 |
|
| 408 |
coder->temp_pos = 0; |
| 409 |
coder->temp_size = 0; |
| 410 |
|
| 411 |
|
| 412 |
if (seek_to_pos(coder, coder->file_target_pos, |
| 413 |
in_start, in_pos, in_size)) |
| 414 |
return LZMA_SEEK_NEEDED; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
case SEQ_INDEX_INIT: { |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
uint64_t memused = 0; |
| 432 |
if (coder->combined_index != NULL) { |
| 433 |
memused = lzma_index_memused(coder->combined_index); |
| 434 |
assert(memused <= coder->memlimit); |
| 435 |
if (memused > coder->memlimit) |
| 436 |
return LZMA_PROG_ERROR; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
return_if_error(lzma_index_decoder_init( |
| 441 |
&coder->index_decoder, allocator, |
| 442 |
&coder->this_index, |
| 443 |
coder->memlimit - memused)); |
| 444 |
|
| 445 |
coder->index_remaining = coder->footer_flags.backward_size; |
| 446 |
coder->sequence = SEQ_INDEX_DECODE; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
case SEQ_INDEX_DECODE: { |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
lzma_ret ret; |
| 457 |
if (coder->temp_size != 0) { |
| 458 |
assert(coder->temp_size - coder->temp_pos |
| 459 |
== coder->index_remaining); |
| 460 |
ret = decode_index(coder, allocator, coder->temp, |
| 461 |
&coder->temp_pos, coder->temp_size, |
| 462 |
false); |
| 463 |
} else { |
| 464 |
|
| 465 |
|
| 466 |
size_t in_stop = in_size; |
| 467 |
if (in_size - *in_pos > coder->index_remaining) |
| 468 |
in_stop = *in_pos |
| 469 |
+ (size_t)(coder->index_remaining); |
| 470 |
|
| 471 |
ret = decode_index(coder, allocator, |
| 472 |
in, in_pos, in_stop, true); |
| 473 |
} |
| 474 |
|
| 475 |
switch (ret) { |
| 476 |
case LZMA_OK: |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
if (coder->index_remaining == 0) |
| 481 |
return LZMA_DATA_ERROR; |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
assert(coder->temp_size == 0); |
| 488 |
|
| 489 |
return LZMA_OK; |
| 490 |
|
| 491 |
case LZMA_STREAM_END: |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
if (coder->index_remaining != 0) |
| 496 |
return LZMA_DATA_ERROR; |
| 497 |
|
| 498 |
break; |
| 499 |
|
| 500 |
default: |
| 501 |
return ret; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
const uint64_t seek_amount |
| 510 |
= lzma_index_total_size(coder->this_index) |
| 511 |
+ LZMA_STREAM_HEADER_SIZE; |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
if (coder->file_target_pos < seek_amount) |
| 520 |
return LZMA_DATA_ERROR; |
| 521 |
|
| 522 |
|
| 523 |
coder->file_target_pos -= seek_amount; |
| 524 |
|
| 525 |
if (coder->file_target_pos == 0) { |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
coder->header_flags = coder->first_header_flags; |
| 531 |
coder->sequence = SEQ_HEADER_COMPARE; |
| 532 |
break; |
| 533 |
} |
| 534 |
|
| 535 |
coder->sequence = SEQ_HEADER_DECODE; |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
coder->file_target_pos += LZMA_STREAM_HEADER_SIZE; |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
assert(coder->temp_size == 0 || coder->temp_size |
| 546 |
>= coder->footer_flags.backward_size); |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
if (coder->temp_size != 0 && coder->temp_size |
| 557 |
- coder->footer_flags.backward_size |
| 558 |
>= seek_amount) { |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
coder->temp_pos = coder->temp_size |
| 564 |
- coder->footer_flags.backward_size |
| 565 |
- seek_amount |
| 566 |
+ LZMA_STREAM_HEADER_SIZE; |
| 567 |
coder->temp_size = coder->temp_pos; |
| 568 |
} else { |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
return_if_error(reverse_seek(coder, |
| 575 |
in_start, in_pos, in_size)); |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
case SEQ_HEADER_DECODE: |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
if (fill_temp(coder, in, in_pos, in_size)) |
| 586 |
return LZMA_OK; |
| 587 |
|
| 588 |
|
| 589 |
coder->file_target_pos -= LZMA_STREAM_HEADER_SIZE; |
| 590 |
coder->temp_size -= LZMA_STREAM_HEADER_SIZE; |
| 591 |
coder->temp_pos = coder->temp_size; |
| 592 |
|
| 593 |
|
| 594 |
return_if_error(hide_format_error(lzma_stream_header_decode( |
| 595 |
&coder->header_flags, |
| 596 |
coder->temp + coder->temp_size))); |
| 597 |
|
| 598 |
coder->sequence = SEQ_HEADER_COMPARE; |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
case SEQ_HEADER_COMPARE: |
| 603 |
|
| 604 |
|
| 605 |
return_if_error(lzma_stream_flags_compare( |
| 606 |
&coder->header_flags, &coder->footer_flags)); |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
if (lzma_index_stream_flags(coder->this_index, |
| 612 |
&coder->footer_flags) != LZMA_OK) |
| 613 |
return LZMA_PROG_ERROR; |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
if (lzma_index_stream_padding(coder->this_index, |
| 618 |
coder->stream_padding) != LZMA_OK) |
| 619 |
return LZMA_PROG_ERROR; |
| 620 |
|
| 621 |
|
| 622 |
coder->stream_padding = 0; |
| 623 |
|
| 624 |
|
| 625 |
if (coder->combined_index != NULL) |
| 626 |
return_if_error(lzma_index_cat(coder->this_index, |
| 627 |
coder->combined_index, allocator)); |
| 628 |
|
| 629 |
coder->combined_index = coder->this_index; |
| 630 |
coder->this_index = NULL; |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
if (coder->file_target_pos == 0) { |
| 635 |
|
| 636 |
|
| 637 |
assert(lzma_index_file_size(coder->combined_index) |
| 638 |
== coder->file_size); |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
*coder->dest_index = coder->combined_index; |
| 643 |
coder->combined_index = NULL; |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
*in_pos = in_size; |
| 651 |
return LZMA_STREAM_END; |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
coder->sequence = coder->temp_size > 0 |
| 663 |
? SEQ_PADDING_DECODE : SEQ_PADDING_SEEK; |
| 664 |
break; |
| 665 |
|
| 666 |
default: |
| 667 |
assert(0); |
| 668 |
return LZMA_PROG_ERROR; |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
static lzma_ret |
| 674 |
file_info_decoder_memconfig(void *coder_ptr, uint64_t *memusage, |
| 675 |
uint64_t *old_memlimit, uint64_t new_memlimit) |
| 676 |
{ |
| 677 |
lzma_file_info_coder *coder = coder_ptr; |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
uint64_t combined_index_memusage = 0; |
| 695 |
uint64_t this_index_memusage = 0; |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
if (coder->combined_index != NULL) |
| 700 |
combined_index_memusage = lzma_index_memused( |
| 701 |
coder->combined_index); |
| 702 |
|
| 703 |
|
| 704 |
if (coder->this_index != NULL) { |
| 705 |
|
| 706 |
this_index_memusage = lzma_index_memused(coder->this_index); |
| 707 |
|
| 708 |
} else if (coder->sequence == SEQ_INDEX_DECODE) { |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
uint64_t dummy; |
| 716 |
if (coder->index_decoder.memconfig(coder->index_decoder.coder, |
| 717 |
&this_index_memusage, &dummy, 0) |
| 718 |
!= LZMA_OK) { |
| 719 |
assert(0); |
| 720 |
return LZMA_PROG_ERROR; |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
*memusage = combined_index_memusage + this_index_memusage; |
| 730 |
if (*memusage == 0) |
| 731 |
*memusage = lzma_index_memusage(1, 0); |
| 732 |
|
| 733 |
*old_memlimit = coder->memlimit; |
| 734 |
|
| 735 |
|
| 736 |
if (new_memlimit != 0) { |
| 737 |
if (new_memlimit < *memusage) |
| 738 |
return LZMA_MEMLIMIT_ERROR; |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
if (coder->this_index == NULL |
| 743 |
&& coder->sequence == SEQ_INDEX_DECODE) { |
| 744 |
const uint64_t idec_new_memlimit = new_memlimit |
| 745 |
- combined_index_memusage; |
| 746 |
|
| 747 |
assert(this_index_memusage > 0); |
| 748 |
assert(idec_new_memlimit > 0); |
| 749 |
|
| 750 |
uint64_t dummy1; |
| 751 |
uint64_t dummy2; |
| 752 |
|
| 753 |
if (coder->index_decoder.memconfig( |
| 754 |
coder->index_decoder.coder, |
| 755 |
&dummy1, &dummy2, idec_new_memlimit) |
| 756 |
!= LZMA_OK) { |
| 757 |
assert(0); |
| 758 |
return LZMA_PROG_ERROR; |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
coder->memlimit = new_memlimit; |
| 763 |
} |
| 764 |
|
| 765 |
return LZMA_OK; |
| 766 |
} |
| 767 |
|
| 768 |
|
| 769 |
static void |
| 770 |
file_info_decoder_end(void *coder_ptr, const lzma_allocator *allocator) |
| 771 |
{ |
| 772 |
lzma_file_info_coder *coder = coder_ptr; |
| 773 |
|
| 774 |
lzma_next_end(&coder->index_decoder, allocator); |
| 775 |
lzma_index_end(coder->this_index, allocator); |
| 776 |
lzma_index_end(coder->combined_index, allocator); |
| 777 |
|
| 778 |
lzma_free(coder, allocator); |
| 779 |
return; |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
static lzma_ret |
| 784 |
lzma_file_info_decoder_init(lzma_next_coder *next, |
| 785 |
const lzma_allocator *allocator, uint64_t *seek_pos, |
| 786 |
lzma_index **dest_index, |
| 787 |
uint64_t memlimit, uint64_t file_size) |
| 788 |
{ |
| 789 |
lzma_next_coder_init(&lzma_file_info_decoder_init, next, allocator); |
| 790 |
|
| 791 |
if (dest_index == NULL) |
| 792 |
return LZMA_PROG_ERROR; |
| 793 |
|
| 794 |
lzma_file_info_coder *coder = next->coder; |
| 795 |
if (coder == NULL) { |
| 796 |
coder = lzma_alloc(sizeof(lzma_file_info_coder), allocator); |
| 797 |
if (coder == NULL) |
| 798 |
return LZMA_MEM_ERROR; |
| 799 |
|
| 800 |
next->coder = coder; |
| 801 |
next->code = &file_info_decode; |
| 802 |
next->end = &file_info_decoder_end; |
| 803 |
next->memconfig = &file_info_decoder_memconfig; |
| 804 |
|
| 805 |
coder->index_decoder = LZMA_NEXT_CODER_INIT; |
| 806 |
coder->this_index = NULL; |
| 807 |
coder->combined_index = NULL; |
| 808 |
} |
| 809 |
|
| 810 |
coder->sequence = SEQ_MAGIC_BYTES; |
| 811 |
coder->file_cur_pos = 0; |
| 812 |
coder->file_target_pos = 0; |
| 813 |
coder->file_size = file_size; |
| 814 |
|
| 815 |
lzma_index_end(coder->this_index, allocator); |
| 816 |
coder->this_index = NULL; |
| 817 |
|
| 818 |
lzma_index_end(coder->combined_index, allocator); |
| 819 |
coder->combined_index = NULL; |
| 820 |
|
| 821 |
coder->stream_padding = 0; |
| 822 |
|
| 823 |
coder->dest_index = dest_index; |
| 824 |
coder->external_seek_pos = seek_pos; |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
coder->memlimit = my_max(1, memlimit); |
| 829 |
|
| 830 |
|
| 831 |
coder->temp_pos = 0; |
| 832 |
coder->temp_size = LZMA_STREAM_HEADER_SIZE; |
| 833 |
|
| 834 |
return LZMA_OK; |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
extern LZMA_API(lzma_ret) |
| 839 |
lzma_file_info_decoder(lzma_stream *strm, lzma_index **dest_index, |
| 840 |
uint64_t memlimit, uint64_t file_size) |
| 841 |
{ |
| 842 |
lzma_next_strm_init(lzma_file_info_decoder_init, strm, &strm->seek_pos, |
| 843 |
dest_index, memlimit, file_size); |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
strm->internal->supported_actions[LZMA_RUN] = true; |
| 851 |
strm->internal->supported_actions[LZMA_FINISH] = true; |
| 852 |
|
| 853 |
return LZMA_OK; |
| 854 |
} |