xref: /openbmc/linux/lib/ts_bm.c (revision d5cb9783536a41df9f9cba5b0a1d78047ed787f7)
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/config.h>
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/types.h>
42 #include <linux/string.h>
43 #include <linux/textsearch.h>
44 
45 /* Alphabet size, use ASCII */
46 #define ASIZE 256
47 
48 #if 0
49 #define DEBUGP printk
50 #else
51 #define DEBUGP(args, format...)
52 #endif
53 
54 struct ts_bm
55 {
56 	u8 *		pattern;
57 	unsigned int	patlen;
58 	unsigned int 	bad_shift[ASIZE];
59 	unsigned int	good_shift[0];
60 };
61 
62 static unsigned int bm_find(struct ts_config *conf, struct ts_state *state)
63 {
64 	struct ts_bm *bm = ts_config_priv(conf);
65 	unsigned int i, text_len, consumed = state->offset;
66 	const u8 *text;
67 	int shift = bm->patlen, bs;
68 
69 	for (;;) {
70 		text_len = conf->get_next_block(consumed, &text, conf, state);
71 
72 		if (unlikely(text_len == 0))
73 			break;
74 
75 		while (shift < text_len) {
76 			DEBUGP("Searching in position %d (%c)\n",
77 				shift, text[shift]);
78 			for (i = 0; i < bm->patlen; i++)
79 			     if (text[shift-i] != bm->pattern[bm->patlen-1-i])
80 				     goto next;
81 
82 			/* London calling... */
83 			DEBUGP("found!\n");
84 			return consumed += (shift-(bm->patlen-1));
85 
86 next:			bs = bm->bad_shift[text[shift-i]];
87 
88 			/* Now jumping to... */
89 			shift = max_t(int, shift-i+bs, shift+bm->good_shift[i]);
90 		}
91 		consumed += text_len;
92 	}
93 
94 	return UINT_MAX;
95 }
96 
97 static void compute_prefix_tbl(struct ts_bm *bm, const u8 *pattern,
98 			       unsigned int len)
99 {
100 	int i, j, ended, l[ASIZE];
101 
102 	for (i = 0; i < ASIZE; i++)
103 		bm->bad_shift[i] = len;
104 	for (i = 0; i < len - 1; i++)
105 		bm->bad_shift[pattern[i]] = len - 1 - i;
106 
107 	/* Compute the good shift array, used to match reocurrences
108 	 * of a subpattern */
109 	for (i = 1; i < bm->patlen; i++) {
110 		for (j = 0; j < bm->patlen && bm->pattern[bm->patlen - 1 - j]
111 				== bm->pattern[bm->patlen - 1 - i - j]; j++);
112 		l[i] = j;
113 	}
114 
115 	bm->good_shift[0] = 1;
116 	for (i = 1; i < bm->patlen; i++)
117 		bm->good_shift[i] = bm->patlen;
118 	for (i = bm->patlen - 1; i > 0; i--)
119 		bm->good_shift[l[i]] = i;
120 	ended = 0;
121 	for (i = 0; i < bm->patlen; i++) {
122 		if (l[i] == bm->patlen - 1 - i)
123 			ended = i;
124 		if (ended)
125 			bm->good_shift[i] = ended;
126 	}
127 }
128 
129 static struct ts_config *bm_init(const void *pattern, unsigned int len,
130 				 gfp_t gfp_mask)
131 {
132 	struct ts_config *conf;
133 	struct ts_bm *bm;
134 	unsigned int prefix_tbl_len = len * sizeof(unsigned int);
135 	size_t priv_size = sizeof(*bm) + len + prefix_tbl_len;
136 
137 	conf = alloc_ts_config(priv_size, gfp_mask);
138 	if (IS_ERR(conf))
139 		return conf;
140 
141 	bm = ts_config_priv(conf);
142 	bm->patlen = len;
143 	bm->pattern = (u8 *) bm->good_shift + prefix_tbl_len;
144 	compute_prefix_tbl(bm, pattern, len);
145 	memcpy(bm->pattern, pattern, len);
146 
147 	return conf;
148 }
149 
150 static void *bm_get_pattern(struct ts_config *conf)
151 {
152 	struct ts_bm *bm = ts_config_priv(conf);
153 	return bm->pattern;
154 }
155 
156 static unsigned int bm_get_pattern_len(struct ts_config *conf)
157 {
158 	struct ts_bm *bm = ts_config_priv(conf);
159 	return bm->patlen;
160 }
161 
162 static struct ts_ops bm_ops = {
163 	.name		  = "bm",
164 	.find		  = bm_find,
165 	.init		  = bm_init,
166 	.get_pattern	  = bm_get_pattern,
167 	.get_pattern_len  = bm_get_pattern_len,
168 	.owner		  = THIS_MODULE,
169 	.list		  = LIST_HEAD_INIT(bm_ops.list)
170 };
171 
172 static int __init init_bm(void)
173 {
174 	return textsearch_register(&bm_ops);
175 }
176 
177 static void __exit exit_bm(void)
178 {
179 	textsearch_unregister(&bm_ops);
180 }
181 
182 MODULE_LICENSE("GPL");
183 
184 module_init(init_bm);
185 module_exit(exit_bm);
186