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

Encode files using LZMA_SYNC_FLUSH. More...

#include "sysdefs.h"
#include "lzma.h"
#include <stdio.h>

Go to the source code of this file.

Macros

#define CHUNK   64
 

Functions

static void encode (size_t size, lzma_action action)
 
int main (int argc, char **argv)
 

Variables

static lzma_stream strm = LZMA_STREAM_INIT
 
static FILE * file_in
 

Detailed Description

Encode files using LZMA_SYNC_FLUSH.

Definition in file sync_flush.c.

Macro Definition Documentation

◆ CHUNK

#define CHUNK   64

Definition at line 17 of file sync_flush.c.

Function Documentation

◆ encode()

static void encode ( size_t  size,
lzma_action  action 
)
static

Definition at line 25 of file sync_flush.c.

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 }
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
voidpf void uLong size
Definition: ioapi.h:138
unsigned char uint8_t
Definition: sftypes.h:31
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
static lzma_stream strm
Definition: sync_flush.c:20
static FILE * file_in
Definition: sync_flush.c:21
#define CHUNK
Definition: sync_flush.c:17
#define my_min(x, y)
Definition: sysdefs.h:185
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_RUN
Continue coding.
Definition: base.h:251

References test-lz4-speed::action, lzma_stream::avail_in, lzma_stream::avail_out, CHUNK, test-lz4-list::exit, file_in, in, LZMA_OK, LZMA_RUN, LZMA_STREAM_END, my_min, lzma_stream::next_in, lzma_stream::next_out, out, and strm.

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 64 of file sync_flush.c.

65 {
66  file_in = argc > 1 ? fopen(argv[1], "rb") : stdin;
67 
68  // Config
70  .dict_size = 1U << 16,
71  .lc = LZMA_LC_DEFAULT,
72  .lp = LZMA_LP_DEFAULT,
73  .pb = LZMA_PB_DEFAULT,
74  .preset_dict = NULL,
75  .mode = LZMA_MODE_NORMAL,
76  .nice_len = 32,
77  .mf = LZMA_MF_HC3,
78  .depth = 0,
79  };
80 
81  lzma_options_delta opt_delta = {
82  .dist = 16
83  };
84 
87  filters[0].options = &opt_lzma;
89 
90  // Init
91  if (lzma_stream_encoder(&strm, filters, LZMA_CHECK_CRC32) != LZMA_OK) {
92  fprintf(stderr, "init failed\n");
93  exit(1);
94  }
95 
96  // Encoding
102  encode(0, LZMA_FINISH);
103 
104 /*
105  encode(53, LZMA_SYNC_FLUSH);
106  opt_lzma.lc = 2;
107  opt_lzma.lp = 1;
108  opt_lzma.pb = 0;
109  if (lzma_filters_update(&strm, filters) != LZMA_OK) {
110  fprintf(stderr, "update failed\n");
111  exit(1);
112  }
113  encode(404, LZMA_FINISH);
114 */
115 
116  // Clean up
117  lzma_end(&strm);
118 
119  return 0;
120 
121  // Prevent useless warnings so we don't need to have special CFLAGS
122  // to disable -Werror.
123  (void)opt_lzma;
124  (void)opt_delta;
125 }
@ LZMA_CHECK_CRC32
Definition: check.h:35
const lzma_filter * filters
Definition: container.h:315
#define NULL
Definition: cris-opc.c:27
#define LZMA_FILTERS_MAX
Maximum number of filters in a chain.
Definition: filter.h:26
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
@ LZMA_MODE_NORMAL
Normal compression.
Definition: lzma12.h:147
#define LZMA_PB_DEFAULT
Definition: lzma12.h:319
@ LZMA_MF_HC3
Hash Chain with 2- and 3-byte hashing.
Definition: lzma12.h:59
#define LZMA_LC_DEFAULT
Definition: lzma12.h:284
#define LZMA_LP_DEFAULT
Definition: lzma12.h:294
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 for the Delta filter.
Definition: delta.h:45
uint32_t dist
Delta distance.
Definition: delta.h:59
Options specific to the LZMA1 and LZMA2 filters.
Definition: lzma12.h:185
uint32_t dict_size
Dictionary size in bytes.
Definition: lzma12.h:217
static void encode(size_t size, lzma_action action)
Definition: sync_flush.c:25
static lzma_options_lzma opt_lzma
#define LZMA_VLI_UNKNOWN
VLI value to denote that the value is unknown.
Definition: vli.h:39
@ LZMA_SYNC_FLUSH
Make all the input available at output.
Definition: base.h:265
@ LZMA_FINISH
Finish the coding operation.
Definition: base.h:328

References argv, lzma_options_lzma::dict_size, lzma_options_delta::dist, encode(), test-lz4-list::exit, file_in, filters, lzma_filter::id, LZMA_CHECK_CRC32, LZMA_FILTER_LZMA2, LZMA_FILTERS_MAX, LZMA_FINISH, LZMA_LC_DEFAULT, LZMA_LP_DEFAULT, LZMA_MF_HC3, LZMA_MODE_NORMAL, LZMA_OK, LZMA_PB_DEFAULT, LZMA_SYNC_FLUSH, LZMA_VLI_UNKNOWN, NULL, opt_lzma, lzma_filter::options, and strm.

Variable Documentation

◆ file_in

FILE* file_in
static

Definition at line 21 of file sync_flush.c.

Referenced by encode(), and main().

◆ strm

Definition at line 20 of file sync_flush.c.

Referenced by encode(), and main().