Advertisement
CmdEngineer

Untitled

May 22nd, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.69 KB | None | 0 0
  1. /*
  2. * The memmem() function finds the start of the first occurrence of the
  3. * substring 'needle' of length 'nlen' in the memory area 'haystack' of
  4. * length 'hlen'.
  5. *
  6. * The return value is a pointer to the beginning of the sub-string, or
  7. * NULL if the substring is not found.
  8. */
  9. char* memmem(char* haystack, size_t hlen, char* needle, size_t nlen)
  10. {
  11.     int needle_first;
  12.     char *p = haystack;
  13.     size_t plen = hlen;
  14.  
  15.     if (!nlen)
  16.         return NULL;
  17.  
  18.     needle_first = *(unsigned char *)needle;
  19.  
  20.     while (plen >= nlen && (p = memchr(p, needle_first, plen - nlen + 1)) != NULL)
  21.     {
  22.         if (!memcmp(p, needle, nlen))
  23.             return (char*)p;
  24.  
  25.         p++;
  26.         plen = hlen - (p - haystack);
  27.     }
  28.  
  29.     return NULL;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement