memmem()
Find the first occurrence of a pattern inside a memory region
Synopsis:
#include <string.h>
void* memmem( void const * const buf,
size_t const buflen,
void const * const pattern,
size_t const patternlen );
Arguments:
- buf
- The buffer to search in.
- buflen
- The length of the buffer.
- pattern
- The pattern to find.
- patternlen
- The length of the pattern.
Library:
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
Description:
The memmem() function finds the first occurence of a specified pattern inside a memory region. This function is similar to strstr(), except that it doesn't assume that the buffers are NUL-terminated strings. It also enables searching within memory regions or with patterns, where either contains embedded NULs.
Returns:
A pointer to the beginning of the match in the buffer, or NULL if a match isn't found.
Examples:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
unsigned char const data[] = { 0x10, 0x34, 0x98, 0xf2, 0x16, 0x9a };
unsigned char const pattern[] = { 0xf2, 0x16 };
unsigned char *found = memmem(data, sizeof(data), pattern, sizeof(pattern));
if (found != NULL)
{
printf("Found pattern at offset %zd\n", found - data);
} else
{
printf("Pattern not found\n");
}
return EXIT_SUCCESS;
}
Classification:
Safety: | |
---|---|
Cancellation point | No |
Signal handler | Yes |
Thread | Yes |