1 /* vi: set sw=4 ts=4: */
3 * Common code for gunzip-like applets
5 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 #include "bb_archive.h"
10 /* lzop_main() uses bbunpack(), need this: */
11 //kbuild:lib-$(CONFIG_LZOP) += bbunzip.o
12 //kbuild:lib-$(CONFIG_LZOPCAT) += bbunzip.o
13 //kbuild:lib-$(CONFIG_UNLZOP) += bbunzip.o
14 /* bzip2_main() too: */
15 //kbuild:lib-$(CONFIG_FEATURE_BZIP2_DECOMPRESS) += bbunzip.o
16 /* gzip_main() too: */
17 //kbuild:lib-$(CONFIG_FEATURE_GZIP_DECOMPRESS) += bbunzip.o
19 /* Note: must be kept in sync with archival/lzop.c */
23 /* only some decompressors: */
26 OPT_DECOMPRESS = 1 << 4,
28 SEAMLESS_MAGIC = (1 << 31) * ENABLE_ZCAT * SEAMLESS_COMPRESSION,
32 int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
34 int fd = open3_or_warn(filename, flags, mode);
42 char* FAST_FUNC append_ext(char *filename, const char *expected_ext)
44 return xasprintf("%s.%s", filename, expected_ext);
47 int FAST_FUNC bbunpack(char **argv,
48 IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(transformer_state_t *xstate),
49 char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext),
50 const char *expected_ext
54 IF_DESKTOP(long long) int status = 0;
55 char *filename, *new_name;
56 smallint exitcode = 0;
57 transformer_state_t xstate;
60 /* NB: new_name is *maybe* malloc'ed! */
62 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
64 if (filename && LONE_DASH(filename))
69 if (!(option_mask32 & SEAMLESS_MAGIC)) {
70 if (stat(filename, &stat_buf) != 0) {
72 bb_simple_perror_msg(filename);
77 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
80 /* "clever zcat" with FILE */
81 /* fail_if_not_compressed because zcat refuses uncompressed input */
82 int fd = open_zipped(filename, /*fail_if_not_compressed:*/ 1);
85 xmove_fd(fd, STDIN_FILENO);
88 if (option_mask32 & SEAMLESS_MAGIC) {
89 /* "clever zcat" on stdin */
90 if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_compressed*/ 1))
94 /* Special cases: test, stdout */
95 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
96 if (option_mask32 & OPT_TEST)
97 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
102 /* Open dst if we are going to unpack to file */
104 new_name = make_new_name(filename, expected_ext);
106 bb_error_msg("%s: unknown suffix - ignored", filename);
110 /* -f: overwrite existing output files */
111 if (option_mask32 & OPT_FORCE) {
115 /* O_EXCL: "real" bunzip2 doesn't overwrite files */
116 /* GNU gunzip does not bail out, but goes to next file */
117 if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
122 /* Check that the input is sane */
123 if (!(option_mask32 & OPT_FORCE) && isatty(STDIN_FILENO)) {
124 bb_error_msg_and_die("compressed data not read from terminal, "
125 "use -f to force it");
128 if (!(option_mask32 & SEAMLESS_MAGIC)) {
129 init_transformer_state(&xstate);
130 /*xstate.signature_skipped = 0; - already is */
131 /*xstate.src_fd = STDIN_FILENO; - already is */
132 xstate.dst_fd = STDOUT_FILENO;
133 status = unpacker(&xstate);
137 if (bb_copyfd_eof(STDIN_FILENO, STDOUT_FILENO) < 0)
138 /* Disk full, tty closed, etc. No point in continuing */
142 if (!(option_mask32 & OPT_STDOUT))
143 xclose(STDOUT_FILENO); /* with error check! */
146 char *del = new_name;
149 unsigned new_name_len;
151 /* TODO: restore other things? */
152 if (xstate.mtime != 0) {
153 struct timeval times[2];
155 times[1].tv_sec = times[0].tv_sec = xstate.mtime;
156 times[1].tv_usec = times[0].tv_usec = 0;
157 /* Note: we closed it first.
158 * On some systems calling utimes
159 * then closing resets the mtime
160 * back to current time. */
161 utimes(new_name, times); /* ignoring errors */
165 new_name_len = strlen(new_name);
166 /* Restore source filename (unless tgz -> tar case) */
167 if (new_name == filename) {
168 new_name_len = strlen(filename);
169 filename[new_name_len] = '.';
171 /* Extreme bloat for gunzip compat */
172 /* Some users do want this info... */
173 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE)) {
174 unsigned percent = status
175 ? ((uoff_t)stat_buf.st_size * 100u / (unsigned long long)status)
177 fprintf(stderr, "%s: %u%% - replaced with %.*s\n",
180 new_name_len, new_name
183 /* Delete _source_ file */
188 if (new_name != filename)
191 } while (*argv && *++argv);
193 if (option_mask32 & OPT_STDOUT)
194 xclose(STDOUT_FILENO); /* with error check! */
199 #if ENABLE_UNCOMPRESS \
200 || ENABLE_FEATURE_BZIP2_DECOMPRESS \
201 || ENABLE_UNLZMA || ENABLE_LZCAT || ENABLE_LZMA \
202 || ENABLE_UNXZ || ENABLE_XZCAT || ENABLE_XZ
204 char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
206 char *extension = strrchr(filename, '.');
207 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
208 /* Mimic GNU gunzip - "real" bunzip2 tries to */
209 /* unpack file anyway, to file.out */
219 * Uncompress applet for busybox (c) 2002 Glenn McGrath
221 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
223 //usage:#define uncompress_trivial_usage
224 //usage: "[-cf] [FILE]..."
225 //usage:#define uncompress_full_usage "\n\n"
226 //usage: "Decompress .Z file[s]\n"
227 //usage: "\n -c Write to stdout"
228 //usage: "\n -f Overwrite"
230 //config:config UNCOMPRESS
231 //config: bool "uncompress"
232 //config: default n # ancient
234 //config: uncompress is used to decompress archives created by compress.
235 //config: Not much used anymore, replaced by gzip/gunzip.
237 //applet:IF_UNCOMPRESS(APPLET(uncompress, BB_DIR_BIN, BB_SUID_DROP))
238 //kbuild:lib-$(CONFIG_UNCOMPRESS) += bbunzip.o
239 #if ENABLE_UNCOMPRESS
240 int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
241 int uncompress_main(int argc UNUSED_PARAM, char **argv)
243 getopt32(argv, "cf");
246 return bbunpack(argv, unpack_Z_stream, make_new_name_generic, "Z");
252 * Gzip implementation for busybox
254 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
256 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
257 * based on gzip sources
259 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
260 * well as stdin/stdout, and to generally behave itself wrt command line
263 * General cleanup to better adhere to the style guide and make use of standard
264 * busybox functions by Glenn McGrath
266 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
268 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
269 * Copyright (C) 1992-1993 Jean-loup Gailly
270 * The unzip code was written and put in the public domain by Mark Adler.
271 * Portions of the lzw code are derived from the public domain 'compress'
272 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
273 * Ken Turkowski, Dave Mack and Peter Jannesen.
275 //usage:#define gunzip_trivial_usage
276 //usage: "[-cft] [FILE]..."
277 //usage:#define gunzip_full_usage "\n\n"
278 //usage: "Decompress FILEs (or stdin)\n"
279 //usage: "\n -c Write to stdout"
280 //usage: "\n -f Force"
281 //usage: "\n -t Test file integrity"
283 //usage:#define gunzip_example_usage
284 //usage: "$ ls -la /tmp/BusyBox*\n"
285 //usage: "-rw-rw-r-- 1 andersen andersen 557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n"
286 //usage: "$ gunzip /tmp/BusyBox-0.43.tar.gz\n"
287 //usage: "$ ls -la /tmp/BusyBox*\n"
288 //usage: "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
290 //usage:#define zcat_trivial_usage
292 //usage:#define zcat_full_usage "\n\n"
293 //usage: "Decompress to stdout"
295 //config:config GUNZIP
296 //config: bool "gunzip"
298 //config: select FEATURE_GZIP_DECOMPRESS
300 //config: gunzip is used to decompress archives created by gzip.
301 //config: You can use the `-t' option to test the integrity of
302 //config: an archive, without decompressing it.
305 //config: bool "zcat"
307 //config: select FEATURE_GZIP_DECOMPRESS
309 //config: Alias to "gunzip -c".
311 //config:config FEATURE_GUNZIP_LONG_OPTIONS
312 //config: bool "Enable long options"
314 //config: depends on (GUNZIP || ZCAT) && LONG_OPTS
316 //applet:IF_GUNZIP(APPLET(gunzip, BB_DIR_BIN, BB_SUID_DROP))
317 //applet:IF_ZCAT(APPLET_ODDNAME(zcat, gunzip, BB_DIR_BIN, BB_SUID_DROP, zcat))
318 #if ENABLE_FEATURE_GZIP_DECOMPRESS
320 char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
322 char *extension = strrchr(filename, '.');
328 if (strcmp(extension, "tgz" + 1) == 0
329 #if ENABLE_FEATURE_SEAMLESS_Z
330 || (extension[0] == 'Z' && extension[1] == '\0')
333 extension[-1] = '\0';
334 } else if (strcmp(extension, "tgz") == 0) {
335 filename = xstrdup(filename);
336 extension = strrchr(filename, '.');
345 #if ENABLE_FEATURE_GUNZIP_LONG_OPTIONS
346 static const char gunzip_longopts[] ALIGN1 =
347 "stdout\0" No_argument "c"
348 "to-stdout\0" No_argument "c"
349 "force\0" No_argument "f"
350 "test\0" No_argument "t"
351 "no-name\0" No_argument "n"
356 * Linux kernel build uses gzip -d -n. We accept and ignore it.
359 * gzip: do not save the original file name and time stamp.
360 * (The original name is always saved if the name had to be truncated.)
361 * gunzip: do not restore the original file name/time even if present
362 * (remove only the gzip suffix from the compressed file name).
363 * This option is the default when decompressing.
365 * gzip: always save the original file name and time stamp (this is the default)
366 * gunzip: restore the original file name and time stamp if present.
368 int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
369 int gunzip_main(int argc UNUSED_PARAM, char **argv)
371 #if ENABLE_FEATURE_GUNZIP_LONG_OPTIONS
372 applet_long_options = gunzip_longopts;
374 getopt32(argv, "cfvqdtn");
377 /* If called as zcat...
378 * Normally, "zcat" is just "gunzip -c".
379 * But if seamless magic is enabled, then we are much more clever.
381 if (ENABLE_ZCAT && applet_name[1] == 'c')
382 option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
384 return bbunpack(argv, unpack_gz_stream, make_new_name_gunzip, /*unused:*/ NULL);
386 #endif /* FEATURE_GZIP_DECOMPRESS */
390 * Modified for busybox by Glenn McGrath
391 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
393 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
395 //usage:#define bunzip2_trivial_usage
396 //usage: "[-cf] [FILE]..."
397 //usage:#define bunzip2_full_usage "\n\n"
398 //usage: "Decompress FILEs (or stdin)\n"
399 //usage: "\n -c Write to stdout"
400 //usage: "\n -f Force"
401 //usage:#define bzcat_trivial_usage
403 //usage:#define bzcat_full_usage "\n\n"
404 //usage: "Decompress to stdout"
406 //config:config BUNZIP2
407 //config: bool "bunzip2"
409 //config: select FEATURE_BZIP2_DECOMPRESS
411 //config: bunzip2 is a compression utility using the Burrows-Wheeler block
412 //config: sorting text compression algorithm, and Huffman coding. Compression
413 //config: is generally considerably better than that achieved by more
414 //config: conventional LZ77/LZ78-based compressors, and approaches the
415 //config: performance of the PPM family of statistical compressors.
417 //config: Unless you have a specific application which requires bunzip2, you
418 //config: should probably say N here.
420 //config:config BZCAT
421 //config: bool "bzcat"
423 //config: select FEATURE_BZIP2_DECOMPRESS
425 //config: Alias to "bunzip2 -c".
427 //applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
428 //applet:IF_BZCAT(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
429 #if ENABLE_FEATURE_BZIP2_DECOMPRESS
430 int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
431 int bunzip2_main(int argc UNUSED_PARAM, char **argv)
433 getopt32(argv, "cfvqdt");
435 if (ENABLE_BZCAT && applet_name[2] == 'c') /* bzcat */
436 option_mask32 |= OPT_STDOUT;
438 return bbunpack(argv, unpack_bz2_stream, make_new_name_generic, "bz2");
444 * Small lzma deflate implementation.
445 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
447 * Based on bunzip.c from busybox
449 * Licensed under GPLv2, see file LICENSE in this source tree.
451 //usage:#define unlzma_trivial_usage
452 //usage: "[-cf] [FILE]..."
453 //usage:#define unlzma_full_usage "\n\n"
454 //usage: "Decompress FILE (or stdin)\n"
455 //usage: "\n -c Write to stdout"
456 //usage: "\n -f Force"
458 //usage:#define lzma_trivial_usage
459 //usage: "-d [-cf] [FILE]..."
460 //usage:#define lzma_full_usage "\n\n"
461 //usage: "Decompress FILE (or stdin)\n"
462 //usage: "\n -d Decompress"
463 //usage: "\n -c Write to stdout"
464 //usage: "\n -f Force"
466 //usage:#define lzcat_trivial_usage
468 //usage:#define lzcat_full_usage "\n\n"
469 //usage: "Decompress to stdout"
471 //usage:#define unxz_trivial_usage
472 //usage: "[-cf] [FILE]..."
473 //usage:#define unxz_full_usage "\n\n"
474 //usage: "Decompress FILE (or stdin)\n"
475 //usage: "\n -c Write to stdout"
476 //usage: "\n -f Force"
478 //usage:#define xz_trivial_usage
479 //usage: "-d [-cf] [FILE]..."
480 //usage:#define xz_full_usage "\n\n"
481 //usage: "Decompress FILE (or stdin)\n"
482 //usage: "\n -d Decompress"
483 //usage: "\n -c Write to stdout"
484 //usage: "\n -f Force"
486 //usage:#define xzcat_trivial_usage
488 //usage:#define xzcat_full_usage "\n\n"
489 //usage: "Decompress to stdout"
491 //config:config UNLZMA
492 //config: bool "unlzma"
495 //config: unlzma is a compression utility using the Lempel-Ziv-Markov chain
496 //config: compression algorithm, and range coding. Compression
497 //config: is generally considerably better than that achieved by the bzip2
498 //config: compressors.
500 //config: The BusyBox unlzma applet is limited to decompression only.
501 //config: On an x86 system, this applet adds about 4K.
503 //config:config LZCAT
504 //config: bool "lzcat"
507 //config: unlzma is a compression utility using the Lempel-Ziv-Markov chain
508 //config: compression algorithm, and range coding. Compression
509 //config: is generally considerably better than that achieved by the bzip2
510 //config: compressors.
512 //config: The BusyBox unlzma applet is limited to decompression only.
513 //config: On an x86 system, this applet adds about 4K.
516 //config: bool "lzma -d"
519 //config: Enable this option if you want commands like "lzma -d" to work.
520 //config: IOW: you'll get lzma applet, but it will always require -d option.
522 //config:config FEATURE_LZMA_FAST
523 //config: bool "Optimize for speed"
525 //config: depends on UNLZMA || LZCAT || LZMA
527 //config: This option reduces decompression time by about 25% at the cost of
528 //config: a 1K bigger binary.
530 //applet:IF_UNLZMA(APPLET(unlzma, BB_DIR_USR_BIN, BB_SUID_DROP))
531 //applet:IF_LZCAT(APPLET_ODDNAME(lzcat, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzcat))
532 //applet:IF_LZMA(APPLET_ODDNAME(lzma, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzma))
533 //kbuild:lib-$(CONFIG_UNLZMA) += bbunzip.o
534 //kbuild:lib-$(CONFIG_LZCAT) += bbunzip.o
535 //kbuild:lib-$(CONFIG_LZMA) += bbunzip.o
536 #if ENABLE_UNLZMA || ENABLE_LZCAT || ENABLE_LZMA
537 int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
538 int unlzma_main(int argc UNUSED_PARAM, char **argv)
540 IF_LZMA(int opts =) getopt32(argv, "cfvqdt");
542 /* lzma without -d or -t? */
543 if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
547 if (ENABLE_LZCAT && applet_name[2] == 'c')
548 option_mask32 |= OPT_STDOUT;
551 return bbunpack(argv, unpack_lzma_stream, make_new_name_generic, "lzma");
557 //config: bool "unxz"
560 //config: unxz is a unlzma successor.
562 //config:config XZCAT
563 //config: bool "xzcat"
566 //config: Alias to "unxz -c".
569 //config: bool "xz -d"
572 //config: Enable this option if you want commands like "xz -d" to work.
573 //config: IOW: you'll get xz applet, but it will always require -d option.
575 //applet:IF_UNXZ(APPLET(unxz, BB_DIR_USR_BIN, BB_SUID_DROP))
576 //applet:IF_XZCAT(APPLET_ODDNAME(xzcat, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xzcat))
577 //applet:IF_XZ(APPLET_ODDNAME(xz, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xz))
578 //kbuild:lib-$(CONFIG_UNXZ) += bbunzip.o
579 //kbuild:lib-$(CONFIG_XZCAT) += bbunzip.o
580 //kbuild:lib-$(CONFIG_XZ) += bbunzip.o
581 #if ENABLE_UNXZ || ENABLE_XZCAT || ENABLE_XZ
582 int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
583 int unxz_main(int argc UNUSED_PARAM, char **argv)
585 IF_XZ(int opts =) getopt32(argv, "cfvqdt");
587 /* xz without -d or -t? */
588 if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
592 if (ENABLE_XZCAT && applet_name[2] == 'c')
593 option_mask32 |= OPT_STDOUT;
596 return bbunpack(argv, unpack_xz_stream, make_new_name_generic, "xz");