memmem.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #ifndef _LIBC
  15. # include <config.h>
  16. #endif
  17. #include <string.h>
  18. #ifndef _LIBC
  19. # define __memmem memmem
  20. #endif
  21. #define RETURN_TYPE void *
  22. #define AVAILABLE(h, h_l, j, n_l) ((j) <= (h_l) - (n_l))
  23. #define FASTSEARCH(S,C,N) (void*) memchr ((void *)(S), (C), (N))
  24. #include "str-two-way.h"
  25. #undef memmem
  26. /* Hash character pairs so a small shift table can be used. All bits of
  27. p[0] are included, but not all bits from p[-1]. So if two equal hashes
  28. match on p[-1], p[0] matches too. Hash collisions are harmless and result
  29. in smaller shifts. */
  30. #define hash2(p) (((size_t)(p)[0] - ((size_t)(p)[-1] << 3)) % sizeof (shift))
  31. /* Fast memmem algorithm with guaranteed linear-time performance.
  32. Small needles up to size 2 use a dedicated linear search. Longer needles
  33. up to size 256 use a novel modified Horspool algorithm. It hashes pairs
  34. of characters to quickly skip past mismatches. The main search loop only
  35. exits if the last 2 characters match, avoiding unnecessary calls to memcmp
  36. and allowing for a larger skip if there is no match. A self-adapting
  37. filtering check is used to quickly detect mismatches in long needles.
  38. By limiting the needle length to 256, the shift table can be reduced to 8
  39. bits per entry, lowering preprocessing overhead and minimizing cache effects.
  40. The limit also implies worst-case performance is linear.
  41. Needles larger than 256 characters use the linear-time Two-Way algorithm. */
  42. void *
  43. __memmem (const void *haystack, size_t hs_len,
  44. const void *needle, size_t ne_len)
  45. {
  46. const unsigned char *hs = (const unsigned char *) haystack;
  47. const unsigned char *ne = (const unsigned char *) needle;
  48. if (ne_len == 0)
  49. return (void *) hs;
  50. if (ne_len == 1)
  51. return (void *) memchr (hs, ne[0], hs_len);
  52. /* Ensure haystack length is >= needle length. */
  53. if (hs_len < ne_len)
  54. return NULL;
  55. const unsigned char *end = hs + hs_len - ne_len;
  56. if (ne_len == 2)
  57. {
  58. uint32_t nw = ne[0] << 16 | ne[1], hw = hs[0] << 16 | hs[1];
  59. for (hs++; hs <= end && hw != nw; )
  60. hw = hw << 16 | *++hs;
  61. return hw == nw ? (void *)hs - 1 : NULL;
  62. }
  63. /* Use Two-Way algorithm for very long needles. */
  64. if (__builtin_expect (ne_len > 256, 0))
  65. return two_way_long_needle (hs, hs_len, ne, ne_len);
  66. uint8_t shift[256];
  67. size_t tmp, shift1;
  68. size_t m1 = ne_len - 1;
  69. size_t offset = 0;
  70. memset (shift, 0, sizeof (shift));
  71. for (int i = 1; i < m1; i++)
  72. shift[hash2 (ne + i)] = i;
  73. /* Shift1 is the amount we can skip after matching the hash of the
  74. needle end but not the full needle. */
  75. shift1 = m1 - shift[hash2 (ne + m1)];
  76. shift[hash2 (ne + m1)] = m1;
  77. for ( ; hs <= end; )
  78. {
  79. /* Skip past character pairs not in the needle. */
  80. do
  81. {
  82. hs += m1;
  83. tmp = shift[hash2 (hs)];
  84. }
  85. while (tmp == 0 && hs <= end);
  86. /* If the match is not at the end of the needle, shift to the end
  87. and continue until we match the hash of the needle end. */
  88. hs -= tmp;
  89. if (tmp < m1)
  90. continue;
  91. /* Hash of the last 2 characters matches. If the needle is long,
  92. try to quickly filter out mismatches. */
  93. if (m1 < 15 || memcmp (hs + offset, ne + offset, 8) == 0)
  94. {
  95. if (memcmp (hs, ne, m1) == 0)
  96. return (void *) hs;
  97. /* Adjust filter offset when it doesn't find the mismatch. */
  98. offset = (offset >= 8 ? offset : m1) - 8;
  99. }
  100. /* Skip based on matching the hash of the needle end. */
  101. hs += shift1;
  102. }
  103. return NULL;
  104. }
  105. libc_hidden_def (__memmem)
  106. static_weak_alias (__memmem, memmem)
  107. libc_hidden_weak (memmem)