1880b10faSchristos ///////////////////////////////////////////////////////////////////////////////
2880b10faSchristos //
3880b10faSchristos /// \file 02_decompress.c
4880b10faSchristos /// \brief Decompress .xz files to stdout
5880b10faSchristos ///
6880b10faSchristos /// Usage: ./02_decompress INPUT_FILES... > OUTFILE
7880b10faSchristos ///
8880b10faSchristos /// Example: ./02_decompress foo.xz bar.xz > foobar
9880b10faSchristos //
10880b10faSchristos // Author: Lasse Collin
11880b10faSchristos //
12880b10faSchristos // This file has been put into the public domain.
13880b10faSchristos // You can do whatever you want with this file.
14880b10faSchristos //
15880b10faSchristos ///////////////////////////////////////////////////////////////////////////////
16880b10faSchristos
17880b10faSchristos #include <stdbool.h>
18880b10faSchristos #include <stdlib.h>
19880b10faSchristos #include <stdio.h>
20880b10faSchristos #include <string.h>
21880b10faSchristos #include <errno.h>
22880b10faSchristos #include <lzma.h>
23880b10faSchristos
24880b10faSchristos
25880b10faSchristos static bool
init_decoder(lzma_stream * strm)26880b10faSchristos init_decoder(lzma_stream *strm)
27880b10faSchristos {
28880b10faSchristos // Initialize a .xz decoder. The decoder supports a memory usage limit
29880b10faSchristos // and a set of flags.
30880b10faSchristos //
31880b10faSchristos // The memory usage of the decompressor depends on the settings used
32880b10faSchristos // to compress a .xz file. It can vary from less than a megabyte to
33880b10faSchristos // a few gigabytes, but in practice (at least for now) it rarely
34880b10faSchristos // exceeds 65 MiB because that's how much memory is required to
35880b10faSchristos // decompress files created with "xz -9". Settings requiring more
36880b10faSchristos // memory take extra effort to use and don't (at least for now)
37880b10faSchristos // provide significantly better compression in most cases.
38880b10faSchristos //
39880b10faSchristos // Memory usage limit is useful if it is important that the
40880b10faSchristos // decompressor won't consume gigabytes of memory. The need
41880b10faSchristos // for limiting depends on the application. In this example,
42880b10faSchristos // no memory usage limiting is used. This is done by setting
43880b10faSchristos // the limit to UINT64_MAX.
44880b10faSchristos //
45880b10faSchristos // The .xz format allows concatenating compressed files as is:
46880b10faSchristos //
47880b10faSchristos // echo foo | xz > foobar.xz
48880b10faSchristos // echo bar | xz >> foobar.xz
49880b10faSchristos //
50880b10faSchristos // When decompressing normal standalone .xz files, LZMA_CONCATENATED
51880b10faSchristos // should always be used to support decompression of concatenated
52880b10faSchristos // .xz files. If LZMA_CONCATENATED isn't used, the decoder will stop
53880b10faSchristos // after the first .xz stream. This can be useful when .xz data has
54880b10faSchristos // been embedded inside another file format.
55880b10faSchristos //
56880b10faSchristos // Flags other than LZMA_CONCATENATED are supported too, and can
57880b10faSchristos // be combined with bitwise-or. See lzma/container.h
58880b10faSchristos // (src/liblzma/api/lzma/container.h in the source package or e.g.
59880b10faSchristos // /usr/include/lzma/container.h depending on the install prefix)
60880b10faSchristos // for details.
61880b10faSchristos lzma_ret ret = lzma_stream_decoder(
62880b10faSchristos strm, UINT64_MAX, LZMA_CONCATENATED);
63880b10faSchristos
64880b10faSchristos // Return successfully if the initialization went fine.
65880b10faSchristos if (ret == LZMA_OK)
66880b10faSchristos return true;
67880b10faSchristos
68880b10faSchristos // Something went wrong. The possible errors are documented in
69880b10faSchristos // lzma/container.h (src/liblzma/api/lzma/container.h in the source
70880b10faSchristos // package or e.g. /usr/include/lzma/container.h depending on the
71880b10faSchristos // install prefix).
72880b10faSchristos //
73880b10faSchristos // Note that LZMA_MEMLIMIT_ERROR is never possible here. If you
74880b10faSchristos // specify a very tiny limit, the error will be delayed until
75880b10faSchristos // the first headers have been parsed by a call to lzma_code().
76880b10faSchristos const char *msg;
77880b10faSchristos switch (ret) {
78880b10faSchristos case LZMA_MEM_ERROR:
79880b10faSchristos msg = "Memory allocation failed";
80880b10faSchristos break;
81880b10faSchristos
82880b10faSchristos case LZMA_OPTIONS_ERROR:
83880b10faSchristos msg = "Unsupported decompressor flags";
84880b10faSchristos break;
85880b10faSchristos
86880b10faSchristos default:
87880b10faSchristos // This is most likely LZMA_PROG_ERROR indicating a bug in
88880b10faSchristos // this program or in liblzma. It is inconvenient to have a
89880b10faSchristos // separate error message for errors that should be impossible
90880b10faSchristos // to occur, but knowing the error code is important for
91880b10faSchristos // debugging. That's why it is good to print the error code
92880b10faSchristos // at least when there is no good error message to show.
93880b10faSchristos msg = "Unknown error, possibly a bug";
94880b10faSchristos break;
95880b10faSchristos }
96880b10faSchristos
97880b10faSchristos fprintf(stderr, "Error initializing the decoder: %s (error code %u)\n",
98880b10faSchristos msg, ret);
99880b10faSchristos return false;
100880b10faSchristos }
101880b10faSchristos
102880b10faSchristos
103880b10faSchristos static bool
decompress(lzma_stream * strm,const char * inname,FILE * infile,FILE * outfile)104880b10faSchristos decompress(lzma_stream *strm, const char *inname, FILE *infile, FILE *outfile)
105880b10faSchristos {
106880b10faSchristos // When LZMA_CONCATENATED flag was used when initializing the decoder,
107880b10faSchristos // we need to tell lzma_code() when there will be no more input.
108880b10faSchristos // This is done by setting action to LZMA_FINISH instead of LZMA_RUN
109880b10faSchristos // in the same way as it is done when encoding.
110880b10faSchristos //
111880b10faSchristos // When LZMA_CONCATENATED isn't used, there is no need to use
112880b10faSchristos // LZMA_FINISH to tell when all the input has been read, but it
113880b10faSchristos // is still OK to use it if you want. When LZMA_CONCATENATED isn't
114880b10faSchristos // used, the decoder will stop after the first .xz stream. In that
115880b10faSchristos // case some unused data may be left in strm->next_in.
116880b10faSchristos lzma_action action = LZMA_RUN;
117880b10faSchristos
118880b10faSchristos uint8_t inbuf[BUFSIZ];
119880b10faSchristos uint8_t outbuf[BUFSIZ];
120880b10faSchristos
121880b10faSchristos strm->next_in = NULL;
122880b10faSchristos strm->avail_in = 0;
123880b10faSchristos strm->next_out = outbuf;
124880b10faSchristos strm->avail_out = sizeof(outbuf);
125880b10faSchristos
126880b10faSchristos while (true) {
127880b10faSchristos if (strm->avail_in == 0 && !feof(infile)) {
128880b10faSchristos strm->next_in = inbuf;
129880b10faSchristos strm->avail_in = fread(inbuf, 1, sizeof(inbuf),
130880b10faSchristos infile);
131880b10faSchristos
132880b10faSchristos if (ferror(infile)) {
133880b10faSchristos fprintf(stderr, "%s: Read error: %s\n",
134880b10faSchristos inname, strerror(errno));
135880b10faSchristos return false;
136880b10faSchristos }
137880b10faSchristos
138880b10faSchristos // Once the end of the input file has been reached,
139880b10faSchristos // we need to tell lzma_code() that no more input
140880b10faSchristos // will be coming. As said before, this isn't required
141*75cbb3d6Sjoerg // if the LZMA_CONCATENATED flag isn't used when
142880b10faSchristos // initializing the decoder.
143880b10faSchristos if (feof(infile))
144880b10faSchristos action = LZMA_FINISH;
145880b10faSchristos }
146880b10faSchristos
147880b10faSchristos lzma_ret ret = lzma_code(strm, action);
148880b10faSchristos
149880b10faSchristos if (strm->avail_out == 0 || ret == LZMA_STREAM_END) {
150880b10faSchristos size_t write_size = sizeof(outbuf) - strm->avail_out;
151880b10faSchristos
152880b10faSchristos if (fwrite(outbuf, 1, write_size, outfile)
153880b10faSchristos != write_size) {
154880b10faSchristos fprintf(stderr, "Write error: %s\n",
155880b10faSchristos strerror(errno));
156880b10faSchristos return false;
157880b10faSchristos }
158880b10faSchristos
159880b10faSchristos strm->next_out = outbuf;
160880b10faSchristos strm->avail_out = sizeof(outbuf);
161880b10faSchristos }
162880b10faSchristos
163880b10faSchristos if (ret != LZMA_OK) {
164880b10faSchristos // Once everything has been decoded successfully, the
165880b10faSchristos // return value of lzma_code() will be LZMA_STREAM_END.
166880b10faSchristos //
167880b10faSchristos // It is important to check for LZMA_STREAM_END. Do not
168880b10faSchristos // assume that getting ret != LZMA_OK would mean that
169880b10faSchristos // everything has gone well or that when you aren't
170880b10faSchristos // getting more output it must have successfully
171880b10faSchristos // decoded everything.
172880b10faSchristos if (ret == LZMA_STREAM_END)
173880b10faSchristos return true;
174880b10faSchristos
175880b10faSchristos // It's not LZMA_OK nor LZMA_STREAM_END,
176880b10faSchristos // so it must be an error code. See lzma/base.h
177880b10faSchristos // (src/liblzma/api/lzma/base.h in the source package
178880b10faSchristos // or e.g. /usr/include/lzma/base.h depending on the
179880b10faSchristos // install prefix) for the list and documentation of
180880b10faSchristos // possible values. Many values listen in lzma_ret
181880b10faSchristos // enumeration aren't possible in this example, but
182880b10faSchristos // can be made possible by enabling memory usage limit
183880b10faSchristos // or adding flags to the decoder initialization.
184880b10faSchristos const char *msg;
185880b10faSchristos switch (ret) {
186880b10faSchristos case LZMA_MEM_ERROR:
187880b10faSchristos msg = "Memory allocation failed";
188880b10faSchristos break;
189880b10faSchristos
190880b10faSchristos case LZMA_FORMAT_ERROR:
191880b10faSchristos // .xz magic bytes weren't found.
192880b10faSchristos msg = "The input is not in the .xz format";
193880b10faSchristos break;
194880b10faSchristos
195880b10faSchristos case LZMA_OPTIONS_ERROR:
196880b10faSchristos // For example, the headers specify a filter
197880b10faSchristos // that isn't supported by this liblzma
198880b10faSchristos // version (or it hasn't been enabled when
199880b10faSchristos // building liblzma, but no-one sane does
200880b10faSchristos // that unless building liblzma for an
201880b10faSchristos // embedded system). Upgrading to a newer
202880b10faSchristos // liblzma might help.
203880b10faSchristos //
204880b10faSchristos // Note that it is unlikely that the file has
205880b10faSchristos // accidentally became corrupt if you get this
206880b10faSchristos // error. The integrity of the .xz headers is
207880b10faSchristos // always verified with a CRC32, so
208880b10faSchristos // unintentionally corrupt files can be
209880b10faSchristos // distinguished from unsupported files.
210880b10faSchristos msg = "Unsupported compression options";
211880b10faSchristos break;
212880b10faSchristos
213880b10faSchristos case LZMA_DATA_ERROR:
214880b10faSchristos msg = "Compressed file is corrupt";
215880b10faSchristos break;
216880b10faSchristos
217880b10faSchristos case LZMA_BUF_ERROR:
218880b10faSchristos // Typically this error means that a valid
219880b10faSchristos // file has got truncated, but it might also
220880b10faSchristos // be a damaged part in the file that makes
221880b10faSchristos // the decoder think the file is truncated.
222880b10faSchristos // If you prefer, you can use the same error
223880b10faSchristos // message for this as for LZMA_DATA_ERROR.
224880b10faSchristos msg = "Compressed file is truncated or "
225880b10faSchristos "otherwise corrupt";
226880b10faSchristos break;
227880b10faSchristos
228880b10faSchristos default:
229880b10faSchristos // This is most likely LZMA_PROG_ERROR.
230880b10faSchristos msg = "Unknown error, possibly a bug";
231880b10faSchristos break;
232880b10faSchristos }
233880b10faSchristos
234880b10faSchristos fprintf(stderr, "%s: Decoder error: "
235880b10faSchristos "%s (error code %u)\n",
236880b10faSchristos inname, msg, ret);
237880b10faSchristos return false;
238880b10faSchristos }
239880b10faSchristos }
240880b10faSchristos }
241880b10faSchristos
242880b10faSchristos
243880b10faSchristos extern int
main(int argc,char ** argv)244880b10faSchristos main(int argc, char **argv)
245880b10faSchristos {
246880b10faSchristos if (argc <= 1) {
247880b10faSchristos fprintf(stderr, "Usage: %s FILES...\n", argv[0]);
248880b10faSchristos return EXIT_FAILURE;
249880b10faSchristos }
250880b10faSchristos
251880b10faSchristos lzma_stream strm = LZMA_STREAM_INIT;
252880b10faSchristos
253880b10faSchristos bool success = true;
254880b10faSchristos
255880b10faSchristos // Try to decompress all files.
256880b10faSchristos for (int i = 1; i < argc; ++i) {
257880b10faSchristos if (!init_decoder(&strm)) {
258880b10faSchristos // Decoder initialization failed. There's no point
259880b10faSchristos // to retry it so we need to exit.
260880b10faSchristos success = false;
261880b10faSchristos break;
262880b10faSchristos }
263880b10faSchristos
264880b10faSchristos FILE *infile = fopen(argv[i], "rb");
265880b10faSchristos
266880b10faSchristos if (infile == NULL) {
267880b10faSchristos fprintf(stderr, "%s: Error opening the "
268880b10faSchristos "input file: %s\n",
269880b10faSchristos argv[i], strerror(errno));
270880b10faSchristos success = false;
271880b10faSchristos } else {
272880b10faSchristos success &= decompress(&strm, argv[i], infile, stdout);
273880b10faSchristos fclose(infile);
274880b10faSchristos }
275880b10faSchristos }
276880b10faSchristos
277880b10faSchristos // Free the memory allocated for the decoder. This only needs to be
278880b10faSchristos // done after the last file.
279880b10faSchristos lzma_end(&strm);
280880b10faSchristos
281880b10faSchristos if (fclose(stdout)) {
282880b10faSchristos fprintf(stderr, "Write error: %s\n", strerror(errno));
283880b10faSchristos success = false;
284880b10faSchristos }
285880b10faSchristos
286880b10faSchristos return success ? EXIT_SUCCESS : EXIT_FAILURE;
287880b10faSchristos }
288