unxz.c revision 319280
1/*	$NetBSD: unxz.c,v 1.6 2016/01/29 15:19:01 christos 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 319280 2017-05-31 05:29:20Z 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	strm.avail_in += prelen;
60	*bytes_in = strm.avail_in;
61
62	if ((ret = lzma_stream_decoder(&strm, UINT64_MAX, flags)) != LZMA_OK)
63		maybe_errx("Can't initialize decoder (%d)", ret);
64
65	strm.next_out = NULL;
66	strm.avail_out = 0;
67	if ((ret = lzma_code(&strm, LZMA_RUN)) != LZMA_OK)
68		maybe_errx("Can't read headers (%d)", ret);
69
70	bytes_out = 0;
71	strm.next_out = obuf;
72	strm.avail_out = sizeof(obuf);
73
74	for (;;) {
75		if (strm.avail_in == 0) {
76			strm.next_in = ibuf;
77			strm.avail_in = read(i, ibuf, sizeof(ibuf));
78			switch (strm.avail_in) {
79			case (size_t)-1:
80				maybe_err("read failed");
81				/*NOTREACHED*/
82			case 0:
83				action = LZMA_FINISH;
84				break;
85			default:
86				*bytes_in += strm.avail_in;
87				break;
88			}
89		}
90
91		ret = lzma_code(&strm, action);
92
93		// Write and check write error before checking decoder error.
94		// This way as much data as possible gets written to output
95		// even if decoder detected an error.
96		if (strm.avail_out == 0 || ret != LZMA_OK) {
97			const size_t write_size = sizeof(obuf) - strm.avail_out;
98
99			if (write(o, obuf, write_size) != (ssize_t)write_size)
100				maybe_err("write failed");
101
102			strm.next_out = obuf;
103			strm.avail_out = sizeof(obuf);
104			bytes_out += write_size;
105		}
106
107		if (ret != LZMA_OK) {
108			if (ret == LZMA_STREAM_END) {
109				// Check that there's no trailing garbage.
110				if (strm.avail_in != 0 || read(i, ibuf, 1))
111					ret = LZMA_DATA_ERROR;
112				else {
113					lzma_end(&strm);
114					return bytes_out;
115				}
116			}
117
118			const char *msg;
119			switch (ret) {
120			case LZMA_MEM_ERROR:
121				msg = strerror(ENOMEM);
122				break;
123
124			case LZMA_FORMAT_ERROR:
125				msg = "File format not recognized";
126				break;
127
128			case LZMA_OPTIONS_ERROR:
129				// FIXME: Better message?
130				msg = "Unsupported compression options";
131				break;
132
133			case LZMA_DATA_ERROR:
134				msg = "File is corrupt";
135				break;
136
137			case LZMA_BUF_ERROR:
138				msg = "Unexpected end of input";
139				break;
140
141			case LZMA_MEMLIMIT_ERROR:
142				msg = "Reached memory limit";
143				break;
144
145			default:
146				maybe_errx("Unknown error (%d)", ret);
147				break;
148			}
149			maybe_errx("%s", msg);
150
151		}
152	}
153}
154