blob: 3735e3282c09b6e626dc1ad305457cb0ebaf5e07 [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
Mike Frysinger93b51812013-03-03 00:48:53 -050010/* Note: must be kept in sync with archival/lzop.c */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000011enum {
Denys Vlasenkoccb88a62010-05-27 02:22:54 +020012 OPT_STDOUT = 1 << 0,
13 OPT_FORCE = 1 << 1,
14 /* only some decompressors: */
15 OPT_VERBOSE = 1 << 2,
Mike Frysinger920c1ba2013-02-28 17:21:50 -050016 OPT_QUIET = 1 << 3,
17 OPT_DECOMPRESS = 1 << 4,
18 OPT_TEST = 1 << 5,
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010019 SEAMLESS_MAGIC = (1 << 31) * SEAMLESS_COMPRESSION,
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000020};
21
22static
23int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
24{
Denis Vlasenko50f7f442007-04-11 23:20:53 +000025 int fd = open3_or_warn(filename, flags, mode);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000026 if (fd < 0) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000027 return 1;
28 }
Denis Vlasenko50f7f442007-04-11 23:20:53 +000029 xmove_fd(fd, to_fd);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000030 return 0;
31}
32
Denys Vlasenko39a04f72010-05-31 14:18:57 +020033char* FAST_FUNC append_ext(char *filename, const char *expected_ext)
34{
35 return xasprintf("%s.%s", filename, expected_ext);
36}
37
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000038int FAST_FUNC bbunpack(char **argv,
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010039 IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(transformer_aux_data_t *aux),
Denys Vlasenko39a04f72010-05-31 14:18:57 +020040 char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext),
41 const char *expected_ext
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000042)
43{
44 struct stat stat_buf;
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010045 IF_DESKTOP(long long) int status = 0;
Denis Vlasenko6c939e02007-03-07 23:22:47 +000046 char *filename, *new_name;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000047 smallint exitcode = 0;
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010048 transformer_aux_data_t aux;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000049
50 do {
Denis Vlasenko6c939e02007-03-07 23:22:47 +000051 /* NB: new_name is *maybe* malloc'ed! */
52 new_name = NULL;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000053 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
Denis Vlasenko6c939e02007-03-07 23:22:47 +000054
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000055 if (filename && LONE_DASH(filename))
56 filename = NULL;
57
58 /* Open src */
59 if (filename) {
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010060 if (!(option_mask32 & SEAMLESS_MAGIC)) {
61 if (stat(filename, &stat_buf) != 0) {
62 err_name:
63 bb_simple_perror_msg(filename);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000064 err:
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010065 exitcode = 1;
66 goto free_name;
67 }
68 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
69 goto err;
70 } else {
Denys Vlasenko41655432013-02-28 18:37:04 +010071 /* "clever zcat" with FILE */
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010072 int fd = open_zipped(filename);
73 if (fd < 0)
74 goto err_name;
75 xmove_fd(fd, STDIN_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000076 }
Denys Vlasenko41655432013-02-28 18:37:04 +010077 } else
78 if (option_mask32 & SEAMLESS_MAGIC) {
79 /* "clever zcat" on stdin */
80 if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_detected*/ 0))
81 goto err;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000082 }
83
84 /* Special cases: test, stdout */
85 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
86 if (option_mask32 & OPT_TEST)
87 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
Denys Vlasenkof2d84782013-02-28 18:04:22 +010088 xfunc_die();
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000089 filename = NULL;
90 }
91
Denis Vlasenko6c939e02007-03-07 23:22:47 +000092 /* Open dst if we are going to unpack to file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000093 if (filename) {
Denys Vlasenko39a04f72010-05-31 14:18:57 +020094 new_name = make_new_name(filename, expected_ext);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000095 if (!new_name) {
96 bb_error_msg("%s: unknown suffix - ignored", filename);
97 goto err;
98 }
Denis Vlasenkoa04cc472008-06-25 20:54:45 +000099
100 /* -f: overwrite existing output files */
101 if (option_mask32 & OPT_FORCE) {
102 unlink(new_name);
103 }
104
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000105 /* O_EXCL: "real" bunzip2 doesn't overwrite files */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +0000106 /* GNU gunzip does not bail out, but goes to next file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000107 if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
108 stat_buf.st_mode))
109 goto err;
110 }
111
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000112 /* Check that the input is sane */
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100113 if (!(option_mask32 & OPT_FORCE) && isatty(STDIN_FILENO)) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000114 bb_error_msg_and_die("compressed data not read from terminal, "
115 "use -f to force it");
116 }
117
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100118 if (!(option_mask32 & SEAMLESS_MAGIC)) {
119 init_transformer_aux_data(&aux);
120 aux.check_signature = 1;
121 status = unpacker(&aux);
122 if (status < 0)
123 exitcode = 1;
124 } else {
Denys Vlasenko41655432013-02-28 18:37:04 +0100125 if (bb_copyfd_eof(STDIN_FILENO, STDOUT_FILENO) < 0)
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100126 /* Disk full, tty closed, etc. No point in continuing */
127 xfunc_die();
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100128 }
Denys Vlasenkoc531b9a2011-10-31 01:05:16 +0100129
130 if (!(option_mask32 & OPT_STDOUT))
131 xclose(STDOUT_FILENO); /* with error check! */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000132
133 if (filename) {
134 char *del = new_name;
Denys Vlasenkod0bc7082013-07-30 05:41:11 +0200135
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000136 if (status >= 0) {
Denys Vlasenkod0bc7082013-07-30 05:41:11 +0200137 unsigned new_name_len;
138
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000139 /* TODO: restore other things? */
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100140 if (aux.mtime != 0) {
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100141 struct timeval times[2];
Denys Vlasenko389cca42009-11-15 02:28:56 +0100142
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100143 times[1].tv_sec = times[0].tv_sec = aux.mtime;
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100144 times[1].tv_usec = times[0].tv_usec = 0;
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200145 /* Note: we closed it first.
Denys Vlasenko389cca42009-11-15 02:28:56 +0100146 * On some systems calling utimes
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200147 * then closing resets the mtime
148 * back to current time. */
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100149 utimes(new_name, times); /* ignoring errors */
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000150 }
151
Denys Vlasenkod0bc7082013-07-30 05:41:11 +0200152 if (ENABLE_DESKTOP)
153 new_name_len = strlen(new_name);
154 /* Restore source filename (unless tgz -> tar case) */
155 if (new_name == filename) {
156 new_name_len = strlen(filename);
157 filename[new_name_len] = '.';
158 }
159 /* Extreme bloat for gunzip compat */
160 /* Some users do want this info... */
161 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE)) {
162 unsigned percent = status
163 ? ((uoff_t)stat_buf.st_size * 100u / (unsigned long long)status)
164 : 0;
165 fprintf(stderr, "%s: %u%% - replaced with %.*s\n",
166 filename,
167 100u - percent,
168 new_name_len, new_name
169 );
170 }
171 /* Delete _source_ file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000172 del = filename;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000173 }
Denis Vlasenko1bb552b2007-04-05 21:25:15 +0000174 xunlink(del);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000175 free_name:
176 if (new_name != filename)
177 free(new_name);
178 }
179 } while (*argv && *++argv);
180
Denys Vlasenkoc531b9a2011-10-31 01:05:16 +0100181 if (option_mask32 & OPT_STDOUT)
182 xclose(STDOUT_FILENO); /* with error check! */
183
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000184 return exitcode;
185}
186
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200187#if ENABLE_UNCOMPRESS || ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNXZ
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000188static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200189char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000190{
191 char *extension = strrchr(filename, '.');
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000192 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000193 /* Mimic GNU gunzip - "real" bunzip2 tries to */
194 /* unpack file anyway, to file.out */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000195 return NULL;
196 }
197 *extension = '\0';
198 return filename;
199}
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000200#endif
201
202
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000203/*
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200204 * Uncompress applet for busybox (c) 2002 Glenn McGrath
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000205 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200206 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000207 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200208
209//usage:#define uncompress_trivial_usage
210//usage: "[-cf] [FILE]..."
211//usage:#define uncompress_full_usage "\n\n"
212//usage: "Decompress .Z file[s]\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200213//usage: "\n -c Write to stdout"
214//usage: "\n -f Overwrite"
215
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200216#if ENABLE_UNCOMPRESS
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000217static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100218IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(transformer_aux_data_t *aux)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000219{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100220 return unpack_Z_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200221}
222int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
223int uncompress_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000224{
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200225 getopt32(argv, "cf");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000226 argv += optind;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000227
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200228 return bbunpack(argv, unpack_uncompress, make_new_name_generic, "Z");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000229}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000230#endif
231
232
233/*
234 * Gzip implementation for busybox
235 *
236 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
237 *
238 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
239 * based on gzip sources
240 *
241 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
242 * well as stdin/stdout, and to generally behave itself wrt command line
243 * handling.
244 *
245 * General cleanup to better adhere to the style guide and make use of standard
Denis Vlasenko0beaff82007-09-21 13:16:32 +0000246 * busybox functions by Glenn McGrath
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000247 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200248 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000249 *
250 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
251 * Copyright (C) 1992-1993 Jean-loup Gailly
252 * The unzip code was written and put in the public domain by Mark Adler.
253 * Portions of the lzw code are derived from the public domain 'compress'
254 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
255 * Ken Turkowski, Dave Mack and Peter Jannesen.
256 *
257 * See the license_msg below and the file COPYING for the software license.
258 * See the file algorithm.doc for the compression algorithms and file formats.
259 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200260
261//usage:#define gunzip_trivial_usage
262//usage: "[-cft] [FILE]..."
263//usage:#define gunzip_full_usage "\n\n"
264//usage: "Decompress FILEs (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200265//usage: "\n -c Write to stdout"
266//usage: "\n -f Force"
267//usage: "\n -t Test file integrity"
268//usage:
269//usage:#define gunzip_example_usage
270//usage: "$ ls -la /tmp/BusyBox*\n"
271//usage: "-rw-rw-r-- 1 andersen andersen 557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n"
272//usage: "$ gunzip /tmp/BusyBox-0.43.tar.gz\n"
273//usage: "$ ls -la /tmp/BusyBox*\n"
274//usage: "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
275//usage:
276//usage:#define zcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100277//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200278//usage:#define zcat_full_usage "\n\n"
279//usage: "Decompress to stdout"
280
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000281#if ENABLE_GUNZIP
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000282static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200283char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000284{
285 char *extension = strrchr(filename, '.');
286
287 if (!extension)
288 return NULL;
289
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000290 extension++;
291 if (strcmp(extension, "tgz" + 1) == 0
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000292#if ENABLE_FEATURE_SEAMLESS_Z
293 || (extension[0] == 'Z' && extension[1] == '\0')
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000294#endif
295 ) {
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000296 extension[-1] = '\0';
Denis Vlasenko51742f42007-04-12 00:32:05 +0000297 } else if (strcmp(extension, "tgz") == 0) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000298 filename = xstrdup(filename);
299 extension = strrchr(filename, '.');
300 extension[2] = 'a';
301 extension[3] = 'r';
302 } else {
303 return NULL;
304 }
305 return filename;
306}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000307static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100308IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(transformer_aux_data_t *aux)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000309{
Denys Vlasenko02c3c382012-03-06 16:32:06 +0100310 return unpack_gz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000311}
Denis Vlasenko52978092008-03-30 13:11:47 +0000312/*
313 * Linux kernel build uses gzip -d -n. We accept and ignore it.
314 * Man page says:
315 * -n --no-name
316 * gzip: do not save the original file name and time stamp.
317 * (The original name is always saved if the name had to be truncated.)
318 * gunzip: do not restore the original file name/time even if present
319 * (remove only the gzip suffix from the compressed file name).
320 * This option is the default when decompressing.
321 * -N --name
322 * gzip: always save the original file name and time stamp (this is the default)
323 * gunzip: restore the original file name and time stamp if present.
324 */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000325int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000326int gunzip_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000327{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500328 getopt32(argv, "cfvqdtn");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000329 argv += optind;
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100330
331 /* If called as zcat...
332 * Normally, "zcat" is just "gunzip -c".
333 * But if seamless magic is enabled, then we are much more clever.
334 */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000335 if (applet_name[1] == 'c')
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100336 option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000337
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200338 return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000339}
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200340#endif
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000341
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200342
343/*
344 * Modified for busybox by Glenn McGrath
345 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
346 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200347 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200348 */
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200349//usage:#define bunzip2_trivial_usage
Denys Vlasenko3b2acb72010-10-09 21:10:32 +0200350//usage: "[-cf] [FILE]..."
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200351//usage:#define bunzip2_full_usage "\n\n"
352//usage: "Decompress FILEs (or stdin)\n"
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200353//usage: "\n -c Write to stdout"
354//usage: "\n -f Force"
355//usage:#define bzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100356//usage: "[FILE]..."
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200357//usage:#define bzcat_full_usage "\n\n"
358//usage: "Decompress to stdout"
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +0100359//applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
360//applet:IF_BUNZIP2(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200361#if ENABLE_BUNZIP2
362static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100363IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(transformer_aux_data_t *aux)
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200364{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100365 return unpack_bz2_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200366}
367int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
368int bunzip2_main(int argc UNUSED_PARAM, char **argv)
369{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500370 getopt32(argv, "cfvqdt");
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200371 argv += optind;
372 if (applet_name[2] == 'c') /* bzcat */
373 option_mask32 |= OPT_STDOUT;
374
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200375 return bbunpack(argv, unpack_bunzip2, make_new_name_generic, "bz2");
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200376}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000377#endif
378
379
380/*
381 * Small lzma deflate implementation.
382 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
383 *
384 * Based on bunzip.c from busybox
385 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200386 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000387 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200388
389//usage:#define unlzma_trivial_usage
390//usage: "[-cf] [FILE]..."
391//usage:#define unlzma_full_usage "\n\n"
392//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200393//usage: "\n -c Write to stdout"
394//usage: "\n -f Force"
395//usage:
396//usage:#define lzma_trivial_usage
397//usage: "-d [-cf] [FILE]..."
398//usage:#define lzma_full_usage "\n\n"
399//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200400//usage: "\n -d Decompress"
401//usage: "\n -c Write to stdout"
402//usage: "\n -f Force"
403//usage:
404//usage:#define lzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100405//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200406//usage:#define lzcat_full_usage "\n\n"
407//usage: "Decompress to stdout"
408//usage:
409//usage:#define unxz_trivial_usage
410//usage: "[-cf] [FILE]..."
411//usage:#define unxz_full_usage "\n\n"
412//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200413//usage: "\n -c Write to stdout"
414//usage: "\n -f Force"
415//usage:
416//usage:#define xz_trivial_usage
417//usage: "-d [-cf] [FILE]..."
418//usage:#define xz_full_usage "\n\n"
419//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200420//usage: "\n -d Decompress"
421//usage: "\n -c Write to stdout"
422//usage: "\n -f Force"
423//usage:
424//usage:#define xzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100425//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200426//usage:#define xzcat_full_usage "\n\n"
427//usage: "Decompress to stdout"
428
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000429#if ENABLE_UNLZMA
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000430static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100431IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(transformer_aux_data_t *aux)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000432{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100433 return unpack_lzma_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000434}
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000435int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000436int unlzma_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000437{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500438 IF_LZMA(int opts =) getopt32(argv, "cfvqdt");
Denys Vlasenkoe04c8672010-05-30 03:33:50 +0200439# if ENABLE_LZMA
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200440 /* lzma without -d or -t? */
441 if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
442 bb_show_usage();
443# endif
444 /* lzcat? */
445 if (applet_name[2] == 'c')
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000446 option_mask32 |= OPT_STDOUT;
447
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200448 argv += optind;
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200449 return bbunpack(argv, unpack_unlzma, make_new_name_generic, "lzma");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000450}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000451#endif
452
453
Denys Vlasenko602ce692010-05-30 03:35:18 +0200454#if ENABLE_UNXZ
Denys Vlasenko602ce692010-05-30 03:35:18 +0200455static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100456IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(transformer_aux_data_t *aux)
Denys Vlasenko602ce692010-05-30 03:35:18 +0200457{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100458 return unpack_xz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denys Vlasenko602ce692010-05-30 03:35:18 +0200459}
Denys Vlasenko602ce692010-05-30 03:35:18 +0200460int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
461int unxz_main(int argc UNUSED_PARAM, char **argv)
462{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500463 IF_XZ(int opts =) getopt32(argv, "cfvqdt");
Denys Vlasenko602ce692010-05-30 03:35:18 +0200464# if ENABLE_XZ
465 /* xz without -d or -t? */
466 if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
467 bb_show_usage();
468# endif
469 /* xzcat? */
470 if (applet_name[2] == 'c')
471 option_mask32 |= OPT_STDOUT;
472
473 argv += optind;
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200474 return bbunpack(argv, unpack_unxz, make_new_name_generic, "xz");
Denys Vlasenko602ce692010-05-30 03:35:18 +0200475}
Denys Vlasenko602ce692010-05-30 03:35:18 +0200476#endif