1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
45#define ASIZE 256
46
47#if 0
48#define DEBUGP printk
49#else
50#define DEBUGP(args, format...)
51#endif
52
53struct ts_bm
54{
55 u8 * pattern;
56 unsigned int patlen;
57 unsigned int bad_shift[ASIZE];
58 unsigned int good_shift[0];
59};
60
61static 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
82 DEBUGP("found!\n");
83 return consumed += (shift-(bm->patlen-1));
84
85next: bs = bm->bad_shift[text[shift-i]];
86
87
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
96static 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
114static void compute_prefix_tbl(struct ts_bm *bm)
115{
116 int i, j, g;
117
118 for (i = 0; i < ASIZE; i++)
119 bm->bad_shift[i] = bm->patlen;
120 for (i = 0; i < bm->patlen - 1; i++)
121 bm->bad_shift[bm->pattern[i]] = bm->patlen - 1 - i;
122
123
124
125 bm->good_shift[0] = 1;
126 for (i = 1; i < bm->patlen; i++)
127 bm->good_shift[i] = bm->patlen;
128 for (i = bm->patlen-1, g = 1; i > 0; g++, i--) {
129 for (j = i-1; j >= 1-g ; j--)
130 if (subpattern(bm->pattern, i, j, g)) {
131 bm->good_shift[g] = bm->patlen-j-g;
132 break;
133 }
134 }
135}
136
137static struct ts_config *bm_init(const void *pattern, unsigned int len,
138 gfp_t gfp_mask)
139{
140 struct ts_config *conf;
141 struct ts_bm *bm;
142 unsigned int prefix_tbl_len = len * sizeof(unsigned int);
143 size_t priv_size = sizeof(*bm) + len + prefix_tbl_len;
144
145 conf = alloc_ts_config(priv_size, gfp_mask);
146 if (IS_ERR(conf))
147 return conf;
148
149 bm = ts_config_priv(conf);
150 bm->patlen = len;
151 bm->pattern = (u8 *) bm->good_shift + prefix_tbl_len;
152 memcpy(bm->pattern, pattern, len);
153 compute_prefix_tbl(bm);
154
155 return conf;
156}
157
158static void *bm_get_pattern(struct ts_config *conf)
159{
160 struct ts_bm *bm = ts_config_priv(conf);
161 return bm->pattern;
162}
163
164static unsigned int bm_get_pattern_len(struct ts_config *conf)
165{
166 struct ts_bm *bm = ts_config_priv(conf);
167 return bm->patlen;
168}
169
170static struct ts_ops bm_ops = {
171 .name = "bm",
172 .find = bm_find,
173 .init = bm_init,
174 .get_pattern = bm_get_pattern,
175 .get_pattern_len = bm_get_pattern_len,
176 .owner = THIS_MODULE,
177 .list = LIST_HEAD_INIT(bm_ops.list)
178};
179
180static int __init init_bm(void)
181{
182 return textsearch_register(&bm_ops);
183}
184
185static void __exit exit_bm(void)
186{
187 textsearch_unregister(&bm_ops);
188}
189
190MODULE_LICENSE("GPL");
191
192module_init(init_bm);
193module_exit(exit_bm);
194