From 5776a5f8d5bfe83b72dc3d9788e92a2a11b22cd2 Mon Sep 17 00:00:00 2001 From: Ron Yorston <rmy@pobox.com> Date: Tue, 27 Jul 2021 13:09:09 +0100 Subject: 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. --- win32/mingw.c | 24 +++++++++++------------- 1 file 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) */ static int has_exec_format(const char *name) { - int n, sig; - unsigned int offset; + int n; unsigned char buf[1024]; /* special case: skip DLLs, there are thousands of them! */ @@ -436,20 +435,19 @@ static int has_exec_format(const char *name) * the magic from the file command. */ if (buf[0] == 'M' && buf[1] == 'Z') { - offset = (buf[0x19] << 8) + buf[0x18]; + unsigned int offset = (buf[0x19] << 8) | buf[0x18]; if (offset > 0x3f) { - offset = (buf[0x3f] << 24) + (buf[0x3e] << 16) + - (buf[0x3d] << 8) + buf[0x3c]; - if (offset < sizeof(buf)-100) { - if (memcmp(buf+offset, "PE\0\0", 4) == 0) { - sig = (buf[offset+25] << 8) + buf[offset+24]; + offset = (buf[0x3f] << 24) | (buf[0x3e] << 16) | + (buf[0x3d] << 8) | buf[0x3c]; + if (offset + 100 < n) { + unsigned char *ptr = buf + offset; + if (memcmp(ptr, "PE\0\0", 4) == 0) { + unsigned int sig = (ptr[25] << 8) | ptr[24]; if (sig == 0x10b || sig == 0x20b) { - sig = (buf[offset+23] << 8) + buf[offset+22]; - if ((sig & 0x2000) != 0) { - /* DLL */ + sig = (ptr[23] << 8) | ptr[22]; + if ((sig & 0x2000) != 0) // DLL return 0; - } - sig = buf[offset+92]; + sig = ptr[92]; return (sig == 1 || sig == 2 || sig == 3 || sig == 7); } } -- cgit v1.2.3-55-g6feb