1*d2c16535SElliott Hughes /* SPDX-License-Identifier: 0BSD */
2*d2c16535SElliott Hughes
3*d2c16535SElliott Hughes /*
4*d2c16535SElliott Hughes * LZMA2 definitions
5*d2c16535SElliott Hughes *
6*d2c16535SElliott Hughes * Authors: Lasse Collin <[email protected]>
7*d2c16535SElliott Hughes * Igor Pavlov <https://7-zip.org/>
8*d2c16535SElliott Hughes */
9*d2c16535SElliott Hughes
10*d2c16535SElliott Hughes #ifndef XZ_LZMA2_H
11*d2c16535SElliott Hughes #define XZ_LZMA2_H
12*d2c16535SElliott Hughes
13*d2c16535SElliott Hughes /* Range coder constants */
14*d2c16535SElliott Hughes #define RC_SHIFT_BITS 8
15*d2c16535SElliott Hughes #define RC_TOP_BITS 24
16*d2c16535SElliott Hughes #define RC_TOP_VALUE (1 << RC_TOP_BITS)
17*d2c16535SElliott Hughes #define RC_BIT_MODEL_TOTAL_BITS 11
18*d2c16535SElliott Hughes #define RC_BIT_MODEL_TOTAL (1 << RC_BIT_MODEL_TOTAL_BITS)
19*d2c16535SElliott Hughes #define RC_MOVE_BITS 5
20*d2c16535SElliott Hughes
21*d2c16535SElliott Hughes /*
22*d2c16535SElliott Hughes * Maximum number of position states. A position state is the lowest pb
23*d2c16535SElliott Hughes * number of bits of the current uncompressed offset. In some places there
24*d2c16535SElliott Hughes * are different sets of probabilities for different position states.
25*d2c16535SElliott Hughes */
26*d2c16535SElliott Hughes #define POS_STATES_MAX (1 << 4)
27*d2c16535SElliott Hughes
28*d2c16535SElliott Hughes /*
29*d2c16535SElliott Hughes * This enum is used to track which LZMA symbols have occurred most recently
30*d2c16535SElliott Hughes * and in which order. This information is used to predict the next symbol.
31*d2c16535SElliott Hughes *
32*d2c16535SElliott Hughes * Symbols:
33*d2c16535SElliott Hughes * - Literal: One 8-bit byte
34*d2c16535SElliott Hughes * - Match: Repeat a chunk of data at some distance
35*d2c16535SElliott Hughes * - Long repeat: Multi-byte match at a recently seen distance
36*d2c16535SElliott Hughes * - Short repeat: One-byte repeat at a recently seen distance
37*d2c16535SElliott Hughes *
38*d2c16535SElliott Hughes * The symbol names are in from STATE_oldest_older_previous. REP means
39*d2c16535SElliott Hughes * either short or long repeated match, and NONLIT means any non-literal.
40*d2c16535SElliott Hughes */
41*d2c16535SElliott Hughes enum lzma_state {
42*d2c16535SElliott Hughes STATE_LIT_LIT,
43*d2c16535SElliott Hughes STATE_MATCH_LIT_LIT,
44*d2c16535SElliott Hughes STATE_REP_LIT_LIT,
45*d2c16535SElliott Hughes STATE_SHORTREP_LIT_LIT,
46*d2c16535SElliott Hughes STATE_MATCH_LIT,
47*d2c16535SElliott Hughes STATE_REP_LIT,
48*d2c16535SElliott Hughes STATE_SHORTREP_LIT,
49*d2c16535SElliott Hughes STATE_LIT_MATCH,
50*d2c16535SElliott Hughes STATE_LIT_LONGREP,
51*d2c16535SElliott Hughes STATE_LIT_SHORTREP,
52*d2c16535SElliott Hughes STATE_NONLIT_MATCH,
53*d2c16535SElliott Hughes STATE_NONLIT_REP
54*d2c16535SElliott Hughes };
55*d2c16535SElliott Hughes
56*d2c16535SElliott Hughes /* Total number of states */
57*d2c16535SElliott Hughes #define STATES 12
58*d2c16535SElliott Hughes
59*d2c16535SElliott Hughes /* The lowest 7 states indicate that the previous state was a literal. */
60*d2c16535SElliott Hughes #define LIT_STATES 7
61*d2c16535SElliott Hughes
62*d2c16535SElliott Hughes /* Indicate that the latest symbol was a literal. */
lzma_state_literal(enum lzma_state * state)63*d2c16535SElliott Hughes static inline void lzma_state_literal(enum lzma_state *state)
64*d2c16535SElliott Hughes {
65*d2c16535SElliott Hughes if (*state <= STATE_SHORTREP_LIT_LIT)
66*d2c16535SElliott Hughes *state = STATE_LIT_LIT;
67*d2c16535SElliott Hughes else if (*state <= STATE_LIT_SHORTREP)
68*d2c16535SElliott Hughes *state -= 3;
69*d2c16535SElliott Hughes else
70*d2c16535SElliott Hughes *state -= 6;
71*d2c16535SElliott Hughes }
72*d2c16535SElliott Hughes
73*d2c16535SElliott Hughes /* Indicate that the latest symbol was a match. */
lzma_state_match(enum lzma_state * state)74*d2c16535SElliott Hughes static inline void lzma_state_match(enum lzma_state *state)
75*d2c16535SElliott Hughes {
76*d2c16535SElliott Hughes *state = *state < LIT_STATES ? STATE_LIT_MATCH : STATE_NONLIT_MATCH;
77*d2c16535SElliott Hughes }
78*d2c16535SElliott Hughes
79*d2c16535SElliott Hughes /* Indicate that the latest state was a long repeated match. */
lzma_state_long_rep(enum lzma_state * state)80*d2c16535SElliott Hughes static inline void lzma_state_long_rep(enum lzma_state *state)
81*d2c16535SElliott Hughes {
82*d2c16535SElliott Hughes *state = *state < LIT_STATES ? STATE_LIT_LONGREP : STATE_NONLIT_REP;
83*d2c16535SElliott Hughes }
84*d2c16535SElliott Hughes
85*d2c16535SElliott Hughes /* Indicate that the latest symbol was a short match. */
lzma_state_short_rep(enum lzma_state * state)86*d2c16535SElliott Hughes static inline void lzma_state_short_rep(enum lzma_state *state)
87*d2c16535SElliott Hughes {
88*d2c16535SElliott Hughes *state = *state < LIT_STATES ? STATE_LIT_SHORTREP : STATE_NONLIT_REP;
89*d2c16535SElliott Hughes }
90*d2c16535SElliott Hughes
91*d2c16535SElliott Hughes /* Test if the previous symbol was a literal. */
lzma_state_is_literal(enum lzma_state state)92*d2c16535SElliott Hughes static inline bool lzma_state_is_literal(enum lzma_state state)
93*d2c16535SElliott Hughes {
94*d2c16535SElliott Hughes return state < LIT_STATES;
95*d2c16535SElliott Hughes }
96*d2c16535SElliott Hughes
97*d2c16535SElliott Hughes /* Each literal coder is divided in three sections:
98*d2c16535SElliott Hughes * - 0x001-0x0FF: Without match byte
99*d2c16535SElliott Hughes * - 0x101-0x1FF: With match byte; match bit is 0
100*d2c16535SElliott Hughes * - 0x201-0x2FF: With match byte; match bit is 1
101*d2c16535SElliott Hughes *
102*d2c16535SElliott Hughes * Match byte is used when the previous LZMA symbol was something else than
103*d2c16535SElliott Hughes * a literal (that is, it was some kind of match).
104*d2c16535SElliott Hughes */
105*d2c16535SElliott Hughes #define LITERAL_CODER_SIZE 0x300
106*d2c16535SElliott Hughes
107*d2c16535SElliott Hughes /* Maximum number of literal coders */
108*d2c16535SElliott Hughes #define LITERAL_CODERS_MAX (1 << 4)
109*d2c16535SElliott Hughes
110*d2c16535SElliott Hughes /* Minimum length of a match is two bytes. */
111*d2c16535SElliott Hughes #define MATCH_LEN_MIN 2
112*d2c16535SElliott Hughes
113*d2c16535SElliott Hughes /* Match length is encoded with 4, 5, or 10 bits.
114*d2c16535SElliott Hughes *
115*d2c16535SElliott Hughes * Length Bits
116*d2c16535SElliott Hughes * 2-9 4 = Choice=0 + 3 bits
117*d2c16535SElliott Hughes * 10-17 5 = Choice=1 + Choice2=0 + 3 bits
118*d2c16535SElliott Hughes * 18-273 10 = Choice=1 + Choice2=1 + 8 bits
119*d2c16535SElliott Hughes */
120*d2c16535SElliott Hughes #define LEN_LOW_BITS 3
121*d2c16535SElliott Hughes #define LEN_LOW_SYMBOLS (1 << LEN_LOW_BITS)
122*d2c16535SElliott Hughes #define LEN_MID_BITS 3
123*d2c16535SElliott Hughes #define LEN_MID_SYMBOLS (1 << LEN_MID_BITS)
124*d2c16535SElliott Hughes #define LEN_HIGH_BITS 8
125*d2c16535SElliott Hughes #define LEN_HIGH_SYMBOLS (1 << LEN_HIGH_BITS)
126*d2c16535SElliott Hughes #define LEN_SYMBOLS (LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS + LEN_HIGH_SYMBOLS)
127*d2c16535SElliott Hughes
128*d2c16535SElliott Hughes /*
129*d2c16535SElliott Hughes * Maximum length of a match is 273 which is a result of the encoding
130*d2c16535SElliott Hughes * described above.
131*d2c16535SElliott Hughes */
132*d2c16535SElliott Hughes #define MATCH_LEN_MAX (MATCH_LEN_MIN + LEN_SYMBOLS - 1)
133*d2c16535SElliott Hughes
134*d2c16535SElliott Hughes /*
135*d2c16535SElliott Hughes * Different sets of probabilities are used for match distances that have
136*d2c16535SElliott Hughes * very short match length: Lengths of 2, 3, and 4 bytes have a separate
137*d2c16535SElliott Hughes * set of probabilities for each length. The matches with longer length
138*d2c16535SElliott Hughes * use a shared set of probabilities.
139*d2c16535SElliott Hughes */
140*d2c16535SElliott Hughes #define DIST_STATES 4
141*d2c16535SElliott Hughes
142*d2c16535SElliott Hughes /*
143*d2c16535SElliott Hughes * Get the index of the appropriate probability array for decoding
144*d2c16535SElliott Hughes * the distance slot.
145*d2c16535SElliott Hughes */
lzma_get_dist_state(uint32_t len)146*d2c16535SElliott Hughes static inline uint32_t lzma_get_dist_state(uint32_t len)
147*d2c16535SElliott Hughes {
148*d2c16535SElliott Hughes return len < DIST_STATES + MATCH_LEN_MIN
149*d2c16535SElliott Hughes ? len - MATCH_LEN_MIN : DIST_STATES - 1;
150*d2c16535SElliott Hughes }
151*d2c16535SElliott Hughes
152*d2c16535SElliott Hughes /*
153*d2c16535SElliott Hughes * The highest two bits of a 32-bit match distance are encoded using six bits.
154*d2c16535SElliott Hughes * This six-bit value is called a distance slot. This way encoding a 32-bit
155*d2c16535SElliott Hughes * value takes 6-36 bits, larger values taking more bits.
156*d2c16535SElliott Hughes */
157*d2c16535SElliott Hughes #define DIST_SLOT_BITS 6
158*d2c16535SElliott Hughes #define DIST_SLOTS (1 << DIST_SLOT_BITS)
159*d2c16535SElliott Hughes
160*d2c16535SElliott Hughes /* Match distances up to 127 are fully encoded using probabilities. Since
161*d2c16535SElliott Hughes * the highest two bits (distance slot) are always encoded using six bits,
162*d2c16535SElliott Hughes * the distances 0-3 don't need any additional bits to encode, since the
163*d2c16535SElliott Hughes * distance slot itself is the same as the actual distance. DIST_MODEL_START
164*d2c16535SElliott Hughes * indicates the first distance slot where at least one additional bit is
165*d2c16535SElliott Hughes * needed.
166*d2c16535SElliott Hughes */
167*d2c16535SElliott Hughes #define DIST_MODEL_START 4
168*d2c16535SElliott Hughes
169*d2c16535SElliott Hughes /*
170*d2c16535SElliott Hughes * Match distances greater than 127 are encoded in three pieces:
171*d2c16535SElliott Hughes * - distance slot: the highest two bits
172*d2c16535SElliott Hughes * - direct bits: 2-26 bits below the highest two bits
173*d2c16535SElliott Hughes * - alignment bits: four lowest bits
174*d2c16535SElliott Hughes *
175*d2c16535SElliott Hughes * Direct bits don't use any probabilities.
176*d2c16535SElliott Hughes *
177*d2c16535SElliott Hughes * The distance slot value of 14 is for distances 128-191.
178*d2c16535SElliott Hughes */
179*d2c16535SElliott Hughes #define DIST_MODEL_END 14
180*d2c16535SElliott Hughes
181*d2c16535SElliott Hughes /* Distance slots that indicate a distance <= 127. */
182*d2c16535SElliott Hughes #define FULL_DISTANCES_BITS (DIST_MODEL_END / 2)
183*d2c16535SElliott Hughes #define FULL_DISTANCES (1 << FULL_DISTANCES_BITS)
184*d2c16535SElliott Hughes
185*d2c16535SElliott Hughes /*
186*d2c16535SElliott Hughes * For match distances greater than 127, only the highest two bits and the
187*d2c16535SElliott Hughes * lowest four bits (alignment) is encoded using probabilities.
188*d2c16535SElliott Hughes */
189*d2c16535SElliott Hughes #define ALIGN_BITS 4
190*d2c16535SElliott Hughes #define ALIGN_SIZE (1 << ALIGN_BITS)
191*d2c16535SElliott Hughes #define ALIGN_MASK (ALIGN_SIZE - 1)
192*d2c16535SElliott Hughes
193*d2c16535SElliott Hughes /* Total number of all probability variables */
194*d2c16535SElliott Hughes #define PROBS_TOTAL (1846 + LITERAL_CODERS_MAX * LITERAL_CODER_SIZE)
195*d2c16535SElliott Hughes
196*d2c16535SElliott Hughes /*
197*d2c16535SElliott Hughes * LZMA remembers the four most recent match distances. Reusing these
198*d2c16535SElliott Hughes * distances tends to take less space than re-encoding the actual
199*d2c16535SElliott Hughes * distance value.
200*d2c16535SElliott Hughes */
201*d2c16535SElliott Hughes #define REPS 4
202*d2c16535SElliott Hughes
203*d2c16535SElliott Hughes #endif
204