[master] d8efa56 Unify sendhex and fix at least one bug therein.
Poul-Henning Kamp
phk at FreeBSD.org
Fri Feb 17 23:48:05 CET 2017
commit d8efa56adcc504f36a9fded104e99b6bfc457733
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Fri Feb 17 21:54:47 2017 +0000
Unify sendhex and fix at least one bug therein.
diff --git a/bin/varnishtest/Makefile.am b/bin/varnishtest/Makefile.am
index 636edd4..3710cf9 100644
--- a/bin/varnishtest/Makefile.am
+++ b/bin/varnishtest/Makefile.am
@@ -47,6 +47,7 @@ varnishtest_SOURCES = \
vtc_process.c \
vtc_proxy.c \
vtc_server.c \
+ vtc_subr.c \
vtc_varnish.c
varnishtest_LDADD = \
diff --git a/bin/varnishtest/vtc.h b/bin/varnishtest/vtc.h
index a25d4ff..34460ce 100644
--- a/bin/varnishtest/vtc.h
+++ b/bin/varnishtest/vtc.h
@@ -124,3 +124,6 @@ void cmd_stream(CMD_ARGS);
void start_h2(struct http *hp);
void stop_h2(struct http *hp);
void b64_settings(const struct http *hp, const char *s);
+
+/* vtc_subr.c */
+struct vsb *vtc_hex_to_bin(struct vtclog *vl, const char *arg);
diff --git a/bin/varnishtest/vtc_http.c b/bin/varnishtest/vtc_http.c
index 77a545e..d9a233d 100644
--- a/bin/varnishtest/vtc_http.c
+++ b/bin/varnishtest/vtc_http.c
@@ -1404,38 +1404,20 @@ cmd_http_send_urgent(CMD_ARGS)
static void
cmd_http_sendhex(CMD_ARGS)
{
+ struct vsb *vsb;
struct http *hp;
- char buf[3], *q;
- uint8_t *p;
- int i, j, l;
+ int j;
(void)cmd;
(void)vl;
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
AN(av[1]);
AZ(av[2]);
- l = strlen(av[1]) / 2;
- p = calloc(1, l);
- AN(p);
- q = av[1];
- for (i = 0; i < l; i++) {
- while (vct_issp(*q))
- q++;
- if (*q == '\0')
- break;
- memcpy(buf, q, 2);
- q += 2;
- buf[2] = '\0';
- if (!vct_ishex(buf[0]) || !vct_ishex(buf[1]))
- vtc_fatal(hp->vl, "Illegal Hex char \"%c%c\"",
- buf[0], buf[1]);
- p[i] = (uint8_t)strtoul(buf, NULL, 16);
- }
- vtc_hexdump(hp->vl, 4, "sendhex", (void*)p, i);
- j = write(hp->fd, p, i);
- assert(j == i);
- free(p);
-
+ vsb = vtc_hex_to_bin(hp->vl, av[1]);
+ vtc_hexdump(hp->vl, 4, "sendhex", VSB_data(vsb), VSB_len(vsb));
+ j = write(hp->fd, VSB_data(vsb), VSB_len(vsb));
+ assert(j == VSB_len(vsb));
+ VSB_destroy(&vsb);
}
/* SECTION: client-server.spec.chunked
diff --git a/bin/varnishtest/vtc_http2.c b/bin/varnishtest/vtc_http2.c
index e87fd88..cbd3281 100644
--- a/bin/varnishtest/vtc_http2.c
+++ b/bin/varnishtest/vtc_http2.c
@@ -1225,41 +1225,20 @@ cmd_sendhex(CMD_ARGS)
{
struct http *hp;
struct stream *s;
- char *q;
- char *buf;
- char tmp[3];
- int i;
- unsigned size = 0;
- (void)cmd;
+ struct vsb *vsb;
+ (void)cmd;
+ (void)vl;
CAST_OBJ_NOTNULL(s, priv, STREAM_MAGIC);
CAST_OBJ_NOTNULL(hp, s->hp, HTTP_MAGIC);
AN(av[1]);
AZ(av[2]);
-
- q = av[1];
- size = strlen(q)/2;
- buf = malloc(size);
- AN(buf);
- for (i = 0; i < size; i++) {
- while (vct_issp(*q))
- q++;
- if (*q == '\0')
- break;
- memcpy(tmp, q, 2);
- q += 2;
- tmp[2] = '\0';
- if (!vct_ishex(tmp[0]) || !vct_ishex(tmp[1]))
- vtc_fatal(vl, "Illegal Hex char \"%c%c\"",
- tmp[0], tmp[1]);
- buf[i] = (uint8_t)strtoul(tmp, NULL, 16);
- }
+ vsb = vtc_hex_to_bin(hp->vl, av[1]);
+ vtc_hexdump(hp->vl, 4, "sendhex", VSB_data(vsb), VSB_len(vsb));
AZ(pthread_mutex_lock(&hp->mtx));
- http_write(hp, 4, buf, i, "sendhex");
-
+ http_write(hp, 4, VSB_data(vsb), VSB_len(vsb), "sendhex");
AZ(pthread_mutex_unlock(&hp->mtx));
- vtc_hexdump(vl, 4, "sendhex", (void *)buf, size);
- free(buf);
+ VSB_destroy(&vsb);
}
#define ENC(hdr, k, v) \
diff --git a/bin/varnishtest/vtc_subr.c b/bin/varnishtest/vtc_subr.c
new file mode 100644
index 0000000..6a8f1b7
--- /dev/null
+++ b/bin/varnishtest/vtc_subr.c
@@ -0,0 +1,66 @@
+/*-
+ * Copyright (c) 2008-2017 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#include "vtc.h"
+
+#include "vct.h"
+
+struct vsb *
+vtc_hex_to_bin(struct vtclog *vl, const char *arg)
+{
+ struct vsb *vsb;
+ unsigned sh = 4;
+ unsigned c, b = 0;
+
+ vsb = VSB_new_auto();
+ AN(vsb);
+ for (; *arg != '\0'; arg++) {
+ if (vct_issp(*arg))
+ continue;
+ c = (uint8_t)*arg;
+ if (c >= '0' && c <= '9')
+ b |= (c - 48U) << sh;
+ else if (c >= 'A' && c <= 'F')
+ b |= (c - 55U) << sh;
+ else if (c >= 'a' && c <= 'f')
+ b |= (c - 87U) << sh;
+ else
+ vtc_fatal(vl,"Illegal hex string");
+ sh = 4 - sh;
+ if (sh == 4) {
+ VSB_putc(vsb, b);
+ b = 0;
+ }
+ }
+ if (sh != 4)
+ VSB_putc(vsb, b);
+ AZ(VSB_finish(vsb));
+ return (vsb);
+}
More information about the varnish-commit
mailing list