Skip to content

Commit 1859ad1

Browse files
committed
base64 decoding now uses a lookup table, not repeated branching
1 parent f2d0311 commit 1859ad1

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

src/6model/base64.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,7 @@ char* base64_encode(const void* buf, size_t size)
5656

5757

5858
// single base64 character conversion
59-
//
60-
static int POS(char c)
61-
{
62-
if (c>='A' && c<='Z') return c - 'A';
63-
if (c>='a' && c<='z') return c - 'a' + 26;
64-
if (c>='0' && c<='9') return c - '0' + 52;
65-
if (c == '+') return 62;
66-
if (c == '/') return 63;
67-
if (c == '=') return -1;
68-
return -2;
69-
}
59+
static const int POS[256] = {-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -1, -2, -2, -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2, -2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2};
7060

7161
// base64 decoding
7262
//
@@ -88,10 +78,10 @@ void* base64_decode(char* s, size_t *data_len)
8878
q = (unsigned char*) data;
8979

9080
for (p = s; *p; ) {
91-
n[0] = POS(*p++);
92-
n[1] = POS(*p++);
93-
n[2] = POS(*p++);
94-
n[3] = POS(*p++);
81+
n[0] = POS[*p++];
82+
n[1] = POS[*p++];
83+
n[2] = POS[*p++];
84+
n[3] = POS[*p++];
9585

9686
if (n[0] == -2 || n[1] == -2 || n[2] == -2 || n[3] == -2)
9787
return NULL;

0 commit comments

Comments
 (0)