From 2bc1d16b56f562c70fda6a3762bc8130951202a2 Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Sun, 29 Nov 2020 18:55:20 -0500 Subject: Simplify base64 table --- decode.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'decode.c') 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; -- cgit 1.4.1