blob: 0b79908dfe892e5bc635408538a8049c844a2888 [file] [log] [blame]
Thomas Graf2de4ff72005-06-23 20:49:30 -07001/*
2 * lib/textsearch.c Generic text search interface
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: Thomas Graf <tgraf@suug.ch>
Pablo Neira Ayusoe03ba842007-12-01 00:03:52 +110010 * Pablo Neira Ayuso <pablo@netfilter.org>
Thomas Graf2de4ff72005-06-23 20:49:30 -070011 *
12 * ==========================================================================
13 *
14 * INTRODUCTION
15 *
Jesper Dangaard Brouerde0368d2011-01-24 02:41:37 +000016 * The textsearch infrastructure provides text searching facilities for
Thomas Graf2de4ff72005-06-23 20:49:30 -070017 * both linear and non-linear data. Individual search algorithms are
18 * implemented in modules and chosen by the user.
19 *
20 * ARCHITECTURE
21 *
22 * User
23 * +----------------+
24 * | finish()|<--------------(6)-----------------+
25 * |get_next_block()|<--------------(5)---------------+ |
26 * | | Algorithm | |
27 * | | +------------------------------+
28 * | | | init() find() destroy() |
29 * | | +------------------------------+
30 * | | Core API ^ ^ ^
31 * | | +---------------+ (2) (4) (8)
32 * | (1)|----->| prepare() |---+ | |
33 * | (3)|----->| find()/next() |-----------+ |
34 * | (7)|----->| destroy() |----------------------+
35 * +----------------+ +---------------+
36 *
37 * (1) User configures a search by calling _prepare() specifying the
38 * search parameters such as the pattern and algorithm name.
39 * (2) Core requests the algorithm to allocate and initialize a search
40 * configuration according to the specified parameters.
41 * (3) User starts the search(es) by calling _find() or _next() to
42 * fetch subsequent occurrences. A state variable is provided
Robert P. J. Dayd08df602007-02-17 19:07:33 +010043 * to the algorithm to store persistent variables.
Thomas Graf2de4ff72005-06-23 20:49:30 -070044 * (4) Core eventually resets the search offset and forwards the find()
45 * request to the algorithm.
Jesper Dangaard Brouerde0368d2011-01-24 02:41:37 +000046 * (5) Algorithm calls get_next_block() provided by the user continuously
Thomas Graf2de4ff72005-06-23 20:49:30 -070047 * to fetch the data to be searched in block by block.
48 * (6) Algorithm invokes finish() after the last call to get_next_block
49 * to clean up any leftovers from get_next_block. (Optional)
50 * (7) User destroys the configuration by calling _destroy().
51 * (8) Core notifies the algorithm to destroy algorithm specific
52 * allocations. (Optional)
53 *
54 * USAGE
55 *
56 * Before a search can be performed, a configuration must be created
Joonwoo Parkb9c79672008-07-08 02:37:31 -070057 * by calling textsearch_prepare() specifying the searching algorithm,
58 * the pattern to look for and flags. As a flag, you can set TS_IGNORECASE
59 * to perform case insensitive matching. But it might slow down
60 * performance of algorithm, so you should use it at own your risk.
Jesper Dangaard Brouerde0368d2011-01-24 02:41:37 +000061 * The returned configuration may then be used for an arbitrary
Joonwoo Parkb9c79672008-07-08 02:37:31 -070062 * amount of times and even in parallel as long as a separate struct
63 * ts_state variable is provided to every instance.
Thomas Graf2de4ff72005-06-23 20:49:30 -070064 *
65 * The actual search is performed by either calling textsearch_find_-
66 * continuous() for linear data or by providing an own get_next_block()
67 * implementation and calling textsearch_find(). Both functions return
Jesper Dangaard Brouerde0368d2011-01-24 02:41:37 +000068 * the position of the first occurrence of the pattern or UINT_MAX if
69 * no match was found. Subsequent occurrences can be found by calling
Thomas Graf2de4ff72005-06-23 20:49:30 -070070 * textsearch_next() regardless of the linearity of the data.
71 *
72 * Once you're done using a configuration it must be given back via
73 * textsearch_destroy.
74 *
75 * EXAMPLE
76 *
77 * int pos;
78 * struct ts_config *conf;
79 * struct ts_state state;
80 * const char *pattern = "chicken";
81 * const char *example = "We dance the funky chicken";
82 *
83 * conf = textsearch_prepare("kmp", pattern, strlen(pattern),
84 * GFP_KERNEL, TS_AUTOLOAD);
85 * if (IS_ERR(conf)) {
86 * err = PTR_ERR(conf);
87 * goto errout;
88 * }
89 *
90 * pos = textsearch_find_continuous(conf, &state, example, strlen(example));
91 * if (pos != UINT_MAX)
92 * panic("Oh my god, dancing chickens at %d\n", pos);
93 *
94 * textsearch_destroy(conf);
Thomas Graf2de4ff72005-06-23 20:49:30 -070095 * ==========================================================================
96 */
97
Thomas Graf2de4ff72005-06-23 20:49:30 -070098#include <linux/module.h>
99#include <linux/types.h>
100#include <linux/string.h>
101#include <linux/init.h>
Franck Bui-Huu82524742008-05-12 21:21:05 +0200102#include <linux/rculist.h>
Thomas Graf2de4ff72005-06-23 20:49:30 -0700103#include <linux/rcupdate.h>
104#include <linux/err.h>
105#include <linux/textsearch.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +0900106#include <linux/slab.h>
Thomas Graf2de4ff72005-06-23 20:49:30 -0700107
108static LIST_HEAD(ts_ops);
109static DEFINE_SPINLOCK(ts_mod_lock);
110
111static inline struct ts_ops *lookup_ts_algo(const char *name)
112{
113 struct ts_ops *o;
114
115 rcu_read_lock();
116 list_for_each_entry_rcu(o, &ts_ops, list) {
117 if (!strcmp(name, o->name)) {
118 if (!try_module_get(o->owner))
119 o = NULL;
120 rcu_read_unlock();
121 return o;
122 }
123 }
124 rcu_read_unlock();
125
126 return NULL;
127}
128
129/**
130 * textsearch_register - register a textsearch module
131 * @ops: operations lookup table
132 *
133 * This function must be called by textsearch modules to announce
134 * their presence. The specified &@ops must have %name set to a
135 * unique identifier and the callbacks find(), init(), get_pattern(),
136 * and get_pattern_len() must be implemented.
137 *
138 * Returns 0 or -EEXISTS if another module has already registered
139 * with same name.
140 */
141int textsearch_register(struct ts_ops *ops)
142{
143 int err = -EEXIST;
144 struct ts_ops *o;
145
146 if (ops->name == NULL || ops->find == NULL || ops->init == NULL ||
147 ops->get_pattern == NULL || ops->get_pattern_len == NULL)
148 return -EINVAL;
149
150 spin_lock(&ts_mod_lock);
151 list_for_each_entry(o, &ts_ops, list) {
152 if (!strcmp(ops->name, o->name))
153 goto errout;
154 }
155
156 list_add_tail_rcu(&ops->list, &ts_ops);
157 err = 0;
158errout:
159 spin_unlock(&ts_mod_lock);
160 return err;
161}
Fabian Frederickce643a32014-06-04 16:11:58 -0700162EXPORT_SYMBOL(textsearch_register);
Thomas Graf2de4ff72005-06-23 20:49:30 -0700163
164/**
165 * textsearch_unregister - unregister a textsearch module
166 * @ops: operations lookup table
167 *
168 * This function must be called by textsearch modules to announce
169 * their disappearance for examples when the module gets unloaded.
170 * The &ops parameter must be the same as the one during the
171 * registration.
172 *
173 * Returns 0 on success or -ENOENT if no matching textsearch
174 * registration was found.
175 */
176int textsearch_unregister(struct ts_ops *ops)
177{
178 int err = 0;
179 struct ts_ops *o;
180
181 spin_lock(&ts_mod_lock);
182 list_for_each_entry(o, &ts_ops, list) {
183 if (o == ops) {
184 list_del_rcu(&o->list);
185 goto out;
186 }
187 }
188
189 err = -ENOENT;
190out:
191 spin_unlock(&ts_mod_lock);
192 return err;
193}
Fabian Frederickce643a32014-06-04 16:11:58 -0700194EXPORT_SYMBOL(textsearch_unregister);
Thomas Graf2de4ff72005-06-23 20:49:30 -0700195
196struct ts_linear_state
197{
198 unsigned int len;
199 const void *data;
200};
201
202static unsigned int get_linear_data(unsigned int consumed, const u8 **dst,
203 struct ts_config *conf,
204 struct ts_state *state)
205{
206 struct ts_linear_state *st = (struct ts_linear_state *) state->cb;
207
208 if (likely(consumed < st->len)) {
209 *dst = st->data + consumed;
210 return st->len - consumed;
211 }
212
213 return 0;
214}
215
216/**
217 * textsearch_find_continuous - search a pattern in continuous/linear data
218 * @conf: search configuration
219 * @state: search state
220 * @data: data to search in
221 * @len: length of data
222 *
223 * A simplified version of textsearch_find() for continuous/linear data.
224 * Call textsearch_next() to retrieve subsequent matches.
225 *
226 * Returns the position of first occurrence of the pattern or
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800227 * %UINT_MAX if no occurrence was found.
Thomas Graf2de4ff72005-06-23 20:49:30 -0700228 */
229unsigned int textsearch_find_continuous(struct ts_config *conf,
230 struct ts_state *state,
231 const void *data, unsigned int len)
232{
233 struct ts_linear_state *st = (struct ts_linear_state *) state->cb;
234
235 conf->get_next_block = get_linear_data;
236 st->data = data;
237 st->len = len;
238
239 return textsearch_find(conf, state);
240}
Fabian Frederickce643a32014-06-04 16:11:58 -0700241EXPORT_SYMBOL(textsearch_find_continuous);
Thomas Graf2de4ff72005-06-23 20:49:30 -0700242
243/**
244 * textsearch_prepare - Prepare a search
245 * @algo: name of search algorithm
246 * @pattern: pattern data
247 * @len: length of pattern
248 * @gfp_mask: allocation mask
249 * @flags: search flags
250 *
251 * Looks up the search algorithm module and creates a new textsearch
Raphael Silvafec22902014-10-13 15:51:34 -0700252 * configuration for the specified pattern.
Thomas Graf2de4ff72005-06-23 20:49:30 -0700253 *
254 * Note: The format of the pattern may not be compatible between
255 * the various search algorithms.
256 *
257 * Returns a new textsearch configuration according to the specified
Pablo Neira Ayusoe03ba842007-12-01 00:03:52 +1100258 * parameters or a ERR_PTR(). If a zero length pattern is passed, this
259 * function returns EINVAL.
Thomas Graf2de4ff72005-06-23 20:49:30 -0700260 */
261struct ts_config *textsearch_prepare(const char *algo, const void *pattern,
Al Virofd4f2df2005-10-21 03:18:50 -0400262 unsigned int len, gfp_t gfp_mask, int flags)
Thomas Graf2de4ff72005-06-23 20:49:30 -0700263{
264 int err = -ENOENT;
265 struct ts_config *conf;
266 struct ts_ops *ops;
267
Pablo Neira Ayusoe03ba842007-12-01 00:03:52 +1100268 if (len == 0)
269 return ERR_PTR(-EINVAL);
270
Thomas Graf2de4ff72005-06-23 20:49:30 -0700271 ops = lookup_ts_algo(algo);
Johannes Berga00caa1f2008-07-08 19:00:24 +0200272#ifdef CONFIG_MODULES
Thomas Graf2de4ff72005-06-23 20:49:30 -0700273 /*
274 * Why not always autoload you may ask. Some users are
275 * in a situation where requesting a module may deadlock,
276 * especially when the module is located on a NFS mount.
277 */
278 if (ops == NULL && flags & TS_AUTOLOAD) {
279 request_module("ts_%s", algo);
280 ops = lookup_ts_algo(algo);
281 }
282#endif
283
284 if (ops == NULL)
285 goto errout;
286
Joonwoo Parkb9c79672008-07-08 02:37:31 -0700287 conf = ops->init(pattern, len, gfp_mask, flags);
Thomas Graf2de4ff72005-06-23 20:49:30 -0700288 if (IS_ERR(conf)) {
289 err = PTR_ERR(conf);
290 goto errout;
291 }
292
293 conf->ops = ops;
294 return conf;
295
296errout:
297 if (ops)
298 module_put(ops->owner);
299
300 return ERR_PTR(err);
301}
Fabian Frederickce643a32014-06-04 16:11:58 -0700302EXPORT_SYMBOL(textsearch_prepare);
Thomas Graf2de4ff72005-06-23 20:49:30 -0700303
304/**
305 * textsearch_destroy - destroy a search configuration
306 * @conf: search configuration
307 *
308 * Releases all references of the configuration and frees
309 * up the memory.
310 */
311void textsearch_destroy(struct ts_config *conf)
312{
313 if (conf->ops) {
314 if (conf->ops->destroy)
315 conf->ops->destroy(conf);
316 module_put(conf->ops->owner);
317 }
318
319 kfree(conf);
320}
Thomas Graf2de4ff72005-06-23 20:49:30 -0700321EXPORT_SYMBOL(textsearch_destroy);