Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

base64 decoding now uses a lookup table, not repeated branching

  • Loading branch information...
commit 1859ad17ef24a1c43d98ee847d42cce5d7284ae8 1 parent f2d0311
Moritz Lenz moritz authored
Showing with 5 additions and 15 deletions.
  1. +5 −15 src/6model/base64.c
20 src/6model/base64.c
View
@@ -56,17 +56,7 @@ char* base64_encode(const void* buf, size_t size)
// single base64 character conversion
-//
-static int POS(char c)
-{
- if (c>='A' && c<='Z') return c - 'A';
- if (c>='a' && c<='z') return c - 'a' + 26;
- if (c>='0' && c<='9') return c - '0' + 52;
- if (c == '+') return 62;
- if (c == '/') return 63;
- if (c == '=') return -1;
- return -2;
-}
+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};
// base64 decoding
//
@@ -88,10 +78,10 @@ void* base64_decode(char* s, size_t *data_len)
q = (unsigned char*) data;
for (p = s; *p; ) {
- n[0] = POS(*p++);
- n[1] = POS(*p++);
- n[2] = POS(*p++);
- n[3] = POS(*p++);
+ n[0] = POS[*p++];
+ n[1] = POS[*p++];
+ n[2] = POS[*p++];
+ n[3] = POS[*p++];
if (n[0] == -2 || n[1] == -2 || n[2] == -2 || n[3] == -2)
return NULL;
Please sign in to comment.
Something went wrong with that request. Please try again.