2008-06-18 15:02:10 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file block_header.c
|
2008-08-28 19:53:15 +00:00
|
|
|
/// \brief Utility functions to handle lzma_block
|
2008-06-18 15:02:10 +00:00
|
|
|
//
|
2009-04-13 08:27:40 +00:00
|
|
|
// Author: Lasse Collin
|
2008-06-18 15:02:10 +00:00
|
|
|
//
|
2009-04-13 08:27:40 +00:00
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
2008-06-18 15:02:10 +00:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "common.h"
|
2008-11-19 18:46:52 +00:00
|
|
|
#include "index.h"
|
2008-06-18 15:02:10 +00:00
|
|
|
|
|
|
|
|
2009-02-02 18:14:03 +00:00
|
|
|
extern LZMA_API(lzma_ret)
|
2008-12-15 17:39:13 +00:00
|
|
|
lzma_block_compressed_size(lzma_block *block, lzma_vli total_size)
|
2008-06-18 15:02:10 +00:00
|
|
|
{
|
2008-12-15 17:39:13 +00:00
|
|
|
// Validate everything but Uncompressed Size and filters.
|
|
|
|
if (lzma_block_unpadded_size(block) == 0)
|
2008-06-18 15:02:10 +00:00
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
|
2008-12-15 17:39:13 +00:00
|
|
|
const uint32_t container_size = block->header_size
|
|
|
|
+ lzma_check_size(block->check);
|
2008-06-18 15:02:10 +00:00
|
|
|
|
|
|
|
// Validate that Compressed Size will be greater than zero.
|
|
|
|
if (container_size <= total_size)
|
|
|
|
return LZMA_DATA_ERROR;
|
|
|
|
|
2008-12-15 17:39:13 +00:00
|
|
|
// Calculate what Compressed Size is supposed to be.
|
|
|
|
// If Compressed Size was present in Block Header,
|
|
|
|
// compare that the new value matches it.
|
|
|
|
const lzma_vli compressed_size = total_size - container_size;
|
|
|
|
if (block->compressed_size != LZMA_VLI_UNKNOWN
|
|
|
|
&& block->compressed_size != compressed_size)
|
|
|
|
return LZMA_DATA_ERROR;
|
|
|
|
|
|
|
|
block->compressed_size = compressed_size;
|
2008-06-18 15:02:10 +00:00
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-02 18:14:03 +00:00
|
|
|
extern LZMA_API(lzma_vli)
|
2008-12-15 17:39:13 +00:00
|
|
|
lzma_block_unpadded_size(const lzma_block *block)
|
2008-06-18 15:02:10 +00:00
|
|
|
{
|
2008-11-19 18:46:52 +00:00
|
|
|
// Validate the values that we are interested in i.e. all but
|
|
|
|
// Uncompressed Size and the filters.
|
|
|
|
//
|
|
|
|
// NOTE: This function is used for validation too, so it is
|
|
|
|
// essential that these checks are always done even if
|
|
|
|
// Compressed Size is unknown.
|
2009-01-26 11:06:49 +00:00
|
|
|
if (block == NULL || block->version != 0
|
2008-12-27 17:27:49 +00:00
|
|
|
|| block->header_size < LZMA_BLOCK_HEADER_SIZE_MIN
|
2008-12-15 17:39:13 +00:00
|
|
|
|| block->header_size > LZMA_BLOCK_HEADER_SIZE_MAX
|
|
|
|
|| (block->header_size & 3)
|
|
|
|
|| !lzma_vli_is_valid(block->compressed_size)
|
|
|
|
|| block->compressed_size == 0
|
|
|
|
|| (unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
|
2008-06-18 15:02:10 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// If Compressed Size is unknown, return that we cannot know
|
2008-11-19 18:46:52 +00:00
|
|
|
// size of the Block either.
|
2008-12-15 17:39:13 +00:00
|
|
|
if (block->compressed_size == LZMA_VLI_UNKNOWN)
|
2008-09-13 09:10:43 +00:00
|
|
|
return LZMA_VLI_UNKNOWN;
|
2008-06-18 15:02:10 +00:00
|
|
|
|
2008-11-19 18:46:52 +00:00
|
|
|
// Calculate Unpadded Size and validate it.
|
2008-12-15 17:39:13 +00:00
|
|
|
const lzma_vli unpadded_size = block->compressed_size
|
|
|
|
+ block->header_size
|
|
|
|
+ lzma_check_size(block->check);
|
2008-06-18 15:02:10 +00:00
|
|
|
|
2008-11-19 18:46:52 +00:00
|
|
|
assert(unpadded_size >= UNPADDED_SIZE_MIN);
|
|
|
|
if (unpadded_size > UNPADDED_SIZE_MAX)
|
2008-06-18 15:02:10 +00:00
|
|
|
return 0;
|
|
|
|
|
2008-11-19 18:46:52 +00:00
|
|
|
return unpadded_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-02 18:14:03 +00:00
|
|
|
extern LZMA_API(lzma_vli)
|
2008-12-15 17:39:13 +00:00
|
|
|
lzma_block_total_size(const lzma_block *block)
|
2008-11-19 18:46:52 +00:00
|
|
|
{
|
2008-12-15 17:39:13 +00:00
|
|
|
lzma_vli unpadded_size = lzma_block_unpadded_size(block);
|
2008-11-19 18:46:52 +00:00
|
|
|
|
2008-12-15 17:39:13 +00:00
|
|
|
if (unpadded_size != LZMA_VLI_UNKNOWN)
|
2008-11-19 18:46:52 +00:00
|
|
|
unpadded_size = vli_ceil4(unpadded_size);
|
|
|
|
|
|
|
|
return unpadded_size;
|
2008-06-18 15:02:10 +00:00
|
|
|
}
|