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