]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/block_util.c
Bunch of liblzma API cleanups and fixes.
[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->header_size < LZMA_BLOCK_HEADER_SIZE_MIN
62                         || block->header_size > LZMA_BLOCK_HEADER_SIZE_MAX
63                         || (block->header_size & 3)
64                         || !lzma_vli_is_valid(block->compressed_size)
65                         || block->compressed_size == 0
66                         || (unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
67                 return 0;
68
69         // If Compressed Size is unknown, return that we cannot know
70         // size of the Block either.
71         if (block->compressed_size == LZMA_VLI_UNKNOWN)
72                 return LZMA_VLI_UNKNOWN;
73
74         // Calculate Unpadded Size and validate it.
75         const lzma_vli unpadded_size = block->compressed_size
76                                 + block->header_size
77                                 + lzma_check_size(block->check);
78
79         assert(unpadded_size >= UNPADDED_SIZE_MIN);
80         if (unpadded_size > UNPADDED_SIZE_MAX)
81                 return 0;
82
83         return unpadded_size;
84 }
85
86
87 extern LZMA_API lzma_vli
88 lzma_block_total_size(const lzma_block *block)
89 {
90         lzma_vli unpadded_size = lzma_block_unpadded_size(block);
91
92         if (unpadded_size != LZMA_VLI_UNKNOWN)
93                 unpadded_size = vli_ceil4(unpadded_size);
94
95         return unpadded_size;
96 }