ts_fsm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * lib/ts_fsm.c A naive finite state machine text search approach
  4. *
  5. * Authors: Thomas Graf <tgraf@suug.ch>
  6. *
  7. * ==========================================================================
  8. *
  9. * A finite state machine consists of n states (struct ts_fsm_token)
  10. * representing the pattern as a finite automaton. The data is read
  11. * sequentially on an octet basis. Every state token specifies the number
  12. * of recurrences and the type of value accepted which can be either a
  13. * specific character or ctype based set of characters. The available
  14. * type of recurrences include 1, (0|1), [0 n], and [1 n].
  15. *
  16. * The algorithm differs between strict/non-strict mode specifying
  17. * whether the pattern has to start at the first octet. Strict mode
  18. * is enabled by default and can be disabled by inserting
  19. * TS_FSM_HEAD_IGNORE as the first token in the chain.
  20. *
  21. * The runtime performance of the algorithm should be around O(n),
  22. * however while in strict mode the average runtime can be better.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/types.h>
  26. #include <linux/string.h>
  27. #include <linux/ctype.h>
  28. #include <linux/textsearch.h>
  29. #include <linux/textsearch_fsm.h>
  30. struct ts_fsm
  31. {
  32. unsigned int ntokens;
  33. struct ts_fsm_token tokens[];
  34. };
  35. /* other values derived from ctype.h */
  36. #define _A 0x100 /* ascii */
  37. #define _W 0x200 /* wildcard */
  38. /* Map to _ctype flags and some magic numbers */
  39. static const u16 token_map[TS_FSM_TYPE_MAX+1] = {
  40. [TS_FSM_SPECIFIC] = 0,
  41. [TS_FSM_WILDCARD] = _W,
  42. [TS_FSM_CNTRL] = _C,
  43. [TS_FSM_LOWER] = _L,
  44. [TS_FSM_UPPER] = _U,
  45. [TS_FSM_PUNCT] = _P,
  46. [TS_FSM_SPACE] = _S,
  47. [TS_FSM_DIGIT] = _D,
  48. [TS_FSM_XDIGIT] = _D | _X,
  49. [TS_FSM_ALPHA] = _U | _L,
  50. [TS_FSM_ALNUM] = _U | _L | _D,
  51. [TS_FSM_PRINT] = _P | _U | _L | _D | _SP,
  52. [TS_FSM_GRAPH] = _P | _U | _L | _D,
  53. [TS_FSM_ASCII] = _A,
  54. };
  55. static const u16 token_lookup_tbl[256] = {
  56. _W|_A|_C, _W|_A|_C, _W|_A|_C, _W|_A|_C, /* 0- 3 */
  57. _W|_A|_C, _W|_A|_C, _W|_A|_C, _W|_A|_C, /* 4- 7 */
  58. _W|_A|_C, _W|_A|_C|_S, _W|_A|_C|_S, _W|_A|_C|_S, /* 8- 11 */
  59. _W|_A|_C|_S, _W|_A|_C|_S, _W|_A|_C, _W|_A|_C, /* 12- 15 */
  60. _W|_A|_C, _W|_A|_C, _W|_A|_C, _W|_A|_C, /* 16- 19 */
  61. _W|_A|_C, _W|_A|_C, _W|_A|_C, _W|_A|_C, /* 20- 23 */
  62. _W|_A|_C, _W|_A|_C, _W|_A|_C, _W|_A|_C, /* 24- 27 */
  63. _W|_A|_C, _W|_A|_C, _W|_A|_C, _W|_A|_C, /* 28- 31 */
  64. _W|_A|_S|_SP, _W|_A|_P, _W|_A|_P, _W|_A|_P, /* 32- 35 */
  65. _W|_A|_P, _W|_A|_P, _W|_A|_P, _W|_A|_P, /* 36- 39 */
  66. _W|_A|_P, _W|_A|_P, _W|_A|_P, _W|_A|_P, /* 40- 43 */
  67. _W|_A|_P, _W|_A|_P, _W|_A|_P, _W|_A|_P, /* 44- 47 */
  68. _W|_A|_D, _W|_A|_D, _W|_A|_D, _W|_A|_D, /* 48- 51 */
  69. _W|_A|_D, _W|_A|_D, _W|_A|_D, _W|_A|_D, /* 52- 55 */
  70. _W|_A|_D, _W|_A|_D, _W|_A|_P, _W|_A|_P, /* 56- 59 */
  71. _W|_A|_P, _W|_A|_P, _W|_A|_P, _W|_A|_P, /* 60- 63 */
  72. _W|_A|_P, _W|_A|_U|_X, _W|_A|_U|_X, _W|_A|_U|_X, /* 64- 67 */
  73. _W|_A|_U|_X, _W|_A|_U|_X, _W|_A|_U|_X, _W|_A|_U, /* 68- 71 */
  74. _W|_A|_U, _W|_A|_U, _W|_A|_U, _W|_A|_U, /* 72- 75 */
  75. _W|_A|_U, _W|_A|_U, _W|_A|_U, _W|_A|_U, /* 76- 79 */
  76. _W|_A|_U, _W|_A|_U, _W|_A|_U, _W|_A|_U, /* 80- 83 */
  77. _W|_A|_U, _W|_A|_U, _W|_A|_U, _W|_A|_U, /* 84- 87 */
  78. _W|_A|_U, _W|_A|_U, _W|_A|_U, _W|_A|_P, /* 88- 91 */
  79. _W|_A|_P, _W|_A|_P, _W|_A|_P, _W|_A|_P, /* 92- 95 */
  80. _W|_A|_P, _W|_A|_L|_X, _W|_A|_L|_X, _W|_A|_L|_X, /* 96- 99 */
  81. _W|_A|_L|_X, _W|_A|_L|_X, _W|_A|_L|_X, _W|_A|_L, /* 100-103 */
  82. _W|_A|_L, _W|_A|_L, _W|_A|_L, _W|_A|_L, /* 104-107 */
  83. _W|_A|_L, _W|_A|_L, _W|_A|_L, _W|_A|_L, /* 108-111 */
  84. _W|_A|_L, _W|_A|_L, _W|_A|_L, _W|_A|_L, /* 112-115 */
  85. _W|_A|_L, _W|_A|_L, _W|_A|_L, _W|_A|_L, /* 116-119 */
  86. _W|_A|_L, _W|_A|_L, _W|_A|_L, _W|_A|_P, /* 120-123 */
  87. _W|_A|_P, _W|_A|_P, _W|_A|_P, _W|_A|_C, /* 124-127 */
  88. _W, _W, _W, _W, /* 128-131 */
  89. _W, _W, _W, _W, /* 132-135 */
  90. _W, _W, _W, _W, /* 136-139 */
  91. _W, _W, _W, _W, /* 140-143 */
  92. _W, _W, _W, _W, /* 144-147 */
  93. _W, _W, _W, _W, /* 148-151 */
  94. _W, _W, _W, _W, /* 152-155 */
  95. _W, _W, _W, _W, /* 156-159 */
  96. _W|_S|_SP, _W|_P, _W|_P, _W|_P, /* 160-163 */
  97. _W|_P, _W|_P, _W|_P, _W|_P, /* 164-167 */
  98. _W|_P, _W|_P, _W|_P, _W|_P, /* 168-171 */
  99. _W|_P, _W|_P, _W|_P, _W|_P, /* 172-175 */
  100. _W|_P, _W|_P, _W|_P, _W|_P, /* 176-179 */
  101. _W|_P, _W|_P, _W|_P, _W|_P, /* 180-183 */
  102. _W|_P, _W|_P, _W|_P, _W|_P, /* 184-187 */
  103. _W|_P, _W|_P, _W|_P, _W|_P, /* 188-191 */
  104. _W|_U, _W|_U, _W|_U, _W|_U, /* 192-195 */
  105. _W|_U, _W|_U, _W|_U, _W|_U, /* 196-199 */
  106. _W|_U, _W|_U, _W|_U, _W|_U, /* 200-203 */
  107. _W|_U, _W|_U, _W|_U, _W|_U, /* 204-207 */
  108. _W|_U, _W|_U, _W|_U, _W|_U, /* 208-211 */
  109. _W|_U, _W|_U, _W|_U, _W|_P, /* 212-215 */
  110. _W|_U, _W|_U, _W|_U, _W|_U, /* 216-219 */
  111. _W|_U, _W|_U, _W|_U, _W|_L, /* 220-223 */
  112. _W|_L, _W|_L, _W|_L, _W|_L, /* 224-227 */
  113. _W|_L, _W|_L, _W|_L, _W|_L, /* 228-231 */
  114. _W|_L, _W|_L, _W|_L, _W|_L, /* 232-235 */
  115. _W|_L, _W|_L, _W|_L, _W|_L, /* 236-239 */
  116. _W|_L, _W|_L, _W|_L, _W|_L, /* 240-243 */
  117. _W|_L, _W|_L, _W|_L, _W|_P, /* 244-247 */
  118. _W|_L, _W|_L, _W|_L, _W|_L, /* 248-251 */
  119. _W|_L, _W|_L, _W|_L, _W|_L}; /* 252-255 */
  120. static inline int match_token(struct ts_fsm_token *t, u8 d)
  121. {
  122. if (t->type)
  123. return (token_lookup_tbl[d] & t->type) != 0;
  124. else
  125. return t->value == d;
  126. }
  127. static unsigned int fsm_find(struct ts_config *conf, struct ts_state *state)
  128. {
  129. struct ts_fsm *fsm = ts_config_priv(conf);
  130. struct ts_fsm_token *cur = NULL, *next;
  131. unsigned int match_start, block_idx = 0, tok_idx;
  132. unsigned block_len = 0, strict, consumed = state->offset;
  133. const u8 *data;
  134. #define GET_NEXT_BLOCK() \
  135. ({ consumed += block_idx; \
  136. block_idx = 0; \
  137. block_len = conf->get_next_block(consumed, &data, conf, state); })
  138. #define TOKEN_MISMATCH() \
  139. do { \
  140. if (strict) \
  141. goto no_match; \
  142. block_idx++; \
  143. goto startover; \
  144. } while(0)
  145. #define end_of_data() unlikely(block_idx >= block_len && !GET_NEXT_BLOCK())
  146. if (end_of_data())
  147. goto no_match;
  148. strict = fsm->tokens[0].recur != TS_FSM_HEAD_IGNORE;
  149. startover:
  150. match_start = consumed + block_idx;
  151. for (tok_idx = 0; tok_idx < fsm->ntokens; tok_idx++) {
  152. cur = &fsm->tokens[tok_idx];
  153. if (likely(tok_idx < (fsm->ntokens - 1)))
  154. next = &fsm->tokens[tok_idx + 1];
  155. else
  156. next = NULL;
  157. switch (cur->recur) {
  158. case TS_FSM_SINGLE:
  159. if (end_of_data())
  160. goto no_match;
  161. if (!match_token(cur, data[block_idx]))
  162. TOKEN_MISMATCH();
  163. break;
  164. case TS_FSM_PERHAPS:
  165. if (end_of_data() ||
  166. !match_token(cur, data[block_idx]))
  167. continue;
  168. break;
  169. case TS_FSM_MULTI:
  170. if (end_of_data())
  171. goto no_match;
  172. if (!match_token(cur, data[block_idx]))
  173. TOKEN_MISMATCH();
  174. block_idx++;
  175. /* fall through */
  176. case TS_FSM_ANY:
  177. if (next == NULL)
  178. goto found_match;
  179. if (end_of_data())
  180. continue;
  181. while (!match_token(next, data[block_idx])) {
  182. if (!match_token(cur, data[block_idx]))
  183. TOKEN_MISMATCH();
  184. block_idx++;
  185. if (end_of_data())
  186. goto no_match;
  187. }
  188. continue;
  189. /*
  190. * Optimization: Prefer small local loop over jumping
  191. * back and forth until garbage at head is munched.
  192. */
  193. case TS_FSM_HEAD_IGNORE:
  194. if (end_of_data())
  195. continue;
  196. while (!match_token(next, data[block_idx])) {
  197. /*
  198. * Special case, don't start over upon
  199. * a mismatch, give the user the
  200. * chance to specify the type of data
  201. * allowed to be ignored.
  202. */
  203. if (!match_token(cur, data[block_idx]))
  204. goto no_match;
  205. block_idx++;
  206. if (end_of_data())
  207. goto no_match;
  208. }
  209. match_start = consumed + block_idx;
  210. continue;
  211. }
  212. block_idx++;
  213. }
  214. if (end_of_data())
  215. goto found_match;
  216. no_match:
  217. return UINT_MAX;
  218. found_match:
  219. state->offset = consumed + block_idx;
  220. return match_start;
  221. }
  222. static struct ts_config *fsm_init(const void *pattern, unsigned int len,
  223. gfp_t gfp_mask, int flags)
  224. {
  225. int i, err = -EINVAL;
  226. struct ts_config *conf;
  227. struct ts_fsm *fsm;
  228. struct ts_fsm_token *tokens = (struct ts_fsm_token *) pattern;
  229. unsigned int ntokens = len / sizeof(*tokens);
  230. size_t priv_size = sizeof(*fsm) + len;
  231. if (len % sizeof(struct ts_fsm_token) || ntokens < 1)
  232. goto errout;
  233. if (flags & TS_IGNORECASE)
  234. goto errout;
  235. for (i = 0; i < ntokens; i++) {
  236. struct ts_fsm_token *t = &tokens[i];
  237. if (t->type > TS_FSM_TYPE_MAX || t->recur > TS_FSM_RECUR_MAX)
  238. goto errout;
  239. if (t->recur == TS_FSM_HEAD_IGNORE &&
  240. (i != 0 || i == (ntokens - 1)))
  241. goto errout;
  242. }
  243. conf = alloc_ts_config(priv_size, gfp_mask);
  244. if (IS_ERR(conf))
  245. return conf;
  246. conf->flags = flags;
  247. fsm = ts_config_priv(conf);
  248. fsm->ntokens = ntokens;
  249. memcpy(fsm->tokens, pattern, len);
  250. for (i = 0; i < fsm->ntokens; i++) {
  251. struct ts_fsm_token *t = &fsm->tokens[i];
  252. t->type = token_map[t->type];
  253. }
  254. return conf;
  255. errout:
  256. return ERR_PTR(err);
  257. }
  258. static void *fsm_get_pattern(struct ts_config *conf)
  259. {
  260. struct ts_fsm *fsm = ts_config_priv(conf);
  261. return fsm->tokens;
  262. }
  263. static unsigned int fsm_get_pattern_len(struct ts_config *conf)
  264. {
  265. struct ts_fsm *fsm = ts_config_priv(conf);
  266. return fsm->ntokens * sizeof(struct ts_fsm_token);
  267. }
  268. static struct ts_ops fsm_ops = {
  269. .name = "fsm",
  270. .find = fsm_find,
  271. .init = fsm_init,
  272. .get_pattern = fsm_get_pattern,
  273. .get_pattern_len = fsm_get_pattern_len,
  274. .owner = THIS_MODULE,
  275. .list = LIST_HEAD_INIT(fsm_ops.list)
  276. };
  277. static int __init init_fsm(void)
  278. {
  279. return textsearch_register(&fsm_ops);
  280. }
  281. static void __exit exit_fsm(void)
  282. {
  283. textsearch_unregister(&fsm_ops);
  284. }
  285. MODULE_LICENSE("GPL");
  286. module_init(init_fsm);
  287. module_exit(exit_fsm);