ts_bm.c 5.0 KB

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