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 "rangecoder.h"
37 } __attribute__ ((packed)) lzma_header_t;
40 #define LZMA_BASE_SIZE 1846
41 #define LZMA_LIT_SIZE 768
43 #define LZMA_NUM_POS_BITS_MAX 4
45 #define LZMA_LEN_NUM_LOW_BITS 3
46 #define LZMA_LEN_NUM_MID_BITS 3
47 #define LZMA_LEN_NUM_HIGH_BITS 8
49 #define LZMA_LEN_CHOICE 0
50 #define LZMA_LEN_CHOICE_2 (LZMA_LEN_CHOICE + 1)
51 #define LZMA_LEN_LOW (LZMA_LEN_CHOICE_2 + 1)
52 #define LZMA_LEN_MID (LZMA_LEN_LOW \
53 + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS)))
54 #define LZMA_LEN_HIGH (LZMA_LEN_MID \
55 +(1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS)))
56 #define LZMA_NUM_LEN_PROBS (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS))
58 #define LZMA_NUM_STATES 12
59 #define LZMA_NUM_LIT_STATES 7
61 #define LZMA_START_POS_MODEL_INDEX 4
62 #define LZMA_END_POS_MODEL_INDEX 14
63 #define LZMA_NUM_FULL_DISTANCES (1 << (LZMA_END_POS_MODEL_INDEX >> 1))
65 #define LZMA_NUM_POS_SLOT_BITS 6
66 #define LZMA_NUM_LEN_TO_POS_STATES 4
68 #define LZMA_NUM_ALIGN_BITS 4
70 #define LZMA_MATCH_MIN_LEN 2
72 #define LZMA_IS_MATCH 0
73 #define LZMA_IS_REP (LZMA_IS_MATCH + (LZMA_NUM_STATES <<LZMA_NUM_POS_BITS_MAX))
74 #define LZMA_IS_REP_G0 (LZMA_IS_REP + LZMA_NUM_STATES)
75 #define LZMA_IS_REP_G1 (LZMA_IS_REP_G0 + LZMA_NUM_STATES)
76 #define LZMA_IS_REP_G2 (LZMA_IS_REP_G1 + LZMA_NUM_STATES)
77 #define LZMA_IS_REP_0_LONG (LZMA_IS_REP_G2 + LZMA_NUM_STATES)
78 #define LZMA_POS_SLOT (LZMA_IS_REP_0_LONG \
79 + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
80 #define LZMA_SPEC_POS (LZMA_POS_SLOT \
81 +(LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS))
82 #define LZMA_ALIGN (LZMA_SPEC_POS \
83 + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX)
84 #define LZMA_LEN_CODER (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS))
85 #define LZMA_REP_LEN_CODER (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS)
86 #define LZMA_LITERAL (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS)
89 int unlzma(int src_fd, int dst_fd)
93 uint32_t pos_state_mask;
94 uint32_t literal_pos_mask;
104 uint8_t previous_byte = 0;
105 size_t buffer_pos = 0, global_pos = 0;
108 uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
110 if (read(src_fd, &header, sizeof(header)) != sizeof(header))
111 bb_error_msg_and_die("can't read header");
113 if (header.pos >= (9 * 5 * 5))
114 bb_error_msg_and_die("bad header");
119 pos_state_mask = (1 << pb) - 1;
120 literal_pos_mask = (1 << lp) - 1;
122 #if __BYTE_ORDER == __BIG_ENDIAN
123 header.dict_size = bswap_32(header.dict_size);
124 header.dst_size = bswap_64(header.dst_size);
125 #endif /* __BYTE_ORDER */
127 if (header.dict_size == 0)
128 header.dict_size = 1;
130 buffer = xmalloc(MIN(header.dst_size, header.dict_size));
132 num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
133 p = xmalloc(num_probs * sizeof(*p));
134 num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp));
135 for (i = 0; i < num_probs; i++)
136 p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
138 rc_init(&rc, src_fd, 0x10000);
140 while (global_pos + buffer_pos < header.dst_size) {
141 int pos_state = (buffer_pos + global_pos) & pos_state_mask;
144 p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
145 if (rc_is_bit_0(&rc, prob)) {
147 rc_update_bit_0(&rc, prob);
148 prob = (p + LZMA_LITERAL + (LZMA_LIT_SIZE
149 * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
150 + (previous_byte >> (8 - lc)))));
152 if (state >= LZMA_NUM_LIT_STATES) {
155 pos = buffer_pos - rep0;
156 while (pos >= header.dict_size)
157 pos += header.dict_size;
158 match_byte = buffer[pos];
163 bit = match_byte & 0x100;
164 prob_lit = prob + 0x100 + bit + mi;
165 if (rc_get_bit(&rc, prob_lit, &mi)) {
172 } while (mi < 0x100);
175 prob_lit = prob + mi;
176 rc_get_bit(&rc, prob_lit, &mi);
178 previous_byte = (uint8_t) mi;
180 buffer[buffer_pos++] = previous_byte;
181 if (buffer_pos == header.dict_size) {
183 global_pos += header.dict_size;
184 write(dst_fd, buffer, header.dict_size);
196 rc_update_bit_1(&rc, prob);
197 prob = p + LZMA_IS_REP + state;
198 if (rc_is_bit_0(&rc, prob)) {
199 rc_update_bit_0(&rc, prob);
203 state = state < LZMA_NUM_LIT_STATES ? 0 : 3;
204 prob = p + LZMA_LEN_CODER;
206 rc_update_bit_1(&rc, prob);
207 prob = p + LZMA_IS_REP_G0 + state;
208 if (rc_is_bit_0(&rc, prob)) {
209 rc_update_bit_0(&rc, prob);
210 prob = (p + LZMA_IS_REP_0_LONG
211 + (state << LZMA_NUM_POS_BITS_MAX) + pos_state);
212 if (rc_is_bit_0(&rc, prob)) {
213 rc_update_bit_0(&rc, prob);
215 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
216 pos = buffer_pos - rep0;
217 while (pos >= header.dict_size)
218 pos += header.dict_size;
219 previous_byte = buffer[pos];
220 buffer[buffer_pos++] = previous_byte;
221 if (buffer_pos == header.dict_size) {
223 global_pos += header.dict_size;
224 write(dst_fd, buffer, header.dict_size);
228 rc_update_bit_1(&rc, prob);
233 rc_update_bit_1(&rc, prob);
234 prob = p + LZMA_IS_REP_G1 + state;
235 if (rc_is_bit_0(&rc, prob)) {
236 rc_update_bit_0(&rc, prob);
239 rc_update_bit_1(&rc, prob);
240 prob = p + LZMA_IS_REP_G2 + state;
241 if (rc_is_bit_0(&rc, prob)) {
242 rc_update_bit_0(&rc, prob);
245 rc_update_bit_1(&rc, prob);
254 state = state < LZMA_NUM_LIT_STATES ? 8 : 11;
255 prob = p + LZMA_REP_LEN_CODER;
258 prob_len = prob + LZMA_LEN_CHOICE;
259 if (rc_is_bit_0(&rc, prob_len)) {
260 rc_update_bit_0(&rc, prob_len);
261 prob_len = (prob + LZMA_LEN_LOW
262 + (pos_state << LZMA_LEN_NUM_LOW_BITS));
264 num_bits = LZMA_LEN_NUM_LOW_BITS;
266 rc_update_bit_1(&rc, prob_len);
267 prob_len = prob + LZMA_LEN_CHOICE_2;
268 if (rc_is_bit_0(&rc, prob_len)) {
269 rc_update_bit_0(&rc, prob_len);
270 prob_len = (prob + LZMA_LEN_MID
271 + (pos_state << LZMA_LEN_NUM_MID_BITS));
272 offset = 1 << LZMA_LEN_NUM_LOW_BITS;
273 num_bits = LZMA_LEN_NUM_MID_BITS;
275 rc_update_bit_1(&rc, prob_len);
276 prob_len = prob + LZMA_LEN_HIGH;
277 offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
278 + (1 << LZMA_LEN_NUM_MID_BITS));
279 num_bits = LZMA_LEN_NUM_HIGH_BITS;
282 rc_bit_tree_decode(&rc, prob_len, num_bits, &len);
288 state += LZMA_NUM_LIT_STATES;
292 LZMA_NUM_LEN_TO_POS_STATES ? len :
293 LZMA_NUM_LEN_TO_POS_STATES - 1)
294 << LZMA_NUM_POS_SLOT_BITS);
295 rc_bit_tree_decode(&rc, prob, LZMA_NUM_POS_SLOT_BITS,
297 if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
298 num_bits = (pos_slot >> 1) - 1;
299 rep0 = 2 | (pos_slot & 1);
300 if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
302 prob = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
304 num_bits -= LZMA_NUM_ALIGN_BITS;
306 rep0 = (rep0 << 1) | rc_direct_bit(&rc);
307 prob = p + LZMA_ALIGN;
308 rep0 <<= LZMA_NUM_ALIGN_BITS;
309 num_bits = LZMA_NUM_ALIGN_BITS;
314 if (rc_get_bit(&rc, prob + mi, &mi))
324 len += LZMA_MATCH_MIN_LEN;
327 pos = buffer_pos - rep0;
328 while (pos >= header.dict_size)
329 pos += header.dict_size;
330 previous_byte = buffer[pos];
331 buffer[buffer_pos++] = previous_byte;
332 if (buffer_pos == header.dict_size) {
334 global_pos += header.dict_size;
335 write(dst_fd, buffer, header.dict_size);
338 } while (len != 0 && buffer_pos < header.dst_size);
342 write(dst_fd, buffer, buffer_pos);