1 #ifndef __LINUX_TEXTSEARCH_H
2 #define __LINUX_TEXTSEARCH_H
6 #include <linux/types.h>
7 #include <linux/list.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/err.h>
15 * TS_AUTOLOAD - Automatically load textsearch modules when needed
20 * struct ts_state - search state
21 * @offset: offset for next match
22 * @cb: control buffer, for persistant variables of get_next_block()
31 * struct ts_ops - search module operations
32 * @name: name of search algorithm
33 * @init: initialization function to prepare a search
34 * @find: find the next occurrence of the pattern
35 * @destroy: destroy algorithm specific parts of a search configuration
36 * @get_pattern: return head of pattern
37 * @get_pattern_len: return length of pattern
38 * @owner: module reference to algorithm
43 struct ts_config
* (*init
)(const void *, unsigned int, int);
44 unsigned int (*find
)(struct ts_config
*,
46 void (*destroy
)(struct ts_config
*);
47 void * (*get_pattern
)(struct ts_config
*);
48 unsigned int (*get_pattern_len
)(struct ts_config
*);
50 struct list_head list
;
54 * struct ts_config - search configuration
55 * @ops: operations of chosen algorithm
56 * @get_next_block: callback to fetch the next block to search in
57 * @finish: callback to finalize a search
64 * get_next_block - fetch next block of data
65 * @consumed: number of bytes consumed by the caller
66 * @dst: destination buffer
67 * @conf: search configuration
68 * @state: search state
70 * Called repeatedly until 0 is returned. Must assign the
71 * head of the next block of data to &*dst and return the length
72 * of the block or 0 if at the end. consumed == 0 indicates
73 * a new search. May store/read persistant values in state->cb.
75 unsigned int (*get_next_block
)(unsigned int consumed
,
77 struct ts_config
*conf
,
78 struct ts_state
*state
);
81 * finish - finalize/clean a series of get_next_block() calls
82 * @conf: search configuration
83 * @state: search state
85 * Called after the last use of get_next_block(), may be used
86 * to cleanup any leftovers.
88 void (*finish
)(struct ts_config
*conf
,
89 struct ts_state
*state
);
93 * textsearch_next - continue searching for a pattern
94 * @conf: search configuration
95 * @state: search state
97 * Continues a search looking for more occurrences of the pattern.
98 * textsearch_find() must be called to find the first occurrence
99 * in order to reset the state.
101 * Returns the position of the next occurrence of the pattern or
102 * UINT_MAX if not match was found.
104 static inline unsigned int textsearch_next(struct ts_config
*conf
,
105 struct ts_state
*state
)
107 unsigned int ret
= conf
->ops
->find(conf
, state
);
110 conf
->finish(conf
, state
);
116 * textsearch_find - start searching for a pattern
117 * @conf: search configuration
118 * @state: search state
120 * Returns the position of first occurrence of the pattern or
121 * UINT_MAX if no match was found.
123 static inline unsigned int textsearch_find(struct ts_config
*conf
,
124 struct ts_state
*state
)
127 return textsearch_next(conf
, state
);
131 * textsearch_get_pattern - return head of the pattern
132 * @conf: search configuration
134 static inline void *textsearch_get_pattern(struct ts_config
*conf
)
136 return conf
->ops
->get_pattern(conf
);
140 * textsearch_get_pattern_len - return length of the pattern
141 * @conf: search configuration
143 static inline unsigned int textsearch_get_pattern_len(struct ts_config
*conf
)
145 return conf
->ops
->get_pattern_len(conf
);
148 extern int textsearch_register(struct ts_ops
*);
149 extern int textsearch_unregister(struct ts_ops
*);
150 extern struct ts_config
*textsearch_prepare(const char *, const void *,
151 unsigned int, int, int);
152 extern void textsearch_destroy(struct ts_config
*conf
);
153 extern unsigned int textsearch_find_continuous(struct ts_config
*,
155 const void *, unsigned int);
158 #define TS_PRIV_ALIGNTO 8
159 #define TS_PRIV_ALIGN(len) (((len) + TS_PRIV_ALIGNTO-1) & ~(TS_PRIV_ALIGNTO-1))
161 static inline struct ts_config
*alloc_ts_config(size_t payload
,
164 struct ts_config
*conf
;
166 conf
= kmalloc(TS_PRIV_ALIGN(sizeof(*conf
)) + payload
, gfp_mask
);
168 return ERR_PTR(-ENOMEM
);
170 memset(conf
, 0, TS_PRIV_ALIGN(sizeof(*conf
)) + payload
);
174 static inline void *ts_config_priv(struct ts_config
*conf
)
176 return ((u8
*) conf
+ TS_PRIV_ALIGN(sizeof(struct ts_config
)));
179 #endif /* __KERNEL__ */