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