]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Add method to check regexp maps
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 16 Mar 2016 17:50:14 +0000 (17:50 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 16 Mar 2016 17:50:14 +0000 (17:50 +0000)
src/libutil/map.c
src/libutil/map.h

index b1ebc1e4c52f4b5b1096a647c11afe80278f4535..ffb426ea08427b271a6ad209a109d2b284dab7ec 100644 (file)
@@ -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;
+}
index c37e9024c193bba895c2a6464706d1f0bdb0084d..f7cbc30761a072e2196bca370d0700ffbe7e0cb8 100644 (file)
@@ -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