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