aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-03-07 22:02:23 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-03-07 22:02:23 +0000
commitab9eef21a57c23567505e8fbceb8e5ea76306ce1 (patch)
tree800845b64d0e9a204743656c4d267cc6c9917cde
parent8e858e2700651a0e973169b579622cd3dcd0defd (diff)
downloadbusybox-w32-ab9eef21a57c23567505e8fbceb8e5ea76306ce1.tar.gz
busybox-w32-ab9eef21a57c23567505e8fbceb8e5ea76306ce1.tar.bz2
busybox-w32-ab9eef21a57c23567505e8fbceb8e5ea76306ce1.zip
bunzip2/gunzip/uncompress/unlzma: merge into common code -
fix few corner cases, reduce size by 450 bytes. Update testsuite.
-rw-r--r--archival/Kbuild8
-rw-r--r--archival/bbunzip.c346
-rw-r--r--archival/bunzip2.c67
-rw-r--r--archival/gunzip.c163
-rw-r--r--archival/libunarchive/check_header_gzip.c9
-rw-r--r--archival/libunarchive/get_header_tar_gz.c2
-rw-r--r--archival/rpm.c2
-rw-r--r--archival/rpm2cpio.c2
-rw-r--r--archival/uncompress.c95
-rw-r--r--archival/unlzma.c65
-rw-r--r--include/unarchive.h2
-rwxr-xr-xtestsuite/bunzip2.tests84
-rwxr-xr-xtestsuite/bzcat.tests49
-rwxr-xr-xtestsuite/gunzip.tests3
14 files changed, 493 insertions, 404 deletions
diff --git a/archival/Kbuild b/archival/Kbuild
index f85e0c2a7..50b90fa93 100644
--- a/archival/Kbuild
+++ b/archival/Kbuild
@@ -8,15 +8,15 @@ libs-y += libunarchive/
8 8
9lib-y:= 9lib-y:=
10lib-$(CONFIG_AR) += ar.o 10lib-$(CONFIG_AR) += ar.o
11lib-$(CONFIG_BUNZIP2) += bunzip2.o 11lib-$(CONFIG_BUNZIP2) += bbunzip.o ### bunzip2.o
12lib-$(CONFIG_UNLZMA) += unlzma.o 12lib-$(CONFIG_UNLZMA) += bbunzip.o ### unlzma.o
13lib-$(CONFIG_CPIO) += cpio.o 13lib-$(CONFIG_CPIO) += cpio.o
14lib-$(CONFIG_DPKG) += dpkg.o 14lib-$(CONFIG_DPKG) += dpkg.o
15lib-$(CONFIG_DPKG_DEB) += dpkg_deb.o 15lib-$(CONFIG_DPKG_DEB) += dpkg_deb.o
16lib-$(CONFIG_GUNZIP) += gunzip.o 16lib-$(CONFIG_GUNZIP) += bbunzip.o ### gunzip.o
17lib-$(CONFIG_GZIP) += gzip.o 17lib-$(CONFIG_GZIP) += gzip.o
18lib-$(CONFIG_RPM2CPIO) += rpm2cpio.o 18lib-$(CONFIG_RPM2CPIO) += rpm2cpio.o
19lib-$(CONFIG_RPM) += rpm.o 19lib-$(CONFIG_RPM) += rpm.o
20lib-$(CONFIG_TAR) += tar.o 20lib-$(CONFIG_TAR) += tar.o
21lib-$(CONFIG_UNCOMPRESS) += uncompress.o 21lib-$(CONFIG_UNCOMPRESS) += bbunzip.o ### uncompress.o
22lib-$(CONFIG_UNZIP) += unzip.o 22lib-$(CONFIG_UNZIP) += unzip.o
diff --git a/archival/bbunzip.c b/archival/bbunzip.c
new file mode 100644
index 000000000..7ec8f4f0a
--- /dev/null
+++ b/archival/bbunzip.c
@@ -0,0 +1,346 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Modified for busybox by Glenn McGrath <bug1@iinet.net.au>
4 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
5 *
6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
7 */
8
9#include "busybox.h"
10#include "unarchive.h"
11
12enum {
13 OPT_STDOUT = 1,
14 OPT_FORCE = 2,
15/* gunzip only: */
16 OPT_TEST = 4,
17 OPT_DECOMPRESS = 8,
18 OPT_VERBOSE = 0x10,
19};
20
21static
22int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
23{
24 int fd = open(filename, flags, mode);
25 if (fd < 0) {
26 bb_perror_msg("%s", filename);
27 return 1;
28 }
29 if (fd != to_fd) {
30 if (dup2(fd, to_fd) < 0)
31 bb_perror_msg_and_die("cannot dup");
32 close(fd);
33 }
34 return 0;
35}
36
37static
38int unpack(char **argv,
39 char* (*make_new_name)(char *filename),
40 USE_DESKTOP(long long) int (*unpacker)(void)
41)
42{
43 struct stat stat_buf;
44 USE_DESKTOP(long long) int status;
45 char *filename;
46 /* NB: new_name is *possibly* malloc'ed! */
47 smallint exitcode = 0;
48
49 do {
50 char *new_name = NULL;
51
52 filename = *argv; /* can be NULL - 'streaming' bunzip2 */
53 if (filename && LONE_DASH(filename))
54 filename = NULL;
55
56 /* Open src */
57 if (filename) {
58 if (stat(filename, &stat_buf) != 0) {
59 bb_perror_msg("%s", filename);
60 err:
61 exitcode = 1;
62 goto free_name;
63 }
64 if (open_to_or_warn(STDIN_FILENO, filename, O_RDONLY, 0))
65 goto err;
66 }
67
68 /* Special cases: test, stdout */
69 if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
70 if (option_mask32 & OPT_TEST)
71 if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
72 goto err;
73 filename = NULL;
74 }
75
76 /* Open dst unless -c, "-" or called as bzcat */
77 if (filename) {
78 new_name = make_new_name(filename);
79 if (!new_name) {
80 bb_error_msg("%s: unknown suffix - ignored", filename);
81 goto err;
82 }
83 /* O_EXCL: "real" bunzip2 doesn't overwrite files too */
84 /* TODO: "real" gunzip goes not bail out, but goes
85 * to next file */
86 if (open_to_or_warn(STDOUT_FILENO, new_name, O_WRONLY | O_CREAT | O_EXCL,
87 stat_buf.st_mode))
88 goto err;
89 }
90
91 /* Check that the input is sane. */
92 if (isatty(STDIN_FILENO) && (option_mask32 & OPT_FORCE) == 0) {
93 bb_error_msg_and_die("compressed data not read from terminal, "
94 "use -f to force it");
95 }
96
97 status = unpacker();
98 if (status < 0)
99 exitcode = 1;
100
101 if (filename) {
102 char *del = new_name;
103 if (status >= 0) {
104 /* TODO: restore user/group/times here? */
105 /* delete _old_ file */
106 del = filename;
107 /* Restore extension (unless tgz -> tar case) */
108 if (new_name == filename)
109 filename[strlen(filename)] = '.';
110 }
111 if (unlink(del) < 0)
112 bb_perror_msg_and_die("cannot remove %s", del);
113#if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */
114 /* Extreme bloat for gunzip compat */
115 if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE) && status >= 0) {
116 fprintf(stderr, "%s: %u%% - replaced with %s\n",
117 filename, (unsigned)(stat_buf.st_size*100 / (status+1)), new_name);
118 }
119#endif
120 free_name:
121 if (new_name != filename)
122 free(new_name);
123 }
124 } while (*argv && *++argv);
125
126 return exitcode;
127}
128
129#if ENABLE_BUNZIP2
130
131static
132char* make_new_name_bunzip2(char *filename)
133{
134 char *extension = strrchr(filename, '.');
135 if (!extension || strcmp(extension, ".bz2") != 0) {
136 /* Mimic GNU gunzip ("real" bunzip2 tries to */
137 /* unpack file anyway, to file.out) */
138 return NULL;
139 }
140 *extension = '\0';
141 return filename;
142}
143
144static
145USE_DESKTOP(long long) int unpack_bunzip2(void)
146{
147 return uncompressStream(STDIN_FILENO, STDOUT_FILENO);
148}
149
150int bunzip2_main(int argc, char **argv);
151int bunzip2_main(int argc, char **argv)
152{
153 getopt32(argc, argv, "cf");
154 argv += optind;
155 if (applet_name[2] == 'c')
156 option_mask32 |= OPT_STDOUT;
157
158 return unpack(argv, make_new_name_bunzip2, unpack_bunzip2);
159}
160
161#endif
162
163
164/*
165 * Gzip implementation for busybox
166 *
167 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
168 *
169 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
170 * based on gzip sources
171 *
172 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
173 * well as stdin/stdout, and to generally behave itself wrt command line
174 * handling.
175 *
176 * General cleanup to better adhere to the style guide and make use of standard
177 * busybox functions by Glenn McGrath <bug1@iinet.net.au>
178 *
179 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
180 *
181 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
182 * Copyright (C) 1992-1993 Jean-loup Gailly
183 * The unzip code was written and put in the public domain by Mark Adler.
184 * Portions of the lzw code are derived from the public domain 'compress'
185 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
186 * Ken Turkowski, Dave Mack and Peter Jannesen.
187 *
188 * See the license_msg below and the file COPYING for the software license.
189 * See the file algorithm.doc for the compression algorithms and file formats.
190 */
191
192#if ENABLE_GUNZIP
193
194static
195char* make_new_name_gunzip(char *filename)
196{
197 char *extension = strrchr(filename, '.');
198
199 if (!extension)
200 return NULL;
201
202 if (strcmp(extension, ".gz") == 0
203#ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS
204 || strcmp(extension, ".Z") == 0
205#endif
206 ) {
207 *extension = '\0';
208 } else if(strcmp(extension, ".tgz") == 0) {
209 filename = xstrdup(filename);
210 extension = strrchr(filename, '.');
211 extension[2] = 'a';
212 extension[3] = 'r';
213 } else {
214 return NULL;
215 }
216 return filename;
217}
218
219static
220USE_DESKTOP(long long) int unpack_gunzip(void)
221{
222 USE_DESKTOP(long long) int status = -1;
223
224 /* do the decompression, and cleanup */
225 if (xread_char(STDIN_FILENO) == 0x1f) {
226 unsigned char magic2;
227
228 magic2 = xread_char(STDIN_FILENO);
229 if (ENABLE_FEATURE_GUNZIP_UNCOMPRESS && magic2 == 0x9d) {
230 status = uncompress(STDIN_FILENO, STDOUT_FILENO);
231 } else if (magic2 == 0x8b) {
232 check_header_gzip_or_die(STDIN_FILENO);
233 status = inflate_gunzip(STDIN_FILENO, STDOUT_FILENO);
234 } else {
235 goto bad_magic;
236 }
237 if (status < 0) {
238 bb_error_msg("error inflating");
239 }
240 } else {
241 bad_magic:
242 bb_error_msg("invalid magic");
243 /* status is still == -1 */
244 }
245 return status;
246}
247
248int gunzip_main(int argc, char **argv);
249int gunzip_main(int argc, char **argv)
250{
251 getopt32(argc, argv, "cftdv");
252 argv += optind;
253 /* if called as zcat */
254 if (applet_name[1] == 'c')
255 option_mask32 |= OPT_STDOUT;
256
257 return unpack(argv, make_new_name_gunzip, unpack_gunzip);
258}
259
260#endif
261
262
263/*
264 * Small lzma deflate implementation.
265 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
266 *
267 * Based on bunzip.c from busybox
268 *
269 * Licensed under GPL v2, see file LICENSE in this tarball for details.
270 */
271
272#if ENABLE_UNLZMA
273
274static
275char* make_new_name_unlzma(char *filename)
276{
277 char *extension = strrchr(filename, '.');
278 if (!extension || strcmp(extension, ".lzma") != 0)
279 return NULL;
280 *extension = '\0';
281 return filename;
282}
283
284static
285USE_DESKTOP(long long) int unpack_unlzma(void)
286{
287 return unlzma(STDIN_FILENO, STDOUT_FILENO);
288}
289
290int unlzma_main(int argc, char **argv);
291int unlzma_main(int argc, char **argv)
292{
293 getopt32(argc, argv, "c");
294 argv += optind;
295 /* lzmacat? */
296 if (applet_name[4] == 'c')
297 option_mask32 |= OPT_STDOUT;
298
299 return unpack(argv, make_new_name_unlzma, unpack_unlzma);
300}
301
302#endif
303
304
305/* vi: set sw=4 ts=4: */
306/*
307 * Uncompress applet for busybox (c) 2002 Glenn McGrath
308 *
309 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
310 */
311
312#if ENABLE_UNCOMPRESS
313
314static
315char* make_new_name_uncompress(char *filename)
316{
317 char *extension = strrchr(filename, '.');
318 if (!extension || strcmp(extension, ".Z") != 0)
319 return NULL;
320 *extension = '\0';
321 return filename;
322}
323
324static
325USE_DESKTOP(long long) int unpack_uncompress(void)
326{
327 USE_DESKTOP(long long) int status = -1;
328
329 if ((xread_char(STDIN_FILENO) != 0x1f) || (xread_char(STDIN_FILENO) != 0x9d)) {
330 bb_error_msg("invalid magic");
331 } else {
332 status = uncompress(STDIN_FILENO, STDOUT_FILENO);
333 }
334 return status;
335}
336
337int uncompress_main(int argc, char **argv);
338int uncompress_main(int argc, char **argv)
339{
340 getopt32(argc, argv, "cf");
341 argv += optind;
342
343 return unpack(argv, make_new_name_uncompress, unpack_uncompress);
344}
345
346#endif
diff --git a/archival/bunzip2.c b/archival/bunzip2.c
deleted file mode 100644
index 1deac7b53..000000000
--- a/archival/bunzip2.c
+++ /dev/null
@@ -1,67 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Modified for busybox by Glenn McGrath <bug1@iinet.net.au>
4 * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
5 *
6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
7 */
8
9#include "busybox.h"
10#include "unarchive.h"
11
12#define BUNZIP2_OPT_STDOUT 1
13#define BUNZIP2_OPT_FORCE 2
14
15int bunzip2_main(int argc, char **argv);
16int bunzip2_main(int argc, char **argv)
17{
18 USE_DESKTOP(long long) int status;
19 char *filename;
20 unsigned opt;
21 int src_fd, dst_fd;
22
23 opt = getopt32(argc, argv, "cf");
24
25 /* Set input filename and number */
26 filename = argv[optind];
27 if (filename && NOT_LONE_DASH(filename)) {
28 /* Open input file */
29 src_fd = xopen(filename, O_RDONLY);
30 } else {
31 src_fd = STDIN_FILENO;
32 filename = 0;
33 }
34
35 /* if called as bzcat force the stdout flag */
36 if ((opt & BUNZIP2_OPT_STDOUT) || applet_name[2] == 'c')
37 filename = 0;
38
39 /* Check that the input is sane. */
40 if (isatty(src_fd) && (opt & BUNZIP2_OPT_FORCE) == 0) {
41 bb_error_msg_and_die("compressed data not read from terminal, "
42 "use -f to force it");
43 }
44
45 if (filename) {
46 struct stat stat_buf;
47 /* extension = filename+strlen(filename)-4 is buggy:
48 * strlen may be less than 4 */
49 char *extension = strrchr(filename, '.');
50 if (!extension || strcmp(extension, ".bz2") != 0) {
51 bb_error_msg_and_die("invalid extension");
52 }
53 xstat(filename, &stat_buf);
54 *extension = '\0';
55 dst_fd = xopen3(filename, O_WRONLY | O_CREAT | O_TRUNC,
56 stat_buf.st_mode);
57 } else dst_fd = STDOUT_FILENO;
58 status = uncompressStream(src_fd, dst_fd);
59 if (filename) {
60 if (status >= 0) filename[strlen(filename)] = '.';
61 if (unlink(filename) < 0) {
62 bb_error_msg_and_die("cannot remove %s", filename);
63 }
64 }
65
66 return status;
67}
diff --git a/archival/gunzip.c b/archival/gunzip.c
deleted file mode 100644
index 3d99fe506..000000000
--- a/archival/gunzip.c
+++ /dev/null
@@ -1,163 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Gzip implementation for busybox
4 *
5 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
6 *
7 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
8 * based on gzip sources
9 *
10 * Adjusted further by Erik Andersen <andersen@codepoet.org> to support files as
11 * well as stdin/stdout, and to generally behave itself wrt command line
12 * handling.
13 *
14 * General cleanup to better adhere to the style guide and make use of standard
15 * busybox functions by Glenn McGrath <bug1@iinet.net.au>
16 *
17 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
18 *
19 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
20 * Copyright (C) 1992-1993 Jean-loup Gailly
21 * The unzip code was written and put in the public domain by Mark Adler.
22 * Portions of the lzw code are derived from the public domain 'compress'
23 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
24 * Ken Turkowski, Dave Mack and Peter Jannesen.
25 *
26 * See the license_msg below and the file COPYING for the software license.
27 * See the file algorithm.doc for the compression algorithms and file formats.
28 */
29
30#include "busybox.h"
31#include "unarchive.h"
32
33#define GUNZIP_OPT_STDOUT 1
34#define GUNZIP_OPT_FORCE 2
35#define GUNZIP_OPT_TEST 4
36#define GUNZIP_OPT_DECOMPRESS 8
37#define GUNZIP_OPT_VERBOSE 0x10
38
39int gunzip_main(int argc, char **argv);
40int gunzip_main(int argc, char **argv)
41{
42 USE_DESKTOP(long long) int status;
43 int exitcode = 0;
44 unsigned opt;
45
46 opt = getopt32(argc, argv, "cftdv");
47 /* if called as zcat */
48 if (strcmp(applet_name, "zcat") == 0) {
49 opt |= GUNZIP_OPT_STDOUT;
50 }
51
52 do {
53 struct stat stat_buf;
54 char *old_path = argv[optind];
55 char *delete_path = NULL;
56 char *new_path = NULL;
57 int src_fd;
58 int dst_fd;
59
60 optind++;
61
62 if (old_path == NULL || LONE_DASH(old_path)) {
63 src_fd = STDIN_FILENO;
64 opt |= GUNZIP_OPT_STDOUT;
65 USE_DESKTOP(opt &= ~GUNZIP_OPT_VERBOSE;)
66 optind = argc; /* we don't handle "gunzip - a.gz b.gz" */
67 } else {
68 src_fd = xopen(old_path, O_RDONLY);
69 /* Get the time stamp on the input file. */
70 fstat(src_fd, &stat_buf);
71 }
72
73 /* Check that the input is sane. */
74 if (isatty(src_fd) && !(opt & GUNZIP_OPT_FORCE)) {
75 bb_error_msg_and_die
76 ("compressed data not read from terminal, use -f to force it");
77 }
78
79 /* Set output filename and number */
80 if (opt & GUNZIP_OPT_TEST) {
81 dst_fd = xopen(bb_dev_null, O_WRONLY); /* why does test use filenum 2 ? */
82 } else if (opt & GUNZIP_OPT_STDOUT) {
83 dst_fd = STDOUT_FILENO;
84 } else {
85 char *extension;
86
87 new_path = xstrdup(old_path);
88
89 extension = strrchr(new_path, '.');
90#ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS
91 if (extension && (strcmp(extension, ".Z") == 0)) {
92 *extension = '\0';
93 } else
94#endif
95 if (extension && (strcmp(extension, ".gz") == 0)) {
96 *extension = '\0';
97 } else if (extension && (strcmp(extension, ".tgz") == 0)) {
98 extension[2] = 'a';
99 extension[3] = 'r';
100 } else {
101 // FIXME: should we die or just skip to next?
102 bb_error_msg_and_die("invalid extension");
103 }
104
105 /* Open output file (with correct permissions) */
106 dst_fd = xopen3(new_path, O_WRONLY | O_CREAT | O_TRUNC,
107 stat_buf.st_mode);
108
109 /* If unzip succeeds remove the old file */
110 delete_path = old_path;
111 }
112
113 status = -1;
114 /* do the decompression, and cleanup */
115 if (xread_char(src_fd) == 0x1f) {
116 unsigned char magic2;
117
118 magic2 = xread_char(src_fd);
119 if (ENABLE_FEATURE_GUNZIP_UNCOMPRESS && magic2 == 0x9d) {
120 status = uncompress(src_fd, dst_fd);
121 } else if (magic2 == 0x8b) {
122 check_header_gzip(src_fd); // FIXME: xfunc? _or_die?
123 status = inflate_gunzip(src_fd, dst_fd);
124 } else {
125 bb_error_msg("invalid magic");
126 exitcode = 1;
127 }
128 if (status < 0) {
129 bb_error_msg("error inflating");
130 exitcode = 1;
131 }
132 else if (ENABLE_DESKTOP && (opt & GUNZIP_OPT_VERBOSE)) {
133 fprintf(stderr, "%s: %u%% - replaced with %s\n",
134 old_path, (unsigned)(stat_buf.st_size*100 / (status+1)), new_path);
135 }
136 } else {
137 bb_error_msg("invalid magic");
138 exitcode = 1;
139 }
140 if (status < 0 && new_path) {
141 /* Unzip failed, remove new path instead of old path */
142 delete_path = new_path;
143 }
144
145 if (dst_fd != STDOUT_FILENO) {
146 close(dst_fd);
147 }
148 if (src_fd != STDIN_FILENO) {
149 close(src_fd);
150 }
151
152 /* delete_path will be NULL if in test mode or from stdin */
153 if (delete_path && (unlink(delete_path) == -1)) {
154 bb_error_msg("cannot remove %s", delete_path);
155 exitcode = 1;
156 }
157
158 free(new_path);
159
160 } while (optind < argc);
161
162 return exitcode;
163}
diff --git a/archival/libunarchive/check_header_gzip.c b/archival/libunarchive/check_header_gzip.c
index 0cfa94454..09cd6a94e 100644
--- a/archival/libunarchive/check_header_gzip.c
+++ b/archival/libunarchive/check_header_gzip.c
@@ -2,12 +2,11 @@
2/* 2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4 */ 4 */
5#include <stdlib.h> 5
6#include <unistd.h>
7#include "libbb.h" 6#include "libbb.h"
8#include "unarchive.h" /* for external decl of check_header_gzip */ 7#include "unarchive.h" /* for external decl of check_header_gzip_or_die */
9 8
10void check_header_gzip(int src_fd) 9void check_header_gzip_or_die(int src_fd)
11{ 10{
12 union { 11 union {
13 unsigned char raw[8]; 12 unsigned char raw[8];
@@ -57,6 +56,4 @@ void check_header_gzip(int src_fd)
57 xread_char(src_fd); 56 xread_char(src_fd);
58 xread_char(src_fd); 57 xread_char(src_fd);
59 } 58 }
60
61 return;
62} 59}
diff --git a/archival/libunarchive/get_header_tar_gz.c b/archival/libunarchive/get_header_tar_gz.c
index 41c02e16a..1f07e4e64 100644
--- a/archival/libunarchive/get_header_tar_gz.c
+++ b/archival/libunarchive/get_header_tar_gz.c
@@ -20,7 +20,7 @@ char get_header_tar_gz(archive_handle_t *archive_handle)
20 bb_error_msg_and_die("invalid gzip magic"); 20 bb_error_msg_and_die("invalid gzip magic");
21 } 21 }
22 22
23 check_header_gzip(archive_handle->src_fd); 23 check_header_gzip_or_die(archive_handle->src_fd);
24 24
25 archive_handle->src_fd = open_transformer(archive_handle->src_fd, inflate_gunzip); 25 archive_handle->src_fd = open_transformer(archive_handle->src_fd, inflate_gunzip);
26 archive_handle->offset = 0; 26 archive_handle->offset = 0;
diff --git a/archival/rpm.c b/archival/rpm.c
index 9ab12df2d..a48dda3bc 100644
--- a/archival/rpm.c
+++ b/archival/rpm.c
@@ -203,7 +203,7 @@ static void extract_cpio_gz(int fd) {
203 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { 203 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
204 bb_error_msg_and_die("invalid gzip magic"); 204 bb_error_msg_and_die("invalid gzip magic");
205 } 205 }
206 check_header_gzip(archive_handle->src_fd); 206 check_header_gzip_or_die(archive_handle->src_fd);
207 xchdir("/"); /* Install RPM's to root */ 207 xchdir("/"); /* Install RPM's to root */
208 208
209 archive_handle->src_fd = open_transformer(archive_handle->src_fd, inflate_gunzip); 209 archive_handle->src_fd = open_transformer(archive_handle->src_fd, inflate_gunzip);
diff --git a/archival/rpm2cpio.c b/archival/rpm2cpio.c
index 7a2e64b86..307d1a0f6 100644
--- a/archival/rpm2cpio.c
+++ b/archival/rpm2cpio.c
@@ -79,7 +79,7 @@ int rpm2cpio_main(int argc, char **argv)
79 bb_error_msg_and_die("invalid gzip magic"); 79 bb_error_msg_and_die("invalid gzip magic");
80 } 80 }
81 81
82 check_header_gzip(rpm_fd); 82 check_header_gzip_or_die(rpm_fd);
83 if (inflate_gunzip(rpm_fd, STDOUT_FILENO) < 0) { 83 if (inflate_gunzip(rpm_fd, STDOUT_FILENO) < 0) {
84 bb_error_msg("error inflating"); 84 bb_error_msg("error inflating");
85 } 85 }
diff --git a/archival/uncompress.c b/archival/uncompress.c
deleted file mode 100644
index b16c353ec..000000000
--- a/archival/uncompress.c
+++ /dev/null
@@ -1,95 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Uncompress applet for busybox (c) 2002 Glenn McGrath
4 *
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
11#define GUNZIP_TO_STDOUT 1
12#define GUNZIP_FORCE 2
13
14int uncompress_main(int argc, char **argv);
15int uncompress_main(int argc, char **argv)
16{
17 int status = EXIT_SUCCESS;
18 unsigned long flags;
19
20 flags = getopt32(argc, argv, "cf");
21
22 while (optind < argc) {
23 char *compressed_file = argv[optind++];
24 char *delete_path = NULL;
25 char *uncompressed_file = NULL;
26 int src_fd;
27 int dst_fd;
28
29 if (LONE_DASH(compressed_file)) {
30 src_fd = STDIN_FILENO;
31 flags |= GUNZIP_TO_STDOUT;
32 } else {
33 src_fd = xopen(compressed_file, O_RDONLY);
34 }
35
36 /* Check that the input is sane. */
37 if (isatty(src_fd) && ((flags & GUNZIP_FORCE) == 0)) {
38 bb_error_msg_and_die
39 ("compressed data not read from terminal. Use -f to force it.");
40 }
41
42 /* Set output filename and number */
43 if (flags & GUNZIP_TO_STDOUT) {
44 dst_fd = STDOUT_FILENO;
45 } else {
46 struct stat stat_buf;
47 char *extension;
48
49 uncompressed_file = xstrdup(compressed_file);
50
51 extension = strrchr(uncompressed_file, '.');
52 if (!extension || (strcmp(extension, ".Z") != 0)) {
53 bb_error_msg_and_die("invalid extension");
54 }
55 *extension = '\0';
56
57 /* Open output file */
58 xstat(compressed_file, &stat_buf);
59 dst_fd = xopen3(uncompressed_file,
60 O_WRONLY | O_CREAT | O_TRUNC,
61 stat_buf.st_mode);
62
63 /* If unzip succeeds remove the old file */
64 delete_path = compressed_file;
65 }
66
67 /* do the decompression, and cleanup */
68 if ((xread_char(src_fd) != 0x1f) || (xread_char(src_fd) != 0x9d)) {
69 bb_error_msg_and_die("invalid magic");
70 }
71
72 status = uncompress(src_fd, dst_fd);
73
74 if ((status != EXIT_SUCCESS) && (uncompressed_file)) {
75 /* Unzip failed, remove the uncomressed file instead of compressed file */
76 delete_path = uncompressed_file;
77 }
78
79 if (dst_fd != STDOUT_FILENO) {
80 close(dst_fd);
81 }
82 if (src_fd != STDIN_FILENO) {
83 close(src_fd);
84 }
85
86 /* delete_path will be NULL if in test mode or from stdin */
87 if (delete_path && (unlink(delete_path) == -1)) {
88 bb_error_msg_and_die("cannot remove %s", delete_path);
89 }
90
91 free(uncompressed_file);
92 }
93
94 return status;
95}
diff --git a/archival/unlzma.c b/archival/unlzma.c
deleted file mode 100644
index 24632c4a7..000000000
--- a/archival/unlzma.c
+++ /dev/null
@@ -1,65 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Small lzma deflate implementation.
4 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
5 *
6 * Based on bunzip.c from busybox
7 *
8 * Licensed under GPL v2, see file LICENSE in this tarball for details.
9 */
10
11/* Why our g[un]zip/bunzip2 are so ugly compared to this beauty? */
12
13#include "busybox.h"
14#include "unarchive.h"
15
16#define UNLZMA_OPT_STDOUT 1
17
18int unlzma_main(int argc, char **argv);
19int unlzma_main(int argc, char **argv)
20{
21 USE_DESKTOP(long long) int status;
22 char *filename;
23 unsigned opt;
24 int src_fd, dst_fd;
25
26 opt = getopt32(argc, argv, "c");
27
28 /* Set input filename and number */
29 filename = argv[optind];
30 if (filename && NOT_LONE_DASH(filename)) {
31 /* Open input file */
32 src_fd = xopen(filename, O_RDONLY);
33 } else {
34 src_fd = STDIN_FILENO;
35 filename = 0;
36 }
37
38 /* if called as lzmacat force the stdout flag */
39 if ((opt & UNLZMA_OPT_STDOUT) || applet_name[4] == 'c')
40 filename = 0;
41
42 if (filename) {
43 struct stat stat_buf;
44 /* bug: char *extension = filename + strlen(filename) - 5; */
45 char *extension = strrchr(filename, '.');
46 if (!extension || strcmp(extension, ".lzma") != 0) {
47 bb_error_msg_and_die("invalid extension");
48 }
49 xstat(filename, &stat_buf);
50 *extension = '\0';
51 dst_fd = xopen3(filename, O_WRONLY | O_CREAT | O_TRUNC,
52 stat_buf.st_mode);
53 } else
54 dst_fd = STDOUT_FILENO;
55 status = unlzma(src_fd, dst_fd);
56 if (filename) {
57 if (status >= 0) /* if success delete src, else delete dst */
58 filename[strlen(filename)] = '.';
59 if (unlink(filename) < 0) {
60 bb_error_msg_and_die("cannot remove %s", filename);
61 }
62 }
63
64 return (status < 0);
65}
diff --git a/include/unarchive.h b/include/unarchive.h
index 5e87d088e..36b56a925 100644
--- a/include/unarchive.h
+++ b/include/unarchive.h
@@ -83,7 +83,7 @@ extern void header_skip(const file_header_t *file_header);
83extern void header_list(const file_header_t *file_header); 83extern void header_list(const file_header_t *file_header);
84extern void header_verbose_list(const file_header_t *file_header); 84extern void header_verbose_list(const file_header_t *file_header);
85 85
86extern void check_header_gzip(int src_fd); 86extern void check_header_gzip_or_die(int src_fd);
87 87
88extern char get_header_ar(archive_handle_t *archive_handle); 88extern char get_header_ar(archive_handle_t *archive_handle);
89extern char get_header_cpio(archive_handle_t *archive_handle); 89extern char get_header_cpio(archive_handle_t *archive_handle);
diff --git a/testsuite/bunzip2.tests b/testsuite/bunzip2.tests
new file mode 100755
index 000000000..83c365d75
--- /dev/null
+++ b/testsuite/bunzip2.tests
@@ -0,0 +1,84 @@
1#!/bin/sh
2
3if test "${0##*/}" = "gunzip.tests"; then
4 unpack=gunzip
5 ext=gz
6elif test "${0##*/}" = "bunzip2.tests"; then
7 unpack=bunzip2
8 ext=bz2
9else
10 echo "WTF? argv0='$0'"
11 exit 1
12fi
13
14bb="busybox "
15
16unset LC_ALL
17unset LC_MESSAGES
18unset LANG
19unset LANGUAGE
20
21hello_gz() {
22 # Gzipped "HELLO\n"
23 #_________________________ vvv vvv vvv vvv - mtime
24 echo -ne "\x1f\x8b\x08\x00\x85\x1d\xef\x45\x02\x03\xf3\x70\xf5\xf1\xf1\xe7"
25 echo -ne "\x02\x00\x6e\xd7\xac\xfd\x06\x00\x00\x00"
26}
27
28hello_bz2() {
29 # Bzipped "HELLO\n"
30 echo -ne "\x42\x5a\x68\x39\x31\x41\x59\x26\x53\x59\x5b\xb8\xe8\xa3\x00\x00"
31 echo -ne "\x01\x44\x00\x00\x10\x02\x44\xa0\x00\x30\xcd\x00\xc3\x46\x29\x97"
32 echo -ne "\x17\x72\x45\x38\x50\x90\x5b\xb8\xe8\xa3"
33}
34
35prep() {
36 rm -f t*
37 hello_$ext >t1.$ext
38 hello_$ext >t2.$ext
39}
40
41check() {
42 eval $2 >t_actual 2>&1
43 if echo -ne "$expected" | cmp - t_actual; then
44 echo "$1: PASS"
45 else
46 echo "$1: FAIL"
47 fi
48}
49
50mkdir testdir 2>/dev/null
51(
52cd testdir || { echo "cannot cd testdir!"; exit 1; }
53
54expected="$unpack: z: No such file or directory
551
56HELLO
57"
58prep; check "$unpack: doesnt exist" "${bb}$unpack z t1.$ext; echo \$?; cat t1"
59
60
61expected="$unpack: t.zz: unknown suffix - ignored
621
63HELLO
64"
65prep; >t.zz; check "$unpack: unknown suffix" "${bb}$unpack t.zz t1.$ext; echo \$?; cat t1"
66
67
68# In this case file "t1" exists, and we skip t1.gz and unpack t2.gz
69expected="$unpack: t1: File exists
701
71HELLO
72"
73prep; >t1; check "$unpack: already exists" "${bb}$unpack t1.$ext t2.$ext; echo \$?; cat t1 t2"
74
75
76# From old testsuite
77expected="HELLO\n0\n"
78prep; check "$unpack: stream unpack" "cat t1.$ext | ${bb}$unpack; echo $?"
79
80expected="ok\n"
81prep; check "$unpack: delete src" "${bb}$unpack t2.$ext; test ! -f t2.$ext && echo ok"
82
83)
84rm -rf testdir \ No newline at end of file
diff --git a/testsuite/bzcat.tests b/testsuite/bzcat.tests
new file mode 100755
index 000000000..16fa3c248
--- /dev/null
+++ b/testsuite/bzcat.tests
@@ -0,0 +1,49 @@
1#!/bin/sh
2
3ext=bz2
4
5bb="busybox "
6
7unset LC_ALL
8unset LC_MESSAGES
9unset LANG
10unset LANGUAGE
11
12hello_gz() {
13 # Gzipped "HELLO\n"
14 #_________________________ vvv vvv vvv vvv - mtime
15 echo -ne "\x1f\x8b\x08\x00\x85\x1d\xef\x45\x02\x03\xf3\x70\xf5\xf1\xf1\xe7"
16 echo -ne "\x02\x00\x6e\xd7\xac\xfd\x06\x00\x00\x00"
17}
18
19hello_bz2() {
20 # Bzipped "HELLO\n"
21 echo -ne "\x42\x5a\x68\x39\x31\x41\x59\x26\x53\x59\x5b\xb8\xe8\xa3\x00\x00"
22 echo -ne "\x01\x44\x00\x00\x10\x02\x44\xa0\x00\x30\xcd\x00\xc3\x46\x29\x97"
23 echo -ne "\x17\x72\x45\x38\x50\x90\x5b\xb8\xe8\xa3"
24}
25
26prep() {
27 rm -f t*
28 hello_$ext >t1.$ext
29 hello_$ext >t2.$ext
30}
31
32check() {
33 eval $2 >t_actual 2>&1
34 if echo -ne "$expected" | cmp - t_actual; then
35 echo "$1: PASS"
36 else
37 echo "$1: FAIL"
38 fi
39}
40
41mkdir testdir 2>/dev/null
42(
43cd testdir || { echo "cannot cd testdir!"; exit 1; }
44
45expected="HELLO\nok\n"
46prep; check "bzcat: dont delete src" "${bb}bzcat t2.bz2; test -f t2.bz2 && echo ok"
47
48)
49rm -rf testdir
diff --git a/testsuite/gunzip.tests b/testsuite/gunzip.tests
new file mode 100755
index 000000000..d7810044f
--- /dev/null
+++ b/testsuite/gunzip.tests
@@ -0,0 +1,3 @@
1#!/bin/sh
2
3. bunzip2.tests