]> icculus.org git repositories - icculus/xz.git/blob - src/xzdec/xzdec.c
Add lzma_physmem().
[icculus/xz.git] / src / xzdec / xzdec.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       xzdec.c
4 /// \brief      Simple single-threaded tool to uncompress .xz or .lzma files
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #include "sysdefs.h"
14 #include "lzma.h"
15
16 #include <stdarg.h>
17 #include <errno.h>
18 #include <stdio.h>
19 #include <unistd.h>
20
21 #include "getopt.h"
22 #include "tuklib_progname.h"
23 #include "tuklib_exit.h"
24
25 #ifdef TUKLIB_DOSLIKE
26 #       include <fcntl.h>
27 #       include <io.h>
28 #endif
29
30
31 #ifdef LZMADEC
32 #       define TOOL_FORMAT "lzma"
33 #else
34 #       define TOOL_FORMAT "xz"
35 #endif
36
37
38 /// Number of bytes to use memory at maximum
39 static uint64_t memlimit;
40
41 /// Error messages are suppressed if this is zero, which is the case when
42 /// --quiet has been given at least twice.
43 static unsigned int display_errors = 2;
44
45
46 static void lzma_attribute((format(printf, 1, 2)))
47 my_errorf(const char *fmt, ...)
48 {
49         va_list ap;
50         va_start(ap, fmt);
51
52         if (display_errors) {
53                 fprintf(stderr, "%s: ", progname);
54                 vfprintf(stderr, fmt, ap);
55                 fprintf(stderr, "\n");
56         }
57
58         va_end(ap);
59         return;
60 }
61
62
63 static void lzma_attribute((noreturn))
64 help(void)
65 {
66         printf(
67 "Usage: %s [OPTION]... [FILE]...\n"
68 "Uncompress files in the ." TOOL_FORMAT " format to the standard output.\n"
69 "\n"
70 "  -c, --stdout       (ignored)\n"
71 "  -d, --decompress   (ignored)\n"
72 "  -k, --keep         (ignored)\n"
73 "  -M, --memory=NUM   use NUM bytes of memory at maximum (0 means default)\n"
74 "  -q, --quiet        specify *twice* to suppress errors\n"
75 "  -Q, --no-warn      (ignored)\n"
76 "  -h, --help         display this help and exit\n"
77 "  -V, --version      display the version number and exit\n"
78 "\n"
79 "With no FILE, or when FILE is -, read standard input.\n"
80 "\n"
81 "On this system and configuration, this program will use a maximum of roughly\n"
82 "%" PRIu64 " MiB RAM.\n"
83 "\n"
84 "Report bugs to <" PACKAGE_BUGREPORT "> (in English or Finnish).\n"
85 PACKAGE_NAME " home page: <" PACKAGE_HOMEPAGE ">\n",
86                 progname, memlimit / (1024 * 1024));
87         tuklib_exit(EXIT_SUCCESS, EXIT_FAILURE, display_errors);
88 }
89
90
91 static void lzma_attribute((noreturn))
92 version(void)
93 {
94         printf(TOOL_FORMAT "dec (" PACKAGE_NAME ") " LZMA_VERSION_STRING "\n"
95                         "liblzma %s\n", lzma_version_string());
96
97         tuklib_exit(EXIT_SUCCESS, EXIT_FAILURE, display_errors);
98 }
99
100
101 /// Find out the amount of physical memory (RAM) in the system, and set
102 /// the memory usage limit to the given percentage of RAM.
103 static void
104 memlimit_set_percentage(uint32_t percentage)
105 {
106         uint64_t mem = lzma_physmem();
107
108         // If we cannot determine the amount of RAM, use the assumption
109         // set by the configure script.
110         if (mem == 0)
111                 mem = (uint64_t)(ASSUME_RAM) * 1024 * 1024;
112
113         memlimit = percentage * mem / 100;
114         return;
115 }
116
117
118 /// Set the memory usage limit to give number of bytes. Zero is a special
119 /// value to indicate the default limit.
120 static void
121 memlimit_set(uint64_t new_memlimit)
122 {
123         if (new_memlimit == 0)
124                 memlimit_set_percentage(40);
125         else
126                 memlimit = new_memlimit;
127
128         return;
129 }
130
131
132 /// \brief      Convert a string to uint64_t
133 ///
134 /// This is rudely copied from src/xz/util.c and modified a little. :-(
135 ///
136 /// \param      max     Return value when the string "max" was specified.
137 ///
138 static uint64_t
139 str_to_uint64(const char *value, uint64_t max)
140 {
141         uint64_t result = 0;
142
143         // Accept special value "max".
144         if (strcmp(value, "max") == 0)
145                 return max;
146
147         if (*value < '0' || *value > '9') {
148                 my_errorf("%s: Value is not a non-negative decimal integer",
149                                 value);
150                 exit(EXIT_FAILURE);
151         }
152
153         do {
154                 // Don't overflow.
155                 if (result > (UINT64_MAX - 9) / 10)
156                         return UINT64_MAX;
157
158                 result *= 10;
159                 result += *value - '0';
160                 ++value;
161         } while (*value >= '0' && *value <= '9');
162
163         if (*value != '\0') {
164                 // Look for suffix.
165                 static const struct {
166                         const char name[4];
167                         uint32_t multiplier;
168                 } suffixes[] = {
169                         { "k",   1000 },
170                         { "kB",  1000 },
171                         { "M",   1000000 },
172                         { "MB",  1000000 },
173                         { "G",   1000000000 },
174                         { "GB",  1000000000 },
175                         { "Ki",  1024 },
176                         { "KiB", 1024 },
177                         { "Mi",  1048576 },
178                         { "MiB", 1048576 },
179                         { "Gi",  1073741824 },
180                         { "GiB", 1073741824 }
181                 };
182
183                 uint32_t multiplier = 0;
184                 for (size_t i = 0; i < ARRAY_SIZE(suffixes); ++i) {
185                         if (strcmp(value, suffixes[i].name) == 0) {
186                                 multiplier = suffixes[i].multiplier;
187                                 break;
188                         }
189                 }
190
191                 if (multiplier == 0) {
192                         my_errorf("%s: Invalid suffix", value);
193                         exit(EXIT_FAILURE);
194                 }
195
196                 // Don't overflow here either.
197                 if (result > UINT64_MAX / multiplier)
198                         result = UINT64_MAX;
199                 else
200                         result *= multiplier;
201         }
202
203         return result;
204 }
205
206
207 /// Parses command line options.
208 static void
209 parse_options(int argc, char **argv)
210 {
211         static const char short_opts[] = "cdkM:hqQV";
212         static const struct option long_opts[] = {
213                 { "stdout",       no_argument,         NULL, 'c' },
214                 { "to-stdout",    no_argument,         NULL, 'c' },
215                 { "decompress",   no_argument,         NULL, 'd' },
216                 { "uncompress",   no_argument,         NULL, 'd' },
217                 { "keep",         no_argument,         NULL, 'k' },
218                 { "memory",       required_argument,   NULL, 'M' },
219                 { "quiet",        no_argument,         NULL, 'q' },
220                 { "no-warn",      no_argument,         NULL, 'Q' },
221                 { "help",         no_argument,         NULL, 'h' },
222                 { "version",      no_argument,         NULL, 'V' },
223                 { NULL,           0,                   NULL, 0   }
224         };
225
226         int c;
227
228         while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL))
229                         != -1) {
230                 switch (c) {
231                 case 'c':
232                 case 'd':
233                 case 'k':
234                 case 'Q':
235                         break;
236
237                 case 'M': {
238                         // Support specifying the limit as a percentage of
239                         // installed physical RAM.
240                         const size_t len = strlen(optarg);
241                         if (len > 0 && optarg[len - 1] == '%') {
242                                 // Memory limit is a percentage of total
243                                 // installed RAM.
244                                 optarg[len - 1] = '\0';
245                                 const uint64_t percentage
246                                                 = str_to_uint64(optarg, 100);
247                                 if (percentage < 1 || percentage > 100) {
248                                         my_errorf("Percentage must be in "
249                                                         "the range [1, 100]");
250                                         exit(EXIT_FAILURE);
251                                 }
252
253                                 memlimit_set_percentage(percentage);
254                         } else {
255                                 memlimit_set(str_to_uint64(
256                                                 optarg, UINT64_MAX));
257                         }
258
259                         break;
260                 }
261
262                 case 'q':
263                         if (display_errors > 0)
264                                 --display_errors;
265
266                         break;
267
268                 case 'h':
269                         help();
270
271                 case 'V':
272                         version();
273
274                 default:
275                         exit(EXIT_FAILURE);
276                 }
277         }
278
279         return;
280 }
281
282
283 static void
284 uncompress(lzma_stream *strm, FILE *file, const char *filename)
285 {
286         lzma_ret ret;
287
288         // Initialize the decoder
289 #ifdef LZMADEC
290         ret = lzma_alone_decoder(strm, memlimit);
291 #else
292         ret = lzma_stream_decoder(strm, memlimit, LZMA_CONCATENATED);
293 #endif
294
295         // The only reasonable error here is LZMA_MEM_ERROR.
296         // FIXME: Maybe also LZMA_MEMLIMIT_ERROR in future?
297         if (ret != LZMA_OK) {
298                 my_errorf("%s", ret == LZMA_MEM_ERROR ? strerror(ENOMEM)
299                                 : "Internal error (bug)");
300                 exit(EXIT_FAILURE);
301         }
302
303         // Input and output buffers
304         uint8_t in_buf[BUFSIZ];
305         uint8_t out_buf[BUFSIZ];
306
307         strm->avail_in = 0;
308         strm->next_out = out_buf;
309         strm->avail_out = BUFSIZ;
310
311         lzma_action action = LZMA_RUN;
312
313         while (true) {
314                 if (strm->avail_in == 0) {
315                         strm->next_in = in_buf;
316                         strm->avail_in = fread(in_buf, 1, BUFSIZ, file);
317
318                         if (ferror(file)) {
319                                 // POSIX says that fread() sets errno if
320                                 // an error occurred. ferror() doesn't
321                                 // touch errno.
322                                 my_errorf("%s: Error reading input file: %s",
323                                                 filename, strerror(errno));
324                                 exit(EXIT_FAILURE);
325                         }
326
327 #ifndef LZMADEC
328                         // When using LZMA_CONCATENATED, we need to tell
329                         // liblzma when it has got all the input.
330                         if (feof(file))
331                                 action = LZMA_FINISH;
332 #endif
333                 }
334
335                 ret = lzma_code(strm, action);
336
337                 // Write and check write error before checking decoder error.
338                 // This way as much data as possible gets written to output
339                 // even if decoder detected an error.
340                 if (strm->avail_out == 0 || ret != LZMA_OK) {
341                         const size_t write_size = BUFSIZ - strm->avail_out;
342
343                         if (fwrite(out_buf, 1, write_size, stdout)
344                                         != write_size) {
345                                 // Wouldn't be a surprise if writing to stderr
346                                 // would fail too but at least try to show an
347                                 // error message.
348                                 my_errorf("Cannot write to standard output: "
349                                                 "%s", strerror(errno));
350                                 exit(EXIT_FAILURE);
351                         }
352
353                         strm->next_out = out_buf;
354                         strm->avail_out = BUFSIZ;
355                 }
356
357                 if (ret != LZMA_OK) {
358                         if (ret == LZMA_STREAM_END) {
359 #ifdef LZMADEC
360                                 // Check that there's no trailing garbage.
361                                 if (strm->avail_in != 0
362                                                 || fread(in_buf, 1, 1, file)
363                                                         != 0
364                                                 || !feof(file))
365                                         ret = LZMA_DATA_ERROR;
366                                 else
367                                         return;
368 #else
369                                 // lzma_stream_decoder() already guarantees
370                                 // that there's no trailing garbage.
371                                 assert(strm->avail_in == 0);
372                                 assert(action == LZMA_FINISH);
373                                 assert(feof(file));
374                                 return;
375 #endif
376                         }
377
378                         const char *msg;
379                         switch (ret) {
380                         case LZMA_MEM_ERROR:
381                                 msg = strerror(ENOMEM);
382                                 break;
383
384                         case LZMA_MEMLIMIT_ERROR:
385                                 msg = "Memory usage limit reached";
386                                 break;
387
388                         case LZMA_FORMAT_ERROR:
389                                 msg = "File format not recognized";
390                                 break;
391
392                         case LZMA_OPTIONS_ERROR:
393                                 // FIXME: Better message?
394                                 msg = "Unsupported compression options";
395                                 break;
396
397                         case LZMA_DATA_ERROR:
398                                 msg = "File is corrupt";
399                                 break;
400
401                         case LZMA_BUF_ERROR:
402                                 msg = "Unexpected end of input";
403                                 break;
404
405                         default:
406                                 msg = "Internal error (bug)";
407                                 break;
408                         }
409
410                         my_errorf("%s: %s", filename, msg);
411                         exit(EXIT_FAILURE);
412                 }
413         }
414 }
415
416
417 int
418 main(int argc, char **argv)
419 {
420         // Initialize progname which we will be used in error messages.
421         tuklib_progname_init(argv);
422
423         // Set the default memory usage limit. This is needed before parsing
424         // the command line arguments.
425         memlimit_set(0);
426
427         // Parse the command line options.
428         parse_options(argc, argv);
429
430         // The same lzma_stream is used for all files that we decode. This way
431         // we don't need to reallocate memory for every file if they use same
432         // compression settings.
433         lzma_stream strm = LZMA_STREAM_INIT;
434
435         // Some systems require setting stdin and stdout to binary mode.
436 #ifdef TUKLIB_DOSLIKE
437         setmode(fileno(stdin), O_BINARY);
438         setmode(fileno(stdout), O_BINARY);
439 #endif
440
441         if (optind == argc) {
442                 // No filenames given, decode from stdin.
443                 uncompress(&strm, stdin, "(stdin)");
444         } else {
445                 // Loop through the filenames given on the command line.
446                 do {
447                         // "-" indicates stdin.
448                         if (strcmp(argv[optind], "-") == 0) {
449                                 uncompress(&strm, stdin, "(stdin)");
450                         } else {
451                                 FILE *file = fopen(argv[optind], "rb");
452                                 if (file == NULL) {
453                                         my_errorf("%s: %s", argv[optind],
454                                                         strerror(errno));
455                                         exit(EXIT_FAILURE);
456                                 }
457
458                                 uncompress(&strm, file, argv[optind]);
459                                 fclose(file);
460                         }
461                 } while (++optind < argc);
462         }
463
464 #ifndef NDEBUG
465         // Free the memory only when debugging. Freeing wastes some time,
466         // but allows detecting possible memory leaks with Valgrind.
467         lzma_end(&strm);
468 #endif
469
470         tuklib_exit(EXIT_SUCCESS, EXIT_FAILURE, display_errors);
471 }