diff --git a/include/common/standard.h b/include/common/standard.h index 8c5871f64..fe3e13c99 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -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 characters from and always terminates with '\0' */ char *my_strndup(const char *src, int n); diff --git a/src/pattern.c b/src/pattern.c index b6f75021a..4f049f735 100644 --- a/src/pattern.c +++ b/src/pattern.c @@ -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. */ diff --git a/src/standard.c b/src/standard.c index 41359c9c8..c31913980 100644 --- a/src/standard.c +++ b/src/standard.c @@ -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 characters from and always terminates with '\0' */ char *my_strndup(const char *src, int n) {