]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: regex: Create regex_comp function that compiles regex using compilation...
authorThierry FOURNIER <tfournier@exceliance.fr>
Mon, 14 Oct 2013 12:07:36 +0000 (14:07 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 14 Oct 2013 12:42:50 +0000 (14:42 +0200)
The current file "regex.h" define an abstraction for the regex. It
provides the same struct name and the same "regexec" function for the
3 regex types supported: standard libc, basic pcre and jit pcre.

The regex compilation function is not provided by this file. If the
developper wants to use regex, he must write regex compilation code
containing "#define *JIT*".

This patch provides a unique regex compilation function according to
the compilation options.

In addition, the "regex.h" file checks the presence of the "#define
PCRE_CONFIG_JIT" when "USE_PCRE_JIT" is enabled. If this flag is not
present, the pcre lib doesn't support JIT and "#error" is emitted.

include/common/regex.h
src/acl.c
src/regex.c

index 1cc471b3329c476b102119315daf5c8997eb0ff2..9080bda444cc8dc38bf9efe3565350b45462cc9a 100644 (file)
@@ -31,6 +31,9 @@
 #include <pcreposix.h>
 
 #ifdef USE_PCRE_JIT
+#ifndef PCRE_CONFIG_JIT
+#error "The PCRE lib doesn't support JIT. Change your lib, or remove the option USE_PCRE_JIT."
+#endif
 struct jit_regex {
     pcre *reg;
     pcre_extra *extra;
@@ -64,6 +67,17 @@ struct hdr_exp {
 
 extern regmatch_t pmatch[MAX_MATCH];
 
+/* "str" is the string that contain the regex to compile.
+ * "regex" is preallocated memory. After the execution of this function, this
+ *         struct contain the compiled regex.
+ * "cs" is the case sensitive flag. If cs is true, case sensitive is enabled.
+ * "cap" is capture flag. If cap if true the regex can capture into
+ *       parenthesis strings.
+ * "err" is the standar error message pointer.
+ *
+ * The function return 1 is succes case, else return 0 and err is filled.
+ */
+int regex_comp(const char *str, regex *regex, int cs, int cap, char **err);
 int exp_replace(char *dst, char *src, const char *str, const regmatch_t *matches);
 const char *check_replace_string(const char *str);
 const char *chain_regex(struct hdr_exp **head, const regex_t *preg,
index b6e55ec2628d745a5f851c3e06bb13d0cecf132c..7dc25fb9b4f37e084c582f468d97f2d3e311bd96 100644 (file)
--- a/src/acl.c
+++ b/src/acl.c
@@ -543,11 +543,6 @@ static void acl_free_reg(void *ptr)
 int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
 {
        regex *preg;
-       int icase;
-#ifdef USE_PCRE_JIT
-       const char *error;
-       int erroffset;
-#endif
 
        preg = calloc(1, sizeof(*preg));
 
@@ -556,31 +551,10 @@ int acl_parse_reg(const char **text, struct acl_pattern *pattern, int *opaque, c
                return 0;
        }
 
-#ifdef USE_PCRE_JIT
-       icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? PCRE_CASELESS : 0;
-       preg->reg = pcre_compile(*text, PCRE_NO_AUTO_CAPTURE | icase, &error, &erroffset,
-               NULL);
-       if (!preg->reg) {
-               free(preg);
-               memprintf(err, "regex '%s' is invalid (error=%s, erroffset=%d)", *text, error, erroffset);
-               return 0;
-       }
-
-       preg->extra = pcre_study(preg->reg, PCRE_STUDY_JIT_COMPILE, &error);
-       if (!preg->extra) {
-               pcre_free(preg->reg);
-               free(preg);
-               memprintf(err, "failed to compile regex '%s' (error=%s)", *text, error);
-               return 0;
-       }
-#else
-       icase = (pattern->flags & ACL_PAT_F_IGNORE_CASE) ? REG_ICASE : 0;
-       if (regcomp(preg, *text, REG_EXTENDED | REG_NOSUB | icase) != 0) {
+       if (!regex_comp(*text, preg, !(pattern->flags & ACL_PAT_F_IGNORE_CASE), 0, err)) {
                free(preg);
-               memprintf(err, "regex '%s' is invalid", *text);
                return 0;
        }
-#endif
 
        pattern->ptr.reg = preg;
        pattern->freeptrbuf = &acl_free_reg;
index 1455fb4529d87bd1c3d1c5215f994e4efa47643e..a268996b01bcd9a7f2926ffa5739d2b57358fefd 100644 (file)
@@ -122,7 +122,45 @@ const char *chain_regex(struct hdr_exp **head, const regex_t *preg,
        return NULL;
 }
 
+int regex_comp(const char *str, regex *regex, int cs, int cap, char **err)
+{
+#ifdef USE_PCRE_JIT
+       int flags = 0;
+       const char *error;
+       int erroffset;
+
+       if (!cs)
+               flags |= PCRE_CASELESS;
+       if (!cap)
+               flags |= PCRE_NO_AUTO_CAPTURE;
+
+       regex->reg = pcre_compile(str, flags, &error, &erroffset, NULL);
+       if (!regex->reg) {
+               memprintf(err, "regex '%s' is invalid (error=%s, erroffset=%d)", str, error, erroffset);
+               return 0;
+       }
+
+       regex->extra = pcre_study(regex->reg, PCRE_STUDY_JIT_COMPILE, &error);
+       if (!regex->extra) {
+               pcre_free(regex->reg);
+               memprintf(err, "failed to compile regex '%s' (error=%s)", str, error);
+               return 0;
+       }
+#else
+       int flags = REG_EXTENDED;
 
+       if (!cs)
+               flags |= REG_ICASE;
+       if (!cap)
+               flags |= REG_NOSUB;
+
+       if (regcomp(regex, str, flags) != 0) {
+               memprintf(err, "regex '%s' is invalid", str);
+               return 0;
+       }
+#endif
+       return 1;
+}
 
 /*
  * Local variables: