Home | History | Annotate | Line # | Download | only in gzip
      1 /*	$NetBSD: unxz.c,v 1.11 2026/01/10 05:11:23 mrg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     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  *
     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.
     30  */
     31 #include <sys/cdefs.h>
     32 __RCSID("$NetBSD: unxz.c,v 1.11 2026/01/10 05:11:23 mrg Exp $");
     33 
     34 #include <stdarg.h>
     35 #include <errno.h>
     36 #include <stdio.h>
     37 #include <unistd.h>
     38 #include <lzma.h>
     39 
     40 static off_t
     41 unxz(int i, int o, char *pre, size_t prelen, off_t *bytes_in)
     42 {
     43 	lzma_stream strm = LZMA_STREAM_INIT;
     44 	static const int flags = LZMA_TELL_UNSUPPORTED_CHECK|LZMA_CONCATENATED;
     45 	lzma_ret ret;
     46 	lzma_action action = LZMA_RUN;
     47 	off_t bytes_out, bp;
     48 	uint8_t obuf[BUFSIZ];
     49 	uint8_t ibuf[BUFSIZ];
     50 
     51 	if (bytes_in == NULL)
     52 		bytes_in = &bp;
     53 
     54 	strm.next_in = NULL;
     55 	strm.avail_in = 0;
     56 	*bytes_in = strm.avail_in;
     57 
     58 	if ((ret = lzma_stream_decoder(&strm, UINT64_MAX, flags)) != LZMA_OK)
     59 		maybe_errx("Can't initialize decoder (%d)", ret);
     60 
     61 	bytes_out = 0;
     62 	strm.next_out = obuf;
     63 	strm.avail_out = sizeof(obuf);
     64 
     65 	strm.next_in = (uint8_t *)pre;
     66 	strm.avail_in = prelen;
     67 
     68 	for (;;) {
     69 		check_siginfo();
     70 		if (strm.avail_in == 0) {
     71 			strm.next_in = ibuf;
     72 			strm.avail_in = read(i, ibuf, sizeof ibuf);
     73 			switch (strm.avail_in) {
     74 			case (size_t)-1:
     75 				maybe_err("read failed");
     76 				/*NOTREACHED*/
     77 			case 0:
     78 				action = LZMA_FINISH;
     79 				break;
     80 			default:
     81 				infile_newdata(strm.avail_in);
     82 				*bytes_in += strm.avail_in;
     83 				break;
     84 			}
     85 		}
     86 
     87 		ret = lzma_code(&strm, action);
     88 
     89 		// Write and check write error before checking decoder error.
     90 		// This way as much data as possible gets written to output
     91 		// even if decoder detected an error.
     92 		if (strm.avail_out == 0 || ret != LZMA_OK) {
     93 			const size_t write_size = sizeof(obuf) - strm.avail_out;
     94 
     95 			if (!tflag &&
     96 			    write(o, obuf, write_size) != (ssize_t)write_size)
     97 				maybe_err("write failed");
     98 
     99 			strm.next_out = obuf;
    100 			strm.avail_out = sizeof(obuf);
    101 			bytes_out += write_size;
    102 		}
    103 
    104 		if (ret != LZMA_OK) {
    105 			if (ret == LZMA_STREAM_END) {
    106 				// Check that there's no trailing garbage.
    107 				if (strm.avail_in != 0 || read(i, ibuf, 1))
    108 					ret = LZMA_DATA_ERROR;
    109 				else {
    110 					lzma_end(&strm);
    111 					return bytes_out;
    112 				}
    113 			}
    114 
    115 			const char *msg;
    116 			switch (ret) {
    117 			case LZMA_MEM_ERROR:
    118 				msg = strerror(ENOMEM);
    119 				break;
    120 
    121 			case LZMA_FORMAT_ERROR:
    122 				msg = "File format not recognized";
    123 				break;
    124 
    125 			case LZMA_OPTIONS_ERROR:
    126 				// FIXME: Better message?
    127 				msg = "Unsupported compression options";
    128 				break;
    129 
    130 			case LZMA_DATA_ERROR:
    131 				msg = "File is corrupt";
    132 				break;
    133 
    134 			case LZMA_BUF_ERROR:
    135 				msg = "Unexpected end of input";
    136 				break;
    137 
    138 			case LZMA_MEMLIMIT_ERROR:
    139 				msg = "Reached memory limit";
    140 				break;
    141 
    142 			default:
    143 				maybe_errx("Unknown error (%d)", ret);
    144 				break;
    145 			}
    146 			maybe_errx("%s", msg);
    147 
    148 		}
    149 	}
    150 }
    151 
    152 #include <stdbool.h>
    153 
    154 /*
    155  * Copied various bits and pieces from xz support code or brute force
    156  * replacements.
    157  */
    158 
    159 #define	my_min(A,B)	((A)<(B)?(A):(B))
    160 
    161 // Some systems have suboptimal BUFSIZ. Use a bit bigger value on them.
    162 // We also need that IO_BUFFER_SIZE is a multiple of 8 (sizeof(uint64_t))
    163 #if BUFSIZ <= 1024
    164 #       define IO_BUFFER_SIZE 8192
    165 #else
    166 #       define IO_BUFFER_SIZE (BUFSIZ & ~7U)
    167 #endif
    168 
    169 /// is_sparse() accesses the buffer as uint64_t for maximum speed.
    170 /// Use an union to make sure that the buffer is properly aligned.
    171 typedef union {
    172         uint8_t u8[IO_BUFFER_SIZE];
    173         uint32_t u32[IO_BUFFER_SIZE / sizeof(uint32_t)];
    174         uint64_t u64[IO_BUFFER_SIZE / sizeof(uint64_t)];
    175 } io_buf;
    176 
    177 
    178 static bool
    179 io_pread(int fd, io_buf *buf, size_t size, off_t pos)
    180 {
    181 	// Using lseek() and read() is more portable than pread() and
    182 	// for us it is as good as real pread().
    183 	if (lseek(fd, pos, SEEK_SET) != pos) {
    184 		return true;
    185 	}
    186 
    187 	const size_t amount = read(fd, buf, size);
    188 	if (amount == SIZE_MAX)
    189 		return true;
    190 
    191 	if (amount != size) {
    192 		return true;
    193 	}
    194 
    195 	return false;
    196 }
    197 
    198 /*
    199  * Most of the following is copied (mostly verbatim) from the xz
    200  * distribution, from file src/xz/list.c
    201  */
    202 
    203 ///////////////////////////////////////////////////////////////////////////////
    204 //
    205 /// \file       list.c
    206 /// \brief      Listing information about .xz files
    207 //
    208 //  Author:     Lasse Collin
    209 //
    210 //  This file has been put into the public domain.
    211 //  You can do whatever you want with this file.
    212 //
    213 ///////////////////////////////////////////////////////////////////////////////
    214 
    215 
    216 /// Information about a .xz file
    217 typedef struct {
    218 	/// Combined Index of all Streams in the file
    219 	lzma_index *idx;
    220 
    221 	/// Total amount of Stream Padding
    222 	uint64_t stream_padding;
    223 
    224 	/// Highest memory usage so far
    225 	uint64_t memusage_max;
    226 
    227 	/// True if all Blocks so far have Compressed Size and
    228 	/// Uncompressed Size fields
    229 	bool all_have_sizes;
    230 
    231 	/// Oldest XZ Utils version that will decompress the file
    232 	uint32_t min_version;
    233 
    234 } xz_file_info;
    235 
    236 #define XZ_FILE_INFO_INIT { NULL, 0, 0, true, 50000002 }
    237 
    238 
    239 /// \brief      Parse the Index(es) from the given .xz file
    240 ///
    241 /// \param      xfi     Pointer to structure where the decoded information
    242 ///                     is stored.
    243 /// \param      pair    Input file
    244 ///
    245 /// \return     On success, false is returned. On error, true is returned.
    246 ///
    247 // TODO: This function is pretty big. liblzma should have a function that
    248 // takes a callback function to parse the Index(es) from a .xz file to make
    249 // it easy for applications.
    250 static bool
    251 parse_indexes(xz_file_info *xfi, int src_fd)
    252 {
    253 	struct stat st;
    254 
    255 	fstat(src_fd, &st);
    256 	if (st.st_size <= 0) {
    257 		return true;
    258 	}
    259 
    260 	if (st.st_size < 2 * LZMA_STREAM_HEADER_SIZE) {
    261 		return true;
    262 	}
    263 
    264 	io_buf buf;
    265 	lzma_stream_flags header_flags;
    266 	lzma_stream_flags footer_flags;
    267 	lzma_ret ret;
    268 
    269 	// lzma_stream for the Index decoder
    270 	lzma_stream strm = LZMA_STREAM_INIT;
    271 
    272 	// All Indexes decoded so far
    273 	lzma_index *combined_index = NULL;
    274 
    275 	// The Index currently being decoded
    276 	lzma_index *this_index = NULL;
    277 
    278 	// Current position in the file. We parse the file backwards so
    279 	// initialize it to point to the end of the file.
    280 	off_t pos = st.st_size;
    281 
    282 	// Each loop iteration decodes one Index.
    283 	do {
    284 		// Check that there is enough data left to contain at least
    285 		// the Stream Header and Stream Footer. This check cannot
    286 		// fail in the first pass of this loop.
    287 		if (pos < 2 * LZMA_STREAM_HEADER_SIZE) {
    288 			goto error;
    289 		}
    290 
    291 		pos -= LZMA_STREAM_HEADER_SIZE;
    292 		lzma_vli stream_padding = 0;
    293 
    294 		// Locate the Stream Footer. There may be Stream Padding which
    295 		// we must skip when reading backwards.
    296 		while (true) {
    297 			if (pos < LZMA_STREAM_HEADER_SIZE) {
    298 				goto error;
    299 			}
    300 
    301 			if (io_pread(src_fd, &buf,
    302 					LZMA_STREAM_HEADER_SIZE, pos))
    303 				goto error;
    304 
    305 			// Stream Padding is always a multiple of four bytes.
    306 			int i = 2;
    307 			if (buf.u32[i] != 0)
    308 				break;
    309 
    310 			// To avoid calling io_pread() for every four bytes
    311 			// of Stream Padding, take advantage that we read
    312 			// 12 bytes (LZMA_STREAM_HEADER_SIZE) already and
    313 			// check them too before calling io_pread() again.
    314 			do {
    315 				stream_padding += 4;
    316 				pos -= 4;
    317 				--i;
    318 			} while (i >= 0 && buf.u32[i] == 0);
    319 		}
    320 
    321 		// Decode the Stream Footer.
    322 		ret = lzma_stream_footer_decode(&footer_flags, buf.u8);
    323 		if (ret != LZMA_OK) {
    324 			goto error;
    325 		}
    326 
    327 		// Check that the Stream Footer doesn't specify something
    328 		// that we don't support. This can only happen if the xz
    329 		// version is older than liblzma and liblzma supports
    330 		// something new.
    331 		//
    332 		// It is enough to check Stream Footer. Stream Header must
    333 		// match when it is compared against Stream Footer with
    334 		// lzma_stream_flags_compare().
    335 		if (footer_flags.version != 0) {
    336 			goto error;
    337 		}
    338 
    339 		// Check that the size of the Index field looks sane.
    340 		lzma_vli index_size = footer_flags.backward_size;
    341 		if ((lzma_vli)(pos) < index_size + LZMA_STREAM_HEADER_SIZE) {
    342 			goto error;
    343 		}
    344 
    345 		// Set pos to the beginning of the Index.
    346 		pos -= index_size;
    347 
    348 		// Decode the Index.
    349 		ret = lzma_index_decoder(&strm, &this_index, UINT64_MAX);
    350 		if (ret != LZMA_OK) {
    351 			goto error;
    352 		}
    353 
    354 		do {
    355 			// Don't give the decoder more input than the
    356 			// Index size.
    357 			strm.avail_in = my_min(IO_BUFFER_SIZE, index_size);
    358 			if (io_pread(src_fd, &buf, strm.avail_in, pos))
    359 				goto error;
    360 
    361 			pos += strm.avail_in;
    362 			index_size -= strm.avail_in;
    363 
    364 			strm.next_in = buf.u8;
    365 			ret = lzma_code(&strm, LZMA_RUN);
    366 
    367 		} while (ret == LZMA_OK);
    368 
    369 		// If the decoding seems to be successful, check also that
    370 		// the Index decoder consumed as much input as indicated
    371 		// by the Backward Size field.
    372 		if (ret == LZMA_STREAM_END)
    373 			if (index_size != 0 || strm.avail_in != 0)
    374 				ret = LZMA_DATA_ERROR;
    375 
    376 		if (ret != LZMA_STREAM_END) {
    377 			// LZMA_BUFFER_ERROR means that the Index decoder
    378 			// would have liked more input than what the Index
    379 			// size should be according to Stream Footer.
    380 			// The message for LZMA_DATA_ERROR makes more
    381 			// sense in that case.
    382 			if (ret == LZMA_BUF_ERROR)
    383 				ret = LZMA_DATA_ERROR;
    384 
    385 			goto error;
    386 		}
    387 
    388 		// Decode the Stream Header and check that its Stream Flags
    389 		// match the Stream Footer.
    390 		pos -= footer_flags.backward_size + LZMA_STREAM_HEADER_SIZE;
    391 		if ((lzma_vli)(pos) < lzma_index_total_size(this_index)) {
    392 			goto error;
    393 		}
    394 
    395 		pos -= lzma_index_total_size(this_index);
    396 		if (io_pread(src_fd, &buf, LZMA_STREAM_HEADER_SIZE, pos))
    397 			goto error;
    398 
    399 		ret = lzma_stream_header_decode(&header_flags, buf.u8);
    400 		if (ret != LZMA_OK) {
    401 			goto error;
    402 		}
    403 
    404 		ret = lzma_stream_flags_compare(&header_flags, &footer_flags);
    405 		if (ret != LZMA_OK) {
    406 			goto error;
    407 		}
    408 
    409 		// Store the decoded Stream Flags into this_index. This is
    410 		// needed so that we can print which Check is used in each
    411 		// Stream.
    412 		ret = lzma_index_stream_flags(this_index, &footer_flags);
    413 		if (ret != LZMA_OK)
    414 			goto error;
    415 
    416 		// Store also the size of the Stream Padding field. It is
    417 		// needed to show the offsets of the Streams correctly.
    418 		ret = lzma_index_stream_padding(this_index, stream_padding);
    419 		if (ret != LZMA_OK)
    420 			goto error;
    421 
    422 		if (combined_index != NULL) {
    423 			// Append the earlier decoded Indexes
    424 			// after this_index.
    425 			ret = lzma_index_cat(
    426 					this_index, combined_index, NULL);
    427 			if (ret != LZMA_OK) {
    428 				goto error;
    429 			}
    430 		}
    431 
    432 		combined_index = this_index;
    433 		this_index = NULL;
    434 
    435 		xfi->stream_padding += stream_padding;
    436 
    437 	} while (pos > 0);
    438 
    439 	lzma_end(&strm);
    440 
    441 	// All OK. Make combined_index available to the caller.
    442 	xfi->idx = combined_index;
    443 	return false;
    444 
    445 error:
    446 	// Something went wrong, free the allocated memory.
    447 	lzma_end(&strm);
    448 	lzma_index_end(combined_index, NULL);
    449 	lzma_index_end(this_index, NULL);
    450 	return true;
    451 }
    452 
    453 /***************** end of copy form list.c *************************/
    454 
    455 /*
    456  * Small wrapper to extract total length of a file
    457  */
    458 off_t
    459 unxz_len(int fd)
    460 {
    461 	xz_file_info xfi = XZ_FILE_INFO_INIT;
    462 	if (!parse_indexes(&xfi, fd)) {
    463 		off_t res = lzma_index_uncompressed_size(xfi.idx);
    464 		lzma_index_end(xfi.idx, NULL);
    465 		return res;
    466 	}
    467 	return 0;
    468 }
    469 
    470