blob: 68085d68c8a42d008cc0b146d7d8217c193f2371 [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
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +000016# define size_inline
Rob Landleyb13fee42006-06-20 22:38:00 +000017#else
18# define speed_inline
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +000019# 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
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +000048/* 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);
52 if (buffer_size <= 0)
Rob Landleyb13fee42006-06-20 22:38:00 +000053 bb_error_msg_and_die("unexpected EOF");
Denis Vlasenko98b8e942007-01-05 13:59:05 +000054 rc->ptr = RC_BUFFER;
Denis Vlasenko33762982007-01-05 14:04:47 +000055 rc->buffer_end = RC_BUFFER + buffer_size;
Rob Landleyb13fee42006-06-20 22:38:00 +000056}
57
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +000058/* Called twice, but one callsite is in speed_inline'd rc_is_bit_1() */
59static void rc_do_normalize(rc_t *rc)
60{
61 if (rc->ptr >= rc->buffer_end)
62 rc_read(rc);
63 rc->range <<= 8;
64 rc->code = (rc->code << 8) | *rc->ptr++;
65}
66
Rob Landleyb13fee42006-06-20 22:38:00 +000067/* Called once */
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +000068static ALWAYS_INLINE rc_t* rc_init(int fd) /*, int buffer_size) */
Rob Landleyb13fee42006-06-20 22:38:00 +000069{
70 int i;
Denis Vlasenko2bbdda02008-06-27 15:48:45 +000071 rc_t *rc;
Denis Vlasenko98b8e942007-01-05 13:59:05 +000072
Denis Vlasenko2bbdda02008-06-27 15:48:45 +000073 rc = xmalloc(sizeof(*rc) + RC_BUFFER_SIZE);
Rob Landleyb13fee42006-06-20 22:38:00 +000074
75 rc->fd = fd;
Rob Landleyb13fee42006-06-20 22:38:00 +000076 rc->ptr = rc->buffer_end;
77
Rob Landleyb13fee42006-06-20 22:38:00 +000078 for (i = 0; i < 5; i++) {
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +000079#if ENABLE_FEATURE_LZMA_FAST
Rob Landleyb13fee42006-06-20 22:38:00 +000080 if (rc->ptr >= rc->buffer_end)
81 rc_read(rc);
82 rc->code = (rc->code << 8) | *rc->ptr++;
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +000083#else
84 rc_do_normalize(rc);
85#endif
Rob Landleyb13fee42006-06-20 22:38:00 +000086 }
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +000087 rc->range = 0xFFFFFFFF;
Denis Vlasenko98b8e942007-01-05 13:59:05 +000088 return rc;
Rob Landleyb13fee42006-06-20 22:38:00 +000089}
90
Denis Vlasenko98b8e942007-01-05 13:59:05 +000091/* Called once */
Denis Vlasenko2bbdda02008-06-27 15:48:45 +000092static ALWAYS_INLINE void rc_free(rc_t *rc)
Rob Landleyb13fee42006-06-20 22:38:00 +000093{
Denis Vlasenko2bbdda02008-06-27 15:48:45 +000094 free(rc);
Rob Landleyb13fee42006-06-20 22:38:00 +000095}
96
Denis Vlasenko2bbdda02008-06-27 15:48:45 +000097static ALWAYS_INLINE void rc_normalize(rc_t *rc)
Rob Landleyb13fee42006-06-20 22:38:00 +000098{
99 if (rc->range < (1 << RC_TOP_BITS)) {
100 rc_do_normalize(rc);
101 }
102}
103
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +0000104/* rc_is_bit_1 is called 9 times */
105static speed_inline int rc_is_bit_1(rc_t *rc, uint16_t *p)
Rob Landleyb13fee42006-06-20 22:38:00 +0000106{
107 rc_normalize(rc);
108 rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +0000109 if (rc->code < rc->bound) {
110 rc->range = rc->bound;
111 *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
112 return 0;
113 }
Rob Landleyb13fee42006-06-20 22:38:00 +0000114 rc->range -= rc->bound;
115 rc->code -= rc->bound;
116 *p -= *p >> RC_MOVE_BITS;
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +0000117 return 1;
Rob Landleyb13fee42006-06-20 22:38:00 +0000118}
119
120/* Called 4 times in unlzma loop */
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +0000121static speed_inline int rc_get_bit(rc_t *rc, uint16_t *p, int *symbol)
Rob Landleyb13fee42006-06-20 22:38:00 +0000122{
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +0000123 int ret = rc_is_bit_1(rc, p);
124 *symbol = *symbol * 2 + ret;
125 return ret;
Rob Landleyb13fee42006-06-20 22:38:00 +0000126}
127
128/* Called once */
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000129static ALWAYS_INLINE int rc_direct_bit(rc_t *rc)
Rob Landleyb13fee42006-06-20 22:38:00 +0000130{
131 rc_normalize(rc);
132 rc->range >>= 1;
133 if (rc->code >= rc->range) {
134 rc->code -= rc->range;
135 return 1;
136 }
137 return 0;
138}
139
140/* Called twice */
141static speed_inline void
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000142rc_bit_tree_decode(rc_t *rc, uint16_t *p, int num_levels, int *symbol)
Rob Landleyb13fee42006-06-20 22:38:00 +0000143{
144 int i = num_levels;
145
146 *symbol = 1;
147 while (i--)
148 rc_get_bit(rc, p + *symbol, symbol);
149 *symbol -= 1 << num_levels;
150}
Rob Landleyc1d69902006-01-20 18:28:50 +0000151
152
153typedef struct {
154 uint8_t pos;
155 uint32_t dict_size;
156 uint64_t dst_size;
157} __attribute__ ((packed)) lzma_header_t;
158
159
Denis Vlasenko33762982007-01-05 14:04:47 +0000160/* #defines will force compiler to compute/optimize each one with each usage.
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000161 * Have heart and use enum instead. */
162enum {
163 LZMA_BASE_SIZE = 1846,
164 LZMA_LIT_SIZE = 768,
Rob Landleyc1d69902006-01-20 18:28:50 +0000165
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000166 LZMA_NUM_POS_BITS_MAX = 4,
Rob Landleyc1d69902006-01-20 18:28:50 +0000167
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000168 LZMA_LEN_NUM_LOW_BITS = 3,
169 LZMA_LEN_NUM_MID_BITS = 3,
170 LZMA_LEN_NUM_HIGH_BITS = 8,
Rob Landleyc1d69902006-01-20 18:28:50 +0000171
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000172 LZMA_LEN_CHOICE = 0,
173 LZMA_LEN_CHOICE_2 = (LZMA_LEN_CHOICE + 1),
174 LZMA_LEN_LOW = (LZMA_LEN_CHOICE_2 + 1),
175 LZMA_LEN_MID = (LZMA_LEN_LOW \
176 + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS))),
177 LZMA_LEN_HIGH = (LZMA_LEN_MID \
178 + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS))),
179 LZMA_NUM_LEN_PROBS = (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS)),
Rob Landleyc1d69902006-01-20 18:28:50 +0000180
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000181 LZMA_NUM_STATES = 12,
182 LZMA_NUM_LIT_STATES = 7,
Rob Landleyc1d69902006-01-20 18:28:50 +0000183
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000184 LZMA_START_POS_MODEL_INDEX = 4,
185 LZMA_END_POS_MODEL_INDEX = 14,
186 LZMA_NUM_FULL_DISTANCES = (1 << (LZMA_END_POS_MODEL_INDEX >> 1)),
Rob Landleyc1d69902006-01-20 18:28:50 +0000187
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000188 LZMA_NUM_POS_SLOT_BITS = 6,
189 LZMA_NUM_LEN_TO_POS_STATES = 4,
Rob Landleyc1d69902006-01-20 18:28:50 +0000190
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000191 LZMA_NUM_ALIGN_BITS = 4,
Rob Landleyc1d69902006-01-20 18:28:50 +0000192
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000193 LZMA_MATCH_MIN_LEN = 2,
Rob Landleyc1d69902006-01-20 18:28:50 +0000194
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000195 LZMA_IS_MATCH = 0,
196 LZMA_IS_REP = (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)),
197 LZMA_IS_REP_G0 = (LZMA_IS_REP + LZMA_NUM_STATES),
198 LZMA_IS_REP_G1 = (LZMA_IS_REP_G0 + LZMA_NUM_STATES),
199 LZMA_IS_REP_G2 = (LZMA_IS_REP_G1 + LZMA_NUM_STATES),
200 LZMA_IS_REP_0_LONG = (LZMA_IS_REP_G2 + LZMA_NUM_STATES),
201 LZMA_POS_SLOT = (LZMA_IS_REP_0_LONG \
202 + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)),
203 LZMA_SPEC_POS = (LZMA_POS_SLOT \
204 + (LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS)),
205 LZMA_ALIGN = (LZMA_SPEC_POS \
206 + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX),
207 LZMA_LEN_CODER = (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS)),
208 LZMA_REP_LEN_CODER = (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS),
209 LZMA_LITERAL = (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS),
210};
211
Rob Landleyc1d69902006-01-20 18:28:50 +0000212
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000213IF_DESKTOP(long long) int FAST_FUNC
Denis Vlasenkoc14d39e2007-06-08 13:05:39 +0000214unpack_lzma_stream(int src_fd, int dst_fd)
Rob Landleyc1d69902006-01-20 18:28:50 +0000215{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000216 IF_DESKTOP(long long total_written = 0;)
Rob Landleyc1d69902006-01-20 18:28:50 +0000217 lzma_header_t header;
218 int lc, pb, lp;
219 uint32_t pos_state_mask;
220 uint32_t literal_pos_mask;
221 uint32_t pos;
222 uint16_t *p;
223 uint16_t *prob;
224 uint16_t *prob_lit;
225 int num_bits;
226 int num_probs;
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000227 rc_t *rc;
Rob Landleyc1d69902006-01-20 18:28:50 +0000228 int i, mi;
229 uint8_t *buffer;
230 uint8_t previous_byte = 0;
231 size_t buffer_pos = 0, global_pos = 0;
232 int len = 0;
233 int state = 0;
234 uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
235
Denis Vlasenko33762982007-01-05 14:04:47 +0000236 xread(src_fd, &header, sizeof(header));
Rob Landleyc1d69902006-01-20 18:28:50 +0000237
238 if (header.pos >= (9 * 5 * 5))
239 bb_error_msg_and_die("bad header");
240 mi = header.pos / 9;
241 lc = header.pos % 9;
242 pb = mi / 5;
243 lp = mi % 5;
244 pos_state_mask = (1 << pb) - 1;
245 literal_pos_mask = (1 << lp) - 1;
246
Rob Landleybba7f082006-05-29 05:51:12 +0000247 header.dict_size = SWAP_LE32(header.dict_size);
248 header.dst_size = SWAP_LE64(header.dst_size);
Rob Landleyc1d69902006-01-20 18:28:50 +0000249
250 if (header.dict_size == 0)
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +0000251 header.dict_size++;
Rob Landleyc1d69902006-01-20 18:28:50 +0000252
253 buffer = xmalloc(MIN(header.dst_size, header.dict_size));
254
255 num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
256 p = xmalloc(num_probs * sizeof(*p));
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +0000257 num_probs += LZMA_LITERAL - LZMA_BASE_SIZE;
Rob Landleyc1d69902006-01-20 18:28:50 +0000258 for (i = 0; i < num_probs; i++)
259 p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
260
Denis Vlasenko33762982007-01-05 14:04:47 +0000261 rc = rc_init(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;
265
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000266 prob = p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +0000267 if (!rc_is_bit_1(rc, prob)) {
Rob Landleyc1d69902006-01-20 18:28:50 +0000268 mi = 1;
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000269 prob = (p + LZMA_LITERAL
270 + (LZMA_LIT_SIZE * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
271 + (previous_byte >> (8 - lc))
272 )
273 )
274 );
Rob Landleyc1d69902006-01-20 18:28:50 +0000275
276 if (state >= LZMA_NUM_LIT_STATES) {
277 int match_byte;
278
279 pos = buffer_pos - rep0;
280 while (pos >= header.dict_size)
281 pos += header.dict_size;
282 match_byte = buffer[pos];
283 do {
284 int bit;
285
286 match_byte <<= 1;
287 bit = match_byte & 0x100;
288 prob_lit = prob + 0x100 + bit + mi;
Denis Vlasenko444639c2008-03-02 18:56:23 +0000289 bit ^= (rc_get_bit(rc, prob_lit, &mi) << 8); /* 0x100 or 0 */
290 if (bit)
291 break;
Rob Landleyc1d69902006-01-20 18:28:50 +0000292 } while (mi < 0x100);
293 }
294 while (mi < 0x100) {
295 prob_lit = prob + mi;
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000296 rc_get_bit(rc, prob_lit, &mi);
Rob Landleyc1d69902006-01-20 18:28:50 +0000297 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000298
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000299 state -= 3;
300 if (state < 4-3)
301 state = 0;
302 if (state >= 10-3)
303 state -= 6-3;
304
305 previous_byte = (uint8_t) mi;
306#if ENABLE_FEATURE_LZMA_FAST
Denis Vlasenkoc0183e62008-03-18 03:13:25 +0000307 one_byte1:
Rob Landleyc1d69902006-01-20 18:28:50 +0000308 buffer[buffer_pos++] = previous_byte;
309 if (buffer_pos == header.dict_size) {
310 buffer_pos = 0;
311 global_pos += header.dict_size;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000312 if (full_write(dst_fd, buffer, header.dict_size) != (ssize_t)header.dict_size)
Denis Vlasenko33762982007-01-05 14:04:47 +0000313 goto bad;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000314 IF_DESKTOP(total_written += header.dict_size;)
Rob Landleyc1d69902006-01-20 18:28:50 +0000315 }
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000316#else
317 len = 1;
Denis Vlasenkoc0183e62008-03-18 03:13:25 +0000318 goto one_byte2;
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000319#endif
Rob Landleyc1d69902006-01-20 18:28:50 +0000320 } else {
321 int offset;
322 uint16_t *prob_len;
323
Rob Landleyc1d69902006-01-20 18:28:50 +0000324 prob = p + LZMA_IS_REP + state;
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +0000325 if (!rc_is_bit_1(rc, prob)) {
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;
330 prob = p + LZMA_LEN_CODER;
331 } else {
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +0000332 prob += LZMA_IS_REP_G0 - LZMA_IS_REP;
333 if (!rc_is_bit_1(rc, prob)) {
Rob Landleyc1d69902006-01-20 18:28:50 +0000334 prob = (p + LZMA_IS_REP_0_LONG
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000335 + (state << LZMA_NUM_POS_BITS_MAX)
336 + pos_state
337 );
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +0000338 if (!rc_is_bit_1(rc, prob)) {
Rob Landleyc1d69902006-01-20 18:28:50 +0000339 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000340#if ENABLE_FEATURE_LZMA_FAST
Rob Landleyc1d69902006-01-20 18:28:50 +0000341 pos = buffer_pos - rep0;
342 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
347 len = 1;
348 goto string;
349#endif
Rob Landleyc1d69902006-01-20 18:28:50 +0000350 }
351 } else {
352 uint32_t distance;
353
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +0000354 prob += LZMA_IS_REP_G1 - LZMA_IS_REP_G0;
355 distance = rep1;
356 if (rc_is_bit_1(rc, prob)) {
357 prob += LZMA_IS_REP_G2 - LZMA_IS_REP_G1;
358 distance = rep2;
359 if (rc_is_bit_1(rc, prob)) {
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;
369 prob = p + LZMA_REP_LEN_CODER;
370 }
371
372 prob_len = prob + LZMA_LEN_CHOICE;
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +0000373 if (!rc_is_bit_1(rc, prob_len)) {
374 prob_len += LZMA_LEN_LOW - LZMA_LEN_CHOICE
375 + (pos_state << LZMA_LEN_NUM_LOW_BITS);
Rob Landleyc1d69902006-01-20 18:28:50 +0000376 offset = 0;
377 num_bits = LZMA_LEN_NUM_LOW_BITS;
378 } else {
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +0000379 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;
384 num_bits = LZMA_LEN_NUM_MID_BITS;
385 } else {
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +0000386 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));
Rob Landleyc1d69902006-01-20 18:28:50 +0000389 num_bits = LZMA_LEN_NUM_HIGH_BITS;
390 }
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;
397
398 state += LZMA_NUM_LIT_STATES;
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000399 prob = p + LZMA_POS_SLOT +
400 ((len < LZMA_NUM_LEN_TO_POS_STATES ? len :
401 LZMA_NUM_LEN_TO_POS_STATES - 1)
402 << LZMA_NUM_POS_SLOT_BITS);
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +0000403 rc_bit_tree_decode(rc, prob,
404 LZMA_NUM_POS_SLOT_BITS, &pos_slot);
405 rep0 = pos_slot;
Rob Landleyc1d69902006-01-20 18:28:50 +0000406 if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
407 num_bits = (pos_slot >> 1) - 1;
408 rep0 = 2 | (pos_slot & 1);
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +0000409 prob = p + LZMA_ALIGN;
Rob Landleyc1d69902006-01-20 18:28:50 +0000410 if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
411 rep0 <<= num_bits;
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +0000412 prob += LZMA_SPEC_POS - LZMA_ALIGN - 1 + rep0 - pos_slot;
Rob Landleyc1d69902006-01-20 18:28:50 +0000413 } else {
414 num_bits -= LZMA_NUM_ALIGN_BITS;
415 while (num_bits--)
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;
418 num_bits = LZMA_NUM_ALIGN_BITS;
419 }
420 i = 1;
421 mi = 1;
422 while (num_bits--) {
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000423 if (rc_get_bit(rc, prob + mi, &mi))
Rob Landleyc1d69902006-01-20 18:28:50 +0000424 rep0 |= i;
425 i <<= 1;
426 }
Denis Vlasenko9ac3dc72009-04-25 12:19:35 +0000427 }
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 {
435 pos = buffer_pos - rep0;
436 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}