lzma2_encoder.c 9.9 KB

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