Path: blob/master/Utilities/cmliblzma/liblzma/lz/lz_encoder.h
3156 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file lz_encoder.h5/// \brief LZ in window and match finder API6///7// Authors: Igor Pavlov8// Lasse Collin9//10///////////////////////////////////////////////////////////////////////////////1112#ifndef LZMA_LZ_ENCODER_H13#define LZMA_LZ_ENCODER_H1415#include "common.h"161718// For now, the dictionary size is limited to 1.5 GiB. This may grow19// in the future if needed, but it needs a little more work than just20// changing this check.21#define IS_ENC_DICT_SIZE_VALID(size) \22((size) >= LZMA_DICT_SIZE_MIN \23&& (size) <= (UINT32_C(1) << 30) + (UINT32_C(1) << 29))242526/// A table of these is used by the LZ-based encoder to hold27/// the length-distance pairs found by the match finder.28typedef struct {29uint32_t len;30uint32_t dist;31} lzma_match;323334typedef struct lzma_mf_s lzma_mf;35struct lzma_mf_s {36///////////////37// In Window //38///////////////3940/// Pointer to buffer with data to be compressed41uint8_t *buffer;4243/// Total size of the allocated buffer (that is, including all44/// the extra space)45uint32_t size;4647/// Number of bytes that must be kept available in our input history.48/// That is, once keep_size_before bytes have been processed,49/// buffer[read_pos - keep_size_before] is the oldest byte that50/// must be available for reading.51uint32_t keep_size_before;5253/// Number of bytes that must be kept in buffer after read_pos.54/// That is, read_pos <= write_pos - keep_size_after as long as55/// action is LZMA_RUN; when action != LZMA_RUN, read_pos is allowed56/// to reach write_pos so that the last bytes get encoded too.57uint32_t keep_size_after;5859/// Match finders store locations of matches using 32-bit integers.60/// To avoid adjusting several megabytes of integers every time the61/// input window is moved with move_window, we only adjust the62/// offset of the buffer. Thus, buffer[value_in_hash_table - offset]63/// is the byte pointed by value_in_hash_table.64uint32_t offset;6566/// buffer[read_pos] is the next byte to run through the match67/// finder. This is incremented in the match finder once the byte68/// has been processed.69uint32_t read_pos;7071/// Number of bytes that have been ran through the match finder, but72/// which haven't been encoded by the LZ-based encoder yet.73uint32_t read_ahead;7475/// As long as read_pos is less than read_limit, there is enough76/// input available in buffer for at least one encoding loop.77///78/// Because of the stateful API, read_limit may and will get greater79/// than read_pos quite often. This is taken into account when80/// calculating the value for keep_size_after.81uint32_t read_limit;8283/// buffer[write_pos] is the first byte that doesn't contain valid84/// uncompressed data; that is, the next input byte will be copied85/// to buffer[write_pos].86uint32_t write_pos;8788/// Number of bytes not hashed before read_pos. This is needed to89/// restart the match finder after LZMA_SYNC_FLUSH.90uint32_t pending;9192//////////////////93// Match Finder //94//////////////////9596/// Find matches. Returns the number of distance-length pairs written97/// to the matches array. This is called only via lzma_mf_find().98uint32_t (*find)(lzma_mf *mf, lzma_match *matches);99100/// Skips num bytes. This is like find() but doesn't make the101/// distance-length pairs available, thus being a little faster.102/// This is called only via mf_skip().103void (*skip)(lzma_mf *mf, uint32_t num);104105uint32_t *hash;106uint32_t *son;107uint32_t cyclic_pos;108uint32_t cyclic_size; // Must be dictionary size + 1.109uint32_t hash_mask;110111/// Maximum number of loops in the match finder112uint32_t depth;113114/// Maximum length of a match that the match finder will try to find.115uint32_t nice_len;116117/// Maximum length of a match supported by the LZ-based encoder.118/// If the longest match found by the match finder is nice_len,119/// mf_find() tries to expand it up to match_len_max bytes.120uint32_t match_len_max;121122/// When running out of input, binary tree match finders need to know123/// if it is due to flushing or finishing. The action is used also124/// by the LZ-based encoders themselves.125lzma_action action;126127/// Number of elements in hash[]128uint32_t hash_count;129130/// Number of elements in son[]131uint32_t sons_count;132};133134135typedef struct {136/// Extra amount of data to keep available before the "actual"137/// dictionary.138size_t before_size;139140/// Size of the history buffer141size_t dict_size;142143/// Extra amount of data to keep available after the "actual"144/// dictionary.145size_t after_size;146147/// Maximum length of a match that the LZ-based encoder can accept.148/// This is used to extend matches of length nice_len to the149/// maximum possible length.150size_t match_len_max;151152/// Match finder will search matches up to this length.153/// This must be less than or equal to match_len_max.154size_t nice_len;155156/// Type of the match finder to use157lzma_match_finder match_finder;158159/// Maximum search depth160uint32_t depth;161162/// Initial dictionary for the match finder to search.163const uint8_t *preset_dict;164165/// If the preset dictionary is NULL, this value is ignored.166/// Otherwise this member must indicate the preset dictionary's167/// buffer size. If this size is larger than dict_size, then only168/// the dict_size sized tail of the preset_dict will be used.169uint32_t preset_dict_size;170171} lzma_lz_options;172173174// The total usable buffer space at any moment outside the match finder:175// before_size + dict_size + after_size + match_len_max176//177// In reality, there's some extra space allocated to prevent the number of178// memmove() calls reasonable. The bigger the dict_size is, the bigger179// this extra buffer will be since with bigger dictionaries memmove() would180// also take longer.181//182// A single encoder loop in the LZ-based encoder may call the match finder183// (mf_find() or mf_skip()) at most after_size times. In other words,184// a single encoder loop may increment lzma_mf.read_pos at most after_size185// times. Since matches are looked up to186// lzma_mf.buffer[lzma_mf.read_pos + match_len_max - 1], the total187// amount of extra buffer needed after dict_size becomes188// after_size + match_len_max.189//190// before_size has two uses. The first one is to keep literals available191// in cases when the LZ-based encoder has made some read ahead.192// TODO: Maybe this could be changed by making the LZ-based encoders to193// store the actual literals as they do with length-distance pairs.194//195// Algorithms such as LZMA2 first try to compress a chunk, and then check196// if the encoded result is smaller than the uncompressed one. If the chunk197// was incompressible, it is better to store it in uncompressed form in198// the output stream. To do this, the whole uncompressed chunk has to be199// still available in the history buffer. before_size achieves that.200201202typedef struct {203/// Data specific to the LZ-based encoder204void *coder;205206/// Function to encode from *dict to out[]207lzma_ret (*code)(void *coder,208lzma_mf *restrict mf, uint8_t *restrict out,209size_t *restrict out_pos, size_t out_size);210211/// Free allocated resources212void (*end)(void *coder, const lzma_allocator *allocator);213214/// Update the options in the middle of the encoding.215lzma_ret (*options_update)(void *coder, const lzma_filter *filter);216217/// Set maximum allowed output size218lzma_ret (*set_out_limit)(void *coder, uint64_t *uncomp_size,219uint64_t out_limit);220221} lzma_lz_encoder;222223224// Basic steps:225// 1. Input gets copied into the dictionary.226// 2. Data in dictionary gets run through the match finder byte by byte.227// 3. The literals and matches are encoded using e.g. LZMA.228//229// The bytes that have been ran through the match finder, but not encoded yet,230// are called 'read ahead'.231232233/// Get how many bytes the match finder hashes in its initial step.234/// This is also the minimum nice_len value with the match finder.235static inline uint32_t236mf_get_hash_bytes(lzma_match_finder match_finder)237{238return (uint32_t)match_finder & 0x0F;239}240241242/// Get pointer to the first byte not ran through the match finder243static inline const uint8_t *244mf_ptr(const lzma_mf *mf)245{246return mf->buffer + mf->read_pos;247}248249250/// Get the number of bytes that haven't been ran through the match finder yet.251static inline uint32_t252mf_avail(const lzma_mf *mf)253{254return mf->write_pos - mf->read_pos;255}256257258/// Get the number of bytes that haven't been encoded yet (some of these259/// bytes may have been ran through the match finder though).260static inline uint32_t261mf_unencoded(const lzma_mf *mf)262{263return mf->write_pos - mf->read_pos + mf->read_ahead;264}265266267/// Calculate the absolute offset from the beginning of the most recent268/// dictionary reset. Only the lowest four bits are important, so there's no269/// problem that we don't know the 64-bit size of the data encoded so far.270///271/// NOTE: When moving the input window, we need to do it so that the lowest272/// bits of dict->read_pos are not modified to keep this macro working273/// as intended.274static inline uint32_t275mf_position(const lzma_mf *mf)276{277return mf->read_pos - mf->read_ahead;278}279280281/// Since everything else begins with mf_, use it also for lzma_mf_find().282#define mf_find lzma_mf_find283284285/// Skip the given number of bytes. This is used when a good match was found.286/// For example, if mf_find() finds a match of 200 bytes long, the first byte287/// of that match was already consumed by mf_find(), and the rest 199 bytes288/// have to be skipped with mf_skip(mf, 199).289static inline void290mf_skip(lzma_mf *mf, uint32_t amount)291{292if (amount != 0) {293mf->skip(mf, amount);294mf->read_ahead += amount;295}296}297298299/// Copies at most *left number of bytes from the history buffer300/// to out[]. This is needed by LZMA2 to encode uncompressed chunks.301static inline void302mf_read(lzma_mf *mf, uint8_t *out, size_t *out_pos, size_t out_size,303size_t *left)304{305const size_t out_avail = out_size - *out_pos;306const size_t copy_size = my_min(out_avail, *left);307308assert(mf->read_ahead == 0);309assert(mf->read_pos >= *left);310311memcpy(out + *out_pos, mf->buffer + mf->read_pos - *left,312copy_size);313314*out_pos += copy_size;315*left -= copy_size;316return;317}318319320extern lzma_ret lzma_lz_encoder_init(321lzma_next_coder *next, const lzma_allocator *allocator,322const lzma_filter_info *filters,323lzma_ret (*lz_init)(lzma_lz_encoder *lz,324const lzma_allocator *allocator,325lzma_vli id, const void *options,326lzma_lz_options *lz_options));327328329extern uint64_t lzma_lz_encoder_memusage(const lzma_lz_options *lz_options);330331332// These are only for LZ encoder's internal use.333extern uint32_t lzma_mf_find(334lzma_mf *mf, uint32_t *count, lzma_match *matches);335336extern uint32_t lzma_mf_hc3_find(lzma_mf *dict, lzma_match *matches);337extern void lzma_mf_hc3_skip(lzma_mf *dict, uint32_t amount);338339extern uint32_t lzma_mf_hc4_find(lzma_mf *dict, lzma_match *matches);340extern void lzma_mf_hc4_skip(lzma_mf *dict, uint32_t amount);341342extern uint32_t lzma_mf_bt2_find(lzma_mf *dict, lzma_match *matches);343extern void lzma_mf_bt2_skip(lzma_mf *dict, uint32_t amount);344345extern uint32_t lzma_mf_bt3_find(lzma_mf *dict, lzma_match *matches);346extern void lzma_mf_bt3_skip(lzma_mf *dict, uint32_t amount);347348extern uint32_t lzma_mf_bt4_find(lzma_mf *dict, lzma_match *matches);349extern void lzma_mf_bt4_skip(lzma_mf *dict, uint32_t amount);350351#endif352353354