GNU Linux-libre 4.14.332-gnu1
[releases.git] / lib / ts_bm.c
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/ctype.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 bs;
68         const u8 icase = conf->flags & TS_IGNORECASE;
69
70         for (;;) {
71                 int shift = bm->patlen - 1;
72
73                 text_len = conf->get_next_block(consumed, &text, conf, state);
74
75                 if (unlikely(text_len == 0))
76                         break;
77
78                 while (shift < text_len) {
79                         DEBUGP("Searching in position %d (%c)\n", 
80                                 shift, text[shift]);
81                         for (i = 0; i < bm->patlen; i++) 
82                                 if ((icase ? toupper(text[shift-i])
83                                     : text[shift-i])
84                                         != bm->pattern[bm->patlen-1-i])
85                                      goto next;
86
87                         /* London calling... */
88                         DEBUGP("found!\n");
89                         return consumed += (shift-(bm->patlen-1));
90
91 next:                   bs = bm->bad_shift[text[shift-i]];
92
93                         /* Now jumping to... */
94                         shift = max_t(int, shift-i+bs, shift+bm->good_shift[i]);
95                 }
96                 consumed += text_len;
97         }
98
99         return UINT_MAX;
100 }
101
102 static int subpattern(u8 *pattern, int i, int j, int g)
103 {
104         int x = i+g-1, y = j+g-1, ret = 0;
105
106         while(pattern[x--] == pattern[y--]) {
107                 if (y < 0) {
108                         ret = 1;
109                         break;
110                 }
111                 if (--g == 0) {
112                         ret = pattern[i-1] != pattern[j-1];
113                         break;
114                 }
115         }
116
117         return ret;
118 }
119
120 static void compute_prefix_tbl(struct ts_bm *bm, int flags)
121 {
122         int i, j, g;
123
124         for (i = 0; i < ASIZE; i++)
125                 bm->bad_shift[i] = bm->patlen;
126         for (i = 0; i < bm->patlen - 1; i++) {
127                 bm->bad_shift[bm->pattern[i]] = bm->patlen - 1 - i;
128                 if (flags & TS_IGNORECASE)
129                         bm->bad_shift[tolower(bm->pattern[i])]
130                             = bm->patlen - 1 - i;
131         }
132
133         /* Compute the good shift array, used to match reocurrences 
134          * of a subpattern */
135         bm->good_shift[0] = 1;
136         for (i = 1; i < bm->patlen; i++)
137                 bm->good_shift[i] = bm->patlen;
138         for (i = bm->patlen-1, g = 1; i > 0; g++, i--) {
139                 for (j = i-1; j >= 1-g ; j--)
140                         if (subpattern(bm->pattern, i, j, g)) {
141                                 bm->good_shift[g] = bm->patlen-j-g;
142                                 break;
143                         }
144         }
145 }
146
147 static struct ts_config *bm_init(const void *pattern, unsigned int len,
148                                  gfp_t gfp_mask, int flags)
149 {
150         struct ts_config *conf;
151         struct ts_bm *bm;
152         int i;
153         unsigned int prefix_tbl_len = len * sizeof(unsigned int);
154         size_t priv_size = sizeof(*bm) + len + prefix_tbl_len;
155
156         conf = alloc_ts_config(priv_size, gfp_mask);
157         if (IS_ERR(conf))
158                 return conf;
159
160         conf->flags = flags;
161         bm = ts_config_priv(conf);
162         bm->patlen = len;
163         bm->pattern = (u8 *) bm->good_shift + prefix_tbl_len;
164         if (flags & TS_IGNORECASE)
165                 for (i = 0; i < len; i++)
166                         bm->pattern[i] = toupper(((u8 *)pattern)[i]);
167         else
168                 memcpy(bm->pattern, pattern, len);
169         compute_prefix_tbl(bm, flags);
170
171         return conf;
172 }
173
174 static void *bm_get_pattern(struct ts_config *conf)
175 {
176         struct ts_bm *bm = ts_config_priv(conf);
177         return bm->pattern;
178 }
179
180 static unsigned int bm_get_pattern_len(struct ts_config *conf)
181 {
182         struct ts_bm *bm = ts_config_priv(conf);
183         return bm->patlen;
184 }
185
186 static struct ts_ops bm_ops = {
187         .name             = "bm",
188         .find             = bm_find,
189         .init             = bm_init,
190         .get_pattern      = bm_get_pattern,
191         .get_pattern_len  = bm_get_pattern_len,
192         .owner            = THIS_MODULE,
193         .list             = LIST_HEAD_INIT(bm_ops.list)
194 };
195
196 static int __init init_bm(void)
197 {
198         return textsearch_register(&bm_ops);
199 }
200
201 static void __exit exit_bm(void)
202 {
203         textsearch_unregister(&bm_ops);
204 }
205
206 MODULE_LICENSE("GPL");
207
208 module_init(init_bm);
209 module_exit(exit_bm);