diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-06-02 14:14:48 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-06-02 14:14:48 +0200 |
commit | 26b6ccf340243ec3acd594c6928b18e95d3deea9 (patch) | |
tree | 90955d90056adbd8724db164f7cbb3e56833ed8b /libbb | |
parent | abb154ba02f9a9a98465cb1f816f974cca536392 (diff) | |
download | busybox-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>
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/read.c | 21 |
1 files changed, 13 insertions, 8 deletions
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) | |||
312 | void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/) | 312 | void 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 | } |