Rizin
unix-like reverse engineering framework and cli tools
lzma_common.h
Go to the documentation of this file.
1 //
6 // Authors: Igor Pavlov
7 // Lasse Collin
8 //
9 // This file has been put into the public domain.
10 // You can do whatever you want with this file.
11 //
13 
14 #ifndef LZMA_LZMA_COMMON_H
15 #define LZMA_LZMA_COMMON_H
16 
17 #include "common.h"
18 #include "range_common.h"
19 
20 
22 // Miscellaneous //
24 
28 #define POS_STATES_MAX (1 << LZMA_PB_MAX)
29 
30 
32 static inline bool
34 {
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;
38 }
39 
40 
42 // State //
44 
56 typedef enum {
70 
71 
73 #define STATES 12
74 
76 #define LIT_STATES 7
77 
78 
80 #define update_literal(state) \
81  state = ((state) <= STATE_SHORTREP_LIT_LIT \
82  ? STATE_LIT_LIT \
83  : ((state) <= STATE_LIT_SHORTREP \
84  ? (state) - 3 \
85  : (state) - 6))
86 
88 #define update_match(state) \
89  state = ((state) < LIT_STATES ? STATE_LIT_MATCH : STATE_NONLIT_MATCH)
90 
92 #define update_long_rep(state) \
93  state = ((state) < LIT_STATES ? STATE_LIT_LONGREP : STATE_NONLIT_REP)
94 
96 #define update_short_rep(state) \
97  state = ((state) < LIT_STATES ? STATE_LIT_SHORTREP : STATE_NONLIT_REP)
98 
100 #define is_literal_state(state) \
101  ((state) < LIT_STATES)
102 
103 
105 // Literal //
107 
115 #define LITERAL_CODER_SIZE 0x300
116 
118 #define LITERAL_CODERS_MAX (1 << LZMA_LCLP_MAX)
119 
124 #define literal_subcoder(probs, lc, lp_mask, pos, prev_byte) \
125  ((probs)[(((pos) & (lp_mask)) << (lc)) \
126  + ((uint32_t)(prev_byte) >> (8U - (lc)))])
127 
128 
129 static inline void
131  uint32_t lc, uint32_t lp)
132 {
133  assert(lc + lp <= LZMA_LCLP_MAX);
134 
135  const uint32_t coders = 1U << (lc + lp);
136 
137  for (uint32_t i = 0; i < coders; ++i)
138  for (uint32_t j = 0; j < LITERAL_CODER_SIZE; ++j)
139  bit_reset(probs[i][j]);
140 
141  return;
142 }
143 
144 
146 // Match length //
148 
149 // Minimum length of a match is two bytes.
150 #define MATCH_LEN_MIN 2
151 
152 // Match length is encoded with 4, 5, or 10 bits.
153 //
154 // Length Bits
155 // 2-9 4 = Choice=0 + 3 bits
156 // 10-17 5 = Choice=1 + Choice2=0 + 3 bits
157 // 18-273 10 = Choice=1 + Choice2=1 + 8 bits
158 #define LEN_LOW_BITS 3
159 #define LEN_LOW_SYMBOLS (1 << LEN_LOW_BITS)
160 #define LEN_MID_BITS 3
161 #define LEN_MID_SYMBOLS (1 << LEN_MID_BITS)
162 #define LEN_HIGH_BITS 8
163 #define LEN_HIGH_SYMBOLS (1 << LEN_HIGH_BITS)
164 #define LEN_SYMBOLS (LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS + LEN_HIGH_SYMBOLS)
165 
166 // Maximum length of a match is 273 which is a result of the encoding
167 // described above.
168 #define MATCH_LEN_MAX (MATCH_LEN_MIN + LEN_SYMBOLS - 1)
169 
170 
172 // Match distance //
174 
175 // Different sets of probabilities are used for match distances that have very
176 // short match length: Lengths of 2, 3, and 4 bytes have a separate set of
177 // probabilities for each length. The matches with longer length use a shared
178 // set of probabilities.
179 #define DIST_STATES 4
180 
181 // Macro to get the index of the appropriate probability array.
182 #define get_dist_state(len) \
183  ((len) < DIST_STATES + MATCH_LEN_MIN \
184  ? (len) - MATCH_LEN_MIN \
185  : DIST_STATES - 1)
186 
187 // The highest two bits of a match distance (distance slot) are encoded
188 // using six bits. See fastpos.h for more explanation.
189 #define DIST_SLOT_BITS 6
190 #define DIST_SLOTS (1 << DIST_SLOT_BITS)
191 
192 // Match distances up to 127 are fully encoded using probabilities. Since
193 // the highest two bits (distance slot) are always encoded using six bits,
194 // the distances 0-3 don't need any additional bits to encode, since the
195 // distance slot itself is the same as the actual distance. DIST_MODEL_START
196 // indicates the first distance slot where at least one additional bit is
197 // needed.
198 #define DIST_MODEL_START 4
199 
200 // Match distances greater than 127 are encoded in three pieces:
201 // - distance slot: the highest two bits
202 // - direct bits: 2-26 bits below the highest two bits
203 // - alignment bits: four lowest bits
204 //
205 // Direct bits don't use any probabilities.
206 //
207 // The distance slot value of 14 is for distances 128-191 (see the table in
208 // fastpos.h to understand why).
209 #define DIST_MODEL_END 14
210 
211 // Distance slots that indicate a distance <= 127.
212 #define FULL_DISTANCES_BITS (DIST_MODEL_END / 2)
213 #define FULL_DISTANCES (1 << FULL_DISTANCES_BITS)
214 
215 // For match distances greater than 127, only the highest two bits and the
216 // lowest four bits (alignment) is encoded using probabilities.
217 #define ALIGN_BITS 4
218 #define ALIGN_SIZE (1 << ALIGN_BITS)
219 #define ALIGN_MASK (ALIGN_SIZE - 1)
220 
221 // LZMA remembers the four most recent match distances. Reusing these distances
222 // tends to take less space than re-encoding the actual distance value.
223 #define REPS 4
224 
225 #endif
lzma_index ** i
Definition: index.h:629
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
#define LZMA_PB_MAX
Definition: lzma12.h:318
#define LZMA_LCLP_MAX
Definition: lzma12.h:283
lzma_lzma_state
Definition: lzma_common.h:56
@ STATE_MATCH_LIT_LIT
Definition: lzma_common.h:58
@ STATE_NONLIT_MATCH
Definition: lzma_common.h:67
@ STATE_LIT_LIT
Definition: lzma_common.h:57
@ STATE_LIT_LONGREP
Definition: lzma_common.h:65
@ STATE_SHORTREP_LIT
Definition: lzma_common.h:63
@ STATE_NONLIT_REP
Definition: lzma_common.h:68
@ STATE_LIT_SHORTREP
Definition: lzma_common.h:66
@ STATE_REP_LIT_LIT
Definition: lzma_common.h:59
@ STATE_LIT_MATCH
Definition: lzma_common.h:64
@ STATE_REP_LIT
Definition: lzma_common.h:62
@ STATE_MATCH_LIT
Definition: lzma_common.h:61
@ STATE_SHORTREP_LIT_LIT
Definition: lzma_common.h:60
#define LITERAL_CODER_SIZE
Definition: lzma_common.h:115
static void literal_init(probability(*probs)[LITERAL_CODER_SIZE], uint32_t lc, uint32_t lp)
Definition: lzma_common.h:130
static bool is_lclppb_valid(const lzma_options_lzma *options)
Validates lc, lp, and pb.
Definition: lzma_common.h:33
assert(limit<=UINT32_MAX/2)
Common things for range encoder and decoder.
uint16_t probability
Type of probabilities used with range coder.
Definition: range_common.h:69
#define bit_reset(prob)
Definition: range_common.h:37
unsigned int uint32_t
Definition: sftypes.h:29
Options specific to the LZMA1 and LZMA2 filters.
Definition: lzma12.h:185