From d710fb44035a4db469fad751f03724b498d4852a Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sun, 3 Nov 2024 09:48:32 +0100 Subject: [PATCH] Fix serialization --- src/emulator/serialization.hpp | 12 ++++----- src/emulator/serialization_helper.hpp | 30 +++++++++++++++++++++++ src/windows-emulator/process_context.hpp | 15 +----------- src/windows-emulator/registry_manager.hpp | 10 ++++---- 4 files changed, 42 insertions(+), 25 deletions(-) create mode 100644 src/emulator/serialization_helper.hpp diff --git a/src/emulator/serialization.hpp b/src/emulator/serialization.hpp index 0cb844cb..342735aa 100644 --- a/src/emulator/serialization.hpp +++ b/src/emulator/serialization.hpp @@ -38,8 +38,8 @@ namespace utils }; template - struct has_serialize_function(), - std::declval()))>> + struct has_serialize_function(), + std::declval&>()))>> : std::true_type { }; @@ -50,8 +50,8 @@ namespace utils }; template - struct has_deserialize_function(), std::declval()))>> + struct has_deserialize_function(), std::declval&>()))>> : std::true_type { }; @@ -122,7 +122,7 @@ namespace utils } else if constexpr (detail::has_deserialize_function::value) { - deserialize(*this, object); + ::deserialize(*this, object); } else if constexpr (std::is_trivially_copyable_v) { @@ -338,7 +338,7 @@ namespace utils } else if constexpr (detail::has_serialize_function::value) { - serialize(*this, object); + ::serialize(*this, object); } else if constexpr (std::is_trivially_copyable_v) { diff --git a/src/emulator/serialization_helper.hpp b/src/emulator/serialization_helper.hpp new file mode 100644 index 00000000..fbf769cd --- /dev/null +++ b/src/emulator/serialization_helper.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include "serialization.hpp" + +#include +#include + +inline void serialize(utils::buffer_serializer& buffer, const std::chrono::steady_clock::time_point& tp) +{ + buffer.write(tp.time_since_epoch().count()); +} + +inline void deserialize(utils::buffer_deserializer& buffer, std::chrono::steady_clock::time_point& tp) +{ + using time_point = std::chrono::steady_clock::time_point; + using duration = time_point::duration; + + const auto count = buffer.read(); + tp = time_point{duration{count}}; +} + +inline void serialize(utils::buffer_serializer& buffer, const std::filesystem::path& path) +{ + buffer.write_string(path.wstring()); +} + +inline void deserialize(utils::buffer_deserializer& buffer, std::filesystem::path& path) +{ + path = buffer.read_string(); +} diff --git a/src/windows-emulator/process_context.hpp b/src/windows-emulator/process_context.hpp index 3caf52ce..46ec0318 100644 --- a/src/windows-emulator/process_context.hpp +++ b/src/windows-emulator/process_context.hpp @@ -9,6 +9,7 @@ #include #include +#include #define PEB_SEGMENT_SIZE (20 << 20) // 20 MB @@ -24,20 +25,6 @@ #define GDT_LIMIT 0x1000 #define GDT_ENTRY_SIZE 0x8 -inline void serialize(utils::buffer_serializer& buffer, const std::chrono::steady_clock::time_point& tp) -{ - buffer.write(tp.time_since_epoch().count()); -} - -inline void deserialize(utils::buffer_deserializer& buffer, std::chrono::steady_clock::time_point& tp) -{ - using time_point = std::chrono::steady_clock::time_point; - using duration = time_point::duration; - - const auto count = buffer.read(); - tp = time_point{duration{count}}; -} - struct ref_counted_object { uint32_t ref_count{1}; diff --git a/src/windows-emulator/registry_manager.hpp b/src/windows-emulator/registry_manager.hpp index d8fb7ce9..bbdeabd8 100644 --- a/src/windows-emulator/registry_manager.hpp +++ b/src/windows-emulator/registry_manager.hpp @@ -1,7 +1,7 @@ #pragma once #include "std_include.hpp" -#include +#include class hive_parser; @@ -12,14 +12,14 @@ struct registry_key void serialize(utils::buffer_serializer& buffer) const { - buffer.write_string(this->hive.wstring()); - buffer.write_string(this->path.wstring()); + buffer.write(this->hive); + buffer.write(this->path); } void deserialize(utils::buffer_deserializer& buffer) { - this->hive = buffer.read_string(); - this->path = buffer.read_string(); + buffer.read(this->hive); + buffer.read(this->path); } };