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