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