Skip to content

Commit

Permalink
switch to CTRE (fixes #128)
Browse files Browse the repository at this point in the history
  • Loading branch information
namazso committed Jul 2, 2022
1 parent 498459a commit d55f174
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 33 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@
[submodule "Algorithms/deps/QuickXorHash"]
path = Algorithms/deps/QuickXorHash
url = https://github.com/namazso/QuickXorHash.git
[submodule "compile-time-regular-expressions"]
path = compile-time-regular-expressions
url = https://github.com/hanickadot/compile-time-regular-expressions.git
4 changes: 2 additions & 2 deletions OpenHashTab/OpenHashTab.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<IgnoreImportLibrary>true</IgnoreImportLibrary>
<IncludePath>$(SolutionDir)concurrentqueue;$(SolutionDir)phnt;$(SolutionDir)tiny-json;$(IncludePath)</IncludePath>
<IncludePath>$(SolutionDir)concurrentqueue;$(SolutionDir)phnt;$(SolutionDir)tiny-json;$(SolutionDir)compile-time-regular-expressions\include;$(IncludePath)</IncludePath>
<OutDir>$(SolutionDir)bin\$(Configuration)\$(Platform)\</OutDir>
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<PreprocessorDefinitions>CI_VERSION="$(CI_VERSION)";CI_VERSION_MAJOR=$(CI_VERSION_MAJOR);CI_VERSION_MINOR=$(CI_VERSION_MINOR);CI_VERSION_PATCH=$(CI_VERSION_PATCH);%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
<LanguageStandard>stdcpp17</LanguageStandard>
<LanguageStandard>stdcpp20</LanguageStandard>
<PrecompiledHeader>Use</PrecompiledHeader>
</ClCompile>
<Midl>
Expand Down
35 changes: 12 additions & 23 deletions OpenHashTab/SumFileParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,19 @@

#include "SumFileParser.h"

#include <regex>
#include <ctre-unicode.hpp>
#include <string_view>

#include "base64.h"
#include "utl.h"
#include "../AlgorithmsLoader/Hasher.h"

constexpr char k_regex_hex[] = R"(([0-9a-fA-F]{8,512}) [ \*](.+))";
constexpr char k_regex_b64[] = R"(([0-9a-zA-Z=+\/,\-_]{6,512}) [ \*](.+))";
constexpr char k_regex_sfv[] = R"((.+)\s+([0-9a-fA-F]{8}))";
auto k_regex_hex = ctre::match<R"(([0-9a-fA-F]{8,512}) [ \*](.+))">;
auto k_regex_b64 = ctre::match<R"(([0-9a-zA-Z=+\/,\-_]{6,512}) [ \*](.+))">;
auto k_regex_sfv = ctre::match<R"((.+)\s+([0-9a-fA-F]{8}))">;

class SumFileParser2
{
std::regex _hex{ k_regex_hex };
std::regex _b64{ k_regex_b64 };
std::regex _sfv{ k_regex_sfv };

enum class CommentStyle
{
Unknown,
Expand All @@ -54,8 +50,6 @@ class SumFileParser2

bool ProcessLine(std::string_view sv)
{
using svmatch = std::match_results<std::string_view::iterator>;

if (sv.find_first_not_of("\r\n\t\f\v ") == std::string_view::npos)
return true; // empty line

Expand All @@ -73,15 +67,13 @@ class SumFileParser2

if ((_hash == HashStyle::Unknown || _hash == HashStyle::Sfv))
{
svmatch pieces;
if(std::regex_match(begin(sv), end(sv), pieces, _sfv))
if(auto [whole, file_match, hash_str] = k_regex_sfv(sv); whole)
{
_hash = HashStyle::Sfv;
auto file = pieces[1].str();
// According to wikipedia delimiter is always space.
auto file = std::string(file_match);
const auto notspace = file.find_last_not_of(' ');
file.resize(notspace == std::string_view::npos ? 0 : notspace + 1);
const auto hash_str = pieces[2].str();
auto hash = utl::HashStringToBytes(std::string_view{ hash_str });
if (!hash.empty())
{
Expand All @@ -93,31 +85,28 @@ class SumFileParser2

if ((_hash == HashStyle::Unknown || _hash == HashStyle::Hex))
{
svmatch pieces;
if (std::regex_match(begin(sv), end(sv), pieces, _hex))
if (auto [whole, hash_str, file] = k_regex_hex(sv); whole)
{
_hash = HashStyle::Hex;
const auto hash_str = pieces[1].str();
auto hash = utl::HashStringToBytes(std::string_view{ hash_str });
if (!hash.empty())
{
files.emplace_back(pieces[2], std::move(hash));
files.emplace_back(file, std::move(hash));
return true;
}
}
}

if ((_hash == HashStyle::Unknown || _hash == HashStyle::Base64))
{
svmatch pieces;
if (std::regex_match(begin(sv), end(sv), pieces, _b64))
if (auto [whole, hash_match, file] = k_regex_hex(sv); whole)
{
_hash = HashStyle::Base64;
const auto str = pieces[1].str();
auto hash = b64::decode(str.c_str(), str.size());
const auto hash_str = std::string(hash_match);
auto hash = b64::decode(hash_str.c_str(), hash_str.size());
if (!hash.empty())
{
files.emplace_back(pieces[2], std::move(hash));
files.emplace_back(file, std::move(hash));
return true;
}
}
Expand Down
13 changes: 5 additions & 8 deletions OpenHashTab/utl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "utl.h"

#include <memory>
#include <regex>
#include <ctre-unicode.hpp>

#include "Settings.h"

Expand All @@ -35,13 +35,10 @@ 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"(((?:[0-9A-F]{2} ?){4,}|(?:[0-9a-f]{2} ?){4,}))";
static std::wregex regex{ regex_str };

wvmatch pieces;
if (std::regex_search(begin(wv), end(wv), pieces, regex))
return HashStringToBytes(std::wstring_view{ pieces[1].str() });
static auto regex = ctre::match<LR"(((?:[0-9A-F]{2} ?){4,}|(?:[0-9a-f]{2} ?){4,}))">;

if (auto [whole, hash] = regex(wv); whole)
return HashStringToBytes(std::wstring_view{ std::wstring(hash)});
return {};
}

Expand Down
1 change: 1 addition & 0 deletions compile-time-regular-expressions

0 comments on commit d55f174

Please sign in to comment.