[master] df8532818 Add BASE64CF codec

Nils Goroll nils.goroll at uplex.de
Thu Mar 25 11:01:04 UTC 2021


commit df85328189b2a0cd626e8c31ab2d20ac31e50d78
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Thu Mar 25 11:37:42 2021 +0100

    Add BASE64CF codec
    
    Each their own variant, *sigh*

diff --git a/vmod/tests/blob_b00004.vtc b/vmod/tests/blob_b00004.vtc
index 3d463ec0e..e735d583b 100644
--- a/vmod/tests/blob_b00004.vtc
+++ b/vmod/tests/blob_b00004.vtc
@@ -96,6 +96,14 @@ varnish v1 -vcl {
 	    set resp.http.b64urlnopadxcode =
 	      blob.transcode(IDENTITY, BASE64URLNOPAD,
 			     encoded=req.url + "Hello world" + req.url);
+
+	    set resp.http.b64cfxcodeout =
+	      blob.transcode(BASE64, BASE64CF,
+			     encoded="+/Y=");
+
+	    set resp.http.b64cfxcodein =
+	      blob.transcode(BASE64CF, BASE64,
+			     encoded="-~Y_");
 	}
 } -start
 
@@ -122,6 +130,8 @@ client c1 {
 	expect resp.http.b64xcode == "L0hlbGxvIHdvcmxkLw=="
 	expect resp.http.b64urlxcode == "L0hlbGxvIHdvcmxkLw=="
 	expect resp.http.b64urlnopadxcode == "L0hlbGxvIHdvcmxkLw"
+	expect resp.http.b64cfxcodeout == "-~Y_"
+	expect resp.http.b64cfxcodein == "+/Y="
 } -run
 
 varnish v1 -vcl {
diff --git a/vmod/vmod_blob.c b/vmod/vmod_blob.c
index 70644f5ad..3ebb97edf 100644
--- a/vmod/vmod_blob.c
+++ b/vmod/vmod_blob.c
@@ -76,6 +76,10 @@ static const struct vmod_blob_fptr {
 		B64_FUNCS,
 		.encode_l	= base64nopad_encode_l
 	},
+	[BASE64CF] = {
+		B64_FUNCS,
+		.encode_l	= base64_encode_l
+	},
 	[HEX] = {
 		.decode_l	= hex_decode_l,
 		.decode		= hex_decode,
diff --git a/vmod/vmod_blob.vcc b/vmod/vmod_blob.vcc
index 514f6028e..b3644c66c 100644
--- a/vmod/vmod_blob.vcc
+++ b/vmod/vmod_blob.vcc
@@ -70,6 +70,7 @@ ENUM values for an encoding scheme can be one of:
 * ``BASE64``
 * ``BASE64URL``
 * ``BASE64URLNOPAD``
+* ``BASE64CF``
 * ``HEX``
 * ``URL``
 
@@ -141,6 +142,11 @@ The ``BASE64URLNOPAD`` encoding uses the same alphabet as
 ``BASE6URL``, but leaves out the padding. Thus the length of an
 encoding with this scheme is not necessarily a multiple of four.
 
+The ``BASE64CF` is similar to ``BASE64URL``, with the following
+changes to ``BASE64``: ``+`` replaced with ``-``, ``/`` replaced with
+``~`` and ``_`` as the padding character. It is used by a certain CDN
+provider who also inspired the name.
+
 The *case* ENUM MUST be set to ``DEFAULT`` for for all of the
 ``BASE64*`` encodings.
 
@@ -176,8 +182,8 @@ The ``URL`` encoding implements "percent encoding" as per RFC3986. The
 affect alphabetic characters that are not percent-encoded.
 
 $Function BLOB decode(ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD,
-			    HEX, URL} decoding="IDENTITY", INT length=0,
-		      STRANDS encoded)
+			    BASE64CF, HEX, URL} decoding="IDENTITY",
+			    INT length=0, STRANDS encoded)
 
 Returns the BLOB derived from the string *encoded* according to the
 scheme specified by *decoding*.
@@ -201,7 +207,7 @@ Example::
 
 
 $Function STRING encode(ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD,
-			      HEX, URL} encoding="IDENTITY",
+			      BASE64CF, HEX, URL} encoding="IDENTITY",
 			ENUM {LOWER, UPPER, DEFAULT} case="DEFAULT", BLOB blob)
 
 Returns a string representation of the BLOB *blob* as specified by
@@ -230,9 +236,9 @@ Example::
 	    = blob.encode(blob=blob.decode(encoded="foo"));
 
 $Function STRING transcode(ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD,
-				 HEX, URL} decoding="IDENTITY",
+				 BASE64CF, HEX, URL} decoding="IDENTITY",
 			   ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD,
-				 HEX, URL} encoding="IDENTITY",
+				 BASE64CF, HEX, URL} encoding="IDENTITY",
 			   ENUM {LOWER, UPPER, DEFAULT} case="DEFAULT",
 			   INT length=0, STRANDS encoded)
 
@@ -303,8 +309,8 @@ default value of *offset* is ``0B``.
 ``offset + length`` requires more bytes than are available in the
 BLOB.
 
-$Object blob(ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD, HEX,
-		   URL} decoding="IDENTITY",
+$Object blob(ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD, BASE64CF,
+		   HEX, URL} decoding="IDENTITY",
 	     STRANDS encoded)
 
 Creates an object that contains the BLOB derived from the string
@@ -335,8 +341,8 @@ Example::
 	set resp.http.The-Stringblob =
 	    blob.encode(blob=stringblob.get());
 
-$Method STRING .encode(ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD, HEX,
-			     URL} encoding="IDENTITY",
+$Method STRING .encode(ENUM {IDENTITY, BASE64, BASE64URL, BASE64URLNOPAD,
+			     BASE64CF, HEX,URL} encoding="IDENTITY",
 		       ENUM {LOWER, UPPER, DEFAULT} case="DEFAULT")
 
 Returns an encoding of BLOB created by the constructor, according to
diff --git a/vmod/vmod_blob_base64.c b/vmod/vmod_blob_base64.c
index e275b963c..876f6e309 100644
--- a/vmod/vmod_blob_base64.c
+++ b/vmod/vmod_blob_base64.c
@@ -158,6 +158,45 @@ static const struct b64_alphabet {
 		},
 		0
 	},
+	[BASE64CF] = {
+		"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef"
+		"ghijklmnopqrstuvwxyz0123456789-~",
+		{
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL,  62, ILL, ILL, /* -          */
+			 52,  53,  54,  55,  56,  57,  58,  59, /* 0 - 7      */
+			 60,  61, ILL, ILL, ILL, ILL, ILL, ILL, /* 8, 9       */
+			ILL,   0,   1,   2,   3,   4,   5,   6, /* A - G      */
+			  7,   8,   9,  10,  11,  12,  13,  14, /* H - O      */
+			 15,  16,  17,  18,  19,  20,  21,  22, /* P - W      */
+			 23,  24,  25, ILL, ILL, ILL, ILL, PAD, /* X, Y, Z, _ */
+			ILL,  26,  27,  28,  29,  30,  31,  32, /* a - g      */
+			 33,  34,  35,  36,  37,  38,  39,  40, /* h - o      */
+			 41,  42,  43,  44,  45,  46,  47,  48, /* p - w      */
+			 49,  50,  51, ILL, ILL, ILL,  63, ILL, /* x, y, z,  ~*/
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+			ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL,
+		},
+		'_'
+	}
 };
 #define base64_l(l)		(((l) << 2) / 3)
 
diff --git a/vmod/vmod_blob_tbl_encodings.h b/vmod/vmod_blob_tbl_encodings.h
index 48765b27c..b61e211d6 100644
--- a/vmod/vmod_blob_tbl_encodings.h
+++ b/vmod/vmod_blob_tbl_encodings.h
@@ -6,6 +6,7 @@
 VMODENUM(BASE64)
 VMODENUM(BASE64URL)
 VMODENUM(BASE64URLNOPAD)
+VMODENUM(BASE64CF)
 VMODENUM(HEX)
 VMODENUM(IDENTITY)
 VMODENUM(URL)


More information about the varnish-commit mailing list