Rizin
unix-like reverse engineering framework and cli tools
block_util.c
Go to the documentation of this file.
1 //
5 //
6 // Author: Lasse Collin
7 //
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
10 //
12 
13 #include "common.h"
14 #include "index.h"
15 
16 
18 lzma_block_compressed_size(lzma_block *block, lzma_vli unpadded_size)
19 {
20  // Validate everything but Uncompressed Size and filters.
21  if (lzma_block_unpadded_size(block) == 0)
22  return LZMA_PROG_ERROR;
23 
24  const uint32_t container_size = block->header_size
25  + lzma_check_size(block->check);
26 
27  // Validate that Compressed Size will be greater than zero.
28  if (unpadded_size <= container_size)
29  return LZMA_DATA_ERROR;
30 
31  // Calculate what Compressed Size is supposed to be.
32  // If Compressed Size was present in Block Header,
33  // compare that the new value matches it.
34  const lzma_vli compressed_size = unpadded_size - container_size;
35  if (block->compressed_size != LZMA_VLI_UNKNOWN
36  && block->compressed_size != compressed_size)
37  return LZMA_DATA_ERROR;
38 
40 
41  return LZMA_OK;
42 }
43 
44 
46 lzma_block_unpadded_size(const lzma_block *block)
47 {
48  // Validate the values that we are interested in i.e. all but
49  // Uncompressed Size and the filters.
50  //
51  // NOTE: This function is used for validation too, so it is
52  // essential that these checks are always done even if
53  // Compressed Size is unknown.
54  if (block == NULL || block->version > 1
57  || (block->header_size & 3)
59  || block->compressed_size == 0
60  || (unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
61  return 0;
62 
63  // If Compressed Size is unknown, return that we cannot know
64  // size of the Block either.
65  if (block->compressed_size == LZMA_VLI_UNKNOWN)
66  return LZMA_VLI_UNKNOWN;
67 
68  // Calculate Unpadded Size and validate it.
70  + block->header_size
71  + lzma_check_size(block->check);
72 
75  return 0;
76 
77  return unpadded_size;
78 }
79 
80 
82 lzma_block_total_size(const lzma_block *block)
83 {
84  lzma_vli unpadded_size = lzma_block_unpadded_size(block);
85 
88 
89  return unpadded_size;
90 }
#define LZMA_CHECK_ID_MAX
Maximum valid Check ID.
Definition: check.h:68
const lzma_allocator lzma_vli unpadded_size
Definition: index.h:345
#define LZMA_BLOCK_HEADER_SIZE_MIN
Definition: block.h:73
#define LZMA_BLOCK_HEADER_SIZE_MAX
Definition: block.h:74
LZMA_API(lzma_ret)
Definition: block_util.c:17
Handling of Index.
static lzma_vli vli_ceil4(lzma_vli vli)
Round the variable-length integer to the next multiple of four.
Definition: index.h:39
#define UNPADDED_SIZE_MAX
Maximum Unpadded Size.
Definition: index.h:23
#define UNPADDED_SIZE_MIN
Minimum Unpadded Size.
Definition: index.h:20
#define NULL
Definition: cris-opc.c:27
assert(limit<=UINT32_MAX/2)
unsigned int uint32_t
Definition: sftypes.h:29
Options for the Block and Block Header encoders and decoders.
Definition: block.h:30
uint32_t header_size
Size of the Block Header field.
Definition: block.h:72
lzma_check check
Type of integrity Check.
Definition: block.h:93
lzma_vli compressed_size
Size of the Compressed Data in bytes.
Definition: block.h:148
uint32_t version
Block format version.
Definition: block.h:52
Definitions common to the whole liblzma library.
uint64_t compressed_size
Definition: list.c:105
uint64_t lzma_vli
Variable-length integer type.
Definition: vli.h:63
#define lzma_vli_is_valid(vli)
Validate a variable-length integer.
Definition: vli.h:75
#define LZMA_VLI_UNKNOWN
VLI value to denote that the value is unknown.
Definition: vli.h:39
lzma_ret
Return values used by several functions in liblzma.
Definition: base.h:57
@ LZMA_PROG_ERROR
Programming error.
Definition: base.h:218
@ LZMA_DATA_ERROR
Data is corrupt.
Definition: base.h:172
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58