1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file lzma_common.h
4 /// \brief Private definitions common to LZMA encoder and decoder
6 // Authors: Igor Pavlov
9 // This file has been put into the public domain.
10 // You can do whatever you want with this file.
12 ///////////////////////////////////////////////////////////////////////////////
14 #ifndef LZMA_LZMA_COMMON_H
15 #define LZMA_LZMA_COMMON_H
18 #include "range_common.h"
25 /// Maximum number of position states. A position state is the lowest pos bits
26 /// number of bits of the current uncompressed offset. In some places there
27 /// are different sets of probabilities for different pos states.
28 #define POS_STATES_MAX (1 << LZMA_PB_MAX)
31 /// Validates lc, lp, and pb.
33 is_lclppb_valid(const lzma_options_lzma *options)
35 return options->lc <= LZMA_LCLP_MAX && options->lp <= LZMA_LCLP_MAX
36 && options->lc + options->lp <= LZMA_LCLP_MAX
37 && options->pb <= LZMA_PB_MAX;
45 /// This enum is used to track which events have occurred most recently and
46 /// in which order. This information is used to predict the next event.
49 /// - Literal: One 8-bit byte
50 /// - Match: Repeat a chunk of data at some distance
51 /// - Long repeat: Multi-byte match at a recently seen distance
52 /// - Short repeat: One-byte repeat at a recently seen distance
54 /// The event names are in from STATE_oldest_older_previous. REP means
55 /// either short or long repeated match, and NONLIT means any non-literal.
60 STATE_SHORTREP_LIT_LIT,
72 /// Total number of states
75 /// The lowest 7 states indicate that the previous state was a literal.
79 /// Indicate that the latest state was a literal.
80 #define update_literal(state) \
81 state = ((state) <= STATE_SHORTREP_LIT_LIT \
83 : ((state) <= STATE_LIT_SHORTREP \
87 /// Indicate that the latest state was a match.
88 #define update_match(state) \
89 state = ((state) < LIT_STATES ? STATE_LIT_MATCH : STATE_NONLIT_MATCH)
91 /// Indicate that the latest state was a long repeated match.
92 #define update_long_rep(state) \
93 state = ((state) < LIT_STATES ? STATE_LIT_LONGREP : STATE_NONLIT_REP)
95 /// Indicate that the latest state was a short match.
96 #define update_short_rep(state) \
97 state = ((state) < LIT_STATES ? STATE_LIT_SHORTREP : STATE_NONLIT_REP)
99 /// Test if the previous state was a literal.
100 #define is_literal_state(state) \
101 ((state) < LIT_STATES)
108 /// Each literal coder is divided in three sections:
109 /// - 0x001-0x0FF: Without match byte
110 /// - 0x101-0x1FF: With match byte; match bit is 0
111 /// - 0x201-0x2FF: With match byte; match bit is 1
113 /// Match byte is used when the previous LZMA symbol was something else than
114 /// a literal (that is, it was some kind of match).
115 #define LITERAL_CODER_SIZE 0x300
117 /// Maximum number of literal coders
118 #define LITERAL_CODERS_MAX (1 << LZMA_LCLP_MAX)
120 /// Locate the literal coder for the next literal byte. The choice depends on
121 /// - the lowest literal_pos_bits bits of the position of the current
123 /// - the highest literal_context_bits bits of the previous byte.
124 #define literal_subcoder(probs, lc, lp_mask, pos, prev_byte) \
125 ((probs)[(((pos) & lp_mask) << lc) + ((prev_byte) >> (8 - lc))])
129 literal_init(probability (*probs)[LITERAL_CODER_SIZE],
130 uint32_t lc, uint32_t lp)
132 assert(lc + lp <= LZMA_LCLP_MAX);
134 const uint32_t coders = 1U << (lc + lp);
136 for (uint32_t i = 0; i < coders; ++i)
137 for (uint32_t j = 0; j < LITERAL_CODER_SIZE; ++j)
138 bit_reset(probs[i][j]);
148 // Minimum length of a match is two bytes.
149 #define MATCH_LEN_MIN 2
151 // Match length is encoded with 4, 5, or 10 bits.
154 // 2-9 4 = Choice=0 + 3 bits
155 // 10-17 5 = Choice=1 + Choice2=0 + 3 bits
156 // 18-273 10 = Choice=1 + Choice2=1 + 8 bits
157 #define LEN_LOW_BITS 3
158 #define LEN_LOW_SYMBOLS (1 << LEN_LOW_BITS)
159 #define LEN_MID_BITS 3
160 #define LEN_MID_SYMBOLS (1 << LEN_MID_BITS)
161 #define LEN_HIGH_BITS 8
162 #define LEN_HIGH_SYMBOLS (1 << LEN_HIGH_BITS)
163 #define LEN_SYMBOLS (LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS + LEN_HIGH_SYMBOLS)
165 // Maximum length of a match is 273 which is a result of the encoding
167 #define MATCH_LEN_MAX (MATCH_LEN_MIN + LEN_SYMBOLS - 1)
174 // Different sets of probabilities are used for match distances that have very
175 // short match length: Lengths of 2, 3, and 4 bytes have a separate set of
176 // probabilities for each length. The matches with longer length use a shared
177 // set of probabilities.
178 #define DIST_STATES 4
180 // Macro to get the index of the appropriate probability array.
181 #define get_dist_state(len) \
182 ((len) < DIST_STATES + MATCH_LEN_MIN \
183 ? (len) - MATCH_LEN_MIN \
186 // The highest two bits of a match distance (distance slot) are encoded
187 // using six bits. See fastpos.h for more explanation.
188 #define DIST_SLOT_BITS 6
189 #define DIST_SLOTS (1 << DIST_SLOT_BITS)
191 // Match distances up to 127 are fully encoded using probabilities. Since
192 // the highest two bits (distance slot) are always encoded using six bits,
193 // the distances 0-3 don't need any additional bits to encode, since the
194 // distance slot itself is the same as the actual distance. DIST_MODEL_START
195 // indicates the first distance slot where at least one additional bit is
197 #define DIST_MODEL_START 4
199 // Match distances greater than 127 are encoded in three pieces:
200 // - distance slot: the highest two bits
201 // - direct bits: 2-26 bits below the highest two bits
202 // - alignment bits: four lowest bits
204 // Direct bits don't use any probabilities.
206 // The distance slot value of 14 is for distances 128-191 (see the table in
207 // fastpos.h to understand why).
208 #define DIST_MODEL_END 14
210 // Distance slots that indicate a distance <= 127.
211 #define FULL_DISTANCES_BITS (DIST_MODEL_END / 2)
212 #define FULL_DISTANCES (1 << FULL_DISTANCES_BITS)
214 // For match distances greater than 127, only the highest two bits and the
215 // lowest four bits (alignment) is encoded using probabilities.
217 #define ALIGN_SIZE (1 << ALIGN_BITS)
218 #define ALIGN_MASK (ALIGN_SIZE - 1)
220 // LZMA remembers the four most recent match distances. Reusing these distances
221 // tends to take less space than re-encoding the actual distance value.