aboutsummaryrefslogtreecommitdiff
path: root/src/liblzma/common/vli_decoder.c
blob: 2b89c1a7735d89c7dc517c72a8e7f9ed7c0d7cf4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
///////////////////////////////////////////////////////////////////////////////
//
/// \file       vli_decoder.c
/// \brief      Decodes variable-length integers
//
//  Copyright (C) 2007 Lasse Collin
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
///////////////////////////////////////////////////////////////////////////////

#include "common.h"


extern LZMA_API lzma_ret
lzma_vli_decode(lzma_vli *restrict vli, size_t *restrict vli_pos,
		const uint8_t *restrict in, size_t *restrict in_pos,
		size_t in_size)
{
	if (*vli > LZMA_VLI_VALUE_MAX || *vli_pos >= 9
			|| (*vli >> (7 * *vli_pos)) != 0)
		return LZMA_PROG_ERROR;

	if (*in_pos >= in_size)
		return LZMA_BUF_ERROR;

	if (*vli_pos == 0) {
		*vli_pos = 1;

		if (in[*in_pos] <= 0x7F) {
			// Single-byte integer
			*vli = in[*in_pos];
			++*in_pos;
			return LZMA_STREAM_END;
		}

		*vli = in[*in_pos] & 0x7F;
		++*in_pos;
	}

	while (*in_pos < in_size) {
		// Read in the next byte.
		*vli |= (lzma_vli)(in[*in_pos] & 0x7F) << (*vli_pos * 7);
		++*vli_pos;

		// Check if this is the last byte of a multibyte integer.
		if (in[*in_pos] & 0x80) {
			++*in_pos;
			return LZMA_STREAM_END;
		}

		// Limit variable-length representation to nine bytes.
		if (*vli_pos == 9)
			return LZMA_DATA_ERROR;

		// Increment input position only when the byte was accepted.
		++*in_pos;
	}

	return LZMA_OK;
}