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

Decodes Block Header from .xz files. More...

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

Go to the source code of this file.

Functions

static void free_properties (lzma_block *block, const lzma_allocator *allocator)
 
 LZMA_API (lzma_ret)
 

Detailed Description

Decodes Block Header from .xz files.

Definition in file block_header_decoder.c.

Function Documentation

◆ free_properties()

static void free_properties ( lzma_block block,
const lzma_allocator allocator 
)
static

Definition at line 18 of file block_header_decoder.c.

19 {
20  // Free allocated filter options. The last array member is not
21  // touched after the initialization in the beginning of
22  // lzma_block_header_decode(), so we don't need to touch that here.
23  for (size_t i = 0; i < LZMA_FILTERS_MAX; ++i) {
25  block->filters[i].id = LZMA_VLI_UNKNOWN;
26  block->filters[i].options = NULL;
27  }
28 
29  return;
30 }
lzma_index ** i
Definition: index.h:629
const lzma_allocator * allocator
Definition: block.h:377
#define NULL
Definition: cris-opc.c:27
#define LZMA_FILTERS_MAX
Maximum number of filters in a chain.
Definition: filter.h:26
lzma_filter * filters
Array of filters.
Definition: block.h:200
void * options
Pointer to filter-specific options structure.
Definition: filter.h:63
lzma_vli id
Filter ID.
Definition: filter.h:54
#define LZMA_VLI_UNKNOWN
VLI value to denote that the value is unknown.
Definition: vli.h:39
void lzma_free(void *ptr, const lzma_allocator *allocator)
Frees memory.
Definition: common.c:78

References allocator, lzma_block::filters, i, lzma_filter::id, LZMA_FILTERS_MAX, lzma_free(), LZMA_VLI_UNKNOWN, NULL, and lzma_filter::options.

Referenced by LZMA_API().

◆ LZMA_API()

LZMA_API ( lzma_ret  )

Definition at line 33 of file block_header_decoder.c.

36 {
37  // NOTE: We consider the header to be corrupt not only when the
38  // CRC32 doesn't match, but also when variable-length integers
39  // are invalid or over 63 bits, or if the header is too small
40  // to contain the claimed information.
41 
42  // Initialize the filter options array. This way the caller can
43  // safely free() the options even if an error occurs in this function.
44  for (size_t i = 0; i <= LZMA_FILTERS_MAX; ++i) {
45  block->filters[i].id = LZMA_VLI_UNKNOWN;
46  block->filters[i].options = NULL;
47  }
48 
49  // Versions 0 and 1 are supported. If a newer version was specified,
50  // we need to downgrade it.
51  if (block->version > 1)
52  block->version = 1;
53 
54  // This isn't a Block Header option, but since the decompressor will
55  // read it if version >= 1, it's better to initialize it here than
56  // to expect the caller to do it since in almost all cases this
57  // should be false.
58  block->ignore_check = false;
59 
60  // Validate Block Header Size and Check type. The caller must have
61  // already set these, so it is a programming error if this test fails.
62  if (lzma_block_header_size_decode(in[0]) != block->header_size
63  || (unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
64  return LZMA_PROG_ERROR;
65 
66  // Exclude the CRC32 field.
67  const size_t in_size = block->header_size - 4;
68 
69  // Verify CRC32
70  if (lzma_crc32(in, in_size, 0) != read32le(in + in_size))
71  return LZMA_DATA_ERROR;
72 
73  // Check for unsupported flags.
74  if (in[1] & 0x3C)
75  return LZMA_OPTIONS_ERROR;
76 
77  // Start after the Block Header Size and Block Flags fields.
78  size_t in_pos = 2;
79 
80  // Compressed Size
81  if (in[1] & 0x40) {
82  return_if_error(lzma_vli_decode(&block->compressed_size,
83  NULL, in, &in_pos, in_size));
84 
85  // Validate Compressed Size. This checks that it isn't zero
86  // and that the total size of the Block is a valid VLI.
87  if (lzma_block_unpadded_size(block) == 0)
88  return LZMA_DATA_ERROR;
89  } else {
90  block->compressed_size = LZMA_VLI_UNKNOWN;
91  }
92 
93  // Uncompressed Size
94  if (in[1] & 0x80)
95  return_if_error(lzma_vli_decode(&block->uncompressed_size,
96  NULL, in, &in_pos, in_size));
97  else
98  block->uncompressed_size = LZMA_VLI_UNKNOWN;
99 
100  // Filter Flags
101  const size_t filter_count = (in[1] & 3U) + 1;
102  for (size_t i = 0; i < filter_count; ++i) {
103  const lzma_ret ret = lzma_filter_flags_decode(
104  &block->filters[i], allocator,
105  in, &in_pos, in_size);
106  if (ret != LZMA_OK) {
107  free_properties(block, allocator);
108  return ret;
109  }
110  }
111 
112  // Padding
113  while (in_pos < in_size) {
114  if (in[in_pos++] != 0x00) {
115  free_properties(block, allocator);
116 
117  // Possibly some new field present so use
118  // LZMA_OPTIONS_ERROR instead of LZMA_DATA_ERROR.
119  return LZMA_OPTIONS_ERROR;
120  }
121  }
122 
123  return LZMA_OK;
124 }
#define LZMA_CHECK_ID_MAX
Maximum valid Check ID.
Definition: check.h:68
const lzma_allocator const uint8_t size_t * in_pos
Definition: block.h:579
const lzma_allocator const uint8_t size_t in_size
Definition: block.h:527
const lzma_allocator const uint8_t * in
Definition: block.h:527
#define lzma_block_header_size_decode(b)
Decode the Block Header Size field.
Definition: block.h:285
static void free_properties(lzma_block *block, const lzma_allocator *allocator)
#define return_if_error(expr)
Return if expression doesn't evaluate to LZMA_OK.
Definition: common.h:278
static uint32_t read32le(const uint8_t *buf)
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_OPTIONS_ERROR
Invalid or unsupported options.
Definition: base.h:160
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58

References allocator, lzma_block::check, lzma_block::compressed_size, lzma_block::filters, free_properties(), lzma_block::header_size, i, lzma_filter::id, lzma_block::ignore_check, in, in_pos, in_size, lzma_block_header_size_decode, LZMA_CHECK_ID_MAX, LZMA_DATA_ERROR, LZMA_FILTERS_MAX, LZMA_OK, LZMA_OPTIONS_ERROR, LZMA_PROG_ERROR, LZMA_VLI_UNKNOWN, NULL, lzma_filter::options, read32le(), return_if_error, lzma_block::uncompressed_size, and lzma_block::version.