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