1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
5 /// \file vli_decoder.c
6 /// \brief Decodes variable-length integers
8 // Author: Lasse Collin
10 ///////////////////////////////////////////////////////////////////////////////
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
,
20 // If we haven't been given vli_pos, work in single-call mode.
21 size_t vli_pos_internal
= 0;
22 if (vli_pos
== NULL
) {
23 vli_pos
= &vli_pos_internal
;
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
30 if (*in_pos
>= in_size
)
31 return LZMA_DATA_ERROR
;
34 // Initialize *vli when starting to decode a new integer.
38 // Validate the arguments.
39 if (*vli_pos
>= LZMA_VLI_BYTES_MAX
40 || (*vli
>> (*vli_pos
* 7)) != 0)
41 return LZMA_PROG_ERROR
;;
43 if (*in_pos
>= in_size
)
44 return LZMA_BUF_ERROR
;
48 // Read the next byte. Use a temporary variable so that we
49 // can update *in_pos immediately.
50 const uint8_t byte
= in
[*in_pos
];
53 // Add the newly read byte to *vli.
54 *vli
+= (lzma_vli
)(byte
& 0x7F) << (*vli_pos
* 7);
57 // Check if this is the last byte of a multibyte integer.
58 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
62 if (byte
== 0x00 && *vli_pos
> 1)
63 return LZMA_DATA_ERROR
;
65 return vli_pos
== &vli_pos_internal
66 ? LZMA_OK
: LZMA_STREAM_END
;
69 // There is at least one more byte coming. If we have already
70 // read maximum number of bytes, the integer is considered
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 if (*vli_pos
== LZMA_VLI_BYTES_MAX
)
80 return LZMA_DATA_ERROR
;
82 } while (*in_pos
< in_size
);
84 return vli_pos
== &vli_pos_internal
? LZMA_DATA_ERROR
: LZMA_OK
;