[master] f16f148f9 Use VENC instead of local b64 decoder.

Poul-Henning Kamp phk at FreeBSD.org
Fri Aug 18 09:48:09 UTC 2023


commit f16f148f94db153e36fe8b31b2eb4057566103eb
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Fri Aug 18 09:47:14 2023 +0000

    Use VENC instead of local b64 decoder.

diff --git a/bin/varnishncsa/Makefile.am b/bin/varnishncsa/Makefile.am
index 08c8421e9..519f13342 100644
--- a/bin/varnishncsa/Makefile.am
+++ b/bin/varnishncsa/Makefile.am
@@ -8,9 +8,7 @@ bin_PROGRAMS = varnishncsa
 
 varnishncsa_SOURCES = \
 	varnishncsa.c \
-	varnishncsa_options.h \
-	b64.h \
-	b64.c
+	varnishncsa_options.h
 
 varnishncsa_LDADD = \
 	$(top_builddir)/lib/libvarnishapi/libvarnishapi.la \
diff --git a/bin/varnishncsa/b64.c b/bin/varnishncsa/b64.c
deleted file mode 100644
index 5d398f449..000000000
--- a/bin/varnishncsa/b64.c
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Written by Poul-Henning Kamp <phk at phk.freebsd.dk>
- *
- * This file is in the public domain.
- */
-
-#include "config.h"
-
-#include <stdlib.h>
-#include <string.h>
-
-#include "b64.h"
-
-static const char b64[] =
-    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
-static char i64[256];
-
-void
-VB64_init(void)
-{
-	int i;
-	const char *p;
-
-	for (i = 0; i < 256; i++)
-		i64[i] = -1;
-	for (p = b64, i = 0; *p; p++, i++)
-		i64[(int)*p] = (char)i;
-	i64['='] = 0;
-}
-
-int
-VB64_decode(char *d, unsigned dlen, const char *s, const char *e)
-{
-	unsigned u, v, l;
-	int i;
-
-	if (e == NULL)
-		e = s + strlen(s);
-	u = 0;
-	l = 0;
-	while (s < e) {
-		for (v = 0; s < e && v < 4; v++) {
-			i = i64[(int)*s++];
-			if (i < 0)
-				return (-1);
-			u <<= 6;
-			u |= i;
-		}
-		for (v = 0; v < 3; v++) {
-			if (l >= dlen - 1)
-				return (-1);
-			*d = (u >> 16) & 0xff;
-			u <<= 8;
-			l++;
-			d++;
-		}
-	}
-	*d = '\0';
-	return (0);
-}
-
-#ifdef TEST_DRIVER
-
-#include <stdio.h>		// for test-prog
-
-const char *test1 =
-"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz"
-"IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg"
-"dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu"
-"dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo"
-"ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=";
-
-int
-main(int argc, char **argv)
-{
-	int i;
-	char buf[BUFSIZ];
-	unsigned l;
-
-	(void)argc;
-	(void)argv;
-
-	VB64_init();
-	l = sizeof buf;
-	VB64_decode(buf, l, test1, NULL);
-	printf("%s\n", buf);
-	return (0);
-}
-#endif
diff --git a/bin/varnishncsa/b64.h b/bin/varnishncsa/b64.h
deleted file mode 100644
index 6b0aab35b..000000000
--- a/bin/varnishncsa/b64.h
+++ /dev/null
@@ -1,34 +0,0 @@
-/*-
- * Copyright (c) 2006 Verdens Gang AS
- * Copyright (c) 2006-2011 Varnish Software AS
- * All rights reserved.
- *
- * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * 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.
- *
- */
-
-void VB64_init(void);
-int VB64_decode(char *d, unsigned dlen, const char *s, const char *e);
diff --git a/bin/varnishncsa/varnishncsa.c b/bin/varnishncsa/varnishncsa.c
index 37b27834b..0ad6a8708 100644
--- a/bin/varnishncsa/varnishncsa.c
+++ b/bin/varnishncsa/varnishncsa.c
@@ -58,10 +58,10 @@
 
 #include "vdef.h"
 
-#include "b64.h"
 #include "vapi/vsl.h"
 #include "vapi/voptget.h"
 #include "vas.h"
+#include "venc.h"
 #include "vsb.h"
 #include "vut.h"
 #include "vqueue.h"
@@ -361,21 +361,24 @@ format_requestline(const struct format *format)
 static int v_matchproto_(format_f)
 format_auth(const struct format *format)
 {
-	char buf[128];
+	struct vsb *vsb = VSB_new_auto();
+	AN(vsb);
 	char *q;
 
 	if (CTX.frag[F_auth].gen != CTX.gen ||
-	    VB64_decode(buf, sizeof buf, CTX.frag[F_auth].b,
-	    CTX.frag[F_auth].e)) {
+	    VENC_Decode_Base64(vsb, CTX.frag[F_auth].b, CTX.frag[F_auth].e)) {
+		VSB_destroy(&vsb);
 		if (format->string == NULL)
 			return (-1);
 		VSB_quote(CTX.vsb, format->string, -1, CTX.quote_how);
 		return (0);
 	}
-	q = strchr(buf, ':');
+	AZ(VSB_finish(vsb));
+	q = strchr(VSB_data(vsb), ':');
 	if (q != NULL)
 		*q = '\0';
-	VSB_quote(CTX.vsb, buf, -1, CTX.quote_how);
+	VSB_quote(CTX.vsb, VSB_data(vsb), -1, CTX.quote_how);
+	VSB_destroy(&vsb);
 	return (1);
 }
 
@@ -1160,7 +1163,6 @@ main(int argc, char * const *argv)
 	VTAILQ_INIT(&CTX.watch_vsl);
 	CTX.vsb = VSB_new_auto();
 	AN(CTX.vsb);
-	VB64_init();
 	CTX.quote_how = VSB_QUOTE_ESCHEX;
 	REPLACE(CTX.missing_string, "-");
 	REPLACE(CTX.missing_int, "-");


More information about the varnish-commit mailing list