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 /* bzip2_main() too: */
13 //kbuild:lib-$(CONFIG_BZIP2) += bbunzip.o
15 /* Note: must be kept in sync with archival/lzop.c */
19 /* only some decompressors: */
22 OPT_DECOMPRESS = 1 << 4,
24 SEAMLESS_MAGIC = (1 << 31) * SEAMLESS_COMPRESSION,
28 int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
30 int fd = open3_or_warn(filename, flags, mode);
38 char* FAST_FUNC append_ext(char *filename, const char *expected_ext)
40 return xasprintf("%s.%s", filename, expected_ext);
43 int FAST_FUNC bbunpack(char **argv,
44 IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(transformer_state_t *xstate),
45 char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext),
46 const char *expected_ext
50 IF_DESKTOP(long long) int status = 0;
51 char *filename, *new_name;
52 smallint exitcode = 0;
53 transformer_state_t xstate;
56 /* NB: new_name is *maybe* malloc'ed! */
58 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
60 if (filename && LONE_DASH(filename))
65 if (!(option_mask32 & SEAMLESS_MAGIC)) {
66 if (stat(filename, &stat_buf) != 0) {
68 bb_simple_perror_msg(filename);
73 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
76 /* "clever zcat" with FILE */
77 /* fail_if_not_compressed because zcat refuses uncompressed input */
78 int fd = open_zipped(filename, /*fail_if_not_compressed:*/ 1);
81 xmove_fd(fd, STDIN_FILENO);
84 if (option_mask32 & SEAMLESS_MAGIC) {
85 /* "clever zcat" on stdin */
86 if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_compressed*/ 1))
90 /* Special cases: test, stdout */
91 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
92 if (option_mask32 & OPT_TEST)
93 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
98 /* Open dst if we are going to unpack to file */
100 new_name = make_new_name(filename, expected_ext);
102 bb_error_msg("%s: unknown suffix - ignored", filename);
106 /* -f: overwrite existing output files */
107 if (option_mask32 & OPT_FORCE) {
111 /* O_EXCL: "real" bunzip2 doesn't overwrite files */
112 /* GNU gunzip does not bail out, but goes to next file */
113 if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
118 /* Check that the input is sane */
119 if (!(option_mask32 & OPT_FORCE) && isatty(STDIN_FILENO)) {
120 bb_error_msg_and_die("compressed data not read from terminal, "
121 "use -f to force it");
124 if (!(option_mask32 & SEAMLESS_MAGIC)) {
125 init_transformer_state(&xstate);
126 xstate.signature_skipped = 0;
127 /*xstate.src_fd = STDIN_FILENO; - already is */
128 xstate.dst_fd = STDOUT_FILENO;
129 status = unpacker(&xstate);
133 if (bb_copyfd_eof(STDIN_FILENO, STDOUT_FILENO) < 0)
134 /* Disk full, tty closed, etc. No point in continuing */
138 if (!(option_mask32 & OPT_STDOUT))
139 xclose(STDOUT_FILENO); /* with error check! */
142 char *del = new_name;
145 unsigned new_name_len;
147 /* TODO: restore other things? */
148 if (xstate.mtime != 0) {
149 struct timeval times[2];
151 times[1].tv_sec = times[0].tv_sec = xstate.mtime;
152 times[1].tv_usec = times[0].tv_usec = 0;
153 /* Note: we closed it first.
154 * On some systems calling utimes
155 * then closing resets the mtime
156 * back to current time. */
157 utimes(new_name, times); /* ignoring errors */
161 new_name_len = strlen(new_name);
162 /* Restore source filename (unless tgz -> tar case) */
163 if (new_name == filename) {
164 new_name_len = strlen(filename);
165 filename[new_name_len] = '.';
167 /* Extreme bloat for gunzip compat */
168 /* Some users do want this info... */
169 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE)) {
170 unsigned percent = status
171 ? ((uoff_t)stat_buf.st_size * 100u / (unsigned long long)status)
173 fprintf(stderr, "%s: %u%% - replaced with %.*s\n",
176 new_name_len, new_name
179 /* Delete _source_ file */
184 if (new_name != filename)
187 } while (*argv && *++argv);
189 if (option_mask32 & OPT_STDOUT)
190 xclose(STDOUT_FILENO); /* with error check! */
195 #if ENABLE_UNCOMPRESS \
196 || ENABLE_BUNZIP2 || ENABLE_BZCAT \
197 || ENABLE_UNLZMA || ENABLE_LZCAT || ENABLE_LZMA \
198 || ENABLE_UNXZ || ENABLE_XZCAT || ENABLE_XZ
200 char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
202 char *extension = strrchr(filename, '.');
203 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
204 /* Mimic GNU gunzip - "real" bunzip2 tries to */
205 /* unpack file anyway, to file.out */
215 * Uncompress applet for busybox (c) 2002 Glenn McGrath
217 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
219 //usage:#define uncompress_trivial_usage
220 //usage: "[-cf] [FILE]..."
221 //usage:#define uncompress_full_usage "\n\n"
222 //usage: "Decompress .Z file[s]\n"
223 //usage: "\n -c Write to stdout"
224 //usage: "\n -f Overwrite"
226 //config:config UNCOMPRESS
227 //config: bool "uncompress"
228 //config: default n # ancient
230 //config: uncompress is used to decompress archives created by compress.
231 //config: Not much used anymore, replaced by gzip/gunzip.
233 //applet:IF_UNCOMPRESS(APPLET(uncompress, BB_DIR_BIN, BB_SUID_DROP))
234 //kbuild:lib-$(CONFIG_UNCOMPRESS) += bbunzip.o
235 #if ENABLE_UNCOMPRESS
236 int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
237 int uncompress_main(int argc UNUSED_PARAM, char **argv)
239 getopt32(argv, "cf");
242 return bbunpack(argv, unpack_Z_stream, make_new_name_generic, "Z");
248 * Gzip implementation for busybox
250 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
252 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
253 * based on gzip sources
255 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
256 * well as stdin/stdout, and to generally behave itself wrt command line
259 * General cleanup to better adhere to the style guide and make use of standard
260 * busybox functions by Glenn McGrath
262 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
264 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
265 * Copyright (C) 1992-1993 Jean-loup Gailly
266 * The unzip code was written and put in the public domain by Mark Adler.
267 * Portions of the lzw code are derived from the public domain 'compress'
268 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
269 * Ken Turkowski, Dave Mack and Peter Jannesen.
271 //usage:#define gunzip_trivial_usage
272 //usage: "[-cft] [FILE]..."
273 //usage:#define gunzip_full_usage "\n\n"
274 //usage: "Decompress FILEs (or stdin)\n"
275 //usage: "\n -c Write to stdout"
276 //usage: "\n -f Force"
277 //usage: "\n -t Test file integrity"
279 //usage:#define gunzip_example_usage
280 //usage: "$ ls -la /tmp/BusyBox*\n"
281 //usage: "-rw-rw-r-- 1 andersen andersen 557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n"
282 //usage: "$ gunzip /tmp/BusyBox-0.43.tar.gz\n"
283 //usage: "$ ls -la /tmp/BusyBox*\n"
284 //usage: "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
286 //usage:#define zcat_trivial_usage
288 //usage:#define zcat_full_usage "\n\n"
289 //usage: "Decompress to stdout"
291 //config:config GUNZIP
292 //config: bool "gunzip"
295 //config: gunzip is used to decompress archives created by gzip.
296 //config: You can use the `-t' option to test the integrity of
297 //config: an archive, without decompressing it.
300 //config: bool "zcat"
303 //config: Alias to "gunzip -c".
305 //config:config FEATURE_GUNZIP_LONG_OPTIONS
306 //config: bool "Enable long options"
308 //config: depends on (GUNZIP || ZCAT) && LONG_OPTS
310 //config: Enable use of long options.
312 //applet:IF_GUNZIP(APPLET(gunzip, BB_DIR_BIN, BB_SUID_DROP))
313 //applet:IF_ZCAT(APPLET_ODDNAME(zcat, gunzip, BB_DIR_BIN, BB_SUID_DROP, zcat))
314 //kbuild:lib-$(CONFIG_GUNZIP) += bbunzip.o
315 //kbuild:lib-$(CONFIG_ZCAT) += bbunzip.o
316 #if ENABLE_GUNZIP || ENABLE_ZCAT
318 char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
320 char *extension = strrchr(filename, '.');
326 if (strcmp(extension, "tgz" + 1) == 0
327 #if ENABLE_FEATURE_SEAMLESS_Z
328 || (extension[0] == 'Z' && extension[1] == '\0')
331 extension[-1] = '\0';
332 } else if (strcmp(extension, "tgz") == 0) {
333 filename = xstrdup(filename);
334 extension = strrchr(filename, '.');
343 #if ENABLE_FEATURE_GUNZIP_LONG_OPTIONS
344 static const char gunzip_longopts[] ALIGN1 =
345 "stdout\0" No_argument "c"
346 "to-stdout\0" No_argument "c"
347 "force\0" No_argument "f"
348 "test\0" No_argument "t"
349 "no-name\0" No_argument "n"
354 * Linux kernel build uses gzip -d -n. We accept and ignore it.
357 * gzip: do not save the original file name and time stamp.
358 * (The original name is always saved if the name had to be truncated.)
359 * gunzip: do not restore the original file name/time even if present
360 * (remove only the gzip suffix from the compressed file name).
361 * This option is the default when decompressing.
363 * gzip: always save the original file name and time stamp (this is the default)
364 * gunzip: restore the original file name and time stamp if present.
366 int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
367 int gunzip_main(int argc UNUSED_PARAM, char **argv)
369 #if ENABLE_FEATURE_GUNZIP_LONG_OPTIONS
370 applet_long_options = gunzip_longopts;
372 getopt32(argv, "cfvqdtn");
375 /* If called as zcat...
376 * Normally, "zcat" is just "gunzip -c".
377 * But if seamless magic is enabled, then we are much more clever.
379 if (ENABLE_ZCAT && applet_name[1] == 'c')
380 option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
382 return bbunpack(argv, unpack_gz_stream, make_new_name_gunzip, /*unused:*/ NULL);
388 * Modified for busybox by Glenn McGrath
389 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
391 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
393 //usage:#define bunzip2_trivial_usage
394 //usage: "[-cf] [FILE]..."
395 //usage:#define bunzip2_full_usage "\n\n"
396 //usage: "Decompress FILEs (or stdin)\n"
397 //usage: "\n -c Write to stdout"
398 //usage: "\n -f Force"
399 //usage:#define bzcat_trivial_usage
401 //usage:#define bzcat_full_usage "\n\n"
402 //usage: "Decompress to stdout"
404 //config:config BUNZIP2
405 //config: bool "bunzip2"
408 //config: bunzip2 is a compression utility using the Burrows-Wheeler block
409 //config: sorting text compression algorithm, and Huffman coding. Compression
410 //config: is generally considerably better than that achieved by more
411 //config: conventional LZ77/LZ78-based compressors, and approaches the
412 //config: performance of the PPM family of statistical compressors.
414 //config: Unless you have a specific application which requires bunzip2, you
415 //config: should probably say N here.
417 //config:config BZCAT
418 //config: bool "bzcat"
421 //config: Alias to "bunzip2 -c".
423 //applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
424 //applet:IF_BZCAT(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
425 //kbuild:lib-$(CONFIG_BUNZIP2) += bbunzip.o
426 //kbuild:lib-$(CONFIG_BZCAT) += bbunzip.o
427 #if ENABLE_BUNZIP2 || ENABLE_BZCAT
428 int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
429 int bunzip2_main(int argc UNUSED_PARAM, char **argv)
431 getopt32(argv, "cfvqdt");
433 if (ENABLE_BZCAT && applet_name[2] == 'c') /* bzcat */
434 option_mask32 |= OPT_STDOUT;
436 return bbunpack(argv, unpack_bz2_stream, make_new_name_generic, "bz2");
442 * Small lzma deflate implementation.
443 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
445 * Based on bunzip.c from busybox
447 * Licensed under GPLv2, see file LICENSE in this source tree.
449 //usage:#define unlzma_trivial_usage
450 //usage: "[-cf] [FILE]..."
451 //usage:#define unlzma_full_usage "\n\n"
452 //usage: "Decompress FILE (or stdin)\n"
453 //usage: "\n -c Write to stdout"
454 //usage: "\n -f Force"
456 //usage:#define lzma_trivial_usage
457 //usage: "-d [-cf] [FILE]..."
458 //usage:#define lzma_full_usage "\n\n"
459 //usage: "Decompress FILE (or stdin)\n"
460 //usage: "\n -d Decompress"
461 //usage: "\n -c Write to stdout"
462 //usage: "\n -f Force"
464 //usage:#define lzcat_trivial_usage
466 //usage:#define lzcat_full_usage "\n\n"
467 //usage: "Decompress to stdout"
469 //usage:#define unxz_trivial_usage
470 //usage: "[-cf] [FILE]..."
471 //usage:#define unxz_full_usage "\n\n"
472 //usage: "Decompress FILE (or stdin)\n"
473 //usage: "\n -c Write to stdout"
474 //usage: "\n -f Force"
476 //usage:#define xz_trivial_usage
477 //usage: "-d [-cf] [FILE]..."
478 //usage:#define xz_full_usage "\n\n"
479 //usage: "Decompress FILE (or stdin)\n"
480 //usage: "\n -d Decompress"
481 //usage: "\n -c Write to stdout"
482 //usage: "\n -f Force"
484 //usage:#define xzcat_trivial_usage
486 //usage:#define xzcat_full_usage "\n\n"
487 //usage: "Decompress to stdout"
489 //config:config UNLZMA
490 //config: bool "unlzma"
493 //config: unlzma is a compression utility using the Lempel-Ziv-Markov chain
494 //config: compression algorithm, and range coding. Compression
495 //config: is generally considerably better than that achieved by the bzip2
496 //config: compressors.
498 //config: The BusyBox unlzma applet is limited to decompression only.
499 //config: On an x86 system, this applet adds about 4K.
501 //config:config LZCAT
502 //config: bool "lzcat"
505 //config: unlzma is a compression utility using the Lempel-Ziv-Markov chain
506 //config: compression algorithm, and range coding. Compression
507 //config: is generally considerably better than that achieved by the bzip2
508 //config: compressors.
510 //config: The BusyBox unlzma applet is limited to decompression only.
511 //config: On an x86 system, this applet adds about 4K.
514 //config: bool "lzma -d"
517 //config: Enable this option if you want commands like "lzma -d" to work.
518 //config: IOW: you'll get lzma applet, but it will always require -d option.
520 //config:config FEATURE_LZMA_FAST
521 //config: bool "Optimize unlzma for speed"
523 //config: depends on UNLZMA || LZCAT || LZMA
525 //config: This option reduces decompression time by about 25% at the cost of
526 //config: a 1K bigger binary.
528 //applet:IF_UNLZMA(APPLET(unlzma, BB_DIR_USR_BIN, BB_SUID_DROP))
529 //applet:IF_LZCAT(APPLET_ODDNAME(lzcat, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzcat))
530 //applet:IF_LZMA(APPLET_ODDNAME(lzma, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzma))
531 //kbuild:lib-$(CONFIG_UNLZMA) += bbunzip.o
532 //kbuild:lib-$(CONFIG_LZCAT) += bbunzip.o
533 //kbuild:lib-$(CONFIG_LZMA) += bbunzip.o
534 #if ENABLE_UNLZMA || ENABLE_LZCAT || ENABLE_LZMA
535 int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
536 int unlzma_main(int argc UNUSED_PARAM, char **argv)
538 IF_LZMA(int opts =) getopt32(argv, "cfvqdt");
540 /* lzma without -d or -t? */
541 if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
545 if (ENABLE_LZCAT && applet_name[2] == 'c')
546 option_mask32 |= OPT_STDOUT;
549 return bbunpack(argv, unpack_lzma_stream, make_new_name_generic, "lzma");
555 //config: bool "unxz"
558 //config: unxz is a unlzma successor.
560 //config:config XZCAT
561 //config: bool "xzcat"
564 //config: Alias to "unxz -c".
567 //config: bool "xz -d"
570 //config: Enable this option if you want commands like "xz -d" to work.
571 //config: IOW: you'll get xz applet, but it will always require -d option.
573 //applet:IF_UNXZ(APPLET(unxz, BB_DIR_USR_BIN, BB_SUID_DROP))
574 //applet:IF_XZCAT(APPLET_ODDNAME(xzcat, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xzcat))
575 //applet:IF_XZ(APPLET_ODDNAME(xz, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xz))
576 //kbuild:lib-$(CONFIG_UNXZ) += bbunzip.o
577 //kbuild:lib-$(CONFIG_XZCAT) += bbunzip.o
578 //kbuild:lib-$(CONFIG_XZ) += bbunzip.o
579 #if ENABLE_UNXZ || ENABLE_XZCAT || ENABLE_XZ
580 int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
581 int unxz_main(int argc UNUSED_PARAM, char **argv)
583 IF_XZ(int opts =) getopt32(argv, "cfvqdt");
585 /* xz without -d or -t? */
586 if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
590 if (ENABLE_XZCAT && applet_name[2] == 'c')
591 option_mask32 |= OPT_STDOUT;
594 return bbunpack(argv, unpack_xz_stream, make_new_name_generic, "xz");