17 #define RC_INIT_BYTES 5
26 #define LZMA_IN_REQUIRED 21
300 static void dict_limit(
struct dictionary *dict,
size_t out_max)
302 if (dict->
end - dict->
pos <= out_max)
309 static inline bool dict_has_space(
const struct dictionary *dict)
324 if (dist >= dict->
pos)
351 if (dist >= dict->
full || dist >= dict->
size)
357 back = dict->
pos - dist - 1;
358 if (dist >= dict->
pos)
362 dict->
buf[dict->
pos++] = dict->
buf[back++];
363 if (back == dict->
end)
365 }
while (--left > 0);
383 if (copy_size > dict->
end - dict->
pos)
384 copy_size = dict->
end - dict->
pos;
385 if (copy_size > *left)
391 dict->
pos += copy_size;
397 if (dict->
pos == dict->
end)
418 size_t copy_size = dict->
pos - dict->
start;
421 if (dict->
pos == dict->
end)
438 static void rc_reset(
struct rc_dec *
rc)
463 static inline bool rc_limit_exceeded(
const struct rc_dec *rc)
472 static inline bool rc_is_finished(
const struct rc_dec *rc)
474 return rc->
code == 0;
504 if (rc->
code < bound) {
525 if (rc_bit(rc, &probs[symbol]))
526 symbol = (symbol << 1) + 1;
529 }
while (symbol < limit);
543 if (rc_bit(rc, &probs[symbol])) {
544 symbol = (symbol << 1) + 1;
549 }
while (++i < limit);
563 *dest = (*dest << 1) + (mask + 1);
564 }
while (--limit > 0);
577 return s->
lzma.literal[low + high];
590 probs = lzma_literal_probs(s);
592 if (lzma_state_is_literal(s->
lzma.state)) {
593 symbol = rc_bittree(&s->
rc, probs, 0x100);
596 match_byte = dict_get(&s->
dict, s->
lzma.rep0) << 1;
600 match_bit = match_byte &
offset;
602 i = offset + match_bit + symbol;
604 if (rc_bit(&s->
rc, &probs[i])) {
605 symbol = (symbol << 1) + 1;
609 offset &= ~match_bit;
611 }
while (symbol < 0x100);
615 lzma_state_literal(&s->
lzma.state);
626 probs = l->
low[pos_state];
631 probs = l->
mid[pos_state];
642 s->
lzma.len += rc_bittree(&s->
rc, probs, limit) -
limit;
652 lzma_state_match(&s->
lzma.state);
658 lzma_len(s, &s->
lzma.match_len_dec, pos_state);
660 probs = s->
lzma.dist_slot[lzma_get_dist_state(s->
lzma.len)];
664 s->
lzma.rep0 = dist_slot;
666 limit = (dist_slot >> 1) - 1;
667 s->
lzma.rep0 = 2 + (dist_slot & 1);
671 probs = s->
lzma.dist_special + s->
lzma.rep0
673 rc_bittree_reverse(&s->
rc, probs,
674 &s->
lzma.rep0, limit);
678 rc_bittree_reverse(&s->
rc, s->
lzma.dist_align,
692 if (!rc_bit(&s->
rc, &s->
lzma.is_rep0[s->
lzma.state])) {
693 if (!rc_bit(&s->
rc, &s->
lzma.is_rep0_long[
694 s->
lzma.state][pos_state])) {
695 lzma_state_short_rep(&s->
lzma.state);
700 if (!rc_bit(&s->
rc, &s->
lzma.is_rep1[s->
lzma.state])) {
703 if (!rc_bit(&s->
rc, &s->
lzma.is_rep2[s->
lzma.state])) {
717 lzma_state_long_rep(&s->
lzma.state);
718 lzma_len(s, &s->
lzma.rep_len_dec, pos_state);
730 if (dict_has_space(&s->
dict) && s->
lzma.len > 0)
737 while (dict_has_space(&s->
dict) && !rc_limit_exceeded(&s->
rc)) {
738 pos_state = s->
dict.pos & s->
lzma.pos_mask;
740 if (!rc_bit(&s->
rc, &s->
lzma.is_match[
741 s->
lzma.state][pos_state])) {
744 if (rc_bit(&s->
rc, &s->
lzma.is_rep[s->
lzma.state]))
745 lzma_rep_match(s, pos_state);
747 lzma_match(s, pos_state);
758 rc_normalize(&s->
rc);
787 probs = s->
lzma.is_match[0];
801 if (props > (4 * 5 + 4) * 9 + 8)
804 s->
lzma.pos_mask = 0;
805 while (props >= 9 * 5) {
810 s->
lzma.pos_mask = (1 << s->
lzma.pos_mask) - 1;
812 s->
lzma.literal_pos_mask = 0;
815 ++s->
lzma.literal_pos_mask;
820 if (s->
lzma.lc + s->
lzma.literal_pos_mask > 4)
823 s->
lzma.literal_pos_mask = (1 << s->
lzma.literal_pos_mask) - 1;
852 if (s->
temp.size > 0 || s->
lzma2.compressed == 0) {
854 if (tmp > s->
lzma2.compressed - s->
temp.size)
855 tmp = s->
lzma2.compressed - s->
temp.size;
861 if (s->
temp.size + tmp == s->
lzma2.compressed) {
864 - s->
temp.size - tmp);
877 if (!lzma_main(s) || s->
rc.in_pos > s->
temp.size + tmp)
880 s->
lzma2.compressed -= s->
rc.in_pos;
882 if (s->
rc.in_pos < s->
temp.size) {
883 s->
temp.size -= s->
rc.in_pos;
906 in_avail = s->
rc.in_pos - b->
in_pos;
907 if (in_avail > s->
lzma2.compressed)
910 s->
lzma2.compressed -= in_avail;
916 if (in_avail > s->
lzma2.compressed)
917 in_avail = s->
lzma2.compressed;
920 s->
temp.size = in_avail;
937 switch (s->
lzma2.sequence) {
975 if (tmp >= 0xE0 || tmp == 0x01) {
976 s->
lzma2.need_props =
true;
977 s->
lzma2.need_dict_reset =
false;
978 dict_reset(&s->
dict, b);
979 }
else if (s->
lzma2.need_dict_reset) {
984 s->
lzma2.uncompressed = (tmp & 0x1F) << 16;
985 s->
lzma2.sequence = SEQ_UNCOMPRESSED_1;
993 s->
lzma2.need_props =
false;
994 s->
lzma2.next_sequence
997 }
else if (s->
lzma2.need_props) {
1001 s->
lzma2.next_sequence
1010 s->
lzma2.sequence = SEQ_COMPRESSED_0;
1011 s->
lzma2.next_sequence = SEQ_COPY;
1016 case SEQ_UNCOMPRESSED_1:
1017 s->
lzma2.uncompressed
1019 s->
lzma2.sequence = SEQ_UNCOMPRESSED_2;
1022 case SEQ_UNCOMPRESSED_2:
1023 s->
lzma2.uncompressed
1025 s->
lzma2.sequence = SEQ_COMPRESSED_0;
1028 case SEQ_COMPRESSED_0:
1031 s->
lzma2.sequence = SEQ_COMPRESSED_1;
1034 case SEQ_COMPRESSED_1:
1040 case SEQ_PROPERTIES:
1041 if (!lzma_props(s, b->
in[b->
in_pos++]))
1044 s->
lzma2.sequence = SEQ_LZMA_PREPARE;
1046 case SEQ_LZMA_PREPARE:
1050 if (!rc_read_init(&s->
rc, b))
1054 s->
lzma2.sequence = SEQ_LZMA_RUN;
1068 s->
lzma2.uncompressed));
1069 if (!lzma2_lzma(s, b))
1072 s->
lzma2.uncompressed -= dict_flush(&s->
dict, b);
1074 if (s->
lzma2.uncompressed == 0) {
1075 if (s->
lzma2.compressed > 0 || s->
lzma.len > 0
1076 || !rc_is_finished(&s->
rc))
1085 < s->
lzma2.compressed)) {
1092 dict_uncompressed(&s->
dict, b, &s->
lzma2.compressed);
1093 if (s->
lzma2.compressed > 0)
1112 s->
dict.size_max = dict_max;
1122 s->
dict.allocated = 0;
1134 s->
dict.size = 2 + (props & 1);
1135 s->
dict.size <<= (props >> 1) + 11;
1138 if (s->
dict.size > s->
dict.size_max)
1144 if (s->
dict.allocated < s->
dict.size) {
1148 s->
dict.allocated = 0;
1158 s->
lzma2.need_dict_reset =
true;