1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file block_header.c
4 /// \brief Utility functions to handle lzma_block
6 // Copyright (C) 2008 Lasse Collin
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
24 extern LZMA_API lzma_ret
25 lzma_block_compressed_size(lzma_block *block, lzma_vli total_size)
27 // Validate everything but Uncompressed Size and filters.
28 if (lzma_block_unpadded_size(block) == 0)
29 return LZMA_PROG_ERROR;
31 const uint32_t container_size = block->header_size
32 + lzma_check_size(block->check);
34 // Validate that Compressed Size will be greater than zero.
35 if (container_size <= total_size)
36 return LZMA_DATA_ERROR;
38 // Calculate what Compressed Size is supposed to be.
39 // If Compressed Size was present in Block Header,
40 // compare that the new value matches it.
41 const lzma_vli compressed_size = total_size - container_size;
42 if (block->compressed_size != LZMA_VLI_UNKNOWN
43 && block->compressed_size != compressed_size)
44 return LZMA_DATA_ERROR;
46 block->compressed_size = compressed_size;
52 extern LZMA_API lzma_vli
53 lzma_block_unpadded_size(const lzma_block *block)
55 // Validate the values that we are interested in i.e. all but
56 // Uncompressed Size and the filters.
58 // NOTE: This function is used for validation too, so it is
59 // essential that these checks are always done even if
60 // Compressed Size is unknown.
61 if (block->version != 0
62 || block->header_size < LZMA_BLOCK_HEADER_SIZE_MIN
63 || block->header_size > LZMA_BLOCK_HEADER_SIZE_MAX
64 || (block->header_size & 3)
65 || !lzma_vli_is_valid(block->compressed_size)
66 || block->compressed_size == 0
67 || (unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
70 // If Compressed Size is unknown, return that we cannot know
71 // size of the Block either.
72 if (block->compressed_size == LZMA_VLI_UNKNOWN)
73 return LZMA_VLI_UNKNOWN;
75 // Calculate Unpadded Size and validate it.
76 const lzma_vli unpadded_size = block->compressed_size
78 + lzma_check_size(block->check);
80 assert(unpadded_size >= UNPADDED_SIZE_MIN);
81 if (unpadded_size > UNPADDED_SIZE_MAX)
88 extern LZMA_API lzma_vli
89 lzma_block_total_size(const lzma_block *block)
91 lzma_vli unpadded_size = lzma_block_unpadded_size(block);
93 if (unpadded_size != LZMA_VLI_UNKNOWN)
94 unpadded_size = vli_ceil4(unpadded_size);