From b9c796783151678d08b1ec1ef410685b2515ba51 Mon Sep 17 00:00:00 2001 From: Joonwoo Park Date: Tue, 8 Jul 2008 02:37:31 -0700 Subject: textsearch: support for case insensitive searching The function textsearch_prepare has a new flag to support case insensitive searching. Signed-off-by: Joonwoo Park Signed-off-by: Patrick McHardy Signed-off-by: David S. Miller --- lib/textsearch.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/textsearch.c b/lib/textsearch.c index be8bda3862f5..b451fcc9354c 100644 --- a/lib/textsearch.c +++ b/lib/textsearch.c @@ -54,10 +54,13 @@ * USAGE * * Before a search can be performed, a configuration must be created - * by calling textsearch_prepare() specyfing the searching algorithm and - * the pattern to look for. The returned configuration may then be used - * for an arbitary amount of times and even in parallel as long as a - * separate struct ts_state variable is provided to every instance. + * by calling textsearch_prepare() specifying the searching algorithm, + * the pattern to look for and flags. As a flag, you can set TS_IGNORECASE + * to perform case insensitive matching. But it might slow down + * performance of algorithm, so you should use it at own your risk. + * The returned configuration may then be used for an arbitary + * amount of times and even in parallel as long as a separate struct + * ts_state variable is provided to every instance. * * The actual search is performed by either calling textsearch_find_- * continuous() for linear data or by providing an own get_next_block() @@ -89,7 +92,6 @@ * panic("Oh my god, dancing chickens at %d\n", pos); * * textsearch_destroy(conf); - * * ========================================================================== */ @@ -279,7 +281,7 @@ struct ts_config *textsearch_prepare(const char *algo, const void *pattern, if (ops == NULL) goto errout; - conf = ops->init(pattern, len, gfp_mask); + conf = ops->init(pattern, len, gfp_mask, flags); if (IS_ERR(conf)) { err = PTR_ERR(conf); goto errout; -- cgit v1.2.3-59-g8ed1b From 3b76d08190e6b420ed3f5534aa99ee29d82d731f Mon Sep 17 00:00:00 2001 From: Joonwoo Park Date: Tue, 8 Jul 2008 02:37:54 -0700 Subject: textsearch: ts_bm: support case insensitive searching in Boyer-Moore algorithm Add support for case insensitive search to Boyer-Moore algorithm. Signed-off-by: Joonwoo Park Signed-off-by: Patrick McHardy Signed-off-by: David S. Miller --- lib/ts_bm.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/ts_bm.c b/lib/ts_bm.c index 4a7fce72898e..9e66ee4020e9 100644 --- a/lib/ts_bm.c +++ b/lib/ts_bm.c @@ -39,6 +39,7 @@ #include #include #include +#include #include /* Alphabet size, use ASCII */ @@ -64,6 +65,7 @@ static unsigned int bm_find(struct ts_config *conf, struct ts_state *state) unsigned int i, text_len, consumed = state->offset; const u8 *text; int shift = bm->patlen - 1, bs; + const u8 icase = conf->flags & TS_IGNORECASE; for (;;) { text_len = conf->get_next_block(consumed, &text, conf, state); @@ -75,7 +77,9 @@ static unsigned int bm_find(struct ts_config *conf, struct ts_state *state) DEBUGP("Searching in position %d (%c)\n", shift, text[shift]); for (i = 0; i < bm->patlen; i++) - if (text[shift-i] != bm->pattern[bm->patlen-1-i]) + if ((icase ? toupper(text[shift-i]) + : text[shift-i]) + != bm->pattern[bm->patlen-1-i]) goto next; /* London calling... */ @@ -111,14 +115,18 @@ static int subpattern(u8 *pattern, int i, int j, int g) return ret; } -static void compute_prefix_tbl(struct ts_bm *bm) +static void compute_prefix_tbl(struct ts_bm *bm, int flags) { int i, j, g; for (i = 0; i < ASIZE; i++) bm->bad_shift[i] = bm->patlen; - for (i = 0; i < bm->patlen - 1; i++) + for (i = 0; i < bm->patlen - 1; i++) { bm->bad_shift[bm->pattern[i]] = bm->patlen - 1 - i; + if (flags & TS_IGNORECASE) + bm->bad_shift[tolower(bm->pattern[i])] + = bm->patlen - 1 - i; + } /* Compute the good shift array, used to match reocurrences * of a subpattern */ @@ -135,10 +143,11 @@ static void compute_prefix_tbl(struct ts_bm *bm) } static struct ts_config *bm_init(const void *pattern, unsigned int len, - gfp_t gfp_mask) + gfp_t gfp_mask, int flags) { struct ts_config *conf; struct ts_bm *bm; + int i; unsigned int prefix_tbl_len = len * sizeof(unsigned int); size_t priv_size = sizeof(*bm) + len + prefix_tbl_len; @@ -146,11 +155,16 @@ static struct ts_config *bm_init(const void *pattern, unsigned int len, if (IS_ERR(conf)) return conf; + conf->flags = flags; bm = ts_config_priv(conf); bm->patlen = len; bm->pattern = (u8 *) bm->good_shift + prefix_tbl_len; - memcpy(bm->pattern, pattern, len); - compute_prefix_tbl(bm); + if (flags & TS_IGNORECASE) + for (i = 0; i < len; i++) + bm->pattern[i] = toupper(((u8 *)pattern)[i]); + else + memcpy(bm->pattern, pattern, len); + compute_prefix_tbl(bm, flags); return conf; } -- cgit v1.2.3-59-g8ed1b From 2523c3fc2bc9e34c06a71517844d55353f1f904a Mon Sep 17 00:00:00 2001 From: Joonwoo Park Date: Tue, 8 Jul 2008 02:38:09 -0700 Subject: textsearch: ts_kmp: support case insensitive searching in Knuth-Morris-Pratt algorithm Add support for case insensitive search to Knuth-Morris-Pratt algorithm. Signed-off-by: Joonwoo Park Signed-off-by: Patrick McHardy Signed-off-by: David S. Miller --- lib/ts_kmp.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/ts_kmp.c b/lib/ts_kmp.c index 3ced628cab4b..632f783e65f1 100644 --- a/lib/ts_kmp.c +++ b/lib/ts_kmp.c @@ -33,6 +33,7 @@ #include #include #include +#include #include struct ts_kmp @@ -47,6 +48,7 @@ static unsigned int kmp_find(struct ts_config *conf, struct ts_state *state) struct ts_kmp *kmp = ts_config_priv(conf); unsigned int i, q = 0, text_len, consumed = state->offset; const u8 *text; + const int icase = conf->flags & TS_IGNORECASE; for (;;) { text_len = conf->get_next_block(consumed, &text, conf, state); @@ -55,9 +57,11 @@ static unsigned int kmp_find(struct ts_config *conf, struct ts_state *state) break; for (i = 0; i < text_len; i++) { - while (q > 0 && kmp->pattern[q] != text[i]) + while (q > 0 && kmp->pattern[q] + != (icase ? toupper(text[i]) : text[i])) q = kmp->prefix_tbl[q - 1]; - if (kmp->pattern[q] == text[i]) + if (kmp->pattern[q] + == (icase ? toupper(text[i]) : text[i])) q++; if (unlikely(q == kmp->pattern_len)) { state->offset = consumed + i + 1; @@ -72,24 +76,28 @@ static unsigned int kmp_find(struct ts_config *conf, struct ts_state *state) } static inline void compute_prefix_tbl(const u8 *pattern, unsigned int len, - unsigned int *prefix_tbl) + unsigned int *prefix_tbl, int flags) { unsigned int k, q; + const u8 icase = flags & TS_IGNORECASE; for (k = 0, q = 1; q < len; q++) { - while (k > 0 && pattern[k] != pattern[q]) + while (k > 0 && (icase ? toupper(pattern[k]) : pattern[k]) + != (icase ? toupper(pattern[q]) : pattern[q])) k = prefix_tbl[k-1]; - if (pattern[k] == pattern[q]) + if ((icase ? toupper(pattern[k]) : pattern[k]) + == (icase ? toupper(pattern[q]) : pattern[q])) k++; prefix_tbl[q] = k; } } static struct ts_config *kmp_init(const void *pattern, unsigned int len, - gfp_t gfp_mask) + gfp_t gfp_mask, int flags) { struct ts_config *conf; struct ts_kmp *kmp; + int i; unsigned int prefix_tbl_len = len * sizeof(unsigned int); size_t priv_size = sizeof(*kmp) + len + prefix_tbl_len; @@ -97,11 +105,16 @@ static struct ts_config *kmp_init(const void *pattern, unsigned int len, if (IS_ERR(conf)) return conf; + conf->flags = flags; kmp = ts_config_priv(conf); kmp->pattern_len = len; - compute_prefix_tbl(pattern, len, kmp->prefix_tbl); + compute_prefix_tbl(pattern, len, kmp->prefix_tbl, flags); kmp->pattern = (u8 *) kmp->prefix_tbl + prefix_tbl_len; - memcpy(kmp->pattern, pattern, len); + if (flags & TS_IGNORECASE) + for (i = 0; i < len; i++) + kmp->pattern[i] = toupper(((u8 *)pattern)[i]); + else + memcpy(kmp->pattern, pattern, len); return conf; } -- cgit v1.2.3-59-g8ed1b From 43138833ee9af07e865a4dcbfe81684c9c2f2262 Mon Sep 17 00:00:00 2001 From: Joonwoo Park Date: Tue, 8 Jul 2008 02:38:27 -0700 Subject: textsearch: ts_fsm: return error on request for case insensitive search For fsm text search, handle case insensitive parameter as -EINVAL. Signed-off-by: Joonwoo Park Signed-off-by: Patrick McHardy Signed-off-by: David S. Miller --- lib/ts_fsm.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/ts_fsm.c b/lib/ts_fsm.c index af575b61526b..5696a35184e4 100644 --- a/lib/ts_fsm.c +++ b/lib/ts_fsm.c @@ -257,7 +257,7 @@ found_match: } static struct ts_config *fsm_init(const void *pattern, unsigned int len, - gfp_t gfp_mask) + gfp_t gfp_mask, int flags) { int i, err = -EINVAL; struct ts_config *conf; @@ -269,6 +269,9 @@ static struct ts_config *fsm_init(const void *pattern, unsigned int len, if (len % sizeof(struct ts_fsm_token) || ntokens < 1) goto errout; + if (flags & TS_IGNORECASE) + goto errout; + for (i = 0; i < ntokens; i++) { struct ts_fsm_token *t = &tokens[i]; @@ -284,6 +287,7 @@ static struct ts_config *fsm_init(const void *pattern, unsigned int len, if (IS_ERR(conf)) return conf; + conf->flags = flags; fsm = ts_config_priv(conf); fsm->ntokens = ntokens; memcpy(fsm->tokens, pattern, len); -- cgit v1.2.3-59-g8ed1b