summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-11-29 18:55:20 -0500
committerJune McEnroe <june@causal.agency>2020-11-29 18:55:20 -0500
commit2bc1d16b56f562c70fda6a3762bc8130951202a2 (patch)
tree08575e45a9882b6cffbe15a71959273ebf83e1d1
parentFix bufferDest for when len requires more than cap * 2 (diff)
downloadbubger-2bc1d16b56f562c70fda6a3762bc8130951202a2.tar.gz
bubger-2bc1d16b56f562c70fda6a3762bc8130951202a2.zip
Simplify base64 table
-rw-r--r--decode.c23
1 files changed, 10 insertions, 13 deletions
diff --git a/decode.c b/decode.c
index 5d875ca..d158c5d 100644
--- a/decode.c
+++ b/decode.c
@@ -97,30 +97,27 @@ static void convertCharset(
 	iconv_close(conv);
 }
 
-static const uint8_t Base64[64] = {
-	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
-};
-static uint8_t Table64[256];
-
-static uint8_t unbase64(char ch) {
-	if (!Table64[Base64[1]]) {
+static void decodeBase64(struct Buffer *dst, const char *src) {
+	static const uint8_t Base64[64] = {
+		"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
+	};
+	static uint8_t table[256];
+	if (!table[0]) {
+		memset(table, 0xFF, sizeof(table));
 		for (size_t i = 0; i < sizeof(Base64); ++i) {
-			Table64[Base64[i]] = i;
+			table[Base64[i]] = i;
 		}
 	}
-	return Table64[(uint8_t)ch];
-}
 
-static void decodeBase64(struct Buffer *dst, const char *src) {
 	while (src[0] && src[1] && src[2] && src[3]) {
-		if (!unbase64(src[0]) && src[0] != Base64[0]) {
+		if (table[(uint8_t)src[0]] == 0xFF) {
 			src++;
 			continue;
 		}
 		uint32_t bits = 0;
 		for (int i = 0; i < 4; ++i) {
 			bits <<= 6;
-			bits |= unbase64(src[i]);
+			bits |= table[(uint8_t)src[i]];
 		}
 		*bufferDest(dst, 1) = bits >> 16;
 		if (src[2] != '=') *bufferDest(dst, 1) = bits >> 8;