Coverage Report

Created: 2026-12-01 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/lib/xz/xz_dec_lzma2.c
Line
Count
Source
1
/*
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
47.6k
#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
361k
#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
37
 *    size <= size_max
38
 *    allocated <= size
39
 *
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
 */
44
struct 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
  /*
78
   * 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.)
87
   */
88
  uint32_t allocated;
89
90
  /* Operation mode */
91
  enum xz_mode mode;
92
};
93
94
/* Range decoder */
95
struct 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. */
115
struct 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
132
struct lzma_dec {
133
  /* 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
148
  /*
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
158
  /* 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
214
struct 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
253
struct xz_dec_lzma2 {
254
  /*
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;
267
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;
276
};
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
 */
286
static void dict_reset(struct dictionary *dict, struct xz_buf *b)
287
16.4k
{
288
16.4k
  if (DEC_IS_SINGLE(dict->mode)) {
289
0
    dict->buf = b->out + b->out_pos;
290
0
    dict->end = b->out_size - b->out_pos;
291
0
  }
292
293
16.4k
  dict->start = 0;
294
16.4k
  dict->pos = 0;
295
16.4k
  dict->limit = 0;
296
16.4k
  dict->full = 0;
297
16.4k
}
298
299
/* Set dictionary write limit */
300
static void dict_limit(struct dictionary *dict, size_t out_max)
301
114k
{
302
114k
  if (dict->end - dict->pos <= out_max)
303
12.5k
    dict->limit = dict->end;
304
102k
  else
305
102k
    dict->limit = dict->pos + out_max;
306
114k
}
307
308
/* Return true if at least one byte can be written into the dictionary. */
309
static inline bool dict_has_space(const struct dictionary *dict)
310
7.03M
{
311
7.03M
  return dict->pos < dict->limit;
312
7.03M
}
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
 */
320
static inline uint32_t dict_get(const struct dictionary *dict, uint32_t dist)
321
5.32M
{
322
5.32M
  size_t offset = dict->pos - dist - 1;
323
324
5.32M
  if (dist >= dict->pos)
325
15.2k
    offset += dict->end;
326
327
5.32M
  return dict->full > 0 ? dict->buf[offset] : 0;
328
5.32M
}
329
330
/*
331
 * Put one byte into the dictionary. It is assumed that there is space for it.
332
 */
333
static inline void dict_put(struct dictionary *dict, uint8_t byte)
334
5.24M
{
335
5.24M
  dict->buf[dict->pos++] = byte;
336
337
5.24M
  if (dict->full < dict->pos)
338
3.56M
    dict->full = dict->pos;
339
5.24M
}
340
341
/*
342
 * Repeat given number of bytes from the given distance. If the distance is
343
 * invalid, false is returned. On success, true is returned and *len is
344
 * updated to indicate how many bytes were left to be repeated.
345
 */
346
static bool dict_repeat(struct dictionary *dict, uint32_t *len, uint32_t dist)
347
1.65M
{
348
1.65M
  size_t back;
349
1.65M
  uint32_t left;
350
351
1.65M
  if (dist >= dict->full || dist >= dict->size)
352
4.29k
    return false;
353
354
1.64M
  left = min_t(size_t, dict->limit - dict->pos, *len);
355
1.64M
  *len -= left;
356
357
1.64M
  back = dict->pos - dist - 1;
358
1.64M
  if (dist >= dict->pos)
359
16.4k
    back += dict->end;
360
361
392M
  do {
362
392M
    dict->buf[dict->pos++] = dict->buf[back++];
363
392M
    if (back == dict->end)
364
10.0k
      back = 0;
365
392M
  } while (--left > 0);
366
367
1.64M
  if (dict->full < dict->pos)
368
967k
    dict->full = dict->pos;
369
370
1.64M
  return true;
371
1.65M
}
372
373
/* Copy uncompressed data as is from input to dictionary and output buffers. */
374
static void dict_uncompressed(struct dictionary *dict, struct xz_buf *b,
375
            uint32_t *left)
376
3.88k
{
377
3.88k
  size_t copy_size;
378
379
8.18k
  while (*left > 0 && b->in_pos < b->in_size
380
5.16k
      && b->out_pos < b->out_size) {
381
4.30k
    copy_size = min(b->in_size - b->in_pos,
382
4.30k
        b->out_size - b->out_pos);
383
4.30k
    if (copy_size > dict->end - dict->pos)
384
556
      copy_size = dict->end - dict->pos;
385
4.30k
    if (copy_size > *left)
386
2.58k
      copy_size = *left;
387
388
4.30k
    *left -= copy_size;
389
390
4.30k
    memcpy(dict->buf + dict->pos, b->in + b->in_pos, copy_size);
391
4.30k
    dict->pos += copy_size;
392
393
4.30k
    if (dict->full < dict->pos)
394
2.82k
      dict->full = dict->pos;
395
396
4.30k
    if (DEC_IS_MULTI(dict->mode)) {
397
4.30k
      if (dict->pos == dict->end)
398
519
        dict->pos = 0;
399
400
4.30k
      memcpy(b->out + b->out_pos, b->in + b->in_pos,
401
4.30k
          copy_size);
402
4.30k
    }
403
404
4.30k
    dict->start = dict->pos;
405
406
4.30k
    b->out_pos += copy_size;
407
4.30k
    b->in_pos += copy_size;
408
4.30k
  }
409
3.88k
}
410
411
/*
412
 * Flush pending data from dictionary to b->out. It is assumed that there is
413
 * enough space in b->out. This is guaranteed because caller uses dict_limit()
414
 * before decoding data into the dictionary.
415
 */
416
static uint32_t dict_flush(struct dictionary *dict, struct xz_buf *b)
417
109k
{
418
109k
  size_t copy_size = dict->pos - dict->start;
419
420
109k
  if (DEC_IS_MULTI(dict->mode)) {
421
109k
    if (dict->pos == dict->end)
422
12.1k
      dict->pos = 0;
423
424
109k
    memcpy(b->out + b->out_pos, dict->buf + dict->start,
425
109k
        copy_size);
426
109k
  }
427
428
109k
  dict->start = dict->pos;
429
109k
  b->out_pos += copy_size;
430
109k
  return copy_size;
431
109k
}
432
433
/*****************
434
 * Range decoder *
435
 *****************/
436
437
/* Reset the range decoder. */
438
static void rc_reset(struct rc_dec *rc)
439
19.2k
{
440
19.2k
  rc->range = (uint32_t)-1;
441
19.2k
  rc->code = 0;
442
19.2k
  rc->init_bytes_left = RC_INIT_BYTES;
443
19.2k
}
444
445
/*
446
 * Read the first five initial bytes into rc->code if they haven't been
447
 * read already. (Yes, the first byte gets completely ignored.)
448
 */
449
static bool rc_read_init(struct rc_dec *rc, struct xz_buf *b)
450
14.0k
{
451
84.4k
  while (rc->init_bytes_left > 0) {
452
70.3k
    if (b->in_pos == b->in_size)
453
40
      return false;
454
455
70.3k
    rc->code = (rc->code << 8) + b->in[b->in_pos++];
456
70.3k
    --rc->init_bytes_left;
457
70.3k
  }
458
459
14.0k
  return true;
460
14.0k
}
461
462
/* Return true if there may not be enough input for the next decoding loop. */
463
static inline bool rc_limit_exceeded(const struct rc_dec *rc)
464
6.81M
{
465
6.81M
  return rc->in_pos > rc->in_limit;
466
6.81M
}
467
468
/*
469
 * Return true if it is possible (from point of view of range decoder) that
470
 * we have reached the end of the LZMA chunk.
471
 */
472
static inline bool rc_is_finished(const struct rc_dec *rc)
473
5.47k
{
474
5.47k
  return rc->code == 0;
475
5.47k
}
476
477
/* Read the next input byte if needed. */
478
static __always_inline void rc_normalize(struct rc_dec *rc)
479
69.0M
{
480
69.0M
  if (rc->range < RC_TOP_VALUE) {
481
875k
    rc->range <<= RC_SHIFT_BITS;
482
875k
    rc->code = (rc->code << RC_SHIFT_BITS) + rc->in[rc->in_pos++];
483
875k
  }
484
69.0M
}
485
486
/*
487
 * Decode one bit. In some versions, this function has been splitted in three
488
 * functions so that the compiler is supposed to be able to more easily avoid
489
 * an extra branch. In this particular version of the LZMA decoder, this
490
 * doesn't seem to be a good idea (tested with GCC 3.3.6, 3.4.6, and 4.3.3
491
 * on x86). Using a non-splitted version results in nicer looking code too.
492
 *
493
 * NOTE: This must return an int. Do not make it return a bool or the speed
494
 * of the code generated by GCC 3.x decreases 10-15 %. (GCC 4.3 doesn't care,
495
 * and it generates 10-20 % faster code than GCC 3.x from this file anyway.)
496
 */
497
static __always_inline int rc_bit(struct rc_dec *rc, uint16_t *prob)
498
68.8M
{
499
68.8M
  uint32_t bound;
500
68.8M
  int bit;
501
502
68.8M
  rc_normalize(rc);
503
68.8M
  bound = (rc->range >> RC_BIT_MODEL_TOTAL_BITS) * *prob;
504
68.8M
  if (rc->code < bound) {
505
48.7M
    rc->range = bound;
506
48.7M
    *prob += (RC_BIT_MODEL_TOTAL - *prob) >> RC_MOVE_BITS;
507
48.7M
    bit = 0;
508
48.7M
  } else {
509
20.1M
    rc->range -= bound;
510
20.1M
    rc->code -= bound;
511
20.1M
    *prob -= *prob >> RC_MOVE_BITS;
512
20.1M
    bit = 1;
513
20.1M
  }
514
515
68.8M
  return bit;
516
68.8M
}
517
518
/* Decode a bittree starting from the most significant bit. */
519
static __always_inline uint32_t rc_bittree(struct rc_dec *rc,
520
             uint16_t *probs, uint32_t limit)
521
6.75M
{
522
6.75M
  uint32_t symbol = 1;
523
524
53.4M
  do {
525
53.4M
    if (rc_bit(rc, &probs[symbol]))
526
11.9M
      symbol = (symbol << 1) + 1;
527
41.5M
    else
528
41.5M
      symbol <<= 1;
529
53.4M
  } while (symbol < limit);
530
531
6.75M
  return symbol;
532
6.75M
}
533
534
/* Decode a bittree starting from the least significant bit. */
535
static __always_inline void rc_bittree_reverse(struct rc_dec *rc,
536
                 uint16_t *probs,
537
                 uint32_t *dest, uint32_t limit)
538
35.9k
{
539
35.9k
  uint32_t symbol = 1;
540
35.9k
  uint32_t i = 0;
541
542
128k
  do {
543
128k
    if (rc_bit(rc, &probs[symbol])) {
544
73.5k
      symbol = (symbol << 1) + 1;
545
73.5k
      *dest += 1 << i;
546
73.5k
    } else {
547
54.9k
      symbol <<= 1;
548
54.9k
    }
549
128k
  } while (++i < limit);
550
35.9k
}
551
552
/* Decode direct bits (fixed fifty-fifty probability) */
553
static inline void rc_direct(struct rc_dec *rc, uint32_t *dest, uint32_t limit)
554
15.6k
{
555
15.6k
  uint32_t mask;
556
557
109k
  do {
558
109k
    rc_normalize(rc);
559
109k
    rc->range >>= 1;
560
109k
    rc->code -= rc->range;
561
109k
    mask = (uint32_t)0 - (rc->code >> 31);
562
109k
    rc->code += rc->range & mask;
563
109k
    *dest = (*dest << 1) + (mask + 1);
564
109k
  } while (--limit > 0);
565
15.6k
}
566
567
/********
568
 * LZMA *
569
 ********/
570
571
/* Get pointer to literal coder probability array. */
572
static uint16_t *lzma_literal_probs(struct xz_dec_lzma2 *s)
573
5.24M
{
574
5.24M
  uint32_t prev_byte = dict_get(&s->dict, 0);
575
5.24M
  uint32_t low = prev_byte >> (8 - s->lzma.lc);
576
5.24M
  uint32_t high = (s->dict.pos & s->lzma.literal_pos_mask) << s->lzma.lc;
577
5.24M
  return s->lzma.literal[low + high];
578
5.24M
}
579
580
/* Decode a literal (one 8-bit byte) */
581
static void lzma_literal(struct xz_dec_lzma2 *s)
582
5.24M
{
583
5.24M
  uint16_t *probs;
584
5.24M
  uint32_t symbol;
585
5.24M
  uint32_t match_byte;
586
5.24M
  uint32_t match_bit;
587
5.24M
  uint32_t offset;
588
5.24M
  uint32_t i;
589
590
5.24M
  probs = lzma_literal_probs(s);
591
592
5.24M
  if (lzma_state_is_literal(s->lzma.state)) {
593
5.17M
    symbol = rc_bittree(&s->rc, probs, 0x100);
594
5.17M
  } else {
595
76.1k
    symbol = 1;
596
76.1k
    match_byte = dict_get(&s->dict, s->lzma.rep0) << 1;
597
76.1k
    offset = 0x100;
598
599
609k
    do {
600
609k
      match_bit = match_byte & offset;
601
609k
      match_byte <<= 1;
602
609k
      i = offset + match_bit + symbol;
603
604
609k
      if (rc_bit(&s->rc, &probs[i])) {
605
241k
        symbol = (symbol << 1) + 1;
606
241k
        offset &= match_bit;
607
367k
      } else {
608
367k
        symbol <<= 1;
609
367k
        offset &= ~match_bit;
610
367k
      }
611
609k
    } while (symbol < 0x100);
612
76.1k
  }
613
614
5.24M
  dict_put(&s->dict, (uint8_t)symbol);
615
5.24M
  lzma_state_literal(&s->lzma.state);
616
5.24M
}
617
618
/* Decode the length of the match into s->lzma.len. */
619
static void lzma_len(struct xz_dec_lzma2 *s, struct lzma_len_dec *l,
620
         uint32_t pos_state)
621
1.53M
{
622
1.53M
  uint16_t *probs;
623
1.53M
  uint32_t limit;
624
625
1.53M
  if (!rc_bit(&s->rc, &l->choice)) {
626
67.4k
    probs = l->low[pos_state];
627
67.4k
    limit = LEN_LOW_SYMBOLS;
628
67.4k
    s->lzma.len = MATCH_LEN_MIN;
629
1.46M
  } else {
630
1.46M
    if (!rc_bit(&s->rc, &l->choice2)) {
631
23.2k
      probs = l->mid[pos_state];
632
23.2k
      limit = LEN_MID_SYMBOLS;
633
23.2k
      s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS;
634
1.44M
    } else {
635
1.44M
      probs = l->high;
636
1.44M
      limit = LEN_HIGH_SYMBOLS;
637
1.44M
      s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS
638
1.44M
          + LEN_MID_SYMBOLS;
639
1.44M
    }
640
1.46M
  }
641
642
1.53M
  s->lzma.len += rc_bittree(&s->rc, probs, limit) - limit;
643
1.53M
}
644
645
/* Decode a match. The distance will be stored in s->lzma.rep0. */
646
static void lzma_match(struct xz_dec_lzma2 *s, uint32_t pos_state)
647
43.3k
{
648
43.3k
  uint16_t *probs;
649
43.3k
  uint32_t dist_slot;
650
43.3k
  uint32_t limit;
651
652
43.3k
  lzma_state_match(&s->lzma.state);
653
654
43.3k
  s->lzma.rep3 = s->lzma.rep2;
655
43.3k
  s->lzma.rep2 = s->lzma.rep1;
656
43.3k
  s->lzma.rep1 = s->lzma.rep0;
657
658
43.3k
  lzma_len(s, &s->lzma.match_len_dec, pos_state);
659
660
43.3k
  probs = s->lzma.dist_slot[lzma_get_dist_state(s->lzma.len)];
661
43.3k
  dist_slot = rc_bittree(&s->rc, probs, DIST_SLOTS) - DIST_SLOTS;
662
663
43.3k
  if (dist_slot < DIST_MODEL_START) {
664
7.39k
    s->lzma.rep0 = dist_slot;
665
35.9k
  } else {
666
35.9k
    limit = (dist_slot >> 1) - 1;
667
35.9k
    s->lzma.rep0 = 2 + (dist_slot & 1);
668
669
35.9k
    if (dist_slot < DIST_MODEL_END) {
670
20.3k
      s->lzma.rep0 <<= limit;
671
20.3k
      probs = s->lzma.dist_special + s->lzma.rep0
672
20.3k
          - dist_slot - 1;
673
20.3k
      rc_bittree_reverse(&s->rc, probs,
674
20.3k
          &s->lzma.rep0, limit);
675
20.3k
    } else {
676
15.6k
      rc_direct(&s->rc, &s->lzma.rep0, limit - ALIGN_BITS);
677
15.6k
      s->lzma.rep0 <<= ALIGN_BITS;
678
15.6k
      rc_bittree_reverse(&s->rc, s->lzma.dist_align,
679
15.6k
          &s->lzma.rep0, ALIGN_BITS);
680
15.6k
    }
681
35.9k
  }
682
43.3k
}
683
684
/*
685
 * Decode a repeated match. The distance is one of the four most recently
686
 * seen matches. The distance will be stored in s->lzma.rep0.
687
 */
688
static void lzma_rep_match(struct xz_dec_lzma2 *s, uint32_t pos_state)
689
1.52M
{
690
1.52M
  uint32_t tmp;
691
692
1.52M
  if (!rc_bit(&s->rc, &s->lzma.is_rep0[s->lzma.state])) {
693
1.30M
    if (!rc_bit(&s->rc, &s->lzma.is_rep0_long[
694
1.30M
        s->lzma.state][pos_state])) {
695
26.9k
      lzma_state_short_rep(&s->lzma.state);
696
26.9k
      s->lzma.len = 1;
697
26.9k
      return;
698
26.9k
    }
699
1.30M
  } else {
700
216k
    if (!rc_bit(&s->rc, &s->lzma.is_rep1[s->lzma.state])) {
701
19.2k
      tmp = s->lzma.rep1;
702
196k
    } else {
703
196k
      if (!rc_bit(&s->rc, &s->lzma.is_rep2[s->lzma.state])) {
704
13.1k
        tmp = s->lzma.rep2;
705
183k
      } else {
706
183k
        tmp = s->lzma.rep3;
707
183k
        s->lzma.rep3 = s->lzma.rep2;
708
183k
      }
709
710
196k
      s->lzma.rep2 = s->lzma.rep1;
711
196k
    }
712
713
216k
    s->lzma.rep1 = s->lzma.rep0;
714
216k
    s->lzma.rep0 = tmp;
715
216k
  }
716
717
1.49M
  lzma_state_long_rep(&s->lzma.state);
718
1.49M
  lzma_len(s, &s->lzma.rep_len_dec, pos_state);
719
1.49M
}
720
721
/* LZMA decoder core */
722
static bool lzma_main(struct xz_dec_lzma2 *s)
723
112k
{
724
112k
  uint32_t pos_state;
725
726
  /*
727
   * If the dictionary was reached during the previous call, try to
728
   * finish the possibly pending repeat in the dictionary.
729
   */
730
112k
  if (dict_has_space(&s->dict) && s->lzma.len > 0)
731
90.0k
    dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0);
732
733
  /*
734
   * Decode more LZMA symbols. One iteration may consume up to
735
   * LZMA_IN_REQUIRED - 1 bytes.
736
   */
737
6.92M
  while (dict_has_space(&s->dict) && !rc_limit_exceeded(&s->rc)) {
738
6.81M
    pos_state = s->dict.pos & s->lzma.pos_mask;
739
740
6.81M
    if (!rc_bit(&s->rc, &s->lzma.is_match[
741
6.81M
        s->lzma.state][pos_state])) {
742
5.24M
      lzma_literal(s);
743
5.24M
    } else {
744
1.56M
      if (rc_bit(&s->rc, &s->lzma.is_rep[s->lzma.state]))
745
1.52M
        lzma_rep_match(s, pos_state);
746
43.3k
      else
747
43.3k
        lzma_match(s, pos_state);
748
749
1.56M
      if (!dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0))
750
4.29k
        return false;
751
1.56M
    }
752
6.81M
  }
753
754
  /*
755
   * Having the range decoder always normalized when we are outside
756
   * this function makes it easier to correctly handle end of the chunk.
757
   */
758
108k
  rc_normalize(&s->rc);
759
760
108k
  return true;
761
112k
}
762
763
/*
764
 * Reset the LZMA decoder and range decoder state. Dictionary is nore reset
765
 * here, because LZMA state may be reset without resetting the dictionary.
766
 */
767
static void lzma_reset(struct xz_dec_lzma2 *s)
768
14.1k
{
769
14.1k
  uint16_t *probs;
770
14.1k
  size_t i;
771
772
14.1k
  s->lzma.state = STATE_LIT_LIT;
773
14.1k
  s->lzma.rep0 = 0;
774
14.1k
  s->lzma.rep1 = 0;
775
14.1k
  s->lzma.rep2 = 0;
776
14.1k
  s->lzma.rep3 = 0;
777
778
  /*
779
   * All probabilities are initialized to the same value. This hack
780
   * makes the code smaller by avoiding a separate loop for each
781
   * probability array.
782
   *
783
   * This could be optimized so that only that part of literal
784
   * probabilities that are actually required. In the common case
785
   * we would write 12 KiB less.
786
   */
787
14.1k
  probs = s->lzma.is_match[0];
788
199M
  for (i = 0; i < PROBS_TOTAL; ++i)
789
199M
    probs[i] = RC_BIT_MODEL_TOTAL / 2;
790
791
14.1k
  rc_reset(&s->rc);
792
14.1k
}
793
794
/*
795
 * Decode and validate LZMA properties (lc/lp/pb) and calculate the bit masks
796
 * from the decoded lp and pb values. On success, the LZMA decoder state is
797
 * reset and true is returned.
798
 */
799
static bool lzma_props(struct xz_dec_lzma2 *s, uint8_t props)
800
14.6k
{
801
14.6k
  if (props > (4 * 5 + 4) * 9 + 8)
802
325
    return false;
803
804
14.2k
  s->lzma.pos_mask = 0;
805
38.4k
  while (props >= 9 * 5) {
806
24.1k
    props -= 9 * 5;
807
24.1k
    ++s->lzma.pos_mask;
808
24.1k
  }
809
810
14.2k
  s->lzma.pos_mask = (1 << s->lzma.pos_mask) - 1;
811
812
14.2k
  s->lzma.literal_pos_mask = 0;
813
22.4k
  while (props >= 9) {
814
8.17k
    props -= 9;
815
8.17k
    ++s->lzma.literal_pos_mask;
816
8.17k
  }
817
818
14.2k
  s->lzma.lc = props;
819
820
14.2k
  if (s->lzma.lc + s->lzma.literal_pos_mask > 4)
821
618
    return false;
822
823
13.6k
  s->lzma.literal_pos_mask = (1 << s->lzma.literal_pos_mask) - 1;
824
825
13.6k
  lzma_reset(s);
826
827
13.6k
  return true;
828
14.2k
}
829
830
/*********
831
 * LZMA2 *
832
 *********/
833
834
/*
835
 * The LZMA decoder assumes that if the input limit (s->rc.in_limit) hasn't
836
 * been exceeded, it is safe to read up to LZMA_IN_REQUIRED bytes. This
837
 * wrapper function takes care of making the LZMA decoder's assumption safe.
838
 *
839
 * As long as there is plenty of input left to be decoded in the current LZMA
840
 * chunk, we decode directly from the caller-supplied input buffer until
841
 * there's LZMA_IN_REQUIRED bytes left. Those remaining bytes are copied into
842
 * s->temp.buf, which (hopefully) gets filled on the next call to this
843
 * function. We decode a few bytes from the temporary buffer so that we can
844
 * continue decoding from the caller-supplied input buffer again.
845
 */
846
static bool lzma2_lzma(struct xz_dec_lzma2 *s, struct xz_buf *b)
847
114k
{
848
114k
  size_t in_avail;
849
114k
  uint32_t tmp;
850
851
114k
  in_avail = b->in_size - b->in_pos;
852
114k
  if (s->temp.size > 0 || s->lzma2.compressed == 0) {
853
5.15k
    tmp = 2 * LZMA_IN_REQUIRED - s->temp.size;
854
5.15k
    if (tmp > s->lzma2.compressed - s->temp.size)
855
4.07k
      tmp = s->lzma2.compressed - s->temp.size;
856
5.15k
    if (tmp > in_avail)
857
1.15k
      tmp = in_avail;
858
859
5.15k
    memcpy(s->temp.buf + s->temp.size, b->in + b->in_pos, tmp);
860
861
5.15k
    if (s->temp.size + tmp == s->lzma2.compressed) {
862
3.99k
      memzero(s->temp.buf + s->temp.size + tmp,
863
3.99k
          sizeof(s->temp.buf)
864
3.99k
            - s->temp.size - tmp);
865
3.99k
      s->rc.in_limit = s->temp.size + tmp;
866
3.99k
    } else if (s->temp.size + tmp < LZMA_IN_REQUIRED) {
867
1.15k
      s->temp.size += tmp;
868
1.15k
      b->in_pos += tmp;
869
1.15k
      return true;
870
1.15k
    } else {
871
0
      s->rc.in_limit = s->temp.size + tmp - LZMA_IN_REQUIRED;
872
0
    }
873
874
3.99k
    s->rc.in = s->temp.buf;
875
3.99k
    s->rc.in_pos = 0;
876
877
3.99k
    if (!lzma_main(s) || s->rc.in_pos > s->temp.size + tmp)
878
1.39k
      return false;
879
880
2.60k
    s->lzma2.compressed -= s->rc.in_pos;
881
882
2.60k
    if (s->rc.in_pos < s->temp.size) {
883
1.92k
      s->temp.size -= s->rc.in_pos;
884
1.92k
      memmove(s->temp.buf, s->temp.buf + s->rc.in_pos,
885
1.92k
          s->temp.size);
886
1.92k
      return true;
887
1.92k
    }
888
889
682
    b->in_pos += s->rc.in_pos - s->temp.size;
890
682
    s->temp.size = 0;
891
682
  }
892
893
110k
  in_avail = b->in_size - b->in_pos;
894
110k
  if (in_avail >= LZMA_IN_REQUIRED) {
895
108k
    s->rc.in = b->in;
896
108k
    s->rc.in_pos = b->in_pos;
897
898
108k
    if (in_avail >= s->lzma2.compressed + LZMA_IN_REQUIRED)
899
79.6k
      s->rc.in_limit = b->in_pos + s->lzma2.compressed;
900
29.2k
    else
901
29.2k
      s->rc.in_limit = b->in_size - LZMA_IN_REQUIRED;
902
903
108k
    if (!lzma_main(s))
904
3.76k
      return false;
905
906
105k
    in_avail = s->rc.in_pos - b->in_pos;
907
105k
    if (in_avail > s->lzma2.compressed)
908
533
      return false;
909
910
104k
    s->lzma2.compressed -= in_avail;
911
104k
    b->in_pos = s->rc.in_pos;
912
104k
  }
913
914
106k
  in_avail = b->in_size - b->in_pos;
915
106k
  if (in_avail < LZMA_IN_REQUIRED) {
916
3.22k
    if (in_avail > s->lzma2.compressed)
917
690
      in_avail = s->lzma2.compressed;
918
919
3.22k
    memcpy(s->temp.buf, b->in + b->in_pos, in_avail);
920
3.22k
    s->temp.size = in_avail;
921
3.22k
    b->in_pos += in_avail;
922
3.22k
  }
923
924
106k
  return true;
925
110k
}
926
927
/*
928
 * Take care of the LZMA2 control layer, and forward the job of actual LZMA
929
 * decoding or copying of uncompressed chunks to other functions.
930
 */
931
XZ_EXTERN enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s,
932
               struct xz_buf *b)
933
113k
{
934
113k
  uint32_t tmp;
935
936
213k
  while (b->in_pos < b->in_size || s->lzma2.sequence == SEQ_LZMA_RUN) {
937
212k
    switch (s->lzma2.sequence) {
938
24.7k
    case SEQ_CONTROL:
939
      /*
940
       * LZMA2 control byte
941
       *
942
       * Exact values:
943
       *   0x00   End marker
944
       *   0x01   Dictionary reset followed by
945
       *          an uncompressed chunk
946
       *   0x02   Uncompressed chunk (no dictionary reset)
947
       *
948
       * Highest three bits (s->control & 0xE0):
949
       *   0xE0   Dictionary reset, new properties and state
950
       *          reset, followed by LZMA compressed chunk
951
       *   0xC0   New properties and state reset, followed
952
       *          by LZMA compressed chunk (no dictionary
953
       *          reset)
954
       *   0xA0   State reset using old properties,
955
       *          followed by LZMA compressed chunk (no
956
       *          dictionary reset)
957
       *   0x80   LZMA chunk (no dictionary or state reset)
958
       *
959
       * For LZMA compressed chunks, the lowest five bits
960
       * (s->control & 1F) are the highest bits of the
961
       * uncompressed size (bits 16-20).
962
       *
963
       * A new LZMA2 stream must begin with a dictionary
964
       * reset. The first LZMA chunk must set new
965
       * properties and reset the LZMA state.
966
       *
967
       * Values that don't match anything described above
968
       * are invalid and we return XZ_DATA_ERROR.
969
       */
970
24.7k
      tmp = b->in[b->in_pos++];
971
972
24.7k
      if (tmp == 0x00)
973
5.45k
        return XZ_STREAM_END;
974
975
19.3k
      if (tmp >= 0xE0 || tmp == 0x01) {
976
16.4k
        s->lzma2.need_props = true;
977
16.4k
        s->lzma2.need_dict_reset = false;
978
16.4k
        dict_reset(&s->dict, b);
979
16.4k
      } else if (s->lzma2.need_dict_reset) {
980
406
        return XZ_DATA_ERROR;
981
406
      }
982
983
18.9k
      if (tmp >= 0x80) {
984
15.6k
        s->lzma2.uncompressed = (tmp & 0x1F) << 16;
985
15.6k
        s->lzma2.sequence = SEQ_UNCOMPRESSED_1;
986
987
15.6k
        if (tmp >= 0xC0) {
988
          /*
989
           * When there are new properties,
990
           * state reset is done at
991
           * SEQ_PROPERTIES.
992
           */
993
14.6k
          s->lzma2.need_props = false;
994
14.6k
          s->lzma2.next_sequence
995
14.6k
              = SEQ_PROPERTIES;
996
997
14.6k
        } else if (s->lzma2.need_props) {
998
268
          return XZ_DATA_ERROR;
999
1000
707
        } else {
1001
707
          s->lzma2.next_sequence
1002
707
              = SEQ_LZMA_PREPARE;
1003
707
          if (tmp >= 0xA0)
1004
425
            lzma_reset(s);
1005
707
        }
1006
15.6k
      } else {
1007
3.31k
        if (tmp > 0x02)
1008
275
          return XZ_DATA_ERROR;
1009
1010
3.03k
        s->lzma2.sequence = SEQ_COMPRESSED_0;
1011
3.03k
        s->lzma2.next_sequence = SEQ_COPY;
1012
3.03k
      }
1013
1014
18.3k
      break;
1015
1016
18.3k
    case SEQ_UNCOMPRESSED_1:
1017
15.3k
      s->lzma2.uncompressed
1018
15.3k
          += (uint32_t)b->in[b->in_pos++] << 8;
1019
15.3k
      s->lzma2.sequence = SEQ_UNCOMPRESSED_2;
1020
15.3k
      break;
1021
1022
15.3k
    case SEQ_UNCOMPRESSED_2:
1023
15.3k
      s->lzma2.uncompressed
1024
15.3k
          += (uint32_t)b->in[b->in_pos++] + 1;
1025
15.3k
      s->lzma2.sequence = SEQ_COMPRESSED_0;
1026
15.3k
      break;
1027
1028
18.3k
    case SEQ_COMPRESSED_0:
1029
18.3k
      s->lzma2.compressed
1030
18.3k
          = (uint32_t)b->in[b->in_pos++] << 8;
1031
18.3k
      s->lzma2.sequence = SEQ_COMPRESSED_1;
1032
18.3k
      break;
1033
1034
18.3k
    case SEQ_COMPRESSED_1:
1035
18.3k
      s->lzma2.compressed
1036
18.3k
          += (uint32_t)b->in[b->in_pos++] + 1;
1037
18.3k
      s->lzma2.sequence = s->lzma2.next_sequence;
1038
18.3k
      break;
1039
1040
14.6k
    case SEQ_PROPERTIES:
1041
14.6k
      if (!lzma_props(s, b->in[b->in_pos++]))
1042
943
        return XZ_DATA_ERROR;
1043
1044
13.6k
      s->lzma2.sequence = SEQ_LZMA_PREPARE;
1045
1046
    /* Fall through */
1047
1048
14.3k
    case SEQ_LZMA_PREPARE:
1049
14.3k
      if (s->lzma2.compressed < RC_INIT_BYTES)
1050
267
        return XZ_DATA_ERROR;
1051
1052
14.0k
      if (!rc_read_init(&s->rc, b))
1053
40
        return XZ_OK;
1054
1055
14.0k
      s->lzma2.compressed -= RC_INIT_BYTES;
1056
14.0k
      s->lzma2.sequence = SEQ_LZMA_RUN;
1057
1058
    /* Fall through */
1059
1060
114k
    case SEQ_LZMA_RUN:
1061
      /*
1062
       * Set dictionary limit to indicate how much we want
1063
       * to be encoded at maximum. Decode new data into the
1064
       * dictionary. Flush the new data from dictionary to
1065
       * b->out. Check if we finished decoding this chunk.
1066
       * In case the dictionary got full but we didn't fill
1067
       * the output buffer yet, we may run this loop
1068
       * multiple times without changing s->lzma2.sequence.
1069
       */
1070
114k
      dict_limit(&s->dict, min_t(size_t,
1071
114k
          b->out_size - b->out_pos,
1072
114k
          s->lzma2.uncompressed));
1073
114k
      if (!lzma2_lzma(s, b))
1074
5.69k
        return XZ_DATA_ERROR;
1075
1076
109k
      s->lzma2.uncompressed -= dict_flush(&s->dict, b);
1077
1078
109k
      if (s->lzma2.uncompressed == 0) {
1079
6.57k
        if (s->lzma2.compressed > 0 || s->lzma.len > 0
1080
5.47k
            || !rc_is_finished(&s->rc))
1081
1.46k
          return XZ_DATA_ERROR;
1082
1083
5.11k
        rc_reset(&s->rc);
1084
5.11k
        s->lzma2.sequence = SEQ_CONTROL;
1085
1086
102k
      } else if (b->out_pos == b->out_size
1087
9.15k
          || (b->in_pos == b->in_size
1088
3.56k
            && s->temp.size
1089
95.9k
            < s->lzma2.compressed)) {
1090
95.9k
        return XZ_OK;
1091
95.9k
      }
1092
1093
11.8k
      break;
1094
1095
11.8k
    case SEQ_COPY:
1096
3.88k
      dict_uncompressed(&s->dict, b, &s->lzma2.compressed);
1097
3.88k
      if (s->lzma2.compressed > 0)
1098
1.29k
        return XZ_OK;
1099
1100
2.59k
      s->lzma2.sequence = SEQ_CONTROL;
1101
2.59k
      break;
1102
212k
    }
1103
212k
  }
1104
1105
1.13k
  return XZ_OK;
1106
113k
}
1107
1108
XZ_EXTERN struct xz_dec_lzma2 *xz_dec_lzma2_create(enum xz_mode mode,
1109
               uint32_t dict_max)
1110
4.09k
{
1111
4.09k
  struct xz_dec_lzma2 *s = MALLOC(sizeof(*s));
1112
4.09k
  if (s == NULL)
1113
0
    return NULL;
1114
1115
4.09k
  s->dict.mode = mode;
1116
4.09k
  s->dict.size_max = dict_max;
1117
1118
4.09k
  if (DEC_IS_PREALLOC(mode)) {
1119
1.60k
    s->dict.buf = MALLOC(dict_max);
1120
1.60k
    if (s->dict.buf == NULL) {
1121
1
      FREE(s);
1122
1
      return NULL;
1123
1
    }
1124
2.48k
  } else if (DEC_IS_DYNALLOC(mode)) {
1125
2.48k
    s->dict.buf = NULL;
1126
2.48k
    s->dict.allocated = 0;
1127
2.48k
  }
1128
1129
4.08k
  return s;
1130
4.09k
}
1131
1132
XZ_EXTERN enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s, uint8_t props)
1133
17.3k
{
1134
  /* This limits dictionary size to 3 GiB to keep parsing simpler. */
1135
17.3k
  if (props > 39)
1136
1
    return XZ_OPTIONS_ERROR;
1137
1138
17.3k
  s->dict.size = 2 + (props & 1);
1139
17.3k
  s->dict.size <<= (props >> 1) + 11;
1140
1141
17.3k
  if (DEC_IS_MULTI(s->dict.mode)) {
1142
17.3k
    if (s->dict.size > s->dict.size_max)
1143
215
      return XZ_MEMLIMIT_ERROR;
1144
1145
17.1k
    s->dict.end = s->dict.size;
1146
1147
17.1k
    if (DEC_IS_DYNALLOC(s->dict.mode)) {
1148
4.20k
      if (s->dict.allocated < s->dict.size) {
1149
4.20k
        FREE(s->dict.buf);
1150
4.20k
        s->dict.buf = MALLOC(s->dict.size);
1151
4.20k
        if (s->dict.buf == NULL) {
1152
2
          s->dict.allocated = 0;
1153
2
          return XZ_MEM_ERROR;
1154
2
        }
1155
4.20k
      }
1156
4.20k
    }
1157
17.1k
  }
1158
1159
17.1k
  s->lzma.len = 0;
1160
1161
17.1k
  s->lzma2.sequence = SEQ_CONTROL;
1162
17.1k
  s->lzma2.need_dict_reset = true;
1163
1164
17.1k
  s->temp.size = 0;
1165
1166
17.1k
  return XZ_OK;
1167
17.3k
}
1168
1169
XZ_EXTERN void xz_dec_lzma2_end(struct xz_dec_lzma2 *s)
1170
4.08k
{
1171
4.08k
  if (DEC_IS_MULTI(s->dict.mode))
1172
4.08k
    FREE(s->dict.buf);
1173
1174
4.08k
  FREE(s);
1175
4.08k
}