blob: ecda17481dd95916b40282470e3e6faf061991dd [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 *
Rob Landleyb13fee42006-06-20 22:38:00 +00009 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Rob Landleyc1d69902006-01-20 18:28:50 +000010 */
Rob Landleyc1d69902006-01-20 18:28:50 +000011#include "libbb.h"
Bernhard Reutner-Fischercfb53df2006-04-02 21:50:01 +000012#include "unarchive.h"
13
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 Vlasenkof2c184b2009-09-15 23:40:08 +020048/* Called twice: once at startup (LZMA_FAST only) and once in rc_normalize() */
49static size_inline 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 Vlasenko98b8e942007-01-05 13:59:05 +000056 rc->ptr = RC_BUFFER;
Denis Vlasenko33762982007-01-05 14:04:47 +000057 rc->buffer_end = RC_BUFFER + buffer_size;
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#if ENABLE_FEATURE_LZMA_FAST
Rob Landleyb13fee42006-06-20 22:38:00 +000082 if (rc->ptr >= rc->buffer_end)
83 rc_read(rc);
84 rc->code = (rc->code << 8) | *rc->ptr++;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020085#else
86 rc_do_normalize(rc);
87#endif
Rob Landleyb13fee42006-06-20 22:38:00 +000088 }
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020089 rc->range = 0xFFFFFFFF;
Denis Vlasenko98b8e942007-01-05 13:59:05 +000090 return rc;
Rob Landleyb13fee42006-06-20 22:38:00 +000091}
92
Denis Vlasenko98b8e942007-01-05 13:59:05 +000093/* Called once */
Denis Vlasenko2bbdda02008-06-27 15:48:45 +000094static ALWAYS_INLINE void rc_free(rc_t *rc)
Rob Landleyb13fee42006-06-20 22:38:00 +000095{
Denis Vlasenko2bbdda02008-06-27 15:48:45 +000096 free(rc);
Rob Landleyb13fee42006-06-20 22:38:00 +000097}
98
Denis Vlasenko2bbdda02008-06-27 15:48:45 +000099static ALWAYS_INLINE void rc_normalize(rc_t *rc)
Rob Landleyb13fee42006-06-20 22:38:00 +0000100{
101 if (rc->range < (1 << RC_TOP_BITS)) {
102 rc_do_normalize(rc);
103 }
104}
105
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200106/* rc_is_bit_1 is called 9 times */
107static speed_inline int rc_is_bit_1(rc_t *rc, uint16_t *p)
Rob Landleyb13fee42006-06-20 22:38:00 +0000108{
109 rc_normalize(rc);
110 rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200111 if (rc->code < rc->bound) {
112 rc->range = rc->bound;
113 *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
114 return 0;
115 }
Rob Landleyb13fee42006-06-20 22:38:00 +0000116 rc->range -= rc->bound;
117 rc->code -= rc->bound;
118 *p -= *p >> RC_MOVE_BITS;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200119 return 1;
Rob Landleyb13fee42006-06-20 22:38:00 +0000120}
121
122/* Called 4 times in unlzma loop */
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200123static speed_inline int rc_get_bit(rc_t *rc, uint16_t *p, int *symbol)
Rob Landleyb13fee42006-06-20 22:38:00 +0000124{
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200125 int ret = rc_is_bit_1(rc, p);
126 *symbol = *symbol * 2 + ret;
127 return ret;
Rob Landleyb13fee42006-06-20 22:38:00 +0000128}
129
130/* Called once */
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000131static ALWAYS_INLINE int rc_direct_bit(rc_t *rc)
Rob Landleyb13fee42006-06-20 22:38:00 +0000132{
133 rc_normalize(rc);
134 rc->range >>= 1;
135 if (rc->code >= rc->range) {
136 rc->code -= rc->range;
137 return 1;
138 }
139 return 0;
140}
141
142/* Called twice */
143static speed_inline void
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000144rc_bit_tree_decode(rc_t *rc, uint16_t *p, int num_levels, int *symbol)
Rob Landleyb13fee42006-06-20 22:38:00 +0000145{
146 int i = num_levels;
147
148 *symbol = 1;
149 while (i--)
150 rc_get_bit(rc, p + *symbol, symbol);
151 *symbol -= 1 << num_levels;
152}
Rob Landleyc1d69902006-01-20 18:28:50 +0000153
154
155typedef struct {
156 uint8_t pos;
157 uint32_t dict_size;
158 uint64_t dst_size;
Denys Vlasenko8dc0e192009-09-16 00:58:11 +0200159} PACKED lzma_header_t;
Rob Landleyc1d69902006-01-20 18:28:50 +0000160
161
Denis Vlasenko33762982007-01-05 14:04:47 +0000162/* #defines will force compiler to compute/optimize each one with each usage.
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000163 * Have heart and use enum instead. */
164enum {
165 LZMA_BASE_SIZE = 1846,
166 LZMA_LIT_SIZE = 768,
Rob Landleyc1d69902006-01-20 18:28:50 +0000167
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000168 LZMA_NUM_POS_BITS_MAX = 4,
Rob Landleyc1d69902006-01-20 18:28:50 +0000169
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000170 LZMA_LEN_NUM_LOW_BITS = 3,
171 LZMA_LEN_NUM_MID_BITS = 3,
172 LZMA_LEN_NUM_HIGH_BITS = 8,
Rob Landleyc1d69902006-01-20 18:28:50 +0000173
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000174 LZMA_LEN_CHOICE = 0,
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)),
Rob Landleyc1d69902006-01-20 18:28:50 +0000182
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000183 LZMA_NUM_STATES = 12,
184 LZMA_NUM_LIT_STATES = 7,
Rob Landleyc1d69902006-01-20 18:28:50 +0000185
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000186 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)),
Rob Landleyc1d69902006-01-20 18:28:50 +0000189
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000190 LZMA_NUM_POS_SLOT_BITS = 6,
191 LZMA_NUM_LEN_TO_POS_STATES = 4,
Rob Landleyc1d69902006-01-20 18:28:50 +0000192
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000193 LZMA_NUM_ALIGN_BITS = 4,
Rob Landleyc1d69902006-01-20 18:28:50 +0000194
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000195 LZMA_MATCH_MIN_LEN = 2,
Rob Landleyc1d69902006-01-20 18:28:50 +0000196
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000197 LZMA_IS_MATCH = 0,
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),
212};
213
Rob Landleyc1d69902006-01-20 18:28:50 +0000214
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000215IF_DESKTOP(long long) int FAST_FUNC
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000216unpack_lzma_stream(int src_fd, int dst_fd)
Rob Landleyc1d69902006-01-20 18:28:50 +0000217{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000218 IF_DESKTOP(long long total_written = 0;)
Rob Landleyc1d69902006-01-20 18:28:50 +0000219 lzma_header_t header;
220 int lc, pb, lp;
221 uint32_t pos_state_mask;
222 uint32_t literal_pos_mask;
Rob Landleyc1d69902006-01-20 18:28:50 +0000223 uint16_t *p;
Rob Landleyc1d69902006-01-20 18:28:50 +0000224 int num_bits;
225 int num_probs;
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000226 rc_t *rc;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200227 int i;
Rob Landleyc1d69902006-01-20 18:28:50 +0000228 uint8_t *buffer;
229 uint8_t previous_byte = 0;
230 size_t buffer_pos = 0, global_pos = 0;
231 int len = 0;
232 int state = 0;
233 uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
234
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200235 if (full_read(src_fd, &header, sizeof(header)) != sizeof(header)
236 || header.pos >= (9 * 5 * 5)
237 ) {
238 bb_error_msg("bad lzma header");
239 return -1;
240 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000241
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200242 i = header.pos / 9;
Rob Landleyc1d69902006-01-20 18:28:50 +0000243 lc = header.pos % 9;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200244 pb = i / 5;
245 lp = i % 5;
Rob Landleyc1d69902006-01-20 18:28:50 +0000246 pos_state_mask = (1 << pb) - 1;
247 literal_pos_mask = (1 << lp) - 1;
248
Rob Landleybba7f082006-05-29 05:51:12 +0000249 header.dict_size = SWAP_LE32(header.dict_size);
250 header.dst_size = SWAP_LE64(header.dst_size);
Rob Landleyc1d69902006-01-20 18:28:50 +0000251
252 if (header.dict_size == 0)
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200253 header.dict_size++;
Rob Landleyc1d69902006-01-20 18:28:50 +0000254
255 buffer = xmalloc(MIN(header.dst_size, header.dict_size));
256
257 num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
258 p = xmalloc(num_probs * sizeof(*p));
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200259 num_probs += LZMA_LITERAL - LZMA_BASE_SIZE;
Rob Landleyc1d69902006-01-20 18:28:50 +0000260 for (i = 0; i < num_probs; i++)
261 p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
262
Denis Vlasenko33762982007-01-05 14:04:47 +0000263 rc = rc_init(src_fd); /*, RC_BUFFER_SIZE); */
Rob Landleyc1d69902006-01-20 18:28:50 +0000264
265 while (global_pos + buffer_pos < header.dst_size) {
266 int pos_state = (buffer_pos + global_pos) & pos_state_mask;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200267 uint16_t *prob = p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
Rob Landleyc1d69902006-01-20 18:28:50 +0000268
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200269 if (!rc_is_bit_1(rc, prob)) {
270 static const char next_state[LZMA_NUM_STATES] =
271 { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
272 int mi = 1;
273
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000274 prob = (p + LZMA_LITERAL
275 + (LZMA_LIT_SIZE * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
276 + (previous_byte >> (8 - lc))
277 )
278 )
279 );
Rob Landleyc1d69902006-01-20 18:28:50 +0000280
281 if (state >= LZMA_NUM_LIT_STATES) {
282 int match_byte;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200283 uint32_t pos = buffer_pos - rep0;
Rob Landleyc1d69902006-01-20 18:28:50 +0000284
Rob Landleyc1d69902006-01-20 18:28:50 +0000285 while (pos >= header.dict_size)
286 pos += header.dict_size;
287 match_byte = buffer[pos];
288 do {
289 int bit;
290
291 match_byte <<= 1;
292 bit = match_byte & 0x100;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200293 bit ^= (rc_get_bit(rc, prob + 0x100 + bit + mi, &mi) << 8); /* 0x100 or 0 */
Denis Vlasenko444639c2008-03-02 18:56:23 +0000294 if (bit)
295 break;
Rob Landleyc1d69902006-01-20 18:28:50 +0000296 } while (mi < 0x100);
297 }
298 while (mi < 0x100) {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200299 rc_get_bit(rc, prob + mi, &mi);
Rob Landleyc1d69902006-01-20 18:28:50 +0000300 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000301
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200302 state = next_state[state];
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000303
304 previous_byte = (uint8_t) mi;
305#if ENABLE_FEATURE_LZMA_FAST
Denis Vlasenkoc0183e62008-03-18 03:13:25 +0000306 one_byte1:
Rob Landleyc1d69902006-01-20 18:28:50 +0000307 buffer[buffer_pos++] = previous_byte;
308 if (buffer_pos == header.dict_size) {
309 buffer_pos = 0;
310 global_pos += header.dict_size;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000311 if (full_write(dst_fd, buffer, header.dict_size) != (ssize_t)header.dict_size)
Denis Vlasenko33762982007-01-05 14:04:47 +0000312 goto bad;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000313 IF_DESKTOP(total_written += header.dict_size;)
Rob Landleyc1d69902006-01-20 18:28:50 +0000314 }
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000315#else
316 len = 1;
Denis Vlasenkoc0183e62008-03-18 03:13:25 +0000317 goto one_byte2;
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000318#endif
Rob Landleyc1d69902006-01-20 18:28:50 +0000319 } else {
320 int offset;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200321 uint16_t *prob2;
322#define prob_len prob2
Rob Landleyc1d69902006-01-20 18:28:50 +0000323
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200324 prob2 = p + LZMA_IS_REP + state;
325 if (!rc_is_bit_1(rc, prob2)) {
Rob Landleyc1d69902006-01-20 18:28:50 +0000326 rep3 = rep2;
327 rep2 = rep1;
328 rep1 = rep0;
329 state = state < LZMA_NUM_LIT_STATES ? 0 : 3;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200330 prob2 = p + LZMA_LEN_CODER;
Rob Landleyc1d69902006-01-20 18:28:50 +0000331 } else {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200332 prob2 += LZMA_IS_REP_G0 - LZMA_IS_REP;
333 if (!rc_is_bit_1(rc, prob2)) {
334 prob2 = (p + LZMA_IS_REP_0_LONG
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000335 + (state << LZMA_NUM_POS_BITS_MAX)
336 + pos_state
337 );
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200338 if (!rc_is_bit_1(rc, prob2)) {
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000339#if ENABLE_FEATURE_LZMA_FAST
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200340 uint32_t pos = buffer_pos - rep0;
341 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
Rob Landleyc1d69902006-01-20 18:28:50 +0000342 while (pos >= header.dict_size)
343 pos += header.dict_size;
344 previous_byte = buffer[pos];
Denis Vlasenkoc0183e62008-03-18 03:13:25 +0000345 goto one_byte1;
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000346#else
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200347 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000348 len = 1;
349 goto string;
350#endif
Rob Landleyc1d69902006-01-20 18:28:50 +0000351 }
352 } else {
353 uint32_t distance;
354
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200355 prob2 += LZMA_IS_REP_G1 - LZMA_IS_REP_G0;
356 distance = rep1;
357 if (rc_is_bit_1(rc, prob2)) {
358 prob2 += LZMA_IS_REP_G2 - LZMA_IS_REP_G1;
359 distance = rep2;
360 if (rc_is_bit_1(rc, prob2)) {
Rob Landleyc1d69902006-01-20 18:28:50 +0000361 distance = rep3;
362 rep3 = rep2;
363 }
364 rep2 = rep1;
365 }
366 rep1 = rep0;
367 rep0 = distance;
368 }
369 state = state < LZMA_NUM_LIT_STATES ? 8 : 11;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200370 prob2 = p + LZMA_REP_LEN_CODER;
Rob Landleyc1d69902006-01-20 18:28:50 +0000371 }
372
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200373 prob_len = prob2 + LZMA_LEN_CHOICE;
374 num_bits = LZMA_LEN_NUM_LOW_BITS;
375 if (!rc_is_bit_1(rc, prob_len)) {
376 prob_len += LZMA_LEN_LOW - LZMA_LEN_CHOICE
377 + (pos_state << LZMA_LEN_NUM_LOW_BITS);
Rob Landleyc1d69902006-01-20 18:28:50 +0000378 offset = 0;
Rob Landleyc1d69902006-01-20 18:28:50 +0000379 } else {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200380 prob_len += LZMA_LEN_CHOICE_2 - LZMA_LEN_CHOICE;
381 if (!rc_is_bit_1(rc, prob_len)) {
382 prob_len += LZMA_LEN_MID - LZMA_LEN_CHOICE_2
383 + (pos_state << LZMA_LEN_NUM_MID_BITS);
Rob Landleyc1d69902006-01-20 18:28:50 +0000384 offset = 1 << LZMA_LEN_NUM_LOW_BITS;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200385 num_bits += LZMA_LEN_NUM_MID_BITS - LZMA_LEN_NUM_LOW_BITS;
Rob Landleyc1d69902006-01-20 18:28:50 +0000386 } else {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200387 prob_len += LZMA_LEN_HIGH - LZMA_LEN_CHOICE_2;
Rob Landleyc1d69902006-01-20 18:28:50 +0000388 offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000389 + (1 << LZMA_LEN_NUM_MID_BITS));
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200390 num_bits += LZMA_LEN_NUM_HIGH_BITS - LZMA_LEN_NUM_LOW_BITS;
Rob Landleyc1d69902006-01-20 18:28:50 +0000391 }
392 }
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000393 rc_bit_tree_decode(rc, prob_len, num_bits, &len);
Rob Landleyc1d69902006-01-20 18:28:50 +0000394 len += offset;
395
396 if (state < 4) {
397 int pos_slot;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200398 uint16_t *prob3;
Rob Landleyc1d69902006-01-20 18:28:50 +0000399
400 state += LZMA_NUM_LIT_STATES;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200401 prob3 = p + LZMA_POS_SLOT +
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000402 ((len < LZMA_NUM_LEN_TO_POS_STATES ? len :
403 LZMA_NUM_LEN_TO_POS_STATES - 1)
404 << LZMA_NUM_POS_SLOT_BITS);
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200405 rc_bit_tree_decode(rc, prob3,
406 LZMA_NUM_POS_SLOT_BITS, &pos_slot);
407 rep0 = pos_slot;
Rob Landleyc1d69902006-01-20 18:28:50 +0000408 if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200409 int i2, mi2, num_bits2 = (pos_slot >> 1) - 1;
Rob Landleyc1d69902006-01-20 18:28:50 +0000410 rep0 = 2 | (pos_slot & 1);
411 if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200412 rep0 <<= num_bits2;
413 prob3 = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
Rob Landleyc1d69902006-01-20 18:28:50 +0000414 } else {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200415 for (; num_bits2 != LZMA_NUM_ALIGN_BITS; num_bits2--)
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000416 rep0 = (rep0 << 1) | rc_direct_bit(rc);
Rob Landleyc1d69902006-01-20 18:28:50 +0000417 rep0 <<= LZMA_NUM_ALIGN_BITS;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200418 prob3 = p + LZMA_ALIGN;
Rob Landleyc1d69902006-01-20 18:28:50 +0000419 }
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200420 i2 = 1;
421 mi2 = 1;
422 while (num_bits2--) {
423 if (rc_get_bit(rc, prob3 + mi2, &mi2))
424 rep0 |= i2;
425 i2 <<= 1;
Rob Landleyc1d69902006-01-20 18:28:50 +0000426 }
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200427 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000428 if (++rep0 == 0)
429 break;
430 }
431
432 len += LZMA_MATCH_MIN_LEN;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000433 IF_NOT_FEATURE_LZMA_FAST(string:)
Rob Landleyc1d69902006-01-20 18:28:50 +0000434 do {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200435 uint32_t pos = buffer_pos - rep0;
Rob Landleyc1d69902006-01-20 18:28:50 +0000436 while (pos >= header.dict_size)
437 pos += header.dict_size;
438 previous_byte = buffer[pos];
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000439 IF_NOT_FEATURE_LZMA_FAST(one_byte2:)
Rob Landleyc1d69902006-01-20 18:28:50 +0000440 buffer[buffer_pos++] = previous_byte;
441 if (buffer_pos == header.dict_size) {
442 buffer_pos = 0;
443 global_pos += header.dict_size;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000444 if (full_write(dst_fd, buffer, header.dict_size) != (ssize_t)header.dict_size)
Denis Vlasenko33762982007-01-05 14:04:47 +0000445 goto bad;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000446 IF_DESKTOP(total_written += header.dict_size;)
Rob Landleyc1d69902006-01-20 18:28:50 +0000447 }
448 len--;
449 } while (len != 0 && buffer_pos < header.dst_size);
450 }
451 }
452
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000453 {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000454 IF_NOT_DESKTOP(int total_written = 0; /* success */)
455 IF_DESKTOP(total_written += buffer_pos;)
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000456 if (full_write(dst_fd, buffer, buffer_pos) != (ssize_t)buffer_pos) {
Denis Vlasenko33762982007-01-05 14:04:47 +0000457 bad:
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000458 total_written = -1; /* failure */
459 }
Denis Vlasenko33762982007-01-05 14:04:47 +0000460 rc_free(rc);
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000461 free(p);
462 free(buffer);
463 return total_written;
Denis Vlasenko33762982007-01-05 14:04:47 +0000464 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000465}