From 83b78af8033e10e959e7b56c74b1c3c5ee2f8fc5 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Wed, 28 Jul 2021 12:48:21 +0100 Subject: win32: code shrink using is_prefixed_with() Use is_prefixed_with() rather than strncmp() in a few places, and the case-insensitive analogues. Saves 96 bytes in 64-bit build, 192 bytes in 32-bit. --- networking/httpd.c | 5 +++++ shell/ash.c | 8 ++++---- win32/mingw.c | 34 ++++++++++++++++++---------------- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/networking/httpd.c b/networking/httpd.c index fcc49853a..fcd1627b1 100644 --- a/networking/httpd.c +++ b/networking/httpd.c @@ -557,7 +557,12 @@ enum { } while (0) +#if !ENABLE_PLATFORM_MINGW32 #define STRNCASECMP(a, str) strncasecmp((a), (str), sizeof(str)-1) +#else +/* Not exactly equivalent to strncasecmp(), but OK for its use here */ +#define STRNCASECMP(a, str) (!is_prefixed_with_case((a), (str))) +#endif /* Prototypes */ enum { diff --git a/shell/ash.c b/shell/ash.c index 3e02a4e1f..08e34d6e7 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -15269,8 +15269,8 @@ init(void) * because it appears first. */ for (envp = environ; envp && *envp; envp++) { - if (strncasecmp(*envp, "PATH=", 5) == 0 && - strncmp(*envp, "PATH=", 5) != 0) { + if (is_prefixed_with_case(*envp, "PATH=") && + !is_prefixed_with(*envp, "PATH=")) { break; } } @@ -15295,8 +15295,8 @@ init(void) /* Convert backslashes to forward slashes in value but * not if we're on Windows XP or for variables known to * cause problems */ - if ( !winxp && strncmp(*envp, "SYSTEMROOT=", 11) != 0 && - strncmp(*envp, "COMSPEC=", 8) != 0 ) { + if (!winxp && !is_prefixed_with(*envp, "SYSTEMROOT=") && + !is_prefixed_with(*envp, "COMSPEC=")) { bs_to_slash(end+1); } diff --git a/win32/mingw.c b/win32/mingw.c index 1e3cce17a..943202f41 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -171,7 +171,7 @@ static int rand_fd = -1; */ int get_dev_type(const char *filename) { - if (filename && !strncmp(filename, "/dev/", 5)) + if (filename && is_prefixed_with(filename, "/dev/")) return index_in_strings("null\0zero\0urandom\0", filename+5); return NOT_DEVICE; @@ -414,7 +414,8 @@ mode_t mingw_umask(mode_t new_mode) */ static int has_exec_format(const char *name) { - int n; + int n, sig; + unsigned int offset; unsigned char buf[1024]; /* special case: skip DLLs, there are thousands of them! */ @@ -435,19 +436,20 @@ static int has_exec_format(const char *name) * the magic from the file command. */ if (buf[0] == 'M' && buf[1] == 'Z') { - unsigned int offset = (buf[0x19] << 8) | buf[0x18]; + offset = (buf[0x19] << 8) + buf[0x18]; if (offset > 0x3f) { - 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]; + 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]; if (sig == 0x10b || sig == 0x20b) { - sig = (ptr[23] << 8) | ptr[22]; - if ((sig & 0x2000) != 0) // DLL + sig = (buf[offset+23] << 8) + buf[offset+22]; + if ((sig & 0x2000) != 0) { + /* DLL */ return 0; - sig = ptr[92]; + } + sig = buf[offset+92]; return (sig == 1 || sig == 2 || sig == 3 || sig == 7); } } @@ -1230,13 +1232,13 @@ static char *normalize_ntpathA(char *buf) /* fix absolute path prefixes */ if (buf[0] == '\\') { /* strip NT namespace prefixes */ - if (!strncmp(buf, "\\??\\", 4) || - !strncmp(buf, "\\\\?\\", 4)) + if (is_prefixed_with(buf, "\\??\\") || + is_prefixed_with(buf, "\\\\?\\")) buf += 4; - else if (!strncasecmp(buf, "\\DosDevices\\", 12)) + else if (is_prefixed_with_case(buf, "\\DosDevices\\")) buf += 12; /* replace remaining '...UNC\' with '\\' */ - if (!strncasecmp(buf, "UNC\\", 4)) { + if (is_prefixed_with_case(buf, "UNC\\")) { buf += 2; *buf = '\\'; } -- cgit v1.2.3-55-g6feb