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

Encodes .lzma Stream with sizes known in Block Header. More...

#include "sysdefs.h"
#include "lzma.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/unistd.h>
#include <stdio.h>

Go to the source code of this file.

Macros

#define BUFFER_SIZE   (1U << 20)
 

Functions

int main (void)
 

Detailed Description

Encodes .lzma Stream with sizes known in Block Header.

The input file is encoded in RAM, and the known Compressed Size and/or Uncompressed Size values are stored in the Block Header. As of writing there's no such Stream encoder in liblzma.

Definition in file known_sizes.c.

Macro Definition Documentation

◆ BUFFER_SIZE

#define BUFFER_SIZE   (1U << 20)

Definition at line 28 of file known_sizes.c.

Function Documentation

◆ main()

int main ( void  )

Definition at line 32 of file known_sizes.c.

33 {
34  // Allocate the buffers.
37  if (in == NULL || out == NULL)
38  return 1;
39 
40  // Fill the input buffer.
41  const size_t in_size = fread(in, 1, BUFFER_SIZE, stdin);
42 
43  // Filter setup
45  if (lzma_lzma_preset(&opt_lzma, 1))
46  return 1;
47 
48  lzma_filter filters[] = {
49  {
51  .options = &opt_lzma
52  },
53  {
54  .id = LZMA_VLI_UNKNOWN
55  }
56  };
57 
58  lzma_block block = {
60  .compressed_size = BUFFER_SIZE, // Worst case reserve
61  .uncompressed_size = in_size,
62  .filters = filters,
63  };
64 
66  if (lzma_block_encoder(&strm, &block) != LZMA_OK)
67  return 1;
68 
69  // Reserve space for Stream Header and Block Header. We need to
70  // calculate the size of the Block Header first.
71  if (lzma_block_header_size(&block) != LZMA_OK)
72  return 1;
73 
74  size_t out_size = LZMA_STREAM_HEADER_SIZE + block.header_size;
75 
76  strm.next_in = in;
78  strm.next_out = out + out_size;
79  strm.avail_out = BUFFER_SIZE - out_size;
80 
81  if (lzma_code(&strm, LZMA_FINISH) != LZMA_STREAM_END)
82  return 1;
83 
84  out_size += strm.total_out;
85 
86  if (lzma_block_header_encode(&block, out + LZMA_STREAM_HEADER_SIZE)
87  != LZMA_OK)
88  return 1;
89 
90  lzma_index *idx = lzma_index_init(NULL);
91  if (idx == NULL)
92  return 1;
93 
94  if (lzma_index_append(idx, NULL, block.header_size + strm.total_out,
95  strm.total_in) != LZMA_OK)
96  return 1;
97 
98  if (lzma_index_encoder(&strm, idx) != LZMA_OK)
99  return 1;
100 
101  if (lzma_code(&strm, LZMA_RUN) != LZMA_STREAM_END)
102  return 1;
103 
104  out_size += strm.total_out;
105 
106  lzma_end(&strm);
107 
108  lzma_index_end(idx, NULL);
109 
110  // Encode the Stream Header and Stream Footer. backwards_size is
111  // needed only for the Stream Footer.
112  lzma_stream_flags sf = {
114  .check = block.check,
115  };
116 
117  if (lzma_stream_header_encode(&sf, out) != LZMA_OK)
118  return 1;
119 
120  if (lzma_stream_footer_encode(&sf, out + out_size) != LZMA_OK)
121  return 1;
122 
123  out_size += LZMA_STREAM_HEADER_SIZE;
124 
125  // Write out the file.
126  fwrite(out, 1, out_size, stdout);
127 
128  return 0;
129 }
@ LZMA_CHECK_CRC32
Definition: check.h:35
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
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
const lzma_filter * filters
Definition: container.h:315
#define NULL
Definition: cris-opc.c:27
static lzma_stream strm
Definition: full_flush.c:20
#define BUFFER_SIZE
Definition: known_sizes.c:28
void * malloc(size_t size)
Definition: malloc.c:123
#define LZMA_FILTER_LZMA2
LZMA2 Filter ID.
Definition: lzma12.h:40
int idx
Definition: setup.py:197
unsigned char uint8_t
Definition: sftypes.h:31
#define LZMA_STREAM_HEADER_SIZE
Size of Stream Header and Stream Footer.
Definition: stream_flags.h:27
Options for the Block and Block Header encoders and decoders.
Definition: block.h:30
uint32_t header_size
Size of the Block Header field.
Definition: block.h:72
lzma_check check
Type of integrity Check.
Definition: block.h:93
Filter options.
Definition: filter.h:43
lzma_vli id
Filter ID.
Definition: filter.h:54
Options specific to the LZMA1 and LZMA2 filters.
Definition: lzma12.h:185
Options for encoding/decoding Stream Header and Stream Footer.
Definition: stream_flags.h:33
lzma_vli backward_size
Backward Size.
Definition: stream_flags.h:69
Passing data to and from liblzma.
Definition: base.h:485
uint8_t * next_out
Definition: base.h:490
uint64_t total_in
Definition: base.h:488
size_t avail_out
Definition: base.h:491
const uint8_t * next_in
Definition: base.h:486
uint64_t total_out
Definition: base.h:492
size_t avail_in
Definition: base.h:487
static lzma_options_lzma opt_lzma
#define LZMA_VLI_UNKNOWN
VLI value to denote that the value is unknown.
Definition: vli.h:39
@ LZMA_STREAM_END
End of stream was reached.
Definition: base.h:63
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58
@ LZMA_FINISH
Finish the coding operation.
Definition: base.h:328
@ LZMA_RUN
Continue coding.
Definition: base.h:251
#define LZMA_STREAM_INIT
Initialization for lzma_stream.
Definition: base.h:545

References lzma_stream::avail_in, lzma_stream::avail_out, lzma_stream_flags::backward_size, BUFFER_SIZE, lzma_block::check, filters, lzma_block::header_size, lzma_filter::id, setup::idx, in, in_size, LZMA_CHECK_CRC32, LZMA_FILTER_LZMA2, LZMA_FINISH, LZMA_OK, LZMA_RUN, LZMA_STREAM_END, LZMA_STREAM_HEADER_SIZE, LZMA_STREAM_INIT, LZMA_VLI_UNKNOWN, malloc(), lzma_stream::next_in, lzma_stream::next_out, NULL, opt_lzma, out, strm, lzma_stream::total_in, and lzma_stream::total_out.