Blame view

lib/textsearch.c 9.33 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
2
3
4
  /*
   * lib/textsearch.c	Generic text search interface
   *
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
5
   * Authors:	Thomas Graf <tgraf@suug.ch>
e03ba84ad   Pablo Neira Ayuso   [TEXTSEARCH]: Do ...
6
   * 		Pablo Neira Ayuso <pablo@netfilter.org>
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
7
8
   *
   * ==========================================================================
5968a70d7   Randy Dunlap   textsearch: fix k...
9
10
11
12
   */
  
  /**
   * DOC: ts_intro
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
13
14
   * INTRODUCTION
   *
de0368d5f   Jesper Dangaard Brouer   textsearch: doc -...
15
   *   The textsearch infrastructure provides text searching facilities for
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
16
17
18
19
20
   *   both linear and non-linear data. Individual search algorithms are
   *   implemented in modules and chosen by the user.
   *
   * ARCHITECTURE
   *
5968a70d7   Randy Dunlap   textsearch: fix k...
21
22
23
   * .. code-block:: none
   *
   *     User
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
24
25
26
27
28
29
30
31
32
33
34
35
36
   *     +----------------+
   *     |        finish()|<--------------(6)-----------------+
   *     |get_next_block()|<--------------(5)---------------+ |
   *     |                |                     Algorithm   | |
   *     |                |                    +------------------------------+
   *     |                |                    |  init()   find()   destroy() |
   *     |                |                    +------------------------------+
   *     |                |       Core API           ^       ^          ^
   *     |                |      +---------------+  (2)     (4)        (8)
   *     |             (1)|----->| prepare()     |---+       |          |
   *     |             (3)|----->| find()/next() |-----------+          |
   *     |             (7)|----->| destroy()     |----------------------+
   *     +----------------+      +---------------+
5968a70d7   Randy Dunlap   textsearch: fix k...
37
38
39
   *
   *   (1) User configures a search by calling textsearch_prepare() specifying
   *       the search parameters such as the pattern and algorithm name.
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
40
41
   *   (2) Core requests the algorithm to allocate and initialize a search
   *       configuration according to the specified parameters.
5968a70d7   Randy Dunlap   textsearch: fix k...
42
43
44
   *   (3) User starts the search(es) by calling textsearch_find() or
   *       textsearch_next() to fetch subsequent occurrences. A state variable
   *       is provided to the algorithm to store persistent variables.
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
45
46
   *   (4) Core eventually resets the search offset and forwards the find()
   *       request to the algorithm.
de0368d5f   Jesper Dangaard Brouer   textsearch: doc -...
47
   *   (5) Algorithm calls get_next_block() provided by the user continuously
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
48
49
50
   *       to fetch the data to be searched in block by block.
   *   (6) Algorithm invokes finish() after the last call to get_next_block
   *       to clean up any leftovers from get_next_block. (Optional)
5968a70d7   Randy Dunlap   textsearch: fix k...
51
   *   (7) User destroys the configuration by calling textsearch_destroy().
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
52
53
54
55
56
57
   *   (8) Core notifies the algorithm to destroy algorithm specific
   *       allocations. (Optional)
   *
   * USAGE
   *
   *   Before a search can be performed, a configuration must be created
b9c796783   Joonwoo Park   textsearch: suppo...
58
59
60
61
   *   by calling textsearch_prepare() specifying the searching algorithm,
   *   the pattern to look for and flags. As a flag, you can set TS_IGNORECASE
   *   to perform case insensitive matching. But it might slow down
   *   performance of algorithm, so you should use it at own your risk.
de0368d5f   Jesper Dangaard Brouer   textsearch: doc -...
62
   *   The returned configuration may then be used for an arbitrary
b9c796783   Joonwoo Park   textsearch: suppo...
63
64
   *   amount of times and even in parallel as long as a separate struct
   *   ts_state variable is provided to every instance.
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
65
   *
5968a70d7   Randy Dunlap   textsearch: fix k...
66
67
68
69
   *   The actual search is performed by either calling
   *   textsearch_find_continuous() for linear data or by providing
   *   an own get_next_block() implementation and
   *   calling textsearch_find(). Both functions return
de0368d5f   Jesper Dangaard Brouer   textsearch: doc -...
70
71
   *   the position of the first occurrence of the pattern or UINT_MAX if
   *   no match was found. Subsequent occurrences can be found by calling
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
72
73
74
75
76
   *   textsearch_next() regardless of the linearity of the data.
   *
   *   Once you're done using a configuration it must be given back via
   *   textsearch_destroy.
   *
5968a70d7   Randy Dunlap   textsearch: fix k...
77
   * EXAMPLE::
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
78
79
80
81
82
83
84
85
86
87
88
89
90
91
   *
   *   int pos;
   *   struct ts_config *conf;
   *   struct ts_state state;
   *   const char *pattern = "chicken";
   *   const char *example = "We dance the funky chicken";
   *
   *   conf = textsearch_prepare("kmp", pattern, strlen(pattern),
   *                             GFP_KERNEL, TS_AUTOLOAD);
   *   if (IS_ERR(conf)) {
   *       err = PTR_ERR(conf);
   *       goto errout;
   *   }
   *
2105b52e3   Randy Dunlap   lib: textsearch: ...
92
   *   pos = textsearch_find_continuous(conf, &state, example, strlen(example));
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
93
   *   if (pos != UINT_MAX)
2105b52e3   Randy Dunlap   lib: textsearch: ...
94
95
   *       panic("Oh my god, dancing chickens at %d
  ", pos);
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
96
97
   *
   *   textsearch_destroy(conf);
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
98
   */
5968a70d7   Randy Dunlap   textsearch: fix k...
99
  /* ========================================================================== */
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
100

2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
101
102
103
104
  #include <linux/module.h>
  #include <linux/types.h>
  #include <linux/string.h>
  #include <linux/init.h>
82524746c   Franck Bui-Huu   rcu: split list.h...
105
  #include <linux/rculist.h>
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
106
107
108
  #include <linux/rcupdate.h>
  #include <linux/err.h>
  #include <linux/textsearch.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
109
  #include <linux/slab.h>
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  
  static LIST_HEAD(ts_ops);
  static DEFINE_SPINLOCK(ts_mod_lock);
  
  static inline struct ts_ops *lookup_ts_algo(const char *name)
  {
  	struct ts_ops *o;
  
  	rcu_read_lock();
  	list_for_each_entry_rcu(o, &ts_ops, list) {
  		if (!strcmp(name, o->name)) {
  			if (!try_module_get(o->owner))
  				o = NULL;
  			rcu_read_unlock();
  			return o;
  		}
  	}
  	rcu_read_unlock();
  
  	return NULL;
  }
  
  /**
   * textsearch_register - register a textsearch module
   * @ops: operations lookup table
   *
   * This function must be called by textsearch modules to announce
   * their presence. The specified &@ops must have %name set to a
   * unique identifier and the callbacks find(), init(), get_pattern(),
   * and get_pattern_len() must be implemented.
   *
   * Returns 0 or -EEXISTS if another module has already registered
   * with same name.
   */
  int textsearch_register(struct ts_ops *ops)
  {
  	int err = -EEXIST;
  	struct ts_ops *o;
  
  	if (ops->name == NULL || ops->find == NULL || ops->init == NULL ||
  	    ops->get_pattern == NULL || ops->get_pattern_len == NULL)
  		return -EINVAL;
  
  	spin_lock(&ts_mod_lock);
  	list_for_each_entry(o, &ts_ops, list) {
  		if (!strcmp(ops->name, o->name))
  			goto errout;
  	}
  
  	list_add_tail_rcu(&ops->list, &ts_ops);
  	err = 0;
  errout:
  	spin_unlock(&ts_mod_lock);
  	return err;
  }
ce643a30d   Fabian Frederick   lib/textsearch.c:...
165
  EXPORT_SYMBOL(textsearch_register);
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
  
  /**
   * textsearch_unregister - unregister a textsearch module
   * @ops: operations lookup table
   *
   * This function must be called by textsearch modules to announce
   * their disappearance for examples when the module gets unloaded.
   * The &ops parameter must be the same as the one during the
   * registration.
   *
   * Returns 0 on success or -ENOENT if no matching textsearch
   * registration was found.
   */
  int textsearch_unregister(struct ts_ops *ops)
  {
  	int err = 0;
  	struct ts_ops *o;
  
  	spin_lock(&ts_mod_lock);
  	list_for_each_entry(o, &ts_ops, list) {
  		if (o == ops) {
  			list_del_rcu(&o->list);
  			goto out;
  		}
  	}
  
  	err = -ENOENT;
  out:
  	spin_unlock(&ts_mod_lock);
  	return err;
  }
ce643a30d   Fabian Frederick   lib/textsearch.c:...
197
  EXPORT_SYMBOL(textsearch_unregister);
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  
  struct ts_linear_state
  {
  	unsigned int	len;
  	const void	*data;
  };
  
  static unsigned int get_linear_data(unsigned int consumed, const u8 **dst,
  				    struct ts_config *conf,
  				    struct ts_state *state)
  {
  	struct ts_linear_state *st = (struct ts_linear_state *) state->cb;
  
  	if (likely(consumed < st->len)) {
  		*dst = st->data + consumed;
  		return st->len - consumed;
  	}
  
  	return 0;
  }
  
  /**
   * textsearch_find_continuous - search a pattern in continuous/linear data
   * @conf: search configuration
   * @state: search state
   * @data: data to search in
   * @len: length of data
   *
   * A simplified version of textsearch_find() for continuous/linear data.
   * Call textsearch_next() to retrieve subsequent matches.
   *
   * Returns the position of first occurrence of the pattern or
72fd4a35a   Robert P. J. Day   [PATCH] Numerous ...
230
   * %UINT_MAX if no occurrence was found.
5968a70d7   Randy Dunlap   textsearch: fix k...
231
   */
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
232
233
234
235
236
237
238
239
240
241
242
243
  unsigned int textsearch_find_continuous(struct ts_config *conf,
  					struct ts_state *state,
  					const void *data, unsigned int len)
  {
  	struct ts_linear_state *st = (struct ts_linear_state *) state->cb;
  
  	conf->get_next_block = get_linear_data;
  	st->data = data;
  	st->len = len;
  
  	return textsearch_find(conf, state);
  }
ce643a30d   Fabian Frederick   lib/textsearch.c:...
244
  EXPORT_SYMBOL(textsearch_find_continuous);
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
245
246
247
248
249
250
251
252
253
254
  
  /**
   * textsearch_prepare - Prepare a search
   * @algo: name of search algorithm
   * @pattern: pattern data
   * @len: length of pattern
   * @gfp_mask: allocation mask
   * @flags: search flags
   *
   * Looks up the search algorithm module and creates a new textsearch
fec229083   Raphael Silva   lib/textsearch.c:...
255
   * configuration for the specified pattern.
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
256
257
258
259
260
   *
   * Note: The format of the pattern may not be compatible between
   *       the various search algorithms.
   *
   * Returns a new textsearch configuration according to the specified
e03ba84ad   Pablo Neira Ayuso   [TEXTSEARCH]: Do ...
261
262
   * parameters or a ERR_PTR(). If a zero length pattern is passed, this
   * function returns EINVAL.
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
263
264
   */
  struct ts_config *textsearch_prepare(const char *algo, const void *pattern,
fd4f2df24   Al Viro   [PATCH] gfp_t: lib/*
265
  				     unsigned int len, gfp_t gfp_mask, int flags)
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
266
267
268
269
270
  {
  	int err = -ENOENT;
  	struct ts_config *conf;
  	struct ts_ops *ops;
  	
e03ba84ad   Pablo Neira Ayuso   [TEXTSEARCH]: Do ...
271
272
  	if (len == 0)
  		return ERR_PTR(-EINVAL);
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
273
  	ops = lookup_ts_algo(algo);
a00caa1fa   Johannes Berg   remove CONFIG_KMO...
274
  #ifdef CONFIG_MODULES
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
275
276
277
278
279
280
281
282
283
284
285
286
287
  	/*
  	 * Why not always autoload you may ask. Some users are
  	 * in a situation where requesting a module may deadlock,
  	 * especially when the module is located on a NFS mount.
  	 */
  	if (ops == NULL && flags & TS_AUTOLOAD) {
  		request_module("ts_%s", algo);
  		ops = lookup_ts_algo(algo);
  	}
  #endif
  
  	if (ops == NULL)
  		goto errout;
b9c796783   Joonwoo Park   textsearch: suppo...
288
  	conf = ops->init(pattern, len, gfp_mask, flags);
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
289
290
291
292
293
294
295
296
297
298
299
300
301
302
  	if (IS_ERR(conf)) {
  		err = PTR_ERR(conf);
  		goto errout;
  	}
  
  	conf->ops = ops;
  	return conf;
  
  errout:
  	if (ops)
  		module_put(ops->owner);
  		
  	return ERR_PTR(err);
  }
ce643a30d   Fabian Frederick   lib/textsearch.c:...
303
  EXPORT_SYMBOL(textsearch_prepare);
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
  
  /**
   * textsearch_destroy - destroy a search configuration
   * @conf: search configuration
   *
   * Releases all references of the configuration and frees
   * up the memory.
   */
  void textsearch_destroy(struct ts_config *conf)
  {
  	if (conf->ops) {
  		if (conf->ops->destroy)
  			conf->ops->destroy(conf);
  		module_put(conf->ops->owner);
  	}
  
  	kfree(conf);
  }
2de4ff7bd   Thomas Graf   [LIB]: Textsearch...
322
  EXPORT_SYMBOL(textsearch_destroy);