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 *options, lzma_vli total_size)
28 if (options->header_size < LZMA_BLOCK_HEADER_SIZE_MIN
29 || options->header_size > LZMA_BLOCK_HEADER_SIZE_MAX
30 || (options->header_size & 3)
31 || (unsigned)(options->check) > LZMA_CHECK_ID_MAX
33 return LZMA_PROG_ERROR;
35 const uint32_t container_size = options->header_size
36 + lzma_check_size(options->check);
38 // Validate that Compressed Size will be greater than zero.
39 if (container_size <= total_size)
40 return LZMA_DATA_ERROR;
42 options->compressed_size = total_size - container_size;
48 extern LZMA_API lzma_vli
49 lzma_block_unpadded_size(const lzma_block *options)
51 // Validate the values that we are interested in i.e. all but
52 // Uncompressed Size and the filters.
54 // NOTE: This function is used for validation too, so it is
55 // essential that these checks are always done even if
56 // Compressed Size is unknown.
57 if (options->header_size < LZMA_BLOCK_HEADER_SIZE_MIN
58 || options->header_size > LZMA_BLOCK_HEADER_SIZE_MAX
59 || (options->header_size & 3)
60 || !lzma_vli_is_valid(options->compressed_size)
61 || options->compressed_size == 0
62 || (unsigned int)(options->check) > LZMA_CHECK_ID_MAX)
65 // If Compressed Size is unknown, return that we cannot know
66 // size of the Block either.
67 if (options->compressed_size == LZMA_VLI_UNKNOWN)
68 return LZMA_VLI_UNKNOWN;
70 // Calculate Unpadded Size and validate it.
71 const lzma_vli unpadded_size = options->compressed_size
72 + options->header_size
73 + lzma_check_size(options->check);
75 assert(unpadded_size >= UNPADDED_SIZE_MIN);
76 if (unpadded_size > UNPADDED_SIZE_MAX)
83 extern LZMA_API lzma_vli
84 lzma_block_total_size(const lzma_block *options)
86 lzma_vli unpadded_size = lzma_block_unpadded_size(options);
88 if (unpadded_size != 0 && unpadded_size != LZMA_VLI_UNKNOWN)
89 unpadded_size = vli_ceil4(unpadded_size);