aboutsummaryrefslogtreecommitdiff
path: root/contrib
diff options
context:
space:
mode:
authorHoward Chu <hyc@symas.com>2018-11-14 21:56:32 +0000
committermoneromooo-monero <moneromooo-monero@users.noreply.github.com>2018-11-15 23:34:15 +0000
commitf6187cd8111e5f7e812d7a153f8cd64ac2c04cb9 (patch)
tree4be995773df79afbc18fc9a3f43877863cd82fc2 /contrib
parentMerge pull request #4814 (diff)
downloadmonero-f6187cd8111e5f7e812d7a153f8cd64ac2c04cb9.tar.xz
epee: speed up parse_hexstr_to_binbuff a little
Diffstat (limited to 'contrib')
-rw-r--r--contrib/epee/include/string_tools.h50
1 files changed, 35 insertions, 15 deletions
diff --git a/contrib/epee/include/string_tools.h b/contrib/epee/include/string_tools.h
index aba065cc7..6a063cc36 100644
--- a/contrib/epee/include/string_tools.h
+++ b/contrib/epee/include/string_tools.h
@@ -59,6 +59,26 @@
#pragma comment (lib, "Rpcrt4.lib")
#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 string_tools
@@ -98,30 +118,30 @@ namespace string_tools
}
//----------------------------------------------------------------------------
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();
- if (!allow_partial_byte && (s.size() & 1))
+ if (s.size() & 1)
return false;
try
{
- long v = 0;
- for(size_t i = 0; i < (s.size() + 1) / 2; i++)
+ res.resize(s.size() / 2);
+ 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];
- size_t copied = s.copy(byte_str, 2, 2 * i);
- byte_str[copied] = CharT(0);
- CharT* endptr;
- v = strtoul(byte_str, &endptr, 16);
- if (v < 0 || 0xFF < v || endptr != byte_str + copied)
- {
- return false;
- }
- res.push_back(static_cast<unsigned char>(v));
+ int tmp = *src++;
+ tmp = isx[tmp];
+ if (tmp == 0xff) return false;
+ int t2 = *src++;
+ t2 = isx[t2];
+ if (t2 == 0xff) return false;
+ *dst++ = (tmp << 4) | t2;
}
return true;
- }catch(...)
+ }
+ catch(...)
{
return false;
}