1226184Sdelphij/*	$NetBSD: unxz.c,v 1.5 2011/09/30 01:32:21 christos Exp $	*/
2226184Sdelphij
3226184Sdelphij/*-
4226184Sdelphij * Copyright (c) 2011 The NetBSD Foundation, Inc.
5226184Sdelphij * All rights reserved.
6226184Sdelphij *
7226184Sdelphij * This code is derived from software contributed to The NetBSD Foundation
8226184Sdelphij * by Christos Zoulas.
9226184Sdelphij *
10226184Sdelphij * Redistribution and use in source and binary forms, with or without
11226184Sdelphij * modification, are permitted provided that the following conditions
12226184Sdelphij * are met:
13226184Sdelphij * 1. Redistributions of source code must retain the above copyright
14226184Sdelphij *    notice, this list of conditions and the following disclaimer.
15226184Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
16226184Sdelphij *    notice, this list of conditions and the following disclaimer in the
17226184Sdelphij *    documentation and/or other materials provided with the distribution.
18226184Sdelphij *
19226184Sdelphij * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20226184Sdelphij * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21226184Sdelphij * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22226184Sdelphij * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23226184Sdelphij * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24226184Sdelphij * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25226184Sdelphij * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26226184Sdelphij * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27226184Sdelphij * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28226184Sdelphij * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29226184Sdelphij * POSSIBILITY OF SUCH DAMAGE.
30226184Sdelphij */
31226184Sdelphij#include <sys/cdefs.h>
32226184Sdelphij__FBSDID("$FreeBSD$");
33226184Sdelphij
34226184Sdelphij#include <stdarg.h>
35226184Sdelphij#include <errno.h>
36226184Sdelphij#include <stdio.h>
37226184Sdelphij#include <unistd.h>
38226184Sdelphij#include <lzma.h>
39226184Sdelphij
40226184Sdelphijstatic off_t
41226184Sdelphijunxz(int i, int o, char *pre, size_t prelen, off_t *bytes_in)
42226184Sdelphij{
43226184Sdelphij	lzma_stream strm = LZMA_STREAM_INIT;
44226184Sdelphij	static const int flags = LZMA_TELL_UNSUPPORTED_CHECK|LZMA_CONCATENATED;
45226184Sdelphij	lzma_ret ret;
46226184Sdelphij	lzma_action action = LZMA_RUN;
47226184Sdelphij	off_t bytes_out, bp;
48226184Sdelphij	uint8_t ibuf[BUFSIZ];
49226184Sdelphij	uint8_t obuf[BUFSIZ];
50226184Sdelphij
51226184Sdelphij	if (bytes_in == NULL)
52226184Sdelphij		bytes_in = &bp;
53226184Sdelphij
54226184Sdelphij	strm.next_in = ibuf;
55226184Sdelphij	memcpy(ibuf, pre, prelen);
56226184Sdelphij	strm.avail_in = read(i, ibuf + prelen, sizeof(ibuf) - prelen);
57226184Sdelphij	if (strm.avail_in == (size_t)-1)
58226184Sdelphij		maybe_err("read failed");
59226184Sdelphij	strm.avail_in += prelen;
60226184Sdelphij	*bytes_in = strm.avail_in;
61226184Sdelphij
62226184Sdelphij	if ((ret = lzma_stream_decoder(&strm, UINT64_MAX, flags)) != LZMA_OK)
63226184Sdelphij		maybe_errx("Can't initialize decoder (%d)", ret);
64226184Sdelphij
65226184Sdelphij	strm.next_out = NULL;
66226184Sdelphij	strm.avail_out = 0;
67226184Sdelphij	if ((ret = lzma_code(&strm, LZMA_RUN)) != LZMA_OK)
68226184Sdelphij		maybe_errx("Can't read headers (%d)", ret);
69226184Sdelphij
70226184Sdelphij	bytes_out = 0;
71226184Sdelphij	strm.next_out = obuf;
72226184Sdelphij	strm.avail_out = sizeof(obuf);
73226184Sdelphij
74226184Sdelphij	for (;;) {
75226184Sdelphij		if (strm.avail_in == 0) {
76226184Sdelphij			strm.next_in = ibuf;
77226184Sdelphij			strm.avail_in = read(i, ibuf, sizeof(ibuf));
78226184Sdelphij			switch (strm.avail_in) {
79226184Sdelphij			case (size_t)-1:
80226184Sdelphij				maybe_err("read failed");
81226184Sdelphij				/*NOTREACHED*/
82226184Sdelphij			case 0:
83226184Sdelphij				action = LZMA_FINISH;
84226184Sdelphij				break;
85226184Sdelphij			default:
86226184Sdelphij				*bytes_in += strm.avail_in;
87226184Sdelphij				break;
88226184Sdelphij			}
89226184Sdelphij		}
90226184Sdelphij
91226184Sdelphij		ret = lzma_code(&strm, action);
92226184Sdelphij
93226184Sdelphij		// Write and check write error before checking decoder error.
94226184Sdelphij		// This way as much data as possible gets written to output
95226184Sdelphij		// even if decoder detected an error.
96226184Sdelphij		if (strm.avail_out == 0 || ret != LZMA_OK) {
97226184Sdelphij			const size_t write_size = sizeof(obuf) - strm.avail_out;
98226184Sdelphij
99226184Sdelphij			if (write(o, obuf, write_size) != (ssize_t)write_size)
100226184Sdelphij				maybe_err("write failed");
101226184Sdelphij
102226184Sdelphij			strm.next_out = obuf;
103226184Sdelphij			strm.avail_out = sizeof(obuf);
104226184Sdelphij			bytes_out += write_size;
105226184Sdelphij		}
106226184Sdelphij
107226184Sdelphij		if (ret != LZMA_OK) {
108226184Sdelphij			if (ret == LZMA_STREAM_END) {
109226184Sdelphij				// Check that there's no trailing garbage.
110226184Sdelphij				if (strm.avail_in != 0 || read(i, ibuf, 1))
111226184Sdelphij					ret = LZMA_DATA_ERROR;
112226184Sdelphij				else {
113226184Sdelphij					lzma_end(&strm);
114226184Sdelphij					return bytes_out;
115226184Sdelphij				}
116226184Sdelphij			}
117226184Sdelphij
118226184Sdelphij			const char *msg;
119226184Sdelphij			switch (ret) {
120226184Sdelphij			case LZMA_MEM_ERROR:
121226184Sdelphij				msg = strerror(ENOMEM);
122226184Sdelphij				break;
123226184Sdelphij
124226184Sdelphij			case LZMA_FORMAT_ERROR:
125226184Sdelphij				msg = "File format not recognized";
126226184Sdelphij				break;
127226184Sdelphij
128226184Sdelphij			case LZMA_OPTIONS_ERROR:
129226184Sdelphij				// FIXME: Better message?
130226184Sdelphij				msg = "Unsupported compression options";
131226184Sdelphij				break;
132226184Sdelphij
133226184Sdelphij			case LZMA_DATA_ERROR:
134226184Sdelphij				msg = "File is corrupt";
135226184Sdelphij				break;
136226184Sdelphij
137226184Sdelphij			case LZMA_BUF_ERROR:
138226184Sdelphij				msg = "Unexpected end of input";
139226184Sdelphij				break;
140226184Sdelphij
141226184Sdelphij			case LZMA_MEMLIMIT_ERROR:
142226184Sdelphij				msg = "Reached memory limit";
143226184Sdelphij				break;
144226184Sdelphij
145226184Sdelphij			default:
146226184Sdelphij				maybe_errx("Unknown error (%d)", ret);
147226184Sdelphij				break;
148226184Sdelphij			}
149226184Sdelphij			maybe_errx("%s", msg);
150226184Sdelphij
151226184Sdelphij		}
152226184Sdelphij	}
153226184Sdelphij}
154