// SPDX-License-Identifier: GPL-2.0-or-later /* * lib/ts_fsm.c A naive finite state machine text search approach * * Authors: Thomas Graf <[email protected]> * * ========================================================================== * * A finite state machine consists of n states (struct ts_fsm_token) * representing the pattern as a finite automaton. The data is read * sequentially on an octet basis. Every state token specifies the number * of recurrences and the type of value accepted which can be either a * specific character or ctype based set of characters. The available * type of recurrences include 1, (0|1), [0 n], and [1 n]. * * The algorithm differs between strict/non-strict mode specifying * whether the pattern has to start at the first octet. Strict mode * is enabled by default and can be disabled by inserting * TS_FSM_HEAD_IGNORE as the first token in the chain. * * The runtime performance of the algorithm should be around O(n), * however while in strict mode the average runtime can be better. */ #include <linux/module.h> #include <linux/types.h> #include <linux/string.h> #include <linux/ctype.h> #include <linux/textsearch.h> #include <linux/textsearch_fsm.h> struct ts_fsm { … }; /* other values derived from ctype.h */ #define _A … #define _W … /* Map to _ctype flags and some magic numbers */ static const u16 token_map[TS_FSM_TYPE_MAX+1] = …; static const u16 token_lookup_tbl[256] = …; /* 252-255 */ static inline int match_token(struct ts_fsm_token *t, u8 d) { … } static unsigned int fsm_find(struct ts_config *conf, struct ts_state *state) { … } static struct ts_config *fsm_init(const void *pattern, unsigned int len, gfp_t gfp_mask, int flags) { … } static void *fsm_get_pattern(struct ts_config *conf) { … } static unsigned int fsm_get_pattern_len(struct ts_config *conf) { … } static struct ts_ops fsm_ops = …; static int __init init_fsm(void) { … } static void __exit exit_fsm(void) { … } MODULE_DESCRIPTION(…) …; MODULE_LICENSE(…) …; module_init(…) …; module_exit(exit_fsm);