]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/block_util.c
Bunch of liblzma tweaks, including some API changes.
[icculus/xz.git] / src / liblzma / common / block_util.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       block_header.c
4 /// \brief      Utility functions to handle lzma_block
5 //
6 //  Copyright (C) 2008 Lasse Collin
7 //
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.
12 //
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.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "common.h"
21 #include "index.h"
22
23
24 extern LZMA_API lzma_ret
25 lzma_block_compressed_size(lzma_block *block, lzma_vli total_size)
26 {
27         // Validate everything but Uncompressed Size and filters.
28         if (lzma_block_unpadded_size(block) == 0)
29                 return LZMA_PROG_ERROR;
30
31         const uint32_t container_size = block->header_size
32                         + lzma_check_size(block->check);
33
34         // Validate that Compressed Size will be greater than zero.
35         if (container_size <= total_size)
36                 return LZMA_DATA_ERROR;
37
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;
45
46         block->compressed_size = compressed_size;
47
48         return LZMA_OK;
49 }
50
51
52 extern LZMA_API lzma_vli
53 lzma_block_unpadded_size(const lzma_block *block)
54 {
55         // Validate the values that we are interested in i.e. all but
56         // Uncompressed Size and the filters.
57         //
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)
68                 return 0;
69
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;
74
75         // Calculate Unpadded Size and validate it.
76         const lzma_vli unpadded_size = block->compressed_size
77                                 + block->header_size
78                                 + lzma_check_size(block->check);
79
80         assert(unpadded_size >= UNPADDED_SIZE_MIN);
81         if (unpadded_size > UNPADDED_SIZE_MAX)
82                 return 0;
83
84         return unpadded_size;
85 }
86
87
88 extern LZMA_API lzma_vli
89 lzma_block_total_size(const lzma_block *block)
90 {
91         lzma_vli unpadded_size = lzma_block_unpadded_size(block);
92
93         if (unpadded_size != LZMA_VLI_UNKNOWN)
94                 unpadded_size = vli_ceil4(unpadded_size);
95
96         return unpadded_size;
97 }