[master] b6ce6d6 Vastly simplify the parse_encoding() and parse_case() functions now that VCL_ENUM have canonical pointers.
Poul-Henning Kamp
phk at FreeBSD.org
Fri Dec 1 23:02:06 UTC 2017
commit b6ce6d6770541bdf14383086691f1721efc49a6d
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Fri Dec 1 23:01:07 2017 +0000
Vastly simplify the parse_encoding() and parse_case() functions now
that VCL_ENUM have canonical pointers.
diff --git a/lib/libvmod_blob/Makefile.am b/lib/libvmod_blob/Makefile.am
index 7b5ce59..888895a 100644
--- a/lib/libvmod_blob/Makefile.am
+++ b/lib/libvmod_blob/Makefile.am
@@ -10,9 +10,7 @@ libvmod_blob_la_SOURCES = \
hex.c \
url.c \
wb.h \
- wb.c \
- parse_encoding.h \
- parse_encoding.c
+ wb.c
base64.o: base64.c base64.h
diff --git a/lib/libvmod_blob/parse_encoding.c b/lib/libvmod_blob/parse_encoding.c
deleted file mode 100644
index 2f7a538..0000000
--- a/lib/libvmod_blob/parse_encoding.c
+++ /dev/null
@@ -1,112 +0,0 @@
-/*
- * for the time being, this code is auto-generated outside the varnishd source
- * tree, see
- * https://code.uplex.de/uplex-varnish/libvmod-blobcode/blob/master/src/gen_enum_parse.pl
- *
- * TODO: integrate in vmodtool.py or replace with something else
- * cf. the same TODO for the shard director in libvmod_directors
- * make generation of the offset-pointer p optional
- */
-
-#include "parse_encoding.h"
-#define term(c) ((c) == '\0')
-
-enum encoding
-parse_encoding (const char *m) {
- enum encoding r;
-
- switch (m[0]) {
- case 'B': goto _0B; // BASE64, BASE64URL, BASE64URLNOPAD
- case 'H': goto _0H; // HEX
- case 'I': goto _0I; // IDENTITY
- case 'U': goto _0U; // URL
- default: goto invalid;
- }
- _0B:
- switch (m[1]) {
- case 'A': goto _1BA; // BASE64, BASE64URL, BASE64URLNOPAD
- default: goto invalid;
- }
- _1BA:
- switch (m[2]) {
- case 'S': goto _2BAS; // BASE64, BASE64URL, BASE64URLNOPAD
- default: goto invalid;
- }
- _2BAS:
- switch (m[3]) {
- case 'E': goto _3BASE; // BASE64, BASE64URL, BASE64URLNOPAD
- default: goto invalid;
- }
- _3BASE:
- switch (m[4]) {
- case '6': goto _4BASE6; // BASE64, BASE64URL, BASE64URLNOPAD
- default: goto invalid;
- }
- _4BASE6:
- switch (m[5]) {
- case '4': goto _5BASE64; // BASE64, BASE64URL, BASE64URLNOPAD
- default: goto invalid;
- }
- _5BASE64:
- //BASE64
- if (term(m[6])) {
- r = BASE64;
- goto ok;
- }
- switch (m[6]) {
- case 'U': goto _6BASE64U; // BASE64URL, BASE64URLNOPAD
- default: goto invalid;
- }
- _6BASE64U:
- switch (m[7]) {
- case 'R': goto _7BASE64UR; // BASE64URL, BASE64URLNOPAD
- default: goto invalid;
- }
- _7BASE64UR:
- switch (m[8]) {
- case 'L': goto _8BASE64URL; // BASE64URL, BASE64URLNOPAD
- default: goto invalid;
- }
- _8BASE64URL:
- //BASE64URL
- if (term(m[9])) {
- r = BASE64URL;
- goto ok;
- }
- switch (m[9]) {
- case 'N': goto _9BASE64URLN; // BASE64URLNOPAD
- default: goto invalid;
- }
- _9BASE64URLN:
- //BASE64URLNOPAD
- if ((m[10] == 'O') && (m[11] == 'P') && (m[12] == 'A') && (m[13] == 'D') && (term(m[14]))) {
- r = BASE64URLNOPAD;
- goto ok;
- }
- goto invalid;
- _0H:
- //HEX
- if ((m[1] == 'E') && (m[2] == 'X') && (term(m[3]))) {
- r = HEX;
- goto ok;
- }
- goto invalid;
- _0I:
- //IDENTITY
- if ((m[1] == 'D') && (m[2] == 'E') && (m[3] == 'N') && (m[4] == 'T') && (m[5] == 'I') && (m[6] == 'T') && (m[7] == 'Y') && (term(m[8]))) {
- r = IDENTITY;
- goto ok;
- }
- goto invalid;
- _0U:
- //URL
- if ((m[1] == 'R') && (m[2] == 'L') && (term(m[3]))) {
- r = URL;
- goto ok;
- }
- goto invalid;
- ok:
- return r;
- invalid:
- return _INVALID;
-}
diff --git a/lib/libvmod_blob/parse_encoding.h b/lib/libvmod_blob/parse_encoding.h
deleted file mode 100644
index 472ebf8..0000000
--- a/lib/libvmod_blob/parse_encoding.h
+++ /dev/null
@@ -1,22 +0,0 @@
-/*
- * for the time being, this code is auto-generated outside the varnishd source
- * tree, see
- * https://code.uplex.de/uplex-varnish/libvmod-blobcode/blob/master/src/gen_enum_parse.pl
- *
- * TODO: integrate in vmodtool.py or replace with something else
- * cf. the same TODO for the shard director in libvmod_directors
- */
-
-enum encoding {
- _INVALID = 0,
- IDENTITY,
- BASE64,
- BASE64URL,
- BASE64URLNOPAD,
- HEX,
- URL,
- __MAX_ENCODING
-};
-
-enum encoding parse_encoding (const char *);
-
diff --git a/lib/libvmod_blob/vmod_blob.c b/lib/libvmod_blob/vmod_blob.c
index 927af20..72ec60f 100644
--- a/lib/libvmod_blob/vmod_blob.c
+++ b/lib/libvmod_blob/vmod_blob.c
@@ -55,13 +55,6 @@ static const struct vmod_blob_fptr {
len_f *const encode_l;
encode_f *const encode;
} func[__MAX_ENCODING] = {
- [_INVALID] = {
- /* make implicit null init explicit for clarity */
- .decode_l = NULL,
- .decode = NULL,
- .encode_l = NULL,
- .encode = NULL
- },
[IDENTITY] = {
.decode_l = id_decode_l,
.decode = id_decode,
@@ -123,6 +116,19 @@ static const struct vmod_priv null_blob[1] =
}
};
+#include <stdio.h>
+static enum encoding
+parse_encoding(VCL_ENUM e)
+{
+ if (e == vmod_enum_BASE64) return(BASE64);
+ if (e == vmod_enum_BASE64URL) return(BASE64URL);
+ if (e == vmod_enum_BASE64URLNOPAD) return(BASE64URLNOPAD);
+ if (e == vmod_enum_HEX) return(HEX);
+ if (e == vmod_enum_IDENTITY) return(IDENTITY);
+ if (e == vmod_enum_URL) return(URL);
+ WRONG("illegal encoding enum");
+}
+
static inline size_t
decode_l_va(enum encoding dec, const char * const p, va_list ap)
{
@@ -156,19 +162,10 @@ err_decode(VRT_CTX, const char *enc)
static inline enum case_e
parse_case(VCL_ENUM case_s)
{
- switch (*case_s) {
- case 'D':
- AZ(strcmp(case_s + 1, "EFAULT"));
- return DEFAULT;
- case 'L':
- AZ(strcmp(case_s + 1, "OWER"));
- return LOWER;
- case 'U':
- AZ(strcmp(case_s + 1, "PPER"));
- return UPPER;
- default:
- WRONG("illegal case enum");
- }
+ if (case_s == vmod_enum_DEFAULT) return(DEFAULT);
+ if (case_s == vmod_enum_LOWER) return(LOWER);
+ if (case_s == vmod_enum_UPPER) return(UPPER);
+ WRONG("illegal case enum");
}
static inline int
diff --git a/lib/libvmod_blob/vmod_blob.h b/lib/libvmod_blob/vmod_blob.h
index 04663e5..ba359c1 100644
--- a/lib/libvmod_blob/vmod_blob.h
+++ b/lib/libvmod_blob/vmod_blob.h
@@ -30,9 +30,17 @@
#include <stdarg.h>
#include <sys/types.h>
-#include "parse_encoding.h"
+enum encoding {
+ IDENTITY = 1,
+ BASE64,
+ BASE64URL,
+ BASE64URLNOPAD,
+ HEX,
+ URL,
+ __MAX_ENCODING
+};
-#define AENC(enc) assert((enc) > _INVALID && (enc) < __MAX_ENCODING)
+#define AENC(enc) assert((enc) >= IDENTITY && (enc) < __MAX_ENCODING)
/*
* The enums MUST appear in this order, since LOWER and UPPER are used to
More information about the varnish-commit
mailing list