diff options
author | JoelKatz <DavidJoelSchwartz@GMail.com> | 2011-07-25 15:13:55 -0700 |
---|---|---|
committer | Pieter Wuille <pieter.wuille@gmail.com> | 2011-09-27 19:47:34 +0200 |
commit | 4e67a6216bf3633320950117aba3566cbfd5ffda (patch) | |
tree | e5739dd58720f6d65979f158bf51eb006ce77000 | |
parent | 9a1ce869690fe87fbce169a4b685695dc9e575f6 (diff) |
Faster Base64 decoder.
-rw-r--r-- | src/bitcoinrpc.cpp | 18 | ||||
-rw-r--r-- | src/util.cpp | 59 | ||||
-rw-r--r-- | src/util.h | 1 |
3 files changed, 60 insertions, 18 deletions
diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 5a1fab6943..db2e610fad 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<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"]; diff --git a/src/util.cpp b/src/util.cpp index a5e3d30995..5b6d26bb89 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<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) diff --git a/src/util.h b/src/util.h index 33013a2f81..92a2b839ca 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); |