blob: dd08e77f35dc45e9cbebf23b716511e83849d9d4 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Rob Landleyc1d69902006-01-20 18:28:50 +00002/*
3 * Small lzma deflate implementation.
4 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
5 *
6 * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
7 * Copyright (C) 1999-2005 Igor Pavlov
8 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Rob Landleyc1d69902006-01-20 18:28:50 +000010 */
Rob Landleyc1d69902006-01-20 18:28:50 +000011#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +020012#include "bb_archive.h"
Bernhard Reutner-Fischercfb53df2006-04-02 21:50:01 +000013
Denis Vlasenkoe3241842007-08-13 10:36:25 +000014#if ENABLE_FEATURE_LZMA_FAST
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +000015# define speed_inline ALWAYS_INLINE
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020016# define size_inline
Rob Landleyb13fee42006-06-20 22:38:00 +000017#else
18# define speed_inline
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020019# define size_inline ALWAYS_INLINE
Rob Landleyb13fee42006-06-20 22:38:00 +000020#endif
21
22
23typedef struct {
24 int fd;
25 uint8_t *ptr;
Denis Vlasenko33762982007-01-05 14:04:47 +000026
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))
31
Rob Landleyb13fee42006-06-20 22:38:00 +000032 uint8_t *buffer_end;
Denis Vlasenko33762982007-01-05 14:04:47 +000033
34/* Had provisions for variable buffer, but we don't need it here */
35 /* int buffer_size; */
36#define RC_BUFFER_SIZE 0x10000
37
Rob Landleyb13fee42006-06-20 22:38:00 +000038 uint32_t code;
39 uint32_t range;
40 uint32_t bound;
41} rc_t;
42
Rob Landleyb13fee42006-06-20 22:38:00 +000043#define RC_TOP_BITS 24
44#define RC_MOVE_BITS 5
45#define RC_MODEL_TOTAL_BITS 11
46
47
Denys Vlasenkoc09fd272013-03-01 14:37:58 +010048/* Called once in rc_do_normalize() */
49static void rc_read(rc_t *rc)
Rob Landleyb13fee42006-06-20 22:38:00 +000050{
Denis Vlasenko33762982007-01-05 14:04:47 +000051 int buffer_size = safe_read(rc->fd, RC_BUFFER, RC_BUFFER_SIZE);
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020052//TODO: return -1 instead
53//This will make unlzma delete broken unpacked file on unpack errors
Denis Vlasenko33762982007-01-05 14:04:47 +000054 if (buffer_size <= 0)
Rob Landleyb13fee42006-06-20 22:38:00 +000055 bb_error_msg_and_die("unexpected EOF");
Denis Vlasenko33762982007-01-05 14:04:47 +000056 rc->buffer_end = RC_BUFFER + buffer_size;
Denys Vlasenkoc09fd272013-03-01 14:37:58 +010057 rc->ptr = RC_BUFFER;
Rob Landleyb13fee42006-06-20 22:38:00 +000058}
59
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020060/* Called twice, but one callsite is in speed_inline'd rc_is_bit_1() */
61static void rc_do_normalize(rc_t *rc)
62{
63 if (rc->ptr >= rc->buffer_end)
64 rc_read(rc);
65 rc->range <<= 8;
66 rc->code = (rc->code << 8) | *rc->ptr++;
67}
68
Rob Landleyb13fee42006-06-20 22:38:00 +000069/* Called once */
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020070static ALWAYS_INLINE rc_t* rc_init(int fd) /*, int buffer_size) */
Rob Landleyb13fee42006-06-20 22:38:00 +000071{
72 int i;
Denis Vlasenko2bbdda02008-06-27 15:48:45 +000073 rc_t *rc;
Denis Vlasenko98b8e942007-01-05 13:59:05 +000074
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020075 rc = xzalloc(sizeof(*rc) + RC_BUFFER_SIZE);
Rob Landleyb13fee42006-06-20 22:38:00 +000076
77 rc->fd = fd;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020078 /* rc->ptr = rc->buffer_end; */
Rob Landleyb13fee42006-06-20 22:38:00 +000079
Rob Landleyb13fee42006-06-20 22:38:00 +000080 for (i = 0; i < 5; i++) {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020081 rc_do_normalize(rc);
Rob Landleyb13fee42006-06-20 22:38:00 +000082 }
Denys Vlasenkoc09fd272013-03-01 14:37:58 +010083 rc->range = 0xffffffff;
Denis Vlasenko98b8e942007-01-05 13:59:05 +000084 return rc;
Rob Landleyb13fee42006-06-20 22:38:00 +000085}
86
Denis Vlasenko98b8e942007-01-05 13:59:05 +000087/* Called once */
Denis Vlasenko2bbdda02008-06-27 15:48:45 +000088static ALWAYS_INLINE void rc_free(rc_t *rc)
Rob Landleyb13fee42006-06-20 22:38:00 +000089{
Denis Vlasenko2bbdda02008-06-27 15:48:45 +000090 free(rc);
Rob Landleyb13fee42006-06-20 22:38:00 +000091}
92
Denis Vlasenko2bbdda02008-06-27 15:48:45 +000093static ALWAYS_INLINE void rc_normalize(rc_t *rc)
Rob Landleyb13fee42006-06-20 22:38:00 +000094{
95 if (rc->range < (1 << RC_TOP_BITS)) {
96 rc_do_normalize(rc);
97 }
98}
99
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200100/* rc_is_bit_1 is called 9 times */
101static speed_inline int rc_is_bit_1(rc_t *rc, uint16_t *p)
Rob Landleyb13fee42006-06-20 22:38:00 +0000102{
103 rc_normalize(rc);
104 rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200105 if (rc->code < rc->bound) {
106 rc->range = rc->bound;
107 *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
108 return 0;
109 }
Rob Landleyb13fee42006-06-20 22:38:00 +0000110 rc->range -= rc->bound;
111 rc->code -= rc->bound;
112 *p -= *p >> RC_MOVE_BITS;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200113 return 1;
Rob Landleyb13fee42006-06-20 22:38:00 +0000114}
115
116/* Called 4 times in unlzma loop */
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200117static speed_inline int rc_get_bit(rc_t *rc, uint16_t *p, int *symbol)
Rob Landleyb13fee42006-06-20 22:38:00 +0000118{
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200119 int ret = rc_is_bit_1(rc, p);
120 *symbol = *symbol * 2 + ret;
121 return ret;
Rob Landleyb13fee42006-06-20 22:38:00 +0000122}
123
124/* Called once */
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000125static ALWAYS_INLINE int rc_direct_bit(rc_t *rc)
Rob Landleyb13fee42006-06-20 22:38:00 +0000126{
127 rc_normalize(rc);
128 rc->range >>= 1;
129 if (rc->code >= rc->range) {
130 rc->code -= rc->range;
131 return 1;
132 }
133 return 0;
134}
135
136/* Called twice */
137static speed_inline void
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000138rc_bit_tree_decode(rc_t *rc, uint16_t *p, int num_levels, int *symbol)
Rob Landleyb13fee42006-06-20 22:38:00 +0000139{
140 int i = num_levels;
141
142 *symbol = 1;
143 while (i--)
144 rc_get_bit(rc, p + *symbol, symbol);
145 *symbol -= 1 << num_levels;
146}
Rob Landleyc1d69902006-01-20 18:28:50 +0000147
148
149typedef struct {
150 uint8_t pos;
151 uint32_t dict_size;
152 uint64_t dst_size;
Denys Vlasenko8dc0e192009-09-16 00:58:11 +0200153} PACKED lzma_header_t;
Rob Landleyc1d69902006-01-20 18:28:50 +0000154
155
Denis Vlasenko33762982007-01-05 14:04:47 +0000156/* #defines will force compiler to compute/optimize each one with each usage.
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000157 * Have heart and use enum instead. */
158enum {
159 LZMA_BASE_SIZE = 1846,
160 LZMA_LIT_SIZE = 768,
Rob Landleyc1d69902006-01-20 18:28:50 +0000161
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000162 LZMA_NUM_POS_BITS_MAX = 4,
Rob Landleyc1d69902006-01-20 18:28:50 +0000163
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000164 LZMA_LEN_NUM_LOW_BITS = 3,
165 LZMA_LEN_NUM_MID_BITS = 3,
166 LZMA_LEN_NUM_HIGH_BITS = 8,
Rob Landleyc1d69902006-01-20 18:28:50 +0000167
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000168 LZMA_LEN_CHOICE = 0,
169 LZMA_LEN_CHOICE_2 = (LZMA_LEN_CHOICE + 1),
170 LZMA_LEN_LOW = (LZMA_LEN_CHOICE_2 + 1),
171 LZMA_LEN_MID = (LZMA_LEN_LOW \
172 + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS))),
173 LZMA_LEN_HIGH = (LZMA_LEN_MID \
174 + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS))),
175 LZMA_NUM_LEN_PROBS = (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS)),
Rob Landleyc1d69902006-01-20 18:28:50 +0000176
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000177 LZMA_NUM_STATES = 12,
178 LZMA_NUM_LIT_STATES = 7,
Rob Landleyc1d69902006-01-20 18:28:50 +0000179
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000180 LZMA_START_POS_MODEL_INDEX = 4,
181 LZMA_END_POS_MODEL_INDEX = 14,
182 LZMA_NUM_FULL_DISTANCES = (1 << (LZMA_END_POS_MODEL_INDEX >> 1)),
Rob Landleyc1d69902006-01-20 18:28:50 +0000183
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000184 LZMA_NUM_POS_SLOT_BITS = 6,
185 LZMA_NUM_LEN_TO_POS_STATES = 4,
Rob Landleyc1d69902006-01-20 18:28:50 +0000186
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000187 LZMA_NUM_ALIGN_BITS = 4,
Rob Landleyc1d69902006-01-20 18:28:50 +0000188
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000189 LZMA_MATCH_MIN_LEN = 2,
Rob Landleyc1d69902006-01-20 18:28:50 +0000190
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000191 LZMA_IS_MATCH = 0,
192 LZMA_IS_REP = (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)),
193 LZMA_IS_REP_G0 = (LZMA_IS_REP + LZMA_NUM_STATES),
194 LZMA_IS_REP_G1 = (LZMA_IS_REP_G0 + LZMA_NUM_STATES),
195 LZMA_IS_REP_G2 = (LZMA_IS_REP_G1 + LZMA_NUM_STATES),
196 LZMA_IS_REP_0_LONG = (LZMA_IS_REP_G2 + LZMA_NUM_STATES),
197 LZMA_POS_SLOT = (LZMA_IS_REP_0_LONG \
198 + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)),
199 LZMA_SPEC_POS = (LZMA_POS_SLOT \
200 + (LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS)),
201 LZMA_ALIGN = (LZMA_SPEC_POS \
202 + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX),
203 LZMA_LEN_CODER = (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS)),
204 LZMA_REP_LEN_CODER = (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS),
205 LZMA_LITERAL = (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS),
206};
207
Rob Landleyc1d69902006-01-20 18:28:50 +0000208
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000209IF_DESKTOP(long long) int FAST_FUNC
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100210unpack_lzma_stream(transformer_aux_data_t *aux UNUSED_PARAM, int src_fd, int dst_fd)
Rob Landleyc1d69902006-01-20 18:28:50 +0000211{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000212 IF_DESKTOP(long long total_written = 0;)
Rob Landleyc1d69902006-01-20 18:28:50 +0000213 lzma_header_t header;
214 int lc, pb, lp;
215 uint32_t pos_state_mask;
216 uint32_t literal_pos_mask;
Rob Landleyc1d69902006-01-20 18:28:50 +0000217 uint16_t *p;
Rob Landleyc1d69902006-01-20 18:28:50 +0000218 int num_bits;
219 int num_probs;
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000220 rc_t *rc;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200221 int i;
Rob Landleyc1d69902006-01-20 18:28:50 +0000222 uint8_t *buffer;
223 uint8_t previous_byte = 0;
224 size_t buffer_pos = 0, global_pos = 0;
225 int len = 0;
226 int state = 0;
227 uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
228
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200229 if (full_read(src_fd, &header, sizeof(header)) != sizeof(header)
230 || header.pos >= (9 * 5 * 5)
231 ) {
232 bb_error_msg("bad lzma header");
233 return -1;
234 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000235
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200236 i = header.pos / 9;
Rob Landleyc1d69902006-01-20 18:28:50 +0000237 lc = header.pos % 9;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200238 pb = i / 5;
239 lp = i % 5;
Rob Landleyc1d69902006-01-20 18:28:50 +0000240 pos_state_mask = (1 << pb) - 1;
241 literal_pos_mask = (1 << lp) - 1;
242
Rob Landleybba7f082006-05-29 05:51:12 +0000243 header.dict_size = SWAP_LE32(header.dict_size);
244 header.dst_size = SWAP_LE64(header.dst_size);
Rob Landleyc1d69902006-01-20 18:28:50 +0000245
246 if (header.dict_size == 0)
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200247 header.dict_size++;
Rob Landleyc1d69902006-01-20 18:28:50 +0000248
249 buffer = xmalloc(MIN(header.dst_size, header.dict_size));
250
251 num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
252 p = xmalloc(num_probs * sizeof(*p));
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200253 num_probs += LZMA_LITERAL - LZMA_BASE_SIZE;
Rob Landleyc1d69902006-01-20 18:28:50 +0000254 for (i = 0; i < num_probs; i++)
255 p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
256
Denis Vlasenko33762982007-01-05 14:04:47 +0000257 rc = rc_init(src_fd); /*, RC_BUFFER_SIZE); */
Rob Landleyc1d69902006-01-20 18:28:50 +0000258
259 while (global_pos + buffer_pos < header.dst_size) {
260 int pos_state = (buffer_pos + global_pos) & pos_state_mask;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200261 uint16_t *prob = p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
Rob Landleyc1d69902006-01-20 18:28:50 +0000262
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200263 if (!rc_is_bit_1(rc, prob)) {
264 static const char next_state[LZMA_NUM_STATES] =
265 { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
266 int mi = 1;
267
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000268 prob = (p + LZMA_LITERAL
269 + (LZMA_LIT_SIZE * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
270 + (previous_byte >> (8 - lc))
271 )
272 )
273 );
Rob Landleyc1d69902006-01-20 18:28:50 +0000274
275 if (state >= LZMA_NUM_LIT_STATES) {
276 int match_byte;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200277 uint32_t pos = buffer_pos - rep0;
Rob Landleyc1d69902006-01-20 18:28:50 +0000278
Rob Landleyc1d69902006-01-20 18:28:50 +0000279 while (pos >= header.dict_size)
280 pos += header.dict_size;
281 match_byte = buffer[pos];
282 do {
283 int bit;
284
285 match_byte <<= 1;
286 bit = match_byte & 0x100;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200287 bit ^= (rc_get_bit(rc, prob + 0x100 + bit + mi, &mi) << 8); /* 0x100 or 0 */
Denis Vlasenko444639c2008-03-02 18:56:23 +0000288 if (bit)
289 break;
Rob Landleyc1d69902006-01-20 18:28:50 +0000290 } while (mi < 0x100);
291 }
292 while (mi < 0x100) {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200293 rc_get_bit(rc, prob + mi, &mi);
Rob Landleyc1d69902006-01-20 18:28:50 +0000294 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000295
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200296 state = next_state[state];
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000297
298 previous_byte = (uint8_t) mi;
299#if ENABLE_FEATURE_LZMA_FAST
Denis Vlasenkoc0183e62008-03-18 03:13:25 +0000300 one_byte1:
Rob Landleyc1d69902006-01-20 18:28:50 +0000301 buffer[buffer_pos++] = previous_byte;
302 if (buffer_pos == header.dict_size) {
303 buffer_pos = 0;
304 global_pos += header.dict_size;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000305 if (full_write(dst_fd, buffer, header.dict_size) != (ssize_t)header.dict_size)
Denis Vlasenko33762982007-01-05 14:04:47 +0000306 goto bad;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000307 IF_DESKTOP(total_written += header.dict_size;)
Rob Landleyc1d69902006-01-20 18:28:50 +0000308 }
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000309#else
310 len = 1;
Denis Vlasenkoc0183e62008-03-18 03:13:25 +0000311 goto one_byte2;
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000312#endif
Rob Landleyc1d69902006-01-20 18:28:50 +0000313 } else {
314 int offset;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200315 uint16_t *prob2;
316#define prob_len prob2
Rob Landleyc1d69902006-01-20 18:28:50 +0000317
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200318 prob2 = p + LZMA_IS_REP + state;
319 if (!rc_is_bit_1(rc, prob2)) {
Rob Landleyc1d69902006-01-20 18:28:50 +0000320 rep3 = rep2;
321 rep2 = rep1;
322 rep1 = rep0;
323 state = state < LZMA_NUM_LIT_STATES ? 0 : 3;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200324 prob2 = p + LZMA_LEN_CODER;
Rob Landleyc1d69902006-01-20 18:28:50 +0000325 } else {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200326 prob2 += LZMA_IS_REP_G0 - LZMA_IS_REP;
327 if (!rc_is_bit_1(rc, prob2)) {
328 prob2 = (p + LZMA_IS_REP_0_LONG
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000329 + (state << LZMA_NUM_POS_BITS_MAX)
330 + pos_state
331 );
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200332 if (!rc_is_bit_1(rc, prob2)) {
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000333#if ENABLE_FEATURE_LZMA_FAST
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200334 uint32_t pos = buffer_pos - rep0;
335 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
Rob Landleyc1d69902006-01-20 18:28:50 +0000336 while (pos >= header.dict_size)
337 pos += header.dict_size;
338 previous_byte = buffer[pos];
Denis Vlasenkoc0183e62008-03-18 03:13:25 +0000339 goto one_byte1;
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000340#else
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200341 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000342 len = 1;
343 goto string;
344#endif
Rob Landleyc1d69902006-01-20 18:28:50 +0000345 }
346 } else {
347 uint32_t distance;
348
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200349 prob2 += LZMA_IS_REP_G1 - LZMA_IS_REP_G0;
350 distance = rep1;
351 if (rc_is_bit_1(rc, prob2)) {
352 prob2 += LZMA_IS_REP_G2 - LZMA_IS_REP_G1;
353 distance = rep2;
354 if (rc_is_bit_1(rc, prob2)) {
Rob Landleyc1d69902006-01-20 18:28:50 +0000355 distance = rep3;
356 rep3 = rep2;
357 }
358 rep2 = rep1;
359 }
360 rep1 = rep0;
361 rep0 = distance;
362 }
363 state = state < LZMA_NUM_LIT_STATES ? 8 : 11;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200364 prob2 = p + LZMA_REP_LEN_CODER;
Rob Landleyc1d69902006-01-20 18:28:50 +0000365 }
366
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200367 prob_len = prob2 + LZMA_LEN_CHOICE;
368 num_bits = LZMA_LEN_NUM_LOW_BITS;
369 if (!rc_is_bit_1(rc, prob_len)) {
370 prob_len += LZMA_LEN_LOW - LZMA_LEN_CHOICE
371 + (pos_state << LZMA_LEN_NUM_LOW_BITS);
Rob Landleyc1d69902006-01-20 18:28:50 +0000372 offset = 0;
Rob Landleyc1d69902006-01-20 18:28:50 +0000373 } else {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200374 prob_len += LZMA_LEN_CHOICE_2 - LZMA_LEN_CHOICE;
375 if (!rc_is_bit_1(rc, prob_len)) {
376 prob_len += LZMA_LEN_MID - LZMA_LEN_CHOICE_2
377 + (pos_state << LZMA_LEN_NUM_MID_BITS);
Rob Landleyc1d69902006-01-20 18:28:50 +0000378 offset = 1 << LZMA_LEN_NUM_LOW_BITS;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200379 num_bits += LZMA_LEN_NUM_MID_BITS - LZMA_LEN_NUM_LOW_BITS;
Rob Landleyc1d69902006-01-20 18:28:50 +0000380 } else {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200381 prob_len += LZMA_LEN_HIGH - LZMA_LEN_CHOICE_2;
Rob Landleyc1d69902006-01-20 18:28:50 +0000382 offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000383 + (1 << LZMA_LEN_NUM_MID_BITS));
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200384 num_bits += LZMA_LEN_NUM_HIGH_BITS - LZMA_LEN_NUM_LOW_BITS;
Rob Landleyc1d69902006-01-20 18:28:50 +0000385 }
386 }
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000387 rc_bit_tree_decode(rc, prob_len, num_bits, &len);
Rob Landleyc1d69902006-01-20 18:28:50 +0000388 len += offset;
389
390 if (state < 4) {
391 int pos_slot;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200392 uint16_t *prob3;
Rob Landleyc1d69902006-01-20 18:28:50 +0000393
394 state += LZMA_NUM_LIT_STATES;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200395 prob3 = p + LZMA_POS_SLOT +
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000396 ((len < LZMA_NUM_LEN_TO_POS_STATES ? len :
397 LZMA_NUM_LEN_TO_POS_STATES - 1)
398 << LZMA_NUM_POS_SLOT_BITS);
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200399 rc_bit_tree_decode(rc, prob3,
400 LZMA_NUM_POS_SLOT_BITS, &pos_slot);
401 rep0 = pos_slot;
Rob Landleyc1d69902006-01-20 18:28:50 +0000402 if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200403 int i2, mi2, num_bits2 = (pos_slot >> 1) - 1;
Rob Landleyc1d69902006-01-20 18:28:50 +0000404 rep0 = 2 | (pos_slot & 1);
405 if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200406 rep0 <<= num_bits2;
407 prob3 = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
Rob Landleyc1d69902006-01-20 18:28:50 +0000408 } else {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200409 for (; num_bits2 != LZMA_NUM_ALIGN_BITS; num_bits2--)
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000410 rep0 = (rep0 << 1) | rc_direct_bit(rc);
Rob Landleyc1d69902006-01-20 18:28:50 +0000411 rep0 <<= LZMA_NUM_ALIGN_BITS;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200412 prob3 = p + LZMA_ALIGN;
Rob Landleyc1d69902006-01-20 18:28:50 +0000413 }
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200414 i2 = 1;
415 mi2 = 1;
416 while (num_bits2--) {
417 if (rc_get_bit(rc, prob3 + mi2, &mi2))
418 rep0 |= i2;
419 i2 <<= 1;
Rob Landleyc1d69902006-01-20 18:28:50 +0000420 }
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200421 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000422 if (++rep0 == 0)
423 break;
424 }
425
426 len += LZMA_MATCH_MIN_LEN;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000427 IF_NOT_FEATURE_LZMA_FAST(string:)
Rob Landleyc1d69902006-01-20 18:28:50 +0000428 do {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200429 uint32_t pos = buffer_pos - rep0;
Rob Landleyc1d69902006-01-20 18:28:50 +0000430 while (pos >= header.dict_size)
431 pos += header.dict_size;
432 previous_byte = buffer[pos];
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000433 IF_NOT_FEATURE_LZMA_FAST(one_byte2:)
Rob Landleyc1d69902006-01-20 18:28:50 +0000434 buffer[buffer_pos++] = previous_byte;
435 if (buffer_pos == header.dict_size) {
436 buffer_pos = 0;
437 global_pos += header.dict_size;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000438 if (full_write(dst_fd, buffer, header.dict_size) != (ssize_t)header.dict_size)
Denis Vlasenko33762982007-01-05 14:04:47 +0000439 goto bad;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000440 IF_DESKTOP(total_written += header.dict_size;)
Rob Landleyc1d69902006-01-20 18:28:50 +0000441 }
442 len--;
443 } while (len != 0 && buffer_pos < header.dst_size);
444 }
445 }
446
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000447 {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000448 IF_NOT_DESKTOP(int total_written = 0; /* success */)
449 IF_DESKTOP(total_written += buffer_pos;)
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000450 if (full_write(dst_fd, buffer, buffer_pos) != (ssize_t)buffer_pos) {
Denis Vlasenko33762982007-01-05 14:04:47 +0000451 bad:
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000452 total_written = -1; /* failure */
453 }
Denis Vlasenko33762982007-01-05 14:04:47 +0000454 rc_free(rc);
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000455 free(p);
456 free(buffer);
457 return total_written;
Denis Vlasenko33762982007-01-05 14:04:47 +0000458 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000459}