| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#include "lz_encoder.h" |
| 14 |
#include "lz_encoder_hash.h" |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#if defined(WORDS_BIGENDIAN) && !defined(HAVE_SMALL) |
| 19 |
# include "lz_encoder_hash_table.h" |
| 20 |
#endif |
| 21 |
|
| 22 |
#include "memcmplen.h" |
| 23 |
|
| 24 |
|
| 25 |
typedef struct { |
| 26 |
|
| 27 |
lzma_lz_encoder lz; |
| 28 |
|
| 29 |
|
| 30 |
lzma_mf mf; |
| 31 |
|
| 32 |
|
| 33 |
lzma_next_coder next; |
| 34 |
} lzma_coder; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
static void |
| 45 |
move_window(lzma_mf *mf) |
| 46 |
{ |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
assert(mf->read_pos > mf->keep_size_before); |
| 52 |
const uint32_t move_offset |
| 53 |
= (mf->read_pos - mf->keep_size_before) & ~UINT32_C(15); |
| 54 |
|
| 55 |
assert(mf->write_pos > move_offset); |
| 56 |
const size_t move_size = mf->write_pos - move_offset; |
| 57 |
|
| 58 |
assert(move_offset + move_size <= mf->size); |
| 59 |
|
| 60 |
memmove(mf->buffer, mf->buffer + move_offset, move_size); |
| 61 |
|
| 62 |
mf->offset += move_offset; |
| 63 |
mf->read_pos -= move_offset; |
| 64 |
mf->read_limit -= move_offset; |
| 65 |
mf->write_pos -= move_offset; |
| 66 |
|
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
static lzma_ret |
| 80 |
fill_window(lzma_coder *coder, const lzma_allocator *allocator, |
| 81 |
const uint8_t *in, size_t *in_pos, size_t in_size, |
| 82 |
lzma_action action) |
| 83 |
{ |
| 84 |
assert(coder->mf.read_pos <= coder->mf.write_pos); |
| 85 |
|
| 86 |
|
| 87 |
if (coder->mf.read_pos >= coder->mf.size - coder->mf.keep_size_after) |
| 88 |
move_window(&coder->mf); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
size_t write_pos = coder->mf.write_pos; |
| 94 |
lzma_ret ret; |
| 95 |
if (coder->next.code == NULL) { |
| 96 |
|
| 97 |
lzma_bufcpy(in, in_pos, in_size, coder->mf.buffer, |
| 98 |
&write_pos, coder->mf.size); |
| 99 |
|
| 100 |
ret = action != LZMA_RUN && *in_pos == in_size |
| 101 |
? LZMA_STREAM_END : LZMA_OK; |
| 102 |
|
| 103 |
} else { |
| 104 |
ret = coder->next.code(coder->next.coder, allocator, |
| 105 |
in, in_pos, in_size, |
| 106 |
coder->mf.buffer, &write_pos, |
| 107 |
coder->mf.size, action); |
| 108 |
} |
| 109 |
|
| 110 |
coder->mf.write_pos = write_pos; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
memzero(coder->mf.buffer + write_pos, LZMA_MEMCMPLEN_EXTRA); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
if (ret == LZMA_STREAM_END) { |
| 123 |
assert(*in_pos == in_size); |
| 124 |
ret = LZMA_OK; |
| 125 |
coder->mf.action = action; |
| 126 |
coder->mf.read_limit = coder->mf.write_pos; |
| 127 |
|
| 128 |
} else if (coder->mf.write_pos > coder->mf.keep_size_after) { |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
coder->mf.read_limit = coder->mf.write_pos |
| 133 |
- coder->mf.keep_size_after; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
if (coder->mf.pending > 0 |
| 138 |
&& coder->mf.read_pos < coder->mf.read_limit) { |
| 139 |
|
| 140 |
|
| 141 |
const uint32_t pending = coder->mf.pending; |
| 142 |
coder->mf.pending = 0; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
assert(coder->mf.read_pos >= pending); |
| 147 |
coder->mf.read_pos -= pending; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
coder->mf.skip(&coder->mf, pending); |
| 152 |
} |
| 153 |
|
| 154 |
return ret; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
static lzma_ret |
| 159 |
lz_encode(void *coder_ptr, const lzma_allocator *allocator, |
| 160 |
const uint8_t *restrict in, size_t *restrict in_pos, |
| 161 |
size_t in_size, |
| 162 |
uint8_t *restrict out, size_t *restrict out_pos, |
| 163 |
size_t out_size, lzma_action action) |
| 164 |
{ |
| 165 |
lzma_coder *coder = coder_ptr; |
| 166 |
|
| 167 |
while (*out_pos < out_size |
| 168 |
&& (*in_pos < in_size || action != LZMA_RUN)) { |
| 169 |
|
| 170 |
if (coder->mf.action == LZMA_RUN && coder->mf.read_pos |
| 171 |
>= coder->mf.read_limit) |
| 172 |
return_if_error(fill_window(coder, allocator, |
| 173 |
in, in_pos, in_size, action)); |
| 174 |
|
| 175 |
|
| 176 |
const lzma_ret ret = coder->lz.code(coder->lz.coder, |
| 177 |
&coder->mf, out, out_pos, out_size); |
| 178 |
if (ret != LZMA_OK) { |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
coder->mf.action = LZMA_RUN; |
| 183 |
return ret; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
return LZMA_OK; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
static bool |
| 192 |
lz_encoder_prepare(lzma_mf *mf, const lzma_allocator *allocator, |
| 193 |
const lzma_lz_options *lz_options) |
| 194 |
{ |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
if (!IS_ENC_DICT_SIZE_VALID(lz_options->dict_size) |
| 199 |
|| lz_options->nice_len > lz_options->match_len_max) |
| 200 |
return true; |
| 201 |
|
| 202 |
mf->keep_size_before = lz_options->before_size + lz_options->dict_size; |
| 203 |
|
| 204 |
mf->keep_size_after = lz_options->after_size |
| 205 |
+ lz_options->match_len_max; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
uint32_t reserve = lz_options->dict_size / 2; |
| 219 |
if (reserve > (UINT32_C(1) << 30)) |
| 220 |
reserve /= 2; |
| 221 |
|
| 222 |
reserve += (lz_options->before_size + lz_options->match_len_max |
| 223 |
+ lz_options->after_size) / 2 + (UINT32_C(1) << 19); |
| 224 |
|
| 225 |
const uint32_t old_size = mf->size; |
| 226 |
mf->size = mf->keep_size_before + reserve + mf->keep_size_after; |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
if (mf->buffer != NULL && old_size != mf->size) { |
| 231 |
lzma_free(mf->buffer, allocator); |
| 232 |
mf->buffer = NULL; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
mf->match_len_max = lz_options->match_len_max; |
| 237 |
mf->nice_len = lz_options->nice_len; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
mf->cyclic_size = lz_options->dict_size + 1; |
| 255 |
|
| 256 |
|
| 257 |
switch (lz_options->match_finder) { |
| 258 |
#ifdef HAVE_MF_HC3 |
| 259 |
case LZMA_MF_HC3: |
| 260 |
mf->find = &lzma_mf_hc3_find; |
| 261 |
mf->skip = &lzma_mf_hc3_skip; |
| 262 |
break; |
| 263 |
#endif |
| 264 |
#ifdef HAVE_MF_HC4 |
| 265 |
case LZMA_MF_HC4: |
| 266 |
mf->find = &lzma_mf_hc4_find; |
| 267 |
mf->skip = &lzma_mf_hc4_skip; |
| 268 |
break; |
| 269 |
#endif |
| 270 |
#ifdef HAVE_MF_BT2 |
| 271 |
case LZMA_MF_BT2: |
| 272 |
mf->find = &lzma_mf_bt2_find; |
| 273 |
mf->skip = &lzma_mf_bt2_skip; |
| 274 |
break; |
| 275 |
#endif |
| 276 |
#ifdef HAVE_MF_BT3 |
| 277 |
case LZMA_MF_BT3: |
| 278 |
mf->find = &lzma_mf_bt3_find; |
| 279 |
mf->skip = &lzma_mf_bt3_skip; |
| 280 |
break; |
| 281 |
#endif |
| 282 |
#ifdef HAVE_MF_BT4 |
| 283 |
case LZMA_MF_BT4: |
| 284 |
mf->find = &lzma_mf_bt4_find; |
| 285 |
mf->skip = &lzma_mf_bt4_skip; |
| 286 |
break; |
| 287 |
#endif |
| 288 |
|
| 289 |
default: |
| 290 |
return true; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
const uint32_t hash_bytes |
| 300 |
= mf_get_hash_bytes(lz_options->match_finder); |
| 301 |
assert(hash_bytes <= mf->nice_len); |
| 302 |
|
| 303 |
const bool is_bt = (lz_options->match_finder & 0x10) != 0; |
| 304 |
uint32_t hs; |
| 305 |
|
| 306 |
if (hash_bytes == 2) { |
| 307 |
hs = 0xFFFF; |
| 308 |
} else { |
| 309 |
|
| 310 |
|
| 311 |
hs = lz_options->dict_size - 1; |
| 312 |
hs |= hs >> 1; |
| 313 |
hs |= hs >> 2; |
| 314 |
hs |= hs >> 4; |
| 315 |
hs |= hs >> 8; |
| 316 |
hs >>= 1; |
| 317 |
hs |= 0xFFFF; |
| 318 |
|
| 319 |
if (hs > (UINT32_C(1) << 24)) { |
| 320 |
if (hash_bytes == 3) |
| 321 |
hs = (UINT32_C(1) << 24) - 1; |
| 322 |
else |
| 323 |
hs >>= 1; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
mf->hash_mask = hs; |
| 328 |
|
| 329 |
++hs; |
| 330 |
if (hash_bytes > 2) |
| 331 |
hs += HASH_2_SIZE; |
| 332 |
if (hash_bytes > 3) |
| 333 |
hs += HASH_3_SIZE; |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
const uint32_t old_hash_count = mf->hash_count; |
| 341 |
const uint32_t old_sons_count = mf->sons_count; |
| 342 |
mf->hash_count = hs; |
| 343 |
mf->sons_count = mf->cyclic_size; |
| 344 |
if (is_bt) |
| 345 |
mf->sons_count *= 2; |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
if (old_hash_count != mf->hash_count |
| 350 |
|| old_sons_count != mf->sons_count) { |
| 351 |
lzma_free(mf->hash, allocator); |
| 352 |
mf->hash = NULL; |
| 353 |
|
| 354 |
lzma_free(mf->son, allocator); |
| 355 |
mf->son = NULL; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
mf->depth = lz_options->depth; |
| 360 |
if (mf->depth == 0) { |
| 361 |
if (is_bt) |
| 362 |
mf->depth = 16 + mf->nice_len / 2; |
| 363 |
else |
| 364 |
mf->depth = 4 + mf->nice_len / 4; |
| 365 |
} |
| 366 |
|
| 367 |
return false; |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
static bool |
| 372 |
lz_encoder_init(lzma_mf *mf, const lzma_allocator *allocator, |
| 373 |
const lzma_lz_options *lz_options) |
| 374 |
{ |
| 375 |
|
| 376 |
if (mf->buffer == NULL) { |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
mf->buffer = lzma_alloc(mf->size + LZMA_MEMCMPLEN_EXTRA, |
| 381 |
allocator); |
| 382 |
if (mf->buffer == NULL) |
| 383 |
return true; |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
memzero(mf->buffer + mf->size, LZMA_MEMCMPLEN_EXTRA); |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
mf->offset = mf->cyclic_size; |
| 396 |
mf->read_pos = 0; |
| 397 |
mf->read_ahead = 0; |
| 398 |
mf->read_limit = 0; |
| 399 |
mf->write_pos = 0; |
| 400 |
mf->pending = 0; |
| 401 |
|
| 402 |
#if UINT32_MAX >= SIZE_MAX / 4 |
| 403 |
|
| 404 |
|
| 405 |
if (mf->hash_count > SIZE_MAX / sizeof(uint32_t) |
| 406 |
|| mf->sons_count > SIZE_MAX / sizeof(uint32_t)) |
| 407 |
return true; |
| 408 |
#endif |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
if (mf->hash == NULL) { |
| 421 |
mf->hash = lzma_alloc_zero(mf->hash_count * sizeof(uint32_t), |
| 422 |
allocator); |
| 423 |
mf->son = lzma_alloc(mf->sons_count * sizeof(uint32_t), |
| 424 |
allocator); |
| 425 |
|
| 426 |
if (mf->hash == NULL || mf->son == NULL) { |
| 427 |
lzma_free(mf->hash, allocator); |
| 428 |
mf->hash = NULL; |
| 429 |
|
| 430 |
lzma_free(mf->son, allocator); |
| 431 |
mf->son = NULL; |
| 432 |
|
| 433 |
return true; |
| 434 |
} |
| 435 |
} else { |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
memzero(mf->hash, mf->hash_count * sizeof(uint32_t)); |
| 441 |
} |
| 442 |
|
| 443 |
mf->cyclic_pos = 0; |
| 444 |
|
| 445 |
|
| 446 |
if (lz_options->preset_dict != NULL |
| 447 |
&& lz_options->preset_dict_size > 0) { |
| 448 |
|
| 449 |
|
| 450 |
mf->write_pos = my_min(lz_options->preset_dict_size, mf->size); |
| 451 |
memcpy(mf->buffer, lz_options->preset_dict |
| 452 |
+ lz_options->preset_dict_size - mf->write_pos, |
| 453 |
mf->write_pos); |
| 454 |
mf->action = LZMA_SYNC_FLUSH; |
| 455 |
mf->skip(mf, mf->write_pos); |
| 456 |
} |
| 457 |
|
| 458 |
mf->action = LZMA_RUN; |
| 459 |
|
| 460 |
return false; |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
extern uint64_t |
| 465 |
lzma_lz_encoder_memusage(const lzma_lz_options *lz_options) |
| 466 |
{ |
| 467 |
|
| 468 |
lzma_mf mf = { |
| 469 |
.buffer = NULL, |
| 470 |
.hash = NULL, |
| 471 |
.son = NULL, |
| 472 |
.hash_count = 0, |
| 473 |
.sons_count = 0, |
| 474 |
}; |
| 475 |
|
| 476 |
|
| 477 |
if (lz_encoder_prepare(&mf, NULL, lz_options)) |
| 478 |
return UINT64_MAX; |
| 479 |
|
| 480 |
|
| 481 |
return ((uint64_t)(mf.hash_count) + mf.sons_count) * sizeof(uint32_t) |
| 482 |
+ mf.size + sizeof(lzma_coder); |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
static void |
| 487 |
lz_encoder_end(void *coder_ptr, const lzma_allocator *allocator) |
| 488 |
{ |
| 489 |
lzma_coder *coder = coder_ptr; |
| 490 |
|
| 491 |
lzma_next_end(&coder->next, allocator); |
| 492 |
|
| 493 |
lzma_free(coder->mf.son, allocator); |
| 494 |
lzma_free(coder->mf.hash, allocator); |
| 495 |
lzma_free(coder->mf.buffer, allocator); |
| 496 |
|
| 497 |
if (coder->lz.end != NULL) |
| 498 |
coder->lz.end(coder->lz.coder, allocator); |
| 499 |
else |
| 500 |
lzma_free(coder->lz.coder, allocator); |
| 501 |
|
| 502 |
lzma_free(coder, allocator); |
| 503 |
return; |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
static lzma_ret |
| 508 |
lz_encoder_update(void *coder_ptr, const lzma_allocator *allocator, |
| 509 |
const lzma_filter *filters_null lzma_attribute((__unused__)), |
| 510 |
const lzma_filter *reversed_filters) |
| 511 |
{ |
| 512 |
lzma_coder *coder = coder_ptr; |
| 513 |
|
| 514 |
if (coder->lz.options_update == NULL) |
| 515 |
return LZMA_PROG_ERROR; |
| 516 |
|
| 517 |
return_if_error(coder->lz.options_update( |
| 518 |
coder->lz.coder, reversed_filters)); |
| 519 |
|
| 520 |
return lzma_next_filter_update( |
| 521 |
&coder->next, allocator, reversed_filters + 1); |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
static lzma_ret |
| 526 |
lz_encoder_set_out_limit(void *coder_ptr, uint64_t *uncomp_size, |
| 527 |
uint64_t out_limit) |
| 528 |
{ |
| 529 |
lzma_coder *coder = coder_ptr; |
| 530 |
|
| 531 |
|
| 532 |
if (coder->next.code == NULL && coder->lz.set_out_limit != NULL) |
| 533 |
return coder->lz.set_out_limit( |
| 534 |
coder->lz.coder, uncomp_size, out_limit); |
| 535 |
|
| 536 |
return LZMA_OPTIONS_ERROR; |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
extern lzma_ret |
| 541 |
lzma_lz_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator, |
| 542 |
const lzma_filter_info *filters, |
| 543 |
lzma_ret (*lz_init)(lzma_lz_encoder *lz, |
| 544 |
const lzma_allocator *allocator, |
| 545 |
lzma_vli id, const void *options, |
| 546 |
lzma_lz_options *lz_options)) |
| 547 |
{ |
| 548 |
#if defined(HAVE_SMALL) && !defined(HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR) |
| 549 |
|
| 550 |
lzma_crc32_init(); |
| 551 |
#endif |
| 552 |
|
| 553 |
|
| 554 |
lzma_coder *coder = next->coder; |
| 555 |
if (coder == NULL) { |
| 556 |
coder = lzma_alloc(sizeof(lzma_coder), allocator); |
| 557 |
if (coder == NULL) |
| 558 |
return LZMA_MEM_ERROR; |
| 559 |
|
| 560 |
next->coder = coder; |
| 561 |
next->code = &lz_encode; |
| 562 |
next->end = &lz_encoder_end; |
| 563 |
next->update = &lz_encoder_update; |
| 564 |
next->set_out_limit = &lz_encoder_set_out_limit; |
| 565 |
|
| 566 |
coder->lz.coder = NULL; |
| 567 |
coder->lz.code = NULL; |
| 568 |
coder->lz.end = NULL; |
| 569 |
coder->lz.options_update = NULL; |
| 570 |
coder->lz.set_out_limit = NULL; |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
coder->mf.buffer = NULL; |
| 576 |
coder->mf.size = 0; |
| 577 |
coder->mf.hash = NULL; |
| 578 |
coder->mf.son = NULL; |
| 579 |
coder->mf.hash_count = 0; |
| 580 |
coder->mf.sons_count = 0; |
| 581 |
|
| 582 |
coder->next = LZMA_NEXT_CODER_INIT; |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
lzma_lz_options lz_options; |
| 587 |
return_if_error(lz_init(&coder->lz, allocator, |
| 588 |
filters[0].id, filters[0].options, &lz_options)); |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
if (lz_encoder_prepare(&coder->mf, allocator, &lz_options)) |
| 593 |
return LZMA_OPTIONS_ERROR; |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
if (lz_encoder_init(&coder->mf, allocator, &lz_options)) |
| 598 |
return LZMA_MEM_ERROR; |
| 599 |
|
| 600 |
|
| 601 |
return lzma_next_filter_init(&coder->next, allocator, filters + 1); |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
extern LZMA_API(lzma_bool) |
| 606 |
lzma_mf_is_supported(lzma_match_finder mf) |
| 607 |
{ |
| 608 |
switch (mf) { |
| 609 |
#ifdef HAVE_MF_HC3 |
| 610 |
case LZMA_MF_HC3: |
| 611 |
return true; |
| 612 |
#endif |
| 613 |
#ifdef HAVE_MF_HC4 |
| 614 |
case LZMA_MF_HC4: |
| 615 |
return true; |
| 616 |
#endif |
| 617 |
#ifdef HAVE_MF_BT2 |
| 618 |
case LZMA_MF_BT2: |
| 619 |
return true; |
| 620 |
#endif |
| 621 |
#ifdef HAVE_MF_BT3 |
| 622 |
case LZMA_MF_BT3: |
| 623 |
return true; |
| 624 |
#endif |
| 625 |
#ifdef HAVE_MF_BT4 |
| 626 |
case LZMA_MF_BT4: |
| 627 |
return true; |
| 628 |
#endif |
| 629 |
default: |
| 630 |
return false; |
| 631 |
} |
| 632 |
} |