xref: /openbmc/linux/include/linux/textsearch.h (revision b228c9b0)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
22de4ff7bSThomas Graf #ifndef __LINUX_TEXTSEARCH_H
32de4ff7bSThomas Graf #define __LINUX_TEXTSEARCH_H
42de4ff7bSThomas Graf 
52de4ff7bSThomas Graf #include <linux/types.h>
62de4ff7bSThomas Graf #include <linux/list.h>
72de4ff7bSThomas Graf #include <linux/kernel.h>
82de4ff7bSThomas Graf #include <linux/err.h>
94e57b681STim Schmielau #include <linux/slab.h>
102de4ff7bSThomas Graf 
11de477254SPaul Gortmaker struct module;
12de477254SPaul Gortmaker 
132de4ff7bSThomas Graf struct ts_config;
142de4ff7bSThomas Graf 
15b9c79678SJoonwoo Park #define TS_AUTOLOAD	1 /* Automatically load textsearch modules when needed */
16b9c79678SJoonwoo Park #define TS_IGNORECASE	2 /* Searches string case insensitively */
172de4ff7bSThomas Graf 
182de4ff7bSThomas Graf /**
192de4ff7bSThomas Graf  * struct ts_state - search state
202de4ff7bSThomas Graf  * @offset: offset for next match
2103a67a46SJan Engelhardt  * @cb: control buffer, for persistent variables of get_next_block()
222de4ff7bSThomas Graf  */
232de4ff7bSThomas Graf struct ts_state
242de4ff7bSThomas Graf {
252de4ff7bSThomas Graf 	unsigned int		offset;
26*b228c9b0SWillem de Bruijn 	char			cb[48];
272de4ff7bSThomas Graf };
282de4ff7bSThomas Graf 
292de4ff7bSThomas Graf /**
302de4ff7bSThomas Graf  * struct ts_ops - search module operations
312de4ff7bSThomas Graf  * @name: name of search algorithm
322de4ff7bSThomas Graf  * @init: initialization function to prepare a search
332de4ff7bSThomas Graf  * @find: find the next occurrence of the pattern
342de4ff7bSThomas Graf  * @destroy: destroy algorithm specific parts of a search configuration
352de4ff7bSThomas Graf  * @get_pattern: return head of pattern
362de4ff7bSThomas Graf  * @get_pattern_len: return length of pattern
372de4ff7bSThomas Graf  * @owner: module reference to algorithm
382de4ff7bSThomas Graf  */
392de4ff7bSThomas Graf struct ts_ops
402de4ff7bSThomas Graf {
412de4ff7bSThomas Graf 	const char		*name;
42b9c79678SJoonwoo Park 	struct ts_config *	(*init)(const void *, unsigned int, gfp_t, int);
432de4ff7bSThomas Graf 	unsigned int		(*find)(struct ts_config *,
442de4ff7bSThomas Graf 					struct ts_state *);
452de4ff7bSThomas Graf 	void			(*destroy)(struct ts_config *);
462de4ff7bSThomas Graf 	void *			(*get_pattern)(struct ts_config *);
472de4ff7bSThomas Graf 	unsigned int		(*get_pattern_len)(struct ts_config *);
482de4ff7bSThomas Graf 	struct module		*owner;
492de4ff7bSThomas Graf 	struct list_head	list;
502de4ff7bSThomas Graf };
512de4ff7bSThomas Graf 
522de4ff7bSThomas Graf /**
532de4ff7bSThomas Graf  * struct ts_config - search configuration
542de4ff7bSThomas Graf  * @ops: operations of chosen algorithm
55b9c79678SJoonwoo Park  * @flags: flags
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;
62b9c79678SJoonwoo Park 	int 			flags;
632de4ff7bSThomas Graf 
642de4ff7bSThomas Graf 	/**
655968a70dSRandy Dunlap 	 * @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
7403a67a46SJan Engelhardt 	 * a new search. May store/read persistent 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 	/**
825968a70dSRandy Dunlap 	 * @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  */
textsearch_next(struct ts_config * conf,struct ts_state * state)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  */
textsearch_find(struct ts_config * conf,struct ts_state * state)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  */
textsearch_get_pattern(struct ts_config * conf)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  */
textsearch_get_pattern_len(struct ts_config * conf)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 
alloc_ts_config(size_t payload,gfp_t gfp_mask)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 
167dde77e60SJoonwoo Park 	conf = kzalloc(TS_PRIV_ALIGN(sizeof(*conf)) + payload, gfp_mask);
1682de4ff7bSThomas Graf 	if (conf == NULL)
1692de4ff7bSThomas Graf 		return ERR_PTR(-ENOMEM);
1702de4ff7bSThomas Graf 
1712de4ff7bSThomas Graf 	return conf;
1722de4ff7bSThomas Graf }
1732de4ff7bSThomas Graf 
ts_config_priv(struct ts_config * conf)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
180