xref: /minix3/usr.bin/gzip/unxz.c (revision 5a645f22a86f086849945a5dd6acbf59f38c913a)
1*5a645f22SBen Gras /*	$NetBSD: unxz.c,v 1.5 2011/09/30 01:32:21 christos Exp $	*/
2*5a645f22SBen Gras 
3*5a645f22SBen Gras /*-
4*5a645f22SBen Gras  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5*5a645f22SBen Gras  * All rights reserved.
6*5a645f22SBen Gras  *
7*5a645f22SBen Gras  * This code is derived from software contributed to The NetBSD Foundation
8*5a645f22SBen Gras  * by Christos Zoulas.
9*5a645f22SBen Gras  *
10*5a645f22SBen Gras  * Redistribution and use in source and binary forms, with or without
11*5a645f22SBen Gras  * modification, are permitted provided that the following conditions
12*5a645f22SBen Gras  * are met:
13*5a645f22SBen Gras  * 1. Redistributions of source code must retain the above copyright
14*5a645f22SBen Gras  *    notice, this list of conditions and the following disclaimer.
15*5a645f22SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
16*5a645f22SBen Gras  *    notice, this list of conditions and the following disclaimer in the
17*5a645f22SBen Gras  *    documentation and/or other materials provided with the distribution.
18*5a645f22SBen Gras  * 3. All advertising materials mentioning features or use of this software
19*5a645f22SBen Gras  *    must display the following acknowledgement:
20*5a645f22SBen Gras  *        This product includes software developed by the NetBSD
21*5a645f22SBen Gras  *        Foundation, Inc. and its contributors.
22*5a645f22SBen Gras  * 4. Neither the name of The NetBSD Foundation nor the names of its
23*5a645f22SBen Gras  *    contributors may be used to endorse or promote products derived
24*5a645f22SBen Gras  *    from this software without specific prior written permission.
25*5a645f22SBen Gras  *
26*5a645f22SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27*5a645f22SBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28*5a645f22SBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29*5a645f22SBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30*5a645f22SBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31*5a645f22SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32*5a645f22SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33*5a645f22SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34*5a645f22SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35*5a645f22SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36*5a645f22SBen Gras  * POSSIBILITY OF SUCH DAMAGE.
37*5a645f22SBen Gras  */
38*5a645f22SBen Gras #include <sys/cdefs.h>
39*5a645f22SBen Gras __RCSID("$NetBSD: unxz.c,v 1.5 2011/09/30 01:32:21 christos Exp $");
40*5a645f22SBen Gras 
41*5a645f22SBen Gras #include <stdarg.h>
42*5a645f22SBen Gras #include <errno.h>
43*5a645f22SBen Gras #include <stdio.h>
44*5a645f22SBen Gras #include <unistd.h>
45*5a645f22SBen Gras #include <lzma.h>
46*5a645f22SBen Gras 
47*5a645f22SBen Gras static off_t
unxz(int i,int o,char * pre,size_t prelen,off_t * bytes_in)48*5a645f22SBen Gras unxz(int i, int o, char *pre, size_t prelen, off_t *bytes_in)
49*5a645f22SBen Gras {
50*5a645f22SBen Gras 	lzma_stream strm = LZMA_STREAM_INIT;
51*5a645f22SBen Gras 	static const int flags = LZMA_TELL_UNSUPPORTED_CHECK|LZMA_CONCATENATED;
52*5a645f22SBen Gras 	lzma_ret ret;
53*5a645f22SBen Gras 	lzma_action action = LZMA_RUN;
54*5a645f22SBen Gras 	off_t bytes_out, bp;
55*5a645f22SBen Gras 	uint8_t ibuf[BUFSIZ];
56*5a645f22SBen Gras 	uint8_t obuf[BUFSIZ];
57*5a645f22SBen Gras 
58*5a645f22SBen Gras 	if (bytes_in == NULL)
59*5a645f22SBen Gras 		bytes_in = &bp;
60*5a645f22SBen Gras 
61*5a645f22SBen Gras 	strm.next_in = ibuf;
62*5a645f22SBen Gras 	memcpy(ibuf, pre, prelen);
63*5a645f22SBen Gras 	strm.avail_in = read(i, ibuf + prelen, sizeof(ibuf) - prelen);
64*5a645f22SBen Gras 	if (strm.avail_in == (size_t)-1)
65*5a645f22SBen Gras 		maybe_err("read failed");
66*5a645f22SBen Gras 	strm.avail_in += prelen;
67*5a645f22SBen Gras 	*bytes_in = strm.avail_in;
68*5a645f22SBen Gras 
69*5a645f22SBen Gras 	if ((ret = lzma_stream_decoder(&strm, UINT64_MAX, flags)) != LZMA_OK)
70*5a645f22SBen Gras 		maybe_errx("Can't initialize decoder (%d)", ret);
71*5a645f22SBen Gras 
72*5a645f22SBen Gras 	strm.next_out = NULL;
73*5a645f22SBen Gras 	strm.avail_out = 0;
74*5a645f22SBen Gras 	if ((ret = lzma_code(&strm, LZMA_RUN)) != LZMA_OK)
75*5a645f22SBen Gras 		maybe_errx("Can't read headers (%d)", ret);
76*5a645f22SBen Gras 
77*5a645f22SBen Gras 	bytes_out = 0;
78*5a645f22SBen Gras 	strm.next_out = obuf;
79*5a645f22SBen Gras 	strm.avail_out = sizeof(obuf);
80*5a645f22SBen Gras 
81*5a645f22SBen Gras 	for (;;) {
82*5a645f22SBen Gras 		if (strm.avail_in == 0) {
83*5a645f22SBen Gras 			strm.next_in = ibuf;
84*5a645f22SBen Gras 			strm.avail_in = read(i, ibuf, sizeof(ibuf));
85*5a645f22SBen Gras 			switch (strm.avail_in) {
86*5a645f22SBen Gras 			case (size_t)-1:
87*5a645f22SBen Gras 				maybe_err("read failed");
88*5a645f22SBen Gras 				/*NOTREACHED*/
89*5a645f22SBen Gras 			case 0:
90*5a645f22SBen Gras 				action = LZMA_FINISH;
91*5a645f22SBen Gras 				break;
92*5a645f22SBen Gras 			default:
93*5a645f22SBen Gras 				*bytes_in += strm.avail_in;
94*5a645f22SBen Gras 				break;
95*5a645f22SBen Gras 			}
96*5a645f22SBen Gras 		}
97*5a645f22SBen Gras 
98*5a645f22SBen Gras 		ret = lzma_code(&strm, action);
99*5a645f22SBen Gras 
100*5a645f22SBen Gras 		// Write and check write error before checking decoder error.
101*5a645f22SBen Gras 		// This way as much data as possible gets written to output
102*5a645f22SBen Gras 		// even if decoder detected an error.
103*5a645f22SBen Gras 		if (strm.avail_out == 0 || ret != LZMA_OK) {
104*5a645f22SBen Gras 			const size_t write_size = sizeof(obuf) - strm.avail_out;
105*5a645f22SBen Gras 
106*5a645f22SBen Gras 			if (write(o, obuf, write_size) != (ssize_t)write_size)
107*5a645f22SBen Gras 				maybe_err("write failed");
108*5a645f22SBen Gras 
109*5a645f22SBen Gras 			strm.next_out = obuf;
110*5a645f22SBen Gras 			strm.avail_out = sizeof(obuf);
111*5a645f22SBen Gras 			bytes_out += write_size;
112*5a645f22SBen Gras 		}
113*5a645f22SBen Gras 
114*5a645f22SBen Gras 		if (ret != LZMA_OK) {
115*5a645f22SBen Gras 			if (ret == LZMA_STREAM_END) {
116*5a645f22SBen Gras 				// Check that there's no trailing garbage.
117*5a645f22SBen Gras 				if (strm.avail_in != 0 || read(i, ibuf, 1))
118*5a645f22SBen Gras 					ret = LZMA_DATA_ERROR;
119*5a645f22SBen Gras 				else {
120*5a645f22SBen Gras 					lzma_end(&strm);
121*5a645f22SBen Gras 					return bytes_out;
122*5a645f22SBen Gras 				}
123*5a645f22SBen Gras 			}
124*5a645f22SBen Gras 
125*5a645f22SBen Gras 			const char *msg;
126*5a645f22SBen Gras 			switch (ret) {
127*5a645f22SBen Gras 			case LZMA_MEM_ERROR:
128*5a645f22SBen Gras 				msg = strerror(ENOMEM);
129*5a645f22SBen Gras 				break;
130*5a645f22SBen Gras 
131*5a645f22SBen Gras 			case LZMA_FORMAT_ERROR:
132*5a645f22SBen Gras 				msg = "File format not recognized";
133*5a645f22SBen Gras 				break;
134*5a645f22SBen Gras 
135*5a645f22SBen Gras 			case LZMA_OPTIONS_ERROR:
136*5a645f22SBen Gras 				// FIXME: Better message?
137*5a645f22SBen Gras 				msg = "Unsupported compression options";
138*5a645f22SBen Gras 				break;
139*5a645f22SBen Gras 
140*5a645f22SBen Gras 			case LZMA_DATA_ERROR:
141*5a645f22SBen Gras 				msg = "File is corrupt";
142*5a645f22SBen Gras 				break;
143*5a645f22SBen Gras 
144*5a645f22SBen Gras 			case LZMA_BUF_ERROR:
145*5a645f22SBen Gras 				msg = "Unexpected end of input";
146*5a645f22SBen Gras 				break;
147*5a645f22SBen Gras 
148*5a645f22SBen Gras 			case LZMA_MEMLIMIT_ERROR:
149*5a645f22SBen Gras 				msg = "Reached memory limit";
150*5a645f22SBen Gras 				break;
151*5a645f22SBen Gras 
152*5a645f22SBen Gras 			default:
153*5a645f22SBen Gras 				maybe_errx("Unknown error (%d)", ret);
154*5a645f22SBen Gras 				break;
155*5a645f22SBen Gras 			}
156*5a645f22SBen Gras 			maybe_errx("%s", msg);
157*5a645f22SBen Gras 
158*5a645f22SBen Gras 		}
159*5a645f22SBen Gras 	}
160*5a645f22SBen Gras }
161