textsearch.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __LINUX_TEXTSEARCH_H
  3. #define __LINUX_TEXTSEARCH_H
  4. #include <linux/types.h>
  5. #include <linux/list.h>
  6. #include <linux/kernel.h>
  7. #include <linux/err.h>
  8. #include <linux/slab.h>
  9. struct module;
  10. struct ts_config;
  11. #define TS_AUTOLOAD 1 /* Automatically load textsearch modules when needed */
  12. #define TS_IGNORECASE 2 /* Searches string case insensitively */
  13. /**
  14. * struct ts_state - search state
  15. * @offset: offset for next match
  16. * @cb: control buffer, for persistent variables of get_next_block()
  17. */
  18. struct ts_state
  19. {
  20. unsigned int offset;
  21. char cb[40];
  22. };
  23. /**
  24. * struct ts_ops - search module operations
  25. * @name: name of search algorithm
  26. * @init: initialization function to prepare a search
  27. * @find: find the next occurrence of the pattern
  28. * @destroy: destroy algorithm specific parts of a search configuration
  29. * @get_pattern: return head of pattern
  30. * @get_pattern_len: return length of pattern
  31. * @owner: module reference to algorithm
  32. */
  33. struct ts_ops
  34. {
  35. const char *name;
  36. struct ts_config * (*init)(const void *, unsigned int, gfp_t, int);
  37. unsigned int (*find)(struct ts_config *,
  38. struct ts_state *);
  39. void (*destroy)(struct ts_config *);
  40. void * (*get_pattern)(struct ts_config *);
  41. unsigned int (*get_pattern_len)(struct ts_config *);
  42. struct module *owner;
  43. struct list_head list;
  44. };
  45. /**
  46. * struct ts_config - search configuration
  47. * @ops: operations of chosen algorithm
  48. * @flags: flags
  49. * @get_next_block: callback to fetch the next block to search in
  50. * @finish: callback to finalize a search
  51. */
  52. struct ts_config
  53. {
  54. struct ts_ops *ops;
  55. int flags;
  56. /**
  57. * @get_next_block: fetch next block of data
  58. * @consumed: number of bytes consumed by the caller
  59. * @dst: destination buffer
  60. * @conf: search configuration
  61. * @state: search state
  62. *
  63. * Called repeatedly until 0 is returned. Must assign the
  64. * head of the next block of data to &*dst and return the length
  65. * of the block or 0 if at the end. consumed == 0 indicates
  66. * a new search. May store/read persistent values in state->cb.
  67. */
  68. unsigned int (*get_next_block)(unsigned int consumed,
  69. const u8 **dst,
  70. struct ts_config *conf,
  71. struct ts_state *state);
  72. /**
  73. * @finish: finalize/clean a series of get_next_block() calls
  74. * @conf: search configuration
  75. * @state: search state
  76. *
  77. * Called after the last use of get_next_block(), may be used
  78. * to cleanup any leftovers.
  79. */
  80. void (*finish)(struct ts_config *conf,
  81. struct ts_state *state);
  82. };
  83. /**
  84. * textsearch_next - continue searching for a pattern
  85. * @conf: search configuration
  86. * @state: search state
  87. *
  88. * Continues a search looking for more occurrences of the pattern.
  89. * textsearch_find() must be called to find the first occurrence
  90. * in order to reset the state.
  91. *
  92. * Returns the position of the next occurrence of the pattern or
  93. * UINT_MAX if not match was found.
  94. */
  95. static inline unsigned int textsearch_next(struct ts_config *conf,
  96. struct ts_state *state)
  97. {
  98. unsigned int ret = conf->ops->find(conf, state);
  99. if (conf->finish)
  100. conf->finish(conf, state);
  101. return ret;
  102. }
  103. /**
  104. * textsearch_find - start searching for a pattern
  105. * @conf: search configuration
  106. * @state: search state
  107. *
  108. * Returns the position of first occurrence of the pattern or
  109. * UINT_MAX if no match was found.
  110. */
  111. static inline unsigned int textsearch_find(struct ts_config *conf,
  112. struct ts_state *state)
  113. {
  114. state->offset = 0;
  115. return textsearch_next(conf, state);
  116. }
  117. /**
  118. * textsearch_get_pattern - return head of the pattern
  119. * @conf: search configuration
  120. */
  121. static inline void *textsearch_get_pattern(struct ts_config *conf)
  122. {
  123. return conf->ops->get_pattern(conf);
  124. }
  125. /**
  126. * textsearch_get_pattern_len - return length of the pattern
  127. * @conf: search configuration
  128. */
  129. static inline unsigned int textsearch_get_pattern_len(struct ts_config *conf)
  130. {
  131. return conf->ops->get_pattern_len(conf);
  132. }
  133. extern int textsearch_register(struct ts_ops *);
  134. extern int textsearch_unregister(struct ts_ops *);
  135. extern struct ts_config *textsearch_prepare(const char *, const void *,
  136. unsigned int, gfp_t, int);
  137. extern void textsearch_destroy(struct ts_config *conf);
  138. extern unsigned int textsearch_find_continuous(struct ts_config *,
  139. struct ts_state *,
  140. const void *, unsigned int);
  141. #define TS_PRIV_ALIGNTO 8
  142. #define TS_PRIV_ALIGN(len) (((len) + TS_PRIV_ALIGNTO-1) & ~(TS_PRIV_ALIGNTO-1))
  143. static inline struct ts_config *alloc_ts_config(size_t payload,
  144. gfp_t gfp_mask)
  145. {
  146. struct ts_config *conf;
  147. conf = kzalloc(TS_PRIV_ALIGN(sizeof(*conf)) + payload, gfp_mask);
  148. if (conf == NULL)
  149. return ERR_PTR(-ENOMEM);
  150. return conf;
  151. }
  152. static inline void *ts_config_priv(struct ts_config *conf)
  153. {
  154. return ((u8 *) conf + TS_PRIV_ALIGN(sizeof(struct ts_config)));
  155. }
  156. #endif