From 01dc206b226cccc287d6e1bfb2c1ff619728fc72 Mon Sep 17 00:00:00 2001 From: Ed Maste Date: Sat, 18 Mar 2017 00:53:24 +0000 Subject: [PATCH] libc: add reference to two-way algorithm and bad shift table in memmem/strstr Requested by: ed --- lib/libc/string/memmem.c | 8 ++++++++ lib/libc/string/strstr.c | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/libc/string/memmem.c b/lib/libc/string/memmem.c index d5f74c5bf72b..15ad55913d2b 100644 --- a/lib/libc/string/memmem.c +++ b/lib/libc/string/memmem.c @@ -58,6 +58,14 @@ static char *fourbyte_memmem(const unsigned char *h, size_t k, const unsigned ch #define BITOP(a,b,op) \ ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) +/* + * Two Way string search algorithm, with a bad shift table applied to the last + * byte of the window. A bit array marks which entries in the shift table are + * initialized to avoid fully initializing a 1kb/2kb table. + * + * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching, + * Journal of the ACM 38(3):651-675 + */ static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const unsigned char *n, size_t l) { size_t i, ip, jp, k, p, ms, p0, mem, mem0; diff --git a/lib/libc/string/strstr.c b/lib/libc/string/strstr.c index 6b1ac658ec86..8ed58b81a875 100644 --- a/lib/libc/string/strstr.c +++ b/lib/libc/string/strstr.c @@ -55,6 +55,14 @@ static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n) #define BITOP(a,b,op) \ ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) +/* + * Two Way string search algorithm, with a bad shift table applied to the last + * byte of the window. A bit array marks which entries in the shift table are + * initialized to avoid fully initializing a 1kb/2kb table. + * + * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching, + * Journal of the ACM 38(3):651-675 + */ static char *twoway_strstr(const unsigned char *h, const unsigned char *n) { const unsigned char *z;