From d8b58d099340f8f4007b24b211ee41a7210c061c Mon Sep 17 00:00:00 2001 From: Lasse Collin Date: Tue, 20 Jan 2009 13:45:41 +0200 Subject: [PATCH] Block encoder cleanups --- src/liblzma/common/block_encoder.c | 28 +++++++--------------------- src/liblzma/common/block_encoder.h | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/liblzma/common/block_encoder.c b/src/liblzma/common/block_encoder.c index 994de59a..2b9151eb 100644 --- a/src/liblzma/common/block_encoder.c +++ b/src/liblzma/common/block_encoder.c @@ -22,21 +22,6 @@ #include "check.h" -/// The maximum size of a single Block is limited by the maximum size of -/// a Stream, which is 2^63 - 1 bytes (i.e. LZMA_VLI_MAX). We could -/// take into account the headers etc. to determine the exact maximum size -/// of the Compressed Data field, but the complexity would give us nothing -/// useful. Instead, limit the size of Compressed Data so that even with -/// biggest possible Block Header and Check fields the total encoded size of -/// the Block stays as valid VLI. This way we don't produce incorrect output -/// if someone will really try creating a Block of 8 EiB. -/// -/// ~LZMA_VLI_C(3) is to guarantee that if we need padding at the end of -/// the Compressed Data field, it will still stay in the proper limit. -#define COMPRESSED_SIZE_MAX ((LZMA_VLI_MAX - LZMA_BLOCK_HEADER_SIZE_MAX \ - - LZMA_CHECK_SIZE_MAX) & ~LZMA_VLI_C(3)) - - struct lzma_coder_s { /// The filters in the chain; initialized with lzma_raw_decoder_init(). lzma_next_coder next; @@ -58,7 +43,7 @@ struct lzma_coder_s { /// Uncompressed Size calculated while encoding lzma_vli uncompressed_size; - /// Position in Block Padding and the Check fields + /// Position in the Check field size_t pos; /// Check of the uncompressed data @@ -74,7 +59,7 @@ block_encode(lzma_coder *coder, lzma_allocator *allocator, { // Check that our amount of input stays in proper limits. if (LZMA_VLI_MAX - coder->uncompressed_size < in_size - *in_pos) - return LZMA_PROG_ERROR; + return LZMA_DATA_ERROR; switch (coder->sequence) { case SEQ_CODE: { @@ -117,14 +102,16 @@ block_encode(lzma_coder *coder, lzma_allocator *allocator, // Fall through case SEQ_PADDING: - // Pad Compressed Data to a multiple of four bytes. - while ((coder->compressed_size + coder->pos) & 3) { + // Pad Compressed Data to a multiple of four bytes. We can + // use coder->compressed_size for this since we don't need + // it for anything else anymore. + while (coder->compressed_size & 3) { if (*out_pos >= out_size) return LZMA_OK; out[*out_pos] = 0x00; ++*out_pos; - ++coder->pos; + ++coder->compressed_size; } if (coder->block->check == LZMA_CHECK_NONE) @@ -132,7 +119,6 @@ block_encode(lzma_coder *coder, lzma_allocator *allocator, lzma_check_finish(&coder->check, coder->block->check); - coder->pos = 0; coder->sequence = SEQ_CHECK; // Fall through diff --git a/src/liblzma/common/block_encoder.h b/src/liblzma/common/block_encoder.h index d5b3ec0d..3113dab7 100644 --- a/src/liblzma/common/block_encoder.h +++ b/src/liblzma/common/block_encoder.h @@ -23,6 +23,31 @@ #include "common.h" +/// \brief Biggest Compressed Size value that the Block encoder supports +/// +/// The maximum size of a single Block is limited by the maximum size of +/// a Stream, which in theory is 2^63 - 3 bytes (i.e. LZMA_VLI_MAX - 3). +/// While the size is really big and no one should hit it in practice, we +/// take it into account in some places anyway to catch some errors e.g. if +/// application passes insanely big value to some function. +/// +/// We could take into account the headers etc. to determine the exact +/// maximum size of the Compressed Data field, but the complexity would give +/// us nothing useful. Instead, limit the size of Compressed Data so that +/// even with biggest possible Block Header and Check fields the total +/// encoded size of the Block stays as a valid VLI. This doesn't guarantee +/// that the size of the Stream doesn't grow too big, but that problem is +/// taken care outside the Block handling code. +/// +/// ~LZMA_VLI_C(3) is to guarantee that if we need padding at the end of +/// the Compressed Data field, it will still stay in the proper limit. +/// +/// This constant is in this file because it is needed in both +/// block_encoder.c and block_buffer_encoder.c. +#define COMPRESSED_SIZE_MAX ((LZMA_VLI_MAX - LZMA_BLOCK_HEADER_SIZE_MAX \ + - LZMA_CHECK_SIZE_MAX) & ~LZMA_VLI_C(3)) + + extern lzma_ret lzma_block_encoder_init(lzma_next_coder *next, lzma_allocator *allocator, lzma_block *block);