Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

expression.h 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #define RSPAMD_EXPRESSION_MAX_PRIORITY 1024
  25. #define RSPAMD_EXPRESSION_FLAG_NOOPT (1 << 0)
  26. enum rspamd_expression_op {
  27. OP_INVALID = 0,
  28. OP_PLUS, /* + */
  29. OP_MULT, /* * */
  30. OP_MINUS, /* - */
  31. OP_DIVIDE, /* / */
  32. OP_OR, /* || or | */
  33. OP_AND, /* && or & */
  34. OP_NOT, /* ! */
  35. OP_LT, /* < */
  36. OP_GT, /* > */
  37. OP_LE, /* <= */
  38. OP_GE, /* >= */
  39. OP_OBRACE, /* ( */
  40. OP_CBRACE /* ) */
  41. };
  42. typedef struct rspamd_expression_atom_s {
  43. /* Parent node */
  44. GNode *parent;
  45. /* Opaque userdata */
  46. gpointer data;
  47. /* String representation of atom */
  48. const gchar *str;
  49. /* Length of the string representation of atom */
  50. gsize len;
  51. /* Average execution time (in ticks) */
  52. gdouble avg_ticks;
  53. /* Amount of positive triggers */
  54. guint hits;
  55. /* Relative priority */
  56. gint priority;
  57. } rspamd_expression_atom_t;
  58. typedef gdouble (*rspamd_expression_process_cb) (gpointer runtime_data,
  59. rspamd_expression_atom_t *atom);
  60. struct rspamd_atom_subr {
  61. /* Parses atom from string and returns atom structure */
  62. rspamd_expression_atom_t *(*parse) (const gchar *line, gsize len,
  63. rspamd_mempool_t *pool, gpointer ud, GError **err);
  64. /* Process atom via the opaque pointer (e.g. struct rspamd_task *) */
  65. rspamd_expression_process_cb process;
  66. /* Calculates the relative priority of the expression */
  67. gint (*priority) (rspamd_expression_atom_t *atom);
  68. void (*destroy) (rspamd_expression_atom_t *atom);
  69. };
  70. /* Opaque structure */
  71. struct rspamd_expression;
  72. /**
  73. * Parse symbolic expression and create the expression using the specified subroutines for atoms processing
  74. * @param line line to parse
  75. * @param len length of the line (if 0 then line should be NULL terminated)
  76. * @param subr subroutines for atoms parsing
  77. * @param subr_data opaque dat pointer
  78. * @param pool pool to use for memory allocations
  79. * @param err error pointer
  80. * @param target the target expression
  81. * @return TRUE if an expression have been parsed
  82. */
  83. gboolean rspamd_parse_expression (const gchar *line, gsize len,
  84. const struct rspamd_atom_subr *subr, gpointer subr_data,
  85. rspamd_mempool_t *pool, GError **err,
  86. struct rspamd_expression **target);
  87. /**
  88. * Process the expression and return its value using atom 'process' functions with the specified data pointer
  89. * @param expr expression to process
  90. * @param data opaque data pointer for all the atoms
  91. * @return the value of expression
  92. */
  93. gdouble rspamd_process_expression (struct rspamd_expression *expr,
  94. gint flags,
  95. gpointer runtime_ud);
  96. /**
  97. * Process the expression and return its value using atom 'process' functions with the specified data pointer.
  98. * This function also accepts `track` argument where it writes matched atoms (those whose value is more than 0)
  99. * @param expr expression to process
  100. * @param data opaque data pointer for all the atoms
  101. * @param track pointer array to atoms tracking
  102. * @return the value of expression
  103. */
  104. gdouble rspamd_process_expression_track (struct rspamd_expression *expr,
  105. gint flags,
  106. gpointer runtime_ud,
  107. GPtrArray **track);
  108. /**
  109. * Process the expression with the custom processor
  110. * @param expr
  111. * @param cb
  112. * @param process_data
  113. * @return
  114. */
  115. gdouble rspamd_process_expression_closure (struct rspamd_expression *expr,
  116. rspamd_expression_process_cb cb,
  117. gint flags,
  118. gpointer runtime_ud,
  119. GPtrArray **track);
  120. /**
  121. * Shows string representation of an expression
  122. * @param expr expression to show
  123. * @return freshly allocated string with expression
  124. */
  125. GString *rspamd_expression_tostring (struct rspamd_expression *expr);
  126. /**
  127. * Callback that is called on @see rspamd_expression_atom_foreach, atom is ephemeral
  128. * and should not be modified within callback
  129. */
  130. typedef void (*rspamd_expression_atom_foreach_cb) (const rspamd_ftok_t *atom,
  131. gpointer ud);
  132. /**
  133. * Traverse over all atoms in the expression
  134. * @param expr expression
  135. * @param cb callback to be called
  136. * @param ud opaque data passed to `cb`
  137. */
  138. void rspamd_expression_atom_foreach (struct rspamd_expression *expr,
  139. rspamd_expression_atom_foreach_cb cb, gpointer cbdata);
  140. /**
  141. * Checks if a specified node in AST is the specified operation
  142. * @param node AST node packed in GNode container
  143. * @param op operation to check
  144. * @return TRUE if node is operation node and is exactly the specified option
  145. */
  146. gboolean rspamd_expression_node_is_op (GNode *node, enum rspamd_expression_op op);
  147. #ifdef __cplusplus
  148. }
  149. #endif
  150. #endif /* SRC_LIBUTIL_EXPRESSION_H_ */