blob: b4f754e0b2ad30ec4f525ef5ba52c98486654673 [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
12
Mike Frysinger93b51812013-03-03 00:48:53 -050013/* Note: must be kept in sync with archival/lzop.c */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000014enum {
Denys Vlasenkoccb88a62010-05-27 02:22:54 +020015 OPT_STDOUT = 1 << 0,
16 OPT_FORCE = 1 << 1,
17 /* only some decompressors: */
18 OPT_VERBOSE = 1 << 2,
Mike Frysinger920c1ba2013-02-28 17:21:50 -050019 OPT_QUIET = 1 << 3,
20 OPT_DECOMPRESS = 1 << 4,
21 OPT_TEST = 1 << 5,
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010022 SEAMLESS_MAGIC = (1 << 31) * SEAMLESS_COMPRESSION,
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000023};
24
25static
26int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
27{
Denis Vlasenko50f7f442007-04-11 23:20:53 +000028 int fd = open3_or_warn(filename, flags, mode);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000029 if (fd < 0) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000030 return 1;
31 }
Denis Vlasenko50f7f442007-04-11 23:20:53 +000032 xmove_fd(fd, to_fd);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000033 return 0;
34}
35
Denys Vlasenko39a04f72010-05-31 14:18:57 +020036char* FAST_FUNC append_ext(char *filename, const char *expected_ext)
37{
38 return xasprintf("%s.%s", filename, expected_ext);
39}
40
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000041int FAST_FUNC bbunpack(char **argv,
Denys Vlasenkoe7800f32014-12-07 00:42:49 +010042 IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(transformer_state_t *xstate),
Denys Vlasenko39a04f72010-05-31 14:18:57 +020043 char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext),
44 const char *expected_ext
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000045)
46{
47 struct stat stat_buf;
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010048 IF_DESKTOP(long long) int status = 0;
Denis Vlasenko6c939e02007-03-07 23:22:47 +000049 char *filename, *new_name;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000050 smallint exitcode = 0;
Denys Vlasenkoe7800f32014-12-07 00:42:49 +010051 transformer_state_t xstate;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000052
53 do {
Denis Vlasenko6c939e02007-03-07 23:22:47 +000054 /* NB: new_name is *maybe* malloc'ed! */
55 new_name = NULL;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000056 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
Denis Vlasenko6c939e02007-03-07 23:22:47 +000057
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000058 if (filename && LONE_DASH(filename))
59 filename = NULL;
60
61 /* Open src */
62 if (filename) {
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010063 if (!(option_mask32 & SEAMLESS_MAGIC)) {
64 if (stat(filename, &stat_buf) != 0) {
65 err_name:
66 bb_simple_perror_msg(filename);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000067 err:
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010068 exitcode = 1;
69 goto free_name;
70 }
71 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
72 goto err;
73 } else {
Denys Vlasenko41655432013-02-28 18:37:04 +010074 /* "clever zcat" with FILE */
Denys Vlasenko640ce3d2014-02-02 02:06:38 +010075 /* fail_if_not_compressed because zcat refuses uncompressed input */
76 int fd = open_zipped(filename, /*fail_if_not_compressed:*/ 1);
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010077 if (fd < 0)
78 goto err_name;
79 xmove_fd(fd, STDIN_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000080 }
Denys Vlasenko41655432013-02-28 18:37:04 +010081 } else
82 if (option_mask32 & SEAMLESS_MAGIC) {
83 /* "clever zcat" on stdin */
Denys Vlasenko640ce3d2014-02-02 02:06:38 +010084 if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_compressed*/ 1))
Denys Vlasenko41655432013-02-28 18:37:04 +010085 goto err;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000086 }
87
88 /* Special cases: test, stdout */
89 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
90 if (option_mask32 & OPT_TEST)
91 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
Denys Vlasenkof2d84782013-02-28 18:04:22 +010092 xfunc_die();
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000093 filename = NULL;
94 }
95
Denis Vlasenko6c939e02007-03-07 23:22:47 +000096 /* Open dst if we are going to unpack to file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000097 if (filename) {
Denys Vlasenko39a04f72010-05-31 14:18:57 +020098 new_name = make_new_name(filename, expected_ext);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000099 if (!new_name) {
100 bb_error_msg("%s: unknown suffix - ignored", filename);
101 goto err;
102 }
Denis Vlasenkoa04cc472008-06-25 20:54:45 +0000103
104 /* -f: overwrite existing output files */
105 if (option_mask32 & OPT_FORCE) {
106 unlink(new_name);
107 }
108
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000109 /* O_EXCL: "real" bunzip2 doesn't overwrite files */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000110 /* GNU gunzip does not bail out, but goes to next file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000111 if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
112 stat_buf.st_mode))
113 goto err;
114 }
115
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000116 /* Check that the input is sane */
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100117 if (!(option_mask32 & OPT_FORCE) && isatty(STDIN_FILENO)) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000118 bb_error_msg_and_die("compressed data not read from terminal, "
119 "use -f to force it");
120 }
121
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100122 if (!(option_mask32 & SEAMLESS_MAGIC)) {
Denys Vlasenkoe7800f32014-12-07 00:42:49 +0100123 init_transformer_state(&xstate);
124 xstate.check_signature = 1;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100125 /*xstate.src_fd = STDIN_FILENO; - already is */
126 xstate.dst_fd = STDOUT_FILENO;
Denys Vlasenkoe7800f32014-12-07 00:42:49 +0100127 status = unpacker(&xstate);
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100128 if (status < 0)
129 exitcode = 1;
130 } else {
Denys Vlasenko41655432013-02-28 18:37:04 +0100131 if (bb_copyfd_eof(STDIN_FILENO, STDOUT_FILENO) < 0)
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100132 /* Disk full, tty closed, etc. No point in continuing */
133 xfunc_die();
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100134 }
Denys Vlasenkoc531b9a2011-10-31 01:05:16 +0100135
136 if (!(option_mask32 & OPT_STDOUT))
137 xclose(STDOUT_FILENO); /* with error check! */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000138
139 if (filename) {
140 char *del = new_name;
Denys Vlasenkod0bc7082013-07-30 05:41:11 +0200141
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000142 if (status >= 0) {
Denys Vlasenkod0bc7082013-07-30 05:41:11 +0200143 unsigned new_name_len;
144
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000145 /* TODO: restore other things? */
Denys Vlasenkoe7800f32014-12-07 00:42:49 +0100146 if (xstate.mtime != 0) {
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100147 struct timeval times[2];
Denys Vlasenko389cca42009-11-15 02:28:56 +0100148
Denys Vlasenkoe7800f32014-12-07 00:42:49 +0100149 times[1].tv_sec = times[0].tv_sec = xstate.mtime;
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100150 times[1].tv_usec = times[0].tv_usec = 0;
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200151 /* Note: we closed it first.
Denys Vlasenko389cca42009-11-15 02:28:56 +0100152 * On some systems calling utimes
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200153 * then closing resets the mtime
154 * back to current time. */
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100155 utimes(new_name, times); /* ignoring errors */
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000156 }
157
Denys Vlasenkod0bc7082013-07-30 05:41:11 +0200158 if (ENABLE_DESKTOP)
159 new_name_len = strlen(new_name);
160 /* Restore source filename (unless tgz -> tar case) */
161 if (new_name == filename) {
162 new_name_len = strlen(filename);
163 filename[new_name_len] = '.';
164 }
165 /* Extreme bloat for gunzip compat */
166 /* Some users do want this info... */
167 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE)) {
168 unsigned percent = status
169 ? ((uoff_t)stat_buf.st_size * 100u / (unsigned long long)status)
170 : 0;
171 fprintf(stderr, "%s: %u%% - replaced with %.*s\n",
172 filename,
173 100u - percent,
174 new_name_len, new_name
175 );
176 }
177 /* Delete _source_ file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000178 del = filename;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000179 }
Denis Vlasenko1bb552b2007-04-05 21:25:15 +0000180 xunlink(del);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000181 free_name:
182 if (new_name != filename)
183 free(new_name);
184 }
185 } while (*argv && *++argv);
186
Denys Vlasenkoc531b9a2011-10-31 01:05:16 +0100187 if (option_mask32 & OPT_STDOUT)
188 xclose(STDOUT_FILENO); /* with error check! */
189
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000190 return exitcode;
191}
192
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200193#if ENABLE_UNCOMPRESS || ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNXZ
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000194static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200195char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000196{
197 char *extension = strrchr(filename, '.');
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000198 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000199 /* Mimic GNU gunzip - "real" bunzip2 tries to */
200 /* unpack file anyway, to file.out */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000201 return NULL;
202 }
203 *extension = '\0';
204 return filename;
205}
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000206#endif
207
208
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000209/*
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200210 * Uncompress applet for busybox (c) 2002 Glenn McGrath
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000211 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200212 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000213 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200214//usage:#define uncompress_trivial_usage
215//usage: "[-cf] [FILE]..."
216//usage:#define uncompress_full_usage "\n\n"
217//usage: "Decompress .Z file[s]\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200218//usage: "\n -c Write to stdout"
219//usage: "\n -f Overwrite"
220
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100221//config:config UNCOMPRESS
222//config: bool "uncompress"
Denys Vlasenkodb700332015-10-25 20:36:03 +0100223//config: default n # ancient
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100224//config: help
225//config: uncompress is used to decompress archives created by compress.
226//config: Not much used anymore, replaced by gzip/gunzip.
227
Denys Vlasenko36184a42013-11-14 09:54:24 +0100228//applet:IF_UNCOMPRESS(APPLET(uncompress, BB_DIR_BIN, BB_SUID_DROP))
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100229//kbuild:lib-$(CONFIG_UNCOMPRESS) += bbunzip.o
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200230#if ENABLE_UNCOMPRESS
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200231int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
232int uncompress_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000233{
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200234 getopt32(argv, "cf");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000235 argv += optind;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000236
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100237 return bbunpack(argv, unpack_Z_stream, make_new_name_generic, "Z");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000238}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000239#endif
240
241
242/*
243 * Gzip implementation for busybox
244 *
245 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
246 *
247 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
248 * based on gzip sources
249 *
250 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
251 * well as stdin/stdout, and to generally behave itself wrt command line
252 * handling.
253 *
254 * General cleanup to better adhere to the style guide and make use of standard
Denis Vlasenko0beaff82007-09-21 13:16:32 +0000255 * busybox functions by Glenn McGrath
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000256 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200257 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000258 *
259 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
260 * Copyright (C) 1992-1993 Jean-loup Gailly
261 * The unzip code was written and put in the public domain by Mark Adler.
262 * Portions of the lzw code are derived from the public domain 'compress'
263 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
264 * Ken Turkowski, Dave Mack and Peter Jannesen.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000265 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200266//usage:#define gunzip_trivial_usage
267//usage: "[-cft] [FILE]..."
268//usage:#define gunzip_full_usage "\n\n"
269//usage: "Decompress FILEs (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200270//usage: "\n -c Write to stdout"
271//usage: "\n -f Force"
272//usage: "\n -t Test file integrity"
273//usage:
274//usage:#define gunzip_example_usage
275//usage: "$ ls -la /tmp/BusyBox*\n"
276//usage: "-rw-rw-r-- 1 andersen andersen 557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n"
277//usage: "$ gunzip /tmp/BusyBox-0.43.tar.gz\n"
278//usage: "$ ls -la /tmp/BusyBox*\n"
279//usage: "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
280//usage:
281//usage:#define zcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100282//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200283//usage:#define zcat_full_usage "\n\n"
284//usage: "Decompress to stdout"
285
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100286//config:config GUNZIP
287//config: bool "gunzip"
288//config: default y
289//config: help
290//config: gunzip is used to decompress archives created by gzip.
291//config: You can use the `-t' option to test the integrity of
292//config: an archive, without decompressing it.
Aaro Koskinenfbe50cf2015-10-26 23:42:32 +0200293//config:
294//config:config FEATURE_GUNZIP_LONG_OPTIONS
295//config: bool "Enable long options"
296//config: default y
297//config: depends on GUNZIP && LONG_OPTS
298//config: help
299//config: Enable use of long options.
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100300
Denys Vlasenko36184a42013-11-14 09:54:24 +0100301//applet:IF_GUNZIP(APPLET(gunzip, BB_DIR_BIN, BB_SUID_DROP))
302//applet:IF_GUNZIP(APPLET_ODDNAME(zcat, gunzip, BB_DIR_BIN, BB_SUID_DROP, zcat))
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100303//kbuild:lib-$(CONFIG_GZIP) += bbunzip.o
304//kbuild:lib-$(CONFIG_GUNZIP) += bbunzip.o
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000305#if ENABLE_GUNZIP
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000306static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200307char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000308{
309 char *extension = strrchr(filename, '.');
310
311 if (!extension)
312 return NULL;
313
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000314 extension++;
315 if (strcmp(extension, "tgz" + 1) == 0
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000316#if ENABLE_FEATURE_SEAMLESS_Z
317 || (extension[0] == 'Z' && extension[1] == '\0')
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000318#endif
319 ) {
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000320 extension[-1] = '\0';
Denis Vlasenko51742f42007-04-12 00:32:05 +0000321 } else if (strcmp(extension, "tgz") == 0) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000322 filename = xstrdup(filename);
323 extension = strrchr(filename, '.');
324 extension[2] = 'a';
325 extension[3] = 'r';
326 } else {
327 return NULL;
328 }
329 return filename;
330}
Aaro Koskinenfbe50cf2015-10-26 23:42:32 +0200331
332#if ENABLE_FEATURE_GUNZIP_LONG_OPTIONS
333static const char gunzip_longopts[] ALIGN1 =
334 "stdout\0" No_argument "c"
335 "to-stdout\0" No_argument "c"
336 "force\0" No_argument "f"
337 "test\0" No_argument "t"
Aaro Koskinencddc98e2015-10-26 23:42:33 +0200338 "no-name\0" No_argument "n"
Aaro Koskinenfbe50cf2015-10-26 23:42:32 +0200339 ;
340#endif
341
Denis Vlasenko52978092008-03-30 13:11:47 +0000342/*
343 * Linux kernel build uses gzip -d -n. We accept and ignore it.
344 * Man page says:
345 * -n --no-name
346 * gzip: do not save the original file name and time stamp.
347 * (The original name is always saved if the name had to be truncated.)
348 * gunzip: do not restore the original file name/time even if present
349 * (remove only the gzip suffix from the compressed file name).
350 * This option is the default when decompressing.
351 * -N --name
352 * gzip: always save the original file name and time stamp (this is the default)
353 * gunzip: restore the original file name and time stamp if present.
354 */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000355int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000356int gunzip_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000357{
Aaro Koskinenfbe50cf2015-10-26 23:42:32 +0200358#if ENABLE_FEATURE_GUNZIP_LONG_OPTIONS
359 applet_long_options = gunzip_longopts;
360#endif
Mike Frysinger08e28e82013-02-28 21:28:21 -0500361 getopt32(argv, "cfvqdtn");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000362 argv += optind;
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100363
364 /* If called as zcat...
365 * Normally, "zcat" is just "gunzip -c".
366 * But if seamless magic is enabled, then we are much more clever.
367 */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000368 if (applet_name[1] == 'c')
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100369 option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000370
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100371 return bbunpack(argv, unpack_gz_stream, make_new_name_gunzip, /*unused:*/ NULL);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000372}
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200373#endif
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000374
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200375
376/*
377 * Modified for busybox by Glenn McGrath
378 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
379 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200380 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200381 */
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200382//usage:#define bunzip2_trivial_usage
Denys Vlasenko3b2acb72010-10-09 21:10:32 +0200383//usage: "[-cf] [FILE]..."
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200384//usage:#define bunzip2_full_usage "\n\n"
385//usage: "Decompress FILEs (or stdin)\n"
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200386//usage: "\n -c Write to stdout"
387//usage: "\n -f Force"
388//usage:#define bzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100389//usage: "[FILE]..."
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200390//usage:#define bzcat_full_usage "\n\n"
391//usage: "Decompress to stdout"
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100392
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100393//config:config BUNZIP2
394//config: bool "bunzip2"
395//config: default y
396//config: help
397//config: bunzip2 is a compression utility using the Burrows-Wheeler block
398//config: sorting text compression algorithm, and Huffman coding. Compression
399//config: is generally considerably better than that achieved by more
400//config: conventional LZ77/LZ78-based compressors, and approaches the
401//config: performance of the PPM family of statistical compressors.
402//config:
403//config: Unless you have a specific application which requires bunzip2, you
404//config: should probably say N here.
405
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +0100406//applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
407//applet:IF_BUNZIP2(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100408//kbuild:lib-$(CONFIG_BZIP2) += bbunzip.o
409//kbuild:lib-$(CONFIG_BUNZIP2) += bbunzip.o
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200410#if ENABLE_BUNZIP2
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200411int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
412int bunzip2_main(int argc UNUSED_PARAM, char **argv)
413{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500414 getopt32(argv, "cfvqdt");
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200415 argv += optind;
416 if (applet_name[2] == 'c') /* bzcat */
417 option_mask32 |= OPT_STDOUT;
418
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100419 return bbunpack(argv, unpack_bz2_stream, make_new_name_generic, "bz2");
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200420}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000421#endif
422
423
424/*
425 * Small lzma deflate implementation.
426 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
427 *
428 * Based on bunzip.c from busybox
429 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200430 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000431 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200432//usage:#define unlzma_trivial_usage
433//usage: "[-cf] [FILE]..."
434//usage:#define unlzma_full_usage "\n\n"
435//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200436//usage: "\n -c Write to stdout"
437//usage: "\n -f Force"
438//usage:
439//usage:#define lzma_trivial_usage
440//usage: "-d [-cf] [FILE]..."
441//usage:#define lzma_full_usage "\n\n"
442//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200443//usage: "\n -d Decompress"
444//usage: "\n -c Write to stdout"
445//usage: "\n -f Force"
446//usage:
447//usage:#define lzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100448//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200449//usage:#define lzcat_full_usage "\n\n"
450//usage: "Decompress to stdout"
451//usage:
452//usage:#define unxz_trivial_usage
453//usage: "[-cf] [FILE]..."
454//usage:#define unxz_full_usage "\n\n"
455//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200456//usage: "\n -c Write to stdout"
457//usage: "\n -f Force"
458//usage:
459//usage:#define xz_trivial_usage
460//usage: "-d [-cf] [FILE]..."
461//usage:#define xz_full_usage "\n\n"
462//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200463//usage: "\n -d Decompress"
464//usage: "\n -c Write to stdout"
465//usage: "\n -f Force"
466//usage:
467//usage:#define xzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100468//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200469//usage:#define xzcat_full_usage "\n\n"
470//usage: "Decompress to stdout"
471
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100472//config:config UNLZMA
473//config: bool "unlzma"
474//config: default y
475//config: help
476//config: unlzma is a compression utility using the Lempel-Ziv-Markov chain
477//config: compression algorithm, and range coding. Compression
478//config: is generally considerably better than that achieved by the bzip2
479//config: compressors.
480//config:
481//config: The BusyBox unlzma applet is limited to decompression only.
482//config: On an x86 system, this applet adds about 4K.
483//config:
484//config:config FEATURE_LZMA_FAST
485//config: bool "Optimize unlzma for speed"
486//config: default n
487//config: depends on UNLZMA
488//config: help
489//config: This option reduces decompression time by about 25% at the cost of
490//config: a 1K bigger binary.
491//config:
492//config:config LZMA
493//config: bool "Provide lzma alias which supports only unpacking"
494//config: default y
495//config: depends on UNLZMA
496//config: help
497//config: Enable this option if you want commands like "lzma -d" to work.
498//config: IOW: you'll get lzma applet, but it will always require -d option.
499
Denys Vlasenko36184a42013-11-14 09:54:24 +0100500//applet:IF_UNLZMA(APPLET(unlzma, BB_DIR_USR_BIN, BB_SUID_DROP))
Denys Vlasenkoac216872013-11-14 11:38:18 +0100501//applet:IF_UNLZMA(APPLET_ODDNAME(lzcat, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzcat))
502//applet:IF_LZMA(APPLET_ODDNAME(lzma, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzma))
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100503//kbuild:lib-$(CONFIG_UNLZMA) += bbunzip.o
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000504#if ENABLE_UNLZMA
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000505int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000506int unlzma_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000507{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500508 IF_LZMA(int opts =) getopt32(argv, "cfvqdt");
Denys Vlasenkoe04c8672010-05-30 03:33:50 +0200509# if ENABLE_LZMA
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200510 /* lzma without -d or -t? */
511 if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
512 bb_show_usage();
513# endif
514 /* lzcat? */
515 if (applet_name[2] == 'c')
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000516 option_mask32 |= OPT_STDOUT;
517
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200518 argv += optind;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100519 return bbunpack(argv, unpack_lzma_stream, make_new_name_generic, "lzma");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000520}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000521#endif
522
523
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100524//config:config UNXZ
525//config: bool "unxz"
526//config: default y
527//config: help
528//config: unxz is a unlzma successor.
529//config:
530//config:config XZ
531//config: bool "Provide xz alias which supports only unpacking"
532//config: default y
533//config: depends on UNXZ
534//config: help
535//config: Enable this option if you want commands like "xz -d" to work.
536//config: IOW: you'll get xz applet, but it will always require -d option.
537
Denys Vlasenko36184a42013-11-14 09:54:24 +0100538//applet:IF_UNXZ(APPLET(unxz, BB_DIR_USR_BIN, BB_SUID_DROP))
Denys Vlasenkoac216872013-11-14 11:38:18 +0100539//applet:IF_UNXZ(APPLET_ODDNAME(xzcat, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xzcat))
540//applet:IF_XZ(APPLET_ODDNAME(xz, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xz))
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100541//kbuild:lib-$(CONFIG_UNXZ) += bbunzip.o
Denys Vlasenko602ce692010-05-30 03:35:18 +0200542#if ENABLE_UNXZ
Denys Vlasenko602ce692010-05-30 03:35:18 +0200543int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
544int unxz_main(int argc UNUSED_PARAM, char **argv)
545{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500546 IF_XZ(int opts =) getopt32(argv, "cfvqdt");
Denys Vlasenko602ce692010-05-30 03:35:18 +0200547# if ENABLE_XZ
548 /* xz without -d or -t? */
549 if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
550 bb_show_usage();
551# endif
552 /* xzcat? */
553 if (applet_name[2] == 'c')
554 option_mask32 |= OPT_STDOUT;
555
556 argv += optind;
Denys Vlasenkob4c11c12014-12-07 00:44:00 +0100557 return bbunpack(argv, unpack_xz_stream, make_new_name_generic, "xz");
Denys Vlasenko602ce692010-05-30 03:35:18 +0200558}
Denys Vlasenko602ce692010-05-30 03:35:18 +0200559#endif