blob: 54dc2f5f1c3198ae8d64d00e2b517d73d0b3d387 [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
10enum {
Denys Vlasenkoccb88a62010-05-27 02:22:54 +020011 OPT_STDOUT = 1 << 0,
12 OPT_FORCE = 1 << 1,
13 /* only some decompressors: */
14 OPT_VERBOSE = 1 << 2,
15 OPT_DECOMPRESS = 1 << 3,
16 OPT_TEST = 1 << 4,
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010017 SEAMLESS_MAGIC = (1 << 31) * SEAMLESS_COMPRESSION,
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000018};
19
20static
21int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
22{
Denis Vlasenko50f7f442007-04-11 23:20:53 +000023 int fd = open3_or_warn(filename, flags, mode);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000024 if (fd < 0) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000025 return 1;
26 }
Denis Vlasenko50f7f442007-04-11 23:20:53 +000027 xmove_fd(fd, to_fd);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000028 return 0;
29}
30
Denys Vlasenko39a04f72010-05-31 14:18:57 +020031char* FAST_FUNC append_ext(char *filename, const char *expected_ext)
32{
33 return xasprintf("%s.%s", filename, expected_ext);
34}
35
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000036int FAST_FUNC bbunpack(char **argv,
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010037 IF_DESKTOP(long long) int FAST_FUNC (*unpacker)(transformer_aux_data_t *aux),
Denys Vlasenko39a04f72010-05-31 14:18:57 +020038 char* FAST_FUNC (*make_new_name)(char *filename, const char *expected_ext),
39 const char *expected_ext
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000040)
41{
42 struct stat stat_buf;
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010043 IF_DESKTOP(long long) int status = 0;
Denis Vlasenko6c939e02007-03-07 23:22:47 +000044 char *filename, *new_name;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000045 smallint exitcode = 0;
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010046 transformer_aux_data_t aux;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000047
48 do {
Denis Vlasenko6c939e02007-03-07 23:22:47 +000049 /* NB: new_name is *maybe* malloc'ed! */
50 new_name = NULL;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000051 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
Denis Vlasenko6c939e02007-03-07 23:22:47 +000052
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000053 if (filename && LONE_DASH(filename))
54 filename = NULL;
55
56 /* Open src */
57 if (filename) {
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010058 if (!(option_mask32 & SEAMLESS_MAGIC)) {
59 if (stat(filename, &stat_buf) != 0) {
60 err_name:
61 bb_simple_perror_msg(filename);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000062 err:
Denys Vlasenko8e96efa2013-02-28 18:06:09 +010063 exitcode = 1;
64 goto free_name;
65 }
66 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
67 goto err;
68 } else {
69 /* "clever zcat" */
70 int fd = open_zipped(filename);
71 if (fd < 0)
72 goto err_name;
73 xmove_fd(fd, STDIN_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000074 }
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000075 }
76
77 /* Special cases: test, stdout */
78 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
79 if (option_mask32 & OPT_TEST)
80 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
Denys Vlasenkof2d84782013-02-28 18:04:22 +010081 xfunc_die();
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000082 filename = NULL;
83 }
84
Denis Vlasenko6c939e02007-03-07 23:22:47 +000085 /* Open dst if we are going to unpack to file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000086 if (filename) {
Denys Vlasenko39a04f72010-05-31 14:18:57 +020087 new_name = make_new_name(filename, expected_ext);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000088 if (!new_name) {
89 bb_error_msg("%s: unknown suffix - ignored", filename);
90 goto err;
91 }
Denis Vlasenkoa04cc472008-06-25 20:54:45 +000092
93 /* -f: overwrite existing output files */
94 if (option_mask32 & OPT_FORCE) {
95 unlink(new_name);
96 }
97
Denis Vlasenko6c939e02007-03-07 23:22:47 +000098 /* O_EXCL: "real" bunzip2 doesn't overwrite files */
Denis Vlasenko5dd8a032007-10-05 15:26:08 +000099 /* GNU gunzip does not bail out, but goes to next file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000100 if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
101 stat_buf.st_mode))
102 goto err;
103 }
104
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000105 /* Check that the input is sane */
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100106 if (!(option_mask32 & OPT_FORCE) && isatty(STDIN_FILENO)) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000107 bb_error_msg_and_die("compressed data not read from terminal, "
108 "use -f to force it");
109 }
110
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100111 if (!(option_mask32 & SEAMLESS_MAGIC)) {
112 init_transformer_aux_data(&aux);
113 aux.check_signature = 1;
114 status = unpacker(&aux);
115 if (status < 0)
116 exitcode = 1;
117 } else {
118 /* "clever zcat" */
119 if (!filename) {
120 if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_detected*/ 0))
121 goto err;
122 }
123 if (bb_copyfd_eof(STDIN_FILENO, STDOUT_FILENO) < 0) {
124 /* Disk full, tty closed, etc. No point in continuing */
125 xfunc_die();
126 }
127 }
Denys Vlasenkoc531b9a2011-10-31 01:05:16 +0100128
129 if (!(option_mask32 & OPT_STDOUT))
130 xclose(STDOUT_FILENO); /* with error check! */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000131
132 if (filename) {
133 char *del = new_name;
134 if (status >= 0) {
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000135 /* TODO: restore other things? */
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100136 if (aux.mtime != 0) {
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100137 struct timeval times[2];
Denys Vlasenko389cca42009-11-15 02:28:56 +0100138
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100139 times[1].tv_sec = times[0].tv_sec = aux.mtime;
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100140 times[1].tv_usec = times[0].tv_usec = 0;
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200141 /* Note: we closed it first.
Denys Vlasenko389cca42009-11-15 02:28:56 +0100142 * On some systems calling utimes
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200143 * then closing resets the mtime
144 * back to current time. */
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100145 utimes(new_name, times); /* ignoring errors */
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000146 }
147
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000148 /* Delete _compressed_ file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000149 del = filename;
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000150 /* restore extension (unless tgz -> tar case) */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000151 if (new_name == filename)
152 filename[strlen(filename)] = '.';
153 }
Denis Vlasenko1bb552b2007-04-05 21:25:15 +0000154 xunlink(del);
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000155
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000156#if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
157 /* Extreme bloat for gunzip compat */
158 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
159 fprintf(stderr, "%s: %u%% - replaced with %s\n",
160 filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
161 }
162#endif
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000163
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000164 free_name:
165 if (new_name != filename)
166 free(new_name);
167 }
168 } while (*argv && *++argv);
169
Denys Vlasenkoc531b9a2011-10-31 01:05:16 +0100170 if (option_mask32 & OPT_STDOUT)
171 xclose(STDOUT_FILENO); /* with error check! */
172
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000173 return exitcode;
174}
175
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200176#if ENABLE_UNCOMPRESS || ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNXZ
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000177static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200178char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000179{
180 char *extension = strrchr(filename, '.');
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000181 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000182 /* Mimic GNU gunzip - "real" bunzip2 tries to */
183 /* unpack file anyway, to file.out */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000184 return NULL;
185 }
186 *extension = '\0';
187 return filename;
188}
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000189#endif
190
191
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000192/*
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200193 * Uncompress applet for busybox (c) 2002 Glenn McGrath
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000194 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200195 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000196 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200197
198//usage:#define uncompress_trivial_usage
199//usage: "[-cf] [FILE]..."
200//usage:#define uncompress_full_usage "\n\n"
201//usage: "Decompress .Z file[s]\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200202//usage: "\n -c Write to stdout"
203//usage: "\n -f Overwrite"
204
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200205#if ENABLE_UNCOMPRESS
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000206static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100207IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(transformer_aux_data_t *aux)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000208{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100209 return unpack_Z_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200210}
211int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
212int uncompress_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000213{
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200214 getopt32(argv, "cf");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000215 argv += optind;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000216
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200217 return bbunpack(argv, unpack_uncompress, make_new_name_generic, "Z");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000218}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000219#endif
220
221
222/*
223 * Gzip implementation for busybox
224 *
225 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
226 *
227 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
228 * based on gzip sources
229 *
230 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
231 * well as stdin/stdout, and to generally behave itself wrt command line
232 * handling.
233 *
234 * General cleanup to better adhere to the style guide and make use of standard
Denis Vlasenko0beaff82007-09-21 13:16:32 +0000235 * busybox functions by Glenn McGrath
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000236 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200237 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000238 *
239 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
240 * Copyright (C) 1992-1993 Jean-loup Gailly
241 * The unzip code was written and put in the public domain by Mark Adler.
242 * Portions of the lzw code are derived from the public domain 'compress'
243 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
244 * Ken Turkowski, Dave Mack and Peter Jannesen.
245 *
246 * See the license_msg below and the file COPYING for the software license.
247 * See the file algorithm.doc for the compression algorithms and file formats.
248 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200249
250//usage:#define gunzip_trivial_usage
251//usage: "[-cft] [FILE]..."
252//usage:#define gunzip_full_usage "\n\n"
253//usage: "Decompress FILEs (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200254//usage: "\n -c Write to stdout"
255//usage: "\n -f Force"
256//usage: "\n -t Test file integrity"
257//usage:
258//usage:#define gunzip_example_usage
259//usage: "$ ls -la /tmp/BusyBox*\n"
260//usage: "-rw-rw-r-- 1 andersen andersen 557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n"
261//usage: "$ gunzip /tmp/BusyBox-0.43.tar.gz\n"
262//usage: "$ ls -la /tmp/BusyBox*\n"
263//usage: "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
264//usage:
265//usage:#define zcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100266//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200267//usage:#define zcat_full_usage "\n\n"
268//usage: "Decompress to stdout"
269
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000270#if ENABLE_GUNZIP
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000271static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200272char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000273{
274 char *extension = strrchr(filename, '.');
275
276 if (!extension)
277 return NULL;
278
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000279 extension++;
280 if (strcmp(extension, "tgz" + 1) == 0
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000281#if ENABLE_FEATURE_SEAMLESS_Z
282 || (extension[0] == 'Z' && extension[1] == '\0')
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000283#endif
284 ) {
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000285 extension[-1] = '\0';
Denis Vlasenko51742f42007-04-12 00:32:05 +0000286 } else if (strcmp(extension, "tgz") == 0) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000287 filename = xstrdup(filename);
288 extension = strrchr(filename, '.');
289 extension[2] = 'a';
290 extension[3] = 'r';
291 } else {
292 return NULL;
293 }
294 return filename;
295}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000296static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100297IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(transformer_aux_data_t *aux)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000298{
Denys Vlasenko02c3c382012-03-06 16:32:06 +0100299 return unpack_gz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000300}
Denis Vlasenko52978092008-03-30 13:11:47 +0000301/*
302 * Linux kernel build uses gzip -d -n. We accept and ignore it.
303 * Man page says:
304 * -n --no-name
305 * gzip: do not save the original file name and time stamp.
306 * (The original name is always saved if the name had to be truncated.)
307 * gunzip: do not restore the original file name/time even if present
308 * (remove only the gzip suffix from the compressed file name).
309 * This option is the default when decompressing.
310 * -N --name
311 * gzip: always save the original file name and time stamp (this is the default)
312 * gunzip: restore the original file name and time stamp if present.
313 */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000314int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000315int gunzip_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000316{
Denis Vlasenko52978092008-03-30 13:11:47 +0000317 getopt32(argv, "cfvdtn");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000318 argv += optind;
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100319
320 /* If called as zcat...
321 * Normally, "zcat" is just "gunzip -c".
322 * But if seamless magic is enabled, then we are much more clever.
323 */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000324 if (applet_name[1] == 'c')
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100325 option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000326
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200327 return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000328}
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200329#endif
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000330
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200331
332/*
333 * Modified for busybox by Glenn McGrath
334 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
335 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200336 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200337 */
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200338//usage:#define bunzip2_trivial_usage
Denys Vlasenko3b2acb72010-10-09 21:10:32 +0200339//usage: "[-cf] [FILE]..."
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200340//usage:#define bunzip2_full_usage "\n\n"
341//usage: "Decompress FILEs (or stdin)\n"
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200342//usage: "\n -c Write to stdout"
343//usage: "\n -f Force"
344//usage:#define bzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100345//usage: "[FILE]..."
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200346//usage:#define bzcat_full_usage "\n\n"
347//usage: "Decompress to stdout"
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +0100348//applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
349//applet:IF_BUNZIP2(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200350#if ENABLE_BUNZIP2
351static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100352IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(transformer_aux_data_t *aux)
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200353{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100354 return unpack_bz2_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200355}
356int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
357int bunzip2_main(int argc UNUSED_PARAM, char **argv)
358{
359 getopt32(argv, "cfvdt");
360 argv += optind;
361 if (applet_name[2] == 'c') /* bzcat */
362 option_mask32 |= OPT_STDOUT;
363
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200364 return bbunpack(argv, unpack_bunzip2, make_new_name_generic, "bz2");
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200365}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000366#endif
367
368
369/*
370 * Small lzma deflate implementation.
371 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
372 *
373 * Based on bunzip.c from busybox
374 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200375 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000376 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200377
378//usage:#define unlzma_trivial_usage
379//usage: "[-cf] [FILE]..."
380//usage:#define unlzma_full_usage "\n\n"
381//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200382//usage: "\n -c Write to stdout"
383//usage: "\n -f Force"
384//usage:
385//usage:#define lzma_trivial_usage
386//usage: "-d [-cf] [FILE]..."
387//usage:#define lzma_full_usage "\n\n"
388//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200389//usage: "\n -d Decompress"
390//usage: "\n -c Write to stdout"
391//usage: "\n -f Force"
392//usage:
393//usage:#define lzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100394//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200395//usage:#define lzcat_full_usage "\n\n"
396//usage: "Decompress to stdout"
397//usage:
398//usage:#define unxz_trivial_usage
399//usage: "[-cf] [FILE]..."
400//usage:#define unxz_full_usage "\n\n"
401//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200402//usage: "\n -c Write to stdout"
403//usage: "\n -f Force"
404//usage:
405//usage:#define xz_trivial_usage
406//usage: "-d [-cf] [FILE]..."
407//usage:#define xz_full_usage "\n\n"
408//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200409//usage: "\n -d Decompress"
410//usage: "\n -c Write to stdout"
411//usage: "\n -f Force"
412//usage:
413//usage:#define xzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100414//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200415//usage:#define xzcat_full_usage "\n\n"
416//usage: "Decompress to stdout"
417
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000418#if ENABLE_UNLZMA
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000419static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100420IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(transformer_aux_data_t *aux)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000421{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100422 return unpack_lzma_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000423}
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000424int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000425int unlzma_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000426{
Denys Vlasenkoe04c8672010-05-30 03:33:50 +0200427 IF_LZMA(int opts =) getopt32(argv, "cfvdt");
428# if ENABLE_LZMA
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200429 /* lzma without -d or -t? */
430 if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
431 bb_show_usage();
432# endif
433 /* lzcat? */
434 if (applet_name[2] == 'c')
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000435 option_mask32 |= OPT_STDOUT;
436
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200437 argv += optind;
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200438 return bbunpack(argv, unpack_unlzma, make_new_name_generic, "lzma");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000439}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000440#endif
441
442
Denys Vlasenko602ce692010-05-30 03:35:18 +0200443#if ENABLE_UNXZ
Denys Vlasenko602ce692010-05-30 03:35:18 +0200444static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100445IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(transformer_aux_data_t *aux)
Denys Vlasenko602ce692010-05-30 03:35:18 +0200446{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100447 return unpack_xz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denys Vlasenko602ce692010-05-30 03:35:18 +0200448}
Denys Vlasenko602ce692010-05-30 03:35:18 +0200449int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
450int unxz_main(int argc UNUSED_PARAM, char **argv)
451{
Denys Vlasenko9297dbc2010-07-05 21:37:12 +0200452 IF_XZ(int opts =) getopt32(argv, "cfvdt");
Denys Vlasenko602ce692010-05-30 03:35:18 +0200453# if ENABLE_XZ
454 /* xz without -d or -t? */
455 if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
456 bb_show_usage();
457# endif
458 /* xzcat? */
459 if (applet_name[2] == 'c')
460 option_mask32 |= OPT_STDOUT;
461
462 argv += optind;
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200463 return bbunpack(argv, unpack_unxz, make_new_name_generic, "xz");
Denys Vlasenko602ce692010-05-30 03:35:18 +0200464}
Denys Vlasenko602ce692010-05-30 03:35:18 +0200465#endif