]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: standard: add function "escape_string"
authorDragan Dosen <ddosen@haproxy.com>
Fri, 22 Jul 2016 14:00:31 +0000 (16:00 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 26 Jul 2016 13:25:32 +0000 (15:25 +0200)
Similar to "escape_chunk", this function tries to prefix all characters
tagged in the <map> with the <escape> character. The specified <string>
contains the input to be escaped.

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

index 962a759ca1b4971e9e22def421f0d136bd49f674..63f0345250fe568de91262f367f30f62f1f78ed6 100644 (file)
@@ -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 <map> with the <escape>
+ * character. The input <string> must be zero-terminated. The result will
+ * be stored between <start> (included) and <stop> (excluded). This
+ * function will always try to terminate the resulting string with a '\0'
+ * before <stop>, 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 <map> with the <escape>
  * character. <chunk> contains the input to be escaped. The result will be
index 4d0e3ed14635125ea54bb8aa87fbd195dd2752e8..f002573d7589d5752c860d19f559889f90b131eb 100644 (file)
@@ -1470,6 +1470,36 @@ char *encode_chunk(char *start, char *stop,
        return start;
 }
 
+/*
+ * Tries to prefix characters tagged in the <map> with the <escape>
+ * character. The input <string> must be zero-terminated. The result will
+ * be stored between <start> (included) and <stop> (excluded). This
+ * function will always try to terminate the resulting string with a '\0'
+ * before <stop>, 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 <map> with the <escape>
  * character. <chunk> contains the input to be escaped. The result will be