From: Dragan Dosen Date: Fri, 22 Jul 2016 14:00:31 +0000 (+0200) Subject: MINOR: standard: add function "escape_string" X-Git-Tag: v1.7-dev4~33 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1a5d06032b989f81c13dee32ea297ede6f1e8b94;p=thirdparty%2Fhaproxy.git MINOR: standard: add function "escape_string" Similar to "escape_chunk", this function tries to prefix all characters tagged in the with the character. The specified contains the input to be escaped. --- diff --git a/include/common/standard.h b/include/common/standard.h index 962a759ca1..63f0345250 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -380,6 +380,18 @@ char *encode_chunk(char *start, char *stop, const char escape, const fd_set *map, const struct chunk *chunk); +/* + * Tries to prefix characters tagged in the with the + * character. The input must be zero-terminated. The result will + * be stored between (included) and (excluded). This + * function will always try to terminate the resulting string with a '\0' + * before , and will return its position if the conversion + * completes. + */ +char *escape_string(char *start, char *stop, + const char escape, const fd_set *map, + const char *string); + /* * Tries to prefix characters tagged in the with the * character. contains the input to be escaped. The result will be diff --git a/src/standard.c b/src/standard.c index 4d0e3ed146..f002573d75 100644 --- a/src/standard.c +++ b/src/standard.c @@ -1470,6 +1470,36 @@ char *encode_chunk(char *start, char *stop, return start; } +/* + * Tries to prefix characters tagged in the with the + * character. The input must be zero-terminated. The result will + * be stored between (included) and (excluded). This + * function will always try to terminate the resulting string with a '\0' + * before , and will return its position if the conversion + * completes. + */ +char *escape_string(char *start, char *stop, + const char escape, const fd_set *map, + const char *string) +{ + if (start < stop) { + stop--; /* reserve one byte for the final '\0' */ + while (start < stop && *string != '\0') { + if (!FD_ISSET((unsigned char)(*string), map)) + *start++ = *string; + else { + if (start + 2 >= stop) + break; + *start++ = escape; + *start++ = *string; + } + string++; + } + *start = '\0'; + } + return start; +} + /* * Tries to prefix characters tagged in the with the * character. contains the input to be escaped. The result will be