xref: /dragonfly/contrib/xz/src/liblzma/lz/lz_encoder.h (revision 46a2189d)
12940b44dSPeter Avalos ///////////////////////////////////////////////////////////////////////////////
22940b44dSPeter Avalos //
32940b44dSPeter Avalos /// \file       lz_encoder.h
42940b44dSPeter Avalos /// \brief      LZ in window and match finder API
52940b44dSPeter Avalos ///
62940b44dSPeter Avalos //  Authors:    Igor Pavlov
72940b44dSPeter Avalos //              Lasse Collin
82940b44dSPeter Avalos //
92940b44dSPeter Avalos //  This file has been put into the public domain.
102940b44dSPeter Avalos //  You can do whatever you want with this file.
112940b44dSPeter Avalos //
122940b44dSPeter Avalos ///////////////////////////////////////////////////////////////////////////////
132940b44dSPeter Avalos 
142940b44dSPeter Avalos #ifndef LZMA_LZ_ENCODER_H
152940b44dSPeter Avalos #define LZMA_LZ_ENCODER_H
162940b44dSPeter Avalos 
172940b44dSPeter Avalos #include "common.h"
182940b44dSPeter Avalos 
192940b44dSPeter Avalos 
202940b44dSPeter Avalos /// A table of these is used by the LZ-based encoder to hold
212940b44dSPeter Avalos /// the length-distance pairs found by the match finder.
222940b44dSPeter Avalos typedef struct {
232940b44dSPeter Avalos 	uint32_t len;
242940b44dSPeter Avalos 	uint32_t dist;
252940b44dSPeter Avalos } lzma_match;
262940b44dSPeter Avalos 
272940b44dSPeter Avalos 
282940b44dSPeter Avalos typedef struct lzma_mf_s lzma_mf;
292940b44dSPeter Avalos struct lzma_mf_s {
302940b44dSPeter Avalos 	///////////////
312940b44dSPeter Avalos 	// In Window //
322940b44dSPeter Avalos 	///////////////
332940b44dSPeter Avalos 
342940b44dSPeter Avalos 	/// Pointer to buffer with data to be compressed
352940b44dSPeter Avalos 	uint8_t *buffer;
362940b44dSPeter Avalos 
372940b44dSPeter Avalos 	/// Total size of the allocated buffer (that is, including all
382940b44dSPeter Avalos 	/// the extra space)
392940b44dSPeter Avalos 	uint32_t size;
402940b44dSPeter Avalos 
412940b44dSPeter Avalos 	/// Number of bytes that must be kept available in our input history.
422940b44dSPeter Avalos 	/// That is, once keep_size_before bytes have been processed,
432940b44dSPeter Avalos 	/// buffer[read_pos - keep_size_before] is the oldest byte that
442940b44dSPeter Avalos 	/// must be available for reading.
452940b44dSPeter Avalos 	uint32_t keep_size_before;
462940b44dSPeter Avalos 
472940b44dSPeter Avalos 	/// Number of bytes that must be kept in buffer after read_pos.
482940b44dSPeter Avalos 	/// That is, read_pos <= write_pos - keep_size_after as long as
492940b44dSPeter Avalos 	/// action is LZMA_RUN; when action != LZMA_RUN, read_pos is allowed
502940b44dSPeter Avalos 	/// to reach write_pos so that the last bytes get encoded too.
512940b44dSPeter Avalos 	uint32_t keep_size_after;
522940b44dSPeter Avalos 
532940b44dSPeter Avalos 	/// Match finders store locations of matches using 32-bit integers.
542940b44dSPeter Avalos 	/// To avoid adjusting several megabytes of integers every time the
552940b44dSPeter Avalos 	/// input window is moved with move_window, we only adjust the
562940b44dSPeter Avalos 	/// offset of the buffer. Thus, buffer[value_in_hash_table - offset]
572940b44dSPeter Avalos 	/// is the byte pointed by value_in_hash_table.
582940b44dSPeter Avalos 	uint32_t offset;
592940b44dSPeter Avalos 
602940b44dSPeter Avalos 	/// buffer[read_pos] is the next byte to run through the match
612940b44dSPeter Avalos 	/// finder. This is incremented in the match finder once the byte
622940b44dSPeter Avalos 	/// has been processed.
632940b44dSPeter Avalos 	uint32_t read_pos;
642940b44dSPeter Avalos 
652940b44dSPeter Avalos 	/// Number of bytes that have been ran through the match finder, but
662940b44dSPeter Avalos 	/// which haven't been encoded by the LZ-based encoder yet.
672940b44dSPeter Avalos 	uint32_t read_ahead;
682940b44dSPeter Avalos 
692940b44dSPeter Avalos 	/// As long as read_pos is less than read_limit, there is enough
702940b44dSPeter Avalos 	/// input available in buffer for at least one encoding loop.
712940b44dSPeter Avalos 	///
722940b44dSPeter Avalos 	/// Because of the stateful API, read_limit may and will get greater
732940b44dSPeter Avalos 	/// than read_pos quite often. This is taken into account when
742940b44dSPeter Avalos 	/// calculating the value for keep_size_after.
752940b44dSPeter Avalos 	uint32_t read_limit;
762940b44dSPeter Avalos 
772940b44dSPeter Avalos 	/// buffer[write_pos] is the first byte that doesn't contain valid
782940b44dSPeter Avalos 	/// uncompressed data; that is, the next input byte will be copied
792940b44dSPeter Avalos 	/// to buffer[write_pos].
802940b44dSPeter Avalos 	uint32_t write_pos;
812940b44dSPeter Avalos 
822940b44dSPeter Avalos 	/// Number of bytes not hashed before read_pos. This is needed to
832940b44dSPeter Avalos 	/// restart the match finder after LZMA_SYNC_FLUSH.
842940b44dSPeter Avalos 	uint32_t pending;
852940b44dSPeter Avalos 
862940b44dSPeter Avalos 	//////////////////
872940b44dSPeter Avalos 	// Match Finder //
882940b44dSPeter Avalos 	//////////////////
892940b44dSPeter Avalos 
902940b44dSPeter Avalos 	/// Find matches. Returns the number of distance-length pairs written
912940b44dSPeter Avalos 	/// to the matches array. This is called only via lzma_mf_find().
922940b44dSPeter Avalos 	uint32_t (*find)(lzma_mf *mf, lzma_match *matches);
932940b44dSPeter Avalos 
942940b44dSPeter Avalos 	/// Skips num bytes. This is like find() but doesn't make the
952940b44dSPeter Avalos 	/// distance-length pairs available, thus being a little faster.
962940b44dSPeter Avalos 	/// This is called only via mf_skip().
972940b44dSPeter Avalos 	void (*skip)(lzma_mf *mf, uint32_t num);
982940b44dSPeter Avalos 
992940b44dSPeter Avalos 	uint32_t *hash;
1002940b44dSPeter Avalos 	uint32_t *son;
1012940b44dSPeter Avalos 	uint32_t cyclic_pos;
1022940b44dSPeter Avalos 	uint32_t cyclic_size; // Must be dictionary size + 1.
1032940b44dSPeter Avalos 	uint32_t hash_mask;
1042940b44dSPeter Avalos 
1052940b44dSPeter Avalos 	/// Maximum number of loops in the match finder
1062940b44dSPeter Avalos 	uint32_t depth;
1072940b44dSPeter Avalos 
1082940b44dSPeter Avalos 	/// Maximum length of a match that the match finder will try to find.
1092940b44dSPeter Avalos 	uint32_t nice_len;
1102940b44dSPeter Avalos 
1112940b44dSPeter Avalos 	/// Maximum length of a match supported by the LZ-based encoder.
1122940b44dSPeter Avalos 	/// If the longest match found by the match finder is nice_len,
1132940b44dSPeter Avalos 	/// mf_find() tries to expand it up to match_len_max bytes.
1142940b44dSPeter Avalos 	uint32_t match_len_max;
1152940b44dSPeter Avalos 
1162940b44dSPeter Avalos 	/// When running out of input, binary tree match finders need to know
1172940b44dSPeter Avalos 	/// if it is due to flushing or finishing. The action is used also
1182940b44dSPeter Avalos 	/// by the LZ-based encoders themselves.
1192940b44dSPeter Avalos 	lzma_action action;
1202940b44dSPeter Avalos 
1212940b44dSPeter Avalos 	/// Number of elements in hash[]
12215ab8c86SJohn Marino 	uint32_t hash_count;
1232940b44dSPeter Avalos 
1242940b44dSPeter Avalos 	/// Number of elements in son[]
1252940b44dSPeter Avalos 	uint32_t sons_count;
1262940b44dSPeter Avalos };
1272940b44dSPeter Avalos 
1282940b44dSPeter Avalos 
1292940b44dSPeter Avalos typedef struct {
1302940b44dSPeter Avalos 	/// Extra amount of data to keep available before the "actual"
1312940b44dSPeter Avalos 	/// dictionary.
1322940b44dSPeter Avalos 	size_t before_size;
1332940b44dSPeter Avalos 
1342940b44dSPeter Avalos 	/// Size of the history buffer
1352940b44dSPeter Avalos 	size_t dict_size;
1362940b44dSPeter Avalos 
1372940b44dSPeter Avalos 	/// Extra amount of data to keep available after the "actual"
1382940b44dSPeter Avalos 	/// dictionary.
1392940b44dSPeter Avalos 	size_t after_size;
1402940b44dSPeter Avalos 
1412940b44dSPeter Avalos 	/// Maximum length of a match that the LZ-based encoder can accept.
1422940b44dSPeter Avalos 	/// This is used to extend matches of length nice_len to the
1432940b44dSPeter Avalos 	/// maximum possible length.
1442940b44dSPeter Avalos 	size_t match_len_max;
1452940b44dSPeter Avalos 
1462940b44dSPeter Avalos 	/// Match finder will search matches up to this length.
1472940b44dSPeter Avalos 	/// This must be less than or equal to match_len_max.
1482940b44dSPeter Avalos 	size_t nice_len;
1492940b44dSPeter Avalos 
1502940b44dSPeter Avalos 	/// Type of the match finder to use
1512940b44dSPeter Avalos 	lzma_match_finder match_finder;
1522940b44dSPeter Avalos 
1532940b44dSPeter Avalos 	/// Maximum search depth
1542940b44dSPeter Avalos 	uint32_t depth;
1552940b44dSPeter Avalos 
1562940b44dSPeter Avalos 	/// TODO: Comment
1572940b44dSPeter Avalos 	const uint8_t *preset_dict;
1582940b44dSPeter Avalos 
1592940b44dSPeter Avalos 	uint32_t preset_dict_size;
1602940b44dSPeter Avalos 
1612940b44dSPeter Avalos } lzma_lz_options;
1622940b44dSPeter Avalos 
1632940b44dSPeter Avalos 
1642940b44dSPeter Avalos // The total usable buffer space at any moment outside the match finder:
1652940b44dSPeter Avalos // before_size + dict_size + after_size + match_len_max
1662940b44dSPeter Avalos //
1672940b44dSPeter Avalos // In reality, there's some extra space allocated to prevent the number of
1682940b44dSPeter Avalos // memmove() calls reasonable. The bigger the dict_size is, the bigger
1692940b44dSPeter Avalos // this extra buffer will be since with bigger dictionaries memmove() would
1702940b44dSPeter Avalos // also take longer.
1712940b44dSPeter Avalos //
1722940b44dSPeter Avalos // A single encoder loop in the LZ-based encoder may call the match finder
1732940b44dSPeter Avalos // (mf_find() or mf_skip()) at most after_size times. In other words,
1742940b44dSPeter Avalos // a single encoder loop may increment lzma_mf.read_pos at most after_size
1752940b44dSPeter Avalos // times. Since matches are looked up to
1762940b44dSPeter Avalos // lzma_mf.buffer[lzma_mf.read_pos + match_len_max - 1], the total
1772940b44dSPeter Avalos // amount of extra buffer needed after dict_size becomes
1782940b44dSPeter Avalos // after_size + match_len_max.
1792940b44dSPeter Avalos //
1802940b44dSPeter Avalos // before_size has two uses. The first one is to keep literals available
1812940b44dSPeter Avalos // in cases when the LZ-based encoder has made some read ahead.
1822940b44dSPeter Avalos // TODO: Maybe this could be changed by making the LZ-based encoders to
1832940b44dSPeter Avalos // store the actual literals as they do with length-distance pairs.
1842940b44dSPeter Avalos //
1852940b44dSPeter Avalos // Algorithms such as LZMA2 first try to compress a chunk, and then check
1862940b44dSPeter Avalos // if the encoded result is smaller than the uncompressed one. If the chunk
1872940b44dSPeter Avalos // was uncompressible, it is better to store it in uncompressed form in
1882940b44dSPeter Avalos // the output stream. To do this, the whole uncompressed chunk has to be
1892940b44dSPeter Avalos // still available in the history buffer. before_size achieves that.
1902940b44dSPeter Avalos 
1912940b44dSPeter Avalos 
1922940b44dSPeter Avalos typedef struct {
1932940b44dSPeter Avalos 	/// Data specific to the LZ-based encoder
194*46a2189dSzrj 	void *coder;
1952940b44dSPeter Avalos 
1962940b44dSPeter Avalos 	/// Function to encode from *dict to out[]
197*46a2189dSzrj 	lzma_ret (*code)(void *coder,
1982940b44dSPeter Avalos 			lzma_mf *restrict mf, uint8_t *restrict out,
1992940b44dSPeter Avalos 			size_t *restrict out_pos, size_t out_size);
2002940b44dSPeter Avalos 
2012940b44dSPeter Avalos 	/// Free allocated resources
202*46a2189dSzrj 	void (*end)(void *coder, const lzma_allocator *allocator);
2032940b44dSPeter Avalos 
2042940b44dSPeter Avalos 	/// Update the options in the middle of the encoding.
205*46a2189dSzrj 	lzma_ret (*options_update)(void *coder, const lzma_filter *filter);
2062940b44dSPeter Avalos 
2072940b44dSPeter Avalos } lzma_lz_encoder;
2082940b44dSPeter Avalos 
2092940b44dSPeter Avalos 
2102940b44dSPeter Avalos // Basic steps:
2112940b44dSPeter Avalos //  1. Input gets copied into the dictionary.
2122940b44dSPeter Avalos //  2. Data in dictionary gets run through the match finder byte by byte.
2132940b44dSPeter Avalos //  3. The literals and matches are encoded using e.g. LZMA.
2142940b44dSPeter Avalos //
2152940b44dSPeter Avalos // The bytes that have been ran through the match finder, but not encoded yet,
2162940b44dSPeter Avalos // are called `read ahead'.
2172940b44dSPeter Avalos 
2182940b44dSPeter Avalos 
2192940b44dSPeter Avalos /// Get pointer to the first byte not ran through the match finder
2202940b44dSPeter Avalos static inline const uint8_t *
mf_ptr(const lzma_mf * mf)2212940b44dSPeter Avalos mf_ptr(const lzma_mf *mf)
2222940b44dSPeter Avalos {
2232940b44dSPeter Avalos 	return mf->buffer + mf->read_pos;
2242940b44dSPeter Avalos }
2252940b44dSPeter Avalos 
2262940b44dSPeter Avalos 
2272940b44dSPeter Avalos /// Get the number of bytes that haven't been ran through the match finder yet.
2282940b44dSPeter Avalos static inline uint32_t
mf_avail(const lzma_mf * mf)2292940b44dSPeter Avalos mf_avail(const lzma_mf *mf)
2302940b44dSPeter Avalos {
2312940b44dSPeter Avalos 	return mf->write_pos - mf->read_pos;
2322940b44dSPeter Avalos }
2332940b44dSPeter Avalos 
2342940b44dSPeter Avalos 
2352940b44dSPeter Avalos /// Get the number of bytes that haven't been encoded yet (some of these
2362940b44dSPeter Avalos /// bytes may have been ran through the match finder though).
2372940b44dSPeter Avalos static inline uint32_t
mf_unencoded(const lzma_mf * mf)2382940b44dSPeter Avalos mf_unencoded(const lzma_mf *mf)
2392940b44dSPeter Avalos {
2402940b44dSPeter Avalos 	return mf->write_pos - mf->read_pos + mf->read_ahead;
2412940b44dSPeter Avalos }
2422940b44dSPeter Avalos 
2432940b44dSPeter Avalos 
2442940b44dSPeter Avalos /// Calculate the absolute offset from the beginning of the most recent
2452940b44dSPeter Avalos /// dictionary reset. Only the lowest four bits are important, so there's no
2462940b44dSPeter Avalos /// problem that we don't know the 64-bit size of the data encoded so far.
2472940b44dSPeter Avalos ///
2482940b44dSPeter Avalos /// NOTE: When moving the input window, we need to do it so that the lowest
2492940b44dSPeter Avalos /// bits of dict->read_pos are not modified to keep this macro working
2502940b44dSPeter Avalos /// as intended.
2512940b44dSPeter Avalos static inline uint32_t
mf_position(const lzma_mf * mf)2522940b44dSPeter Avalos mf_position(const lzma_mf *mf)
2532940b44dSPeter Avalos {
2542940b44dSPeter Avalos 	return mf->read_pos - mf->read_ahead;
2552940b44dSPeter Avalos }
2562940b44dSPeter Avalos 
2572940b44dSPeter Avalos 
2582940b44dSPeter Avalos /// Since everything else begins with mf_, use it also for lzma_mf_find().
2592940b44dSPeter Avalos #define mf_find lzma_mf_find
2602940b44dSPeter Avalos 
2612940b44dSPeter Avalos 
2622940b44dSPeter Avalos /// Skip the given number of bytes. This is used when a good match was found.
2632940b44dSPeter Avalos /// For example, if mf_find() finds a match of 200 bytes long, the first byte
2642940b44dSPeter Avalos /// of that match was already consumed by mf_find(), and the rest 199 bytes
2652940b44dSPeter Avalos /// have to be skipped with mf_skip(mf, 199).
2662940b44dSPeter Avalos static inline void
mf_skip(lzma_mf * mf,uint32_t amount)2672940b44dSPeter Avalos mf_skip(lzma_mf *mf, uint32_t amount)
2682940b44dSPeter Avalos {
2692940b44dSPeter Avalos 	if (amount != 0) {
2702940b44dSPeter Avalos 		mf->skip(mf, amount);
2712940b44dSPeter Avalos 		mf->read_ahead += amount;
2722940b44dSPeter Avalos 	}
2732940b44dSPeter Avalos }
2742940b44dSPeter Avalos 
2752940b44dSPeter Avalos 
2762940b44dSPeter Avalos /// Copies at most *left number of bytes from the history buffer
2772940b44dSPeter Avalos /// to out[]. This is needed by LZMA2 to encode uncompressed chunks.
2782940b44dSPeter Avalos static inline void
mf_read(lzma_mf * mf,uint8_t * out,size_t * out_pos,size_t out_size,size_t * left)2792940b44dSPeter Avalos mf_read(lzma_mf *mf, uint8_t *out, size_t *out_pos, size_t out_size,
2802940b44dSPeter Avalos 		size_t *left)
2812940b44dSPeter Avalos {
2822940b44dSPeter Avalos 	const size_t out_avail = out_size - *out_pos;
2832940b44dSPeter Avalos 	const size_t copy_size = my_min(out_avail, *left);
2842940b44dSPeter Avalos 
2852940b44dSPeter Avalos 	assert(mf->read_ahead == 0);
2862940b44dSPeter Avalos 	assert(mf->read_pos >= *left);
2872940b44dSPeter Avalos 
2882940b44dSPeter Avalos 	memcpy(out + *out_pos, mf->buffer + mf->read_pos - *left,
2892940b44dSPeter Avalos 			copy_size);
2902940b44dSPeter Avalos 
2912940b44dSPeter Avalos 	*out_pos += copy_size;
2922940b44dSPeter Avalos 	*left -= copy_size;
2932940b44dSPeter Avalos 	return;
2942940b44dSPeter Avalos }
2952940b44dSPeter Avalos 
2962940b44dSPeter Avalos 
2972940b44dSPeter Avalos extern lzma_ret lzma_lz_encoder_init(
29815ab8c86SJohn Marino 		lzma_next_coder *next, const lzma_allocator *allocator,
2992940b44dSPeter Avalos 		const lzma_filter_info *filters,
3002940b44dSPeter Avalos 		lzma_ret (*lz_init)(lzma_lz_encoder *lz,
30115ab8c86SJohn Marino 			const lzma_allocator *allocator, const void *options,
3022940b44dSPeter Avalos 			lzma_lz_options *lz_options));
3032940b44dSPeter Avalos 
3042940b44dSPeter Avalos 
3052940b44dSPeter Avalos extern uint64_t lzma_lz_encoder_memusage(const lzma_lz_options *lz_options);
3062940b44dSPeter Avalos 
3072940b44dSPeter Avalos 
3082940b44dSPeter Avalos // These are only for LZ encoder's internal use.
3092940b44dSPeter Avalos extern uint32_t lzma_mf_find(
3102940b44dSPeter Avalos 		lzma_mf *mf, uint32_t *count, lzma_match *matches);
3112940b44dSPeter Avalos 
3122940b44dSPeter Avalos extern uint32_t lzma_mf_hc3_find(lzma_mf *dict, lzma_match *matches);
3132940b44dSPeter Avalos extern void lzma_mf_hc3_skip(lzma_mf *dict, uint32_t amount);
3142940b44dSPeter Avalos 
3152940b44dSPeter Avalos extern uint32_t lzma_mf_hc4_find(lzma_mf *dict, lzma_match *matches);
3162940b44dSPeter Avalos extern void lzma_mf_hc4_skip(lzma_mf *dict, uint32_t amount);
3172940b44dSPeter Avalos 
3182940b44dSPeter Avalos extern uint32_t lzma_mf_bt2_find(lzma_mf *dict, lzma_match *matches);
3192940b44dSPeter Avalos extern void lzma_mf_bt2_skip(lzma_mf *dict, uint32_t amount);
3202940b44dSPeter Avalos 
3212940b44dSPeter Avalos extern uint32_t lzma_mf_bt3_find(lzma_mf *dict, lzma_match *matches);
3222940b44dSPeter Avalos extern void lzma_mf_bt3_skip(lzma_mf *dict, uint32_t amount);
3232940b44dSPeter Avalos 
3242940b44dSPeter Avalos extern uint32_t lzma_mf_bt4_find(lzma_mf *dict, lzma_match *matches);
3252940b44dSPeter Avalos extern void lzma_mf_bt4_skip(lzma_mf *dict, uint32_t amount);
3262940b44dSPeter Avalos 
3272940b44dSPeter Avalos #endif
328