#include <sys/cdefs.h>
#include <stdarg.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <lzma.h>
static off_t
unxz(int i, int o, char *pre, size_t prelen, off_t *bytes_in)
{
lzma_stream strm = LZMA_STREAM_INIT;
static const int flags = LZMA_TELL_UNSUPPORTED_CHECK|LZMA_CONCATENATED;
lzma_ret ret;
lzma_action action = LZMA_RUN;
off_t bytes_out, bp;
uint8_t ibuf[BUFSIZ];
uint8_t obuf[BUFSIZ];
if (bytes_in == NULL)
bytes_in = &bp;
strm.next_in = ibuf;
memcpy(ibuf, pre, prelen);
strm.avail_in = read(i, ibuf + prelen, sizeof(ibuf) - prelen);
if (strm.avail_in == (size_t)-1)
maybe_err("read failed");
infile_newdata(strm.avail_in);
strm.avail_in += prelen;
*bytes_in = strm.avail_in;
if ((ret = lzma_stream_decoder(&strm, UINT64_MAX, flags)) != LZMA_OK)
maybe_errx("Can't initialize decoder (%d)", ret);
strm.next_out = NULL;
strm.avail_out = 0;
if ((ret = lzma_code(&strm, LZMA_RUN)) != LZMA_OK)
maybe_errx("Can't read headers (%d)", ret);
bytes_out = 0;
strm.next_out = obuf;
strm.avail_out = sizeof(obuf);
for (;;) {
check_siginfo();
if (strm.avail_in == 0) {
strm.next_in = ibuf;
strm.avail_in = read(i, ibuf, sizeof(ibuf));
switch (strm.avail_in) {
case (size_t)-1:
maybe_err("read failed");
case 0:
action = LZMA_FINISH;
break;
default:
infile_newdata(strm.avail_in);
*bytes_in += strm.avail_in;
break;
}
}
ret = lzma_code(&strm, action);
if (strm.avail_out == 0 || ret != LZMA_OK) {
const size_t write_size = sizeof(obuf) - strm.avail_out;
if (write(o, obuf, write_size) != (ssize_t)write_size)
maybe_err("write failed");
strm.next_out = obuf;
strm.avail_out = sizeof(obuf);
bytes_out += write_size;
}
if (ret != LZMA_OK) {
if (ret == LZMA_STREAM_END) {
if (strm.avail_in != 0 || read(i, ibuf, 1))
ret = LZMA_DATA_ERROR;
else {
lzma_end(&strm);
return bytes_out;
}
}
const char *msg;
switch (ret) {
case LZMA_MEM_ERROR:
msg = strerror(ENOMEM);
break;
case LZMA_FORMAT_ERROR:
msg = "File format not recognized";
break;
case LZMA_OPTIONS_ERROR:
msg = "Unsupported compression options";
break;
case LZMA_DATA_ERROR:
msg = "File is corrupt";
break;
case LZMA_BUF_ERROR:
msg = "Unexpected end of input";
break;
case LZMA_MEMLIMIT_ERROR:
msg = "Reached memory limit";
break;
default:
maybe_errx("Unknown error (%d)", ret);
break;
}
maybe_errx("%s", msg);
}
}
}
#include <stdbool.h>
#define my_min(A,B) ((A)<(B)?(A):(B))
#if BUFSIZ <= 1024
# define IO_BUFFER_SIZE 8192
#else
# define IO_BUFFER_SIZE (BUFSIZ & ~7U)
#endif
typedef union {
uint8_t u8[IO_BUFFER_SIZE];
uint32_t u32[IO_BUFFER_SIZE / sizeof(uint32_t)];
uint64_t u64[IO_BUFFER_SIZE / sizeof(uint64_t)];
} io_buf;
static bool
io_pread(int fd, io_buf *buf, size_t size, off_t pos)
{
if (lseek(fd, pos, SEEK_SET) != pos) {
return true;
}
const size_t amount = read(fd, buf, size);
if (amount == SIZE_MAX)
return true;
if (amount != size) {
return true;
}
return false;
}
typedef struct {
lzma_index *idx;
uint64_t stream_padding;
uint64_t memusage_max;
bool all_have_sizes;
uint32_t min_version;
} xz_file_info;
#define XZ_FILE_INFO_INIT { NULL, 0, 0, true, 50000002 }
static bool
parse_indexes(xz_file_info *xfi, int src_fd)
{
struct stat st;
if (fstat(src_fd, &st) != 0) {
return true;
}
if (st.st_size < 2 * LZMA_STREAM_HEADER_SIZE) {
return true;
}
io_buf buf;
lzma_stream_flags header_flags;
lzma_stream_flags footer_flags;
lzma_ret ret;
lzma_stream strm = LZMA_STREAM_INIT;
lzma_index *combined_index = NULL;
lzma_index *this_index = NULL;
off_t pos = st.st_size;
do {
if (pos < 2 * LZMA_STREAM_HEADER_SIZE) {
goto error;
}
pos -= LZMA_STREAM_HEADER_SIZE;
lzma_vli stream_padding = 0;
while (true) {
if (pos < LZMA_STREAM_HEADER_SIZE) {
goto error;
}
if (io_pread(src_fd, &buf,
LZMA_STREAM_HEADER_SIZE, pos))
goto error;
int i = 2;
if (buf.u32[i] != 0)
break;
do {
stream_padding += 4;
pos -= 4;
--i;
} while (i >= 0 && buf.u32[i] == 0);
}
ret = lzma_stream_footer_decode(&footer_flags, buf.u8);
if (ret != LZMA_OK) {
goto error;
}
if (footer_flags.version != 0) {
goto error;
}
lzma_vli index_size = footer_flags.backward_size;
if ((lzma_vli)(pos) < index_size + LZMA_STREAM_HEADER_SIZE) {
goto error;
}
pos -= index_size;
ret = lzma_index_decoder(&strm, &this_index, UINT64_MAX);
if (ret != LZMA_OK) {
goto error;
}
do {
strm.avail_in = my_min(IO_BUFFER_SIZE, index_size);
if (io_pread(src_fd, &buf, strm.avail_in, pos))
goto error;
pos += strm.avail_in;
index_size -= strm.avail_in;
strm.next_in = buf.u8;
ret = lzma_code(&strm, LZMA_RUN);
} while (ret == LZMA_OK);
if (ret == LZMA_STREAM_END)
if (index_size != 0 || strm.avail_in != 0)
ret = LZMA_DATA_ERROR;
if (ret != LZMA_STREAM_END) {
if (ret == LZMA_BUF_ERROR)
ret = LZMA_DATA_ERROR;
goto error;
}
pos -= footer_flags.backward_size + LZMA_STREAM_HEADER_SIZE;
if ((lzma_vli)(pos) < lzma_index_total_size(this_index)) {
goto error;
}
pos -= lzma_index_total_size(this_index);
if (io_pread(src_fd, &buf, LZMA_STREAM_HEADER_SIZE, pos))
goto error;
ret = lzma_stream_header_decode(&header_flags, buf.u8);
if (ret != LZMA_OK) {
goto error;
}
ret = lzma_stream_flags_compare(&header_flags, &footer_flags);
if (ret != LZMA_OK) {
goto error;
}
ret = lzma_index_stream_flags(this_index, &footer_flags);
if (ret != LZMA_OK)
goto error;
ret = lzma_index_stream_padding(this_index, stream_padding);
if (ret != LZMA_OK)
goto error;
if (combined_index != NULL) {
ret = lzma_index_cat(
this_index, combined_index, NULL);
if (ret != LZMA_OK) {
goto error;
}
}
combined_index = this_index;
this_index = NULL;
xfi->stream_padding += stream_padding;
} while (pos > 0);
lzma_end(&strm);
xfi->idx = combined_index;
return false;
error:
lzma_end(&strm);
lzma_index_end(combined_index, NULL);
lzma_index_end(this_index, NULL);
return true;
}
off_t
unxz_len(int fd)
{
xz_file_info xfi = XZ_FILE_INFO_INIT;
if (!parse_indexes(&xfi, fd)) {
off_t res = lzma_index_uncompressed_size(xfi.idx);
lzma_index_end(xfi.idx, NULL);
return res;
}
return 0;
}