3 * Small lzma deflate implementation.
4 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
6 * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
7 * Copyright (C) 1999-2005 Igor Pavlov
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
13 #include "unarchive.h"
15 #ifdef CONFIG_FEATURE_LZMA_FAST
16 # define speed_inline ATTRIBUTE_ALWAYS_INLINE
34 #define RC_TOP_BITS 24
35 #define RC_MOVE_BITS 5
36 #define RC_MODEL_TOTAL_BITS 11
39 /* Called twice: once at startup and once in rc_normalize() */
40 static void rc_read(rc_t * rc)
42 rc->buffer_size = read(rc->fd, rc->buffer, rc->buffer_size);
43 if (rc->buffer_size <= 0)
44 bb_error_msg_and_die("unexpected EOF");
46 rc->buffer_end = rc->buffer + rc->buffer_size;
50 static void rc_init(rc_t * rc, int fd, int buffer_size)
55 rc->buffer = xmalloc(buffer_size);
56 rc->buffer_size = buffer_size;
57 rc->buffer_end = rc->buffer + rc->buffer_size;
58 rc->ptr = rc->buffer_end;
61 rc->range = 0xFFFFFFFF;
62 for (i = 0; i < 5; i++) {
63 if (rc->ptr >= rc->buffer_end)
65 rc->code = (rc->code << 8) | *rc->ptr++;
69 /* Called once. TODO: bb_maybe_free() */
70 static ATTRIBUTE_ALWAYS_INLINE void rc_free(rc_t * rc)
72 if (ENABLE_FEATURE_CLEAN_UP)
76 /* Called twice, but one callsite is in speed_inline'd rc_is_bit_0_helper() */
77 static void rc_do_normalize(rc_t * rc)
79 if (rc->ptr >= rc->buffer_end)
82 rc->code = (rc->code << 8) | *rc->ptr++;
84 static ATTRIBUTE_ALWAYS_INLINE void rc_normalize(rc_t * rc)
86 if (rc->range < (1 << RC_TOP_BITS)) {
92 /* Why rc_is_bit_0_helper exists?
93 * Because we want to always expose (rc->code < rc->bound) to optimizer
95 static speed_inline uint32_t rc_is_bit_0_helper(rc_t * rc, uint16_t * p)
98 rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
101 static ATTRIBUTE_ALWAYS_INLINE int rc_is_bit_0(rc_t * rc, uint16_t * p)
103 uint32_t t = rc_is_bit_0_helper(rc, p);
107 /* Called ~10 times, but very small, thus inlined */
108 static speed_inline void rc_update_bit_0(rc_t * rc, uint16_t * p)
110 rc->range = rc->bound;
111 *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
113 static speed_inline void rc_update_bit_1(rc_t * rc, uint16_t * p)
115 rc->range -= rc->bound;
116 rc->code -= rc->bound;
117 *p -= *p >> RC_MOVE_BITS;
120 /* Called 4 times in unlzma loop */
121 static int rc_get_bit(rc_t * rc, uint16_t * p, int *symbol)
123 if (rc_is_bit_0(rc, p)) {
124 rc_update_bit_0(rc, p);
128 rc_update_bit_1(rc, p);
129 *symbol = *symbol * 2 + 1;
135 static ATTRIBUTE_ALWAYS_INLINE int rc_direct_bit(rc_t * rc)
139 if (rc->code >= rc->range) {
140 rc->code -= rc->range;
147 static speed_inline void
148 rc_bit_tree_decode(rc_t * rc, uint16_t * p, int num_levels, int *symbol)
154 rc_get_bit(rc, p + *symbol, symbol);
155 *symbol -= 1 << num_levels;
163 } __attribute__ ((packed)) lzma_header_t;
166 #define LZMA_BASE_SIZE 1846
167 #define LZMA_LIT_SIZE 768
169 #define LZMA_NUM_POS_BITS_MAX 4
171 #define LZMA_LEN_NUM_LOW_BITS 3
172 #define LZMA_LEN_NUM_MID_BITS 3
173 #define LZMA_LEN_NUM_HIGH_BITS 8
175 #define LZMA_LEN_CHOICE 0
176 #define LZMA_LEN_CHOICE_2 (LZMA_LEN_CHOICE + 1)
177 #define LZMA_LEN_LOW (LZMA_LEN_CHOICE_2 + 1)
178 #define LZMA_LEN_MID (LZMA_LEN_LOW \
179 + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS)))
180 #define LZMA_LEN_HIGH (LZMA_LEN_MID \
181 +(1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS)))
182 #define LZMA_NUM_LEN_PROBS (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS))
184 #define LZMA_NUM_STATES 12
185 #define LZMA_NUM_LIT_STATES 7
187 #define LZMA_START_POS_MODEL_INDEX 4
188 #define LZMA_END_POS_MODEL_INDEX 14
189 #define LZMA_NUM_FULL_DISTANCES (1 << (LZMA_END_POS_MODEL_INDEX >> 1))
191 #define LZMA_NUM_POS_SLOT_BITS 6
192 #define LZMA_NUM_LEN_TO_POS_STATES 4
194 #define LZMA_NUM_ALIGN_BITS 4
196 #define LZMA_MATCH_MIN_LEN 2
198 #define LZMA_IS_MATCH 0
199 #define LZMA_IS_REP (LZMA_IS_MATCH + (LZMA_NUM_STATES <<LZMA_NUM_POS_BITS_MAX))
200 #define LZMA_IS_REP_G0 (LZMA_IS_REP + LZMA_NUM_STATES)
201 #define LZMA_IS_REP_G1 (LZMA_IS_REP_G0 + LZMA_NUM_STATES)
202 #define LZMA_IS_REP_G2 (LZMA_IS_REP_G1 + LZMA_NUM_STATES)
203 #define LZMA_IS_REP_0_LONG (LZMA_IS_REP_G2 + LZMA_NUM_STATES)
204 #define LZMA_POS_SLOT (LZMA_IS_REP_0_LONG \
205 + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
206 #define LZMA_SPEC_POS (LZMA_POS_SLOT \
207 +(LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS))
208 #define LZMA_ALIGN (LZMA_SPEC_POS \
209 + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX)
210 #define LZMA_LEN_CODER (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS))
211 #define LZMA_REP_LEN_CODER (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS)
212 #define LZMA_LITERAL (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS)
215 int unlzma(int src_fd, int dst_fd)
217 lzma_header_t header;
219 uint32_t pos_state_mask;
220 uint32_t literal_pos_mask;
230 uint8_t previous_byte = 0;
231 size_t buffer_pos = 0, global_pos = 0;
234 uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
236 if (read(src_fd, &header, sizeof(header)) != sizeof(header))
237 bb_error_msg_and_die("can't read header");
239 if (header.pos >= (9 * 5 * 5))
240 bb_error_msg_and_die("bad header");
245 pos_state_mask = (1 << pb) - 1;
246 literal_pos_mask = (1 << lp) - 1;
248 header.dict_size = SWAP_LE32(header.dict_size);
249 header.dst_size = SWAP_LE64(header.dst_size);
251 if (header.dict_size == 0)
252 header.dict_size = 1;
254 buffer = xmalloc(MIN(header.dst_size, header.dict_size));
256 num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
257 p = xmalloc(num_probs * sizeof(*p));
258 num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp));
259 for (i = 0; i < num_probs; i++)
260 p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
262 rc_init(&rc, src_fd, 0x10000);
264 while (global_pos + buffer_pos < header.dst_size) {
265 int pos_state = (buffer_pos + global_pos) & pos_state_mask;
268 p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
269 if (rc_is_bit_0(&rc, prob)) {
271 rc_update_bit_0(&rc, prob);
272 prob = (p + LZMA_LITERAL + (LZMA_LIT_SIZE
273 * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
274 + (previous_byte >> (8 - lc)))));
276 if (state >= LZMA_NUM_LIT_STATES) {
279 pos = buffer_pos - rep0;
280 while (pos >= header.dict_size)
281 pos += header.dict_size;
282 match_byte = buffer[pos];
287 bit = match_byte & 0x100;
288 prob_lit = prob + 0x100 + bit + mi;
289 if (rc_get_bit(&rc, prob_lit, &mi)) {
296 } while (mi < 0x100);
299 prob_lit = prob + mi;
300 rc_get_bit(&rc, prob_lit, &mi);
302 previous_byte = (uint8_t) mi;
304 buffer[buffer_pos++] = previous_byte;
305 if (buffer_pos == header.dict_size) {
307 global_pos += header.dict_size;
308 write(dst_fd, buffer, header.dict_size);
320 rc_update_bit_1(&rc, prob);
321 prob = p + LZMA_IS_REP + state;
322 if (rc_is_bit_0(&rc, prob)) {
323 rc_update_bit_0(&rc, prob);
327 state = state < LZMA_NUM_LIT_STATES ? 0 : 3;
328 prob = p + LZMA_LEN_CODER;
330 rc_update_bit_1(&rc, prob);
331 prob = p + LZMA_IS_REP_G0 + state;
332 if (rc_is_bit_0(&rc, prob)) {
333 rc_update_bit_0(&rc, prob);
334 prob = (p + LZMA_IS_REP_0_LONG
335 + (state << LZMA_NUM_POS_BITS_MAX) + pos_state);
336 if (rc_is_bit_0(&rc, prob)) {
337 rc_update_bit_0(&rc, prob);
339 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
340 pos = buffer_pos - rep0;
341 while (pos >= header.dict_size)
342 pos += header.dict_size;
343 previous_byte = buffer[pos];
344 buffer[buffer_pos++] = previous_byte;
345 if (buffer_pos == header.dict_size) {
347 global_pos += header.dict_size;
348 write(dst_fd, buffer, header.dict_size);
352 rc_update_bit_1(&rc, prob);
357 rc_update_bit_1(&rc, prob);
358 prob = p + LZMA_IS_REP_G1 + state;
359 if (rc_is_bit_0(&rc, prob)) {
360 rc_update_bit_0(&rc, prob);
363 rc_update_bit_1(&rc, prob);
364 prob = p + LZMA_IS_REP_G2 + state;
365 if (rc_is_bit_0(&rc, prob)) {
366 rc_update_bit_0(&rc, prob);
369 rc_update_bit_1(&rc, prob);
378 state = state < LZMA_NUM_LIT_STATES ? 8 : 11;
379 prob = p + LZMA_REP_LEN_CODER;
382 prob_len = prob + LZMA_LEN_CHOICE;
383 if (rc_is_bit_0(&rc, prob_len)) {
384 rc_update_bit_0(&rc, prob_len);
385 prob_len = (prob + LZMA_LEN_LOW
386 + (pos_state << LZMA_LEN_NUM_LOW_BITS));
388 num_bits = LZMA_LEN_NUM_LOW_BITS;
390 rc_update_bit_1(&rc, prob_len);
391 prob_len = prob + LZMA_LEN_CHOICE_2;
392 if (rc_is_bit_0(&rc, prob_len)) {
393 rc_update_bit_0(&rc, prob_len);
394 prob_len = (prob + LZMA_LEN_MID
395 + (pos_state << LZMA_LEN_NUM_MID_BITS));
396 offset = 1 << LZMA_LEN_NUM_LOW_BITS;
397 num_bits = LZMA_LEN_NUM_MID_BITS;
399 rc_update_bit_1(&rc, prob_len);
400 prob_len = prob + LZMA_LEN_HIGH;
401 offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
402 + (1 << LZMA_LEN_NUM_MID_BITS));
403 num_bits = LZMA_LEN_NUM_HIGH_BITS;
406 rc_bit_tree_decode(&rc, prob_len, num_bits, &len);
412 state += LZMA_NUM_LIT_STATES;
416 LZMA_NUM_LEN_TO_POS_STATES ? len :
417 LZMA_NUM_LEN_TO_POS_STATES - 1)
418 << LZMA_NUM_POS_SLOT_BITS);
419 rc_bit_tree_decode(&rc, prob, LZMA_NUM_POS_SLOT_BITS,
421 if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
422 num_bits = (pos_slot >> 1) - 1;
423 rep0 = 2 | (pos_slot & 1);
424 if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
426 prob = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
428 num_bits -= LZMA_NUM_ALIGN_BITS;
430 rep0 = (rep0 << 1) | rc_direct_bit(&rc);
431 prob = p + LZMA_ALIGN;
432 rep0 <<= LZMA_NUM_ALIGN_BITS;
433 num_bits = LZMA_NUM_ALIGN_BITS;
438 if (rc_get_bit(&rc, prob + mi, &mi))
448 len += LZMA_MATCH_MIN_LEN;
451 pos = buffer_pos - rep0;
452 while (pos >= header.dict_size)
453 pos += header.dict_size;
454 previous_byte = buffer[pos];
455 buffer[buffer_pos++] = previous_byte;
456 if (buffer_pos == header.dict_size) {
458 global_pos += header.dict_size;
459 write(dst_fd, buffer, header.dict_size);
462 } while (len != 0 && buffer_pos < header.dst_size);
466 write(dst_fd, buffer, buffer_pos);