metaforce/hecl/lib/winsupport.cpp

50 lines
1.2 KiB
C++
Raw Normal View History

2017-12-29 07:56:31 +00:00
#include <cstdlib>
#include <cstdint>
#include <cstring>
2015-08-31 03:36:24 +00:00
#include <string>
2016-03-04 23:02:44 +00:00
#include "hecl/winsupport.hpp"
2015-08-31 03:36:24 +00:00
/*
* 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<const uint8_t*>(haystack);
size_t plen = hlen;
if (!nlen)
return NULL;
needle_first = *(unsigned char *)needle;
while (plen >= nlen && (p = static_cast<const uint8_t*>(memchr(p, needle_first, plen - nlen + 1))))
{
if (!memcmp(p, needle, nlen))
return (void *)p;
p++;
plen = hlen - (p - static_cast<const uint8_t*>(haystack));
}
return NULL;
}
2017-11-25 02:49:50 +00:00
int asprintf(char** buf, const char* format, ...)
{
va_list ap;
va_start(ap, format);
int len = vsnprintf(nullptr, 0, format, ap);
va_end(ap);
*buf = (char*)malloc(len + 1);
va_start(ap, format);
vsnprintf(*buf, len + 1, format, ap);
va_end(ap);
return len;
}