lzma_encoder.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file lzma_encoder.c
  4. /// \brief LZMA encoder
  5. ///
  6. // Authors: Igor Pavlov
  7. // Lasse Collin
  8. //
  9. // This file has been put into the public domain.
  10. // You can do whatever you want with this file.
  11. //
  12. ///////////////////////////////////////////////////////////////////////////////
  13. #include "lzma2_encoder.h"
  14. #include "lzma_encoder_private.h"
  15. #include "fastpos.h"
  16. /////////////
  17. // Literal //
  18. /////////////
  19. static inline void
  20. literal_matched(lzma_range_encoder *rc, probability *subcoder,
  21. uint32_t match_byte, uint32_t symbol)
  22. {
  23. uint32_t offset = 0x100;
  24. symbol += UINT32_C(1) << 8;
  25. do {
  26. match_byte <<= 1;
  27. const uint32_t match_bit = match_byte & offset;
  28. const uint32_t subcoder_index
  29. = offset + match_bit + (symbol >> 8);
  30. const uint32_t bit = (symbol >> 7) & 1;
  31. rc_bit(rc, &subcoder[subcoder_index], bit);
  32. symbol <<= 1;
  33. offset &= ~(match_byte ^ symbol);
  34. } while (symbol < (UINT32_C(1) << 16));
  35. }
  36. static inline void
  37. literal(lzma_lzma1_encoder *coder, lzma_mf *mf, uint32_t position)
  38. {
  39. // Locate the literal byte to be encoded and the subcoder.
  40. const uint8_t cur_byte = mf->buffer[
  41. mf->read_pos - mf->read_ahead];
  42. probability *subcoder = literal_subcoder(coder->literal,
  43. coder->literal_context_bits, coder->literal_pos_mask,
  44. position, mf->buffer[mf->read_pos - mf->read_ahead - 1]);
  45. if (is_literal_state(coder->state)) {
  46. // Previous LZMA-symbol was a literal. Encode a normal
  47. // literal without a match byte.
  48. rc_bittree(&coder->rc, subcoder, 8, cur_byte);
  49. } else {
  50. // Previous LZMA-symbol was a match. Use the last byte of
  51. // the match as a "match byte". That is, compare the bits
  52. // of the current literal and the match byte.
  53. const uint8_t match_byte = mf->buffer[
  54. mf->read_pos - coder->reps[0] - 1
  55. - mf->read_ahead];
  56. literal_matched(&coder->rc, subcoder, match_byte, cur_byte);
  57. }
  58. update_literal(coder->state);
  59. }
  60. //////////////////
  61. // Match length //
  62. //////////////////
  63. static void
  64. length_update_prices(lzma_length_encoder *lc, const uint32_t pos_state)
  65. {
  66. const uint32_t table_size = lc->table_size;
  67. lc->counters[pos_state] = table_size;
  68. const uint32_t a0 = rc_bit_0_price(lc->choice);
  69. const uint32_t a1 = rc_bit_1_price(lc->choice);
  70. const uint32_t b0 = a1 + rc_bit_0_price(lc->choice2);
  71. const uint32_t b1 = a1 + rc_bit_1_price(lc->choice2);
  72. uint32_t *const prices = lc->prices[pos_state];
  73. uint32_t i;
  74. for (i = 0; i < table_size && i < LEN_LOW_SYMBOLS; ++i)
  75. prices[i] = a0 + rc_bittree_price(lc->low[pos_state],
  76. LEN_LOW_BITS, i);
  77. for (; i < table_size && i < LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS; ++i)
  78. prices[i] = b0 + rc_bittree_price(lc->mid[pos_state],
  79. LEN_MID_BITS, i - LEN_LOW_SYMBOLS);
  80. for (; i < table_size; ++i)
  81. prices[i] = b1 + rc_bittree_price(lc->high, LEN_HIGH_BITS,
  82. i - LEN_LOW_SYMBOLS - LEN_MID_SYMBOLS);
  83. return;
  84. }
  85. static inline void
  86. length(lzma_range_encoder *rc, lzma_length_encoder *lc,
  87. const uint32_t pos_state, uint32_t len, const bool fast_mode)
  88. {
  89. assert(len <= MATCH_LEN_MAX);
  90. len -= MATCH_LEN_MIN;
  91. if (len < LEN_LOW_SYMBOLS) {
  92. rc_bit(rc, &lc->choice, 0);
  93. rc_bittree(rc, lc->low[pos_state], LEN_LOW_BITS, len);
  94. } else {
  95. rc_bit(rc, &lc->choice, 1);
  96. len -= LEN_LOW_SYMBOLS;
  97. if (len < LEN_MID_SYMBOLS) {
  98. rc_bit(rc, &lc->choice2, 0);
  99. rc_bittree(rc, lc->mid[pos_state], LEN_MID_BITS, len);
  100. } else {
  101. rc_bit(rc, &lc->choice2, 1);
  102. len -= LEN_MID_SYMBOLS;
  103. rc_bittree(rc, lc->high, LEN_HIGH_BITS, len);
  104. }
  105. }
  106. // Only getoptimum uses the prices so don't update the table when
  107. // in fast mode.
  108. if (!fast_mode)
  109. if (--lc->counters[pos_state] == 0)
  110. length_update_prices(lc, pos_state);
  111. }
  112. ///////////
  113. // Match //
  114. ///////////
  115. static inline void
  116. match(lzma_lzma1_encoder *coder, const uint32_t pos_state,
  117. const uint32_t distance, const uint32_t len)
  118. {
  119. update_match(coder->state);
  120. length(&coder->rc, &coder->match_len_encoder, pos_state, len,
  121. coder->fast_mode);
  122. const uint32_t dist_slot = get_dist_slot(distance);
  123. const uint32_t dist_state = get_dist_state(len);
  124. rc_bittree(&coder->rc, coder->dist_slot[dist_state],
  125. DIST_SLOT_BITS, dist_slot);
  126. if (dist_slot >= DIST_MODEL_START) {
  127. const uint32_t footer_bits = (dist_slot >> 1) - 1;
  128. const uint32_t base = (2 | (dist_slot & 1)) << footer_bits;
  129. const uint32_t dist_reduced = distance - base;
  130. if (dist_slot < DIST_MODEL_END) {
  131. // Careful here: base - dist_slot - 1 can be -1, but
  132. // rc_bittree_reverse starts at probs[1], not probs[0].
  133. rc_bittree_reverse(&coder->rc,
  134. coder->dist_special + base - dist_slot - 1,
  135. footer_bits, dist_reduced);
  136. } else {
  137. rc_direct(&coder->rc, dist_reduced >> ALIGN_BITS,
  138. footer_bits - ALIGN_BITS);
  139. rc_bittree_reverse(
  140. &coder->rc, coder->dist_align,
  141. ALIGN_BITS, dist_reduced & ALIGN_MASK);
  142. ++coder->align_price_count;
  143. }
  144. }
  145. coder->reps[3] = coder->reps[2];
  146. coder->reps[2] = coder->reps[1];
  147. coder->reps[1] = coder->reps[0];
  148. coder->reps[0] = distance;
  149. ++coder->match_price_count;
  150. }
  151. ////////////////////
  152. // Repeated match //
  153. ////////////////////
  154. static inline void
  155. rep_match(lzma_lzma1_encoder *coder, const uint32_t pos_state,
  156. const uint32_t rep, const uint32_t len)
  157. {
  158. if (rep == 0) {
  159. rc_bit(&coder->rc, &coder->is_rep0[coder->state], 0);
  160. rc_bit(&coder->rc,
  161. &coder->is_rep0_long[coder->state][pos_state],
  162. len != 1);
  163. } else {
  164. const uint32_t distance = coder->reps[rep];
  165. rc_bit(&coder->rc, &coder->is_rep0[coder->state], 1);
  166. if (rep == 1) {
  167. rc_bit(&coder->rc, &coder->is_rep1[coder->state], 0);
  168. } else {
  169. rc_bit(&coder->rc, &coder->is_rep1[coder->state], 1);
  170. rc_bit(&coder->rc, &coder->is_rep2[coder->state],
  171. rep - 2);
  172. if (rep == 3)
  173. coder->reps[3] = coder->reps[2];
  174. coder->reps[2] = coder->reps[1];
  175. }
  176. coder->reps[1] = coder->reps[0];
  177. coder->reps[0] = distance;
  178. }
  179. if (len == 1) {
  180. update_short_rep(coder->state);
  181. } else {
  182. length(&coder->rc, &coder->rep_len_encoder, pos_state, len,
  183. coder->fast_mode);
  184. update_long_rep(coder->state);
  185. }
  186. }
  187. //////////
  188. // Main //
  189. //////////
  190. static void
  191. encode_symbol(lzma_lzma1_encoder *coder, lzma_mf *mf,
  192. uint32_t back, uint32_t len, uint32_t position)
  193. {
  194. const uint32_t pos_state = position & coder->pos_mask;
  195. if (back == UINT32_MAX) {
  196. // Literal i.e. eight-bit byte
  197. assert(len == 1);
  198. rc_bit(&coder->rc,
  199. &coder->is_match[coder->state][pos_state], 0);
  200. literal(coder, mf, position);
  201. } else {
  202. // Some type of match
  203. rc_bit(&coder->rc,
  204. &coder->is_match[coder->state][pos_state], 1);
  205. if (back < REPS) {
  206. // It's a repeated match i.e. the same distance
  207. // has been used earlier.
  208. rc_bit(&coder->rc, &coder->is_rep[coder->state], 1);
  209. rep_match(coder, pos_state, back, len);
  210. } else {
  211. // Normal match
  212. rc_bit(&coder->rc, &coder->is_rep[coder->state], 0);
  213. match(coder, pos_state, back - REPS, len);
  214. }
  215. }
  216. assert(mf->read_ahead >= len);
  217. mf->read_ahead -= len;
  218. }
  219. static bool
  220. encode_init(lzma_lzma1_encoder *coder, lzma_mf *mf)
  221. {
  222. assert(mf_position(mf) == 0);
  223. if (mf->read_pos == mf->read_limit) {
  224. if (mf->action == LZMA_RUN)
  225. return false; // We cannot do anything.
  226. // We are finishing (we cannot get here when flushing).
  227. assert(mf->write_pos == mf->read_pos);
  228. assert(mf->action == LZMA_FINISH);
  229. } else {
  230. // Do the actual initialization. The first LZMA symbol must
  231. // always be a literal.
  232. mf_skip(mf, 1);
  233. mf->read_ahead = 0;
  234. rc_bit(&coder->rc, &coder->is_match[0][0], 0);
  235. rc_bittree(&coder->rc, coder->literal[0], 8, mf->buffer[0]);
  236. }
  237. // Initialization is done (except if empty file).
  238. coder->is_initialized = true;
  239. return true;
  240. }
  241. static void
  242. encode_eopm(lzma_lzma1_encoder *coder, uint32_t position)
  243. {
  244. const uint32_t pos_state = position & coder->pos_mask;
  245. rc_bit(&coder->rc, &coder->is_match[coder->state][pos_state], 1);
  246. rc_bit(&coder->rc, &coder->is_rep[coder->state], 0);
  247. match(coder, pos_state, UINT32_MAX, MATCH_LEN_MIN);
  248. }
  249. /// Number of bytes that a single encoding loop in lzma_lzma_encode() can
  250. /// consume from the dictionary. This limit comes from lzma_lzma_optimum()
  251. /// and may need to be updated if that function is significantly modified.
  252. #define LOOP_INPUT_MAX (OPTS + 1)
  253. extern lzma_ret
  254. lzma_lzma_encode(lzma_lzma1_encoder *restrict coder, lzma_mf *restrict mf,
  255. uint8_t *restrict out, size_t *restrict out_pos,
  256. size_t out_size, uint32_t limit)
  257. {
  258. // Initialize the stream if no data has been encoded yet.
  259. if (!coder->is_initialized && !encode_init(coder, mf))
  260. return LZMA_OK;
  261. // Get the lowest bits of the uncompressed offset from the LZ layer.
  262. uint32_t position = mf_position(mf);
  263. while (true) {
  264. // Encode pending bits, if any. Calling this before encoding
  265. // the next symbol is needed only with plain LZMA, since
  266. // LZMA2 always provides big enough buffer to flush
  267. // everything out from the range encoder. For the same reason,
  268. // rc_encode() never returns true when this function is used
  269. // as part of LZMA2 encoder.
  270. if (rc_encode(&coder->rc, out, out_pos, out_size)) {
  271. assert(limit == UINT32_MAX);
  272. return LZMA_OK;
  273. }
  274. // With LZMA2 we need to take care that compressed size of
  275. // a chunk doesn't get too big.
  276. // FIXME? Check if this could be improved.
  277. if (limit != UINT32_MAX
  278. && (mf->read_pos - mf->read_ahead >= limit
  279. || *out_pos + rc_pending(&coder->rc)
  280. >= LZMA2_CHUNK_MAX
  281. - LOOP_INPUT_MAX))
  282. break;
  283. // Check that there is some input to process.
  284. if (mf->read_pos >= mf->read_limit) {
  285. if (mf->action == LZMA_RUN)
  286. return LZMA_OK;
  287. if (mf->read_ahead == 0)
  288. break;
  289. }
  290. // Get optimal match (repeat position and length).
  291. // Value ranges for pos:
  292. // - [0, REPS): repeated match
  293. // - [REPS, UINT32_MAX):
  294. // match at (pos - REPS)
  295. // - UINT32_MAX: not a match but a literal
  296. // Value ranges for len:
  297. // - [MATCH_LEN_MIN, MATCH_LEN_MAX]
  298. uint32_t len;
  299. uint32_t back;
  300. if (coder->fast_mode)
  301. lzma_lzma_optimum_fast(coder, mf, &back, &len);
  302. else
  303. lzma_lzma_optimum_normal(
  304. coder, mf, &back, &len, position);
  305. encode_symbol(coder, mf, back, len, position);
  306. position += len;
  307. }
  308. if (!coder->is_flushed) {
  309. coder->is_flushed = true;
  310. // We don't support encoding plain LZMA streams without EOPM,
  311. // and LZMA2 doesn't use EOPM at LZMA level.
  312. if (limit == UINT32_MAX)
  313. encode_eopm(coder, position);
  314. // Flush the remaining bytes from the range encoder.
  315. rc_flush(&coder->rc);
  316. // Copy the remaining bytes to the output buffer. If there
  317. // isn't enough output space, we will copy out the remaining
  318. // bytes on the next call to this function by using
  319. // the rc_encode() call in the encoding loop above.
  320. if (rc_encode(&coder->rc, out, out_pos, out_size)) {
  321. assert(limit == UINT32_MAX);
  322. return LZMA_OK;
  323. }
  324. }
  325. // Make it ready for the next LZMA2 chunk.
  326. coder->is_flushed = false;
  327. return LZMA_STREAM_END;
  328. }
  329. static lzma_ret
  330. lzma_encode(void *coder, lzma_mf *restrict mf,
  331. uint8_t *restrict out, size_t *restrict out_pos,
  332. size_t out_size)
  333. {
  334. // Plain LZMA has no support for sync-flushing.
  335. if (unlikely(mf->action == LZMA_SYNC_FLUSH))
  336. return LZMA_OPTIONS_ERROR;
  337. return lzma_lzma_encode(coder, mf, out, out_pos, out_size, UINT32_MAX);
  338. }
  339. ////////////////////
  340. // Initialization //
  341. ////////////////////
  342. static bool
  343. is_options_valid(const lzma_options_lzma *options)
  344. {
  345. // Validate some of the options. LZ encoder validates nice_len too
  346. // but we need a valid value here earlier.
  347. return is_lclppb_valid(options)
  348. && options->nice_len >= MATCH_LEN_MIN
  349. && options->nice_len <= MATCH_LEN_MAX
  350. && (options->mode == LZMA_MODE_FAST
  351. || options->mode == LZMA_MODE_NORMAL);
  352. }
  353. static void
  354. set_lz_options(lzma_lz_options *lz_options, const lzma_options_lzma *options)
  355. {
  356. // LZ encoder initialization does the validation for these so we
  357. // don't need to validate here.
  358. lz_options->before_size = OPTS;
  359. lz_options->dict_size = options->dict_size;
  360. lz_options->after_size = LOOP_INPUT_MAX;
  361. lz_options->match_len_max = MATCH_LEN_MAX;
  362. lz_options->nice_len = options->nice_len;
  363. lz_options->match_finder = options->mf;
  364. lz_options->depth = options->depth;
  365. lz_options->preset_dict = options->preset_dict;
  366. lz_options->preset_dict_size = options->preset_dict_size;
  367. return;
  368. }
  369. static void
  370. length_encoder_reset(lzma_length_encoder *lencoder,
  371. const uint32_t num_pos_states, const bool fast_mode)
  372. {
  373. bit_reset(lencoder->choice);
  374. bit_reset(lencoder->choice2);
  375. for (size_t pos_state = 0; pos_state < num_pos_states; ++pos_state) {
  376. bittree_reset(lencoder->low[pos_state], LEN_LOW_BITS);
  377. bittree_reset(lencoder->mid[pos_state], LEN_MID_BITS);
  378. }
  379. bittree_reset(lencoder->high, LEN_HIGH_BITS);
  380. if (!fast_mode)
  381. for (uint32_t pos_state = 0; pos_state < num_pos_states;
  382. ++pos_state)
  383. length_update_prices(lencoder, pos_state);
  384. return;
  385. }
  386. extern lzma_ret
  387. lzma_lzma_encoder_reset(lzma_lzma1_encoder *coder,
  388. const lzma_options_lzma *options)
  389. {
  390. if (!is_options_valid(options))
  391. return LZMA_OPTIONS_ERROR;
  392. coder->pos_mask = (1U << options->pb) - 1;
  393. coder->literal_context_bits = options->lc;
  394. coder->literal_pos_mask = (1U << options->lp) - 1;
  395. // Range coder
  396. rc_reset(&coder->rc);
  397. // State
  398. coder->state = STATE_LIT_LIT;
  399. for (size_t i = 0; i < REPS; ++i)
  400. coder->reps[i] = 0;
  401. literal_init(coder->literal, options->lc, options->lp);
  402. // Bit encoders
  403. for (size_t i = 0; i < STATES; ++i) {
  404. for (size_t j = 0; j <= coder->pos_mask; ++j) {
  405. bit_reset(coder->is_match[i][j]);
  406. bit_reset(coder->is_rep0_long[i][j]);
  407. }
  408. bit_reset(coder->is_rep[i]);
  409. bit_reset(coder->is_rep0[i]);
  410. bit_reset(coder->is_rep1[i]);
  411. bit_reset(coder->is_rep2[i]);
  412. }
  413. for (size_t i = 0; i < FULL_DISTANCES - DIST_MODEL_END; ++i)
  414. bit_reset(coder->dist_special[i]);
  415. // Bit tree encoders
  416. for (size_t i = 0; i < DIST_STATES; ++i)
  417. bittree_reset(coder->dist_slot[i], DIST_SLOT_BITS);
  418. bittree_reset(coder->dist_align, ALIGN_BITS);
  419. // Length encoders
  420. length_encoder_reset(&coder->match_len_encoder,
  421. 1U << options->pb, coder->fast_mode);
  422. length_encoder_reset(&coder->rep_len_encoder,
  423. 1U << options->pb, coder->fast_mode);
  424. // Price counts are incremented every time appropriate probabilities
  425. // are changed. price counts are set to zero when the price tables
  426. // are updated, which is done when the appropriate price counts have
  427. // big enough value, and lzma_mf.read_ahead == 0 which happens at
  428. // least every OPTS (a few thousand) possible price count increments.
  429. //
  430. // By resetting price counts to UINT32_MAX / 2, we make sure that the
  431. // price tables will be initialized before they will be used (since
  432. // the value is definitely big enough), and that it is OK to increment
  433. // price counts without risk of integer overflow (since UINT32_MAX / 2
  434. // is small enough). The current code doesn't increment price counts
  435. // before initializing price tables, but it maybe done in future if
  436. // we add support for saving the state between LZMA2 chunks.
  437. coder->match_price_count = UINT32_MAX / 2;
  438. coder->align_price_count = UINT32_MAX / 2;
  439. coder->opts_end_index = 0;
  440. coder->opts_current_index = 0;
  441. return LZMA_OK;
  442. }
  443. extern lzma_ret
  444. lzma_lzma_encoder_create(void **coder_ptr,
  445. const lzma_allocator *allocator,
  446. const lzma_options_lzma *options, lzma_lz_options *lz_options)
  447. {
  448. // Allocate lzma_lzma1_encoder if it wasn't already allocated.
  449. if (*coder_ptr == NULL) {
  450. *coder_ptr = lzma_alloc(sizeof(lzma_lzma1_encoder), allocator);
  451. if (*coder_ptr == NULL)
  452. return LZMA_MEM_ERROR;
  453. }
  454. lzma_lzma1_encoder *coder = *coder_ptr;
  455. // Set compression mode. We haven't validates the options yet,
  456. // but it's OK here, since nothing bad happens with invalid
  457. // options in the code below, and they will get rejected by
  458. // lzma_lzma_encoder_reset() call at the end of this function.
  459. switch (options->mode) {
  460. case LZMA_MODE_FAST:
  461. coder->fast_mode = true;
  462. break;
  463. case LZMA_MODE_NORMAL: {
  464. coder->fast_mode = false;
  465. // Set dist_table_size.
  466. // Round the dictionary size up to next 2^n.
  467. uint32_t log_size = 0;
  468. while ((UINT32_C(1) << log_size) < options->dict_size)
  469. ++log_size;
  470. coder->dist_table_size = log_size * 2;
  471. // Length encoders' price table size
  472. coder->match_len_encoder.table_size
  473. = options->nice_len + 1 - MATCH_LEN_MIN;
  474. coder->rep_len_encoder.table_size
  475. = options->nice_len + 1 - MATCH_LEN_MIN;
  476. break;
  477. }
  478. default:
  479. return LZMA_OPTIONS_ERROR;
  480. }
  481. // We don't need to write the first byte as literal if there is
  482. // a non-empty preset dictionary. encode_init() wouldn't even work
  483. // if there is a non-empty preset dictionary, because encode_init()
  484. // assumes that position is zero and previous byte is also zero.
  485. coder->is_initialized = options->preset_dict != NULL
  486. && options->preset_dict_size > 0;
  487. coder->is_flushed = false;
  488. set_lz_options(lz_options, options);
  489. return lzma_lzma_encoder_reset(coder, options);
  490. }
  491. static lzma_ret
  492. lzma_encoder_init(lzma_lz_encoder *lz, const lzma_allocator *allocator,
  493. const void *options, lzma_lz_options *lz_options)
  494. {
  495. lz->code = &lzma_encode;
  496. return lzma_lzma_encoder_create(
  497. &lz->coder, allocator, options, lz_options);
  498. }
  499. extern lzma_ret
  500. lzma_lzma_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  501. const lzma_filter_info *filters)
  502. {
  503. return lzma_lz_encoder_init(
  504. next, allocator, filters, &lzma_encoder_init);
  505. }
  506. extern uint64_t
  507. lzma_lzma_encoder_memusage(const void *options)
  508. {
  509. if (!is_options_valid(options))
  510. return UINT64_MAX;
  511. lzma_lz_options lz_options;
  512. set_lz_options(&lz_options, options);
  513. const uint64_t lz_memusage = lzma_lz_encoder_memusage(&lz_options);
  514. if (lz_memusage == UINT64_MAX)
  515. return UINT64_MAX;
  516. return (uint64_t)(sizeof(lzma_lzma1_encoder)) + lz_memusage;
  517. }
  518. extern bool
  519. lzma_lzma_lclppb_encode(const lzma_options_lzma *options, uint8_t *byte)
  520. {
  521. if (!is_lclppb_valid(options))
  522. return true;
  523. *byte = (options->pb * 5 + options->lp) * 9 + options->lc;
  524. assert(*byte <= (4 * 5 + 4) * 9 + 8);
  525. return false;
  526. }
  527. #ifdef HAVE_ENCODER_LZMA1
  528. extern lzma_ret
  529. lzma_lzma_props_encode(const void *options, uint8_t *out)
  530. {
  531. const lzma_options_lzma *const opt = options;
  532. if (lzma_lzma_lclppb_encode(opt, out))
  533. return LZMA_PROG_ERROR;
  534. unaligned_write32le(out + 1, opt->dict_size);
  535. return LZMA_OK;
  536. }
  537. #endif
  538. extern LZMA_API(lzma_bool)
  539. lzma_mode_is_supported(lzma_mode mode)
  540. {
  541. return mode == LZMA_MODE_FAST || mode == LZMA_MODE_NORMAL;
  542. }