2007-12-08 22:42:33 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file vli_encoder.c
|
|
|
|
/// \brief Encodes variable-length integers
|
|
|
|
//
|
2008-06-18 15:02:10 +00:00
|
|
|
// Copyright (C) 2007-2008 Lasse Collin
|
2007-12-08 22:42:33 +00:00
|
|
|
//
|
|
|
|
// 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
|
2008-06-18 15:02:10 +00:00
|
|
|
lzma_vli_encode(lzma_vli vli, size_t *restrict vli_pos,
|
2007-12-08 22:42:33 +00:00
|
|
|
uint8_t *restrict out, size_t *restrict out_pos,
|
|
|
|
size_t out_size)
|
|
|
|
{
|
2008-06-18 15:02:10 +00:00
|
|
|
// If we haven't been given vli_pos, work in single-call mode.
|
|
|
|
size_t vli_pos_internal = 0;
|
|
|
|
if (vli_pos == NULL)
|
|
|
|
vli_pos = &vli_pos_internal;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
// Validate the arguments.
|
|
|
|
if (*vli_pos >= LZMA_VLI_BYTES_MAX || *out_pos >= out_size
|
|
|
|
|| vli > LZMA_VLI_VALUE_MAX)
|
|
|
|
return LZMA_PROG_ERROR;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
// Write the non-last bytes in a loop.
|
|
|
|
while ((vli >> (*vli_pos * 7)) >= 0x80) {
|
|
|
|
out[*out_pos] = (uint8_t)(vli >> (*vli_pos * 7)) | 0x80;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
++*vli_pos;
|
|
|
|
assert(*vli_pos < LZMA_VLI_BYTES_MAX);
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
if (++*out_pos == out_size)
|
|
|
|
return vli_pos == &vli_pos_internal
|
|
|
|
? LZMA_PROG_ERROR : LZMA_OK;
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
// Write the last byte.
|
|
|
|
out[*out_pos] = (uint8_t)(vli >> (*vli_pos * 7));
|
|
|
|
++*out_pos;
|
|
|
|
++*vli_pos;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
return vli_pos == &vli_pos_internal ? LZMA_OK : LZMA_STREAM_END;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
extern LZMA_API uint32_t
|
2007-12-08 22:42:33 +00:00
|
|
|
lzma_vli_size(lzma_vli vli)
|
|
|
|
{
|
|
|
|
if (vli > LZMA_VLI_VALUE_MAX)
|
|
|
|
return 0;
|
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
uint32_t i = 0;
|
2007-12-08 22:42:33 +00:00
|
|
|
do {
|
|
|
|
vli >>= 7;
|
|
|
|
++i;
|
|
|
|
} while (vli != 0);
|
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
assert(i <= LZMA_VLI_BYTES_MAX);
|
2007-12-08 22:42:33 +00:00
|
|
|
return i;
|
|
|
|
}
|