Coverage Report

Created: 2026-04-29 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmliblzma/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
18.2k
{
88
  // Initialize the Index hash used to verify the Index.
89
18.2k
  coder->index_hash = lzma_index_hash_init(coder->index_hash, allocator);
90
18.2k
  if (coder->index_hash == NULL)
91
0
    return LZMA_MEM_ERROR;
92
93
  // Reset the rest of the variables.
94
18.2k
  coder->sequence = SEQ_STREAM_HEADER;
95
18.2k
  coder->pos = 0;
96
97
18.2k
  return LZMA_OK;
98
18.2k
}
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
4.86k
{
107
4.86k
  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
4.86k
  while (true)
112
156k
  switch (coder->sequence) {
113
18.2k
  case SEQ_STREAM_HEADER: {
114
    // Copy the Stream Header to the internal buffer.
115
18.2k
    lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
116
18.2k
        LZMA_STREAM_HEADER_SIZE);
117
118
    // Return if we didn't get the whole Stream Header yet.
119
18.2k
    if (coder->pos < LZMA_STREAM_HEADER_SIZE)
120
64
      return LZMA_OK;
121
122
18.1k
    coder->pos = 0;
123
124
    // Decode the Stream Header.
125
18.1k
    const lzma_ret ret = lzma_stream_header_decode(
126
18.1k
        &coder->stream_flags, coder->buffer);
127
18.1k
    if (ret != LZMA_OK)
128
38
      return ret == LZMA_FORMAT_ERROR && !coder->first_stream
129
38
          ? 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
18.1k
    coder->first_stream = false;
135
136
    // Copy the type of the Check so that Block Header and Block
137
    // decoders see it.
138
18.1k
    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
18.1k
    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
18.1k
    if (coder->tell_no_check && coder->stream_flags.check
147
0
        == LZMA_CHECK_NONE)
148
0
      return LZMA_NO_CHECK;
149
150
18.1k
    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
18.1k
    if (coder->tell_any_check)
156
0
      return LZMA_GET_CHECK;
157
18.1k
  }
158
159
  // Fall through
160
161
138k
  case SEQ_BLOCK_HEADER: {
162
138k
    if (*in_pos >= in_size)
163
180
      return LZMA_OK;
164
165
138k
    if (coder->pos == 0) {
166
      // Detect if it's Index.
167
138k
      if (in[*in_pos] == INDEX_INDICATOR) {
168
15.9k
        coder->sequence = SEQ_INDEX;
169
15.9k
        break;
170
15.9k
      }
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
122k
      coder->block_options.header_size
176
122k
          = lzma_block_header_size_decode(
177
122k
            in[*in_pos]);
178
122k
    }
179
180
    // Copy the Block Header to the internal buffer.
181
122k
    lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
182
122k
        coder->block_options.header_size);
183
184
    // Return if we didn't get the whole Block Header yet.
185
122k
    if (coder->pos < coder->block_options.header_size)
186
68
      return LZMA_OK;
187
188
122k
    coder->pos = 0;
189
122k
    coder->sequence = SEQ_BLOCK_INIT;
190
122k
  }
191
192
  // Fall through
193
194
122k
  case SEQ_BLOCK_INIT: {
195
    // Checking memusage and doing the initialization needs
196
    // its own sequence point because we need to be able to
197
    // retry if we return LZMA_MEMLIMIT_ERROR.
198
199
    // Version 1 is needed to support the .ignore_check option.
200
122k
    coder->block_options.version = 1;
201
202
    // Set up a buffer to hold the filter chain. Block Header
203
    // decoder will initialize all members of this array so
204
    // we don't need to do it here.
205
122k
    lzma_filter filters[LZMA_FILTERS_MAX + 1];
206
122k
    coder->block_options.filters = filters;
207
208
    // Decode the Block Header.
209
122k
    return_if_error(lzma_block_header_decode(&coder->block_options,
210
122k
        allocator, coder->buffer));
211
212
    // If LZMA_IGNORE_CHECK was used, this flag needs to be set.
213
    // It has to be set after lzma_block_header_decode() because
214
    // it always resets this to false.
215
121k
    coder->block_options.ignore_check = coder->ignore_check;
216
217
    // Check the memory usage limit.
218
121k
    const uint64_t memusage = lzma_raw_decoder_memusage(filters);
219
121k
    lzma_ret ret;
220
221
121k
    if (memusage == UINT64_MAX) {
222
      // One or more unknown Filter IDs.
223
12
      ret = LZMA_OPTIONS_ERROR;
224
121k
    } else {
225
      // Now we can set coder->memusage since we know that
226
      // the filter chain is valid. We don't want
227
      // lzma_memusage() to return UINT64_MAX in case of
228
      // invalid filter chain.
229
121k
      coder->memusage = memusage;
230
231
121k
      if (memusage > coder->memlimit) {
232
        // The chain would need too much memory.
233
0
        ret = LZMA_MEMLIMIT_ERROR;
234
121k
      } else {
235
        // Memory usage is OK.
236
        // Initialize the Block decoder.
237
121k
        ret = lzma_block_decoder_init(
238
121k
            &coder->block_decoder,
239
121k
            allocator,
240
121k
            &coder->block_options);
241
121k
      }
242
121k
    }
243
244
    // Free the allocated filter options since they are needed
245
    // only to initialize the Block decoder.
246
121k
    lzma_filters_free(filters, allocator);
247
121k
    coder->block_options.filters = NULL;
248
249
    // Check if memory usage calculation and Block decoder
250
    // initialization succeeded.
251
121k
    if (ret != LZMA_OK)
252
18
      return ret;
253
254
121k
    coder->sequence = SEQ_BLOCK_RUN;
255
121k
  }
256
257
  // Fall through
258
259
123k
  case SEQ_BLOCK_RUN: {
260
123k
    const lzma_ret ret = coder->block_decoder.code(
261
123k
        coder->block_decoder.coder, allocator,
262
123k
        in, in_pos, in_size, out, out_pos, out_size,
263
123k
        action);
264
265
123k
    if (ret != LZMA_STREAM_END)
266
3.26k
      return ret;
267
268
    // Block decoded successfully. Add the new size pair to
269
    // the Index hash.
270
120k
    return_if_error(lzma_index_hash_append(coder->index_hash,
271
120k
        lzma_block_unpadded_size(
272
120k
          &coder->block_options),
273
120k
        coder->block_options.uncompressed_size));
274
275
120k
    coder->sequence = SEQ_BLOCK_HEADER;
276
120k
    break;
277
120k
  }
278
279
15.9k
  case SEQ_INDEX: {
280
    // If we don't have any input, don't call
281
    // lzma_index_hash_decode() since it would return
282
    // LZMA_BUF_ERROR, which we must not do here.
283
15.9k
    if (*in_pos >= in_size)
284
52
      return LZMA_OK;
285
286
    // Decode the Index and compare it to the hash calculated
287
    // from the sizes of the Blocks (if any).
288
15.9k
    const lzma_ret ret = lzma_index_hash_decode(coder->index_hash,
289
15.9k
        in, in_pos, in_size);
290
15.9k
    if (ret != LZMA_STREAM_END)
291
320
      return ret;
292
293
15.6k
    coder->sequence = SEQ_STREAM_FOOTER;
294
15.6k
  }
295
296
  // Fall through
297
298
15.6k
  case SEQ_STREAM_FOOTER: {
299
    // Copy the Stream Footer to the internal buffer.
300
15.6k
    lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
301
15.6k
        LZMA_STREAM_HEADER_SIZE);
302
303
    // Return if we didn't get the whole Stream Footer yet.
304
15.6k
    if (coder->pos < LZMA_STREAM_HEADER_SIZE)
305
46
      return LZMA_OK;
306
307
15.6k
    coder->pos = 0;
308
309
    // Decode the Stream Footer. The decoder gives
310
    // LZMA_FORMAT_ERROR if the magic bytes don't match,
311
    // so convert that return code to LZMA_DATA_ERROR.
312
15.6k
    lzma_stream_flags footer_flags;
313
15.6k
    const lzma_ret ret = lzma_stream_footer_decode(
314
15.6k
        &footer_flags, coder->buffer);
315
15.6k
    if (ret != LZMA_OK)
316
74
      return ret == LZMA_FORMAT_ERROR
317
74
          ? LZMA_DATA_ERROR : ret;
318
319
    // Check that Index Size stored in the Stream Footer matches
320
    // the real size of the Index field.
321
15.5k
    if (lzma_index_hash_size(coder->index_hash)
322
15.5k
        != footer_flags.backward_size)
323
8
      return LZMA_DATA_ERROR;
324
325
    // Compare that the Stream Flags fields are identical in
326
    // both Stream Header and Stream Footer.
327
15.5k
    return_if_error(lzma_stream_flags_compare(
328
15.5k
        &coder->stream_flags, &footer_flags));
329
330
15.5k
    if (!coder->concatenated)
331
0
      return LZMA_STREAM_END;
332
333
15.5k
    coder->sequence = SEQ_STREAM_PADDING;
334
15.5k
  }
335
336
  // Fall through
337
338
15.5k
  case SEQ_STREAM_PADDING:
339
15.5k
    assert(coder->concatenated);
340
341
    // Skip over possible Stream Padding.
342
3.74M
    while (true) {
343
3.74M
      if (*in_pos >= in_size) {
344
        // Unless LZMA_FINISH was used, we cannot
345
        // know if there's more input coming later.
346
50
        if (action != LZMA_FINISH)
347
40
          return LZMA_OK;
348
349
        // Stream Padding must be a multiple of
350
        // four bytes.
351
10
        return coder->pos == 0
352
10
            ? LZMA_STREAM_END
353
10
            : LZMA_DATA_ERROR;
354
50
      }
355
356
      // If the byte is not zero, it probably indicates
357
      // beginning of a new Stream (or the file is corrupt).
358
3.74M
      if (in[*in_pos] != 0x00)
359
15.5k
        break;
360
361
3.73M
      ++*in_pos;
362
3.73M
      coder->pos = (coder->pos + 1) & 3;
363
3.73M
    }
364
365
    // Stream Padding must be a multiple of four bytes (empty
366
    // Stream Padding is OK).
367
15.5k
    if (coder->pos != 0) {
368
16
      ++*in_pos;
369
16
      return LZMA_DATA_ERROR;
370
16
    }
371
372
    // Prepare to decode the next Stream.
373
15.4k
    return_if_error(stream_decoder_reset(coder, allocator));
374
15.4k
    break;
375
376
15.4k
  default:
377
0
    assert(0);
378
0
    return LZMA_PROG_ERROR;
379
156k
  }
380
381
  // Never reached
382
4.86k
}
383
384
385
static void
386
stream_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
387
2.73k
{
388
2.73k
  lzma_stream_coder *coder = coder_ptr;
389
2.73k
  lzma_next_end(&coder->block_decoder, allocator);
390
2.73k
  lzma_index_hash_end(coder->index_hash, allocator);
391
2.73k
  lzma_free(coder, allocator);
392
2.73k
  return;
393
2.73k
}
394
395
396
static lzma_check
397
stream_decoder_get_check(const void *coder_ptr)
398
0
{
399
0
  const lzma_stream_coder *coder = coder_ptr;
400
0
  return coder->stream_flags.check;
401
0
}
402
403
404
static lzma_ret
405
stream_decoder_memconfig(void *coder_ptr, uint64_t *memusage,
406
    uint64_t *old_memlimit, uint64_t new_memlimit)
407
0
{
408
0
  lzma_stream_coder *coder = coder_ptr;
409
410
0
  *memusage = coder->memusage;
411
0
  *old_memlimit = coder->memlimit;
412
413
0
  if (new_memlimit != 0) {
414
0
    if (new_memlimit < coder->memusage)
415
0
      return LZMA_MEMLIMIT_ERROR;
416
417
0
    coder->memlimit = new_memlimit;
418
0
  }
419
420
0
  return LZMA_OK;
421
0
}
422
423
424
extern lzma_ret
425
lzma_stream_decoder_init(
426
    lzma_next_coder *next, const lzma_allocator *allocator,
427
    uint64_t memlimit, uint32_t flags)
428
2.73k
{
429
2.73k
  lzma_next_coder_init(&lzma_stream_decoder_init, next, allocator);
430
431
2.73k
  if (flags & ~LZMA_SUPPORTED_FLAGS)
432
0
    return LZMA_OPTIONS_ERROR;
433
434
2.73k
  lzma_stream_coder *coder = next->coder;
435
2.73k
  if (coder == NULL) {
436
2.73k
    coder = lzma_alloc(sizeof(lzma_stream_coder), allocator);
437
2.73k
    if (coder == NULL)
438
0
      return LZMA_MEM_ERROR;
439
440
2.73k
    next->coder = coder;
441
2.73k
    next->code = &stream_decode;
442
2.73k
    next->end = &stream_decoder_end;
443
2.73k
    next->get_check = &stream_decoder_get_check;
444
2.73k
    next->memconfig = &stream_decoder_memconfig;
445
446
2.73k
    coder->block_decoder = LZMA_NEXT_CODER_INIT;
447
2.73k
    coder->index_hash = NULL;
448
2.73k
  }
449
450
2.73k
  coder->memlimit = my_max(1, memlimit);
451
2.73k
  coder->memusage = LZMA_MEMUSAGE_BASE;
452
2.73k
  coder->tell_no_check = (flags & LZMA_TELL_NO_CHECK) != 0;
453
2.73k
  coder->tell_unsupported_check
454
2.73k
      = (flags & LZMA_TELL_UNSUPPORTED_CHECK) != 0;
455
2.73k
  coder->tell_any_check = (flags & LZMA_TELL_ANY_CHECK) != 0;
456
2.73k
  coder->ignore_check = (flags & LZMA_IGNORE_CHECK) != 0;
457
2.73k
  coder->concatenated = (flags & LZMA_CONCATENATED) != 0;
458
2.73k
  coder->first_stream = true;
459
460
2.73k
  return stream_decoder_reset(coder, allocator);
461
2.73k
}
462
463
464
extern LZMA_API(lzma_ret)
465
lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
466
2.73k
{
467
2.73k
  lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags);
468
469
2.73k
  strm->internal->supported_actions[LZMA_RUN] = true;
470
2.73k
  strm->internal->supported_actions[LZMA_FINISH] = true;
471
472
2.73k
  return LZMA_OK;
473
2.73k
}