epee: speed up parse_hexstr_to_binbuff a little

This commit is contained in:
Howard Chu 2018-11-14 21:56:32 +00:00 committed by moneromooo-monero
parent 8534f71eed
commit f6187cd811
No known key found for this signature in database
GPG Key ID: 686F07454D6CEFC3
1 changed files with 35 additions and 15 deletions

View File

@ -59,6 +59,26 @@
#pragma comment (lib, "Rpcrt4.lib") #pragma comment (lib, "Rpcrt4.lib")
#endif #endif
static const constexpr unsigned char isx[256] =
{
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 10, 11, 12, 13, 14, 15, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 10, 11, 12, 13, 14, 15, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
};
namespace epee namespace epee
{ {
namespace string_tools namespace string_tools
@ -98,30 +118,30 @@ namespace string_tools
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
template<class CharT> template<class CharT>
bool parse_hexstr_to_binbuff(const std::basic_string<CharT>& s, std::basic_string<CharT>& res, bool allow_partial_byte = false) bool parse_hexstr_to_binbuff(const std::basic_string<CharT>& s, std::basic_string<CharT>& res)
{ {
res.clear(); res.clear();
if (!allow_partial_byte && (s.size() & 1)) if (s.size() & 1)
return false; return false;
try try
{ {
long v = 0; res.resize(s.size() / 2);
for(size_t i = 0; i < (s.size() + 1) / 2; i++) unsigned char *dst = (unsigned char *)res.data();
const unsigned char *src = (const unsigned char *)s.data();
for(size_t i = 0; i < s.size(); i += 2)
{ {
CharT byte_str[3]; int tmp = *src++;
size_t copied = s.copy(byte_str, 2, 2 * i); tmp = isx[tmp];
byte_str[copied] = CharT(0); if (tmp == 0xff) return false;
CharT* endptr; int t2 = *src++;
v = strtoul(byte_str, &endptr, 16); t2 = isx[t2];
if (v < 0 || 0xFF < v || endptr != byte_str + copied) if (t2 == 0xff) return false;
{ *dst++ = (tmp << 4) | t2;
return false;
}
res.push_back(static_cast<unsigned char>(v));
} }
return true; return true;
}catch(...) }
catch(...)
{ {
return false; return false;
} }