aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-06-02 14:14:48 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2010-06-02 14:14:48 +0200
commit26b6ccf340243ec3acd594c6928b18e95d3deea9 (patch)
tree90955d90056adbd8724db164f7cbb3e56833ed8b
parentabb154ba02f9a9a98465cb1f816f974cca536392 (diff)
downloadbusybox-w32-26b6ccf340243ec3acd594c6928b18e95d3deea9.tar.gz
busybox-w32-26b6ccf340243ec3acd594c6928b18e95d3deea9.tar.bz2
busybox-w32-26b6ccf340243ec3acd594c6928b18e95d3deea9.zip
*: simplify checks for gz/bz2/xz magic
function old new delta send_tree 360 355 -5 rpm2cpio_main 249 220 -29 setup_unzip_on_fd 150 118 -32 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-66) Total: -66 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--archival/rpm2cpio.c19
-rw-r--r--include/unarchive.h16
-rw-r--r--libbb/read.c21
3 files changed, 41 insertions, 15 deletions
diff --git a/archival/rpm2cpio.c b/archival/rpm2cpio.c
index 598ec8670..1c67dcc6e 100644
--- a/archival/rpm2cpio.c
+++ b/archival/rpm2cpio.c
@@ -68,26 +68,31 @@ int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
68#else 68#else
69 /* BLOAT */ 69 /* BLOAT */
70 { 70 {
71 unsigned char magic[8]; 71 union {
72 uint8_t b[4];
73 uint16_t b16[2];
74 uint32_t b32[1];
75 } magic;
72 IF_DESKTOP(long long) int FAST_FUNC (*unpack)(int src_fd, int dst_fd); 76 IF_DESKTOP(long long) int FAST_FUNC (*unpack)(int src_fd, int dst_fd);
73 77
74 xread(rpm_fd, &magic, 2); 78 xread(rpm_fd, magic.b16, sizeof(magic.b16));
75 if (magic[0] == 0x1f && magic[1] == 0x8b) { 79 if (magic.b16[0] == GZIP_MAGIC) {
76 unpack = unpack_gz_stream; 80 unpack = unpack_gz_stream;
77 } else 81 } else
78 if (ENABLE_FEATURE_SEAMLESS_BZ2 82 if (ENABLE_FEATURE_SEAMLESS_BZ2
79 && magic[0] == 'B' && magic[1] == 'Z' 83 && magic.b16[0] == BZIP2_MAGIC
80 ) { 84 ) {
81 unpack = unpack_bz2_stream; 85 unpack = unpack_bz2_stream;
82 } else 86 } else
83 if (ENABLE_FEATURE_SEAMLESS_XZ 87 if (ENABLE_FEATURE_SEAMLESS_XZ
84 && magic[0] == 0xfd && magic[1] == '7' 88 && magic.b16[0] == XZ_MAGIC1
85 ) { 89 ) {
86 /* .xz signature: 0xfd, '7', 'z', 'X', 'Z', 0x00 */ 90 /* .xz signature: 0xfd, '7', 'z', 'X', 'Z', 0x00 */
87 /* More info at: http://tukaani.org/xz/xz-file-format.txt */ 91 /* More info at: http://tukaani.org/xz/xz-file-format.txt */
88 xread(rpm_fd, magic + 2, 4); 92 xread(rpm_fd, magic.b32, sizeof(magic.b32));
89 if (strcmp((char*)magic + 2, "zXZ") != 0) 93 if (magic.b32[0] != XZ_MAGIC2)
90 goto no_magic; 94 goto no_magic;
95 /* unpack_xz_stream wants fd at position 0 */
91 xlseek(rpm_fd, -6, SEEK_CUR); 96 xlseek(rpm_fd, -6, SEEK_CUR);
92 unpack = unpack_xz_stream; 97 unpack = unpack_xz_stream;
93 } else { 98 } else {
diff --git a/include/unarchive.h b/include/unarchive.h
index aa7ecec55..8009de282 100644
--- a/include/unarchive.h
+++ b/include/unarchive.h
@@ -4,6 +4,22 @@
4 4
5PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN 5PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
6 6
7enum {
8#if BB_BIG_ENDIAN
9 COMPRESS_MAGIC = 0x1f9d,
10 GZIP_MAGIC = 0x1f8b,
11 BZIP2_MAGIC = ('B'<<8) + 'Z',
12 XZ_MAGIC1 = (0xfd<<8) + '7',
13 XZ_MAGIC2 = ((((('z'<<8) + 'X')<<8) + 'Z')<<8) + 0,
14#else
15 COMPRESS_MAGIC = 0x9d1f,
16 GZIP_MAGIC = 0x8b1f,
17 BZIP2_MAGIC = ('Z'<<8) + 'B',
18 XZ_MAGIC1 = ('7'<<8) + 0xfd,
19 XZ_MAGIC2 = (((((0<<8) + 'Z')<<8) + 'X')<<8) + 'z',
20#endif
21};
22
7typedef struct file_header_t { 23typedef struct file_header_t {
8 char *name; 24 char *name;
9 char *link_target; 25 char *link_target;
diff --git a/libbb/read.c b/libbb/read.c
index cd6bbeb13..b1eb3f24b 100644
--- a/libbb/read.c
+++ b/libbb/read.c
@@ -312,7 +312,11 @@ void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p)
312void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/) 312void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/)
313{ 313{
314 const int fail_if_not_detected = 1; 314 const int fail_if_not_detected = 1;
315 unsigned char magic[8]; 315 union {
316 uint8_t b[4];
317 uint16_t b16[2];
318 uint32_t b32[1];
319 } magic;
316 int offset = -2; 320 int offset = -2;
317# if BB_MMU 321# if BB_MMU
318 IF_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd); 322 IF_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd);
@@ -324,9 +328,9 @@ void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/)
324 328
325 /* .gz and .bz2 both have 2-byte signature, and their 329 /* .gz and .bz2 both have 2-byte signature, and their
326 * unpack_XXX_stream wants this header skipped. */ 330 * unpack_XXX_stream wants this header skipped. */
327 xread(fd, magic, 2); 331 xread(fd, magic.b16, sizeof(magic.b16));
328 if (ENABLE_FEATURE_SEAMLESS_GZ 332 if (ENABLE_FEATURE_SEAMLESS_GZ
329 && magic[0] == 0x1f && magic[1] == 0x8b 333 && magic.b16[0] == GZIP_MAGIC
330 ) { 334 ) {
331# if BB_MMU 335# if BB_MMU
332 xformer = unpack_gz_stream; 336 xformer = unpack_gz_stream;
@@ -336,7 +340,7 @@ void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/)
336 goto found_magic; 340 goto found_magic;
337 } 341 }
338 if (ENABLE_FEATURE_SEAMLESS_BZ2 342 if (ENABLE_FEATURE_SEAMLESS_BZ2
339 && magic[0] == 'B' && magic[1] == 'Z' 343 && magic.b16[0] == BZIP2_MAGIC
340 ) { 344 ) {
341# if BB_MMU 345# if BB_MMU
342 xformer = unpack_bz2_stream; 346 xformer = unpack_bz2_stream;
@@ -346,19 +350,20 @@ void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/)
346 goto found_magic; 350 goto found_magic;
347 } 351 }
348 if (ENABLE_FEATURE_SEAMLESS_XZ 352 if (ENABLE_FEATURE_SEAMLESS_XZ
349 && magic[0] == 0xfd && magic[1] == '7' 353 && magic.b16[0] == XZ_MAGIC1
350 ) { 354 ) {
351 /* .xz signature: 0xfd, '7', 'z', 'X', 'Z', 0x00 */ 355 /* .xz signature: 0xfd, '7', 'z', 'X', 'Z', 0x00 */
352 /* More info at: http://tukaani.org/xz/xz-file-format.txt */ 356 /* More info at: http://tukaani.org/xz/xz-file-format.txt */
353 offset = -6; 357 offset = -6;
354 xread(fd, magic + 2, 4); 358 xread(fd, magic.b32, sizeof(magic.b32));
355 if (strcmp((char*)magic + 2, "zXZ") == 0) { 359 if (magic.b32[0] == XZ_MAGIC2) {
356# if BB_MMU 360# if BB_MMU
357 xformer = unpack_xz_stream; 361 xformer = unpack_xz_stream;
362 /* unpack_xz_stream wants fd at position 0 */
363 xlseek(fd, offset, SEEK_CUR);
358# else 364# else
359 xformer_prog = "unxz"; 365 xformer_prog = "unxz";
360# endif 366# endif
361 xlseek(fd, offset, SEEK_CUR);
362 goto found_magic; 367 goto found_magic;
363 } 368 }
364 } 369 }