Coverage Report

Created: 2026-06-30 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xz/src/liblzma/common/stream_decoder.c
Line
Count
Source
1
// SPDX-License-Identifier: 0BSD
2
3
///////////////////////////////////////////////////////////////////////////////
4
//
5
/// \file       stream_decoder.c
6
/// \brief      Decodes .xz Streams
7
//
8
//  Author:     Lasse Collin
9
//
10
///////////////////////////////////////////////////////////////////////////////
11
12
#include "stream_decoder.h"
13
#include "block_decoder.h"
14
#include "index.h"
15
16
17
typedef struct {
18
  enum {
19
    SEQ_STREAM_HEADER,
20
    SEQ_BLOCK_HEADER,
21
    SEQ_BLOCK_INIT,
22
    SEQ_BLOCK_RUN,
23
    SEQ_INDEX,
24
    SEQ_STREAM_FOOTER,
25
    SEQ_STREAM_PADDING,
26
  } sequence;
27
28
  /// Block decoder
29
  lzma_next_coder block_decoder;
30
31
  /// Block options decoded by the Block Header decoder and used by
32
  /// the Block decoder.
33
  lzma_block block_options;
34
35
  /// Stream Flags from Stream Header
36
  lzma_stream_flags stream_flags;
37
38
  /// Index is hashed so that it can be compared to the sizes of Blocks
39
  /// with O(1) memory usage.
40
  lzma_index_hash *index_hash;
41
42
  /// Memory usage limit
43
  uint64_t memlimit;
44
45
  /// Amount of memory actually needed (only an estimate)
46
  uint64_t memusage;
47
48
  /// If true, LZMA_NO_CHECK is returned if the Stream has
49
  /// no integrity check.
50
  bool tell_no_check;
51
52
  /// If true, LZMA_UNSUPPORTED_CHECK is returned if the Stream has
53
  /// an integrity check that isn't supported by this liblzma build.
54
  bool tell_unsupported_check;
55
56
  /// If true, LZMA_GET_CHECK is returned after decoding Stream Header.
57
  bool tell_any_check;
58
59
  /// If true, we will tell the Block decoder to skip calculating
60
  /// and verifying the integrity check.
61
  bool ignore_check;
62
63
  /// If true, we will decode concatenated Streams that possibly have
64
  /// Stream Padding between or after them. LZMA_STREAM_END is returned
65
  /// once the application isn't giving us any new input (LZMA_FINISH),
66
  /// and we aren't in the middle of a Stream, and possible
67
  /// Stream Padding is a multiple of four bytes.
68
  bool concatenated;
69
70
  /// When decoding concatenated Streams, this is true as long as we
71
  /// are decoding the first Stream. This is needed to avoid misleading
72
  /// LZMA_FORMAT_ERROR in case the later Streams don't have valid magic
73
  /// bytes.
74
  bool first_stream;
75
76
  /// Write position in buffer[] and position in Stream Padding
77
  size_t pos;
78
79
  /// Buffer to hold Stream Header, Block Header, and Stream Footer.
80
  /// Block Header has biggest maximum size.
81
  uint8_t buffer[LZMA_BLOCK_HEADER_SIZE_MAX];
82
} lzma_stream_coder;
83
84
85
static lzma_ret
86
stream_decoder_reset(lzma_stream_coder *coder, const lzma_allocator *allocator)
87
4.41k
{
88
  // Initialize the Index hash used to verify the Index.
89
4.41k
  coder->index_hash = lzma_index_hash_init(coder->index_hash, allocator);
90
4.41k
  if (coder->index_hash == NULL)
91
0
    return LZMA_MEM_ERROR;
92
93
  // Reset the rest of the variables.
94
4.41k
  coder->sequence = SEQ_STREAM_HEADER;
95
4.41k
  coder->pos = 0;
96
97
4.41k
  return LZMA_OK;
98
4.41k
}
99
100
101
static lzma_ret
102
stream_decode(void *coder_ptr, const lzma_allocator *allocator,
103
    const uint8_t *restrict in, size_t *restrict in_pos,
104
    size_t in_size, uint8_t *restrict out,
105
    size_t *restrict out_pos, size_t out_size, lzma_action action)
106
104k
{
107
104k
  lzma_stream_coder *coder = coder_ptr;
108
109
  // When decoding the actual Block, it may be able to produce more
110
  // output even if we don't give it any new input.
111
104k
  while (true)
112
106k
  switch (coder->sequence) {
113
4.57k
  case SEQ_STREAM_HEADER: {
114
    // Copy the Stream Header to the internal buffer.
115
4.57k
    lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
116
4.57k
        LZMA_STREAM_HEADER_SIZE);
117
118
    // Return if we didn't get the whole Stream Header yet.
119
4.57k
    if (coder->pos < LZMA_STREAM_HEADER_SIZE)
120
248
      return LZMA_OK;
121
122
4.32k
    coder->pos = 0;
123
124
    // Decode the Stream Header.
125
4.32k
    const lzma_ret ret = lzma_stream_header_decode(
126
4.32k
        &coder->stream_flags, coder->buffer);
127
4.32k
    if (ret != LZMA_OK)
128
178
      return ret == LZMA_FORMAT_ERROR && !coder->first_stream
129
178
          ? LZMA_DATA_ERROR : ret;
130
131
    // If we are decoding concatenated Streams, and the later
132
    // Streams have invalid Header Magic Bytes, we give
133
    // LZMA_DATA_ERROR instead of LZMA_FORMAT_ERROR.
134
4.15k
    coder->first_stream = false;
135
136
    // Copy the type of the Check so that Block Header and Block
137
    // decoders see it.
138
4.15k
    coder->block_options.check = coder->stream_flags.check;
139
140
    // Even if we return LZMA_*_CHECK below, we want
141
    // to continue from Block Header decoding.
142
4.15k
    coder->sequence = SEQ_BLOCK_HEADER;
143
144
    // Detect if there's no integrity check or if it is
145
    // unsupported if those were requested by the application.
146
4.15k
    if (coder->tell_no_check && coder->stream_flags.check
147
0
        == LZMA_CHECK_NONE)
148
0
      return LZMA_NO_CHECK;
149
150
4.15k
    if (coder->tell_unsupported_check
151
0
        && !lzma_check_is_supported(
152
0
          coder->stream_flags.check))
153
0
      return LZMA_UNSUPPORTED_CHECK;
154
155
4.15k
    if (coder->tell_any_check)
156
0
      return LZMA_GET_CHECK;
157
158
4.15k
    FALLTHROUGH;
159
4.15k
  }
160
161
5.86k
  case SEQ_BLOCK_HEADER: {
162
5.86k
    if (*in_pos >= in_size)
163
67
      return LZMA_OK;
164
165
5.79k
    if (coder->pos == 0) {
166
      // Detect if it's Index.
167
5.75k
      if (in[*in_pos] == INDEX_INDICATOR) {
168
834
        coder->sequence = SEQ_INDEX;
169
834
        break;
170
834
      }
171
172
      // Calculate the size of the Block Header. Note that
173
      // Block Header decoder wants to see this byte too
174
      // so don't advance *in_pos.
175
4.91k
      coder->block_options.header_size
176
4.91k
          = lzma_block_header_size_decode(
177
4.91k
            in[*in_pos]);
178
4.91k
    }
179
180
    // Copy the Block Header to the internal buffer.
181
4.96k
    lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
182
4.96k
        coder->block_options.header_size);
183
184
    // Return if we didn't get the whole Block Header yet.
185
4.96k
    if (coder->pos < coder->block_options.header_size)
186
122
      return LZMA_OK;
187
188
4.84k
    coder->pos = 0;
189
4.84k
    coder->sequence = SEQ_BLOCK_INIT;
190
4.84k
    FALLTHROUGH;
191
4.84k
  }
192
193
4.84k
  case SEQ_BLOCK_INIT: {
194
    // Checking memusage and doing the initialization needs
195
    // its own sequence point because we need to be able to
196
    // retry if we return LZMA_MEMLIMIT_ERROR.
197
198
    // Version 1 is needed to support the .ignore_check option.
199
4.84k
    coder->block_options.version = 1;
200
201
    // Set up a buffer to hold the filter chain. Block Header
202
    // decoder will initialize all members of this array so
203
    // we don't need to do it here.
204
4.84k
    lzma_filter filters[LZMA_FILTERS_MAX + 1];
205
4.84k
    coder->block_options.filters = filters;
206
207
    // Decode the Block Header.
208
4.84k
    return_if_error(lzma_block_header_decode(&coder->block_options,
209
4.84k
        allocator, coder->buffer));
210
211
    // If LZMA_IGNORE_CHECK was used, this flag needs to be set.
212
    // It has to be set after lzma_block_header_decode() because
213
    // it always resets this to false.
214
4.40k
    coder->block_options.ignore_check = coder->ignore_check;
215
216
    // Check the memory usage limit.
217
4.40k
    const uint64_t memusage = lzma_raw_decoder_memusage(filters);
218
4.40k
    lzma_ret ret;
219
220
4.40k
    if (memusage == UINT64_MAX) {
221
      // One or more unknown Filter IDs.
222
29
      ret = LZMA_OPTIONS_ERROR;
223
4.38k
    } else {
224
      // Now we can set coder->memusage since we know that
225
      // the filter chain is valid. We don't want
226
      // lzma_memusage() to return UINT64_MAX in case of
227
      // invalid filter chain.
228
4.38k
      coder->memusage = memusage;
229
230
4.38k
      if (memusage > coder->memlimit) {
231
        // The chain would need too much memory.
232
0
        ret = LZMA_MEMLIMIT_ERROR;
233
4.38k
      } else {
234
        // Memory usage is OK.
235
        // Initialize the Block decoder.
236
4.38k
        ret = lzma_block_decoder_init(
237
4.38k
            &coder->block_decoder,
238
4.38k
            allocator,
239
4.38k
            &coder->block_options);
240
4.38k
      }
241
4.38k
    }
242
243
    // Free the allocated filter options since they are needed
244
    // only to initialize the Block decoder.
245
4.40k
    lzma_filters_free(filters, allocator);
246
4.40k
    coder->block_options.filters = NULL;
247
248
    // Check if memory usage calculation and Block decoder
249
    // initialization succeeded.
250
4.40k
    if (ret != LZMA_OK)
251
39
      return ret;
252
253
4.37k
    coder->sequence = SEQ_BLOCK_RUN;
254
4.37k
    FALLTHROUGH;
255
4.37k
  }
256
257
103k
  case SEQ_BLOCK_RUN: {
258
103k
    const lzma_ret ret = coder->block_decoder.code(
259
103k
        coder->block_decoder.coder, allocator,
260
103k
        in, in_pos, in_size, out, out_pos, out_size,
261
103k
        action);
262
263
103k
    if (ret != LZMA_STREAM_END)
264
102k
      return ret;
265
266
    // Block decoded successfully. Add the new size pair to
267
    // the Index hash.
268
1.60k
    return_if_error(lzma_index_hash_append(coder->index_hash,
269
1.60k
        lzma_block_unpadded_size(
270
1.60k
          &coder->block_options),
271
1.60k
        coder->block_options.uncompressed_size));
272
273
1.60k
    coder->sequence = SEQ_BLOCK_HEADER;
274
1.60k
    break;
275
1.60k
  }
276
277
982
  case SEQ_INDEX: {
278
    // If we don't have any input, don't call
279
    // lzma_index_hash_decode() since it would return
280
    // LZMA_BUF_ERROR, which we must not do here.
281
982
    if (*in_pos >= in_size)
282
108
      return LZMA_OK;
283
284
    // Decode the Index and compare it to the hash calculated
285
    // from the sizes of the Blocks (if any).
286
874
    const lzma_ret ret = lzma_index_hash_decode(coder->index_hash,
287
874
        in, in_pos, in_size);
288
874
    if (ret != LZMA_STREAM_END)
289
457
      return ret;
290
291
417
    coder->sequence = SEQ_STREAM_FOOTER;
292
417
    FALLTHROUGH;
293
417
  }
294
295
436
  case SEQ_STREAM_FOOTER: {
296
    // Copy the Stream Footer to the internal buffer.
297
436
    lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
298
436
        LZMA_STREAM_HEADER_SIZE);
299
300
    // Return if we didn't get the whole Stream Footer yet.
301
436
    if (coder->pos < LZMA_STREAM_HEADER_SIZE)
302
34
      return LZMA_OK;
303
304
402
    coder->pos = 0;
305
306
    // Decode the Stream Footer. The decoder gives
307
    // LZMA_FORMAT_ERROR if the magic bytes don't match,
308
    // so convert that return code to LZMA_DATA_ERROR.
309
402
    lzma_stream_flags footer_flags;
310
402
    const lzma_ret ret = lzma_stream_footer_decode(
311
402
        &footer_flags, coder->buffer);
312
402
    if (ret != LZMA_OK)
313
41
      return ret == LZMA_FORMAT_ERROR
314
41
          ? LZMA_DATA_ERROR : ret;
315
316
    // Check that Index Size stored in the Stream Footer matches
317
    // the real size of the Index field.
318
361
    if (lzma_index_hash_size(coder->index_hash)
319
361
        != footer_flags.backward_size)
320
25
      return LZMA_DATA_ERROR;
321
322
    // Compare that the Stream Flags fields are identical in
323
    // both Stream Header and Stream Footer.
324
336
    return_if_error(lzma_stream_flags_compare(
325
336
        &coder->stream_flags, &footer_flags));
326
327
328
    if (!coder->concatenated)
328
328
      return LZMA_STREAM_END;
329
330
0
    coder->sequence = SEQ_STREAM_PADDING;
331
0
    FALLTHROUGH;
332
0
  }
333
334
0
  case SEQ_STREAM_PADDING:
335
0
    assert(coder->concatenated);
336
337
    // Skip over possible Stream Padding.
338
0
    while (true) {
339
0
      if (*in_pos >= in_size) {
340
        // Unless LZMA_FINISH was used, we cannot
341
        // know if there's more input coming later.
342
0
        if (action != LZMA_FINISH)
343
0
          return LZMA_OK;
344
345
        // Stream Padding must be a multiple of
346
        // four bytes.
347
0
        return coder->pos == 0
348
0
            ? LZMA_STREAM_END
349
0
            : LZMA_DATA_ERROR;
350
0
      }
351
352
      // If the byte is not zero, it probably indicates
353
      // beginning of a new Stream (or the file is corrupt).
354
0
      if (in[*in_pos] != 0x00)
355
0
        break;
356
357
0
      ++*in_pos;
358
0
      coder->pos = (coder->pos + 1) & 3;
359
0
    }
360
361
    // Stream Padding must be a multiple of four bytes (empty
362
    // Stream Padding is OK).
363
0
    if (coder->pos != 0) {
364
0
      ++*in_pos;
365
0
      return LZMA_DATA_ERROR;
366
0
    }
367
368
    // Prepare to decode the next Stream.
369
0
    return_if_error(stream_decoder_reset(coder, allocator));
370
0
    break;
371
372
0
  default:
373
0
    assert(0);
374
0
    return LZMA_PROG_ERROR;
375
106k
  }
376
377
  // Never reached
378
104k
}
379
380
381
static void
382
stream_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
383
3.54k
{
384
3.54k
  lzma_stream_coder *coder = coder_ptr;
385
3.54k
  lzma_next_end(&coder->block_decoder, allocator);
386
3.54k
  lzma_index_hash_end(coder->index_hash, allocator);
387
3.54k
  lzma_free(coder, allocator);
388
3.54k
  return;
389
3.54k
}
390
391
392
static lzma_check
393
stream_decoder_get_check(const void *coder_ptr)
394
0
{
395
0
  const lzma_stream_coder *coder = coder_ptr;
396
0
  return coder->stream_flags.check;
397
0
}
398
399
400
static lzma_ret
401
stream_decoder_memconfig(void *coder_ptr, uint64_t *memusage,
402
    uint64_t *old_memlimit, uint64_t new_memlimit)
403
0
{
404
0
  lzma_stream_coder *coder = coder_ptr;
405
406
0
  *memusage = coder->memusage;
407
0
  *old_memlimit = coder->memlimit;
408
409
0
  if (new_memlimit != 0) {
410
0
    if (new_memlimit < coder->memusage)
411
0
      return LZMA_MEMLIMIT_ERROR;
412
413
0
    coder->memlimit = new_memlimit;
414
0
  }
415
416
0
  return LZMA_OK;
417
0
}
418
419
420
extern lzma_ret
421
lzma_stream_decoder_init(
422
    lzma_next_coder *next, const lzma_allocator *allocator,
423
    uint64_t memlimit, uint32_t flags)
424
4.41k
{
425
4.41k
  lzma_next_coder_init(&lzma_stream_decoder_init, next, allocator);
426
427
4.41k
  if (flags & ~LZMA_SUPPORTED_FLAGS)
428
0
    return LZMA_OPTIONS_ERROR;
429
430
4.41k
  lzma_stream_coder *coder = next->coder;
431
4.41k
  if (coder == NULL) {
432
3.54k
    coder = lzma_alloc(sizeof(lzma_stream_coder), allocator);
433
3.54k
    if (coder == NULL)
434
0
      return LZMA_MEM_ERROR;
435
436
3.54k
    next->coder = coder;
437
3.54k
    next->code = &stream_decode;
438
3.54k
    next->end = &stream_decoder_end;
439
3.54k
    next->get_check = &stream_decoder_get_check;
440
3.54k
    next->memconfig = &stream_decoder_memconfig;
441
442
3.54k
    coder->block_decoder = LZMA_NEXT_CODER_INIT;
443
3.54k
    coder->index_hash = NULL;
444
3.54k
  }
445
446
4.41k
  coder->memlimit = my_max(1, memlimit);
447
4.41k
  coder->memusage = LZMA_MEMUSAGE_BASE;
448
4.41k
  coder->tell_no_check = (flags & LZMA_TELL_NO_CHECK) != 0;
449
4.41k
  coder->tell_unsupported_check
450
4.41k
      = (flags & LZMA_TELL_UNSUPPORTED_CHECK) != 0;
451
4.41k
  coder->tell_any_check = (flags & LZMA_TELL_ANY_CHECK) != 0;
452
4.41k
  coder->ignore_check = (flags & LZMA_IGNORE_CHECK) != 0;
453
4.41k
  coder->concatenated = (flags & LZMA_CONCATENATED) != 0;
454
4.41k
  coder->first_stream = true;
455
456
4.41k
  return stream_decoder_reset(coder, allocator);
457
4.41k
}
458
459
460
extern LZMA_API(lzma_ret)
461
lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
462
2.06k
{
463
2.06k
  lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags);
464
465
2.06k
  strm->internal->supported_actions[LZMA_RUN] = true;
466
2.06k
  strm->internal->supported_actions[LZMA_FINISH] = true;
467
468
2.06k
  return LZMA_OK;
469
2.06k
}