From 2a463de413f977cb78998bc71a3f020c3c92bf01 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Wed, 12 Jun 2019 15:33:09 -1000 Subject: [PATCH] Windows build fixes --- include/kabufuda/winsupport.hpp | 25 +++++++++++++++++++++++- lib/kabufuda/winsupport.cpp | 34 --------------------------------- 2 files changed, 24 insertions(+), 35 deletions(-) delete mode 100644 lib/kabufuda/winsupport.cpp diff --git a/include/kabufuda/winsupport.hpp b/include/kabufuda/winsupport.hpp index da9e8e1..b66b9e9 100644 --- a/include/kabufuda/winsupport.hpp +++ b/include/kabufuda/winsupport.hpp @@ -7,5 +7,28 @@ #define NOMINMAX 1 #endif #include "windows.h" +#include -void* memmem(const void* haystack, size_t hlen, const void* needle, size_t nlen); +#ifndef DEF_INLINE_MEMMEM +#define DEF_INLINE_MEMMEM +inline void* memmem(const void* haystack, size_t hlen, const void* needle, size_t nlen) { + int needle_first; + const uint8_t* p = static_cast(haystack); + size_t plen = hlen; + + if (!nlen) + return NULL; + + needle_first = *(unsigned char*)needle; + + while (plen >= nlen && (p = static_cast(memchr(p, needle_first, plen - nlen + 1)))) { + if (!memcmp(p, needle, nlen)) + return (void*)p; + + p++; + plen = hlen - (p - static_cast(haystack)); + } + + return NULL; +} +#endif diff --git a/lib/kabufuda/winsupport.cpp b/lib/kabufuda/winsupport.cpp deleted file mode 100644 index 663feb6..0000000 --- a/lib/kabufuda/winsupport.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include -#include -#include "hecl/winsupport.hpp" - -/* - * The memmem() function finds the start of the first occurrence of the - * substring 'needle' of length 'nlen' in the memory area 'haystack' of - * length 'hlen'. - * - * The return value is a pointer to the beginning of the sub-string, or - * NULL if the substring is not found. - */ -void* memmem(const void* haystack, size_t hlen, const void* needle, size_t nlen) { - int needle_first; - const uint8_t* p = static_cast(haystack); - size_t plen = hlen; - - if (!nlen) - return NULL; - - needle_first = *(unsigned char*)needle; - - while (plen >= nlen && (p = static_cast(memchr(p, needle_first, plen - nlen + 1)))) { - if (!memcmp(p, needle, nlen)) - return (void*)p; - - p++; - plen = hlen - (p - static_cast(haystack)); - } - - return NULL; -}