From 4e67a6216bf3633320950117aba3566cbfd5ffda Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Mon, 25 Jul 2011 15:13:55 -0700 Subject: [PATCH] Faster Base64 decoder. --- src/bitcoinrpc.cpp | 18 -------------- src/util.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 1 + 3 files changed, 60 insertions(+), 18 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 5a1fab694..db2e610fa 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -1830,24 +1830,6 @@ string EncodeBase64(string s) return result; } -string DecodeBase64(string s) -{ - BIO *b64, *bmem; - - char* buffer = static_cast(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(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& mapHeaders) { string strAuth = mapHeaders["authorization"]; diff --git a/src/util.cpp b/src/util.cpp index a5e3d3099..5b6d26bb8 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -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>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) diff --git a/src/util.h b/src/util.h index 33013a2f8..92a2b839c 100644 --- a/src/util.h +++ b/src/util.h @@ -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);