123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- #include <linux/module.h>
- #include <linux/types.h>
- #include <linux/string.h>
- #include <linux/init.h>
- #include <linux/rculist.h>
- #include <linux/rcupdate.h>
- #include <linux/err.h>
- #include <linux/textsearch.h>
- #include <linux/slab.h>
- 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;
- }
- 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;
- }
- EXPORT_SYMBOL(textsearch_register);
- 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;
- }
- EXPORT_SYMBOL(textsearch_unregister);
- 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;
- }
-
- 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);
- }
- EXPORT_SYMBOL(textsearch_find_continuous);
- struct ts_config *textsearch_prepare(const char *algo, const void *pattern,
- unsigned int len, gfp_t gfp_mask, int flags)
- {
- int err = -ENOENT;
- struct ts_config *conf;
- struct ts_ops *ops;
-
- if (len == 0)
- return ERR_PTR(-EINVAL);
- ops = lookup_ts_algo(algo);
- #ifdef CONFIG_MODULES
-
- if (ops == NULL && flags & TS_AUTOLOAD) {
- request_module("ts_%s", algo);
- ops = lookup_ts_algo(algo);
- }
- #endif
- if (ops == NULL)
- goto errout;
- conf = ops->init(pattern, len, gfp_mask, flags);
- 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);
- }
- EXPORT_SYMBOL(textsearch_prepare);
- 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);
- }
- EXPORT_SYMBOL(textsearch_destroy);
|