12de4ff7bSThomas Graf #ifndef __LINUX_TEXTSEARCH_H 22de4ff7bSThomas Graf #define __LINUX_TEXTSEARCH_H 32de4ff7bSThomas Graf 42de4ff7bSThomas Graf #include <linux/types.h> 52de4ff7bSThomas Graf #include <linux/list.h> 62de4ff7bSThomas Graf #include <linux/kernel.h> 72de4ff7bSThomas Graf #include <linux/err.h> 84e57b681STim Schmielau #include <linux/slab.h> 92de4ff7bSThomas Graf 10*de477254SPaul Gortmaker struct module; 11*de477254SPaul Gortmaker 122de4ff7bSThomas Graf struct ts_config; 132de4ff7bSThomas Graf 14b9c79678SJoonwoo Park #define TS_AUTOLOAD 1 /* Automatically load textsearch modules when needed */ 15b9c79678SJoonwoo Park #define TS_IGNORECASE 2 /* Searches string case insensitively */ 162de4ff7bSThomas Graf 172de4ff7bSThomas Graf /** 182de4ff7bSThomas Graf * struct ts_state - search state 192de4ff7bSThomas Graf * @offset: offset for next match 2003a67a46SJan Engelhardt * @cb: control buffer, for persistent variables of get_next_block() 212de4ff7bSThomas Graf */ 222de4ff7bSThomas Graf struct ts_state 232de4ff7bSThomas Graf { 242de4ff7bSThomas Graf unsigned int offset; 252de4ff7bSThomas Graf char cb[40]; 262de4ff7bSThomas Graf }; 272de4ff7bSThomas Graf 282de4ff7bSThomas Graf /** 292de4ff7bSThomas Graf * struct ts_ops - search module operations 302de4ff7bSThomas Graf * @name: name of search algorithm 312de4ff7bSThomas Graf * @init: initialization function to prepare a search 322de4ff7bSThomas Graf * @find: find the next occurrence of the pattern 332de4ff7bSThomas Graf * @destroy: destroy algorithm specific parts of a search configuration 342de4ff7bSThomas Graf * @get_pattern: return head of pattern 352de4ff7bSThomas Graf * @get_pattern_len: return length of pattern 362de4ff7bSThomas Graf * @owner: module reference to algorithm 372de4ff7bSThomas Graf */ 382de4ff7bSThomas Graf struct ts_ops 392de4ff7bSThomas Graf { 402de4ff7bSThomas Graf const char *name; 41b9c79678SJoonwoo Park struct ts_config * (*init)(const void *, unsigned int, gfp_t, int); 422de4ff7bSThomas Graf unsigned int (*find)(struct ts_config *, 432de4ff7bSThomas Graf struct ts_state *); 442de4ff7bSThomas Graf void (*destroy)(struct ts_config *); 452de4ff7bSThomas Graf void * (*get_pattern)(struct ts_config *); 462de4ff7bSThomas Graf unsigned int (*get_pattern_len)(struct ts_config *); 472de4ff7bSThomas Graf struct module *owner; 482de4ff7bSThomas Graf struct list_head list; 492de4ff7bSThomas Graf }; 502de4ff7bSThomas Graf 512de4ff7bSThomas Graf /** 522de4ff7bSThomas Graf * struct ts_config - search configuration 532de4ff7bSThomas Graf * @ops: operations of chosen algorithm 54b9c79678SJoonwoo Park * @flags: flags 552de4ff7bSThomas Graf * @get_next_block: callback to fetch the next block to search in 562de4ff7bSThomas Graf * @finish: callback to finalize a search 572de4ff7bSThomas Graf */ 582de4ff7bSThomas Graf struct ts_config 592de4ff7bSThomas Graf { 602de4ff7bSThomas Graf struct ts_ops *ops; 61b9c79678SJoonwoo Park int flags; 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 7303a67a46SJan Engelhardt * a new search. May store/read persistent 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 *, 151fd4f2df2SAl Viro unsigned int, gfp_t, 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 1613d2aef66SRandy Dunlap static inline struct ts_config *alloc_ts_config(size_t payload, 162dd0fc66fSAl Viro gfp_t gfp_mask) 1632de4ff7bSThomas Graf { 1642de4ff7bSThomas Graf struct ts_config *conf; 1652de4ff7bSThomas Graf 166dde77e60SJoonwoo Park conf = kzalloc(TS_PRIV_ALIGN(sizeof(*conf)) + payload, gfp_mask); 1672de4ff7bSThomas Graf if (conf == NULL) 1682de4ff7bSThomas Graf return ERR_PTR(-ENOMEM); 1692de4ff7bSThomas Graf 1702de4ff7bSThomas Graf return conf; 1712de4ff7bSThomas Graf } 1722de4ff7bSThomas Graf 1732de4ff7bSThomas Graf static inline void *ts_config_priv(struct ts_config *conf) 1742de4ff7bSThomas Graf { 1752de4ff7bSThomas Graf return ((u8 *) conf + TS_PRIV_ALIGN(sizeof(struct ts_config))); 1762de4ff7bSThomas Graf } 1772de4ff7bSThomas Graf 1782de4ff7bSThomas Graf #endif 179