1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file vli_encoder.c
4 /// \brief Encodes variable-length integers
6 // Copyright (C) 2007-2008 Lasse Collin
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.
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.
18 ///////////////////////////////////////////////////////////////////////////////
23 extern LZMA_API lzma_ret
24 lzma_vli_encode(lzma_vli vli, size_t *restrict vli_pos,
25 uint8_t *restrict out, size_t *restrict out_pos,
28 // If we haven't been given vli_pos, work in single-call mode.
29 size_t vli_pos_internal = 0;
31 vli_pos = &vli_pos_internal;
33 // Validate the arguments.
34 if (*vli_pos >= LZMA_VLI_BYTES_MAX || *out_pos >= out_size
35 || vli > LZMA_VLI_VALUE_MAX)
36 return LZMA_PROG_ERROR;
38 // Write the non-last bytes in a loop.
39 while ((vli >> (*vli_pos * 7)) >= 0x80) {
40 out[*out_pos] = (uint8_t)(vli >> (*vli_pos * 7)) | 0x80;
43 assert(*vli_pos < LZMA_VLI_BYTES_MAX);
45 if (++*out_pos == out_size)
46 return vli_pos == &vli_pos_internal
47 ? LZMA_PROG_ERROR : LZMA_OK;
50 // Write the last byte.
51 out[*out_pos] = (uint8_t)(vli >> (*vli_pos * 7));
55 return vli_pos == &vli_pos_internal ? LZMA_OK : LZMA_STREAM_END;
60 extern LZMA_API uint32_t
61 lzma_vli_size(lzma_vli vli)
63 if (vli > LZMA_VLI_VALUE_MAX)
72 assert(i <= LZMA_VLI_BYTES_MAX);