Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:10

0001 /*
0002  * LZMA2 decoder
0003  *
0004  * Authors: Lasse Collin <lasse.collin@tukaani.org>
0005  *          Igor Pavlov <http://7-zip.org/>
0006  *
0007  * This file has been put into the public domain.
0008  * You can do whatever you want with this file.
0009  */
0010 
0011 #include "xz_private.h"
0012 #include "xz_lzma2.h"
0013 
0014 /*
0015  * Range decoder initialization eats the first five bytes of each LZMA chunk.
0016  */
0017 #define RC_INIT_BYTES 5
0018 
0019 /*
0020  * Minimum number of usable input buffer to safely decode one LZMA symbol.
0021  * The worst case is that we decode 22 bits using probabilities and 26
0022  * direct bits. This may decode at maximum of 20 bytes of input. However,
0023  * lzma_main() does an extra normalization before returning, thus we
0024  * need to put 21 here.
0025  */
0026 #define LZMA_IN_REQUIRED 21
0027 
0028 /*
0029  * Dictionary (history buffer)
0030  *
0031  * These are always true:
0032  *    start <= pos <= full <= end
0033  *    pos <= limit <= end
0034  *
0035  * In multi-call mode, also these are true:
0036  *    end == size
0037  *    size <= size_max
0038  *    allocated <= size
0039  *
0040  * Most of these variables are size_t to support single-call mode,
0041  * in which the dictionary variables address the actual output
0042  * buffer directly.
0043  */
0044 struct dictionary {
0045     /* Beginning of the history buffer */
0046     uint8_t *buf;
0047 
0048     /* Old position in buf (before decoding more data) */
0049     size_t start;
0050 
0051     /* Position in buf */
0052     size_t pos;
0053 
0054     /*
0055      * How full dictionary is. This is used to detect corrupt input that
0056      * would read beyond the beginning of the uncompressed stream.
0057      */
0058     size_t full;
0059 
0060     /* Write limit; we don't write to buf[limit] or later bytes. */
0061     size_t limit;
0062 
0063     /*
0064      * End of the dictionary buffer. In multi-call mode, this is
0065      * the same as the dictionary size. In single-call mode, this
0066      * indicates the size of the output buffer.
0067      */
0068     size_t end;
0069 
0070     /*
0071      * Size of the dictionary as specified in Block Header. This is used
0072      * together with "full" to detect corrupt input that would make us
0073      * read beyond the beginning of the uncompressed stream.
0074      */
0075     uint32_t size;
0076 
0077     /*
0078      * Maximum allowed dictionary size in multi-call mode.
0079      * This is ignored in single-call mode.
0080      */
0081     uint32_t size_max;
0082 
0083     /*
0084      * Amount of memory currently allocated for the dictionary.
0085      * This is used only with XZ_DYNALLOC. (With XZ_PREALLOC,
0086      * size_max is always the same as the allocated size.)
0087      */
0088     uint32_t allocated;
0089 
0090     /* Operation mode */
0091     enum xz_mode mode;
0092 };
0093 
0094 /* Range decoder */
0095 struct rc_dec {
0096     uint32_t range;
0097     uint32_t code;
0098 
0099     /*
0100      * Number of initializing bytes remaining to be read
0101      * by rc_read_init().
0102      */
0103     uint32_t init_bytes_left;
0104 
0105     /*
0106      * Buffer from which we read our input. It can be either
0107      * temp.buf or the caller-provided input buffer.
0108      */
0109     const uint8_t *in;
0110     size_t in_pos;
0111     size_t in_limit;
0112 };
0113 
0114 /* Probabilities for a length decoder. */
0115 struct lzma_len_dec {
0116     /* Probability of match length being at least 10 */
0117     uint16_t choice;
0118 
0119     /* Probability of match length being at least 18 */
0120     uint16_t choice2;
0121 
0122     /* Probabilities for match lengths 2-9 */
0123     uint16_t low[POS_STATES_MAX][LEN_LOW_SYMBOLS];
0124 
0125     /* Probabilities for match lengths 10-17 */
0126     uint16_t mid[POS_STATES_MAX][LEN_MID_SYMBOLS];
0127 
0128     /* Probabilities for match lengths 18-273 */
0129     uint16_t high[LEN_HIGH_SYMBOLS];
0130 };
0131 
0132 struct lzma_dec {
0133     /* Distances of latest four matches */
0134     uint32_t rep0;
0135     uint32_t rep1;
0136     uint32_t rep2;
0137     uint32_t rep3;
0138 
0139     /* Types of the most recently seen LZMA symbols */
0140     enum lzma_state state;
0141 
0142     /*
0143      * Length of a match. This is updated so that dict_repeat can
0144      * be called again to finish repeating the whole match.
0145      */
0146     uint32_t len;
0147 
0148     /*
0149      * LZMA properties or related bit masks (number of literal
0150      * context bits, a mask dervied from the number of literal
0151      * position bits, and a mask dervied from the number
0152      * position bits)
0153      */
0154     uint32_t lc;
0155     uint32_t literal_pos_mask; /* (1 << lp) - 1 */
0156     uint32_t pos_mask;         /* (1 << pb) - 1 */
0157 
0158     /* If 1, it's a match. Otherwise it's a single 8-bit literal. */
0159     uint16_t is_match[STATES][POS_STATES_MAX];
0160 
0161     /* If 1, it's a repeated match. The distance is one of rep0 .. rep3. */
0162     uint16_t is_rep[STATES];
0163 
0164     /*
0165      * If 0, distance of a repeated match is rep0.
0166      * Otherwise check is_rep1.
0167      */
0168     uint16_t is_rep0[STATES];
0169 
0170     /*
0171      * If 0, distance of a repeated match is rep1.
0172      * Otherwise check is_rep2.
0173      */
0174     uint16_t is_rep1[STATES];
0175 
0176     /* If 0, distance of a repeated match is rep2. Otherwise it is rep3. */
0177     uint16_t is_rep2[STATES];
0178 
0179     /*
0180      * If 1, the repeated match has length of one byte. Otherwise
0181      * the length is decoded from rep_len_decoder.
0182      */
0183     uint16_t is_rep0_long[STATES][POS_STATES_MAX];
0184 
0185     /*
0186      * Probability tree for the highest two bits of the match
0187      * distance. There is a separate probability tree for match
0188      * lengths of 2 (i.e. MATCH_LEN_MIN), 3, 4, and [5, 273].
0189      */
0190     uint16_t dist_slot[DIST_STATES][DIST_SLOTS];
0191 
0192     /*
0193      * Probility trees for additional bits for match distance
0194      * when the distance is in the range [4, 127].
0195      */
0196     uint16_t dist_special[FULL_DISTANCES - DIST_MODEL_END];
0197 
0198     /*
0199      * Probability tree for the lowest four bits of a match
0200      * distance that is equal to or greater than 128.
0201      */
0202     uint16_t dist_align[ALIGN_SIZE];
0203 
0204     /* Length of a normal match */
0205     struct lzma_len_dec match_len_dec;
0206 
0207     /* Length of a repeated match */
0208     struct lzma_len_dec rep_len_dec;
0209 
0210     /* Probabilities of literals */
0211     uint16_t literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE];
0212 };
0213 
0214 struct lzma2_dec {
0215     /* Position in xz_dec_lzma2_run(). */
0216     enum lzma2_seq {
0217         SEQ_CONTROL,
0218         SEQ_UNCOMPRESSED_1,
0219         SEQ_UNCOMPRESSED_2,
0220         SEQ_COMPRESSED_0,
0221         SEQ_COMPRESSED_1,
0222         SEQ_PROPERTIES,
0223         SEQ_LZMA_PREPARE,
0224         SEQ_LZMA_RUN,
0225         SEQ_COPY
0226     } sequence;
0227 
0228     /* Next position after decoding the compressed size of the chunk. */
0229     enum lzma2_seq next_sequence;
0230 
0231     /* Uncompressed size of LZMA chunk (2 MiB at maximum) */
0232     uint32_t uncompressed;
0233 
0234     /*
0235      * Compressed size of LZMA chunk or compressed/uncompressed
0236      * size of uncompressed chunk (64 KiB at maximum)
0237      */
0238     uint32_t compressed;
0239 
0240     /*
0241      * True if dictionary reset is needed. This is false before
0242      * the first chunk (LZMA or uncompressed).
0243      */
0244     bool need_dict_reset;
0245 
0246     /*
0247      * True if new LZMA properties are needed. This is false
0248      * before the first LZMA chunk.
0249      */
0250     bool need_props;
0251 };
0252 
0253 struct xz_dec_lzma2 {
0254     /*
0255      * The order below is important on x86 to reduce code size and
0256      * it shouldn't hurt on other platforms. Everything up to and
0257      * including lzma.pos_mask are in the first 128 bytes on x86-32,
0258      * which allows using smaller instructions to access those
0259      * variables. On x86-64, fewer variables fit into the first 128
0260      * bytes, but this is still the best order without sacrificing
0261      * the readability by splitting the structures.
0262      */
0263     struct rc_dec rc;
0264     struct dictionary dict;
0265     struct lzma2_dec lzma2;
0266     struct lzma_dec lzma;
0267 
0268     /*
0269      * Temporary buffer which holds small number of input bytes between
0270      * decoder calls. See lzma2_lzma() for details.
0271      */
0272     struct {
0273         uint32_t size;
0274         uint8_t buf[3 * LZMA_IN_REQUIRED];
0275     } temp;
0276 };
0277 
0278 /**************
0279  * Dictionary *
0280  **************/
0281 
0282 /*
0283  * Reset the dictionary state. When in single-call mode, set up the beginning
0284  * of the dictionary to point to the actual output buffer.
0285  */
0286 static void dict_reset(struct dictionary *dict, struct xz_buf *b)
0287 {
0288     if (DEC_IS_SINGLE(dict->mode)) {
0289         dict->buf = b->out + b->out_pos;
0290         dict->end = b->out_size - b->out_pos;
0291     }
0292 
0293     dict->start = 0;
0294     dict->pos = 0;
0295     dict->limit = 0;
0296     dict->full = 0;
0297 }
0298 
0299 /* Set dictionary write limit */
0300 static void dict_limit(struct dictionary *dict, size_t out_max)
0301 {
0302     if (dict->end - dict->pos <= out_max)
0303         dict->limit = dict->end;
0304     else
0305         dict->limit = dict->pos + out_max;
0306 }
0307 
0308 /* Return true if at least one byte can be written into the dictionary. */
0309 static inline bool dict_has_space(const struct dictionary *dict)
0310 {
0311     return dict->pos < dict->limit;
0312 }
0313 
0314 /*
0315  * Get a byte from the dictionary at the given distance. The distance is
0316  * assumed to valid, or as a special case, zero when the dictionary is
0317  * still empty. This special case is needed for single-call decoding to
0318  * avoid writing a '\0' to the end of the destination buffer.
0319  */
0320 static inline uint32_t dict_get(const struct dictionary *dict, uint32_t dist)
0321 {
0322     size_t offset = dict->pos - dist - 1;
0323 
0324     if (dist >= dict->pos)
0325         offset += dict->end;
0326 
0327     return dict->full > 0 ? dict->buf[offset] : 0;
0328 }
0329 
0330 /*
0331  * Put one byte into the dictionary. It is assumed that there is space for it.
0332  */
0333 static inline void dict_put(struct dictionary *dict, uint8_t byte)
0334 {
0335     dict->buf[dict->pos++] = byte;
0336 
0337     if (dict->full < dict->pos)
0338         dict->full = dict->pos;
0339 }
0340 
0341 /*
0342  * Repeat given number of bytes from the given distance. If the distance is
0343  * invalid, false is returned. On success, true is returned and *len is
0344  * updated to indicate how many bytes were left to be repeated.
0345  */
0346 static bool dict_repeat(struct dictionary *dict, uint32_t *len, uint32_t dist)
0347 {
0348     size_t back;
0349     uint32_t left;
0350 
0351     if (dist >= dict->full || dist >= dict->size)
0352         return false;
0353 
0354     left = min_t(size_t, dict->limit - dict->pos, *len);
0355     *len -= left;
0356 
0357     back = dict->pos - dist - 1;
0358     if (dist >= dict->pos)
0359         back += dict->end;
0360 
0361     do {
0362         dict->buf[dict->pos++] = dict->buf[back++];
0363         if (back == dict->end)
0364             back = 0;
0365     } while (--left > 0);
0366 
0367     if (dict->full < dict->pos)
0368         dict->full = dict->pos;
0369 
0370     return true;
0371 }
0372 
0373 /* Copy uncompressed data as is from input to dictionary and output buffers. */
0374 static void dict_uncompressed(struct dictionary *dict, struct xz_buf *b,
0375                   uint32_t *left)
0376 {
0377     size_t copy_size;
0378 
0379     while (*left > 0 && b->in_pos < b->in_size
0380             && b->out_pos < b->out_size) {
0381         copy_size = min(b->in_size - b->in_pos,
0382                 b->out_size - b->out_pos);
0383         if (copy_size > dict->end - dict->pos)
0384             copy_size = dict->end - dict->pos;
0385         if (copy_size > *left)
0386             copy_size = *left;
0387 
0388         *left -= copy_size;
0389 
0390         memcpy(dict->buf + dict->pos, b->in + b->in_pos, copy_size);
0391         dict->pos += copy_size;
0392 
0393         if (dict->full < dict->pos)
0394             dict->full = dict->pos;
0395 
0396         if (DEC_IS_MULTI(dict->mode)) {
0397             if (dict->pos == dict->end)
0398                 dict->pos = 0;
0399 
0400             memcpy(b->out + b->out_pos, b->in + b->in_pos,
0401                     copy_size);
0402         }
0403 
0404         dict->start = dict->pos;
0405 
0406         b->out_pos += copy_size;
0407         b->in_pos += copy_size;
0408     }
0409 }
0410 
0411 /*
0412  * Flush pending data from dictionary to b->out. It is assumed that there is
0413  * enough space in b->out. This is guaranteed because caller uses dict_limit()
0414  * before decoding data into the dictionary.
0415  */
0416 static uint32_t dict_flush(struct dictionary *dict, struct xz_buf *b)
0417 {
0418     size_t copy_size = dict->pos - dict->start;
0419 
0420     if (DEC_IS_MULTI(dict->mode)) {
0421         if (dict->pos == dict->end)
0422             dict->pos = 0;
0423 
0424         memcpy(b->out + b->out_pos, dict->buf + dict->start,
0425                 copy_size);
0426     }
0427 
0428     dict->start = dict->pos;
0429     b->out_pos += copy_size;
0430     return copy_size;
0431 }
0432 
0433 /*****************
0434  * Range decoder *
0435  *****************/
0436 
0437 /* Reset the range decoder. */
0438 static void rc_reset(struct rc_dec *rc)
0439 {
0440     rc->range = (uint32_t)-1;
0441     rc->code = 0;
0442     rc->init_bytes_left = RC_INIT_BYTES;
0443 }
0444 
0445 /*
0446  * Read the first five initial bytes into rc->code if they haven't been
0447  * read already. (Yes, the first byte gets completely ignored.)
0448  */
0449 static bool rc_read_init(struct rc_dec *rc, struct xz_buf *b)
0450 {
0451     while (rc->init_bytes_left > 0) {
0452         if (b->in_pos == b->in_size)
0453             return false;
0454 
0455         rc->code = (rc->code << 8) + b->in[b->in_pos++];
0456         --rc->init_bytes_left;
0457     }
0458 
0459     return true;
0460 }
0461 
0462 /* Return true if there may not be enough input for the next decoding loop. */
0463 static inline bool rc_limit_exceeded(const struct rc_dec *rc)
0464 {
0465     return rc->in_pos > rc->in_limit;
0466 }
0467 
0468 /*
0469  * Return true if it is possible (from point of view of range decoder) that
0470  * we have reached the end of the LZMA chunk.
0471  */
0472 static inline bool rc_is_finished(const struct rc_dec *rc)
0473 {
0474     return rc->code == 0;
0475 }
0476 
0477 #ifdef __rtems__
0478 #pragma GCC diagnostic push
0479 #pragma GCC diagnostic ignored "-Wattributes"
0480 #endif /* __rtems__ */
0481 /* Read the next input byte if needed. */
0482 static __always_inline void rc_normalize(struct rc_dec *rc)
0483 {
0484     if (rc->range < RC_TOP_VALUE) {
0485         rc->range <<= RC_SHIFT_BITS;
0486         rc->code = (rc->code << RC_SHIFT_BITS) + rc->in[rc->in_pos++];
0487     }
0488 }
0489 
0490 /*
0491  * Decode one bit. In some versions, this function has been splitted in three
0492  * functions so that the compiler is supposed to be able to more easily avoid
0493  * an extra branch. In this particular version of the LZMA decoder, this
0494  * doesn't seem to be a good idea (tested with GCC 3.3.6, 3.4.6, and 4.3.3
0495  * on x86). Using a non-splitted version results in nicer looking code too.
0496  *
0497  * NOTE: This must return an int. Do not make it return a bool or the speed
0498  * of the code generated by GCC 3.x decreases 10-15 %. (GCC 4.3 doesn't care,
0499  * and it generates 10-20 % faster code than GCC 3.x from this file anyway.)
0500  */
0501 static __always_inline int rc_bit(struct rc_dec *rc, uint16_t *prob)
0502 {
0503     uint32_t bound;
0504     int bit;
0505 
0506     rc_normalize(rc);
0507     bound = (rc->range >> RC_BIT_MODEL_TOTAL_BITS) * *prob;
0508     if (rc->code < bound) {
0509         rc->range = bound;
0510         *prob += (RC_BIT_MODEL_TOTAL - *prob) >> RC_MOVE_BITS;
0511         bit = 0;
0512     } else {
0513         rc->range -= bound;
0514         rc->code -= bound;
0515         *prob -= *prob >> RC_MOVE_BITS;
0516         bit = 1;
0517     }
0518 
0519     return bit;
0520 }
0521 
0522 /* Decode a bittree starting from the most significant bit. */
0523 static __always_inline uint32_t rc_bittree(struct rc_dec *rc,
0524                        uint16_t *probs, uint32_t limit)
0525 {
0526     uint32_t symbol = 1;
0527 
0528     do {
0529         if (rc_bit(rc, &probs[symbol]))
0530             symbol = (symbol << 1) + 1;
0531         else
0532             symbol <<= 1;
0533     } while (symbol < limit);
0534 
0535     return symbol;
0536 }
0537 
0538 /* Decode a bittree starting from the least significant bit. */
0539 static __always_inline void rc_bittree_reverse(struct rc_dec *rc,
0540                            uint16_t *probs,
0541                            uint32_t *dest, uint32_t limit)
0542 {
0543     uint32_t symbol = 1;
0544     uint32_t i = 0;
0545 
0546     do {
0547         if (rc_bit(rc, &probs[symbol])) {
0548             symbol = (symbol << 1) + 1;
0549             *dest += 1 << i;
0550         } else {
0551             symbol <<= 1;
0552         }
0553     } while (++i < limit);
0554 }
0555 #ifdef __rtems__
0556 #pragma GCC diagnostic pop
0557 #endif /* __rtems__ */
0558 
0559 /* Decode direct bits (fixed fifty-fifty probability) */
0560 static inline void rc_direct(struct rc_dec *rc, uint32_t *dest, uint32_t limit)
0561 {
0562     uint32_t mask;
0563 
0564     do {
0565         rc_normalize(rc);
0566         rc->range >>= 1;
0567         rc->code -= rc->range;
0568         mask = (uint32_t)0 - (rc->code >> 31);
0569         rc->code += rc->range & mask;
0570         *dest = (*dest << 1) + (mask + 1);
0571     } while (--limit > 0);
0572 }
0573 
0574 /********
0575  * LZMA *
0576  ********/
0577 
0578 /* Get pointer to literal coder probability array. */
0579 static uint16_t *lzma_literal_probs(struct xz_dec_lzma2 *s)
0580 {
0581     uint32_t prev_byte = dict_get(&s->dict, 0);
0582     uint32_t low = prev_byte >> (8 - s->lzma.lc);
0583     uint32_t high = (s->dict.pos & s->lzma.literal_pos_mask) << s->lzma.lc;
0584     return s->lzma.literal[low + high];
0585 }
0586 
0587 /* Decode a literal (one 8-bit byte) */
0588 static void lzma_literal(struct xz_dec_lzma2 *s)
0589 {
0590     uint16_t *probs;
0591     uint32_t symbol;
0592     uint32_t match_byte;
0593     uint32_t match_bit;
0594     uint32_t offset;
0595     uint32_t i;
0596 
0597     probs = lzma_literal_probs(s);
0598 
0599     if (lzma_state_is_literal(s->lzma.state)) {
0600         symbol = rc_bittree(&s->rc, probs, 0x100);
0601     } else {
0602         symbol = 1;
0603         match_byte = dict_get(&s->dict, s->lzma.rep0) << 1;
0604         offset = 0x100;
0605 
0606         do {
0607             match_bit = match_byte & offset;
0608             match_byte <<= 1;
0609             i = offset + match_bit + symbol;
0610 
0611             if (rc_bit(&s->rc, &probs[i])) {
0612                 symbol = (symbol << 1) + 1;
0613                 offset &= match_bit;
0614             } else {
0615                 symbol <<= 1;
0616                 offset &= ~match_bit;
0617             }
0618         } while (symbol < 0x100);
0619     }
0620 
0621     dict_put(&s->dict, (uint8_t)symbol);
0622     lzma_state_literal(&s->lzma.state);
0623 }
0624 
0625 /* Decode the length of the match into s->lzma.len. */
0626 static void lzma_len(struct xz_dec_lzma2 *s, struct lzma_len_dec *l,
0627              uint32_t pos_state)
0628 {
0629     uint16_t *probs;
0630     uint32_t limit;
0631 
0632     if (!rc_bit(&s->rc, &l->choice)) {
0633         probs = l->low[pos_state];
0634         limit = LEN_LOW_SYMBOLS;
0635         s->lzma.len = MATCH_LEN_MIN;
0636     } else {
0637         if (!rc_bit(&s->rc, &l->choice2)) {
0638             probs = l->mid[pos_state];
0639             limit = LEN_MID_SYMBOLS;
0640             s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS;
0641         } else {
0642             probs = l->high;
0643             limit = LEN_HIGH_SYMBOLS;
0644             s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS
0645                     + LEN_MID_SYMBOLS;
0646         }
0647     }
0648 
0649     s->lzma.len += rc_bittree(&s->rc, probs, limit) - limit;
0650 }
0651 
0652 /* Decode a match. The distance will be stored in s->lzma.rep0. */
0653 static void lzma_match(struct xz_dec_lzma2 *s, uint32_t pos_state)
0654 {
0655     uint16_t *probs;
0656     uint32_t dist_slot;
0657     uint32_t limit;
0658 
0659     lzma_state_match(&s->lzma.state);
0660 
0661     s->lzma.rep3 = s->lzma.rep2;
0662     s->lzma.rep2 = s->lzma.rep1;
0663     s->lzma.rep1 = s->lzma.rep0;
0664 
0665     lzma_len(s, &s->lzma.match_len_dec, pos_state);
0666 
0667     probs = s->lzma.dist_slot[lzma_get_dist_state(s->lzma.len)];
0668     dist_slot = rc_bittree(&s->rc, probs, DIST_SLOTS) - DIST_SLOTS;
0669 
0670     if (dist_slot < DIST_MODEL_START) {
0671         s->lzma.rep0 = dist_slot;
0672     } else {
0673         limit = (dist_slot >> 1) - 1;
0674         s->lzma.rep0 = 2 + (dist_slot & 1);
0675 
0676         if (dist_slot < DIST_MODEL_END) {
0677             s->lzma.rep0 <<= limit;
0678             probs = s->lzma.dist_special + s->lzma.rep0
0679                     - dist_slot - 1;
0680             rc_bittree_reverse(&s->rc, probs,
0681                     &s->lzma.rep0, limit);
0682         } else {
0683             rc_direct(&s->rc, &s->lzma.rep0, limit - ALIGN_BITS);
0684             s->lzma.rep0 <<= ALIGN_BITS;
0685             rc_bittree_reverse(&s->rc, s->lzma.dist_align,
0686                     &s->lzma.rep0, ALIGN_BITS);
0687         }
0688     }
0689 }
0690 
0691 /*
0692  * Decode a repeated match. The distance is one of the four most recently
0693  * seen matches. The distance will be stored in s->lzma.rep0.
0694  */
0695 static void lzma_rep_match(struct xz_dec_lzma2 *s, uint32_t pos_state)
0696 {
0697     uint32_t tmp;
0698 
0699     if (!rc_bit(&s->rc, &s->lzma.is_rep0[s->lzma.state])) {
0700         if (!rc_bit(&s->rc, &s->lzma.is_rep0_long[
0701                 s->lzma.state][pos_state])) {
0702             lzma_state_short_rep(&s->lzma.state);
0703             s->lzma.len = 1;
0704             return;
0705         }
0706     } else {
0707         if (!rc_bit(&s->rc, &s->lzma.is_rep1[s->lzma.state])) {
0708             tmp = s->lzma.rep1;
0709         } else {
0710             if (!rc_bit(&s->rc, &s->lzma.is_rep2[s->lzma.state])) {
0711                 tmp = s->lzma.rep2;
0712             } else {
0713                 tmp = s->lzma.rep3;
0714                 s->lzma.rep3 = s->lzma.rep2;
0715             }
0716 
0717             s->lzma.rep2 = s->lzma.rep1;
0718         }
0719 
0720         s->lzma.rep1 = s->lzma.rep0;
0721         s->lzma.rep0 = tmp;
0722     }
0723 
0724     lzma_state_long_rep(&s->lzma.state);
0725     lzma_len(s, &s->lzma.rep_len_dec, pos_state);
0726 }
0727 
0728 /* LZMA decoder core */
0729 static bool lzma_main(struct xz_dec_lzma2 *s)
0730 {
0731     uint32_t pos_state;
0732 
0733     /*
0734      * If the dictionary was reached during the previous call, try to
0735      * finish the possibly pending repeat in the dictionary.
0736      */
0737     if (dict_has_space(&s->dict) && s->lzma.len > 0)
0738         dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0);
0739 
0740     /*
0741      * Decode more LZMA symbols. One iteration may consume up to
0742      * LZMA_IN_REQUIRED - 1 bytes.
0743      */
0744     while (dict_has_space(&s->dict) && !rc_limit_exceeded(&s->rc)) {
0745         pos_state = s->dict.pos & s->lzma.pos_mask;
0746 
0747         if (!rc_bit(&s->rc, &s->lzma.is_match[
0748                 s->lzma.state][pos_state])) {
0749             lzma_literal(s);
0750         } else {
0751             if (rc_bit(&s->rc, &s->lzma.is_rep[s->lzma.state]))
0752                 lzma_rep_match(s, pos_state);
0753             else
0754                 lzma_match(s, pos_state);
0755 
0756             if (!dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0))
0757                 return false;
0758         }
0759     }
0760 
0761     /*
0762      * Having the range decoder always normalized when we are outside
0763      * this function makes it easier to correctly handle end of the chunk.
0764      */
0765     rc_normalize(&s->rc);
0766 
0767     return true;
0768 }
0769 
0770 /*
0771  * Reset the LZMA decoder and range decoder state. Dictionary is nore reset
0772  * here, because LZMA state may be reset without resetting the dictionary.
0773  */
0774 static void lzma_reset(struct xz_dec_lzma2 *s)
0775 {
0776     uint16_t *probs;
0777     size_t i;
0778 
0779     s->lzma.state = STATE_LIT_LIT;
0780     s->lzma.rep0 = 0;
0781     s->lzma.rep1 = 0;
0782     s->lzma.rep2 = 0;
0783     s->lzma.rep3 = 0;
0784 
0785     /*
0786      * All probabilities are initialized to the same value. This hack
0787      * makes the code smaller by avoiding a separate loop for each
0788      * probability array.
0789      *
0790      * This could be optimized so that only that part of literal
0791      * probabilities that are actually required. In the common case
0792      * we would write 12 KiB less.
0793      */
0794     probs = s->lzma.is_match[0];
0795     for (i = 0; i < PROBS_TOTAL; ++i)
0796         probs[i] = RC_BIT_MODEL_TOTAL / 2;
0797 
0798     rc_reset(&s->rc);
0799 }
0800 
0801 /*
0802  * Decode and validate LZMA properties (lc/lp/pb) and calculate the bit masks
0803  * from the decoded lp and pb values. On success, the LZMA decoder state is
0804  * reset and true is returned.
0805  */
0806 static bool lzma_props(struct xz_dec_lzma2 *s, uint8_t props)
0807 {
0808     if (props > (4 * 5 + 4) * 9 + 8)
0809         return false;
0810 
0811     s->lzma.pos_mask = 0;
0812     while (props >= 9 * 5) {
0813         props -= 9 * 5;
0814         ++s->lzma.pos_mask;
0815     }
0816 
0817     s->lzma.pos_mask = (1 << s->lzma.pos_mask) - 1;
0818 
0819     s->lzma.literal_pos_mask = 0;
0820     while (props >= 9) {
0821         props -= 9;
0822         ++s->lzma.literal_pos_mask;
0823     }
0824 
0825     s->lzma.lc = props;
0826 
0827     if (s->lzma.lc + s->lzma.literal_pos_mask > 4)
0828         return false;
0829 
0830     s->lzma.literal_pos_mask = (1 << s->lzma.literal_pos_mask) - 1;
0831 
0832     lzma_reset(s);
0833 
0834     return true;
0835 }
0836 
0837 /*********
0838  * LZMA2 *
0839  *********/
0840 
0841 /*
0842  * The LZMA decoder assumes that if the input limit (s->rc.in_limit) hasn't
0843  * been exceeded, it is safe to read up to LZMA_IN_REQUIRED bytes. This
0844  * wrapper function takes care of making the LZMA decoder's assumption safe.
0845  *
0846  * As long as there is plenty of input left to be decoded in the current LZMA
0847  * chunk, we decode directly from the caller-supplied input buffer until
0848  * there's LZMA_IN_REQUIRED bytes left. Those remaining bytes are copied into
0849  * s->temp.buf, which (hopefully) gets filled on the next call to this
0850  * function. We decode a few bytes from the temporary buffer so that we can
0851  * continue decoding from the caller-supplied input buffer again.
0852  */
0853 static bool lzma2_lzma(struct xz_dec_lzma2 *s, struct xz_buf *b)
0854 {
0855     size_t in_avail;
0856     uint32_t tmp;
0857 
0858     in_avail = b->in_size - b->in_pos;
0859     if (s->temp.size > 0 || s->lzma2.compressed == 0) {
0860         tmp = 2 * LZMA_IN_REQUIRED - s->temp.size;
0861         if (tmp > s->lzma2.compressed - s->temp.size)
0862             tmp = s->lzma2.compressed - s->temp.size;
0863         if (tmp > in_avail)
0864             tmp = in_avail;
0865 
0866         memcpy(s->temp.buf + s->temp.size, b->in + b->in_pos, tmp);
0867 
0868         if (s->temp.size + tmp == s->lzma2.compressed) {
0869             memzero(s->temp.buf + s->temp.size + tmp,
0870                     sizeof(s->temp.buf)
0871                         - s->temp.size - tmp);
0872             s->rc.in_limit = s->temp.size + tmp;
0873         } else if (s->temp.size + tmp < LZMA_IN_REQUIRED) {
0874             s->temp.size += tmp;
0875             b->in_pos += tmp;
0876             return true;
0877         } else {
0878             s->rc.in_limit = s->temp.size + tmp - LZMA_IN_REQUIRED;
0879         }
0880 
0881         s->rc.in = s->temp.buf;
0882         s->rc.in_pos = 0;
0883 
0884         if (!lzma_main(s) || s->rc.in_pos > s->temp.size + tmp)
0885             return false;
0886 
0887         s->lzma2.compressed -= s->rc.in_pos;
0888 
0889         if (s->rc.in_pos < s->temp.size) {
0890             s->temp.size -= s->rc.in_pos;
0891             memmove(s->temp.buf, s->temp.buf + s->rc.in_pos,
0892                     s->temp.size);
0893             return true;
0894         }
0895 
0896         b->in_pos += s->rc.in_pos - s->temp.size;
0897         s->temp.size = 0;
0898     }
0899 
0900     in_avail = b->in_size - b->in_pos;
0901     if (in_avail >= LZMA_IN_REQUIRED) {
0902         s->rc.in = b->in;
0903         s->rc.in_pos = b->in_pos;
0904 
0905         if (in_avail >= s->lzma2.compressed + LZMA_IN_REQUIRED)
0906             s->rc.in_limit = b->in_pos + s->lzma2.compressed;
0907         else
0908             s->rc.in_limit = b->in_size - LZMA_IN_REQUIRED;
0909 
0910         if (!lzma_main(s))
0911             return false;
0912 
0913         in_avail = s->rc.in_pos - b->in_pos;
0914         if (in_avail > s->lzma2.compressed)
0915             return false;
0916 
0917         s->lzma2.compressed -= in_avail;
0918         b->in_pos = s->rc.in_pos;
0919     }
0920 
0921     in_avail = b->in_size - b->in_pos;
0922     if (in_avail < LZMA_IN_REQUIRED) {
0923         if (in_avail > s->lzma2.compressed)
0924             in_avail = s->lzma2.compressed;
0925 
0926         memcpy(s->temp.buf, b->in + b->in_pos, in_avail);
0927         s->temp.size = in_avail;
0928         b->in_pos += in_avail;
0929     }
0930 
0931     return true;
0932 }
0933 
0934 /*
0935  * Take care of the LZMA2 control layer, and forward the job of actual LZMA
0936  * decoding or copying of uncompressed chunks to other functions.
0937  */
0938 XZ_EXTERN enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s,
0939                        struct xz_buf *b)
0940 {
0941     uint32_t tmp;
0942 
0943     while (b->in_pos < b->in_size || s->lzma2.sequence == SEQ_LZMA_RUN) {
0944         switch (s->lzma2.sequence) {
0945         case SEQ_CONTROL:
0946             /*
0947              * LZMA2 control byte
0948              *
0949              * Exact values:
0950              *   0x00   End marker
0951              *   0x01   Dictionary reset followed by
0952              *          an uncompressed chunk
0953              *   0x02   Uncompressed chunk (no dictionary reset)
0954              *
0955              * Highest three bits (s->control & 0xE0):
0956              *   0xE0   Dictionary reset, new properties and state
0957              *          reset, followed by LZMA compressed chunk
0958              *   0xC0   New properties and state reset, followed
0959              *          by LZMA compressed chunk (no dictionary
0960              *          reset)
0961              *   0xA0   State reset using old properties,
0962              *          followed by LZMA compressed chunk (no
0963              *          dictionary reset)
0964              *   0x80   LZMA chunk (no dictionary or state reset)
0965              *
0966              * For LZMA compressed chunks, the lowest five bits
0967              * (s->control & 1F) are the highest bits of the
0968              * uncompressed size (bits 16-20).
0969              *
0970              * A new LZMA2 stream must begin with a dictionary
0971              * reset. The first LZMA chunk must set new
0972              * properties and reset the LZMA state.
0973              *
0974              * Values that don't match anything described above
0975              * are invalid and we return XZ_DATA_ERROR.
0976              */
0977             tmp = b->in[b->in_pos++];
0978 
0979             if (tmp == 0x00)
0980                 return XZ_STREAM_END;
0981 
0982             if (tmp >= 0xE0 || tmp == 0x01) {
0983                 s->lzma2.need_props = true;
0984                 s->lzma2.need_dict_reset = false;
0985                 dict_reset(&s->dict, b);
0986             } else if (s->lzma2.need_dict_reset) {
0987                 return XZ_DATA_ERROR;
0988             }
0989 
0990             if (tmp >= 0x80) {
0991                 s->lzma2.uncompressed = (tmp & 0x1F) << 16;
0992                 s->lzma2.sequence = SEQ_UNCOMPRESSED_1;
0993 
0994                 if (tmp >= 0xC0) {
0995                     /*
0996                      * When there are new properties,
0997                      * state reset is done at
0998                      * SEQ_PROPERTIES.
0999                      */
1000                     s->lzma2.need_props = false;
1001                     s->lzma2.next_sequence
1002                             = SEQ_PROPERTIES;
1003 
1004                 } else if (s->lzma2.need_props) {
1005                     return XZ_DATA_ERROR;
1006 
1007                 } else {
1008                     s->lzma2.next_sequence
1009                             = SEQ_LZMA_PREPARE;
1010                     if (tmp >= 0xA0)
1011                         lzma_reset(s);
1012                 }
1013             } else {
1014                 if (tmp > 0x02)
1015                     return XZ_DATA_ERROR;
1016 
1017                 s->lzma2.sequence = SEQ_COMPRESSED_0;
1018                 s->lzma2.next_sequence = SEQ_COPY;
1019             }
1020 
1021             break;
1022 
1023         case SEQ_UNCOMPRESSED_1:
1024             s->lzma2.uncompressed
1025                     += (uint32_t)b->in[b->in_pos++] << 8;
1026             s->lzma2.sequence = SEQ_UNCOMPRESSED_2;
1027             break;
1028 
1029         case SEQ_UNCOMPRESSED_2:
1030             s->lzma2.uncompressed
1031                     += (uint32_t)b->in[b->in_pos++] + 1;
1032             s->lzma2.sequence = SEQ_COMPRESSED_0;
1033             break;
1034 
1035         case SEQ_COMPRESSED_0:
1036             s->lzma2.compressed
1037                     = (uint32_t)b->in[b->in_pos++] << 8;
1038             s->lzma2.sequence = SEQ_COMPRESSED_1;
1039             break;
1040 
1041         case SEQ_COMPRESSED_1:
1042             s->lzma2.compressed
1043                     += (uint32_t)b->in[b->in_pos++] + 1;
1044             s->lzma2.sequence = s->lzma2.next_sequence;
1045             break;
1046 
1047         case SEQ_PROPERTIES:
1048             if (!lzma_props(s, b->in[b->in_pos++]))
1049                 return XZ_DATA_ERROR;
1050 
1051             s->lzma2.sequence = SEQ_LZMA_PREPARE;
1052 
1053         /* Fall through */
1054 
1055         case SEQ_LZMA_PREPARE:
1056             if (s->lzma2.compressed < RC_INIT_BYTES)
1057                 return XZ_DATA_ERROR;
1058 
1059             if (!rc_read_init(&s->rc, b))
1060                 return XZ_OK;
1061 
1062             s->lzma2.compressed -= RC_INIT_BYTES;
1063             s->lzma2.sequence = SEQ_LZMA_RUN;
1064 
1065         /* Fall through */
1066 
1067         case SEQ_LZMA_RUN:
1068             /*
1069              * Set dictionary limit to indicate how much we want
1070              * to be encoded at maximum. Decode new data into the
1071              * dictionary. Flush the new data from dictionary to
1072              * b->out. Check if we finished decoding this chunk.
1073              * In case the dictionary got full but we didn't fill
1074              * the output buffer yet, we may run this loop
1075              * multiple times without changing s->lzma2.sequence.
1076              */
1077             dict_limit(&s->dict, min_t(size_t,
1078                     b->out_size - b->out_pos,
1079                     s->lzma2.uncompressed));
1080             if (!lzma2_lzma(s, b))
1081                 return XZ_DATA_ERROR;
1082 
1083             s->lzma2.uncompressed -= dict_flush(&s->dict, b);
1084 
1085             if (s->lzma2.uncompressed == 0) {
1086                 if (s->lzma2.compressed > 0 || s->lzma.len > 0
1087                         || !rc_is_finished(&s->rc))
1088                     return XZ_DATA_ERROR;
1089 
1090                 rc_reset(&s->rc);
1091                 s->lzma2.sequence = SEQ_CONTROL;
1092 
1093             } else if (b->out_pos == b->out_size
1094                     || (b->in_pos == b->in_size
1095                         && s->temp.size
1096                         < s->lzma2.compressed)) {
1097                 return XZ_OK;
1098             }
1099 
1100             break;
1101 
1102         case SEQ_COPY:
1103             dict_uncompressed(&s->dict, b, &s->lzma2.compressed);
1104             if (s->lzma2.compressed > 0)
1105                 return XZ_OK;
1106 
1107             s->lzma2.sequence = SEQ_CONTROL;
1108             break;
1109         }
1110     }
1111 
1112     return XZ_OK;
1113 }
1114 
1115 XZ_EXTERN struct xz_dec_lzma2 *xz_dec_lzma2_create(enum xz_mode mode,
1116                            uint32_t dict_max)
1117 {
1118     struct xz_dec_lzma2 *s = kmalloc(sizeof(*s), GFP_KERNEL);
1119     if (s == NULL)
1120         return NULL;
1121 
1122     s->dict.mode = mode;
1123     s->dict.size_max = dict_max;
1124 
1125     if (DEC_IS_PREALLOC(mode)) {
1126         s->dict.buf = vmalloc(dict_max);
1127         if (s->dict.buf == NULL) {
1128             kfree(s);
1129             return NULL;
1130         }
1131     } else if (DEC_IS_DYNALLOC(mode)) {
1132         s->dict.buf = NULL;
1133         s->dict.allocated = 0;
1134     }
1135 
1136     return s;
1137 }
1138 
1139 XZ_EXTERN enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s, uint8_t props)
1140 {
1141     /* This limits dictionary size to 3 GiB to keep parsing simpler. */
1142     if (props > 39)
1143         return XZ_OPTIONS_ERROR;
1144 
1145     s->dict.size = 2 + (props & 1);
1146     s->dict.size <<= (props >> 1) + 11;
1147 
1148     if (DEC_IS_MULTI(s->dict.mode)) {
1149         if (s->dict.size > s->dict.size_max)
1150             return XZ_MEMLIMIT_ERROR;
1151 
1152         s->dict.end = s->dict.size;
1153 
1154         if (DEC_IS_DYNALLOC(s->dict.mode)) {
1155             if (s->dict.allocated < s->dict.size) {
1156                 vfree(s->dict.buf);
1157                 s->dict.buf = vmalloc(s->dict.size);
1158                 if (s->dict.buf == NULL) {
1159                     s->dict.allocated = 0;
1160                     return XZ_MEM_ERROR;
1161                 }
1162             }
1163         }
1164     }
1165 
1166     s->lzma.len = 0;
1167 
1168     s->lzma2.sequence = SEQ_CONTROL;
1169     s->lzma2.need_dict_reset = true;
1170 
1171     s->temp.size = 0;
1172 
1173     return XZ_OK;
1174 }
1175 
1176 XZ_EXTERN void xz_dec_lzma2_end(struct xz_dec_lzma2 *s)
1177 {
1178     if (DEC_IS_MULTI(s->dict.mode))
1179         vfree(s->dict.buf);
1180 
1181     kfree(s);
1182 }