1 /* Lzma decompressor for Linux kernel. Shamelessly snarfed
4 *Linux kernel adaptation
5 *Copyright (C) 2006 Alain < alain@knaff.lu >
7 *Based on small lzma deflate implementation/Small range coder
8 *implementation for lzma.
9 *Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
11 *Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
12 *Copyright (C) 1999-2005 Igor Pavlov
14 *Copyrights of the parts, see headers below.
17 *This program is free software; you can redistribute it and/or
18 *modify it under the terms of the GNU Lesser General Public
19 *License as published by the Free Software Foundation; either
20 *version 2.1 of the License, or (at your option) any later version.
22 *This program is distributed in the hope that it will be useful,
23 *but WITHOUT ANY WARRANTY; without even the implied warranty of
24 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 *Lesser General Public License for more details.
27 *You should have received a copy of the GNU Lesser General Public
28 *License along with this library; if not, write to the Free Software
29 *Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include <linux/decompress/unlzma.h>
36 #include <linux/decompress/mm.h>
38 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
40 static long long INIT read_int(unsigned char *ptr, int size)
45 for (i = 0; i < size; i++)
46 ret = (ret << 8) | ptr[size-i-1];
50 #define ENDIAN_CONVERT(x) \
51 x = (typeof(x))read_int((unsigned char *)&x, sizeof(x))
54 /* Small range coder implementation for lzma.
55 *Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
57 *Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
58 *Copyright (c) 1999-2005 Igor Pavlov
61 #include <linux/compiler.h>
63 #define LZMA_IOBUF_SIZE 0x10000
66 int (*fill)(void*, unsigned int);
77 #define RC_TOP_BITS 24
78 #define RC_MOVE_BITS 5
79 #define RC_MODEL_TOTAL_BITS 11
82 /* Called twice: once at startup and once in rc_normalize() */
83 static void INIT rc_read(struct rc *rc)
85 rc->buffer_size = rc->fill((char *)rc->buffer, LZMA_IOBUF_SIZE);
86 if (rc->buffer_size <= 0)
87 error("unexpected EOF");
89 rc->buffer_end = rc->buffer + rc->buffer_size;
93 static inline void INIT rc_init(struct rc *rc,
94 int (*fill)(void*, unsigned int),
95 char *buffer, int buffer_size)
98 rc->buffer = (uint8_t *)buffer;
99 rc->buffer_size = buffer_size;
100 rc->buffer_end = rc->buffer + rc->buffer_size;
101 rc->ptr = rc->buffer;
104 rc->range = 0xFFFFFFFF;
107 static inline void INIT rc_init_code(struct rc *rc)
111 for (i = 0; i < 5; i++) {
112 if (rc->ptr >= rc->buffer_end)
114 rc->code = (rc->code << 8) | *rc->ptr++;
119 /* Called once. TODO: bb_maybe_free() */
120 static inline void INIT rc_free(struct rc *rc)
125 /* Called twice, but one callsite is in inline'd rc_is_bit_0_helper() */
126 static void INIT rc_do_normalize(struct rc *rc)
128 if (rc->ptr >= rc->buffer_end)
131 rc->code = (rc->code << 8) | *rc->ptr++;
133 static inline void INIT rc_normalize(struct rc *rc)
135 if (rc->range < (1 << RC_TOP_BITS))
140 /* Why rc_is_bit_0_helper exists?
141 *Because we want to always expose (rc->code < rc->bound) to optimizer
143 static inline uint32_t INIT rc_is_bit_0_helper(struct rc *rc, uint16_t *p)
146 rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
149 static inline int INIT rc_is_bit_0(struct rc *rc, uint16_t *p)
151 uint32_t t = rc_is_bit_0_helper(rc, p);
155 /* Called ~10 times, but very small, thus inlined */
156 static inline void INIT rc_update_bit_0(struct rc *rc, uint16_t *p)
158 rc->range = rc->bound;
159 *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
161 static inline void rc_update_bit_1(struct rc *rc, uint16_t *p)
163 rc->range -= rc->bound;
164 rc->code -= rc->bound;
165 *p -= *p >> RC_MOVE_BITS;
168 /* Called 4 times in unlzma loop */
169 static int INIT rc_get_bit(struct rc *rc, uint16_t *p, int *symbol)
171 if (rc_is_bit_0(rc, p)) {
172 rc_update_bit_0(rc, p);
176 rc_update_bit_1(rc, p);
177 *symbol = *symbol * 2 + 1;
183 static inline int INIT rc_direct_bit(struct rc *rc)
187 if (rc->code >= rc->range) {
188 rc->code -= rc->range;
195 static inline void INIT
196 rc_bit_tree_decode(struct rc *rc, uint16_t *p, int num_levels, int *symbol)
202 rc_get_bit(rc, p + *symbol, symbol);
203 *symbol -= 1 << num_levels;
208 * Small lzma deflate implementation.
209 * Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
211 * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
212 * Copyright (C) 1999-2005 Igor Pavlov
220 } __attribute__ ((packed)) ;
223 #define LZMA_BASE_SIZE 1846
224 #define LZMA_LIT_SIZE 768
226 #define LZMA_NUM_POS_BITS_MAX 4
228 #define LZMA_LEN_NUM_LOW_BITS 3
229 #define LZMA_LEN_NUM_MID_BITS 3
230 #define LZMA_LEN_NUM_HIGH_BITS 8
232 #define LZMA_LEN_CHOICE 0
233 #define LZMA_LEN_CHOICE_2 (LZMA_LEN_CHOICE + 1)
234 #define LZMA_LEN_LOW (LZMA_LEN_CHOICE_2 + 1)
235 #define LZMA_LEN_MID (LZMA_LEN_LOW \
236 + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS)))
237 #define LZMA_LEN_HIGH (LZMA_LEN_MID \
238 +(1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS)))
239 #define LZMA_NUM_LEN_PROBS (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS))
241 #define LZMA_NUM_STATES 12
242 #define LZMA_NUM_LIT_STATES 7
244 #define LZMA_START_POS_MODEL_INDEX 4
245 #define LZMA_END_POS_MODEL_INDEX 14
246 #define LZMA_NUM_FULL_DISTANCES (1 << (LZMA_END_POS_MODEL_INDEX >> 1))
248 #define LZMA_NUM_POS_SLOT_BITS 6
249 #define LZMA_NUM_LEN_TO_POS_STATES 4
251 #define LZMA_NUM_ALIGN_BITS 4
253 #define LZMA_MATCH_MIN_LEN 2
255 #define LZMA_IS_MATCH 0
256 #define LZMA_IS_REP (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
257 #define LZMA_IS_REP_G0 (LZMA_IS_REP + LZMA_NUM_STATES)
258 #define LZMA_IS_REP_G1 (LZMA_IS_REP_G0 + LZMA_NUM_STATES)
259 #define LZMA_IS_REP_G2 (LZMA_IS_REP_G1 + LZMA_NUM_STATES)
260 #define LZMA_IS_REP_0_LONG (LZMA_IS_REP_G2 + LZMA_NUM_STATES)
261 #define LZMA_POS_SLOT (LZMA_IS_REP_0_LONG \
262 + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
263 #define LZMA_SPEC_POS (LZMA_POS_SLOT \
264 +(LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS))
265 #define LZMA_ALIGN (LZMA_SPEC_POS \
266 + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX)
267 #define LZMA_LEN_CODER (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS))
268 #define LZMA_REP_LEN_CODER (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS)
269 #define LZMA_LITERAL (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS)
274 uint8_t previous_byte;
278 int(*flush)(void*, unsigned int);
279 struct lzma_header *header;
284 uint32_t rep0, rep1, rep2, rep3;
287 static inline size_t INIT get_pos(struct writer *wr)
290 wr->global_pos + wr->buffer_pos;
293 static inline uint8_t INIT peek_old_byte(struct writer *wr,
298 while (offs > wr->header->dict_size)
299 offs -= wr->header->dict_size;
300 pos = wr->buffer_pos - offs;
301 return wr->buffer[pos];
303 uint32_t pos = wr->buffer_pos - offs;
304 while (pos >= wr->header->dict_size)
305 pos += wr->header->dict_size;
306 return wr->buffer[pos];
311 static inline void INIT write_byte(struct writer *wr, uint8_t byte)
313 wr->buffer[wr->buffer_pos++] = wr->previous_byte = byte;
314 if (wr->flush && wr->buffer_pos == wr->header->dict_size) {
316 wr->global_pos += wr->header->dict_size;
317 wr->flush((char *)wr->buffer, wr->header->dict_size);
322 static inline void INIT copy_byte(struct writer *wr, uint32_t offs)
324 write_byte(wr, peek_old_byte(wr, offs));
327 static inline void INIT copy_bytes(struct writer *wr,
328 uint32_t rep0, int len)
333 } while (len != 0 && wr->buffer_pos < wr->header->dst_size);
336 static inline void INIT process_bit0(struct writer *wr, struct rc *rc,
337 struct cstate *cst, uint16_t *p,
338 int pos_state, uint16_t *prob,
339 int lc, uint32_t literal_pos_mask) {
341 rc_update_bit_0(rc, prob);
342 prob = (p + LZMA_LITERAL +
344 * (((get_pos(wr) & literal_pos_mask) << lc)
345 + (wr->previous_byte >> (8 - lc))))
348 if (cst->state >= LZMA_NUM_LIT_STATES) {
349 int match_byte = peek_old_byte(wr, cst->rep0);
355 bit = match_byte & 0x100;
356 prob_lit = prob + 0x100 + bit + mi;
357 if (rc_get_bit(rc, prob_lit, &mi)) {
364 } while (mi < 0x100);
367 uint16_t *prob_lit = prob + mi;
368 rc_get_bit(rc, prob_lit, &mi);
373 else if (cst->state < 10)
379 static inline void INIT process_bit1(struct writer *wr, struct rc *rc,
380 struct cstate *cst, uint16_t *p,
381 int pos_state, uint16_t *prob) {
387 rc_update_bit_1(rc, prob);
388 prob = p + LZMA_IS_REP + cst->state;
389 if (rc_is_bit_0(rc, prob)) {
390 rc_update_bit_0(rc, prob);
391 cst->rep3 = cst->rep2;
392 cst->rep2 = cst->rep1;
393 cst->rep1 = cst->rep0;
394 cst->state = cst->state < LZMA_NUM_LIT_STATES ? 0 : 3;
395 prob = p + LZMA_LEN_CODER;
397 rc_update_bit_1(rc, prob);
398 prob = p + LZMA_IS_REP_G0 + cst->state;
399 if (rc_is_bit_0(rc, prob)) {
400 rc_update_bit_0(rc, prob);
401 prob = (p + LZMA_IS_REP_0_LONG
403 LZMA_NUM_POS_BITS_MAX) +
405 if (rc_is_bit_0(rc, prob)) {
406 rc_update_bit_0(rc, prob);
408 cst->state = cst->state < LZMA_NUM_LIT_STATES ?
410 copy_byte(wr, cst->rep0);
413 rc_update_bit_1(rc, prob);
418 rc_update_bit_1(rc, prob);
419 prob = p + LZMA_IS_REP_G1 + cst->state;
420 if (rc_is_bit_0(rc, prob)) {
421 rc_update_bit_0(rc, prob);
422 distance = cst->rep1;
424 rc_update_bit_1(rc, prob);
425 prob = p + LZMA_IS_REP_G2 + cst->state;
426 if (rc_is_bit_0(rc, prob)) {
427 rc_update_bit_0(rc, prob);
428 distance = cst->rep2;
430 rc_update_bit_1(rc, prob);
431 distance = cst->rep3;
432 cst->rep3 = cst->rep2;
434 cst->rep2 = cst->rep1;
436 cst->rep1 = cst->rep0;
437 cst->rep0 = distance;
439 cst->state = cst->state < LZMA_NUM_LIT_STATES ? 8 : 11;
440 prob = p + LZMA_REP_LEN_CODER;
443 prob_len = prob + LZMA_LEN_CHOICE;
444 if (rc_is_bit_0(rc, prob_len)) {
445 rc_update_bit_0(rc, prob_len);
446 prob_len = (prob + LZMA_LEN_LOW
448 LZMA_LEN_NUM_LOW_BITS));
450 num_bits = LZMA_LEN_NUM_LOW_BITS;
452 rc_update_bit_1(rc, prob_len);
453 prob_len = prob + LZMA_LEN_CHOICE_2;
454 if (rc_is_bit_0(rc, prob_len)) {
455 rc_update_bit_0(rc, prob_len);
456 prob_len = (prob + LZMA_LEN_MID
458 LZMA_LEN_NUM_MID_BITS));
459 offset = 1 << LZMA_LEN_NUM_LOW_BITS;
460 num_bits = LZMA_LEN_NUM_MID_BITS;
462 rc_update_bit_1(rc, prob_len);
463 prob_len = prob + LZMA_LEN_HIGH;
464 offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
465 + (1 << LZMA_LEN_NUM_MID_BITS));
466 num_bits = LZMA_LEN_NUM_HIGH_BITS;
470 rc_bit_tree_decode(rc, prob_len, num_bits, &len);
473 if (cst->state < 4) {
476 cst->state += LZMA_NUM_LIT_STATES;
480 LZMA_NUM_LEN_TO_POS_STATES ? len :
481 LZMA_NUM_LEN_TO_POS_STATES - 1)
482 << LZMA_NUM_POS_SLOT_BITS);
483 rc_bit_tree_decode(rc, prob,
484 LZMA_NUM_POS_SLOT_BITS,
486 if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
488 num_bits = (pos_slot >> 1) - 1;
489 cst->rep0 = 2 | (pos_slot & 1);
490 if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
491 cst->rep0 <<= num_bits;
492 prob = p + LZMA_SPEC_POS +
493 cst->rep0 - pos_slot - 1;
495 num_bits -= LZMA_NUM_ALIGN_BITS;
497 cst->rep0 = (cst->rep0 << 1) |
499 prob = p + LZMA_ALIGN;
500 cst->rep0 <<= LZMA_NUM_ALIGN_BITS;
501 num_bits = LZMA_NUM_ALIGN_BITS;
506 if (rc_get_bit(rc, prob + mi, &mi))
511 cst->rep0 = pos_slot;
512 if (++(cst->rep0) == 0)
516 len += LZMA_MATCH_MIN_LEN;
518 copy_bytes(wr, cst->rep0, len);
523 STATIC inline int INIT unlzma(unsigned char *buf, int in_len,
524 int(*fill)(void*, unsigned int),
525 int(*flush)(void*, unsigned int),
526 unsigned char *output,
528 void(*error_fn)(char *x)
531 struct lzma_header header;
533 uint32_t pos_state_mask;
534 uint32_t literal_pos_mask;
541 unsigned char *inbuf;
544 set_error_fn(error_fn);
546 in_len -= 4; /* Uncompressed size hack active in pre-boot
551 inbuf = malloc(LZMA_IOBUF_SIZE);
553 error("Could not allocate input bufer");
558 cst.rep0 = cst.rep1 = cst.rep2 = cst.rep3 = 1;
563 wr.previous_byte = 0;
566 rc_init(&rc, fill, inbuf, in_len);
568 for (i = 0; i < sizeof(header); i++) {
569 if (rc.ptr >= rc.buffer_end)
571 ((unsigned char *)&header)[i] = *rc.ptr++;
574 if (header.pos >= (9 * 5 * 5))
589 pos_state_mask = (1 << pb) - 1;
590 literal_pos_mask = (1 << lp) - 1;
592 ENDIAN_CONVERT(header.dict_size);
593 ENDIAN_CONVERT(header.dst_size);
595 if (header.dict_size == 0)
596 header.dict_size = 1;
601 wr.bufsize = MIN(header.dst_size, header.dict_size);
602 wr.buffer = large_malloc(wr.bufsize);
604 if (wr.buffer == NULL)
607 num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
608 p = (uint16_t *) large_malloc(num_probs * sizeof(*p));
611 num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp));
612 for (i = 0; i < num_probs; i++)
613 p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
617 while (get_pos(&wr) < header.dst_size) {
618 int pos_state = get_pos(&wr) & pos_state_mask;
619 uint16_t *prob = p + LZMA_IS_MATCH +
620 (cst.state << LZMA_NUM_POS_BITS_MAX) + pos_state;
621 if (rc_is_bit_0(&rc, prob))
622 process_bit0(&wr, &rc, &cst, p, pos_state, prob,
623 lc, literal_pos_mask);
625 process_bit1(&wr, &rc, &cst, p, pos_state, prob);
632 *posp = rc.ptr-rc.buffer;
634 wr.flush(wr.buffer, wr.buffer_pos);
639 large_free(wr.buffer);
647 #define decompress unlzma