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