/*
* Rspamd regexp utility functions
*/
-struct rspamd_regexp *
+struct rspamd_regexp_element *
parse_regexp (rspamd_mempool_t * pool, const gchar *line, gboolean raw_mode)
{
const gchar *begin, *end, *p, *src, *start;
gchar *dbegin, *dend;
- struct rspamd_regexp *result, *check;
+ struct rspamd_regexp_element *result, *check;
gint regexp_flags = G_REGEX_OPTIMIZE | G_REGEX_NO_AUTO_CAPTURE;
GError *err = NULL;
}
src = line;
- result = rspamd_mempool_alloc0 (pool, sizeof (struct rspamd_regexp));
+ result = rspamd_mempool_alloc0 (pool, sizeof (struct rspamd_regexp_element));
/* Skip whitespaces */
while (g_ascii_isspace (*line)) {
line++;
/* Avoid multiply regexp structures for similar regexps */
if ((check =
- (struct rspamd_regexp *)re_cache_check (result->regexp_text,
+ (struct rspamd_regexp_element *)re_cache_check (result->regexp_text,
pool)) != NULL) {
/* Additional check for headers */
if (result->type == REGEXP_HEADER || result->type ==
#include <lua.h>
struct rspamd_task;
-struct rspamd_regexp;
+struct rspamd_regexp_element;
/**
* Rspamd expression function
* @param line incoming line
* @return regexp structure or NULL in case of error
*/
-struct rspamd_regexp * parse_regexp (rspamd_mempool_t *pool,
+struct rspamd_regexp_element * parse_regexp (rspamd_mempool_t *pool,
const gchar *line,
gboolean raw_mode);
* @param result numeric result of this regexp
*/
void task_cache_add (struct rspamd_task *task,
- struct rspamd_regexp *re,
+ struct rspamd_regexp_element *re,
gint32 result);
/**
* @param pointer regexp data
* @return numeric result if value exists or -1 if not
*/
-gint32 task_cache_check (struct rspamd_task *task, struct rspamd_regexp *re);
+gint32 task_cache_check (struct rspamd_task *task, struct rspamd_regexp_element *re);
/**
* Parse and return a single function argument for a function (may recurse)
/**
* Regexp structure
*/
-struct rspamd_regexp {
+struct rspamd_regexp_element {
enum rspamd_regexp_type type; /**< regexp type */
gchar *regexp_text; /**< regexp text representation */
GRegex *regexp; /**< glib regexp structure */
${CMAKE_CURRENT_SOURCE_DIR}/mem_pool.c
${CMAKE_CURRENT_SOURCE_DIR}/printf.c
${CMAKE_CURRENT_SOURCE_DIR}/radix.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/regexp.c
${CMAKE_CURRENT_SOURCE_DIR}/rrd.c
${CMAKE_CURRENT_SOURCE_DIR}/shingles.c
${CMAKE_CURRENT_SOURCE_DIR}/trie.c
--- /dev/null
+/*
+ * Copyright (c) 2015, Vsevolod Stakhov
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "regexp.h"
+#include "blake2.h"
+#include "ref.h"
+#include <pcre.h>
+
+struct rspamd_regexp_s {
+ gdouble exec_time; /**< average execution time */
+ pcre *re;
+ pcre_extra *extra;
+ pcre *raw_re;
+ pcre_extra *raw_extra;
+ guchar id[BLAKE2B_OUTBYTES / 2];
+ ref_entry_t ref;
+};
+
+struct rspamd_regexp_cache {
+ GHashTable *tbl;
+};
+
+static struct rspamd_regexp_cache *global_re_cache = NULL;
+
+rspamd_regexp_t*
+rspamd_regexp_new (const gchar *pattern, const gchar *flags,
+ GError **err)
+{
+ return NULL;
+}
+
+gboolean
+rspamd_regexp_search (rspamd_regexp_t *re, const gchar *text, gsize len)
+{
+ return FALSE;
+}
+
+gboolean
+rspamd_regexp_match (rspamd_regexp_t *re, const gchar *text, gsize len)
+{
+ return FALSE;
+}
+
+void
+rspamd_regexp_unref (rspamd_regexp_t *re)
+{
+ REF_RELEASE (re);
+}
+
+struct rspamd_regexp_cache*
+rspamd_regexp_cache_new (void)
+{
+ return NULL;
+}
+
+
+rspamd_regexp_t*
+rspamd_regexp_cache_query (struct rspamd_regexp_cache* cache,
+ const gchar *pattern,
+ const gchar *flags)
+{
+ return NULL;
+}
+
+
+rspamd_regexp_t*
+rspamd_regexp_cache_create (struct rspamd_regexp_cache *cache,
+ const gchar *pattern,
+ const gchar *flags, GError **err)
+{
+ return NULL;
+}
+
+void
+rspamd_regexp_cache_destroy (struct rspamd_regexp_cache *cache)
+{
+
+}
--- /dev/null
+/*
+ * Copyright (c) 2015, Vsevolod Stakhov
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#ifndef REGEXP_H_
+#define REGEXP_H_
+
+#include "config.h"
+
+typedef struct rspamd_regexp_s rspamd_regexp_t;
+struct rspamd_regexp_cache;
+
+/**
+ * Create new rspamd regexp
+ * @param pattern regexp pattern
+ * @param flags flags (may be enclosed inside pattern)
+ * @param err error pointer set if compilation failed
+ * @return new regexp object
+ */
+rspamd_regexp_t* rspamd_regexp_new (const gchar *pattern, const gchar *flags,
+ GError **err);
+
+/**
+ * Search the specified regexp in the text
+ * @param re
+ * @param text
+ * @param len
+ * @return
+ */
+gboolean rspamd_regexp_search (rspamd_regexp_t *re, const gchar *text, gsize len);
+
+
+/**
+ * Exact match of the specified text against the regexp
+ * @param re
+ * @param text
+ * @param len
+ * @return
+ */
+gboolean rspamd_regexp_match (rspamd_regexp_t *re, const gchar *text, gsize len);
+
+/**
+ * Increase refcount for a regexp object
+ */
+rspamd_regexp_t* rspamd_regexp_ref (rspamd_regexp_t *re);
+
+/**
+ * Unref regexp object
+ * @param re
+ */
+void rspamd_regexp_unref (rspamd_regexp_t *re);
+
+/**
+ * Create new regexp cache
+ * @return
+ */
+struct rspamd_regexp_cache* rspamd_regexp_cache_new (void);
+
+/**
+ * Query rspamd cache for a specified regexp
+ * @param cache regexp cache. if NULL, the superglobal cache is used (*not* thread-safe)
+ * @param pattern
+ * @param flags
+ * @return
+ */
+rspamd_regexp_t* rspamd_regexp_cache_query (struct rspamd_regexp_cache* cache,
+ const gchar *pattern,
+ const gchar *flags);
+
+/**
+ * Create or get cached regexp from the specified cache
+ * @param cache regexp cache. if NULL, the superglobal cache is used (*not* thread-safe)
+ * @param pattern regexp pattern
+ * @param flags flags (may be enclosed inside pattern)
+ * @param err error pointer set if compilation failed
+ * @return new regexp object
+ */
+rspamd_regexp_t* rspamd_regexp_cache_create (struct rspamd_regexp_cache *cache,
+ const gchar *pattern,
+ const gchar *flags, GError **err);
+
+/**
+ * Destroy regexp cache and unref all elements inside it
+ * @param cache
+ */
+void rspamd_regexp_cache_destroy (struct rspamd_regexp_cache *cache);
+
+#endif /* REGEXP_H_ */
void
task_cache_add (struct rspamd_task *task,
- struct rspamd_regexp *re,
+ struct rspamd_regexp_element *re,
gint32 result)
{
if (result == 0) {
}
gint32
-task_cache_check (struct rspamd_task *task, struct rspamd_regexp *re)
+task_cache_check (struct rspamd_task *task, struct rspamd_regexp_element *re)
{
gpointer res;
gint32 r;
struct url_regexp_param {
struct rspamd_task *task;
GRegex *regexp;
- struct rspamd_regexp *re;
+ struct rspamd_regexp_element *re;
gboolean found;
};
}
static gsize
-process_regexp (struct rspamd_regexp *re,
+process_regexp (struct rspamd_regexp_element *re,
struct rspamd_task *task,
const gchar *additional,
gint limit,
GQueue *stack;
gsize cur, op1, op2;
struct expression *it = expr;
- struct rspamd_regexp *re;
+ struct rspamd_regexp_element *re;
gboolean try_optimize = TRUE;
stack = g_queue_new ();
while (it) {
if (it->type == EXPR_REGEXP_PARSED) {
/* Find corresponding symbol */
- cur = process_regexp ((struct rspamd_regexp *)it->content.operand,
+ cur = process_regexp ((struct rspamd_regexp_element *)it->content.operand,
task,
additional,
0,
{
gint limit;
struct expression_argument *arg;
- struct rspamd_regexp *re;
+ struct rspamd_regexp_element *re;
gchar *param, *err_str, op;
int_compare_func f = NULL;
const gchar *re_text,
const gchar *what)
{
- struct rspamd_regexp *re;
+ struct rspamd_regexp_element *re;
gint r;
if (*re_text == '/') {
void *ud = luaL_checkudata (L, 1, "rspamd{task}");
struct rspamd_task *task;
const gchar *re_text;
- struct rspamd_regexp *re;
+ struct rspamd_regexp_element *re;
gint r = 0;
luaL_argcheck (L, ud != NULL, 1, "'task' expected");
{
gchar *param_name, *param_pattern;
const gchar *param_data;
- struct rspamd_regexp *re;
+ struct rspamd_regexp_element *re;
struct expression_argument *arg, *arg1;
GMimeObject *part;
GMimeContentType *ct;
void *unused)
{
gchar *param_pattern;
- struct rspamd_regexp *re;
+ struct rspamd_regexp_element *re;
struct expression_argument *arg, *arg1;
GMimeObject *part;
GMimeContentType *ct;
void *unused)
{
gchar *param_pattern;
- struct rspamd_regexp *re;
+ struct rspamd_regexp_element *re;
struct expression_argument *arg, *arg1;
GMimeObject *part;
GMimeContentType *ct;
compare_subtype (struct rspamd_task *task, GMimeContentType * ct,
gchar *subtype)
{
- struct rspamd_regexp *re;
+ struct rspamd_regexp_element *re;
gint r;
if (subtype == NULL || ct == NULL) {
gint min_len,
gint max_len)
{
- struct rspamd_regexp *re;
+ struct rspamd_regexp_element *re;
struct mime_part *part;
GList *cur;
GMimeContentType *ct;