1
/* xz_dec_lzma2.c - LZMA2 decoder */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2010 Free Software Foundation, Inc.
6
* GRUB is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* GRUB is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20
* This file is based on code from XZ embedded project
21
* http://tukaani.org/xz/embedded.html
24
#include "xz_private.h"
28
* Range decoder initialization eats the first five bytes of each LZMA chunk.
30
#define RC_INIT_BYTES 5
33
* Minimum number of usable input buffer to safely decode one LZMA symbol.
34
* The worst case is that we decode 22 bits using probabilities and 26
35
* direct bits. This may decode at maximum of 20 bytes of input. However,
36
* lzma_main() does an extra normalization before returning, thus we
37
* need to put 21 here.
39
#define LZMA_IN_REQUIRED 21
42
* Dictionary (history buffer)
44
* These are always true:
45
* start <= pos <= full <= end
48
* In multi-call mode, also these are true:
52
* Most of these variables are size_t to support single-call mode,
53
* in which the dictionary variables address the actual output
57
/* Beginning of the history buffer */
60
/* Old position in buf (before decoding more data) */
67
* How full dictionary is. This is used to detect corrupt input that
68
* would read beyond the beginning of the uncompressed stream.
72
/* Write limit; we don't write to buf[limit] or later bytes. */
76
* End of the dictionary buffer. In multi-call mode, this is
77
* the same as the dictionary size. In single-call mode, this
78
* indicates the size of the output buffer.
83
* Size of the dictionary as specified in Block Header. This is used
84
* together with "full" to detect corrupt input that would make us
85
* read beyond the beginning of the uncompressed stream.
90
* Amount of memory allocated for the dictionary. A special
91
* value of zero indicates that we are in single-call mode,
92
* where the output buffer works as the dictionary.
103
* Number of initializing bytes remaining to be read
106
uint32_t init_bytes_left;
109
* Buffer from which we read our input. It can be either
110
* temp.buf or the caller-provided input buffer.
117
/* Probabilities for a length decoder. */
118
struct lzma_len_dec {
119
/* Probability of match length being at least 10 */
122
/* Probability of match length being at least 18 */
125
/* Probabilities for match lengths 2-9 */
126
uint16_t low[POS_STATES_MAX][LEN_LOW_SYMBOLS];
128
/* Probabilities for match lengths 10-17 */
129
uint16_t mid[POS_STATES_MAX][LEN_MID_SYMBOLS];
131
/* Probabilities for match lengths 18-273 */
132
uint16_t high[LEN_HIGH_SYMBOLS];
137
* LZMA properties or related bit masks (number of literal
138
* context bits, a mask dervied from the number of literal
139
* position bits, and a mask dervied from the number
143
uint32_t literal_pos_mask; /* (1 << lp) - 1 */
144
uint32_t pos_mask; /* (1 << pb) - 1 */
146
/* Types of the most recently seen LZMA symbols */
147
enum lzma_state state;
149
/* Distances of latest four matches */
156
* Length of a match. This is updated so that dict_repeat can
157
* be called again to finish repeating the whole match.
161
/* If 1, it's a match. Otherwise it's a single 8-bit literal. */
162
uint16_t is_match[STATES][POS_STATES_MAX];
164
/* If 1, it's a repeated match. The distance is one of rep0 .. rep3. */
165
uint16_t is_rep[STATES];
168
* If 0, distance of a repeated match is rep0.
169
* Otherwise check is_rep1.
171
uint16_t is_rep0[STATES];
174
* If 0, distance of a repeated match is rep1.
175
* Otherwise check is_rep2.
177
uint16_t is_rep1[STATES];
179
/* If 0, distance of a repeated match is rep2. Otherwise it is rep3. */
180
uint16_t is_rep2[STATES];
183
* If 1, the repeated match has length of one byte. Otherwise
184
* the length is decoded from rep_len_decoder.
186
uint16_t is_rep0_long[STATES][POS_STATES_MAX];
189
* Probability tree for the highest two bits of the match
190
* distance. There is a separate probability tree for match
191
* lengths of 2 (i.e. MATCH_LEN_MIN), 3, 4, and [5, 273].
193
uint16_t dist_slot[DIST_STATES][DIST_SLOTS];
196
* Probility trees for additional bits for match distance
197
* when the distance is in the range [4, 127].
199
uint16_t dist_special[FULL_DISTANCES - DIST_MODEL_END];
202
* Probability tree for the lowest four bits of a match
203
* distance that is equal to or greater than 128.
205
uint16_t dist_align[ALIGN_SIZE];
207
/* Length of a normal match */
208
struct lzma_len_dec match_len_dec;
210
/* Length of a repeated match */
211
struct lzma_len_dec rep_len_dec;
213
/* Probabilities of literals */
214
uint16_t literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE];
217
struct xz_dec_lzma2 {
220
/* Position in xz_dec_lzma2_run(). */
234
* Next position after decoding the compressed size of
237
enum lzma2_seq next_sequence;
239
/* Uncompressed size of LZMA chunk (2 MiB at maximum) */
240
uint32_t uncompressed;
243
* Compressed size of LZMA chunk or compressed/uncompressed
244
* size of uncompressed chunk (64 KiB at maximum)
249
* True if dictionary reset is needed. This is false before
250
* the first chunk (LZMA or uncompressed).
252
bool need_dict_reset;
255
* True if new LZMA properties are needed. This is false
256
* before the first LZMA chunk.
262
* Temporary buffer which holds small number of input bytes between
263
* decoder calls. See lzma2_lzma() for details.
267
uint8_t buf[3 * LZMA_IN_REQUIRED];
270
struct dictionary dict;
272
struct lzma_dec lzma;
280
* Reset the dictionary state. When in single-call mode, set up the beginning
281
* of the dictionary to point to the actual output buffer.
283
static void dict_reset(struct dictionary *dict, struct xz_buf *b)
285
if (dict->allocated == 0) {
286
dict->buf = b->out + b->out_pos;
287
dict->end = b->out_size - b->out_pos;
295
/* Set dictionary write limit */
296
static void dict_limit(struct dictionary *dict, size_t out_max)
298
if (dict->end - dict->pos <= out_max)
299
dict->limit = dict->end;
301
dict->limit = dict->pos + out_max;
304
/* Return true if at least one byte can be written into the dictionary. */
305
static inline bool dict_has_space(const struct dictionary *dict)
307
return dict->pos < dict->limit;
311
* Get a byte from the dictionary at the given distance. The distance is
312
* assumed to valid, or as a special case, zero when the dictionary is
313
* still empty. This special case is needed for single-call decoding to
314
* avoid writing a '\0' to the end of the destination buffer.
316
static inline uint32_t dict_get(
317
const struct dictionary *dict, uint32_t dist)
319
size_t offset = dict->pos - dist - 1;
321
if (dist >= dict->pos)
324
return dict->full > 0 ? dict->buf[offset] : 0;
328
* Put one byte into the dictionary. It is assumed that there is space for it.
330
static inline void dict_put(struct dictionary *dict, uint8_t byte)
332
dict->buf[dict->pos++] = byte;
334
if (dict->full < dict->pos)
335
dict->full = dict->pos;
339
* Repeat given number of bytes from the given distance. If the distance is
340
* invalid, false is returned. On success, true is returned and *len is
341
* updated to indicate how many bytes were left to be repeated.
343
static bool dict_repeat(
344
struct dictionary *dict, uint32_t *len, uint32_t dist)
349
if (dist >= dict->full || dist >= dict->size)
352
left = min_t(size_t, dict->limit - dict->pos, *len);
355
back = dict->pos - dist - 1;
356
if (dist >= dict->pos)
360
dict->buf[dict->pos++] = dict->buf[back++];
361
if (back == dict->end)
363
} while (--left > 0);
365
if (dict->full < dict->pos)
366
dict->full = dict->pos;
371
/* Copy uncompressed data as is from input to dictionary and output buffers. */
372
static void dict_uncompressed(
373
struct dictionary *dict, struct xz_buf *b, uint32_t *left)
377
while (*left > 0 && b->in_pos < b->in_size
378
&& b->out_pos < b->out_size) {
379
copy_size = min(b->in_size - b->in_pos,
380
b->out_size - b->out_pos);
381
if (copy_size > dict->end - dict->pos)
382
copy_size = dict->end - dict->pos;
383
if (copy_size > *left)
388
memcpy(dict->buf + dict->pos, b->in + b->in_pos, copy_size);
389
dict->pos += copy_size;
391
if (dict->full < dict->pos)
392
dict->full = dict->pos;
394
if (dict->allocated != 0) {
395
if (dict->pos == dict->end)
398
memcpy(b->out + b->out_pos, b->in + b->in_pos,
402
dict->start = dict->pos;
404
b->out_pos += copy_size;
405
b->in_pos += copy_size;
411
* Flush pending data from dictionary to b->out. It is assumed that there is
412
* enough space in b->out. This is guaranteed because caller uses dict_limit()
413
* before decoding data into the dictionary.
415
static uint32_t dict_flush(struct dictionary *dict, struct xz_buf *b)
417
size_t copy_size = dict->pos - dict->start;
419
if (dict->allocated != 0) {
420
if (dict->pos == dict->end)
423
memcpy(b->out + b->out_pos, dict->buf + dict->start,
427
dict->start = dict->pos;
428
b->out_pos += copy_size;
436
/* Reset the range decoder. */
437
static void rc_reset(struct rc_dec *rc)
439
rc->range = (uint32_t)-1;
441
rc->init_bytes_left = RC_INIT_BYTES;
445
* Read the first five initial bytes into rc->code if they haven't been
446
* read already. (Yes, the first byte gets completely ignored.)
448
static bool rc_read_init(struct rc_dec *rc, struct xz_buf *b)
450
while (rc->init_bytes_left > 0) {
451
if (b->in_pos == b->in_size)
454
rc->code = (rc->code << 8) + b->in[b->in_pos++];
455
--rc->init_bytes_left;
461
/* Return true if there may not be enough input for the next decoding loop. */
462
static inline bool rc_limit_exceeded(const struct rc_dec *rc)
464
return rc->in_pos > rc->in_limit;
468
* Return true if it is possible (from point of view of range decoder) that
469
* we have reached the end of the LZMA chunk.
471
static inline bool rc_is_finished(const struct rc_dec *rc)
473
return rc->code == 0;
476
/* Read the next input byte if needed. */
477
static __always_inline void rc_normalize(struct rc_dec *rc)
479
if (rc->range < RC_TOP_VALUE) {
480
rc->range <<= RC_SHIFT_BITS;
481
rc->code = (rc->code << RC_SHIFT_BITS) + rc->in[rc->in_pos++];
486
* Decode one bit. In some versions, this function has been splitted in three
487
* functions so that the compiler is supposed to be able to more easily avoid
488
* an extra branch. In this particular version of the LZMA decoder, this
489
* doesn't seem to be a good idea (tested with GCC 3.3.6, 3.4.6, and 4.3.3
490
* on x86). Using a non-splitted version results in nicer looking code too.
492
* NOTE: This must return an int. Do not make it return a bool or the speed
493
* of the code generated by GCC 3.x decreases 10-15 %. (GCC 4.3 doesn't care,
494
* and it generates 10-20 % faster code than GCC 3.x from this file anyway.)
496
static __always_inline int rc_bit(struct rc_dec *rc, uint16_t *prob)
502
bound = (rc->range >> RC_BIT_MODEL_TOTAL_BITS) * *prob;
503
if (rc->code < bound) {
505
*prob += (RC_BIT_MODEL_TOTAL - *prob) >> RC_MOVE_BITS;
510
*prob -= *prob >> RC_MOVE_BITS;
517
/* Decode a bittree starting from the most significant bit. */
518
static __always_inline uint32_t rc_bittree(
519
struct rc_dec *rc, uint16_t *probs, uint32_t limit)
524
if (rc_bit(rc, &probs[symbol]))
525
symbol = (symbol << 1) + 1;
528
} while (symbol < limit);
533
/* Decode a bittree starting from the least significant bit. */
534
static __always_inline void rc_bittree_reverse(struct rc_dec *rc,
535
uint16_t *probs, uint32_t *dest, uint32_t limit)
541
if (rc_bit(rc, &probs[symbol])) {
542
symbol = (symbol << 1) + 1;
547
} while (++i < limit);
550
/* Decode direct bits (fixed fifty-fifty probability) */
551
static inline void rc_direct(
552
struct rc_dec *rc, uint32_t *dest, uint32_t limit)
559
rc->code -= rc->range;
560
mask = (uint32_t)0 - (rc->code >> 31);
561
rc->code += rc->range & mask;
562
*dest = (*dest << 1) + (mask + 1);
563
} while (--limit > 0);
570
/* Get pointer to literal coder probability array. */
571
static uint16_t * lzma_literal_probs(struct xz_dec_lzma2 *s)
573
uint32_t prev_byte = dict_get(&s->dict, 0);
574
uint32_t low = prev_byte >> (8 - s->lzma.lc);
575
uint32_t high = (s->dict.pos & s->lzma.literal_pos_mask) << s->lzma.lc;
576
return s->lzma.literal[low + high];
579
/* Decode a literal (one 8-bit byte) */
580
static void lzma_literal(struct xz_dec_lzma2 *s)
589
probs = lzma_literal_probs(s);
591
if (lzma_state_is_literal(s->lzma.state)) {
592
symbol = rc_bittree(&s->rc, probs, 0x100);
595
match_byte = dict_get(&s->dict, s->lzma.rep0) << 1;
599
match_bit = match_byte & offset;
601
i = offset + match_bit + symbol;
603
if (rc_bit(&s->rc, &probs[i])) {
604
symbol = (symbol << 1) + 1;
608
offset &= ~match_bit;
610
} while (symbol < 0x100);
613
dict_put(&s->dict, (uint8_t)symbol);
614
lzma_state_literal(&s->lzma.state);
617
/* Decode the length of the match into s->lzma.len. */
618
static void lzma_len(struct xz_dec_lzma2 *s, struct lzma_len_dec *l,
624
if (!rc_bit(&s->rc, &l->choice)) {
625
probs = l->low[pos_state];
626
limit = LEN_LOW_SYMBOLS;
627
s->lzma.len = MATCH_LEN_MIN;
629
if (!rc_bit(&s->rc, &l->choice2)) {
630
probs = l->mid[pos_state];
631
limit = LEN_MID_SYMBOLS;
632
s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS;
635
limit = LEN_HIGH_SYMBOLS;
636
s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS
641
s->lzma.len += rc_bittree(&s->rc, probs, limit) - limit;
644
/* Decode a match. The distance will be stored in s->lzma.rep0. */
645
static void lzma_match(struct xz_dec_lzma2 *s, uint32_t pos_state)
651
lzma_state_match(&s->lzma.state);
653
s->lzma.rep3 = s->lzma.rep2;
654
s->lzma.rep2 = s->lzma.rep1;
655
s->lzma.rep1 = s->lzma.rep0;
657
lzma_len(s, &s->lzma.match_len_dec, pos_state);
659
probs = s->lzma.dist_slot[lzma_get_dist_state(s->lzma.len)];
660
dist_slot = rc_bittree(&s->rc, probs, DIST_SLOTS) - DIST_SLOTS;
662
if (dist_slot < DIST_MODEL_START) {
663
s->lzma.rep0 = dist_slot;
665
limit = (dist_slot >> 1) - 1;
666
s->lzma.rep0 = 2 + (dist_slot & 1);
668
if (dist_slot < DIST_MODEL_END) {
669
s->lzma.rep0 <<= limit;
670
probs = s->lzma.dist_special + s->lzma.rep0
672
rc_bittree_reverse(&s->rc, probs,
673
&s->lzma.rep0, limit);
675
rc_direct(&s->rc, &s->lzma.rep0, limit - ALIGN_BITS);
676
s->lzma.rep0 <<= ALIGN_BITS;
677
rc_bittree_reverse(&s->rc, s->lzma.dist_align,
678
&s->lzma.rep0, ALIGN_BITS);
684
* Decode a repeated match. The distance is one of the four most recently
685
* seen matches. The distance will be stored in s->lzma.rep0.
687
static void lzma_rep_match(struct xz_dec_lzma2 *s, uint32_t pos_state)
691
if (!rc_bit(&s->rc, &s->lzma.is_rep0[s->lzma.state])) {
692
if (!rc_bit(&s->rc, &s->lzma.is_rep0_long[
693
s->lzma.state][pos_state])) {
694
lzma_state_short_rep(&s->lzma.state);
699
if (!rc_bit(&s->rc, &s->lzma.is_rep1[s->lzma.state])) {
702
if (!rc_bit(&s->rc, &s->lzma.is_rep2[s->lzma.state])) {
706
s->lzma.rep3 = s->lzma.rep2;
709
s->lzma.rep2 = s->lzma.rep1;
712
s->lzma.rep1 = s->lzma.rep0;
716
lzma_state_long_rep(&s->lzma.state);
717
lzma_len(s, &s->lzma.rep_len_dec, pos_state);
720
/* LZMA decoder core */
721
static bool lzma_main(struct xz_dec_lzma2 *s)
726
* If the dictionary was reached during the previous call, try to
727
* finish the possibly pending repeat in the dictionary.
729
if (dict_has_space(&s->dict) && s->lzma.len > 0)
730
dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0);
733
* Decode more LZMA symbols. One iteration may consume up to
734
* LZMA_IN_REQUIRED - 1 bytes.
736
while (dict_has_space(&s->dict) && !rc_limit_exceeded(&s->rc)) {
737
pos_state = s->dict.pos & s->lzma.pos_mask;
739
if (!rc_bit(&s->rc, &s->lzma.is_match[
740
s->lzma.state][pos_state])) {
743
if (rc_bit(&s->rc, &s->lzma.is_rep[s->lzma.state]))
744
lzma_rep_match(s, pos_state);
746
lzma_match(s, pos_state);
748
if (!dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0))
754
* Having the range decoder always normalized when we are outside
755
* this function makes it easier to correctly handle end of the chunk.
757
rc_normalize(&s->rc);
763
* Reset the LZMA decoder and range decoder state. Dictionary is nore reset
764
* here, because LZMA state may be reset without resetting the dictionary.
766
static void lzma_reset(struct xz_dec_lzma2 *s)
771
s->lzma.state = STATE_LIT_LIT;
778
* All probabilities are initialized to the same value. This hack
779
* makes the code smaller by avoiding a separate loop for each
782
* This could be optimized so that only that part of literal
783
* probabilities that are actually required. In the common case
784
* we would write 12 KiB less.
786
probs = s->lzma.is_match[0];
787
for (i = 0; i < PROBS_TOTAL; ++i)
788
probs[i] = RC_BIT_MODEL_TOTAL / 2;
794
* Decode and validate LZMA properties (lc/lp/pb) and calculate the bit masks
795
* from the decoded lp and pb values. On success, the LZMA decoder state is
796
* reset and true is returned.
798
static bool lzma_props(struct xz_dec_lzma2 *s, uint8_t props)
800
if (props > (4 * 5 + 4) * 9 + 8)
803
s->lzma.pos_mask = 0;
804
while (props >= 9 * 5) {
809
s->lzma.pos_mask = (1 << s->lzma.pos_mask) - 1;
811
s->lzma.literal_pos_mask = 0;
814
++s->lzma.literal_pos_mask;
819
if (s->lzma.lc + s->lzma.literal_pos_mask > 4)
822
s->lzma.literal_pos_mask = (1 << s->lzma.literal_pos_mask) - 1;
834
* The LZMA decoder assumes that if the input limit (s->rc.in_limit) hasn't
835
* been exceeded, it is safe to read up to LZMA_IN_REQUIRED bytes. This
836
* wrapper function takes care of making the LZMA decoder's assumption safe.
838
* As long as there is plenty of input left to be decoded in the current LZMA
839
* chunk, we decode directly from the caller-supplied input buffer until
840
* there's LZMA_IN_REQUIRED bytes left. Those remaining bytes are copied into
841
* s->temp.buf, which (hopefully) gets filled on the next call to this
842
* function. We decode a few bytes from the temporary buffer so that we can
843
* continue decoding from the caller-supplied input buffer again.
845
static bool lzma2_lzma(struct xz_dec_lzma2 *s, struct xz_buf *b)
850
in_avail = b->in_size - b->in_pos;
851
if (s->temp.size > 0 || s->lzma2.compressed == 0) {
852
tmp = 2 * LZMA_IN_REQUIRED - s->temp.size;
853
if (tmp > s->lzma2.compressed - s->temp.size)
854
tmp = s->lzma2.compressed - s->temp.size;
858
memcpy(s->temp.buf + s->temp.size, b->in + b->in_pos, tmp);
860
if (s->temp.size + tmp == s->lzma2.compressed) {
861
memzero(s->temp.buf + s->temp.size + tmp,
863
- s->temp.size - tmp);
864
s->rc.in_limit = s->temp.size + tmp;
865
} else if (s->temp.size + tmp < LZMA_IN_REQUIRED) {
870
s->rc.in_limit = s->temp.size + tmp - LZMA_IN_REQUIRED;
873
s->rc.in = s->temp.buf;
876
if (!lzma_main(s) || s->rc.in_pos > s->temp.size + tmp)
879
s->lzma2.compressed -= s->rc.in_pos;
881
if (s->rc.in_pos < s->temp.size) {
882
s->temp.size -= s->rc.in_pos;
883
memmove(s->temp.buf, s->temp.buf + s->rc.in_pos,
888
b->in_pos += s->rc.in_pos - s->temp.size;
892
in_avail = b->in_size - b->in_pos;
893
if (in_avail >= LZMA_IN_REQUIRED) {
895
s->rc.in_pos = b->in_pos;
897
if (in_avail >= s->lzma2.compressed + LZMA_IN_REQUIRED)
898
s->rc.in_limit = b->in_pos + s->lzma2.compressed;
900
s->rc.in_limit = b->in_size - LZMA_IN_REQUIRED;
905
in_avail = s->rc.in_pos - b->in_pos;
906
if (in_avail > s->lzma2.compressed)
909
s->lzma2.compressed -= in_avail;
910
b->in_pos = s->rc.in_pos;
913
in_avail = b->in_size - b->in_pos;
914
if (in_avail < LZMA_IN_REQUIRED) {
915
if (in_avail > s->lzma2.compressed)
916
in_avail = s->lzma2.compressed;
918
memcpy(s->temp.buf, b->in + b->in_pos, in_avail);
919
s->temp.size = in_avail;
920
b->in_pos += in_avail;
927
* Take care of the LZMA2 control layer, and forward the job of actual LZMA
928
* decoding or copying of uncompressed chunks to other functions.
930
enum xz_ret xz_dec_lzma2_run(
931
struct xz_dec_lzma2 *s, struct xz_buf *b)
935
while (b->in_pos < b->in_size || s->lzma2.sequence == SEQ_LZMA_RUN) {
936
switch (s->lzma2.sequence) {
943
* 0x01 Dictionary reset followed by
944
* an uncompressed chunk
945
* 0x02 Uncompressed chunk (no dictionary reset)
947
* Highest three bits (s->control & 0xE0):
948
* 0xE0 Dictionary reset, new properties and state
949
* reset, followed by LZMA compressed chunk
950
* 0xC0 New properties and state reset, followed
951
* by LZMA compressed chunk (no dictionary
953
* 0xA0 State reset using old properties,
954
* followed by LZMA compressed chunk (no
956
* 0x80 LZMA chunk (no dictionary or state reset)
958
* For LZMA compressed chunks, the lowest five bits
959
* (s->control & 1F) are the highest bits of the
960
* uncompressed size (bits 16-20).
962
* A new LZMA2 stream must begin with a dictionary
963
* reset. The first LZMA chunk must set new
964
* properties and reset the LZMA state.
966
* Values that don't match anything described above
967
* are invalid and we return XZ_DATA_ERROR.
969
tmp = b->in[b->in_pos++];
971
if (tmp >= 0xE0 || tmp == 0x01) {
972
s->lzma2.need_props = true;
973
s->lzma2.need_dict_reset = false;
974
dict_reset(&s->dict, b);
975
} else if (s->lzma2.need_dict_reset) {
976
return XZ_DATA_ERROR;
980
s->lzma2.uncompressed = (tmp & 0x1F) << 16;
981
s->lzma2.sequence = SEQ_UNCOMPRESSED_1;
985
* When there are new properties,
986
* state reset is done at
989
s->lzma2.need_props = false;
990
s->lzma2.next_sequence
993
} else if (s->lzma2.need_props) {
994
return XZ_DATA_ERROR;
997
s->lzma2.next_sequence
1004
return XZ_STREAM_END;
1007
return XZ_DATA_ERROR;
1009
s->lzma2.sequence = SEQ_COMPRESSED_0;
1010
s->lzma2.next_sequence = SEQ_COPY;
1015
case SEQ_UNCOMPRESSED_1:
1016
s->lzma2.uncompressed
1017
+= (uint32_t)b->in[b->in_pos++] << 8;
1018
s->lzma2.sequence = SEQ_UNCOMPRESSED_2;
1021
case SEQ_UNCOMPRESSED_2:
1022
s->lzma2.uncompressed
1023
+= (uint32_t)b->in[b->in_pos++] + 1;
1024
s->lzma2.sequence = SEQ_COMPRESSED_0;
1027
case SEQ_COMPRESSED_0:
1029
= (uint32_t)b->in[b->in_pos++] << 8;
1030
s->lzma2.sequence = SEQ_COMPRESSED_1;
1033
case SEQ_COMPRESSED_1:
1035
+= (uint32_t)b->in[b->in_pos++] + 1;
1036
s->lzma2.sequence = s->lzma2.next_sequence;
1039
case SEQ_PROPERTIES:
1040
if (!lzma_props(s, b->in[b->in_pos++]))
1041
return XZ_DATA_ERROR;
1043
s->lzma2.sequence = SEQ_LZMA_PREPARE;
1045
case SEQ_LZMA_PREPARE:
1046
if (s->lzma2.compressed < RC_INIT_BYTES)
1047
return XZ_DATA_ERROR;
1049
if (!rc_read_init(&s->rc, b))
1052
s->lzma2.compressed -= RC_INIT_BYTES;
1053
s->lzma2.sequence = SEQ_LZMA_RUN;
1057
* Set dictionary limit to indicate how much we want
1058
* to be encoded at maximum. Decode new data into the
1059
* dictionary. Flush the new data from dictionary to
1060
* b->out. Check if we finished decoding this chunk.
1061
* In case the dictionary got full but we didn't fill
1062
* the output buffer yet, we may run this loop
1063
* multiple times without changing s->lzma2.sequence.
1065
dict_limit(&s->dict, min_t(size_t,
1066
b->out_size - b->out_pos,
1067
s->lzma2.uncompressed));
1068
if (!lzma2_lzma(s, b))
1069
return XZ_DATA_ERROR;
1071
s->lzma2.uncompressed -= dict_flush(&s->dict, b);
1073
if (s->lzma2.uncompressed == 0) {
1074
if (s->lzma2.compressed > 0 || s->lzma.len > 0
1075
|| !rc_is_finished(&s->rc))
1076
return XZ_DATA_ERROR;
1079
s->lzma2.sequence = SEQ_CONTROL;
1081
} else if (b->out_pos == b->out_size
1082
|| (b->in_pos == b->in_size
1084
< s->lzma2.compressed)) {
1091
dict_uncompressed(&s->dict, b, &s->lzma2.compressed);
1092
if (s->lzma2.compressed > 0)
1095
s->lzma2.sequence = SEQ_CONTROL;
1103
#ifdef GRUB_EMBED_DECOMPRESSOR
1104
#include <grub/decompressor.h>
1105
static struct xz_dec_lzma2 lzma2;
1108
struct xz_dec_lzma2 * xz_dec_lzma2_create(uint32_t dict_max)
1110
struct xz_dec_lzma2 *s;
1112
#ifndef GRUB_EMBED_DECOMPRESSOR
1113
/* Maximum supported dictionary by this implementation is 3 GiB. */
1114
if (dict_max > ((uint32_t)3 << 30))
1117
s = kmalloc(sizeof(*s), GFP_KERNEL);
1122
s->dict.buf = vmalloc(dict_max);
1123
if (s->dict.buf == NULL) {
1131
s->dict.buf = grub_decompressor_scratch;
1134
s->dict.allocated = dict_max;
1139
enum xz_ret xz_dec_lzma2_reset(
1140
struct xz_dec_lzma2 *s, uint8_t props)
1142
/* This limits dictionary size to 3 GiB (39) to keep parsing simpler. */
1143
if (props > ( min (DICT_BIT_SIZE,39)) )
1144
return XZ_OPTIONS_ERROR;
1146
s->dict.size = 2 + (props & 1);
1147
s->dict.size <<= (props >> 1) + 11;
1149
#ifndef GRUB_EMBED_DECOMPRESSOR
1150
if (s->dict.allocated > 0 && s->dict.allocated < s->dict.size)
1152
/* enlarge dictionary buffer */
1153
uint8_t * newdict = realloc(s->dict.buf,s->dict.size);
1156
return XZ_MEMLIMIT_ERROR;
1158
s->dict.buf = newdict;
1159
s->dict.allocated = s->dict.size;
1162
s->dict.end = s->dict.size;
1166
s->lzma2.sequence = SEQ_CONTROL;
1167
s->lzma2.need_dict_reset = true;
1174
void xz_dec_lzma2_end(struct xz_dec_lzma2 *s __attribute__ ((unused)))
1176
#ifndef GRUB_EMBED_DECOMPRESSOR
1177
if (s->dict.allocated > 0)