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