Rizin
unix-like reverse engineering framework and cli tools
block_header_encoder.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 "check.h"
15 
16 
18 lzma_block_header_size(lzma_block *block)
19 {
20  if (block->version > 1)
21  return LZMA_OPTIONS_ERROR;
22 
23  // Block Header Size + Block Flags + CRC32.
24  uint32_t size = 1 + 1 + 4;
25 
26  // Compressed Size
27  if (block->compressed_size != LZMA_VLI_UNKNOWN) {
28  const uint32_t add = lzma_vli_size(block->compressed_size);
29  if (add == 0 || block->compressed_size == 0)
30  return LZMA_PROG_ERROR;
31 
32  size += add;
33  }
34 
35  // Uncompressed Size
36  if (block->uncompressed_size != LZMA_VLI_UNKNOWN) {
37  const uint32_t add = lzma_vli_size(block->uncompressed_size);
38  if (add == 0)
39  return LZMA_PROG_ERROR;
40 
41  size += add;
42  }
43 
44  // List of Filter Flags
45  if (block->filters == NULL || block->filters[0].id == LZMA_VLI_UNKNOWN)
46  return LZMA_PROG_ERROR;
47 
48  for (size_t i = 0; block->filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
49  // Don't allow too many filters.
50  if (i == LZMA_FILTERS_MAX)
51  return LZMA_PROG_ERROR;
52 
53  uint32_t add;
54  return_if_error(lzma_filter_flags_size(&add,
55  block->filters + i));
56 
57  size += add;
58  }
59 
60  // Pad to a multiple of four bytes.
61  block->header_size = (size + 3) & ~UINT32_C(3);
62 
63  // NOTE: We don't verify that the encoded size of the Block stays
64  // within limits. This is because it is possible that we are called
65  // with exaggerated Compressed Size (e.g. LZMA_VLI_MAX) to reserve
66  // space for Block Header, and later called again with lower,
67  // real values.
68 
69  return LZMA_OK;
70 }
71 
72 
73 extern LZMA_API(lzma_ret)
74 lzma_block_header_encode(const lzma_block *block, uint8_t *out)
75 {
76  // Validate everything but filters.
77  if (lzma_block_unpadded_size(block) == 0
79  return LZMA_PROG_ERROR;
80 
81  // Indicate the size of the buffer _excluding_ the CRC32 field.
82  const size_t out_size = block->header_size - 4;
83 
84  // Store the Block Header Size.
85  out[0] = out_size / 4;
86 
87  // We write Block Flags in pieces.
88  out[1] = 0x00;
89  size_t out_pos = 2;
90 
91  // Compressed Size
92  if (block->compressed_size != LZMA_VLI_UNKNOWN) {
93  return_if_error(lzma_vli_encode(block->compressed_size, NULL,
94  out, &out_pos, out_size));
95 
96  out[1] |= 0x40;
97  }
98 
99  // Uncompressed Size
100  if (block->uncompressed_size != LZMA_VLI_UNKNOWN) {
101  return_if_error(lzma_vli_encode(block->uncompressed_size, NULL,
102  out, &out_pos, out_size));
103 
104  out[1] |= 0x80;
105  }
106 
107  // Filter Flags
108  if (block->filters == NULL || block->filters[0].id == LZMA_VLI_UNKNOWN)
109  return LZMA_PROG_ERROR;
110 
111  size_t filter_count = 0;
112  do {
113  // There can be a maximum of four filters.
114  if (filter_count == LZMA_FILTERS_MAX)
115  return LZMA_PROG_ERROR;
116 
117  return_if_error(lzma_filter_flags_encode(
118  block->filters + filter_count,
119  out, &out_pos, out_size));
120 
121  } while (block->filters[++filter_count].id != LZMA_VLI_UNKNOWN);
122 
123  out[1] |= filter_count - 1;
124 
125  // Padding
126  memzero(out + out_pos, out_size - out_pos);
127 
128  // CRC32
129  write32le(out + out_size, lzma_crc32(out, out_size, 0));
130 
131  return LZMA_OK;
132 }
lzma_index ** i
Definition: index.h:629
const lzma_allocator const uint8_t size_t uint8_t size_t * out_pos
Definition: block.h:528
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
LZMA_API(lzma_ret)
#define NULL
Definition: cris-opc.c:27
#define LZMA_FILTERS_MAX
Maximum number of filters in a chain.
Definition: filter.h:26
voidpf void uLong size
Definition: ioapi.h:138
unsigned int uint32_t
Definition: sftypes.h:29
unsigned char uint8_t
Definition: sftypes.h:31
#define UINT32_C(val)
Options for the Block and Block Header encoders and decoders.
Definition: block.h:30
lzma_vli uncompressed_size
Uncompressed Size in bytes.
Definition: block.h:172
lzma_filter * filters
Array of filters.
Definition: block.h:200
uint32_t header_size
Size of the Block Header field.
Definition: block.h:72
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
lzma_vli id
Filter ID.
Definition: filter.h:54
Definitions common to the whole liblzma library.
#define return_if_error(expr)
Return if expression doesn't evaluate to LZMA_OK.
Definition: common.h:278
#define memzero(s, n)
Definition: sysdefs.h:180
#define write32le(buf, num)
#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_OPTIONS_ERROR
Invalid or unsupported options.
Definition: base.h:160
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58
static int add(char *argv[])
Definition: ziptool.c:84