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 || vli > LZMA_VLI_MAX)
35 return LZMA_PROG_ERROR;
37 if (*out_pos >= out_size)
38 return LZMA_BUF_ERROR;
40 // Write the non-last bytes in a loop.
41 while ((vli >> (*vli_pos * 7)) >= 0x80) {
42 out[*out_pos] = (uint8_t)(vli >> (*vli_pos * 7)) | 0x80;
45 assert(*vli_pos < LZMA_VLI_BYTES_MAX);
47 if (++*out_pos == out_size)
48 return vli_pos == &vli_pos_internal
49 ? LZMA_PROG_ERROR : LZMA_OK;
52 // Write the last byte.
53 out[*out_pos] = (uint8_t)(vli >> (*vli_pos * 7));
57 return vli_pos == &vli_pos_internal ? LZMA_OK : LZMA_STREAM_END;