blob: a6902807be2dd8e2099d86b37ddbccfd80e8e360 [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 */
11
Rob Landleyc1d69902006-01-20 18:28:50 +000012#include "libbb.h"
Bernhard Reutner-Fischercfb53df2006-04-02 21:50:01 +000013#include "unarchive.h"
14
Rob Landleyb13fee42006-06-20 22:38:00 +000015#ifdef CONFIG_FEATURE_LZMA_FAST
16# define speed_inline ATTRIBUTE_ALWAYS_INLINE
17#else
18# define speed_inline
19#endif
20
21
22typedef struct {
23 int fd;
24 uint8_t *ptr;
25 uint8_t *buffer;
26 uint8_t *buffer_end;
27 int buffer_size;
28 uint32_t code;
29 uint32_t range;
30 uint32_t bound;
31} rc_t;
32
33
34#define RC_TOP_BITS 24
35#define RC_MOVE_BITS 5
36#define RC_MODEL_TOTAL_BITS 11
37
38
39/* Called twice: once at startup and once in rc_normalize() */
40static void rc_read(rc_t * rc)
41{
42 rc->buffer_size = read(rc->fd, rc->buffer, rc->buffer_size);
43 if (rc->buffer_size <= 0)
44 bb_error_msg_and_die("unexpected EOF");
45 rc->ptr = rc->buffer;
46 rc->buffer_end = rc->buffer + rc->buffer_size;
47}
48
49/* Called once */
50static void rc_init(rc_t * rc, int fd, int buffer_size)
51{
52 int i;
53
54 rc->fd = fd;
55 rc->buffer = xmalloc(buffer_size);
56 rc->buffer_size = buffer_size;
57 rc->buffer_end = rc->buffer + rc->buffer_size;
58 rc->ptr = rc->buffer_end;
59
60 rc->code = 0;
61 rc->range = 0xFFFFFFFF;
62 for (i = 0; i < 5; i++) {
63 if (rc->ptr >= rc->buffer_end)
64 rc_read(rc);
65 rc->code = (rc->code << 8) | *rc->ptr++;
66 }
67}
68
69/* Called once. TODO: bb_maybe_free() */
70static ATTRIBUTE_ALWAYS_INLINE void rc_free(rc_t * rc)
71{
72 if (ENABLE_FEATURE_CLEAN_UP)
73 free(rc->buffer);
74}
75
76/* Called twice, but one callsite is in speed_inline'd rc_is_bit_0_helper() */
77static void rc_do_normalize(rc_t * rc)
78{
79 if (rc->ptr >= rc->buffer_end)
80 rc_read(rc);
81 rc->range <<= 8;
82 rc->code = (rc->code << 8) | *rc->ptr++;
83}
84static ATTRIBUTE_ALWAYS_INLINE void rc_normalize(rc_t * rc)
85{
86 if (rc->range < (1 << RC_TOP_BITS)) {
87 rc_do_normalize(rc);
88 }
89}
90
91/* Called 9 times */
92/* Why rc_is_bit_0_helper exists?
93 * Because we want to always expose (rc->code < rc->bound) to optimizer
94 */
95static speed_inline uint32_t rc_is_bit_0_helper(rc_t * rc, uint16_t * p)
96{
97 rc_normalize(rc);
98 rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
99 return rc->bound;
100}
101static ATTRIBUTE_ALWAYS_INLINE int rc_is_bit_0(rc_t * rc, uint16_t * p)
102{
103 uint32_t t = rc_is_bit_0_helper(rc, p);
104 return rc->code < t;
105}
106
107/* Called ~10 times, but very small, thus inlined */
108static speed_inline void rc_update_bit_0(rc_t * rc, uint16_t * p)
109{
110 rc->range = rc->bound;
111 *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
112}
113static speed_inline void rc_update_bit_1(rc_t * rc, uint16_t * p)
114{
115 rc->range -= rc->bound;
116 rc->code -= rc->bound;
117 *p -= *p >> RC_MOVE_BITS;
118}
119
120/* Called 4 times in unlzma loop */
121static int rc_get_bit(rc_t * rc, uint16_t * p, int *symbol)
122{
123 if (rc_is_bit_0(rc, p)) {
124 rc_update_bit_0(rc, p);
125 *symbol *= 2;
126 return 0;
127 } else {
128 rc_update_bit_1(rc, p);
129 *symbol = *symbol * 2 + 1;
130 return 1;
131 }
132}
133
134/* Called once */
135static ATTRIBUTE_ALWAYS_INLINE int rc_direct_bit(rc_t * rc)
136{
137 rc_normalize(rc);
138 rc->range >>= 1;
139 if (rc->code >= rc->range) {
140 rc->code -= rc->range;
141 return 1;
142 }
143 return 0;
144}
145
146/* Called twice */
147static speed_inline void
148rc_bit_tree_decode(rc_t * rc, uint16_t * p, int num_levels, int *symbol)
149{
150 int i = num_levels;
151
152 *symbol = 1;
153 while (i--)
154 rc_get_bit(rc, p + *symbol, symbol);
155 *symbol -= 1 << num_levels;
156}
Rob Landleyc1d69902006-01-20 18:28:50 +0000157
158
159typedef struct {
160 uint8_t pos;
161 uint32_t dict_size;
162 uint64_t dst_size;
163} __attribute__ ((packed)) lzma_header_t;
164
165
166#define LZMA_BASE_SIZE 1846
167#define LZMA_LIT_SIZE 768
168
169#define LZMA_NUM_POS_BITS_MAX 4
170
171#define LZMA_LEN_NUM_LOW_BITS 3
172#define LZMA_LEN_NUM_MID_BITS 3
173#define LZMA_LEN_NUM_HIGH_BITS 8
174
175#define LZMA_LEN_CHOICE 0
176#define LZMA_LEN_CHOICE_2 (LZMA_LEN_CHOICE + 1)
177#define LZMA_LEN_LOW (LZMA_LEN_CHOICE_2 + 1)
178#define LZMA_LEN_MID (LZMA_LEN_LOW \
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000179 + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS)))
Rob Landleyc1d69902006-01-20 18:28:50 +0000180#define LZMA_LEN_HIGH (LZMA_LEN_MID \
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000181 +(1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS)))
Rob Landleyc1d69902006-01-20 18:28:50 +0000182#define LZMA_NUM_LEN_PROBS (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS))
183
184#define LZMA_NUM_STATES 12
185#define LZMA_NUM_LIT_STATES 7
186
187#define LZMA_START_POS_MODEL_INDEX 4
188#define LZMA_END_POS_MODEL_INDEX 14
189#define LZMA_NUM_FULL_DISTANCES (1 << (LZMA_END_POS_MODEL_INDEX >> 1))
190
191#define LZMA_NUM_POS_SLOT_BITS 6
192#define LZMA_NUM_LEN_TO_POS_STATES 4
193
194#define LZMA_NUM_ALIGN_BITS 4
195
196#define LZMA_MATCH_MIN_LEN 2
197
198#define LZMA_IS_MATCH 0
199#define LZMA_IS_REP (LZMA_IS_MATCH + (LZMA_NUM_STATES <<LZMA_NUM_POS_BITS_MAX))
200#define LZMA_IS_REP_G0 (LZMA_IS_REP + LZMA_NUM_STATES)
201#define LZMA_IS_REP_G1 (LZMA_IS_REP_G0 + LZMA_NUM_STATES)
202#define LZMA_IS_REP_G2 (LZMA_IS_REP_G1 + LZMA_NUM_STATES)
203#define LZMA_IS_REP_0_LONG (LZMA_IS_REP_G2 + LZMA_NUM_STATES)
204#define LZMA_POS_SLOT (LZMA_IS_REP_0_LONG \
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000205 + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
Rob Landleyc1d69902006-01-20 18:28:50 +0000206#define LZMA_SPEC_POS (LZMA_POS_SLOT \
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000207 +(LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS))
Rob Landleyc1d69902006-01-20 18:28:50 +0000208#define LZMA_ALIGN (LZMA_SPEC_POS \
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000209 + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX)
Rob Landleyc1d69902006-01-20 18:28:50 +0000210#define LZMA_LEN_CODER (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS))
211#define LZMA_REP_LEN_CODER (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS)
212#define LZMA_LITERAL (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS)
213
Denis Vlasenko97a8dd32006-10-01 15:55:11 +0000214USE_DESKTOP(long long) int
215unlzma(int src_fd, int dst_fd)
Rob Landleyc1d69902006-01-20 18:28:50 +0000216{
Denis Vlasenko97a8dd32006-10-01 15:55:11 +0000217 USE_DESKTOP(long long total_written = 0;)
Rob Landleyc1d69902006-01-20 18:28:50 +0000218 lzma_header_t header;
219 int lc, pb, lp;
220 uint32_t pos_state_mask;
221 uint32_t literal_pos_mask;
222 uint32_t pos;
223 uint16_t *p;
224 uint16_t *prob;
225 uint16_t *prob_lit;
226 int num_bits;
227 int num_probs;
228 rc_t rc;
229 int i, mi;
230 uint8_t *buffer;
231 uint8_t previous_byte = 0;
232 size_t buffer_pos = 0, global_pos = 0;
233 int len = 0;
234 int state = 0;
235 uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
236
237 if (read(src_fd, &header, sizeof(header)) != sizeof(header))
238 bb_error_msg_and_die("can't read header");
239
240 if (header.pos >= (9 * 5 * 5))
241 bb_error_msg_and_die("bad header");
242 mi = header.pos / 9;
243 lc = header.pos % 9;
244 pb = mi / 5;
245 lp = mi % 5;
246 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)
253 header.dict_size = 1;
254
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));
259 num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp));
260 for (i = 0; i < num_probs; i++)
261 p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
262
263 rc_init(&rc, src_fd, 0x10000);
264
265 while (global_pos + buffer_pos < header.dst_size) {
266 int pos_state = (buffer_pos + global_pos) & pos_state_mask;
267
268 prob =
269 p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
270 if (rc_is_bit_0(&rc, prob)) {
271 mi = 1;
272 rc_update_bit_0(&rc, prob);
273 prob = (p + LZMA_LITERAL + (LZMA_LIT_SIZE
274 * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
275 + (previous_byte >> (8 - lc)))));
276
277 if (state >= LZMA_NUM_LIT_STATES) {
278 int match_byte;
279
280 pos = buffer_pos - rep0;
281 while (pos >= header.dict_size)
282 pos += header.dict_size;
283 match_byte = buffer[pos];
284 do {
285 int bit;
286
287 match_byte <<= 1;
288 bit = match_byte & 0x100;
289 prob_lit = prob + 0x100 + bit + mi;
290 if (rc_get_bit(&rc, prob_lit, &mi)) {
291 if (!bit)
292 break;
293 } else {
294 if (bit)
295 break;
296 }
297 } while (mi < 0x100);
298 }
299 while (mi < 0x100) {
300 prob_lit = prob + mi;
301 rc_get_bit(&rc, prob_lit, &mi);
302 }
303 previous_byte = (uint8_t) mi;
304
305 buffer[buffer_pos++] = previous_byte;
306 if (buffer_pos == header.dict_size) {
307 buffer_pos = 0;
308 global_pos += header.dict_size;
Denis Vlasenko97a8dd32006-10-01 15:55:11 +0000309 // FIXME: error check
Rob Landleyc1d69902006-01-20 18:28:50 +0000310 write(dst_fd, buffer, header.dict_size);
Denis Vlasenko97a8dd32006-10-01 15:55:11 +0000311 USE_DESKTOP(total_written += header.dict_size;)
Rob Landleyc1d69902006-01-20 18:28:50 +0000312 }
313 if (state < 4)
314 state = 0;
315 else if (state < 10)
316 state -= 3;
317 else
318 state -= 6;
319 } else {
320 int offset;
321 uint16_t *prob_len;
322
323 rc_update_bit_1(&rc, prob);
324 prob = p + LZMA_IS_REP + state;
325 if (rc_is_bit_0(&rc, prob)) {
326 rc_update_bit_0(&rc, prob);
327 rep3 = rep2;
328 rep2 = rep1;
329 rep1 = rep0;
330 state = state < LZMA_NUM_LIT_STATES ? 0 : 3;
331 prob = p + LZMA_LEN_CODER;
332 } else {
333 rc_update_bit_1(&rc, prob);
334 prob = p + LZMA_IS_REP_G0 + state;
335 if (rc_is_bit_0(&rc, prob)) {
336 rc_update_bit_0(&rc, prob);
337 prob = (p + LZMA_IS_REP_0_LONG
338 + (state << LZMA_NUM_POS_BITS_MAX) + pos_state);
339 if (rc_is_bit_0(&rc, prob)) {
340 rc_update_bit_0(&rc, prob);
341
342 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
343 pos = buffer_pos - rep0;
344 while (pos >= header.dict_size)
345 pos += header.dict_size;
346 previous_byte = buffer[pos];
347 buffer[buffer_pos++] = previous_byte;
348 if (buffer_pos == header.dict_size) {
349 buffer_pos = 0;
350 global_pos += header.dict_size;
Denis Vlasenko97a8dd32006-10-01 15:55:11 +0000351 // FIXME: error check
Rob Landleyc1d69902006-01-20 18:28:50 +0000352 write(dst_fd, buffer, header.dict_size);
Denis Vlasenko97a8dd32006-10-01 15:55:11 +0000353 USE_DESKTOP(total_written += header.dict_size;)
Rob Landleyc1d69902006-01-20 18:28:50 +0000354 }
355 continue;
356 } else {
357 rc_update_bit_1(&rc, prob);
358 }
359 } else {
360 uint32_t distance;
361
362 rc_update_bit_1(&rc, prob);
363 prob = p + LZMA_IS_REP_G1 + state;
364 if (rc_is_bit_0(&rc, prob)) {
365 rc_update_bit_0(&rc, prob);
366 distance = rep1;
367 } else {
368 rc_update_bit_1(&rc, prob);
369 prob = p + LZMA_IS_REP_G2 + state;
370 if (rc_is_bit_0(&rc, prob)) {
371 rc_update_bit_0(&rc, prob);
372 distance = rep2;
373 } else {
374 rc_update_bit_1(&rc, prob);
375 distance = rep3;
376 rep3 = rep2;
377 }
378 rep2 = rep1;
379 }
380 rep1 = rep0;
381 rep0 = distance;
382 }
383 state = state < LZMA_NUM_LIT_STATES ? 8 : 11;
384 prob = p + LZMA_REP_LEN_CODER;
385 }
386
387 prob_len = prob + LZMA_LEN_CHOICE;
388 if (rc_is_bit_0(&rc, prob_len)) {
389 rc_update_bit_0(&rc, prob_len);
390 prob_len = (prob + LZMA_LEN_LOW
391 + (pos_state << LZMA_LEN_NUM_LOW_BITS));
392 offset = 0;
393 num_bits = LZMA_LEN_NUM_LOW_BITS;
394 } else {
395 rc_update_bit_1(&rc, prob_len);
396 prob_len = prob + LZMA_LEN_CHOICE_2;
397 if (rc_is_bit_0(&rc, prob_len)) {
398 rc_update_bit_0(&rc, prob_len);
399 prob_len = (prob + LZMA_LEN_MID
400 + (pos_state << LZMA_LEN_NUM_MID_BITS));
401 offset = 1 << LZMA_LEN_NUM_LOW_BITS;
402 num_bits = LZMA_LEN_NUM_MID_BITS;
403 } else {
404 rc_update_bit_1(&rc, prob_len);
405 prob_len = prob + LZMA_LEN_HIGH;
406 offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
407 + (1 << LZMA_LEN_NUM_MID_BITS));
408 num_bits = LZMA_LEN_NUM_HIGH_BITS;
409 }
410 }
411 rc_bit_tree_decode(&rc, prob_len, num_bits, &len);
412 len += offset;
413
414 if (state < 4) {
415 int pos_slot;
416
417 state += LZMA_NUM_LIT_STATES;
418 prob =
419 p + LZMA_POS_SLOT +
420 ((len <
421 LZMA_NUM_LEN_TO_POS_STATES ? len :
422 LZMA_NUM_LEN_TO_POS_STATES - 1)
423 << LZMA_NUM_POS_SLOT_BITS);
424 rc_bit_tree_decode(&rc, prob, LZMA_NUM_POS_SLOT_BITS,
425 &pos_slot);
426 if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
427 num_bits = (pos_slot >> 1) - 1;
428 rep0 = 2 | (pos_slot & 1);
429 if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
430 rep0 <<= num_bits;
431 prob = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
432 } else {
433 num_bits -= LZMA_NUM_ALIGN_BITS;
434 while (num_bits--)
435 rep0 = (rep0 << 1) | rc_direct_bit(&rc);
436 prob = p + LZMA_ALIGN;
437 rep0 <<= LZMA_NUM_ALIGN_BITS;
438 num_bits = LZMA_NUM_ALIGN_BITS;
439 }
440 i = 1;
441 mi = 1;
442 while (num_bits--) {
443 if (rc_get_bit(&rc, prob + mi, &mi))
444 rep0 |= i;
445 i <<= 1;
446 }
447 } else
448 rep0 = pos_slot;
449 if (++rep0 == 0)
450 break;
451 }
452
453 len += LZMA_MATCH_MIN_LEN;
454
455 do {
456 pos = buffer_pos - rep0;
457 while (pos >= header.dict_size)
458 pos += header.dict_size;
459 previous_byte = buffer[pos];
460 buffer[buffer_pos++] = previous_byte;
461 if (buffer_pos == header.dict_size) {
462 buffer_pos = 0;
463 global_pos += header.dict_size;
Denis Vlasenko97a8dd32006-10-01 15:55:11 +0000464 // FIXME: error check
Rob Landleyc1d69902006-01-20 18:28:50 +0000465 write(dst_fd, buffer, header.dict_size);
Denis Vlasenko97a8dd32006-10-01 15:55:11 +0000466 USE_DESKTOP(total_written += header.dict_size;)
Rob Landleyc1d69902006-01-20 18:28:50 +0000467 }
468 len--;
469 } while (len != 0 && buffer_pos < header.dst_size);
470 }
471 }
472
Denis Vlasenko97a8dd32006-10-01 15:55:11 +0000473 // FIXME: error check
Rob Landleyc1d69902006-01-20 18:28:50 +0000474 write(dst_fd, buffer, buffer_pos);
Denis Vlasenko97a8dd32006-10-01 15:55:11 +0000475 USE_DESKTOP(total_written += buffer_pos;)
Rob Landleyc1d69902006-01-20 18:28:50 +0000476 rc_free(&rc);
Denis Vlasenko97a8dd32006-10-01 15:55:11 +0000477 return USE_DESKTOP(total_written) + 0;
Rob Landleyc1d69902006-01-20 18:28:50 +0000478}