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"
15 # define dbg(...) bb_error_msg(__VA_ARGS__)
17 # define dbg(...) ((void)0)
21 #if ENABLE_FEATURE_LZMA_FAST
22 # define speed_inline ALWAYS_INLINE
26 # define size_inline ALWAYS_INLINE
34 /* Was keeping rc on stack in unlzma and separately allocating buffer,
35 * but with "buffer 'attached to' allocated rc" code is smaller: */
36 /* uint8_t *buffer; */
37 #define RC_BUFFER ((uint8_t*)(rc+1))
41 /* Had provisions for variable buffer, but we don't need it here */
42 /* int buffer_size; */
43 #define RC_BUFFER_SIZE 0x10000
50 #define RC_TOP_BITS 24
51 #define RC_MOVE_BITS 5
52 #define RC_MODEL_TOTAL_BITS 11
55 /* Called once in rc_do_normalize() */
56 static void rc_read(rc_t *rc)
58 int buffer_size = safe_read(rc->fd, RC_BUFFER, RC_BUFFER_SIZE);
59 //TODO: return -1 instead
60 //This will make unlzma delete broken unpacked file on unpack errors
62 bb_error_msg_and_die("unexpected EOF");
63 rc->buffer_end = RC_BUFFER + buffer_size;
67 /* Called twice, but one callsite is in speed_inline'd rc_is_bit_1() */
68 static void rc_do_normalize(rc_t *rc)
70 if (rc->ptr >= rc->buffer_end)
73 rc->code = (rc->code << 8) | *rc->ptr++;
75 static ALWAYS_INLINE void rc_normalize(rc_t *rc)
77 if (rc->range < (1 << RC_TOP_BITS)) {
83 static ALWAYS_INLINE rc_t* rc_init(int fd) /*, int buffer_size) */
88 rc = xzalloc(sizeof(*rc) + RC_BUFFER_SIZE);
91 /* rc->ptr = rc->buffer_end; */
93 for (i = 0; i < 5; i++) {
96 rc->range = 0xffffffff;
101 static ALWAYS_INLINE void rc_free(rc_t *rc)
106 /* rc_is_bit_1 is called 9 times */
107 static speed_inline int rc_is_bit_1(rc_t *rc, uint16_t *p)
110 rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
111 if (rc->code < rc->bound) {
112 rc->range = rc->bound;
113 *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
116 rc->range -= rc->bound;
117 rc->code -= rc->bound;
118 *p -= *p >> RC_MOVE_BITS;
122 /* Called 4 times in unlzma loop */
123 static ALWAYS_INLINE int rc_get_bit(rc_t *rc, uint16_t *p, int *symbol)
125 int ret = rc_is_bit_1(rc, p);
126 *symbol = *symbol * 2 + ret;
131 static ALWAYS_INLINE int rc_direct_bit(rc_t *rc)
135 if (rc->code >= rc->range) {
136 rc->code -= rc->range;
143 static speed_inline void
144 rc_bit_tree_decode(rc_t *rc, uint16_t *p, int num_levels, int *symbol)
150 rc_get_bit(rc, p + *symbol, symbol);
151 *symbol -= 1 << num_levels;
159 } PACKED lzma_header_t;
162 /* #defines will force compiler to compute/optimize each one with each usage.
163 * Have heart and use enum instead. */
165 LZMA_BASE_SIZE = 1846,
168 LZMA_NUM_POS_BITS_MAX = 4,
170 LZMA_LEN_NUM_LOW_BITS = 3,
171 LZMA_LEN_NUM_MID_BITS = 3,
172 LZMA_LEN_NUM_HIGH_BITS = 8,
175 LZMA_LEN_CHOICE_2 = (LZMA_LEN_CHOICE + 1),
176 LZMA_LEN_LOW = (LZMA_LEN_CHOICE_2 + 1),
177 LZMA_LEN_MID = (LZMA_LEN_LOW \
178 + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS))),
179 LZMA_LEN_HIGH = (LZMA_LEN_MID \
180 + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS))),
181 LZMA_NUM_LEN_PROBS = (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS)),
183 LZMA_NUM_STATES = 12,
184 LZMA_NUM_LIT_STATES = 7,
186 LZMA_START_POS_MODEL_INDEX = 4,
187 LZMA_END_POS_MODEL_INDEX = 14,
188 LZMA_NUM_FULL_DISTANCES = (1 << (LZMA_END_POS_MODEL_INDEX >> 1)),
190 LZMA_NUM_POS_SLOT_BITS = 6,
191 LZMA_NUM_LEN_TO_POS_STATES = 4,
193 LZMA_NUM_ALIGN_BITS = 4,
195 LZMA_MATCH_MIN_LEN = 2,
198 LZMA_IS_REP = (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)),
199 LZMA_IS_REP_G0 = (LZMA_IS_REP + LZMA_NUM_STATES),
200 LZMA_IS_REP_G1 = (LZMA_IS_REP_G0 + LZMA_NUM_STATES),
201 LZMA_IS_REP_G2 = (LZMA_IS_REP_G1 + LZMA_NUM_STATES),
202 LZMA_IS_REP_0_LONG = (LZMA_IS_REP_G2 + LZMA_NUM_STATES),
203 LZMA_POS_SLOT = (LZMA_IS_REP_0_LONG \
204 + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)),
205 LZMA_SPEC_POS = (LZMA_POS_SLOT \
206 + (LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS)),
207 LZMA_ALIGN = (LZMA_SPEC_POS \
208 + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX),
209 LZMA_LEN_CODER = (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS)),
210 LZMA_REP_LEN_CODER = (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS),
211 LZMA_LITERAL = (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS),
215 IF_DESKTOP(long long) int FAST_FUNC
216 unpack_lzma_stream(transformer_state_t *xstate)
218 IF_DESKTOP(long long total_written = 0;)
219 lzma_header_t header;
221 uint32_t pos_state_mask;
222 uint32_t literal_pos_mask;
227 uint8_t previous_byte = 0;
228 size_t buffer_pos = 0, global_pos = 0;
231 uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
233 if (full_read(xstate->src_fd, &header, sizeof(header)) != sizeof(header)
234 || header.pos >= (9 * 5 * 5)
236 bb_error_msg("bad lzma header");
244 pos_state_mask = (1 << pb) - 1;
245 literal_pos_mask = (1 << lp) - 1;
247 /* Example values from linux-3.3.4.tar.lzma:
248 * dict_size: 64M, dst_size: 2^64-1
250 header.dict_size = SWAP_LE32(header.dict_size);
251 header.dst_size = SWAP_LE64(header.dst_size);
253 if (header.dict_size == 0)
256 buffer = xmalloc(MIN(header.dst_size, header.dict_size));
261 num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
262 p = xmalloc(num_probs * sizeof(*p));
263 num_probs += LZMA_LITERAL - LZMA_BASE_SIZE;
264 for (i = 0; i < num_probs; i++)
265 p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
268 rc = rc_init(xstate->src_fd); /*, RC_BUFFER_SIZE); */
270 while (global_pos + buffer_pos < header.dst_size) {
271 int pos_state = (buffer_pos + global_pos) & pos_state_mask;
272 uint16_t *prob = p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
274 if (!rc_is_bit_1(rc, prob)) {
275 static const char next_state[LZMA_NUM_STATES] =
276 { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
279 prob = (p + LZMA_LITERAL
280 + (LZMA_LIT_SIZE * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
281 + (previous_byte >> (8 - lc))
286 if (state >= LZMA_NUM_LIT_STATES) {
290 pos = buffer_pos - rep0;
291 if ((int32_t)pos < 0)
292 pos += header.dict_size;
293 match_byte = buffer[pos];
298 bit = match_byte & 0x100;
299 bit ^= (rc_get_bit(rc, prob + 0x100 + bit + mi, &mi) << 8); /* 0x100 or 0 */
302 } while (mi < 0x100);
305 rc_get_bit(rc, prob + mi, &mi);
308 state = next_state[state];
310 previous_byte = (uint8_t) mi;
311 #if ENABLE_FEATURE_LZMA_FAST
313 buffer[buffer_pos++] = previous_byte;
314 if (buffer_pos == header.dict_size) {
316 global_pos += header.dict_size;
317 if (transformer_write(xstate, buffer, header.dict_size) != (ssize_t)header.dict_size)
319 IF_DESKTOP(total_written += header.dict_size;)
329 #define prob_len prob2
331 prob2 = p + LZMA_IS_REP + state;
332 if (!rc_is_bit_1(rc, prob2)) {
336 state = state < LZMA_NUM_LIT_STATES ? 0 : 3;
337 prob2 = p + LZMA_LEN_CODER;
339 prob2 += LZMA_IS_REP_G0 - LZMA_IS_REP;
340 if (!rc_is_bit_1(rc, prob2)) {
341 prob2 = (p + LZMA_IS_REP_0_LONG
342 + (state << LZMA_NUM_POS_BITS_MAX)
345 if (!rc_is_bit_1(rc, prob2)) {
346 #if ENABLE_FEATURE_LZMA_FAST
348 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
350 pos = buffer_pos - rep0;
351 if ((int32_t)pos < 0)
352 pos += header.dict_size;
353 previous_byte = buffer[pos];
356 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
364 prob2 += LZMA_IS_REP_G1 - LZMA_IS_REP_G0;
366 if (rc_is_bit_1(rc, prob2)) {
367 prob2 += LZMA_IS_REP_G2 - LZMA_IS_REP_G1;
369 if (rc_is_bit_1(rc, prob2)) {
378 state = state < LZMA_NUM_LIT_STATES ? 8 : 11;
379 prob2 = p + LZMA_REP_LEN_CODER;
382 prob_len = prob2 + LZMA_LEN_CHOICE;
383 num_bits = LZMA_LEN_NUM_LOW_BITS;
384 if (!rc_is_bit_1(rc, prob_len)) {
385 prob_len += LZMA_LEN_LOW - LZMA_LEN_CHOICE
386 + (pos_state << LZMA_LEN_NUM_LOW_BITS);
389 prob_len += LZMA_LEN_CHOICE_2 - LZMA_LEN_CHOICE;
390 if (!rc_is_bit_1(rc, prob_len)) {
391 prob_len += LZMA_LEN_MID - LZMA_LEN_CHOICE_2
392 + (pos_state << LZMA_LEN_NUM_MID_BITS);
393 offset = 1 << LZMA_LEN_NUM_LOW_BITS;
394 num_bits += LZMA_LEN_NUM_MID_BITS - LZMA_LEN_NUM_LOW_BITS;
396 prob_len += LZMA_LEN_HIGH - LZMA_LEN_CHOICE_2;
397 offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
398 + (1 << LZMA_LEN_NUM_MID_BITS));
399 num_bits += LZMA_LEN_NUM_HIGH_BITS - LZMA_LEN_NUM_LOW_BITS;
402 rc_bit_tree_decode(rc, prob_len, num_bits, &len);
409 state += LZMA_NUM_LIT_STATES;
410 prob3 = p + LZMA_POS_SLOT +
411 ((len < LZMA_NUM_LEN_TO_POS_STATES ? len :
412 LZMA_NUM_LEN_TO_POS_STATES - 1)
413 << LZMA_NUM_POS_SLOT_BITS);
414 rc_bit_tree_decode(rc, prob3,
415 LZMA_NUM_POS_SLOT_BITS, &pos_slot);
417 if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
418 int i2, mi2, num_bits2 = (pos_slot >> 1) - 1;
419 rep0 = 2 | (pos_slot & 1);
420 if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
422 prob3 = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
424 for (; num_bits2 != LZMA_NUM_ALIGN_BITS; num_bits2--)
425 rep0 = (rep0 << 1) | rc_direct_bit(rc);
426 rep0 <<= LZMA_NUM_ALIGN_BITS;
427 if ((int32_t)rep0 < 0) {
428 dbg("%d rep0:%d", __LINE__, rep0);
431 prob3 = p + LZMA_ALIGN;
435 while (num_bits2--) {
436 if (rc_get_bit(rc, prob3 + mi2, &mi2))
445 len += LZMA_MATCH_MIN_LEN;
447 * LZMA SDK has this optimized:
448 * it precalculates size and copies many bytes
449 * in a loop with simpler checks, a-la:
451 * *(dest) = *(dest + ofs);
452 * while (++dest != lim);
455 * buffer[buffer_pos++] = buffer[pos];
456 * if (++pos == header.dict_size)
458 * } while (--cur_len != 0);
459 * Our code is slower (more checks per byte copy):
461 IF_NOT_FEATURE_LZMA_FAST(string:)
463 uint32_t pos = buffer_pos - rep0;
464 if ((int32_t)pos < 0) {
465 pos += header.dict_size;
466 /* bug 10436 has an example file where this triggers: */
467 if ((int32_t)pos < 0)
470 previous_byte = buffer[pos];
471 IF_NOT_FEATURE_LZMA_FAST(one_byte2:)
472 buffer[buffer_pos++] = previous_byte;
473 if (buffer_pos == header.dict_size) {
475 global_pos += header.dict_size;
476 if (transformer_write(xstate, buffer, header.dict_size) != (ssize_t)header.dict_size)
478 IF_DESKTOP(total_written += header.dict_size;)
481 } while (len != 0 && buffer_pos < header.dst_size);
482 /* FIXME: ...........^^^^^
483 * shouldn't it be "global_pos + buffer_pos < header.dst_size"?
484 * It probably should, but it is a "do we accidentally
485 * unpack more bytes than expected?" check - which
486 * never happens for well-formed compression data...
492 IF_NOT_DESKTOP(int total_written = 0; /* success */)
493 IF_DESKTOP(total_written += buffer_pos;)
494 if (transformer_write(xstate, buffer, buffer_pos) != (ssize_t)buffer_pos) {
496 total_written = -1; /* failure */
501 return total_written;