15976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#ifndef __LINUX_TEXTSEARCH_H 25976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define __LINUX_TEXTSEARCH_H 35976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 45976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#ifdef __KERNEL__ 55976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 65976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include <linux/types.h> 75976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include <linux/list.h> 85976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include <linux/kernel.h> 95976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include <linux/module.h> 105976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include <linux/err.h> 115976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include <linux/slab.h> 125976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 135976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgstruct ts_config; 145976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 155976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org/** 165976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * TS_AUTOLOAD - Automatically load textsearch modules when needed 175976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org */ 185976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define TS_AUTOLOAD 1 195976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 205976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org/** 215976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * struct ts_state - search state 225976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * @offset: offset for next match 235976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * @cb: control buffer, for persistant variables of get_next_block() 245976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org */ 255976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgstruct ts_state 265976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org{ 275976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org unsigned int offset; 285976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org char cb[40]; 295976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}; 305976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 315976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org/** 325976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * struct ts_ops - search module operations 335976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * @name: name of search algorithm 345976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * @init: initialization function to prepare a search 355976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * @find: find the next occurrence of the pattern 365976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * @destroy: destroy algorithm specific parts of a search configuration 375976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * @get_pattern: return head of pattern 385976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * @get_pattern_len: return length of pattern 395976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * @owner: module reference to algorithm 405976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org */ 415976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgstruct ts_ops 425976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org{ 435976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org const char *name; 445976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org struct ts_config * (*init)(const void *, unsigned int, gfp_t); 455976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org unsigned int (*find)(struct ts_config *, 465976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org struct ts_state *); 475976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org void (*destroy)(struct ts_config *); 485976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org void * (*get_pattern)(struct ts_config *); 495976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org unsigned int (*get_pattern_len)(struct ts_config *); 505976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org struct module *owner; 515976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org struct list_head list; 525976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}; 535976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 545976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org/** 555976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * struct ts_config - search configuration 565976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * @ops: operations of chosen algorithm 575976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * @get_next_block: callback to fetch the next block to search in 585976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * @finish: callback to finalize a search 595976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org */ 605976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgstruct ts_config 615976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org{ 625976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org struct ts_ops *ops; 635976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 645976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org /** 655976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * get_next_block - fetch next block of data 665976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * @consumed: number of bytes consumed by the caller 675976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * @dst: destination buffer 685976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * @conf: search configuration 695976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * @state: search state 705976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * 715976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Called repeatedly until 0 is returned. Must assign the 725976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * head of the next block of data to &*dst and return the length 735976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * of the block or 0 if at the end. consumed == 0 indicates 745976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * a new search. May store/read persistant values in state->cb. 755976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org */ 765976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org unsigned int (*get_next_block)(unsigned int consumed, 775976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org const u8 **dst, 785976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org struct ts_config *conf, 795976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org struct ts_state *state); 805976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 815976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org /** 825976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * finish - finalize/clean a series of get_next_block() calls 835976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * @conf: search configuration 845976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * @state: search state 855976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * 865976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Called after the last use of get_next_block(), may be used 875976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * to cleanup any leftovers. 885976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org */ 895976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org void (*finish)(struct ts_config *conf, 905976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org struct ts_state *state); 915976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}; 925976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 935976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org/** 945976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * textsearch_next - continue searching for a pattern 95 * @conf: search configuration 96 * @state: search state 97 * 98 * Continues a search looking for more occurrences of the pattern. 99 * textsearch_find() must be called to find the first occurrence 100 * in order to reset the state. 101 * 102 * Returns the position of the next occurrence of the pattern or 103 * UINT_MAX if not match was found. 104 */ 105static inline unsigned int textsearch_next(struct ts_config *conf, 106 struct ts_state *state) 107{ 108 unsigned int ret = conf->ops->find(conf, state); 109 110 if (conf->finish) 111 conf->finish(conf, state); 112 113 return ret; 114} 115 116/** 117 * textsearch_find - start searching for a pattern 118 * @conf: search configuration 119 * @state: search state 120 * 121 * Returns the position of first occurrence of the pattern or 122 * UINT_MAX if no match was found. 123 */ 124static inline unsigned int textsearch_find(struct ts_config *conf, 125 struct ts_state *state) 126{ 127 state->offset = 0; 128 return textsearch_next(conf, state); 129} 130 131/** 132 * textsearch_get_pattern - return head of the pattern 133 * @conf: search configuration 134 */ 135static inline void *textsearch_get_pattern(struct ts_config *conf) 136{ 137 return conf->ops->get_pattern(conf); 138} 139 140/** 141 * textsearch_get_pattern_len - return length of the pattern 142 * @conf: search configuration 143 */ 144static inline unsigned int textsearch_get_pattern_len(struct ts_config *conf) 145{ 146 return conf->ops->get_pattern_len(conf); 147} 148 149extern int textsearch_register(struct ts_ops *); 150extern int textsearch_unregister(struct ts_ops *); 151extern struct ts_config *textsearch_prepare(const char *, const void *, 152 unsigned int, gfp_t, int); 153extern void textsearch_destroy(struct ts_config *conf); 154extern unsigned int textsearch_find_continuous(struct ts_config *, 155 struct ts_state *, 156 const void *, unsigned int); 157 158 159#define TS_PRIV_ALIGNTO 8 160#define TS_PRIV_ALIGN(len) (((len) + TS_PRIV_ALIGNTO-1) & ~(TS_PRIV_ALIGNTO-1)) 161 162static inline struct ts_config *alloc_ts_config(size_t payload, 163 gfp_t gfp_mask) 164{ 165 struct ts_config *conf; 166 167 conf = kmalloc(TS_PRIV_ALIGN(sizeof(*conf)) + payload, gfp_mask); 168 if (conf == NULL) 169 return ERR_PTR(-ENOMEM); 170 171 memset(conf, 0, TS_PRIV_ALIGN(sizeof(*conf)) + payload); 172 return conf; 173} 174 175static inline void *ts_config_priv(struct ts_config *conf) 176{ 177 return ((u8 *) conf + TS_PRIV_ALIGN(sizeof(struct ts_config))); 178} 179 180#endif /* __KERNEL__ */ 181 182#endif 183