Rizin
unix-like reverse engineering framework and cli tools
block_encoder.h File Reference

Encodes .xz Blocks. More...

#include "common.h"

Go to the source code of this file.

Macros

#define COMPRESSED_SIZE_MAX
 Biggest Compressed Size value that the Block encoder supports. More...
 

Functions

lzma_ret lzma_block_encoder_init (lzma_next_coder *next, const lzma_allocator *allocator, lzma_block *block)
 

Detailed Description

Encodes .xz Blocks.

Definition in file block_encoder.h.

Macro Definition Documentation

◆ COMPRESSED_SIZE_MAX

#define COMPRESSED_SIZE_MAX
Value:
#define LZMA_CHECK_SIZE_MAX
Maximum size of a Check field.
Definition: check.h:102
#define LZMA_BLOCK_HEADER_SIZE_MAX
Definition: block.h:74
#define LZMA_VLI_C(n)
VLI constant suffix.
Definition: vli.h:49
#define LZMA_VLI_MAX
Maximum supported value of a variable-length integer.
Definition: vli.h:34

Biggest Compressed Size value that the Block encoder supports.

The maximum size of a single Block is limited by the maximum size of a Stream, which in theory is 2^63 - 3 bytes (i.e. LZMA_VLI_MAX - 3). While the size is really big and no one should hit it in practice, we take it into account in some places anyway to catch some errors e.g. if application passes insanely big value to some function.

We could take into account the headers etc. to determine the exact maximum size of the Compressed Data field, but the complexity would give us nothing useful. Instead, limit the size of Compressed Data so that even with biggest possible Block Header and Check fields the total encoded size of the Block stays as a valid VLI. This doesn't guarantee that the size of the Stream doesn't grow too big, but that problem is taken care outside the Block handling code.

~LZMA_VLI_C(3) is to guarantee that if we need padding at the end of the Compressed Data field, it will still stay in the proper limit.

This constant is in this file because it is needed in both block_encoder.c and block_buffer_encoder.c.

Definition at line 40 of file block_encoder.h.

Function Documentation

◆ lzma_block_encoder_init()

lzma_ret lzma_block_encoder_init ( lzma_next_coder next,
const lzma_allocator allocator,
lzma_block block 
)

Definition at line 164 of file block_encoder.c.

166 {
168 
169  if (block == NULL)
170  return LZMA_PROG_ERROR;
171 
172  // The contents of the structure may depend on the version so
173  // check the version first.
174  if (block->version > 1)
175  return LZMA_OPTIONS_ERROR;
176 
177  // If the Check ID is not supported, we cannot calculate the check and
178  // thus not create a proper Block.
179  if ((unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
180  return LZMA_PROG_ERROR;
181 
182  if (!lzma_check_is_supported(block->check))
183  return LZMA_UNSUPPORTED_CHECK;
184 
185  // Allocate and initialize *next->coder if needed.
186  lzma_block_coder *coder = next->coder;
187  if (coder == NULL) {
188  coder = lzma_alloc(sizeof(lzma_block_coder), allocator);
189  if (coder == NULL)
190  return LZMA_MEM_ERROR;
191 
192  next->coder = coder;
193  next->code = &block_encode;
194  next->end = &block_encoder_end;
195  next->update = &block_encoder_update;
196  coder->next = LZMA_NEXT_CODER_INIT;
197  }
198 
199  // Basic initializations
200  coder->sequence = SEQ_CODE;
201  coder->block = block;
202  coder->compressed_size = 0;
203  coder->uncompressed_size = 0;
204  coder->pos = 0;
205 
206  // Initialize the check
207  lzma_check_init(&coder->check, block->check);
208 
209  // Initialize the requested filters.
210  return lzma_raw_encoder_init(&coder->next, allocator, block->filters);
211 }
#define LZMA_CHECK_ID_MAX
Maximum valid Check ID.
Definition: check.h:68
const lzma_allocator * allocator
Definition: block.h:377
lzma_ret lzma_block_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator, lzma_block *block)
static lzma_ret block_encode(void *coder_ptr, const lzma_allocator *allocator, const uint8_t *restrict in, size_t *restrict in_pos, size_t in_size, uint8_t *restrict out, size_t *restrict out_pos, size_t out_size, lzma_action action)
Definition: block_encoder.c:48
static void block_encoder_end(void *coder_ptr, const lzma_allocator *allocator)
static lzma_ret block_encoder_update(void *coder_ptr, const lzma_allocator *allocator, const lzma_filter *filters lzma_attribute((__unused__)), const lzma_filter *reversed_filters)
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
lzma_ret lzma_raw_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter *options)
lzma_check_state check
Check of the uncompressed data.
Definition: block_decoder.c:47
enum lzma_block_coder::@643 sequence
lzma_next_coder next
The filters in the chain; initialized with lzma_raw_decoder_init().
Definition: block_decoder.c:26
lzma_block * block
Definition: block_decoder.c:30
size_t pos
Position in the Check field.
Definition: block_encoder.c:40
lzma_vli uncompressed_size
Uncompressed Size calculated while decoding.
Definition: block_decoder.c:36
lzma_vli compressed_size
Compressed Size calculated while decoding.
Definition: block_decoder.c:33
lzma_filter * filters
Array of filters.
Definition: block.h:200
lzma_check check
Type of integrity Check.
Definition: block.h:93
uint32_t version
Block format version.
Definition: block.h:52
lzma_code_function code
Pointer to function to do the actual coding.
Definition: common.h:150
void * coder
Pointer to coder-specific data.
Definition: common.h:137
lzma_end_function end
Definition: common.h:155
lzma_ret(* update)(void *coder, const lzma_allocator *allocator, const lzma_filter *filters, const lzma_filter *reversed_filters)
Definition: common.h:173
#define LZMA_NEXT_CODER_INIT
Macro to initialize lzma_next_coder structure.
Definition: common.h:180
#define lzma_next_coder_init(func, next, allocator)
Definition: common.h:291
void * lzma_alloc(size_t size, const lzma_allocator *allocator) lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
Allocates memory.
@ LZMA_PROG_ERROR
Programming error.
Definition: base.h:218
@ LZMA_MEM_ERROR
Cannot allocate memory.
Definition: base.h:128
@ LZMA_UNSUPPORTED_CHECK
Cannot calculate the integrity check.
Definition: base.h:90
@ LZMA_OPTIONS_ERROR
Invalid or unsupported options.
Definition: base.h:160

References allocator, lzma_block_coder::block, block_encode(), block_encoder_end(), block_encoder_update(), lzma_block::check, lzma_block_coder::check, lzma_next_coder_s::code, lzma_next_coder_s::coder, lzma_block_coder::compressed_size, lzma_next_coder_s::end, lzma_block::filters, lzma_alloc(), LZMA_CHECK_ID_MAX, lzma_check_init(), LZMA_MEM_ERROR, LZMA_NEXT_CODER_INIT, lzma_next_coder_init, LZMA_OPTIONS_ERROR, LZMA_PROG_ERROR, lzma_raw_encoder_init(), LZMA_UNSUPPORTED_CHECK, lzma_block_coder::next, NULL, lzma_block_coder::pos, lzma_block_coder::sequence, lzma_block_coder::uncompressed_size, lzma_next_coder_s::update, and lzma_block::version.

Referenced by block_encoder_init(), LZMA_API(), and worker_encode().