blob: c8622f97b3b02fbfcd659508e63d73ce370307d6 [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}
Denys Vlasenko507f6ea2013-03-01 14:48:10 +010068static ALWAYS_INLINE void rc_normalize(rc_t *rc)
69{
70 if (rc->range < (1 << RC_TOP_BITS)) {
71 rc_do_normalize(rc);
72 }
73}
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020074
Rob Landleyb13fee42006-06-20 22:38:00 +000075/* Called once */
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020076static ALWAYS_INLINE rc_t* rc_init(int fd) /*, int buffer_size) */
Rob Landleyb13fee42006-06-20 22:38:00 +000077{
78 int i;
Denis Vlasenko2bbdda02008-06-27 15:48:45 +000079 rc_t *rc;
Denis Vlasenko98b8e942007-01-05 13:59:05 +000080
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020081 rc = xzalloc(sizeof(*rc) + RC_BUFFER_SIZE);
Rob Landleyb13fee42006-06-20 22:38:00 +000082
83 rc->fd = fd;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020084 /* rc->ptr = rc->buffer_end; */
Rob Landleyb13fee42006-06-20 22:38:00 +000085
Rob Landleyb13fee42006-06-20 22:38:00 +000086 for (i = 0; i < 5; i++) {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020087 rc_do_normalize(rc);
Rob Landleyb13fee42006-06-20 22:38:00 +000088 }
Denys Vlasenkoc09fd272013-03-01 14:37:58 +010089 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
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020099/* rc_is_bit_1 is called 9 times */
100static speed_inline int rc_is_bit_1(rc_t *rc, uint16_t *p)
Rob Landleyb13fee42006-06-20 22:38:00 +0000101{
102 rc_normalize(rc);
103 rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200104 if (rc->code < rc->bound) {
105 rc->range = rc->bound;
106 *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
107 return 0;
108 }
Rob Landleyb13fee42006-06-20 22:38:00 +0000109 rc->range -= rc->bound;
110 rc->code -= rc->bound;
111 *p -= *p >> RC_MOVE_BITS;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200112 return 1;
Rob Landleyb13fee42006-06-20 22:38:00 +0000113}
114
115/* Called 4 times in unlzma loop */
Denys Vlasenkoa2d04e02013-03-01 14:43:07 +0100116static ALWAYS_INLINE int rc_get_bit(rc_t *rc, uint16_t *p, int *symbol)
Rob Landleyb13fee42006-06-20 22:38:00 +0000117{
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200118 int ret = rc_is_bit_1(rc, p);
119 *symbol = *symbol * 2 + ret;
120 return ret;
Rob Landleyb13fee42006-06-20 22:38:00 +0000121}
122
123/* Called once */
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000124static ALWAYS_INLINE int rc_direct_bit(rc_t *rc)
Rob Landleyb13fee42006-06-20 22:38:00 +0000125{
126 rc_normalize(rc);
127 rc->range >>= 1;
128 if (rc->code >= rc->range) {
129 rc->code -= rc->range;
130 return 1;
131 }
132 return 0;
133}
134
135/* Called twice */
136static speed_inline void
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000137rc_bit_tree_decode(rc_t *rc, uint16_t *p, int num_levels, int *symbol)
Rob Landleyb13fee42006-06-20 22:38:00 +0000138{
139 int i = num_levels;
140
141 *symbol = 1;
142 while (i--)
143 rc_get_bit(rc, p + *symbol, symbol);
144 *symbol -= 1 << num_levels;
145}
Rob Landleyc1d69902006-01-20 18:28:50 +0000146
147
148typedef struct {
149 uint8_t pos;
150 uint32_t dict_size;
151 uint64_t dst_size;
Denys Vlasenko8dc0e192009-09-16 00:58:11 +0200152} PACKED lzma_header_t;
Rob Landleyc1d69902006-01-20 18:28:50 +0000153
154
Denis Vlasenko33762982007-01-05 14:04:47 +0000155/* #defines will force compiler to compute/optimize each one with each usage.
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000156 * Have heart and use enum instead. */
157enum {
158 LZMA_BASE_SIZE = 1846,
159 LZMA_LIT_SIZE = 768,
Rob Landleyc1d69902006-01-20 18:28:50 +0000160
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000161 LZMA_NUM_POS_BITS_MAX = 4,
Rob Landleyc1d69902006-01-20 18:28:50 +0000162
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000163 LZMA_LEN_NUM_LOW_BITS = 3,
164 LZMA_LEN_NUM_MID_BITS = 3,
165 LZMA_LEN_NUM_HIGH_BITS = 8,
Rob Landleyc1d69902006-01-20 18:28:50 +0000166
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000167 LZMA_LEN_CHOICE = 0,
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)),
Rob Landleyc1d69902006-01-20 18:28:50 +0000175
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000176 LZMA_NUM_STATES = 12,
177 LZMA_NUM_LIT_STATES = 7,
Rob Landleyc1d69902006-01-20 18:28:50 +0000178
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000179 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)),
Rob Landleyc1d69902006-01-20 18:28:50 +0000182
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000183 LZMA_NUM_POS_SLOT_BITS = 6,
184 LZMA_NUM_LEN_TO_POS_STATES = 4,
Rob Landleyc1d69902006-01-20 18:28:50 +0000185
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000186 LZMA_NUM_ALIGN_BITS = 4,
Rob Landleyc1d69902006-01-20 18:28:50 +0000187
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000188 LZMA_MATCH_MIN_LEN = 2,
Rob Landleyc1d69902006-01-20 18:28:50 +0000189
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000190 LZMA_IS_MATCH = 0,
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),
205};
206
Rob Landleyc1d69902006-01-20 18:28:50 +0000207
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000208IF_DESKTOP(long long) int FAST_FUNC
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100209unpack_lzma_stream(transformer_state_t *xstate)
Rob Landleyc1d69902006-01-20 18:28:50 +0000210{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000211 IF_DESKTOP(long long total_written = 0;)
Rob Landleyc1d69902006-01-20 18:28:50 +0000212 lzma_header_t header;
213 int lc, pb, lp;
214 uint32_t pos_state_mask;
215 uint32_t literal_pos_mask;
Rob Landleyc1d69902006-01-20 18:28:50 +0000216 uint16_t *p;
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000217 rc_t *rc;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200218 int i;
Rob Landleyc1d69902006-01-20 18:28:50 +0000219 uint8_t *buffer;
220 uint8_t previous_byte = 0;
221 size_t buffer_pos = 0, global_pos = 0;
222 int len = 0;
223 int state = 0;
224 uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
225
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100226 if (full_read(xstate->src_fd, &header, sizeof(header)) != sizeof(header)
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200227 || header.pos >= (9 * 5 * 5)
228 ) {
229 bb_error_msg("bad lzma header");
230 return -1;
231 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000232
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200233 i = header.pos / 9;
Rob Landleyc1d69902006-01-20 18:28:50 +0000234 lc = header.pos % 9;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200235 pb = i / 5;
236 lp = i % 5;
Rob Landleyc1d69902006-01-20 18:28:50 +0000237 pos_state_mask = (1 << pb) - 1;
238 literal_pos_mask = (1 << lp) - 1;
239
Denys Vlasenko81071e62014-02-28 15:42:10 +0100240 /* Example values from linux-3.3.4.tar.lzma:
241 * dict_size: 64M, dst_size: 2^64-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
Denys Vlasenko2108a6f2014-02-28 15:05:43 +0100251 {
252 int num_probs;
253
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;
259 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000260
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100261 rc = rc_init(xstate->src_fd); /*, RC_BUFFER_SIZE); */
Rob Landleyc1d69902006-01-20 18:28:50 +0000262
263 while (global_pos + buffer_pos < header.dst_size) {
264 int pos_state = (buffer_pos + global_pos) & pos_state_mask;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200265 uint16_t *prob = p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
Rob Landleyc1d69902006-01-20 18:28:50 +0000266
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200267 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 };
270 int mi = 1;
271
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000272 prob = (p + LZMA_LITERAL
273 + (LZMA_LIT_SIZE * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
274 + (previous_byte >> (8 - lc))
275 )
276 )
277 );
Rob Landleyc1d69902006-01-20 18:28:50 +0000278
279 if (state >= LZMA_NUM_LIT_STATES) {
280 int match_byte;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200281 uint32_t pos = buffer_pos - rep0;
Rob Landleyc1d69902006-01-20 18:28:50 +0000282
Rob Landleyc1d69902006-01-20 18:28:50 +0000283 while (pos >= header.dict_size)
284 pos += header.dict_size;
285 match_byte = buffer[pos];
286 do {
287 int bit;
288
289 match_byte <<= 1;
290 bit = match_byte & 0x100;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200291 bit ^= (rc_get_bit(rc, prob + 0x100 + bit + mi, &mi) << 8); /* 0x100 or 0 */
Denis Vlasenko444639c2008-03-02 18:56:23 +0000292 if (bit)
293 break;
Rob Landleyc1d69902006-01-20 18:28:50 +0000294 } while (mi < 0x100);
295 }
296 while (mi < 0x100) {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200297 rc_get_bit(rc, prob + mi, &mi);
Rob Landleyc1d69902006-01-20 18:28:50 +0000298 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000299
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200300 state = next_state[state];
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000301
302 previous_byte = (uint8_t) mi;
303#if ENABLE_FEATURE_LZMA_FAST
Denis Vlasenkoc0183e62008-03-18 03:13:25 +0000304 one_byte1:
Rob Landleyc1d69902006-01-20 18:28:50 +0000305 buffer[buffer_pos++] = previous_byte;
306 if (buffer_pos == header.dict_size) {
307 buffer_pos = 0;
308 global_pos += header.dict_size;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100309 if (transformer_write(xstate, buffer, header.dict_size) != (ssize_t)header.dict_size)
Denis Vlasenko33762982007-01-05 14:04:47 +0000310 goto bad;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000311 IF_DESKTOP(total_written += header.dict_size;)
Rob Landleyc1d69902006-01-20 18:28:50 +0000312 }
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000313#else
314 len = 1;
Denis Vlasenkoc0183e62008-03-18 03:13:25 +0000315 goto one_byte2;
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000316#endif
Rob Landleyc1d69902006-01-20 18:28:50 +0000317 } else {
Denys Vlasenko2108a6f2014-02-28 15:05:43 +0100318 int num_bits;
Rob Landleyc1d69902006-01-20 18:28:50 +0000319 int offset;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200320 uint16_t *prob2;
321#define prob_len prob2
Rob Landleyc1d69902006-01-20 18:28:50 +0000322
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200323 prob2 = p + LZMA_IS_REP + state;
324 if (!rc_is_bit_1(rc, prob2)) {
Rob Landleyc1d69902006-01-20 18:28:50 +0000325 rep3 = rep2;
326 rep2 = rep1;
327 rep1 = rep0;
328 state = state < LZMA_NUM_LIT_STATES ? 0 : 3;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200329 prob2 = p + LZMA_LEN_CODER;
Rob Landleyc1d69902006-01-20 18:28:50 +0000330 } else {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200331 prob2 += LZMA_IS_REP_G0 - LZMA_IS_REP;
332 if (!rc_is_bit_1(rc, prob2)) {
333 prob2 = (p + LZMA_IS_REP_0_LONG
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000334 + (state << LZMA_NUM_POS_BITS_MAX)
335 + pos_state
336 );
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200337 if (!rc_is_bit_1(rc, prob2)) {
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000338#if ENABLE_FEATURE_LZMA_FAST
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200339 uint32_t pos = buffer_pos - rep0;
340 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
Rob Landleyc1d69902006-01-20 18:28:50 +0000341 while (pos >= header.dict_size)
342 pos += header.dict_size;
343 previous_byte = buffer[pos];
Denis Vlasenkoc0183e62008-03-18 03:13:25 +0000344 goto one_byte1;
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000345#else
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200346 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000347 len = 1;
348 goto string;
349#endif
Rob Landleyc1d69902006-01-20 18:28:50 +0000350 }
351 } else {
352 uint32_t distance;
353
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200354 prob2 += LZMA_IS_REP_G1 - LZMA_IS_REP_G0;
355 distance = rep1;
356 if (rc_is_bit_1(rc, prob2)) {
357 prob2 += LZMA_IS_REP_G2 - LZMA_IS_REP_G1;
358 distance = rep2;
359 if (rc_is_bit_1(rc, prob2)) {
Rob Landleyc1d69902006-01-20 18:28:50 +0000360 distance = rep3;
361 rep3 = rep2;
362 }
363 rep2 = rep1;
364 }
365 rep1 = rep0;
366 rep0 = distance;
367 }
368 state = state < LZMA_NUM_LIT_STATES ? 8 : 11;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200369 prob2 = p + LZMA_REP_LEN_CODER;
Rob Landleyc1d69902006-01-20 18:28:50 +0000370 }
371
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200372 prob_len = prob2 + LZMA_LEN_CHOICE;
373 num_bits = LZMA_LEN_NUM_LOW_BITS;
374 if (!rc_is_bit_1(rc, prob_len)) {
375 prob_len += LZMA_LEN_LOW - LZMA_LEN_CHOICE
376 + (pos_state << LZMA_LEN_NUM_LOW_BITS);
Rob Landleyc1d69902006-01-20 18:28:50 +0000377 offset = 0;
Rob Landleyc1d69902006-01-20 18:28:50 +0000378 } else {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200379 prob_len += LZMA_LEN_CHOICE_2 - LZMA_LEN_CHOICE;
380 if (!rc_is_bit_1(rc, prob_len)) {
381 prob_len += LZMA_LEN_MID - LZMA_LEN_CHOICE_2
382 + (pos_state << LZMA_LEN_NUM_MID_BITS);
Rob Landleyc1d69902006-01-20 18:28:50 +0000383 offset = 1 << LZMA_LEN_NUM_LOW_BITS;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200384 num_bits += LZMA_LEN_NUM_MID_BITS - LZMA_LEN_NUM_LOW_BITS;
Rob Landleyc1d69902006-01-20 18:28:50 +0000385 } else {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200386 prob_len += LZMA_LEN_HIGH - LZMA_LEN_CHOICE_2;
Rob Landleyc1d69902006-01-20 18:28:50 +0000387 offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000388 + (1 << LZMA_LEN_NUM_MID_BITS));
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200389 num_bits += LZMA_LEN_NUM_HIGH_BITS - LZMA_LEN_NUM_LOW_BITS;
Rob Landleyc1d69902006-01-20 18:28:50 +0000390 }
391 }
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000392 rc_bit_tree_decode(rc, prob_len, num_bits, &len);
Rob Landleyc1d69902006-01-20 18:28:50 +0000393 len += offset;
394
395 if (state < 4) {
396 int pos_slot;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200397 uint16_t *prob3;
Rob Landleyc1d69902006-01-20 18:28:50 +0000398
399 state += LZMA_NUM_LIT_STATES;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200400 prob3 = p + LZMA_POS_SLOT +
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000401 ((len < LZMA_NUM_LEN_TO_POS_STATES ? len :
402 LZMA_NUM_LEN_TO_POS_STATES - 1)
403 << LZMA_NUM_POS_SLOT_BITS);
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200404 rc_bit_tree_decode(rc, prob3,
405 LZMA_NUM_POS_SLOT_BITS, &pos_slot);
406 rep0 = pos_slot;
Rob Landleyc1d69902006-01-20 18:28:50 +0000407 if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200408 int i2, mi2, num_bits2 = (pos_slot >> 1) - 1;
Rob Landleyc1d69902006-01-20 18:28:50 +0000409 rep0 = 2 | (pos_slot & 1);
410 if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200411 rep0 <<= num_bits2;
412 prob3 = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
Rob Landleyc1d69902006-01-20 18:28:50 +0000413 } else {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200414 for (; num_bits2 != LZMA_NUM_ALIGN_BITS; num_bits2--)
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000415 rep0 = (rep0 << 1) | rc_direct_bit(rc);
Rob Landleyc1d69902006-01-20 18:28:50 +0000416 rep0 <<= LZMA_NUM_ALIGN_BITS;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200417 prob3 = p + LZMA_ALIGN;
Rob Landleyc1d69902006-01-20 18:28:50 +0000418 }
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200419 i2 = 1;
420 mi2 = 1;
421 while (num_bits2--) {
422 if (rc_get_bit(rc, prob3 + mi2, &mi2))
423 rep0 |= i2;
424 i2 <<= 1;
Rob Landleyc1d69902006-01-20 18:28:50 +0000425 }
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200426 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000427 if (++rep0 == 0)
428 break;
429 }
430
431 len += LZMA_MATCH_MIN_LEN;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000432 IF_NOT_FEATURE_LZMA_FAST(string:)
Rob Landleyc1d69902006-01-20 18:28:50 +0000433 do {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200434 uint32_t pos = buffer_pos - rep0;
Rob Landleyc1d69902006-01-20 18:28:50 +0000435 while (pos >= header.dict_size)
436 pos += header.dict_size;
437 previous_byte = buffer[pos];
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000438 IF_NOT_FEATURE_LZMA_FAST(one_byte2:)
Rob Landleyc1d69902006-01-20 18:28:50 +0000439 buffer[buffer_pos++] = previous_byte;
440 if (buffer_pos == header.dict_size) {
441 buffer_pos = 0;
442 global_pos += header.dict_size;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100443 if (transformer_write(xstate, buffer, header.dict_size) != (ssize_t)header.dict_size)
Denis Vlasenko33762982007-01-05 14:04:47 +0000444 goto bad;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000445 IF_DESKTOP(total_written += header.dict_size;)
Rob Landleyc1d69902006-01-20 18:28:50 +0000446 }
447 len--;
448 } while (len != 0 && buffer_pos < header.dst_size);
Denys Vlasenko81071e62014-02-28 15:42:10 +0100449 /* FIXME: ...........^^^^^
450 * shouldn't it be "global_pos + buffer_pos < header.dst_size"?
451 */
Rob Landleyc1d69902006-01-20 18:28:50 +0000452 }
453 }
454
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000455 {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000456 IF_NOT_DESKTOP(int total_written = 0; /* success */)
457 IF_DESKTOP(total_written += buffer_pos;)
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100458 if (transformer_write(xstate, buffer, buffer_pos) != (ssize_t)buffer_pos) {
Denis Vlasenko33762982007-01-05 14:04:47 +0000459 bad:
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000460 total_written = -1; /* failure */
461 }
Denis Vlasenko33762982007-01-05 14:04:47 +0000462 rc_free(rc);
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000463 free(p);
464 free(buffer);
465 return total_written;
Denis Vlasenko33762982007-01-05 14:04:47 +0000466 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000467}