]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/vli_decoder.c
c2a4b1eb3f44ca99fdaef9e2288e35836e0e876a
[icculus/xz.git] / src / liblzma / common / vli_decoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       vli_decoder.c
4 /// \brief      Decodes variable-length integers
5 //
6 //  Copyright (C) 2007-2008 Lasse Collin
7 //
8 //  This library is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU Lesser General Public
10 //  License as published by the Free Software Foundation; either
11 //  version 2.1 of the License, or (at your option) any later version.
12 //
13 //  This library is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 //  Lesser General Public License for more details.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "common.h"
21
22
23 extern LZMA_API(lzma_ret)
24 lzma_vli_decode(lzma_vli *restrict vli, size_t *restrict vli_pos,
25                 const uint8_t *restrict in, size_t *restrict in_pos,
26                 size_t in_size)
27 {
28         // If we haven't been given vli_pos, work in single-call mode.
29         size_t vli_pos_internal = 0;
30         if (vli_pos == NULL) {
31                 vli_pos = &vli_pos_internal;
32                 *vli = 0;
33
34                 // If there's no input, use LZMA_DATA_ERROR. This way it is
35                 // easy to decode VLIs from buffers that have known size,
36                 // and get the correct error code in case the buffer is
37                 // too short.
38                 if (*in_pos >= in_size)
39                         return LZMA_DATA_ERROR;
40
41         } else {
42                 // Initialize *vli when starting to decode a new integer.
43                 if (*vli_pos == 0)
44                         *vli = 0;
45
46                 // Validate the arguments.
47                 if (*vli_pos >= LZMA_VLI_BYTES_MAX
48                                 || (*vli >> (*vli_pos * 7)) != 0)
49                         return LZMA_PROG_ERROR;;
50
51                 if (*in_pos >= in_size)
52                         return LZMA_BUF_ERROR;
53         }
54
55         do {
56                 // Read the next byte. Use a temporary variable so that we
57                 // can update *in_pos immediatelly.
58                 const uint8_t byte = in[*in_pos];
59                 ++*in_pos;
60
61                 // Add the newly read byte to *vli.
62                 *vli += (lzma_vli)(byte & 0x7F) << (*vli_pos * 7);
63                 ++*vli_pos;
64
65                 // Check if this is the last byte of a multibyte integer.
66                 if ((byte & 0x80) == 0) {
67                         // We don't allow using variable-length integers as
68                         // padding i.e. the encoding must use the most the
69                         // compact form.
70                         if (byte == 0x00 && *vli_pos > 1)
71                                 return LZMA_DATA_ERROR;
72
73                         return vli_pos == &vli_pos_internal
74                                         ? LZMA_OK : LZMA_STREAM_END;
75                 }
76
77                 // There is at least one more byte coming. If we have already
78                 // read maximum number of bytes, the integer is considered
79                 // corrupt.
80                 //
81                 // If we need bigger integers in future, old versions liblzma
82                 // will confusingly indicate the file being corrupt istead of
83                 // unsupported. I suppose it's still better this way, because
84                 // in the foreseeable future (writing this in 2008) the only
85                 // reason why files would appear having over 63-bit integers
86                 // is that the files are simply corrupt.
87                 if (*vli_pos == LZMA_VLI_BYTES_MAX)
88                         return LZMA_DATA_ERROR;
89
90         } while (*in_pos < in_size);
91
92         return vli_pos == &vli_pos_internal ? LZMA_DATA_ERROR : LZMA_OK;
93 }