Faster Base64 decoder.
authorJoelKatz <DavidJoelSchwartz@GMail.com>
Mon, 25 Jul 2011 22:13:55 +0000 (15:13 -0700)
committerPieter Wuille <pieter.wuille@gmail.com>
Tue, 27 Sep 2011 17:47:34 +0000 (19:47 +0200)
src/bitcoinrpc.cpp
src/util.cpp
src/util.h

index 5a1fab6..db2e610 100644 (file)
@@ -1830,24 +1830,6 @@ string EncodeBase64(string s)
     return result;
 }
 
-string DecodeBase64(string s)
-{
-    BIO *b64, *bmem;
-
-    char* buffer = static_cast<char*>(calloc(s.size(), sizeof(char)));
-
-    b64 = BIO_new(BIO_f_base64());
-    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
-    bmem = BIO_new_mem_buf(const_cast<char*>(s.c_str()), s.size());
-    bmem = BIO_push(b64, bmem);
-    BIO_read(bmem, buffer, s.size());
-    BIO_free_all(bmem);
-
-    string result(buffer);
-    free(buffer);
-    return result;
-}
-
 bool HTTPAuthorized(map<string, string>& mapHeaders)
 {
     string strAuth = mapHeaders["authorization"];
index a5e3d30..5b6d26b 100644 (file)
@@ -470,6 +470,65 @@ void ParseParameters(int argc, char* argv[])
     }
 }
 
+static const int decode64_table[256]=
+{
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
+ -1, -1, -1, -1, -1, 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, -1, -1, -1, -1, -1, -1, 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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
+};
+
+std::string DecodeBase64(const std::string &s)
+{
+    char buf[1024];
+    if(s.length()>512) return "";
+    char *optr=buf;
+
+    int dec, mode=0, left=0;
+    size_t index=0;
+    for (int i=0; i<s.length(); i++)
+    {
+         dec=decode64_table[s[i]];
+         if(dec==-1) break;
+         switch(mode)
+         {
+             case 0: // we have no bits and get 6
+                 left = dec;
+                 mode = 1;
+                 break;
+
+              case 1: // we have 6 bits and keep 4
+                 *optr++ = (left<<2) | (dec>>4);
+                  left = dec & 15;
+                  mode = 2;
+                  break;
+                  
+             case 2: // we have 4 bits and get 6, we keep 2            
+                  *optr++ = (left<<4) | (dec>>2);
+                 left = dec & 3;
+                 mode = 3;
+                 break;
+                 
+             case 3: // we have 2 bits and get 6
+                 *optr++ = (left<<6) | dec;
+                 mode=0;
+                 break;
+         }
+    }
+
+    *optr=0;   
+    return buf;
+}
+
 
 
 bool WildcardMatch(const char* psz, const char* mask)
index 33013a2..92a2b83 100644 (file)
@@ -201,6 +201,7 @@ void SetMockTime(int64 nMockTimeIn);
 int64 GetAdjustedTime();
 void AddTimeData(unsigned int ip, int64 nTime);
 std::string FormatFullVersion();
+std::string DecodeBase64(const std::string &s);