1
0
Fork 0
mirror of https://git.tukaani.org/xz.git synced 2024-04-04 12:36:23 +02:00

Block encoder cleanups

This commit is contained in:
Lasse Collin 2009-01-20 13:45:41 +02:00
parent 0c09810cb3
commit d8b58d0993
2 changed files with 32 additions and 21 deletions

View file

@ -22,21 +22,6 @@
#include "check.h" #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 { struct lzma_coder_s {
/// The filters in the chain; initialized with lzma_raw_decoder_init(). /// The filters in the chain; initialized with lzma_raw_decoder_init().
lzma_next_coder next; lzma_next_coder next;
@ -58,7 +43,7 @@ struct lzma_coder_s {
/// Uncompressed Size calculated while encoding /// Uncompressed Size calculated while encoding
lzma_vli uncompressed_size; lzma_vli uncompressed_size;
/// Position in Block Padding and the Check fields /// Position in the Check field
size_t pos; size_t pos;
/// Check of the uncompressed data /// 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. // Check that our amount of input stays in proper limits.
if (LZMA_VLI_MAX - coder->uncompressed_size < in_size - *in_pos) if (LZMA_VLI_MAX - coder->uncompressed_size < in_size - *in_pos)
return LZMA_PROG_ERROR; return LZMA_DATA_ERROR;
switch (coder->sequence) { switch (coder->sequence) {
case SEQ_CODE: { case SEQ_CODE: {
@ -117,14 +102,16 @@ block_encode(lzma_coder *coder, lzma_allocator *allocator,
// Fall through // Fall through
case SEQ_PADDING: case SEQ_PADDING:
// Pad Compressed Data to a multiple of four bytes. // Pad Compressed Data to a multiple of four bytes. We can
while ((coder->compressed_size + coder->pos) & 3) { // 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) if (*out_pos >= out_size)
return LZMA_OK; return LZMA_OK;
out[*out_pos] = 0x00; out[*out_pos] = 0x00;
++*out_pos; ++*out_pos;
++coder->pos; ++coder->compressed_size;
} }
if (coder->block->check == LZMA_CHECK_NONE) 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); lzma_check_finish(&coder->check, coder->block->check);
coder->pos = 0;
coder->sequence = SEQ_CHECK; coder->sequence = SEQ_CHECK;
// Fall through // Fall through

View file

@ -23,6 +23,31 @@
#include "common.h" #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, extern lzma_ret lzma_block_encoder_init(lzma_next_coder *next,
lzma_allocator *allocator, lzma_block *block); lzma_allocator *allocator, lzma_block *block);