]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/block_util.c
Update the code to mostly match the new simpler file format
[icculus/xz.git] / src / liblzma / common / block_util.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       block_header.c
4 /// \brief      Utility functions to handle lzma_options_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
22
23 extern LZMA_API lzma_ret
24 lzma_block_total_size_set(lzma_options_block *options, lzma_vli total_size)
25 {
26         // Validate.
27         if (options->header_size < LZMA_BLOCK_HEADER_SIZE_MIN
28                         || options->header_size > LZMA_BLOCK_HEADER_SIZE_MAX
29                         || (options->header_size & 3)
30                         || (unsigned)(options->check) > LZMA_CHECK_ID_MAX
31                         || (total_size & 3))
32                 return LZMA_PROG_ERROR;
33
34         const uint32_t container_size = options->header_size
35                         + lzma_check_sizes[options->check];
36
37         // Validate that Compressed Size will be greater than zero.
38         if (container_size <= total_size)
39                 return LZMA_DATA_ERROR;
40
41         options->compressed_size = total_size - container_size;
42
43         return LZMA_OK;
44 }
45
46
47 extern LZMA_API lzma_vli
48 lzma_block_total_size_get(const lzma_options_block *options)
49 {
50         // Validate the values that we are interested in.
51         if (options->header_size < LZMA_BLOCK_HEADER_SIZE_MIN
52                         || options->header_size > LZMA_BLOCK_HEADER_SIZE_MAX
53                         || (options->header_size & 3)
54                         || (unsigned)(options->check) > LZMA_CHECK_ID_MAX)
55                 return 0;
56
57         // If Compressed Size is unknown, return that we cannot know
58         // Total Size either.
59         if (options->compressed_size == LZMA_VLI_VALUE_UNKNOWN)
60                 return LZMA_VLI_VALUE_UNKNOWN;
61
62         const lzma_vli total_size = options->compressed_size
63                         + options->header_size
64                         + lzma_check_sizes[options->check];
65
66         // Validate the calculated Total Size.
67         if (options->compressed_size > LZMA_VLI_VALUE_MAX
68                         || (options->compressed_size & 3)
69                         || total_size > LZMA_VLI_VALUE_MAX)
70                 return 0;
71
72         return total_size;
73 }