blob: 6886239d0351ddf33a5289dc11454cc9da5411e2 [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
Denys Vlasenkoa1870f42018-04-08 20:45:16 +020014#if 0
15# define dbg(...) bb_error_msg(__VA_ARGS__)
16#else
17# define dbg(...) ((void)0)
18#endif
19
20
Denis Vlasenkoe3241842007-08-13 10:36:25 +000021#if ENABLE_FEATURE_LZMA_FAST
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +000022# define speed_inline ALWAYS_INLINE
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020023# define size_inline
Rob Landleyb13fee42006-06-20 22:38:00 +000024#else
25# define speed_inline
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020026# define size_inline ALWAYS_INLINE
Rob Landleyb13fee42006-06-20 22:38:00 +000027#endif
28
29
30typedef struct {
31 int fd;
32 uint8_t *ptr;
Denis Vlasenko33762982007-01-05 14:04:47 +000033
34/* Was keeping rc on stack in unlzma and separately allocating buffer,
35 * but with "buffer 'attached to' allocated rc" code is smaller: */
36 /* uint8_t *buffer; */
37#define RC_BUFFER ((uint8_t*)(rc+1))
38
Rob Landleyb13fee42006-06-20 22:38:00 +000039 uint8_t *buffer_end;
Denis Vlasenko33762982007-01-05 14:04:47 +000040
41/* Had provisions for variable buffer, but we don't need it here */
42 /* int buffer_size; */
43#define RC_BUFFER_SIZE 0x10000
44
Rob Landleyb13fee42006-06-20 22:38:00 +000045 uint32_t code;
46 uint32_t range;
47 uint32_t bound;
48} rc_t;
49
Rob Landleyb13fee42006-06-20 22:38:00 +000050#define RC_TOP_BITS 24
51#define RC_MOVE_BITS 5
52#define RC_MODEL_TOTAL_BITS 11
53
54
Denys Vlasenkoc09fd272013-03-01 14:37:58 +010055/* Called once in rc_do_normalize() */
56static void rc_read(rc_t *rc)
Rob Landleyb13fee42006-06-20 22:38:00 +000057{
Denis Vlasenko33762982007-01-05 14:04:47 +000058 int buffer_size = safe_read(rc->fd, RC_BUFFER, RC_BUFFER_SIZE);
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020059//TODO: return -1 instead
60//This will make unlzma delete broken unpacked file on unpack errors
Denis Vlasenko33762982007-01-05 14:04:47 +000061 if (buffer_size <= 0)
Rob Landleyb13fee42006-06-20 22:38:00 +000062 bb_error_msg_and_die("unexpected EOF");
Denis Vlasenko33762982007-01-05 14:04:47 +000063 rc->buffer_end = RC_BUFFER + buffer_size;
Denys Vlasenkoc09fd272013-03-01 14:37:58 +010064 rc->ptr = RC_BUFFER;
Rob Landleyb13fee42006-06-20 22:38:00 +000065}
66
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020067/* Called twice, but one callsite is in speed_inline'd rc_is_bit_1() */
68static void rc_do_normalize(rc_t *rc)
69{
70 if (rc->ptr >= rc->buffer_end)
71 rc_read(rc);
72 rc->range <<= 8;
73 rc->code = (rc->code << 8) | *rc->ptr++;
74}
Denys Vlasenko507f6ea2013-03-01 14:48:10 +010075static ALWAYS_INLINE void rc_normalize(rc_t *rc)
76{
77 if (rc->range < (1 << RC_TOP_BITS)) {
78 rc_do_normalize(rc);
79 }
80}
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020081
Rob Landleyb13fee42006-06-20 22:38:00 +000082/* Called once */
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020083static ALWAYS_INLINE rc_t* rc_init(int fd) /*, int buffer_size) */
Rob Landleyb13fee42006-06-20 22:38:00 +000084{
85 int i;
Denis Vlasenko2bbdda02008-06-27 15:48:45 +000086 rc_t *rc;
Denis Vlasenko98b8e942007-01-05 13:59:05 +000087
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020088 rc = xzalloc(sizeof(*rc) + RC_BUFFER_SIZE);
Rob Landleyb13fee42006-06-20 22:38:00 +000089
90 rc->fd = fd;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020091 /* rc->ptr = rc->buffer_end; */
Rob Landleyb13fee42006-06-20 22:38:00 +000092
Rob Landleyb13fee42006-06-20 22:38:00 +000093 for (i = 0; i < 5; i++) {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +020094 rc_do_normalize(rc);
Rob Landleyb13fee42006-06-20 22:38:00 +000095 }
Denys Vlasenkoc09fd272013-03-01 14:37:58 +010096 rc->range = 0xffffffff;
Denis Vlasenko98b8e942007-01-05 13:59:05 +000097 return rc;
Rob Landleyb13fee42006-06-20 22:38:00 +000098}
99
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000100/* Called once */
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000101static ALWAYS_INLINE void rc_free(rc_t *rc)
Rob Landleyb13fee42006-06-20 22:38:00 +0000102{
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000103 free(rc);
Rob Landleyb13fee42006-06-20 22:38:00 +0000104}
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 Vlasenkoa2d04e02013-03-01 14:43:07 +0100123static ALWAYS_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
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100216unpack_lzma_stream(transformer_state_t *xstate)
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;
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000224 rc_t *rc;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200225 int i;
Rob Landleyc1d69902006-01-20 18:28:50 +0000226 uint8_t *buffer;
Denys Vlasenkoe09c4262018-04-19 19:29:49 +0200227 uint32_t buffer_size;
Rob Landleyc1d69902006-01-20 18:28:50 +0000228 uint8_t previous_byte = 0;
229 size_t buffer_pos = 0, global_pos = 0;
230 int len = 0;
231 int state = 0;
232 uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
233
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100234 if (full_read(xstate->src_fd, &header, sizeof(header)) != sizeof(header)
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200235 || header.pos >= (9 * 5 * 5)
236 ) {
237 bb_error_msg("bad lzma header");
238 return -1;
239 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000240
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200241 i = header.pos / 9;
Rob Landleyc1d69902006-01-20 18:28:50 +0000242 lc = header.pos % 9;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200243 pb = i / 5;
244 lp = i % 5;
Rob Landleyc1d69902006-01-20 18:28:50 +0000245 pos_state_mask = (1 << pb) - 1;
246 literal_pos_mask = (1 << lp) - 1;
247
Denys Vlasenko81071e62014-02-28 15:42:10 +0100248 /* Example values from linux-3.3.4.tar.lzma:
249 * dict_size: 64M, dst_size: 2^64-1
250 */
Rob Landleybba7f082006-05-29 05:51:12 +0000251 header.dict_size = SWAP_LE32(header.dict_size);
252 header.dst_size = SWAP_LE64(header.dst_size);
Rob Landleyc1d69902006-01-20 18:28:50 +0000253
254 if (header.dict_size == 0)
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200255 header.dict_size++;
Rob Landleyc1d69902006-01-20 18:28:50 +0000256
Denys Vlasenkoe09c4262018-04-19 19:29:49 +0200257 buffer_size = MIN(header.dst_size, header.dict_size);
258 buffer = xmalloc(buffer_size);
Rob Landleyc1d69902006-01-20 18:28:50 +0000259
Denys Vlasenko2108a6f2014-02-28 15:05:43 +0100260 {
261 int num_probs;
262
263 num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
264 p = xmalloc(num_probs * sizeof(*p));
265 num_probs += LZMA_LITERAL - LZMA_BASE_SIZE;
266 for (i = 0; i < num_probs; i++)
267 p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
268 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000269
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100270 rc = rc_init(xstate->src_fd); /*, RC_BUFFER_SIZE); */
Rob Landleyc1d69902006-01-20 18:28:50 +0000271
272 while (global_pos + buffer_pos < header.dst_size) {
273 int pos_state = (buffer_pos + global_pos) & pos_state_mask;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200274 uint16_t *prob = p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
Rob Landleyc1d69902006-01-20 18:28:50 +0000275
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200276 if (!rc_is_bit_1(rc, prob)) {
277 static const char next_state[LZMA_NUM_STATES] =
278 { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
279 int mi = 1;
280
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000281 prob = (p + LZMA_LITERAL
282 + (LZMA_LIT_SIZE * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
283 + (previous_byte >> (8 - lc))
284 )
285 )
286 );
Rob Landleyc1d69902006-01-20 18:28:50 +0000287
288 if (state >= LZMA_NUM_LIT_STATES) {
289 int match_byte;
Denys Vlasenko3989e5a2017-01-09 13:55:11 +0100290 uint32_t pos;
Rob Landleyc1d69902006-01-20 18:28:50 +0000291
Denys Vlasenko3989e5a2017-01-09 13:55:11 +0100292 pos = buffer_pos - rep0;
293 if ((int32_t)pos < 0)
Rob Landleyc1d69902006-01-20 18:28:50 +0000294 pos += header.dict_size;
295 match_byte = buffer[pos];
296 do {
297 int bit;
298
299 match_byte <<= 1;
300 bit = match_byte & 0x100;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200301 bit ^= (rc_get_bit(rc, prob + 0x100 + bit + mi, &mi) << 8); /* 0x100 or 0 */
Denis Vlasenko444639c2008-03-02 18:56:23 +0000302 if (bit)
303 break;
Rob Landleyc1d69902006-01-20 18:28:50 +0000304 } while (mi < 0x100);
305 }
306 while (mi < 0x100) {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200307 rc_get_bit(rc, prob + mi, &mi);
Rob Landleyc1d69902006-01-20 18:28:50 +0000308 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000309
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200310 state = next_state[state];
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000311
312 previous_byte = (uint8_t) mi;
313#if ENABLE_FEATURE_LZMA_FAST
Denis Vlasenkoc0183e62008-03-18 03:13:25 +0000314 one_byte1:
Rob Landleyc1d69902006-01-20 18:28:50 +0000315 buffer[buffer_pos++] = previous_byte;
316 if (buffer_pos == header.dict_size) {
317 buffer_pos = 0;
318 global_pos += header.dict_size;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100319 if (transformer_write(xstate, buffer, header.dict_size) != (ssize_t)header.dict_size)
Denis Vlasenko33762982007-01-05 14:04:47 +0000320 goto bad;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000321 IF_DESKTOP(total_written += header.dict_size;)
Rob Landleyc1d69902006-01-20 18:28:50 +0000322 }
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000323#else
324 len = 1;
Denis Vlasenkoc0183e62008-03-18 03:13:25 +0000325 goto one_byte2;
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000326#endif
Rob Landleyc1d69902006-01-20 18:28:50 +0000327 } else {
Denys Vlasenko2108a6f2014-02-28 15:05:43 +0100328 int num_bits;
Rob Landleyc1d69902006-01-20 18:28:50 +0000329 int offset;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200330 uint16_t *prob2;
331#define prob_len prob2
Rob Landleyc1d69902006-01-20 18:28:50 +0000332
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200333 prob2 = p + LZMA_IS_REP + state;
334 if (!rc_is_bit_1(rc, prob2)) {
Rob Landleyc1d69902006-01-20 18:28:50 +0000335 rep3 = rep2;
336 rep2 = rep1;
337 rep1 = rep0;
338 state = state < LZMA_NUM_LIT_STATES ? 0 : 3;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200339 prob2 = p + LZMA_LEN_CODER;
Rob Landleyc1d69902006-01-20 18:28:50 +0000340 } else {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200341 prob2 += LZMA_IS_REP_G0 - LZMA_IS_REP;
342 if (!rc_is_bit_1(rc, prob2)) {
343 prob2 = (p + LZMA_IS_REP_0_LONG
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000344 + (state << LZMA_NUM_POS_BITS_MAX)
345 + pos_state
346 );
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200347 if (!rc_is_bit_1(rc, prob2)) {
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000348#if ENABLE_FEATURE_LZMA_FAST
Denys Vlasenko3989e5a2017-01-09 13:55:11 +0100349 uint32_t pos;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200350 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
Denys Vlasenko3989e5a2017-01-09 13:55:11 +0100351
352 pos = buffer_pos - rep0;
Denys Vlasenkoa36986b2018-05-25 17:03:46 +0200353 if ((int32_t)pos < 0) {
Rob Landleyc1d69902006-01-20 18:28:50 +0000354 pos += header.dict_size;
Denys Vlasenkoa36986b2018-05-25 17:03:46 +0200355 /* see unzip_bad_lzma_2.zip: */
356 if (pos >= buffer_size)
357 goto bad;
358 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000359 previous_byte = buffer[pos];
Denis Vlasenkoc0183e62008-03-18 03:13:25 +0000360 goto one_byte1;
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000361#else
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200362 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000363 len = 1;
364 goto string;
365#endif
Rob Landleyc1d69902006-01-20 18:28:50 +0000366 }
367 } else {
368 uint32_t distance;
369
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200370 prob2 += LZMA_IS_REP_G1 - LZMA_IS_REP_G0;
371 distance = rep1;
372 if (rc_is_bit_1(rc, prob2)) {
373 prob2 += LZMA_IS_REP_G2 - LZMA_IS_REP_G1;
374 distance = rep2;
375 if (rc_is_bit_1(rc, prob2)) {
Rob Landleyc1d69902006-01-20 18:28:50 +0000376 distance = rep3;
377 rep3 = rep2;
378 }
379 rep2 = rep1;
380 }
381 rep1 = rep0;
382 rep0 = distance;
383 }
384 state = state < LZMA_NUM_LIT_STATES ? 8 : 11;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200385 prob2 = p + LZMA_REP_LEN_CODER;
Rob Landleyc1d69902006-01-20 18:28:50 +0000386 }
387
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200388 prob_len = prob2 + LZMA_LEN_CHOICE;
389 num_bits = LZMA_LEN_NUM_LOW_BITS;
390 if (!rc_is_bit_1(rc, prob_len)) {
391 prob_len += LZMA_LEN_LOW - LZMA_LEN_CHOICE
392 + (pos_state << LZMA_LEN_NUM_LOW_BITS);
Rob Landleyc1d69902006-01-20 18:28:50 +0000393 offset = 0;
Rob Landleyc1d69902006-01-20 18:28:50 +0000394 } else {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200395 prob_len += LZMA_LEN_CHOICE_2 - LZMA_LEN_CHOICE;
396 if (!rc_is_bit_1(rc, prob_len)) {
397 prob_len += LZMA_LEN_MID - LZMA_LEN_CHOICE_2
398 + (pos_state << LZMA_LEN_NUM_MID_BITS);
Rob Landleyc1d69902006-01-20 18:28:50 +0000399 offset = 1 << LZMA_LEN_NUM_LOW_BITS;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200400 num_bits += LZMA_LEN_NUM_MID_BITS - LZMA_LEN_NUM_LOW_BITS;
Rob Landleyc1d69902006-01-20 18:28:50 +0000401 } else {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200402 prob_len += LZMA_LEN_HIGH - LZMA_LEN_CHOICE_2;
Rob Landleyc1d69902006-01-20 18:28:50 +0000403 offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000404 + (1 << LZMA_LEN_NUM_MID_BITS));
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200405 num_bits += LZMA_LEN_NUM_HIGH_BITS - LZMA_LEN_NUM_LOW_BITS;
Rob Landleyc1d69902006-01-20 18:28:50 +0000406 }
407 }
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000408 rc_bit_tree_decode(rc, prob_len, num_bits, &len);
Rob Landleyc1d69902006-01-20 18:28:50 +0000409 len += offset;
410
411 if (state < 4) {
412 int pos_slot;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200413 uint16_t *prob3;
Rob Landleyc1d69902006-01-20 18:28:50 +0000414
415 state += LZMA_NUM_LIT_STATES;
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200416 prob3 = p + LZMA_POS_SLOT +
Denis Vlasenkoc3fff872008-03-02 18:55:49 +0000417 ((len < LZMA_NUM_LEN_TO_POS_STATES ? len :
418 LZMA_NUM_LEN_TO_POS_STATES - 1)
419 << LZMA_NUM_POS_SLOT_BITS);
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200420 rc_bit_tree_decode(rc, prob3,
421 LZMA_NUM_POS_SLOT_BITS, &pos_slot);
422 rep0 = pos_slot;
Rob Landleyc1d69902006-01-20 18:28:50 +0000423 if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200424 int i2, mi2, num_bits2 = (pos_slot >> 1) - 1;
Rob Landleyc1d69902006-01-20 18:28:50 +0000425 rep0 = 2 | (pos_slot & 1);
426 if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200427 rep0 <<= num_bits2;
428 prob3 = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
Rob Landleyc1d69902006-01-20 18:28:50 +0000429 } else {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200430 for (; num_bits2 != LZMA_NUM_ALIGN_BITS; num_bits2--)
Denis Vlasenko98b8e942007-01-05 13:59:05 +0000431 rep0 = (rep0 << 1) | rc_direct_bit(rc);
Rob Landleyc1d69902006-01-20 18:28:50 +0000432 rep0 <<= LZMA_NUM_ALIGN_BITS;
Denys Vlasenkoa1870f42018-04-08 20:45:16 +0200433 if ((int32_t)rep0 < 0) {
434 dbg("%d rep0:%d", __LINE__, rep0);
435 goto bad;
436 }
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200437 prob3 = p + LZMA_ALIGN;
Rob Landleyc1d69902006-01-20 18:28:50 +0000438 }
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200439 i2 = 1;
440 mi2 = 1;
441 while (num_bits2--) {
442 if (rc_get_bit(rc, prob3 + mi2, &mi2))
443 rep0 |= i2;
444 i2 <<= 1;
Rob Landleyc1d69902006-01-20 18:28:50 +0000445 }
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200446 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000447 if (++rep0 == 0)
448 break;
449 }
450
451 len += LZMA_MATCH_MIN_LEN;
Denys Vlasenko590402b2017-01-09 14:28:25 +0100452 /*
453 * LZMA SDK has this optimized:
454 * it precalculates size and copies many bytes
455 * in a loop with simpler checks, a-la:
456 * do
457 * *(dest) = *(dest + ofs);
458 * while (++dest != lim);
459 * and
460 * do {
461 * buffer[buffer_pos++] = buffer[pos];
462 * if (++pos == header.dict_size)
463 * pos = 0;
464 * } while (--cur_len != 0);
465 * Our code is slower (more checks per byte copy):
466 */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000467 IF_NOT_FEATURE_LZMA_FAST(string:)
Rob Landleyc1d69902006-01-20 18:28:50 +0000468 do {
Denys Vlasenkof2c184b2009-09-15 23:40:08 +0200469 uint32_t pos = buffer_pos - rep0;
Denys Vlasenko9ac42c52017-10-27 15:37:03 +0200470 if ((int32_t)pos < 0) {
Rob Landleyc1d69902006-01-20 18:28:50 +0000471 pos += header.dict_size;
Denys Vlasenko9ac42c52017-10-27 15:37:03 +0200472 /* bug 10436 has an example file where this triggers: */
Denys Vlasenkoe09c4262018-04-19 19:29:49 +0200473 //if ((int32_t)pos < 0)
474 // goto bad;
475 /* more stringent test (see unzip_bad_lzma_1.zip): */
476 if (pos >= buffer_size)
Denys Vlasenko9ac42c52017-10-27 15:37:03 +0200477 goto bad;
478 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000479 previous_byte = buffer[pos];
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000480 IF_NOT_FEATURE_LZMA_FAST(one_byte2:)
Rob Landleyc1d69902006-01-20 18:28:50 +0000481 buffer[buffer_pos++] = previous_byte;
482 if (buffer_pos == header.dict_size) {
483 buffer_pos = 0;
484 global_pos += header.dict_size;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100485 if (transformer_write(xstate, buffer, header.dict_size) != (ssize_t)header.dict_size)
Denis Vlasenko33762982007-01-05 14:04:47 +0000486 goto bad;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000487 IF_DESKTOP(total_written += header.dict_size;)
Rob Landleyc1d69902006-01-20 18:28:50 +0000488 }
489 len--;
490 } while (len != 0 && buffer_pos < header.dst_size);
Denys Vlasenko81071e62014-02-28 15:42:10 +0100491 /* FIXME: ...........^^^^^
492 * shouldn't it be "global_pos + buffer_pos < header.dst_size"?
Denys Vlasenko590402b2017-01-09 14:28:25 +0100493 * It probably should, but it is a "do we accidentally
494 * unpack more bytes than expected?" check - which
495 * never happens for well-formed compression data...
Denys Vlasenko81071e62014-02-28 15:42:10 +0100496 */
Rob Landleyc1d69902006-01-20 18:28:50 +0000497 }
498 }
499
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000500 {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000501 IF_NOT_DESKTOP(int total_written = 0; /* success */)
502 IF_DESKTOP(total_written += buffer_pos;)
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100503 if (transformer_write(xstate, buffer, buffer_pos) != (ssize_t)buffer_pos) {
Denis Vlasenko33762982007-01-05 14:04:47 +0000504 bad:
Denys Vlasenko77bf05d2018-04-24 13:49:12 +0200505 /* One of our users, bbunpack(), expects _us_ to emit
506 * the error message (since it's the best place to give
507 * potentially more detailed information).
508 * Do not fail silently.
509 */
510 bb_error_msg("corrupted data");
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000511 total_written = -1; /* failure */
512 }
Denis Vlasenko33762982007-01-05 14:04:47 +0000513 rc_free(rc);
Denis Vlasenko2bbdda02008-06-27 15:48:45 +0000514 free(p);
515 free(buffer);
516 return total_written;
Denis Vlasenko33762982007-01-05 14:04:47 +0000517 }
Rob Landleyc1d69902006-01-20 18:28:50 +0000518}