12de4ff7bSThomas Graf #ifndef __LINUX_TEXTSEARCH_H 22de4ff7bSThomas Graf #define __LINUX_TEXTSEARCH_H 32de4ff7bSThomas Graf 42de4ff7bSThomas Graf #ifdef __KERNEL__ 52de4ff7bSThomas Graf 62de4ff7bSThomas Graf #include <linux/types.h> 72de4ff7bSThomas Graf #include <linux/list.h> 82de4ff7bSThomas Graf #include <linux/kernel.h> 92de4ff7bSThomas Graf #include <linux/module.h> 102de4ff7bSThomas Graf #include <linux/err.h> 11*4e57b681STim Schmielau #include <linux/slab.h> 122de4ff7bSThomas Graf 132de4ff7bSThomas Graf struct ts_config; 142de4ff7bSThomas Graf 152de4ff7bSThomas Graf /** 162de4ff7bSThomas Graf * TS_AUTOLOAD - Automatically load textsearch modules when needed 172de4ff7bSThomas Graf */ 182de4ff7bSThomas Graf #define TS_AUTOLOAD 1 192de4ff7bSThomas Graf 202de4ff7bSThomas Graf /** 212de4ff7bSThomas Graf * struct ts_state - search state 222de4ff7bSThomas Graf * @offset: offset for next match 232de4ff7bSThomas Graf * @cb: control buffer, for persistant variables of get_next_block() 242de4ff7bSThomas Graf */ 252de4ff7bSThomas Graf struct ts_state 262de4ff7bSThomas Graf { 272de4ff7bSThomas Graf unsigned int offset; 282de4ff7bSThomas Graf char cb[40]; 292de4ff7bSThomas Graf }; 302de4ff7bSThomas Graf 312de4ff7bSThomas Graf /** 322de4ff7bSThomas Graf * struct ts_ops - search module operations 332de4ff7bSThomas Graf * @name: name of search algorithm 342de4ff7bSThomas Graf * @init: initialization function to prepare a search 352de4ff7bSThomas Graf * @find: find the next occurrence of the pattern 362de4ff7bSThomas Graf * @destroy: destroy algorithm specific parts of a search configuration 372de4ff7bSThomas Graf * @get_pattern: return head of pattern 382de4ff7bSThomas Graf * @get_pattern_len: return length of pattern 392de4ff7bSThomas Graf * @owner: module reference to algorithm 402de4ff7bSThomas Graf */ 412de4ff7bSThomas Graf struct ts_ops 422de4ff7bSThomas Graf { 432de4ff7bSThomas Graf const char *name; 44fd4f2df2SAl Viro struct ts_config * (*init)(const void *, unsigned int, gfp_t); 452de4ff7bSThomas Graf unsigned int (*find)(struct ts_config *, 462de4ff7bSThomas Graf struct ts_state *); 472de4ff7bSThomas Graf void (*destroy)(struct ts_config *); 482de4ff7bSThomas Graf void * (*get_pattern)(struct ts_config *); 492de4ff7bSThomas Graf unsigned int (*get_pattern_len)(struct ts_config *); 502de4ff7bSThomas Graf struct module *owner; 512de4ff7bSThomas Graf struct list_head list; 522de4ff7bSThomas Graf }; 532de4ff7bSThomas Graf 542de4ff7bSThomas Graf /** 552de4ff7bSThomas Graf * struct ts_config - search configuration 562de4ff7bSThomas Graf * @ops: operations of chosen algorithm 572de4ff7bSThomas Graf * @get_next_block: callback to fetch the next block to search in 582de4ff7bSThomas Graf * @finish: callback to finalize a search 592de4ff7bSThomas Graf */ 602de4ff7bSThomas Graf struct ts_config 612de4ff7bSThomas Graf { 622de4ff7bSThomas Graf struct ts_ops *ops; 632de4ff7bSThomas Graf 642de4ff7bSThomas Graf /** 652de4ff7bSThomas Graf * get_next_block - fetch next block of data 662de4ff7bSThomas Graf * @consumed: number of bytes consumed by the caller 672de4ff7bSThomas Graf * @dst: destination buffer 682de4ff7bSThomas Graf * @conf: search configuration 692de4ff7bSThomas Graf * @state: search state 702de4ff7bSThomas Graf * 712de4ff7bSThomas Graf * Called repeatedly until 0 is returned. Must assign the 722de4ff7bSThomas Graf * head of the next block of data to &*dst and return the length 732de4ff7bSThomas Graf * of the block or 0 if at the end. consumed == 0 indicates 742de4ff7bSThomas Graf * a new search. May store/read persistant values in state->cb. 752de4ff7bSThomas Graf */ 762de4ff7bSThomas Graf unsigned int (*get_next_block)(unsigned int consumed, 772de4ff7bSThomas Graf const u8 **dst, 782de4ff7bSThomas Graf struct ts_config *conf, 792de4ff7bSThomas Graf struct ts_state *state); 802de4ff7bSThomas Graf 812de4ff7bSThomas Graf /** 822de4ff7bSThomas Graf * finish - finalize/clean a series of get_next_block() calls 832de4ff7bSThomas Graf * @conf: search configuration 842de4ff7bSThomas Graf * @state: search state 852de4ff7bSThomas Graf * 862de4ff7bSThomas Graf * Called after the last use of get_next_block(), may be used 872de4ff7bSThomas Graf * to cleanup any leftovers. 882de4ff7bSThomas Graf */ 892de4ff7bSThomas Graf void (*finish)(struct ts_config *conf, 902de4ff7bSThomas Graf struct ts_state *state); 912de4ff7bSThomas Graf }; 922de4ff7bSThomas Graf 932de4ff7bSThomas Graf /** 942de4ff7bSThomas Graf * textsearch_next - continue searching for a pattern 952de4ff7bSThomas Graf * @conf: search configuration 962de4ff7bSThomas Graf * @state: search state 972de4ff7bSThomas Graf * 982de4ff7bSThomas Graf * Continues a search looking for more occurrences of the pattern. 992de4ff7bSThomas Graf * textsearch_find() must be called to find the first occurrence 1002de4ff7bSThomas Graf * in order to reset the state. 1012de4ff7bSThomas Graf * 1022de4ff7bSThomas Graf * Returns the position of the next occurrence of the pattern or 1032de4ff7bSThomas Graf * UINT_MAX if not match was found. 1042de4ff7bSThomas Graf */ 1052de4ff7bSThomas Graf static inline unsigned int textsearch_next(struct ts_config *conf, 1062de4ff7bSThomas Graf struct ts_state *state) 1072de4ff7bSThomas Graf { 1082de4ff7bSThomas Graf unsigned int ret = conf->ops->find(conf, state); 1092de4ff7bSThomas Graf 1102de4ff7bSThomas Graf if (conf->finish) 1112de4ff7bSThomas Graf conf->finish(conf, state); 1122de4ff7bSThomas Graf 1132de4ff7bSThomas Graf return ret; 1142de4ff7bSThomas Graf } 1152de4ff7bSThomas Graf 1162de4ff7bSThomas Graf /** 1172de4ff7bSThomas Graf * textsearch_find - start searching for a pattern 1182de4ff7bSThomas Graf * @conf: search configuration 1192de4ff7bSThomas Graf * @state: search state 1202de4ff7bSThomas Graf * 1212de4ff7bSThomas Graf * Returns the position of first occurrence of the pattern or 1222de4ff7bSThomas Graf * UINT_MAX if no match was found. 1232de4ff7bSThomas Graf */ 1242de4ff7bSThomas Graf static inline unsigned int textsearch_find(struct ts_config *conf, 1252de4ff7bSThomas Graf struct ts_state *state) 1262de4ff7bSThomas Graf { 1272de4ff7bSThomas Graf state->offset = 0; 1282de4ff7bSThomas Graf return textsearch_next(conf, state); 1292de4ff7bSThomas Graf } 1302de4ff7bSThomas Graf 1312de4ff7bSThomas Graf /** 1322de4ff7bSThomas Graf * textsearch_get_pattern - return head of the pattern 1332de4ff7bSThomas Graf * @conf: search configuration 1342de4ff7bSThomas Graf */ 1352de4ff7bSThomas Graf static inline void *textsearch_get_pattern(struct ts_config *conf) 1362de4ff7bSThomas Graf { 1372de4ff7bSThomas Graf return conf->ops->get_pattern(conf); 1382de4ff7bSThomas Graf } 1392de4ff7bSThomas Graf 1402de4ff7bSThomas Graf /** 1412de4ff7bSThomas Graf * textsearch_get_pattern_len - return length of the pattern 1422de4ff7bSThomas Graf * @conf: search configuration 1432de4ff7bSThomas Graf */ 1442de4ff7bSThomas Graf static inline unsigned int textsearch_get_pattern_len(struct ts_config *conf) 1452de4ff7bSThomas Graf { 1462de4ff7bSThomas Graf return conf->ops->get_pattern_len(conf); 1472de4ff7bSThomas Graf } 1482de4ff7bSThomas Graf 1492de4ff7bSThomas Graf extern int textsearch_register(struct ts_ops *); 1502de4ff7bSThomas Graf extern int textsearch_unregister(struct ts_ops *); 1512de4ff7bSThomas Graf extern struct ts_config *textsearch_prepare(const char *, const void *, 152fd4f2df2SAl Viro unsigned int, gfp_t, int); 1532de4ff7bSThomas Graf extern void textsearch_destroy(struct ts_config *conf); 1542de4ff7bSThomas Graf extern unsigned int textsearch_find_continuous(struct ts_config *, 1552de4ff7bSThomas Graf struct ts_state *, 1562de4ff7bSThomas Graf const void *, unsigned int); 1572de4ff7bSThomas Graf 1582de4ff7bSThomas Graf 1592de4ff7bSThomas Graf #define TS_PRIV_ALIGNTO 8 1602de4ff7bSThomas Graf #define TS_PRIV_ALIGN(len) (((len) + TS_PRIV_ALIGNTO-1) & ~(TS_PRIV_ALIGNTO-1)) 1612de4ff7bSThomas Graf 1623d2aef66SRandy Dunlap static inline struct ts_config *alloc_ts_config(size_t payload, 163dd0fc66fSAl Viro gfp_t gfp_mask) 1642de4ff7bSThomas Graf { 1652de4ff7bSThomas Graf struct ts_config *conf; 1662de4ff7bSThomas Graf 1672de4ff7bSThomas Graf conf = kmalloc(TS_PRIV_ALIGN(sizeof(*conf)) + payload, gfp_mask); 1682de4ff7bSThomas Graf if (conf == NULL) 1692de4ff7bSThomas Graf return ERR_PTR(-ENOMEM); 1702de4ff7bSThomas Graf 1712de4ff7bSThomas Graf memset(conf, 0, TS_PRIV_ALIGN(sizeof(*conf)) + payload); 1722de4ff7bSThomas Graf return conf; 1732de4ff7bSThomas Graf } 1742de4ff7bSThomas Graf 1752de4ff7bSThomas Graf static inline void *ts_config_priv(struct ts_config *conf) 1762de4ff7bSThomas Graf { 1772de4ff7bSThomas Graf return ((u8 *) conf + TS_PRIV_ALIGN(sizeof(struct ts_config))); 1782de4ff7bSThomas Graf } 1792de4ff7bSThomas Graf 1802de4ff7bSThomas Graf #endif /* __KERNEL__ */ 1812de4ff7bSThomas Graf 1822de4ff7bSThomas Graf #endif 183