Coverage Report

Created: 2026-11-28 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/lib/xz/xz_dec_stream.c
Line
Count
Source
1
/*
2
 * .xz Stream decoder
3
 *
4
 * Author: Lasse Collin <lasse.collin@tukaani.org>
5
 *
6
 * This file has been put into the public domain.
7
 * You can do whatever you want with this file.
8
 */
9
10
#include "xz_private.h"
11
#include "xz_stream.h"
12
13
/* Hash used to validate the Index field */
14
struct xz_dec_hash {
15
  vli_type unpadded;
16
  vli_type uncompressed;
17
  uint32_t crc32;
18
};
19
20
struct xz_dec {
21
  /* Position in dec_main() */
22
  enum {
23
    SEQ_STREAM_HEADER,
24
    SEQ_BLOCK_START,
25
    SEQ_BLOCK_HEADER,
26
    SEQ_BLOCK_UNCOMPRESS,
27
    SEQ_BLOCK_PADDING,
28
    SEQ_BLOCK_CHECK,
29
    SEQ_INDEX,
30
    SEQ_INDEX_PADDING,
31
    SEQ_INDEX_CRC32,
32
    SEQ_STREAM_FOOTER
33
  } sequence;
34
35
  /* Position in variable-length integers and Check fields */
36
  uint32_t pos;
37
38
  /* Variable-length integer decoded by dec_vli() */
39
  vli_type vli;
40
41
  /* Saved in_pos and out_pos */
42
  size_t in_start;
43
  size_t out_start;
44
45
  /* CRC32 value in Block or Index */
46
  uint32_t crc32;
47
48
  /* Type of the integrity check calculated from uncompressed data */
49
  enum xz_check check_type;
50
51
  /* Operation mode */
52
  enum xz_mode mode;
53
54
  /*
55
   * True if the next call to xz_dec_run() is allowed to return
56
   * XZ_BUF_ERROR.
57
   */
58
  bool allow_buf_error;
59
60
  /* Information stored in Block Header */
61
  struct {
62
    /*
63
     * Value stored in the Compressed Size field, or
64
     * VLI_UNKNOWN if Compressed Size is not present.
65
     */
66
    vli_type compressed;
67
68
    /*
69
     * Value stored in the Uncompressed Size field, or
70
     * VLI_UNKNOWN if Uncompressed Size is not present.
71
     */
72
    vli_type uncompressed;
73
74
    /* Size of the Block Header field */
75
    uint32_t size;
76
  } block_header;
77
78
  /* Information collected when decoding Blocks */
79
  struct {
80
    /* Observed compressed size of the current Block */
81
    vli_type compressed;
82
83
    /* Observed uncompressed size of the current Block */
84
    vli_type uncompressed;
85
86
    /* Number of Blocks decoded so far */
87
    vli_type count;
88
89
    /*
90
     * Hash calculated from the Block sizes. This is used to
91
     * validate the Index field.
92
     */
93
    struct xz_dec_hash hash;
94
  } block;
95
96
  /* Variables needed when verifying the Index field */
97
  struct {
98
    /* Position in dec_index() */
99
    enum {
100
      SEQ_INDEX_COUNT,
101
      SEQ_INDEX_UNPADDED,
102
      SEQ_INDEX_UNCOMPRESSED
103
    } sequence;
104
105
    /* Size of the Index in bytes */
106
    vli_type size;
107
108
    /* Number of Records (matches block.count in valid files) */
109
    vli_type count;
110
111
    /*
112
     * Hash calculated from the Records (matches block.hash in
113
     * valid files).
114
     */
115
    struct xz_dec_hash hash;
116
  } index;
117
118
  /*
119
   * Temporary buffer needed to hold Stream Header, Block Header,
120
   * and Stream Footer. The Block Header is the biggest (1 KiB)
121
   * so we reserve space according to that. buf[] has to be aligned
122
   * to a multiple of four bytes; the size_t variables before it
123
   * should guarantee this.
124
   */
125
  struct {
126
    size_t pos;
127
    size_t size;
128
    uint8_t buf[1024];
129
  } temp;
130
131
  struct xz_dec_lzma2 *lzma2;
132
133
#ifdef XZ_DEC_BCJ
134
  struct xz_dec_bcj *bcj;
135
  bool bcj_active;
136
#endif
137
};
138
139
#ifdef XZ_DEC_ANY_CHECK
140
/* Sizes of the Check field with different Check IDs */
141
static const uint8_t check_sizes[16] = {
142
  0,
143
  4, 4, 4,
144
  8, 8, 8,
145
  16, 16, 16,
146
  32, 32, 32,
147
  64, 64, 64
148
};
149
#endif
150
151
/*
152
 * Fill s->temp by copying data starting from b->in[b->in_pos]. Caller
153
 * must have set s->temp.pos to indicate how much data we are supposed
154
 * to copy into s->temp.buf. Return true once s->temp.pos has reached
155
 * s->temp.size.
156
 */
157
static bool fill_temp(struct xz_dec *s, struct xz_buf *b)
158
51.8k
{
159
51.8k
  size_t copy_size = min_t(size_t,
160
51.8k
      b->in_size - b->in_pos, s->temp.size - s->temp.pos);
161
162
51.8k
  memcpy(s->temp.buf + s->temp.pos, b->in + b->in_pos, copy_size);
163
51.8k
  b->in_pos += copy_size;
164
51.8k
  s->temp.pos += copy_size;
165
166
51.8k
  if (s->temp.pos == s->temp.size) {
167
49.8k
    s->temp.pos = 0;
168
49.8k
    return true;
169
49.8k
  }
170
171
2.06k
  return false;
172
51.8k
}
173
174
/* Decode a variable-length integer (little-endian base-128 encoding) */
175
static enum xz_ret dec_vli(struct xz_dec *s, const uint8_t *in,
176
         size_t *in_pos, size_t in_size)
177
33.9k
{
178
33.9k
  uint8_t byte;
179
180
33.9k
  if (s->pos == 0)
181
33.9k
    s->vli = 0;
182
183
63.8k
  while (*in_pos < in_size) {
184
63.7k
    byte = in[*in_pos];
185
63.7k
    ++*in_pos;
186
187
63.7k
    s->vli |= (vli_type)(byte & 0x7F) << s->pos;
188
189
63.7k
    if ((byte & 0x80) == 0) {
190
      /* Don't allow non-minimal encodings. */
191
33.6k
      if (byte == 0 && s->pos != 0)
192
241
        return XZ_DATA_ERROR;
193
194
33.3k
      s->pos = 0;
195
33.3k
      return XZ_STREAM_END;
196
33.6k
    }
197
198
30.1k
    s->pos += 7;
199
30.1k
    if (s->pos == 7 * VLI_BYTES_MAX)
200
254
      return XZ_DATA_ERROR;
201
30.1k
  }
202
203
75
  return XZ_OK;
204
33.9k
}
205
206
/*
207
 * Decode the Compressed Data field from a Block. Update and validate
208
 * the observed compressed and uncompressed sizes of the Block so that
209
 * they don't exceed the values possibly stored in the Block Header
210
 * (validation assumes that no integer overflow occurs, since vli_type
211
 * is normally uint64_t). Update the CRC32 if presence of the CRC32
212
 * field was indicated in Stream Header.
213
 *
214
 * Once the decoding is finished, validate that the observed sizes match
215
 * the sizes possibly stored in the Block Header. Update the hash and
216
 * Block count, which are later used to validate the Index field.
217
 */
218
static enum xz_ret dec_block(struct xz_dec *s, struct xz_buf *b)
219
111k
{
220
111k
  enum xz_ret ret;
221
222
111k
  s->in_start = b->in_pos;
223
111k
  s->out_start = b->out_pos;
224
225
111k
#ifdef XZ_DEC_BCJ
226
111k
  if (s->bcj_active)
227
0
    ret = xz_dec_bcj_run(s->bcj, s->lzma2, b);
228
111k
  else
229
111k
#endif
230
111k
    ret = xz_dec_lzma2_run(s->lzma2, b);
231
232
111k
  s->block.compressed += b->in_pos - s->in_start;
233
111k
  s->block.uncompressed += b->out_pos - s->out_start;
234
235
  /*
236
   * There is no need to separately check for VLI_UNKNOWN, since
237
   * the observed sizes are always smaller than VLI_UNKNOWN.
238
   */
239
111k
  if (s->block.compressed > s->block_header.compressed
240
110k
      || s->block.uncompressed
241
110k
        > s->block_header.uncompressed)
242
1.58k
    return XZ_DATA_ERROR;
243
244
110k
  if (s->check_type == XZ_CHECK_CRC32)
245
91.9k
    s->crc32 = xz_crc32(b->out + s->out_start,
246
91.9k
        b->out_pos - s->out_start, s->crc32);
247
248
110k
  if (ret == XZ_STREAM_END) {
249
5.48k
    if (s->block_header.compressed != VLI_UNKNOWN
250
3.03k
        && s->block_header.compressed
251
3.03k
          != s->block.compressed)
252
232
      return XZ_DATA_ERROR;
253
254
5.25k
    if (s->block_header.uncompressed != VLI_UNKNOWN
255
2.79k
        && s->block_header.uncompressed
256
2.79k
          != s->block.uncompressed)
257
0
      return XZ_DATA_ERROR;
258
259
5.25k
    s->block.hash.unpadded += s->block_header.size
260
5.25k
        + s->block.compressed;
261
262
#ifdef XZ_DEC_ANY_CHECK
263
    s->block.hash.unpadded += check_sizes[s->check_type];
264
#else
265
5.25k
    if (s->check_type == XZ_CHECK_CRC32)
266
3.43k
      s->block.hash.unpadded += 4;
267
5.25k
#endif
268
269
5.25k
    s->block.hash.uncompressed += s->block.uncompressed;
270
5.25k
    s->block.hash.crc32 = xz_crc32(
271
5.25k
        (const uint8_t *)&s->block.hash,
272
5.25k
        sizeof(s->block.hash), s->block.hash.crc32);
273
274
5.25k
    ++s->block.count;
275
5.25k
  }
276
277
110k
  return ret;
278
110k
}
279
280
/* Update the Index size and the CRC32 value. */
281
static void index_update(struct xz_dec *s, const struct xz_buf *b)
282
2.34k
{
283
2.34k
  size_t in_used = b->in_pos - s->in_start;
284
2.34k
  s->index.size += in_used;
285
2.34k
  s->crc32 = xz_crc32(b->in + s->in_start, in_used, s->crc32);
286
2.34k
}
287
288
/*
289
 * Decode the Number of Records, Unpadded Size, and Uncompressed Size
290
 * fields from the Index field. That is, Index Padding and CRC32 are not
291
 * decoded by this function.
292
 *
293
 * This can return XZ_OK (more input needed), XZ_STREAM_END (everything
294
 * successfully decoded), or XZ_DATA_ERROR (input is corrupt).
295
 */
296
static enum xz_ret dec_index(struct xz_dec *s, struct xz_buf *b)
297
3.21k
{
298
3.21k
  enum xz_ret ret;
299
300
7.63k
  do {
301
7.63k
    ret = dec_vli(s, b->in, &b->in_pos, b->in_size);
302
7.63k
    if (ret != XZ_STREAM_END) {
303
570
      index_update(s, b);
304
570
      return ret;
305
570
    }
306
307
7.06k
    switch (s->index.sequence) {
308
2.92k
    case SEQ_INDEX_COUNT:
309
2.92k
      s->index.count = s->vli;
310
311
      /*
312
       * Validate that the Number of Records field
313
       * indicates the same number of Records as
314
       * there were Blocks in the Stream.
315
       */
316
2.92k
      if (s->index.count != s->block.count)
317
623
        return XZ_DATA_ERROR;
318
319
2.30k
      s->index.sequence = SEQ_INDEX_UNPADDED;
320
2.30k
      break;
321
322
2.19k
    case SEQ_INDEX_UNPADDED:
323
2.19k
      s->index.hash.unpadded += s->vli;
324
2.19k
      s->index.sequence = SEQ_INDEX_UNCOMPRESSED;
325
2.19k
      break;
326
327
1.94k
    case SEQ_INDEX_UNCOMPRESSED:
328
1.94k
      s->index.hash.uncompressed += s->vli;
329
1.94k
      s->index.hash.crc32 = xz_crc32(
330
1.94k
          (const uint8_t *)&s->index.hash,
331
1.94k
          sizeof(s->index.hash),
332
1.94k
          s->index.hash.crc32);
333
1.94k
      --s->index.count;
334
1.94k
      s->index.sequence = SEQ_INDEX_UNPADDED;
335
1.94k
      break;
336
7.06k
    }
337
7.06k
  } while (s->index.count > 0);
338
339
2.02k
  return XZ_STREAM_END;
340
3.21k
}
341
342
/*
343
 * Validate that the next four input bytes match the value of s->crc32.
344
 * s->pos must be zero when starting to validate the first byte.
345
 */
346
static enum xz_ret crc32_validate(struct xz_dec *s, struct xz_buf *b)
347
4.70k
{
348
17.0k
  do {
349
17.0k
    if (b->in_pos == b->in_size)
350
48
      return XZ_OK;
351
352
17.0k
    if (((s->crc32 >> s->pos) & 0xFF) != b->in[b->in_pos++])
353
553
      return XZ_DATA_ERROR;
354
355
16.4k
    s->pos += 8;
356
357
16.4k
  } while (s->pos < 32);
358
359
4.10k
  s->crc32 = 0;
360
4.10k
  s->pos = 0;
361
362
4.10k
  return XZ_STREAM_END;
363
4.70k
}
364
365
#ifdef XZ_DEC_ANY_CHECK
366
/*
367
 * Skip over the Check field when the Check ID is not supported.
368
 * Returns true once the whole Check field has been skipped over.
369
 */
370
static bool check_skip(struct xz_dec *s, struct xz_buf *b)
371
{
372
  while (s->pos < check_sizes[s->check_type]) {
373
    if (b->in_pos == b->in_size)
374
      return false;
375
376
    ++b->in_pos;
377
    ++s->pos;
378
  }
379
380
  s->pos = 0;
381
382
  return true;
383
}
384
#endif
385
386
/* Decode the Stream Header field (the first 12 bytes of the .xz Stream). */
387
static enum xz_ret dec_stream_header(struct xz_dec *s)
388
28.6k
{
389
28.6k
  if (!memeq(s->temp.buf, HEADER_MAGIC, HEADER_MAGIC_SIZE))
390
6.86k
    return XZ_FORMAT_ERROR;
391
392
21.7k
  if (xz_crc32(s->temp.buf + HEADER_MAGIC_SIZE, 2, 0)
393
21.7k
      != get_le32(s->temp.buf + HEADER_MAGIC_SIZE + 2))
394
2.73k
    return XZ_DATA_ERROR;
395
396
19.0k
  if (s->temp.buf[HEADER_MAGIC_SIZE] != 0)
397
13
    return XZ_OPTIONS_ERROR;
398
399
  /*
400
   * Of integrity checks, we support only none (Check ID = 0) and
401
   * CRC32 (Check ID = 1). However, if XZ_DEC_ANY_CHECK is defined,
402
   * we will accept other check types too, but then the check won't
403
   * be verified and a warning (XZ_UNSUPPORTED_CHECK) will be given.
404
   */
405
19.0k
  s->check_type = s->temp.buf[HEADER_MAGIC_SIZE + 1];
406
407
#ifdef XZ_DEC_ANY_CHECK
408
  if (s->check_type > XZ_CHECK_MAX)
409
    return XZ_OPTIONS_ERROR;
410
411
  if (s->check_type > XZ_CHECK_CRC32)
412
    return XZ_UNSUPPORTED_CHECK;
413
#else
414
19.0k
  if (s->check_type > XZ_CHECK_CRC32)
415
10
    return XZ_OPTIONS_ERROR;
416
19.0k
#endif
417
418
19.0k
  return XZ_OK;
419
19.0k
}
420
421
/* Decode the Stream Footer field (the last 12 bytes of the .xz Stream) */
422
static enum xz_ret dec_stream_footer(struct xz_dec *s)
423
1.22k
{
424
1.22k
  if (!memeq(s->temp.buf + 10, FOOTER_MAGIC, FOOTER_MAGIC_SIZE))
425
142
    return XZ_DATA_ERROR;
426
427
1.08k
  if (xz_crc32(s->temp.buf + 4, 6, 0) != get_le32(s->temp.buf))
428
162
    return XZ_DATA_ERROR;
429
430
  /*
431
   * Validate Backward Size. Note that we never added the size of the
432
   * Index CRC32 field to s->index.size, thus we use s->index.size / 4
433
   * instead of s->index.size / 4 - 1.
434
   */
435
919
  if ((s->index.size >> 2) != get_le32(s->temp.buf + 4))
436
1
    return XZ_DATA_ERROR;
437
438
918
  if (s->temp.buf[8] != 0 || s->temp.buf[9] != s->check_type)
439
0
    return XZ_DATA_ERROR;
440
441
  /*
442
   * Use XZ_STREAM_END instead of XZ_OK to be more convenient
443
   * for the caller.
444
   */
445
918
  return XZ_STREAM_END;
446
918
}
447
448
/* Decode the Block Header and initialize the filter chain. */
449
static enum xz_ret dec_block_header(struct xz_dec *s)
450
19.9k
{
451
19.9k
  enum xz_ret ret;
452
453
  /*
454
   * Validate the CRC32. We know that the temp buffer is at least
455
   * eight bytes so this is safe.
456
   */
457
19.9k
  s->temp.size -= 4;
458
19.9k
  if (xz_crc32(s->temp.buf, s->temp.size, 0)
459
19.9k
      != get_le32(s->temp.buf + s->temp.size))
460
2.59k
    return XZ_DATA_ERROR;
461
462
17.3k
  s->temp.pos = 2;
463
464
  /*
465
   * Catch unsupported Block Flags. We support only one or two filters
466
   * in the chain, so we catch that with the same test.
467
   */
468
17.3k
#ifdef XZ_DEC_BCJ
469
17.3k
  if (s->temp.buf[1] & 0x3E)
470
#else
471
  if (s->temp.buf[1] & 0x3F)
472
#endif
473
4
    return XZ_OPTIONS_ERROR;
474
475
  /* Compressed Size */
476
17.3k
  if (s->temp.buf[1] & 0x40) {
477
13.1k
    if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size)
478
13.1k
          != XZ_STREAM_END)
479
0
      return XZ_DATA_ERROR;
480
481
13.1k
    s->block_header.compressed = s->vli;
482
13.1k
  } else {
483
4.20k
    s->block_header.compressed = VLI_UNKNOWN;
484
4.20k
  }
485
486
  /* Uncompressed Size */
487
17.3k
  if (s->temp.buf[1] & 0x80) {
488
13.1k
    if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size)
489
13.1k
        != XZ_STREAM_END)
490
0
      return XZ_DATA_ERROR;
491
492
13.1k
    s->block_header.uncompressed = s->vli;
493
13.1k
  } else {
494
4.20k
    s->block_header.uncompressed = VLI_UNKNOWN;
495
4.20k
  }
496
497
17.3k
#ifdef XZ_DEC_BCJ
498
  /* If there are two filters, the first one must be a BCJ filter. */
499
17.3k
  s->bcj_active = s->temp.buf[1] & 0x01;
500
17.3k
  if (s->bcj_active) {
501
4
    if (s->temp.size - s->temp.pos < 2)
502
0
      return XZ_OPTIONS_ERROR;
503
504
4
    ret = xz_dec_bcj_reset(s->bcj, s->temp.buf[s->temp.pos++]);
505
4
    if (ret != XZ_OK)
506
3
      return ret;
507
508
    /*
509
     * We don't support custom start offset,
510
     * so Size of Properties must be zero.
511
     */
512
1
    if (s->temp.buf[s->temp.pos++] != 0x00)
513
1
      return XZ_OPTIONS_ERROR;
514
1
  }
515
17.3k
#endif
516
517
  /* Valid Filter Flags always take at least two bytes. */
518
17.3k
  if (s->temp.size - s->temp.pos < 2)
519
1
    return XZ_DATA_ERROR;
520
521
  /* Filter ID = LZMA2 */
522
17.3k
  if (s->temp.buf[s->temp.pos++] != 0x21)
523
6
    return XZ_OPTIONS_ERROR;
524
525
  /* Size of Properties = 1-byte Filter Properties */
526
17.3k
  if (s->temp.buf[s->temp.pos++] != 0x01)
527
1
    return XZ_OPTIONS_ERROR;
528
529
  /* Filter Properties contains LZMA2 dictionary size. */
530
17.3k
  if (s->temp.size - s->temp.pos < 1)
531
1
    return XZ_DATA_ERROR;
532
533
17.3k
  ret = xz_dec_lzma2_reset(s->lzma2, s->temp.buf[s->temp.pos++]);
534
17.3k
  if (ret != XZ_OK)
535
215
    return ret;
536
537
  /* The rest must be Header Padding. */
538
72.4k
  while (s->temp.pos < s->temp.size)
539
55.3k
    if (s->temp.buf[s->temp.pos++] != 0x00)
540
11
      return XZ_OPTIONS_ERROR;
541
542
17.1k
  s->temp.pos = 0;
543
17.1k
  s->block.compressed = 0;
544
17.1k
  s->block.uncompressed = 0;
545
546
17.1k
  return XZ_OK;
547
17.1k
}
548
549
static enum xz_ret dec_main(struct xz_dec *s, struct xz_buf *b)
550
125k
{
551
125k
  enum xz_ret ret;
552
553
  /*
554
   * Store the start position for the case when we are in the middle
555
   * of the Index field.
556
   */
557
125k
  s->in_start = b->in_pos;
558
559
132k
  while (true) {
560
132k
    switch (s->sequence) {
561
29.0k
    case SEQ_STREAM_HEADER:
562
      /*
563
       * Stream Header is copied to s->temp, and then
564
       * decoded from there. This way if the caller
565
       * gives us only little input at a time, we can
566
       * still keep the Stream Header decoding code
567
       * simple. Similar approach is used in many places
568
       * in this file.
569
       */
570
29.0k
      if (!fill_temp(s, b))
571
379
        return XZ_OK;
572
573
      /*
574
       * If dec_stream_header() returns
575
       * XZ_UNSUPPORTED_CHECK, it is still possible
576
       * to continue decoding if working in multi-call
577
       * mode. Thus, update s->sequence before calling
578
       * dec_stream_header().
579
       */
580
28.6k
      s->sequence = SEQ_BLOCK_START;
581
582
28.6k
      ret = dec_stream_header(s);
583
28.6k
      if (ret != XZ_OK)
584
9.62k
        return ret;
585
586
23.7k
    case SEQ_BLOCK_START:
587
      /* We need one byte of input to continue. */
588
23.7k
      if (b->in_pos == b->in_size)
589
60
        return XZ_OK;
590
591
      /* See if this is the beginning of the Index field. */
592
23.6k
      if (b->in[b->in_pos] == 0) {
593
3.16k
        s->in_start = b->in_pos++;
594
3.16k
        s->sequence = SEQ_INDEX;
595
3.16k
        break;
596
3.16k
      }
597
598
      /*
599
       * Calculate the size of the Block Header and
600
       * prepare to decode it.
601
       */
602
20.5k
      s->block_header.size
603
20.5k
        = ((uint32_t)b->in[b->in_pos] + 1) * 4;
604
605
20.5k
      s->temp.size = s->block_header.size;
606
20.5k
      s->temp.pos = 0;
607
20.5k
      s->sequence = SEQ_BLOCK_HEADER;
608
609
21.6k
    case SEQ_BLOCK_HEADER:
610
21.6k
      if (!fill_temp(s, b))
611
1.66k
        return XZ_OK;
612
613
19.9k
      ret = dec_block_header(s);
614
19.9k
      if (ret != XZ_OK)
615
2.83k
        return ret;
616
617
17.1k
      s->sequence = SEQ_BLOCK_UNCOMPRESS;
618
619
111k
    case SEQ_BLOCK_UNCOMPRESS:
620
111k
      ret = dec_block(s, b);
621
111k
      if (ret != XZ_STREAM_END)
622
106k
        return ret;
623
624
5.25k
      s->sequence = SEQ_BLOCK_PADDING;
625
626
5.29k
    case SEQ_BLOCK_PADDING:
627
      /*
628
       * Size of Compressed Data + Block Padding
629
       * must be a multiple of four. We don't need
630
       * s->block.compressed for anything else
631
       * anymore, so we use it here to test the size
632
       * of the Block Padding field.
633
       */
634
12.8k
      while (s->block.compressed & 3) {
635
7.90k
        if (b->in_pos == b->in_size)
636
60
          return XZ_OK;
637
638
7.84k
        if (b->in[b->in_pos++] != 0)
639
259
          return XZ_DATA_ERROR;
640
641
7.59k
        ++s->block.compressed;
642
7.59k
      }
643
644
4.97k
      s->sequence = SEQ_BLOCK_CHECK;
645
646
4.99k
    case SEQ_BLOCK_CHECK:
647
4.99k
      if (s->check_type == XZ_CHECK_CRC32) {
648
3.20k
        ret = crc32_validate(s, b);
649
3.20k
        if (ret != XZ_STREAM_END)
650
332
          return ret;
651
3.20k
      }
652
#ifdef XZ_DEC_ANY_CHECK
653
      else if (!check_skip(s, b)) {
654
        return XZ_OK;
655
      }
656
#endif
657
658
4.66k
      s->sequence = SEQ_BLOCK_START;
659
4.66k
      break;
660
661
3.21k
    case SEQ_INDEX:
662
3.21k
      ret = dec_index(s, b);
663
3.21k
      if (ret != XZ_STREAM_END)
664
1.19k
        return ret;
665
666
2.02k
      s->sequence = SEQ_INDEX_PADDING;
667
668
2.03k
    case SEQ_INDEX_PADDING:
669
5.33k
      while ((s->index.size + (b->in_pos - s->in_start))
670
5.33k
          & 3) {
671
3.57k
        if (b->in_pos == b->in_size) {
672
18
          index_update(s, b);
673
18
          return XZ_OK;
674
18
        }
675
676
3.55k
        if (b->in[b->in_pos++] != 0)
677
255
          return XZ_DATA_ERROR;
678
3.55k
      }
679
680
      /* Finish the CRC32 value and Index size. */
681
1.76k
      index_update(s, b);
682
683
      /* Compare the hashes to validate the Index field. */
684
1.76k
      if (!memeq(&s->block.hash, &s->index.hash,
685
1.76k
          sizeof(s->block.hash)))
686
270
        return XZ_DATA_ERROR;
687
688
1.49k
      s->sequence = SEQ_INDEX_CRC32;
689
690
1.50k
    case SEQ_INDEX_CRC32:
691
1.50k
      ret = crc32_validate(s, b);
692
1.50k
      if (ret != XZ_STREAM_END)
693
269
        return ret;
694
695
1.23k
      s->temp.size = STREAM_HEADER_SIZE;
696
1.23k
      s->sequence = SEQ_STREAM_FOOTER;
697
698
1.24k
    case SEQ_STREAM_FOOTER:
699
1.24k
      if (!fill_temp(s, b))
700
24
        return XZ_OK;
701
702
1.22k
      return dec_stream_footer(s);
703
132k
    }
704
132k
  }
705
706
  /* Never reached */
707
125k
}
708
709
/*
710
 * xz_dec_run() is a wrapper for dec_main() to handle some special cases in
711
 * multi-call and single-call decoding.
712
 *
713
 * In multi-call mode, we must return XZ_BUF_ERROR when it seems clear that we
714
 * are not going to make any progress anymore. This is to prevent the caller
715
 * from calling us infinitely when the input file is truncated or otherwise
716
 * corrupt. Since zlib-style API allows that the caller fills the input buffer
717
 * only when the decoder doesn't produce any new output, we have to be careful
718
 * to avoid returning XZ_BUF_ERROR too easily: XZ_BUF_ERROR is returned only
719
 * after the second consecutive call to xz_dec_run() that makes no progress.
720
 *
721
 * In single-call mode, if we couldn't decode everything and no error
722
 * occurred, either the input is truncated or the output buffer is too small.
723
 * Since we know that the last input byte never produces any output, we know
724
 * that if all the input was consumed and decoding wasn't finished, the file
725
 * must be corrupt. Otherwise the output buffer has to be too small or the
726
 * file is corrupt in a way that decoding it produces too big output.
727
 *
728
 * If single-call decoding fails, we reset b->in_pos and b->out_pos back to
729
 * their original values. This is because with some filter chains there won't
730
 * be any valid uncompressed data in the output buffer unless the decoding
731
 * actually succeeds (that's the price to pay of using the output buffer as
732
 * the workspace).
733
 */
734
XZ_EXTERN enum xz_ret xz_dec_run(struct xz_dec *s, struct xz_buf *b)
735
125k
{
736
125k
  size_t in_start;
737
125k
  size_t out_start;
738
125k
  enum xz_ret ret;
739
740
125k
  if (DEC_IS_SINGLE(s->mode))
741
0
    xz_dec_reset(s);
742
743
125k
  in_start = b->in_pos;
744
125k
  out_start = b->out_pos;
745
125k
  ret = dec_main(s, b);
746
747
125k
  if (DEC_IS_SINGLE(s->mode)) {
748
0
    if (ret == XZ_OK)
749
0
      ret = b->in_pos == b->in_size
750
0
          ? XZ_DATA_ERROR : XZ_BUF_ERROR;
751
752
0
    if (ret != XZ_STREAM_END) {
753
0
      b->in_pos = in_start;
754
0
      b->out_pos = out_start;
755
0
    }
756
757
125k
  } else if (ret == XZ_OK && in_start == b->in_pos
758
15.2k
      && out_start == b->out_pos) {
759
3.97k
    if (s->allow_buf_error)
760
1.98k
      ret = XZ_BUF_ERROR;
761
762
3.97k
    s->allow_buf_error = true;
763
121k
  } else {
764
121k
    s->allow_buf_error = false;
765
121k
  }
766
767
125k
  return ret;
768
125k
}
769
770
XZ_EXTERN struct xz_dec *xz_dec_init(enum xz_mode mode, uint32_t dict_max)
771
4.09k
{
772
4.09k
  struct xz_dec *s = MALLOC(sizeof(*s));
773
4.09k
  if (s == NULL)
774
0
    return NULL;
775
776
4.09k
  s->mode = mode;
777
778
4.09k
#ifdef XZ_DEC_BCJ
779
4.09k
  s->bcj = xz_dec_bcj_create(DEC_IS_SINGLE(mode));
780
4.09k
  if (s->bcj == NULL)
781
0
    goto error_bcj;
782
4.09k
#endif
783
784
4.09k
  s->lzma2 = xz_dec_lzma2_create(mode, dict_max);
785
4.09k
  if (s->lzma2 == NULL)
786
1
    goto error_lzma2;
787
788
4.09k
  xz_dec_reset(s);
789
4.09k
  return s;
790
791
1
error_lzma2:
792
1
#ifdef XZ_DEC_BCJ
793
1
  xz_dec_bcj_end(s->bcj);
794
1
error_bcj:
795
1
#endif
796
1
  FREE(s);
797
1
  return NULL;
798
1
}
799
800
XZ_EXTERN void xz_dec_reset(struct xz_dec *s)
801
30.4k
{
802
30.4k
  s->sequence = SEQ_STREAM_HEADER;
803
30.4k
  s->allow_buf_error = false;
804
30.4k
  s->pos = 0;
805
30.4k
  s->crc32 = 0;
806
30.4k
  memzero(&s->block, sizeof(s->block));
807
30.4k
  memzero(&s->index, sizeof(s->index));
808
30.4k
  s->temp.pos = 0;
809
30.4k
  s->temp.size = STREAM_HEADER_SIZE;
810
30.4k
}
811
812
XZ_EXTERN void xz_dec_end(struct xz_dec *s)
813
4.09k
{
814
4.09k
  if (s != NULL) {
815
4.09k
    xz_dec_lzma2_end(s->lzma2);
816
4.09k
#ifdef XZ_DEC_BCJ
817
4.09k
    xz_dec_bcj_end(s->bcj);
818
4.09k
#endif
819
4.09k
    FREE(s);
820
4.09k
  }
821
4.09k
}