diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-11-20 09:37:26 +0100 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-11-20 09:37:26 +0100 |
commit | 534e82cb90447b810a7501df38a67cbd9a6759cd (patch) | |
tree | d4d83494ac5aa4a0d95c0389f75e7e77c6edf0e9 /installer | |
parent | version: bump (diff) | |
download | wireguard-windows-534e82cb90447b810a7501df38a67cbd9a6759cd.tar.xz wireguard-windows-534e82cb90447b810a7501df38a67cbd9a6759cd.zip |
installer: remove memmem
There's only one 'h' in the search string, so the efficiency is about
the same.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'installer')
-rw-r--r-- | installer/customactions.c | 124 |
1 files changed, 8 insertions, 116 deletions
diff --git a/installer/customactions.c b/installer/customactions.c index 96bd4bed..eb75d623 100644 --- a/installer/customactions.c +++ b/installer/customactions.c @@ -122,118 +122,6 @@ out: return ret; } -#define MAX(a, b) ((a) > (b) ? (a) : (b)) -#define BITOP(a, b, op) ((a)[(size_t)(b) / (8 * sizeof *(a))] op(size_t) 1 << ((size_t)(b) % (8 * sizeof *(a)))) - -static char *memmem(const void *h0, size_t k, const void *n0, size_t l) -{ - const unsigned char *n = n0; - const unsigned char *h = h0; - const unsigned char *z = h + k; - size_t i, ip, jp, p, ms, p0, mem, mem0; - size_t byteset[32 / sizeof(size_t)] = { 0 }; - size_t shift[256]; - - /* Computing length of needle and fill shift table */ - for (i = 0; i < l; i++) - BITOP(byteset, n[i], |=), shift[n[i]] = i + 1; - - /* Compute maximal suffix */ - ip = -1; - jp = 0; - k = p = 1; - while (jp + k < l) { - if (n[ip + k] == n[jp + k]) { - if (k == p) { - jp += p; - k = 1; - } else - k++; - } else if (n[ip + k] > n[jp + k]) { - jp += k; - k = 1; - p = jp - ip; - } else { - ip = jp++; - k = p = 1; - } - } - ms = ip; - p0 = p; - - /* And with the opposite comparison */ - ip = -1; - jp = 0; - k = p = 1; - while (jp + k < l) { - if (n[ip + k] == n[jp + k]) { - if (k == p) { - jp += p; - k = 1; - } else - k++; - } else if (n[ip + k] < n[jp + k]) { - jp += k; - k = 1; - p = jp - ip; - } else { - ip = jp++; - k = p = 1; - } - } - if (ip + 1 > ms + 1) - ms = ip; - else - p = p0; - - /* Periodic needle? */ - if (memcmp(n, n + p, ms + 1)) { - mem0 = 0; - p = MAX(ms, l - ms - 1) + 1; - } else - mem0 = l - p; - mem = 0; - - /* Search loop */ - for (;;) { - /* If remainder of haystack is shorter than needle, done */ - if (z - h < l) - return 0; - - /* Check last byte first; advance by shift on mismatch */ - if (BITOP(byteset, h[l - 1], &)) { - k = l - shift[h[l - 1]]; - if (k) { - if (k < mem) - k = mem; - h += k; - mem = 0; - continue; - } - } else { - h += l; - mem = 0; - continue; - } - - /* Compare right half */ - for (k = MAX(ms + 1, mem); k < l && n[k] == h[k]; k++) - ; - if (k < l) { - h += k - ms; - mem = 0; - continue; - } - /* Compare left half */ - for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--) - ; - if (k <= mem) - return (char *)h; - h += p; - mem = mem0; - } -} - extern NTAPI __declspec(dllimport) void RtlGetNtVersionNumbers(DWORD *MajorVersion, DWORD *MinorVersion, DWORD *BuildNumber); __declspec(dllexport) UINT __stdcall CheckKB2921916(MSIHANDLE installer) @@ -246,7 +134,7 @@ __declspec(dllexport) UINT __stdcall CheckKB2921916(MSIHANDLE installer) TCHAR setupapi_path[MAX_PATH]; HANDLE setupapi_handle = INVALID_HANDLE_VALUE; HANDLE setupapi_filemapping = NULL; - void *setupapi_bytes = NULL; + const char *setupapi_bytes = NULL; MEMORY_BASIC_INFORMATION setupapi_meminfo; static const char setupapi_marker[] = "Signature Hash"; @@ -270,9 +158,13 @@ __declspec(dllexport) UINT __stdcall CheckKB2921916(MSIHANDLE installer) goto out; if (VirtualQuery(setupapi_bytes, &setupapi_meminfo, sizeof(setupapi_meminfo)) != sizeof(setupapi_meminfo)) goto out; - if (memmem(setupapi_bytes, setupapi_meminfo.RegionSize, setupapi_marker, strlen(setupapi_marker))) { - ret = ERROR_SUCCESS; - goto out; + if (setupapi_meminfo.RegionSize > strlen(setupapi_marker)) { + for (const char *p = setupapi_bytes + strlen(setupapi_marker) - 1; (p = memchr(p, 'h', setupapi_meminfo.RegionSize - (p - setupapi_bytes))); ++p) { + if (!memcmp(p - strlen(setupapi_marker) + 1, setupapi_marker, strlen(setupapi_marker))) { + ret = ERROR_SUCCESS; + goto out; + } + } } len = _countof(uiLevel); |