textsearch.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #ifndef __LINUX_TEXTSEARCH_H
  2. #define __LINUX_TEXTSEARCH_H
  3. #ifdef __KERNEL__
  4. #include <linux/types.h>
  5. #include <linux/list.h>
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/err.h>
  9. #include <linux/slab.h>
  10. struct ts_config;
  11. /**
  12. * TS_AUTOLOAD - Automatically load textsearch modules when needed
  13. */
  14. #define TS_AUTOLOAD 1
  15. /**
  16. * struct ts_state - search state
  17. * @offset: offset for next match
  18. * @cb: control buffer, for persistent variables of get_next_block()
  19. */
  20. struct ts_state
  21. {
  22. unsigned int offset;
  23. char cb[40];
  24. };
  25. /**
  26. * struct ts_ops - search module operations
  27. * @name: name of search algorithm
  28. * @init: initialization function to prepare a search
  29. * @find: find the next occurrence of the pattern
  30. * @destroy: destroy algorithm specific parts of a search configuration
  31. * @get_pattern: return head of pattern
  32. * @get_pattern_len: return length of pattern
  33. * @owner: module reference to algorithm
  34. */
  35. struct ts_ops
  36. {
  37. const char *name;
  38. struct ts_config * (*init)(const void *, unsigned int, gfp_t);
  39. unsigned int (*find)(struct ts_config *,
  40. struct ts_state *);
  41. void (*destroy)(struct ts_config *);
  42. void * (*get_pattern)(struct ts_config *);
  43. unsigned int (*get_pattern_len)(struct ts_config *);
  44. struct module *owner;
  45. struct list_head list;
  46. };
  47. /**
  48. * struct ts_config - search configuration
  49. * @ops: operations of chosen algorithm
  50. * @get_next_block: callback to fetch the next block to search in
  51. * @finish: callback to finalize a search
  52. */
  53. struct ts_config
  54. {
  55. struct ts_ops *ops;
  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 = kmalloc(TS_PRIV_ALIGN(sizeof(*conf)) + payload, gfp_mask);
  148. if (conf == NULL)
  149. return ERR_PTR(-ENOMEM);
  150. memset(conf, 0, TS_PRIV_ALIGN(sizeof(*conf)) + payload);
  151. return conf;
  152. }
  153. static inline void *ts_config_priv(struct ts_config *conf)
  154. {
  155. return ((u8 *) conf + TS_PRIV_ALIGN(sizeof(struct ts_config)));
  156. }
  157. #endif /* __KERNEL__ */
  158. #endif