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