lzma2_encoder.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file lzma2_encoder.c
  5. /// \brief LZMA2 encoder
  6. ///
  7. // Authors: Igor Pavlov
  8. // Lasse Collin
  9. //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "lz_encoder.h"
  12. #include "lzma_encoder.h"
  13. #include "fastpos.h"
  14. #include "lzma2_encoder.h"
  15. typedef struct {
  16. enum {
  17. SEQ_INIT,
  18. SEQ_LZMA_ENCODE,
  19. SEQ_LZMA_COPY,
  20. SEQ_UNCOMPRESSED_HEADER,
  21. SEQ_UNCOMPRESSED_COPY,
  22. } sequence;
  23. /// LZMA encoder
  24. void *lzma;
  25. /// LZMA options currently in use.
  26. lzma_options_lzma opt_cur;
  27. bool need_properties;
  28. bool need_state_reset;
  29. bool need_dictionary_reset;
  30. /// Uncompressed size of a chunk
  31. size_t uncompressed_size;
  32. /// Compressed size of a chunk (excluding headers); this is also used
  33. /// to indicate the end of buf[] in SEQ_LZMA_COPY.
  34. size_t compressed_size;
  35. /// Read position in buf[]
  36. size_t buf_pos;
  37. /// Buffer to hold the chunk header and LZMA compressed data
  38. uint8_t buf[LZMA2_HEADER_MAX + LZMA2_CHUNK_MAX];
  39. } lzma_lzma2_coder;
  40. static void
  41. lzma2_header_lzma(lzma_lzma2_coder *coder)
  42. {
  43. assert(coder->uncompressed_size > 0);
  44. assert(coder->uncompressed_size <= LZMA2_UNCOMPRESSED_MAX);
  45. assert(coder->compressed_size > 0);
  46. assert(coder->compressed_size <= LZMA2_CHUNK_MAX);
  47. size_t pos;
  48. if (coder->need_properties) {
  49. pos = 0;
  50. if (coder->need_dictionary_reset)
  51. coder->buf[pos] = 0x80 + (3 << 5);
  52. else
  53. coder->buf[pos] = 0x80 + (2 << 5);
  54. } else {
  55. pos = 1;
  56. if (coder->need_state_reset)
  57. coder->buf[pos] = 0x80 + (1 << 5);
  58. else
  59. coder->buf[pos] = 0x80;
  60. }
  61. // Set the start position for copying.
  62. coder->buf_pos = pos;
  63. // Uncompressed size
  64. size_t size = coder->uncompressed_size - 1;
  65. coder->buf[pos++] += size >> 16;
  66. coder->buf[pos++] = (size >> 8) & 0xFF;
  67. coder->buf[pos++] = size & 0xFF;
  68. // Compressed size
  69. size = coder->compressed_size - 1;
  70. coder->buf[pos++] = size >> 8;
  71. coder->buf[pos++] = size & 0xFF;
  72. // Properties, if needed
  73. if (coder->need_properties)
  74. lzma_lzma_lclppb_encode(&coder->opt_cur, coder->buf + pos);
  75. coder->need_properties = false;
  76. coder->need_state_reset = false;
  77. coder->need_dictionary_reset = false;
  78. // The copying code uses coder->compressed_size to indicate the end
  79. // of coder->buf[], so we need add the maximum size of the header here.
  80. coder->compressed_size += LZMA2_HEADER_MAX;
  81. return;
  82. }
  83. static void
  84. lzma2_header_uncompressed(lzma_lzma2_coder *coder)
  85. {
  86. assert(coder->uncompressed_size > 0);
  87. assert(coder->uncompressed_size <= LZMA2_CHUNK_MAX);
  88. // If this is the first chunk, we need to include dictionary
  89. // reset indicator.
  90. if (coder->need_dictionary_reset)
  91. coder->buf[0] = 1;
  92. else
  93. coder->buf[0] = 2;
  94. coder->need_dictionary_reset = false;
  95. // "Compressed" size
  96. coder->buf[1] = (coder->uncompressed_size - 1) >> 8;
  97. coder->buf[2] = (coder->uncompressed_size - 1) & 0xFF;
  98. // Set the start position for copying.
  99. coder->buf_pos = 0;
  100. return;
  101. }
  102. static lzma_ret
  103. lzma2_encode(void *coder_ptr, lzma_mf *restrict mf,
  104. uint8_t *restrict out, size_t *restrict out_pos,
  105. size_t out_size)
  106. {
  107. lzma_lzma2_coder *restrict coder = coder_ptr;
  108. while (*out_pos < out_size)
  109. switch (coder->sequence) {
  110. case SEQ_INIT:
  111. // If there's no input left and we are flushing or finishing,
  112. // don't start a new chunk.
  113. if (mf_unencoded(mf) == 0) {
  114. // Write end of payload marker if finishing.
  115. if (mf->action == LZMA_FINISH)
  116. out[(*out_pos)++] = 0;
  117. return mf->action == LZMA_RUN
  118. ? LZMA_OK : LZMA_STREAM_END;
  119. }
  120. if (coder->need_state_reset)
  121. return_if_error(lzma_lzma_encoder_reset(
  122. coder->lzma, &coder->opt_cur));
  123. coder->uncompressed_size = 0;
  124. coder->compressed_size = 0;
  125. coder->sequence = SEQ_LZMA_ENCODE;
  126. // Fall through
  127. case SEQ_LZMA_ENCODE: {
  128. // Calculate how much more uncompressed data this chunk
  129. // could accept.
  130. const uint32_t left = LZMA2_UNCOMPRESSED_MAX
  131. - coder->uncompressed_size;
  132. uint32_t limit;
  133. if (left < mf->match_len_max) {
  134. // Must flush immediately since the next LZMA symbol
  135. // could make the uncompressed size of the chunk too
  136. // big.
  137. limit = 0;
  138. } else {
  139. // Calculate maximum read_limit that is OK from point
  140. // of view of LZMA2 chunk size.
  141. limit = mf->read_pos - mf->read_ahead
  142. + left - mf->match_len_max;
  143. }
  144. // Save the start position so that we can update
  145. // coder->uncompressed_size.
  146. const uint32_t read_start = mf->read_pos - mf->read_ahead;
  147. // Call the LZMA encoder until the chunk is finished.
  148. const lzma_ret ret = lzma_lzma_encode(coder->lzma, mf,
  149. coder->buf + LZMA2_HEADER_MAX,
  150. &coder->compressed_size,
  151. LZMA2_CHUNK_MAX, limit);
  152. coder->uncompressed_size += mf->read_pos - mf->read_ahead
  153. - read_start;
  154. assert(coder->compressed_size <= LZMA2_CHUNK_MAX);
  155. assert(coder->uncompressed_size <= LZMA2_UNCOMPRESSED_MAX);
  156. if (ret != LZMA_STREAM_END)
  157. return LZMA_OK;
  158. // See if the chunk compressed. If it didn't, we encode it
  159. // as uncompressed chunk. This saves a few bytes of space
  160. // and makes decoding faster.
  161. if (coder->compressed_size >= coder->uncompressed_size) {
  162. coder->uncompressed_size += mf->read_ahead;
  163. assert(coder->uncompressed_size
  164. <= LZMA2_UNCOMPRESSED_MAX);
  165. mf->read_ahead = 0;
  166. lzma2_header_uncompressed(coder);
  167. coder->need_state_reset = true;
  168. coder->sequence = SEQ_UNCOMPRESSED_HEADER;
  169. break;
  170. }
  171. // The chunk did compress at least by one byte, so we store
  172. // the chunk as LZMA.
  173. lzma2_header_lzma(coder);
  174. coder->sequence = SEQ_LZMA_COPY;
  175. }
  176. // Fall through
  177. case SEQ_LZMA_COPY:
  178. // Copy the compressed chunk along its headers to the
  179. // output buffer.
  180. lzma_bufcpy(coder->buf, &coder->buf_pos,
  181. coder->compressed_size,
  182. out, out_pos, out_size);
  183. if (coder->buf_pos != coder->compressed_size)
  184. return LZMA_OK;
  185. coder->sequence = SEQ_INIT;
  186. break;
  187. case SEQ_UNCOMPRESSED_HEADER:
  188. // Copy the three-byte header to indicate uncompressed chunk.
  189. lzma_bufcpy(coder->buf, &coder->buf_pos,
  190. LZMA2_HEADER_UNCOMPRESSED,
  191. out, out_pos, out_size);
  192. if (coder->buf_pos != LZMA2_HEADER_UNCOMPRESSED)
  193. return LZMA_OK;
  194. coder->sequence = SEQ_UNCOMPRESSED_COPY;
  195. // Fall through
  196. case SEQ_UNCOMPRESSED_COPY:
  197. // Copy the uncompressed data as is from the dictionary
  198. // to the output buffer.
  199. mf_read(mf, out, out_pos, out_size, &coder->uncompressed_size);
  200. if (coder->uncompressed_size != 0)
  201. return LZMA_OK;
  202. coder->sequence = SEQ_INIT;
  203. break;
  204. }
  205. return LZMA_OK;
  206. }
  207. static void
  208. lzma2_encoder_end(void *coder_ptr, const lzma_allocator *allocator)
  209. {
  210. lzma_lzma2_coder *coder = coder_ptr;
  211. lzma_free(coder->lzma, allocator);
  212. lzma_free(coder, allocator);
  213. return;
  214. }
  215. static lzma_ret
  216. lzma2_encoder_options_update(void *coder_ptr, const lzma_filter *filter)
  217. {
  218. lzma_lzma2_coder *coder = coder_ptr;
  219. // New options can be set only when there is no incomplete chunk.
  220. // This is the case at the beginning of the raw stream and right
  221. // after LZMA_SYNC_FLUSH.
  222. if (filter->options == NULL || coder->sequence != SEQ_INIT)
  223. return LZMA_PROG_ERROR;
  224. // Look if there are new options. At least for now,
  225. // only lc/lp/pb can be changed.
  226. const lzma_options_lzma *opt = filter->options;
  227. if (coder->opt_cur.lc != opt->lc || coder->opt_cur.lp != opt->lp
  228. || coder->opt_cur.pb != opt->pb) {
  229. // Validate the options.
  230. if (opt->lc > LZMA_LCLP_MAX || opt->lp > LZMA_LCLP_MAX
  231. || opt->lc + opt->lp > LZMA_LCLP_MAX
  232. || opt->pb > LZMA_PB_MAX)
  233. return LZMA_OPTIONS_ERROR;
  234. // The new options will be used when the encoder starts
  235. // a new LZMA2 chunk.
  236. coder->opt_cur.lc = opt->lc;
  237. coder->opt_cur.lp = opt->lp;
  238. coder->opt_cur.pb = opt->pb;
  239. coder->need_properties = true;
  240. coder->need_state_reset = true;
  241. }
  242. return LZMA_OK;
  243. }
  244. static lzma_ret
  245. lzma2_encoder_init(lzma_lz_encoder *lz, const lzma_allocator *allocator,
  246. lzma_vli id lzma_attribute((__unused__)), const void *options,
  247. lzma_lz_options *lz_options)
  248. {
  249. if (options == NULL)
  250. return LZMA_PROG_ERROR;
  251. lzma_lzma2_coder *coder = lz->coder;
  252. if (coder == NULL) {
  253. coder = lzma_alloc(sizeof(lzma_lzma2_coder), allocator);
  254. if (coder == NULL)
  255. return LZMA_MEM_ERROR;
  256. lz->coder = coder;
  257. lz->code = &lzma2_encode;
  258. lz->end = &lzma2_encoder_end;
  259. lz->options_update = &lzma2_encoder_options_update;
  260. coder->lzma = NULL;
  261. }
  262. coder->opt_cur = *(const lzma_options_lzma *)(options);
  263. coder->sequence = SEQ_INIT;
  264. coder->need_properties = true;
  265. coder->need_state_reset = false;
  266. coder->need_dictionary_reset
  267. = coder->opt_cur.preset_dict == NULL
  268. || coder->opt_cur.preset_dict_size == 0;
  269. // Initialize LZMA encoder
  270. return_if_error(lzma_lzma_encoder_create(&coder->lzma, allocator,
  271. LZMA_FILTER_LZMA2, &coder->opt_cur, lz_options));
  272. // Make sure that we will always have enough history available in
  273. // case we need to use uncompressed chunks. They are used when the
  274. // compressed size of a chunk is not smaller than the uncompressed
  275. // size, so we need to have at least LZMA2_COMPRESSED_MAX bytes
  276. // history available.
  277. if (lz_options->before_size + lz_options->dict_size < LZMA2_CHUNK_MAX)
  278. lz_options->before_size
  279. = LZMA2_CHUNK_MAX - lz_options->dict_size;
  280. return LZMA_OK;
  281. }
  282. extern lzma_ret
  283. lzma_lzma2_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  284. const lzma_filter_info *filters)
  285. {
  286. return lzma_lz_encoder_init(
  287. next, allocator, filters, &lzma2_encoder_init);
  288. }
  289. extern uint64_t
  290. lzma_lzma2_encoder_memusage(const void *options)
  291. {
  292. const uint64_t lzma_mem = lzma_lzma_encoder_memusage(options);
  293. if (lzma_mem == UINT64_MAX)
  294. return UINT64_MAX;
  295. return sizeof(lzma_lzma2_coder) + lzma_mem;
  296. }
  297. extern lzma_ret
  298. lzma_lzma2_props_encode(const void *options, uint8_t *out)
  299. {
  300. if (options == NULL)
  301. return LZMA_PROG_ERROR;
  302. const lzma_options_lzma *const opt = options;
  303. uint32_t d = my_max(opt->dict_size, LZMA_DICT_SIZE_MIN);
  304. // Round up to the next 2^n - 1 or 2^n + 2^(n - 1) - 1 depending
  305. // on which one is the next:
  306. --d;
  307. d |= d >> 2;
  308. d |= d >> 3;
  309. d |= d >> 4;
  310. d |= d >> 8;
  311. d |= d >> 16;
  312. // Get the highest two bits using the proper encoding:
  313. if (d == UINT32_MAX)
  314. out[0] = 40;
  315. else
  316. out[0] = get_dist_slot(d + 1) - 24;
  317. return LZMA_OK;
  318. }
  319. extern uint64_t
  320. lzma_lzma2_block_size(const void *options)
  321. {
  322. const lzma_options_lzma *const opt = options;
  323. if (!IS_ENC_DICT_SIZE_VALID(opt->dict_size))
  324. return UINT64_MAX;
  325. // Use at least 1 MiB to keep compression ratio better.
  326. return my_max((uint64_t)(opt->dict_size) * 3, UINT64_C(1) << 20);
  327. }