Path: blob/master/Utilities/cmliblzma/liblzma/lzma/lzma_common.h
3156 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file lzma_common.h5/// \brief Private definitions common to LZMA encoder and decoder6///7// Authors: Igor Pavlov8// Lasse Collin9//10///////////////////////////////////////////////////////////////////////////////1112#ifndef LZMA_LZMA_COMMON_H13#define LZMA_LZMA_COMMON_H1415#include "common.h"16#include "range_common.h"171819///////////////////20// Miscellaneous //21///////////////////2223/// Maximum number of position states. A position state is the lowest pos bits24/// number of bits of the current uncompressed offset. In some places there25/// are different sets of probabilities for different pos states.26#define POS_STATES_MAX (1 << LZMA_PB_MAX)272829/// Validates lc, lp, and pb.30static inline bool31is_lclppb_valid(const lzma_options_lzma *options)32{33return options->lc <= LZMA_LCLP_MAX && options->lp <= LZMA_LCLP_MAX34&& options->lc + options->lp <= LZMA_LCLP_MAX35&& options->pb <= LZMA_PB_MAX;36}373839///////////40// State //41///////////4243/// This enum is used to track which events have occurred most recently and44/// in which order. This information is used to predict the next event.45///46/// Events:47/// - Literal: One 8-bit byte48/// - Match: Repeat a chunk of data at some distance49/// - Long repeat: Multi-byte match at a recently seen distance50/// - Short repeat: One-byte repeat at a recently seen distance51///52/// The event names are in from STATE_oldest_older_previous. REP means53/// either short or long repeated match, and NONLIT means any non-literal.54typedef enum {55STATE_LIT_LIT,56STATE_MATCH_LIT_LIT,57STATE_REP_LIT_LIT,58STATE_SHORTREP_LIT_LIT,59STATE_MATCH_LIT,60STATE_REP_LIT,61STATE_SHORTREP_LIT,62STATE_LIT_MATCH,63STATE_LIT_LONGREP,64STATE_LIT_SHORTREP,65STATE_NONLIT_MATCH,66STATE_NONLIT_REP,67} lzma_lzma_state;686970/// Total number of states71#define STATES 127273/// The lowest 7 states indicate that the previous state was a literal.74#define LIT_STATES 7757677/// Indicate that the latest state was a literal.78#define update_literal(state) \79state = ((state) <= STATE_SHORTREP_LIT_LIT \80? STATE_LIT_LIT \81: ((state) <= STATE_LIT_SHORTREP \82? (state) - 3 \83: (state) - 6))8485/// Like update_literal(state) but when it is already known that86/// is_literal_state(state) is true.87#define update_literal_normal(state) \88state = ((state) <= STATE_SHORTREP_LIT_LIT \89? STATE_LIT_LIT \90: (state) - 3);9192/// Like update_literal(state) but when it is already known that93/// is_literal_state(state) is false.94#define update_literal_matched(state) \95state = ((state) <= STATE_LIT_SHORTREP \96? (state) - 3 \97: (state) - 6);9899/// Indicate that the latest state was a match.100#define update_match(state) \101state = ((state) < LIT_STATES ? STATE_LIT_MATCH : STATE_NONLIT_MATCH)102103/// Indicate that the latest state was a long repeated match.104#define update_long_rep(state) \105state = ((state) < LIT_STATES ? STATE_LIT_LONGREP : STATE_NONLIT_REP)106107/// Indicate that the latest state was a short match.108#define update_short_rep(state) \109state = ((state) < LIT_STATES ? STATE_LIT_SHORTREP : STATE_NONLIT_REP)110111/// Test if the previous state was a literal.112#define is_literal_state(state) \113((state) < LIT_STATES)114115116/////////////117// Literal //118/////////////119120/// Each literal coder is divided in three sections:121/// - 0x001-0x0FF: Without match byte122/// - 0x101-0x1FF: With match byte; match bit is 0123/// - 0x201-0x2FF: With match byte; match bit is 1124///125/// Match byte is used when the previous LZMA symbol was something else than126/// a literal (that is, it was some kind of match).127#define LITERAL_CODER_SIZE UINT32_C(0x300)128129/// Maximum number of literal coders130#define LITERAL_CODERS_MAX (1 << LZMA_LCLP_MAX)131132/// Calculates the literal_mask that literal_subcoder() needs.133#define literal_mask_calc(lc, lp) \134((UINT32_C(0x100) << (lp)) - (UINT32_C(0x100) >> (lc)))135136/// Locate the literal coder for the next literal byte. The choice depends on137/// - the lowest literal_pos_bits bits of the position of the current138/// byte; and139/// - the highest literal_context_bits bits of the previous byte.140#define literal_subcoder(probs, lc, literal_mask, pos, prev_byte) \141((probs) + UINT32_C(3) * \142(((((pos) << 8) + (prev_byte)) & (literal_mask)) << (lc)))143144145static inline void146literal_init(probability *probs, uint32_t lc, uint32_t lp)147{148assert(lc + lp <= LZMA_LCLP_MAX);149150const size_t coders = LITERAL_CODER_SIZE << (lc + lp);151152for (size_t i = 0; i < coders; ++i)153bit_reset(probs[i]);154155return;156}157158159//////////////////160// Match length //161//////////////////162163// Minimum length of a match is two bytes.164#define MATCH_LEN_MIN 2165166// Match length is encoded with 4, 5, or 10 bits.167//168// Length Bits169// 2-9 4 = Choice=0 + 3 bits170// 10-17 5 = Choice=1 + Choice2=0 + 3 bits171// 18-273 10 = Choice=1 + Choice2=1 + 8 bits172#define LEN_LOW_BITS 3173#define LEN_LOW_SYMBOLS (1 << LEN_LOW_BITS)174#define LEN_MID_BITS 3175#define LEN_MID_SYMBOLS (1 << LEN_MID_BITS)176#define LEN_HIGH_BITS 8177#define LEN_HIGH_SYMBOLS (1 << LEN_HIGH_BITS)178#define LEN_SYMBOLS (LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS + LEN_HIGH_SYMBOLS)179180// Maximum length of a match is 273 which is a result of the encoding181// described above.182#define MATCH_LEN_MAX (MATCH_LEN_MIN + LEN_SYMBOLS - 1)183184185////////////////////186// Match distance //187////////////////////188189// Different sets of probabilities are used for match distances that have very190// short match length: Lengths of 2, 3, and 4 bytes have a separate set of191// probabilities for each length. The matches with longer length use a shared192// set of probabilities.193#define DIST_STATES 4194195// Macro to get the index of the appropriate probability array.196#define get_dist_state(len) \197((len) < DIST_STATES + MATCH_LEN_MIN \198? (len) - MATCH_LEN_MIN \199: DIST_STATES - 1)200201// The highest two bits of a match distance (distance slot) are encoded202// using six bits. See fastpos.h for more explanation.203#define DIST_SLOT_BITS 6204#define DIST_SLOTS (1 << DIST_SLOT_BITS)205206// Match distances up to 127 are fully encoded using probabilities. Since207// the highest two bits (distance slot) are always encoded using six bits,208// the distances 0-3 don't need any additional bits to encode, since the209// distance slot itself is the same as the actual distance. DIST_MODEL_START210// indicates the first distance slot where at least one additional bit is211// needed.212#define DIST_MODEL_START 4213214// Match distances greater than 127 are encoded in three pieces:215// - distance slot: the highest two bits216// - direct bits: 2-26 bits below the highest two bits217// - alignment bits: four lowest bits218//219// Direct bits don't use any probabilities.220//221// The distance slot value of 14 is for distances 128-191 (see the table in222// fastpos.h to understand why).223#define DIST_MODEL_END 14224225// Distance slots that indicate a distance <= 127.226#define FULL_DISTANCES_BITS (DIST_MODEL_END / 2)227#define FULL_DISTANCES (1 << FULL_DISTANCES_BITS)228229// For match distances greater than 127, only the highest two bits and the230// lowest four bits (alignment) is encoded using probabilities.231#define ALIGN_BITS 4232#define ALIGN_SIZE (1 << ALIGN_BITS)233#define ALIGN_MASK (ALIGN_SIZE - 1)234235// LZMA remembers the four most recent match distances. Reusing these distances236// tends to take less space than re-encoding the actual distance value.237#define REPS 4238239#endif240241242