From 0dca31d70718c5bbefc5013052f94115084fc548 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 16 Mar 2016 17:50:14 +0000 Subject: [PATCH] [Feature] Add method to check regexp maps --- src/libutil/map.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++ src/libutil/map.h | 11 ++++++++++ 2 files changed, 66 insertions(+) diff --git a/src/libutil/map.c b/src/libutil/map.c index b1ebc1e4c..ffb426ea0 100644 --- a/src/libutil/map.c +++ b/src/libutil/map.c @@ -1646,3 +1646,58 @@ rspamd_regexp_list_fin (rspamd_mempool_t *pool, struct map_cb_data *data) re_map->regexps->len); } } + +static int +rspamd_match_hs_single_handler (unsigned int id, unsigned long long from, + unsigned long long to, + unsigned int flags, void *context) +{ + guint *i = context; + /* Always return non-zero as we need a single match here */ + + *i = id; + + return 1; +} + +gpointer +rspamd_match_regexp_map (struct rspamd_regexp_map *map, + const gchar *in, gsize len) +{ + guint i; + rspamd_regexp_t *re; + gint res = 0; + gpointer ret = NULL; + + g_assert (in != NULL && len > 0); + + if (map == NULL) { + return NULL; + } + +#ifdef WITH_HYPERSCAN + if (map->hs_db && map->hs_scratch) { + res = hs_scan (map->hs_db, in, len, 0, map->hs_scratch, + rspamd_match_hs_single_handler, (void *)&i); + + if (res == HS_SCAN_TERMINATED) { + res = 1; + ret = g_ptr_array_index (map->values, i); + } + } +#endif + + if (!res) { + /* PCRE version */ + for (i = 0; i < map->regexps->len; i ++) { + re = g_ptr_array_index (map->regexps, i); + + if (rspamd_regexp_search (re, in, len, NULL, NULL, FALSE, NULL)) { + ret = g_ptr_array_index (map->values, i); + break; + } + } + } + + return ret; +} diff --git a/src/libutil/map.h b/src/libutil/map.h index c37e9024c..f7cbc3076 100644 --- a/src/libutil/map.h +++ b/src/libutil/map.h @@ -128,4 +128,15 @@ rspamd_parse_kv_list (rspamd_mempool_t * pool, const gchar *default_value, gboolean final); +/** + * Find a single (any) matching regexp for the specified text or NULL if + * no matches found + * @param map + * @param in + * @param len + * @return + */ +gpointer rspamd_match_regexp_map (struct rspamd_regexp_map *map, + const gchar *in, gsize len); + #endif -- 2.39.5