decompress_unlzma: make "fast" version a bit smaller
[oweals/busybox.git] / archival / libarchive / decompress_unlzma.c
1 /* vi: set sw=4 ts=4: */
2 /*
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  *
9  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10  */
11 #include "libbb.h"
12 #include "bb_archive.h"
13
14 #if ENABLE_FEATURE_LZMA_FAST
15 #  define speed_inline ALWAYS_INLINE
16 #  define size_inline
17 #else
18 #  define speed_inline
19 #  define size_inline ALWAYS_INLINE
20 #endif
21
22
23 typedef struct {
24         int fd;
25         uint8_t *ptr;
26
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
32         uint8_t *buffer_end;
33
34 /* Had provisions for variable buffer, but we don't need it here */
35         /* int buffer_size; */
36 #define RC_BUFFER_SIZE 0x10000
37
38         uint32_t code;
39         uint32_t range;
40         uint32_t bound;
41 } rc_t;
42
43 #define RC_TOP_BITS 24
44 #define RC_MOVE_BITS 5
45 #define RC_MODEL_TOTAL_BITS 11
46
47
48 /* Called once in rc_do_normalize() */
49 static void rc_read(rc_t *rc)
50 {
51         int buffer_size = safe_read(rc->fd, RC_BUFFER, RC_BUFFER_SIZE);
52 //TODO: return -1 instead
53 //This will make unlzma delete broken unpacked file on unpack errors
54         if (buffer_size <= 0)
55                 bb_error_msg_and_die("unexpected EOF");
56         rc->buffer_end = RC_BUFFER + buffer_size;
57         rc->ptr = RC_BUFFER;
58 }
59
60 /* Called twice, but one callsite is in speed_inline'd rc_is_bit_1() */
61 static 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
69 /* Called once */
70 static ALWAYS_INLINE rc_t* rc_init(int fd) /*, int buffer_size) */
71 {
72         int i;
73         rc_t *rc;
74
75         rc = xzalloc(sizeof(*rc) + RC_BUFFER_SIZE);
76
77         rc->fd = fd;
78         /* rc->ptr = rc->buffer_end; */
79
80         for (i = 0; i < 5; i++) {
81                 rc_do_normalize(rc);
82         }
83         rc->range = 0xffffffff;
84         return rc;
85 }
86
87 /* Called once  */
88 static ALWAYS_INLINE void rc_free(rc_t *rc)
89 {
90         free(rc);
91 }
92
93 static ALWAYS_INLINE void rc_normalize(rc_t *rc)
94 {
95         if (rc->range < (1 << RC_TOP_BITS)) {
96                 rc_do_normalize(rc);
97         }
98 }
99
100 /* rc_is_bit_1 is called 9 times */
101 static speed_inline int rc_is_bit_1(rc_t *rc, uint16_t *p)
102 {
103         rc_normalize(rc);
104         rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
105         if (rc->code < rc->bound) {
106                 rc->range = rc->bound;
107                 *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
108                 return 0;
109         }
110         rc->range -= rc->bound;
111         rc->code -= rc->bound;
112         *p -= *p >> RC_MOVE_BITS;
113         return 1;
114 }
115
116 /* Called 4 times in unlzma loop */
117 static speed_inline int rc_get_bit(rc_t *rc, uint16_t *p, int *symbol)
118 {
119         int ret = rc_is_bit_1(rc, p);
120         *symbol = *symbol * 2 + ret;
121         return ret;
122 }
123
124 /* Called once */
125 static ALWAYS_INLINE int rc_direct_bit(rc_t *rc)
126 {
127         rc_normalize(rc);
128         rc->range >>= 1;
129         if (rc->code >= rc->range) {
130                 rc->code -= rc->range;
131                 return 1;
132         }
133         return 0;
134 }
135
136 /* Called twice */
137 static speed_inline void
138 rc_bit_tree_decode(rc_t *rc, uint16_t *p, int num_levels, int *symbol)
139 {
140         int i = num_levels;
141
142         *symbol = 1;
143         while (i--)
144                 rc_get_bit(rc, p + *symbol, symbol);
145         *symbol -= 1 << num_levels;
146 }
147
148
149 typedef struct {
150         uint8_t pos;
151         uint32_t dict_size;
152         uint64_t dst_size;
153 } PACKED lzma_header_t;
154
155
156 /* #defines will force compiler to compute/optimize each one with each usage.
157  * Have heart and use enum instead. */
158 enum {
159         LZMA_BASE_SIZE = 1846,
160         LZMA_LIT_SIZE  = 768,
161
162         LZMA_NUM_POS_BITS_MAX = 4,
163
164         LZMA_LEN_NUM_LOW_BITS  = 3,
165         LZMA_LEN_NUM_MID_BITS  = 3,
166         LZMA_LEN_NUM_HIGH_BITS = 8,
167
168         LZMA_LEN_CHOICE     = 0,
169         LZMA_LEN_CHOICE_2   = (LZMA_LEN_CHOICE + 1),
170         LZMA_LEN_LOW        = (LZMA_LEN_CHOICE_2 + 1),
171         LZMA_LEN_MID        = (LZMA_LEN_LOW \
172                               + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS))),
173         LZMA_LEN_HIGH       = (LZMA_LEN_MID \
174                               + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS))),
175         LZMA_NUM_LEN_PROBS  = (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS)),
176
177         LZMA_NUM_STATES     = 12,
178         LZMA_NUM_LIT_STATES = 7,
179
180         LZMA_START_POS_MODEL_INDEX = 4,
181         LZMA_END_POS_MODEL_INDEX   = 14,
182         LZMA_NUM_FULL_DISTANCES    = (1 << (LZMA_END_POS_MODEL_INDEX >> 1)),
183
184         LZMA_NUM_POS_SLOT_BITS = 6,
185         LZMA_NUM_LEN_TO_POS_STATES = 4,
186
187         LZMA_NUM_ALIGN_BITS = 4,
188
189         LZMA_MATCH_MIN_LEN  = 2,
190
191         LZMA_IS_MATCH       = 0,
192         LZMA_IS_REP         = (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)),
193         LZMA_IS_REP_G0      = (LZMA_IS_REP + LZMA_NUM_STATES),
194         LZMA_IS_REP_G1      = (LZMA_IS_REP_G0 + LZMA_NUM_STATES),
195         LZMA_IS_REP_G2      = (LZMA_IS_REP_G1 + LZMA_NUM_STATES),
196         LZMA_IS_REP_0_LONG  = (LZMA_IS_REP_G2 + LZMA_NUM_STATES),
197         LZMA_POS_SLOT       = (LZMA_IS_REP_0_LONG \
198                               + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)),
199         LZMA_SPEC_POS       = (LZMA_POS_SLOT \
200                               + (LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS)),
201         LZMA_ALIGN          = (LZMA_SPEC_POS \
202                               + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX),
203         LZMA_LEN_CODER      = (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS)),
204         LZMA_REP_LEN_CODER  = (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS),
205         LZMA_LITERAL        = (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS),
206 };
207
208
209 IF_DESKTOP(long long) int FAST_FUNC
210 unpack_lzma_stream(transformer_aux_data_t *aux UNUSED_PARAM, int src_fd, int dst_fd)
211 {
212         IF_DESKTOP(long long total_written = 0;)
213         lzma_header_t header;
214         int lc, pb, lp;
215         uint32_t pos_state_mask;
216         uint32_t literal_pos_mask;
217         uint16_t *p;
218         int num_bits;
219         int num_probs;
220         rc_t *rc;
221         int i;
222         uint8_t *buffer;
223         uint8_t previous_byte = 0;
224         size_t buffer_pos = 0, global_pos = 0;
225         int len = 0;
226         int state = 0;
227         uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
228
229         if (full_read(src_fd, &header, sizeof(header)) != sizeof(header)
230          || header.pos >= (9 * 5 * 5)
231         ) {
232                 bb_error_msg("bad lzma header");
233                 return -1;
234         }
235
236         i = header.pos / 9;
237         lc = header.pos % 9;
238         pb = i / 5;
239         lp = i % 5;
240         pos_state_mask = (1 << pb) - 1;
241         literal_pos_mask = (1 << lp) - 1;
242
243         header.dict_size = SWAP_LE32(header.dict_size);
244         header.dst_size = SWAP_LE64(header.dst_size);
245
246         if (header.dict_size == 0)
247                 header.dict_size++;
248
249         buffer = xmalloc(MIN(header.dst_size, header.dict_size));
250
251         num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
252         p = xmalloc(num_probs * sizeof(*p));
253         num_probs += LZMA_LITERAL - LZMA_BASE_SIZE;
254         for (i = 0; i < num_probs; i++)
255                 p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
256
257         rc = rc_init(src_fd); /*, RC_BUFFER_SIZE); */
258
259         while (global_pos + buffer_pos < header.dst_size) {
260                 int pos_state = (buffer_pos + global_pos) & pos_state_mask;
261                 uint16_t *prob = p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
262
263                 if (!rc_is_bit_1(rc, prob)) {
264                         static const char next_state[LZMA_NUM_STATES] =
265                                 { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
266                         int mi = 1;
267
268                         prob = (p + LZMA_LITERAL
269                                 + (LZMA_LIT_SIZE * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
270                                                     + (previous_byte >> (8 - lc))
271                                                    )
272                                   )
273                         );
274
275                         if (state >= LZMA_NUM_LIT_STATES) {
276                                 int match_byte;
277                                 uint32_t pos = buffer_pos - rep0;
278
279                                 while (pos >= header.dict_size)
280                                         pos += header.dict_size;
281                                 match_byte = buffer[pos];
282                                 do {
283                                         int bit;
284
285                                         match_byte <<= 1;
286                                         bit = match_byte & 0x100;
287                                         bit ^= (rc_get_bit(rc, prob + 0x100 + bit + mi, &mi) << 8); /* 0x100 or 0 */
288                                         if (bit)
289                                                 break;
290                                 } while (mi < 0x100);
291                         }
292                         while (mi < 0x100) {
293                                 rc_get_bit(rc, prob + mi, &mi);
294                         }
295
296                         state = next_state[state];
297
298                         previous_byte = (uint8_t) mi;
299 #if ENABLE_FEATURE_LZMA_FAST
300  one_byte1:
301                         buffer[buffer_pos++] = previous_byte;
302                         if (buffer_pos == header.dict_size) {
303                                 buffer_pos = 0;
304                                 global_pos += header.dict_size;
305                                 if (full_write(dst_fd, buffer, header.dict_size) != (ssize_t)header.dict_size)
306                                         goto bad;
307                                 IF_DESKTOP(total_written += header.dict_size;)
308                         }
309 #else
310                         len = 1;
311                         goto one_byte2;
312 #endif
313                 } else {
314                         int offset;
315                         uint16_t *prob2;
316 #define prob_len prob2
317
318                         prob2 = p + LZMA_IS_REP + state;
319                         if (!rc_is_bit_1(rc, prob2)) {
320                                 rep3 = rep2;
321                                 rep2 = rep1;
322                                 rep1 = rep0;
323                                 state = state < LZMA_NUM_LIT_STATES ? 0 : 3;
324                                 prob2 = p + LZMA_LEN_CODER;
325                         } else {
326                                 prob2 += LZMA_IS_REP_G0 - LZMA_IS_REP;
327                                 if (!rc_is_bit_1(rc, prob2)) {
328                                         prob2 = (p + LZMA_IS_REP_0_LONG
329                                                 + (state << LZMA_NUM_POS_BITS_MAX)
330                                                 + pos_state
331                                         );
332                                         if (!rc_is_bit_1(rc, prob2)) {
333 #if ENABLE_FEATURE_LZMA_FAST
334                                                 uint32_t pos = buffer_pos - rep0;
335                                                 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
336                                                 while (pos >= header.dict_size)
337                                                         pos += header.dict_size;
338                                                 previous_byte = buffer[pos];
339                                                 goto one_byte1;
340 #else
341                                                 state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
342                                                 len = 1;
343                                                 goto string;
344 #endif
345                                         }
346                                 } else {
347                                         uint32_t distance;
348
349                                         prob2 += LZMA_IS_REP_G1 - LZMA_IS_REP_G0;
350                                         distance = rep1;
351                                         if (rc_is_bit_1(rc, prob2)) {
352                                                 prob2 += LZMA_IS_REP_G2 - LZMA_IS_REP_G1;
353                                                 distance = rep2;
354                                                 if (rc_is_bit_1(rc, prob2)) {
355                                                         distance = rep3;
356                                                         rep3 = rep2;
357                                                 }
358                                                 rep2 = rep1;
359                                         }
360                                         rep1 = rep0;
361                                         rep0 = distance;
362                                 }
363                                 state = state < LZMA_NUM_LIT_STATES ? 8 : 11;
364                                 prob2 = p + LZMA_REP_LEN_CODER;
365                         }
366
367                         prob_len = prob2 + LZMA_LEN_CHOICE;
368                         num_bits = LZMA_LEN_NUM_LOW_BITS;
369                         if (!rc_is_bit_1(rc, prob_len)) {
370                                 prob_len += LZMA_LEN_LOW - LZMA_LEN_CHOICE
371                                             + (pos_state << LZMA_LEN_NUM_LOW_BITS);
372                                 offset = 0;
373                         } else {
374                                 prob_len += LZMA_LEN_CHOICE_2 - LZMA_LEN_CHOICE;
375                                 if (!rc_is_bit_1(rc, prob_len)) {
376                                         prob_len += LZMA_LEN_MID - LZMA_LEN_CHOICE_2
377                                                     + (pos_state << LZMA_LEN_NUM_MID_BITS);
378                                         offset = 1 << LZMA_LEN_NUM_LOW_BITS;
379                                         num_bits += LZMA_LEN_NUM_MID_BITS - LZMA_LEN_NUM_LOW_BITS;
380                                 } else {
381                                         prob_len += LZMA_LEN_HIGH - LZMA_LEN_CHOICE_2;
382                                         offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
383                                                   + (1 << LZMA_LEN_NUM_MID_BITS));
384                                         num_bits += LZMA_LEN_NUM_HIGH_BITS - LZMA_LEN_NUM_LOW_BITS;
385                                 }
386                         }
387                         rc_bit_tree_decode(rc, prob_len, num_bits, &len);
388                         len += offset;
389
390                         if (state < 4) {
391                                 int pos_slot;
392                                 uint16_t *prob3;
393
394                                 state += LZMA_NUM_LIT_STATES;
395                                 prob3 = p + LZMA_POS_SLOT +
396                                        ((len < LZMA_NUM_LEN_TO_POS_STATES ? len :
397                                          LZMA_NUM_LEN_TO_POS_STATES - 1)
398                                          << LZMA_NUM_POS_SLOT_BITS);
399                                 rc_bit_tree_decode(rc, prob3,
400                                         LZMA_NUM_POS_SLOT_BITS, &pos_slot);
401                                 rep0 = pos_slot;
402                                 if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
403                                         int i2, mi2, num_bits2 = (pos_slot >> 1) - 1;
404                                         rep0 = 2 | (pos_slot & 1);
405                                         if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
406                                                 rep0 <<= num_bits2;
407                                                 prob3 = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
408                                         } else {
409                                                 for (; num_bits2 != LZMA_NUM_ALIGN_BITS; num_bits2--)
410                                                         rep0 = (rep0 << 1) | rc_direct_bit(rc);
411                                                 rep0 <<= LZMA_NUM_ALIGN_BITS;
412                                                 prob3 = p + LZMA_ALIGN;
413                                         }
414                                         i2 = 1;
415                                         mi2 = 1;
416                                         while (num_bits2--) {
417                                                 if (rc_get_bit(rc, prob3 + mi2, &mi2))
418                                                         rep0 |= i2;
419                                                 i2 <<= 1;
420                                         }
421                                 }
422                                 if (++rep0 == 0)
423                                         break;
424                         }
425
426                         len += LZMA_MATCH_MIN_LEN;
427  IF_NOT_FEATURE_LZMA_FAST(string:)
428                         do {
429                                 uint32_t pos = buffer_pos - rep0;
430                                 while (pos >= header.dict_size)
431                                         pos += header.dict_size;
432                                 previous_byte = buffer[pos];
433  IF_NOT_FEATURE_LZMA_FAST(one_byte2:)
434                                 buffer[buffer_pos++] = previous_byte;
435                                 if (buffer_pos == header.dict_size) {
436                                         buffer_pos = 0;
437                                         global_pos += header.dict_size;
438                                         if (full_write(dst_fd, buffer, header.dict_size) != (ssize_t)header.dict_size)
439                                                 goto bad;
440                                         IF_DESKTOP(total_written += header.dict_size;)
441                                 }
442                                 len--;
443                         } while (len != 0 && buffer_pos < header.dst_size);
444                 }
445         }
446
447         {
448                 IF_NOT_DESKTOP(int total_written = 0; /* success */)
449                 IF_DESKTOP(total_written += buffer_pos;)
450                 if (full_write(dst_fd, buffer, buffer_pos) != (ssize_t)buffer_pos) {
451  bad:
452                         total_written = -1; /* failure */
453                 }
454                 rc_free(rc);
455                 free(p);
456                 free(buffer);
457                 return total_written;
458         }
459 }