Skip to content

Commit

Permalink
Fix serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
momo5502 committed Nov 3, 2024
1 parent 30ea45d commit 898299d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
12 changes: 6 additions & 6 deletions src/emulator/serialization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ namespace utils
};

template <typename T>
struct has_serialize_function<T, std::void_t<decltype(serialize(std::declval<buffer_serializer&>(),
std::declval<const T&>()))>>
struct has_serialize_function<T, std::void_t<decltype(::serialize(std::declval<buffer_serializer&>(),
std::declval<const std::remove_cvref_t<T>&>()))>>
: std::true_type
{
};
Expand All @@ -50,8 +50,8 @@ namespace utils
};

template <typename T>
struct has_deserialize_function<T, std::void_t<decltype(deserialize(
std::declval<buffer_deserializer&>(), std::declval<T&>()))>>
struct has_deserialize_function<T, std::void_t<decltype(::deserialize(
std::declval<buffer_deserializer&>(), std::declval<std::remove_cvref_t<T>&>()))>>
: std::true_type
{
};
Expand Down Expand Up @@ -122,7 +122,7 @@ namespace utils
}
else if constexpr (detail::has_deserialize_function<T>::value)
{
deserialize(*this, object);
::deserialize(*this, object);
}
else if constexpr (std::is_trivially_copyable_v<T>)
{
Expand Down Expand Up @@ -338,7 +338,7 @@ namespace utils
}
else if constexpr (detail::has_serialize_function<T>::value)
{
serialize(*this, object);
::serialize(*this, object);
}
else if constexpr (std::is_trivially_copyable_v<T>)
{
Expand Down
30 changes: 30 additions & 0 deletions src/emulator/serialization_helper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include "serialization.hpp"

#include <chrono>
#include <filesystem>

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<duration::rep>();
tp = time_point{duration{count}};
}

inline void serialize(utils::buffer_serializer& buffer, const std::filesystem::path& path)
{
buffer.write_string<wchar_t>(path.wstring());
}

inline void deserialize(utils::buffer_deserializer& buffer, std::filesystem::path& path)
{
path = buffer.read_string<wchar_t>();
}
15 changes: 1 addition & 14 deletions src/windows-emulator/process_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <utils/file_handle.hpp>

#include <x64_emulator.hpp>
#include <serialization_helper.hpp>


#define PEB_SEGMENT_SIZE (20 << 20) // 20 MB
Expand All @@ -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<duration::rep>();
tp = time_point{duration{count}};
}

struct ref_counted_object
{
uint32_t ref_count{1};
Expand Down
10 changes: 5 additions & 5 deletions src/windows-emulator/registry_manager.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "std_include.hpp"
#include <serialization.hpp>
#include <serialization_helper.hpp>

class hive_parser;

Expand All @@ -12,14 +12,14 @@ struct registry_key

void serialize(utils::buffer_serializer& buffer) const
{
buffer.write_string<wchar_t>(this->hive.wstring());
buffer.write_string<wchar_t>(this->path.wstring());
buffer.write(this->hive);
buffer.write(this->path);
}

void deserialize(utils::buffer_deserializer& buffer)
{
this->hive = buffer.read_string<wchar_t>();
this->path = buffer.read_string<wchar_t>();
buffer.read(this->hive);
buffer.read(this->path);
}
};

Expand Down

0 comments on commit 898299d

Please sign in to comment.