Rizin
unix-like reverse engineering framework and cli tools
xz_pipe_comp.c File Reference
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdbool.h>
#include <lzma.h>

Go to the source code of this file.

Macros

#define COMPRESSION_LEVEL   6
 
#define COMPRESSION_EXTREME   true
 
#define INTEGRITY_CHECK   LZMA_CHECK_CRC64
 
#define IN_BUF_MAX   4096
 
#define OUT_BUF_MAX   4096
 
#define RET_OK   0
 
#define RET_ERROR_INIT   1
 
#define RET_ERROR_INPUT   2
 
#define RET_ERROR_OUTPUT   3
 
#define RET_ERROR_COMPRESSION   4
 

Functions

int xz_compress (FILE *in_file, FILE *out_file)
 
int main ()
 

Macro Definition Documentation

◆ COMPRESSION_EXTREME

#define COMPRESSION_EXTREME   true

Definition at line 28 of file xz_pipe_comp.c.

◆ COMPRESSION_LEVEL

#define COMPRESSION_LEVEL   6

Definition at line 25 of file xz_pipe_comp.c.

◆ IN_BUF_MAX

#define IN_BUF_MAX   4096

Definition at line 35 of file xz_pipe_comp.c.

◆ INTEGRITY_CHECK

#define INTEGRITY_CHECK   LZMA_CHECK_CRC64

Definition at line 31 of file xz_pipe_comp.c.

◆ OUT_BUF_MAX

#define OUT_BUF_MAX   4096

Definition at line 36 of file xz_pipe_comp.c.

◆ RET_ERROR_COMPRESSION

#define RET_ERROR_COMPRESSION   4

Definition at line 43 of file xz_pipe_comp.c.

◆ RET_ERROR_INIT

#define RET_ERROR_INIT   1

Definition at line 40 of file xz_pipe_comp.c.

◆ RET_ERROR_INPUT

#define RET_ERROR_INPUT   2

Definition at line 41 of file xz_pipe_comp.c.

◆ RET_ERROR_OUTPUT

#define RET_ERROR_OUTPUT   3

Definition at line 42 of file xz_pipe_comp.c.

◆ RET_OK

#define RET_OK   0

Definition at line 39 of file xz_pipe_comp.c.

Function Documentation

◆ main()

int main ( void  )

Definition at line 120 of file xz_pipe_comp.c.

121 {
122  int ret;
123 
124  ret = xz_compress (stdin, stdout);
125  return ret;
126 }
int xz_compress(FILE *in_file, FILE *out_file)
Definition: xz_pipe_comp.c:47

References xz_compress().

◆ xz_compress()

int xz_compress ( FILE *  in_file,
FILE *  out_file 
)

Definition at line 47 of file xz_pipe_comp.c.

48 {
51  lzma_stream strm = LZMA_STREAM_INIT; /* alloc and init lzma_stream struct */
54  size_t in_len; /* length of useful data in in_buf */
55  size_t out_len; /* length of useful data in out_buf */
56  bool in_finished = false;
57  bool out_finished = false;
59  lzma_ret ret_xz;
60  int ret;
61 
62  ret = RET_OK;
63 
64  /* initialize xz encoder */
65  ret_xz = lzma_easy_encoder (&strm, preset, check);
66  if (ret_xz != LZMA_OK) {
67  fprintf (stderr, "lzma_easy_encoder error: %d\n", (int) ret_xz);
68  return RET_ERROR_INIT;
69  }
70 
71  while ((! in_finished) && (! out_finished)) {
72  /* read incoming data */
73  in_len = fread (in_buf, 1, IN_BUF_MAX, in_file);
74 
75  if (feof (in_file)) {
76  in_finished = true;
77  }
78  if (ferror (in_file)) {
79  in_finished = true;
80  ret = RET_ERROR_INPUT;
81  }
82 
84  strm.avail_in = in_len;
85 
86  /* if no more data from in_buf, flushes the
87  internal xz buffers and closes the xz data
88  with LZMA_FINISH */
89  action = in_finished ? LZMA_FINISH : LZMA_RUN;
90 
91  /* loop until there's no pending compressed output */
92  do {
93  /* out_buf is clean at this point */
96 
97  /* compress data */
98  ret_xz = lzma_code (&strm, action);
99 
100  if ((ret_xz != LZMA_OK) && (ret_xz != LZMA_STREAM_END)) {
101  fprintf (stderr, "lzma_code error: %d\n", (int) ret_xz);
102  out_finished = true;
103  ret = RET_ERROR_COMPRESSION;
104  } else {
105  /* write compressed data */
106  out_len = OUT_BUF_MAX - strm.avail_out;
107  fwrite (out_buf, 1, out_len, out_file);
108  if (ferror (out_file)) {
109  out_finished = true;
110  ret = RET_ERROR_OUTPUT;
111  }
112  }
113  } while (strm.avail_out == 0);
114  }
115 
116  lzma_end (&strm);
117  return ret;
118 }
lzma_check
Type of the integrity check (Check ID)
Definition: check.h:27
static io_buf out_buf
Definition: coder.c:40
static io_buf in_buf
Input and output buffers.
Definition: coder.c:39
uint32_t preset
Definition: container.h:259
lzma_check check
Definition: container.h:292
#define LZMA_PRESET_EXTREME
Extreme compression preset.
Definition: container.h:60
static lzma_stream strm
Definition: full_flush.c:20
unsigned int uint32_t
Definition: sftypes.h:29
unsigned char uint8_t
Definition: sftypes.h:31
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
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
#define LZMA_STREAM_INIT
Initialization for lzma_stream.
Definition: base.h:545
#define INTEGRITY_CHECK
Definition: xz_pipe_comp.c:31
#define COMPRESSION_LEVEL
Definition: xz_pipe_comp.c:25
#define RET_ERROR_OUTPUT
Definition: xz_pipe_comp.c:42
#define RET_ERROR_INIT
Definition: xz_pipe_comp.c:40
#define RET_OK
Definition: xz_pipe_comp.c:39
#define COMPRESSION_EXTREME
Definition: xz_pipe_comp.c:28
#define IN_BUF_MAX
Definition: xz_pipe_comp.c:35
#define OUT_BUF_MAX
Definition: xz_pipe_comp.c:36
#define RET_ERROR_INPUT
Definition: xz_pipe_comp.c:41
#define RET_ERROR_COMPRESSION
Definition: xz_pipe_comp.c:43

References test-lz4-speed::action, lzma_stream::avail_in, lzma_stream::avail_out, check, COMPRESSION_EXTREME, COMPRESSION_LEVEL, in_buf, IN_BUF_MAX, INTEGRITY_CHECK, LZMA_FINISH, LZMA_OK, LZMA_PRESET_EXTREME, LZMA_RUN, LZMA_STREAM_END, LZMA_STREAM_INIT, lzma_stream::next_in, lzma_stream::next_out, out_buf, OUT_BUF_MAX, preset, RET_ERROR_COMPRESSION, RET_ERROR_INIT, RET_ERROR_INPUT, RET_ERROR_OUTPUT, RET_OK, and strm.

Referenced by main().