lzma_encoder.c 18 KB

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