From 4b9efc825a1357d37e84ddc69a0771c80c56e649 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sun, 3 Nov 2024 11:10:31 +0100 Subject: [PATCH] Extract container utils --- src/common/utils/container.hpp | 25 ++++++ src/windows-emulator/registry/hive_parser.hpp | 79 +------------------ 2 files changed, 27 insertions(+), 77 deletions(-) create mode 100644 src/common/utils/container.hpp diff --git a/src/common/utils/container.hpp b/src/common/utils/container.hpp new file mode 100644 index 0000000..eeafdf3 --- /dev/null +++ b/src/common/utils/container.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include +#include +#include +#include + +namespace utils +{ + struct string_hash + { + using is_transparent = void; + + size_t operator()(const std::string_view str) const + { + constexpr std::hash hasher{}; + return hasher(str); + } + }; + + template + using unordered_string_map = std::unordered_map>; + + using unordered_string_set = std::unordered_set>; +} diff --git a/src/windows-emulator/registry/hive_parser.hpp b/src/windows-emulator/registry/hive_parser.hpp index a2e8ece..3a2e032 100644 --- a/src/windows-emulator/registry/hive_parser.hpp +++ b/src/windows-emulator/registry/hive_parser.hpp @@ -6,9 +6,7 @@ #include #include #include -#include -#include -#include +#include // Based on this implementation: https://github.com/reahly/windows-hive-parser @@ -63,20 +61,6 @@ namespace detail return {std::istreambuf_iterator(file), std::istreambuf_iterator()}; } - - struct string_hash - { - using is_transparent = void; - - size_t operator()(const std::string_view str) const - { - constexpr std::hash hasher{}; - return hasher(str); - } - }; - - template - using unordered_string_map = std::unordered_map>; } class hive_key_t @@ -151,65 +135,6 @@ class hive_key_t return std::nullopt; } - - template - std::optional get_key_value(const std::string_view& name) - { - const auto value = this->get_key_value(name); - if (!value) - { - return std::nullopt; - } - - const auto [type, data] = *value; - - if constexpr (std::is_same_v) - { - if (type != REG_SZ && type != REG_EXPAND_SZ) - return std::nullopt; - - return data; - } - else if constexpr (std::is_same_v>) - { - if (type != REG_MULTI_SZ) - return std::nullopt; - - std::string_view text; - std::vector out; - for (auto j = 0; j < data.size(); j++) - { - if (data[j] == '\0' && data[j + 1] == '\0' && data[j + 2] == '\0') - { - if (!text.empty()) - out.emplace_back(text); - text = {}; - } - else - { - text = std::string_view(data.data() + j - text.size(), text.size() + 1); - } - } - - return out; - } - else if constexpr (std::is_same_v) - { - if (type != REG_DWORD) - return std::nullopt; - - return *reinterpret_cast(data); - } - else if constexpr (std::is_same_v>) - { - if (type != REG_BINARY) - return std::nullopt; - - return {reinterpret_cast(data.data()), data.size()}; - } - - return std::nullopt; - } }; class hive_parser @@ -229,7 +154,7 @@ class hive_parser key_block_t* main_key_block_data; uintptr_t main_root; std::vector file_data; - detail::unordered_string_map subkey_cache; + utils::unordered_string_map subkey_cache; void reclusive_search(const key_block_t* key_block_data, const std::string& current_path, const bool is_reclusive = false)