Rizin
unix-like reverse engineering framework and cli tools
xz_pipe_decomp.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 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_DECOMPRESSION   4
 

Functions

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

Macro Definition Documentation

◆ IN_BUF_MAX

#define IN_BUF_MAX   4096

Definition at line 23 of file xz_pipe_decomp.c.

◆ OUT_BUF_MAX

#define OUT_BUF_MAX   4096

Definition at line 24 of file xz_pipe_decomp.c.

◆ RET_ERROR_DECOMPRESSION

#define RET_ERROR_DECOMPRESSION   4

Definition at line 31 of file xz_pipe_decomp.c.

◆ RET_ERROR_INIT

#define RET_ERROR_INIT   1

Definition at line 28 of file xz_pipe_decomp.c.

◆ RET_ERROR_INPUT

#define RET_ERROR_INPUT   2

Definition at line 29 of file xz_pipe_decomp.c.

◆ RET_ERROR_OUTPUT

#define RET_ERROR_OUTPUT   3

Definition at line 30 of file xz_pipe_decomp.c.

◆ RET_OK

#define RET_OK   0

Definition at line 27 of file xz_pipe_decomp.c.

Function Documentation

◆ main()

int main ( void  )

Definition at line 116 of file xz_pipe_decomp.c.

117 {
118  int ret;
119 
120  ret = xz_decompress (stdin, stdout);
121  return ret;
122 }
int xz_decompress(FILE *in_file, FILE *out_file)

References xz_decompress().

◆ xz_decompress()

int xz_decompress ( FILE *  in_file,
FILE *  out_file 
)

Definition at line 35 of file xz_pipe_decomp.c.

36 {
37  lzma_stream strm = LZMA_STREAM_INIT; /* alloc and init lzma_stream struct */
39  const uint64_t memory_limit = UINT64_MAX; /* no memory limit */
42  size_t in_len; /* length of useful data in in_buf */
43  size_t out_len; /* length of useful data in out_buf */
44  bool in_finished = false;
45  bool out_finished = false;
47  lzma_ret ret_xz;
48  int ret;
49 
50  ret = RET_OK;
51 
52  /* initialize xz decoder */
53  ret_xz = lzma_stream_decoder (&strm, memory_limit, flags);
54  if (ret_xz != LZMA_OK) {
55  fprintf (stderr, "lzma_stream_decoder error: %d\n", (int) ret_xz);
56  return RET_ERROR_INIT;
57  }
58 
59  while ((! in_finished) && (! out_finished)) {
60  /* read incoming data */
61  in_len = fread (in_buf, 1, IN_BUF_MAX, in_file);
62 
63  if (feof (in_file)) {
64  in_finished = true;
65  }
66  if (ferror (in_file)) {
67  in_finished = true;
68  ret = RET_ERROR_INPUT;
69  }
70 
72  strm.avail_in = in_len;
73 
74  /* if no more data from in_buf, flushes the
75  internal xz buffers and closes the decompressed data
76  with LZMA_FINISH */
77  action = in_finished ? LZMA_FINISH : LZMA_RUN;
78 
79  /* loop until there's no pending decompressed output */
80  do {
81  /* out_buf is clean at this point */
84 
85  /* decompress data */
86  ret_xz = lzma_code (&strm, action);
87 
88  if ((ret_xz != LZMA_OK) && (ret_xz != LZMA_STREAM_END)) {
89  fprintf (stderr, "lzma_code error: %d\n", (int) ret_xz);
90  out_finished = true;
92  } else {
93  /* write decompressed data */
94  out_len = OUT_BUF_MAX - strm.avail_out;
95  fwrite (out_buf, 1, out_len, out_file);
96  if (ferror (out_file)) {
97  out_finished = true;
98  ret = RET_ERROR_OUTPUT;
99  }
100  }
101  } while (strm.avail_out == 0);
102  }
103 
104  /* Bug fix (2012-06-14): If no errors were detected, check
105  that the last lzma_code() call returned LZMA_STREAM_END.
106  If not, the file is probably truncated. */
107  if ((ret == RET_OK) && (ret_xz != LZMA_STREAM_END)) {
108  fprintf (stderr, "Input truncated or corrupt\n");
110  }
111 
112  lzma_end (&strm);
113  return ret;
114 }
static io_buf out_buf
Definition: coder.c:40
static io_buf in_buf
Input and output buffers.
Definition: coder.c:39
#define LZMA_CONCATENATED
Definition: container.h:515
#define LZMA_TELL_UNSUPPORTED_CHECK
Definition: container.h:466
static lzma_stream strm
Definition: full_flush.c:20
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
unsigned int uint32_t
Definition: sftypes.h:29
unsigned long uint64_t
Definition: sftypes.h:28
unsigned char uint8_t
Definition: sftypes.h:31
#define UINT64_MAX
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 RET_ERROR_DECOMPRESSION
#define RET_ERROR_OUTPUT
#define RET_ERROR_INIT
#define RET_OK
#define IN_BUF_MAX
#define OUT_BUF_MAX
#define RET_ERROR_INPUT

References test-lz4-speed::action, lzma_stream::avail_in, lzma_stream::avail_out, flags, in_buf, IN_BUF_MAX, LZMA_CONCATENATED, LZMA_FINISH, LZMA_OK, LZMA_RUN, LZMA_STREAM_END, LZMA_STREAM_INIT, LZMA_TELL_UNSUPPORTED_CHECK, lzma_stream::next_in, lzma_stream::next_out, out_buf, OUT_BUF_MAX, RET_ERROR_DECOMPRESSION, RET_ERROR_INIT, RET_ERROR_INPUT, RET_ERROR_OUTPUT, RET_OK, strm, and UINT64_MAX.

Referenced by main().