From 25cc02431b61097b2adfc2fbc493b648b0300c3b Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Mon, 10 Mar 2025 11:05:40 -0400 Subject: [SharedCache] Refactor Shared Cache In absence of a better name, this commit refactors the shared cache code. --- view/sharedcache/core/MappedFileAccessor.cpp | 101 +++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 view/sharedcache/core/MappedFileAccessor.cpp (limited to 'view/sharedcache/core/MappedFileAccessor.cpp') diff --git a/view/sharedcache/core/MappedFileAccessor.cpp b/view/sharedcache/core/MappedFileAccessor.cpp new file mode 100644 index 00000000..ddd6ce9b --- /dev/null +++ b/view/sharedcache/core/MappedFileAccessor.cpp @@ -0,0 +1,101 @@ +#include "MappedFileAccessor.h" + + +std::shared_ptr MappedFileAccessor::Open(const std::string& filePath) +{ + auto file = MappedFile::OpenFile(filePath); + if (!file.has_value()) + return nullptr; + if (file->Map() != MapStatus::Success) + return nullptr; + return std::make_shared(std::move(*file)); +} + +// TODO: Will obviously not work on 32bit binaries, need to make WritePointer64 and 32 equiv. +void MappedFileAccessor::WritePointer(size_t address, size_t pointer) +{ + m_dirty = true; + *reinterpret_cast(&m_file._mmap[address]) = pointer; +} + +std::string MappedFileAccessor::ReadNullTermString(size_t address, size_t maxLength) const +{ + if (address > Length()) + return ""; + auto start = m_file._mmap + address; + auto endLen = (maxLength > 0) ? maxLength : m_file.len; + auto end = start + endLen; + auto nul = std::find(start, end, 0); + return {start, nul}; +} + +uint8_t MappedFileAccessor::ReadUInt8(size_t address) +{ + return Read(address); +} + +int8_t MappedFileAccessor::ReadInt8(size_t address) +{ + return Read(address); +} + +uint16_t MappedFileAccessor::ReadUInt16(size_t address) +{ + return Read(address); +} + +int16_t MappedFileAccessor::ReadInt16(size_t address) +{ + return Read(address); +} + + +uint32_t MappedFileAccessor::ReadUInt32(size_t address) +{ + return Read(address); +} + +int32_t MappedFileAccessor::ReadInt32(size_t address) +{ + return Read(address); +} + +uint64_t MappedFileAccessor::ReadUInt64(size_t address) +{ + return Read(address); +} + +int64_t MappedFileAccessor::ReadInt64(size_t address) +{ + return Read(address); +} + +BinaryNinja::DataBuffer MappedFileAccessor::ReadBuffer(size_t addr, size_t length) +{ + if (addr + length > Length()) + throw UnmappedAccessException(addr + length, Length()); + return {&m_file._mmap[addr], length}; +} + +std::pair MappedFileAccessor::ReadSpan(size_t addr, size_t length) +{ + if (addr + length > Length()) + throw UnmappedAccessException(addr + length, Length()); + const uint8_t* data = &m_file._mmap[addr]; + return {data, data + length}; +} + +void MappedFileAccessor::Read(void* dest, size_t addr, size_t length) const +{ + if (addr + length > Length()) + throw UnmappedAccessException(addr + length, Length()); + memcpy(dest, &m_file._mmap[addr], length); +} + +template +T MappedFileAccessor::Read(size_t address) +{ + T result; + Read(&result, address, sizeof(T)); + return result; +} -- cgit v1.3.1