Lines Matching full:s
158 /* If 1, it's a match. Otherwise it's a single 8-bit literal. */
161 /* If 1, it's a repeated match. The distance is one of rep0 .. rep3. */
438 * has been allocated by us in this file; it's not in dict_flush()
589 static uint16_t *lzma_literal_probs(struct xz_dec_lzma2 *s) in lzma_literal_probs() argument
591 uint32_t prev_byte = dict_get(&s->dict, 0); in lzma_literal_probs()
592 uint32_t low = prev_byte >> (8 - s->lzma.lc); in lzma_literal_probs()
593 uint32_t high = (s->dict.pos & s->lzma.literal_pos_mask) << s->lzma.lc; in lzma_literal_probs()
594 return s->lzma.literal[low + high]; in lzma_literal_probs()
598 static void lzma_literal(struct xz_dec_lzma2 *s) in lzma_literal() argument
607 probs = lzma_literal_probs(s); in lzma_literal()
609 if (lzma_state_is_literal(s->lzma.state)) { in lzma_literal()
610 symbol = rc_bittree(&s->rc, probs, 0x100); in lzma_literal()
613 match_byte = dict_get(&s->dict, s->lzma.rep0) << 1; in lzma_literal()
621 if (rc_bit(&s->rc, &probs[i])) { in lzma_literal()
631 dict_put(&s->dict, (uint8_t)symbol); in lzma_literal()
632 lzma_state_literal(&s->lzma.state); in lzma_literal()
635 /* Decode the length of the match into s->lzma.len. */
636 static void lzma_len(struct xz_dec_lzma2 *s, struct lzma_len_dec *l, in lzma_len() argument
642 if (!rc_bit(&s->rc, &l->choice)) { in lzma_len()
645 s->lzma.len = MATCH_LEN_MIN; in lzma_len()
647 if (!rc_bit(&s->rc, &l->choice2)) { in lzma_len()
650 s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS; in lzma_len()
654 s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS in lzma_len()
659 s->lzma.len += rc_bittree(&s->rc, probs, limit) - limit; in lzma_len()
662 /* Decode a match. The distance will be stored in s->lzma.rep0. */
663 static void lzma_match(struct xz_dec_lzma2 *s, uint32_t pos_state) in lzma_match() argument
669 lzma_state_match(&s->lzma.state); in lzma_match()
671 s->lzma.rep3 = s->lzma.rep2; in lzma_match()
672 s->lzma.rep2 = s->lzma.rep1; in lzma_match()
673 s->lzma.rep1 = s->lzma.rep0; in lzma_match()
675 lzma_len(s, &s->lzma.match_len_dec, pos_state); in lzma_match()
677 probs = s->lzma.dist_slot[lzma_get_dist_state(s->lzma.len)]; in lzma_match()
678 dist_slot = rc_bittree(&s->rc, probs, DIST_SLOTS) - DIST_SLOTS; in lzma_match()
681 s->lzma.rep0 = dist_slot; in lzma_match()
684 s->lzma.rep0 = 2 + (dist_slot & 1); in lzma_match()
687 s->lzma.rep0 <<= limit; in lzma_match()
688 probs = s->lzma.dist_special + s->lzma.rep0 in lzma_match()
690 rc_bittree_reverse(&s->rc, probs, in lzma_match()
691 &s->lzma.rep0, limit); in lzma_match()
693 rc_direct(&s->rc, &s->lzma.rep0, limit - ALIGN_BITS); in lzma_match()
694 s->lzma.rep0 <<= ALIGN_BITS; in lzma_match()
695 rc_bittree_reverse(&s->rc, s->lzma.dist_align, in lzma_match()
696 &s->lzma.rep0, ALIGN_BITS); in lzma_match()
703 * seen matches. The distance will be stored in s->lzma.rep0.
705 static void lzma_rep_match(struct xz_dec_lzma2 *s, uint32_t pos_state) in lzma_rep_match() argument
709 if (!rc_bit(&s->rc, &s->lzma.is_rep0[s->lzma.state])) { in lzma_rep_match()
710 if (!rc_bit(&s->rc, &s->lzma.is_rep0_long[ in lzma_rep_match()
711 s->lzma.state][pos_state])) { in lzma_rep_match()
712 lzma_state_short_rep(&s->lzma.state); in lzma_rep_match()
713 s->lzma.len = 1; in lzma_rep_match()
717 if (!rc_bit(&s->rc, &s->lzma.is_rep1[s->lzma.state])) { in lzma_rep_match()
718 tmp = s->lzma.rep1; in lzma_rep_match()
720 if (!rc_bit(&s->rc, &s->lzma.is_rep2[s->lzma.state])) { in lzma_rep_match()
721 tmp = s->lzma.rep2; in lzma_rep_match()
723 tmp = s->lzma.rep3; in lzma_rep_match()
724 s->lzma.rep3 = s->lzma.rep2; in lzma_rep_match()
727 s->lzma.rep2 = s->lzma.rep1; in lzma_rep_match()
730 s->lzma.rep1 = s->lzma.rep0; in lzma_rep_match()
731 s->lzma.rep0 = tmp; in lzma_rep_match()
734 lzma_state_long_rep(&s->lzma.state); in lzma_rep_match()
735 lzma_len(s, &s->lzma.rep_len_dec, pos_state); in lzma_rep_match()
739 static bool lzma_main(struct xz_dec_lzma2 *s) in lzma_main() argument
747 if (dict_has_space(&s->dict) && s->lzma.len > 0) in lzma_main()
748 dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0); in lzma_main()
754 while (dict_has_space(&s->dict) && !rc_limit_exceeded(&s->rc)) { in lzma_main()
755 pos_state = s->dict.pos & s->lzma.pos_mask; in lzma_main()
757 if (!rc_bit(&s->rc, &s->lzma.is_match[ in lzma_main()
758 s->lzma.state][pos_state])) { in lzma_main()
759 lzma_literal(s); in lzma_main()
761 if (rc_bit(&s->rc, &s->lzma.is_rep[s->lzma.state])) in lzma_main()
762 lzma_rep_match(s, pos_state); in lzma_main()
764 lzma_match(s, pos_state); in lzma_main()
766 if (!dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0)) in lzma_main()
775 rc_normalize(&s->rc); in lzma_main()
784 static void lzma_reset(struct xz_dec_lzma2 *s) in lzma_reset() argument
789 s->lzma.state = STATE_LIT_LIT; in lzma_reset()
790 s->lzma.rep0 = 0; in lzma_reset()
791 s->lzma.rep1 = 0; in lzma_reset()
792 s->lzma.rep2 = 0; in lzma_reset()
793 s->lzma.rep3 = 0; in lzma_reset()
804 probs = s->lzma.is_match[0]; in lzma_reset()
808 rc_reset(&s->rc); in lzma_reset()
816 static bool lzma_props(struct xz_dec_lzma2 *s, uint8_t props) in lzma_props() argument
821 s->lzma.pos_mask = 0; in lzma_props()
824 ++s->lzma.pos_mask; in lzma_props()
827 s->lzma.pos_mask = (1 << s->lzma.pos_mask) - 1; in lzma_props()
829 s->lzma.literal_pos_mask = 0; in lzma_props()
832 ++s->lzma.literal_pos_mask; in lzma_props()
835 s->lzma.lc = props; in lzma_props()
837 if (s->lzma.lc + s->lzma.literal_pos_mask > 4) in lzma_props()
840 s->lzma.literal_pos_mask = (1 << s->lzma.literal_pos_mask) - 1; in lzma_props()
842 lzma_reset(s); in lzma_props()
852 * The LZMA decoder assumes that if the input limit (s->rc.in_limit) hasn't
854 * wrapper function takes care of making the LZMA decoder's assumption safe.
858 * there's LZMA_IN_REQUIRED bytes left. Those remaining bytes are copied into
859 * s->temp.buf, which (hopefully) gets filled on the next call to this
863 static bool lzma2_lzma(struct xz_dec_lzma2 *s, struct xz_buf *b) in lzma2_lzma() argument
869 if (s->temp.size > 0 || s->lzma2.compressed == 0) { in lzma2_lzma()
870 tmp = 2 * LZMA_IN_REQUIRED - s->temp.size; in lzma2_lzma()
871 if (tmp > s->lzma2.compressed - s->temp.size) in lzma2_lzma()
872 tmp = s->lzma2.compressed - s->temp.size; in lzma2_lzma()
876 memcpy(s->temp.buf + s->temp.size, b->in + b->in_pos, tmp); in lzma2_lzma()
878 if (s->temp.size + tmp == s->lzma2.compressed) { in lzma2_lzma()
879 memzero(s->temp.buf + s->temp.size + tmp, in lzma2_lzma()
880 sizeof(s->temp.buf) in lzma2_lzma()
881 - s->temp.size - tmp); in lzma2_lzma()
882 s->rc.in_limit = s->temp.size + tmp; in lzma2_lzma()
883 } else if (s->temp.size + tmp < LZMA_IN_REQUIRED) { in lzma2_lzma()
884 s->temp.size += tmp; in lzma2_lzma()
888 s->rc.in_limit = s->temp.size + tmp - LZMA_IN_REQUIRED; in lzma2_lzma()
891 s->rc.in = s->temp.buf; in lzma2_lzma()
892 s->rc.in_pos = 0; in lzma2_lzma()
894 if (!lzma_main(s) || s->rc.in_pos > s->temp.size + tmp) in lzma2_lzma()
897 s->lzma2.compressed -= s->rc.in_pos; in lzma2_lzma()
899 if (s->rc.in_pos < s->temp.size) { in lzma2_lzma()
900 s->temp.size -= s->rc.in_pos; in lzma2_lzma()
901 memmove(s->temp.buf, s->temp.buf + s->rc.in_pos, in lzma2_lzma()
902 s->temp.size); in lzma2_lzma()
906 b->in_pos += s->rc.in_pos - s->temp.size; in lzma2_lzma()
907 s->temp.size = 0; in lzma2_lzma()
912 s->rc.in = b->in; in lzma2_lzma()
913 s->rc.in_pos = b->in_pos; in lzma2_lzma()
915 if (in_avail >= s->lzma2.compressed + LZMA_IN_REQUIRED) in lzma2_lzma()
916 s->rc.in_limit = b->in_pos + s->lzma2.compressed; in lzma2_lzma()
918 s->rc.in_limit = b->in_size - LZMA_IN_REQUIRED; in lzma2_lzma()
920 if (!lzma_main(s)) in lzma2_lzma()
923 in_avail = s->rc.in_pos - b->in_pos; in lzma2_lzma()
924 if (in_avail > s->lzma2.compressed) in lzma2_lzma()
927 s->lzma2.compressed -= in_avail; in lzma2_lzma()
928 b->in_pos = s->rc.in_pos; in lzma2_lzma()
933 if (in_avail > s->lzma2.compressed) in lzma2_lzma()
934 in_avail = s->lzma2.compressed; in lzma2_lzma()
936 memcpy(s->temp.buf, b->in + b->in_pos, in_avail); in lzma2_lzma()
937 s->temp.size = in_avail; in lzma2_lzma()
948 XZ_EXTERN enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s, in xz_dec_lzma2_run() argument
953 while (b->in_pos < b->in_size || s->lzma2.sequence == SEQ_LZMA_RUN) { in xz_dec_lzma2_run()
954 switch (s->lzma2.sequence) { in xz_dec_lzma2_run()
965 * Highest three bits (s->control & 0xE0): in xz_dec_lzma2_run()
977 * (s->control & 1F) are the highest bits of the in xz_dec_lzma2_run()
993 s->lzma2.need_props = true; in xz_dec_lzma2_run()
994 s->lzma2.need_dict_reset = false; in xz_dec_lzma2_run()
995 dict_reset(&s->dict, b); in xz_dec_lzma2_run()
996 } else if (s->lzma2.need_dict_reset) { in xz_dec_lzma2_run()
1001 s->lzma2.uncompressed = (tmp & 0x1F) << 16; in xz_dec_lzma2_run()
1002 s->lzma2.sequence = SEQ_UNCOMPRESSED_1; in xz_dec_lzma2_run()
1010 s->lzma2.need_props = false; in xz_dec_lzma2_run()
1011 s->lzma2.next_sequence in xz_dec_lzma2_run()
1014 } else if (s->lzma2.need_props) { in xz_dec_lzma2_run()
1018 s->lzma2.next_sequence in xz_dec_lzma2_run()
1021 lzma_reset(s); in xz_dec_lzma2_run()
1027 s->lzma2.sequence = SEQ_COMPRESSED_0; in xz_dec_lzma2_run()
1028 s->lzma2.next_sequence = SEQ_COPY; in xz_dec_lzma2_run()
1034 s->lzma2.uncompressed in xz_dec_lzma2_run()
1036 s->lzma2.sequence = SEQ_UNCOMPRESSED_2; in xz_dec_lzma2_run()
1040 s->lzma2.uncompressed in xz_dec_lzma2_run()
1042 s->lzma2.sequence = SEQ_COMPRESSED_0; in xz_dec_lzma2_run()
1046 s->lzma2.compressed in xz_dec_lzma2_run()
1048 s->lzma2.sequence = SEQ_COMPRESSED_1; in xz_dec_lzma2_run()
1052 s->lzma2.compressed in xz_dec_lzma2_run()
1054 s->lzma2.sequence = s->lzma2.next_sequence; in xz_dec_lzma2_run()
1058 if (!lzma_props(s, b->in[b->in_pos++])) in xz_dec_lzma2_run()
1061 s->lzma2.sequence = SEQ_LZMA_PREPARE; in xz_dec_lzma2_run()
1066 if (s->lzma2.compressed < RC_INIT_BYTES) in xz_dec_lzma2_run()
1069 if (!rc_read_init(&s->rc, b)) in xz_dec_lzma2_run()
1072 s->lzma2.compressed -= RC_INIT_BYTES; in xz_dec_lzma2_run()
1073 s->lzma2.sequence = SEQ_LZMA_RUN; in xz_dec_lzma2_run()
1085 * multiple times without changing s->lzma2.sequence. in xz_dec_lzma2_run()
1087 dict_limit(&s->dict, min_t(size_t, in xz_dec_lzma2_run()
1089 s->lzma2.uncompressed)); in xz_dec_lzma2_run()
1090 if (!lzma2_lzma(s, b)) in xz_dec_lzma2_run()
1093 s->lzma2.uncompressed -= dict_flush(&s->dict, b); in xz_dec_lzma2_run()
1095 if (s->lzma2.uncompressed == 0) { in xz_dec_lzma2_run()
1096 if (s->lzma2.compressed > 0 || s->lzma.len > 0 in xz_dec_lzma2_run()
1097 || !rc_is_finished(&s->rc)) in xz_dec_lzma2_run()
1100 rc_reset(&s->rc); in xz_dec_lzma2_run()
1101 s->lzma2.sequence = SEQ_CONTROL; in xz_dec_lzma2_run()
1105 && s->temp.size in xz_dec_lzma2_run()
1106 < s->lzma2.compressed)) { in xz_dec_lzma2_run()
1113 dict_uncompressed(&s->dict, b, &s->lzma2.compressed); in xz_dec_lzma2_run()
1114 if (s->lzma2.compressed > 0) in xz_dec_lzma2_run()
1117 s->lzma2.sequence = SEQ_CONTROL; in xz_dec_lzma2_run()
1128 struct xz_dec_lzma2 *s = kmalloc(sizeof(*s), GFP_KERNEL); in xz_dec_lzma2_create() local
1129 if (s == NULL) in xz_dec_lzma2_create()
1132 s->dict.mode = mode; in xz_dec_lzma2_create()
1133 s->dict.size_max = dict_max; in xz_dec_lzma2_create()
1136 s->dict.buf = vmalloc(dict_max); in xz_dec_lzma2_create()
1137 if (s->dict.buf == NULL) { in xz_dec_lzma2_create()
1138 kfree(s); in xz_dec_lzma2_create()
1142 s->dict.buf = NULL; in xz_dec_lzma2_create()
1143 s->dict.allocated = 0; in xz_dec_lzma2_create()
1146 return s; in xz_dec_lzma2_create()
1149 XZ_EXTERN enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s, uint8_t props) in xz_dec_lzma2_reset() argument
1155 s->dict.size = 2 + (props & 1); in xz_dec_lzma2_reset()
1156 s->dict.size <<= (props >> 1) + 11; in xz_dec_lzma2_reset()
1158 if (DEC_IS_MULTI(s->dict.mode)) { in xz_dec_lzma2_reset()
1159 if (s->dict.size > s->dict.size_max) in xz_dec_lzma2_reset()
1162 s->dict.end = s->dict.size; in xz_dec_lzma2_reset()
1164 if (DEC_IS_DYNALLOC(s->dict.mode)) { in xz_dec_lzma2_reset()
1165 if (s->dict.allocated < s->dict.size) { in xz_dec_lzma2_reset()
1166 s->dict.allocated = s->dict.size; in xz_dec_lzma2_reset()
1167 vfree(s->dict.buf); in xz_dec_lzma2_reset()
1168 s->dict.buf = vmalloc(s->dict.size); in xz_dec_lzma2_reset()
1169 if (s->dict.buf == NULL) { in xz_dec_lzma2_reset()
1170 s->dict.allocated = 0; in xz_dec_lzma2_reset()
1177 s->lzma.len = 0; in xz_dec_lzma2_reset()
1179 s->lzma2.sequence = SEQ_CONTROL; in xz_dec_lzma2_reset()
1180 s->lzma2.need_dict_reset = true; in xz_dec_lzma2_reset()
1182 s->temp.size = 0; in xz_dec_lzma2_reset()
1187 XZ_EXTERN void xz_dec_lzma2_end(struct xz_dec_lzma2 *s) in xz_dec_lzma2_end() argument
1189 if (DEC_IS_MULTI(s->dict.mode)) in xz_dec_lzma2_end()
1190 vfree(s->dict.buf); in xz_dec_lzma2_end()
1192 kfree(s); in xz_dec_lzma2_end()