]> source.dussan.org Git - rspamd.git/commitdiff
Implement regexp search.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 5 Mar 2015 16:49:27 +0000 (16:49 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 5 Mar 2015 16:49:27 +0000 (16:49 +0000)
src/libutil/regexp.c
src/libutil/regexp.h

index f9eef03858ceb0463100af6499286dc595bde257..57515703e9e7baf440284f5d6a9c3b694498ec21 100644 (file)
@@ -255,11 +255,84 @@ fin:
 
 gboolean
 rspamd_regexp_search (rspamd_regexp_t *re, const gchar *text, gsize len,
-               gboolean raw)
+               const gchar **start, const gchar **end, gboolean raw)
 {
+       pcre *r;
+       pcre_extra *ext;
+#ifdef HAVE_PCRE_JIT
+       pcre_jit_stack *st;
+#endif
+       const gchar *mt;
+       gsize remain;
+       gint rc, match_flags = 0, ovec[10];
+
        g_assert (re != NULL);
        g_assert (text != NULL);
 
+       if (end != NULL && *end != NULL) {
+               /* Incremental search */
+               mt = (*end) + 1;
+               remain = len - (mt - text);
+       }
+       else {
+               mt = text;
+               remain = len;
+       }
+
+       match_flags = PCRE_NEWLINE_ANYCRLF;
+       if ((re->flags & RSPAMD_REGEXP_FLAG_RAW) || raw) {
+               r = re->raw_re;
+               ext = re->raw_extra;
+#ifdef HAVE_PCRE_JIT
+               st = re->raw_jstack;
+#endif
+       }
+       else {
+               match_flags |= PCRE_NO_UTF8_CHECK;
+               r = re->re;
+               ext = re->extra;
+#ifdef HAVE_PCRE_JIT
+               st = re->jstack;
+#endif
+       }
+
+       g_assert (r != NULL);
+
+#ifdef HAVE_PCRE_JIT
+       if (re->mtx) {
+               rspamd_mutex_lock (re->mtx);
+       }
+#endif
+
+       if (!(re->flags & RSPAMD_REGEXP_FLAG_NOOPT)) {
+#ifdef HAVE_PCRE_JIT
+               rc = pcre_jit_exec (r, ext, mt, remain, 0, match_flags, ovec,
+                               G_N_ELEMENTS (ovec), st);
+#else
+               rc = pcre_exec (r, ext, mt, remain, 0, match_flags, ovec,
+                               G_N_ELEMENTS (ovec));
+#endif
+       }
+       else {
+               rc = pcre_exec (r, ext, mt, remain, 0, match_flags, ovec,
+                               G_N_ELEMENTS (ovec));
+       }
+#ifdef HAVE_PCRE_JIT
+       if (re->mtx) {
+               rspamd_mutex_unlock (re->mtx);
+       }
+#endif
+       if (rc > 0) {
+               if (start) {
+                       *start = mt + ovec[0];
+               }
+               if (end) {
+                       *end = mt + ovec[1];
+               }
+
+               return TRUE;
+       }
+
        return FALSE;
 }
 
index 2c53b20eeda4876294ce2b729c2acb6b5b3b51a5..fdbe44404e699a9801c462864d88b9af804b745e 100644 (file)
@@ -45,10 +45,13 @@ rspamd_regexp_t* rspamd_regexp_new (const gchar *pattern, const gchar *flags,
  * @param re
  * @param text
  * @param len
+ * @param start position of start of match
+ * @param start position of end of match
  * @return
  */
 gboolean rspamd_regexp_search (rspamd_regexp_t *re,
-               const gchar *text, gsize len, gboolean raw);
+               const gchar *text, gsize len,
+               const gchar **start, const gchar **end, gboolean raw);
 
 
 /**