You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

expression.h 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef SRC_LIBUTIL_EXPRESSION_H_
  17. #define SRC_LIBUTIL_EXPRESSION_H_
  18. #include "config.h"
  19. #include "mem_pool.h"
  20. #include "fstring.h"
  21. #include "util.h"
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #define RSPAMD_EXPRESSION_MAX_PRIORITY 1024
  26. #define RSPAMD_EXPRESSION_FLAG_NOOPT (1 << 0)
  27. enum rspamd_expression_op {
  28. OP_INVALID = 0,
  29. OP_PLUS, /* + */
  30. OP_MULT, /* * */
  31. OP_MINUS, /* - */
  32. OP_DIVIDE, /* / */
  33. OP_OR, /* || or | */
  34. OP_AND, /* && or & */
  35. OP_NOT, /* ! */
  36. OP_LT, /* < */
  37. OP_GT, /* > */
  38. OP_LE, /* <= */
  39. OP_GE, /* >= */
  40. OP_EQ, /* == */
  41. OP_NE, /* != */
  42. OP_OBRACE, /* ( */
  43. OP_CBRACE /* ) */
  44. };
  45. typedef struct rspamd_expression_atom_s {
  46. /* Parent node */
  47. GNode *parent;
  48. /* Opaque userdata */
  49. gpointer data;
  50. /* String representation of atom */
  51. const char *str;
  52. /* Length of the string representation of atom */
  53. unsigned int len;
  54. /* Relative priority */
  55. int priority;
  56. unsigned int hits;
  57. struct rspamd_counter_data exec_time;
  58. } rspamd_expression_atom_t;
  59. typedef double (*rspamd_expression_process_cb)(gpointer runtime_data,
  60. rspamd_expression_atom_t *atom);
  61. struct rspamd_atom_subr {
  62. /* Parses atom from string and returns atom structure */
  63. rspamd_expression_atom_t *(*parse)(const char *line, gsize len,
  64. rspamd_mempool_t *pool, gpointer ud, GError **err);
  65. /* Process atom via the opaque pointer (e.g. struct rspamd_task *) */
  66. rspamd_expression_process_cb process;
  67. /* Calculates the relative priority of the expression */
  68. int (*priority)(rspamd_expression_atom_t *atom);
  69. void (*destroy)(rspamd_expression_atom_t *atom);
  70. };
  71. /* Opaque structure */
  72. struct rspamd_expression;
  73. /**
  74. * Parse symbolic expression and create the expression using the specified subroutines for atoms processing
  75. * @param line line to parse
  76. * @param len length of the line (if 0 then line should be NULL terminated)
  77. * @param subr subroutines for atoms parsing
  78. * @param subr_data opaque dat pointer
  79. * @param pool pool to use for memory allocations
  80. * @param err error pointer
  81. * @param target the target expression
  82. * @return TRUE if an expression have been parsed
  83. */
  84. gboolean rspamd_parse_expression(const char *line, gsize len,
  85. const struct rspamd_atom_subr *subr, gpointer subr_data,
  86. rspamd_mempool_t *pool, GError **err,
  87. struct rspamd_expression **target);
  88. /**
  89. * Process the expression and return its value using atom 'process' functions with the specified data pointer
  90. * @param expr expression to process
  91. * @param data opaque data pointer for all the atoms
  92. * @return the value of expression
  93. */
  94. double rspamd_process_expression(struct rspamd_expression *expr,
  95. int flags,
  96. gpointer runtime_ud);
  97. /**
  98. * Process the expression and return its value using atom 'process' functions with the specified data pointer.
  99. * This function also accepts `track` argument where it writes matched atoms (those whose value is more than 0)
  100. * @param expr expression to process
  101. * @param data opaque data pointer for all the atoms
  102. * @param track pointer array to atoms tracking
  103. * @return the value of expression
  104. */
  105. double rspamd_process_expression_track(struct rspamd_expression *expr,
  106. int flags,
  107. gpointer runtime_ud,
  108. GPtrArray **track);
  109. /**
  110. * Process the expression with the custom processor
  111. * @param expr
  112. * @param cb
  113. * @param process_data
  114. * @return
  115. */
  116. double rspamd_process_expression_closure(struct rspamd_expression *expr,
  117. rspamd_expression_process_cb cb,
  118. int flags,
  119. gpointer runtime_ud,
  120. GPtrArray **track);
  121. /**
  122. * Shows string representation of an expression
  123. * @param expr expression to show
  124. * @return freshly allocated string with expression
  125. */
  126. GString *rspamd_expression_tostring(struct rspamd_expression *expr);
  127. /**
  128. * Callback that is called on @see rspamd_expression_atom_foreach, atom is ephemeral
  129. * and should not be modified within callback
  130. */
  131. typedef void (*rspamd_expression_atom_foreach_cb)(const rspamd_ftok_t *atom,
  132. gpointer ud);
  133. /**
  134. * Traverse over all atoms in the expression
  135. * @param expr expression
  136. * @param cb callback to be called
  137. * @param ud opaque data passed to `cb`
  138. */
  139. void rspamd_expression_atom_foreach(struct rspamd_expression *expr,
  140. rspamd_expression_atom_foreach_cb cb, gpointer cbdata);
  141. /**
  142. * Checks if a specified node in AST is the specified operation
  143. * @param node AST node packed in GNode container
  144. * @param op operation to check
  145. * @return TRUE if node is operation node and is exactly the specified option
  146. */
  147. gboolean rspamd_expression_node_is_op(GNode *node, enum rspamd_expression_op op);
  148. #ifdef __cplusplus
  149. }
  150. #endif
  151. #endif /* SRC_LIBUTIL_EXPRESSION_H_ */