Thomas Graf | 2de4ff7 | 2005-06-23 20:49:30 -0700 | [diff] [blame] | 1 | #ifndef __LINUX_TEXTSEARCH_H |
| 2 | #define __LINUX_TEXTSEARCH_H |
| 3 | |
Thomas Graf | 2de4ff7 | 2005-06-23 20:49:30 -0700 | [diff] [blame] | 4 | #include <linux/types.h> |
| 5 | #include <linux/list.h> |
| 6 | #include <linux/kernel.h> |
Thomas Graf | 2de4ff7 | 2005-06-23 20:49:30 -0700 | [diff] [blame] | 7 | #include <linux/err.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 8 | #include <linux/slab.h> |
Thomas Graf | 2de4ff7 | 2005-06-23 20:49:30 -0700 | [diff] [blame] | 9 | |
Paul Gortmaker | de47725 | 2011-05-26 13:46:22 -0400 | [diff] [blame] | 10 | struct module; |
| 11 | |
Thomas Graf | 2de4ff7 | 2005-06-23 20:49:30 -0700 | [diff] [blame] | 12 | struct ts_config; |
| 13 | |
Joonwoo Park | b9c7967 | 2008-07-08 02:37:31 -0700 | [diff] [blame] | 14 | #define TS_AUTOLOAD 1 /* Automatically load textsearch modules when needed */ |
| 15 | #define TS_IGNORECASE 2 /* Searches string case insensitively */ |
Thomas Graf | 2de4ff7 | 2005-06-23 20:49:30 -0700 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * struct ts_state - search state |
| 19 | * @offset: offset for next match |
Jan Engelhardt | 03a67a4 | 2006-11-30 05:32:19 +0100 | [diff] [blame] | 20 | * @cb: control buffer, for persistent variables of get_next_block() |
Thomas Graf | 2de4ff7 | 2005-06-23 20:49:30 -0700 | [diff] [blame] | 21 | */ |
| 22 | struct ts_state |
| 23 | { |
| 24 | unsigned int offset; |
| 25 | char cb[40]; |
| 26 | }; |
| 27 | |
| 28 | /** |
| 29 | * struct ts_ops - search module operations |
| 30 | * @name: name of search algorithm |
| 31 | * @init: initialization function to prepare a search |
| 32 | * @find: find the next occurrence of the pattern |
| 33 | * @destroy: destroy algorithm specific parts of a search configuration |
| 34 | * @get_pattern: return head of pattern |
| 35 | * @get_pattern_len: return length of pattern |
| 36 | * @owner: module reference to algorithm |
| 37 | */ |
| 38 | struct ts_ops |
| 39 | { |
| 40 | const char *name; |
Joonwoo Park | b9c7967 | 2008-07-08 02:37:31 -0700 | [diff] [blame] | 41 | struct ts_config * (*init)(const void *, unsigned int, gfp_t, int); |
Thomas Graf | 2de4ff7 | 2005-06-23 20:49:30 -0700 | [diff] [blame] | 42 | unsigned int (*find)(struct ts_config *, |
| 43 | struct ts_state *); |
| 44 | void (*destroy)(struct ts_config *); |
| 45 | void * (*get_pattern)(struct ts_config *); |
| 46 | unsigned int (*get_pattern_len)(struct ts_config *); |
| 47 | struct module *owner; |
| 48 | struct list_head list; |
| 49 | }; |
| 50 | |
| 51 | /** |
| 52 | * struct ts_config - search configuration |
| 53 | * @ops: operations of chosen algorithm |
Joonwoo Park | b9c7967 | 2008-07-08 02:37:31 -0700 | [diff] [blame] | 54 | * @flags: flags |
Thomas Graf | 2de4ff7 | 2005-06-23 20:49:30 -0700 | [diff] [blame] | 55 | * @get_next_block: callback to fetch the next block to search in |
| 56 | * @finish: callback to finalize a search |
| 57 | */ |
| 58 | struct ts_config |
| 59 | { |
| 60 | struct ts_ops *ops; |
Joonwoo Park | b9c7967 | 2008-07-08 02:37:31 -0700 | [diff] [blame] | 61 | int flags; |
Thomas Graf | 2de4ff7 | 2005-06-23 20:49:30 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * get_next_block - fetch next block of data |
| 65 | * @consumed: number of bytes consumed by the caller |
| 66 | * @dst: destination buffer |
| 67 | * @conf: search configuration |
| 68 | * @state: search state |
| 69 | * |
| 70 | * Called repeatedly until 0 is returned. Must assign the |
| 71 | * head of the next block of data to &*dst and return the length |
| 72 | * of the block or 0 if at the end. consumed == 0 indicates |
Jan Engelhardt | 03a67a4 | 2006-11-30 05:32:19 +0100 | [diff] [blame] | 73 | * a new search. May store/read persistent values in state->cb. |
Thomas Graf | 2de4ff7 | 2005-06-23 20:49:30 -0700 | [diff] [blame] | 74 | */ |
| 75 | unsigned int (*get_next_block)(unsigned int consumed, |
| 76 | const u8 **dst, |
| 77 | struct ts_config *conf, |
| 78 | struct ts_state *state); |
| 79 | |
| 80 | /** |
| 81 | * finish - finalize/clean a series of get_next_block() calls |
| 82 | * @conf: search configuration |
| 83 | * @state: search state |
| 84 | * |
| 85 | * Called after the last use of get_next_block(), may be used |
| 86 | * to cleanup any leftovers. |
| 87 | */ |
| 88 | void (*finish)(struct ts_config *conf, |
| 89 | struct ts_state *state); |
| 90 | }; |
| 91 | |
| 92 | /** |
| 93 | * textsearch_next - continue searching for a pattern |
| 94 | * @conf: search configuration |
| 95 | * @state: search state |
| 96 | * |
| 97 | * Continues a search looking for more occurrences of the pattern. |
| 98 | * textsearch_find() must be called to find the first occurrence |
| 99 | * in order to reset the state. |
| 100 | * |
| 101 | * Returns the position of the next occurrence of the pattern or |
| 102 | * UINT_MAX if not match was found. |
| 103 | */ |
| 104 | static inline unsigned int textsearch_next(struct ts_config *conf, |
| 105 | struct ts_state *state) |
| 106 | { |
| 107 | unsigned int ret = conf->ops->find(conf, state); |
| 108 | |
| 109 | if (conf->finish) |
| 110 | conf->finish(conf, state); |
| 111 | |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * textsearch_find - start searching for a pattern |
| 117 | * @conf: search configuration |
| 118 | * @state: search state |
| 119 | * |
| 120 | * Returns the position of first occurrence of the pattern or |
| 121 | * UINT_MAX if no match was found. |
| 122 | */ |
| 123 | static inline unsigned int textsearch_find(struct ts_config *conf, |
| 124 | struct ts_state *state) |
| 125 | { |
| 126 | state->offset = 0; |
| 127 | return textsearch_next(conf, state); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * textsearch_get_pattern - return head of the pattern |
| 132 | * @conf: search configuration |
| 133 | */ |
| 134 | static inline void *textsearch_get_pattern(struct ts_config *conf) |
| 135 | { |
| 136 | return conf->ops->get_pattern(conf); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * textsearch_get_pattern_len - return length of the pattern |
| 141 | * @conf: search configuration |
| 142 | */ |
| 143 | static inline unsigned int textsearch_get_pattern_len(struct ts_config *conf) |
| 144 | { |
| 145 | return conf->ops->get_pattern_len(conf); |
| 146 | } |
| 147 | |
| 148 | extern int textsearch_register(struct ts_ops *); |
| 149 | extern int textsearch_unregister(struct ts_ops *); |
| 150 | extern struct ts_config *textsearch_prepare(const char *, const void *, |
Al Viro | fd4f2df | 2005-10-21 03:18:50 -0400 | [diff] [blame] | 151 | unsigned int, gfp_t, int); |
Thomas Graf | 2de4ff7 | 2005-06-23 20:49:30 -0700 | [diff] [blame] | 152 | extern void textsearch_destroy(struct ts_config *conf); |
| 153 | extern unsigned int textsearch_find_continuous(struct ts_config *, |
| 154 | struct ts_state *, |
| 155 | const void *, unsigned int); |
| 156 | |
| 157 | |
| 158 | #define TS_PRIV_ALIGNTO 8 |
| 159 | #define TS_PRIV_ALIGN(len) (((len) + TS_PRIV_ALIGNTO-1) & ~(TS_PRIV_ALIGNTO-1)) |
| 160 | |
Randy Dunlap | 3d2aef6 | 2005-10-04 22:45:14 -0700 | [diff] [blame] | 161 | static inline struct ts_config *alloc_ts_config(size_t payload, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 162 | gfp_t gfp_mask) |
Thomas Graf | 2de4ff7 | 2005-06-23 20:49:30 -0700 | [diff] [blame] | 163 | { |
| 164 | struct ts_config *conf; |
| 165 | |
Joonwoo Park | dde77e60 | 2008-07-08 02:38:40 -0700 | [diff] [blame] | 166 | conf = kzalloc(TS_PRIV_ALIGN(sizeof(*conf)) + payload, gfp_mask); |
Thomas Graf | 2de4ff7 | 2005-06-23 20:49:30 -0700 | [diff] [blame] | 167 | if (conf == NULL) |
| 168 | return ERR_PTR(-ENOMEM); |
| 169 | |
Thomas Graf | 2de4ff7 | 2005-06-23 20:49:30 -0700 | [diff] [blame] | 170 | return conf; |
| 171 | } |
| 172 | |
| 173 | static inline void *ts_config_priv(struct ts_config *conf) |
| 174 | { |
| 175 | return ((u8 *) conf + TS_PRIV_ALIGN(sizeof(struct ts_config))); |
| 176 | } |
| 177 | |
Thomas Graf | 2de4ff7 | 2005-06-23 20:49:30 -0700 | [diff] [blame] | 178 | #endif |