mirror of
https://github.com/momo5502/emulator.git
synced 2026-01-19 19:53:56 +00:00
Add memory reading/writing helpers
This commit is contained in:
@@ -33,7 +33,7 @@ public:
|
||||
virtual ~memory_manager() = default;
|
||||
|
||||
template <typename T>
|
||||
T read_memory(const uint64_t address)
|
||||
T read_memory(const uint64_t address) const
|
||||
{
|
||||
T value{};
|
||||
this->read_memory(address, &value, sizeof(value));
|
||||
@@ -41,17 +41,37 @@ public:
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T read_memory(const void* address)
|
||||
T read_memory(const void* address) const
|
||||
{
|
||||
return this->read_memory<T>(reinterpret_cast<uint64_t>(address));
|
||||
}
|
||||
|
||||
std::vector<std::byte> read_memory(const uint64_t address, const size_t size) const
|
||||
{
|
||||
std::vector<std::byte> data{};
|
||||
data.resize(size);
|
||||
|
||||
this->read_memory(address, data.data(), data.size());
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
std::vector<std::byte> read_memory(const void* address, const size_t size) const
|
||||
{
|
||||
return this->read_memory(reinterpret_cast<uint64_t>(address), size);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void write_memory(const uint64_t address, const T& value)
|
||||
{
|
||||
this->write_memory(address, &value, sizeof(value));
|
||||
}
|
||||
|
||||
void write_memory(void* address, const void* data, const size_t size)
|
||||
{
|
||||
this->write_memory(reinterpret_cast<uint64_t>(address), data, size);
|
||||
}
|
||||
|
||||
virtual void read_memory(uint64_t address, void* data, size_t size) const = 0;
|
||||
virtual bool try_read_memory(uint64_t address, void* data, size_t size) const = 0;
|
||||
virtual void write_memory(uint64_t address, const void* data, size_t size) = 0;
|
||||
|
||||
@@ -134,9 +134,7 @@ namespace
|
||||
|
||||
NTSTATUS ioctl_bind(windows_emulator& win_emu, const io_device_context& c) const
|
||||
{
|
||||
std::vector<std::byte> data{};
|
||||
data.resize(c.input_buffer_length);
|
||||
win_emu.emu().read_memory(c.input_buffer, data.data(), c.input_buffer_length);
|
||||
const auto data = win_emu.emu().read_memory(c.input_buffer, c.input_buffer_length);
|
||||
|
||||
constexpr auto address_offset = 4;
|
||||
|
||||
@@ -206,13 +204,13 @@ namespace
|
||||
return STATUS_UNSUCCESSFUL;
|
||||
}
|
||||
|
||||
emu.write_memory(reinterpret_cast<uint64_t>(buffer.buf), data.data(),
|
||||
std::min(data.size(), static_cast<size_t>(recevied_data)));
|
||||
const auto data_size = std::min(data.size(), static_cast<size_t>(recevied_data));
|
||||
emu.write_memory(buffer.buf, data.data(), data_size);
|
||||
|
||||
if (receive_info.Address && address_length)
|
||||
{
|
||||
emu.write_memory(reinterpret_cast<uint64_t>(receive_info.Address), address.data(),
|
||||
std::min(address.size(), static_cast<size_t>(address_length)));
|
||||
const auto address_size = std::min(address.size(), static_cast<size_t>(address_length));
|
||||
emu.write_memory(receive_info.Address, address.data(), address_size);
|
||||
}
|
||||
|
||||
if (c.io_status_block)
|
||||
@@ -237,17 +235,13 @@ namespace
|
||||
const auto send_info = emu.read_memory<AFD_SEND_DATAGRAM_INFO>(c.input_buffer);
|
||||
const auto buffer = emu.read_memory<WSABUF>(send_info.BufferArray);
|
||||
|
||||
std::vector<std::byte> address{};
|
||||
address.resize(send_info.TdiConnInfo.RemoteAddressLength);
|
||||
emu.read_memory(reinterpret_cast<uint64_t>(send_info.TdiConnInfo.RemoteAddress), address.data(),
|
||||
address.size());
|
||||
const auto address = emu.read_memory(send_info.TdiConnInfo.RemoteAddress,
|
||||
send_info.TdiConnInfo.RemoteAddressLength);
|
||||
|
||||
const network::address target(reinterpret_cast<sockaddr*>(address.data()),
|
||||
const network::address target(reinterpret_cast<const sockaddr*>(address.data()),
|
||||
static_cast<int>(address.size()));
|
||||
|
||||
std::vector<std::byte> data{};
|
||||
data.resize(buffer.len);
|
||||
emu.read_memory(reinterpret_cast<uint64_t>(buffer.buf), data.data(), data.size());
|
||||
const auto data = emu.read_memory(buffer.buf, buffer.len);
|
||||
|
||||
const auto sent_data = sendto(*this->s, reinterpret_cast<const char*>(data.data()),
|
||||
static_cast<int>(data.size()), 0 /* ? */, &target.get_addr(),
|
||||
|
||||
@@ -133,6 +133,15 @@ public:
|
||||
this->device_->deserialize(buffer);
|
||||
}
|
||||
|
||||
template <typename T = io_device>
|
||||
requires(std::is_base_of_v<io_device, T> || std::is_same_v<io_device, T>)
|
||||
T* get_internal_device()
|
||||
{
|
||||
this->assert_validity();
|
||||
auto* value = this->device_.get();
|
||||
return dynamic_cast<T*>(value);
|
||||
}
|
||||
|
||||
private:
|
||||
std::wstring device_name_{};
|
||||
std::unique_ptr<io_device> device_{};
|
||||
|
||||
Reference in New Issue
Block a user