textsearch.h 4.7 KB

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