2 * Small lzma deflate implementation.
3 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
5 * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
6 * Copyright (C) 1999-2005 Igor Pavlov
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "unarchive.h"
32 #include "rangecoder.h"
39 } __attribute__ ((packed)) lzma_header_t;
42 #define LZMA_BASE_SIZE 1846
43 #define LZMA_LIT_SIZE 768
45 #define LZMA_NUM_POS_BITS_MAX 4
47 #define LZMA_LEN_NUM_LOW_BITS 3
48 #define LZMA_LEN_NUM_MID_BITS 3
49 #define LZMA_LEN_NUM_HIGH_BITS 8
51 #define LZMA_LEN_CHOICE 0
52 #define LZMA_LEN_CHOICE_2 (LZMA_LEN_CHOICE + 1)
53 #define LZMA_LEN_LOW (LZMA_LEN_CHOICE_2 + 1)
54 #define LZMA_LEN_MID (LZMA_LEN_LOW \
55 + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS)))
56 #define LZMA_LEN_HIGH (LZMA_LEN_MID \
57 +(1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS)))
58 #define LZMA_NUM_LEN_PROBS (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS))
60 #define LZMA_NUM_STATES 12
61 #define LZMA_NUM_LIT_STATES 7
63 #define LZMA_START_POS_MODEL_INDEX 4
64 #define LZMA_END_POS_MODEL_INDEX 14
65 #define LZMA_NUM_FULL_DISTANCES (1 << (LZMA_END_POS_MODEL_INDEX >> 1))
67 #define LZMA_NUM_POS_SLOT_BITS 6
68 #define LZMA_NUM_LEN_TO_POS_STATES 4
70 #define LZMA_NUM_ALIGN_BITS 4
72 #define LZMA_MATCH_MIN_LEN 2
74 #define LZMA_IS_MATCH 0
75 #define LZMA_IS_REP (LZMA_IS_MATCH + (LZMA_NUM_STATES <<LZMA_NUM_POS_BITS_MAX))
76 #define LZMA_IS_REP_G0 (LZMA_IS_REP + LZMA_NUM_STATES)
77 #define LZMA_IS_REP_G1 (LZMA_IS_REP_G0 + LZMA_NUM_STATES)
78 #define LZMA_IS_REP_G2 (LZMA_IS_REP_G1 + LZMA_NUM_STATES)
79 #define LZMA_IS_REP_0_LONG (LZMA_IS_REP_G2 + LZMA_NUM_STATES)
80 #define LZMA_POS_SLOT (LZMA_IS_REP_0_LONG \
81 + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
82 #define LZMA_SPEC_POS (LZMA_POS_SLOT \
83 +(LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS))
84 #define LZMA_ALIGN (LZMA_SPEC_POS \
85 + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX)
86 #define LZMA_LEN_CODER (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS))
87 #define LZMA_REP_LEN_CODER (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS)
88 #define LZMA_LITERAL (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS)
91 int unlzma(int src_fd, int dst_fd)
95 uint32_t pos_state_mask;
96 uint32_t literal_pos_mask;
106 uint8_t previous_byte = 0;
107 size_t buffer_pos = 0, global_pos = 0;
110 uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
112 if (read(src_fd, &header, sizeof(header)) != sizeof(header))
113 bb_error_msg_and_die("can't read header");
115 if (header.pos >= (9 * 5 * 5))
116 bb_error_msg_and_die("bad header");
121 pos_state_mask = (1 << pb) - 1;
122 literal_pos_mask = (1 << lp) - 1;
125 header.dict_size = bswap_32(header.dict_size);
126 header.dst_size = bswap_64(header.dst_size);
129 if (header.dict_size == 0)
130 header.dict_size = 1;
132 buffer = xmalloc(MIN(header.dst_size, header.dict_size));
134 num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
135 p = xmalloc(num_probs * sizeof(*p));
136 num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp));
137 for (i = 0; i < num_probs; i++)
138 p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
140 rc_init(&rc, src_fd, 0x10000);
142 while (global_pos + buffer_pos < header.dst_size) {
143 int pos_state = (buffer_pos + global_pos) & pos_state_mask;
146 p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
147 if (rc_is_bit_0(&rc, prob)) {
149 rc_update_bit_0(&rc, prob);
150 prob = (p + LZMA_LITERAL + (LZMA_LIT_SIZE
151 * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
152 + (previous_byte >> (8 - lc)))));
154 if (state >= LZMA_NUM_LIT_STATES) {
157 pos = buffer_pos - rep0;
158 while (pos >= header.dict_size)
159 pos += header.dict_size;
160 match_byte = buffer[pos];
165 bit = match_byte & 0x100;
166 prob_lit = prob + 0x100 + bit + mi;
167 if (rc_get_bit(&rc, prob_lit, &mi)) {
174 } while (mi < 0x100);
177 prob_lit = prob + mi;
178 rc_get_bit(&rc, prob_lit, &mi);
180 previous_byte = (uint8_t) mi;
182 buffer[buffer_pos++] = previous_byte;
183 if (buffer_pos == header.dict_size) {
185 global_pos += header.dict_size;
186 write(dst_fd, buffer, header.dict_size);
198 rc_update_bit_1(&rc, prob);
199 prob = p + LZMA_IS_REP + state;
200 if (rc_is_bit_0(&rc, prob)) {
201 rc_update_bit_0(&rc, prob);
205 state = state < LZMA_NUM_LIT_STATES ? 0 : 3;
206 prob = p + LZMA_LEN_CODER;
208 rc_update_bit_1(&rc, prob);
209 prob = p + LZMA_IS_REP_G0 + state;
210 if (rc_is_bit_0(&rc, prob)) {
211 rc_update_bit_0(&rc, prob);
212 prob = (p + LZMA_IS_REP_0_LONG
213 + (state << LZMA_NUM_POS_BITS_MAX) + pos_state);
214 if (rc_is_bit_0(&rc, prob)) {
215 rc_update_bit_0(&rc, prob);
217 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
218 pos = buffer_pos - rep0;
219 while (pos >= header.dict_size)
220 pos += header.dict_size;
221 previous_byte = buffer[pos];
222 buffer[buffer_pos++] = previous_byte;
223 if (buffer_pos == header.dict_size) {
225 global_pos += header.dict_size;
226 write(dst_fd, buffer, header.dict_size);
230 rc_update_bit_1(&rc, prob);
235 rc_update_bit_1(&rc, prob);
236 prob = p + LZMA_IS_REP_G1 + state;
237 if (rc_is_bit_0(&rc, prob)) {
238 rc_update_bit_0(&rc, prob);
241 rc_update_bit_1(&rc, prob);
242 prob = p + LZMA_IS_REP_G2 + state;
243 if (rc_is_bit_0(&rc, prob)) {
244 rc_update_bit_0(&rc, prob);
247 rc_update_bit_1(&rc, prob);
256 state = state < LZMA_NUM_LIT_STATES ? 8 : 11;
257 prob = p + LZMA_REP_LEN_CODER;
260 prob_len = prob + LZMA_LEN_CHOICE;
261 if (rc_is_bit_0(&rc, prob_len)) {
262 rc_update_bit_0(&rc, prob_len);
263 prob_len = (prob + LZMA_LEN_LOW
264 + (pos_state << LZMA_LEN_NUM_LOW_BITS));
266 num_bits = LZMA_LEN_NUM_LOW_BITS;
268 rc_update_bit_1(&rc, prob_len);
269 prob_len = prob + LZMA_LEN_CHOICE_2;
270 if (rc_is_bit_0(&rc, prob_len)) {
271 rc_update_bit_0(&rc, prob_len);
272 prob_len = (prob + LZMA_LEN_MID
273 + (pos_state << LZMA_LEN_NUM_MID_BITS));
274 offset = 1 << LZMA_LEN_NUM_LOW_BITS;
275 num_bits = LZMA_LEN_NUM_MID_BITS;
277 rc_update_bit_1(&rc, prob_len);
278 prob_len = prob + LZMA_LEN_HIGH;
279 offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
280 + (1 << LZMA_LEN_NUM_MID_BITS));
281 num_bits = LZMA_LEN_NUM_HIGH_BITS;
284 rc_bit_tree_decode(&rc, prob_len, num_bits, &len);
290 state += LZMA_NUM_LIT_STATES;
294 LZMA_NUM_LEN_TO_POS_STATES ? len :
295 LZMA_NUM_LEN_TO_POS_STATES - 1)
296 << LZMA_NUM_POS_SLOT_BITS);
297 rc_bit_tree_decode(&rc, prob, LZMA_NUM_POS_SLOT_BITS,
299 if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
300 num_bits = (pos_slot >> 1) - 1;
301 rep0 = 2 | (pos_slot & 1);
302 if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
304 prob = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
306 num_bits -= LZMA_NUM_ALIGN_BITS;
308 rep0 = (rep0 << 1) | rc_direct_bit(&rc);
309 prob = p + LZMA_ALIGN;
310 rep0 <<= LZMA_NUM_ALIGN_BITS;
311 num_bits = LZMA_NUM_ALIGN_BITS;
316 if (rc_get_bit(&rc, prob + mi, &mi))
326 len += LZMA_MATCH_MIN_LEN;
329 pos = buffer_pos - rep0;
330 while (pos >= header.dict_size)
331 pos += header.dict_size;
332 previous_byte = buffer[pos];
333 buffer[buffer_pos++] = previous_byte;
334 if (buffer_pos == header.dict_size) {
336 global_pos += header.dict_size;
337 write(dst_fd, buffer, header.dict_size);
340 } while (len != 0 && buffer_pos < header.dst_size);
344 write(dst_fd, buffer, buffer_pos);