blob: e16e6b083781bbf69694c0572f2fc35f6a265491 [file] [log] [blame]
Denis Vlasenkoab9eef22007-03-07 22:02:23 +00001/* vi: set sw=4 ts=4: */
2/*
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +00003 * Common code for gunzip-like applets
Denis Vlasenkoab9eef22007-03-07 22:02:23 +00004 *
5 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
6 */
7
8#include "busybox.h"
9#include "unarchive.h"
10
11enum {
Denis Vlasenko75605782007-03-14 00:07:51 +000012 OPT_STDOUT = 0x1,
13 OPT_FORCE = 0x2,
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000014/* gunzip only: */
Denis Vlasenko75605782007-03-14 00:07:51 +000015 OPT_VERBOSE = 0x4,
16 OPT_DECOMPRESS = 0x8,
17 OPT_TEST = 0x10,
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{
23 int fd = open(filename, flags, mode);
24 if (fd < 0) {
25 bb_perror_msg("%s", filename);
26 return 1;
27 }
28 if (fd != to_fd) {
29 if (dup2(fd, to_fd) < 0)
30 bb_perror_msg_and_die("cannot dup");
31 close(fd);
32 }
33 return 0;
34}
35
Denis Vlasenko75605782007-03-14 00:07:51 +000036int bbunpack(char **argv,
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000037 char* (*make_new_name)(char *filename),
38 USE_DESKTOP(long long) int (*unpacker)(void)
39)
40{
41 struct stat stat_buf;
42 USE_DESKTOP(long long) int status;
Denis Vlasenko6c939e02007-03-07 23:22:47 +000043 char *filename, *new_name;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000044 smallint exitcode = 0;
45
46 do {
Denis Vlasenko6c939e02007-03-07 23:22:47 +000047 /* NB: new_name is *maybe* malloc'ed! */
48 new_name = NULL;
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000049 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
Denis Vlasenko6c939e02007-03-07 23:22:47 +000050
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000051 if (filename && LONE_DASH(filename))
52 filename = NULL;
53
54 /* Open src */
55 if (filename) {
56 if (stat(filename, &stat_buf) != 0) {
57 bb_perror_msg("%s", filename);
58 err:
59 exitcode = 1;
60 goto free_name;
61 }
62 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
63 goto err;
64 }
65
66 /* Special cases: test, stdout */
67 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
68 if (option_mask32 & OPT_TEST)
69 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
70 goto err;
71 filename = NULL;
72 }
73
Denis Vlasenko6c939e02007-03-07 23:22:47 +000074 /* Open dst if we are going to unpack to file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000075 if (filename) {
76 new_name = make_new_name(filename);
77 if (!new_name) {
78 bb_error_msg("%s: unknown suffix - ignored", filename);
79 goto err;
80 }
Denis Vlasenko6c939e02007-03-07 23:22:47 +000081 /* O_EXCL: "real" bunzip2 doesn't overwrite files */
82 /* GNU gunzip goes not bail out, but goes to next file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000083 if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
84 stat_buf.st_mode))
85 goto err;
86 }
87
Denis Vlasenko6c939e02007-03-07 23:22:47 +000088 /* Check that the input is sane */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +000089 if (isatty(STDIN_FILENO) && (option_mask32 & OPT_FORCE) == 0) {
90 bb_error_msg_and_die("compressed data not read from terminal, "
91 "use -f to force it");
92 }
93
94 status = unpacker();
95 if (status < 0)
96 exitcode = 1;
97
98 if (filename) {
99 char *del = new_name;
100 if (status >= 0) {
101 /* TODO: restore user/group/times here? */
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000102 /* Delete _compressed_ file */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000103 del = filename;
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000104 /* restore extension (unless tgz -> tar case) */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000105 if (new_name == filename)
106 filename[strlen(filename)] = '.';
107 }
108 if (unlink(del) < 0)
109 bb_perror_msg_and_die("cannot remove %s", del);
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000110
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000111#if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
112 /* Extreme bloat for gunzip compat */
113 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
114 fprintf(stderr, "%s: %u%% - replaced with %s\n",
115 filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
116 }
117#endif
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000118
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000119 free_name:
120 if (new_name != filename)
121 free(new_name);
122 }
123 } while (*argv && *++argv);
124
125 return exitcode;
126}
127
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000128#if ENABLE_BUNZIP2 || ENABLE_UNLZMA || ENABLE_UNCOMPRESS
129
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000130static
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000131char* make_new_name_generic(char *filename, const char *expected_ext)
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000132{
133 char *extension = strrchr(filename, '.');
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000134 if (!extension || strcmp(extension + 1, expected_ext) != 0) {
Denis Vlasenko6c939e02007-03-07 23:22:47 +0000135 /* Mimic GNU gunzip - "real" bunzip2 tries to */
136 /* unpack file anyway, to file.out */
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000137 return NULL;
138 }
139 *extension = '\0';
140 return filename;
141}
142
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000143#endif
144
145
Denis Vlasenkoa4688bf2007-03-11 10:56:37 +0000146/*
147 * Modified for busybox by Glenn McGrath <bug1@iinet.net.au>
148 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
149 *
150 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
151 */
152
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000153#if ENABLE_BUNZIP2
154
155static
156char* make_new_name_bunzip2(char *filename)
157{
158 return make_new_name_generic(filename, "bz2");
159}
160
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000161static
162USE_DESKTOP(long long) int unpack_bunzip2(void)
163{
164 return uncompressStream(STDIN_FILENO, STDOUT_FILENO);
165}
166
167int bunzip2_main(int argc, char **argv);
168int bunzip2_main(int argc, char **argv)
169{
170 getopt32(argc, argv, "cf");
171 argv += optind;
172 if (applet_name[2] == 'c')
173 option_mask32 |= OPT_STDOUT;
174
Denis Vlasenko75605782007-03-14 00:07:51 +0000175 return bbunpack(argv, make_new_name_bunzip2, unpack_bunzip2);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000176}
177
178#endif
179
180
181/*
182 * Gzip implementation for busybox
183 *
184 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
185 *
186 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
187 * based on gzip sources
188 *
189 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
190 * well as stdin/stdout, and to generally behave itself wrt command line
191 * handling.
192 *
193 * General cleanup to better adhere to the style guide and make use of standard
194 * busybox functions by Glenn McGrath <bug1@iinet.net.au>
195 *
196 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
197 *
198 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
199 * Copyright (C) 1992-1993 Jean-loup Gailly
200 * The unzip code was written and put in the public domain by Mark Adler.
201 * Portions of the lzw code are derived from the public domain 'compress'
202 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
203 * Ken Turkowski, Dave Mack and Peter Jannesen.
204 *
205 * See the license_msg below and the file COPYING for the software license.
206 * See the file algorithm.doc for the compression algorithms and file formats.
207 */
208
209#if ENABLE_GUNZIP
210
211static
212char* make_new_name_gunzip(char *filename)
213{
214 char *extension = strrchr(filename, '.');
215
216 if (!extension)
217 return NULL;
218
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000219 extension++;
220 if (strcmp(extension, "tgz" + 1) == 0
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000221#ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000222 || strcmp(extension, "Z") == 0
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000223#endif
224 ) {
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000225 extension[-1] = '\0';
226 } else if(strcmp(extension, "tgz") == 0) {
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000227 filename = xstrdup(filename);
228 extension = strrchr(filename, '.');
229 extension[2] = 'a';
230 extension[3] = 'r';
231 } else {
232 return NULL;
233 }
234 return filename;
235}
236
237static
238USE_DESKTOP(long long) int unpack_gunzip(void)
239{
240 USE_DESKTOP(long long) int status = -1;
241
242 /* do the decompression, and cleanup */
243 if (xread_char(STDIN_FILENO) == 0x1f) {
244 unsigned char magic2;
245
246 magic2 = xread_char(STDIN_FILENO);
247 if (ENABLE_FEATURE_GUNZIP_UNCOMPRESS && magic2 == 0x9d) {
248 status = uncompress(STDIN_FILENO, STDOUT_FILENO);
249 } else if (magic2 == 0x8b) {
250 check_header_gzip_or_die(STDIN_FILENO);
251 status = inflate_gunzip(STDIN_FILENO, STDOUT_FILENO);
252 } else {
253 goto bad_magic;
254 }
255 if (status < 0) {
256 bb_error_msg("error inflating");
257 }
258 } else {
259 bad_magic:
260 bb_error_msg("invalid magic");
261 /* status is still == -1 */
262 }
263 return status;
264}
265
266int gunzip_main(int argc, char **argv);
267int gunzip_main(int argc, char **argv)
268{
Denis Vlasenko75605782007-03-14 00:07:51 +0000269 getopt32(argc, argv, "cfvdt");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000270 argv += optind;
271 /* if called as zcat */
272 if (applet_name[1] == 'c')
273 option_mask32 |= OPT_STDOUT;
274
Denis Vlasenko75605782007-03-14 00:07:51 +0000275 return bbunpack(argv, make_new_name_gunzip, unpack_gunzip);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000276}
277
278#endif
279
280
281/*
282 * Small lzma deflate implementation.
283 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
284 *
285 * Based on bunzip.c from busybox
286 *
287 * Licensed under GPL v2, see file LICENSE in this tarball for details.
288 */
289
290#if ENABLE_UNLZMA
291
292static
293char* make_new_name_unlzma(char *filename)
294{
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000295 return make_new_name_generic(filename, "lzma");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000296}
297
298static
299USE_DESKTOP(long long) int unpack_unlzma(void)
300{
301 return unlzma(STDIN_FILENO, STDOUT_FILENO);
302}
303
304int unlzma_main(int argc, char **argv);
305int unlzma_main(int argc, char **argv)
306{
307 getopt32(argc, argv, "c");
308 argv += optind;
309 /* lzmacat? */
310 if (applet_name[4] == 'c')
311 option_mask32 |= OPT_STDOUT;
312
Denis Vlasenko75605782007-03-14 00:07:51 +0000313 return bbunpack(argv, make_new_name_unlzma, unpack_unlzma);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000314}
315
316#endif
317
318
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000319/*
320 * Uncompress applet for busybox (c) 2002 Glenn McGrath
321 *
322 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
323 */
324
325#if ENABLE_UNCOMPRESS
326
327static
328char* make_new_name_uncompress(char *filename)
329{
Denis Vlasenkobebbd8c2007-03-09 20:49:55 +0000330 return make_new_name_generic(filename, "Z");
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000331}
332
333static
334USE_DESKTOP(long long) int unpack_uncompress(void)
335{
336 USE_DESKTOP(long long) int status = -1;
337
338 if ((xread_char(STDIN_FILENO) != 0x1f) || (xread_char(STDIN_FILENO) != 0x9d)) {
339 bb_error_msg("invalid magic");
340 } else {
341 status = uncompress(STDIN_FILENO, STDOUT_FILENO);
342 }
343 return status;
344}
345
346int uncompress_main(int argc, char **argv);
347int uncompress_main(int argc, char **argv)
348{
349 getopt32(argc, argv, "cf");
350 argv += optind;
351
Denis Vlasenko75605782007-03-14 00:07:51 +0000352 return bbunpack(argv, make_new_name_uncompress, unpack_uncompress);
Denis Vlasenkoab9eef22007-03-07 22:02:23 +0000353}
354
355#endif