Coverage Report

Created: 2026-03-12 06:35

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
6.51k
{
20
  // If we haven't been given vli_pos, work in single-call mode.
21
6.51k
  size_t vli_pos_internal = 0;
22
6.51k
  if (vli_pos == NULL) {
23
5.69k
    vli_pos = &vli_pos_internal;
24
5.69k
    *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
5.69k
    if (*in_pos >= in_size)
31
20
      return LZMA_DATA_ERROR;
32
33
5.69k
  } else {
34
    // Initialize *vli when starting to decode a new integer.
35
826
    if (*vli_pos == 0)
36
826
      *vli = 0;
37
38
    // Validate the arguments.
39
826
    if (*vli_pos >= LZMA_VLI_BYTES_MAX
40
826
        || (*vli >> (*vli_pos * 7)) != 0)
41
826
      return LZMA_PROG_ERROR;;
42
43
826
    if (*in_pos >= in_size)
44
0
      return LZMA_BUF_ERROR;
45
826
  }
46
47
13.3k
  do {
48
    // Read the next byte. Use a temporary variable so that we
49
    // can update *in_pos immediately.
50
13.3k
    const uint8_t byte = in[*in_pos];
51
13.3k
    ++*in_pos;
52
53
    // Add the newly read byte to *vli.
54
13.3k
    *vli += (lzma_vli)(byte & 0x7F) << (*vli_pos * 7);
55
13.3k
    ++*vli_pos;
56
57
    // Check if this is the last byte of a multibyte integer.
58
13.3k
    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
6.42k
      if (byte == 0x00 && *vli_pos > 1)
63
46
        return LZMA_DATA_ERROR;
64
65
6.37k
      return vli_pos == &vli_pos_internal
66
6.37k
          ? LZMA_OK : LZMA_STREAM_END;
67
6.42k
    }
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
6.97k
    if (*vli_pos == LZMA_VLI_BYTES_MAX)
80
16
      return LZMA_DATA_ERROR;
81
82
6.97k
  } while (*in_pos < in_size);
83
84
60
  return vli_pos == &vli_pos_internal ? LZMA_DATA_ERROR : LZMA_OK;
85
6.49k
}