2007-12-08 22:42:33 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2008-11-19 21:52:24 +00:00
|
|
|
/// \file xzdec.c
|
2008-11-20 16:05:52 +00:00
|
|
|
/// \brief Simple single-threaded tool to uncompress .xz or .lzma files
|
2007-12-08 22:42:33 +00:00
|
|
|
//
|
2009-04-13 08:27:40 +00:00
|
|
|
// Author: Lasse Collin
|
2007-12-08 22:42:33 +00:00
|
|
|
//
|
2009-04-13 08:27:40 +00:00
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
2007-12-08 22:42:33 +00:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "sysdefs.h"
|
2009-01-31 09:01:48 +00:00
|
|
|
#include "lzma.h"
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2009-05-22 13:40:50 +00:00
|
|
|
#include <stdarg.h>
|
2008-11-19 18:46:52 +00:00
|
|
|
#include <errno.h>
|
2007-12-08 22:42:33 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2009-09-19 06:47:30 +00:00
|
|
|
#include "getopt.h"
|
|
|
|
#include "tuklib_progname.h"
|
|
|
|
#include "tuklib_exit.h"
|
|
|
|
|
|
|
|
#ifdef TUKLIB_DOSLIKE
|
2008-01-18 12:17:37 +00:00
|
|
|
# include <fcntl.h>
|
2009-02-13 15:29:02 +00:00
|
|
|
# include <io.h>
|
2008-01-18 12:17:37 +00:00
|
|
|
#endif
|
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-11-20 16:05:52 +00:00
|
|
|
#ifdef LZMADEC
|
|
|
|
# define TOOL_FORMAT "lzma"
|
|
|
|
#else
|
|
|
|
# define TOOL_FORMAT "xz"
|
|
|
|
#endif
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// Number of bytes to use memory at maximum
|
2008-11-19 18:46:52 +00:00
|
|
|
static uint64_t memlimit;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2010-03-07 11:29:28 +00:00
|
|
|
/// Total amount of physical RAM
|
|
|
|
static uint64_t total_ram;
|
|
|
|
|
2009-05-22 13:40:50 +00:00
|
|
|
/// Error messages are suppressed if this is zero, which is the case when
|
|
|
|
/// --quiet has been given at least twice.
|
|
|
|
static unsigned int display_errors = 2;
|
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2009-05-22 13:40:50 +00:00
|
|
|
static void lzma_attribute((format(printf, 1, 2)))
|
|
|
|
my_errorf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
|
|
|
if (display_errors) {
|
2009-09-19 06:47:30 +00:00
|
|
|
fprintf(stderr, "%s: ", progname);
|
2009-05-22 13:40:50 +00:00
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
static void lzma_attribute((noreturn))
|
|
|
|
help(void)
|
|
|
|
{
|
2010-03-07 11:50:23 +00:00
|
|
|
// Round up to the next MiB and do it correctly also with UINT64_MAX.
|
|
|
|
const uint64_t mem_mib = (memlimit >> 20)
|
|
|
|
+ ((memlimit & ((UINT32_C(1) << 20) - 1)) != 0);
|
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
printf(
|
|
|
|
"Usage: %s [OPTION]... [FILE]...\n"
|
2008-11-20 16:05:52 +00:00
|
|
|
"Uncompress files in the ." TOOL_FORMAT " format to the standard output.\n"
|
2007-12-08 22:42:33 +00:00
|
|
|
"\n"
|
|
|
|
" -c, --stdout (ignored)\n"
|
|
|
|
" -d, --decompress (ignored)\n"
|
|
|
|
" -k, --keep (ignored)\n"
|
2009-06-04 20:26:47 +00:00
|
|
|
" -M, --memory=NUM use NUM bytes of memory at maximum (0 means default)\n"
|
2009-05-22 13:40:50 +00:00
|
|
|
" -q, --quiet specify *twice* to suppress errors\n"
|
|
|
|
" -Q, --no-warn (ignored)\n"
|
2007-12-08 22:42:33 +00:00
|
|
|
" -h, --help display this help and exit\n"
|
2009-05-22 13:40:50 +00:00
|
|
|
" -V, --version display the version number and exit\n"
|
2007-12-08 22:42:33 +00:00
|
|
|
"\n"
|
|
|
|
"With no FILE, or when FILE is -, read standard input.\n"
|
|
|
|
"\n"
|
2009-09-12 11:07:36 +00:00
|
|
|
"On this system and configuration, this program will use a maximum of roughly\n"
|
2009-05-22 12:11:52 +00:00
|
|
|
"%" PRIu64 " MiB RAM.\n"
|
2007-12-08 22:42:33 +00:00
|
|
|
"\n"
|
2009-05-22 13:40:50 +00:00
|
|
|
"Report bugs to <" PACKAGE_BUGREPORT "> (in English or Finnish).\n"
|
2010-03-07 11:50:23 +00:00
|
|
|
PACKAGE_NAME " home page: <" PACKAGE_URL ">\n", progname, mem_mib);
|
2009-09-19 06:47:30 +00:00
|
|
|
tuklib_exit(EXIT_SUCCESS, EXIT_FAILURE, display_errors);
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void lzma_attribute((noreturn))
|
|
|
|
version(void)
|
|
|
|
{
|
2009-07-02 11:30:38 +00:00
|
|
|
printf(TOOL_FORMAT "dec (" PACKAGE_NAME ") " LZMA_VERSION_STRING "\n"
|
2008-11-20 16:05:52 +00:00
|
|
|
"liblzma %s\n", lzma_version_string());
|
|
|
|
|
2009-09-19 06:47:30 +00:00
|
|
|
tuklib_exit(EXIT_SUCCESS, EXIT_FAILURE, display_errors);
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-04 20:26:47 +00:00
|
|
|
/// Find out the amount of physical memory (RAM) in the system, and set
|
|
|
|
/// the memory usage limit to the given percentage of RAM.
|
2007-12-08 22:42:33 +00:00
|
|
|
static void
|
2009-06-04 20:26:47 +00:00
|
|
|
memlimit_set_percentage(uint32_t percentage)
|
2007-12-08 22:42:33 +00:00
|
|
|
{
|
2010-03-07 11:29:28 +00:00
|
|
|
memlimit = percentage * total_ram / 100;
|
2009-06-04 20:26:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Set the memory usage limit to give number of bytes. Zero is a special
|
|
|
|
/// value to indicate the default limit.
|
|
|
|
static void
|
|
|
|
memlimit_set(uint64_t new_memlimit)
|
|
|
|
{
|
2010-03-07 11:29:28 +00:00
|
|
|
if (new_memlimit != 0) {
|
2009-06-04 20:26:47 +00:00
|
|
|
memlimit = new_memlimit;
|
2010-03-07 11:29:28 +00:00
|
|
|
} else {
|
|
|
|
memlimit = 40 * total_ram / 100;
|
|
|
|
if (memlimit < UINT64_C(80) * 1024 * 1024) {
|
|
|
|
memlimit = 80 * total_ram / 100;
|
|
|
|
if (memlimit > UINT64_C(80) * 1024 * 1024)
|
|
|
|
memlimit = UINT64_C(80) * 1024 * 1024;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Get the total amount of physical RAM and set the memory usage limit
|
|
|
|
/// to the default value.
|
|
|
|
static void
|
|
|
|
memlimit_init(void)
|
|
|
|
{
|
|
|
|
// If we cannot determine the amount of RAM, use the assumption
|
|
|
|
// defined by the configure script.
|
|
|
|
total_ram = lzma_physmem();
|
|
|
|
if (total_ram == 0)
|
|
|
|
total_ram = (uint64_t)(ASSUME_RAM) * 1024 * 1024;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2010-03-07 11:29:28 +00:00
|
|
|
memlimit_set(0);
|
2007-12-08 22:42:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-04 20:26:47 +00:00
|
|
|
/// \brief Convert a string to uint64_t
|
2007-12-08 22:42:33 +00:00
|
|
|
///
|
2008-11-19 21:52:24 +00:00
|
|
|
/// This is rudely copied from src/xz/util.c and modified a little. :-(
|
2010-06-11 18:43:28 +00:00
|
|
|
/// Since this function is used only for parsing the memory usage limit,
|
|
|
|
/// this cheats a little and saturates too big values to UINT64_MAX instead
|
|
|
|
/// of giving an error.
|
2007-12-08 22:42:33 +00:00
|
|
|
///
|
2009-06-04 20:26:47 +00:00
|
|
|
/// \param max Return value when the string "max" was specified.
|
|
|
|
///
|
2008-11-20 09:01:29 +00:00
|
|
|
static uint64_t
|
2009-06-04 20:26:47 +00:00
|
|
|
str_to_uint64(const char *value, uint64_t max)
|
2007-12-08 22:42:33 +00:00
|
|
|
{
|
2008-11-20 09:01:29 +00:00
|
|
|
uint64_t result = 0;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2009-05-21 14:22:01 +00:00
|
|
|
// Accept special value "max".
|
|
|
|
if (strcmp(value, "max") == 0)
|
2009-06-04 20:26:47 +00:00
|
|
|
return max;
|
2009-05-21 14:22:01 +00:00
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
if (*value < '0' || *value > '9') {
|
2009-06-04 20:26:47 +00:00
|
|
|
my_errorf("%s: Value is not a non-negative decimal integer",
|
|
|
|
value);
|
2008-11-20 16:05:52 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
// Don't overflow.
|
2010-06-11 18:43:28 +00:00
|
|
|
if (result > UINT64_MAX / 10)
|
2008-11-20 09:01:29 +00:00
|
|
|
return UINT64_MAX;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
result *= 10;
|
2010-06-11 18:43:28 +00:00
|
|
|
|
|
|
|
// Another overflow check
|
|
|
|
const uint32_t add = *value - '0';
|
|
|
|
if (UINT64_MAX - add < result)
|
|
|
|
return UINT64_MAX;
|
|
|
|
|
|
|
|
result += add;
|
2007-12-08 22:42:33 +00:00
|
|
|
++value;
|
|
|
|
} while (*value >= '0' && *value <= '9');
|
|
|
|
|
|
|
|
if (*value != '\0') {
|
|
|
|
// Look for suffix.
|
Treat all integer multiplier suffixes as base-2.
Originally both base-2 and base-10 were supported, but since
there seems to be little need for base-10 in XZ Utils, treat
everything as base-2 and also be more relaxed about the case
of the first letter of the suffix. Now xz will accept e.g.
KiB, Ki, k, K, kB, and KB, and interpret them all as 1024. The
recommended spelling of the suffixes are still KiB, MiB, and GiB.
2010-03-07 11:59:32 +00:00
|
|
|
uint64_t multiplier = 0;
|
|
|
|
if (*value == 'k' || *value == 'K')
|
|
|
|
multiplier = UINT64_C(1) << 10;
|
|
|
|
else if (*value == 'm' || *value == 'M')
|
|
|
|
multiplier = UINT64_C(1) << 20;
|
|
|
|
else if (*value == 'g' || *value == 'G')
|
|
|
|
multiplier = UINT64_C(1) << 30;
|
|
|
|
|
|
|
|
++value;
|
|
|
|
|
|
|
|
// Allow also e.g. Ki, KiB, and KB.
|
|
|
|
if (*value != '\0' && strcmp(value, "i") != 0
|
|
|
|
&& strcmp(value, "iB") != 0
|
|
|
|
&& strcmp(value, "B") != 0)
|
|
|
|
multiplier = 0;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
if (multiplier == 0) {
|
Treat all integer multiplier suffixes as base-2.
Originally both base-2 and base-10 were supported, but since
there seems to be little need for base-10 in XZ Utils, treat
everything as base-2 and also be more relaxed about the case
of the first letter of the suffix. Now xz will accept e.g.
KiB, Ki, k, K, kB, and KB, and interpret them all as 1024. The
recommended spelling of the suffixes are still KiB, MiB, and GiB.
2010-03-07 11:59:32 +00:00
|
|
|
my_errorf("%s: Invalid suffix", value - 1);
|
2008-11-20 16:05:52 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't overflow here either.
|
2008-11-20 09:01:29 +00:00
|
|
|
if (result > UINT64_MAX / multiplier)
|
|
|
|
result = UINT64_MAX;
|
2008-06-11 12:08:44 +00:00
|
|
|
else
|
|
|
|
result *= multiplier;
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Parses command line options.
|
|
|
|
static void
|
|
|
|
parse_options(int argc, char **argv)
|
|
|
|
{
|
2009-07-08 19:50:16 +00:00
|
|
|
static const char short_opts[] = "cdkM:hqQV";
|
2007-12-08 22:42:33 +00:00
|
|
|
static const struct option long_opts[] = {
|
|
|
|
{ "stdout", no_argument, NULL, 'c' },
|
|
|
|
{ "to-stdout", no_argument, NULL, 'c' },
|
|
|
|
{ "decompress", no_argument, NULL, 'd' },
|
|
|
|
{ "uncompress", no_argument, NULL, 'd' },
|
|
|
|
{ "keep", no_argument, NULL, 'k' },
|
|
|
|
{ "memory", required_argument, NULL, 'M' },
|
2009-05-22 13:40:50 +00:00
|
|
|
{ "quiet", no_argument, NULL, 'q' },
|
|
|
|
{ "no-warn", no_argument, NULL, 'Q' },
|
2007-12-08 22:42:33 +00:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL))
|
|
|
|
!= -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'c':
|
|
|
|
case 'd':
|
2008-11-20 16:05:52 +00:00
|
|
|
case 'k':
|
2009-05-22 13:40:50 +00:00
|
|
|
case 'Q':
|
2007-12-08 22:42:33 +00:00
|
|
|
break;
|
|
|
|
|
2009-06-04 20:26:47 +00:00
|
|
|
case 'M': {
|
|
|
|
// Support specifying the limit as a percentage of
|
|
|
|
// installed physical RAM.
|
|
|
|
const size_t len = strlen(optarg);
|
|
|
|
if (len > 0 && optarg[len - 1] == '%') {
|
|
|
|
// Memory limit is a percentage of total
|
|
|
|
// installed RAM.
|
|
|
|
optarg[len - 1] = '\0';
|
|
|
|
const uint64_t percentage
|
|
|
|
= str_to_uint64(optarg, 100);
|
|
|
|
if (percentage < 1 || percentage > 100) {
|
|
|
|
my_errorf("Percentage must be in "
|
|
|
|
"the range [1, 100]");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
memlimit_set_percentage(percentage);
|
|
|
|
} else {
|
|
|
|
memlimit_set(str_to_uint64(
|
|
|
|
optarg, UINT64_MAX));
|
|
|
|
}
|
2008-11-19 18:46:52 +00:00
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
break;
|
2009-06-04 20:26:47 +00:00
|
|
|
}
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2009-05-22 13:40:50 +00:00
|
|
|
case 'q':
|
|
|
|
if (display_errors > 0)
|
|
|
|
--display_errors;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
case 'h':
|
|
|
|
help();
|
|
|
|
|
|
|
|
case 'V':
|
|
|
|
version();
|
|
|
|
|
|
|
|
default:
|
2008-11-20 16:05:52 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2008-11-20 16:05:52 +00:00
|
|
|
uncompress(lzma_stream *strm, FILE *file, const char *filename)
|
2007-12-08 22:42:33 +00:00
|
|
|
{
|
|
|
|
lzma_ret ret;
|
|
|
|
|
2008-11-20 16:05:52 +00:00
|
|
|
// Initialize the decoder
|
|
|
|
#ifdef LZMADEC
|
|
|
|
ret = lzma_alone_decoder(strm, memlimit);
|
|
|
|
#else
|
|
|
|
ret = lzma_stream_decoder(strm, memlimit, LZMA_CONCATENATED);
|
|
|
|
#endif
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-11-20 16:05:52 +00:00
|
|
|
// The only reasonable error here is LZMA_MEM_ERROR.
|
|
|
|
// FIXME: Maybe also LZMA_MEMLIMIT_ERROR in future?
|
2007-12-08 22:42:33 +00:00
|
|
|
if (ret != LZMA_OK) {
|
2009-05-22 13:40:50 +00:00
|
|
|
my_errorf("%s", ret == LZMA_MEM_ERROR ? strerror(ENOMEM)
|
2009-08-13 09:42:36 +00:00
|
|
|
: "Internal error (bug)");
|
2008-11-20 16:05:52 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
2008-11-20 16:05:52 +00:00
|
|
|
// Input and output buffers
|
|
|
|
uint8_t in_buf[BUFSIZ];
|
|
|
|
uint8_t out_buf[BUFSIZ];
|
2008-08-28 19:53:15 +00:00
|
|
|
|
2008-11-20 16:05:52 +00:00
|
|
|
strm->avail_in = 0;
|
|
|
|
strm->next_out = out_buf;
|
|
|
|
strm->avail_out = BUFSIZ;
|
2008-08-28 19:53:15 +00:00
|
|
|
|
|
|
|
lzma_action action = LZMA_RUN;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
while (true) {
|
2008-11-20 16:05:52 +00:00
|
|
|
if (strm->avail_in == 0) {
|
|
|
|
strm->next_in = in_buf;
|
|
|
|
strm->avail_in = fread(in_buf, 1, BUFSIZ, file);
|
2008-08-28 19:53:15 +00:00
|
|
|
|
|
|
|
if (ferror(file)) {
|
|
|
|
// POSIX says that fread() sets errno if
|
|
|
|
// an error occurred. ferror() doesn't
|
|
|
|
// touch errno.
|
2009-05-22 13:40:50 +00:00
|
|
|
my_errorf("%s: Error reading input file: %s",
|
|
|
|
filename, strerror(errno));
|
2008-11-20 16:05:52 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2008-08-28 19:53:15 +00:00
|
|
|
}
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-11-20 16:05:52 +00:00
|
|
|
#ifndef LZMADEC
|
|
|
|
// When using LZMA_CONCATENATED, we need to tell
|
|
|
|
// liblzma when it has got all the input.
|
2008-08-28 19:53:15 +00:00
|
|
|
if (feof(file))
|
|
|
|
action = LZMA_FINISH;
|
2008-11-20 16:05:52 +00:00
|
|
|
#endif
|
2008-08-28 19:53:15 +00:00
|
|
|
}
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-11-20 16:05:52 +00:00
|
|
|
ret = lzma_code(strm, action);
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
// Write and check write error before checking decoder error.
|
|
|
|
// This way as much data as possible gets written to output
|
2008-08-28 19:53:15 +00:00
|
|
|
// even if decoder detected an error.
|
2008-11-20 16:05:52 +00:00
|
|
|
if (strm->avail_out == 0 || ret != LZMA_OK) {
|
|
|
|
const size_t write_size = BUFSIZ - strm->avail_out;
|
2008-08-28 19:53:15 +00:00
|
|
|
|
|
|
|
if (fwrite(out_buf, 1, write_size, stdout)
|
|
|
|
!= write_size) {
|
|
|
|
// Wouldn't be a surprise if writing to stderr
|
|
|
|
// would fail too but at least try to show an
|
|
|
|
// error message.
|
2009-05-22 13:40:50 +00:00
|
|
|
my_errorf("Cannot write to standard output: "
|
|
|
|
"%s", strerror(errno));
|
2008-11-20 16:05:52 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2008-08-28 19:53:15 +00:00
|
|
|
}
|
|
|
|
|
2008-11-20 16:05:52 +00:00
|
|
|
strm->next_out = out_buf;
|
|
|
|
strm->avail_out = BUFSIZ;
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != LZMA_OK) {
|
2008-11-20 16:05:52 +00:00
|
|
|
if (ret == LZMA_STREAM_END) {
|
|
|
|
#ifdef LZMADEC
|
|
|
|
// Check that there's no trailing garbage.
|
|
|
|
if (strm->avail_in != 0
|
|
|
|
|| fread(in_buf, 1, 1, file)
|
|
|
|
!= 0
|
|
|
|
|| !feof(file))
|
|
|
|
ret = LZMA_DATA_ERROR;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
#else
|
|
|
|
// lzma_stream_decoder() already guarantees
|
|
|
|
// that there's no trailing garbage.
|
|
|
|
assert(strm->avail_in == 0);
|
|
|
|
assert(action == LZMA_FINISH);
|
|
|
|
assert(feof(file));
|
2007-12-08 22:42:33 +00:00
|
|
|
return;
|
2008-11-20 16:05:52 +00:00
|
|
|
#endif
|
|
|
|
}
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-11-20 16:05:52 +00:00
|
|
|
const char *msg;
|
2007-12-08 22:42:33 +00:00
|
|
|
switch (ret) {
|
|
|
|
case LZMA_MEM_ERROR:
|
2008-11-20 16:05:52 +00:00
|
|
|
msg = strerror(ENOMEM);
|
|
|
|
break;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-08-28 19:53:15 +00:00
|
|
|
case LZMA_MEMLIMIT_ERROR:
|
2008-11-20 16:05:52 +00:00
|
|
|
msg = "Memory usage limit reached";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LZMA_FORMAT_ERROR:
|
|
|
|
msg = "File format not recognized";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LZMA_OPTIONS_ERROR:
|
|
|
|
// FIXME: Better message?
|
|
|
|
msg = "Unsupported compression options";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LZMA_DATA_ERROR:
|
|
|
|
msg = "File is corrupt";
|
|
|
|
break;
|
2008-08-28 19:53:15 +00:00
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
case LZMA_BUF_ERROR:
|
2008-11-20 16:05:52 +00:00
|
|
|
msg = "Unexpected end of input";
|
2007-12-08 22:42:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2009-08-13 09:42:36 +00:00
|
|
|
msg = "Internal error (bug)";
|
2008-11-20 16:05:52 +00:00
|
|
|
break;
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
2008-11-20 16:05:52 +00:00
|
|
|
|
2009-05-22 13:40:50 +00:00
|
|
|
my_errorf("%s: %s", filename, msg);
|
2008-11-20 16:05:52 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2009-09-19 06:47:30 +00:00
|
|
|
// Initialize progname which we will be used in error messages.
|
|
|
|
tuklib_progname_init(argv);
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2009-06-04 20:26:47 +00:00
|
|
|
// Set the default memory usage limit. This is needed before parsing
|
|
|
|
// the command line arguments.
|
2010-03-07 11:29:28 +00:00
|
|
|
memlimit_init();
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-11-20 16:05:52 +00:00
|
|
|
// Parse the command line options.
|
2007-12-08 22:42:33 +00:00
|
|
|
parse_options(argc, argv);
|
|
|
|
|
2008-11-20 16:05:52 +00:00
|
|
|
// The same lzma_stream is used for all files that we decode. This way
|
|
|
|
// we don't need to reallocate memory for every file if they use same
|
|
|
|
// compression settings.
|
|
|
|
lzma_stream strm = LZMA_STREAM_INIT;
|
|
|
|
|
|
|
|
// Some systems require setting stdin and stdout to binary mode.
|
2009-09-19 06:47:30 +00:00
|
|
|
#ifdef TUKLIB_DOSLIKE
|
2008-01-18 12:17:37 +00:00
|
|
|
setmode(fileno(stdin), O_BINARY);
|
|
|
|
setmode(fileno(stdout), O_BINARY);
|
|
|
|
#endif
|
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
if (optind == argc) {
|
2008-11-20 16:05:52 +00:00
|
|
|
// No filenames given, decode from stdin.
|
|
|
|
uncompress(&strm, stdin, "(stdin)");
|
2007-12-08 22:42:33 +00:00
|
|
|
} else {
|
2008-11-20 16:05:52 +00:00
|
|
|
// Loop through the filenames given on the command line.
|
2007-12-08 22:42:33 +00:00
|
|
|
do {
|
2008-11-20 16:05:52 +00:00
|
|
|
// "-" indicates stdin.
|
2007-12-08 22:42:33 +00:00
|
|
|
if (strcmp(argv[optind], "-") == 0) {
|
2008-11-20 16:05:52 +00:00
|
|
|
uncompress(&strm, stdin, "(stdin)");
|
2007-12-08 22:42:33 +00:00
|
|
|
} else {
|
2008-11-20 16:05:52 +00:00
|
|
|
FILE *file = fopen(argv[optind], "rb");
|
2007-12-08 22:42:33 +00:00
|
|
|
if (file == NULL) {
|
2009-05-22 13:40:50 +00:00
|
|
|
my_errorf("%s: %s", argv[optind],
|
2007-12-08 22:42:33 +00:00
|
|
|
strerror(errno));
|
2008-11-20 16:05:52 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
2008-11-20 16:05:52 +00:00
|
|
|
uncompress(&strm, file, argv[optind]);
|
2007-12-08 22:42:33 +00:00
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
} while (++optind < argc);
|
|
|
|
}
|
|
|
|
|
2008-01-15 15:46:59 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// Free the memory only when debugging. Freeing wastes some time,
|
|
|
|
// but allows detecting possible memory leaks with Valgrind.
|
|
|
|
lzma_end(&strm);
|
|
|
|
#endif
|
|
|
|
|
2009-09-19 06:47:30 +00:00
|
|
|
tuklib_exit(EXIT_SUCCESS, EXIT_FAILURE, display_errors);
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|