3 * A simple example of pipe-only xz decompressor implementation.
4 * version: 2012-06-14 - by Daniel Mealha Cabrita
5 * Not copyrighted -- provided to the public domain.
8 * Link with liblzma. GCC example:
9 * $ gcc -llzma xz_pipe_decomp.c -o xz_pipe_decomp
12 * $ cat some_file.xz | ./xz_pipe_decomp > some_file
22 /* read/write buffer sizes */
23 #define IN_BUF_MAX 4096
24 #define OUT_BUF_MAX 4096
28 #define RET_ERROR_INIT 1
29 #define RET_ERROR_INPUT 2
30 #define RET_ERROR_OUTPUT 3
31 #define RET_ERROR_DECOMPRESSION 4
34 /* note: in_file and out_file must be open already */
35 int xz_decompress (FILE *in_file
, FILE *out_file
)
37 lzma_stream strm
= LZMA_STREAM_INIT
; /* alloc and init lzma_stream struct */
38 const uint32_t flags
= LZMA_TELL_UNSUPPORTED_CHECK
| LZMA_CONCATENATED
;
39 const uint64_t memory_limit
= UINT64_MAX
; /* no memory limit */
40 uint8_t in_buf
[IN_BUF_MAX
];
41 uint8_t out_buf
[OUT_BUF_MAX
];
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;
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
;
59 while ((! in_finished
) && (! out_finished
)) {
60 /* read incoming data */
61 in_len
= fread (in_buf
, 1, IN_BUF_MAX
, in_file
);
66 if (ferror (in_file
)) {
68 ret
= RET_ERROR_INPUT
;
71 strm
.next_in
= in_buf
;
72 strm
.avail_in
= in_len
;
74 /* if no more data from in_buf, flushes the
75 internal xz buffers and closes the decompressed data
77 action
= in_finished
? LZMA_FINISH
: LZMA_RUN
;
79 /* loop until there's no pending decompressed output */
81 /* out_buf is clean at this point */
82 strm
.next_out
= out_buf
;
83 strm
.avail_out
= OUT_BUF_MAX
;
86 ret_xz
= lzma_code (&strm
, action
);
88 if ((ret_xz
!= LZMA_OK
) && (ret_xz
!= LZMA_STREAM_END
)) {
89 fprintf (stderr
, "lzma_code error: %d\n", (int) ret_xz
);
91 ret
= RET_ERROR_DECOMPRESSION
;
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
)) {
98 ret
= RET_ERROR_OUTPUT
;
101 } while (strm
.avail_out
== 0);
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");
109 ret
= RET_ERROR_DECOMPRESSION
;
120 ret
= xz_decompress (stdin
, stdout
);