xref: /kernel/linux/linux-5.10/lib/xz/xz_lzma2.h (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * LZMA2 definitions
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Authors: Lasse Collin <lasse.collin@tukaani.org>
58c2ecf20Sopenharmony_ci *          Igor Pavlov <https://7-zip.org/>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * This file has been put into the public domain.
88c2ecf20Sopenharmony_ci * You can do whatever you want with this file.
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#ifndef XZ_LZMA2_H
128c2ecf20Sopenharmony_ci#define XZ_LZMA2_H
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/* Range coder constants */
158c2ecf20Sopenharmony_ci#define RC_SHIFT_BITS 8
168c2ecf20Sopenharmony_ci#define RC_TOP_BITS 24
178c2ecf20Sopenharmony_ci#define RC_TOP_VALUE (1 << RC_TOP_BITS)
188c2ecf20Sopenharmony_ci#define RC_BIT_MODEL_TOTAL_BITS 11
198c2ecf20Sopenharmony_ci#define RC_BIT_MODEL_TOTAL (1 << RC_BIT_MODEL_TOTAL_BITS)
208c2ecf20Sopenharmony_ci#define RC_MOVE_BITS 5
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/*
238c2ecf20Sopenharmony_ci * Maximum number of position states. A position state is the lowest pb
248c2ecf20Sopenharmony_ci * number of bits of the current uncompressed offset. In some places there
258c2ecf20Sopenharmony_ci * are different sets of probabilities for different position states.
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_ci#define POS_STATES_MAX (1 << 4)
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/*
308c2ecf20Sopenharmony_ci * This enum is used to track which LZMA symbols have occurred most recently
318c2ecf20Sopenharmony_ci * and in which order. This information is used to predict the next symbol.
328c2ecf20Sopenharmony_ci *
338c2ecf20Sopenharmony_ci * Symbols:
348c2ecf20Sopenharmony_ci *  - Literal: One 8-bit byte
358c2ecf20Sopenharmony_ci *  - Match: Repeat a chunk of data at some distance
368c2ecf20Sopenharmony_ci *  - Long repeat: Multi-byte match at a recently seen distance
378c2ecf20Sopenharmony_ci *  - Short repeat: One-byte repeat at a recently seen distance
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci * The symbol names are in from STATE_oldest_older_previous. REP means
408c2ecf20Sopenharmony_ci * either short or long repeated match, and NONLIT means any non-literal.
418c2ecf20Sopenharmony_ci */
428c2ecf20Sopenharmony_cienum lzma_state {
438c2ecf20Sopenharmony_ci	STATE_LIT_LIT,
448c2ecf20Sopenharmony_ci	STATE_MATCH_LIT_LIT,
458c2ecf20Sopenharmony_ci	STATE_REP_LIT_LIT,
468c2ecf20Sopenharmony_ci	STATE_SHORTREP_LIT_LIT,
478c2ecf20Sopenharmony_ci	STATE_MATCH_LIT,
488c2ecf20Sopenharmony_ci	STATE_REP_LIT,
498c2ecf20Sopenharmony_ci	STATE_SHORTREP_LIT,
508c2ecf20Sopenharmony_ci	STATE_LIT_MATCH,
518c2ecf20Sopenharmony_ci	STATE_LIT_LONGREP,
528c2ecf20Sopenharmony_ci	STATE_LIT_SHORTREP,
538c2ecf20Sopenharmony_ci	STATE_NONLIT_MATCH,
548c2ecf20Sopenharmony_ci	STATE_NONLIT_REP
558c2ecf20Sopenharmony_ci};
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/* Total number of states */
588c2ecf20Sopenharmony_ci#define STATES 12
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/* The lowest 7 states indicate that the previous state was a literal. */
618c2ecf20Sopenharmony_ci#define LIT_STATES 7
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci/* Indicate that the latest symbol was a literal. */
648c2ecf20Sopenharmony_cistatic inline void lzma_state_literal(enum lzma_state *state)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	if (*state <= STATE_SHORTREP_LIT_LIT)
678c2ecf20Sopenharmony_ci		*state = STATE_LIT_LIT;
688c2ecf20Sopenharmony_ci	else if (*state <= STATE_LIT_SHORTREP)
698c2ecf20Sopenharmony_ci		*state -= 3;
708c2ecf20Sopenharmony_ci	else
718c2ecf20Sopenharmony_ci		*state -= 6;
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/* Indicate that the latest symbol was a match. */
758c2ecf20Sopenharmony_cistatic inline void lzma_state_match(enum lzma_state *state)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	*state = *state < LIT_STATES ? STATE_LIT_MATCH : STATE_NONLIT_MATCH;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/* Indicate that the latest state was a long repeated match. */
818c2ecf20Sopenharmony_cistatic inline void lzma_state_long_rep(enum lzma_state *state)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	*state = *state < LIT_STATES ? STATE_LIT_LONGREP : STATE_NONLIT_REP;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci/* Indicate that the latest symbol was a short match. */
878c2ecf20Sopenharmony_cistatic inline void lzma_state_short_rep(enum lzma_state *state)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	*state = *state < LIT_STATES ? STATE_LIT_SHORTREP : STATE_NONLIT_REP;
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/* Test if the previous symbol was a literal. */
938c2ecf20Sopenharmony_cistatic inline bool lzma_state_is_literal(enum lzma_state state)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	return state < LIT_STATES;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci/* Each literal coder is divided in three sections:
998c2ecf20Sopenharmony_ci *   - 0x001-0x0FF: Without match byte
1008c2ecf20Sopenharmony_ci *   - 0x101-0x1FF: With match byte; match bit is 0
1018c2ecf20Sopenharmony_ci *   - 0x201-0x2FF: With match byte; match bit is 1
1028c2ecf20Sopenharmony_ci *
1038c2ecf20Sopenharmony_ci * Match byte is used when the previous LZMA symbol was something else than
1048c2ecf20Sopenharmony_ci * a literal (that is, it was some kind of match).
1058c2ecf20Sopenharmony_ci */
1068c2ecf20Sopenharmony_ci#define LITERAL_CODER_SIZE 0x300
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci/* Maximum number of literal coders */
1098c2ecf20Sopenharmony_ci#define LITERAL_CODERS_MAX (1 << 4)
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci/* Minimum length of a match is two bytes. */
1128c2ecf20Sopenharmony_ci#define MATCH_LEN_MIN 2
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci/* Match length is encoded with 4, 5, or 10 bits.
1158c2ecf20Sopenharmony_ci *
1168c2ecf20Sopenharmony_ci * Length   Bits
1178c2ecf20Sopenharmony_ci *  2-9      4 = Choice=0 + 3 bits
1188c2ecf20Sopenharmony_ci * 10-17     5 = Choice=1 + Choice2=0 + 3 bits
1198c2ecf20Sopenharmony_ci * 18-273   10 = Choice=1 + Choice2=1 + 8 bits
1208c2ecf20Sopenharmony_ci */
1218c2ecf20Sopenharmony_ci#define LEN_LOW_BITS 3
1228c2ecf20Sopenharmony_ci#define LEN_LOW_SYMBOLS (1 << LEN_LOW_BITS)
1238c2ecf20Sopenharmony_ci#define LEN_MID_BITS 3
1248c2ecf20Sopenharmony_ci#define LEN_MID_SYMBOLS (1 << LEN_MID_BITS)
1258c2ecf20Sopenharmony_ci#define LEN_HIGH_BITS 8
1268c2ecf20Sopenharmony_ci#define LEN_HIGH_SYMBOLS (1 << LEN_HIGH_BITS)
1278c2ecf20Sopenharmony_ci#define LEN_SYMBOLS (LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS + LEN_HIGH_SYMBOLS)
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci/*
1308c2ecf20Sopenharmony_ci * Maximum length of a match is 273 which is a result of the encoding
1318c2ecf20Sopenharmony_ci * described above.
1328c2ecf20Sopenharmony_ci */
1338c2ecf20Sopenharmony_ci#define MATCH_LEN_MAX (MATCH_LEN_MIN + LEN_SYMBOLS - 1)
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/*
1368c2ecf20Sopenharmony_ci * Different sets of probabilities are used for match distances that have
1378c2ecf20Sopenharmony_ci * very short match length: Lengths of 2, 3, and 4 bytes have a separate
1388c2ecf20Sopenharmony_ci * set of probabilities for each length. The matches with longer length
1398c2ecf20Sopenharmony_ci * use a shared set of probabilities.
1408c2ecf20Sopenharmony_ci */
1418c2ecf20Sopenharmony_ci#define DIST_STATES 4
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci/*
1448c2ecf20Sopenharmony_ci * Get the index of the appropriate probability array for decoding
1458c2ecf20Sopenharmony_ci * the distance slot.
1468c2ecf20Sopenharmony_ci */
1478c2ecf20Sopenharmony_cistatic inline uint32_t lzma_get_dist_state(uint32_t len)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	return len < DIST_STATES + MATCH_LEN_MIN
1508c2ecf20Sopenharmony_ci			? len - MATCH_LEN_MIN : DIST_STATES - 1;
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci/*
1548c2ecf20Sopenharmony_ci * The highest two bits of a 32-bit match distance are encoded using six bits.
1558c2ecf20Sopenharmony_ci * This six-bit value is called a distance slot. This way encoding a 32-bit
1568c2ecf20Sopenharmony_ci * value takes 6-36 bits, larger values taking more bits.
1578c2ecf20Sopenharmony_ci */
1588c2ecf20Sopenharmony_ci#define DIST_SLOT_BITS 6
1598c2ecf20Sopenharmony_ci#define DIST_SLOTS (1 << DIST_SLOT_BITS)
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci/* Match distances up to 127 are fully encoded using probabilities. Since
1628c2ecf20Sopenharmony_ci * the highest two bits (distance slot) are always encoded using six bits,
1638c2ecf20Sopenharmony_ci * the distances 0-3 don't need any additional bits to encode, since the
1648c2ecf20Sopenharmony_ci * distance slot itself is the same as the actual distance. DIST_MODEL_START
1658c2ecf20Sopenharmony_ci * indicates the first distance slot where at least one additional bit is
1668c2ecf20Sopenharmony_ci * needed.
1678c2ecf20Sopenharmony_ci */
1688c2ecf20Sopenharmony_ci#define DIST_MODEL_START 4
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci/*
1718c2ecf20Sopenharmony_ci * Match distances greater than 127 are encoded in three pieces:
1728c2ecf20Sopenharmony_ci *   - distance slot: the highest two bits
1738c2ecf20Sopenharmony_ci *   - direct bits: 2-26 bits below the highest two bits
1748c2ecf20Sopenharmony_ci *   - alignment bits: four lowest bits
1758c2ecf20Sopenharmony_ci *
1768c2ecf20Sopenharmony_ci * Direct bits don't use any probabilities.
1778c2ecf20Sopenharmony_ci *
1788c2ecf20Sopenharmony_ci * The distance slot value of 14 is for distances 128-191.
1798c2ecf20Sopenharmony_ci */
1808c2ecf20Sopenharmony_ci#define DIST_MODEL_END 14
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/* Distance slots that indicate a distance <= 127. */
1838c2ecf20Sopenharmony_ci#define FULL_DISTANCES_BITS (DIST_MODEL_END / 2)
1848c2ecf20Sopenharmony_ci#define FULL_DISTANCES (1 << FULL_DISTANCES_BITS)
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci/*
1878c2ecf20Sopenharmony_ci * For match distances greater than 127, only the highest two bits and the
1888c2ecf20Sopenharmony_ci * lowest four bits (alignment) is encoded using probabilities.
1898c2ecf20Sopenharmony_ci */
1908c2ecf20Sopenharmony_ci#define ALIGN_BITS 4
1918c2ecf20Sopenharmony_ci#define ALIGN_SIZE (1 << ALIGN_BITS)
1928c2ecf20Sopenharmony_ci#define ALIGN_MASK (ALIGN_SIZE - 1)
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci/* Total number of all probability variables */
1958c2ecf20Sopenharmony_ci#define PROBS_TOTAL (1846 + LITERAL_CODERS_MAX * LITERAL_CODER_SIZE)
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci/*
1988c2ecf20Sopenharmony_ci * LZMA remembers the four most recent match distances. Reusing these
1998c2ecf20Sopenharmony_ci * distances tends to take less space than re-encoding the actual
2008c2ecf20Sopenharmony_ci * distance value.
2018c2ecf20Sopenharmony_ci */
2028c2ecf20Sopenharmony_ci#define REPS 4
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci#endif
205