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
13 /* Note: must be kept in sync with archival/lzop.c */
17 /* only some decompressors: */
20 OPT_DECOMPRESS = 1 << 4,
22 SEAMLESS_MAGIC = (1 << 31) * SEAMLESS_COMPRESSION,
26 int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
28 int fd = open3_or_warn(filename, flags, mode);
36 char* FAST_FUNC append_ext(char *filename, const char *expected_ext)
38 return xasprintf("%s.%s", filename, expected_ext);
41 int FAST_FUNC bbunpack(char **argv,
42 IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(transformer_state_t *xstate),
43 char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext),
44 const char *expected_ext
48 IF_DESKTOP(long long) int status = 0;
49 char *filename, *new_name;
50 smallint exitcode = 0;
51 transformer_state_t xstate;
54 /* NB: new_name is *maybe* malloc'ed! */
56 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
58 if (filename && LONE_DASH(filename))
63 if (!(option_mask32 & SEAMLESS_MAGIC)) {
64 if (stat(filename, &stat_buf) != 0) {
66 bb_simple_perror_msg(filename);
71 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
74 /* "clever zcat" with FILE */
75 /* fail_if_not_compressed because zcat refuses uncompressed input */
76 int fd = open_zipped(filename, /*fail_if_not_compressed:*/ 1);
79 xmove_fd(fd, STDIN_FILENO);
82 if (option_mask32 & SEAMLESS_MAGIC) {
83 /* "clever zcat" on stdin */
84 if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_compressed*/ 1))
88 /* Special cases: test, stdout */
89 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
90 if (option_mask32 & OPT_TEST)
91 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
96 /* Open dst if we are going to unpack to file */
98 new_name = make_new_name(filename, expected_ext);
100 bb_error_msg("%s: unknown suffix - ignored", filename);
104 /* -f: overwrite existing output files */
105 if (option_mask32 & OPT_FORCE) {
109 /* O_EXCL: "real" bunzip2 doesn't overwrite files */
110 /* GNU gunzip does not bail out, but goes to next file */
111 if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
116 /* Check that the input is sane */
117 if (!(option_mask32 & OPT_FORCE) && isatty(STDIN_FILENO)) {
118 bb_error_msg_and_die("compressed data not read from terminal, "
119 "use -f to force it");
122 if (!(option_mask32 & SEAMLESS_MAGIC)) {
123 init_transformer_state(&xstate);
124 xstate.signature_skipped = 0;
125 /*xstate.src_fd = STDIN_FILENO; - already is */
126 xstate.dst_fd = STDOUT_FILENO;
127 status = unpacker(&xstate);
131 if (bb_copyfd_eof(STDIN_FILENO, STDOUT_FILENO) < 0)
132 /* Disk full, tty closed, etc. No point in continuing */
136 if (!(option_mask32 & OPT_STDOUT))
137 xclose(STDOUT_FILENO); /* with error check! */
140 char *del = new_name;
143 unsigned new_name_len;
145 /* TODO: restore other things? */
146 if (xstate.mtime != 0) {
147 struct timeval times[2];
149 times[1].tv_sec = times[0].tv_sec = xstate.mtime;
150 times[1].tv_usec = times[0].tv_usec = 0;
151 /* Note: we closed it first.
152 * On some systems calling utimes
153 * then closing resets the mtime
154 * back to current time. */
155 utimes(new_name, times); /* ignoring errors */
159 new_name_len = strlen(new_name);
160 /* Restore source filename (unless tgz -> tar case) */
161 if (new_name == filename) {
162 new_name_len = strlen(filename);
163 filename[new_name_len] = '.';
165 /* Extreme bloat for gunzip compat */
166 /* Some users do want this info... */
167 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE)) {
168 unsigned percent = status
169 ? ((uoff_t)stat_buf.st_size * 100u / (unsigned long long)status)
171 fprintf(stderr, "%s: %u%% - replaced with %.*s\n",
174 new_name_len, new_name
177 /* Delete _source_ file */
182 if (new_name != filename)
185 } while (*argv && *++argv);
187 if (option_mask32 & OPT_STDOUT)
188 xclose(STDOUT_FILENO); /* with error check! */
193 #if ENABLE_UNCOMPRESS || ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNXZ
195 char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
197 char *extension = strrchr(filename, '.');
198 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
199 /* Mimic GNU gunzip - "real" bunzip2 tries to */
200 /* unpack file anyway, to file.out */
210 * Uncompress applet for busybox (c) 2002 Glenn McGrath
212 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
214 //usage:#define uncompress_trivial_usage
215 //usage: "[-cf] [FILE]..."
216 //usage:#define uncompress_full_usage "\n\n"
217 //usage: "Decompress .Z file[s]\n"
218 //usage: "\n -c Write to stdout"
219 //usage: "\n -f Overwrite"
221 //config:config UNCOMPRESS
222 //config: bool "uncompress"
223 //config: default n # ancient
225 //config: uncompress is used to decompress archives created by compress.
226 //config: Not much used anymore, replaced by gzip/gunzip.
228 //applet:IF_UNCOMPRESS(APPLET(uncompress, BB_DIR_BIN, BB_SUID_DROP))
229 //kbuild:lib-$(CONFIG_UNCOMPRESS) += bbunzip.o
230 #if ENABLE_UNCOMPRESS
231 int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
232 int uncompress_main(int argc UNUSED_PARAM, char **argv)
234 getopt32(argv, "cf");
237 return bbunpack(argv, unpack_Z_stream, make_new_name_generic, "Z");
243 * Gzip implementation for busybox
245 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
247 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
248 * based on gzip sources
250 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
251 * well as stdin/stdout, and to generally behave itself wrt command line
254 * General cleanup to better adhere to the style guide and make use of standard
255 * busybox functions by Glenn McGrath
257 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
259 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
260 * Copyright (C) 1992-1993 Jean-loup Gailly
261 * The unzip code was written and put in the public domain by Mark Adler.
262 * Portions of the lzw code are derived from the public domain 'compress'
263 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
264 * Ken Turkowski, Dave Mack and Peter Jannesen.
266 //usage:#define gunzip_trivial_usage
267 //usage: "[-cft] [FILE]..."
268 //usage:#define gunzip_full_usage "\n\n"
269 //usage: "Decompress FILEs (or stdin)\n"
270 //usage: "\n -c Write to stdout"
271 //usage: "\n -f Force"
272 //usage: "\n -t Test file integrity"
274 //usage:#define gunzip_example_usage
275 //usage: "$ ls -la /tmp/BusyBox*\n"
276 //usage: "-rw-rw-r-- 1 andersen andersen 557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n"
277 //usage: "$ gunzip /tmp/BusyBox-0.43.tar.gz\n"
278 //usage: "$ ls -la /tmp/BusyBox*\n"
279 //usage: "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
281 //usage:#define zcat_trivial_usage
283 //usage:#define zcat_full_usage "\n\n"
284 //usage: "Decompress to stdout"
286 //config:config GUNZIP
287 //config: bool "gunzip"
290 //config: gunzip is used to decompress archives created by gzip.
291 //config: You can use the `-t' option to test the integrity of
292 //config: an archive, without decompressing it.
295 //config: bool "zcat"
298 //config: Alias to "gunzip -c".
300 //config:config FEATURE_GUNZIP_LONG_OPTIONS
301 //config: bool "Enable long options"
303 //config: depends on (GUNZIP || ZCAT) && LONG_OPTS
305 //config: Enable use of long options.
307 //applet:IF_GUNZIP(APPLET(gunzip, BB_DIR_BIN, BB_SUID_DROP))
308 //applet:IF_ZCAT(APPLET_ODDNAME(zcat, gunzip, BB_DIR_BIN, BB_SUID_DROP, zcat))
309 //kbuild:lib-$(CONFIG_GUNZIP) += bbunzip.o
310 //kbuild:lib-$(CONFIG_ZCAT) += bbunzip.o
311 #if ENABLE_GUNZIP || ENABLE_ZCAT
313 char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
315 char *extension = strrchr(filename, '.');
321 if (strcmp(extension, "tgz" + 1) == 0
322 #if ENABLE_FEATURE_SEAMLESS_Z
323 || (extension[0] == 'Z' && extension[1] == '\0')
326 extension[-1] = '\0';
327 } else if (strcmp(extension, "tgz") == 0) {
328 filename = xstrdup(filename);
329 extension = strrchr(filename, '.');
338 #if ENABLE_FEATURE_GUNZIP_LONG_OPTIONS
339 static const char gunzip_longopts[] ALIGN1 =
340 "stdout\0" No_argument "c"
341 "to-stdout\0" No_argument "c"
342 "force\0" No_argument "f"
343 "test\0" No_argument "t"
344 "no-name\0" No_argument "n"
349 * Linux kernel build uses gzip -d -n. We accept and ignore it.
352 * gzip: do not save the original file name and time stamp.
353 * (The original name is always saved if the name had to be truncated.)
354 * gunzip: do not restore the original file name/time even if present
355 * (remove only the gzip suffix from the compressed file name).
356 * This option is the default when decompressing.
358 * gzip: always save the original file name and time stamp (this is the default)
359 * gunzip: restore the original file name and time stamp if present.
361 int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
362 int gunzip_main(int argc UNUSED_PARAM, char **argv)
364 #if ENABLE_FEATURE_GUNZIP_LONG_OPTIONS
365 applet_long_options = gunzip_longopts;
367 getopt32(argv, "cfvqdtn");
370 /* If called as zcat...
371 * Normally, "zcat" is just "gunzip -c".
372 * But if seamless magic is enabled, then we are much more clever.
374 if (ENABLE_ZCAT && applet_name[1] == 'c')
375 option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
377 return bbunpack(argv, unpack_gz_stream, make_new_name_gunzip, /*unused:*/ NULL);
383 * Modified for busybox by Glenn McGrath
384 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
386 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
388 //usage:#define bunzip2_trivial_usage
389 //usage: "[-cf] [FILE]..."
390 //usage:#define bunzip2_full_usage "\n\n"
391 //usage: "Decompress FILEs (or stdin)\n"
392 //usage: "\n -c Write to stdout"
393 //usage: "\n -f Force"
394 //usage:#define bzcat_trivial_usage
396 //usage:#define bzcat_full_usage "\n\n"
397 //usage: "Decompress to stdout"
399 //config:config BUNZIP2
400 //config: bool "bunzip2"
403 //config: bunzip2 is a compression utility using the Burrows-Wheeler block
404 //config: sorting text compression algorithm, and Huffman coding. Compression
405 //config: is generally considerably better than that achieved by more
406 //config: conventional LZ77/LZ78-based compressors, and approaches the
407 //config: performance of the PPM family of statistical compressors.
409 //config: Unless you have a specific application which requires bunzip2, you
410 //config: should probably say N here.
412 //config:config BZCAT
413 //config: bool "bzcat"
416 //config: Alias to "bunzip2 -c".
418 //applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
419 //applet:IF_BZCAT(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
420 //kbuild:lib-$(CONFIG_BUNZIP2) += bbunzip.o
421 //kbuild:lib-$(CONFIG_BZCAT) += bbunzip.o
422 #if ENABLE_BUNZIP2 || ENABLE_BZCAT
423 int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
424 int bunzip2_main(int argc UNUSED_PARAM, char **argv)
426 getopt32(argv, "cfvqdt");
428 if (ENABLE_BZCAT && applet_name[2] == 'c') /* bzcat */
429 option_mask32 |= OPT_STDOUT;
431 return bbunpack(argv, unpack_bz2_stream, make_new_name_generic, "bz2");
437 * Small lzma deflate implementation.
438 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
440 * Based on bunzip.c from busybox
442 * Licensed under GPLv2, see file LICENSE in this source tree.
444 //usage:#define unlzma_trivial_usage
445 //usage: "[-cf] [FILE]..."
446 //usage:#define unlzma_full_usage "\n\n"
447 //usage: "Decompress FILE (or stdin)\n"
448 //usage: "\n -c Write to stdout"
449 //usage: "\n -f Force"
451 //usage:#define lzma_trivial_usage
452 //usage: "-d [-cf] [FILE]..."
453 //usage:#define lzma_full_usage "\n\n"
454 //usage: "Decompress FILE (or stdin)\n"
455 //usage: "\n -d Decompress"
456 //usage: "\n -c Write to stdout"
457 //usage: "\n -f Force"
459 //usage:#define lzcat_trivial_usage
461 //usage:#define lzcat_full_usage "\n\n"
462 //usage: "Decompress to stdout"
464 //usage:#define unxz_trivial_usage
465 //usage: "[-cf] [FILE]..."
466 //usage:#define unxz_full_usage "\n\n"
467 //usage: "Decompress FILE (or stdin)\n"
468 //usage: "\n -c Write to stdout"
469 //usage: "\n -f Force"
471 //usage:#define xz_trivial_usage
472 //usage: "-d [-cf] [FILE]..."
473 //usage:#define xz_full_usage "\n\n"
474 //usage: "Decompress FILE (or stdin)\n"
475 //usage: "\n -d Decompress"
476 //usage: "\n -c Write to stdout"
477 //usage: "\n -f Force"
479 //usage:#define xzcat_trivial_usage
481 //usage:#define xzcat_full_usage "\n\n"
482 //usage: "Decompress to stdout"
484 //config:config UNLZMA
485 //config: bool "unlzma"
488 //config: unlzma is a compression utility using the Lempel-Ziv-Markov chain
489 //config: compression algorithm, and range coding. Compression
490 //config: is generally considerably better than that achieved by the bzip2
491 //config: compressors.
493 //config: The BusyBox unlzma applet is limited to decompression only.
494 //config: On an x86 system, this applet adds about 4K.
496 //config:config LZCAT
497 //config: bool "lzcat"
500 //config: unlzma is a compression utility using the Lempel-Ziv-Markov chain
501 //config: compression algorithm, and range coding. Compression
502 //config: is generally considerably better than that achieved by the bzip2
503 //config: compressors.
505 //config: The BusyBox unlzma applet is limited to decompression only.
506 //config: On an x86 system, this applet adds about 4K.
509 //config: bool "lzma -d"
512 //config: Enable this option if you want commands like "lzma -d" to work.
513 //config: IOW: you'll get lzma applet, but it will always require -d option.
515 //config:config FEATURE_LZMA_FAST
516 //config: bool "Optimize unlzma for speed"
518 //config: depends on UNLZMA || LZCAT || LZMA
520 //config: This option reduces decompression time by about 25% at the cost of
521 //config: a 1K bigger binary.
523 //applet:IF_UNLZMA(APPLET(unlzma, BB_DIR_USR_BIN, BB_SUID_DROP))
524 //applet:IF_LZCAT(APPLET_ODDNAME(lzcat, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzcat))
525 //applet:IF_LZMA(APPLET_ODDNAME(lzma, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzma))
526 //kbuild:lib-$(CONFIG_UNLZMA) += bbunzip.o
527 //kbuild:lib-$(CONFIG_LZCAT) += bbunzip.o
528 //kbuild:lib-$(CONFIG_LZMA) += bbunzip.o
529 #if ENABLE_UNLZMA || ENABLE_LZCAT || ENABLE_LZMA
530 int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
531 int unlzma_main(int argc UNUSED_PARAM, char **argv)
533 IF_LZMA(int opts =) getopt32(argv, "cfvqdt");
535 /* lzma without -d or -t? */
536 if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
540 if (ENABLE_LZCAT && applet_name[2] == 'c')
541 option_mask32 |= OPT_STDOUT;
544 return bbunpack(argv, unpack_lzma_stream, make_new_name_generic, "lzma");
550 //config: bool "unxz"
553 //config: unxz is a unlzma successor.
555 //config:config XZCAT
556 //config: bool "xzcat"
559 //config: Alias to "unxz -c".
562 //config: bool "xz -d"
565 //config: Enable this option if you want commands like "xz -d" to work.
566 //config: IOW: you'll get xz applet, but it will always require -d option.
568 //applet:IF_UNXZ(APPLET(unxz, BB_DIR_USR_BIN, BB_SUID_DROP))
569 //applet:IF_XZCAT(APPLET_ODDNAME(xzcat, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xzcat))
570 //applet:IF_XZ(APPLET_ODDNAME(xz, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xz))
571 //kbuild:lib-$(CONFIG_UNXZ) += bbunzip.o
572 //kbuild:lib-$(CONFIG_XZCAT) += bbunzip.o
573 //kbuild:lib-$(CONFIG_XZ) += bbunzip.o
574 #if ENABLE_UNXZ || ENABLE_XZCAT || ENABLE_XZ
575 int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
576 int unxz_main(int argc UNUSED_PARAM, char **argv)
578 IF_XZ(int opts =) getopt32(argv, "cfvqdt");
580 /* xz without -d or -t? */
581 if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
585 if (ENABLE_XZCAT && applet_name[2] == 'c')
586 option_mask32 |= OPT_STDOUT;
589 return bbunpack(argv, unpack_xz_stream, make_new_name_generic, "xz");