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