ts_kmp.c 4.1 KB

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