1 /* vi: set sw=4 ts=4: */
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 source tree.
12 #include "bb_archive.h"
14 #if ENABLE_FEATURE_LZMA_FAST
15 # define speed_inline ALWAYS_INLINE
19 # define size_inline ALWAYS_INLINE
27 /* Was keeping rc on stack in unlzma and separately allocating buffer,
28 * but with "buffer 'attached to' allocated rc" code is smaller: */
29 /* uint8_t *buffer; */
30 #define RC_BUFFER ((uint8_t*)(rc+1))
34 /* Had provisions for variable buffer, but we don't need it here */
35 /* int buffer_size; */
36 #define RC_BUFFER_SIZE 0x10000
43 #define RC_TOP_BITS 24
44 #define RC_MOVE_BITS 5
45 #define RC_MODEL_TOTAL_BITS 11
48 /* Called once in rc_do_normalize() */
49 static void rc_read(rc_t *rc)
51 int buffer_size = safe_read(rc->fd, RC_BUFFER, RC_BUFFER_SIZE);
52 //TODO: return -1 instead
53 //This will make unlzma delete broken unpacked file on unpack errors
55 bb_error_msg_and_die("unexpected EOF");
56 rc->buffer_end = RC_BUFFER + buffer_size;
60 /* Called twice, but one callsite is in speed_inline'd rc_is_bit_1() */
61 static void rc_do_normalize(rc_t *rc)
63 if (rc->ptr >= rc->buffer_end)
66 rc->code = (rc->code << 8) | *rc->ptr++;
68 static ALWAYS_INLINE void rc_normalize(rc_t *rc)
70 if (rc->range < (1 << RC_TOP_BITS)) {
76 static ALWAYS_INLINE rc_t* rc_init(int fd) /*, int buffer_size) */
81 rc = xzalloc(sizeof(*rc) + RC_BUFFER_SIZE);
84 /* rc->ptr = rc->buffer_end; */
86 for (i = 0; i < 5; i++) {
89 rc->range = 0xffffffff;
94 static ALWAYS_INLINE void rc_free(rc_t *rc)
99 /* rc_is_bit_1 is called 9 times */
100 static speed_inline int rc_is_bit_1(rc_t *rc, uint16_t *p)
103 rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
104 if (rc->code < rc->bound) {
105 rc->range = rc->bound;
106 *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
109 rc->range -= rc->bound;
110 rc->code -= rc->bound;
111 *p -= *p >> RC_MOVE_BITS;
115 /* Called 4 times in unlzma loop */
116 static ALWAYS_INLINE int rc_get_bit(rc_t *rc, uint16_t *p, int *symbol)
118 int ret = rc_is_bit_1(rc, p);
119 *symbol = *symbol * 2 + ret;
124 static ALWAYS_INLINE int rc_direct_bit(rc_t *rc)
128 if (rc->code >= rc->range) {
129 rc->code -= rc->range;
136 static speed_inline void
137 rc_bit_tree_decode(rc_t *rc, uint16_t *p, int num_levels, int *symbol)
143 rc_get_bit(rc, p + *symbol, symbol);
144 *symbol -= 1 << num_levels;
152 } PACKED lzma_header_t;
155 /* #defines will force compiler to compute/optimize each one with each usage.
156 * Have heart and use enum instead. */
158 LZMA_BASE_SIZE = 1846,
161 LZMA_NUM_POS_BITS_MAX = 4,
163 LZMA_LEN_NUM_LOW_BITS = 3,
164 LZMA_LEN_NUM_MID_BITS = 3,
165 LZMA_LEN_NUM_HIGH_BITS = 8,
168 LZMA_LEN_CHOICE_2 = (LZMA_LEN_CHOICE + 1),
169 LZMA_LEN_LOW = (LZMA_LEN_CHOICE_2 + 1),
170 LZMA_LEN_MID = (LZMA_LEN_LOW \
171 + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS))),
172 LZMA_LEN_HIGH = (LZMA_LEN_MID \
173 + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS))),
174 LZMA_NUM_LEN_PROBS = (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS)),
176 LZMA_NUM_STATES = 12,
177 LZMA_NUM_LIT_STATES = 7,
179 LZMA_START_POS_MODEL_INDEX = 4,
180 LZMA_END_POS_MODEL_INDEX = 14,
181 LZMA_NUM_FULL_DISTANCES = (1 << (LZMA_END_POS_MODEL_INDEX >> 1)),
183 LZMA_NUM_POS_SLOT_BITS = 6,
184 LZMA_NUM_LEN_TO_POS_STATES = 4,
186 LZMA_NUM_ALIGN_BITS = 4,
188 LZMA_MATCH_MIN_LEN = 2,
191 LZMA_IS_REP = (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)),
192 LZMA_IS_REP_G0 = (LZMA_IS_REP + LZMA_NUM_STATES),
193 LZMA_IS_REP_G1 = (LZMA_IS_REP_G0 + LZMA_NUM_STATES),
194 LZMA_IS_REP_G2 = (LZMA_IS_REP_G1 + LZMA_NUM_STATES),
195 LZMA_IS_REP_0_LONG = (LZMA_IS_REP_G2 + LZMA_NUM_STATES),
196 LZMA_POS_SLOT = (LZMA_IS_REP_0_LONG \
197 + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)),
198 LZMA_SPEC_POS = (LZMA_POS_SLOT \
199 + (LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS)),
200 LZMA_ALIGN = (LZMA_SPEC_POS \
201 + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX),
202 LZMA_LEN_CODER = (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS)),
203 LZMA_REP_LEN_CODER = (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS),
204 LZMA_LITERAL = (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS),
208 IF_DESKTOP(long long) int FAST_FUNC
209 unpack_lzma_stream(transformer_state_t *xstate)
211 IF_DESKTOP(long long total_written = 0;)
212 lzma_header_t header;
214 uint32_t pos_state_mask;
215 uint32_t literal_pos_mask;
220 uint8_t previous_byte = 0;
221 size_t buffer_pos = 0, global_pos = 0;
224 uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
226 if (full_read(xstate->src_fd, &header, sizeof(header)) != sizeof(header)
227 || header.pos >= (9 * 5 * 5)
229 bb_error_msg("bad lzma header");
237 pos_state_mask = (1 << pb) - 1;
238 literal_pos_mask = (1 << lp) - 1;
240 /* Example values from linux-3.3.4.tar.lzma:
241 * dict_size: 64M, dst_size: 2^64-1
243 header.dict_size = SWAP_LE32(header.dict_size);
244 header.dst_size = SWAP_LE64(header.dst_size);
246 if (header.dict_size == 0)
249 buffer = xmalloc(MIN(header.dst_size, header.dict_size));
254 num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
255 p = xmalloc(num_probs * sizeof(*p));
256 num_probs += LZMA_LITERAL - LZMA_BASE_SIZE;
257 for (i = 0; i < num_probs; i++)
258 p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
261 rc = rc_init(xstate->src_fd); /*, RC_BUFFER_SIZE); */
263 while (global_pos + buffer_pos < header.dst_size) {
264 int pos_state = (buffer_pos + global_pos) & pos_state_mask;
265 uint16_t *prob = p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
267 if (!rc_is_bit_1(rc, prob)) {
268 static const char next_state[LZMA_NUM_STATES] =
269 { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
272 prob = (p + LZMA_LITERAL
273 + (LZMA_LIT_SIZE * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
274 + (previous_byte >> (8 - lc))
279 if (state >= LZMA_NUM_LIT_STATES) {
283 pos = buffer_pos - rep0;
284 if ((int32_t)pos < 0)
285 pos += header.dict_size;
286 match_byte = buffer[pos];
291 bit = match_byte & 0x100;
292 bit ^= (rc_get_bit(rc, prob + 0x100 + bit + mi, &mi) << 8); /* 0x100 or 0 */
295 } while (mi < 0x100);
298 rc_get_bit(rc, prob + mi, &mi);
301 state = next_state[state];
303 previous_byte = (uint8_t) mi;
304 #if ENABLE_FEATURE_LZMA_FAST
306 buffer[buffer_pos++] = previous_byte;
307 if (buffer_pos == header.dict_size) {
309 global_pos += header.dict_size;
310 if (transformer_write(xstate, buffer, header.dict_size) != (ssize_t)header.dict_size)
312 IF_DESKTOP(total_written += header.dict_size;)
322 #define prob_len prob2
324 prob2 = p + LZMA_IS_REP + state;
325 if (!rc_is_bit_1(rc, prob2)) {
329 state = state < LZMA_NUM_LIT_STATES ? 0 : 3;
330 prob2 = p + LZMA_LEN_CODER;
332 prob2 += LZMA_IS_REP_G0 - LZMA_IS_REP;
333 if (!rc_is_bit_1(rc, prob2)) {
334 prob2 = (p + LZMA_IS_REP_0_LONG
335 + (state << LZMA_NUM_POS_BITS_MAX)
338 if (!rc_is_bit_1(rc, prob2)) {
339 #if ENABLE_FEATURE_LZMA_FAST
341 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
343 pos = buffer_pos - rep0;
344 if ((int32_t)pos < 0)
345 pos += header.dict_size;
346 previous_byte = buffer[pos];
349 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
357 prob2 += LZMA_IS_REP_G1 - LZMA_IS_REP_G0;
359 if (rc_is_bit_1(rc, prob2)) {
360 prob2 += LZMA_IS_REP_G2 - LZMA_IS_REP_G1;
362 if (rc_is_bit_1(rc, prob2)) {
371 state = state < LZMA_NUM_LIT_STATES ? 8 : 11;
372 prob2 = p + LZMA_REP_LEN_CODER;
375 prob_len = prob2 + LZMA_LEN_CHOICE;
376 num_bits = LZMA_LEN_NUM_LOW_BITS;
377 if (!rc_is_bit_1(rc, prob_len)) {
378 prob_len += LZMA_LEN_LOW - LZMA_LEN_CHOICE
379 + (pos_state << LZMA_LEN_NUM_LOW_BITS);
382 prob_len += LZMA_LEN_CHOICE_2 - LZMA_LEN_CHOICE;
383 if (!rc_is_bit_1(rc, prob_len)) {
384 prob_len += LZMA_LEN_MID - LZMA_LEN_CHOICE_2
385 + (pos_state << LZMA_LEN_NUM_MID_BITS);
386 offset = 1 << LZMA_LEN_NUM_LOW_BITS;
387 num_bits += LZMA_LEN_NUM_MID_BITS - LZMA_LEN_NUM_LOW_BITS;
389 prob_len += LZMA_LEN_HIGH - LZMA_LEN_CHOICE_2;
390 offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
391 + (1 << LZMA_LEN_NUM_MID_BITS));
392 num_bits += LZMA_LEN_NUM_HIGH_BITS - LZMA_LEN_NUM_LOW_BITS;
395 rc_bit_tree_decode(rc, prob_len, num_bits, &len);
402 state += LZMA_NUM_LIT_STATES;
403 prob3 = p + LZMA_POS_SLOT +
404 ((len < LZMA_NUM_LEN_TO_POS_STATES ? len :
405 LZMA_NUM_LEN_TO_POS_STATES - 1)
406 << LZMA_NUM_POS_SLOT_BITS);
407 rc_bit_tree_decode(rc, prob3,
408 LZMA_NUM_POS_SLOT_BITS, &pos_slot);
410 if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
411 int i2, mi2, num_bits2 = (pos_slot >> 1) - 1;
412 rep0 = 2 | (pos_slot & 1);
413 if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
415 prob3 = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
417 for (; num_bits2 != LZMA_NUM_ALIGN_BITS; num_bits2--)
418 rep0 = (rep0 << 1) | rc_direct_bit(rc);
419 rep0 <<= LZMA_NUM_ALIGN_BITS;
420 prob3 = p + LZMA_ALIGN;
424 while (num_bits2--) {
425 if (rc_get_bit(rc, prob3 + mi2, &mi2))
434 len += LZMA_MATCH_MIN_LEN;
436 * LZMA SDK has this optimized:
437 * it precalculates size and copies many bytes
438 * in a loop with simpler checks, a-la:
440 * *(dest) = *(dest + ofs);
441 * while (++dest != lim);
444 * buffer[buffer_pos++] = buffer[pos];
445 * if (++pos == header.dict_size)
447 * } while (--cur_len != 0);
448 * Our code is slower (more checks per byte copy):
450 IF_NOT_FEATURE_LZMA_FAST(string:)
452 uint32_t pos = buffer_pos - rep0;
453 if ((int32_t)pos < 0)
454 pos += header.dict_size;
455 previous_byte = buffer[pos];
456 IF_NOT_FEATURE_LZMA_FAST(one_byte2:)
457 buffer[buffer_pos++] = previous_byte;
458 if (buffer_pos == header.dict_size) {
460 global_pos += header.dict_size;
461 if (transformer_write(xstate, buffer, header.dict_size) != (ssize_t)header.dict_size)
463 IF_DESKTOP(total_written += header.dict_size;)
466 } while (len != 0 && buffer_pos < header.dst_size);
467 /* FIXME: ...........^^^^^
468 * shouldn't it be "global_pos + buffer_pos < header.dst_size"?
469 * It probably should, but it is a "do we accidentally
470 * unpack more bytes than expected?" check - which
471 * never happens for well-formed compression data...
477 IF_NOT_DESKTOP(int total_written = 0; /* success */)
478 IF_DESKTOP(total_written += buffer_pos;)
479 if (transformer_write(xstate, buffer, buffer_pos) != (ssize_t)buffer_pos) {
481 total_written = -1; /* failure */
486 return total_written;