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