utils: Add memrchr(3) replacement for platforms that don't support it
For instance, on Mac OS X memrchr(3) is not provided by the C library.
This commit is contained in:
@@ -103,7 +103,7 @@ void memwipe_noinline(void *ptr, size_t n)
|
||||
*/
|
||||
void *memstr(const void *haystack, const char *needle, size_t n)
|
||||
{
|
||||
unsigned const char *pos = haystack;
|
||||
const u_char *pos = haystack;
|
||||
size_t l;
|
||||
|
||||
if (!haystack || !needle || (l = strlen(needle)) == 0)
|
||||
@@ -120,6 +120,28 @@ void *memstr(const void *haystack, const char *needle, size_t n)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Described in header.
|
||||
*/
|
||||
void *utils_memrchr(const void *s, int c, size_t n)
|
||||
{
|
||||
const u_char *pos;
|
||||
|
||||
if (!s || !n)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (pos = s + n - 1; pos >= (u_char*)s; pos--)
|
||||
{
|
||||
if (*pos == (u_char)c)
|
||||
{
|
||||
return (void*)pos;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Described in header.
|
||||
*/
|
||||
|
||||
@@ -464,6 +464,20 @@ static inline void memwipe(void *ptr, size_t n)
|
||||
*/
|
||||
void *memstr(const void *haystack, const char *needle, size_t n);
|
||||
|
||||
/**
|
||||
* Replacement for memrchr(3) if it is not provided by the C library.
|
||||
*
|
||||
* @param s start of the memory area to search
|
||||
* @param c character to search
|
||||
* @param n length of memory area to search
|
||||
* @return pointer to the found character or NULL
|
||||
*/
|
||||
void *utils_memrchr(const void *s, int c, size_t n);
|
||||
|
||||
#ifndef HAVE_MEMRCHR
|
||||
#define memrchr(s,c,n) utils_memrchr(s,c,n)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Translates the characters in the given string, searching for characters
|
||||
* in 'from' and mapping them to characters in 'to'.
|
||||
|
||||
Reference in New Issue
Block a user