Skip to content

Commit

Permalink
make non-strict match more forgiving (closes #100)
Browse files Browse the repository at this point in the history
  • Loading branch information
namazso committed Apr 18, 2022
1 parent 3170ed7 commit 8de2854
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions OpenHashTab/utl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ extern "C" NTSTATUS NTAPI RtlLoadString(
std::vector<uint8_t> utl::FindHashInString(std::wstring_view wv)
{
using wvmatch = std::match_results<std::wstring_view::iterator>;
constexpr static wchar_t regex_str[] = LR"([^\u0080-\uFFFFa-zA-Z0-9]*((?:[0-9a-fA-F]{2})+)([^\u0080-\uFFFFa-zA-Z0-9].*)?)";
constexpr static wchar_t regex_str[] = LR"(((?:[0-9A-F]{2} ?){2,}|(?:[0-9a-f]{2} ?){2,}))";
static std::wregex regex{ regex_str };

wvmatch pieces;
if (std::regex_match(begin(wv), end(wv), pieces, regex))
if (std::regex_search(begin(wv), end(wv), pieces, regex))
return HashStringToBytes(std::wstring_view{ pieces[1].str() });
return {};
}
Expand Down
15 changes: 9 additions & 6 deletions OpenHashTab/utl.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,18 @@ namespace utl
template <typename Char>
std::vector<uint8_t> HashStringToBytes(std::basic_string_view<Char> str)
{
if (str.size() % 2 != 0)
return {}; // odd

std::vector<uint8_t> res;

for (size_t i = 0u; i < str.size() / 2; ++i)
for (size_t i = 0u; i < str.size() - 1; i += 2)
{
const auto a = utl::unhex<Char>(str[i * 2]);
const auto b = utl::unhex<Char>(str[i * 2 + 1]);
while (str[i] == ' ')
{
i++;
if (!(i < str.size() - 1))
break;
}
const auto a = utl::unhex<Char>(str[i]);
const auto b = utl::unhex<Char>(str[i + 1]);
if (a == 0xFF || b == 0xFF)
return {}; // invalid
res.push_back(a << 4 | b);
Expand Down

0 comments on commit 8de2854

Please sign in to comment.