textsearch.h 4.7 KB

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