blob: da71cb4d42e63e99b6006d7c1803e38f4be58118 [file] [log] [blame]
Denys Vlasenko602ce692010-05-30 03:35:18 +02001/*
2 * LZMA2 decoder
3 *
4 * Authors: Lasse Collin <lasse.collin@tukaani.org>
5 * Igor Pavlov <http://7-zip.org/>
6 *
7 * This file has been put into the public domain.
8 * You can do whatever you want with this file.
9 */
10
11#include "xz_private.h"
12#include "xz_lzma2.h"
13
14/*
15 * Range decoder initialization eats the first five bytes of each LZMA chunk.
16 */
17#define RC_INIT_BYTES 5
18
19/*
20 * Minimum number of usable input buffer to safely decode one LZMA symbol.
21 * The worst case is that we decode 22 bits using probabilities and 26
22 * direct bits. This may decode at maximum of 20 bytes of input. However,
23 * lzma_main() does an extra normalization before returning, thus we
24 * need to put 21 here.
25 */
26#define LZMA_IN_REQUIRED 21
27
28/*
29 * Dictionary (history buffer)
30 *
31 * These are always true:
32 * start <= pos <= full <= end
33 * pos <= limit <= end
34 *
35 * In multi-call mode, also these are true:
36 * end == size
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +020037 * size <= size_max
38 * allocated <= size
Denys Vlasenko602ce692010-05-30 03:35:18 +020039 *
40 * Most of these variables are size_t to support single-call mode,
41 * in which the dictionary variables address the actual output
42 * buffer directly.
43 */
44struct dictionary {
45 /* Beginning of the history buffer */
46 uint8_t *buf;
47
48 /* Old position in buf (before decoding more data) */
49 size_t start;
50
51 /* Position in buf */
52 size_t pos;
53
54 /*
55 * How full dictionary is. This is used to detect corrupt input that
56 * would read beyond the beginning of the uncompressed stream.
57 */
58 size_t full;
59
60 /* Write limit; we don't write to buf[limit] or later bytes. */
61 size_t limit;
62
63 /*
64 * End of the dictionary buffer. In multi-call mode, this is
65 * the same as the dictionary size. In single-call mode, this
66 * indicates the size of the output buffer.
67 */
68 size_t end;
69
70 /*
71 * Size of the dictionary as specified in Block Header. This is used
72 * together with "full" to detect corrupt input that would make us
73 * read beyond the beginning of the uncompressed stream.
74 */
75 uint32_t size;
76
77 /*
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +020078 * Maximum allowed dictionary size in multi-call mode.
79 * This is ignored in single-call mode.
80 */
81 uint32_t size_max;
82
83 /*
84 * Amount of memory currently allocated for the dictionary.
85 * This is used only with XZ_DYNALLOC. (With XZ_PREALLOC,
86 * size_max is always the same as the allocated size.)
Denys Vlasenko602ce692010-05-30 03:35:18 +020087 */
88 uint32_t allocated;
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +020089
90 /* Operation mode */
91 enum xz_mode mode;
Denys Vlasenko602ce692010-05-30 03:35:18 +020092};
93
94/* Range decoder */
95struct rc_dec {
96 uint32_t range;
97 uint32_t code;
98
99 /*
100 * Number of initializing bytes remaining to be read
101 * by rc_read_init().
102 */
103 uint32_t init_bytes_left;
104
105 /*
106 * Buffer from which we read our input. It can be either
107 * temp.buf or the caller-provided input buffer.
108 */
109 const uint8_t *in;
110 size_t in_pos;
111 size_t in_limit;
112};
113
114/* Probabilities for a length decoder. */
115struct lzma_len_dec {
116 /* Probability of match length being at least 10 */
117 uint16_t choice;
118
119 /* Probability of match length being at least 18 */
120 uint16_t choice2;
121
122 /* Probabilities for match lengths 2-9 */
123 uint16_t low[POS_STATES_MAX][LEN_LOW_SYMBOLS];
124
125 /* Probabilities for match lengths 10-17 */
126 uint16_t mid[POS_STATES_MAX][LEN_MID_SYMBOLS];
127
128 /* Probabilities for match lengths 18-273 */
129 uint16_t high[LEN_HIGH_SYMBOLS];
130};
131
132struct lzma_dec {
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +0200133 /* Distances of latest four matches */
134 uint32_t rep0;
135 uint32_t rep1;
136 uint32_t rep2;
137 uint32_t rep3;
138
139 /* Types of the most recently seen LZMA symbols */
140 enum lzma_state state;
141
142 /*
143 * Length of a match. This is updated so that dict_repeat can
144 * be called again to finish repeating the whole match.
145 */
146 uint32_t len;
147
Denys Vlasenko602ce692010-05-30 03:35:18 +0200148 /*
149 * LZMA properties or related bit masks (number of literal
150 * context bits, a mask dervied from the number of literal
151 * position bits, and a mask dervied from the number
152 * position bits)
153 */
154 uint32_t lc;
155 uint32_t literal_pos_mask; /* (1 << lp) - 1 */
156 uint32_t pos_mask; /* (1 << pb) - 1 */
157
Denys Vlasenko602ce692010-05-30 03:35:18 +0200158 /* If 1, it's a match. Otherwise it's a single 8-bit literal. */
159 uint16_t is_match[STATES][POS_STATES_MAX];
160
161 /* If 1, it's a repeated match. The distance is one of rep0 .. rep3. */
162 uint16_t is_rep[STATES];
163
164 /*
165 * If 0, distance of a repeated match is rep0.
166 * Otherwise check is_rep1.
167 */
168 uint16_t is_rep0[STATES];
169
170 /*
171 * If 0, distance of a repeated match is rep1.
172 * Otherwise check is_rep2.
173 */
174 uint16_t is_rep1[STATES];
175
176 /* If 0, distance of a repeated match is rep2. Otherwise it is rep3. */
177 uint16_t is_rep2[STATES];
178
179 /*
180 * If 1, the repeated match has length of one byte. Otherwise
181 * the length is decoded from rep_len_decoder.
182 */
183 uint16_t is_rep0_long[STATES][POS_STATES_MAX];
184
185 /*
186 * Probability tree for the highest two bits of the match
187 * distance. There is a separate probability tree for match
188 * lengths of 2 (i.e. MATCH_LEN_MIN), 3, 4, and [5, 273].
189 */
190 uint16_t dist_slot[DIST_STATES][DIST_SLOTS];
191
192 /*
193 * Probility trees for additional bits for match distance
194 * when the distance is in the range [4, 127].
195 */
196 uint16_t dist_special[FULL_DISTANCES - DIST_MODEL_END];
197
198 /*
199 * Probability tree for the lowest four bits of a match
200 * distance that is equal to or greater than 128.
201 */
202 uint16_t dist_align[ALIGN_SIZE];
203
204 /* Length of a normal match */
205 struct lzma_len_dec match_len_dec;
206
207 /* Length of a repeated match */
208 struct lzma_len_dec rep_len_dec;
209
210 /* Probabilities of literals */
211 uint16_t literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE];
212};
213
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +0200214struct lzma2_dec {
215 /* Position in xz_dec_lzma2_run(). */
216 enum lzma2_seq {
217 SEQ_CONTROL,
218 SEQ_UNCOMPRESSED_1,
219 SEQ_UNCOMPRESSED_2,
220 SEQ_COMPRESSED_0,
221 SEQ_COMPRESSED_1,
222 SEQ_PROPERTIES,
223 SEQ_LZMA_PREPARE,
224 SEQ_LZMA_RUN,
225 SEQ_COPY
226 } sequence;
227
228 /* Next position after decoding the compressed size of the chunk. */
229 enum lzma2_seq next_sequence;
230
231 /* Uncompressed size of LZMA chunk (2 MiB at maximum) */
232 uint32_t uncompressed;
233
234 /*
235 * Compressed size of LZMA chunk or compressed/uncompressed
236 * size of uncompressed chunk (64 KiB at maximum)
237 */
238 uint32_t compressed;
239
240 /*
241 * True if dictionary reset is needed. This is false before
242 * the first chunk (LZMA or uncompressed).
243 */
244 bool need_dict_reset;
245
246 /*
247 * True if new LZMA properties are needed. This is false
248 * before the first LZMA chunk.
249 */
250 bool need_props;
251};
252
Denys Vlasenko602ce692010-05-30 03:35:18 +0200253struct xz_dec_lzma2 {
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +0200254 /*
255 * The order below is important on x86 to reduce code size and
256 * it shouldn't hurt on other platforms. Everything up to and
257 * including lzma.pos_mask are in the first 128 bytes on x86-32,
258 * which allows using smaller instructions to access those
259 * variables. On x86-64, fewer variables fit into the first 128
260 * bytes, but this is still the best order without sacrificing
261 * the readability by splitting the structures.
262 */
263 struct rc_dec rc;
264 struct dictionary dict;
265 struct lzma2_dec lzma2;
266 struct lzma_dec lzma;
Denys Vlasenko602ce692010-05-30 03:35:18 +0200267
268 /*
269 * Temporary buffer which holds small number of input bytes between
270 * decoder calls. See lzma2_lzma() for details.
271 */
272 struct {
273 uint32_t size;
274 uint8_t buf[3 * LZMA_IN_REQUIRED];
275 } temp;
Denys Vlasenko602ce692010-05-30 03:35:18 +0200276};
277
278/**************
279 * Dictionary *
280 **************/
281
282/*
283 * Reset the dictionary state. When in single-call mode, set up the beginning
284 * of the dictionary to point to the actual output buffer.
285 */
286static void XZ_FUNC dict_reset(struct dictionary *dict, struct xz_buf *b)
287{
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +0200288 if (DEC_IS_SINGLE(dict->mode)) {
Denys Vlasenko602ce692010-05-30 03:35:18 +0200289 dict->buf = b->out + b->out_pos;
290 dict->end = b->out_size - b->out_pos;
291 }
292
293 dict->start = 0;
294 dict->pos = 0;
295 dict->limit = 0;
296 dict->full = 0;
297}
298
299/* Set dictionary write limit */
300static void XZ_FUNC dict_limit(struct dictionary *dict, size_t out_max)
301{
302 if (dict->end - dict->pos <= out_max)
303 dict->limit = dict->end;
304 else
305 dict->limit = dict->pos + out_max;
306}
307
308/* Return true if at least one byte can be written into the dictionary. */
309static __always_inline bool XZ_FUNC dict_has_space(const struct dictionary *dict)
310{
311 return dict->pos < dict->limit;
312}
313
314/*
315 * Get a byte from the dictionary at the given distance. The distance is
316 * assumed to valid, or as a special case, zero when the dictionary is
317 * still empty. This special case is needed for single-call decoding to
318 * avoid writing a '\0' to the end of the destination buffer.
319 */
320static __always_inline uint32_t XZ_FUNC dict_get(
321 const struct dictionary *dict, uint32_t dist)
322{
323 size_t offset = dict->pos - dist - 1;
324
325 if (dist >= dict->pos)
326 offset += dict->end;
327
328 return dict->full > 0 ? dict->buf[offset] : 0;
329}
330
331/*
332 * Put one byte into the dictionary. It is assumed that there is space for it.
333 */
334static inline void XZ_FUNC dict_put(struct dictionary *dict, uint8_t byte)
335{
336 dict->buf[dict->pos++] = byte;
337
338 if (dict->full < dict->pos)
339 dict->full = dict->pos;
340}
341
342/*
343 * Repeat given number of bytes from the given distance. If the distance is
344 * invalid, false is returned. On success, true is returned and *len is
345 * updated to indicate how many bytes were left to be repeated.
346 */
347static bool XZ_FUNC dict_repeat(
348 struct dictionary *dict, uint32_t *len, uint32_t dist)
349{
350 size_t back;
351 uint32_t left;
352
353 if (dist >= dict->full || dist >= dict->size)
354 return false;
355
356 left = min_t(size_t, dict->limit - dict->pos, *len);
357 *len -= left;
358
359 back = dict->pos - dist - 1;
360 if (dist >= dict->pos)
361 back += dict->end;
362
363 do {
364 dict->buf[dict->pos++] = dict->buf[back++];
365 if (back == dict->end)
366 back = 0;
367 } while (--left > 0);
368
369 if (dict->full < dict->pos)
370 dict->full = dict->pos;
371
372 return true;
373}
374
375/* Copy uncompressed data as is from input to dictionary and output buffers. */
376static void XZ_FUNC dict_uncompressed(
377 struct dictionary *dict, struct xz_buf *b, uint32_t *left)
378{
379 size_t copy_size;
380
381 while (*left > 0 && b->in_pos < b->in_size
382 && b->out_pos < b->out_size) {
383 copy_size = min(b->in_size - b->in_pos,
384 b->out_size - b->out_pos);
385 if (copy_size > dict->end - dict->pos)
386 copy_size = dict->end - dict->pos;
387 if (copy_size > *left)
388 copy_size = *left;
389
390 *left -= copy_size;
391
392 memcpy(dict->buf + dict->pos, b->in + b->in_pos, copy_size);
393 dict->pos += copy_size;
394
395 if (dict->full < dict->pos)
396 dict->full = dict->pos;
397
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +0200398 if (DEC_IS_MULTI(dict->mode)) {
Denys Vlasenko602ce692010-05-30 03:35:18 +0200399 if (dict->pos == dict->end)
400 dict->pos = 0;
401
402 memcpy(b->out + b->out_pos, b->in + b->in_pos,
403 copy_size);
404 }
405
406 dict->start = dict->pos;
407
408 b->out_pos += copy_size;
409 b->in_pos += copy_size;
410
411 }
412}
413
414/*
415 * Flush pending data from dictionary to b->out. It is assumed that there is
416 * enough space in b->out. This is guaranteed because caller uses dict_limit()
417 * before decoding data into the dictionary.
418 */
419static uint32_t XZ_FUNC dict_flush(struct dictionary *dict, struct xz_buf *b)
420{
421 size_t copy_size = dict->pos - dict->start;
422
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +0200423 if (DEC_IS_MULTI(dict->mode)) {
Denys Vlasenko602ce692010-05-30 03:35:18 +0200424 if (dict->pos == dict->end)
425 dict->pos = 0;
426
427 memcpy(b->out + b->out_pos, dict->buf + dict->start,
428 copy_size);
429 }
430
431 dict->start = dict->pos;
432 b->out_pos += copy_size;
433 return copy_size;
434}
435
436/*****************
437 * Range decoder *
438 *****************/
439
440/* Reset the range decoder. */
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +0200441static void XZ_FUNC rc_reset(struct rc_dec *rc)
Denys Vlasenko602ce692010-05-30 03:35:18 +0200442{
443 rc->range = (uint32_t)-1;
444 rc->code = 0;
445 rc->init_bytes_left = RC_INIT_BYTES;
446}
447
448/*
449 * Read the first five initial bytes into rc->code if they haven't been
450 * read already. (Yes, the first byte gets completely ignored.)
451 */
452static bool XZ_FUNC rc_read_init(struct rc_dec *rc, struct xz_buf *b)
453{
454 while (rc->init_bytes_left > 0) {
455 if (b->in_pos == b->in_size)
456 return false;
457
458 rc->code = (rc->code << 8) + b->in[b->in_pos++];
459 --rc->init_bytes_left;
460 }
461
462 return true;
463}
464
465/* Return true if there may not be enough input for the next decoding loop. */
466static inline bool XZ_FUNC rc_limit_exceeded(const struct rc_dec *rc)
467{
468 return rc->in_pos > rc->in_limit;
469}
470
471/*
472 * Return true if it is possible (from point of view of range decoder) that
473 * we have reached the end of the LZMA chunk.
474 */
475static inline bool XZ_FUNC rc_is_finished(const struct rc_dec *rc)
476{
477 return rc->code == 0;
478}
479
480/* Read the next input byte if needed. */
481static __always_inline void XZ_FUNC rc_normalize(struct rc_dec *rc)
482{
483 if (rc->range < RC_TOP_VALUE) {
484 rc->range <<= RC_SHIFT_BITS;
485 rc->code = (rc->code << RC_SHIFT_BITS) + rc->in[rc->in_pos++];
486 }
487}
488
489/*
490 * Decode one bit. In some versions, this function has been splitted in three
491 * functions so that the compiler is supposed to be able to more easily avoid
492 * an extra branch. In this particular version of the LZMA decoder, this
493 * doesn't seem to be a good idea (tested with GCC 3.3.6, 3.4.6, and 4.3.3
494 * on x86). Using a non-splitted version results in nicer looking code too.
495 *
496 * NOTE: This must return an int. Do not make it return a bool or the speed
497 * of the code generated by GCC 3.x decreases 10-15 %. (GCC 4.3 doesn't care,
498 * and it generates 10-20 % faster code than GCC 3.x from this file anyway.)
499 */
500static __always_inline int XZ_FUNC rc_bit(struct rc_dec *rc, uint16_t *prob)
501{
502 uint32_t bound;
503 int bit;
504
505 rc_normalize(rc);
506 bound = (rc->range >> RC_BIT_MODEL_TOTAL_BITS) * *prob;
507 if (rc->code < bound) {
508 rc->range = bound;
509 *prob += (RC_BIT_MODEL_TOTAL - *prob) >> RC_MOVE_BITS;
510 bit = 0;
511 } else {
512 rc->range -= bound;
513 rc->code -= bound;
514 *prob -= *prob >> RC_MOVE_BITS;
515 bit = 1;
516 }
517
518 return bit;
519}
520
521/* Decode a bittree starting from the most significant bit. */
522static __always_inline uint32_t XZ_FUNC rc_bittree(
523 struct rc_dec *rc, uint16_t *probs, uint32_t limit)
524{
525 uint32_t symbol = 1;
526
527 do {
528 if (rc_bit(rc, &probs[symbol]))
529 symbol = (symbol << 1) + 1;
530 else
531 symbol <<= 1;
532 } while (symbol < limit);
533
534 return symbol;
535}
536
537/* Decode a bittree starting from the least significant bit. */
538static __always_inline void XZ_FUNC rc_bittree_reverse(struct rc_dec *rc,
539 uint16_t *probs, uint32_t *dest, uint32_t limit)
540{
541 uint32_t symbol = 1;
542 uint32_t i = 0;
543
544 do {
545 if (rc_bit(rc, &probs[symbol])) {
546 symbol = (symbol << 1) + 1;
547 *dest += 1 << i;
548 } else {
549 symbol <<= 1;
550 }
551 } while (++i < limit);
552}
553
554/* Decode direct bits (fixed fifty-fifty probability) */
555static inline void XZ_FUNC rc_direct(
556 struct rc_dec *rc, uint32_t *dest, uint32_t limit)
557{
558 uint32_t mask;
559
560 do {
561 rc_normalize(rc);
562 rc->range >>= 1;
563 rc->code -= rc->range;
564 mask = (uint32_t)0 - (rc->code >> 31);
565 rc->code += rc->range & mask;
566 *dest = (*dest << 1) + (mask + 1);
567 } while (--limit > 0);
568}
569
570/********
571 * LZMA *
572 ********/
573
574/* Get pointer to literal coder probability array. */
575static uint16_t * XZ_FUNC lzma_literal_probs(struct xz_dec_lzma2 *s)
576{
577 uint32_t prev_byte = dict_get(&s->dict, 0);
578 uint32_t low = prev_byte >> (8 - s->lzma.lc);
579 uint32_t high = (s->dict.pos & s->lzma.literal_pos_mask) << s->lzma.lc;
580 return s->lzma.literal[low + high];
581}
582
583/* Decode a literal (one 8-bit byte) */
584static void XZ_FUNC lzma_literal(struct xz_dec_lzma2 *s)
585{
586 uint16_t *probs;
587 uint32_t symbol;
588 uint32_t match_byte;
589 uint32_t match_bit;
590 uint32_t offset;
591 uint32_t i;
592
593 probs = lzma_literal_probs(s);
594
595 if (lzma_state_is_literal(s->lzma.state)) {
596 symbol = rc_bittree(&s->rc, probs, 0x100);
597 } else {
598 symbol = 1;
599 match_byte = dict_get(&s->dict, s->lzma.rep0) << 1;
600 offset = 0x100;
601
602 do {
603 match_bit = match_byte & offset;
604 match_byte <<= 1;
605 i = offset + match_bit + symbol;
606
607 if (rc_bit(&s->rc, &probs[i])) {
608 symbol = (symbol << 1) + 1;
609 offset &= match_bit;
610 } else {
611 symbol <<= 1;
612 offset &= ~match_bit;
613 }
614 } while (symbol < 0x100);
615 }
616
617 dict_put(&s->dict, (uint8_t)symbol);
618 lzma_state_literal(&s->lzma.state);
619}
620
621/* Decode the length of the match into s->lzma.len. */
622static void XZ_FUNC lzma_len(struct xz_dec_lzma2 *s, struct lzma_len_dec *l,
623 uint32_t pos_state)
624{
625 uint16_t *probs;
626 uint32_t limit;
627
628 if (!rc_bit(&s->rc, &l->choice)) {
629 probs = l->low[pos_state];
630 limit = LEN_LOW_SYMBOLS;
631 s->lzma.len = MATCH_LEN_MIN;
632 } else {
633 if (!rc_bit(&s->rc, &l->choice2)) {
634 probs = l->mid[pos_state];
635 limit = LEN_MID_SYMBOLS;
636 s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS;
637 } else {
638 probs = l->high;
639 limit = LEN_HIGH_SYMBOLS;
640 s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS
641 + LEN_MID_SYMBOLS;
642 }
643 }
644
645 s->lzma.len += rc_bittree(&s->rc, probs, limit) - limit;
646}
647
648/* Decode a match. The distance will be stored in s->lzma.rep0. */
649static void XZ_FUNC lzma_match(struct xz_dec_lzma2 *s, uint32_t pos_state)
650{
651 uint16_t *probs;
652 uint32_t dist_slot;
653 uint32_t limit;
654
655 lzma_state_match(&s->lzma.state);
656
657 s->lzma.rep3 = s->lzma.rep2;
658 s->lzma.rep2 = s->lzma.rep1;
659 s->lzma.rep1 = s->lzma.rep0;
660
661 lzma_len(s, &s->lzma.match_len_dec, pos_state);
662
663 probs = s->lzma.dist_slot[lzma_get_dist_state(s->lzma.len)];
664 dist_slot = rc_bittree(&s->rc, probs, DIST_SLOTS) - DIST_SLOTS;
665
666 if (dist_slot < DIST_MODEL_START) {
667 s->lzma.rep0 = dist_slot;
668 } else {
669 limit = (dist_slot >> 1) - 1;
670 s->lzma.rep0 = 2 + (dist_slot & 1);
671
672 if (dist_slot < DIST_MODEL_END) {
673 s->lzma.rep0 <<= limit;
674 probs = s->lzma.dist_special + s->lzma.rep0
675 - dist_slot - 1;
676 rc_bittree_reverse(&s->rc, probs,
677 &s->lzma.rep0, limit);
678 } else {
679 rc_direct(&s->rc, &s->lzma.rep0, limit - ALIGN_BITS);
680 s->lzma.rep0 <<= ALIGN_BITS;
681 rc_bittree_reverse(&s->rc, s->lzma.dist_align,
682 &s->lzma.rep0, ALIGN_BITS);
683 }
684 }
685}
686
687/*
688 * Decode a repeated match. The distance is one of the four most recently
689 * seen matches. The distance will be stored in s->lzma.rep0.
690 */
691static void XZ_FUNC lzma_rep_match(struct xz_dec_lzma2 *s, uint32_t pos_state)
692{
693 uint32_t tmp;
694
695 if (!rc_bit(&s->rc, &s->lzma.is_rep0[s->lzma.state])) {
696 if (!rc_bit(&s->rc, &s->lzma.is_rep0_long[
697 s->lzma.state][pos_state])) {
698 lzma_state_short_rep(&s->lzma.state);
699 s->lzma.len = 1;
700 return;
701 }
702 } else {
703 if (!rc_bit(&s->rc, &s->lzma.is_rep1[s->lzma.state])) {
704 tmp = s->lzma.rep1;
705 } else {
706 if (!rc_bit(&s->rc, &s->lzma.is_rep2[s->lzma.state])) {
707 tmp = s->lzma.rep2;
708 } else {
709 tmp = s->lzma.rep3;
710 s->lzma.rep3 = s->lzma.rep2;
711 }
712
713 s->lzma.rep2 = s->lzma.rep1;
714 }
715
716 s->lzma.rep1 = s->lzma.rep0;
717 s->lzma.rep0 = tmp;
718 }
719
720 lzma_state_long_rep(&s->lzma.state);
721 lzma_len(s, &s->lzma.rep_len_dec, pos_state);
722}
723
724/* LZMA decoder core */
725static bool XZ_FUNC lzma_main(struct xz_dec_lzma2 *s)
726{
727 uint32_t pos_state;
728
729 /*
730 * If the dictionary was reached during the previous call, try to
731 * finish the possibly pending repeat in the dictionary.
732 */
733 if (dict_has_space(&s->dict) && s->lzma.len > 0)
734 dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0);
735
736 /*
737 * Decode more LZMA symbols. One iteration may consume up to
738 * LZMA_IN_REQUIRED - 1 bytes.
739 */
740 while (dict_has_space(&s->dict) && !rc_limit_exceeded(&s->rc)) {
741 pos_state = s->dict.pos & s->lzma.pos_mask;
742
743 if (!rc_bit(&s->rc, &s->lzma.is_match[
744 s->lzma.state][pos_state])) {
745 lzma_literal(s);
746 } else {
747 if (rc_bit(&s->rc, &s->lzma.is_rep[s->lzma.state]))
748 lzma_rep_match(s, pos_state);
749 else
750 lzma_match(s, pos_state);
751
752 if (!dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0))
753 return false;
754 }
755 }
756
757 /*
758 * Having the range decoder always normalized when we are outside
759 * this function makes it easier to correctly handle end of the chunk.
760 */
761 rc_normalize(&s->rc);
762
763 return true;
764}
765
766/*
767 * Reset the LZMA decoder and range decoder state. Dictionary is nore reset
768 * here, because LZMA state may be reset without resetting the dictionary.
769 */
770static void XZ_FUNC lzma_reset(struct xz_dec_lzma2 *s)
771{
772 uint16_t *probs;
773 size_t i;
774
775 s->lzma.state = STATE_LIT_LIT;
776 s->lzma.rep0 = 0;
777 s->lzma.rep1 = 0;
778 s->lzma.rep2 = 0;
779 s->lzma.rep3 = 0;
780
781 /*
782 * All probabilities are initialized to the same value. This hack
783 * makes the code smaller by avoiding a separate loop for each
784 * probability array.
785 *
786 * This could be optimized so that only that part of literal
787 * probabilities that are actually required. In the common case
788 * we would write 12 KiB less.
789 */
790 probs = s->lzma.is_match[0];
791 for (i = 0; i < PROBS_TOTAL; ++i)
792 probs[i] = RC_BIT_MODEL_TOTAL / 2;
793
794 rc_reset(&s->rc);
795}
796
797/*
798 * Decode and validate LZMA properties (lc/lp/pb) and calculate the bit masks
799 * from the decoded lp and pb values. On success, the LZMA decoder state is
800 * reset and true is returned.
801 */
802static bool XZ_FUNC lzma_props(struct xz_dec_lzma2 *s, uint8_t props)
803{
804 if (props > (4 * 5 + 4) * 9 + 8)
805 return false;
806
807 s->lzma.pos_mask = 0;
808 while (props >= 9 * 5) {
809 props -= 9 * 5;
810 ++s->lzma.pos_mask;
811 }
812
813 s->lzma.pos_mask = (1 << s->lzma.pos_mask) - 1;
814
815 s->lzma.literal_pos_mask = 0;
816 while (props >= 9) {
817 props -= 9;
818 ++s->lzma.literal_pos_mask;
819 }
820
821 s->lzma.lc = props;
822
823 if (s->lzma.lc + s->lzma.literal_pos_mask > 4)
824 return false;
825
826 s->lzma.literal_pos_mask = (1 << s->lzma.literal_pos_mask) - 1;
827
828 lzma_reset(s);
829
830 return true;
831}
832
833/*********
834 * LZMA2 *
835 *********/
836
837/*
838 * The LZMA decoder assumes that if the input limit (s->rc.in_limit) hasn't
839 * been exceeded, it is safe to read up to LZMA_IN_REQUIRED bytes. This
840 * wrapper function takes care of making the LZMA decoder's assumption safe.
841 *
842 * As long as there is plenty of input left to be decoded in the current LZMA
843 * chunk, we decode directly from the caller-supplied input buffer until
844 * there's LZMA_IN_REQUIRED bytes left. Those remaining bytes are copied into
845 * s->temp.buf, which (hopefully) gets filled on the next call to this
846 * function. We decode a few bytes from the temporary buffer so that we can
847 * continue decoding from the caller-supplied input buffer again.
848 */
849static bool XZ_FUNC lzma2_lzma(struct xz_dec_lzma2 *s, struct xz_buf *b)
850{
851 size_t in_avail;
852 uint32_t tmp;
853
854 in_avail = b->in_size - b->in_pos;
855 if (s->temp.size > 0 || s->lzma2.compressed == 0) {
856 tmp = 2 * LZMA_IN_REQUIRED - s->temp.size;
857 if (tmp > s->lzma2.compressed - s->temp.size)
858 tmp = s->lzma2.compressed - s->temp.size;
859 if (tmp > in_avail)
860 tmp = in_avail;
861
862 memcpy(s->temp.buf + s->temp.size, b->in + b->in_pos, tmp);
863
864 if (s->temp.size + tmp == s->lzma2.compressed) {
865 memzero(s->temp.buf + s->temp.size + tmp,
866 sizeof(s->temp.buf)
867 - s->temp.size - tmp);
868 s->rc.in_limit = s->temp.size + tmp;
869 } else if (s->temp.size + tmp < LZMA_IN_REQUIRED) {
870 s->temp.size += tmp;
871 b->in_pos += tmp;
872 return true;
873 } else {
874 s->rc.in_limit = s->temp.size + tmp - LZMA_IN_REQUIRED;
875 }
876
877 s->rc.in = s->temp.buf;
878 s->rc.in_pos = 0;
879
880 if (!lzma_main(s) || s->rc.in_pos > s->temp.size + tmp)
881 return false;
882
883 s->lzma2.compressed -= s->rc.in_pos;
884
885 if (s->rc.in_pos < s->temp.size) {
886 s->temp.size -= s->rc.in_pos;
887 memmove(s->temp.buf, s->temp.buf + s->rc.in_pos,
888 s->temp.size);
889 return true;
890 }
891
892 b->in_pos += s->rc.in_pos - s->temp.size;
893 s->temp.size = 0;
894 }
895
896 in_avail = b->in_size - b->in_pos;
897 if (in_avail >= LZMA_IN_REQUIRED) {
898 s->rc.in = b->in;
899 s->rc.in_pos = b->in_pos;
900
901 if (in_avail >= s->lzma2.compressed + LZMA_IN_REQUIRED)
902 s->rc.in_limit = b->in_pos + s->lzma2.compressed;
903 else
904 s->rc.in_limit = b->in_size - LZMA_IN_REQUIRED;
905
906 if (!lzma_main(s))
907 return false;
908
909 in_avail = s->rc.in_pos - b->in_pos;
910 if (in_avail > s->lzma2.compressed)
911 return false;
912
913 s->lzma2.compressed -= in_avail;
914 b->in_pos = s->rc.in_pos;
915 }
916
917 in_avail = b->in_size - b->in_pos;
918 if (in_avail < LZMA_IN_REQUIRED) {
919 if (in_avail > s->lzma2.compressed)
920 in_avail = s->lzma2.compressed;
921
922 memcpy(s->temp.buf, b->in + b->in_pos, in_avail);
923 s->temp.size = in_avail;
924 b->in_pos += in_avail;
925 }
926
927 return true;
928}
929
930/*
931 * Take care of the LZMA2 control layer, and forward the job of actual LZMA
932 * decoding or copying of uncompressed chunks to other functions.
933 */
Denys Vlasenkob9542cb2010-06-01 23:16:46 +0200934XZ_EXTERN NOINLINE enum xz_ret XZ_FUNC xz_dec_lzma2_run(
Denys Vlasenko602ce692010-05-30 03:35:18 +0200935 struct xz_dec_lzma2 *s, struct xz_buf *b)
936{
937 uint32_t tmp;
938
939 while (b->in_pos < b->in_size || s->lzma2.sequence == SEQ_LZMA_RUN) {
940 switch (s->lzma2.sequence) {
941 case SEQ_CONTROL:
942 /*
943 * LZMA2 control byte
944 *
945 * Exact values:
946 * 0x00 End marker
947 * 0x01 Dictionary reset followed by
948 * an uncompressed chunk
949 * 0x02 Uncompressed chunk (no dictionary reset)
950 *
951 * Highest three bits (s->control & 0xE0):
952 * 0xE0 Dictionary reset, new properties and state
953 * reset, followed by LZMA compressed chunk
954 * 0xC0 New properties and state reset, followed
955 * by LZMA compressed chunk (no dictionary
956 * reset)
957 * 0xA0 State reset using old properties,
958 * followed by LZMA compressed chunk (no
959 * dictionary reset)
960 * 0x80 LZMA chunk (no dictionary or state reset)
961 *
962 * For LZMA compressed chunks, the lowest five bits
963 * (s->control & 1F) are the highest bits of the
964 * uncompressed size (bits 16-20).
965 *
966 * A new LZMA2 stream must begin with a dictionary
967 * reset. The first LZMA chunk must set new
968 * properties and reset the LZMA state.
969 *
970 * Values that don't match anything described above
971 * are invalid and we return XZ_DATA_ERROR.
972 */
973 tmp = b->in[b->in_pos++];
974
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) {
980 return XZ_DATA_ERROR;
981 }
982
983 if (tmp >= 0x80) {
984 s->lzma2.uncompressed = (tmp & 0x1F) << 16;
985 s->lzma2.sequence = SEQ_UNCOMPRESSED_1;
986
987 if (tmp >= 0xC0) {
988 /*
989 * When there are new properties,
990 * state reset is done at
991 * SEQ_PROPERTIES.
992 */
993 s->lzma2.need_props = false;
994 s->lzma2.next_sequence
995 = SEQ_PROPERTIES;
996
997 } else if (s->lzma2.need_props) {
998 return XZ_DATA_ERROR;
999
1000 } else {
1001 s->lzma2.next_sequence
1002 = SEQ_LZMA_PREPARE;
1003 if (tmp >= 0xA0)
1004 lzma_reset(s);
1005 }
1006 } else {
1007 if (tmp == 0x00)
1008 return XZ_STREAM_END;
1009
1010 if (tmp > 0x02)
1011 return XZ_DATA_ERROR;
1012
1013 s->lzma2.sequence = SEQ_COMPRESSED_0;
1014 s->lzma2.next_sequence = SEQ_COPY;
1015 }
1016
1017 break;
1018
1019 case SEQ_UNCOMPRESSED_1:
1020 s->lzma2.uncompressed
1021 += (uint32_t)b->in[b->in_pos++] << 8;
1022 s->lzma2.sequence = SEQ_UNCOMPRESSED_2;
1023 break;
1024
1025 case SEQ_UNCOMPRESSED_2:
1026 s->lzma2.uncompressed
1027 += (uint32_t)b->in[b->in_pos++] + 1;
1028 s->lzma2.sequence = SEQ_COMPRESSED_0;
1029 break;
1030
1031 case SEQ_COMPRESSED_0:
1032 s->lzma2.compressed
1033 = (uint32_t)b->in[b->in_pos++] << 8;
1034 s->lzma2.sequence = SEQ_COMPRESSED_1;
1035 break;
1036
1037 case SEQ_COMPRESSED_1:
1038 s->lzma2.compressed
1039 += (uint32_t)b->in[b->in_pos++] + 1;
1040 s->lzma2.sequence = s->lzma2.next_sequence;
1041 break;
1042
1043 case SEQ_PROPERTIES:
1044 if (!lzma_props(s, b->in[b->in_pos++]))
1045 return XZ_DATA_ERROR;
1046
1047 s->lzma2.sequence = SEQ_LZMA_PREPARE;
1048
1049 case SEQ_LZMA_PREPARE:
1050 if (s->lzma2.compressed < RC_INIT_BYTES)
1051 return XZ_DATA_ERROR;
1052
1053 if (!rc_read_init(&s->rc, b))
1054 return XZ_OK;
1055
1056 s->lzma2.compressed -= RC_INIT_BYTES;
1057 s->lzma2.sequence = SEQ_LZMA_RUN;
1058
1059 case SEQ_LZMA_RUN:
1060 /*
1061 * Set dictionary limit to indicate how much we want
1062 * to be encoded at maximum. Decode new data into the
1063 * dictionary. Flush the new data from dictionary to
1064 * b->out. Check if we finished decoding this chunk.
1065 * In case the dictionary got full but we didn't fill
1066 * the output buffer yet, we may run this loop
1067 * multiple times without changing s->lzma2.sequence.
1068 */
1069 dict_limit(&s->dict, min_t(size_t,
1070 b->out_size - b->out_pos,
1071 s->lzma2.uncompressed));
1072 if (!lzma2_lzma(s, b))
1073 return XZ_DATA_ERROR;
1074
1075 s->lzma2.uncompressed -= dict_flush(&s->dict, b);
1076
1077 if (s->lzma2.uncompressed == 0) {
1078 if (s->lzma2.compressed > 0 || s->lzma.len > 0
1079 || !rc_is_finished(&s->rc))
1080 return XZ_DATA_ERROR;
1081
1082 rc_reset(&s->rc);
1083 s->lzma2.sequence = SEQ_CONTROL;
1084
1085 } else if (b->out_pos == b->out_size
1086 || (b->in_pos == b->in_size
1087 && s->temp.size
1088 < s->lzma2.compressed)) {
1089 return XZ_OK;
1090 }
1091
1092 break;
1093
1094 case SEQ_COPY:
1095 dict_uncompressed(&s->dict, b, &s->lzma2.compressed);
1096 if (s->lzma2.compressed > 0)
1097 return XZ_OK;
1098
1099 s->lzma2.sequence = SEQ_CONTROL;
1100 break;
1101 }
1102 }
1103
1104 return XZ_OK;
1105}
1106
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +02001107XZ_EXTERN struct xz_dec_lzma2 * XZ_FUNC xz_dec_lzma2_create(
1108 enum xz_mode mode, uint32_t dict_max)
Denys Vlasenko602ce692010-05-30 03:35:18 +02001109{
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +02001110 struct xz_dec_lzma2 *s = kmalloc(sizeof(*s), GFP_KERNEL);
Denys Vlasenko602ce692010-05-30 03:35:18 +02001111 if (s == NULL)
1112 return NULL;
1113
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +02001114 s->dict.mode = mode;
1115 s->dict.size_max = dict_max;
1116
1117 if (DEC_IS_PREALLOC(mode)) {
Denys Vlasenko602ce692010-05-30 03:35:18 +02001118 s->dict.buf = vmalloc(dict_max);
1119 if (s->dict.buf == NULL) {
1120 kfree(s);
1121 return NULL;
1122 }
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +02001123 } else if (DEC_IS_DYNALLOC(mode)) {
1124 s->dict.buf = NULL;
1125 s->dict.allocated = 0;
Denys Vlasenko602ce692010-05-30 03:35:18 +02001126 }
1127
Denys Vlasenko602ce692010-05-30 03:35:18 +02001128 return s;
1129}
1130
1131XZ_EXTERN enum xz_ret XZ_FUNC xz_dec_lzma2_reset(
1132 struct xz_dec_lzma2 *s, uint8_t props)
1133{
1134 /* This limits dictionary size to 3 GiB to keep parsing simpler. */
Denys Vlasenkoacaaca82010-05-30 04:50:21 +02001135 if (props > 39)
Denys Vlasenko602ce692010-05-30 03:35:18 +02001136 return XZ_OPTIONS_ERROR;
Denys Vlasenko602ce692010-05-30 03:35:18 +02001137
1138 s->dict.size = 2 + (props & 1);
1139 s->dict.size <<= (props >> 1) + 11;
1140
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +02001141 if (DEC_IS_MULTI(s->dict.mode)) {
1142 if (s->dict.size > s->dict.size_max)
1143 return XZ_MEMLIMIT_ERROR;
Denys Vlasenko602ce692010-05-30 03:35:18 +02001144
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +02001145 s->dict.end = s->dict.size;
1146
1147 if (DEC_IS_DYNALLOC(s->dict.mode)) {
1148 if (s->dict.allocated < s->dict.size) {
1149 vfree(s->dict.buf);
1150 s->dict.buf = vmalloc(s->dict.size);
1151 if (s->dict.buf == NULL) {
1152 s->dict.allocated = 0;
1153 return XZ_MEM_ERROR;
1154 }
1155 }
1156 }
1157 }
Denys Vlasenko602ce692010-05-30 03:35:18 +02001158
1159 s->lzma.len = 0;
1160
1161 s->lzma2.sequence = SEQ_CONTROL;
1162 s->lzma2.need_dict_reset = true;
1163
1164 s->temp.size = 0;
1165
1166 return XZ_OK;
1167}
1168
1169XZ_EXTERN void XZ_FUNC xz_dec_lzma2_end(struct xz_dec_lzma2 *s)
1170{
Denys Vlasenkoba73cfd2010-06-20 02:40:56 +02001171 if (DEC_IS_MULTI(s->dict.mode))
Denys Vlasenko602ce692010-05-30 03:35:18 +02001172 vfree(s->dict.buf);
1173
1174 kfree(s);
1175}