| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#include "lzma2_encoder.h" |
| 14 |
#include "lzma_encoder_private.h" |
| 15 |
#include "fastpos.h" |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
static inline void |
| 23 |
literal_matched(lzma_range_encoder *rc, probability *subcoder, |
| 24 |
uint32_t match_byte, uint32_t symbol) |
| 25 |
{ |
| 26 |
uint32_t offset = 0x100; |
| 27 |
symbol += UINT32_C(1) << 8; |
| 28 |
|
| 29 |
do { |
| 30 |
match_byte <<= 1; |
| 31 |
const uint32_t match_bit = match_byte & offset; |
| 32 |
const uint32_t subcoder_index |
| 33 |
= offset + match_bit + (symbol >> 8); |
| 34 |
const uint32_t bit = (symbol >> 7) & 1; |
| 35 |
rc_bit(rc, &subcoder[subcoder_index], bit); |
| 36 |
|
| 37 |
symbol <<= 1; |
| 38 |
offset &= ~(match_byte ^ symbol); |
| 39 |
|
| 40 |
} while (symbol < (UINT32_C(1) << 16)); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
static inline void |
| 45 |
literal(lzma_lzma1_encoder *coder, lzma_mf *mf, uint32_t position) |
| 46 |
{ |
| 47 |
|
| 48 |
const uint8_t cur_byte = mf->buffer[ |
| 49 |
mf->read_pos - mf->read_ahead]; |
| 50 |
probability *subcoder = literal_subcoder(coder->literal, |
| 51 |
coder->literal_context_bits, coder->literal_mask, |
| 52 |
position, mf->buffer[mf->read_pos - mf->read_ahead - 1]); |
| 53 |
|
| 54 |
if (is_literal_state(coder->state)) { |
| 55 |
|
| 56 |
|
| 57 |
update_literal_normal(coder->state); |
| 58 |
rc_bittree(&coder->rc, subcoder, 8, cur_byte); |
| 59 |
} else { |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
update_literal_matched(coder->state); |
| 64 |
const uint8_t match_byte = mf->buffer[ |
| 65 |
mf->read_pos - coder->reps[0] - 1 |
| 66 |
- mf->read_ahead]; |
| 67 |
literal_matched(&coder->rc, subcoder, match_byte, cur_byte); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
static void |
| 77 |
length_update_prices(lzma_length_encoder *lc, const uint32_t pos_state) |
| 78 |
{ |
| 79 |
const uint32_t table_size = lc->table_size; |
| 80 |
lc->counters[pos_state] = table_size; |
| 81 |
|
| 82 |
const uint32_t a0 = rc_bit_0_price(lc->choice); |
| 83 |
const uint32_t a1 = rc_bit_1_price(lc->choice); |
| 84 |
const uint32_t b0 = a1 + rc_bit_0_price(lc->choice2); |
| 85 |
const uint32_t b1 = a1 + rc_bit_1_price(lc->choice2); |
| 86 |
uint32_t *const prices = lc->prices[pos_state]; |
| 87 |
|
| 88 |
uint32_t i; |
| 89 |
for (i = 0; i < table_size && i < LEN_LOW_SYMBOLS; ++i) |
| 90 |
prices[i] = a0 + rc_bittree_price(lc->low[pos_state], |
| 91 |
LEN_LOW_BITS, i); |
| 92 |
|
| 93 |
for (; i < table_size && i < LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS; ++i) |
| 94 |
prices[i] = b0 + rc_bittree_price(lc->mid[pos_state], |
| 95 |
LEN_MID_BITS, i - LEN_LOW_SYMBOLS); |
| 96 |
|
| 97 |
for (; i < table_size; ++i) |
| 98 |
prices[i] = b1 + rc_bittree_price(lc->high, LEN_HIGH_BITS, |
| 99 |
i - LEN_LOW_SYMBOLS - LEN_MID_SYMBOLS); |
| 100 |
|
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
static inline void |
| 106 |
length(lzma_range_encoder *rc, lzma_length_encoder *lc, |
| 107 |
const uint32_t pos_state, uint32_t len, const bool fast_mode) |
| 108 |
{ |
| 109 |
assert(len <= MATCH_LEN_MAX); |
| 110 |
len -= MATCH_LEN_MIN; |
| 111 |
|
| 112 |
if (len < LEN_LOW_SYMBOLS) { |
| 113 |
rc_bit(rc, &lc->choice, 0); |
| 114 |
rc_bittree(rc, lc->low[pos_state], LEN_LOW_BITS, len); |
| 115 |
} else { |
| 116 |
rc_bit(rc, &lc->choice, 1); |
| 117 |
len -= LEN_LOW_SYMBOLS; |
| 118 |
|
| 119 |
if (len < LEN_MID_SYMBOLS) { |
| 120 |
rc_bit(rc, &lc->choice2, 0); |
| 121 |
rc_bittree(rc, lc->mid[pos_state], LEN_MID_BITS, len); |
| 122 |
} else { |
| 123 |
rc_bit(rc, &lc->choice2, 1); |
| 124 |
len -= LEN_MID_SYMBOLS; |
| 125 |
rc_bittree(rc, lc->high, LEN_HIGH_BITS, len); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
if (!fast_mode) |
| 132 |
if (--lc->counters[pos_state] == 0) |
| 133 |
length_update_prices(lc, pos_state); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
static inline void |
| 142 |
match(lzma_lzma1_encoder *coder, const uint32_t pos_state, |
| 143 |
const uint32_t distance, const uint32_t len) |
| 144 |
{ |
| 145 |
update_match(coder->state); |
| 146 |
|
| 147 |
length(&coder->rc, &coder->match_len_encoder, pos_state, len, |
| 148 |
coder->fast_mode); |
| 149 |
|
| 150 |
const uint32_t dist_slot = get_dist_slot(distance); |
| 151 |
const uint32_t dist_state = get_dist_state(len); |
| 152 |
rc_bittree(&coder->rc, coder->dist_slot[dist_state], |
| 153 |
DIST_SLOT_BITS, dist_slot); |
| 154 |
|
| 155 |
if (dist_slot >= DIST_MODEL_START) { |
| 156 |
const uint32_t footer_bits = (dist_slot >> 1) - 1; |
| 157 |
const uint32_t base = (2 | (dist_slot & 1)) << footer_bits; |
| 158 |
const uint32_t dist_reduced = distance - base; |
| 159 |
|
| 160 |
if (dist_slot < DIST_MODEL_END) { |
| 161 |
|
| 162 |
|
| 163 |
rc_bittree_reverse(&coder->rc, |
| 164 |
coder->dist_special + base - dist_slot - 1, |
| 165 |
footer_bits, dist_reduced); |
| 166 |
} else { |
| 167 |
rc_direct(&coder->rc, dist_reduced >> ALIGN_BITS, |
| 168 |
footer_bits - ALIGN_BITS); |
| 169 |
rc_bittree_reverse( |
| 170 |
&coder->rc, coder->dist_align, |
| 171 |
ALIGN_BITS, dist_reduced & ALIGN_MASK); |
| 172 |
++coder->align_price_count; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
coder->reps[3] = coder->reps[2]; |
| 177 |
coder->reps[2] = coder->reps[1]; |
| 178 |
coder->reps[1] = coder->reps[0]; |
| 179 |
coder->reps[0] = distance; |
| 180 |
++coder->match_price_count; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
static inline void |
| 189 |
rep_match(lzma_lzma1_encoder *coder, const uint32_t pos_state, |
| 190 |
const uint32_t rep, const uint32_t len) |
| 191 |
{ |
| 192 |
if (rep == 0) { |
| 193 |
rc_bit(&coder->rc, &coder->is_rep0[coder->state], 0); |
| 194 |
rc_bit(&coder->rc, |
| 195 |
&coder->is_rep0_long[coder->state][pos_state], |
| 196 |
len != 1); |
| 197 |
} else { |
| 198 |
const uint32_t distance = coder->reps[rep]; |
| 199 |
rc_bit(&coder->rc, &coder->is_rep0[coder->state], 1); |
| 200 |
|
| 201 |
if (rep == 1) { |
| 202 |
rc_bit(&coder->rc, &coder->is_rep1[coder->state], 0); |
| 203 |
} else { |
| 204 |
rc_bit(&coder->rc, &coder->is_rep1[coder->state], 1); |
| 205 |
rc_bit(&coder->rc, &coder->is_rep2[coder->state], |
| 206 |
rep - 2); |
| 207 |
|
| 208 |
if (rep == 3) |
| 209 |
coder->reps[3] = coder->reps[2]; |
| 210 |
|
| 211 |
coder->reps[2] = coder->reps[1]; |
| 212 |
} |
| 213 |
|
| 214 |
coder->reps[1] = coder->reps[0]; |
| 215 |
coder->reps[0] = distance; |
| 216 |
} |
| 217 |
|
| 218 |
if (len == 1) { |
| 219 |
update_short_rep(coder->state); |
| 220 |
} else { |
| 221 |
length(&coder->rc, &coder->rep_len_encoder, pos_state, len, |
| 222 |
coder->fast_mode); |
| 223 |
update_long_rep(coder->state); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
static void |
| 233 |
encode_symbol(lzma_lzma1_encoder *coder, lzma_mf *mf, |
| 234 |
uint32_t back, uint32_t len, uint32_t position) |
| 235 |
{ |
| 236 |
const uint32_t pos_state = position & coder->pos_mask; |
| 237 |
|
| 238 |
if (back == UINT32_MAX) { |
| 239 |
|
| 240 |
assert(len == 1); |
| 241 |
rc_bit(&coder->rc, |
| 242 |
&coder->is_match[coder->state][pos_state], 0); |
| 243 |
literal(coder, mf, position); |
| 244 |
} else { |
| 245 |
|
| 246 |
rc_bit(&coder->rc, |
| 247 |
&coder->is_match[coder->state][pos_state], 1); |
| 248 |
|
| 249 |
if (back < REPS) { |
| 250 |
|
| 251 |
|
| 252 |
rc_bit(&coder->rc, &coder->is_rep[coder->state], 1); |
| 253 |
rep_match(coder, pos_state, back, len); |
| 254 |
} else { |
| 255 |
|
| 256 |
rc_bit(&coder->rc, &coder->is_rep[coder->state], 0); |
| 257 |
match(coder, pos_state, back - REPS, len); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
assert(mf->read_ahead >= len); |
| 262 |
mf->read_ahead -= len; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
static bool |
| 267 |
encode_init(lzma_lzma1_encoder *coder, lzma_mf *mf) |
| 268 |
{ |
| 269 |
assert(mf_position(mf) == 0); |
| 270 |
assert(coder->uncomp_size == 0); |
| 271 |
|
| 272 |
if (mf->read_pos == mf->read_limit) { |
| 273 |
if (mf->action == LZMA_RUN) |
| 274 |
return false; |
| 275 |
|
| 276 |
|
| 277 |
assert(mf->write_pos == mf->read_pos); |
| 278 |
assert(mf->action == LZMA_FINISH); |
| 279 |
} else { |
| 280 |
|
| 281 |
|
| 282 |
mf_skip(mf, 1); |
| 283 |
mf->read_ahead = 0; |
| 284 |
rc_bit(&coder->rc, &coder->is_match[0][0], 0); |
| 285 |
rc_bittree(&coder->rc, coder->literal + 0, 8, mf->buffer[0]); |
| 286 |
++coder->uncomp_size; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
coder->is_initialized = true; |
| 291 |
|
| 292 |
return true; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
static void |
| 297 |
encode_eopm(lzma_lzma1_encoder *coder, uint32_t position) |
| 298 |
{ |
| 299 |
const uint32_t pos_state = position & coder->pos_mask; |
| 300 |
rc_bit(&coder->rc, &coder->is_match[coder->state][pos_state], 1); |
| 301 |
rc_bit(&coder->rc, &coder->is_rep[coder->state], 0); |
| 302 |
match(coder, pos_state, UINT32_MAX, MATCH_LEN_MIN); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
#define LOOP_INPUT_MAX (OPTS + 1) |
| 310 |
|
| 311 |
|
| 312 |
extern lzma_ret |
| 313 |
lzma_lzma_encode(lzma_lzma1_encoder *restrict coder, lzma_mf *restrict mf, |
| 314 |
uint8_t *restrict out, size_t *restrict out_pos, |
| 315 |
size_t out_size, uint32_t limit) |
| 316 |
{ |
| 317 |
|
| 318 |
if (!coder->is_initialized && !encode_init(coder, mf)) |
| 319 |
return LZMA_OK; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
if (rc_encode(&coder->rc, out, out_pos, out_size)) { |
| 327 |
|
| 328 |
assert(limit == UINT32_MAX); |
| 329 |
return LZMA_OK; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
if (coder->is_flushed) { |
| 338 |
assert(limit == UINT32_MAX); |
| 339 |
return LZMA_STREAM_END; |
| 340 |
} |
| 341 |
|
| 342 |
while (true) { |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
if (limit != UINT32_MAX |
| 347 |
&& (mf->read_pos - mf->read_ahead >= limit |
| 348 |
|| *out_pos + rc_pending(&coder->rc) |
| 349 |
>= LZMA2_CHUNK_MAX |
| 350 |
- LOOP_INPUT_MAX)) |
| 351 |
break; |
| 352 |
|
| 353 |
|
| 354 |
if (mf->read_pos >= mf->read_limit) { |
| 355 |
if (mf->action == LZMA_RUN) |
| 356 |
return LZMA_OK; |
| 357 |
|
| 358 |
if (mf->read_ahead == 0) |
| 359 |
break; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
uint32_t len; |
| 371 |
uint32_t back; |
| 372 |
|
| 373 |
if (coder->fast_mode) |
| 374 |
lzma_lzma_optimum_fast(coder, mf, &back, &len); |
| 375 |
else |
| 376 |
lzma_lzma_optimum_normal(coder, mf, &back, &len, |
| 377 |
(uint32_t)(coder->uncomp_size)); |
| 378 |
|
| 379 |
encode_symbol(coder, mf, back, len, |
| 380 |
(uint32_t)(coder->uncomp_size)); |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
if (coder->out_limit != 0 && rc_encode_dummy( |
| 386 |
&coder->rc, coder->out_limit)) { |
| 387 |
|
| 388 |
|
| 389 |
rc_forget(&coder->rc); |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
break; |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
coder->uncomp_size += len; |
| 401 |
|
| 402 |
|
| 403 |
if (rc_encode(&coder->rc, out, out_pos, out_size)) { |
| 404 |
|
| 405 |
assert(limit == UINT32_MAX); |
| 406 |
return LZMA_OK; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
if (coder->uncomp_size_ptr != NULL) |
| 412 |
*coder->uncomp_size_ptr = coder->uncomp_size; |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
if (coder->use_eopm) |
| 419 |
encode_eopm(coder, (uint32_t)(coder->uncomp_size)); |
| 420 |
|
| 421 |
|
| 422 |
rc_flush(&coder->rc); |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
if (rc_encode(&coder->rc, out, out_pos, out_size)) { |
| 428 |
|
| 429 |
assert(limit == UINT32_MAX); |
| 430 |
|
| 431 |
coder->is_flushed = true; |
| 432 |
return LZMA_OK; |
| 433 |
} |
| 434 |
|
| 435 |
return LZMA_STREAM_END; |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
static lzma_ret |
| 440 |
lzma_encode(void *coder, lzma_mf *restrict mf, |
| 441 |
uint8_t *restrict out, size_t *restrict out_pos, |
| 442 |
size_t out_size) |
| 443 |
{ |
| 444 |
|
| 445 |
if (unlikely(mf->action == LZMA_SYNC_FLUSH)) |
| 446 |
return LZMA_OPTIONS_ERROR; |
| 447 |
|
| 448 |
return lzma_lzma_encode(coder, mf, out, out_pos, out_size, UINT32_MAX); |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
static lzma_ret |
| 453 |
lzma_lzma_set_out_limit( |
| 454 |
void *coder_ptr, uint64_t *uncomp_size, uint64_t out_limit) |
| 455 |
{ |
| 456 |
|
| 457 |
|
| 458 |
if (out_limit < 6) |
| 459 |
return LZMA_BUF_ERROR; |
| 460 |
|
| 461 |
lzma_lzma1_encoder *coder = coder_ptr; |
| 462 |
coder->out_limit = out_limit; |
| 463 |
coder->uncomp_size_ptr = uncomp_size; |
| 464 |
coder->use_eopm = false; |
| 465 |
return LZMA_OK; |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
static bool |
| 474 |
is_options_valid(const lzma_options_lzma *options) |
| 475 |
{ |
| 476 |
|
| 477 |
|
| 478 |
return is_lclppb_valid(options) |
| 479 |
&& options->nice_len >= MATCH_LEN_MIN |
| 480 |
&& options->nice_len <= MATCH_LEN_MAX |
| 481 |
&& (options->mode == LZMA_MODE_FAST |
| 482 |
|| options->mode == LZMA_MODE_NORMAL); |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
static void |
| 487 |
set_lz_options(lzma_lz_options *lz_options, const lzma_options_lzma *options) |
| 488 |
{ |
| 489 |
|
| 490 |
|
| 491 |
lz_options->before_size = OPTS; |
| 492 |
lz_options->dict_size = options->dict_size; |
| 493 |
lz_options->after_size = LOOP_INPUT_MAX; |
| 494 |
lz_options->match_len_max = MATCH_LEN_MAX; |
| 495 |
lz_options->nice_len = my_max(mf_get_hash_bytes(options->mf), |
| 496 |
options->nice_len); |
| 497 |
lz_options->match_finder = options->mf; |
| 498 |
lz_options->depth = options->depth; |
| 499 |
lz_options->preset_dict = options->preset_dict; |
| 500 |
lz_options->preset_dict_size = options->preset_dict_size; |
| 501 |
return; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
static void |
| 506 |
length_encoder_reset(lzma_length_encoder *lencoder, |
| 507 |
const uint32_t num_pos_states, const bool fast_mode) |
| 508 |
{ |
| 509 |
bit_reset(lencoder->choice); |
| 510 |
bit_reset(lencoder->choice2); |
| 511 |
|
| 512 |
for (size_t pos_state = 0; pos_state < num_pos_states; ++pos_state) { |
| 513 |
bittree_reset(lencoder->low[pos_state], LEN_LOW_BITS); |
| 514 |
bittree_reset(lencoder->mid[pos_state], LEN_MID_BITS); |
| 515 |
} |
| 516 |
|
| 517 |
bittree_reset(lencoder->high, LEN_HIGH_BITS); |
| 518 |
|
| 519 |
if (!fast_mode) |
| 520 |
for (uint32_t pos_state = 0; pos_state < num_pos_states; |
| 521 |
++pos_state) |
| 522 |
length_update_prices(lencoder, pos_state); |
| 523 |
|
| 524 |
return; |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
extern lzma_ret |
| 529 |
lzma_lzma_encoder_reset(lzma_lzma1_encoder *coder, |
| 530 |
const lzma_options_lzma *options) |
| 531 |
{ |
| 532 |
if (!is_options_valid(options)) |
| 533 |
return LZMA_OPTIONS_ERROR; |
| 534 |
|
| 535 |
coder->pos_mask = (1U << options->pb) - 1; |
| 536 |
coder->literal_context_bits = options->lc; |
| 537 |
coder->literal_mask = literal_mask_calc(options->lc, options->lp); |
| 538 |
|
| 539 |
|
| 540 |
rc_reset(&coder->rc); |
| 541 |
|
| 542 |
|
| 543 |
coder->state = STATE_LIT_LIT; |
| 544 |
for (size_t i = 0; i < REPS; ++i) |
| 545 |
coder->reps[i] = 0; |
| 546 |
|
| 547 |
literal_init(coder->literal, options->lc, options->lp); |
| 548 |
|
| 549 |
|
| 550 |
for (size_t i = 0; i < STATES; ++i) { |
| 551 |
for (size_t j = 0; j <= coder->pos_mask; ++j) { |
| 552 |
bit_reset(coder->is_match[i][j]); |
| 553 |
bit_reset(coder->is_rep0_long[i][j]); |
| 554 |
} |
| 555 |
|
| 556 |
bit_reset(coder->is_rep[i]); |
| 557 |
bit_reset(coder->is_rep0[i]); |
| 558 |
bit_reset(coder->is_rep1[i]); |
| 559 |
bit_reset(coder->is_rep2[i]); |
| 560 |
} |
| 561 |
|
| 562 |
for (size_t i = 0; i < FULL_DISTANCES - DIST_MODEL_END; ++i) |
| 563 |
bit_reset(coder->dist_special[i]); |
| 564 |
|
| 565 |
|
| 566 |
for (size_t i = 0; i < DIST_STATES; ++i) |
| 567 |
bittree_reset(coder->dist_slot[i], DIST_SLOT_BITS); |
| 568 |
|
| 569 |
bittree_reset(coder->dist_align, ALIGN_BITS); |
| 570 |
|
| 571 |
|
| 572 |
length_encoder_reset(&coder->match_len_encoder, |
| 573 |
1U << options->pb, coder->fast_mode); |
| 574 |
|
| 575 |
length_encoder_reset(&coder->rep_len_encoder, |
| 576 |
1U << options->pb, coder->fast_mode); |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
coder->match_price_count = UINT32_MAX / 2; |
| 592 |
coder->align_price_count = UINT32_MAX / 2; |
| 593 |
|
| 594 |
coder->opts_end_index = 0; |
| 595 |
coder->opts_current_index = 0; |
| 596 |
|
| 597 |
return LZMA_OK; |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
extern lzma_ret |
| 602 |
lzma_lzma_encoder_create(void **coder_ptr, const lzma_allocator *allocator, |
| 603 |
lzma_vli id, const lzma_options_lzma *options, |
| 604 |
lzma_lz_options *lz_options) |
| 605 |
{ |
| 606 |
assert(id == LZMA_FILTER_LZMA1 || id == LZMA_FILTER_LZMA1EXT |
| 607 |
|| id == LZMA_FILTER_LZMA2); |
| 608 |
|
| 609 |
|
| 610 |
if (*coder_ptr == NULL) { |
| 611 |
*coder_ptr = lzma_alloc(sizeof(lzma_lzma1_encoder), allocator); |
| 612 |
if (*coder_ptr == NULL) |
| 613 |
return LZMA_MEM_ERROR; |
| 614 |
} |
| 615 |
|
| 616 |
lzma_lzma1_encoder *coder = *coder_ptr; |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
switch (options->mode) { |
| 622 |
case LZMA_MODE_FAST: |
| 623 |
coder->fast_mode = true; |
| 624 |
break; |
| 625 |
|
| 626 |
case LZMA_MODE_NORMAL: { |
| 627 |
coder->fast_mode = false; |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
if (options->dict_size > (UINT32_C(1) << 30) |
| 640 |
+ (UINT32_C(1) << 29)) |
| 641 |
return LZMA_OPTIONS_ERROR; |
| 642 |
|
| 643 |
uint32_t log_size = 0; |
| 644 |
while ((UINT32_C(1) << log_size) < options->dict_size) |
| 645 |
++log_size; |
| 646 |
|
| 647 |
coder->dist_table_size = log_size * 2; |
| 648 |
|
| 649 |
|
| 650 |
const uint32_t nice_len = my_max( |
| 651 |
mf_get_hash_bytes(options->mf), |
| 652 |
options->nice_len); |
| 653 |
|
| 654 |
coder->match_len_encoder.table_size |
| 655 |
= nice_len + 1 - MATCH_LEN_MIN; |
| 656 |
coder->rep_len_encoder.table_size |
| 657 |
= nice_len + 1 - MATCH_LEN_MIN; |
| 658 |
break; |
| 659 |
} |
| 660 |
|
| 661 |
default: |
| 662 |
return LZMA_OPTIONS_ERROR; |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
coder->is_initialized = options->preset_dict != NULL |
| 670 |
&& options->preset_dict_size > 0; |
| 671 |
coder->is_flushed = false; |
| 672 |
coder->uncomp_size = 0; |
| 673 |
coder->uncomp_size_ptr = NULL; |
| 674 |
|
| 675 |
|
| 676 |
coder->out_limit = 0; |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
coder->use_eopm = (id == LZMA_FILTER_LZMA1); |
| 684 |
if (id == LZMA_FILTER_LZMA1EXT) { |
| 685 |
|
| 686 |
if (options->ext_flags & ~LZMA_LZMA1EXT_ALLOW_EOPM) |
| 687 |
return LZMA_OPTIONS_ERROR; |
| 688 |
|
| 689 |
coder->use_eopm = (options->ext_flags |
| 690 |
& LZMA_LZMA1EXT_ALLOW_EOPM) != 0; |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
} |
| 703 |
|
| 704 |
set_lz_options(lz_options, options); |
| 705 |
|
| 706 |
return lzma_lzma_encoder_reset(coder, options); |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
static lzma_ret |
| 711 |
lzma_encoder_init(lzma_lz_encoder *lz, const lzma_allocator *allocator, |
| 712 |
lzma_vli id, const void *options, lzma_lz_options *lz_options) |
| 713 |
{ |
| 714 |
if (options == NULL) |
| 715 |
return LZMA_PROG_ERROR; |
| 716 |
|
| 717 |
lz->code = &lzma_encode; |
| 718 |
lz->set_out_limit = &lzma_lzma_set_out_limit; |
| 719 |
return lzma_lzma_encoder_create( |
| 720 |
&lz->coder, allocator, id, options, lz_options); |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
extern lzma_ret |
| 725 |
lzma_lzma_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator, |
| 726 |
const lzma_filter_info *filters) |
| 727 |
{ |
| 728 |
return lzma_lz_encoder_init( |
| 729 |
next, allocator, filters, &lzma_encoder_init); |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
extern uint64_t |
| 734 |
lzma_lzma_encoder_memusage(const void *options) |
| 735 |
{ |
| 736 |
if (!is_options_valid(options)) |
| 737 |
return UINT64_MAX; |
| 738 |
|
| 739 |
lzma_lz_options lz_options; |
| 740 |
set_lz_options(&lz_options, options); |
| 741 |
|
| 742 |
const uint64_t lz_memusage = lzma_lz_encoder_memusage(&lz_options); |
| 743 |
if (lz_memusage == UINT64_MAX) |
| 744 |
return UINT64_MAX; |
| 745 |
|
| 746 |
return (uint64_t)(sizeof(lzma_lzma1_encoder)) + lz_memusage; |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
extern bool |
| 751 |
lzma_lzma_lclppb_encode(const lzma_options_lzma *options, uint8_t *byte) |
| 752 |
{ |
| 753 |
if (!is_lclppb_valid(options)) |
| 754 |
return true; |
| 755 |
|
| 756 |
*byte = (options->pb * 5 + options->lp) * 9 + options->lc; |
| 757 |
assert(*byte <= (4 * 5 + 4) * 9 + 8); |
| 758 |
|
| 759 |
return false; |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
#ifdef HAVE_ENCODER_LZMA1 |
| 764 |
extern lzma_ret |
| 765 |
lzma_lzma_props_encode(const void *options, uint8_t *out) |
| 766 |
{ |
| 767 |
if (options == NULL) |
| 768 |
return LZMA_PROG_ERROR; |
| 769 |
|
| 770 |
const lzma_options_lzma *const opt = options; |
| 771 |
|
| 772 |
if (lzma_lzma_lclppb_encode(opt, out)) |
| 773 |
return LZMA_PROG_ERROR; |
| 774 |
|
| 775 |
write32le(out + 1, opt->dict_size); |
| 776 |
|
| 777 |
return LZMA_OK; |
| 778 |
} |
| 779 |
#endif |
| 780 |
|
| 781 |
|
| 782 |
extern LZMA_API(lzma_bool) |
| 783 |
lzma_mode_is_supported(lzma_mode mode) |
| 784 |
{ |
| 785 |
return mode == LZMA_MODE_FAST || mode == LZMA_MODE_NORMAL; |
| 786 |
} |