3 * A simple example of pipe-only xz compressor implementation.
4 * version: 2010-07-12 - by Daniel Mealha Cabrita
5 * Not copyrighted -- provided to the public domain.
8 * Link with liblzma. GCC example:
9 * $ gcc -llzma xz_pipe_comp.c -o xz_pipe_comp
12 * $ cat some_file | ./xz_pipe_comp > some_file.xz
22 /* COMPRESSION SETTINGS */
24 /* analogous to xz CLI options: -0 to -9 */
25 #define COMPRESSION_LEVEL 6
27 /* boolean setting, analogous to xz CLI option: -e */
28 #define COMPRESSION_EXTREME true
30 /* see: /usr/include/lzma/check.h LZMA_CHECK_* */
31 #define INTEGRITY_CHECK LZMA_CHECK_CRC64
34 /* read/write buffer sizes */
35 #define IN_BUF_MAX 4096
36 #define OUT_BUF_MAX 4096
40 #define RET_ERROR_INIT 1
41 #define RET_ERROR_INPUT 2
42 #define RET_ERROR_OUTPUT 3
43 #define RET_ERROR_COMPRESSION 4
46 /* note: in_file and out_file must be open already */
47 int xz_compress (FILE *in_file
, FILE *out_file
)
49 uint32_t preset
= COMPRESSION_LEVEL
| (COMPRESSION_EXTREME
? LZMA_PRESET_EXTREME
: 0);
50 lzma_check check
= INTEGRITY_CHECK
;
51 lzma_stream strm
= LZMA_STREAM_INIT
; /* alloc and init lzma_stream struct */
52 uint8_t in_buf
[IN_BUF_MAX
];
53 uint8_t out_buf
[OUT_BUF_MAX
];
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;
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
;
71 while ((! in_finished
) && (! out_finished
)) {
72 /* read incoming data */
73 in_len
= fread (in_buf
, 1, IN_BUF_MAX
, in_file
);
78 if (ferror (in_file
)) {
80 ret
= RET_ERROR_INPUT
;
83 strm
.next_in
= in_buf
;
84 strm
.avail_in
= in_len
;
86 /* if no more data from in_buf, flushes the
87 internal xz buffers and closes the xz data
89 action
= in_finished
? LZMA_FINISH
: LZMA_RUN
;
91 /* loop until there's no pending compressed output */
93 /* out_buf is clean at this point */
94 strm
.next_out
= out_buf
;
95 strm
.avail_out
= OUT_BUF_MAX
;
98 ret_xz
= lzma_code (&strm
, action
);
100 if ((ret_xz
!= LZMA_OK
) && (ret_xz
!= LZMA_STREAM_END
)) {
101 fprintf (stderr
, "lzma_code error: %d\n", (int) ret_xz
);
103 ret
= RET_ERROR_COMPRESSION
;
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
)) {
110 ret
= RET_ERROR_OUTPUT
;
113 } while (strm
.avail_out
== 0);
124 ret
= xz_compress (stdin
, stdout
);