diff --git a/CMakeLists.txt b/CMakeLists.txt index ab401a3..e15fc09 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,11 @@ endif() include_directories(include) set(KABUFUDA_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include CACHE PATH "kabufuda include path" FORCE) +unset(PLAT_SRCS) +if(WIN32) +list(APPEND PLAT_SRCS lib/kabufuda/winsupport.cpp include/kabufuda/winsupport.hpp) +endif() + add_library(kabufuda STATIC include/kabufuda/Constants.hpp include/kabufuda/BlockAllocationTable.hpp lib/kabufuda/BlockAllocationTable.cpp @@ -18,7 +23,8 @@ add_library(kabufuda STATIC include/kabufuda/File.hpp lib/kabufuda/File.cpp include/kabufuda/Util.hpp lib/kabufuda/Util.cpp include/kabufuda/SRAM.hpp lib/kabufuda/SRAM.cpp - include/kabufuda/WideStringConvert.hpp lib/kabufuda/WideStringConvert.cpp) + include/kabufuda/WideStringConvert.hpp lib/kabufuda/WideStringConvert.cpp + ${PLAT_SRCS}) if (NOT TARGET hecl) add_subdirectory(test) 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; +}