~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to grub-core/lib/xzembed/xz_dec_lzma2.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Robert Millan, Updated translations
  • Date: 2010-11-22 12:24:56 UTC
  • mfrom: (1.26.4 upstream) (17.3.36 sid)
  • mto: (17.3.43 sid)
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: james.westby@ubuntu.com-20101122122456-y82z3sfb7k4zfdcc
Tags: 1.99~20101122-1
[ Colin Watson ]
* New Bazaar snapshot.  Too many changes to list in full, but some of the
  more user-visible ones are as follows:
  - GRUB script:
    + Function parameters, "break", "continue", "shift", "setparams",
      "return", and "!".
    + "export" command supports multiple variable names.
    + Multi-line quoted strings support.
    + Wildcard expansion.
  - sendkey support.
  - USB hotunplugging and USB serial support.
  - Rename CD-ROM to cd on BIOS.
  - Add new --boot-directory option to grub-install, grub-reboot, and
    grub-set-default; the old --root-directory option is still accepted
    but was often confusing.
  - Basic btrfs detection/UUID support (but no file reading yet).
  - bash-completion for utilities.
  - If a device is listed in device.map, always assume that it is
    BIOS-visible rather than using extra layers such as LVM or RAID.
  - Add grub-mknetdir script (closes: #550658).
  - Remove deprecated "root" command.
  - Handle RAID devices containing virtio components.
  - GRUB Legacy configuration file support (via grub-menulst2cfg).
  - Keyboard layout support (via grub-mklayout and grub-kbdcomp).
  - Check generated grub.cfg for syntax errors before saving.
  - Pause execution for at most ten seconds if any errors are displayed,
    so that the user has a chance to see them.
  - Support submenus.
  - Write embedding zone using Reed-Solomon, so that it's robust against
    being partially overwritten (closes: #550702, #591416, #593347).
  - GRUB_DISABLE_LINUX_RECOVERY and GRUB_DISABLE_NETBSD_RECOVERY merged
    into a single GRUB_DISABLE_RECOVERY variable.
  - Fix loader memory allocation failure (closes: #551627).
  - Don't call savedefault on recovery entries (closes: #589325).
  - Support triple-indirect blocks on ext2 (closes: #543924).
  - Recognise DDF1 fake RAID (closes: #603354).

[ Robert Millan ]
* Use dpkg architecture wildcards.

[ Updated translations ]
* Slovenian (Vanja Cvelbar).  Closes: #604003
* Dzongkha (dawa pemo via Tenzin Dendup).  Closes: #604102

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* xz_dec_lzma2.c - LZMA2 decoder */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2010  Free Software Foundation, Inc.
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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/>.
 
18
 */
 
19
/*
 
20
 * This file is based on code from XZ embedded project
 
21
 * http://tukaani.org/xz/embedded.html
 
22
 */
 
23
 
 
24
#include "xz_private.h"
 
25
#include "xz_lzma2.h"
 
26
 
 
27
/*
 
28
 * Range decoder initialization eats the first five bytes of each LZMA chunk.
 
29
 */
 
30
#define RC_INIT_BYTES 5
 
31
 
 
32
/*
 
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.
 
38
 */
 
39
#define LZMA_IN_REQUIRED 21
 
40
 
 
41
/*
 
42
 * Dictionary (history buffer)
 
43
 *
 
44
 * These are always true:
 
45
 *    start <= pos <= full <= end
 
46
 *    pos <= limit <= end
 
47
 *
 
48
 * In multi-call mode, also these are true:
 
49
 *    end == size
 
50
 *    size <= allocated
 
51
 *
 
52
 * Most of these variables are size_t to support single-call mode,
 
53
 * in which the dictionary variables address the actual output
 
54
 * buffer directly.
 
55
 */
 
56
struct dictionary {
 
57
        /* Beginning of the history buffer */
 
58
        uint8_t *buf;
 
59
 
 
60
        /* Old position in buf (before decoding more data) */
 
61
        size_t start;
 
62
 
 
63
        /* Position in buf */
 
64
        size_t pos;
 
65
 
 
66
        /*
 
67
         * How full dictionary is. This is used to detect corrupt input that
 
68
         * would read beyond the beginning of the uncompressed stream.
 
69
         */
 
70
        size_t full;
 
71
 
 
72
        /* Write limit; we don't write to buf[limit] or later bytes. */
 
73
        size_t limit;
 
74
 
 
75
        /*
 
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.
 
79
         */
 
80
        size_t end;
 
81
 
 
82
        /*
 
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.
 
86
         */
 
87
        uint32_t size;
 
88
 
 
89
        /*
 
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.
 
93
         */
 
94
        uint32_t allocated;
 
95
};
 
96
 
 
97
/* Range decoder */
 
98
struct rc_dec {
 
99
        uint32_t range;
 
100
        uint32_t code;
 
101
 
 
102
        /*
 
103
         * Number of initializing bytes remaining to be read
 
104
         * by rc_read_init().
 
105
         */
 
106
        uint32_t init_bytes_left;
 
107
 
 
108
        /*
 
109
         * Buffer from which we read our input. It can be either
 
110
         * temp.buf or the caller-provided input buffer.
 
111
         */
 
112
        const uint8_t *in;
 
113
        size_t in_pos;
 
114
        size_t in_limit;
 
115
};
 
116
 
 
117
/* Probabilities for a length decoder. */
 
118
struct lzma_len_dec {
 
119
        /* Probability of match length being at least 10 */
 
120
        uint16_t choice;
 
121
 
 
122
        /* Probability of match length being at least 18 */
 
123
        uint16_t choice2;
 
124
 
 
125
        /* Probabilities for match lengths 2-9 */
 
126
        uint16_t low[POS_STATES_MAX][LEN_LOW_SYMBOLS];
 
127
 
 
128
        /* Probabilities for match lengths 10-17 */
 
129
        uint16_t mid[POS_STATES_MAX][LEN_MID_SYMBOLS];
 
130
 
 
131
        /* Probabilities for match lengths 18-273 */
 
132
        uint16_t high[LEN_HIGH_SYMBOLS];
 
133
};
 
134
 
 
135
struct lzma_dec {
 
136
        /*
 
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
 
140
         * position bits)
 
141
         */
 
142
        uint32_t lc;
 
143
        uint32_t literal_pos_mask; /* (1 << lp) - 1 */
 
144
        uint32_t pos_mask;         /* (1 << pb) - 1 */
 
145
 
 
146
        /* Types of the most recently seen LZMA symbols */
 
147
        enum lzma_state state;
 
148
 
 
149
        /* Distances of latest four matches */
 
150
        uint32_t rep0;
 
151
        uint32_t rep1;
 
152
        uint32_t rep2;
 
153
        uint32_t rep3;
 
154
 
 
155
        /*
 
156
         * Length of a match. This is updated so that dict_repeat can
 
157
         * be called again to finish repeating the whole match.
 
158
         */
 
159
        uint32_t len;
 
160
 
 
161
        /* If 1, it's a match. Otherwise it's a single 8-bit literal. */
 
162
        uint16_t is_match[STATES][POS_STATES_MAX];
 
163
 
 
164
        /* If 1, it's a repeated match. The distance is one of rep0 .. rep3. */
 
165
        uint16_t is_rep[STATES];
 
166
 
 
167
        /*
 
168
         * If 0, distance of a repeated match is rep0.
 
169
         * Otherwise check is_rep1.
 
170
         */
 
171
        uint16_t is_rep0[STATES];
 
172
 
 
173
        /*
 
174
         * If 0, distance of a repeated match is rep1.
 
175
         * Otherwise check is_rep2.
 
176
         */
 
177
        uint16_t is_rep1[STATES];
 
178
 
 
179
        /* If 0, distance of a repeated match is rep2. Otherwise it is rep3. */
 
180
        uint16_t is_rep2[STATES];
 
181
 
 
182
        /*
 
183
         * If 1, the repeated match has length of one byte. Otherwise
 
184
         * the length is decoded from rep_len_decoder.
 
185
         */
 
186
        uint16_t is_rep0_long[STATES][POS_STATES_MAX];
 
187
 
 
188
        /*
 
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].
 
192
         */
 
193
        uint16_t dist_slot[DIST_STATES][DIST_SLOTS];
 
194
 
 
195
        /*
 
196
         * Probility trees for additional bits for match distance
 
197
         * when the distance is in the range [4, 127].
 
198
         */
 
199
        uint16_t dist_special[FULL_DISTANCES - DIST_MODEL_END];
 
200
 
 
201
        /*
 
202
         * Probability tree for the lowest four bits of a match
 
203
         * distance that is equal to or greater than 128.
 
204
         */
 
205
        uint16_t dist_align[ALIGN_SIZE];
 
206
 
 
207
        /* Length of a normal match */
 
208
        struct lzma_len_dec match_len_dec;
 
209
 
 
210
        /* Length of a repeated match */
 
211
        struct lzma_len_dec rep_len_dec;
 
212
 
 
213
        /* Probabilities of literals */
 
214
        uint16_t literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE];
 
215
};
 
216
 
 
217
struct xz_dec_lzma2 {
 
218
        /* LZMA2 */
 
219
        struct {
 
220
                /* Position in xz_dec_lzma2_run(). */
 
221
                enum lzma2_seq {
 
222
                        SEQ_CONTROL,
 
223
                        SEQ_UNCOMPRESSED_1,
 
224
                        SEQ_UNCOMPRESSED_2,
 
225
                        SEQ_COMPRESSED_0,
 
226
                        SEQ_COMPRESSED_1,
 
227
                        SEQ_PROPERTIES,
 
228
                        SEQ_LZMA_PREPARE,
 
229
                        SEQ_LZMA_RUN,
 
230
                        SEQ_COPY
 
231
                } sequence;
 
232
 
 
233
                /*
 
234
                 * Next position after decoding the compressed size of
 
235
                 * the chunk.
 
236
                 */
 
237
                enum lzma2_seq next_sequence;
 
238
 
 
239
                /* Uncompressed size of LZMA chunk (2 MiB at maximum) */
 
240
                uint32_t uncompressed;
 
241
 
 
242
                /*
 
243
                 * Compressed size of LZMA chunk or compressed/uncompressed
 
244
                 * size of uncompressed chunk (64 KiB at maximum)
 
245
                 */
 
246
                uint32_t compressed;
 
247
 
 
248
                /*
 
249
                 * True if dictionary reset is needed. This is false before
 
250
                 * the first chunk (LZMA or uncompressed).
 
251
                 */
 
252
                bool need_dict_reset;
 
253
 
 
254
                /*
 
255
                 * True if new LZMA properties are needed. This is false
 
256
                 * before the first LZMA chunk.
 
257
                 */
 
258
                bool need_props;
 
259
        } lzma2;
 
260
 
 
261
        /*
 
262
         * Temporary buffer which holds small number of input bytes between
 
263
         * decoder calls. See lzma2_lzma() for details.
 
264
         */
 
265
        struct {
 
266
                uint32_t size;
 
267
                uint8_t buf[3 * LZMA_IN_REQUIRED];
 
268
        } temp;
 
269
 
 
270
        struct dictionary dict;
 
271
        struct rc_dec rc;
 
272
        struct lzma_dec lzma;
 
273
};
 
274
 
 
275
/**************
 
276
 * Dictionary *
 
277
 **************/
 
278
 
 
279
/*
 
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.
 
282
 */
 
283
static void dict_reset(struct dictionary *dict, struct xz_buf *b)
 
284
{
 
285
        if (dict->allocated == 0) {
 
286
                dict->buf = b->out + b->out_pos;
 
287
                dict->end = b->out_size - b->out_pos;
 
288
        }
 
289
        dict->start = 0;
 
290
        dict->pos = 0;
 
291
        dict->limit = 0;
 
292
        dict->full = 0;
 
293
}
 
294
 
 
295
/* Set dictionary write limit */
 
296
static void dict_limit(struct dictionary *dict, size_t out_max)
 
297
{
 
298
        if (dict->end - dict->pos <= out_max)
 
299
                dict->limit = dict->end;
 
300
        else
 
301
                dict->limit = dict->pos + out_max;
 
302
}
 
303
 
 
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)
 
306
{
 
307
        return dict->pos < dict->limit;
 
308
}
 
309
 
 
310
/*
 
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.
 
315
 */
 
316
static inline uint32_t dict_get(
 
317
                const struct dictionary *dict, uint32_t dist)
 
318
{
 
319
        size_t offset = dict->pos - dist - 1;
 
320
 
 
321
        if (dist >= dict->pos)
 
322
                offset += dict->end;
 
323
 
 
324
        return dict->full > 0 ? dict->buf[offset] : 0;
 
325
}
 
326
 
 
327
/*
 
328
 * Put one byte into the dictionary. It is assumed that there is space for it.
 
329
 */
 
330
static inline void dict_put(struct dictionary *dict, uint8_t byte)
 
331
{
 
332
        dict->buf[dict->pos++] = byte;
 
333
 
 
334
        if (dict->full < dict->pos)
 
335
                dict->full = dict->pos;
 
336
}
 
337
 
 
338
/*
 
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.
 
342
 */
 
343
static bool dict_repeat(
 
344
                struct dictionary *dict, uint32_t *len, uint32_t dist)
 
345
{
 
346
        size_t back;
 
347
        uint32_t left;
 
348
 
 
349
        if (dist >= dict->full || dist >= dict->size)
 
350
                return false;
 
351
 
 
352
        left = min_t(size_t, dict->limit - dict->pos, *len);
 
353
        *len -= left;
 
354
 
 
355
        back = dict->pos - dist - 1;
 
356
        if (dist >= dict->pos)
 
357
                back += dict->end;
 
358
 
 
359
        do {
 
360
                dict->buf[dict->pos++] = dict->buf[back++];
 
361
                if (back == dict->end)
 
362
                        back = 0;
 
363
        } while (--left > 0);
 
364
 
 
365
        if (dict->full < dict->pos)
 
366
                dict->full = dict->pos;
 
367
 
 
368
        return true;
 
369
}
 
370
 
 
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)
 
374
{
 
375
        size_t copy_size;
 
376
 
 
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)
 
384
                        copy_size = *left;
 
385
 
 
386
                *left -= copy_size;
 
387
 
 
388
                memcpy(dict->buf + dict->pos, b->in + b->in_pos, copy_size);
 
389
                dict->pos += copy_size;
 
390
 
 
391
                if (dict->full < dict->pos)
 
392
                        dict->full = dict->pos;
 
393
 
 
394
                if (dict->allocated != 0) {
 
395
                        if (dict->pos == dict->end)
 
396
                                dict->pos = 0;
 
397
 
 
398
                        memcpy(b->out + b->out_pos, b->in + b->in_pos,
 
399
                                        copy_size);
 
400
                }
 
401
 
 
402
                dict->start = dict->pos;
 
403
 
 
404
                b->out_pos += copy_size;
 
405
                b->in_pos += copy_size;
 
406
 
 
407
        }
 
408
}
 
409
 
 
410
/*
 
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.
 
414
 */
 
415
static uint32_t dict_flush(struct dictionary *dict, struct xz_buf *b)
 
416
{
 
417
        size_t copy_size = dict->pos - dict->start;
 
418
 
 
419
        if (dict->allocated != 0) {
 
420
                if (dict->pos == dict->end)
 
421
                        dict->pos = 0;
 
422
 
 
423
                memcpy(b->out + b->out_pos, dict->buf + dict->start,
 
424
                                copy_size);
 
425
        }
 
426
 
 
427
        dict->start = dict->pos;
 
428
        b->out_pos += copy_size;
 
429
        return copy_size;
 
430
}
 
431
 
 
432
/*****************
 
433
 * Range decoder *
 
434
 *****************/
 
435
 
 
436
/* Reset the range decoder. */
 
437
static void rc_reset(struct rc_dec *rc)
 
438
{
 
439
        rc->range = (uint32_t)-1;
 
440
        rc->code = 0;
 
441
        rc->init_bytes_left = RC_INIT_BYTES;
 
442
}
 
443
 
 
444
/*
 
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.)
 
447
 */
 
448
static bool rc_read_init(struct rc_dec *rc, struct xz_buf *b)
 
449
{
 
450
        while (rc->init_bytes_left > 0) {
 
451
                if (b->in_pos == b->in_size)
 
452
                        return false;
 
453
 
 
454
                rc->code = (rc->code << 8) + b->in[b->in_pos++];
 
455
                --rc->init_bytes_left;
 
456
        }
 
457
 
 
458
        return true;
 
459
}
 
460
 
 
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)
 
463
{
 
464
        return rc->in_pos > rc->in_limit;
 
465
}
 
466
 
 
467
/*
 
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.
 
470
 */
 
471
static inline bool rc_is_finished(const struct rc_dec *rc)
 
472
{
 
473
        return rc->code == 0;
 
474
}
 
475
 
 
476
/* Read the next input byte if needed. */
 
477
static __always_inline void rc_normalize(struct rc_dec *rc)
 
478
{
 
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++];
 
482
        }
 
483
}
 
484
 
 
485
/*
 
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.
 
491
 *
 
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.)
 
495
 */
 
496
static __always_inline int rc_bit(struct rc_dec *rc, uint16_t *prob)
 
497
{
 
498
        uint32_t bound;
 
499
        int bit;
 
500
 
 
501
        rc_normalize(rc);
 
502
        bound = (rc->range >> RC_BIT_MODEL_TOTAL_BITS) * *prob;
 
503
        if (rc->code < bound) {
 
504
                rc->range = bound;
 
505
                *prob += (RC_BIT_MODEL_TOTAL - *prob) >> RC_MOVE_BITS;
 
506
                bit = 0;
 
507
        } else {
 
508
                rc->range -= bound;
 
509
                rc->code -= bound;
 
510
                *prob -= *prob >> RC_MOVE_BITS;
 
511
                bit = 1;
 
512
        }
 
513
 
 
514
        return bit;
 
515
}
 
516
 
 
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)
 
520
{
 
521
        uint32_t symbol = 1;
 
522
 
 
523
        do {
 
524
                if (rc_bit(rc, &probs[symbol]))
 
525
                        symbol = (symbol << 1) + 1;
 
526
                else
 
527
                        symbol <<= 1;
 
528
        } while (symbol < limit);
 
529
 
 
530
        return symbol;
 
531
}
 
532
 
 
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)
 
536
{
 
537
        uint32_t symbol = 1;
 
538
        uint32_t i = 0;
 
539
 
 
540
        do {
 
541
                if (rc_bit(rc, &probs[symbol])) {
 
542
                        symbol = (symbol << 1) + 1;
 
543
                        *dest += 1 << i;
 
544
                } else {
 
545
                        symbol <<= 1;
 
546
                }
 
547
        } while (++i < limit);
 
548
}
 
549
 
 
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)
 
553
{
 
554
        uint32_t mask;
 
555
 
 
556
        do {
 
557
                rc_normalize(rc);
 
558
                rc->range >>= 1;
 
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);
 
564
}
 
565
 
 
566
/********
 
567
 * LZMA *
 
568
 ********/
 
569
 
 
570
/* Get pointer to literal coder probability array. */
 
571
static uint16_t * lzma_literal_probs(struct xz_dec_lzma2 *s)
 
572
{
 
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];
 
577
}
 
578
 
 
579
/* Decode a literal (one 8-bit byte) */
 
580
static void lzma_literal(struct xz_dec_lzma2 *s)
 
581
{
 
582
        uint16_t *probs;
 
583
        uint32_t symbol;
 
584
        uint32_t match_byte;
 
585
        uint32_t match_bit;
 
586
        uint32_t offset;
 
587
        uint32_t i;
 
588
 
 
589
        probs = lzma_literal_probs(s);
 
590
 
 
591
        if (lzma_state_is_literal(s->lzma.state)) {
 
592
                symbol = rc_bittree(&s->rc, probs, 0x100);
 
593
        } else {
 
594
                symbol = 1;
 
595
                match_byte = dict_get(&s->dict, s->lzma.rep0) << 1;
 
596
                offset = 0x100;
 
597
 
 
598
                do {
 
599
                        match_bit = match_byte & offset;
 
600
                        match_byte <<= 1;
 
601
                        i = offset + match_bit + symbol;
 
602
 
 
603
                        if (rc_bit(&s->rc, &probs[i])) {
 
604
                                symbol = (symbol << 1) + 1;
 
605
                                offset &= match_bit;
 
606
                        } else {
 
607
                                symbol <<= 1;
 
608
                                offset &= ~match_bit;
 
609
                        }
 
610
                } while (symbol < 0x100);
 
611
        }
 
612
 
 
613
        dict_put(&s->dict, (uint8_t)symbol);
 
614
        lzma_state_literal(&s->lzma.state);
 
615
}
 
616
 
 
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,
 
619
                uint32_t pos_state)
 
620
{
 
621
        uint16_t *probs;
 
622
        uint32_t limit;
 
623
 
 
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;
 
628
        } else {
 
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;
 
633
                } else {
 
634
                        probs = l->high;
 
635
                        limit = LEN_HIGH_SYMBOLS;
 
636
                        s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS
 
637
                                        + LEN_MID_SYMBOLS;
 
638
                }
 
639
        }
 
640
 
 
641
        s->lzma.len += rc_bittree(&s->rc, probs, limit) - limit;
 
642
}
 
643
 
 
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)
 
646
{
 
647
        uint16_t *probs;
 
648
        uint32_t dist_slot;
 
649
        uint32_t limit;
 
650
 
 
651
        lzma_state_match(&s->lzma.state);
 
652
 
 
653
        s->lzma.rep3 = s->lzma.rep2;
 
654
        s->lzma.rep2 = s->lzma.rep1;
 
655
        s->lzma.rep1 = s->lzma.rep0;
 
656
 
 
657
        lzma_len(s, &s->lzma.match_len_dec, pos_state);
 
658
 
 
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;
 
661
 
 
662
        if (dist_slot < DIST_MODEL_START) {
 
663
                s->lzma.rep0 = dist_slot;
 
664
        } else {
 
665
                limit = (dist_slot >> 1) - 1;
 
666
                s->lzma.rep0 = 2 + (dist_slot & 1);
 
667
 
 
668
                if (dist_slot < DIST_MODEL_END) {
 
669
                        s->lzma.rep0 <<= limit;
 
670
                        probs = s->lzma.dist_special + s->lzma.rep0
 
671
                                        - dist_slot - 1;
 
672
                        rc_bittree_reverse(&s->rc, probs,
 
673
                                        &s->lzma.rep0, limit);
 
674
                } else {
 
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);
 
679
                }
 
680
        }
 
681
}
 
682
 
 
683
/*
 
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.
 
686
 */
 
687
static void lzma_rep_match(struct xz_dec_lzma2 *s, uint32_t pos_state)
 
688
{
 
689
        uint32_t tmp;
 
690
 
 
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);
 
695
                        s->lzma.len = 1;
 
696
                        return;
 
697
                }
 
698
        } else {
 
699
                if (!rc_bit(&s->rc, &s->lzma.is_rep1[s->lzma.state])) {
 
700
                        tmp = s->lzma.rep1;
 
701
                } else {
 
702
                        if (!rc_bit(&s->rc, &s->lzma.is_rep2[s->lzma.state])) {
 
703
                                tmp = s->lzma.rep2;
 
704
                        } else {
 
705
                                tmp = s->lzma.rep3;
 
706
                                s->lzma.rep3 = s->lzma.rep2;
 
707
                        }
 
708
 
 
709
                        s->lzma.rep2 = s->lzma.rep1;
 
710
                }
 
711
 
 
712
                s->lzma.rep1 = s->lzma.rep0;
 
713
                s->lzma.rep0 = tmp;
 
714
        }
 
715
 
 
716
        lzma_state_long_rep(&s->lzma.state);
 
717
        lzma_len(s, &s->lzma.rep_len_dec, pos_state);
 
718
}
 
719
 
 
720
/* LZMA decoder core */
 
721
static bool lzma_main(struct xz_dec_lzma2 *s)
 
722
{
 
723
        uint32_t pos_state;
 
724
 
 
725
        /*
 
726
         * If the dictionary was reached during the previous call, try to
 
727
         * finish the possibly pending repeat in the dictionary.
 
728
         */
 
729
        if (dict_has_space(&s->dict) && s->lzma.len > 0)
 
730
                dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0);
 
731
 
 
732
        /*
 
733
         * Decode more LZMA symbols. One iteration may consume up to
 
734
         * LZMA_IN_REQUIRED - 1 bytes.
 
735
         */
 
736
        while (dict_has_space(&s->dict) && !rc_limit_exceeded(&s->rc)) {
 
737
                pos_state = s->dict.pos & s->lzma.pos_mask;
 
738
 
 
739
                if (!rc_bit(&s->rc, &s->lzma.is_match[
 
740
                                s->lzma.state][pos_state])) {
 
741
                        lzma_literal(s);
 
742
                } else {
 
743
                        if (rc_bit(&s->rc, &s->lzma.is_rep[s->lzma.state]))
 
744
                                lzma_rep_match(s, pos_state);
 
745
                        else
 
746
                                lzma_match(s, pos_state);
 
747
 
 
748
                        if (!dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0))
 
749
                                return false;
 
750
                }
 
751
        }
 
752
 
 
753
        /*
 
754
         * Having the range decoder always normalized when we are outside
 
755
         * this function makes it easier to correctly handle end of the chunk.
 
756
         */
 
757
        rc_normalize(&s->rc);
 
758
 
 
759
        return true;
 
760
}
 
761
 
 
762
/*
 
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.
 
765
 */
 
766
static void lzma_reset(struct xz_dec_lzma2 *s)
 
767
{
 
768
        uint16_t *probs;
 
769
        size_t i;
 
770
 
 
771
        s->lzma.state = STATE_LIT_LIT;
 
772
        s->lzma.rep0 = 0;
 
773
        s->lzma.rep1 = 0;
 
774
        s->lzma.rep2 = 0;
 
775
        s->lzma.rep3 = 0;
 
776
 
 
777
        /*
 
778
         * All probabilities are initialized to the same value. This hack
 
779
         * makes the code smaller by avoiding a separate loop for each
 
780
         * probability array.
 
781
         *
 
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.
 
785
         */
 
786
        probs = s->lzma.is_match[0];
 
787
        for (i = 0; i < PROBS_TOTAL; ++i)
 
788
                probs[i] = RC_BIT_MODEL_TOTAL / 2;
 
789
 
 
790
        rc_reset(&s->rc);
 
791
}
 
792
 
 
793
/*
 
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.
 
797
 */
 
798
static bool lzma_props(struct xz_dec_lzma2 *s, uint8_t props)
 
799
{
 
800
        if (props > (4 * 5 + 4) * 9 + 8)
 
801
                return false;
 
802
 
 
803
        s->lzma.pos_mask = 0;
 
804
        while (props >= 9 * 5) {
 
805
                props -= 9 * 5;
 
806
                ++s->lzma.pos_mask;
 
807
        }
 
808
 
 
809
        s->lzma.pos_mask = (1 << s->lzma.pos_mask) - 1;
 
810
 
 
811
        s->lzma.literal_pos_mask = 0;
 
812
        while (props >= 9) {
 
813
                props -= 9;
 
814
                ++s->lzma.literal_pos_mask;
 
815
        }
 
816
 
 
817
        s->lzma.lc = props;
 
818
 
 
819
        if (s->lzma.lc + s->lzma.literal_pos_mask > 4)
 
820
                return false;
 
821
 
 
822
        s->lzma.literal_pos_mask = (1 << s->lzma.literal_pos_mask) - 1;
 
823
 
 
824
        lzma_reset(s);
 
825
 
 
826
        return true;
 
827
}
 
828
 
 
829
/*********
 
830
 * LZMA2 *
 
831
 *********/
 
832
 
 
833
/*
 
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.
 
837
 *
 
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.
 
844
 */
 
845
static bool lzma2_lzma(struct xz_dec_lzma2 *s, struct xz_buf *b)
 
846
{
 
847
        size_t in_avail;
 
848
        uint32_t tmp;
 
849
 
 
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;
 
855
                if (tmp > in_avail)
 
856
                        tmp = in_avail;
 
857
 
 
858
                memcpy(s->temp.buf + s->temp.size, b->in + b->in_pos, tmp);
 
859
 
 
860
                if (s->temp.size + tmp == s->lzma2.compressed) {
 
861
                        memzero(s->temp.buf + s->temp.size + tmp,
 
862
                                        sizeof(s->temp.buf)
 
863
                                                - s->temp.size - tmp);
 
864
                        s->rc.in_limit = s->temp.size + tmp;
 
865
                } else if (s->temp.size + tmp < LZMA_IN_REQUIRED) {
 
866
                        s->temp.size += tmp;
 
867
                        b->in_pos += tmp;
 
868
                        return true;
 
869
                } else {
 
870
                        s->rc.in_limit = s->temp.size + tmp - LZMA_IN_REQUIRED;
 
871
                }
 
872
 
 
873
                s->rc.in = s->temp.buf;
 
874
                s->rc.in_pos = 0;
 
875
 
 
876
                if (!lzma_main(s) || s->rc.in_pos > s->temp.size + tmp)
 
877
                        return false;
 
878
 
 
879
                s->lzma2.compressed -= s->rc.in_pos;
 
880
 
 
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,
 
884
                                        s->temp.size);
 
885
                        return true;
 
886
                }
 
887
 
 
888
                b->in_pos += s->rc.in_pos - s->temp.size;
 
889
                s->temp.size = 0;
 
890
        }
 
891
 
 
892
        in_avail = b->in_size - b->in_pos;
 
893
        if (in_avail >= LZMA_IN_REQUIRED) {
 
894
                s->rc.in = b->in;
 
895
                s->rc.in_pos = b->in_pos;
 
896
 
 
897
                if (in_avail >= s->lzma2.compressed + LZMA_IN_REQUIRED)
 
898
                        s->rc.in_limit = b->in_pos + s->lzma2.compressed;
 
899
                else
 
900
                        s->rc.in_limit = b->in_size - LZMA_IN_REQUIRED;
 
901
 
 
902
                if (!lzma_main(s))
 
903
                        return false;
 
904
 
 
905
                in_avail = s->rc.in_pos - b->in_pos;
 
906
                if (in_avail > s->lzma2.compressed)
 
907
                        return false;
 
908
 
 
909
                s->lzma2.compressed -= in_avail;
 
910
                b->in_pos = s->rc.in_pos;
 
911
        }
 
912
 
 
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;
 
917
 
 
918
                memcpy(s->temp.buf, b->in + b->in_pos, in_avail);
 
919
                s->temp.size = in_avail;
 
920
                b->in_pos += in_avail;
 
921
        }
 
922
 
 
923
        return true;
 
924
}
 
925
 
 
926
/*
 
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.
 
929
 */
 
930
enum xz_ret xz_dec_lzma2_run(
 
931
                struct xz_dec_lzma2 *s, struct xz_buf *b)
 
932
{
 
933
        uint32_t tmp;
 
934
 
 
935
        while (b->in_pos < b->in_size || s->lzma2.sequence == SEQ_LZMA_RUN) {
 
936
                switch (s->lzma2.sequence) {
 
937
                case SEQ_CONTROL:
 
938
                        /*
 
939
                         * LZMA2 control byte
 
940
                         *
 
941
                         * Exact values:
 
942
                         *   0x00   End marker
 
943
                         *   0x01   Dictionary reset followed by
 
944
                         *          an uncompressed chunk
 
945
                         *   0x02   Uncompressed chunk (no dictionary reset)
 
946
                         *
 
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
 
952
                         *          reset)
 
953
                         *   0xA0   State reset using old properties,
 
954
                         *          followed by LZMA compressed chunk (no
 
955
                         *          dictionary reset)
 
956
                         *   0x80   LZMA chunk (no dictionary or state reset)
 
957
                         *
 
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).
 
961
                         *
 
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.
 
965
                         *
 
966
                         * Values that don't match anything described above
 
967
                         * are invalid and we return XZ_DATA_ERROR.
 
968
                         */
 
969
                        tmp = b->in[b->in_pos++];
 
970
 
 
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;
 
977
                        }
 
978
 
 
979
                        if (tmp >= 0x80) {
 
980
                                s->lzma2.uncompressed = (tmp & 0x1F) << 16;
 
981
                                s->lzma2.sequence = SEQ_UNCOMPRESSED_1;
 
982
 
 
983
                                if (tmp >= 0xC0) {
 
984
                                        /*
 
985
                                         * When there are new properties,
 
986
                                         * state reset is done at
 
987
                                         * SEQ_PROPERTIES.
 
988
                                         */
 
989
                                        s->lzma2.need_props = false;
 
990
                                        s->lzma2.next_sequence
 
991
                                                        = SEQ_PROPERTIES;
 
992
 
 
993
                                } else if (s->lzma2.need_props) {
 
994
                                        return XZ_DATA_ERROR;
 
995
 
 
996
                                } else {
 
997
                                        s->lzma2.next_sequence
 
998
                                                        = SEQ_LZMA_PREPARE;
 
999
                                        if (tmp >= 0xA0)
 
1000
                                                lzma_reset(s);
 
1001
                                }
 
1002
                        } else {
 
1003
                                if (tmp == 0x00)
 
1004
                                        return XZ_STREAM_END;
 
1005
 
 
1006
                                if (tmp > 0x02)
 
1007
                                        return XZ_DATA_ERROR;
 
1008
 
 
1009
                                s->lzma2.sequence = SEQ_COMPRESSED_0;
 
1010
                                s->lzma2.next_sequence = SEQ_COPY;
 
1011
                        }
 
1012
 
 
1013
                        break;
 
1014
 
 
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;
 
1019
                        break;
 
1020
 
 
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;
 
1025
                        break;
 
1026
 
 
1027
                case SEQ_COMPRESSED_0:
 
1028
                        s->lzma2.compressed
 
1029
                                        = (uint32_t)b->in[b->in_pos++] << 8;
 
1030
                        s->lzma2.sequence = SEQ_COMPRESSED_1;
 
1031
                        break;
 
1032
 
 
1033
                case SEQ_COMPRESSED_1:
 
1034
                        s->lzma2.compressed
 
1035
                                        += (uint32_t)b->in[b->in_pos++] + 1;
 
1036
                        s->lzma2.sequence = s->lzma2.next_sequence;
 
1037
                        break;
 
1038
 
 
1039
                case SEQ_PROPERTIES:
 
1040
                        if (!lzma_props(s, b->in[b->in_pos++]))
 
1041
                                return XZ_DATA_ERROR;
 
1042
 
 
1043
                        s->lzma2.sequence = SEQ_LZMA_PREPARE;
 
1044
 
 
1045
                case SEQ_LZMA_PREPARE:
 
1046
                        if (s->lzma2.compressed < RC_INIT_BYTES)
 
1047
                                return XZ_DATA_ERROR;
 
1048
 
 
1049
                        if (!rc_read_init(&s->rc, b))
 
1050
                                return XZ_OK;
 
1051
 
 
1052
                        s->lzma2.compressed -= RC_INIT_BYTES;
 
1053
                        s->lzma2.sequence = SEQ_LZMA_RUN;
 
1054
 
 
1055
                case SEQ_LZMA_RUN:
 
1056
                        /*
 
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.
 
1064
                         */
 
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;
 
1070
 
 
1071
                        s->lzma2.uncompressed -= dict_flush(&s->dict, b);
 
1072
 
 
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;
 
1077
 
 
1078
                                rc_reset(&s->rc);
 
1079
                                s->lzma2.sequence = SEQ_CONTROL;
 
1080
 
 
1081
                        } else if (b->out_pos == b->out_size
 
1082
                                        || (b->in_pos == b->in_size
 
1083
                                                && s->temp.size
 
1084
                                                < s->lzma2.compressed)) {
 
1085
                                return XZ_OK;
 
1086
                        }
 
1087
 
 
1088
                        break;
 
1089
 
 
1090
                case SEQ_COPY:
 
1091
                        dict_uncompressed(&s->dict, b, &s->lzma2.compressed);
 
1092
                        if (s->lzma2.compressed > 0)
 
1093
                                return XZ_OK;
 
1094
 
 
1095
                        s->lzma2.sequence = SEQ_CONTROL;
 
1096
                        break;
 
1097
                }
 
1098
        }
 
1099
 
 
1100
        return XZ_OK;
 
1101
}
 
1102
 
 
1103
#ifdef GRUB_EMBED_DECOMPRESSOR
 
1104
#include <grub/decompressor.h>
 
1105
static struct xz_dec_lzma2 lzma2;
 
1106
#endif
 
1107
 
 
1108
struct xz_dec_lzma2 * xz_dec_lzma2_create(uint32_t dict_max)
 
1109
{
 
1110
        struct xz_dec_lzma2 *s;
 
1111
 
 
1112
#ifndef GRUB_EMBED_DECOMPRESSOR
 
1113
        /* Maximum supported dictionary by this implementation is 3 GiB. */
 
1114
        if (dict_max > ((uint32_t)3 << 30))
 
1115
                return NULL;
 
1116
 
 
1117
        s = kmalloc(sizeof(*s), GFP_KERNEL);
 
1118
        if (s == NULL)
 
1119
                return NULL;
 
1120
 
 
1121
        if (dict_max > 0) {
 
1122
                s->dict.buf = vmalloc(dict_max);
 
1123
                if (s->dict.buf == NULL) {
 
1124
                        kfree(s);
 
1125
                        return NULL;
 
1126
                }
 
1127
        }
 
1128
 
 
1129
#else
 
1130
        s = &lzma2;
 
1131
        s->dict.buf = grub_decompressor_scratch;
 
1132
#endif
 
1133
 
 
1134
        s->dict.allocated = dict_max;
 
1135
 
 
1136
        return s;
 
1137
}
 
1138
 
 
1139
enum xz_ret xz_dec_lzma2_reset(
 
1140
                struct xz_dec_lzma2 *s, uint8_t props)
 
1141
{
 
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;
 
1145
 
 
1146
        s->dict.size = 2 + (props & 1);
 
1147
        s->dict.size <<= (props >> 1) + 11;
 
1148
 
 
1149
#ifndef GRUB_EMBED_DECOMPRESSOR
 
1150
        if (s->dict.allocated > 0 && s->dict.allocated < s->dict.size)
 
1151
        {
 
1152
                /* enlarge dictionary buffer */
 
1153
                uint8_t * newdict = realloc(s->dict.buf,s->dict.size);
 
1154
 
 
1155
                if (! newdict)
 
1156
                        return XZ_MEMLIMIT_ERROR;
 
1157
 
 
1158
                s->dict.buf = newdict;
 
1159
                s->dict.allocated = s->dict.size;
 
1160
        }
 
1161
#endif
 
1162
        s->dict.end = s->dict.size;
 
1163
 
 
1164
        s->lzma.len = 0;
 
1165
 
 
1166
        s->lzma2.sequence = SEQ_CONTROL;
 
1167
        s->lzma2.need_dict_reset = true;
 
1168
 
 
1169
        s->temp.size = 0;
 
1170
 
 
1171
        return XZ_OK;
 
1172
}
 
1173
 
 
1174
void xz_dec_lzma2_end(struct xz_dec_lzma2 *s __attribute__ ((unused)))
 
1175
{
 
1176
#ifndef GRUB_EMBED_DECOMPRESSOR
 
1177
        if (s->dict.allocated > 0)
 
1178
                vfree(s->dict.buf);
 
1179
 
 
1180
        kfree(s);
 
1181
#endif
 
1182
}