1 /* $NetBSD: unxz.c,v 1.5 2011/09/30 01:32:21 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.
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.
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.
38 #include <sys/cdefs.h>
39 __RCSID("$NetBSD: unxz.c,v 1.5 2011/09/30 01:32:21 christos Exp $");
48 unxz(int i
, int o
, char *pre
, size_t prelen
, off_t
*bytes_in
)
50 lzma_stream strm
= LZMA_STREAM_INIT
;
51 static const int flags
= LZMA_TELL_UNSUPPORTED_CHECK
|LZMA_CONCATENATED
;
53 lzma_action action
= LZMA_RUN
;
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
;
69 if ((ret
= lzma_stream_decoder(&strm
, UINT64_MAX
, flags
)) != LZMA_OK
)
70 maybe_errx("Can't initialize decoder (%d)", ret
);
74 if ((ret
= lzma_code(&strm
, LZMA_RUN
)) != LZMA_OK
)
75 maybe_errx("Can't read headers (%d)", ret
);
79 strm
.avail_out
= sizeof(obuf
);
82 if (strm
.avail_in
== 0) {
84 strm
.avail_in
= read(i
, ibuf
, sizeof(ibuf
));
85 switch (strm
.avail_in
) {
87 maybe_err("read failed");
93 *bytes_in
+= strm
.avail_in
;
98 ret
= lzma_code(&strm
, action
);
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
;
106 if (write(o
, obuf
, write_size
) != (ssize_t
)write_size
)
107 maybe_err("write failed");
109 strm
.next_out
= obuf
;
110 strm
.avail_out
= sizeof(obuf
);
111 bytes_out
+= write_size
;
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
;
128 msg
= strerror(ENOMEM
);
131 case LZMA_FORMAT_ERROR
:
132 msg
= "File format not recognized";
135 case LZMA_OPTIONS_ERROR
:
136 // FIXME: Better message?
137 msg
= "Unsupported compression options";
140 case LZMA_DATA_ERROR
:
141 msg
= "File is corrupt";
145 msg
= "Unexpected end of input";
148 case LZMA_MEMLIMIT_ERROR
:
149 msg
= "Reached memory limit";
153 maybe_errx("Unknown error (%d)", ret
);
156 maybe_errx("%s", msg
);