Rizin
unix-like reverse engineering framework and cli tools
full_flush.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 "sysdefs.h"
14 #include "lzma.h"
15 #include <stdio.h>
16 
17 #define CHUNK 64
18 
19 
21 static FILE *file_in;
22 
23 
24 static void
26 {
27  uint8_t in[CHUNK];
28  uint8_t out[CHUNK];
29  lzma_ret ret;
30 
31  do {
32  if (strm.avail_in == 0 && size > 0) {
33  const size_t amount = my_min(size, CHUNK);
34  strm.avail_in = fread(in, 1, amount, file_in);
35  strm.next_in = in;
36  size -= amount; // Intentionally not using avail_in.
37  }
38 
39  strm.next_out = out;
41 
42  ret = lzma_code(&strm, size == 0 ? action : LZMA_RUN);
43 
44  if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
45  fprintf(stderr, "%s:%u: %s: ret == %d\n",
46  __FILE__, __LINE__, __func__, ret);
47  exit(1);
48  }
49 
50  fwrite(out, 1, CHUNK - strm.avail_out, stdout);
51 
52  } while (size > 0 || strm.avail_out == 0);
53 
54  if ((action == LZMA_RUN && ret != LZMA_OK)
55  || (action != LZMA_RUN && ret != LZMA_STREAM_END)) {
56  fprintf(stderr, "%s:%u: %s: ret == %d\n",
57  __FILE__, __LINE__, __func__, ret);
58  exit(1);
59  }
60 }
61 
62 
63 int
64 main(int argc, char **argv)
65 {
66  file_in = argc > 1 ? fopen(argv[1], "rb") : stdin;
67 
68 
69  // Config
71  if (lzma_lzma_preset(&opt_lzma, 1)) {
72  fprintf(stderr, "preset failed\n");
73  exit(1);
74  }
77  filters[0].options = &opt_lzma;
79 
80  // Init
81  if (lzma_stream_encoder(&strm, filters, LZMA_CHECK_CRC32) != LZMA_OK) {
82  fprintf(stderr, "init failed\n");
83  exit(1);
84  }
85 
86 // if (lzma_easy_encoder(&strm, 1)) {
87 // fprintf(stderr, "init failed\n");
88 // exit(1);
89 // }
90 
91  // Encoding
97  encode(0, LZMA_FINISH);
98 
99  // Clean up
100  lzma_end(&strm);
101 
102  return 0;
103 }
@ LZMA_CHECK_CRC32
Definition: check.h:35
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 LZMA_FILTERS_MAX
Maximum number of filters in a chain.
Definition: filter.h:26
static lzma_stream strm
Definition: full_flush.c:20
static FILE * file_in
Definition: full_flush.c:21
#define CHUNK
Definition: full_flush.c:17
int main(int argc, char **argv)
Definition: full_flush.c:64
static void encode(size_t size, lzma_action action)
Definition: full_flush.c:25
voidpf void uLong size
Definition: ioapi.h:138
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
#define LZMA_FILTER_LZMA2
LZMA2 Filter ID.
Definition: lzma12.h:40
The public API of liblzma data compression library.
string FILE
Definition: benchmark.py:21
unsigned char uint8_t
Definition: sftypes.h:31
Filter options.
Definition: filter.h:43
void * options
Pointer to filter-specific options structure.
Definition: filter.h:63
lzma_vli id
Filter ID.
Definition: filter.h:54
Options specific to the LZMA1 and LZMA2 filters.
Definition: lzma12.h:185
Passing data to and from liblzma.
Definition: base.h:485
uint8_t * next_out
Definition: base.h:490
size_t avail_out
Definition: base.h:491
const uint8_t * next_in
Definition: base.h:486
size_t avail_in
Definition: base.h:487
Common includes, definitions, system-specific things etc.
#define my_min(x, y)
Definition: sysdefs.h:185
static lzma_options_lzma opt_lzma
#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_STREAM_END
End of stream was reached.
Definition: base.h:63
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58
lzma_action
The ‘action’ argument for lzma_code()
Definition: base.h:250
@ LZMA_FINISH
Finish the coding operation.
Definition: base.h:328
@ LZMA_RUN
Continue coding.
Definition: base.h:251
@ LZMA_FULL_FLUSH
Finish encoding of the current Block.
Definition: base.h:290
#define LZMA_STREAM_INIT
Initialization for lzma_stream.
Definition: base.h:545