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 //applet:IF_UNCOMPRESS(APPLET(uncompress, BB_DIR_BIN, BB_SUID_DROP))
219 //kbuild:lib-$(CONFIG_UNCOMPRESS) += bbunzip.o
220 #if ENABLE_UNCOMPRESS
222 IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(transformer_aux_data_t *aux)
224 return unpack_Z_stream(aux, STDIN_FILENO, STDOUT_FILENO);
226 int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
227 int uncompress_main(int argc UNUSED_PARAM, char **argv)
229 getopt32(argv, "cf");
232 return bbunpack(argv, unpack_uncompress, make_new_name_generic, "Z");
238 * Gzip implementation for busybox
240 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
242 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
243 * based on gzip sources
245 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
246 * well as stdin/stdout, and to generally behave itself wrt command line
249 * General cleanup to better adhere to the style guide and make use of standard
250 * busybox functions by Glenn McGrath
252 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
254 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
255 * Copyright (C) 1992-1993 Jean-loup Gailly
256 * The unzip code was written and put in the public domain by Mark Adler.
257 * Portions of the lzw code are derived from the public domain 'compress'
258 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
259 * Ken Turkowski, Dave Mack and Peter Jannesen.
261 //usage:#define gunzip_trivial_usage
262 //usage: "[-cft] [FILE]..."
263 //usage:#define gunzip_full_usage "\n\n"
264 //usage: "Decompress FILEs (or stdin)\n"
265 //usage: "\n -c Write to stdout"
266 //usage: "\n -f Force"
267 //usage: "\n -t Test file integrity"
269 //usage:#define gunzip_example_usage
270 //usage: "$ ls -la /tmp/BusyBox*\n"
271 //usage: "-rw-rw-r-- 1 andersen andersen 557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n"
272 //usage: "$ gunzip /tmp/BusyBox-0.43.tar.gz\n"
273 //usage: "$ ls -la /tmp/BusyBox*\n"
274 //usage: "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
276 //usage:#define zcat_trivial_usage
278 //usage:#define zcat_full_usage "\n\n"
279 //usage: "Decompress to stdout"
281 //applet:IF_GUNZIP(APPLET(gunzip, BB_DIR_BIN, BB_SUID_DROP))
282 //applet:IF_GUNZIP(APPLET_ODDNAME(zcat, gunzip, BB_DIR_BIN, BB_SUID_DROP, zcat))
283 //kbuild:lib-$(CONFIG_GZIP) += bbunzip.o
284 //kbuild:lib-$(CONFIG_GUNZIP) += bbunzip.o
287 char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
289 char *extension = strrchr(filename, '.');
295 if (strcmp(extension, "tgz" + 1) == 0
296 #if ENABLE_FEATURE_SEAMLESS_Z
297 || (extension[0] == 'Z' && extension[1] == '\0')
300 extension[-1] = '\0';
301 } else if (strcmp(extension, "tgz") == 0) {
302 filename = xstrdup(filename);
303 extension = strrchr(filename, '.');
312 IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(transformer_aux_data_t *aux)
314 return unpack_gz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
317 * Linux kernel build uses gzip -d -n. We accept and ignore it.
320 * gzip: do not save the original file name and time stamp.
321 * (The original name is always saved if the name had to be truncated.)
322 * gunzip: do not restore the original file name/time even if present
323 * (remove only the gzip suffix from the compressed file name).
324 * This option is the default when decompressing.
326 * gzip: always save the original file name and time stamp (this is the default)
327 * gunzip: restore the original file name and time stamp if present.
329 int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
330 int gunzip_main(int argc UNUSED_PARAM, char **argv)
332 getopt32(argv, "cfvqdtn");
335 /* If called as zcat...
336 * Normally, "zcat" is just "gunzip -c".
337 * But if seamless magic is enabled, then we are much more clever.
339 if (applet_name[1] == 'c')
340 option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
342 return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL);
348 * Modified for busybox by Glenn McGrath
349 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
351 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
353 //usage:#define bunzip2_trivial_usage
354 //usage: "[-cf] [FILE]..."
355 //usage:#define bunzip2_full_usage "\n\n"
356 //usage: "Decompress FILEs (or stdin)\n"
357 //usage: "\n -c Write to stdout"
358 //usage: "\n -f Force"
359 //usage:#define bzcat_trivial_usage
361 //usage:#define bzcat_full_usage "\n\n"
362 //usage: "Decompress to stdout"
364 //applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
365 //applet:IF_BUNZIP2(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
366 //kbuild:lib-$(CONFIG_BZIP2) += bbunzip.o
367 //kbuild:lib-$(CONFIG_BUNZIP2) += bbunzip.o
370 IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(transformer_aux_data_t *aux)
372 return unpack_bz2_stream(aux, STDIN_FILENO, STDOUT_FILENO);
374 int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
375 int bunzip2_main(int argc UNUSED_PARAM, char **argv)
377 getopt32(argv, "cfvqdt");
379 if (applet_name[2] == 'c') /* bzcat */
380 option_mask32 |= OPT_STDOUT;
382 return bbunpack(argv, unpack_bunzip2, make_new_name_generic, "bz2");
388 * Small lzma deflate implementation.
389 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
391 * Based on bunzip.c from busybox
393 * Licensed under GPLv2, see file LICENSE in this source tree.
395 //usage:#define unlzma_trivial_usage
396 //usage: "[-cf] [FILE]..."
397 //usage:#define unlzma_full_usage "\n\n"
398 //usage: "Decompress FILE (or stdin)\n"
399 //usage: "\n -c Write to stdout"
400 //usage: "\n -f Force"
402 //usage:#define lzma_trivial_usage
403 //usage: "-d [-cf] [FILE]..."
404 //usage:#define lzma_full_usage "\n\n"
405 //usage: "Decompress FILE (or stdin)\n"
406 //usage: "\n -d Decompress"
407 //usage: "\n -c Write to stdout"
408 //usage: "\n -f Force"
410 //usage:#define lzcat_trivial_usage
412 //usage:#define lzcat_full_usage "\n\n"
413 //usage: "Decompress to stdout"
415 //usage:#define unxz_trivial_usage
416 //usage: "[-cf] [FILE]..."
417 //usage:#define unxz_full_usage "\n\n"
418 //usage: "Decompress FILE (or stdin)\n"
419 //usage: "\n -c Write to stdout"
420 //usage: "\n -f Force"
422 //usage:#define xz_trivial_usage
423 //usage: "-d [-cf] [FILE]..."
424 //usage:#define xz_full_usage "\n\n"
425 //usage: "Decompress FILE (or stdin)\n"
426 //usage: "\n -d Decompress"
427 //usage: "\n -c Write to stdout"
428 //usage: "\n -f Force"
430 //usage:#define xzcat_trivial_usage
432 //usage:#define xzcat_full_usage "\n\n"
433 //usage: "Decompress to stdout"
435 //applet:IF_UNLZMA(APPLET(unlzma, BB_DIR_USR_BIN, BB_SUID_DROP))
436 //kbuild:lib-$(CONFIG_UNLZMA) += bbunzip.o
439 IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(transformer_aux_data_t *aux)
441 return unpack_lzma_stream(aux, STDIN_FILENO, STDOUT_FILENO);
443 int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
444 int unlzma_main(int argc UNUSED_PARAM, char **argv)
446 IF_LZMA(int opts =) getopt32(argv, "cfvqdt");
448 /* lzma without -d or -t? */
449 if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
453 if (applet_name[2] == 'c')
454 option_mask32 |= OPT_STDOUT;
457 return bbunpack(argv, unpack_unlzma, make_new_name_generic, "lzma");
462 //applet:IF_UNXZ(APPLET(unxz, BB_DIR_USR_BIN, BB_SUID_DROP))
463 //kbuild:lib-$(CONFIG_UNXZ) += bbunzip.o
466 IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(transformer_aux_data_t *aux)
468 return unpack_xz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
470 int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
471 int unxz_main(int argc UNUSED_PARAM, char **argv)
473 IF_XZ(int opts =) getopt32(argv, "cfvqdt");
475 /* xz without -d or -t? */
476 if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
480 if (applet_name[2] == 'c')
481 option_mask32 |= OPT_STDOUT;
484 return bbunpack(argv, unpack_unxz, make_new_name_generic, "xz");