xref: /minix3/external/public-domain/xz/dist/doc/examples/02_decompress.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc ///////////////////////////////////////////////////////////////////////////////
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc /// \file       02_decompress.c
4*0a6a1f1dSLionel Sambuc /// \brief      Decompress .xz files to stdout
5*0a6a1f1dSLionel Sambuc ///
6*0a6a1f1dSLionel Sambuc /// Usage:      ./02_decompress INPUT_FILES... > OUTFILE
7*0a6a1f1dSLionel Sambuc ///
8*0a6a1f1dSLionel Sambuc /// Example:    ./02_decompress foo.xz bar.xz > foobar
9*0a6a1f1dSLionel Sambuc //
10*0a6a1f1dSLionel Sambuc //  Author:     Lasse Collin
11*0a6a1f1dSLionel Sambuc //
12*0a6a1f1dSLionel Sambuc //  This file has been put into the public domain.
13*0a6a1f1dSLionel Sambuc //  You can do whatever you want with this file.
14*0a6a1f1dSLionel Sambuc //
15*0a6a1f1dSLionel Sambuc ///////////////////////////////////////////////////////////////////////////////
16*0a6a1f1dSLionel Sambuc 
17*0a6a1f1dSLionel Sambuc #include <stdbool.h>
18*0a6a1f1dSLionel Sambuc #include <stdlib.h>
19*0a6a1f1dSLionel Sambuc #include <stdio.h>
20*0a6a1f1dSLionel Sambuc #include <string.h>
21*0a6a1f1dSLionel Sambuc #include <errno.h>
22*0a6a1f1dSLionel Sambuc #include <lzma.h>
23*0a6a1f1dSLionel Sambuc 
24*0a6a1f1dSLionel Sambuc 
25*0a6a1f1dSLionel Sambuc static bool
init_decoder(lzma_stream * strm)26*0a6a1f1dSLionel Sambuc init_decoder(lzma_stream *strm)
27*0a6a1f1dSLionel Sambuc {
28*0a6a1f1dSLionel Sambuc 	// Initialize a .xz decoder. The decoder supports a memory usage limit
29*0a6a1f1dSLionel Sambuc 	// and a set of flags.
30*0a6a1f1dSLionel Sambuc 	//
31*0a6a1f1dSLionel Sambuc 	// The memory usage of the decompressor depends on the settings used
32*0a6a1f1dSLionel Sambuc 	// to compress a .xz file. It can vary from less than a megabyte to
33*0a6a1f1dSLionel Sambuc 	// a few gigabytes, but in practice (at least for now) it rarely
34*0a6a1f1dSLionel Sambuc 	// exceeds 65 MiB because that's how much memory is required to
35*0a6a1f1dSLionel Sambuc 	// decompress files created with "xz -9". Settings requiring more
36*0a6a1f1dSLionel Sambuc 	// memory take extra effort to use and don't (at least for now)
37*0a6a1f1dSLionel Sambuc 	// provide significantly better compression in most cases.
38*0a6a1f1dSLionel Sambuc 	//
39*0a6a1f1dSLionel Sambuc 	// Memory usage limit is useful if it is important that the
40*0a6a1f1dSLionel Sambuc 	// decompressor won't consume gigabytes of memory. The need
41*0a6a1f1dSLionel Sambuc 	// for limiting depends on the application. In this example,
42*0a6a1f1dSLionel Sambuc 	// no memory usage limiting is used. This is done by setting
43*0a6a1f1dSLionel Sambuc 	// the limit to UINT64_MAX.
44*0a6a1f1dSLionel Sambuc 	//
45*0a6a1f1dSLionel Sambuc 	// The .xz format allows concatenating compressed files as is:
46*0a6a1f1dSLionel Sambuc 	//
47*0a6a1f1dSLionel Sambuc 	//     echo foo | xz > foobar.xz
48*0a6a1f1dSLionel Sambuc 	//     echo bar | xz >> foobar.xz
49*0a6a1f1dSLionel Sambuc 	//
50*0a6a1f1dSLionel Sambuc 	// When decompressing normal standalone .xz files, LZMA_CONCATENATED
51*0a6a1f1dSLionel Sambuc 	// should always be used to support decompression of concatenated
52*0a6a1f1dSLionel Sambuc 	// .xz files. If LZMA_CONCATENATED isn't used, the decoder will stop
53*0a6a1f1dSLionel Sambuc 	// after the first .xz stream. This can be useful when .xz data has
54*0a6a1f1dSLionel Sambuc 	// been embedded inside another file format.
55*0a6a1f1dSLionel Sambuc 	//
56*0a6a1f1dSLionel Sambuc 	// Flags other than LZMA_CONCATENATED are supported too, and can
57*0a6a1f1dSLionel Sambuc 	// be combined with bitwise-or. See lzma/container.h
58*0a6a1f1dSLionel Sambuc 	// (src/liblzma/api/lzma/container.h in the source package or e.g.
59*0a6a1f1dSLionel Sambuc 	// /usr/include/lzma/container.h depending on the install prefix)
60*0a6a1f1dSLionel Sambuc 	// for details.
61*0a6a1f1dSLionel Sambuc 	lzma_ret ret = lzma_stream_decoder(
62*0a6a1f1dSLionel Sambuc 			strm, UINT64_MAX, LZMA_CONCATENATED);
63*0a6a1f1dSLionel Sambuc 
64*0a6a1f1dSLionel Sambuc 	// Return successfully if the initialization went fine.
65*0a6a1f1dSLionel Sambuc 	if (ret == LZMA_OK)
66*0a6a1f1dSLionel Sambuc 		return true;
67*0a6a1f1dSLionel Sambuc 
68*0a6a1f1dSLionel Sambuc 	// Something went wrong. The possible errors are documented in
69*0a6a1f1dSLionel Sambuc 	// lzma/container.h (src/liblzma/api/lzma/container.h in the source
70*0a6a1f1dSLionel Sambuc 	// package or e.g. /usr/include/lzma/container.h depending on the
71*0a6a1f1dSLionel Sambuc 	// install prefix).
72*0a6a1f1dSLionel Sambuc 	//
73*0a6a1f1dSLionel Sambuc 	// Note that LZMA_MEMLIMIT_ERROR is never possible here. If you
74*0a6a1f1dSLionel Sambuc 	// specify a very tiny limit, the error will be delayed until
75*0a6a1f1dSLionel Sambuc 	// the first headers have been parsed by a call to lzma_code().
76*0a6a1f1dSLionel Sambuc 	const char *msg;
77*0a6a1f1dSLionel Sambuc 	switch (ret) {
78*0a6a1f1dSLionel Sambuc 	case LZMA_MEM_ERROR:
79*0a6a1f1dSLionel Sambuc 		msg = "Memory allocation failed";
80*0a6a1f1dSLionel Sambuc 		break;
81*0a6a1f1dSLionel Sambuc 
82*0a6a1f1dSLionel Sambuc 	case LZMA_OPTIONS_ERROR:
83*0a6a1f1dSLionel Sambuc 		msg = "Unsupported decompressor flags";
84*0a6a1f1dSLionel Sambuc 		break;
85*0a6a1f1dSLionel Sambuc 
86*0a6a1f1dSLionel Sambuc 	default:
87*0a6a1f1dSLionel Sambuc 		// This is most likely LZMA_PROG_ERROR indicating a bug in
88*0a6a1f1dSLionel Sambuc 		// this program or in liblzma. It is inconvenient to have a
89*0a6a1f1dSLionel Sambuc 		// separate error message for errors that should be impossible
90*0a6a1f1dSLionel Sambuc 		// to occur, but knowing the error code is important for
91*0a6a1f1dSLionel Sambuc 		// debugging. That's why it is good to print the error code
92*0a6a1f1dSLionel Sambuc 		// at least when there is no good error message to show.
93*0a6a1f1dSLionel Sambuc 		msg = "Unknown error, possibly a bug";
94*0a6a1f1dSLionel Sambuc 		break;
95*0a6a1f1dSLionel Sambuc 	}
96*0a6a1f1dSLionel Sambuc 
97*0a6a1f1dSLionel Sambuc 	fprintf(stderr, "Error initializing the decoder: %s (error code %u)\n",
98*0a6a1f1dSLionel Sambuc 			msg, ret);
99*0a6a1f1dSLionel Sambuc 	return false;
100*0a6a1f1dSLionel Sambuc }
101*0a6a1f1dSLionel Sambuc 
102*0a6a1f1dSLionel Sambuc 
103*0a6a1f1dSLionel Sambuc static bool
decompress(lzma_stream * strm,const char * inname,FILE * infile,FILE * outfile)104*0a6a1f1dSLionel Sambuc decompress(lzma_stream *strm, const char *inname, FILE *infile, FILE *outfile)
105*0a6a1f1dSLionel Sambuc {
106*0a6a1f1dSLionel Sambuc 	// When LZMA_CONCATENATED flag was used when initializing the decoder,
107*0a6a1f1dSLionel Sambuc 	// we need to tell lzma_code() when there will be no more input.
108*0a6a1f1dSLionel Sambuc 	// This is done by setting action to LZMA_FINISH instead of LZMA_RUN
109*0a6a1f1dSLionel Sambuc 	// in the same way as it is done when encoding.
110*0a6a1f1dSLionel Sambuc 	//
111*0a6a1f1dSLionel Sambuc 	// When LZMA_CONCATENATED isn't used, there is no need to use
112*0a6a1f1dSLionel Sambuc 	// LZMA_FINISH to tell when all the input has been read, but it
113*0a6a1f1dSLionel Sambuc 	// is still OK to use it if you want. When LZMA_CONCATENATED isn't
114*0a6a1f1dSLionel Sambuc 	// used, the decoder will stop after the first .xz stream. In that
115*0a6a1f1dSLionel Sambuc 	// case some unused data may be left in strm->next_in.
116*0a6a1f1dSLionel Sambuc 	lzma_action action = LZMA_RUN;
117*0a6a1f1dSLionel Sambuc 
118*0a6a1f1dSLionel Sambuc 	uint8_t inbuf[BUFSIZ];
119*0a6a1f1dSLionel Sambuc 	uint8_t outbuf[BUFSIZ];
120*0a6a1f1dSLionel Sambuc 
121*0a6a1f1dSLionel Sambuc 	strm->next_in = NULL;
122*0a6a1f1dSLionel Sambuc 	strm->avail_in = 0;
123*0a6a1f1dSLionel Sambuc 	strm->next_out = outbuf;
124*0a6a1f1dSLionel Sambuc 	strm->avail_out = sizeof(outbuf);
125*0a6a1f1dSLionel Sambuc 
126*0a6a1f1dSLionel Sambuc 	while (true) {
127*0a6a1f1dSLionel Sambuc 		if (strm->avail_in == 0 && !feof(infile)) {
128*0a6a1f1dSLionel Sambuc 			strm->next_in = inbuf;
129*0a6a1f1dSLionel Sambuc 			strm->avail_in = fread(inbuf, 1, sizeof(inbuf),
130*0a6a1f1dSLionel Sambuc 					infile);
131*0a6a1f1dSLionel Sambuc 
132*0a6a1f1dSLionel Sambuc 			if (ferror(infile)) {
133*0a6a1f1dSLionel Sambuc 				fprintf(stderr, "%s: Read error: %s\n",
134*0a6a1f1dSLionel Sambuc 						inname, strerror(errno));
135*0a6a1f1dSLionel Sambuc 				return false;
136*0a6a1f1dSLionel Sambuc 			}
137*0a6a1f1dSLionel Sambuc 
138*0a6a1f1dSLionel Sambuc 			// Once the end of the input file has been reached,
139*0a6a1f1dSLionel Sambuc 			// we need to tell lzma_code() that no more input
140*0a6a1f1dSLionel Sambuc 			// will be coming. As said before, this isn't required
141*0a6a1f1dSLionel Sambuc 			// if the LZMA_CONATENATED flag isn't used when
142*0a6a1f1dSLionel Sambuc 			// initializing the decoder.
143*0a6a1f1dSLionel Sambuc 			if (feof(infile))
144*0a6a1f1dSLionel Sambuc 				action = LZMA_FINISH;
145*0a6a1f1dSLionel Sambuc 		}
146*0a6a1f1dSLionel Sambuc 
147*0a6a1f1dSLionel Sambuc 		lzma_ret ret = lzma_code(strm, action);
148*0a6a1f1dSLionel Sambuc 
149*0a6a1f1dSLionel Sambuc 		if (strm->avail_out == 0 || ret == LZMA_STREAM_END) {
150*0a6a1f1dSLionel Sambuc 			size_t write_size = sizeof(outbuf) - strm->avail_out;
151*0a6a1f1dSLionel Sambuc 
152*0a6a1f1dSLionel Sambuc 			if (fwrite(outbuf, 1, write_size, outfile)
153*0a6a1f1dSLionel Sambuc 					!= write_size) {
154*0a6a1f1dSLionel Sambuc 				fprintf(stderr, "Write error: %s\n",
155*0a6a1f1dSLionel Sambuc 						strerror(errno));
156*0a6a1f1dSLionel Sambuc 				return false;
157*0a6a1f1dSLionel Sambuc 			}
158*0a6a1f1dSLionel Sambuc 
159*0a6a1f1dSLionel Sambuc 			strm->next_out = outbuf;
160*0a6a1f1dSLionel Sambuc 			strm->avail_out = sizeof(outbuf);
161*0a6a1f1dSLionel Sambuc 		}
162*0a6a1f1dSLionel Sambuc 
163*0a6a1f1dSLionel Sambuc 		if (ret != LZMA_OK) {
164*0a6a1f1dSLionel Sambuc 			// Once everything has been decoded successfully, the
165*0a6a1f1dSLionel Sambuc 			// return value of lzma_code() will be LZMA_STREAM_END.
166*0a6a1f1dSLionel Sambuc 			//
167*0a6a1f1dSLionel Sambuc 			// It is important to check for LZMA_STREAM_END. Do not
168*0a6a1f1dSLionel Sambuc 			// assume that getting ret != LZMA_OK would mean that
169*0a6a1f1dSLionel Sambuc 			// everything has gone well or that when you aren't
170*0a6a1f1dSLionel Sambuc 			// getting more output it must have successfully
171*0a6a1f1dSLionel Sambuc 			// decoded everything.
172*0a6a1f1dSLionel Sambuc 			if (ret == LZMA_STREAM_END)
173*0a6a1f1dSLionel Sambuc 				return true;
174*0a6a1f1dSLionel Sambuc 
175*0a6a1f1dSLionel Sambuc 			// It's not LZMA_OK nor LZMA_STREAM_END,
176*0a6a1f1dSLionel Sambuc 			// so it must be an error code. See lzma/base.h
177*0a6a1f1dSLionel Sambuc 			// (src/liblzma/api/lzma/base.h in the source package
178*0a6a1f1dSLionel Sambuc 			// or e.g. /usr/include/lzma/base.h depending on the
179*0a6a1f1dSLionel Sambuc 			// install prefix) for the list and documentation of
180*0a6a1f1dSLionel Sambuc 			// possible values. Many values listen in lzma_ret
181*0a6a1f1dSLionel Sambuc 			// enumeration aren't possible in this example, but
182*0a6a1f1dSLionel Sambuc 			// can be made possible by enabling memory usage limit
183*0a6a1f1dSLionel Sambuc 			// or adding flags to the decoder initialization.
184*0a6a1f1dSLionel Sambuc 			const char *msg;
185*0a6a1f1dSLionel Sambuc 			switch (ret) {
186*0a6a1f1dSLionel Sambuc 			case LZMA_MEM_ERROR:
187*0a6a1f1dSLionel Sambuc 				msg = "Memory allocation failed";
188*0a6a1f1dSLionel Sambuc 				break;
189*0a6a1f1dSLionel Sambuc 
190*0a6a1f1dSLionel Sambuc 			case LZMA_FORMAT_ERROR:
191*0a6a1f1dSLionel Sambuc 				// .xz magic bytes weren't found.
192*0a6a1f1dSLionel Sambuc 				msg = "The input is not in the .xz format";
193*0a6a1f1dSLionel Sambuc 				break;
194*0a6a1f1dSLionel Sambuc 
195*0a6a1f1dSLionel Sambuc 			case LZMA_OPTIONS_ERROR:
196*0a6a1f1dSLionel Sambuc 				// For example, the headers specify a filter
197*0a6a1f1dSLionel Sambuc 				// that isn't supported by this liblzma
198*0a6a1f1dSLionel Sambuc 				// version (or it hasn't been enabled when
199*0a6a1f1dSLionel Sambuc 				// building liblzma, but no-one sane does
200*0a6a1f1dSLionel Sambuc 				// that unless building liblzma for an
201*0a6a1f1dSLionel Sambuc 				// embedded system). Upgrading to a newer
202*0a6a1f1dSLionel Sambuc 				// liblzma might help.
203*0a6a1f1dSLionel Sambuc 				//
204*0a6a1f1dSLionel Sambuc 				// Note that it is unlikely that the file has
205*0a6a1f1dSLionel Sambuc 				// accidentally became corrupt if you get this
206*0a6a1f1dSLionel Sambuc 				// error. The integrity of the .xz headers is
207*0a6a1f1dSLionel Sambuc 				// always verified with a CRC32, so
208*0a6a1f1dSLionel Sambuc 				// unintentionally corrupt files can be
209*0a6a1f1dSLionel Sambuc 				// distinguished from unsupported files.
210*0a6a1f1dSLionel Sambuc 				msg = "Unsupported compression options";
211*0a6a1f1dSLionel Sambuc 				break;
212*0a6a1f1dSLionel Sambuc 
213*0a6a1f1dSLionel Sambuc 			case LZMA_DATA_ERROR:
214*0a6a1f1dSLionel Sambuc 				msg = "Compressed file is corrupt";
215*0a6a1f1dSLionel Sambuc 				break;
216*0a6a1f1dSLionel Sambuc 
217*0a6a1f1dSLionel Sambuc 			case LZMA_BUF_ERROR:
218*0a6a1f1dSLionel Sambuc 				// Typically this error means that a valid
219*0a6a1f1dSLionel Sambuc 				// file has got truncated, but it might also
220*0a6a1f1dSLionel Sambuc 				// be a damaged part in the file that makes
221*0a6a1f1dSLionel Sambuc 				// the decoder think the file is truncated.
222*0a6a1f1dSLionel Sambuc 				// If you prefer, you can use the same error
223*0a6a1f1dSLionel Sambuc 				// message for this as for LZMA_DATA_ERROR.
224*0a6a1f1dSLionel Sambuc 				msg = "Compressed file is truncated or "
225*0a6a1f1dSLionel Sambuc 						"otherwise corrupt";
226*0a6a1f1dSLionel Sambuc 				break;
227*0a6a1f1dSLionel Sambuc 
228*0a6a1f1dSLionel Sambuc 			default:
229*0a6a1f1dSLionel Sambuc 				// This is most likely LZMA_PROG_ERROR.
230*0a6a1f1dSLionel Sambuc 				msg = "Unknown error, possibly a bug";
231*0a6a1f1dSLionel Sambuc 				break;
232*0a6a1f1dSLionel Sambuc 			}
233*0a6a1f1dSLionel Sambuc 
234*0a6a1f1dSLionel Sambuc 			fprintf(stderr, "%s: Decoder error: "
235*0a6a1f1dSLionel Sambuc 					"%s (error code %u)\n",
236*0a6a1f1dSLionel Sambuc 					inname, msg, ret);
237*0a6a1f1dSLionel Sambuc 			return false;
238*0a6a1f1dSLionel Sambuc 		}
239*0a6a1f1dSLionel Sambuc 	}
240*0a6a1f1dSLionel Sambuc }
241*0a6a1f1dSLionel Sambuc 
242*0a6a1f1dSLionel Sambuc 
243*0a6a1f1dSLionel Sambuc extern int
main(int argc,char ** argv)244*0a6a1f1dSLionel Sambuc main(int argc, char **argv)
245*0a6a1f1dSLionel Sambuc {
246*0a6a1f1dSLionel Sambuc 	if (argc <= 1) {
247*0a6a1f1dSLionel Sambuc 		fprintf(stderr, "Usage: %s FILES...\n", argv[0]);
248*0a6a1f1dSLionel Sambuc 		return EXIT_FAILURE;
249*0a6a1f1dSLionel Sambuc 	}
250*0a6a1f1dSLionel Sambuc 
251*0a6a1f1dSLionel Sambuc 	lzma_stream strm = LZMA_STREAM_INIT;
252*0a6a1f1dSLionel Sambuc 
253*0a6a1f1dSLionel Sambuc 	bool success = true;
254*0a6a1f1dSLionel Sambuc 
255*0a6a1f1dSLionel Sambuc 	// Try to decompress all files.
256*0a6a1f1dSLionel Sambuc 	for (int i = 1; i < argc; ++i) {
257*0a6a1f1dSLionel Sambuc 		if (!init_decoder(&strm)) {
258*0a6a1f1dSLionel Sambuc 			// Decoder initialization failed. There's no point
259*0a6a1f1dSLionel Sambuc 			// to retry it so we need to exit.
260*0a6a1f1dSLionel Sambuc 			success = false;
261*0a6a1f1dSLionel Sambuc 			break;
262*0a6a1f1dSLionel Sambuc 		}
263*0a6a1f1dSLionel Sambuc 
264*0a6a1f1dSLionel Sambuc 		FILE *infile = fopen(argv[i], "rb");
265*0a6a1f1dSLionel Sambuc 
266*0a6a1f1dSLionel Sambuc 		if (infile == NULL) {
267*0a6a1f1dSLionel Sambuc 			fprintf(stderr, "%s: Error opening the "
268*0a6a1f1dSLionel Sambuc 					"input file: %s\n",
269*0a6a1f1dSLionel Sambuc 					argv[i], strerror(errno));
270*0a6a1f1dSLionel Sambuc 			success = false;
271*0a6a1f1dSLionel Sambuc 		} else {
272*0a6a1f1dSLionel Sambuc 			success &= decompress(&strm, argv[i], infile, stdout);
273*0a6a1f1dSLionel Sambuc 			fclose(infile);
274*0a6a1f1dSLionel Sambuc 		}
275*0a6a1f1dSLionel Sambuc 	}
276*0a6a1f1dSLionel Sambuc 
277*0a6a1f1dSLionel Sambuc 	// Free the memory allocated for the decoder. This only needs to be
278*0a6a1f1dSLionel Sambuc 	// done after the last file.
279*0a6a1f1dSLionel Sambuc 	lzma_end(&strm);
280*0a6a1f1dSLionel Sambuc 
281*0a6a1f1dSLionel Sambuc 	if (fclose(stdout)) {
282*0a6a1f1dSLionel Sambuc 		fprintf(stderr, "Write error: %s\n", strerror(errno));
283*0a6a1f1dSLionel Sambuc 		success = false;
284*0a6a1f1dSLionel Sambuc 	}
285*0a6a1f1dSLionel Sambuc 
286*0a6a1f1dSLionel Sambuc 	return success ? EXIT_SUCCESS : EXIT_FAILURE;
287*0a6a1f1dSLionel Sambuc }
288