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