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/vli_decoder.c
Line
Count
Source
1
// SPDX-License-Identifier: 0BSD
2
3
///////////////////////////////////////////////////////////////////////////////
4
//
5
/// \file       vli_decoder.c
6
/// \brief      Decodes variable-length integers
7
//
8
//  Author:     Lasse Collin
9
//
10
///////////////////////////////////////////////////////////////////////////////
11
12
#include "common.h"
13
14
15
extern LZMA_API(lzma_ret)
16
lzma_vli_decode(lzma_vli *restrict vli, size_t *vli_pos,
17
    const uint8_t *restrict in, size_t *restrict in_pos,
18
    size_t in_size)
19
511k
{
20
  // If we haven't been given vli_pos, work in single-call mode.
21
511k
  size_t vli_pos_internal = 0;
22
511k
  if (vli_pos == NULL) {
23
495k
    vli_pos = &vli_pos_internal;
24
495k
    *vli = 0;
25
26
    // If there's no input, use LZMA_DATA_ERROR. This way it is
27
    // easy to decode VLIs from buffers that have known size,
28
    // and get the correct error code in case the buffer is
29
    // too short.
30
495k
    if (*in_pos >= in_size)
31
36
      return LZMA_DATA_ERROR;
32
33
495k
  } else {
34
    // Initialize *vli when starting to decode a new integer.
35
16.8k
    if (*vli_pos == 0)
36
16.8k
      *vli = 0;
37
38
    // Validate the arguments.
39
16.8k
    if (*vli_pos >= LZMA_VLI_BYTES_MAX
40
16.8k
        || (*vli >> (*vli_pos * 7)) != 0)
41
16.8k
      return LZMA_PROG_ERROR;;
42
43
16.8k
    if (*in_pos >= in_size)
44
0
      return LZMA_BUF_ERROR;
45
16.8k
  }
46
47
519k
  do {
48
    // Read the next byte. Use a temporary variable so that we
49
    // can update *in_pos immediately.
50
519k
    const uint8_t byte = in[*in_pos];
51
519k
    ++*in_pos;
52
53
    // Add the newly read byte to *vli.
54
519k
    *vli += (lzma_vli)(byte & 0x7F) << (*vli_pos * 7);
55
519k
    ++*vli_pos;
56
57
    // Check if this is the last byte of a multibyte integer.
58
519k
    if ((byte & 0x80) == 0) {
59
      // We don't allow using variable-length integers as
60
      // padding i.e. the encoding must use the most the
61
      // compact form.
62
511k
      if (byte == 0x00 && *vli_pos > 1)
63
30
        return LZMA_DATA_ERROR;
64
65
511k
      return vli_pos == &vli_pos_internal
66
511k
          ? LZMA_OK : LZMA_STREAM_END;
67
511k
    }
68
69
    // There is at least one more byte coming. If we have already
70
    // read maximum number of bytes, the integer is considered
71
    // corrupt.
72
    //
73
    // If we need bigger integers in future, old versions liblzma
74
    // will confusingly indicate the file being corrupt instead of
75
    // unsupported. I suppose it's still better this way, because
76
    // in the foreseeable future (writing this in 2008) the only
77
    // reason why files would appear having over 63-bit integers
78
    // is that the files are simply corrupt.
79
8.08k
    if (*vli_pos == LZMA_VLI_BYTES_MAX)
80
20
      return LZMA_DATA_ERROR;
81
82
8.08k
  } while (*in_pos < in_size);
83
84
90
  return vli_pos == &vli_pos_internal ? LZMA_DATA_ERROR : LZMA_OK;
85
511k
}