Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Pablo Neira Ayuso | 8082e4e | 2005-08-25 16:12:22 -0700 | [diff] [blame] | 2 | /* |
| 3 | * lib/ts_bm.c Boyer-Moore text search implementation |
| 4 | * |
Pablo Neira Ayuso | 8082e4e | 2005-08-25 16:12:22 -0700 | [diff] [blame] | 5 | * Authors: Pablo Neira Ayuso <pablo@eurodev.net> |
| 6 | * |
| 7 | * ========================================================================== |
| 8 | * |
| 9 | * Implements Boyer-Moore string matching algorithm: |
| 10 | * |
| 11 | * [1] A Fast String Searching Algorithm, R.S. Boyer and Moore. |
| 12 | * Communications of the Association for Computing Machinery, |
| 13 | * 20(10), 1977, pp. 762-772. |
Alexander A. Klimov | d89775f | 2020-08-11 18:34:50 -0700 | [diff] [blame] | 14 | * https://www.cs.utexas.edu/users/moore/publications/fstrpos.pdf |
Pablo Neira Ayuso | 8082e4e | 2005-08-25 16:12:22 -0700 | [diff] [blame] | 15 | * |
| 16 | * [2] Handbook of Exact String Matching Algorithms, Thierry Lecroq, 2004 |
| 17 | * http://www-igm.univ-mlv.fr/~lecroq/string/string.pdf |
| 18 | * |
| 19 | * Note: Since Boyer-Moore (BM) performs searches for matchings from right |
| 20 | * to left, it's still possible that a matching could be spread over |
| 21 | * multiple blocks, in that case this algorithm won't find any coincidence. |
| 22 | * |
| 23 | * If you're willing to ensure that such thing won't ever happen, use the |
| 24 | * Knuth-Pratt-Morris (KMP) implementation instead. In conclusion, choose |
| 25 | * the proper string search algorithm depending on your setting. |
| 26 | * |
| 27 | * Say you're using the textsearch infrastructure for filtering, NIDS or |
| 28 | * any similar security focused purpose, then go KMP. Otherwise, if you |
| 29 | * really care about performance, say you're classifying packets to apply |
| 30 | * Quality of Service (QoS) policies, and you don't mind about possible |
| 31 | * matchings spread over multiple fragments, then go BM. |
| 32 | */ |
| 33 | |
Pablo Neira Ayuso | 8082e4e | 2005-08-25 16:12:22 -0700 | [diff] [blame] | 34 | #include <linux/kernel.h> |
| 35 | #include <linux/module.h> |
| 36 | #include <linux/types.h> |
| 37 | #include <linux/string.h> |
Joonwoo Park | 3b76d08 | 2008-07-08 02:37:54 -0700 | [diff] [blame] | 38 | #include <linux/ctype.h> |
Pablo Neira Ayuso | 8082e4e | 2005-08-25 16:12:22 -0700 | [diff] [blame] | 39 | #include <linux/textsearch.h> |
| 40 | |
| 41 | /* Alphabet size, use ASCII */ |
| 42 | #define ASIZE 256 |
| 43 | |
| 44 | #if 0 |
| 45 | #define DEBUGP printk |
| 46 | #else |
| 47 | #define DEBUGP(args, format...) |
| 48 | #endif |
| 49 | |
| 50 | struct ts_bm |
| 51 | { |
| 52 | u8 * pattern; |
| 53 | unsigned int patlen; |
| 54 | unsigned int bad_shift[ASIZE]; |
Gustavo A. R. Silva | c6e2ac3 | 2020-04-06 20:10:00 -0700 | [diff] [blame] | 55 | unsigned int good_shift[]; |
Pablo Neira Ayuso | 8082e4e | 2005-08-25 16:12:22 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | static unsigned int bm_find(struct ts_config *conf, struct ts_state *state) |
| 59 | { |
| 60 | struct ts_bm *bm = ts_config_priv(conf); |
| 61 | unsigned int i, text_len, consumed = state->offset; |
| 62 | const u8 *text; |
Jeremy Sowden | 36e07e8 | 2023-06-19 20:06:57 +0100 | [diff] [blame] | 63 | int bs; |
Joonwoo Park | 3b76d08 | 2008-07-08 02:37:54 -0700 | [diff] [blame] | 64 | const u8 icase = conf->flags & TS_IGNORECASE; |
Pablo Neira Ayuso | 8082e4e | 2005-08-25 16:12:22 -0700 | [diff] [blame] | 65 | |
| 66 | for (;;) { |
Jeremy Sowden | 36e07e8 | 2023-06-19 20:06:57 +0100 | [diff] [blame] | 67 | int shift = bm->patlen - 1; |
| 68 | |
Pablo Neira Ayuso | 8082e4e | 2005-08-25 16:12:22 -0700 | [diff] [blame] | 69 | text_len = conf->get_next_block(consumed, &text, conf, state); |
| 70 | |
| 71 | if (unlikely(text_len == 0)) |
| 72 | break; |
| 73 | |
| 74 | while (shift < text_len) { |
| 75 | DEBUGP("Searching in position %d (%c)\n", |
| 76 | shift, text[shift]); |
| 77 | for (i = 0; i < bm->patlen; i++) |
Joonwoo Park | 3b76d08 | 2008-07-08 02:37:54 -0700 | [diff] [blame] | 78 | if ((icase ? toupper(text[shift-i]) |
| 79 | : text[shift-i]) |
| 80 | != bm->pattern[bm->patlen-1-i]) |
Pablo Neira Ayuso | 8082e4e | 2005-08-25 16:12:22 -0700 | [diff] [blame] | 81 | goto next; |
| 82 | |
| 83 | /* London calling... */ |
| 84 | DEBUGP("found!\n"); |
| 85 | return consumed += (shift-(bm->patlen-1)); |
| 86 | |
| 87 | next: bs = bm->bad_shift[text[shift-i]]; |
| 88 | |
| 89 | /* Now jumping to... */ |
| 90 | shift = max_t(int, shift-i+bs, shift+bm->good_shift[i]); |
| 91 | } |
| 92 | consumed += text_len; |
| 93 | } |
| 94 | |
| 95 | return UINT_MAX; |
| 96 | } |
| 97 | |
Pablo Neira Ayuso | 3f33031 | 2006-02-02 17:15:41 -0800 | [diff] [blame] | 98 | static int subpattern(u8 *pattern, int i, int j, int g) |
| 99 | { |
| 100 | int x = i+g-1, y = j+g-1, ret = 0; |
| 101 | |
| 102 | while(pattern[x--] == pattern[y--]) { |
| 103 | if (y < 0) { |
| 104 | ret = 1; |
| 105 | break; |
| 106 | } |
| 107 | if (--g == 0) { |
| 108 | ret = pattern[i-1] != pattern[j-1]; |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return ret; |
| 114 | } |
| 115 | |
Joonwoo Park | 3b76d08 | 2008-07-08 02:37:54 -0700 | [diff] [blame] | 116 | static void compute_prefix_tbl(struct ts_bm *bm, int flags) |
Pablo Neira Ayuso | 8082e4e | 2005-08-25 16:12:22 -0700 | [diff] [blame] | 117 | { |
Pablo Neira Ayuso | 3f33031 | 2006-02-02 17:15:41 -0800 | [diff] [blame] | 118 | int i, j, g; |
Pablo Neira Ayuso | 8082e4e | 2005-08-25 16:12:22 -0700 | [diff] [blame] | 119 | |
| 120 | for (i = 0; i < ASIZE; i++) |
Michael Rash | 3ffaa8c | 2006-08-22 00:45:22 -0700 | [diff] [blame] | 121 | bm->bad_shift[i] = bm->patlen; |
Joonwoo Park | 3b76d08 | 2008-07-08 02:37:54 -0700 | [diff] [blame] | 122 | for (i = 0; i < bm->patlen - 1; i++) { |
Michael Rash | 3ffaa8c | 2006-08-22 00:45:22 -0700 | [diff] [blame] | 123 | bm->bad_shift[bm->pattern[i]] = bm->patlen - 1 - i; |
Joonwoo Park | 3b76d08 | 2008-07-08 02:37:54 -0700 | [diff] [blame] | 124 | if (flags & TS_IGNORECASE) |
| 125 | bm->bad_shift[tolower(bm->pattern[i])] |
| 126 | = bm->patlen - 1 - i; |
| 127 | } |
Pablo Neira Ayuso | 8082e4e | 2005-08-25 16:12:22 -0700 | [diff] [blame] | 128 | |
| 129 | /* Compute the good shift array, used to match reocurrences |
| 130 | * of a subpattern */ |
Pablo Neira Ayuso | 8082e4e | 2005-08-25 16:12:22 -0700 | [diff] [blame] | 131 | bm->good_shift[0] = 1; |
| 132 | for (i = 1; i < bm->patlen; i++) |
| 133 | bm->good_shift[i] = bm->patlen; |
Pablo Neira Ayuso | 3f33031 | 2006-02-02 17:15:41 -0800 | [diff] [blame] | 134 | for (i = bm->patlen-1, g = 1; i > 0; g++, i--) { |
| 135 | for (j = i-1; j >= 1-g ; j--) |
| 136 | if (subpattern(bm->pattern, i, j, g)) { |
| 137 | bm->good_shift[g] = bm->patlen-j-g; |
| 138 | break; |
| 139 | } |
Pablo Neira Ayuso | 8082e4e | 2005-08-25 16:12:22 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | static struct ts_config *bm_init(const void *pattern, unsigned int len, |
Joonwoo Park | 3b76d08 | 2008-07-08 02:37:54 -0700 | [diff] [blame] | 144 | gfp_t gfp_mask, int flags) |
Pablo Neira Ayuso | 8082e4e | 2005-08-25 16:12:22 -0700 | [diff] [blame] | 145 | { |
| 146 | struct ts_config *conf; |
| 147 | struct ts_bm *bm; |
Joonwoo Park | 3b76d08 | 2008-07-08 02:37:54 -0700 | [diff] [blame] | 148 | int i; |
Pablo Neira Ayuso | 8082e4e | 2005-08-25 16:12:22 -0700 | [diff] [blame] | 149 | unsigned int prefix_tbl_len = len * sizeof(unsigned int); |
| 150 | size_t priv_size = sizeof(*bm) + len + prefix_tbl_len; |
| 151 | |
| 152 | conf = alloc_ts_config(priv_size, gfp_mask); |
| 153 | if (IS_ERR(conf)) |
| 154 | return conf; |
| 155 | |
Joonwoo Park | 3b76d08 | 2008-07-08 02:37:54 -0700 | [diff] [blame] | 156 | conf->flags = flags; |
Pablo Neira Ayuso | 8082e4e | 2005-08-25 16:12:22 -0700 | [diff] [blame] | 157 | bm = ts_config_priv(conf); |
| 158 | bm->patlen = len; |
| 159 | bm->pattern = (u8 *) bm->good_shift + prefix_tbl_len; |
Joonwoo Park | 3b76d08 | 2008-07-08 02:37:54 -0700 | [diff] [blame] | 160 | if (flags & TS_IGNORECASE) |
| 161 | for (i = 0; i < len; i++) |
| 162 | bm->pattern[i] = toupper(((u8 *)pattern)[i]); |
| 163 | else |
| 164 | memcpy(bm->pattern, pattern, len); |
| 165 | compute_prefix_tbl(bm, flags); |
Pablo Neira Ayuso | 8082e4e | 2005-08-25 16:12:22 -0700 | [diff] [blame] | 166 | |
| 167 | return conf; |
| 168 | } |
| 169 | |
| 170 | static void *bm_get_pattern(struct ts_config *conf) |
| 171 | { |
| 172 | struct ts_bm *bm = ts_config_priv(conf); |
| 173 | return bm->pattern; |
| 174 | } |
| 175 | |
| 176 | static unsigned int bm_get_pattern_len(struct ts_config *conf) |
| 177 | { |
| 178 | struct ts_bm *bm = ts_config_priv(conf); |
| 179 | return bm->patlen; |
| 180 | } |
| 181 | |
| 182 | static struct ts_ops bm_ops = { |
| 183 | .name = "bm", |
| 184 | .find = bm_find, |
| 185 | .init = bm_init, |
| 186 | .get_pattern = bm_get_pattern, |
| 187 | .get_pattern_len = bm_get_pattern_len, |
| 188 | .owner = THIS_MODULE, |
| 189 | .list = LIST_HEAD_INIT(bm_ops.list) |
| 190 | }; |
| 191 | |
| 192 | static int __init init_bm(void) |
| 193 | { |
| 194 | return textsearch_register(&bm_ops); |
| 195 | } |
| 196 | |
| 197 | static void __exit exit_bm(void) |
| 198 | { |
| 199 | textsearch_unregister(&bm_ops); |
| 200 | } |
| 201 | |
| 202 | MODULE_LICENSE("GPL"); |
| 203 | |
| 204 | module_init(init_bm); |
| 205 | module_exit(exit_bm); |