~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/lib/textsearch.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /lib/textsearch.c (Version linux-6.12-rc7) and /lib/textsearch.c (Version linux-4.11.12)


  1 // SPDX-License-Identifier: GPL-2.0-or-later   << 
  2 /*                                                  1 /*
  3  * lib/textsearch.c     Generic text search in      2  * lib/textsearch.c     Generic text search interface
  4  *                                                  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  *
  5  * Authors:     Thomas Graf <tgraf@suug.ch>         9  * Authors:     Thomas Graf <tgraf@suug.ch>
  6  *              Pablo Neira Ayuso <pablo@netfi     10  *              Pablo Neira Ayuso <pablo@netfilter.org>
  7  *                                                 11  *
  8  * ===========================================     12  * ==========================================================================
  9  */                                            !!  13  *
 10                                                << 
 11 /**                                            << 
 12  * DOC: ts_intro                               << 
 13  * INTRODUCTION                                    14  * INTRODUCTION
 14  *                                                 15  *
 15  *   The textsearch infrastructure provides te     16  *   The textsearch infrastructure provides text searching facilities for
 16  *   both linear and non-linear data. Individu     17  *   both linear and non-linear data. Individual search algorithms are
 17  *   implemented in modules and chosen by the      18  *   implemented in modules and chosen by the user.
 18  *                                                 19  *
 19  * ARCHITECTURE                                    20  * ARCHITECTURE
 20  *                                                 21  *
 21  * .. code-block:: none                        !!  22  *      User
 22  *                                             << 
 23  *     User                                    << 
 24  *     +----------------+                          23  *     +----------------+
 25  *     |        finish()|<--------------(6)---     24  *     |        finish()|<--------------(6)-----------------+
 26  *     |get_next_block()|<--------------(5)---     25  *     |get_next_block()|<--------------(5)---------------+ |
 27  *     |                |                          26  *     |                |                     Algorithm   | |
 28  *     |                |                    +     27  *     |                |                    +------------------------------+
 29  *     |                |                    |     28  *     |                |                    |  init()   find()   destroy() |
 30  *     |                |                    +     29  *     |                |                    +------------------------------+
 31  *     |                |       Core API           30  *     |                |       Core API           ^       ^          ^
 32  *     |                |      +--------------     31  *     |                |      +---------------+  (2)     (4)        (8)
 33  *     |             (1)|----->| prepare()         32  *     |             (1)|----->| prepare()     |---+       |          |
 34  *     |             (3)|----->| find()/next()     33  *     |             (3)|----->| find()/next() |-----------+          |
 35  *     |             (7)|----->| destroy()         34  *     |             (7)|----->| destroy()     |----------------------+
 36  *     +----------------+      +--------------     35  *     +----------------+      +---------------+
 37  *                                             !!  36  *  
 38  *   (1) User configures a search by calling t !!  37  *   (1) User configures a search by calling _prepare() specifying the
 39  *       the search parameters such as the pat !!  38  *       search parameters such as the pattern and algorithm name.
 40  *   (2) Core requests the algorithm to alloca     39  *   (2) Core requests the algorithm to allocate and initialize a search
 41  *       configuration according to the specif     40  *       configuration according to the specified parameters.
 42  *   (3) User starts the search(es) by calling !!  41  *   (3) User starts the search(es) by calling _find() or _next() to
 43  *       textsearch_next() to fetch subsequent !!  42  *       fetch subsequent occurrences. A state variable is provided
 44  *       is provided to the algorithm to store !!  43  *       to the algorithm to store persistent variables.
 45  *   (4) Core eventually resets the search off     44  *   (4) Core eventually resets the search offset and forwards the find()
 46  *       request to the algorithm.                 45  *       request to the algorithm.
 47  *   (5) Algorithm calls get_next_block() prov     46  *   (5) Algorithm calls get_next_block() provided by the user continuously
 48  *       to fetch the data to be searched in b     47  *       to fetch the data to be searched in block by block.
 49  *   (6) Algorithm invokes finish() after the      48  *   (6) Algorithm invokes finish() after the last call to get_next_block
 50  *       to clean up any leftovers from get_ne     49  *       to clean up any leftovers from get_next_block. (Optional)
 51  *   (7) User destroys the configuration by ca !!  50  *   (7) User destroys the configuration by calling _destroy().
 52  *   (8) Core notifies the algorithm to destro     51  *   (8) Core notifies the algorithm to destroy algorithm specific
 53  *       allocations. (Optional)                   52  *       allocations. (Optional)
 54  *                                                 53  *
 55  * USAGE                                           54  * USAGE
 56  *                                                 55  *
 57  *   Before a search can be performed, a confi     56  *   Before a search can be performed, a configuration must be created
 58  *   by calling textsearch_prepare() specifyin     57  *   by calling textsearch_prepare() specifying the searching algorithm,
 59  *   the pattern to look for and flags. As a f     58  *   the pattern to look for and flags. As a flag, you can set TS_IGNORECASE
 60  *   to perform case insensitive matching. But     59  *   to perform case insensitive matching. But it might slow down
 61  *   performance of algorithm, so you should u     60  *   performance of algorithm, so you should use it at own your risk.
 62  *   The returned configuration may then be us     61  *   The returned configuration may then be used for an arbitrary
 63  *   amount of times and even in parallel as l     62  *   amount of times and even in parallel as long as a separate struct
 64  *   ts_state variable is provided to every in     63  *   ts_state variable is provided to every instance.
 65  *                                                 64  *
 66  *   The actual search is performed by either  !!  65  *   The actual search is performed by either calling textsearch_find_-
 67  *   textsearch_find_continuous() for linear d !!  66  *   continuous() for linear data or by providing an own get_next_block()
 68  *   an own get_next_block() implementation an !!  67  *   implementation and calling textsearch_find(). Both functions return
 69  *   calling textsearch_find(). Both functions << 
 70  *   the position of the first occurrence of t     68  *   the position of the first occurrence of the pattern or UINT_MAX if
 71  *   no match was found. Subsequent occurrence     69  *   no match was found. Subsequent occurrences can be found by calling
 72  *   textsearch_next() regardless of the linea     70  *   textsearch_next() regardless of the linearity of the data.
 73  *                                                 71  *
 74  *   Once you're done using a configuration it     72  *   Once you're done using a configuration it must be given back via
 75  *   textsearch_destroy.                           73  *   textsearch_destroy.
 76  *                                                 74  *
 77  * EXAMPLE::                                   !!  75  * EXAMPLE
 78  *                                                 76  *
 79  *   int pos;                                      77  *   int pos;
 80  *   struct ts_config *conf;                       78  *   struct ts_config *conf;
 81  *   struct ts_state state;                        79  *   struct ts_state state;
 82  *   const char *pattern = "chicken";              80  *   const char *pattern = "chicken";
 83  *   const char *example = "We dance the funky     81  *   const char *example = "We dance the funky chicken";
 84  *                                                 82  *
 85  *   conf = textsearch_prepare("kmp", pattern,     83  *   conf = textsearch_prepare("kmp", pattern, strlen(pattern),
 86  *                             GFP_KERNEL, TS_     84  *                             GFP_KERNEL, TS_AUTOLOAD);
 87  *   if (IS_ERR(conf)) {                           85  *   if (IS_ERR(conf)) {
 88  *       err = PTR_ERR(conf);                      86  *       err = PTR_ERR(conf);
 89  *       goto errout;                              87  *       goto errout;
 90  *   }                                             88  *   }
 91  *                                                 89  *
 92  *   pos = textsearch_find_continuous(conf, &s     90  *   pos = textsearch_find_continuous(conf, &state, example, strlen(example));
 93  *   if (pos != UINT_MAX)                          91  *   if (pos != UINT_MAX)
 94  *       panic("Oh my god, dancing chickens at     92  *       panic("Oh my god, dancing chickens at %d\n", pos);
 95  *                                                 93  *
 96  *   textsearch_destroy(conf);                     94  *   textsearch_destroy(conf);
                                                   >>  95  * ==========================================================================
 97  */                                                96  */
 98 /* =========================================== << 
 99                                                    97 
100 #include <linux/module.h>                          98 #include <linux/module.h>
101 #include <linux/types.h>                           99 #include <linux/types.h>
102 #include <linux/string.h>                         100 #include <linux/string.h>
103 #include <linux/init.h>                           101 #include <linux/init.h>
104 #include <linux/rculist.h>                        102 #include <linux/rculist.h>
105 #include <linux/rcupdate.h>                       103 #include <linux/rcupdate.h>
106 #include <linux/err.h>                            104 #include <linux/err.h>
107 #include <linux/textsearch.h>                     105 #include <linux/textsearch.h>
108 #include <linux/slab.h>                           106 #include <linux/slab.h>
109                                                   107 
110 static LIST_HEAD(ts_ops);                         108 static LIST_HEAD(ts_ops);
111 static DEFINE_SPINLOCK(ts_mod_lock);              109 static DEFINE_SPINLOCK(ts_mod_lock);
112                                                   110 
113 static inline struct ts_ops *lookup_ts_algo(co    111 static inline struct ts_ops *lookup_ts_algo(const char *name)
114 {                                                 112 {
115         struct ts_ops *o;                         113         struct ts_ops *o;
116                                                   114 
117         rcu_read_lock();                          115         rcu_read_lock();
118         list_for_each_entry_rcu(o, &ts_ops, li    116         list_for_each_entry_rcu(o, &ts_ops, list) {
119                 if (!strcmp(name, o->name)) {     117                 if (!strcmp(name, o->name)) {
120                         if (!try_module_get(o-    118                         if (!try_module_get(o->owner))
121                                 o = NULL;         119                                 o = NULL;
122                         rcu_read_unlock();        120                         rcu_read_unlock();
123                         return o;                 121                         return o;
124                 }                                 122                 }
125         }                                         123         }
126         rcu_read_unlock();                        124         rcu_read_unlock();
127                                                   125 
128         return NULL;                              126         return NULL;
129 }                                                 127 }
130                                                   128 
131 /**                                               129 /**
132  * textsearch_register - register a textsearch    130  * textsearch_register - register a textsearch module
133  * @ops: operations lookup table                  131  * @ops: operations lookup table
134  *                                                132  *
135  * This function must be called by textsearch     133  * This function must be called by textsearch modules to announce
136  * their presence. The specified &@ops must ha    134  * their presence. The specified &@ops must have %name set to a
137  * unique identifier and the callbacks find(),    135  * unique identifier and the callbacks find(), init(), get_pattern(),
138  * and get_pattern_len() must be implemented.     136  * and get_pattern_len() must be implemented.
139  *                                                137  *
140  * Returns 0 or -EEXISTS if another module has    138  * Returns 0 or -EEXISTS if another module has already registered
141  * with same name.                                139  * with same name.
142  */                                               140  */
143 int textsearch_register(struct ts_ops *ops)       141 int textsearch_register(struct ts_ops *ops)
144 {                                                 142 {
145         int err = -EEXIST;                        143         int err = -EEXIST;
146         struct ts_ops *o;                         144         struct ts_ops *o;
147                                                   145 
148         if (ops->name == NULL || ops->find ==     146         if (ops->name == NULL || ops->find == NULL || ops->init == NULL ||
149             ops->get_pattern == NULL || ops->g    147             ops->get_pattern == NULL || ops->get_pattern_len == NULL)
150                 return -EINVAL;                   148                 return -EINVAL;
151                                                   149 
152         spin_lock(&ts_mod_lock);                  150         spin_lock(&ts_mod_lock);
153         list_for_each_entry(o, &ts_ops, list)     151         list_for_each_entry(o, &ts_ops, list) {
154                 if (!strcmp(ops->name, o->name    152                 if (!strcmp(ops->name, o->name))
155                         goto errout;              153                         goto errout;
156         }                                         154         }
157                                                   155 
158         list_add_tail_rcu(&ops->list, &ts_ops)    156         list_add_tail_rcu(&ops->list, &ts_ops);
159         err = 0;                                  157         err = 0;
160 errout:                                           158 errout:
161         spin_unlock(&ts_mod_lock);                159         spin_unlock(&ts_mod_lock);
162         return err;                               160         return err;
163 }                                                 161 }
164 EXPORT_SYMBOL(textsearch_register);               162 EXPORT_SYMBOL(textsearch_register);
165                                                   163 
166 /**                                               164 /**
167  * textsearch_unregister - unregister a textse    165  * textsearch_unregister - unregister a textsearch module
168  * @ops: operations lookup table                  166  * @ops: operations lookup table
169  *                                                167  *
170  * This function must be called by textsearch     168  * This function must be called by textsearch modules to announce
171  * their disappearance for examples when the m    169  * their disappearance for examples when the module gets unloaded.
172  * The &ops parameter must be the same as the     170  * The &ops parameter must be the same as the one during the
173  * registration.                                  171  * registration.
174  *                                                172  *
175  * Returns 0 on success or -ENOENT if no match    173  * Returns 0 on success or -ENOENT if no matching textsearch
176  * registration was found.                        174  * registration was found.
177  */                                               175  */
178 int textsearch_unregister(struct ts_ops *ops)     176 int textsearch_unregister(struct ts_ops *ops)
179 {                                                 177 {
180         int err = 0;                              178         int err = 0;
181         struct ts_ops *o;                         179         struct ts_ops *o;
182                                                   180 
183         spin_lock(&ts_mod_lock);                  181         spin_lock(&ts_mod_lock);
184         list_for_each_entry(o, &ts_ops, list)     182         list_for_each_entry(o, &ts_ops, list) {
185                 if (o == ops) {                   183                 if (o == ops) {
186                         list_del_rcu(&o->list)    184                         list_del_rcu(&o->list);
187                         goto out;                 185                         goto out;
188                 }                                 186                 }
189         }                                         187         }
190                                                   188 
191         err = -ENOENT;                            189         err = -ENOENT;
192 out:                                              190 out:
193         spin_unlock(&ts_mod_lock);                191         spin_unlock(&ts_mod_lock);
194         return err;                               192         return err;
195 }                                                 193 }
196 EXPORT_SYMBOL(textsearch_unregister);             194 EXPORT_SYMBOL(textsearch_unregister);
197                                                   195 
198 struct ts_linear_state                            196 struct ts_linear_state
199 {                                                 197 {
200         unsigned int    len;                      198         unsigned int    len;
201         const void      *data;                    199         const void      *data;
202 };                                                200 };
203                                                   201 
204 static unsigned int get_linear_data(unsigned i    202 static unsigned int get_linear_data(unsigned int consumed, const u8 **dst,
205                                     struct ts_    203                                     struct ts_config *conf,
206                                     struct ts_    204                                     struct ts_state *state)
207 {                                                 205 {
208         struct ts_linear_state *st = (struct t    206         struct ts_linear_state *st = (struct ts_linear_state *) state->cb;
209                                                   207 
210         if (likely(consumed < st->len)) {         208         if (likely(consumed < st->len)) {
211                 *dst = st->data + consumed;       209                 *dst = st->data + consumed;
212                 return st->len - consumed;        210                 return st->len - consumed;
213         }                                         211         }
214                                                   212 
215         return 0;                                 213         return 0;
216 }                                                 214 }
217                                                   215 
218 /**                                               216 /**
219  * textsearch_find_continuous - search a patte    217  * textsearch_find_continuous - search a pattern in continuous/linear data
220  * @conf: search configuration                    218  * @conf: search configuration
221  * @state: search state                           219  * @state: search state
222  * @data: data to search in                       220  * @data: data to search in
223  * @len: length of data                           221  * @len: length of data
224  *                                                222  *
225  * A simplified version of textsearch_find() f    223  * A simplified version of textsearch_find() for continuous/linear data.
226  * Call textsearch_next() to retrieve subseque    224  * Call textsearch_next() to retrieve subsequent matches.
227  *                                                225  *
228  * Returns the position of first occurrence of    226  * Returns the position of first occurrence of the pattern or
229  * %UINT_MAX if no occurrence was found.          227  * %UINT_MAX if no occurrence was found.
230  */                                            !! 228  */ 
231 unsigned int textsearch_find_continuous(struct    229 unsigned int textsearch_find_continuous(struct ts_config *conf,
232                                         struct    230                                         struct ts_state *state,
233                                         const     231                                         const void *data, unsigned int len)
234 {                                                 232 {
235         struct ts_linear_state *st = (struct t    233         struct ts_linear_state *st = (struct ts_linear_state *) state->cb;
236                                                   234 
237         conf->get_next_block = get_linear_data    235         conf->get_next_block = get_linear_data;
238         st->data = data;                          236         st->data = data;
239         st->len = len;                            237         st->len = len;
240                                                   238 
241         return textsearch_find(conf, state);      239         return textsearch_find(conf, state);
242 }                                                 240 }
243 EXPORT_SYMBOL(textsearch_find_continuous);        241 EXPORT_SYMBOL(textsearch_find_continuous);
244                                                   242 
245 /**                                               243 /**
246  * textsearch_prepare - Prepare a search          244  * textsearch_prepare - Prepare a search
247  * @algo: name of search algorithm                245  * @algo: name of search algorithm
248  * @pattern: pattern data                         246  * @pattern: pattern data
249  * @len: length of pattern                        247  * @len: length of pattern
250  * @gfp_mask: allocation mask                     248  * @gfp_mask: allocation mask
251  * @flags: search flags                           249  * @flags: search flags
252  *                                                250  *
253  * Looks up the search algorithm module and cr    251  * Looks up the search algorithm module and creates a new textsearch
254  * configuration for the specified pattern.       252  * configuration for the specified pattern.
255  *                                                253  *
256  * Note: The format of the pattern may not be     254  * Note: The format of the pattern may not be compatible between
257  *       the various search algorithms.           255  *       the various search algorithms.
258  *                                                256  *
259  * Returns a new textsearch configuration acco    257  * Returns a new textsearch configuration according to the specified
260  * parameters or a ERR_PTR(). If a zero length    258  * parameters or a ERR_PTR(). If a zero length pattern is passed, this
261  * function returns EINVAL.                       259  * function returns EINVAL.
262  */                                               260  */
263 struct ts_config *textsearch_prepare(const cha    261 struct ts_config *textsearch_prepare(const char *algo, const void *pattern,
264                                      unsigned     262                                      unsigned int len, gfp_t gfp_mask, int flags)
265 {                                                 263 {
266         int err = -ENOENT;                        264         int err = -ENOENT;
267         struct ts_config *conf;                   265         struct ts_config *conf;
268         struct ts_ops *ops;                       266         struct ts_ops *ops;
269                                                   267         
270         if (len == 0)                             268         if (len == 0)
271                 return ERR_PTR(-EINVAL);          269                 return ERR_PTR(-EINVAL);
272                                                   270 
273         ops = lookup_ts_algo(algo);               271         ops = lookup_ts_algo(algo);
274 #ifdef CONFIG_MODULES                             272 #ifdef CONFIG_MODULES
275         /*                                        273         /*
276          * Why not always autoload you may ask    274          * Why not always autoload you may ask. Some users are
277          * in a situation where requesting a m    275          * in a situation where requesting a module may deadlock,
278          * especially when the module is locat    276          * especially when the module is located on a NFS mount.
279          */                                       277          */
280         if (ops == NULL && flags & TS_AUTOLOAD    278         if (ops == NULL && flags & TS_AUTOLOAD) {
281                 request_module("ts_%s", algo);    279                 request_module("ts_%s", algo);
282                 ops = lookup_ts_algo(algo);       280                 ops = lookup_ts_algo(algo);
283         }                                         281         }
284 #endif                                            282 #endif
285                                                   283 
286         if (ops == NULL)                          284         if (ops == NULL)
287                 goto errout;                      285                 goto errout;
288                                                   286 
289         conf = ops->init(pattern, len, gfp_mas    287         conf = ops->init(pattern, len, gfp_mask, flags);
290         if (IS_ERR(conf)) {                       288         if (IS_ERR(conf)) {
291                 err = PTR_ERR(conf);              289                 err = PTR_ERR(conf);
292                 goto errout;                      290                 goto errout;
293         }                                         291         }
294                                                   292 
295         conf->ops = ops;                          293         conf->ops = ops;
296         return conf;                              294         return conf;
297                                                   295 
298 errout:                                           296 errout:
299         if (ops)                                  297         if (ops)
300                 module_put(ops->owner);           298                 module_put(ops->owner);
301                                                   299                 
302         return ERR_PTR(err);                      300         return ERR_PTR(err);
303 }                                                 301 }
304 EXPORT_SYMBOL(textsearch_prepare);                302 EXPORT_SYMBOL(textsearch_prepare);
305                                                   303 
306 /**                                               304 /**
307  * textsearch_destroy - destroy a search confi    305  * textsearch_destroy - destroy a search configuration
308  * @conf: search configuration                    306  * @conf: search configuration
309  *                                                307  *
310  * Releases all references of the configuratio    308  * Releases all references of the configuration and frees
311  * up the memory.                                 309  * up the memory.
312  */                                               310  */
313 void textsearch_destroy(struct ts_config *conf    311 void textsearch_destroy(struct ts_config *conf)
314 {                                                 312 {
315         if (conf->ops) {                          313         if (conf->ops) {
316                 if (conf->ops->destroy)           314                 if (conf->ops->destroy)
317                         conf->ops->destroy(con    315                         conf->ops->destroy(conf);
318                 module_put(conf->ops->owner);     316                 module_put(conf->ops->owner);
319         }                                         317         }
320                                                   318 
321         kfree(conf);                              319         kfree(conf);
322 }                                                 320 }
323 EXPORT_SYMBOL(textsearch_destroy);                321 EXPORT_SYMBOL(textsearch_destroy);
324                                                   322 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php