ts_bm.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * lib/ts_bm.c Boyer-Moore text search implementation
  4. *
  5. * Authors: Pablo Neira Ayuso <pablo@eurodev.net>
  6. *
  7. * ==========================================================================
  8. *
  9. * Implements Boyer-Moore string matching algorithm:
  10. *
  11. * [1] A Fast String Searching Algorithm, R.S. Boyer and Moore.
  12. * Communications of the Association for Computing Machinery,
  13. * 20(10), 1977, pp. 762-772.
  14. * https://www.cs.utexas.edu/users/moore/publications/fstrpos.pdf
  15. *
  16. * [2] Handbook of Exact String Matching Algorithms, Thierry Lecroq, 2004
  17. * http://www-igm.univ-mlv.fr/~lecroq/string/string.pdf
  18. *
  19. * Note: Since Boyer-Moore (BM) performs searches for matchings from right
  20. * to left, it's still possible that a matching could be spread over
  21. * multiple blocks, in that case this algorithm won't find any coincidence.
  22. *
  23. * If you're willing to ensure that such thing won't ever happen, use the
  24. * Knuth-Pratt-Morris (KMP) implementation instead. In conclusion, choose
  25. * the proper string search algorithm depending on your setting.
  26. *
  27. * Say you're using the textsearch infrastructure for filtering, NIDS or
  28. * any similar security focused purpose, then go KMP. Otherwise, if you
  29. * really care about performance, say you're classifying packets to apply
  30. * Quality of Service (QoS) policies, and you don't mind about possible
  31. * matchings spread over multiple fragments, then go BM.
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/types.h>
  36. #include <linux/string.h>
  37. #include <linux/ctype.h>
  38. #include <linux/textsearch.h>
  39. /* Alphabet size, use ASCII */
  40. #define ASIZE 256
  41. #if 0
  42. #define DEBUGP printk
  43. #else
  44. #define DEBUGP(args, format...)
  45. #endif
  46. struct ts_bm
  47. {
  48. u8 * pattern;
  49. unsigned int patlen;
  50. unsigned int bad_shift[ASIZE];
  51. unsigned int good_shift[];
  52. };
  53. static unsigned int bm_find(struct ts_config *conf, struct ts_state *state)
  54. {
  55. struct ts_bm *bm = ts_config_priv(conf);
  56. unsigned int i, text_len, consumed = state->offset;
  57. const u8 *text;
  58. int shift = bm->patlen - 1, bs;
  59. const u8 icase = conf->flags & TS_IGNORECASE;
  60. for (;;) {
  61. text_len = conf->get_next_block(consumed, &text, conf, state);
  62. if (unlikely(text_len == 0))
  63. break;
  64. while (shift < text_len) {
  65. DEBUGP("Searching in position %d (%c)\n",
  66. shift, text[shift]);
  67. for (i = 0; i < bm->patlen; i++)
  68. if ((icase ? toupper(text[shift-i])
  69. : text[shift-i])
  70. != bm->pattern[bm->patlen-1-i])
  71. goto next;
  72. /* London calling... */
  73. DEBUGP("found!\n");
  74. return consumed += (shift-(bm->patlen-1));
  75. next: bs = bm->bad_shift[text[shift-i]];
  76. /* Now jumping to... */
  77. shift = max_t(int, shift-i+bs, shift+bm->good_shift[i]);
  78. }
  79. consumed += text_len;
  80. }
  81. return UINT_MAX;
  82. }
  83. static int subpattern(u8 *pattern, int i, int j, int g)
  84. {
  85. int x = i+g-1, y = j+g-1, ret = 0;
  86. while(pattern[x--] == pattern[y--]) {
  87. if (y < 0) {
  88. ret = 1;
  89. break;
  90. }
  91. if (--g == 0) {
  92. ret = pattern[i-1] != pattern[j-1];
  93. break;
  94. }
  95. }
  96. return ret;
  97. }
  98. static void compute_prefix_tbl(struct ts_bm *bm, int flags)
  99. {
  100. int i, j, g;
  101. for (i = 0; i < ASIZE; i++)
  102. bm->bad_shift[i] = bm->patlen;
  103. for (i = 0; i < bm->patlen - 1; i++) {
  104. bm->bad_shift[bm->pattern[i]] = bm->patlen - 1 - i;
  105. if (flags & TS_IGNORECASE)
  106. bm->bad_shift[tolower(bm->pattern[i])]
  107. = bm->patlen - 1 - i;
  108. }
  109. /* Compute the good shift array, used to match reocurrences
  110. * of a subpattern */
  111. bm->good_shift[0] = 1;
  112. for (i = 1; i < bm->patlen; i++)
  113. bm->good_shift[i] = bm->patlen;
  114. for (i = bm->patlen-1, g = 1; i > 0; g++, i--) {
  115. for (j = i-1; j >= 1-g ; j--)
  116. if (subpattern(bm->pattern, i, j, g)) {
  117. bm->good_shift[g] = bm->patlen-j-g;
  118. break;
  119. }
  120. }
  121. }
  122. static struct ts_config *bm_init(const void *pattern, unsigned int len,
  123. gfp_t gfp_mask, int flags)
  124. {
  125. struct ts_config *conf;
  126. struct ts_bm *bm;
  127. int i;
  128. unsigned int prefix_tbl_len = len * sizeof(unsigned int);
  129. size_t priv_size = sizeof(*bm) + len + prefix_tbl_len;
  130. conf = alloc_ts_config(priv_size, gfp_mask);
  131. if (IS_ERR(conf))
  132. return conf;
  133. conf->flags = flags;
  134. bm = ts_config_priv(conf);
  135. bm->patlen = len;
  136. bm->pattern = (u8 *) bm->good_shift + prefix_tbl_len;
  137. if (flags & TS_IGNORECASE)
  138. for (i = 0; i < len; i++)
  139. bm->pattern[i] = toupper(((u8 *)pattern)[i]);
  140. else
  141. memcpy(bm->pattern, pattern, len);
  142. compute_prefix_tbl(bm, flags);
  143. return conf;
  144. }
  145. static void *bm_get_pattern(struct ts_config *conf)
  146. {
  147. struct ts_bm *bm = ts_config_priv(conf);
  148. return bm->pattern;
  149. }
  150. static unsigned int bm_get_pattern_len(struct ts_config *conf)
  151. {
  152. struct ts_bm *bm = ts_config_priv(conf);
  153. return bm->patlen;
  154. }
  155. static struct ts_ops bm_ops = {
  156. .name = "bm",
  157. .find = bm_find,
  158. .init = bm_init,
  159. .get_pattern = bm_get_pattern,
  160. .get_pattern_len = bm_get_pattern_len,
  161. .owner = THIS_MODULE,
  162. .list = LIST_HEAD_INIT(bm_ops.list)
  163. };
  164. static int __init init_bm(void)
  165. {
  166. return textsearch_register(&bm_ops);
  167. }
  168. static void __exit exit_bm(void)
  169. {
  170. textsearch_unregister(&bm_ops);
  171. }
  172. MODULE_LICENSE("GPL");
  173. module_init(init_bm);
  174. module_exit(exit_bm);