blob: 0e2a261f00164323bc0c4069a11f8b1b5c6e485c [file] [log] [blame]
Denis Vlasenkoab9eef22007-03-07 22:02:23 +00001/* vi: set sw=4 ts=4: */
2/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Common code for gunzip-like applets
Denis Vlasenkoab9eef22007-03-07 22:02:23 +00004 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +00006 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00007#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +02008#include "bb_archive.h"
Denis Vlasenkoab9eef22007-03-07 22:02:23 +00009
Denys Vlasenko66620fa2013-11-14 09:53:52 +010010/* lzop_main() uses bbunpack(), need this: */
11//kbuild:lib-$(CONFIG_LZOP) += bbunzip.o
Denys Vlasenko9cc3d3a2016-12-23 02:42:26 +010012//kbuild:lib-$(CONFIG_LZOPCAT) += bbunzip.o
13//kbuild:lib-$(CONFIG_UNLZOP) += bbunzip.o
Denys Vlasenko548620c2016-12-08 12:24:48 +010014/* bzip2_main() too: */
Kang-Che Sungb130f9f2017-01-08 14:28:34 +080015//kbuild:lib-$(CONFIG_FEATURE_BZIP2_DECOMPRESS) += bbunzip.o
Denys Vlasenko9cc3d3a2016-12-23 02:42:26 +010016/* gzip_main() too: */
Kang-Che Sungb130f9f2017-01-08 14:28:34 +080017//kbuild:lib-$(CONFIG_FEATURE_GZIP_DECOMPRESS) += bbunzip.o
Denys Vlasenko66620fa2013-11-14 09:53:52 +010018
Mike Frysinger93b51812013-03-03 00:48:53 -050019/* Note: must be kept in sync with archival/lzop.c */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000020enum {
Denys Vlasenkoccb88a62010-05-27 02:22:54 +020021 OPT_STDOUT = 1 << 0,
22 OPT_FORCE = 1 << 1,
23 /* only some decompressors: */
24 OPT_VERBOSE = 1 << 2,
Mike Frysinger920c1ba2013-02-28 17:21:50 -050025 OPT_QUIET = 1 << 3,
26 OPT_DECOMPRESS = 1 << 4,
27 OPT_TEST = 1 << 5,
Kang-Che Sungd5e7ff02017-01-08 14:31:06 +010028 SEAMLESS_MAGIC = (1 << 31) * ENABLE_ZCAT * SEAMLESS_COMPRESSION,
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000029};
30
31static
32int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
33{
Denis Vlasenko50f7f442007-04-11 23:20:53 +000034 int fd = open3_or_warn(filename, flags, mode);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000035 if (fd < 0) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000036 return 1;
37 }
Denis Vlasenko50f7f442007-04-11 23:20:53 +000038 xmove_fd(fd, to_fd);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000039 return 0;
40}
41
Denys Vlasenko39a04f72010-05-31 14:18:57 +020042char* FAST_FUNC append_ext(char *filename, const char *expected_ext)
43{
44 return xasprintf("%s.%s", filename, expected_ext);
45}
46
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000047int FAST_FUNC bbunpack(char **argv,
Denys Vlasenkoe7800f32014-12-07 00:42:49 +010048 IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(transformer_state_t *xstate),
Denys Vlasenko39a04f72010-05-31 14:18:57 +020049 char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext),
50 const char *expected_ext
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000051)
52{
53 struct stat stat_buf;
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010054 IF_DESKTOP(long long) int status = 0;
Denis Vlasenko6c939e02007-03-07 23:22:47 +000055 char *filename, *new_name;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000056 smallint exitcode = 0;
Denys Vlasenkoe7800f32014-12-07 00:42:49 +010057 transformer_state_t xstate;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000058
59 do {
Denis Vlasenko6c939e02007-03-07 23:22:47 +000060 /* NB: new_name is *maybe* malloc'ed! */
61 new_name = NULL;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000062 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
Denis Vlasenko6c939e02007-03-07 23:22:47 +000063
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000064 if (filename && LONE_DASH(filename))
65 filename = NULL;
66
67 /* Open src */
68 if (filename) {
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010069 if (!(option_mask32 & SEAMLESS_MAGIC)) {
70 if (stat(filename, &stat_buf) != 0) {
71 err_name:
72 bb_simple_perror_msg(filename);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000073 err:
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010074 exitcode = 1;
75 goto free_name;
76 }
77 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
78 goto err;
79 } else {
Denys Vlasenko41655432013-02-28 18:37:04 +010080 /* "clever zcat" with FILE */
Denys Vlasenko640ce3d2014-02-02 02:06:38 +010081 /* fail_if_not_compressed because zcat refuses uncompressed input */
82 int fd = open_zipped(filename, /*fail_if_not_compressed:*/ 1);
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010083 if (fd < 0)
84 goto err_name;
85 xmove_fd(fd, STDIN_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000086 }
Denys Vlasenko41655432013-02-28 18:37:04 +010087 } else
88 if (option_mask32 & SEAMLESS_MAGIC) {
89 /* "clever zcat" on stdin */
Denys Vlasenko640ce3d2014-02-02 02:06:38 +010090 if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_compressed*/ 1))
Denys Vlasenko41655432013-02-28 18:37:04 +010091 goto err;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000092 }
93
94 /* Special cases: test, stdout */
95 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
96 if (option_mask32 & OPT_TEST)
97 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
Denys Vlasenkof2d84782013-02-28 18:04:22 +010098 xfunc_die();
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000099 filename = NULL;
100 }
101
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000102 /* Open dst if we are going to unpack to file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000103 if (filename) {
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200104 new_name = make_new_name(filename, expected_ext);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000105 if (!new_name) {
106 bb_error_msg("%s: unknown suffix - ignored", filename);
107 goto err;
108 }
Denis Vlasenkoa04cc472008-06-25 20:54:45 +0000109
110 /* -f: overwrite existing output files */
111 if (option_mask32 & OPT_FORCE) {
112 unlink(new_name);
113 }
114
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000115 /* O_EXCL: "real" bunzip2 doesn't overwrite files */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000116 /* GNU gunzip does not bail out, but goes to next file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000117 if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
118 stat_buf.st_mode))
119 goto err;
120 }
121
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000122 /* Check that the input is sane */
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100123 if (!(option_mask32 & OPT_FORCE) && isatty(STDIN_FILENO)) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000124 bb_error_msg_and_die("compressed data not read from terminal, "
125 "use -f to force it");
126 }
127
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100128 if (!(option_mask32 & SEAMLESS_MAGIC)) {
Denys Vlasenkoe7800f32014-12-07 00:42:49 +0100129 init_transformer_state(&xstate);
Denys Vlasenko2a0867a2017-01-09 10:58:37 +0100130 /*xstate.signature_skipped = 0; - already is */
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100131 /*xstate.src_fd = STDIN_FILENO; - already is */
132 xstate.dst_fd = STDOUT_FILENO;
Denys Vlasenkoe7800f32014-12-07 00:42:49 +0100133 status = unpacker(&xstate);
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100134 if (status < 0)
135 exitcode = 1;
136 } else {
Denys Vlasenko41655432013-02-28 18:37:04 +0100137 if (bb_copyfd_eof(STDIN_FILENO, STDOUT_FILENO) < 0)
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100138 /* Disk full, tty closed, etc. No point in continuing */
139 xfunc_die();
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100140 }
Denys Vlasenkoc531b9a2011-10-31 01:05:16 +0100141
142 if (!(option_mask32 & OPT_STDOUT))
143 xclose(STDOUT_FILENO); /* with error check! */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000144
145 if (filename) {
146 char *del = new_name;
Denys Vlasenkod0bc7082013-07-30 05:41:11 +0200147
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000148 if (status >= 0) {
Denys Vlasenkod0bc7082013-07-30 05:41:11 +0200149 unsigned new_name_len;
150
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000151 /* TODO: restore other things? */
Denys Vlasenkoe7800f32014-12-07 00:42:49 +0100152 if (xstate.mtime != 0) {
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100153 struct timeval times[2];
Denys Vlasenko389cca42009-11-15 02:28:56 +0100154
Denys Vlasenkoe7800f32014-12-07 00:42:49 +0100155 times[1].tv_sec = times[0].tv_sec = xstate.mtime;
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100156 times[1].tv_usec = times[0].tv_usec = 0;
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200157 /* Note: we closed it first.
Denys Vlasenko389cca42009-11-15 02:28:56 +0100158 * On some systems calling utimes
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200159 * then closing resets the mtime
160 * back to current time. */
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100161 utimes(new_name, times); /* ignoring errors */
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000162 }
163
Denys Vlasenkod0bc7082013-07-30 05:41:11 +0200164 if (ENABLE_DESKTOP)
165 new_name_len = strlen(new_name);
166 /* Restore source filename (unless tgz -> tar case) */
167 if (new_name == filename) {
168 new_name_len = strlen(filename);
169 filename[new_name_len] = '.';
170 }
171 /* Extreme bloat for gunzip compat */
172 /* Some users do want this info... */
173 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE)) {
174 unsigned percent = status
175 ? ((uoff_t)stat_buf.st_size * 100u / (unsigned long long)status)
176 : 0;
177 fprintf(stderr, "%s: %u%% - replaced with %.*s\n",
178 filename,
179 100u - percent,
180 new_name_len, new_name
181 );
182 }
183 /* Delete _source_ file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000184 del = filename;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000185 }
Denis Vlasenko1bb552b2007-04-05 21:25:15 +0000186 xunlink(del);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000187 free_name:
188 if (new_name != filename)
189 free(new_name);
190 }
191 } while (*argv && *++argv);
192
Denys Vlasenkoc531b9a2011-10-31 01:05:16 +0100193 if (option_mask32 & OPT_STDOUT)
194 xclose(STDOUT_FILENO); /* with error check! */
195
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000196 return exitcode;
197}
198
Denys Vlasenko548620c2016-12-08 12:24:48 +0100199#if ENABLE_UNCOMPRESS \
Kang-Che Sungb130f9f2017-01-08 14:28:34 +0800200 || ENABLE_FEATURE_BZIP2_DECOMPRESS \
Denys Vlasenko548620c2016-12-08 12:24:48 +0100201 || ENABLE_UNLZMA || ENABLE_LZCAT || ENABLE_LZMA \
202 || ENABLE_UNXZ || ENABLE_XZCAT || ENABLE_XZ
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000203static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200204char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000205{
206 char *extension = strrchr(filename, '.');
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000207 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000208 /* Mimic GNU gunzip - "real" bunzip2 tries to */
209 /* unpack file anyway, to file.out */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000210 return NULL;
211 }
212 *extension = '\0';
213 return filename;
214}
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000215#endif
216
217
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000218/*
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200219 * Uncompress applet for busybox (c) 2002 Glenn McGrath
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000220 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200221 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000222 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200223//usage:#define uncompress_trivial_usage
224//usage: "[-cf] [FILE]..."
225//usage:#define uncompress_full_usage "\n\n"
226//usage: "Decompress .Z file[s]\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200227//usage: "\n -c Write to stdout"
228//usage: "\n -f Overwrite"
229
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100230//config:config UNCOMPRESS
231//config: bool "uncompress"
Denys Vlasenkodb700332015-10-25 20:36:03 +0100232//config: default n # ancient
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100233//config: help
234//config: uncompress is used to decompress archives created by compress.
235//config: Not much used anymore, replaced by gzip/gunzip.
236
Denys Vlasenko36184a42013-11-14 09:54:24 +0100237//applet:IF_UNCOMPRESS(APPLET(uncompress, BB_DIR_BIN, BB_SUID_DROP))
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100238//kbuild:lib-$(CONFIG_UNCOMPRESS) += bbunzip.o
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200239#if ENABLE_UNCOMPRESS
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200240int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
241int uncompress_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000242{
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200243 getopt32(argv, "cf");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000244 argv += optind;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000245
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100246 return bbunpack(argv, unpack_Z_stream, make_new_name_generic, "Z");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000247}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000248#endif
249
250
251/*
252 * Gzip implementation for busybox
253 *
254 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
255 *
256 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
257 * based on gzip sources
258 *
259 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
260 * well as stdin/stdout, and to generally behave itself wrt command line
261 * handling.
262 *
263 * General cleanup to better adhere to the style guide and make use of standard
Denis Vlasenko0beaff82007-09-21 13:16:32 +0000264 * busybox functions by Glenn McGrath
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000265 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200266 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000267 *
268 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
269 * Copyright (C) 1992-1993 Jean-loup Gailly
270 * The unzip code was written and put in the public domain by Mark Adler.
271 * Portions of the lzw code are derived from the public domain 'compress'
272 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
273 * Ken Turkowski, Dave Mack and Peter Jannesen.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000274 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200275//usage:#define gunzip_trivial_usage
276//usage: "[-cft] [FILE]..."
277//usage:#define gunzip_full_usage "\n\n"
278//usage: "Decompress FILEs (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200279//usage: "\n -c Write to stdout"
280//usage: "\n -f Force"
281//usage: "\n -t Test file integrity"
282//usage:
283//usage:#define gunzip_example_usage
284//usage: "$ ls -la /tmp/BusyBox*\n"
285//usage: "-rw-rw-r-- 1 andersen andersen 557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n"
286//usage: "$ gunzip /tmp/BusyBox-0.43.tar.gz\n"
287//usage: "$ ls -la /tmp/BusyBox*\n"
288//usage: "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
289//usage:
290//usage:#define zcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100291//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200292//usage:#define zcat_full_usage "\n\n"
293//usage: "Decompress to stdout"
294
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100295//config:config GUNZIP
296//config: bool "gunzip"
297//config: default y
Kang-Che Sungb130f9f2017-01-08 14:28:34 +0800298//config: select FEATURE_GZIP_DECOMPRESS
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100299//config: help
300//config: gunzip is used to decompress archives created by gzip.
301//config: You can use the `-t' option to test the integrity of
302//config: an archive, without decompressing it.
Aaro Koskinenfbe50cf2015-10-26 23:42:32 +0200303//config:
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100304//config:config ZCAT
305//config: bool "zcat"
306//config: default y
Kang-Che Sungb130f9f2017-01-08 14:28:34 +0800307//config: select FEATURE_GZIP_DECOMPRESS
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100308//config: help
309//config: Alias to "gunzip -c".
310//config:
Aaro Koskinenfbe50cf2015-10-26 23:42:32 +0200311//config:config FEATURE_GUNZIP_LONG_OPTIONS
312//config: bool "Enable long options"
313//config: default y
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100314//config: depends on (GUNZIP || ZCAT) && LONG_OPTS
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100315
Denys Vlasenko36184a42013-11-14 09:54:24 +0100316//applet:IF_GUNZIP(APPLET(gunzip, BB_DIR_BIN, BB_SUID_DROP))
Denys Vlasenko205d48e2017-01-29 14:57:33 +0100317// APPLET_ODDNAME:name main location suid_type help
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100318//applet:IF_ZCAT(APPLET_ODDNAME(zcat, gunzip, BB_DIR_BIN, BB_SUID_DROP, zcat))
Kang-Che Sungb130f9f2017-01-08 14:28:34 +0800319#if ENABLE_FEATURE_GZIP_DECOMPRESS
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000320static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200321char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000322{
323 char *extension = strrchr(filename, '.');
324
325 if (!extension)
326 return NULL;
327
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000328 extension++;
329 if (strcmp(extension, "tgz" + 1) == 0
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000330#if ENABLE_FEATURE_SEAMLESS_Z
331 || (extension[0] == 'Z' && extension[1] == '\0')
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000332#endif
333 ) {
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000334 extension[-1] = '\0';
Denis Vlasenko51742f42007-04-12 00:32:05 +0000335 } else if (strcmp(extension, "tgz") == 0) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000336 filename = xstrdup(filename);
337 extension = strrchr(filename, '.');
338 extension[2] = 'a';
339 extension[3] = 'r';
340 } else {
341 return NULL;
342 }
343 return filename;
344}
Aaro Koskinenfbe50cf2015-10-26 23:42:32 +0200345
346#if ENABLE_FEATURE_GUNZIP_LONG_OPTIONS
347static const char gunzip_longopts[] ALIGN1 =
348 "stdout\0" No_argument "c"
349 "to-stdout\0" No_argument "c"
350 "force\0" No_argument "f"
351 "test\0" No_argument "t"
Aaro Koskinencddc98e2015-10-26 23:42:33 +0200352 "no-name\0" No_argument "n"
Aaro Koskinenfbe50cf2015-10-26 23:42:32 +0200353 ;
354#endif
355
Denis Vlasenko52978092008-03-30 13:11:47 +0000356/*
357 * Linux kernel build uses gzip -d -n. We accept and ignore it.
358 * Man page says:
359 * -n --no-name
360 * gzip: do not save the original file name and time stamp.
361 * (The original name is always saved if the name had to be truncated.)
362 * gunzip: do not restore the original file name/time even if present
363 * (remove only the gzip suffix from the compressed file name).
364 * This option is the default when decompressing.
365 * -N --name
366 * gzip: always save the original file name and time stamp (this is the default)
367 * gunzip: restore the original file name and time stamp if present.
368 */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000369int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000370int gunzip_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000371{
Aaro Koskinenfbe50cf2015-10-26 23:42:32 +0200372#if ENABLE_FEATURE_GUNZIP_LONG_OPTIONS
373 applet_long_options = gunzip_longopts;
374#endif
Mike Frysinger08e28e82013-02-28 21:28:21 -0500375 getopt32(argv, "cfvqdtn");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000376 argv += optind;
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100377
378 /* If called as zcat...
379 * Normally, "zcat" is just "gunzip -c".
380 * But if seamless magic is enabled, then we are much more clever.
381 */
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100382 if (ENABLE_ZCAT && applet_name[1] == 'c')
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100383 option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000384
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100385 return bbunpack(argv, unpack_gz_stream, make_new_name_gunzip, /*unused:*/ NULL);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000386}
Kang-Che Sungb130f9f2017-01-08 14:28:34 +0800387#endif /* FEATURE_GZIP_DECOMPRESS */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000388
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200389
390/*
391 * Modified for busybox by Glenn McGrath
392 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
393 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200394 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200395 */
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200396//usage:#define bunzip2_trivial_usage
Denys Vlasenko3b2acb72010-10-09 21:10:32 +0200397//usage: "[-cf] [FILE]..."
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200398//usage:#define bunzip2_full_usage "\n\n"
399//usage: "Decompress FILEs (or stdin)\n"
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200400//usage: "\n -c Write to stdout"
401//usage: "\n -f Force"
402//usage:#define bzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100403//usage: "[FILE]..."
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200404//usage:#define bzcat_full_usage "\n\n"
405//usage: "Decompress to stdout"
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100406
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100407//config:config BUNZIP2
408//config: bool "bunzip2"
409//config: default y
Kang-Che Sungb130f9f2017-01-08 14:28:34 +0800410//config: select FEATURE_BZIP2_DECOMPRESS
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100411//config: help
412//config: bunzip2 is a compression utility using the Burrows-Wheeler block
413//config: sorting text compression algorithm, and Huffman coding. Compression
414//config: is generally considerably better than that achieved by more
415//config: conventional LZ77/LZ78-based compressors, and approaches the
416//config: performance of the PPM family of statistical compressors.
417//config:
418//config: Unless you have a specific application which requires bunzip2, you
419//config: should probably say N here.
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100420//config:
421//config:config BZCAT
422//config: bool "bzcat"
423//config: default y
Kang-Che Sungb130f9f2017-01-08 14:28:34 +0800424//config: select FEATURE_BZIP2_DECOMPRESS
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100425//config: help
426//config: Alias to "bunzip2 -c".
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100427
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +0100428//applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
Denys Vlasenko205d48e2017-01-29 14:57:33 +0100429// APPLET_ODDNAME:name main location suid_type help
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100430//applet:IF_BZCAT(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
Kang-Che Sungb130f9f2017-01-08 14:28:34 +0800431#if ENABLE_FEATURE_BZIP2_DECOMPRESS
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200432int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
433int bunzip2_main(int argc UNUSED_PARAM, char **argv)
434{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500435 getopt32(argv, "cfvqdt");
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200436 argv += optind;
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100437 if (ENABLE_BZCAT && applet_name[2] == 'c') /* bzcat */
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200438 option_mask32 |= OPT_STDOUT;
439
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100440 return bbunpack(argv, unpack_bz2_stream, make_new_name_generic, "bz2");
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200441}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000442#endif
443
444
445/*
446 * Small lzma deflate implementation.
447 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
448 *
449 * Based on bunzip.c from busybox
450 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200451 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000452 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200453//usage:#define unlzma_trivial_usage
454//usage: "[-cf] [FILE]..."
455//usage:#define unlzma_full_usage "\n\n"
456//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200457//usage: "\n -c Write to stdout"
458//usage: "\n -f Force"
459//usage:
460//usage:#define lzma_trivial_usage
461//usage: "-d [-cf] [FILE]..."
462//usage:#define lzma_full_usage "\n\n"
463//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200464//usage: "\n -d Decompress"
465//usage: "\n -c Write to stdout"
466//usage: "\n -f Force"
467//usage:
468//usage:#define lzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100469//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200470//usage:#define lzcat_full_usage "\n\n"
471//usage: "Decompress to stdout"
Pere Orga1f4447b2011-03-27 22:40:30 +0200472
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100473//config:config UNLZMA
474//config: bool "unlzma"
475//config: default y
476//config: help
477//config: unlzma is a compression utility using the Lempel-Ziv-Markov chain
478//config: compression algorithm, and range coding. Compression
479//config: is generally considerably better than that achieved by the bzip2
480//config: compressors.
481//config:
482//config: The BusyBox unlzma applet is limited to decompression only.
483//config: On an x86 system, this applet adds about 4K.
484//config:
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100485//config:config LZCAT
486//config: bool "lzcat"
487//config: default y
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100488//config: help
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100489//config: unlzma is a compression utility using the Lempel-Ziv-Markov chain
490//config: compression algorithm, and range coding. Compression
491//config: is generally considerably better than that achieved by the bzip2
492//config: compressors.
493//config:
494//config: The BusyBox unlzma applet is limited to decompression only.
495//config: On an x86 system, this applet adds about 4K.
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100496//config:
497//config:config LZMA
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100498//config: bool "lzma -d"
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100499//config: default y
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100500//config: help
501//config: Enable this option if you want commands like "lzma -d" to work.
502//config: IOW: you'll get lzma applet, but it will always require -d option.
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100503//config:
504//config:config FEATURE_LZMA_FAST
Denys Vlasenkof5604222017-01-10 14:58:54 +0100505//config: bool "Optimize for speed"
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100506//config: default n
507//config: depends on UNLZMA || LZCAT || LZMA
508//config: help
509//config: This option reduces decompression time by about 25% at the cost of
510//config: a 1K bigger binary.
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100511
Denys Vlasenko36184a42013-11-14 09:54:24 +0100512//applet:IF_UNLZMA(APPLET(unlzma, BB_DIR_USR_BIN, BB_SUID_DROP))
Denys Vlasenko205d48e2017-01-29 14:57:33 +0100513// APPLET_ODDNAME:name main location suid_type help
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100514//applet:IF_LZCAT(APPLET_ODDNAME(lzcat, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzcat))
Denys Vlasenko205d48e2017-01-29 14:57:33 +0100515//applet:IF_LZMA( APPLET_ODDNAME(lzma, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzma))
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100516//kbuild:lib-$(CONFIG_UNLZMA) += bbunzip.o
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100517//kbuild:lib-$(CONFIG_LZCAT) += bbunzip.o
518//kbuild:lib-$(CONFIG_LZMA) += bbunzip.o
519#if ENABLE_UNLZMA || ENABLE_LZCAT || ENABLE_LZMA
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000520int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000521int unlzma_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000522{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500523 IF_LZMA(int opts =) getopt32(argv, "cfvqdt");
Denys Vlasenkoe04c8672010-05-30 03:33:50 +0200524# if ENABLE_LZMA
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200525 /* lzma without -d or -t? */
526 if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
527 bb_show_usage();
528# endif
529 /* lzcat? */
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100530 if (ENABLE_LZCAT && applet_name[2] == 'c')
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000531 option_mask32 |= OPT_STDOUT;
532
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200533 argv += optind;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100534 return bbunpack(argv, unpack_lzma_stream, make_new_name_generic, "lzma");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000535}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000536#endif
537
538
Denys Vlasenko205d48e2017-01-29 14:57:33 +0100539//usage:#define unxz_trivial_usage
540//usage: "[-cf] [FILE]..."
541//usage:#define unxz_full_usage "\n\n"
542//usage: "Decompress FILE (or stdin)\n"
543//usage: "\n -c Write to stdout"
544//usage: "\n -f Force"
545//usage:
546//usage:#define xz_trivial_usage
547//usage: "-d [-cf] [FILE]..."
548//usage:#define xz_full_usage "\n\n"
549//usage: "Decompress FILE (or stdin)\n"
550//usage: "\n -d Decompress"
551//usage: "\n -c Write to stdout"
552//usage: "\n -f Force"
553//usage:
554//usage:#define xzcat_trivial_usage
555//usage: "[FILE]..."
556//usage:#define xzcat_full_usage "\n\n"
557//usage: "Decompress to stdout"
558
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100559//config:config UNXZ
560//config: bool "unxz"
561//config: default y
562//config: help
563//config: unxz is a unlzma successor.
564//config:
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100565//config:config XZCAT
566//config: bool "xzcat"
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100567//config: default y
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100568//config: help
569//config: Alias to "unxz -c".
570//config:
571//config:config XZ
572//config: bool "xz -d"
573//config: default y
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100574//config: help
575//config: Enable this option if you want commands like "xz -d" to work.
576//config: IOW: you'll get xz applet, but it will always require -d option.
577
Denys Vlasenko36184a42013-11-14 09:54:24 +0100578//applet:IF_UNXZ(APPLET(unxz, BB_DIR_USR_BIN, BB_SUID_DROP))
Denys Vlasenko205d48e2017-01-29 14:57:33 +0100579// APPLET_ODDNAME:name main location suid_type help
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100580//applet:IF_XZCAT(APPLET_ODDNAME(xzcat, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xzcat))
Denys Vlasenko205d48e2017-01-29 14:57:33 +0100581//applet:IF_XZ( APPLET_ODDNAME(xz, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xz))
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100582//kbuild:lib-$(CONFIG_UNXZ) += bbunzip.o
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100583//kbuild:lib-$(CONFIG_XZCAT) += bbunzip.o
584//kbuild:lib-$(CONFIG_XZ) += bbunzip.o
585#if ENABLE_UNXZ || ENABLE_XZCAT || ENABLE_XZ
Denys Vlasenko602ce692010-05-30 03:35:18 +0200586int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
587int unxz_main(int argc UNUSED_PARAM, char **argv)
588{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500589 IF_XZ(int opts =) getopt32(argv, "cfvqdt");
Denys Vlasenko602ce692010-05-30 03:35:18 +0200590# if ENABLE_XZ
591 /* xz without -d or -t? */
592 if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
593 bb_show_usage();
594# endif
595 /* xzcat? */
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +0100596 if (ENABLE_XZCAT && applet_name[2] == 'c')
Denys Vlasenko602ce692010-05-30 03:35:18 +0200597 option_mask32 |= OPT_STDOUT;
598
599 argv += optind;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100600 return bbunpack(argv, unpack_xz_stream, make_new_name_generic, "xz");
Denys Vlasenko602ce692010-05-30 03:35:18 +0200601}
Denys Vlasenko602ce692010-05-30 03:35:18 +0200602#endif