Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __LINUX_TEXTSEARCH_H
0003 #define __LINUX_TEXTSEARCH_H
0004 
0005 #include <linux/types.h>
0006 #include <linux/list.h>
0007 #include <linux/kernel.h>
0008 #include <linux/err.h>
0009 #include <linux/slab.h>
0010 
0011 struct module;
0012 
0013 struct ts_config;
0014 
0015 #define TS_AUTOLOAD 1 /* Automatically load textsearch modules when needed */
0016 #define TS_IGNORECASE   2 /* Searches string case insensitively */
0017 
0018 /**
0019  * struct ts_state - search state
0020  * @offset: offset for next match
0021  * @cb: control buffer, for persistent variables of get_next_block()
0022  */
0023 struct ts_state
0024 {
0025     unsigned int        offset;
0026     char            cb[48];
0027 };
0028 
0029 /**
0030  * struct ts_ops - search module operations
0031  * @name: name of search algorithm
0032  * @init: initialization function to prepare a search
0033  * @find: find the next occurrence of the pattern
0034  * @destroy: destroy algorithm specific parts of a search configuration
0035  * @get_pattern: return head of pattern
0036  * @get_pattern_len: return length of pattern
0037  * @owner: module reference to algorithm
0038  */
0039 struct ts_ops
0040 {
0041     const char      *name;
0042     struct ts_config *  (*init)(const void *, unsigned int, gfp_t, int);
0043     unsigned int        (*find)(struct ts_config *,
0044                     struct ts_state *);
0045     void            (*destroy)(struct ts_config *);
0046     void *          (*get_pattern)(struct ts_config *);
0047     unsigned int        (*get_pattern_len)(struct ts_config *);
0048     struct module       *owner;
0049     struct list_head    list;
0050 };
0051 
0052 /**
0053  * struct ts_config - search configuration
0054  * @ops: operations of chosen algorithm
0055  * @flags: flags
0056  * @get_next_block: callback to fetch the next block to search in
0057  * @finish: callback to finalize a search
0058  */
0059 struct ts_config
0060 {
0061     struct ts_ops       *ops;
0062     int             flags;
0063 
0064     /**
0065      * @get_next_block: fetch next block of data
0066      * @consumed: number of bytes consumed by the caller
0067      * @dst: destination buffer
0068      * @conf: search configuration
0069      * @state: search state
0070      *
0071      * Called repeatedly until 0 is returned. Must assign the
0072      * head of the next block of data to &*dst and return the length
0073      * of the block or 0 if at the end. consumed == 0 indicates
0074      * a new search. May store/read persistent values in state->cb.
0075      */
0076     unsigned int        (*get_next_block)(unsigned int consumed,
0077                           const u8 **dst,
0078                           struct ts_config *conf,
0079                           struct ts_state *state);
0080 
0081     /**
0082      * @finish: finalize/clean a series of get_next_block() calls
0083      * @conf: search configuration
0084      * @state: search state
0085      *
0086      * Called after the last use of get_next_block(), may be used
0087      * to cleanup any leftovers.
0088      */
0089     void            (*finish)(struct ts_config *conf,
0090                       struct ts_state *state);
0091 };
0092 
0093 /**
0094  * textsearch_next - continue searching for a pattern
0095  * @conf: search configuration
0096  * @state: search state
0097  *
0098  * Continues a search looking for more occurrences of the pattern.
0099  * textsearch_find() must be called to find the first occurrence
0100  * in order to reset the state.
0101  *
0102  * Returns the position of the next occurrence of the pattern or
0103  * UINT_MAX if not match was found.
0104  */ 
0105 static inline unsigned int textsearch_next(struct ts_config *conf,
0106                        struct ts_state *state)
0107 {
0108     unsigned int ret = conf->ops->find(conf, state);
0109 
0110     if (conf->finish)
0111         conf->finish(conf, state);
0112 
0113     return ret;
0114 }
0115 
0116 /**
0117  * textsearch_find - start searching for a pattern
0118  * @conf: search configuration
0119  * @state: search state
0120  *
0121  * Returns the position of first occurrence of the pattern or
0122  * UINT_MAX if no match was found.
0123  */ 
0124 static inline unsigned int textsearch_find(struct ts_config *conf,
0125                        struct ts_state *state)
0126 {
0127     state->offset = 0;
0128     return textsearch_next(conf, state);
0129 }
0130 
0131 /**
0132  * textsearch_get_pattern - return head of the pattern
0133  * @conf: search configuration
0134  */
0135 static inline void *textsearch_get_pattern(struct ts_config *conf)
0136 {
0137     return conf->ops->get_pattern(conf);
0138 }
0139 
0140 /**
0141  * textsearch_get_pattern_len - return length of the pattern
0142  * @conf: search configuration
0143  */
0144 static inline unsigned int textsearch_get_pattern_len(struct ts_config *conf)
0145 {
0146     return conf->ops->get_pattern_len(conf);
0147 }
0148 
0149 extern int textsearch_register(struct ts_ops *);
0150 extern int textsearch_unregister(struct ts_ops *);
0151 extern struct ts_config *textsearch_prepare(const char *, const void *,
0152                         unsigned int, gfp_t, int);
0153 extern void textsearch_destroy(struct ts_config *conf);
0154 extern unsigned int textsearch_find_continuous(struct ts_config *,
0155                            struct ts_state *,
0156                            const void *, unsigned int);
0157 
0158 
0159 #define TS_PRIV_ALIGNTO 8
0160 #define TS_PRIV_ALIGN(len) (((len) + TS_PRIV_ALIGNTO-1) & ~(TS_PRIV_ALIGNTO-1))
0161 
0162 static inline struct ts_config *alloc_ts_config(size_t payload,
0163                         gfp_t gfp_mask)
0164 {
0165     struct ts_config *conf;
0166 
0167     conf = kzalloc(TS_PRIV_ALIGN(sizeof(*conf)) + payload, gfp_mask);
0168     if (conf == NULL)
0169         return ERR_PTR(-ENOMEM);
0170 
0171     return conf;
0172 }
0173 
0174 static inline void *ts_config_priv(struct ts_config *conf)
0175 {
0176     return ((u8 *) conf + TS_PRIV_ALIGN(sizeof(struct ts_config)));
0177 }
0178 
0179 #endif