diff options
author | Ron Yorston <rmy@pobox.com> | 2021-07-27 13:09:09 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2021-07-28 12:28:12 +0100 |
commit | 5776a5f8d5bfe83b72dc3d9788e92a2a11b22cd2 (patch) | |
tree | ec5205ca568ba92f6857544a1cc73ec9ebdd74aa | |
parent | 5c2241c4b32607f138824c3454b7b32b2916fd02 (diff) | |
download | busybox-w32-5776a5f8d5bfe83b72dc3d9788e92a2a11b22cd2.tar.gz busybox-w32-5776a5f8d5bfe83b72dc3d9788e92a2a11b22cd2.tar.bz2 busybox-w32-5776a5f8d5bfe83b72dc3d9788e92a2a11b22cd2.zip |
win32: code shrink has_exec_format()
In the code to detect binaries:
- use '|' rather than '+' to combine bytes;
- fix the test that the PE header is within the buffer;
- once we have the offset to the PE header make a pointer to it;
- cosmetic changes.
Saves 96 bytes.
-rw-r--r-- | win32/mingw.c | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/win32/mingw.c b/win32/mingw.c index 82316e69d..1e3cce17a 100644 --- a/win32/mingw.c +++ b/win32/mingw.c | |||
@@ -414,8 +414,7 @@ mode_t mingw_umask(mode_t new_mode) | |||
414 | */ | 414 | */ |
415 | static int has_exec_format(const char *name) | 415 | static int has_exec_format(const char *name) |
416 | { | 416 | { |
417 | int n, sig; | 417 | int n; |
418 | unsigned int offset; | ||
419 | unsigned char buf[1024]; | 418 | unsigned char buf[1024]; |
420 | 419 | ||
421 | /* special case: skip DLLs, there are thousands of them! */ | 420 | /* special case: skip DLLs, there are thousands of them! */ |
@@ -436,20 +435,19 @@ static int has_exec_format(const char *name) | |||
436 | * the magic from the file command. | 435 | * the magic from the file command. |
437 | */ | 436 | */ |
438 | if (buf[0] == 'M' && buf[1] == 'Z') { | 437 | if (buf[0] == 'M' && buf[1] == 'Z') { |
439 | offset = (buf[0x19] << 8) + buf[0x18]; | 438 | unsigned int offset = (buf[0x19] << 8) | buf[0x18]; |
440 | if (offset > 0x3f) { | 439 | if (offset > 0x3f) { |
441 | offset = (buf[0x3f] << 24) + (buf[0x3e] << 16) + | 440 | offset = (buf[0x3f] << 24) | (buf[0x3e] << 16) | |
442 | (buf[0x3d] << 8) + buf[0x3c]; | 441 | (buf[0x3d] << 8) | buf[0x3c]; |
443 | if (offset < sizeof(buf)-100) { | 442 | if (offset + 100 < n) { |
444 | if (memcmp(buf+offset, "PE\0\0", 4) == 0) { | 443 | unsigned char *ptr = buf + offset; |
445 | sig = (buf[offset+25] << 8) + buf[offset+24]; | 444 | if (memcmp(ptr, "PE\0\0", 4) == 0) { |
445 | unsigned int sig = (ptr[25] << 8) | ptr[24]; | ||
446 | if (sig == 0x10b || sig == 0x20b) { | 446 | if (sig == 0x10b || sig == 0x20b) { |
447 | sig = (buf[offset+23] << 8) + buf[offset+22]; | 447 | sig = (ptr[23] << 8) | ptr[22]; |
448 | if ((sig & 0x2000) != 0) { | 448 | if ((sig & 0x2000) != 0) // DLL |
449 | /* DLL */ | ||
450 | return 0; | 449 | return 0; |
451 | } | 450 | sig = ptr[92]; |
452 | sig = buf[offset+92]; | ||
453 | return (sig == 1 || sig == 2 || sig == 3 || sig == 7); | 451 | return (sig == 1 || sig == 2 || sig == 3 || sig == 7); |
454 | } | 452 | } |
455 | } | 453 | } |