]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tools: add a generic binary hex string parser
authorWilly Tarreau <w@1wt.eu>
Tue, 3 Dec 2013 16:50:47 +0000 (17:50 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 6 Dec 2013 10:50:47 +0000 (11:50 +0100)
We currently use such an hex parser in pat_parse_bin() to parse hex
string patterns. We'll need another generic one so let's move it to
standard.c and have pat_parse_bin() make use of it.

include/common/standard.h
src/pattern.c
src/standard.c

index 8c5871f64c3f7b1390a28ed39fcdca4b69e9aac3..fe3e13c99547c288f667f03e826c481a2a79183a 100644 (file)
@@ -508,6 +508,14 @@ static inline unsigned int popcount(unsigned int a)
        return cnt;
 }
 
+/*
+ * Parse binary string written in hexadecimal (source) and store the decoded
+ * result into binstr and set binstrlen to the lengh of binstr. Memory for
+ * binstr is allocated by the function. In case of error, returns 0 with an
+ * error message in err.
+ */
+int parse_binary(const char *source, char **binstr, int *binstrlen, char **err);
+
 /* copies at most <n> characters from <src> and always terminates with '\0' */
 char *my_strndup(const char *src, int n);
 
index b6f75021a400935512fee2879962ccb045c4420d..4f049f735b3cc2cd3a91509e510663f282905560 100644 (file)
@@ -440,39 +440,10 @@ int pat_parse_str(const char **text, struct pattern *pattern, struct sample_stor
 /* Parse a binary written in hexa. It is allocated. */
 int pat_parse_bin(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
 {
-       int len;
-       const char *p = *text;
-       int i,j;
-
-       len  = strlen(p);
-       if (len%2) {
-               memprintf(err, "an even number of hex digit is expected");
-               return 0;
-       }
-
        pattern->type = SMP_T_CBIN;
-       pattern->len = len >> 1;
-       pattern->ptr.str = malloc(pattern->len);
        pattern->smp = smp;
-       if (!pattern->ptr.str) {
-               memprintf(err, "out of memory while loading string pattern");
-               return 0;
-       }
-
-       i = j = 0;
-       while (j < pattern->len) {
-               if (!ishex(p[i++]))
-                       goto bad_input;
-               if (!ishex(p[i++]))
-                       goto bad_input;
-               pattern->ptr.str[j++] =  (hex2i(p[i-2]) << 4) + hex2i(p[i-1]);
-       }
-       return 1;
 
-bad_input:
-       memprintf(err, "an hex digit is expected (found '%c')", p[i-1]);
-       free(pattern->ptr.str);
-       return 0;
+       return parse_binary(*text, &pattern->ptr.str, &pattern->len, err);
 }
 
 /* Parse and concatenate all further strings into one. */
index 41359c9c840b428a67c5a5b00ec98a36dbb0c449..c31913980257e13a0c831ebe3cd6fbe5853f1ceb 100644 (file)
@@ -1356,6 +1356,48 @@ const char *parse_size_err(const char *text, unsigned *ret) {
        return NULL;
 }
 
+/*
+ * Parse binary string written in hexadecimal (source) and store the decoded
+ * result into binstr and set binstrlen to the lengh of binstr. Memory for
+ * binstr is allocated by the function. In case of error, returns 0 with an
+ * error message in err.
+ */
+int parse_binary(const char *source, char **binstr, int *binstrlen, char **err)
+{
+       int len;
+       const char *p = source;
+       int i,j;
+
+       len = strlen(source);
+       if (len % 2) {
+               memprintf(err, "an even number of hex digit is expected");
+               return 0;
+       }
+
+       len = len >> 1;
+       *binstrlen = len;
+       *binstr = calloc(len, sizeof(char));
+       if (!*binstr) {
+               memprintf(err, "out of memory while loading string pattern");
+               return 0;
+       }
+
+       i = j = 0;
+       while (j < len) {
+               if (!ishex(p[i++]))
+                       goto bad_input;
+               if (!ishex(p[i++]))
+                       goto bad_input;
+               (*binstr)[j++] =  (hex2i(p[i-2]) << 4) + hex2i(p[i-1]);
+       }
+       return len;
+
+bad_input:
+       memprintf(err, "an hex digit is expected (found '%c')", p[i-1]);
+       free(binstr);
+       return 0;
+}
+
 /* copies at most <n> characters from <src> and always terminates with '\0' */
 char *my_strndup(const char *src, int n)
 {