Parolin 0.7.9 6796
Console (soon DLLs) to do a tar like job
Loading...
Searching...
No Matches
lzma_common.h
Go to the documentation of this file.
1// SPDX-License-Identifier: 0BSD
2
4//
8// Authors: Igor Pavlov
9// Lasse Collin
10//
12
13#ifndef LZMA_LZMA_COMMON_H
14#define LZMA_LZMA_COMMON_H
15
16#include "common.h"
17#include "range_common.h"
18
19
21// Miscellaneous //
23
27#define POS_STATES_MAX (1 << LZMA_PB_MAX)
28
29
31static inline bool
32is_lclppb_valid(const lzma_options_lzma *options)
33{
36 && options->pb <= LZMA_PB_MAX;
37}
38
39
41// State //
43
69
70
72#define STATES 12
73
75#define LIT_STATES 7
76
77
79#define update_literal(state) \
80 state = ((state) <= STATE_SHORTREP_LIT_LIT \
81 ? STATE_LIT_LIT \
82 : ((state) <= STATE_LIT_SHORTREP \
83 ? (state) - 3 \
84 : (state) - 6))
85
88#define update_literal_normal(state) \
89 state = ((state) <= STATE_SHORTREP_LIT_LIT \
90 ? STATE_LIT_LIT \
91 : (state) - 3);
92
95#define update_literal_matched(state) \
96 state = ((state) <= STATE_LIT_SHORTREP \
97 ? (state) - 3 \
98 : (state) - 6);
99
101#define update_match(state) \
102 state = ((state) < LIT_STATES ? STATE_LIT_MATCH : STATE_NONLIT_MATCH)
103
105#define update_long_rep(state) \
106 state = ((state) < LIT_STATES ? STATE_LIT_LONGREP : STATE_NONLIT_REP)
107
109#define update_short_rep(state) \
110 state = ((state) < LIT_STATES ? STATE_LIT_SHORTREP : STATE_NONLIT_REP)
111
113#define is_literal_state(state) \
114 ((state) < LIT_STATES)
115
116
118// Literal //
120
128#define LITERAL_CODER_SIZE UINT32_C(0x300)
129
131#define LITERAL_CODERS_MAX (1 << LZMA_LCLP_MAX)
132
134#define literal_mask_calc(lc, lp) \
135 ((UINT32_C(0x100) << (lp)) - (UINT32_C(0x100) >> (lc)))
136
141#define literal_subcoder(probs, lc, literal_mask, pos, prev_byte) \
142 ((probs) + UINT32_C(3) * \
143 (((((pos) << 8) + (prev_byte)) & (literal_mask)) << (lc)))
144
145
146static inline void
147literal_init(probability *probs, uint32_t lc, uint32_t lp)
148{
149 assert(lc + lp <= LZMA_LCLP_MAX);
150
151 const size_t coders = LITERAL_CODER_SIZE << (lc + lp);
152
153 for (size_t i = 0; i < coders; ++i)
154 bit_reset(probs[i]);
155
156 return;
157}
158
159
161// Match length //
163
164// Minimum length of a match is two bytes.
165#define MATCH_LEN_MIN 2
166
167// Match length is encoded with 4, 5, or 10 bits.
168//
169// Length Bits
170// 2-9 4 = Choice=0 + 3 bits
171// 10-17 5 = Choice=1 + Choice2=0 + 3 bits
172// 18-273 10 = Choice=1 + Choice2=1 + 8 bits
173#define LEN_LOW_BITS 3
174#define LEN_LOW_SYMBOLS (1 << LEN_LOW_BITS)
175#define LEN_MID_BITS 3
176#define LEN_MID_SYMBOLS (1 << LEN_MID_BITS)
177#define LEN_HIGH_BITS 8
178#define LEN_HIGH_SYMBOLS (1 << LEN_HIGH_BITS)
179#define LEN_SYMBOLS (LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS + LEN_HIGH_SYMBOLS)
180
181// Maximum length of a match is 273 which is a result of the encoding
182// described above.
183#define MATCH_LEN_MAX (MATCH_LEN_MIN + LEN_SYMBOLS - 1)
184
185
187// Match distance //
189
190// Different sets of probabilities are used for match distances that have very
191// short match length: Lengths of 2, 3, and 4 bytes have a separate set of
192// probabilities for each length. The matches with longer length use a shared
193// set of probabilities.
194#define DIST_STATES 4
195
196// Macro to get the index of the appropriate probability array.
197#define get_dist_state(len) \
198 ((len) < DIST_STATES + MATCH_LEN_MIN \
199 ? (len) - MATCH_LEN_MIN \
200 : DIST_STATES - 1)
201
202// The highest two bits of a match distance (distance slot) are encoded
203// using six bits. See fastpos.h for more explanation.
204#define DIST_SLOT_BITS 6
205#define DIST_SLOTS (1 << DIST_SLOT_BITS)
206
207// Match distances up to 127 are fully encoded using probabilities. Since
208// the highest two bits (distance slot) are always encoded using six bits,
209// the distances 0-3 don't need any additional bits to encode, since the
210// distance slot itself is the same as the actual distance. DIST_MODEL_START
211// indicates the first distance slot where at least one additional bit is
212// needed.
213#define DIST_MODEL_START 4
214
215// Match distances greater than 127 are encoded in three pieces:
216// - distance slot: the highest two bits
217// - direct bits: 2-26 bits below the highest two bits
218// - alignment bits: four lowest bits
219//
220// Direct bits don't use any probabilities.
221//
222// The distance slot value of 14 is for distances 128-191 (see the table in
223// fastpos.h to understand why).
224#define DIST_MODEL_END 14
225
226// Distance slots that indicate a distance <= 127.
227#define FULL_DISTANCES_BITS (DIST_MODEL_END / 2)
228#define FULL_DISTANCES (1 << FULL_DISTANCES_BITS)
229
230// For match distances greater than 127, only the highest two bits and the
231// lowest four bits (alignment) is encoded using probabilities.
232#define ALIGN_BITS 4
233#define ALIGN_SIZE (1 << ALIGN_BITS)
234#define ALIGN_MASK (ALIGN_SIZE - 1)
235
236// LZMA remembers the four most recent match distances. Reusing these distances
237// tends to take less space than re-encoding the actual distance value.
238#define REPS 4
239
240#endif
#define LZMA_PB_MAX
Definition LzmaEnc.c:330
#define assert(condition)
Definition lz4.c:273
Options specific to the LZMA1 and LZMA2 filters.
Definition lzma12.h:185
uint32_t lp
Number of literal position bits.
Definition lzma12.h:293
uint32_t lc
Number of literal context bits.
Definition lzma12.h:281
uint32_t pb
Number of position bits.
Definition lzma12.h:316
lzma_index ** i
Definition index.h:629
#define LZMA_LCLP_MAX
Definition lzma12.h:283
const lzma_options_lzma * options
Definition container.h:545
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
uint16_t probability
Type of probabilities used with range coder.
Definition range_common.h:69
#define bit_reset(prob)
Definition range_common.h:37