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

Utility functions to handle lzma_block. More...

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

Go to the source code of this file.

Functions

 LZMA_API (lzma_ret)
 
 LZMA_API (lzma_vli)
 Calculate approximate memory usage of easy encoder. More...
 

Detailed Description

Utility functions to handle lzma_block.

Definition in file block_util.c.

Function Documentation

◆ LZMA_API() [1/2]

LZMA_API ( lzma_ret  )

Definition at line 17 of file block_util.c.

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 
39  block->compressed_size = compressed_size;
40 
41  return LZMA_OK;
42 }
const lzma_allocator lzma_vli unpadded_size
Definition: index.h:345
unsigned int uint32_t
Definition: sftypes.h:29
uint64_t compressed_size
Definition: list.c:105
uint64_t lzma_vli
Variable-length integer type.
Definition: vli.h:63
#define LZMA_VLI_UNKNOWN
VLI value to denote that the value is unknown.
Definition: vli.h:39
@ 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

References lzma_block::check, lzma_block::compressed_size, compressed_size, lzma_block::header_size, LZMA_DATA_ERROR, LZMA_OK, LZMA_PROG_ERROR, LZMA_VLI_UNKNOWN, and unpadded_size.

◆ LZMA_API() [2/2]

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 45 of file block_util.c.

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
55  || block->header_size < LZMA_BLOCK_HEADER_SIZE_MIN
56  || block->header_size > LZMA_BLOCK_HEADER_SIZE_MAX
57  || (block->header_size & 3)
58  || !lzma_vli_is_valid(block->compressed_size)
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.
69  const lzma_vli unpadded_size = block->compressed_size
70  + block->header_size
71  + lzma_check_size(block->check);
72 
75  return 0;
76 
77  return unpadded_size;
78 }
#define LZMA_CHECK_ID_MAX
Maximum valid Check ID.
Definition: check.h:68
#define LZMA_BLOCK_HEADER_SIZE_MIN
Definition: block.h:73
#define LZMA_BLOCK_HEADER_SIZE_MAX
Definition: block.h:74
#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)
#define lzma_vli_is_valid(vli)
Validate a variable-length integer.
Definition: vli.h:75

References assert(), lzma_block::check, lzma_block::compressed_size, lzma_block::header_size, LZMA_BLOCK_HEADER_SIZE_MAX, LZMA_BLOCK_HEADER_SIZE_MIN, LZMA_CHECK_ID_MAX, lzma_vli_is_valid, LZMA_VLI_UNKNOWN, NULL, unpadded_size, UNPADDED_SIZE_MAX, UNPADDED_SIZE_MIN, and lzma_block::version.