Rizin
unix-like reverse engineering framework and cli tools
index_hash.c File Reference

Validates Index by using a hash function. More...

#include "common.h"
#include "index.h"
#include "check.h"

Go to the source code of this file.

Classes

struct  lzma_index_hash_info
 
struct  lzma_index_hash_s
 

Functions

 LZMA_API (lzma_index_hash *)
 
 LZMA_API (void)
 
 LZMA_API (lzma_vli)
 Calculate approximate memory usage of easy encoder. More...
 
static lzma_ret hash_append (lzma_index_hash_info *info, lzma_vli unpadded_size, lzma_vli uncompressed_size)
 Updates the sizes and the hash without any validation. More...
 
 LZMA_API (lzma_ret)
 

Detailed Description

Validates Index by using a hash function.

Definition in file index_hash.c.

Function Documentation

◆ hash_append()

static lzma_ret hash_append ( lzma_index_hash_info info,
lzma_vli  unpadded_size,
lzma_vli  uncompressed_size 
)
static

Updates the sizes and the hash without any validation.

Definition at line 126 of file index_hash.c.

128 {
129  info->blocks_size += vli_ceil4(unpadded_size);
130  info->uncompressed_size += uncompressed_size;
131  info->index_list_size += lzma_vli_size(unpadded_size)
132  + lzma_vli_size(uncompressed_size);
133  ++info->count;
134 
135  const lzma_vli sizes[2] = { unpadded_size, uncompressed_size };
137  (const uint8_t *)(sizes), sizeof(sizes));
138 
139  return LZMA_OK;
140 }
const lzma_allocator lzma_vli unpadded_size
Definition: index.h:345
RzBinInfo * info(RzBinFile *bf)
Definition: bin_ne.c:86
#define LZMA_CHECK_BEST
Definition: check.h:73
void lzma_check_update(lzma_check_state *check, lzma_check type, const uint8_t *buf, size_t size)
Update the check state.
Definition: check.c:117
static lzma_vli vli_ceil4(lzma_vli vli)
Round the variable-length integer to the next multiple of four.
Definition: index.h:39
unsigned char uint8_t
Definition: sftypes.h:31
uint64_t uncompressed_size
Definition: list.c:106
uint64_t lzma_vli
Variable-length integer type.
Definition: vli.h:63
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58

References info(), LZMA_CHECK_BEST, lzma_check_update(), LZMA_OK, uncompressed_size, unpadded_size, and vli_ceil4().

Referenced by LZMA_API().

◆ LZMA_API() [1/4]

LZMA_API ( lzma_index_hash )

Definition at line 72 of file index_hash.c.

75 {
76  if (index_hash == NULL) {
77  index_hash = lzma_alloc(sizeof(lzma_index_hash), allocator);
78  if (index_hash == NULL)
79  return NULL;
80  }
81 
82  index_hash->sequence = SEQ_BLOCK;
83  index_hash->blocks.blocks_size = 0;
84  index_hash->blocks.uncompressed_size = 0;
85  index_hash->blocks.count = 0;
86  index_hash->blocks.index_list_size = 0;
87  index_hash->records.blocks_size = 0;
88  index_hash->records.uncompressed_size = 0;
89  index_hash->records.count = 0;
90  index_hash->records.index_list_size = 0;
91  index_hash->unpadded_size = 0;
92  index_hash->uncompressed_size = 0;
93  index_hash->pos = 0;
94  index_hash->crc32 = 0;
95 
96  // These cannot fail because LZMA_CHECK_BEST is known to be supported.
97  (void)lzma_check_init(&index_hash->blocks.check, LZMA_CHECK_BEST);
98  (void)lzma_check_init(&index_hash->records.check, LZMA_CHECK_BEST);
99 
100  return index_hash;
101 }
const lzma_allocator * allocator
Definition: block.h:377
void lzma_check_init(lzma_check_state *check, lzma_check type)
Initialize *check depending on type.
Definition: check.c:84
#define NULL
Definition: cris-opc.c:27
void * lzma_alloc(size_t size, const lzma_allocator *allocator) lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
Allocates memory.

References allocator, lzma_index_hash_s::blocks, lzma_index_hash_info::blocks_size, lzma_index_hash_info::check, lzma_index_hash_info::count, lzma_index_hash_s::crc32, lzma_index_hash_info::index_list_size, lzma_alloc(), LZMA_CHECK_BEST, lzma_check_init(), NULL, lzma_index_hash_s::pos, lzma_index_hash_s::records, lzma_index_hash_s::sequence, lzma_index_hash_info::uncompressed_size, lzma_index_hash_s::uncompressed_size, and lzma_index_hash_s::unpadded_size.

◆ LZMA_API() [2/4]

LZMA_API ( lzma_ret  )

Definition at line 143 of file index_hash.c.

146 {
147  // Validate the arguments.
148  if (index_hash->sequence != SEQ_BLOCK
152  return LZMA_PROG_ERROR;
153 
154  // Update the hash.
155  return_if_error(hash_append(&index_hash->blocks,
157 
158  // Validate the properties of *info are still in allowed limits.
159  if (index_hash->blocks.blocks_size > LZMA_VLI_MAX
160  || index_hash->blocks.uncompressed_size > LZMA_VLI_MAX
161  || index_size(index_hash->blocks.count,
162  index_hash->blocks.index_list_size)
164  || index_stream_size(index_hash->blocks.blocks_size,
165  index_hash->blocks.count,
166  index_hash->blocks.index_list_size)
167  > LZMA_VLI_MAX)
168  return LZMA_DATA_ERROR;
169 
170  return LZMA_OK;
171 }
static lzma_vli index_stream_size(lzma_vli blocks_size, lzma_vli count, lzma_vli index_list_size)
Calculate the total size of the Stream.
Definition: index.h:65
#define UNPADDED_SIZE_MAX
Maximum Unpadded Size.
Definition: index.h:23
static lzma_vli index_size(lzma_vli count, lzma_vli index_list_size)
Calculate the size of the Index field including Index Padding.
Definition: index.h:57
#define UNPADDED_SIZE_MIN
Minimum Unpadded Size.
Definition: index.h:20
static lzma_ret hash_append(lzma_index_hash_info *info, lzma_vli unpadded_size, lzma_vli uncompressed_size)
Updates the sizes and the hash without any validation.
Definition: index_hash.c:126
#define LZMA_BACKWARD_SIZE_MAX
Definition: stream_flags.h:71
#define return_if_error(expr)
Return if expression doesn't evaluate to LZMA_OK.
Definition: common.h:278
#define LZMA_VLI_MAX
Maximum supported value of a variable-length integer.
Definition: vli.h:34
@ LZMA_PROG_ERROR
Programming error.
Definition: base.h:218
@ LZMA_DATA_ERROR
Data is corrupt.
Definition: base.h:172

References lzma_index_hash_s::blocks, lzma_index_hash_info::blocks_size, lzma_index_hash_info::count, hash_append(), lzma_index_hash_info::index_list_size, index_size(), index_stream_size(), LZMA_BACKWARD_SIZE_MAX, LZMA_DATA_ERROR, LZMA_OK, LZMA_PROG_ERROR, LZMA_VLI_MAX, return_if_error, lzma_index_hash_s::sequence, lzma_index_hash_info::uncompressed_size, uncompressed_size, unpadded_size, UNPADDED_SIZE_MAX, and UNPADDED_SIZE_MIN.

◆ LZMA_API() [3/4]

LZMA_API ( uint64_t  )

Calculate approximate memory usage of easy encoder.

Get the total amount of physical memory (RAM) in bytes.

Calculate approximate memory usage of multithreaded .xz encoder.

Calculate approximate decoder memory usage of a preset.

This function is a wrapper for lzma_raw_encoder_memusage().

Parameters
presetCompression preset (level and possible flags)
Returns
Number of bytes of memory required for the given preset when encoding. If an error occurs, for example due to unsupported preset, UINT64_MAX is returned.

This function is a wrapper for lzma_raw_decoder_memusage().

Parameters
presetCompression preset (level and possible flags)
Returns
Number of bytes of memory required to decompress a file that was compressed using the given preset. If an error occurs, for example due to unsupported preset, UINT64_MAX is returned.

Since doing the encoding in threaded mode doesn't affect the memory requirements of single-threaded decompressor, you can use lzma_easy_decoder_memusage(options->preset) or lzma_raw_decoder_memusage(options->filters) to calculate the decompressor memory requirements.

Parameters
optionsCompression options
Returns
Number of bytes of memory required for encoding with the given options. If an error occurs, for example due to unsupported preset or filter chain, UINT64_MAX is returned.

Calculate approximate memory usage of easy encoder.

Get the uncompressed size of the file.

Get the total size of the file.

Get the total size of the Blocks.

Get the total size of the Stream.

Get the size of the Index field as bytes.

Get the number of Blocks.

Get the number of Streams.

Calculate the memory usage of an existing lzma_index.

On disk, the size of the Index field depends on both the number of Records stored and how big values the Records store (due to variable-length integer encoding). When the Index is kept in lzma_index structure, the memory usage depends only on the number of Records/Blocks stored in the Index(es), and in case of concatenated lzma_indexes, the number of Streams. The size in RAM is almost always significantly bigger than in the encoded form on disk.

This function calculates an approximate amount of memory needed hold the given number of Streams and Blocks in lzma_index structure. This value may vary between CPU architectures and also between liblzma versions if the internal implementation is modified.

This is a shorthand for lzma_index_memusage(lzma_index_stream_count(i), lzma_index_block_count(i)).

This returns the total number of Blocks in lzma_index. To get number of Blocks in individual Streams, use lzma_index_iter.

This is needed to verify the Backward Size field in the Stream Footer.

If multiple lzma_indexes have been combined, this works as if the Blocks were in a single Stream. This is useful if you are going to combine Blocks from multiple Streams into a single new Stream.

This doesn't include the Stream Header, Stream Footer, Stream Padding, or Index fields.

When no lzma_indexes have been combined with lzma_index_cat() and there is no Stream Padding, this function is identical to lzma_index_stream_size(). If multiple lzma_indexes have been combined, this includes also the headers of each separate Stream and the possible Stream Padding fields.

Definition at line 113 of file index_hash.c.

115 {
116  // Get the size of the Index from ->blocks instead of ->records for
117  // cases where application wants to know the Index Size before
118  // decoding the Index.
119  return index_size(index_hash->blocks.count,
120  index_hash->blocks.index_list_size);
121 }

References lzma_index_hash_s::blocks, lzma_index_hash_info::count, lzma_index_hash_info::index_list_size, and index_size().

◆ LZMA_API() [4/4]

LZMA_API ( void  )

Definition at line 104 of file index_hash.c.

107 {
108  lzma_free(index_hash, allocator);
109  return;
110 }
void lzma_free(void *ptr, const lzma_allocator *allocator)
Frees memory.
Definition: common.c:78

References allocator, and lzma_free().