blob: b332eecd690b73f6e31ac5cf55fd38c4f060631d [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;
135 if (status >= 0) {
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000136 /* TODO: restore other things? */
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100137 if (aux.mtime != 0) {
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100138 struct timeval times[2];
Denys Vlasenko389cca42009-11-15 02:28:56 +0100139
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100140 times[1].tv_sec = times[0].tv_sec = aux.mtime;
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100141 times[1].tv_usec = times[0].tv_usec = 0;
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200142 /* Note: we closed it first.
Denys Vlasenko389cca42009-11-15 02:28:56 +0100143 * On some systems calling utimes
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200144 * then closing resets the mtime
145 * back to current time. */
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100146 utimes(new_name, times); /* ignoring errors */
Denis Vlasenko1a9e9bd2008-11-01 12:54:56 +0000147 }
148
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000149 /* Delete _compressed_ file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000150 del = filename;
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000151 /* restore extension (unless tgz -> tar case) */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000152 if (new_name == filename)
153 filename[strlen(filename)] = '.';
154 }
Denis Vlasenko1bb552b2007-04-05 21:25:15 +0000155 xunlink(del);
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000156
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000157#if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
158 /* Extreme bloat for gunzip compat */
159 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
160 fprintf(stderr, "%s: %u%% - replaced with %s\n",
161 filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
162 }
163#endif
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000164
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000165 free_name:
166 if (new_name != filename)
167 free(new_name);
168 }
169 } while (*argv && *++argv);
170
Denys Vlasenkoc531b9a2011-10-31 01:05:16 +0100171 if (option_mask32 & OPT_STDOUT)
172 xclose(STDOUT_FILENO); /* with error check! */
173
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000174 return exitcode;
175}
176
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200177#if ENABLE_UNCOMPRESS || ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNXZ
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000178static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200179char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000180{
181 char *extension = strrchr(filename, '.');
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000182 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000183 /* Mimic GNU gunzip - "real" bunzip2 tries to */
184 /* unpack file anyway, to file.out */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000185 return NULL;
186 }
187 *extension = '\0';
188 return filename;
189}
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000190#endif
191
192
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000193/*
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200194 * Uncompress applet for busybox (c) 2002 Glenn McGrath
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000195 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200196 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000197 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200198
199//usage:#define uncompress_trivial_usage
200//usage: "[-cf] [FILE]..."
201//usage:#define uncompress_full_usage "\n\n"
202//usage: "Decompress .Z file[s]\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200203//usage: "\n -c Write to stdout"
204//usage: "\n -f Overwrite"
205
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200206#if ENABLE_UNCOMPRESS
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000207static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100208IF_DESKTOP(long long) int FAST_FUNC unpack_uncompress(transformer_aux_data_t *aux)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000209{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100210 return unpack_Z_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200211}
212int uncompress_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
213int uncompress_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000214{
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200215 getopt32(argv, "cf");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000216 argv += optind;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000217
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200218 return bbunpack(argv, unpack_uncompress, make_new_name_generic, "Z");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000219}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000220#endif
221
222
223/*
224 * Gzip implementation for busybox
225 *
226 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
227 *
228 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
229 * based on gzip sources
230 *
231 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
232 * well as stdin/stdout, and to generally behave itself wrt command line
233 * handling.
234 *
235 * General cleanup to better adhere to the style guide and make use of standard
Denis Vlasenko0beaff82007-09-21 13:16:32 +0000236 * busybox functions by Glenn McGrath
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000237 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200238 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000239 *
240 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
241 * Copyright (C) 1992-1993 Jean-loup Gailly
242 * The unzip code was written and put in the public domain by Mark Adler.
243 * Portions of the lzw code are derived from the public domain 'compress'
244 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
245 * Ken Turkowski, Dave Mack and Peter Jannesen.
246 *
247 * See the license_msg below and the file COPYING for the software license.
248 * See the file algorithm.doc for the compression algorithms and file formats.
249 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200250
251//usage:#define gunzip_trivial_usage
252//usage: "[-cft] [FILE]..."
253//usage:#define gunzip_full_usage "\n\n"
254//usage: "Decompress FILEs (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200255//usage: "\n -c Write to stdout"
256//usage: "\n -f Force"
257//usage: "\n -t Test file integrity"
258//usage:
259//usage:#define gunzip_example_usage
260//usage: "$ ls -la /tmp/BusyBox*\n"
261//usage: "-rw-rw-r-- 1 andersen andersen 557009 Apr 11 10:55 /tmp/BusyBox-0.43.tar.gz\n"
262//usage: "$ gunzip /tmp/BusyBox-0.43.tar.gz\n"
263//usage: "$ ls -la /tmp/BusyBox*\n"
264//usage: "-rw-rw-r-- 1 andersen andersen 1761280 Apr 14 17:47 /tmp/BusyBox-0.43.tar\n"
265//usage:
266//usage:#define zcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100267//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200268//usage:#define zcat_full_usage "\n\n"
269//usage: "Decompress to stdout"
270
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000271#if ENABLE_GUNZIP
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000272static
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200273char* FAST_FUNC make_new_name_gunzip(char *filename, const char *expected_ext UNUSED_PARAM)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000274{
275 char *extension = strrchr(filename, '.');
276
277 if (!extension)
278 return NULL;
279
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000280 extension++;
281 if (strcmp(extension, "tgz" + 1) == 0
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +0000282#if ENABLE_FEATURE_SEAMLESS_Z
283 || (extension[0] == 'Z' && extension[1] == '\0')
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000284#endif
285 ) {
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000286 extension[-1] = '\0';
Denis Vlasenko51742f42007-04-12 00:32:05 +0000287 } else if (strcmp(extension, "tgz") == 0) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000288 filename = xstrdup(filename);
289 extension = strrchr(filename, '.');
290 extension[2] = 'a';
291 extension[3] = 'r';
292 } else {
293 return NULL;
294 }
295 return filename;
296}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000297static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100298IF_DESKTOP(long long) int FAST_FUNC unpack_gunzip(transformer_aux_data_t *aux)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000299{
Denys Vlasenko02c3c382012-03-06 16:32:06 +0100300 return unpack_gz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000301}
Denis Vlasenko52978092008-03-30 13:11:47 +0000302/*
303 * Linux kernel build uses gzip -d -n. We accept and ignore it.
304 * Man page says:
305 * -n --no-name
306 * gzip: do not save the original file name and time stamp.
307 * (The original name is always saved if the name had to be truncated.)
308 * gunzip: do not restore the original file name/time even if present
309 * (remove only the gzip suffix from the compressed file name).
310 * This option is the default when decompressing.
311 * -N --name
312 * gzip: always save the original file name and time stamp (this is the default)
313 * gunzip: restore the original file name and time stamp if present.
314 */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000315int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000316int gunzip_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000317{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500318 getopt32(argv, "cfvqdtn");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000319 argv += optind;
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100320
321 /* If called as zcat...
322 * Normally, "zcat" is just "gunzip -c".
323 * But if seamless magic is enabled, then we are much more clever.
324 */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000325 if (applet_name[1] == 'c')
Denys Vlasenko8e96efa2013-02-28 18:06:09 +0100326 option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000327
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200328 return bbunpack(argv, unpack_gunzip, make_new_name_gunzip, /*unused:*/ NULL);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000329}
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200330#endif
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000331
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200332
333/*
334 * Modified for busybox by Glenn McGrath
335 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
336 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200337 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200338 */
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200339//usage:#define bunzip2_trivial_usage
Denys Vlasenko3b2acb72010-10-09 21:10:32 +0200340//usage: "[-cf] [FILE]..."
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200341//usage:#define bunzip2_full_usage "\n\n"
342//usage: "Decompress FILEs (or stdin)\n"
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200343//usage: "\n -c Write to stdout"
344//usage: "\n -f Force"
345//usage:#define bzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100346//usage: "[FILE]..."
Denys Vlasenkof0f94702010-06-06 01:53:38 +0200347//usage:#define bzcat_full_usage "\n\n"
348//usage: "Decompress to stdout"
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +0100349//applet:IF_BUNZIP2(APPLET(bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP))
350//applet:IF_BUNZIP2(APPLET_ODDNAME(bzcat, bunzip2, BB_DIR_USR_BIN, BB_SUID_DROP, bzcat))
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200351#if ENABLE_BUNZIP2
352static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100353IF_DESKTOP(long long) int FAST_FUNC unpack_bunzip2(transformer_aux_data_t *aux)
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200354{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100355 return unpack_bz2_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200356}
357int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
358int bunzip2_main(int argc UNUSED_PARAM, char **argv)
359{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500360 getopt32(argv, "cfvqdt");
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200361 argv += optind;
362 if (applet_name[2] == 'c') /* bzcat */
363 option_mask32 |= OPT_STDOUT;
364
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200365 return bbunpack(argv, unpack_bunzip2, make_new_name_generic, "bz2");
Denys Vlasenkod93f19e2010-05-30 03:46:54 +0200366}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000367#endif
368
369
370/*
371 * Small lzma deflate implementation.
372 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
373 *
374 * Based on bunzip.c from busybox
375 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +0200376 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000377 */
Pere Orga1f4447b2011-03-27 22:40:30 +0200378
379//usage:#define unlzma_trivial_usage
380//usage: "[-cf] [FILE]..."
381//usage:#define unlzma_full_usage "\n\n"
382//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200383//usage: "\n -c Write to stdout"
384//usage: "\n -f Force"
385//usage:
386//usage:#define lzma_trivial_usage
387//usage: "-d [-cf] [FILE]..."
388//usage:#define lzma_full_usage "\n\n"
389//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200390//usage: "\n -d Decompress"
391//usage: "\n -c Write to stdout"
392//usage: "\n -f Force"
393//usage:
394//usage:#define lzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100395//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200396//usage:#define lzcat_full_usage "\n\n"
397//usage: "Decompress to stdout"
398//usage:
399//usage:#define unxz_trivial_usage
400//usage: "[-cf] [FILE]..."
401//usage:#define unxz_full_usage "\n\n"
402//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200403//usage: "\n -c Write to stdout"
404//usage: "\n -f Force"
405//usage:
406//usage:#define xz_trivial_usage
407//usage: "-d [-cf] [FILE]..."
408//usage:#define xz_full_usage "\n\n"
409//usage: "Decompress FILE (or stdin)\n"
Pere Orga1f4447b2011-03-27 22:40:30 +0200410//usage: "\n -d Decompress"
411//usage: "\n -c Write to stdout"
412//usage: "\n -f Force"
413//usage:
414//usage:#define xzcat_trivial_usage
Denys Vlasenkof2d84782013-02-28 18:04:22 +0100415//usage: "[FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +0200416//usage:#define xzcat_full_usage "\n\n"
417//usage: "Decompress to stdout"
418
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000419#if ENABLE_UNLZMA
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000420static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100421IF_DESKTOP(long long) int FAST_FUNC unpack_unlzma(transformer_aux_data_t *aux)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000422{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100423 return unpack_lzma_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000424}
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000425int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000426int unlzma_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000427{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500428 IF_LZMA(int opts =) getopt32(argv, "cfvqdt");
Denys Vlasenkoe04c8672010-05-30 03:33:50 +0200429# if ENABLE_LZMA
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200430 /* lzma without -d or -t? */
431 if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
432 bb_show_usage();
433# endif
434 /* lzcat? */
435 if (applet_name[2] == 'c')
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000436 option_mask32 |= OPT_STDOUT;
437
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200438 argv += optind;
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200439 return bbunpack(argv, unpack_unlzma, make_new_name_generic, "lzma");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000440}
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000441#endif
442
443
Denys Vlasenko602ce692010-05-30 03:35:18 +0200444#if ENABLE_UNXZ
Denys Vlasenko602ce692010-05-30 03:35:18 +0200445static
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100446IF_DESKTOP(long long) int FAST_FUNC unpack_unxz(transformer_aux_data_t *aux)
Denys Vlasenko602ce692010-05-30 03:35:18 +0200447{
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100448 return unpack_xz_stream(aux, STDIN_FILENO, STDOUT_FILENO);
Denys Vlasenko602ce692010-05-30 03:35:18 +0200449}
Denys Vlasenko602ce692010-05-30 03:35:18 +0200450int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
451int unxz_main(int argc UNUSED_PARAM, char **argv)
452{
Mike Frysinger08e28e82013-02-28 21:28:21 -0500453 IF_XZ(int opts =) getopt32(argv, "cfvqdt");
Denys Vlasenko602ce692010-05-30 03:35:18 +0200454# if ENABLE_XZ
455 /* xz without -d or -t? */
456 if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
457 bb_show_usage();
458# endif
459 /* xzcat? */
460 if (applet_name[2] == 'c')
461 option_mask32 |= OPT_STDOUT;
462
463 argv += optind;
Denys Vlasenko39a04f72010-05-31 14:18:57 +0200464 return bbunpack(argv, unpack_unxz, make_new_name_generic, "xz");
Denys Vlasenko602ce692010-05-30 03:35:18 +0200465}
Denys Vlasenko602ce692010-05-30 03:35:18 +0200466#endif