1
0
mirror of synced 2025-01-18 09:04:52 +01:00

fix: Inconsistent provider address reading

This commit is contained in:
WerWolv 2021-12-09 21:10:24 +01:00
parent 2ac6348fbf
commit 6a0ad22774
10 changed files with 47 additions and 56 deletions

View File

@ -44,11 +44,10 @@ namespace hex::plugin::builtin::prv {
void FileProvider::read(u64 offset, void *buffer, size_t size, bool overlays) {
if ((offset - this->getBaseAddress()) > (this->getSize() - size) || buffer == nullptr || size == 0)
if ((offset - this->getBaseAddress()) > (this->getActualSize() - size) || buffer == nullptr || size == 0)
return;
std::memcpy(buffer, reinterpret_cast<u8*>(this->m_mappedFile) + PageSize * this->m_currPage + offset - this->getBaseAddress(), size);
this->readRaw(offset - this->getBaseAddress(), buffer, size);
for (u64 i = 0; i < size; i++)
if (getPatches().contains(offset + i))
@ -59,25 +58,21 @@ namespace hex::plugin::builtin::prv {
}
void FileProvider::write(u64 offset, const void *buffer, size_t size) {
if (((offset - this->getBaseAddress()) + size) > this->getSize() || buffer == nullptr || size == 0)
if ((offset - this->getBaseAddress()) > (this->getActualSize() - size) || buffer == nullptr || size == 0)
return;
addPatch(offset, buffer, size);
}
void FileProvider::readRaw(u64 offset, void *buffer, size_t size) {
offset -= this->getBaseAddress();
if ((offset + size) > this->getSize() || buffer == nullptr || size == 0)
if ((offset + size) > this->getActualSize() || buffer == nullptr || size == 0)
return;
std::memcpy(buffer, reinterpret_cast<u8*>(this->m_mappedFile) + PageSize * this->m_currPage + offset, size);
}
void FileProvider::writeRaw(u64 offset, const void *buffer, size_t size) {
offset -= this->getBaseAddress();
if ((offset + size) > this->getSize() || buffer == nullptr || size == 0)
if ((offset + size) > this->getActualSize() || buffer == nullptr || size == 0)
return;
std::memcpy(reinterpret_cast<u8*>(this->m_mappedFile) + PageSize * this->m_currPage + offset, buffer, size);
@ -100,7 +95,7 @@ namespace hex::plugin::builtin::prv {
if (bufferSize > provider->getActualSize() - offset)
bufferSize = provider->getActualSize() - offset;
provider->readRelative(offset, buffer.data(), bufferSize);
provider->read(offset + this->getBaseAddress(), buffer.data(), bufferSize);
file.write(buffer);
}
}

View File

@ -130,9 +130,11 @@ namespace hex::plugin::builtin::prv {
void GDBProvider::read(u64 offset, void *buffer, size_t size, bool overlays) {
if ((offset - this->getBaseAddress()) > (this->getSize() - size) || buffer == nullptr || size == 0)
if ((offset - this->getBaseAddress()) > (this->getActualSize() - size) || buffer == nullptr || size == 0)
return;
offset -= this->getBaseAddress();
u64 alignedOffset = offset - (offset & 0xFFF);
{
@ -164,16 +166,11 @@ namespace hex::plugin::builtin::prv {
}
void GDBProvider::write(u64 offset, const void *buffer, size_t size) {
if (((offset - this->getBaseAddress()) + size) > this->getSize() || buffer == nullptr || size == 0)
return;
addPatch(offset, buffer, size);
}
void GDBProvider::readRaw(u64 offset, void *buffer, size_t size) {
offset -= this->getBaseAddress();
if ((offset + size) > this->getSize() || buffer == nullptr || size == 0)
if ((offset - this->getBaseAddress()) > (this->getActualSize() - size) || buffer == nullptr || size == 0)
return;
auto data = gdb::readMemory(this->m_socket, offset, size);
@ -181,10 +178,7 @@ namespace hex::plugin::builtin::prv {
}
void GDBProvider::writeRaw(u64 offset, const void *buffer, size_t size) {
offset -= this->getBaseAddress();
if ((offset + size) > this->getSize() || buffer == nullptr || size == 0)
return;
}
void GDBProvider::save() {

View File

@ -13,7 +13,12 @@ namespace hex::plugin::builtin {
if (ImGui::Begin(View::toWindowName("hex.builtin.view.gdb.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) {
ImGui::Header("hex.builtin.view.gdb.settings"_lang);
ImGui::InputText("hex.builtin.view.gdb.ip"_lang, this->m_address.data(), this->m_address.capacity(), ImGuiInputTextFlags_CallbackEdit, ImGui::UpdateStringSizeCallback, &this->m_address);
ImGui::InputInt("hex.builtin.view.gdb.port"_lang, &this->m_port, 1, 0xFFFF);
ImGui::InputInt("hex.builtin.view.gdb.port"_lang, &this->m_port, 0, 0);
if (this->m_port < 0)
this->m_port = 0;
else if (this->m_port > 0xFFFF)
this->m_port = 0xFFFF;
ImGui::NewLine();

View File

@ -41,7 +41,7 @@ namespace hex::plugin::builtin {
return 0x00;
ImU8 byte;
provider->readRelative(off, &byte, sizeof(ImU8));
provider->read(off + provider->getBaseAddress() + provider->getCurrentPageAddress(), &byte, sizeof(ImU8));
return byte;
};
@ -51,7 +51,7 @@ namespace hex::plugin::builtin {
if (!provider->isAvailable() || !provider->isWritable())
return;
provider->writeRelative(off, &d, sizeof(ImU8));
provider->write(off + provider->getBaseAddress() + provider->getCurrentPageAddress(), &d, sizeof(ImU8));
EventManager::post<EventDataChanged>();
ProjectFile::markDirty();
};
@ -61,7 +61,9 @@ namespace hex::plugin::builtin {
std::optional<u32> currColor, prevColor;
off += ImHexApi::Provider::get()->getBaseAddress();
auto provider = ImHexApi::Provider::get();
off += provider->getBaseAddress() + provider->getCurrentPageAddress();
u32 alpha = static_cast<u32>(_this->m_highlightAlpha) << 24;
@ -125,7 +127,7 @@ namespace hex::plugin::builtin {
size_t size = std::min<size_t>(_this->m_currEncodingFile.getLongestSequence(), provider->getActualSize() - addr);
std::vector<u8> buffer(size);
provider->readRelative(addr, buffer.data(), size);
provider->read(addr + provider->getBaseAddress() + provider->getCurrentPageAddress(), buffer.data(), size);
auto [decoded, advance] = _this->m_currEncodingFile.getEncodingFor(buffer);
@ -153,8 +155,8 @@ namespace hex::plugin::builtin {
if (region.size != 0) {
provider->setCurrentPage(page.value());
u64 start = region.address;
this->m_memoryEditor.GotoAddrAndSelect(start - provider->getBaseAddress(), start + region.size - provider->getBaseAddress() - 1);
u64 start = region.address - provider->getBaseAddress() - provider->getCurrentPageAddress();
this->m_memoryEditor.GotoAddrAndSelect(start, start + region.size - 1);
}
EventManager::post<EventRegionSelected>(Region { this->m_memoryEditor.DataPreviewAddr, (this->m_memoryEditor.DataPreviewAddrEnd - this->m_memoryEditor.DataPreviewAddr) + 1});
@ -284,7 +286,7 @@ namespace hex::plugin::builtin {
size_t dataSize = (!ImHexApi::Provider::isValid() || !provider->isReadable()) ? 0x00 : provider->getSize();
this->m_memoryEditor.DrawWindow(View::toWindowName("hex.builtin.view.hexeditor.name").c_str(), &this->getWindowOpenState(), this, dataSize, dataSize == 0 ? 0x00 : provider->getBaseAddress());
this->m_memoryEditor.DrawWindow(View::toWindowName("hex.builtin.view.hexeditor.name").c_str(), &this->getWindowOpenState(), this, dataSize, dataSize == 0 ? 0x00 : provider->getBaseAddress() + provider->getCurrentPageAddress());
if (dataSize != 0x00) {
if (ImGui::Begin(View::toWindowName("hex.builtin.view.hexeditor.name").c_str())) {
@ -758,7 +760,7 @@ namespace hex::plugin::builtin {
size_t copySize = (end - start) + 1;
std::vector<u8> buffer(copySize, 0x00);
provider->readRelative(start, buffer.data(), buffer.size());
provider->read(start + provider->getBaseAddress() + provider->getCurrentPageAddress(), buffer.data(), buffer.size());
std::string str;
for (const auto &byte : buffer)
@ -807,7 +809,7 @@ namespace hex::plugin::builtin {
}
// Write bytes
provider->writeRelative(start, buffer.data(), std::min(end - start + 1, buffer.size()));
provider->read(start + provider->getBaseAddress() + provider->getCurrentPageAddress(), buffer.data(), std::min(end - start + 1, buffer.size()));
}
void ViewHexEditor::copyString() const {
@ -820,7 +822,7 @@ namespace hex::plugin::builtin {
std::string buffer(copySize, 0x00);
buffer.reserve(copySize + 1);
provider->readRelative(start, buffer.data(), copySize);
provider->read(start + provider->getBaseAddress() + provider->getCurrentPageAddress(), buffer.data(), copySize);
ImGui::SetClipboardText(buffer.c_str());
}
@ -834,7 +836,7 @@ namespace hex::plugin::builtin {
size_t copySize = (end - start) + 1;
std::vector<u8> buffer(copySize, 0x00);
provider->readRelative(start, buffer.data(), buffer.size());
provider->read(start + provider->getBaseAddress() + provider->getCurrentPageAddress(), buffer.data(), buffer.size());
std::string str;
switch (language) {
@ -936,7 +938,7 @@ namespace hex::plugin::builtin {
size_t copySize = (end - start) + 1;
std::vector<u8> buffer(copySize, 0x00);
provider->readRelative(start, buffer.data(), buffer.size());
provider->read(start + provider->getBaseAddress() + provider->getCurrentPageAddress(), buffer.data(), buffer.size());
std::string str = "Hex View 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n\n";
@ -983,7 +985,7 @@ namespace hex::plugin::builtin {
size_t copySize = (end - start) + 1;
std::vector<u8> buffer(copySize, 0x00);
provider->readRelative(start, buffer.data(), buffer.size());
provider->read(start + provider->getBaseAddress() + provider->getCurrentPageAddress(), buffer.data(), buffer.size());
std::string str =
R"(
@ -1048,7 +1050,7 @@ R"(
size_t dataSize = provider->getSize();
for (u64 offset = 0; offset < dataSize; offset += 1024) {
size_t usedBufferSize = std::min(u64(buffer.size()), dataSize - offset);
provider->readRelative(offset, buffer.data(), usedBufferSize);
provider->read(offset + provider->getBaseAddress() + provider->getCurrentPageAddress(), buffer.data(), usedBufferSize);
for (u64 i = 0; i < usedBufferSize; i++) {
if (buffer[i] == string[foundCharacters])
@ -1086,7 +1088,7 @@ R"(
size_t dataSize = provider->getSize();
for (u64 offset = 0; offset < dataSize; offset += 1024) {
size_t usedBufferSize = std::min(u64(buffer.size()), dataSize - offset);
provider->readRelative(offset, buffer.data(), usedBufferSize);
provider->read(offset + provider->getBaseAddress() + provider->getCurrentPageAddress(), buffer.data(), usedBufferSize);
for (u64 i = 0; i < usedBufferSize; i++) {
if (buffer[i] == hex[foundCharacters])

View File

@ -87,7 +87,7 @@ namespace hex::plugin::builtin {
for (u64 i = 0; i < provider->getSize(); i += this->m_blockSize) {
std::array<ImU64, 256> blockValueCounts = { 0 };
provider->readRelative(i, buffer.data(), std::min(u64(this->m_blockSize), provider->getSize() - i));
provider->read(i + provider->getBaseAddress(), buffer.data(), std::min(u64(this->m_blockSize), provider->getSize() - i));
for (size_t j = 0; j < this->m_blockSize; j++) {
blockValueCounts[buffer[j]]++;

View File

@ -71,7 +71,7 @@ namespace hex::plugin::builtin {
for (u64 offset = 0; offset < provider->getSize(); offset += buffer.size()) {
size_t readSize = std::min(u64(buffer.size()), provider->getSize() - offset);
provider->readRelative(offset, buffer.data(), readSize);
provider->read(offset + provider->getBaseAddress(), buffer.data(), readSize);
for (u32 i = 0; i < readSize; i++) {
if (buffer[i] >= ' ' && buffer[i] <= '~' && offset < provider->getSize() - 1)

View File

@ -207,7 +207,7 @@ namespace hex::plugin::builtin {
if (context.buffer.empty()) return nullptr;
provider->readRelative(context.currBlock.base, context.buffer.data(), context.buffer.size());
provider->read(context.currBlock.base + provider->getBaseAddress() + provider->getCurrentPageAddress(), context.buffer.data(), context.buffer.size());
return context.buffer.data();
};

View File

@ -26,9 +26,7 @@ namespace hex::prv {
virtual bool isSavable() const = 0;
virtual void read(u64 offset, void *buffer, size_t size, bool overlays = true);
virtual void readRelative(u64 offset, void *buffer, size_t size, bool overlays = true);
virtual void write(u64 offset, const void *buffer, size_t size);
virtual void writeRelative(u64 offset, const void *buffer, size_t size);
virtual void resize(ssize_t newSize);
@ -55,6 +53,7 @@ namespace hex::prv {
virtual void setBaseAddress(u64 address);
virtual u64 getBaseAddress() const;
virtual u64 getCurrentPageAddress() const;
virtual size_t getSize() const;
virtual std::optional<u32> getPageOfAddress(u64 address) const;

View File

@ -65,7 +65,7 @@ namespace hex::magic {
std::string getDescription(prv::Provider *provider, size_t size) {
std::vector<u8> buffer(std::min(provider->getSize(), size), 0x00);
provider->readRelative(0x00, buffer.data(), buffer.size());
provider->read(provider->getBaseAddress(), buffer.data(), buffer.size());
return getDescription(buffer);
}
@ -86,7 +86,7 @@ namespace hex::magic {
std::string getMIMEType(prv::Provider *provider, size_t size) {
std::vector<u8> buffer(std::min(provider->getSize(), size), 0x00);
provider->readRelative(0x00, buffer.data(), buffer.size());
provider->read(provider->getBaseAddress(), buffer.data(), buffer.size());
return getMIMEType(buffer);
}

View File

@ -20,19 +20,11 @@ namespace hex::prv {
}
void Provider::read(u64 offset, void *buffer, size_t size, bool overlays) {
this->readRaw(offset, buffer, size);
}
void Provider::readRelative(u64 offset, void *buffer, size_t size, bool overlays) {
this->read(offset + this->getBaseAddress(), buffer, size);
this->readRaw(offset - this->getBaseAddress(), buffer, size);
}
void Provider::write(u64 offset, const void *buffer, size_t size) {
this->writeRaw(offset, buffer, size);
}
void Provider::writeRelative(u64 offset, const void *buffer, size_t size) {
this->write(offset + this->getBaseAddress(), buffer, size);
this->writeRaw(offset - this->getBaseAddress(), buffer, size);
}
void Provider::save() { }
@ -63,7 +55,7 @@ namespace hex::prv {
void Provider::applyPatches() {
for (auto &[patchAddress, patch] : getPatches())
this->writeRaw(patchAddress, &patch, 1);
this->writeRaw( - this->getBaseAddress(), &patch, 1);
}
@ -100,7 +92,11 @@ namespace hex::prv {
}
u64 Provider::getBaseAddress() const {
return this->m_baseAddress + PageSize * this->m_currPage;
return this->m_baseAddress;
}
u64 Provider::getCurrentPageAddress() const {
return PageSize * this->getCurrentPage();
}
size_t Provider::getSize() const {