ts_kmp.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * lib/ts_kmp.c Knuth-Morris-Pratt 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: Thomas Graf <tgraf@suug.ch>
  10. *
  11. * ==========================================================================
  12. *
  13. * Implements a linear-time string-matching algorithm due to Knuth,
  14. * Morris, and Pratt [1]. Their algorithm avoids the explicit
  15. * computation of the transition function DELTA altogether. Its
  16. * matching time is O(n), for n being length(text), using just an
  17. * auxiliary function PI[1..m], for m being length(pattern),
  18. * precomputed from the pattern in time O(m). The array PI allows
  19. * the transition function DELTA to be computed efficiently
  20. * "on the fly" as needed. Roughly speaking, for any state
  21. * "q" = 0,1,...,m and any character "a" in SIGMA, the value
  22. * PI["q"] contains the information that is independent of "a" and
  23. * is needed to compute DELTA("q", "a") [2]. Since the array PI
  24. * has only m entries, whereas DELTA has O(m|SIGMA|) entries, we
  25. * save a factor of |SIGMA| in the preprocessing time by computing
  26. * PI rather than DELTA.
  27. *
  28. * [1] Cormen, Leiserson, Rivest, Stein
  29. * Introdcution to Algorithms, 2nd Edition, MIT Press
  30. * [2] See finite automation theory
  31. */
  32. #include <linux/module.h>
  33. #include <linux/types.h>
  34. #include <linux/string.h>
  35. #include <linux/textsearch.h>
  36. struct ts_kmp
  37. {
  38. u8 * pattern;
  39. unsigned int pattern_len;
  40. unsigned int prefix_tbl[0];
  41. };
  42. static unsigned int kmp_find(struct ts_config *conf, struct ts_state *state)
  43. {
  44. struct ts_kmp *kmp = ts_config_priv(conf);
  45. unsigned int i, q = 0, text_len, consumed = state->offset;
  46. const u8 *text;
  47. for (;;) {
  48. text_len = conf->get_next_block(consumed, &text, conf, state);
  49. if (unlikely(text_len == 0))
  50. break;
  51. for (i = 0; i < text_len; i++) {
  52. while (q > 0 && kmp->pattern[q] != text[i])
  53. q = kmp->prefix_tbl[q - 1];
  54. if (kmp->pattern[q] == text[i])
  55. q++;
  56. if (unlikely(q == kmp->pattern_len)) {
  57. state->offset = consumed + i + 1;
  58. return state->offset - kmp->pattern_len;
  59. }
  60. }
  61. consumed += text_len;
  62. }
  63. return UINT_MAX;
  64. }
  65. static inline void compute_prefix_tbl(const u8 *pattern, unsigned int len,
  66. unsigned int *prefix_tbl)
  67. {
  68. unsigned int k, q;
  69. for (k = 0, q = 1; q < len; q++) {
  70. while (k > 0 && pattern[k] != pattern[q])
  71. k = prefix_tbl[k-1];
  72. if (pattern[k] == pattern[q])
  73. k++;
  74. prefix_tbl[q] = k;
  75. }
  76. }
  77. static struct ts_config *kmp_init(const void *pattern, unsigned int len,
  78. gfp_t gfp_mask)
  79. {
  80. struct ts_config *conf;
  81. struct ts_kmp *kmp;
  82. unsigned int prefix_tbl_len = len * sizeof(unsigned int);
  83. size_t priv_size = sizeof(*kmp) + len + prefix_tbl_len;
  84. conf = alloc_ts_config(priv_size, gfp_mask);
  85. if (IS_ERR(conf))
  86. return conf;
  87. kmp = ts_config_priv(conf);
  88. kmp->pattern_len = len;
  89. compute_prefix_tbl(pattern, len, kmp->prefix_tbl);
  90. kmp->pattern = (u8 *) kmp->prefix_tbl + prefix_tbl_len;
  91. memcpy(kmp->pattern, pattern, len);
  92. return conf;
  93. }
  94. static void *kmp_get_pattern(struct ts_config *conf)
  95. {
  96. struct ts_kmp *kmp = ts_config_priv(conf);
  97. return kmp->pattern;
  98. }
  99. static unsigned int kmp_get_pattern_len(struct ts_config *conf)
  100. {
  101. struct ts_kmp *kmp = ts_config_priv(conf);
  102. return kmp->pattern_len;
  103. }
  104. static struct ts_ops kmp_ops = {
  105. .name = "kmp",
  106. .find = kmp_find,
  107. .init = kmp_init,
  108. .get_pattern = kmp_get_pattern,
  109. .get_pattern_len = kmp_get_pattern_len,
  110. .owner = THIS_MODULE,
  111. .list = LIST_HEAD_INIT(kmp_ops.list)
  112. };
  113. static int __init init_kmp(void)
  114. {
  115. return textsearch_register(&kmp_ops);
  116. }
  117. static void __exit exit_kmp(void)
  118. {
  119. textsearch_unregister(&kmp_ops);
  120. }
  121. MODULE_LICENSE("GPL");
  122. module_init(init_kmp);
  123. module_exit(exit_kmp);