1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Simple single-threaded tool to uncompress .xz or .lzma files
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
22 #include "tuklib_progname.h"
23 #include "tuklib_exit.h"
32 # define TOOL_FORMAT "lzma"
34 # define TOOL_FORMAT "xz"
38 /// Number of bytes to use memory at maximum
39 static uint64_t memlimit;
41 /// Total amount of physical RAM
42 static uint64_t total_ram;
44 /// Error messages are suppressed if this is zero, which is the case when
45 /// --quiet has been given at least twice.
46 static unsigned int display_errors = 2;
49 static void lzma_attribute((format(printf, 1, 2)))
50 my_errorf(const char *fmt, ...)
56 fprintf(stderr, "%s: ", progname);
57 vfprintf(stderr, fmt, ap);
58 fprintf(stderr, "\n");
66 static void lzma_attribute((noreturn))
69 // Round up to the next MiB and do it correctly also with UINT64_MAX.
70 const uint64_t mem_mib = (memlimit >> 20)
71 + ((memlimit & ((UINT32_C(1) << 20) - 1)) != 0);
74 "Usage: %s [OPTION]... [FILE]...\n"
75 "Uncompress files in the ." TOOL_FORMAT " format to the standard output.\n"
77 " -c, --stdout (ignored)\n"
78 " -d, --decompress (ignored)\n"
79 " -k, --keep (ignored)\n"
80 " -M, --memory=NUM use NUM bytes of memory at maximum (0 means default)\n"
81 " -q, --quiet specify *twice* to suppress errors\n"
82 " -Q, --no-warn (ignored)\n"
83 " -h, --help display this help and exit\n"
84 " -V, --version display the version number and exit\n"
86 "With no FILE, or when FILE is -, read standard input.\n"
88 "On this system and configuration, this program will use a maximum of roughly\n"
89 "%" PRIu64 " MiB RAM.\n"
91 "Report bugs to <" PACKAGE_BUGREPORT "> (in English or Finnish).\n"
92 PACKAGE_NAME " home page: <" PACKAGE_URL ">\n", progname, mem_mib);
93 tuklib_exit(EXIT_SUCCESS, EXIT_FAILURE, display_errors);
97 static void lzma_attribute((noreturn))
100 printf(TOOL_FORMAT "dec (" PACKAGE_NAME ") " LZMA_VERSION_STRING "\n"
101 "liblzma %s\n", lzma_version_string());
103 tuklib_exit(EXIT_SUCCESS, EXIT_FAILURE, display_errors);
107 /// Find out the amount of physical memory (RAM) in the system, and set
108 /// the memory usage limit to the given percentage of RAM.
110 memlimit_set_percentage(uint32_t percentage)
112 memlimit = percentage * total_ram / 100;
117 /// Set the memory usage limit to give number of bytes. Zero is a special
118 /// value to indicate the default limit.
120 memlimit_set(uint64_t new_memlimit)
122 if (new_memlimit != 0) {
123 memlimit = new_memlimit;
125 memlimit = 40 * total_ram / 100;
126 if (memlimit < UINT64_C(80) * 1024 * 1024) {
127 memlimit = 80 * total_ram / 100;
128 if (memlimit > UINT64_C(80) * 1024 * 1024)
129 memlimit = UINT64_C(80) * 1024 * 1024;
137 /// Get the total amount of physical RAM and set the memory usage limit
138 /// to the default value.
142 // If we cannot determine the amount of RAM, use the assumption
143 // defined by the configure script.
144 total_ram = lzma_physmem();
146 total_ram = (uint64_t)(ASSUME_RAM) * 1024 * 1024;
153 /// \brief Convert a string to uint64_t
155 /// This is rudely copied from src/xz/util.c and modified a little. :-(
156 /// Since this function is used only for parsing the memory usage limit,
157 /// this cheats a little and saturates too big values to UINT64_MAX instead
158 /// of giving an error.
160 /// \param max Return value when the string "max" was specified.
163 str_to_uint64(const char *value, uint64_t max)
167 // Accept special value "max".
168 if (strcmp(value, "max") == 0)
171 if (*value < '0' || *value > '9') {
172 my_errorf("%s: Value is not a non-negative decimal integer",
179 if (result > UINT64_MAX / 10)
184 // Another overflow check
185 const uint32_t add = *value - '0';
186 if (UINT64_MAX - add < result)
191 } while (*value >= '0' && *value <= '9');
193 if (*value != '\0') {
195 uint64_t multiplier = 0;
196 if (*value == 'k' || *value == 'K')
197 multiplier = UINT64_C(1) << 10;
198 else if (*value == 'm' || *value == 'M')
199 multiplier = UINT64_C(1) << 20;
200 else if (*value == 'g' || *value == 'G')
201 multiplier = UINT64_C(1) << 30;
205 // Allow also e.g. Ki, KiB, and KB.
206 if (*value != '\0' && strcmp(value, "i") != 0
207 && strcmp(value, "iB") != 0
208 && strcmp(value, "B") != 0)
211 if (multiplier == 0) {
212 my_errorf("%s: Invalid suffix", value - 1);
216 // Don't overflow here either.
217 if (result > UINT64_MAX / multiplier)
220 result *= multiplier;
227 /// Parses command line options.
229 parse_options(int argc, char **argv)
231 static const char short_opts[] = "cdkM:hqQV";
232 static const struct option long_opts[] = {
233 { "stdout", no_argument, NULL, 'c' },
234 { "to-stdout", no_argument, NULL, 'c' },
235 { "decompress", no_argument, NULL, 'd' },
236 { "uncompress", no_argument, NULL, 'd' },
237 { "keep", no_argument, NULL, 'k' },
238 { "memory", required_argument, NULL, 'M' },
239 { "quiet", no_argument, NULL, 'q' },
240 { "no-warn", no_argument, NULL, 'Q' },
241 { "help", no_argument, NULL, 'h' },
242 { "version", no_argument, NULL, 'V' },
248 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL))
258 // Support specifying the limit as a percentage of
259 // installed physical RAM.
260 const size_t len = strlen(optarg);
261 if (len > 0 && optarg[len - 1] == '%') {
262 // Memory limit is a percentage of total
264 optarg[len - 1] = '\0';
265 const uint64_t percentage
266 = str_to_uint64(optarg, 100);
267 if (percentage < 1 || percentage > 100) {
268 my_errorf("Percentage must be in "
269 "the range [1, 100]");
273 memlimit_set_percentage(percentage);
275 memlimit_set(str_to_uint64(
276 optarg, UINT64_MAX));
283 if (display_errors > 0)
304 uncompress(lzma_stream *strm, FILE *file, const char *filename)
308 // Initialize the decoder
310 ret = lzma_alone_decoder(strm, memlimit);
312 ret = lzma_stream_decoder(strm, memlimit, LZMA_CONCATENATED);
315 // The only reasonable error here is LZMA_MEM_ERROR.
316 // FIXME: Maybe also LZMA_MEMLIMIT_ERROR in future?
317 if (ret != LZMA_OK) {
318 my_errorf("%s", ret == LZMA_MEM_ERROR ? strerror(ENOMEM)
319 : "Internal error (bug)");
323 // Input and output buffers
324 uint8_t in_buf[BUFSIZ];
325 uint8_t out_buf[BUFSIZ];
328 strm->next_out = out_buf;
329 strm->avail_out = BUFSIZ;
331 lzma_action action = LZMA_RUN;
334 if (strm->avail_in == 0) {
335 strm->next_in = in_buf;
336 strm->avail_in = fread(in_buf, 1, BUFSIZ, file);
339 // POSIX says that fread() sets errno if
340 // an error occurred. ferror() doesn't
342 my_errorf("%s: Error reading input file: %s",
343 filename, strerror(errno));
348 // When using LZMA_CONCATENATED, we need to tell
349 // liblzma when it has got all the input.
351 action = LZMA_FINISH;
355 ret = lzma_code(strm, action);
357 // Write and check write error before checking decoder error.
358 // This way as much data as possible gets written to output
359 // even if decoder detected an error.
360 if (strm->avail_out == 0 || ret != LZMA_OK) {
361 const size_t write_size = BUFSIZ - strm->avail_out;
363 if (fwrite(out_buf, 1, write_size, stdout)
365 // Wouldn't be a surprise if writing to stderr
366 // would fail too but at least try to show an
368 my_errorf("Cannot write to standard output: "
369 "%s", strerror(errno));
373 strm->next_out = out_buf;
374 strm->avail_out = BUFSIZ;
377 if (ret != LZMA_OK) {
378 if (ret == LZMA_STREAM_END) {
380 // Check that there's no trailing garbage.
381 if (strm->avail_in != 0
382 || fread(in_buf, 1, 1, file)
385 ret = LZMA_DATA_ERROR;
389 // lzma_stream_decoder() already guarantees
390 // that there's no trailing garbage.
391 assert(strm->avail_in == 0);
392 assert(action == LZMA_FINISH);
401 msg = strerror(ENOMEM);
404 case LZMA_MEMLIMIT_ERROR:
405 msg = "Memory usage limit reached";
408 case LZMA_FORMAT_ERROR:
409 msg = "File format not recognized";
412 case LZMA_OPTIONS_ERROR:
413 // FIXME: Better message?
414 msg = "Unsupported compression options";
417 case LZMA_DATA_ERROR:
418 msg = "File is corrupt";
422 msg = "Unexpected end of input";
426 msg = "Internal error (bug)";
430 my_errorf("%s: %s", filename, msg);
438 main(int argc, char **argv)
440 // Initialize progname which we will be used in error messages.
441 tuklib_progname_init(argv);
443 // Set the default memory usage limit. This is needed before parsing
444 // the command line arguments.
447 // Parse the command line options.
448 parse_options(argc, argv);
450 // The same lzma_stream is used for all files that we decode. This way
451 // we don't need to reallocate memory for every file if they use same
452 // compression settings.
453 lzma_stream strm = LZMA_STREAM_INIT;
455 // Some systems require setting stdin and stdout to binary mode.
456 #ifdef TUKLIB_DOSLIKE
457 setmode(fileno(stdin), O_BINARY);
458 setmode(fileno(stdout), O_BINARY);
461 if (optind == argc) {
462 // No filenames given, decode from stdin.
463 uncompress(&strm, stdin, "(stdin)");
465 // Loop through the filenames given on the command line.
467 // "-" indicates stdin.
468 if (strcmp(argv[optind], "-") == 0) {
469 uncompress(&strm, stdin, "(stdin)");
471 FILE *file = fopen(argv[optind], "rb");
473 my_errorf("%s: %s", argv[optind],
478 uncompress(&strm, file, argv[optind]);
481 } while (++optind < argc);
485 // Free the memory only when debugging. Freeing wastes some time,
486 // but allows detecting possible memory leaks with Valgrind.
490 tuklib_exit(EXIT_SUCCESS, EXIT_FAILURE, display_errors);