diff options
| author | Thomas Graf <tgraf@suug.ch> | 2005-06-23 23:59:16 -0400 |
|---|---|---|
| committer | David S. Miller <davem@davemloft.net> | 2005-06-23 23:59:16 -0400 |
| commit | 6408f79cce401e1bfecf923e7156f84f96e021e3 (patch) | |
| tree | 203624ffacf60d364293adc47d2f59f6ba81dd35 | |
| parent | df3fb93ad9ec0b20c785c0ad82d42d159a1af272 (diff) | |
[LIB]: Naive finite state machine based textsearch
A finite state machine consists of n states (struct ts_fsm_token)
representing the pattern as a finite automation. The data is read
sequentially on a 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 specyfing
whether the pattern has to start at the first octect. 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.
Signed-off-by: Thomas Graf <tgraf@suug.ch>
Signed-off-by: David S. Miller <davem@davemloft.net>
| -rw-r--r-- | include/linux/textsearch_fsm.h | 48 | ||||
| -rw-r--r-- | lib/Kconfig | 11 | ||||
| -rw-r--r-- | lib/Makefile | 1 | ||||
| -rw-r--r-- | lib/ts_fsm.c | 338 |
4 files changed, 398 insertions, 0 deletions
diff --git a/include/linux/textsearch_fsm.h b/include/linux/textsearch_fsm.h new file mode 100644 index 000000000000..fdfa078c66e5 --- /dev/null +++ b/include/linux/textsearch_fsm.h | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | #ifndef __LINUX_TEXTSEARCH_FSM_H | ||
| 2 | #define __LINUX_TEXTSEARCH_FSM_H | ||
| 3 | |||
| 4 | #include <linux/types.h> | ||
| 5 | |||
| 6 | enum { | ||
| 7 | TS_FSM_SPECIFIC, /* specific character */ | ||
| 8 | TS_FSM_WILDCARD, /* any character */ | ||
| 9 | TS_FSM_DIGIT, /* isdigit() */ | ||
| 10 | TS_FSM_XDIGIT, /* isxdigit() */ | ||
| 11 | TS_FSM_PRINT, /* isprint() */ | ||
| 12 | TS_FSM_ALPHA, /* isalpha() */ | ||
| 13 | TS_FSM_ALNUM, /* isalnum() */ | ||
| 14 | TS_FSM_ASCII, /* isascii() */ | ||
| 15 | TS_FSM_CNTRL, /* iscntrl() */ | ||
| 16 | TS_FSM_GRAPH, /* isgraph() */ | ||
| 17 | TS_FSM_LOWER, /* islower() */ | ||
| 18 | TS_FSM_UPPER, /* isupper() */ | ||
| 19 | TS_FSM_PUNCT, /* ispunct() */ | ||
| 20 | TS_FSM_SPACE, /* isspace() */ | ||
| 21 | __TS_FSM_TYPE_MAX, | ||
| 22 | }; | ||
| 23 | #define TS_FSM_TYPE_MAX (__TS_FSM_TYPE_MAX - 1) | ||
| 24 | |||
| 25 | enum { | ||
| 26 | TS_FSM_SINGLE, /* 1 occurrence */ | ||
| 27 | TS_FSM_PERHAPS, /* 1 or 0 occurrence */ | ||
| 28 | TS_FSM_ANY, /* 0..n occurrences */ | ||
| 29 | TS_FSM_MULTI, /* 1..n occurrences */ | ||
| 30 | TS_FSM_HEAD_IGNORE, /* 0..n ignored occurrences at head */ | ||
| 31 | __TS_FSM_RECUR_MAX, | ||
| 32 | }; | ||
| 33 | #define TS_FSM_RECUR_MAX (__TS_FSM_RECUR_MAX - 1) | ||
| 34 | |||
| 35 | /** | ||
| 36 | * struct ts_fsm_token - state machine token (state) | ||
| 37 | * @type: type of token | ||
| 38 | * @recur: number of recurrences | ||
| 39 | * @value: character value for TS_FSM_SPECIFIC | ||
| 40 | */ | ||
| 41 | struct ts_fsm_token | ||
| 42 | { | ||
| 43 | __u16 type; | ||
| 44 | __u8 recur; | ||
| 45 | __u8 value; | ||
| 46 | }; | ||
| 47 | |||
| 48 | #endif | ||
diff --git a/lib/Kconfig b/lib/Kconfig index 16b8fa2175e4..455833a9e31a 100644 --- a/lib/Kconfig +++ b/lib/Kconfig | |||
| @@ -80,4 +80,15 @@ config TEXTSEARCH_KMP | |||
| 80 | To compile this code as a module, choose M here: the | 80 | To compile this code as a module, choose M here: the |
| 81 | module will be called ts_kmp. | 81 | module will be called ts_kmp. |
| 82 | 82 | ||
| 83 | config TEXTSEARCH_FSM | ||
| 84 | depends on TEXTSEARCH | ||
| 85 | tristate "Finite state machine" | ||
| 86 | help | ||
| 87 | Say Y here if you want to be able to search text using a | ||
| 88 | naive finite state machine approach implementing a subset | ||
| 89 | of regular expressions. | ||
| 90 | |||
| 91 | To compile this code as a module, choose M here: the | ||
| 92 | module will be called ts_fsm. | ||
| 93 | |||
| 83 | endmenu | 94 | endmenu |
diff --git a/lib/Makefile b/lib/Makefile index 6cdb10f312df..7f6eda449102 100644 --- a/lib/Makefile +++ b/lib/Makefile | |||
| @@ -38,6 +38,7 @@ obj-$(CONFIG_REED_SOLOMON) += reed_solomon/ | |||
| 38 | 38 | ||
| 39 | lib-$(CONFIG_TEXTSEARCH) += textsearch.o | 39 | lib-$(CONFIG_TEXTSEARCH) += textsearch.o |
| 40 | obj-$(CONFIG_TEXTSEARCH_KMP) += ts_kmp.o | 40 | obj-$(CONFIG_TEXTSEARCH_KMP) += ts_kmp.o |
| 41 | obj-$(CONFIG_TEXTSEARCH_FSM) += ts_fsm.o | ||
| 41 | 42 | ||
| 42 | hostprogs-y := gen_crc32table | 43 | hostprogs-y := gen_crc32table |
| 43 | clean-files := crc32table.h | 44 | clean-files := crc32table.h |
diff --git a/lib/ts_fsm.c b/lib/ts_fsm.c new file mode 100644 index 000000000000..d27c0a072940 --- /dev/null +++ b/lib/ts_fsm.c | |||
| @@ -0,0 +1,338 @@ | |||
| 1 | /* | ||
| 2 | * lib/ts_fsm.c A naive finite state machine text search approach | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or | ||
| 5 | * modify it under the terms of the GNU General Public License | ||
| 6 | * as published by the Free Software Foundation; either version | ||
| 7 | * 2 of the License, or (at your option) any later version. | ||
| 8 | * | ||
| 9 | * Authors: Thomas Graf <tgraf@suug.ch> | ||
| 10 | * | ||
| 11 | * ========================================================================== | ||
| 12 | * | ||
| 13 | * A finite state machine consists of n states (struct ts_fsm_token) | ||
| 14 | * representing the pattern as a finite automation. The data is read | ||
| 15 | * sequentially on a octet basis. Every state token specifies the number | ||
| 16 | * of recurrences and the type of value accepted which can be either a | ||
| 17 | * specific character or ctype based set of characters. The available | ||
| 18 | * type of recurrences include 1, (0|1), [0 n], and [1 n]. | ||
| 19 | * | ||
| 20 | * The algorithm differs between strict/non-strict mode specyfing | ||
| 21 | * whether the pattern has to start at the first octect. Strict mode | ||
| 22 | * is enabled by default and can be disabled by inserting | ||
| 23 | * TS_FSM_HEAD_IGNORE as the first token in the chain. | ||
| 24 | * | ||
| 25 | * The runtime performance of the algorithm should be around O(n), | ||
| 26 | * however while in strict mode the average runtime can be better. | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include <linux/config.h> | ||
| 30 | #include <linux/module.h> | ||
| 31 | #include <linux/types.h> | ||
| 32 | #include <linux/string.h> | ||
| 33 | #include <linux/ctype.h> | ||
| 34 | #include <linux/textsearch.h> | ||
| 35 | #include <linux/textsearch_fsm.h> | ||
| 36 | |||
| 37 | struct ts_fsm | ||
| 38 | { | ||
| 39 | unsigned int ntokens; | ||
| 40 | struct ts_fsm_token tokens[0]; | ||
| 41 | }; | ||
| 42 | |||
| 43 | /* other values derived from ctype.h */ | ||
| 44 | #define _A 0x100 /* ascii */ | ||
| 45 | #define _W 0x200 /* wildcard */ | ||
| 46 | |||
| 47 | /* Map to _ctype flags and some magic numbers */ | ||
| 48 | static u16 token_map[TS_FSM_TYPE_MAX+1] = { | ||
| 49 | [TS_FSM_SPECIFIC] = 0, | ||
| 50 | [TS_FSM_WILDCARD] = _W, | ||
| 51 | [TS_FSM_CNTRL] = _C, | ||
| 52 | [TS_FSM_LOWER] = _L, | ||
| 53 | [TS_FSM_UPPER] = _U, | ||
| 54 | [TS_FSM_PUNCT] = _P, | ||
| 55 | [TS_FSM_SPACE] = _S, | ||
| 56 | [TS_FSM_DIGIT] = _D, | ||
| 57 | [TS_FSM_XDIGIT] = _D | _X, | ||
| 58 | [TS_FSM_ALPHA] = _U | _L, | ||
| 59 | [TS_FSM_ALNUM] = _U | _L | _D, | ||
| 60 | [TS_FSM_PRINT] = _P | _U | _L | _D | _SP, | ||
| 61 | [TS_FSM_GRAPH] = _P | _U | _L | _D, | ||
| 62 | [TS_FSM_ASCII] = _A, | ||
| 63 | }; | ||
| 64 | |||
| 65 | static u16 token_lookup_tbl[256] = { | ||
| 66 | _W|_A|_C, _W|_A|_C, _W|_A|_C, _W|_A|_C, /* 0- 3 */ | ||
| 67 | _W|_A|_C, _W|_A|_C, _W|_A|_C, _W|_A|_C, /* 4- 7 */ | ||
| 68 | _W|_A|_C, _W|_A|_C|_S, _W|_A|_C|_S, _W|_A|_C|_S, /* 8- 11 */ | ||
| 69 | _W|_A|_C|_S, _W|_A|_C|_S, _W|_A|_C, _W|_A|_C, /* 12- 15 */ | ||
| 70 | _W|_A|_C, _W|_A|_C, _W|_A|_C, _W|_A|_C, /* 16- 19 */ | ||
| 71 | _W|_A|_C, _W|_A|_C, _W|_A|_C, _W|_A|_C, /* 20- 23 */ | ||
| 72 | _W|_A|_C, _W|_A|_C, _W|_A|_C, _W|_A|_C, /* 24- 27 */ | ||
| 73 | _W|_A|_C, _W|_A|_C, _W|_A|_C, _W|_A|_C, /* 28- 31 */ | ||
| 74 | _W|_A|_S|_SP, _W|_A|_P, _W|_A|_P, _W|_A|_P, /* 32- 35 */ | ||
| 75 | _W|_A|_P, _W|_A|_P, _W|_A|_P, _W|_A|_P, /* 36- 39 */ | ||
| 76 | _W|_A|_P, _W|_A|_P, _W|_A|_P, _W|_A|_P, /* 40- 43 */ | ||
| 77 | _W|_A|_P, _W|_A|_P, _W|_A|_P, _W|_A|_P, /* 44- 47 */ | ||
| 78 | _W|_A|_D, _W|_A|_D, _W|_A|_D, _W|_A|_D, /* 48- 51 */ | ||
| 79 | _W|_A|_D, _W|_A|_D, _W|_A|_D, _W|_A|_D, /* 52- 55 */ | ||
| 80 | _W|_A|_D, _W|_A|_D, _W|_A|_P, _W|_A|_P, /* 56- 59 */ | ||
| 81 | _W|_A|_P, _W|_A|_P, _W|_A|_P, _W|_A|_P, /* 60- 63 */ | ||
| 82 | _W|_A|_P, _W|_A|_U|_X, _W|_A|_U|_X, _W|_A|_U|_X, /* 64- 67 */ | ||
| 83 | _W|_A|_U|_X, _W|_A|_U|_X, _W|_A|_U|_X, _W|_A|_U, /* 68- 71 */ | ||
| 84 | _W|_A|_U, _W|_A|_U, _W|_A|_U, _W|_A|_U, /* 72- 75 */ | ||
| 85 | _W|_A|_U, _W|_A|_U, _W|_A|_U, _W|_A|_U, /* 76- 79 */ | ||
| 86 | _W|_A|_U, _W|_A|_U, _W|_A|_U, _W|_A|_U, /* 80- 83 */ | ||
| 87 | _W|_A|_U, _W|_A|_U, _W|_A|_U, _W|_A|_U, /* 84- 87 */ | ||
| 88 | _W|_A|_U, _W|_A|_U, _W|_A|_U, _W|_A|_P, /* 88- 91 */ | ||
| 89 | _W|_A|_P, _W|_A|_P, _W|_A|_P, _W|_A|_P, /* 92- 95 */ | ||
| 90 | _W|_A|_P, _W|_A|_L|_X, _W|_A|_L|_X, _W|_A|_L|_X, /* 96- 99 */ | ||
| 91 | _W|_A|_L|_X, _W|_A|_L|_X, _W|_A|_L|_X, _W|_A|_L, /* 100-103 */ | ||
| 92 | _W|_A|_L, _W|_A|_L, _W|_A|_L, _W|_A|_L, /* 104-107 */ | ||
| 93 | _W|_A|_L, _W|_A|_L, _W|_A|_L, _W|_A|_L, /* 108-111 */ | ||
| 94 | _W|_A|_L, _W|_A|_L, _W|_A|_L, _W|_A|_L, /* 112-115 */ | ||
| 95 | _W|_A|_L, _W|_A|_L, _W|_A|_L, _W|_A|_L, /* 116-119 */ | ||
| 96 | _W|_A|_L, _W|_A|_L, _W|_A|_L, _W|_A|_P, /* 120-123 */ | ||
| 97 | _W|_A|_P, _W|_A|_P, _W|_A|_P, _W|_A|_C, /* 124-127 */ | ||
| 98 | _W, _W, _W, _W, /* 128-131 */ | ||
| 99 | _W, _W, _W, _W, /* 132-135 */ | ||
| 100 | _W, _W, _W, _W, /* 136-139 */ | ||
| 101 | _W, _W, _W, _W, /* 140-143 */ | ||
| 102 | _W, _W, _W, _W, /* 144-147 */ | ||
| 103 | _W, _W, _W, _W, /* 148-151 */ | ||
| 104 | _W, _W, _W, _W, /* 152-155 */ | ||
| 105 | _W, _W, _W, _W, /* 156-159 */ | ||
| 106 | _W|_S|_SP, _W|_P, _W|_P, _W|_P, /* 160-163 */ | ||
| 107 | _W|_P, _W|_P, _W|_P, _W|_P, /* 164-167 */ | ||
| 108 | _W|_P, _W|_P, _W|_P, _W|_P, /* 168-171 */ | ||
| 109 | _W|_P, _W|_P, _W|_P, _W|_P, /* 172-175 */ | ||
