2874c5fd28426 (Thomas Gleixner 2019-05-27 08:55:01 +0200 1) // SPDX-License-Identifier: GPL-2.0-or-later
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 2) /*
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 3) * lib/ts_kmp.c Knuth-Morris-Pratt text search implementation
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 4) *
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 5) * Authors: Thomas Graf <tgraf@suug.ch>
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 6) *
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 7) * ==========================================================================
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 8) *
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 9) * Implements a linear-time string-matching algorithm due to Knuth,
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 10) * Morris, and Pratt [1]. Their algorithm avoids the explicit
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 11) * computation of the transition function DELTA altogether. Its
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 12) * matching time is O(n), for n being length(text), using just an
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 13) * auxiliary function PI[1..m], for m being length(pattern),
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 14) * precomputed from the pattern in time O(m). The array PI allows
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 15) * the transition function DELTA to be computed efficiently
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 16) * "on the fly" as needed. Roughly speaking, for any state
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 17) * "q" = 0,1,...,m and any character "a" in SIGMA, the value
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 18) * PI["q"] contains the information that is independent of "a" and
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 19) * is needed to compute DELTA("q", "a") [2]. Since the array PI
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 20) * has only m entries, whereas DELTA has O(m|SIGMA|) entries, we
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 21) * save a factor of |SIGMA| in the preprocessing time by computing
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 22) * PI rather than DELTA.
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 23) *
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 24) * [1] Cormen, Leiserson, Rivest, Stein
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 25) * Introdcution to Algorithms, 2nd Edition, MIT Press
7433a8d6fa60a (Randy Dunlap 2017-10-20 12:15:52 -0700 26) * [2] See finite automaton theory
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 27) */
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 28)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 29) #include <linux/module.h>
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 30) #include <linux/types.h>
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 31) #include <linux/string.h>
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 32) #include <linux/ctype.h>
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 33) #include <linux/textsearch.h>
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 34)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 35) struct ts_kmp
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 36) {
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 37) u8 * pattern;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 38) unsigned int pattern_len;
51022f8715bbb (Gustavo A. R. Silva 2020-04-06 20:10:06 -0700 39) unsigned int prefix_tbl[];
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 40) };
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 41)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 42) static unsigned int kmp_find(struct ts_config *conf, struct ts_state *state)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 43) {
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 44) struct ts_kmp *kmp = ts_config_priv(conf);
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 45) unsigned int i, q = 0, text_len, consumed = state->offset;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 46) const u8 *text;
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 47) const int icase = conf->flags & TS_IGNORECASE;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 48)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 49) for (;;) {
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 50) text_len = conf->get_next_block(consumed, &text, conf, state);
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 51)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 52) if (unlikely(text_len == 0))
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 53) break;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 54)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 55) for (i = 0; i < text_len; i++) {
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 56) while (q > 0 && kmp->pattern[q]
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 57) != (icase ? toupper(text[i]) : text[i]))
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 58) q = kmp->prefix_tbl[q - 1];
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 59) if (kmp->pattern[q]
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 60) == (icase ? toupper(text[i]) : text[i]))
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 61) q++;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 62) if (unlikely(q == kmp->pattern_len)) {
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 63) state->offset = consumed + i + 1;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 64) return state->offset - kmp->pattern_len;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 65) }
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 66) }
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 67)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 68) consumed += text_len;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 69) }
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 70)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 71) return UINT_MAX;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 72) }
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 73)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 74) static inline void compute_prefix_tbl(const u8 *pattern, unsigned int len,
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 75) unsigned int *prefix_tbl, int flags)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 76) {
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 77) unsigned int k, q;
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 78) const u8 icase = flags & TS_IGNORECASE;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 79)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 80) for (k = 0, q = 1; q < len; q++) {
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 81) while (k > 0 && (icase ? toupper(pattern[k]) : pattern[k])
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 82) != (icase ? toupper(pattern[q]) : pattern[q]))
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 83) k = prefix_tbl[k-1];
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 84) if ((icase ? toupper(pattern[k]) : pattern[k])
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 85) == (icase ? toupper(pattern[q]) : pattern[q]))
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 86) k++;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 87) prefix_tbl[q] = k;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 88) }
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 89) }
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 90)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 91) static struct ts_config *kmp_init(const void *pattern, unsigned int len,
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 92) gfp_t gfp_mask, int flags)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 93) {
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 94) struct ts_config *conf;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 95) struct ts_kmp *kmp;
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 96) int i;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 97) unsigned int prefix_tbl_len = len * sizeof(unsigned int);
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 98) size_t priv_size = sizeof(*kmp) + len + prefix_tbl_len;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 99)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 100) conf = alloc_ts_config(priv_size, gfp_mask);
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 101) if (IS_ERR(conf))
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 102) return conf;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 103)
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 104) conf->flags = flags;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 105) kmp = ts_config_priv(conf);
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 106) kmp->pattern_len = len;
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 107) compute_prefix_tbl(pattern, len, kmp->prefix_tbl, flags);
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 108) kmp->pattern = (u8 *) kmp->prefix_tbl + prefix_tbl_len;
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 109) if (flags & TS_IGNORECASE)
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 110) for (i = 0; i < len; i++)
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 111) kmp->pattern[i] = toupper(((u8 *)pattern)[i]);
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 112) else
2523c3fc2bc9e (Joonwoo Park 2008-07-08 02:38:09 -0700 113) memcpy(kmp->pattern, pattern, len);
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 114)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 115) return conf;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 116) }
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 117)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 118) static void *kmp_get_pattern(struct ts_config *conf)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 119) {
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 120) struct ts_kmp *kmp = ts_config_priv(conf);
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 121) return kmp->pattern;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 122) }
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 123)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 124) static unsigned int kmp_get_pattern_len(struct ts_config *conf)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 125) {
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 126) struct ts_kmp *kmp = ts_config_priv(conf);
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 127) return kmp->pattern_len;
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 128) }
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 129)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 130) static struct ts_ops kmp_ops = {
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 131) .name = "kmp",
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 132) .find = kmp_find,
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 133) .init = kmp_init,
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 134) .get_pattern = kmp_get_pattern,
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 135) .get_pattern_len = kmp_get_pattern_len,
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 136) .owner = THIS_MODULE,
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 137) .list = LIST_HEAD_INIT(kmp_ops.list)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 138) };
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 139)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 140) static int __init init_kmp(void)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 141) {
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 142) return textsearch_register(&kmp_ops);
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 143) }
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 144)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 145) static void __exit exit_kmp(void)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 146) {
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 147) textsearch_unregister(&kmp_ops);
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 148) }
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 149)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 150) MODULE_LICENSE("GPL");
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 151)
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 152) module_init(init_kmp);
df3fb93ad9ec0 (Thomas Graf 2005-06-23 20:58:37 -0700 153) module_exit(exit_kmp);