]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: standard: add function "encode_chunk"
authorThierry FOURNIER <tfournier@exceliance.fr>
Mon, 17 Mar 2014 11:01:13 +0000 (12:01 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 17 Mar 2014 15:38:56 +0000 (16:38 +0100)
This function has the same behavior as encode_string(), except it
takes a "struct chunk" instead of a "char *" on input.

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

index 14b0eba9b83e703ced20c96e4ae2475fdb2fbbc5..bdb4ef571a80cc3af9204c659b136cf695827852 100644 (file)
@@ -29,6 +29,7 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <netinet/in.h>
+#include <common/chunk.h>
 #include <common/config.h>
 #include <eb32tree.h>
 
@@ -282,6 +283,14 @@ char *encode_string(char *start, char *stop,
                    const char escape, const fd_set *map,
                    const char *string);
 
+/*
+ * Same behavior, except that it encodes chunk <chunk> instead of a string.
+ */
+char *encode_chunk(char *start, char *stop,
+                   const char escape, const fd_set *map,
+                   const struct chunk *chunk);
+
+
 /* Decode an URL-encoded string in-place. The resulting string might
  * be shorter. If some forbidden characters are found, the conversion is
  * aborted, the string is truncated before the issue and non-zero is returned,
index adaba75ba8060e36f4771ea89d833c75733a5981..46c940d66ca0781986893e2a674d0fcaa1974685 100644 (file)
@@ -21,6 +21,7 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
+#include <common/chunk.h>
 #include <common/config.h>
 #include <common/standard.h>
 #include <eb32tree.h>
@@ -1045,6 +1046,36 @@ char *encode_string(char *start, char *stop,
        return start;
 }
 
+/*
+ * Same behavior as encode_string() above, except that it encodes chunk
+ * <chunk> instead of a string.
+ */
+char *encode_chunk(char *start, char *stop,
+                   const char escape, const fd_set *map,
+                   const struct chunk *chunk)
+{
+       char *str = chunk->str;
+       char *end = chunk->str + chunk->len;
+
+       if (start < stop) {
+               stop--; /* reserve one byte for the final '\0' */
+               while (start < stop && str < end) {
+                       if (!FD_ISSET((unsigned char)(*str), map))
+                               *start++ = *str;
+                       else {
+                               if (start + 3 >= stop)
+                                       break;
+                               *start++ = escape;
+                               *start++ = hextab[(*str >> 4) & 15];
+                               *start++ = hextab[*str & 15];
+                       }
+                       str++;
+               }
+               *start = '\0';
+       }
+       return start;
+}
+
 /* Decode an URL-encoded string in-place. The resulting string might
  * be shorter. If some forbidden characters are found, the conversion is
  * aborted, the string is truncated before the issue and a negative value is