[master] b0d1a40f3 add VSB_QUOTE_GLOB

Guillaume Quintard guillaume at varnish-software.com
Fri Feb 21 16:28:06 UTC 2020


commit b0d1a40f326f07824af91a2b548d3e2e8e8828f3
Author: Guillaume Quintard <guillaume at varnish-software.com>
Date:   Wed Feb 12 01:07:53 2020 -0500

    add VSB_QUOTE_GLOB

diff --git a/include/vsb.h b/include/vsb.h
index 806719b70..a8fe4f6e5 100644
--- a/include/vsb.h
+++ b/include/vsb.h
@@ -84,6 +84,7 @@ void		 VSB_destroy(struct vsb **);
 #define VSB_QUOTE_CSTR		8
 #define VSB_QUOTE_UNSAFE	16
 #define VSB_QUOTE_ESCHEX	32
+#define VSB_QUOTE_GLOB		64
 void		 VSB_quote_pfx(struct vsb *, const char*, const void *,
 		     int len, int how);
 void		 VSB_quote(struct vsb *, const void *, int len, int how);
diff --git a/lib/libvarnish/Makefile.am b/lib/libvarnish/Makefile.am
index bb27d4e1e..6545d0b1b 100644
--- a/lib/libvarnish/Makefile.am
+++ b/lib/libvarnish/Makefile.am
@@ -43,7 +43,7 @@ libvarnish_a_SOURCES = \
 	vtim.c \
 	vus.c
 
-TESTS = vjsn_test vnum_c_test binheap
+TESTS = vjsn_test vnum_c_test binheap vsb_test
 
 noinst_PROGRAMS = ${TESTS}
 
@@ -57,3 +57,6 @@ vnum_c_test_LDADD = ${LIBM} libvarnish.a @SAN_LDFLAGS@
 vjsn_test_SOURCES = vjsn.c
 vjsn_test_CFLAGS = -DVJSN_TEST @SAN_CFLAGS@
 vjsn_test_LDADD = libvarnish.a @SAN_LDFLAGS@
+
+vsb_test_SOURCES = vsb_test.c vsb.c vas.c
+vsb_test_LDADD = libvarnish.a
diff --git a/lib/libvarnish/vsb.c b/lib/libvarnish/vsb.c
index 0762c175e..8964971f2 100644
--- a/lib/libvarnish/vsb.c
+++ b/lib/libvarnish/vsb.c
@@ -563,7 +563,7 @@ VSB_quote_pfx(struct vsb *s, const char *pfx, const void *v, int len, int how)
 		nl = 0;
 		switch (*q) {
 		case '?':
-			if (how & VSB_QUOTE_CSTR)
+			if (how & (VSB_QUOTE_CSTR | VSB_QUOTE_GLOB))
 				(void)VSB_putc(s, '\\');
 			(void)VSB_putc(s, *q);
 			break;
@@ -571,6 +571,10 @@ VSB_quote_pfx(struct vsb *s, const char *pfx, const void *v, int len, int how)
 			(void)VSB_putc(s, *q);
 			break;
 		case '\\':
+			if (!(how & (VSB_QUOTE_UNSAFE)))
+				(void)VSB_putc(s, '\\');
+			(void)VSB_putc(s, *q);
+			break;
 		case '"':
 			if (!(how & VSB_QUOTE_UNSAFE))
 				(void)VSB_putc(s, '\\');
@@ -582,18 +586,42 @@ VSB_quote_pfx(struct vsb *s, const char *pfx, const void *v, int len, int how)
 			} else if (how & (VSB_QUOTE_NONL|VSB_QUOTE_UNSAFE)) {
 				(void)VSB_printf(s, "\n");
 				nl = 1;
+			} else if (how & VSB_QUOTE_GLOB) {
+				(void)VSB_printf(s, "\\\\n");
 			} else {
 				(void)VSB_printf(s, "\\n");
 			}
 			break;
 		case '\r':
-			(void)VSB_cat(s, "\\r");
+			if (how & VSB_QUOTE_GLOB)
+				(void)VSB_cat(s, "\\\\r");
+			else
+				(void)VSB_cat(s, "\\r");
 			break;
 		case '\t':
-			(void)VSB_cat(s, "\\t");
+			if (how & VSB_QUOTE_GLOB)
+				(void)VSB_cat(s, "\\\\t");
+			else
+				(void)VSB_cat(s, "\\t");
 			break;
 		case '\v':
-			(void)VSB_cat(s, "\\v");
+			if (how & VSB_QUOTE_GLOB)
+				(void)VSB_cat(s, "\\\\v");
+			else
+				(void)VSB_cat(s, "\\v");
+			break;
+		case '[':
+			/* FALLTHROUGH */
+		case ']':
+			/* FALLTHROUGH */
+		case '{':
+			/* FALLTHROUGH */
+		case '}':
+			/* FALLTHROUGH */
+		case '*':
+			if (how & VSB_QUOTE_GLOB)
+				(void)VSB_putc(s, '\\');
+			(void)VSB_putc(s, *q);
 			break;
 		default:
 			/* XXX: Implement VSB_QUOTE_JSON */
diff --git a/lib/libvarnish/vsb_test.c b/lib/libvarnish/vsb_test.c
new file mode 100644
index 000000000..5de87118a
--- /dev/null
+++ b/lib/libvarnish/vsb_test.c
@@ -0,0 +1,49 @@
+#include <stdio.h>
+#include <string.h>
+
+#include "vdef.h"
+#include "vas.h"
+#include "vsb.h"
+
+struct tc {
+	int		how;
+	const char	*in;
+	const char	*out;
+};
+
+struct tc tcs[] = {
+	{ VSB_QUOTE_GLOB, "abcdefghijklmnopqrstvwxyz", "abcdefghijklmnopqrstvwxyz" },
+	{ VSB_QUOTE_GLOB, "ABCDEFGHIJKLMNOPQRSTVWXYZ", "ABCDEFGHIJKLMNOPQRSTVWXYZ" },
+	{ VSB_QUOTE_GLOB, "01234567789", "01234567789"},
+	{ VSB_QUOTE_GLOB, "abcde[f-g]{h,i,j}\\l?*xyz", "abcde\\[f-g\\]\\{h,i,j\\}\\\\l\\?\\*xyz"},
+	{ VSB_QUOTE_GLOB, "0123\t \"\r\v\n'", "0123\\\\t \\\"\\\\r\\\\v\\\\n'"},
+	{0, NULL, NULL}
+};
+
+int main(int argc, char *argv[])
+{
+	int err = 0;
+	struct tc *tc;
+	struct vsb *vsb;
+
+	(void)argc;
+	(void)argv;
+	vsb = VSB_new_auto();
+	AN(vsb);
+
+	for (tc = tcs; tc->in; tc++) {
+		VSB_quote(vsb, tc->in, -1, tc->how);
+		VSB_finish(vsb);
+
+		printf("%s -> %s", tc->in, VSB_data(vsb));
+		if (strcmp(VSB_data(vsb), tc->out)) {
+			printf(", but should have been %s",  tc->out);
+			err = 1;
+		}
+		printf("\n");
+		VSB_clear(vsb);
+	}
+	VSB_delete(vsb);
+	printf("error is %i\n", err);
+	return (err);
+}


More information about the varnish-commit mailing list