1
0
Fork 0
mirror of https://git.tukaani.org/xz.git synced 2024-04-04 12:36:23 +02:00
xz-archive/src/xzdec/xzdec.c

324 lines
7.4 KiB
C
Raw Normal View History

2007-12-08 23:42:33 +01:00
///////////////////////////////////////////////////////////////////////////////
//
/// \file xzdec.c
/// \brief Simple single-threaded tool to uncompress .xz or .lzma files
2007-12-08 23:42:33 +01:00
//
// Author: Lasse Collin
2007-12-08 23:42:33 +01:00
//
// This file has been put into the public domain.
// You can do whatever you want with this file.
2007-12-08 23:42:33 +01:00
//
///////////////////////////////////////////////////////////////////////////////
#include "sysdefs.h"
#include "lzma.h"
2007-12-08 23:42:33 +01:00
#include <stdarg.h>
#include <errno.h>
2007-12-08 23:42:33 +01:00
#include <stdio.h>
#include <unistd.h>
#include "getopt.h"
#include "tuklib_progname.h"
#include "tuklib_exit.h"
#ifdef TUKLIB_DOSLIKE
# include <fcntl.h>
# include <io.h>
#endif
2007-12-08 23:42:33 +01:00
#ifdef LZMADEC
# define TOOL_FORMAT "lzma"
#else
# define TOOL_FORMAT "xz"
#endif
2007-12-08 23:42:33 +01:00
/// Error messages are suppressed if this is zero, which is the case when
/// --quiet has been given at least twice.
static int display_errors = 2;
2007-12-08 23:42:33 +01:00
static void lzma_attribute((__format__(__printf__, 1, 2)))
my_errorf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (display_errors) {
fprintf(stderr, "%s: ", progname);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
}
va_end(ap);
return;
}
static void lzma_attribute((__noreturn__))
2007-12-08 23:42:33 +01:00
help(void)
{
printf(
"Usage: %s [OPTION]... [FILE]...\n"
"Decompress files in the ." TOOL_FORMAT " format to standard output.\n"
2007-12-08 23:42:33 +01:00
"\n"
" -d, --decompress (ignored, only decompression is supported)\n"
" -k, --keep (ignored, files are never deleted)\n"
" -c, --stdout (ignored, output is always written to standard output)\n"
" -q, --quiet specify *twice* to suppress errors\n"
" -Q, --no-warn (ignored, the exit status 2 is never used)\n"
2007-12-08 23:42:33 +01:00
" -h, --help display this help and exit\n"
" -V, --version display the version number and exit\n"
2007-12-08 23:42:33 +01:00
"\n"
"With no FILE, or when FILE is -, read standard input.\n"
"\n"
"Report bugs to <" PACKAGE_BUGREPORT "> (in English or Finnish).\n"
PACKAGE_NAME " home page: <" PACKAGE_URL ">\n", progname);
tuklib_exit(EXIT_SUCCESS, EXIT_FAILURE, display_errors);
2007-12-08 23:42:33 +01:00
}
static void lzma_attribute((__noreturn__))
2007-12-08 23:42:33 +01:00
version(void)
{
printf(TOOL_FORMAT "dec (" PACKAGE_NAME ") " LZMA_VERSION_STRING "\n"
"liblzma %s\n", lzma_version_string());
tuklib_exit(EXIT_SUCCESS, EXIT_FAILURE, display_errors);
2007-12-08 23:42:33 +01:00
}
/// Parses command line options.
static void
parse_options(int argc, char **argv)
{
static const char short_opts[] = "cdkM:hqQV";
2007-12-08 23:42:33 +01: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' },
{ "quiet", no_argument, NULL, 'q' },
{ "no-warn", no_argument, NULL, 'Q' },
2007-12-08 23:42:33 +01: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':
case 'k':
case 'Q':
2007-12-08 23:42:33 +01:00
break;
case 'q':
if (display_errors > 0)
--display_errors;
break;
2007-12-08 23:42:33 +01:00
case 'h':
help();
case 'V':
version();
default:
exit(EXIT_FAILURE);
2007-12-08 23:42:33 +01:00
}
}
return;
}
static void
uncompress(lzma_stream *strm, FILE *file, const char *filename)
2007-12-08 23:42:33 +01:00
{
lzma_ret ret;
// Initialize the decoder
#ifdef LZMADEC
ret = lzma_alone_decoder(strm, UINT64_MAX);
#else
ret = lzma_stream_decoder(strm, UINT64_MAX, LZMA_CONCATENATED);
#endif
2007-12-08 23:42:33 +01:00
// The only reasonable error here is LZMA_MEM_ERROR.
2007-12-08 23:42:33 +01:00
if (ret != LZMA_OK) {
my_errorf("%s", ret == LZMA_MEM_ERROR ? strerror(ENOMEM)
: "Internal error (bug)");
exit(EXIT_FAILURE);
2007-12-08 23:42:33 +01:00
}
// Input and output buffers
uint8_t in_buf[BUFSIZ];
uint8_t out_buf[BUFSIZ];
strm->avail_in = 0;
strm->next_out = out_buf;
strm->avail_out = BUFSIZ;
lzma_action action = LZMA_RUN;
2007-12-08 23:42:33 +01:00
while (true) {
if (strm->avail_in == 0) {
strm->next_in = in_buf;
strm->avail_in = fread(in_buf, 1, BUFSIZ, file);
if (ferror(file)) {
// POSIX says that fread() sets errno if
// an error occurred. ferror() doesn't
// touch errno.
my_errorf("%s: Error reading input file: %s",
filename, strerror(errno));
exit(EXIT_FAILURE);
}
2007-12-08 23:42:33 +01:00
#ifndef LZMADEC
// When using LZMA_CONCATENATED, we need to tell
// liblzma when it has got all the input.
if (feof(file))
action = LZMA_FINISH;
#endif
}
2007-12-08 23:42:33 +01:00
ret = lzma_code(strm, action);
2007-12-08 23:42:33 +01:00
// Write and check write error before checking decoder error.
// This way as much data as possible gets written to output
// even if decoder detected an error.
if (strm->avail_out == 0 || ret != LZMA_OK) {
const size_t write_size = BUFSIZ - strm->avail_out;
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.
my_errorf("Cannot write to standard output: "
"%s", strerror(errno));
exit(EXIT_FAILURE);
}
strm->next_out = out_buf;
strm->avail_out = BUFSIZ;
2007-12-08 23:42:33 +01:00
}
if (ret != LZMA_OK) {
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 23:42:33 +01:00
return;
#endif
}
2007-12-08 23:42:33 +01:00
const char *msg;
2007-12-08 23:42:33 +01:00
switch (ret) {
case LZMA_MEM_ERROR:
msg = strerror(ENOMEM);
break;
2007-12-08 23:42:33 +01:00
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;
2007-12-08 23:42:33 +01:00
case LZMA_BUF_ERROR:
msg = "Unexpected end of input";
2007-12-08 23:42:33 +01:00
break;
default:
msg = "Internal error (bug)";
break;
2007-12-08 23:42:33 +01:00
}
my_errorf("%s: %s", filename, msg);
exit(EXIT_FAILURE);
2007-12-08 23:42:33 +01:00
}
}
}
int
main(int argc, char **argv)
{
// Initialize progname which we will be used in error messages.
tuklib_progname_init(argv);
2007-12-08 23:42:33 +01:00
// Parse the command line options.
2007-12-08 23:42:33 +01:00
parse_options(argc, argv);
// 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.
#ifdef TUKLIB_DOSLIKE
setmode(fileno(stdin), O_BINARY);
setmode(fileno(stdout), O_BINARY);
#endif
2007-12-08 23:42:33 +01:00
if (optind == argc) {
// No filenames given, decode from stdin.
uncompress(&strm, stdin, "(stdin)");
2007-12-08 23:42:33 +01:00
} else {
// Loop through the filenames given on the command line.
2007-12-08 23:42:33 +01:00
do {
// "-" indicates stdin.
2007-12-08 23:42:33 +01:00
if (strcmp(argv[optind], "-") == 0) {
uncompress(&strm, stdin, "(stdin)");
2007-12-08 23:42:33 +01:00
} else {
FILE *file = fopen(argv[optind], "rb");
2007-12-08 23:42:33 +01:00
if (file == NULL) {
my_errorf("%s: %s", argv[optind],
2007-12-08 23:42:33 +01:00
strerror(errno));
exit(EXIT_FAILURE);
2007-12-08 23:42:33 +01:00
}
uncompress(&strm, file, argv[optind]);
2007-12-08 23:42:33 +01:00
fclose(file);
}
} while (++optind < argc);
}
#ifndef NDEBUG
// Free the memory only when debugging. Freeing wastes some time,
// but allows detecting possible memory leaks with Valgrind.
lzma_end(&strm);
#endif
tuklib_exit(EXIT_SUCCESS, EXIT_FAILURE, display_errors);
2007-12-08 23:42:33 +01:00
}