aboutsummaryrefslogtreecommitdiff
path: root/src/liblzma/common/vli_encoder.c
diff options
context:
space:
mode:
authorLasse Collin <lasse.collin@tukaani.org>2008-06-18 18:02:10 +0300
committerLasse Collin <lasse.collin@tukaani.org>2008-06-18 18:02:10 +0300
commit7d17818cec8597f847b0a2537fde991bbc3d9e96 (patch)
tree9c41502e3eb96f103fe98e13456b382fbba7a292 /src/liblzma/common/vli_encoder.c
parentUpdate the file format specification draft. The new one is (diff)
downloadxz-7d17818cec8597f847b0a2537fde991bbc3d9e96.tar.xz
Update the code to mostly match the new simpler file format
specification. Simplify things by removing most of the support for known uncompressed size in most places. There are some miscellaneous changes here and there too. The API of liblzma has got many changes and still some more will be done soon. While most of the code has been updated, some things are not fixed (the command line tool will choke with invalid filter chain, if nothing else). Subblock filter is somewhat broken for now. It will be updated once the encoded format of the Subblock filter has been decided.
Diffstat (limited to '')
-rw-r--r--src/liblzma/common/vli_encoder.c59
1 files changed, 26 insertions, 33 deletions
diff --git a/src/liblzma/common/vli_encoder.c b/src/liblzma/common/vli_encoder.c
index 1ecdb0d2..c48d6474 100644
--- a/src/liblzma/common/vli_encoder.c
+++ b/src/liblzma/common/vli_encoder.c
@@ -3,7 +3,7 @@
/// \file vli_encoder.c
/// \brief Encodes variable-length integers
//
-// Copyright (C) 2007 Lasse Collin
+// Copyright (C) 2007-2008 Lasse Collin
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
@@ -21,61 +21,54 @@
extern LZMA_API lzma_ret
-lzma_vli_encode(lzma_vli vli, size_t *restrict vli_pos, size_t vli_size,
+lzma_vli_encode(lzma_vli vli, size_t *restrict vli_pos,
uint8_t *restrict out, size_t *restrict out_pos,
size_t out_size)
{
- if (vli > LZMA_VLI_VALUE_MAX || *vli_pos >= 9 || vli_size > 9
- || (vli != 0 && (vli >> (7 * *vli_pos)) == 0))
- return LZMA_PROG_ERROR;
+ // 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;
- if (*out_pos >= out_size)
- return LZMA_BUF_ERROR;
+ // Validate the arguments.
+ if (*vli_pos >= LZMA_VLI_BYTES_MAX || *out_pos >= out_size
+ || vli > LZMA_VLI_VALUE_MAX)
+ return LZMA_PROG_ERROR;
- if (*vli_pos == 0) {
- *vli_pos = 1;
+ // Write the non-last bytes in a loop.
+ while ((vli >> (*vli_pos * 7)) >= 0x80) {
+ out[*out_pos] = (uint8_t)(vli >> (*vli_pos * 7)) | 0x80;
- if (vli <= 0x7F && *vli_pos >= vli_size) {
- // Single-byte integer
- out[(*out_pos)++] = vli;
- return LZMA_STREAM_END;
- }
+ ++*vli_pos;
+ assert(*vli_pos < LZMA_VLI_BYTES_MAX);
- // First byte of a multibyte integer
- out[(*out_pos)++] = (vli & 0x7F) | 0x80;
+ if (++*out_pos == out_size)
+ return vli_pos == &vli_pos_internal
+ ? LZMA_PROG_ERROR : LZMA_OK;
}
- while (*out_pos < out_size) {
- const lzma_vli b = vli >> (7 * *vli_pos);
- ++*vli_pos;
-
- if (b <= 0x7F && *vli_pos >= vli_size) {
- // Last byte of a multibyte integer
- out[(*out_pos)++] = (b & 0xFF) | 0x80;
- return LZMA_STREAM_END;
- }
+ // Write the last byte.
+ out[*out_pos] = (uint8_t)(vli >> (*vli_pos * 7));
+ ++*out_pos;
+ ++*vli_pos;
- // Middle byte of a multibyte integer
- out[(*out_pos)++] = b & 0x7F;
- }
+ return vli_pos == &vli_pos_internal ? LZMA_OK : LZMA_STREAM_END;
- // vli is not yet completely written out.
- return LZMA_OK;
}
-extern LZMA_API size_t
+extern LZMA_API uint32_t
lzma_vli_size(lzma_vli vli)
{
if (vli > LZMA_VLI_VALUE_MAX)
return 0;
- size_t i = 0;
+ uint32_t i = 0;
do {
vli >>= 7;
++i;
} while (vli != 0);
- assert(i <= 9);
+ assert(i <= LZMA_VLI_BYTES_MAX);
return i;
}