From c75c6c74c542b28d1d5489cfd3fa9008c8b8c028 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Thu, 30 Jun 2016 09:40:35 -1000 Subject: [PATCH] Add winsupport from hecl --- include/kabufuda/winsupport.hpp | 14 +++++++++++++ lib/kabufuda/winsupport.cpp | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 include/kabufuda/winsupport.hpp create mode 100644 lib/kabufuda/winsupport.cpp diff --git a/include/kabufuda/winsupport.hpp b/include/kabufuda/winsupport.hpp new file mode 100644 index 0000000..1371532 --- /dev/null +++ b/include/kabufuda/winsupport.hpp @@ -0,0 +1,14 @@ +#ifndef _HECL_WINSUPPORT_H_ +#define _HECL_WINSUPPORT_H_ + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN 1 +#endif +#ifndef NOMINMAX +#define NOMINMAX 1 +#endif +#include "windows.h" + +void* memmem(const void *haystack, size_t hlen, const void *needle, size_t nlen); + +#endif // _HECL_WINSUPPORT_H_ diff --git a/lib/kabufuda/winsupport.cpp b/lib/kabufuda/winsupport.cpp new file mode 100644 index 0000000..e004d9a --- /dev/null +++ b/lib/kabufuda/winsupport.cpp @@ -0,0 +1,36 @@ +#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; +}