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"
13 /* only some decompressors: */
16 OPT_DECOMPRESS = 1 << 4,
18 SEAMLESS_MAGIC = (1 << 31) * SEAMLESS_COMPRESSION,
22 int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
24 int fd = open3_or_warn(filename, flags, mode);
32 char* FAST_FUNC append_ext(char *filename, const char *expected_ext)
34 return xasprintf("%s.%s", filename, expected_ext);
37 int FAST_FUNC bbunpack(char **argv,
38 IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(transformer_aux_data_t *aux),
39 char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext),
40 const char *expected_ext
44 IF_DESKTOP(long long) int status = 0;
45 char *filename, *new_name;
46 smallint exitcode = 0;
47 transformer_aux_data_t aux;
50 /* NB: new_name is *maybe* malloc'ed! */
52 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
54 if (filename && LONE_DASH(filename))
59 if (!(option_mask32 & SEAMLESS_MAGIC)) {
60 if (stat(filename, &stat_buf) != 0) {
62 bb_simple_perror_msg(filename);
67 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
70 /* "clever zcat" with FILE */
71 int fd = open_zipped(filename);
74 xmove_fd(fd, STDIN_FILENO);
77 if (option_mask32 & SEAMLESS_MAGIC) {
78 /* "clever zcat" on stdin */
79 if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_detected*/ 0))
83 /* Special cases: test, stdout */
84 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
85 if (option_mask32 & OPT_TEST)
86 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
91 /* Open dst if we are going to unpack to file */
93 new_name = make_new_name(filename, expected_ext);
95 bb_error_msg("%s: unknown suffix - ignored", filename);
99 /* -f: overwrite existing output files */
100 if (option_mask32 & OPT_FORCE) {
104 /* O_EXCL: "real" bunzip2 doesn't overwrite files */
105 /* GNU gunzip does not bail out, but goes to next file */
106 if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
111 /* Check that the input is sane */
112 if (!(option_mask32 & OPT_FORCE) && isatty(STDIN_FILENO)) {
113 bb_error_msg_and_die("compressed data not read from terminal, "
114 "use -f to force it");
117 if (!(option_mask32 & SEAMLESS_MAGIC)) {
118 init_transformer_aux_data(&aux);
119 aux.check_signature = 1;
120 status = unpacker(&aux);
124 if (bb_copyfd_eof(STDIN_FILENO, STDOUT_FILENO) < 0)
125 /* Disk full, tty closed, etc. No point in continuing */
129 if (!(option_mask32 & OPT_STDOUT))
130 xclose(STDOUT_FILENO); /* with error check! */
133 char *del = new_name;
135 /* TODO: restore other things? */
136 if (aux.mtime != 0) {
137 struct timeval times[2];
139 times[1].tv_sec = times[0].tv_sec = aux.mtime;
140 times[1].tv_usec = times[0].tv_usec = 0;
141 /* Note: we closed it first.
142 * On some systems calling utimes
143 * then closing resets the mtime
144 * back to current time. */
145 utimes(new_name, times); /* ignoring errors */
148 /* Delete _compressed_ file */
150 /* restore extension (unless tgz -> tar case) */
151 if (new_name == filename)
152 filename[strlen(filename)] = '.';
156 #if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
157 /* Extreme bloat for gunzip compat */
158 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
159 fprintf(stderr, "%s: %u%% - replaced with %s\n",
160 filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
165 if (new_name != filename)
168 } while (*argv && *++argv);
170 if (option_mask32 & OPT_STDOUT)
171 xclose(STDOUT_FILENO); /* with error check! */
176 #if ENABLE_UNCOMPRESS || ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNXZ
178 char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
180 char *extension = strrchr(filename, '.');
181 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
182 /* Mimic GNU gunzip - "real" bunzip2 tries to */
183 /* unpack file anyway, to file.out */
193 * Uncompress applet for busybox (c) 2002 Glenn McGrath
195 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
198 //usage:#define uncompress_trivial_usage
199 //usage: "[-cf] [FILE]..."
200 //usage:#define uncompress_full_usage "\n\n"
201 //usage: "Decompress .Z file[s]\n"
202 //usage: "\n -c Write to stdout"
203 //usage: "\n -f Overwrite"
205 #if ENABLE_UNCOMPRESS
207 IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(transformer_aux_data_t *aux)
209 return unpack_Z_stream(aux, STDIN_FILENO, STDOUT_FILENO);
211 int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
212 int uncompress_main(int argc UNUSED_PARAM, char **argv)
214 getopt32(argv, "cf");
217 return bbunpack(argv, unpack_uncompress, make_new_name_generic, "Z");
223 * Gzip implementation for busybox
225 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
227 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
228 * based on gzip sources
230 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
231 * well as stdin/stdout, and to generally behave itself wrt command line
234 * General cleanup to better adhere to the style guide and make use of standard
235 * busybox functions by Glenn McGrath
237 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
239 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
240 * Copyright (C) 1992-1993 Jean-loup Gailly
241 * The unzip code was written and put in the public domain by Mark Adler.
242 * Portions of the lzw code are derived from the public domain 'compress'
243 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
244 * Ken Turkowski, Dave Mack and Peter Jannesen.
246 * See the license_msg below and the file COPYING for the software license.
247 * See the file algorithm.doc for the compression algorithms and file formats.
250 //usage:#define gunzip_trivial_usage
251 //usage: "[-cft] [FILE]..."
252 //usage:#define gunzip_full_usage "\n\n"
253 //usage: "Decompress FILEs (or stdin)\n"
254 //usage: "\n -c Write to stdout"
255 //usage: "\n -f Force"
256 //usage: "\n -t Test file integrity"
258 //usage:#define gunzip_example_usage
259 //usage: "$ ls -la /tmp/BusyBox*\n"
260 //usage: "-rw-rw-r-- 1 andersen andersen 557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n"
261 //usage: "$ gunzip /tmp/BusyBox-0.43.tar.gz\n"
262 //usage: "$ ls -la /tmp/BusyBox*\n"
263 //usage: "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
265 //usage:#define zcat_trivial_usage
267 //usage:#define zcat_full_usage "\n\n"
268 //usage: "Decompress to stdout"
272 char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
274 char *extension = strrchr(filename, '.');
280 if (strcmp(extension, "tgz" + 1) == 0
281 #if ENABLE_FEATURE_SEAMLESS_Z
282 || (extension[0] == 'Z' && extension[1] == '\0')
285 extension[-1] = '\0';
286 } else if (strcmp(extension, "tgz") == 0) {
287 filename = xstrdup(filename);
288 extension = strrchr(filename, '.');
297 IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(transformer_aux_data_t *aux)
299 return unpack_gz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
302 * Linux kernel build uses gzip -d -n. We accept and ignore it.
305 * gzip: do not save the original file name and time stamp.
306 * (The original name is always saved if the name had to be truncated.)
307 * gunzip: do not restore the original file name/time even if present
308 * (remove only the gzip suffix from the compressed file name).
309 * This option is the default when decompressing.
311 * gzip: always save the original file name and time stamp (this is the default)
312 * gunzip: restore the original file name and time stamp if present.
314 int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
315 int gunzip_main(int argc UNUSED_PARAM, char **argv)
317 getopt32(argv, "cfvdtqn");
320 /* If called as zcat...
321 * Normally, "zcat" is just "gunzip -c".
322 * But if seamless magic is enabled, then we are much more clever.
324 if (applet_name[1] == 'c')
325 option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
327 return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL);
333 * Modified for busybox by Glenn McGrath
334 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
336 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
338 //usage:#define bunzip2_trivial_usage
339 //usage: "[-cf] [FILE]..."
340 //usage:#define bunzip2_full_usage "\n\n"
341 //usage: "Decompress FILEs (or stdin)\n"
342 //usage: "\n -c Write to stdout"
343 //usage: "\n -f Force"
344 //usage:#define bzcat_trivial_usage
346 //usage:#define bzcat_full_usage "\n\n"
347 //usage: "Decompress to stdout"
348 //applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
349 //applet:IF_BUNZIP2(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
352 IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(transformer_aux_data_t *aux)
354 return unpack_bz2_stream(aux, STDIN_FILENO, STDOUT_FILENO);
356 int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
357 int bunzip2_main(int argc UNUSED_PARAM, char **argv)
359 getopt32(argv, "cfvdtq");
361 if (applet_name[2] == 'c') /* bzcat */
362 option_mask32 |= OPT_STDOUT;
364 return bbunpack(argv, unpack_bunzip2, make_new_name_generic, "bz2");
370 * Small lzma deflate implementation.
371 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
373 * Based on bunzip.c from busybox
375 * Licensed under GPLv2, see file LICENSE in this source tree.
378 //usage:#define unlzma_trivial_usage
379 //usage: "[-cf] [FILE]..."
380 //usage:#define unlzma_full_usage "\n\n"
381 //usage: "Decompress FILE (or stdin)\n"
382 //usage: "\n -c Write to stdout"
383 //usage: "\n -f Force"
385 //usage:#define lzma_trivial_usage
386 //usage: "-d [-cf] [FILE]..."
387 //usage:#define lzma_full_usage "\n\n"
388 //usage: "Decompress FILE (or stdin)\n"
389 //usage: "\n -d Decompress"
390 //usage: "\n -c Write to stdout"
391 //usage: "\n -f Force"
393 //usage:#define lzcat_trivial_usage
395 //usage:#define lzcat_full_usage "\n\n"
396 //usage: "Decompress to stdout"
398 //usage:#define unxz_trivial_usage
399 //usage: "[-cf] [FILE]..."
400 //usage:#define unxz_full_usage "\n\n"
401 //usage: "Decompress FILE (or stdin)\n"
402 //usage: "\n -c Write to stdout"
403 //usage: "\n -f Force"
405 //usage:#define xz_trivial_usage
406 //usage: "-d [-cf] [FILE]..."
407 //usage:#define xz_full_usage "\n\n"
408 //usage: "Decompress FILE (or stdin)\n"
409 //usage: "\n -d Decompress"
410 //usage: "\n -c Write to stdout"
411 //usage: "\n -f Force"
413 //usage:#define xzcat_trivial_usage
415 //usage:#define xzcat_full_usage "\n\n"
416 //usage: "Decompress to stdout"
420 IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(transformer_aux_data_t *aux)
422 return unpack_lzma_stream(aux, STDIN_FILENO, STDOUT_FILENO);
424 int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
425 int unlzma_main(int argc UNUSED_PARAM, char **argv)
427 IF_LZMA(int opts =) getopt32(argv, "cfvdtq");
429 /* lzma without -d or -t? */
430 if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
434 if (applet_name[2] == 'c')
435 option_mask32 |= OPT_STDOUT;
438 return bbunpack(argv, unpack_unlzma, make_new_name_generic, "lzma");
445 IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(transformer_aux_data_t *aux)
447 return unpack_xz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
449 int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
450 int unxz_main(int argc UNUSED_PARAM, char **argv)
452 IF_XZ(int opts =) getopt32(argv, "cfvdtq");
454 /* xz without -d or -t? */
455 if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
459 if (applet_name[2] == 'c')
460 option_mask32 |= OPT_STDOUT;
463 return bbunpack(argv, unpack_unxz, make_new_name_generic, "xz");