memmem.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@internal @file memmem.c
  25. *
  26. * @brief Backup implementation of memmem()
  27. *
  28. * @author Pekka Pessi <Pekka.Pessi@nokia.com>
  29. *
  30. * @date Created: Sat Apr 12 19:32:33 2003 ppessi
  31. *
  32. */
  33. #include "config.h"
  34. #include <string.h>
  35. /* Naive implementation of memmem() */
  36. void *memmem(const void *haystack, size_t haystacklen,
  37. const void *needle, size_t needlelen)
  38. {
  39. size_t i;
  40. char const *hs = haystack;
  41. if (needlelen == 0)
  42. return (void *)haystack;
  43. if (needlelen > haystacklen || haystack == NULL || needle == NULL)
  44. return NULL;
  45. for (i = 0; i <= haystacklen - needlelen; i++) {
  46. if (memcmp(hs + i, needle, needlelen) == 0)
  47. return (void *)(hs + i);
  48. }
  49. return NULL;
  50. }