blob: fce5ab9e152f13c5f4026603bbf46bb328e3ffdd [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 Vlasenko8a6a2f92012-03-06 16:27:48 +010042 IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(transformer_aux_data_t *aux),
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 Vlasenko8a6a2f92012-03-06 16:27:48 +010051 transformer_aux_data_t aux;
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)) {
123 init_transformer_aux_data(&aux);
124 aux.check_signature = 1;
125 status = unpacker(&aux);
126 if (status < 0)
127 exitcode = 1;
128 } else {
Denys Vlasenko41655432013-02-28 18:37:04 +0100129 if (bb_copyfd_eof(STDIN_FILENO, STDOUT_FILENO) < 0)
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100130 /* Disk full, tty closed, etc. No point in continuing */
131 xfunc_die();
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100132 }
Denys Vlasenkoc531b9a2011-10-31 01:05:16 +0100133
134 if (!(option_mask32 & OPT_STDOUT))
135 xclose(STDOUT_FILENO); /* with error check! */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000136
137 if (filename) {
138 char *del = new_name;
Denys Vlasenkod0bc7082013-07-30 05:41:11 +0200139
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000140 if (status >= 0) {
Denys Vlasenkod0bc7082013-07-30 05:41:11 +0200141 unsigned new_name_len;
142
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000143 /* TODO: restore other things? */
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100144 if (aux.mtime != 0) {
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100145 struct timeval times[2];
Denys Vlasenko389cca42009-11-15 02:28:56 +0100146
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100147 times[1].tv_sec = times[0].tv_sec = aux.mtime;
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100148 times[1].tv_usec = times[0].tv_usec = 0;
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200149 /* Note: we closed it first.
Denys Vlasenko389cca42009-11-15 02:28:56 +0100150 * On some systems calling utimes
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200151 * then closing resets the mtime
152 * back to current time. */
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100153 utimes(new_name, times); /* ignoring errors */
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000154 }
155
Denys Vlasenkod0bc7082013-07-30 05:41:11 +0200156 if (ENABLE_DESKTOP)
157 new_name_len = strlen(new_name);
158 /* Restore source filename (unless tgz -> tar case) */
159 if (new_name == filename) {
160 new_name_len = strlen(filename);
161 filename[new_name_len] = '.';
162 }
163 /* Extreme bloat for gunzip compat */
164 /* Some users do want this info... */
165 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE)) {
166 unsigned percent = status
167 ? ((uoff_t)stat_buf.st_size * 100u / (unsigned long long)status)
168 : 0;
169 fprintf(stderr, "%s: %u%% - replaced with %.*s\n",
170 filename,
171 100u - percent,
172 new_name_len, new_name
173 );
174 }
175 /* Delete _source_ file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000176 del = filename;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000177 }
Denis Vlasenko1bb552b2007-04-05 21:25:15 +0000178 xunlink(del);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000179 free_name:
180 if (new_name != filename)
181 free(new_name);
182 }
183 } while (*argv && *++argv);
184
Denys Vlasenkoc531b9a2011-10-31 01:05:16 +0100185 if (option_mask32 & OPT_STDOUT)
186 xclose(STDOUT_FILENO); /* with error check! */
187
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000188 return exitcode;
189}
190
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200191#if ENABLE_UNCOMPRESS || ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNXZ
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000192static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200193char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000194{
195 char *extension = strrchr(filename, '.');
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000196 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000197 /* Mimic GNU gunzip - "real" bunzip2 tries to */
198 /* unpack file anyway, to file.out */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000199 return NULL;
200 }
201 *extension = '\0';
202 return filename;
203}
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000204#endif
205
206
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000207/*
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200208 * Uncompress applet for busybox (c) 2002 Glenn McGrath
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000209 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200210 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000211 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200212//usage:#define uncompress_trivial_usage
213//usage: "[-cf] [FILE]..."
214//usage:#define uncompress_full_usage "\n\n"
215//usage: "Decompress .Z file[s]\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200216//usage: "\n -c Write to stdout"
217//usage: "\n -f Overwrite"
218
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100219//config:config UNCOMPRESS
220//config: bool "uncompress"
221//config: default n
222//config: help
223//config: uncompress is used to decompress archives created by compress.
224//config: Not much used anymore, replaced by gzip/gunzip.
225
Denys Vlasenko36184a42013-11-14 09:54:24 +0100226//applet:IF_UNCOMPRESS(APPLET(uncompress, BB_DIR_BIN, BB_SUID_DROP))
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100227//kbuild:lib-$(CONFIG_UNCOMPRESS) += bbunzip.o
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200228#if ENABLE_UNCOMPRESS
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000229static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100230IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(transformer_aux_data_t *aux)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000231{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100232 return unpack_Z_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200233}
234int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
235int uncompress_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000236{
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200237 getopt32(argv, "cf");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000238 argv += optind;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000239
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200240 return bbunpack(argv, unpack_uncompress, make_new_name_generic, "Z");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000241}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000242#endif
243
244
245/*
246 * Gzip implementation for busybox
247 *
248 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
249 *
250 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
251 * based on gzip sources
252 *
253 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
254 * well as stdin/stdout, and to generally behave itself wrt command line
255 * handling.
256 *
257 * General cleanup to better adhere to the style guide and make use of standard
Denis Vlasenko0beaff82007-09-21 13:16:32 +0000258 * busybox functions by Glenn McGrath
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000259 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200260 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000261 *
262 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
263 * Copyright (C) 1992-1993 Jean-loup Gailly
264 * The unzip code was written and put in the public domain by Mark Adler.
265 * Portions of the lzw code are derived from the public domain 'compress'
266 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
267 * Ken Turkowski, Dave Mack and Peter Jannesen.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000268 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200269//usage:#define gunzip_trivial_usage
270//usage: "[-cft] [FILE]..."
271//usage:#define gunzip_full_usage "\n\n"
272//usage: "Decompress FILEs (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200273//usage: "\n -c Write to stdout"
274//usage: "\n -f Force"
275//usage: "\n -t Test file integrity"
276//usage:
277//usage:#define gunzip_example_usage
278//usage: "$ ls -la /tmp/BusyBox*\n"
279//usage: "-rw-rw-r-- 1 andersen andersen 557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n"
280//usage: "$ gunzip /tmp/BusyBox-0.43.tar.gz\n"
281//usage: "$ ls -la /tmp/BusyBox*\n"
282//usage: "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
283//usage:
284//usage:#define zcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100285//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200286//usage:#define zcat_full_usage "\n\n"
287//usage: "Decompress to stdout"
288
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100289//config:config GUNZIP
290//config: bool "gunzip"
291//config: default y
292//config: help
293//config: gunzip is used to decompress archives created by gzip.
294//config: You can use the `-t' option to test the integrity of
295//config: an archive, without decompressing it.
296
Denys Vlasenko36184a42013-11-14 09:54:24 +0100297//applet:IF_GUNZIP(APPLET(gunzip, BB_DIR_BIN, BB_SUID_DROP))
298//applet:IF_GUNZIP(APPLET_ODDNAME(zcat, gunzip, BB_DIR_BIN, BB_SUID_DROP, zcat))
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100299//kbuild:lib-$(CONFIG_GZIP) += bbunzip.o
300//kbuild:lib-$(CONFIG_GUNZIP) += bbunzip.o
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000301#if ENABLE_GUNZIP
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000302static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200303char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000304{
305 char *extension = strrchr(filename, '.');
306
307 if (!extension)
308 return NULL;
309
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000310 extension++;
311 if (strcmp(extension, "tgz" + 1) == 0
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000312#if ENABLE_FEATURE_SEAMLESS_Z
313 || (extension[0] == 'Z' && extension[1] == '\0')
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000314#endif
315 ) {
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000316 extension[-1] = '\0';
Denis Vlasenko51742f42007-04-12 00:32:05 +0000317 } else if (strcmp(extension, "tgz") == 0) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000318 filename = xstrdup(filename);
319 extension = strrchr(filename, '.');
320 extension[2] = 'a';
321 extension[3] = 'r';
322 } else {
323 return NULL;
324 }
325 return filename;
326}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000327static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100328IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(transformer_aux_data_t *aux)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000329{
Denys Vlasenko02c3c382012-03-06 16:32:06 +0100330 return unpack_gz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000331}
Denis Vlasenko52978092008-03-30 13:11:47 +0000332/*
333 * Linux kernel build uses gzip -d -n. We accept and ignore it.
334 * Man page says:
335 * -n --no-name
336 * gzip: do not save the original file name and time stamp.
337 * (The original name is always saved if the name had to be truncated.)
338 * gunzip: do not restore the original file name/time even if present
339 * (remove only the gzip suffix from the compressed file name).
340 * This option is the default when decompressing.
341 * -N --name
342 * gzip: always save the original file name and time stamp (this is the default)
343 * gunzip: restore the original file name and time stamp if present.
344 */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000345int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000346int gunzip_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000347{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500348 getopt32(argv, "cfvqdtn");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000349 argv += optind;
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100350
351 /* If called as zcat...
352 * Normally, "zcat" is just "gunzip -c".
353 * But if seamless magic is enabled, then we are much more clever.
354 */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000355 if (applet_name[1] == 'c')
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100356 option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000357
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200358 return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000359}
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200360#endif
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000361
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200362
363/*
364 * Modified for busybox by Glenn McGrath
365 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
366 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200367 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200368 */
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200369//usage:#define bunzip2_trivial_usage
Denys Vlasenko3b2acb72010-10-09 21:10:32 +0200370//usage: "[-cf] [FILE]..."
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200371//usage:#define bunzip2_full_usage "\n\n"
372//usage: "Decompress FILEs (or stdin)\n"
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200373//usage: "\n -c Write to stdout"
374//usage: "\n -f Force"
375//usage:#define bzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100376//usage: "[FILE]..."
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200377//usage:#define bzcat_full_usage "\n\n"
378//usage: "Decompress to stdout"
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100379
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100380//config:config BUNZIP2
381//config: bool "bunzip2"
382//config: default y
383//config: help
384//config: bunzip2 is a compression utility using the Burrows-Wheeler block
385//config: sorting text compression algorithm, and Huffman coding. Compression
386//config: is generally considerably better than that achieved by more
387//config: conventional LZ77/LZ78-based compressors, and approaches the
388//config: performance of the PPM family of statistical compressors.
389//config:
390//config: Unless you have a specific application which requires bunzip2, you
391//config: should probably say N here.
392
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +0100393//applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
394//applet:IF_BUNZIP2(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100395//kbuild:lib-$(CONFIG_BZIP2) += bbunzip.o
396//kbuild:lib-$(CONFIG_BUNZIP2) += bbunzip.o
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200397#if ENABLE_BUNZIP2
398static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100399IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(transformer_aux_data_t *aux)
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200400{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100401 return unpack_bz2_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200402}
403int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
404int bunzip2_main(int argc UNUSED_PARAM, char **argv)
405{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500406 getopt32(argv, "cfvqdt");
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200407 argv += optind;
408 if (applet_name[2] == 'c') /* bzcat */
409 option_mask32 |= OPT_STDOUT;
410
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200411 return bbunpack(argv, unpack_bunzip2, make_new_name_generic, "bz2");
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200412}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000413#endif
414
415
416/*
417 * Small lzma deflate implementation.
418 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
419 *
420 * Based on bunzip.c from busybox
421 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200422 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000423 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200424//usage:#define unlzma_trivial_usage
425//usage: "[-cf] [FILE]..."
426//usage:#define unlzma_full_usage "\n\n"
427//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200428//usage: "\n -c Write to stdout"
429//usage: "\n -f Force"
430//usage:
431//usage:#define lzma_trivial_usage
432//usage: "-d [-cf] [FILE]..."
433//usage:#define lzma_full_usage "\n\n"
434//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200435//usage: "\n -d Decompress"
436//usage: "\n -c Write to stdout"
437//usage: "\n -f Force"
438//usage:
439//usage:#define lzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100440//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200441//usage:#define lzcat_full_usage "\n\n"
442//usage: "Decompress to stdout"
443//usage:
444//usage:#define unxz_trivial_usage
445//usage: "[-cf] [FILE]..."
446//usage:#define unxz_full_usage "\n\n"
447//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200448//usage: "\n -c Write to stdout"
449//usage: "\n -f Force"
450//usage:
451//usage:#define xz_trivial_usage
452//usage: "-d [-cf] [FILE]..."
453//usage:#define xz_full_usage "\n\n"
454//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200455//usage: "\n -d Decompress"
456//usage: "\n -c Write to stdout"
457//usage: "\n -f Force"
458//usage:
459//usage:#define xzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100460//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200461//usage:#define xzcat_full_usage "\n\n"
462//usage: "Decompress to stdout"
463
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100464//config:config UNLZMA
465//config: bool "unlzma"
466//config: default y
467//config: help
468//config: unlzma is a compression utility using the Lempel-Ziv-Markov chain
469//config: compression algorithm, and range coding. Compression
470//config: is generally considerably better than that achieved by the bzip2
471//config: compressors.
472//config:
473//config: The BusyBox unlzma applet is limited to decompression only.
474//config: On an x86 system, this applet adds about 4K.
475//config:
476//config:config FEATURE_LZMA_FAST
477//config: bool "Optimize unlzma for speed"
478//config: default n
479//config: depends on UNLZMA
480//config: help
481//config: This option reduces decompression time by about 25% at the cost of
482//config: a 1K bigger binary.
483//config:
484//config:config LZMA
485//config: bool "Provide lzma alias which supports only unpacking"
486//config: default y
487//config: depends on UNLZMA
488//config: help
489//config: Enable this option if you want commands like "lzma -d" to work.
490//config: IOW: you'll get lzma applet, but it will always require -d option.
491
Denys Vlasenko36184a42013-11-14 09:54:24 +0100492//applet:IF_UNLZMA(APPLET(unlzma, BB_DIR_USR_BIN, BB_SUID_DROP))
Denys Vlasenkoac216872013-11-14 11:38:18 +0100493//applet:IF_UNLZMA(APPLET_ODDNAME(lzcat, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzcat))
494//applet:IF_LZMA(APPLET_ODDNAME(lzma, unlzma, BB_DIR_USR_BIN, BB_SUID_DROP, lzma))
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100495//kbuild:lib-$(CONFIG_UNLZMA) += bbunzip.o
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000496#if ENABLE_UNLZMA
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000497static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100498IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(transformer_aux_data_t *aux)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000499{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100500 return unpack_lzma_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000501}
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000502int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000503int unlzma_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000504{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500505 IF_LZMA(int opts =) getopt32(argv, "cfvqdt");
Denys Vlasenkoe04c8672010-05-30 03:33:50 +0200506# if ENABLE_LZMA
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200507 /* lzma without -d or -t? */
508 if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
509 bb_show_usage();
510# endif
511 /* lzcat? */
512 if (applet_name[2] == 'c')
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000513 option_mask32 |= OPT_STDOUT;
514
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200515 argv += optind;
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200516 return bbunpack(argv, unpack_unlzma, make_new_name_generic, "lzma");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000517}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000518#endif
519
520
Denys Vlasenkof6beef62013-11-14 11:39:00 +0100521//config:config UNXZ
522//config: bool "unxz"
523//config: default y
524//config: help
525//config: unxz is a unlzma successor.
526//config:
527//config:config XZ
528//config: bool "Provide xz alias which supports only unpacking"
529//config: default y
530//config: depends on UNXZ
531//config: help
532//config: Enable this option if you want commands like "xz -d" to work.
533//config: IOW: you'll get xz applet, but it will always require -d option.
534
Denys Vlasenko36184a42013-11-14 09:54:24 +0100535//applet:IF_UNXZ(APPLET(unxz, BB_DIR_USR_BIN, BB_SUID_DROP))
Denys Vlasenkoac216872013-11-14 11:38:18 +0100536//applet:IF_UNXZ(APPLET_ODDNAME(xzcat, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xzcat))
537//applet:IF_XZ(APPLET_ODDNAME(xz, unxz, BB_DIR_USR_BIN, BB_SUID_DROP, xz))
Denys Vlasenko66620fa2013-11-14 09:53:52 +0100538//kbuild:lib-$(CONFIG_UNXZ) += bbunzip.o
Denys Vlasenko602ce692010-05-30 03:35:18 +0200539#if ENABLE_UNXZ
Denys Vlasenko602ce692010-05-30 03:35:18 +0200540static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100541IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(transformer_aux_data_t *aux)
Denys Vlasenko602ce692010-05-30 03:35:18 +0200542{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100543 return unpack_xz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denys Vlasenko602ce692010-05-30 03:35:18 +0200544}
Denys Vlasenko602ce692010-05-30 03:35:18 +0200545int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
546int unxz_main(int argc UNUSED_PARAM, char **argv)
547{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500548 IF_XZ(int opts =) getopt32(argv, "cfvqdt");
Denys Vlasenko602ce692010-05-30 03:35:18 +0200549# if ENABLE_XZ
550 /* xz without -d or -t? */
551 if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
552 bb_show_usage();
553# endif
554 /* xzcat? */
555 if (applet_name[2] == 'c')
556 option_mask32 |= OPT_STDOUT;
557
558 argv += optind;
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200559 return bbunpack(argv, unpack_unxz, make_new_name_generic, "xz");
Denys Vlasenko602ce692010-05-30 03:35:18 +0200560}
Denys Vlasenko602ce692010-05-30 03:35:18 +0200561#endif