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_aux_data_t *aux),
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_aux_data_t aux;
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 int fd = open_zipped(filename);
78 xmove_fd(fd, STDIN_FILENO);
81 if (option_mask32 & SEAMLESS_MAGIC) {
82 /* "clever zcat" on stdin */
83 if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_detected*/ 0))
87 /* Special cases: test, stdout */
88 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
89 if (option_mask32 & OPT_TEST)
90 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
95 /* Open dst if we are going to unpack to file */
97 new_name = make_new_name(filename, expected_ext);
99 bb_error_msg("%s: unknown suffix - ignored", filename);
103 /* -f: overwrite existing output files */
104 if (option_mask32 & OPT_FORCE) {
108 /* O_EXCL: "real" bunzip2 doesn't overwrite files */
109 /* GNU gunzip does not bail out, but goes to next file */
110 if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
115 /* Check that the input is sane */
116 if (!(option_mask32 & OPT_FORCE) && isatty(STDIN_FILENO)) {
117 bb_error_msg_and_die("compressed data not read from terminal, "
118 "use -f to force it");
121 if (!(option_mask32 & SEAMLESS_MAGIC)) {
122 init_transformer_aux_data(&aux);
123 aux.check_signature = 1;
124 status = unpacker(&aux);
128 if (bb_copyfd_eof(STDIN_FILENO, STDOUT_FILENO) < 0)
129 /* Disk full, tty closed, etc. No point in continuing */
133 if (!(option_mask32 & OPT_STDOUT))
134 xclose(STDOUT_FILENO); /* with error check! */
137 char *del = new_name;
140 unsigned new_name_len;
142 /* TODO: restore other things? */
143 if (aux.mtime != 0) {
144 struct timeval times[2];
146 times[1].tv_sec = times[0].tv_sec = aux.mtime;
147 times[1].tv_usec = times[0].tv_usec = 0;
148 /* Note: we closed it first.
149 * On some systems calling utimes
150 * then closing resets the mtime
151 * back to current time. */
152 utimes(new_name, times); /* ignoring errors */
156 new_name_len = strlen(new_name);
157 /* Restore source filename (unless tgz -> tar case) */
158 if (new_name == filename) {
159 new_name_len = strlen(filename);
160 filename[new_name_len] = '.';
162 /* Extreme bloat for gunzip compat */
163 /* Some users do want this info... */
164 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE)) {
165 unsigned percent = status
166 ? ((uoff_t)stat_buf.st_size * 100u / (unsigned long long)status)
168 fprintf(stderr, "%s: %u%% - replaced with %.*s\n",
171 new_name_len, new_name
174 /* Delete _source_ file */
179 if (new_name != filename)
182 } while (*argv && *++argv);
184 if (option_mask32 & OPT_STDOUT)
185 xclose(STDOUT_FILENO); /* with error check! */
190 #if ENABLE_UNCOMPRESS || ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNXZ
192 char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
194 char *extension = strrchr(filename, '.');
195 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
196 /* Mimic GNU gunzip - "real" bunzip2 tries to */
197 /* unpack file anyway, to file.out */
207 * Uncompress applet for busybox (c) 2002 Glenn McGrath
209 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
211 //usage:#define uncompress_trivial_usage
212 //usage: "[-cf] [FILE]..."
213 //usage:#define uncompress_full_usage "\n\n"
214 //usage: "Decompress .Z file[s]\n"
215 //usage: "\n -c Write to stdout"
216 //usage: "\n -f Overwrite"
218 //config:config UNCOMPRESS
219 //config: bool "uncompress"
222 //config: uncompress is used to decompress archives created by compress.
223 //config: Not much used anymore, replaced by gzip/gunzip.
225 //applet:IF_UNCOMPRESS(APPLET(uncompress, BB_DIR_BIN, BB_SUID_DROP))
226 //kbuild:lib-$(CONFIG_UNCOMPRESS) += bbunzip.o
227 #if ENABLE_UNCOMPRESS
229 IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(transformer_aux_data_t *aux)
231 return unpack_Z_stream(aux, STDIN_FILENO, STDOUT_FILENO);
233 int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
234 int uncompress_main(int argc UNUSED_PARAM, char **argv)
236 getopt32(argv, "cf");
239 return bbunpack(argv, unpack_uncompress, make_new_name_generic, "Z");
245 * Gzip implementation for busybox
247 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
249 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
250 * based on gzip sources
252 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
253 * well as stdin/stdout, and to generally behave itself wrt command line
256 * General cleanup to better adhere to the style guide and make use of standard
257 * busybox functions by Glenn McGrath
259 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
261 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
262 * Copyright (C) 1992-1993 Jean-loup Gailly
263 * The unzip code was written and put in the public domain by Mark Adler.
264 * Portions of the lzw code are derived from the public domain 'compress'
265 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
266 * Ken Turkowski, Dave Mack and Peter Jannesen.
268 //usage:#define gunzip_trivial_usage
269 //usage: "[-cft] [FILE]..."
270 //usage:#define gunzip_full_usage "\n\n"
271 //usage: "Decompress FILEs (or stdin)\n"
272 //usage: "\n -c Write to stdout"
273 //usage: "\n -f Force"
274 //usage: "\n -t Test file integrity"
276 //usage:#define gunzip_example_usage
277 //usage: "$ ls -la /tmp/BusyBox*\n"
278 //usage: "-rw-rw-r-- 1 andersen andersen 557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n"
279 //usage: "$ gunzip /tmp/BusyBox-0.43.tar.gz\n"
280 //usage: "$ ls -la /tmp/BusyBox*\n"
281 //usage: "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
283 //usage:#define zcat_trivial_usage
285 //usage:#define zcat_full_usage "\n\n"
286 //usage: "Decompress to stdout"
288 //config:config GUNZIP
289 //config: bool "gunzip"
292 //config: gunzip is used to decompress archives created by gzip.
293 //config: You can use the `-t' option to test the integrity of
294 //config: an archive, without decompressing it.
296 //applet:IF_GUNZIP(APPLET(gunzip, BB_DIR_BIN, BB_SUID_DROP))
297 //applet:IF_GUNZIP(APPLET_ODDNAME(zcat, gunzip, BB_DIR_BIN, BB_SUID_DROP, zcat))
298 //kbuild:lib-$(CONFIG_GZIP) += bbunzip.o
299 //kbuild:lib-$(CONFIG_GUNZIP) += bbunzip.o
302 char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
304 char *extension = strrchr(filename, '.');
310 if (strcmp(extension, "tgz" + 1) == 0
311 #if ENABLE_FEATURE_SEAMLESS_Z
312 || (extension[0] == 'Z' && extension[1] == '\0')
315 extension[-1] = '\0';
316 } else if (strcmp(extension, "tgz") == 0) {
317 filename = xstrdup(filename);
318 extension = strrchr(filename, '.');
327 IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(transformer_aux_data_t *aux)
329 return unpack_gz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
332 * Linux kernel build uses gzip -d -n. We accept and ignore it.
335 * gzip: do not save the original file name and time stamp.
336 * (The original name is always saved if the name had to be truncated.)
337 * gunzip: do not restore the original file name/time even if present
338 * (remove only the gzip suffix from the compressed file name).
339 * This option is the default when decompressing.
341 * gzip: always save the original file name and time stamp (this is the default)
342 * gunzip: restore the original file name and time stamp if present.
344 int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
345 int gunzip_main(int argc UNUSED_PARAM, char **argv)
347 getopt32(argv, "cfvqdtn");
350 /* If called as zcat...
351 * Normally, "zcat" is just "gunzip -c".
352 * But if seamless magic is enabled, then we are much more clever.
354 if (applet_name[1] == 'c')
355 option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
357 return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL);
363 * Modified for busybox by Glenn McGrath
364 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
366 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
368 //usage:#define bunzip2_trivial_usage
369 //usage: "[-cf] [FILE]..."
370 //usage:#define bunzip2_full_usage "\n\n"
371 //usage: "Decompress FILEs (or stdin)\n"
372 //usage: "\n -c Write to stdout"
373 //usage: "\n -f Force"
374 //usage:#define bzcat_trivial_usage
376 //usage:#define bzcat_full_usage "\n\n"
377 //usage: "Decompress to stdout"
379 //config:config BUNZIP2
380 //config: bool "bunzip2"
383 //config: bunzip2 is a compression utility using the Burrows-Wheeler block
384 //config: sorting text compression algorithm, and Huffman coding. Compression
385 //config: is generally considerably better than that achieved by more
386 //config: conventional LZ77/LZ78-based compressors, and approaches the
387 //config: performance of the PPM family of statistical compressors.
389 //config: Unless you have a specific application which requires bunzip2, you
390 //config: should probably say N here.
392 //applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
393 //applet:IF_BUNZIP2(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
394 //kbuild:lib-$(CONFIG_BZIP2) += bbunzip.o
395 //kbuild:lib-$(CONFIG_BUNZIP2) += bbunzip.o
398 IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(transformer_aux_data_t *aux)
400 return unpack_bz2_stream(aux, STDIN_FILENO, STDOUT_FILENO);
402 int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
403 int bunzip2_main(int argc UNUSED_PARAM, char **argv)
405 getopt32(argv, "cfvqdt");
407 if (applet_name[2] == 'c') /* bzcat */
408 option_mask32 |= OPT_STDOUT;
410 return bbunpack(argv, unpack_bunzip2, make_new_name_generic, "bz2");
416 * Small lzma deflate implementation.
417 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
419 * Based on bunzip.c from busybox
421 * Licensed under GPLv2, see file LICENSE in this source tree.
423 //usage:#define unlzma_trivial_usage
424 //usage: "[-cf] [FILE]..."
425 //usage:#define unlzma_full_usage "\n\n"
426 //usage: "Decompress FILE (or stdin)\n"
427 //usage: "\n -c Write to stdout"
428 //usage: "\n -f Force"
430 //usage:#define lzma_trivial_usage
431 //usage: "-d [-cf] [FILE]..."
432 //usage:#define lzma_full_usage "\n\n"
433 //usage: "Decompress FILE (or stdin)\n"
434 //usage: "\n -d Decompress"
435 //usage: "\n -c Write to stdout"
436 //usage: "\n -f Force"
438 //usage:#define lzcat_trivial_usage
440 //usage:#define lzcat_full_usage "\n\n"
441 //usage: "Decompress to stdout"
443 //usage:#define unxz_trivial_usage
444 //usage: "[-cf] [FILE]..."
445 //usage:#define unxz_full_usage "\n\n"
446 //usage: "Decompress FILE (or stdin)\n"
447 //usage: "\n -c Write to stdout"
448 //usage: "\n -f Force"
450 //usage:#define xz_trivial_usage
451 //usage: "-d [-cf] [FILE]..."
452 //usage:#define xz_full_usage "\n\n"
453 //usage: "Decompress FILE (or stdin)\n"
454 //usage: "\n -d Decompress"
455 //usage: "\n -c Write to stdout"
456 //usage: "\n -f Force"
458 //usage:#define xzcat_trivial_usage
460 //usage:#define xzcat_full_usage "\n\n"
461 //usage: "Decompress to stdout"
463 //config:config UNLZMA
464 //config: bool "unlzma"
467 //config: unlzma is a compression utility using the Lempel-Ziv-Markov chain
468 //config: compression algorithm, and range coding. Compression
469 //config: is generally considerably better than that achieved by the bzip2
470 //config: compressors.
472 //config: The BusyBox unlzma applet is limited to decompression only.
473 //config: On an x86 system, this applet adds about 4K.
475 //config:config FEATURE_LZMA_FAST
476 //config: bool "Optimize unlzma for speed"
478 //config: depends on UNLZMA
480 //config: This option reduces decompression time by about 25% at the cost of
481 //config: a 1K bigger binary.
484 //config: bool "Provide lzma alias which supports only unpacking"
486 //config: depends on UNLZMA
488 //config: Enable this option if you want commands like "lzma -d" to work.
489 //config: IOW: you'll get lzma applet, but it will always require -d option.
491 //applet:IF_UNLZMA(APPLET(unlzma, BB_DIR_USR_BIN, BB_SUID_DROP))
492 //applet:IF_UNLZMA(APPLET_ODDNAME(lzcat, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzcat))
493 //applet:IF_LZMA(APPLET_ODDNAME(lzma, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzma))
494 //kbuild:lib-$(CONFIG_UNLZMA) += bbunzip.o
497 IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(transformer_aux_data_t *aux)
499 return unpack_lzma_stream(aux, STDIN_FILENO, STDOUT_FILENO);
501 int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
502 int unlzma_main(int argc UNUSED_PARAM, char **argv)
504 IF_LZMA(int opts =) getopt32(argv, "cfvqdt");
506 /* lzma without -d or -t? */
507 if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
511 if (applet_name[2] == 'c')
512 option_mask32 |= OPT_STDOUT;
515 return bbunpack(argv, unpack_unlzma, make_new_name_generic, "lzma");
521 //config: bool "unxz"
524 //config: unxz is a unlzma successor.
527 //config: bool "Provide xz alias which supports only unpacking"
529 //config: depends on UNXZ
531 //config: Enable this option if you want commands like "xz -d" to work.
532 //config: IOW: you'll get xz applet, but it will always require -d option.
534 //applet:IF_UNXZ(APPLET(unxz, BB_DIR_USR_BIN, BB_SUID_DROP))
535 //applet:IF_UNXZ(APPLET_ODDNAME(xzcat, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xzcat))
536 //applet:IF_XZ(APPLET_ODDNAME(xz, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xz))
537 //kbuild:lib-$(CONFIG_UNXZ) += bbunzip.o
540 IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(transformer_aux_data_t *aux)
542 return unpack_xz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
544 int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
545 int unxz_main(int argc UNUSED_PARAM, char **argv)
547 IF_XZ(int opts =) getopt32(argv, "cfvqdt");
549 /* xz without -d or -t? */
550 if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
554 if (applet_name[2] == 'c')
555 option_mask32 |= OPT_STDOUT;
558 return bbunpack(argv, unpack_unxz, make_new_name_generic, "xz");