]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/block_util.c
Oh well, big messy commit again. Some highlights:
[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 *options, lzma_vli total_size)
26 {
27         // Validate.
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
32                         || (total_size & 3))
33                 return LZMA_PROG_ERROR;
34
35         const uint32_t container_size = options->header_size
36                         + lzma_check_size(options->check);
37
38         // Validate that Compressed Size will be greater than zero.
39         if (container_size <= total_size)
40                 return LZMA_DATA_ERROR;
41
42         options->compressed_size = total_size - container_size;
43
44         return LZMA_OK;
45 }
46
47
48 extern LZMA_API lzma_vli
49 lzma_block_unpadded_size(const lzma_block *options)
50 {
51         // Validate the values that we are interested in i.e. all but
52         // Uncompressed Size and the filters.
53         //
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)
63                 return 0;
64
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;
69
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);
74
75         assert(unpadded_size >= UNPADDED_SIZE_MIN);
76         if (unpadded_size > UNPADDED_SIZE_MAX)
77                 return 0;
78
79         return unpadded_size;
80 }
81
82
83 extern LZMA_API lzma_vli
84 lzma_block_total_size(const lzma_block *options)
85 {
86         lzma_vli unpadded_size = lzma_block_unpadded_size(options);
87
88         if (unpadded_size != 0 && unpadded_size != LZMA_VLI_UNKNOWN)
89                 unpadded_size = vli_ceil4(unpadded_size);
90
91         return unpadded_size;
92 }