1 /* $NetBSD: unxz.c,v 1.6 2016/01/29 15:19:01 christos Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
41 unxz(int i, int o, char *pre, size_t prelen, off_t *bytes_in)
43 lzma_stream strm = LZMA_STREAM_INIT;
44 static const int flags = LZMA_TELL_UNSUPPORTED_CHECK|LZMA_CONCATENATED;
46 lzma_action action = LZMA_RUN;
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;
62 if ((ret = lzma_stream_decoder(&strm, UINT64_MAX, flags)) != LZMA_OK)
63 maybe_errx("Can't initialize decoder (%d)", ret);
67 if ((ret = lzma_code(&strm, LZMA_RUN)) != LZMA_OK)
68 maybe_errx("Can't read headers (%d)", ret);
72 strm.avail_out = sizeof(obuf);
75 if (strm.avail_in == 0) {
77 strm.avail_in = read(i, ibuf, sizeof(ibuf));
78 switch (strm.avail_in) {
80 maybe_err("read failed");
86 *bytes_in += strm.avail_in;
91 ret = lzma_code(&strm, action);
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;
99 if (write(o, obuf, write_size) != (ssize_t)write_size)
100 maybe_err("write failed");
102 strm.next_out = obuf;
103 strm.avail_out = sizeof(obuf);
104 bytes_out += write_size;
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;
121 msg = strerror(ENOMEM);
124 case LZMA_FORMAT_ERROR:
125 msg = "File format not recognized";
128 case LZMA_OPTIONS_ERROR:
129 // FIXME: Better message?
130 msg = "Unsupported compression options";
133 case LZMA_DATA_ERROR:
134 msg = "File is corrupt";
138 msg = "Unexpected end of input";
141 case LZMA_MEMLIMIT_ERROR:
142 msg = "Reached memory limit";
146 maybe_errx("Unknown error (%d)", ret);
149 maybe_errx("%s", msg);