ts_fsm.c 11 KB

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