summaryrefslogtreecommitdiff
path: root/archival/bbunzip.c (plain)
blob: d5db4627f245238f1fd7ab8106f3f10f3066d683
1/* vi: set sw=4 ts=4: */
2/*
3 * Common code for gunzip-like applets
4 *
5 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6 */
7#include "libbb.h"
8#include "bb_archive.h"
9
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
14
15/* Note: must be kept in sync with archival/lzop.c */
16enum {
17 OPT_STDOUT = 1 << 0,
18 OPT_FORCE = 1 << 1,
19 /* only some decompressors: */
20 OPT_VERBOSE = 1 << 2,
21 OPT_QUIET = 1 << 3,
22 OPT_DECOMPRESS = 1 << 4,
23 OPT_TEST = 1 << 5,
24 SEAMLESS_MAGIC = (1 << 31) * SEAMLESS_COMPRESSION,
25};
26
27static
28int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
29{
30 int fd = open3_or_warn(filename, flags, mode);
31 if (fd < 0) {
32 return 1;
33 }
34 xmove_fd(fd, to_fd);
35 return 0;
36}
37
38char* FAST_FUNC append_ext(char *filename, const char *expected_ext)
39{
40 return xasprintf("%s.%s", filename, expected_ext);
41}
42
43int 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
47)
48{
49 struct stat stat_buf;
50 IF_DESKTOP(long long) int status = 0;
51 char *filename, *new_name;
52 smallint exitcode = 0;
53 transformer_state_t xstate;
54
55 do {
56 /* NB: new_name is *maybe* malloc'ed! */
57 new_name = NULL;
58 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
59
60 if (filename && LONE_DASH(filename))
61 filename = NULL;
62
63 /* Open src */
64 if (filename) {
65 if (!(option_mask32 & SEAMLESS_MAGIC)) {
66 if (stat(filename, &stat_buf) != 0) {
67 err_name:
68 bb_simple_perror_msg(filename);
69 err:
70 exitcode = 1;
71 goto free_name;
72 }
73 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
74 goto err;
75 } else {
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);
79 if (fd < 0)
80 goto err_name;
81 xmove_fd(fd, STDIN_FILENO);
82 }
83 } else
84 if (option_mask32 & SEAMLESS_MAGIC) {
85 /* "clever zcat" on stdin */
86 if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_compressed*/ 1))
87 goto err;
88 }
89
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))
94 xfunc_die();
95 filename = NULL;
96 }
97
98 /* Open dst if we are going to unpack to file */
99 if (filename) {
100 new_name = make_new_name(filename, expected_ext);
101 if (!new_name) {
102 bb_error_msg("%s: unknown suffix - ignored", filename);
103 goto err;
104 }
105
106 /* -f: overwrite existing output files */
107 if (option_mask32 & OPT_FORCE) {
108 unlink(new_name);
109 }
110
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,
114 stat_buf.st_mode))
115 goto err;
116 }
117
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");
122 }
123
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);
130 if (status < 0)
131 exitcode = 1;
132 } else {
133 if (bb_copyfd_eof(STDIN_FILENO, STDOUT_FILENO) < 0)
134 /* Disk full, tty closed, etc. No point in continuing */
135 xfunc_die();
136 }
137
138 if (!(option_mask32 & OPT_STDOUT))
139 xclose(STDOUT_FILENO); /* with error check! */
140
141 if (filename) {
142 char *del = new_name;
143
144 if (status >= 0) {
145 unsigned new_name_len;
146
147 /* TODO: restore other things? */
148 if (xstate.mtime != 0) {
149 struct timeval times[2];
150
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 */
158 }
159
160 if (ENABLE_DESKTOP)
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] = '.';
166 }
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)
172 : 0;
173 fprintf(stderr, "%s: %u%% - replaced with %.*s\n",
174 filename,
175 100u - percent,
176 new_name_len, new_name
177 );
178 }
179 /* Delete _source_ file */
180 del = filename;
181 }
182 xunlink(del);
183 free_name:
184 if (new_name != filename)
185 free(new_name);
186 }
187 } while (*argv && *++argv);
188
189 if (option_mask32 & OPT_STDOUT)
190 xclose(STDOUT_FILENO); /* with error check! */
191
192 return exitcode;
193}
194
195#if ENABLE_UNCOMPRESS \
196 || ENABLE_BUNZIP2 || ENABLE_BZCAT \
197 || ENABLE_UNLZMA || ENABLE_LZCAT || ENABLE_LZMA \
198 || ENABLE_UNXZ || ENABLE_XZCAT || ENABLE_XZ
199static
200char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
201{
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 */
206 return NULL;
207 }
208 *extension = '\0';
209 return filename;
210}
211#endif
212
213
214/*
215 * Uncompress applet for busybox (c) 2002 Glenn McGrath
216 *
217 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
218 */
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"
225
226//config:config UNCOMPRESS
227//config: bool "uncompress"
228//config: default n # ancient
229//config: help
230//config: uncompress is used to decompress archives created by compress.
231//config: Not much used anymore, replaced by gzip/gunzip.
232
233//applet:IF_UNCOMPRESS(APPLET(uncompress, BB_DIR_BIN, BB_SUID_DROP))
234//kbuild:lib-$(CONFIG_UNCOMPRESS) += bbunzip.o
235#if ENABLE_UNCOMPRESS
236int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
237int uncompress_main(int argc UNUSED_PARAM, char **argv)
238{
239 getopt32(argv, "cf");
240 argv += optind;
241
242 return bbunpack(argv, unpack_Z_stream, make_new_name_generic, "Z");
243}
244#endif
245
246
247/*
248 * Gzip implementation for busybox
249 *
250 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
251 *
252 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
253 * based on gzip sources
254 *
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
257 * handling.
258 *
259 * General cleanup to better adhere to the style guide and make use of standard
260 * busybox functions by Glenn McGrath
261 *
262 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
263 *
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.
270 */
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"
278//usage:
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"
285//usage:
286//usage:#define zcat_trivial_usage
287//usage: "[FILE]..."
288//usage:#define zcat_full_usage "\n\n"
289//usage: "Decompress to stdout"
290
291//config:config GUNZIP
292//config: bool "gunzip"
293//config: default y
294//config: help
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.
298//config:
299//config:config ZCAT
300//config: bool "zcat"
301//config: default y
302//config: help
303//config: Alias to "gunzip -c".
304//config:
305//config:config FEATURE_GUNZIP_LONG_OPTIONS
306//config: bool "Enable long options"
307//config: default y
308//config: depends on (GUNZIP || ZCAT) && LONG_OPTS
309//config: help
310//config: Enable use of long options.
311
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
317static
318char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
319{
320 char *extension = strrchr(filename, '.');
321
322 if (!extension)
323 return NULL;
324
325 extension++;
326 if (strcmp(extension, "tgz" + 1) == 0
327#if ENABLE_FEATURE_SEAMLESS_Z
328 || (extension[0] == 'Z' && extension[1] == '\0')
329#endif
330 ) {
331 extension[-1] = '\0';
332 } else if (strcmp(extension, "tgz") == 0) {
333 filename = xstrdup(filename);
334 extension = strrchr(filename, '.');
335 extension[2] = 'a';
336 extension[3] = 'r';
337 } else {
338 return NULL;
339 }
340 return filename;
341}
342
343#if ENABLE_FEATURE_GUNZIP_LONG_OPTIONS
344static 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"
350 ;
351#endif
352
353/*
354 * Linux kernel build uses gzip -d -n. We accept and ignore it.
355 * Man page says:
356 * -n --no-name
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.
362 * -N --name
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.
365 */
366int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
367int gunzip_main(int argc UNUSED_PARAM, char **argv)
368{
369#if ENABLE_FEATURE_GUNZIP_LONG_OPTIONS
370 applet_long_options = gunzip_longopts;
371#endif
372 getopt32(argv, "cfvqdtn");
373 argv += optind;
374
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.
378 */
379 if (ENABLE_ZCAT && applet_name[1] == 'c')
380 option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
381
382 return bbunpack(argv, unpack_gz_stream, make_new_name_gunzip, /*unused:*/ NULL);
383}
384#endif
385
386
387/*
388 * Modified for busybox by Glenn McGrath
389 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
390 *
391 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
392 */
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
400//usage: "[FILE]..."
401//usage:#define bzcat_full_usage "\n\n"
402//usage: "Decompress to stdout"
403
404//config:config BUNZIP2
405//config: bool "bunzip2"
406//config: default y
407//config: help
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.
413//config:
414//config: Unless you have a specific application which requires bunzip2, you
415//config: should probably say N here.
416//config:
417//config:config BZCAT
418//config: bool "bzcat"
419//config: default y
420//config: help
421//config: Alias to "bunzip2 -c".
422
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
428int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
429int bunzip2_main(int argc UNUSED_PARAM, char **argv)
430{
431 getopt32(argv, "cfvqdt");
432 argv += optind;
433 if (ENABLE_BZCAT && applet_name[2] == 'c') /* bzcat */
434 option_mask32 |= OPT_STDOUT;
435
436 return bbunpack(argv, unpack_bz2_stream, make_new_name_generic, "bz2");
437}
438#endif
439
440
441/*
442 * Small lzma deflate implementation.
443 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
444 *
445 * Based on bunzip.c from busybox
446 *
447 * Licensed under GPLv2, see file LICENSE in this source tree.
448 */
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"
455//usage:
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"
463//usage:
464//usage:#define lzcat_trivial_usage
465//usage: "[FILE]..."
466//usage:#define lzcat_full_usage "\n\n"
467//usage: "Decompress to stdout"
468//usage:
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"
475//usage:
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"
483//usage:
484//usage:#define xzcat_trivial_usage
485//usage: "[FILE]..."
486//usage:#define xzcat_full_usage "\n\n"
487//usage: "Decompress to stdout"
488
489//config:config UNLZMA
490//config: bool "unlzma"
491//config: default y
492//config: help
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.
497//config:
498//config: The BusyBox unlzma applet is limited to decompression only.
499//config: On an x86 system, this applet adds about 4K.
500//config:
501//config:config LZCAT
502//config: bool "lzcat"
503//config: default y
504//config: help
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.
509//config:
510//config: The BusyBox unlzma applet is limited to decompression only.
511//config: On an x86 system, this applet adds about 4K.
512//config:
513//config:config LZMA
514//config: bool "lzma -d"
515//config: default y
516//config: help
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.
519//config:
520//config:config FEATURE_LZMA_FAST
521//config: bool "Optimize unlzma for speed"
522//config: default n
523//config: depends on UNLZMA || LZCAT || LZMA
524//config: help
525//config: This option reduces decompression time by about 25% at the cost of
526//config: a 1K bigger binary.
527
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
535int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
536int unlzma_main(int argc UNUSED_PARAM, char **argv)
537{
538 IF_LZMA(int opts =) getopt32(argv, "cfvqdt");
539# if ENABLE_LZMA
540 /* lzma without -d or -t? */
541 if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
542 bb_show_usage();
543# endif
544 /* lzcat? */
545 if (ENABLE_LZCAT && applet_name[2] == 'c')
546 option_mask32 |= OPT_STDOUT;
547
548 argv += optind;
549 return bbunpack(argv, unpack_lzma_stream, make_new_name_generic, "lzma");
550}
551#endif
552
553
554//config:config UNXZ
555//config: bool "unxz"
556//config: default y
557//config: help
558//config: unxz is a unlzma successor.
559//config:
560//config:config XZCAT
561//config: bool "xzcat"
562//config: default y
563//config: help
564//config: Alias to "unxz -c".
565//config:
566//config:config XZ
567//config: bool "xz -d"
568//config: default y
569//config: help
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.
572
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
580int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
581int unxz_main(int argc UNUSED_PARAM, char **argv)
582{
583 IF_XZ(int opts =) getopt32(argv, "cfvqdt");
584# if ENABLE_XZ
585 /* xz without -d or -t? */
586 if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
587 bb_show_usage();
588# endif
589 /* xzcat? */
590 if (ENABLE_XZCAT && applet_name[2] == 'c')
591 option_mask32 |= OPT_STDOUT;
592
593 argv += optind;
594 return bbunpack(argv, unpack_xz_stream, make_new_name_generic, "xz");
595}
596#endif
597