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 (https://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
35 #include <linux/decompress/unlzma.h>
38 #include <linux/decompress/mm.h>
41 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
44 static long long INIT
read_int(unsigned char *ptr
, int size
)
49 for (i
= 0; i
< size
; i
++)
50 ret
= (ret
<< 8) | ptr
[size
-i
-1];
54 #define ENDIAN_CONVERT(x) \
55 x = (typeof(x))read_int((unsigned char *)&x, sizeof(x))
58 /* Small range coder implementation for lzma.
59 *Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
61 *Based on LzmaDecode.c from the LZMA SDK 4.22 (https://www.7-zip.org/)
62 *Copyright (c) 1999-2005 Igor Pavlov
65 #include <linux/compiler.h>
67 #define LZMA_IOBUF_SIZE 0x10000
70 long (*fill
)(void*, unsigned long);
78 void (*error
)(char *);
82 #define RC_TOP_BITS 24
83 #define RC_MOVE_BITS 5
84 #define RC_MODEL_TOTAL_BITS 11
87 static long INIT
nofill(void *buffer
, unsigned long len
)
92 /* Called twice: once at startup and once in rc_normalize() */
93 static void INIT
rc_read(struct rc
*rc
)
95 rc
->buffer_size
= rc
->fill((char *)rc
->buffer
, LZMA_IOBUF_SIZE
);
96 if (rc
->buffer_size
<= 0)
97 rc
->error("unexpected EOF");
99 rc
->buffer_end
= rc
->buffer
+ rc
->buffer_size
;
103 static inline void INIT
rc_init(struct rc
*rc
,
104 long (*fill
)(void*, unsigned long),
105 char *buffer
, long buffer_size
)
111 rc
->buffer
= (uint8_t *)buffer
;
112 rc
->buffer_size
= buffer_size
;
113 rc
->buffer_end
= rc
->buffer
+ rc
->buffer_size
;
114 rc
->ptr
= rc
->buffer
;
117 rc
->range
= 0xFFFFFFFF;
120 static inline void INIT
rc_init_code(struct rc
*rc
)
124 for (i
= 0; i
< 5; i
++) {
125 if (rc
->ptr
>= rc
->buffer_end
)
127 rc
->code
= (rc
->code
<< 8) | *rc
->ptr
++;
132 /* Called twice, but one callsite is in inline'd rc_is_bit_0_helper() */
133 static void INIT
rc_do_normalize(struct rc
*rc
)
135 if (rc
->ptr
>= rc
->buffer_end
)
138 rc
->code
= (rc
->code
<< 8) | *rc
->ptr
++;
140 static inline void INIT
rc_normalize(struct rc
*rc
)
142 if (rc
->range
< (1 << RC_TOP_BITS
))
147 /* Why rc_is_bit_0_helper exists?
148 *Because we want to always expose (rc->code < rc->bound) to optimizer
150 static inline uint32_t INIT
rc_is_bit_0_helper(struct rc
*rc
, uint16_t *p
)
153 rc
->bound
= *p
* (rc
->range
>> RC_MODEL_TOTAL_BITS
);
156 static inline int INIT
rc_is_bit_0(struct rc
*rc
, uint16_t *p
)
158 uint32_t t
= rc_is_bit_0_helper(rc
, p
);
162 /* Called ~10 times, but very small, thus inlined */
163 static inline void INIT
rc_update_bit_0(struct rc
*rc
, uint16_t *p
)
165 rc
->range
= rc
->bound
;
166 *p
+= ((1 << RC_MODEL_TOTAL_BITS
) - *p
) >> RC_MOVE_BITS
;
168 static inline void INIT
rc_update_bit_1(struct rc
*rc
, uint16_t *p
)
170 rc
->range
-= rc
->bound
;
171 rc
->code
-= rc
->bound
;
172 *p
-= *p
>> RC_MOVE_BITS
;
175 /* Called 4 times in unlzma loop */
176 static int INIT
rc_get_bit(struct rc
*rc
, uint16_t *p
, int *symbol
)
178 if (rc_is_bit_0(rc
, p
)) {
179 rc_update_bit_0(rc
, p
);
183 rc_update_bit_1(rc
, p
);
184 *symbol
= *symbol
* 2 + 1;
190 static inline int INIT
rc_direct_bit(struct rc
*rc
)
194 if (rc
->code
>= rc
->range
) {
195 rc
->code
-= rc
->range
;
202 static inline void INIT
203 rc_bit_tree_decode(struct rc
*rc
, uint16_t *p
, int num_levels
, int *symbol
)
209 rc_get_bit(rc
, p
+ *symbol
, symbol
);
210 *symbol
-= 1 << num_levels
;
215 * Small lzma deflate implementation.
216 * Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
218 * Based on LzmaDecode.c from the LZMA SDK 4.22 (https://www.7-zip.org/)
219 * Copyright (C) 1999-2005 Igor Pavlov
227 } __attribute__ ((packed
)) ;
230 #define LZMA_BASE_SIZE 1846
231 #define LZMA_LIT_SIZE 768
233 #define LZMA_NUM_POS_BITS_MAX 4
235 #define LZMA_LEN_NUM_LOW_BITS 3
236 #define LZMA_LEN_NUM_MID_BITS 3
237 #define LZMA_LEN_NUM_HIGH_BITS 8
239 #define LZMA_LEN_CHOICE 0
240 #define LZMA_LEN_CHOICE_2 (LZMA_LEN_CHOICE + 1)
241 #define LZMA_LEN_LOW (LZMA_LEN_CHOICE_2 + 1)
242 #define LZMA_LEN_MID (LZMA_LEN_LOW \
243 + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS)))
244 #define LZMA_LEN_HIGH (LZMA_LEN_MID \
245 +(1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS)))
246 #define LZMA_NUM_LEN_PROBS (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS))
248 #define LZMA_NUM_STATES 12
249 #define LZMA_NUM_LIT_STATES 7
251 #define LZMA_START_POS_MODEL_INDEX 4
252 #define LZMA_END_POS_MODEL_INDEX 14
253 #define LZMA_NUM_FULL_DISTANCES (1 << (LZMA_END_POS_MODEL_INDEX >> 1))
255 #define LZMA_NUM_POS_SLOT_BITS 6
256 #define LZMA_NUM_LEN_TO_POS_STATES 4
258 #define LZMA_NUM_ALIGN_BITS 4
260 #define LZMA_MATCH_MIN_LEN 2
262 #define LZMA_IS_MATCH 0
263 #define LZMA_IS_REP (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
264 #define LZMA_IS_REP_G0 (LZMA_IS_REP + LZMA_NUM_STATES)
265 #define LZMA_IS_REP_G1 (LZMA_IS_REP_G0 + LZMA_NUM_STATES)
266 #define LZMA_IS_REP_G2 (LZMA_IS_REP_G1 + LZMA_NUM_STATES)
267 #define LZMA_IS_REP_0_LONG (LZMA_IS_REP_G2 + LZMA_NUM_STATES)
268 #define LZMA_POS_SLOT (LZMA_IS_REP_0_LONG \
269 + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
270 #define LZMA_SPEC_POS (LZMA_POS_SLOT \
271 +(LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS))
272 #define LZMA_ALIGN (LZMA_SPEC_POS \
273 + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX)
274 #define LZMA_LEN_CODER (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS))
275 #define LZMA_REP_LEN_CODER (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS)
276 #define LZMA_LITERAL (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS)
281 uint8_t previous_byte
;
285 long (*flush
)(void*, unsigned long);
286 struct lzma_header
*header
;
291 uint32_t rep0
, rep1
, rep2
, rep3
;
294 static inline size_t INIT
get_pos(struct writer
*wr
)
297 wr
->global_pos
+ wr
->buffer_pos
;
300 static inline uint8_t INIT
peek_old_byte(struct writer
*wr
,
305 while (offs
> wr
->header
->dict_size
)
306 offs
-= wr
->header
->dict_size
;
307 pos
= wr
->buffer_pos
- offs
;
308 return wr
->buffer
[pos
];
310 uint32_t pos
= wr
->buffer_pos
- offs
;
311 while (pos
>= wr
->header
->dict_size
)
312 pos
+= wr
->header
->dict_size
;
313 return wr
->buffer
[pos
];
318 static inline int INIT
write_byte(struct writer
*wr
, uint8_t byte
)
320 wr
->buffer
[wr
->buffer_pos
++] = wr
->previous_byte
= byte
;
321 if (wr
->flush
&& wr
->buffer_pos
== wr
->header
->dict_size
) {
323 wr
->global_pos
+= wr
->header
->dict_size
;
324 if (wr
->flush((char *)wr
->buffer
, wr
->header
->dict_size
)
325 != wr
->header
->dict_size
)
332 static inline int INIT
copy_byte(struct writer
*wr
, uint32_t offs
)
334 return write_byte(wr
, peek_old_byte(wr
, offs
));
337 static inline int INIT
copy_bytes(struct writer
*wr
,
338 uint32_t rep0
, int len
)
341 if (copy_byte(wr
, rep0
))
344 } while (len
!= 0 && wr
->buffer_pos
< wr
->header
->dst_size
);
349 static inline int INIT
process_bit0(struct writer
*wr
, struct rc
*rc
,
350 struct cstate
*cst
, uint16_t *p
,
351 int pos_state
, uint16_t *prob
,
352 int lc
, uint32_t literal_pos_mask
) {
354 rc_update_bit_0(rc
, prob
);
355 prob
= (p
+ LZMA_LITERAL
+
357 * (((get_pos(wr
) & literal_pos_mask
) << lc
)
358 + (wr
->previous_byte
>> (8 - lc
))))
361 if (cst
->state
>= LZMA_NUM_LIT_STATES
) {
362 int match_byte
= peek_old_byte(wr
, cst
->rep0
);
368 bit
= match_byte
& 0x100;
369 prob_lit
= prob
+ 0x100 + bit
+ mi
;
370 if (rc_get_bit(rc
, prob_lit
, &mi
)) {
377 } while (mi
< 0x100);
380 uint16_t *prob_lit
= prob
+ mi
;
381 rc_get_bit(rc
, prob_lit
, &mi
);
385 else if (cst
->state
< 10)
390 return write_byte(wr
, mi
);
393 static inline int INIT
process_bit1(struct writer
*wr
, struct rc
*rc
,
394 struct cstate
*cst
, uint16_t *p
,
395 int pos_state
, uint16_t *prob
) {
401 rc_update_bit_1(rc
, prob
);
402 prob
= p
+ LZMA_IS_REP
+ cst
->state
;
403 if (rc_is_bit_0(rc
, prob
)) {
404 rc_update_bit_0(rc
, prob
);
405 cst
->rep3
= cst
->rep2
;
406 cst
->rep2
= cst
->rep1
;
407 cst
->rep1
= cst
->rep0
;
408 cst
->state
= cst
->state
< LZMA_NUM_LIT_STATES
? 0 : 3;
409 prob
= p
+ LZMA_LEN_CODER
;
411 rc_update_bit_1(rc
, prob
);
412 prob
= p
+ LZMA_IS_REP_G0
+ cst
->state
;
413 if (rc_is_bit_0(rc
, prob
)) {
414 rc_update_bit_0(rc
, prob
);
415 prob
= (p
+ LZMA_IS_REP_0_LONG
417 LZMA_NUM_POS_BITS_MAX
) +
419 if (rc_is_bit_0(rc
, prob
)) {
420 rc_update_bit_0(rc
, prob
);
422 cst
->state
= cst
->state
< LZMA_NUM_LIT_STATES
?
424 return copy_byte(wr
, cst
->rep0
);
426 rc_update_bit_1(rc
, prob
);
431 rc_update_bit_1(rc
, prob
);
432 prob
= p
+ LZMA_IS_REP_G1
+ cst
->state
;
433 if (rc_is_bit_0(rc
, prob
)) {
434 rc_update_bit_0(rc
, prob
);
435 distance
= cst
->rep1
;
437 rc_update_bit_1(rc
, prob
);
438 prob
= p
+ LZMA_IS_REP_G2
+ cst
->state
;
439 if (rc_is_bit_0(rc
, prob
)) {
440 rc_update_bit_0(rc
, prob
);
441 distance
= cst
->rep2
;
443 rc_update_bit_1(rc
, prob
);
444 distance
= cst
->rep3
;
445 cst
->rep3
= cst
->rep2
;
447 cst
->rep2
= cst
->rep1
;
449 cst
->rep1
= cst
->rep0
;
450 cst
->rep0
= distance
;
452 cst
->state
= cst
->state
< LZMA_NUM_LIT_STATES
? 8 : 11;
453 prob
= p
+ LZMA_REP_LEN_CODER
;
456 prob_len
= prob
+ LZMA_LEN_CHOICE
;
457 if (rc_is_bit_0(rc
, prob_len
)) {
458 rc_update_bit_0(rc
, prob_len
);
459 prob_len
= (prob
+ LZMA_LEN_LOW
461 LZMA_LEN_NUM_LOW_BITS
));
463 num_bits
= LZMA_LEN_NUM_LOW_BITS
;
465 rc_update_bit_1(rc
, prob_len
);
466 prob_len
= prob
+ LZMA_LEN_CHOICE_2
;
467 if (rc_is_bit_0(rc
, prob_len
)) {
468 rc_update_bit_0(rc
, prob_len
);
469 prob_len
= (prob
+ LZMA_LEN_MID
471 LZMA_LEN_NUM_MID_BITS
));
472 offset
= 1 << LZMA_LEN_NUM_LOW_BITS
;
473 num_bits
= LZMA_LEN_NUM_MID_BITS
;
475 rc_update_bit_1(rc
, prob_len
);
476 prob_len
= prob
+ LZMA_LEN_HIGH
;
477 offset
= ((1 << LZMA_LEN_NUM_LOW_BITS
)
478 + (1 << LZMA_LEN_NUM_MID_BITS
));
479 num_bits
= LZMA_LEN_NUM_HIGH_BITS
;
483 rc_bit_tree_decode(rc
, prob_len
, num_bits
, &len
);
486 if (cst
->state
< 4) {
489 cst
->state
+= LZMA_NUM_LIT_STATES
;
493 LZMA_NUM_LEN_TO_POS_STATES
? len
:
494 LZMA_NUM_LEN_TO_POS_STATES
- 1)
495 << LZMA_NUM_POS_SLOT_BITS
);
496 rc_bit_tree_decode(rc
, prob
,
497 LZMA_NUM_POS_SLOT_BITS
,
499 if (pos_slot
>= LZMA_START_POS_MODEL_INDEX
) {
501 num_bits
= (pos_slot
>> 1) - 1;
502 cst
->rep0
= 2 | (pos_slot
& 1);
503 if (pos_slot
< LZMA_END_POS_MODEL_INDEX
) {
504 cst
->rep0
<<= num_bits
;
505 prob
= p
+ LZMA_SPEC_POS
+
506 cst
->rep0
- pos_slot
- 1;
508 num_bits
-= LZMA_NUM_ALIGN_BITS
;
510 cst
->rep0
= (cst
->rep0
<< 1) |
512 prob
= p
+ LZMA_ALIGN
;
513 cst
->rep0
<<= LZMA_NUM_ALIGN_BITS
;
514 num_bits
= LZMA_NUM_ALIGN_BITS
;
519 if (rc_get_bit(rc
, prob
+ mi
, &mi
))
524 cst
->rep0
= pos_slot
;
525 if (++(cst
->rep0
) == 0)
527 if (cst
->rep0
> wr
->header
->dict_size
528 || cst
->rep0
> get_pos(wr
))
532 len
+= LZMA_MATCH_MIN_LEN
;
534 return copy_bytes(wr
, cst
->rep0
, len
);
539 STATIC
inline int INIT
unlzma(unsigned char *buf
, long in_len
,
540 long (*fill
)(void*, unsigned long),
541 long (*flush
)(void*, unsigned long),
542 unsigned char *output
,
544 void(*error
)(char *x
)
547 struct lzma_header header
;
549 uint32_t pos_state_mask
;
550 uint32_t literal_pos_mask
;
557 unsigned char *inbuf
;
565 inbuf
= malloc(LZMA_IOBUF_SIZE
);
567 error("Could not allocate input buffer");
572 cst
.rep0
= cst
.rep1
= cst
.rep2
= cst
.rep3
= 1;
577 wr
.previous_byte
= 0;
580 rc_init(&rc
, fill
, inbuf
, in_len
);
582 for (i
= 0; i
< sizeof(header
); i
++) {
583 if (rc
.ptr
>= rc
.buffer_end
)
585 ((unsigned char *)&header
)[i
] = *rc
.ptr
++;
588 if (header
.pos
>= (9 * 5 * 5)) {
605 pos_state_mask
= (1 << pb
) - 1;
606 literal_pos_mask
= (1 << lp
) - 1;
608 ENDIAN_CONVERT(header
.dict_size
);
609 ENDIAN_CONVERT(header
.dst_size
);
611 if (header
.dict_size
== 0)
612 header
.dict_size
= 1;
617 wr
.bufsize
= MIN(header
.dst_size
, header
.dict_size
);
618 wr
.buffer
= large_malloc(wr
.bufsize
);
620 if (wr
.buffer
== NULL
)
623 num_probs
= LZMA_BASE_SIZE
+ (LZMA_LIT_SIZE
<< (lc
+ lp
));
624 p
= (uint16_t *) large_malloc(num_probs
* sizeof(*p
));
627 num_probs
= LZMA_LITERAL
+ (LZMA_LIT_SIZE
<< (lc
+ lp
));
628 for (i
= 0; i
< num_probs
; i
++)
629 p
[i
] = (1 << RC_MODEL_TOTAL_BITS
) >> 1;
633 while (get_pos(&wr
) < header
.dst_size
) {
634 int pos_state
= get_pos(&wr
) & pos_state_mask
;
635 uint16_t *prob
= p
+ LZMA_IS_MATCH
+
636 (cst
.state
<< LZMA_NUM_POS_BITS_MAX
) + pos_state
;
637 if (rc_is_bit_0(&rc
, prob
)) {
638 if (process_bit0(&wr
, &rc
, &cst
, p
, pos_state
, prob
,
639 lc
, literal_pos_mask
)) {
640 error("LZMA data is corrupt");
644 if (process_bit1(&wr
, &rc
, &cst
, p
, pos_state
, prob
)) {
645 error("LZMA data is corrupt");
651 if (rc
.buffer_size
<= 0)
656 *posp
= rc
.ptr
-rc
.buffer
;
657 if (!wr
.flush
|| wr
.flush(wr
.buffer
, wr
.buffer_pos
) == wr
.buffer_pos
)
663 large_free(wr
.buffer
);
672 STATIC
int INIT
__decompress(unsigned char *buf
, long in_len
,
673 long (*fill
)(void*, unsigned long),
674 long (*flush
)(void*, unsigned long),
675 unsigned char *output
, long out_len
,
677 void (*error
)(char *x
))
679 return unlzma(buf
, in_len
- 4, fill
, flush
, output
, posp
, error
);