Path: blob/main/sys/contrib/xz-embedded/linux/lib/xz/xz_lzma2.h
48521 views
/*1* LZMA2 definitions2*3* Authors: Lasse Collin <[email protected]>4* Igor Pavlov <https://7-zip.org/>5*6* This file has been put into the public domain.7* You can do whatever you want with this file.8*/910#ifndef XZ_LZMA2_H11#define XZ_LZMA2_H1213/* Range coder constants */14#define RC_SHIFT_BITS 815#define RC_TOP_BITS 2416#define RC_TOP_VALUE (1 << RC_TOP_BITS)17#define RC_BIT_MODEL_TOTAL_BITS 1118#define RC_BIT_MODEL_TOTAL (1 << RC_BIT_MODEL_TOTAL_BITS)19#define RC_MOVE_BITS 52021/*22* Maximum number of position states. A position state is the lowest pb23* number of bits of the current uncompressed offset. In some places there24* are different sets of probabilities for different position states.25*/26#define POS_STATES_MAX (1 << 4)2728/*29* This enum is used to track which LZMA symbols have occurred most recently30* and in which order. This information is used to predict the next symbol.31*32* Symbols:33* - Literal: One 8-bit byte34* - Match: Repeat a chunk of data at some distance35* - Long repeat: Multi-byte match at a recently seen distance36* - Short repeat: One-byte repeat at a recently seen distance37*38* The symbol names are in from STATE_oldest_older_previous. REP means39* either short or long repeated match, and NONLIT means any non-literal.40*/41enum lzma_state {42STATE_LIT_LIT,43STATE_MATCH_LIT_LIT,44STATE_REP_LIT_LIT,45STATE_SHORTREP_LIT_LIT,46STATE_MATCH_LIT,47STATE_REP_LIT,48STATE_SHORTREP_LIT,49STATE_LIT_MATCH,50STATE_LIT_LONGREP,51STATE_LIT_SHORTREP,52STATE_NONLIT_MATCH,53STATE_NONLIT_REP54};5556/* Total number of states */57#define STATES 125859/* The lowest 7 states indicate that the previous state was a literal. */60#define LIT_STATES 76162/* Indicate that the latest symbol was a literal. */63static inline void lzma_state_literal(enum lzma_state *state)64{65if (*state <= STATE_SHORTREP_LIT_LIT)66*state = STATE_LIT_LIT;67else if (*state <= STATE_LIT_SHORTREP)68*state -= 3;69else70*state -= 6;71}7273/* Indicate that the latest symbol was a match. */74static inline void lzma_state_match(enum lzma_state *state)75{76*state = *state < LIT_STATES ? STATE_LIT_MATCH : STATE_NONLIT_MATCH;77}7879/* Indicate that the latest state was a long repeated match. */80static inline void lzma_state_long_rep(enum lzma_state *state)81{82*state = *state < LIT_STATES ? STATE_LIT_LONGREP : STATE_NONLIT_REP;83}8485/* Indicate that the latest symbol was a short match. */86static inline void lzma_state_short_rep(enum lzma_state *state)87{88*state = *state < LIT_STATES ? STATE_LIT_SHORTREP : STATE_NONLIT_REP;89}9091/* Test if the previous symbol was a literal. */92static inline bool lzma_state_is_literal(enum lzma_state state)93{94return state < LIT_STATES;95}9697/* Each literal coder is divided in three sections:98* - 0x001-0x0FF: Without match byte99* - 0x101-0x1FF: With match byte; match bit is 0100* - 0x201-0x2FF: With match byte; match bit is 1101*102* Match byte is used when the previous LZMA symbol was something else than103* a literal (that is, it was some kind of match).104*/105#define LITERAL_CODER_SIZE 0x300106107/* Maximum number of literal coders */108#define LITERAL_CODERS_MAX (1 << 4)109110/* Minimum length of a match is two bytes. */111#define MATCH_LEN_MIN 2112113/* Match length is encoded with 4, 5, or 10 bits.114*115* Length Bits116* 2-9 4 = Choice=0 + 3 bits117* 10-17 5 = Choice=1 + Choice2=0 + 3 bits118* 18-273 10 = Choice=1 + Choice2=1 + 8 bits119*/120#define LEN_LOW_BITS 3121#define LEN_LOW_SYMBOLS (1 << LEN_LOW_BITS)122#define LEN_MID_BITS 3123#define LEN_MID_SYMBOLS (1 << LEN_MID_BITS)124#define LEN_HIGH_BITS 8125#define LEN_HIGH_SYMBOLS (1 << LEN_HIGH_BITS)126#define LEN_SYMBOLS (LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS + LEN_HIGH_SYMBOLS)127128/*129* Maximum length of a match is 273 which is a result of the encoding130* described above.131*/132#define MATCH_LEN_MAX (MATCH_LEN_MIN + LEN_SYMBOLS - 1)133134/*135* Different sets of probabilities are used for match distances that have136* very short match length: Lengths of 2, 3, and 4 bytes have a separate137* set of probabilities for each length. The matches with longer length138* use a shared set of probabilities.139*/140#define DIST_STATES 4141142/*143* Get the index of the appropriate probability array for decoding144* the distance slot.145*/146static inline uint32_t lzma_get_dist_state(uint32_t len)147{148return len < DIST_STATES + MATCH_LEN_MIN149? len - MATCH_LEN_MIN : DIST_STATES - 1;150}151152/*153* The highest two bits of a 32-bit match distance are encoded using six bits.154* This six-bit value is called a distance slot. This way encoding a 32-bit155* value takes 6-36 bits, larger values taking more bits.156*/157#define DIST_SLOT_BITS 6158#define DIST_SLOTS (1 << DIST_SLOT_BITS)159160/* Match distances up to 127 are fully encoded using probabilities. Since161* the highest two bits (distance slot) are always encoded using six bits,162* the distances 0-3 don't need any additional bits to encode, since the163* distance slot itself is the same as the actual distance. DIST_MODEL_START164* indicates the first distance slot where at least one additional bit is165* needed.166*/167#define DIST_MODEL_START 4168169/*170* Match distances greater than 127 are encoded in three pieces:171* - distance slot: the highest two bits172* - direct bits: 2-26 bits below the highest two bits173* - alignment bits: four lowest bits174*175* Direct bits don't use any probabilities.176*177* The distance slot value of 14 is for distances 128-191.178*/179#define DIST_MODEL_END 14180181/* Distance slots that indicate a distance <= 127. */182#define FULL_DISTANCES_BITS (DIST_MODEL_END / 2)183#define FULL_DISTANCES (1 << FULL_DISTANCES_BITS)184185/*186* For match distances greater than 127, only the highest two bits and the187* lowest four bits (alignment) is encoded using probabilities.188*/189#define ALIGN_BITS 4190#define ALIGN_SIZE (1 << ALIGN_BITS)191#define ALIGN_MASK (ALIGN_SIZE - 1)192193/* Total number of all probability variables */194#define PROBS_TOTAL (1846 + LITERAL_CODERS_MAX * LITERAL_CODER_SIZE)195196/*197* LZMA remembers the four most recent match distances. Reusing these198* distances tends to take less space than re-encoding the actual199* distance value.200*/201#define REPS 4202203#endif204205206