2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-01-20 23:58:50 +05:30
|
|
|
/*
|
|
|
|
* Small lzma deflate implementation.
|
|
|
|
* Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
|
|
|
|
*
|
|
|
|
* Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
|
|
|
|
* Copyright (C) 1999-2005 Igor Pavlov
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2006-01-20 23:58:50 +05:30
|
|
|
*/
|
|
|
|
#include "libbb.h"
|
2011-09-22 16:15:14 +05:30
|
|
|
#include "bb_archive.h"
|
2006-04-03 03:20:01 +05:30
|
|
|
|
2018-04-09 00:15:16 +05:30
|
|
|
#if 0
|
|
|
|
# define dbg(...) bb_error_msg(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
# define dbg(...) ((void)0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2007-08-13 16:06:25 +05:30
|
|
|
#if ENABLE_FEATURE_LZMA_FAST
|
2007-06-13 02:24:54 +05:30
|
|
|
# define speed_inline ALWAYS_INLINE
|
2009-09-16 03:10:08 +05:30
|
|
|
# define size_inline
|
2006-06-21 04:08:00 +05:30
|
|
|
#else
|
|
|
|
# define speed_inline
|
2009-09-16 03:10:08 +05:30
|
|
|
# define size_inline ALWAYS_INLINE
|
2006-06-21 04:08:00 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int fd;
|
|
|
|
uint8_t *ptr;
|
2007-01-05 19:34:47 +05:30
|
|
|
|
|
|
|
/* Was keeping rc on stack in unlzma and separately allocating buffer,
|
|
|
|
* but with "buffer 'attached to' allocated rc" code is smaller: */
|
|
|
|
/* uint8_t *buffer; */
|
|
|
|
#define RC_BUFFER ((uint8_t*)(rc+1))
|
|
|
|
|
2006-06-21 04:08:00 +05:30
|
|
|
uint8_t *buffer_end;
|
2007-01-05 19:34:47 +05:30
|
|
|
|
|
|
|
/* Had provisions for variable buffer, but we don't need it here */
|
|
|
|
/* int buffer_size; */
|
|
|
|
#define RC_BUFFER_SIZE 0x10000
|
|
|
|
|
2006-06-21 04:08:00 +05:30
|
|
|
uint32_t code;
|
|
|
|
uint32_t range;
|
|
|
|
uint32_t bound;
|
|
|
|
} rc_t;
|
|
|
|
|
|
|
|
#define RC_TOP_BITS 24
|
|
|
|
#define RC_MOVE_BITS 5
|
|
|
|
#define RC_MODEL_TOTAL_BITS 11
|
|
|
|
|
|
|
|
|
2013-03-01 19:07:58 +05:30
|
|
|
/* Called once in rc_do_normalize() */
|
|
|
|
static void rc_read(rc_t *rc)
|
2006-06-21 04:08:00 +05:30
|
|
|
{
|
2007-01-05 19:34:47 +05:30
|
|
|
int buffer_size = safe_read(rc->fd, RC_BUFFER, RC_BUFFER_SIZE);
|
2009-09-16 03:10:08 +05:30
|
|
|
//TODO: return -1 instead
|
|
|
|
//This will make unlzma delete broken unpacked file on unpack errors
|
2007-01-05 19:34:47 +05:30
|
|
|
if (buffer_size <= 0)
|
2006-06-21 04:08:00 +05:30
|
|
|
bb_error_msg_and_die("unexpected EOF");
|
2007-01-05 19:34:47 +05:30
|
|
|
rc->buffer_end = RC_BUFFER + buffer_size;
|
2013-03-01 19:07:58 +05:30
|
|
|
rc->ptr = RC_BUFFER;
|
2006-06-21 04:08:00 +05:30
|
|
|
}
|
|
|
|
|
2009-09-16 03:10:08 +05:30
|
|
|
/* Called twice, but one callsite is in speed_inline'd rc_is_bit_1() */
|
|
|
|
static void rc_do_normalize(rc_t *rc)
|
|
|
|
{
|
|
|
|
if (rc->ptr >= rc->buffer_end)
|
|
|
|
rc_read(rc);
|
|
|
|
rc->range <<= 8;
|
|
|
|
rc->code = (rc->code << 8) | *rc->ptr++;
|
|
|
|
}
|
2013-03-01 19:18:10 +05:30
|
|
|
static ALWAYS_INLINE void rc_normalize(rc_t *rc)
|
|
|
|
{
|
|
|
|
if (rc->range < (1 << RC_TOP_BITS)) {
|
|
|
|
rc_do_normalize(rc);
|
|
|
|
}
|
|
|
|
}
|
2009-09-16 03:10:08 +05:30
|
|
|
|
2006-06-21 04:08:00 +05:30
|
|
|
/* Called once */
|
2009-09-16 03:10:08 +05:30
|
|
|
static ALWAYS_INLINE rc_t* rc_init(int fd) /*, int buffer_size) */
|
2006-06-21 04:08:00 +05:30
|
|
|
{
|
|
|
|
int i;
|
2008-06-27 21:18:45 +05:30
|
|
|
rc_t *rc;
|
2007-01-05 19:29:05 +05:30
|
|
|
|
2009-09-16 03:10:08 +05:30
|
|
|
rc = xzalloc(sizeof(*rc) + RC_BUFFER_SIZE);
|
2006-06-21 04:08:00 +05:30
|
|
|
|
|
|
|
rc->fd = fd;
|
2009-09-16 03:10:08 +05:30
|
|
|
/* rc->ptr = rc->buffer_end; */
|
2006-06-21 04:08:00 +05:30
|
|
|
|
|
|
|
for (i = 0; i < 5; i++) {
|
2009-09-16 03:10:08 +05:30
|
|
|
rc_do_normalize(rc);
|
2006-06-21 04:08:00 +05:30
|
|
|
}
|
2013-03-01 19:07:58 +05:30
|
|
|
rc->range = 0xffffffff;
|
2007-01-05 19:29:05 +05:30
|
|
|
return rc;
|
2006-06-21 04:08:00 +05:30
|
|
|
}
|
|
|
|
|
2007-01-05 19:29:05 +05:30
|
|
|
/* Called once */
|
2008-06-27 21:18:45 +05:30
|
|
|
static ALWAYS_INLINE void rc_free(rc_t *rc)
|
2006-06-21 04:08:00 +05:30
|
|
|
{
|
2008-06-27 21:18:45 +05:30
|
|
|
free(rc);
|
2006-06-21 04:08:00 +05:30
|
|
|
}
|
|
|
|
|
2009-09-16 03:10:08 +05:30
|
|
|
/* rc_is_bit_1 is called 9 times */
|
|
|
|
static speed_inline int rc_is_bit_1(rc_t *rc, uint16_t *p)
|
2006-06-21 04:08:00 +05:30
|
|
|
{
|
|
|
|
rc_normalize(rc);
|
|
|
|
rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
|
2009-09-16 03:10:08 +05:30
|
|
|
if (rc->code < rc->bound) {
|
|
|
|
rc->range = rc->bound;
|
|
|
|
*p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-06-21 04:08:00 +05:30
|
|
|
rc->range -= rc->bound;
|
|
|
|
rc->code -= rc->bound;
|
|
|
|
*p -= *p >> RC_MOVE_BITS;
|
2009-09-16 03:10:08 +05:30
|
|
|
return 1;
|
2006-06-21 04:08:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Called 4 times in unlzma loop */
|
2013-03-01 19:13:07 +05:30
|
|
|
static ALWAYS_INLINE int rc_get_bit(rc_t *rc, uint16_t *p, int *symbol)
|
2006-06-21 04:08:00 +05:30
|
|
|
{
|
2009-09-16 03:10:08 +05:30
|
|
|
int ret = rc_is_bit_1(rc, p);
|
|
|
|
*symbol = *symbol * 2 + ret;
|
|
|
|
return ret;
|
2006-06-21 04:08:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Called once */
|
2008-06-27 21:18:45 +05:30
|
|
|
static ALWAYS_INLINE int rc_direct_bit(rc_t *rc)
|
2006-06-21 04:08:00 +05:30
|
|
|
{
|
|
|
|
rc_normalize(rc);
|
|
|
|
rc->range >>= 1;
|
|
|
|
if (rc->code >= rc->range) {
|
|
|
|
rc->code -= rc->range;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called twice */
|
|
|
|
static speed_inline void
|
2008-06-27 21:18:45 +05:30
|
|
|
rc_bit_tree_decode(rc_t *rc, uint16_t *p, int num_levels, int *symbol)
|
2006-06-21 04:08:00 +05:30
|
|
|
{
|
|
|
|
int i = num_levels;
|
|
|
|
|
|
|
|
*symbol = 1;
|
|
|
|
while (i--)
|
|
|
|
rc_get_bit(rc, p + *symbol, symbol);
|
|
|
|
*symbol -= 1 << num_levels;
|
|
|
|
}
|
2006-01-20 23:58:50 +05:30
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t pos;
|
|
|
|
uint32_t dict_size;
|
|
|
|
uint64_t dst_size;
|
2009-09-16 04:28:11 +05:30
|
|
|
} PACKED lzma_header_t;
|
2006-01-20 23:58:50 +05:30
|
|
|
|
|
|
|
|
2007-01-05 19:34:47 +05:30
|
|
|
/* #defines will force compiler to compute/optimize each one with each usage.
|
2007-01-05 19:29:05 +05:30
|
|
|
* Have heart and use enum instead. */
|
|
|
|
enum {
|
|
|
|
LZMA_BASE_SIZE = 1846,
|
|
|
|
LZMA_LIT_SIZE = 768,
|
|
|
|
|
|
|
|
LZMA_NUM_POS_BITS_MAX = 4,
|
2006-01-20 23:58:50 +05:30
|
|
|
|
2007-01-05 19:29:05 +05:30
|
|
|
LZMA_LEN_NUM_LOW_BITS = 3,
|
|
|
|
LZMA_LEN_NUM_MID_BITS = 3,
|
|
|
|
LZMA_LEN_NUM_HIGH_BITS = 8,
|
2006-01-20 23:58:50 +05:30
|
|
|
|
2007-01-05 19:29:05 +05:30
|
|
|
LZMA_LEN_CHOICE = 0,
|
|
|
|
LZMA_LEN_CHOICE_2 = (LZMA_LEN_CHOICE + 1),
|
|
|
|
LZMA_LEN_LOW = (LZMA_LEN_CHOICE_2 + 1),
|
|
|
|
LZMA_LEN_MID = (LZMA_LEN_LOW \
|
|
|
|
+ (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS))),
|
|
|
|
LZMA_LEN_HIGH = (LZMA_LEN_MID \
|
|
|
|
+ (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS))),
|
|
|
|
LZMA_NUM_LEN_PROBS = (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS)),
|
2006-01-20 23:58:50 +05:30
|
|
|
|
2007-01-05 19:29:05 +05:30
|
|
|
LZMA_NUM_STATES = 12,
|
|
|
|
LZMA_NUM_LIT_STATES = 7,
|
2006-01-20 23:58:50 +05:30
|
|
|
|
2007-01-05 19:29:05 +05:30
|
|
|
LZMA_START_POS_MODEL_INDEX = 4,
|
|
|
|
LZMA_END_POS_MODEL_INDEX = 14,
|
|
|
|
LZMA_NUM_FULL_DISTANCES = (1 << (LZMA_END_POS_MODEL_INDEX >> 1)),
|
2006-01-20 23:58:50 +05:30
|
|
|
|
2007-01-05 19:29:05 +05:30
|
|
|
LZMA_NUM_POS_SLOT_BITS = 6,
|
|
|
|
LZMA_NUM_LEN_TO_POS_STATES = 4,
|
2006-01-20 23:58:50 +05:30
|
|
|
|
2007-01-05 19:29:05 +05:30
|
|
|
LZMA_NUM_ALIGN_BITS = 4,
|
2006-01-20 23:58:50 +05:30
|
|
|
|
2007-01-05 19:29:05 +05:30
|
|
|
LZMA_MATCH_MIN_LEN = 2,
|
2006-01-20 23:58:50 +05:30
|
|
|
|
2007-01-05 19:29:05 +05:30
|
|
|
LZMA_IS_MATCH = 0,
|
|
|
|
LZMA_IS_REP = (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)),
|
|
|
|
LZMA_IS_REP_G0 = (LZMA_IS_REP + LZMA_NUM_STATES),
|
|
|
|
LZMA_IS_REP_G1 = (LZMA_IS_REP_G0 + LZMA_NUM_STATES),
|
|
|
|
LZMA_IS_REP_G2 = (LZMA_IS_REP_G1 + LZMA_NUM_STATES),
|
|
|
|
LZMA_IS_REP_0_LONG = (LZMA_IS_REP_G2 + LZMA_NUM_STATES),
|
|
|
|
LZMA_POS_SLOT = (LZMA_IS_REP_0_LONG \
|
|
|
|
+ (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)),
|
|
|
|
LZMA_SPEC_POS = (LZMA_POS_SLOT \
|
|
|
|
+ (LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS)),
|
|
|
|
LZMA_ALIGN = (LZMA_SPEC_POS \
|
|
|
|
+ LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX),
|
|
|
|
LZMA_LEN_CODER = (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS)),
|
|
|
|
LZMA_REP_LEN_CODER = (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS),
|
|
|
|
LZMA_LITERAL = (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS),
|
|
|
|
};
|
2006-01-20 23:58:50 +05:30
|
|
|
|
|
|
|
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_DESKTOP(long long) int FAST_FUNC
|
2014-12-07 05:14:00 +05:30
|
|
|
unpack_lzma_stream(transformer_state_t *xstate)
|
2006-01-20 23:58:50 +05:30
|
|
|
{
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_DESKTOP(long long total_written = 0;)
|
2006-01-20 23:58:50 +05:30
|
|
|
lzma_header_t header;
|
|
|
|
int lc, pb, lp;
|
|
|
|
uint32_t pos_state_mask;
|
|
|
|
uint32_t literal_pos_mask;
|
|
|
|
uint16_t *p;
|
2007-01-05 19:29:05 +05:30
|
|
|
rc_t *rc;
|
2009-09-16 03:10:08 +05:30
|
|
|
int i;
|
2006-01-20 23:58:50 +05:30
|
|
|
uint8_t *buffer;
|
2018-04-19 22:59:49 +05:30
|
|
|
uint32_t buffer_size;
|
2006-01-20 23:58:50 +05:30
|
|
|
uint8_t previous_byte = 0;
|
|
|
|
size_t buffer_pos = 0, global_pos = 0;
|
|
|
|
int len = 0;
|
|
|
|
int state = 0;
|
|
|
|
uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
|
|
|
|
|
2014-12-07 05:14:00 +05:30
|
|
|
if (full_read(xstate->src_fd, &header, sizeof(header)) != sizeof(header)
|
2009-09-16 03:10:08 +05:30
|
|
|
|| header.pos >= (9 * 5 * 5)
|
|
|
|
) {
|
|
|
|
bb_error_msg("bad lzma header");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-01-20 23:58:50 +05:30
|
|
|
|
2009-09-16 03:10:08 +05:30
|
|
|
i = header.pos / 9;
|
2006-01-20 23:58:50 +05:30
|
|
|
lc = header.pos % 9;
|
2009-09-16 03:10:08 +05:30
|
|
|
pb = i / 5;
|
|
|
|
lp = i % 5;
|
2006-01-20 23:58:50 +05:30
|
|
|
pos_state_mask = (1 << pb) - 1;
|
|
|
|
literal_pos_mask = (1 << lp) - 1;
|
|
|
|
|
2014-02-28 20:12:10 +05:30
|
|
|
/* Example values from linux-3.3.4.tar.lzma:
|
|
|
|
* dict_size: 64M, dst_size: 2^64-1
|
|
|
|
*/
|
2006-05-29 11:21:12 +05:30
|
|
|
header.dict_size = SWAP_LE32(header.dict_size);
|
|
|
|
header.dst_size = SWAP_LE64(header.dst_size);
|
2006-01-20 23:58:50 +05:30
|
|
|
|
|
|
|
if (header.dict_size == 0)
|
2009-09-16 03:10:08 +05:30
|
|
|
header.dict_size++;
|
2006-01-20 23:58:50 +05:30
|
|
|
|
2018-04-19 22:59:49 +05:30
|
|
|
buffer_size = MIN(header.dst_size, header.dict_size);
|
|
|
|
buffer = xmalloc(buffer_size);
|
2006-01-20 23:58:50 +05:30
|
|
|
|
2014-02-28 19:35:43 +05:30
|
|
|
{
|
|
|
|
int num_probs;
|
|
|
|
|
|
|
|
num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
|
|
|
|
p = xmalloc(num_probs * sizeof(*p));
|
|
|
|
num_probs += LZMA_LITERAL - LZMA_BASE_SIZE;
|
|
|
|
for (i = 0; i < num_probs; i++)
|
|
|
|
p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
|
|
|
|
}
|
2006-01-20 23:58:50 +05:30
|
|
|
|
2014-12-07 05:14:00 +05:30
|
|
|
rc = rc_init(xstate->src_fd); /*, RC_BUFFER_SIZE); */
|
2006-01-20 23:58:50 +05:30
|
|
|
|
|
|
|
while (global_pos + buffer_pos < header.dst_size) {
|
|
|
|
int pos_state = (buffer_pos + global_pos) & pos_state_mask;
|
2009-09-16 03:10:08 +05:30
|
|
|
uint16_t *prob = p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
|
|
|
|
|
|
|
|
if (!rc_is_bit_1(rc, prob)) {
|
|
|
|
static const char next_state[LZMA_NUM_STATES] =
|
|
|
|
{ 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
|
|
|
|
int mi = 1;
|
2006-01-20 23:58:50 +05:30
|
|
|
|
2008-03-03 00:25:49 +05:30
|
|
|
prob = (p + LZMA_LITERAL
|
|
|
|
+ (LZMA_LIT_SIZE * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
|
|
|
|
+ (previous_byte >> (8 - lc))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2006-01-20 23:58:50 +05:30
|
|
|
|
|
|
|
if (state >= LZMA_NUM_LIT_STATES) {
|
|
|
|
int match_byte;
|
2017-01-09 18:25:11 +05:30
|
|
|
uint32_t pos;
|
2006-01-20 23:58:50 +05:30
|
|
|
|
2017-01-09 18:25:11 +05:30
|
|
|
pos = buffer_pos - rep0;
|
|
|
|
if ((int32_t)pos < 0)
|
2006-01-20 23:58:50 +05:30
|
|
|
pos += header.dict_size;
|
|
|
|
match_byte = buffer[pos];
|
|
|
|
do {
|
|
|
|
int bit;
|
|
|
|
|
|
|
|
match_byte <<= 1;
|
|
|
|
bit = match_byte & 0x100;
|
2009-09-16 03:10:08 +05:30
|
|
|
bit ^= (rc_get_bit(rc, prob + 0x100 + bit + mi, &mi) << 8); /* 0x100 or 0 */
|
2008-03-03 00:26:23 +05:30
|
|
|
if (bit)
|
|
|
|
break;
|
2006-01-20 23:58:50 +05:30
|
|
|
} while (mi < 0x100);
|
|
|
|
}
|
|
|
|
while (mi < 0x100) {
|
2009-09-16 03:10:08 +05:30
|
|
|
rc_get_bit(rc, prob + mi, &mi);
|
2006-01-20 23:58:50 +05:30
|
|
|
}
|
|
|
|
|
2009-09-16 03:10:08 +05:30
|
|
|
state = next_state[state];
|
2008-03-03 00:25:49 +05:30
|
|
|
|
|
|
|
previous_byte = (uint8_t) mi;
|
|
|
|
#if ENABLE_FEATURE_LZMA_FAST
|
2008-03-18 08:43:25 +05:30
|
|
|
one_byte1:
|
2006-01-20 23:58:50 +05:30
|
|
|
buffer[buffer_pos++] = previous_byte;
|
|
|
|
if (buffer_pos == header.dict_size) {
|
|
|
|
buffer_pos = 0;
|
|
|
|
global_pos += header.dict_size;
|
2014-12-07 05:14:00 +05:30
|
|
|
if (transformer_write(xstate, buffer, header.dict_size) != (ssize_t)header.dict_size)
|
2007-01-05 19:34:47 +05:30
|
|
|
goto bad;
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_DESKTOP(total_written += header.dict_size;)
|
2006-01-20 23:58:50 +05:30
|
|
|
}
|
2008-03-03 00:25:49 +05:30
|
|
|
#else
|
|
|
|
len = 1;
|
2008-03-18 08:43:25 +05:30
|
|
|
goto one_byte2;
|
2008-03-03 00:25:49 +05:30
|
|
|
#endif
|
2006-01-20 23:58:50 +05:30
|
|
|
} else {
|
2014-02-28 19:35:43 +05:30
|
|
|
int num_bits;
|
2006-01-20 23:58:50 +05:30
|
|
|
int offset;
|
2009-09-16 03:10:08 +05:30
|
|
|
uint16_t *prob2;
|
|
|
|
#define prob_len prob2
|
2006-01-20 23:58:50 +05:30
|
|
|
|
2009-09-16 03:10:08 +05:30
|
|
|
prob2 = p + LZMA_IS_REP + state;
|
|
|
|
if (!rc_is_bit_1(rc, prob2)) {
|
2006-01-20 23:58:50 +05:30
|
|
|
rep3 = rep2;
|
|
|
|
rep2 = rep1;
|
|
|
|
rep1 = rep0;
|
|
|
|
state = state < LZMA_NUM_LIT_STATES ? 0 : 3;
|
2009-09-16 03:10:08 +05:30
|
|
|
prob2 = p + LZMA_LEN_CODER;
|
2006-01-20 23:58:50 +05:30
|
|
|
} else {
|
2009-09-16 03:10:08 +05:30
|
|
|
prob2 += LZMA_IS_REP_G0 - LZMA_IS_REP;
|
|
|
|
if (!rc_is_bit_1(rc, prob2)) {
|
|
|
|
prob2 = (p + LZMA_IS_REP_0_LONG
|
2008-03-03 00:25:49 +05:30
|
|
|
+ (state << LZMA_NUM_POS_BITS_MAX)
|
|
|
|
+ pos_state
|
|
|
|
);
|
2009-09-16 03:10:08 +05:30
|
|
|
if (!rc_is_bit_1(rc, prob2)) {
|
2008-03-03 00:25:49 +05:30
|
|
|
#if ENABLE_FEATURE_LZMA_FAST
|
2017-01-09 18:25:11 +05:30
|
|
|
uint32_t pos;
|
2009-09-16 03:10:08 +05:30
|
|
|
state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
|
2017-01-09 18:25:11 +05:30
|
|
|
|
|
|
|
pos = buffer_pos - rep0;
|
|
|
|
if ((int32_t)pos < 0)
|
2006-01-20 23:58:50 +05:30
|
|
|
pos += header.dict_size;
|
|
|
|
previous_byte = buffer[pos];
|
2008-03-18 08:43:25 +05:30
|
|
|
goto one_byte1;
|
2008-03-03 00:25:49 +05:30
|
|
|
#else
|
2009-09-16 03:10:08 +05:30
|
|
|
state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
|
2008-03-03 00:25:49 +05:30
|
|
|
len = 1;
|
|
|
|
goto string;
|
|
|
|
#endif
|
2006-01-20 23:58:50 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uint32_t distance;
|
|
|
|
|
2009-09-16 03:10:08 +05:30
|
|
|
prob2 += LZMA_IS_REP_G1 - LZMA_IS_REP_G0;
|
|
|
|
distance = rep1;
|
|
|
|
if (rc_is_bit_1(rc, prob2)) {
|
|
|
|
prob2 += LZMA_IS_REP_G2 - LZMA_IS_REP_G1;
|
|
|
|
distance = rep2;
|
|
|
|
if (rc_is_bit_1(rc, prob2)) {
|
2006-01-20 23:58:50 +05:30
|
|
|
distance = rep3;
|
|
|
|
rep3 = rep2;
|
|
|
|
}
|
|
|
|
rep2 = rep1;
|
|
|
|
}
|
|
|
|
rep1 = rep0;
|
|
|
|
rep0 = distance;
|
|
|
|
}
|
|
|
|
state = state < LZMA_NUM_LIT_STATES ? 8 : 11;
|
2009-09-16 03:10:08 +05:30
|
|
|
prob2 = p + LZMA_REP_LEN_CODER;
|
2006-01-20 23:58:50 +05:30
|
|
|
}
|
|
|
|
|
2009-09-16 03:10:08 +05:30
|
|
|
prob_len = prob2 + LZMA_LEN_CHOICE;
|
|
|
|
num_bits = LZMA_LEN_NUM_LOW_BITS;
|
|
|
|
if (!rc_is_bit_1(rc, prob_len)) {
|
|
|
|
prob_len += LZMA_LEN_LOW - LZMA_LEN_CHOICE
|
|
|
|
+ (pos_state << LZMA_LEN_NUM_LOW_BITS);
|
2006-01-20 23:58:50 +05:30
|
|
|
offset = 0;
|
|
|
|
} else {
|
2009-09-16 03:10:08 +05:30
|
|
|
prob_len += LZMA_LEN_CHOICE_2 - LZMA_LEN_CHOICE;
|
|
|
|
if (!rc_is_bit_1(rc, prob_len)) {
|
|
|
|
prob_len += LZMA_LEN_MID - LZMA_LEN_CHOICE_2
|
|
|
|
+ (pos_state << LZMA_LEN_NUM_MID_BITS);
|
2006-01-20 23:58:50 +05:30
|
|
|
offset = 1 << LZMA_LEN_NUM_LOW_BITS;
|
2009-09-16 03:10:08 +05:30
|
|
|
num_bits += LZMA_LEN_NUM_MID_BITS - LZMA_LEN_NUM_LOW_BITS;
|
2006-01-20 23:58:50 +05:30
|
|
|
} else {
|
2009-09-16 03:10:08 +05:30
|
|
|
prob_len += LZMA_LEN_HIGH - LZMA_LEN_CHOICE_2;
|
2006-01-20 23:58:50 +05:30
|
|
|
offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
|
2008-03-03 00:25:49 +05:30
|
|
|
+ (1 << LZMA_LEN_NUM_MID_BITS));
|
2009-09-16 03:10:08 +05:30
|
|
|
num_bits += LZMA_LEN_NUM_HIGH_BITS - LZMA_LEN_NUM_LOW_BITS;
|
2006-01-20 23:58:50 +05:30
|
|
|
}
|
|
|
|
}
|
2007-01-05 19:29:05 +05:30
|
|
|
rc_bit_tree_decode(rc, prob_len, num_bits, &len);
|
2006-01-20 23:58:50 +05:30
|
|
|
len += offset;
|
|
|
|
|
|
|
|
if (state < 4) {
|
|
|
|
int pos_slot;
|
2009-09-16 03:10:08 +05:30
|
|
|
uint16_t *prob3;
|
2006-01-20 23:58:50 +05:30
|
|
|
|
|
|
|
state += LZMA_NUM_LIT_STATES;
|
2009-09-16 03:10:08 +05:30
|
|
|
prob3 = p + LZMA_POS_SLOT +
|
2008-03-03 00:25:49 +05:30
|
|
|
((len < LZMA_NUM_LEN_TO_POS_STATES ? len :
|
|
|
|
LZMA_NUM_LEN_TO_POS_STATES - 1)
|
|
|
|
<< LZMA_NUM_POS_SLOT_BITS);
|
2009-09-16 03:10:08 +05:30
|
|
|
rc_bit_tree_decode(rc, prob3,
|
|
|
|
LZMA_NUM_POS_SLOT_BITS, &pos_slot);
|
|
|
|
rep0 = pos_slot;
|
2006-01-20 23:58:50 +05:30
|
|
|
if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
|
2009-09-16 03:10:08 +05:30
|
|
|
int i2, mi2, num_bits2 = (pos_slot >> 1) - 1;
|
2006-01-20 23:58:50 +05:30
|
|
|
rep0 = 2 | (pos_slot & 1);
|
|
|
|
if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
|
2009-09-16 03:10:08 +05:30
|
|
|
rep0 <<= num_bits2;
|
|
|
|
prob3 = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
|
2006-01-20 23:58:50 +05:30
|
|
|
} else {
|
2009-09-16 03:10:08 +05:30
|
|
|
for (; num_bits2 != LZMA_NUM_ALIGN_BITS; num_bits2--)
|
2007-01-05 19:29:05 +05:30
|
|
|
rep0 = (rep0 << 1) | rc_direct_bit(rc);
|
2006-01-20 23:58:50 +05:30
|
|
|
rep0 <<= LZMA_NUM_ALIGN_BITS;
|
2018-04-09 00:15:16 +05:30
|
|
|
if ((int32_t)rep0 < 0) {
|
|
|
|
dbg("%d rep0:%d", __LINE__, rep0);
|
|
|
|
goto bad;
|
|
|
|
}
|
2009-09-16 03:10:08 +05:30
|
|
|
prob3 = p + LZMA_ALIGN;
|
2006-01-20 23:58:50 +05:30
|
|
|
}
|
2009-09-16 03:10:08 +05:30
|
|
|
i2 = 1;
|
|
|
|
mi2 = 1;
|
|
|
|
while (num_bits2--) {
|
|
|
|
if (rc_get_bit(rc, prob3 + mi2, &mi2))
|
|
|
|
rep0 |= i2;
|
|
|
|
i2 <<= 1;
|
2006-01-20 23:58:50 +05:30
|
|
|
}
|
2009-09-16 03:10:08 +05:30
|
|
|
}
|
2006-01-20 23:58:50 +05:30
|
|
|
if (++rep0 == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
len += LZMA_MATCH_MIN_LEN;
|
2017-01-09 18:58:25 +05:30
|
|
|
/*
|
|
|
|
* LZMA SDK has this optimized:
|
|
|
|
* it precalculates size and copies many bytes
|
|
|
|
* in a loop with simpler checks, a-la:
|
|
|
|
* do
|
|
|
|
* *(dest) = *(dest + ofs);
|
|
|
|
* while (++dest != lim);
|
|
|
|
* and
|
|
|
|
* do {
|
|
|
|
* buffer[buffer_pos++] = buffer[pos];
|
|
|
|
* if (++pos == header.dict_size)
|
|
|
|
* pos = 0;
|
|
|
|
* } while (--cur_len != 0);
|
|
|
|
* Our code is slower (more checks per byte copy):
|
|
|
|
*/
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_NOT_FEATURE_LZMA_FAST(string:)
|
2006-01-20 23:58:50 +05:30
|
|
|
do {
|
2009-09-16 03:10:08 +05:30
|
|
|
uint32_t pos = buffer_pos - rep0;
|
2017-10-27 19:07:03 +05:30
|
|
|
if ((int32_t)pos < 0) {
|
2006-01-20 23:58:50 +05:30
|
|
|
pos += header.dict_size;
|
2017-10-27 19:07:03 +05:30
|
|
|
/* bug 10436 has an example file where this triggers: */
|
2018-04-19 22:59:49 +05:30
|
|
|
//if ((int32_t)pos < 0)
|
|
|
|
// goto bad;
|
|
|
|
/* more stringent test (see unzip_bad_lzma_1.zip): */
|
|
|
|
if (pos >= buffer_size)
|
2017-10-27 19:07:03 +05:30
|
|
|
goto bad;
|
|
|
|
}
|
2006-01-20 23:58:50 +05:30
|
|
|
previous_byte = buffer[pos];
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_NOT_FEATURE_LZMA_FAST(one_byte2:)
|
2006-01-20 23:58:50 +05:30
|
|
|
buffer[buffer_pos++] = previous_byte;
|
|
|
|
if (buffer_pos == header.dict_size) {
|
|
|
|
buffer_pos = 0;
|
|
|
|
global_pos += header.dict_size;
|
2014-12-07 05:14:00 +05:30
|
|
|
if (transformer_write(xstate, buffer, header.dict_size) != (ssize_t)header.dict_size)
|
2007-01-05 19:34:47 +05:30
|
|
|
goto bad;
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_DESKTOP(total_written += header.dict_size;)
|
2006-01-20 23:58:50 +05:30
|
|
|
}
|
|
|
|
len--;
|
|
|
|
} while (len != 0 && buffer_pos < header.dst_size);
|
2014-02-28 20:12:10 +05:30
|
|
|
/* FIXME: ...........^^^^^
|
|
|
|
* shouldn't it be "global_pos + buffer_pos < header.dst_size"?
|
2017-01-09 18:58:25 +05:30
|
|
|
* It probably should, but it is a "do we accidentally
|
|
|
|
* unpack more bytes than expected?" check - which
|
|
|
|
* never happens for well-formed compression data...
|
2014-02-28 20:12:10 +05:30
|
|
|
*/
|
2006-01-20 23:58:50 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-27 21:18:45 +05:30
|
|
|
{
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_NOT_DESKTOP(int total_written = 0; /* success */)
|
|
|
|
IF_DESKTOP(total_written += buffer_pos;)
|
2014-12-07 05:14:00 +05:30
|
|
|
if (transformer_write(xstate, buffer, buffer_pos) != (ssize_t)buffer_pos) {
|
2007-01-05 19:34:47 +05:30
|
|
|
bad:
|
2018-04-24 17:19:12 +05:30
|
|
|
/* One of our users, bbunpack(), expects _us_ to emit
|
|
|
|
* the error message (since it's the best place to give
|
|
|
|
* potentially more detailed information).
|
|
|
|
* Do not fail silently.
|
|
|
|
*/
|
|
|
|
bb_error_msg("corrupted data");
|
2008-06-27 21:18:45 +05:30
|
|
|
total_written = -1; /* failure */
|
|
|
|
}
|
2007-01-05 19:34:47 +05:30
|
|
|
rc_free(rc);
|
2008-06-27 21:18:45 +05:30
|
|
|
free(p);
|
|
|
|
free(buffer);
|
|
|
|
return total_written;
|
2007-01-05 19:34:47 +05:30
|
|
|
}
|
2006-01-20 23:58:50 +05:30
|
|
|
}
|