40 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
42 static long long INIT read_int(
unsigned char *
ptr,
int size)
47 for (i = 0; i <
size; i++)
48 ret = (ret << 8) | ptr[size-i-1];
52 #define ENDIAN_CONVERT(x) \
53 x = (typeof(x))read_int((unsigned char *)&x, sizeof(x))
63 #include <linux/compiler.h>
65 #define LZMA_IOBUF_SIZE 0x10000
80 #define RC_TOP_BITS 24
81 #define RC_MOVE_BITS 5
82 #define RC_MODEL_TOTAL_BITS 11
85 static int INIT nofill(
void *
buffer,
unsigned int len)
91 static void INIT rc_read(
struct rc *
rc)
95 rc->
error(
"unexpected EOF");
101 static inline void INIT rc_init(
struct rc *rc,
102 int (*
fill)(
void*,
unsigned int),
103 char *
buffer,
int buffer_size)
115 rc->
range = 0xFFFFFFFF;
118 static inline void INIT rc_init_code(
struct rc *rc)
122 for (i = 0; i < 5; i++) {
131 static void INIT rc_do_normalize(
struct rc *rc)
138 static inline void INIT rc_normalize(
struct rc *rc)
154 static inline int INIT rc_is_bit_0(
struct rc *rc,
uint16_t *
p)
161 static inline void INIT rc_update_bit_0(
struct rc *rc,
uint16_t *
p)
166 static inline void INIT rc_update_bit_1(
struct rc *rc,
uint16_t *
p)
176 if (rc_is_bit_0(rc, p)) {
177 rc_update_bit_0(rc, p);
181 rc_update_bit_1(rc, p);
182 *symbol = *symbol * 2 + 1;
188 static inline int INIT rc_direct_bit(
struct rc *rc)
200 static inline void INIT
201 rc_bit_tree_decode(
struct rc *rc,
uint16_t *p,
int num_levels,
int *symbol)
207 rc_get_bit(rc, p + *symbol, symbol);
208 *symbol -= 1 << num_levels;
228 #define LZMA_BASE_SIZE 1846
229 #define LZMA_LIT_SIZE 768
231 #define LZMA_NUM_POS_BITS_MAX 4
233 #define LZMA_LEN_NUM_LOW_BITS 3
234 #define LZMA_LEN_NUM_MID_BITS 3
235 #define LZMA_LEN_NUM_HIGH_BITS 8
237 #define LZMA_LEN_CHOICE 0
238 #define LZMA_LEN_CHOICE_2 (LZMA_LEN_CHOICE + 1)
239 #define LZMA_LEN_LOW (LZMA_LEN_CHOICE_2 + 1)
240 #define LZMA_LEN_MID (LZMA_LEN_LOW \
241 + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS)))
242 #define LZMA_LEN_HIGH (LZMA_LEN_MID \
243 +(1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS)))
244 #define LZMA_NUM_LEN_PROBS (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS))
246 #define LZMA_NUM_STATES 12
247 #define LZMA_NUM_LIT_STATES 7
249 #define LZMA_START_POS_MODEL_INDEX 4
250 #define LZMA_END_POS_MODEL_INDEX 14
251 #define LZMA_NUM_FULL_DISTANCES (1 << (LZMA_END_POS_MODEL_INDEX >> 1))
253 #define LZMA_NUM_POS_SLOT_BITS 6
254 #define LZMA_NUM_LEN_TO_POS_STATES 4
256 #define LZMA_NUM_ALIGN_BITS 4
258 #define LZMA_MATCH_MIN_LEN 2
260 #define LZMA_IS_MATCH 0
261 #define LZMA_IS_REP (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
262 #define LZMA_IS_REP_G0 (LZMA_IS_REP + LZMA_NUM_STATES)
263 #define LZMA_IS_REP_G1 (LZMA_IS_REP_G0 + LZMA_NUM_STATES)
264 #define LZMA_IS_REP_G2 (LZMA_IS_REP_G1 + LZMA_NUM_STATES)
265 #define LZMA_IS_REP_0_LONG (LZMA_IS_REP_G2 + LZMA_NUM_STATES)
266 #define LZMA_POS_SLOT (LZMA_IS_REP_0_LONG \
267 + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
268 #define LZMA_SPEC_POS (LZMA_POS_SLOT \
269 +(LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS))
270 #define LZMA_ALIGN (LZMA_SPEC_POS \
271 + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX)
272 #define LZMA_LEN_CODER (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS))
273 #define LZMA_REP_LEN_CODER (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS)
274 #define LZMA_LITERAL (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS)
303 while (offs > wr->
header->dict_size)
304 offs -= wr->
header->dict_size;
309 while (pos >= wr->
header->dict_size)
310 pos += wr->
header->dict_size;
332 return write_byte(wr, peek_old_byte(wr, offs));
335 static inline int INIT copy_bytes(
struct writer *wr,
339 if (copy_byte(wr, rep0))
347 static inline int INIT process_bit0(
struct writer *wr,
struct rc *rc,
352 rc_update_bit_0(rc, prob);
355 * (((get_pos(wr) & literal_pos_mask) << lc)
360 int match_byte = peek_old_byte(wr, cst->rep0);
366 bit = match_byte & 0x100;
367 prob_lit = prob + 0x100 + bit + mi;
368 if (rc_get_bit(rc, prob_lit, &mi)) {
375 }
while (mi < 0x100);
379 rc_get_bit(rc, prob_lit, &mi);
383 else if (cst->state < 10)
388 return write_byte(wr, mi);
391 static inline int INIT process_bit1(
struct writer *wr,
struct rc *rc,
399 rc_update_bit_1(rc, prob);
401 if (rc_is_bit_0(rc, prob)) {
402 rc_update_bit_0(rc, prob);
409 rc_update_bit_1(rc, prob);
411 if (rc_is_bit_0(rc, prob)) {
412 rc_update_bit_0(rc, prob);
417 if (rc_is_bit_0(rc, prob)) {
418 rc_update_bit_0(rc, prob);
422 return copy_byte(wr, cst->
rep0);
424 rc_update_bit_1(rc, prob);
429 rc_update_bit_1(rc, prob);
431 if (rc_is_bit_0(rc, prob)) {
432 rc_update_bit_0(rc, prob);
433 distance = cst->
rep1;
435 rc_update_bit_1(rc, prob);
437 if (rc_is_bit_0(rc, prob)) {
438 rc_update_bit_0(rc, prob);
439 distance = cst->
rep2;
441 rc_update_bit_1(rc, prob);
442 distance = cst->
rep3;
448 cst->
rep0 = distance;
455 if (rc_is_bit_0(rc, prob_len)) {
456 rc_update_bit_0(rc, prob_len);
463 rc_update_bit_1(rc, prob_len);
465 if (rc_is_bit_0(rc, prob_len)) {
466 rc_update_bit_0(rc, prob_len);
473 rc_update_bit_1(rc, prob_len);
481 rc_bit_tree_decode(rc, prob_len, num_bits, &len);
484 if (cst->
state < 4) {
494 rc_bit_tree_decode(rc, prob,
499 num_bits = (pos_slot >> 1) - 1;
500 cst->
rep0 = 2 | (pos_slot & 1);
502 cst->
rep0 <<= num_bits;
504 cst->
rep0 - pos_slot - 1;
517 if (rc_get_bit(rc, prob + mi, &mi))
522 cst->
rep0 = pos_slot;
523 if (++(cst->
rep0) == 0)
526 || cst->
rep0 > get_pos(wr))
532 return copy_bytes(wr, cst->
rep0, len);
538 int(*
fill)(
void*,
unsigned int),
539 int(*flush)(
void*,
unsigned int),
540 unsigned char *output,
555 unsigned char *
inbuf;
565 error(
"Could not allocate input buffer");
578 rc_init(&rc,
fill, inbuf, in_len);
580 for (i = 0; i <
sizeof(header); i++) {
583 ((
unsigned char *)&header)[
i] = *rc.
ptr++;
586 if (header.
pos >= (9 * 5 * 5)) {
603 pos_state_mask = (1 << pb) - 1;
604 literal_pos_mask = (1 << lp) - 1;
626 for (i = 0; i < num_probs; i++)
631 while (get_pos(&wr) < header.
dst_size) {
632 int pos_state = get_pos(&wr) & pos_state_mask;
635 if (rc_is_bit_0(&rc, prob)) {
636 if (process_bit0(&wr, &rc, &cst, p, pos_state, prob,
637 lc, literal_pos_mask)) {
638 error(
"LZMA data is corrupt");
642 if (process_bit1(&wr, &rc, &cst, p, pos_state, prob)) {
643 error(
"LZMA data is corrupt");
671 int(*
fill)(
void*,
unsigned int),
672 int(*flush)(
void*,
unsigned int),
673 unsigned char *output,