lz_encoder.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file lz_encoder.h
  5. /// \brief LZ in window and match finder API
  6. ///
  7. // Authors: Igor Pavlov
  8. // Lasse Collin
  9. //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #ifndef LZMA_LZ_ENCODER_H
  12. #define LZMA_LZ_ENCODER_H
  13. #include "common.h"
  14. // For now, the dictionary size is limited to 1.5 GiB. This may grow
  15. // in the future if needed, but it needs a little more work than just
  16. // changing this check.
  17. #define IS_ENC_DICT_SIZE_VALID(size) \
  18. ((size) >= LZMA_DICT_SIZE_MIN \
  19. && (size) <= (UINT32_C(1) << 30) + (UINT32_C(1) << 29))
  20. /// A table of these is used by the LZ-based encoder to hold
  21. /// the length-distance pairs found by the match finder.
  22. typedef struct {
  23. uint32_t len;
  24. uint32_t dist;
  25. } lzma_match;
  26. typedef struct lzma_mf_s lzma_mf;
  27. struct lzma_mf_s {
  28. ///////////////
  29. // In Window //
  30. ///////////////
  31. /// Pointer to buffer with data to be compressed
  32. uint8_t *buffer;
  33. /// Total size of the allocated buffer (that is, including all
  34. /// the extra space)
  35. uint32_t size;
  36. /// Number of bytes that must be kept available in our input history.
  37. /// That is, once keep_size_before bytes have been processed,
  38. /// buffer[read_pos - keep_size_before] is the oldest byte that
  39. /// must be available for reading.
  40. uint32_t keep_size_before;
  41. /// Number of bytes that must be kept in buffer after read_pos.
  42. /// That is, read_pos <= write_pos - keep_size_after as long as
  43. /// action is LZMA_RUN; when action != LZMA_RUN, read_pos is allowed
  44. /// to reach write_pos so that the last bytes get encoded too.
  45. uint32_t keep_size_after;
  46. /// Match finders store locations of matches using 32-bit integers.
  47. /// To avoid adjusting several megabytes of integers every time the
  48. /// input window is moved with move_window, we only adjust the
  49. /// offset of the buffer. Thus, buffer[value_in_hash_table - offset]
  50. /// is the byte pointed by value_in_hash_table.
  51. uint32_t offset;
  52. /// buffer[read_pos] is the next byte to run through the match
  53. /// finder. This is incremented in the match finder once the byte
  54. /// has been processed.
  55. uint32_t read_pos;
  56. /// Number of bytes that have been ran through the match finder, but
  57. /// which haven't been encoded by the LZ-based encoder yet.
  58. uint32_t read_ahead;
  59. /// As long as read_pos is less than read_limit, there is enough
  60. /// input available in buffer for at least one encoding loop.
  61. ///
  62. /// Because of the stateful API, read_limit may and will get greater
  63. /// than read_pos quite often. This is taken into account when
  64. /// calculating the value for keep_size_after.
  65. uint32_t read_limit;
  66. /// buffer[write_pos] is the first byte that doesn't contain valid
  67. /// uncompressed data; that is, the next input byte will be copied
  68. /// to buffer[write_pos].
  69. uint32_t write_pos;
  70. /// Number of bytes not hashed before read_pos. This is needed to
  71. /// restart the match finder after LZMA_SYNC_FLUSH.
  72. uint32_t pending;
  73. //////////////////
  74. // Match Finder //
  75. //////////////////
  76. /// Find matches. Returns the number of distance-length pairs written
  77. /// to the matches array. This is called only via lzma_mf_find().
  78. uint32_t (*find)(lzma_mf *mf, lzma_match *matches);
  79. /// Skips num bytes. This is like find() but doesn't make the
  80. /// distance-length pairs available, thus being a little faster.
  81. /// This is called only via mf_skip().
  82. void (*skip)(lzma_mf *mf, uint32_t num);
  83. uint32_t *hash;
  84. uint32_t *son;
  85. uint32_t cyclic_pos;
  86. uint32_t cyclic_size; // Must be dictionary size + 1.
  87. uint32_t hash_mask;
  88. /// Maximum number of loops in the match finder
  89. uint32_t depth;
  90. /// Maximum length of a match that the match finder will try to find.
  91. uint32_t nice_len;
  92. /// Maximum length of a match supported by the LZ-based encoder.
  93. /// If the longest match found by the match finder is nice_len,
  94. /// mf_find() tries to expand it up to match_len_max bytes.
  95. uint32_t match_len_max;
  96. /// When running out of input, binary tree match finders need to know
  97. /// if it is due to flushing or finishing. The action is used also
  98. /// by the LZ-based encoders themselves.
  99. lzma_action action;
  100. /// Number of elements in hash[]
  101. uint32_t hash_count;
  102. /// Number of elements in son[]
  103. uint32_t sons_count;
  104. };
  105. typedef struct {
  106. /// Extra amount of data to keep available before the "actual"
  107. /// dictionary.
  108. size_t before_size;
  109. /// Size of the history buffer
  110. size_t dict_size;
  111. /// Extra amount of data to keep available after the "actual"
  112. /// dictionary.
  113. size_t after_size;
  114. /// Maximum length of a match that the LZ-based encoder can accept.
  115. /// This is used to extend matches of length nice_len to the
  116. /// maximum possible length.
  117. size_t match_len_max;
  118. /// Match finder will search matches up to this length.
  119. /// This must be less than or equal to match_len_max.
  120. size_t nice_len;
  121. /// Type of the match finder to use
  122. lzma_match_finder match_finder;
  123. /// Maximum search depth
  124. uint32_t depth;
  125. /// Initial dictionary for the match finder to search.
  126. const uint8_t *preset_dict;
  127. /// If the preset dictionary is NULL, this value is ignored.
  128. /// Otherwise this member must indicate the preset dictionary's
  129. /// buffer size. If this size is larger than dict_size, then only
  130. /// the dict_size sized tail of the preset_dict will be used.
  131. uint32_t preset_dict_size;
  132. } lzma_lz_options;
  133. // The total usable buffer space at any moment outside the match finder:
  134. // before_size + dict_size + after_size + match_len_max
  135. //
  136. // In reality, there's some extra space allocated to prevent the number of
  137. // memmove() calls reasonable. The bigger the dict_size is, the bigger
  138. // this extra buffer will be since with bigger dictionaries memmove() would
  139. // also take longer.
  140. //
  141. // A single encoder loop in the LZ-based encoder may call the match finder
  142. // (mf_find() or mf_skip()) at most after_size times. In other words,
  143. // a single encoder loop may increment lzma_mf.read_pos at most after_size
  144. // times. Since matches are looked up to
  145. // lzma_mf.buffer[lzma_mf.read_pos + match_len_max - 1], the total
  146. // amount of extra buffer needed after dict_size becomes
  147. // after_size + match_len_max.
  148. //
  149. // before_size has two uses. The first one is to keep literals available
  150. // in cases when the LZ-based encoder has made some read ahead.
  151. // TODO: Maybe this could be changed by making the LZ-based encoders to
  152. // store the actual literals as they do with length-distance pairs.
  153. //
  154. // Algorithms such as LZMA2 first try to compress a chunk, and then check
  155. // if the encoded result is smaller than the uncompressed one. If the chunk
  156. // was incompressible, it is better to store it in uncompressed form in
  157. // the output stream. To do this, the whole uncompressed chunk has to be
  158. // still available in the history buffer. before_size achieves that.
  159. typedef struct {
  160. /// Data specific to the LZ-based encoder
  161. void *coder;
  162. /// Function to encode from *dict to out[]
  163. lzma_ret (*code)(void *coder,
  164. lzma_mf *restrict mf, uint8_t *restrict out,
  165. size_t *restrict out_pos, size_t out_size);
  166. /// Free allocated resources
  167. void (*end)(void *coder, const lzma_allocator *allocator);
  168. /// Update the options in the middle of the encoding.
  169. lzma_ret (*options_update)(void *coder, const lzma_filter *filter);
  170. /// Set maximum allowed output size
  171. lzma_ret (*set_out_limit)(void *coder, uint64_t *uncomp_size,
  172. uint64_t out_limit);
  173. } lzma_lz_encoder;
  174. // Basic steps:
  175. // 1. Input gets copied into the dictionary.
  176. // 2. Data in dictionary gets run through the match finder byte by byte.
  177. // 3. The literals and matches are encoded using e.g. LZMA.
  178. //
  179. // The bytes that have been ran through the match finder, but not encoded yet,
  180. // are called 'read ahead'.
  181. /// Get how many bytes the match finder hashes in its initial step.
  182. /// This is also the minimum nice_len value with the match finder.
  183. static inline uint32_t
  184. mf_get_hash_bytes(lzma_match_finder match_finder)
  185. {
  186. return (uint32_t)match_finder & 0x0F;
  187. }
  188. /// Get pointer to the first byte not ran through the match finder
  189. static inline const uint8_t *
  190. mf_ptr(const lzma_mf *mf)
  191. {
  192. return mf->buffer + mf->read_pos;
  193. }
  194. /// Get the number of bytes that haven't been ran through the match finder yet.
  195. static inline uint32_t
  196. mf_avail(const lzma_mf *mf)
  197. {
  198. return mf->write_pos - mf->read_pos;
  199. }
  200. /// Get the number of bytes that haven't been encoded yet (some of these
  201. /// bytes may have been ran through the match finder though).
  202. static inline uint32_t
  203. mf_unencoded(const lzma_mf *mf)
  204. {
  205. return mf->write_pos - mf->read_pos + mf->read_ahead;
  206. }
  207. /// Calculate the absolute offset from the beginning of the most recent
  208. /// dictionary reset. Only the lowest four bits are important, so there's no
  209. /// problem that we don't know the 64-bit size of the data encoded so far.
  210. ///
  211. /// NOTE: When moving the input window, we need to do it so that the lowest
  212. /// bits of dict->read_pos are not modified to keep this macro working
  213. /// as intended.
  214. static inline uint32_t
  215. mf_position(const lzma_mf *mf)
  216. {
  217. return mf->read_pos - mf->read_ahead;
  218. }
  219. /// Since everything else begins with mf_, use it also for lzma_mf_find().
  220. #define mf_find lzma_mf_find
  221. /// Skip the given number of bytes. This is used when a good match was found.
  222. /// For example, if mf_find() finds a match of 200 bytes long, the first byte
  223. /// of that match was already consumed by mf_find(), and the rest 199 bytes
  224. /// have to be skipped with mf_skip(mf, 199).
  225. static inline void
  226. mf_skip(lzma_mf *mf, uint32_t amount)
  227. {
  228. if (amount != 0) {
  229. mf->skip(mf, amount);
  230. mf->read_ahead += amount;
  231. }
  232. }
  233. /// Copies at most *left number of bytes from the history buffer
  234. /// to out[]. This is needed by LZMA2 to encode uncompressed chunks.
  235. static inline void
  236. mf_read(lzma_mf *mf, uint8_t *out, size_t *out_pos, size_t out_size,
  237. size_t *left)
  238. {
  239. const size_t out_avail = out_size - *out_pos;
  240. const size_t copy_size = my_min(out_avail, *left);
  241. assert(mf->read_ahead == 0);
  242. assert(mf->read_pos >= *left);
  243. memcpy(out + *out_pos, mf->buffer + mf->read_pos - *left,
  244. copy_size);
  245. *out_pos += copy_size;
  246. *left -= copy_size;
  247. return;
  248. }
  249. extern lzma_ret lzma_lz_encoder_init(
  250. lzma_next_coder *next, const lzma_allocator *allocator,
  251. const lzma_filter_info *filters,
  252. lzma_ret (*lz_init)(lzma_lz_encoder *lz,
  253. const lzma_allocator *allocator,
  254. lzma_vli id, const void *options,
  255. lzma_lz_options *lz_options));
  256. extern uint64_t lzma_lz_encoder_memusage(const lzma_lz_options *lz_options);
  257. // These are only for LZ encoder's internal use.
  258. extern uint32_t lzma_mf_find(
  259. lzma_mf *mf, uint32_t *count, lzma_match *matches);
  260. extern uint32_t lzma_mf_hc3_find(lzma_mf *dict, lzma_match *matches);
  261. extern void lzma_mf_hc3_skip(lzma_mf *dict, uint32_t amount);
  262. extern uint32_t lzma_mf_hc4_find(lzma_mf *dict, lzma_match *matches);
  263. extern void lzma_mf_hc4_skip(lzma_mf *dict, uint32_t amount);
  264. extern uint32_t lzma_mf_bt2_find(lzma_mf *dict, lzma_match *matches);
  265. extern void lzma_mf_bt2_skip(lzma_mf *dict, uint32_t amount);
  266. extern uint32_t lzma_mf_bt3_find(lzma_mf *dict, lzma_match *matches);
  267. extern void lzma_mf_bt3_skip(lzma_mf *dict, uint32_t amount);
  268. extern uint32_t lzma_mf_bt4_find(lzma_mf *dict, lzma_match *matches);
  269. extern void lzma_mf_bt4_skip(lzma_mf *dict, uint32_t amount);
  270. #endif