mirror of
https://github.com/kvazar-network/kevacoin.git
synced 2025-01-18 19:10:11 +00:00
Inline base64 encoder/decoder
This replaces the openssl-based base64 encoder and decoder with a more efficient internal one. Tested against the rfc4648 test vectors. Decoder is based on JoelKatz' version.
This commit is contained in:
parent
4e67a6216b
commit
4b603f1cd6
@ -1811,25 +1811,6 @@ int ReadHTTP(std::basic_istream<char>& stream, map<string, string>& mapHeadersRe
|
|||||||
return nStatus;
|
return nStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
string EncodeBase64(string s)
|
|
||||||
{
|
|
||||||
BIO *b64, *bmem;
|
|
||||||
BUF_MEM *bptr;
|
|
||||||
|
|
||||||
b64 = BIO_new(BIO_f_base64());
|
|
||||||
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
|
|
||||||
bmem = BIO_new(BIO_s_mem());
|
|
||||||
b64 = BIO_push(b64, bmem);
|
|
||||||
BIO_write(b64, s.c_str(), s.size());
|
|
||||||
BIO_flush(b64);
|
|
||||||
BIO_get_mem_ptr(b64, &bptr);
|
|
||||||
|
|
||||||
string result(bptr->data, bptr->length);
|
|
||||||
BIO_free_all(b64);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool HTTPAuthorized(map<string, string>& mapHeaders)
|
bool HTTPAuthorized(map<string, string>& mapHeaders)
|
||||||
{
|
{
|
||||||
string strAuth = mapHeaders["authorization"];
|
string strAuth = mapHeaders["authorization"];
|
||||||
|
151
src/util.cpp
151
src/util.cpp
@ -443,7 +443,6 @@ vector<unsigned char> ParseHex(const string& str)
|
|||||||
return ParseHex(str.c_str());
|
return ParseHex(str.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ParseParameters(int argc, char* argv[])
|
void ParseParameters(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
mapArgs.clear();
|
mapArgs.clear();
|
||||||
@ -470,36 +469,91 @@ void ParseParameters(int argc, char* argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static const int decode64_table[256]=
|
string EncodeBase64(const unsigned char* pch, size_t len)
|
||||||
{
|
{
|
||||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
-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)
|
string strRet="";
|
||||||
{
|
strRet.reserve((len+2)/3*4);
|
||||||
char buf[1024];
|
|
||||||
if(s.length()>512) return "";
|
|
||||||
char *optr=buf;
|
|
||||||
|
|
||||||
int dec, mode=0, left=0;
|
int mode=0, left=0;
|
||||||
size_t index=0;
|
const unsigned char *pchEnd = pch+len;
|
||||||
for (int i=0; i<s.length(); i++)
|
|
||||||
|
while (pch<pchEnd)
|
||||||
{
|
{
|
||||||
dec=decode64_table[s[i]];
|
int enc = *(pch++);
|
||||||
if(dec==-1) break;
|
switch (mode)
|
||||||
switch(mode)
|
{
|
||||||
|
case 0: // we have no bits
|
||||||
|
strRet += pbase64[enc >> 2];
|
||||||
|
left = (enc & 3) << 4;
|
||||||
|
mode = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1: // we have two bits
|
||||||
|
strRet += pbase64[left | (enc >> 4)];
|
||||||
|
left = (enc & 15) << 2;
|
||||||
|
mode = 2;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2: // we have four bits
|
||||||
|
strRet += pbase64[left | (enc >> 6)];
|
||||||
|
strRet += pbase64[enc & 63];
|
||||||
|
mode = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mode)
|
||||||
|
{
|
||||||
|
strRet += pbase64[left];
|
||||||
|
strRet += '=';
|
||||||
|
if (mode == 1)
|
||||||
|
strRet += '=';
|
||||||
|
}
|
||||||
|
|
||||||
|
return strRet;
|
||||||
|
}
|
||||||
|
|
||||||
|
string EncodeBase64(const string& str)
|
||||||
|
{
|
||||||
|
return EncodeBase64((const unsigned char*)str.c_str(), str.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid)
|
||||||
|
{
|
||||||
|
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
|
||||||
|
};
|
||||||
|
|
||||||
|
if (pfInvalid)
|
||||||
|
*pfInvalid = false;
|
||||||
|
|
||||||
|
vector<unsigned char> vchRet;
|
||||||
|
vchRet.reserve(strlen(p)*3/4);
|
||||||
|
|
||||||
|
int mode = 0;
|
||||||
|
int left = 0;
|
||||||
|
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
int dec = decode64_table[*p];
|
||||||
|
if (dec == -1) break;
|
||||||
|
p++;
|
||||||
|
switch (mode)
|
||||||
{
|
{
|
||||||
case 0: // we have no bits and get 6
|
case 0: // we have no bits and get 6
|
||||||
left = dec;
|
left = dec;
|
||||||
@ -507,28 +561,53 @@ std::string DecodeBase64(const std::string &s)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 1: // we have 6 bits and keep 4
|
case 1: // we have 6 bits and keep 4
|
||||||
*optr++ = (left<<2) | (dec>>4);
|
vchRet.push_back((left<<2) | (dec>>4));
|
||||||
left = dec & 15;
|
left = dec & 15;
|
||||||
mode = 2;
|
mode = 2;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 2: // we have 4 bits and get 6, we keep 2
|
case 2: // we have 4 bits and get 6, we keep 2
|
||||||
*optr++ = (left<<4) | (dec>>2);
|
vchRet.push_back((left<<4) | (dec>>2));
|
||||||
left = dec & 3;
|
left = dec & 3;
|
||||||
mode = 3;
|
mode = 3;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 3: // we have 2 bits and get 6
|
case 3: // we have 2 bits and get 6
|
||||||
*optr++ = (left<<6) | dec;
|
vchRet.push_back((left<<6) | dec);
|
||||||
mode=0;
|
mode = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*optr=0;
|
if (pfInvalid)
|
||||||
return buf;
|
switch (mode)
|
||||||
|
{
|
||||||
|
case 0: // 4n base64 characters processed: ok
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1: // 4n+1 base64 character processed: impossible
|
||||||
|
*pfInvalid = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2: // 4n+2 base64 characters processed: require '=='
|
||||||
|
if (left || p[0] != '=' || p[1] != '=' || decode64_table[p[2]] != -1)
|
||||||
|
*pfInvalid = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3: // 4n+3 base64 characters processed: require '='
|
||||||
|
if (left || p[0] != '=' || decode64_table[p[1]] != -1)
|
||||||
|
*pfInvalid = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return vchRet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string DecodeBase64(const string& str)
|
||||||
|
{
|
||||||
|
vector<unsigned char> vchRet = DecodeBase64(str.c_str());
|
||||||
|
return string((const char*)&vchRet[0], vchRet.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool WildcardMatch(const char* psz, const char* mask)
|
bool WildcardMatch(const char* psz, const char* mask)
|
||||||
|
@ -178,6 +178,10 @@ bool ParseMoney(const std::string& str, int64& nRet);
|
|||||||
bool ParseMoney(const char* pszIn, int64& nRet);
|
bool ParseMoney(const char* pszIn, int64& nRet);
|
||||||
std::vector<unsigned char> ParseHex(const char* psz);
|
std::vector<unsigned char> ParseHex(const char* psz);
|
||||||
std::vector<unsigned char> ParseHex(const std::string& str);
|
std::vector<unsigned char> ParseHex(const std::string& str);
|
||||||
|
std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid = NULL);
|
||||||
|
std::string DecodeBase64(const std::string& str);
|
||||||
|
std::string EncodeBase64(const unsigned char* pch, size_t len);
|
||||||
|
std::string EncodeBase64(const std::string& str);
|
||||||
void ParseParameters(int argc, char* argv[]);
|
void ParseParameters(int argc, char* argv[]);
|
||||||
const char* wxGetTranslation(const char* psz);
|
const char* wxGetTranslation(const char* psz);
|
||||||
bool WildcardMatch(const char* psz, const char* mask);
|
bool WildcardMatch(const char* psz, const char* mask);
|
||||||
@ -201,7 +205,6 @@ void SetMockTime(int64 nMockTimeIn);
|
|||||||
int64 GetAdjustedTime();
|
int64 GetAdjustedTime();
|
||||||
void AddTimeData(unsigned int ip, int64 nTime);
|
void AddTimeData(unsigned int ip, int64 nTime);
|
||||||
std::string FormatFullVersion();
|
std::string FormatFullVersion();
|
||||||
std::string DecodeBase64(const std::string &s);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user