1
/* xz_lzma2.h - LZMA2 definitions */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2010 Free Software Foundation, Inc.
6
* GRUB is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* GRUB is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20
* This file is based on code from XZ embedded project
21
* http://tukaani.org/xz/embedded.html
27
/* dictionary size hard limit
28
* actual size limit is calculated as shown in 5.3.1
29
* http://tukaani.org/xz/xz-file-format.txt
31
* if bits > 39 dictionary_size = UINT32_MAX
33
* dictionary_size = 2 | (bits & 1);
34
* dictionary_size <<= bits / 2 + 11;
42
* note: implementation supports 39 at maximum
44
#define DICT_BIT_SIZE 30
46
/* Range coder constants */
47
#define RC_SHIFT_BITS 8
48
#define RC_TOP_BITS 24
49
#define RC_TOP_VALUE (1 << RC_TOP_BITS)
50
#define RC_BIT_MODEL_TOTAL_BITS 11
51
#define RC_BIT_MODEL_TOTAL (1 << RC_BIT_MODEL_TOTAL_BITS)
52
#define RC_MOVE_BITS 5
55
* Maximum number of position states. A position state is the lowest pb
56
* number of bits of the current uncompressed offset. In some places there
57
* are different sets of probabilities for different position states.
59
#define POS_STATES_MAX (1 << 4)
62
* This enum is used to track which LZMA symbols have occurred most recently
63
* and in which order. This information is used to predict the next symbol.
66
* - Literal: One 8-bit byte
67
* - Match: Repeat a chunk of data at some distance
68
* - Long repeat: Multi-byte match at a recently seen distance
69
* - Short repeat: One-byte repeat at a recently seen distance
71
* The symbol names are in from STATE_oldest_older_previous. REP means
72
* either short or long repeated match, and NONLIT means any non-literal.
78
STATE_SHORTREP_LIT_LIT,
89
/* Total number of states */
92
/* The lowest 7 states indicate that the previous state was a literal. */
95
/* Indicate that the latest symbol was a literal. */
96
static inline void lzma_state_literal(enum lzma_state *state)
98
if (*state <= STATE_SHORTREP_LIT_LIT)
99
*state = STATE_LIT_LIT;
100
else if (*state <= STATE_LIT_SHORTREP)
106
/* Indicate that the latest symbol was a match. */
107
static inline void lzma_state_match(enum lzma_state *state)
109
*state = *state < LIT_STATES ? STATE_LIT_MATCH : STATE_NONLIT_MATCH;
112
/* Indicate that the latest state was a long repeated match. */
113
static inline void lzma_state_long_rep(enum lzma_state *state)
115
*state = *state < LIT_STATES ? STATE_LIT_LONGREP : STATE_NONLIT_REP;
118
/* Indicate that the latest symbol was a short match. */
119
static inline void lzma_state_short_rep(enum lzma_state *state)
121
*state = *state < LIT_STATES ? STATE_LIT_SHORTREP : STATE_NONLIT_REP;
124
/* Test if the previous symbol was a literal. */
125
static inline bool lzma_state_is_literal(enum lzma_state state)
127
return state < LIT_STATES;
130
/* Each literal coder is divided in three sections:
131
* - 0x001-0x0FF: Without match byte
132
* - 0x101-0x1FF: With match byte; match bit is 0
133
* - 0x201-0x2FF: With match byte; match bit is 1
135
* Match byte is used when the previous LZMA symbol was something else than
136
* a literal (that is, it was some kind of match).
138
#define LITERAL_CODER_SIZE 0x300
140
/* Maximum number of literal coders */
141
#define LITERAL_CODERS_MAX (1 << 4)
143
/* Minimum length of a match is two bytes. */
144
#define MATCH_LEN_MIN 2
146
/* Match length is encoded with 4, 5, or 10 bits.
149
* 2-9 4 = Choice=0 + 3 bits
150
* 10-17 5 = Choice=1 + Choice2=0 + 3 bits
151
* 18-273 10 = Choice=1 + Choice2=1 + 8 bits
153
#define LEN_LOW_BITS 3
154
#define LEN_LOW_SYMBOLS (1 << LEN_LOW_BITS)
155
#define LEN_MID_BITS 3
156
#define LEN_MID_SYMBOLS (1 << LEN_MID_BITS)
157
#define LEN_HIGH_BITS 8
158
#define LEN_HIGH_SYMBOLS (1 << LEN_HIGH_BITS)
159
#define LEN_SYMBOLS (LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS + LEN_HIGH_SYMBOLS)
162
* Maximum length of a match is 273 which is a result of the encoding
165
#define MATCH_LEN_MAX (MATCH_LEN_MIN + LEN_SYMBOLS - 1)
168
* Different sets of probabilities are used for match distances that have
169
* very short match length: Lengths of 2, 3, and 4 bytes have a separate
170
* set of probabilities for each length. The matches with longer length
171
* use a shared set of probabilities.
173
#define DIST_STATES 4
176
* Get the index of the appropriate probability array for decoding
179
static inline uint32_t lzma_get_dist_state(uint32_t len)
181
return len < DIST_STATES + MATCH_LEN_MIN
182
? len - MATCH_LEN_MIN : DIST_STATES - 1;
186
* The highest two bits of a 32-bit match distance are encoded using six bits.
187
* This six-bit value is called a distance slot. This way encoding a 32-bit
188
* value takes 6-36 bits, larger values taking more bits.
190
#define DIST_SLOT_BITS 6
191
#define DIST_SLOTS (1 << DIST_SLOT_BITS)
193
/* Match distances up to 127 are fully encoded using probabilities. Since
194
* the highest two bits (distance slot) are always encoded using six bits,
195
* the distances 0-3 don't need any additional bits to encode, since the
196
* distance slot itself is the same as the actual distance. DIST_MODEL_START
197
* indicates the first distance slot where at least one additional bit is
200
#define DIST_MODEL_START 4
203
* Match distances greater than 127 are encoded in three pieces:
204
* - distance slot: the highest two bits
205
* - direct bits: 2-26 bits below the highest two bits
206
* - alignment bits: four lowest bits
208
* Direct bits don't use any probabilities.
210
* The distance slot value of 14 is for distances 128-191.
212
#define DIST_MODEL_END 14
214
/* Distance slots that indicate a distance <= 127. */
215
#define FULL_DISTANCES_BITS (DIST_MODEL_END / 2)
216
#define FULL_DISTANCES (1 << FULL_DISTANCES_BITS)
219
* For match distances greater than 127, only the highest two bits and the
220
* lowest four bits (alignment) is encoded using probabilities.
223
#define ALIGN_SIZE (1 << ALIGN_BITS)
224
#define ALIGN_MASK (ALIGN_SIZE - 1)
226
/* Total number of all probability variables */
227
#define PROBS_TOTAL (1846 + LITERAL_CODERS_MAX * LITERAL_CODER_SIZE)
230
* LZMA remembers the four most recent match distances. Reusing these
231
* distances tends to take less space than re-encoding the actual