2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/providers/provider.hpp>
|
2020-12-22 18:10:01 +01:00
|
|
|
|
|
|
|
#include <hex.hpp>
|
2022-08-08 21:23:52 +02:00
|
|
|
#include <hex/api/event.hpp>
|
2020-12-22 18:10:01 +01:00
|
|
|
|
|
|
|
#include <cmath>
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <cstring>
|
2020-12-22 18:10:01 +01:00
|
|
|
#include <map>
|
|
|
|
#include <optional>
|
2022-04-17 16:57:30 +02:00
|
|
|
|
2023-01-24 09:07:11 +01:00
|
|
|
#include <hex/helpers/magic.hpp>
|
2023-03-17 08:15:43 +01:00
|
|
|
#include <wolv/io/file.hpp>
|
2023-01-24 09:07:11 +01:00
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
namespace hex::prv {
|
|
|
|
|
2023-07-26 13:50:51 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
u32 s_idCounter = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
|
|
|
|
Provider::Provider() : m_id(s_idCounter++) {
|
2020-12-22 18:10:01 +01:00
|
|
|
this->m_patches.emplace_back();
|
2023-04-12 19:21:48 +02:00
|
|
|
this->m_currPatches = this->m_patches.begin();
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 01:14:05 +01:00
|
|
|
Provider::~Provider() {
|
2023-06-03 22:50:37 +02:00
|
|
|
this->m_overlays.clear();
|
2023-09-03 16:18:29 +02:00
|
|
|
|
|
|
|
if (auto selection = ImHexApi::HexEditor::getSelection(); selection.has_value() && selection->provider == this)
|
|
|
|
EventManager::post<EventRegionSelected>(ImHexApi::HexEditor::ProviderRegion { { 0x00, 0x00 }, nullptr });
|
2021-02-04 01:14:05 +01:00
|
|
|
}
|
|
|
|
|
2021-03-21 14:50:47 +01:00
|
|
|
void Provider::read(u64 offset, void *buffer, size_t size, bool overlays) {
|
2022-03-27 00:01:28 +01:00
|
|
|
hex::unused(overlays);
|
|
|
|
|
2021-12-09 21:10:24 +01:00
|
|
|
this->readRaw(offset - this->getBaseAddress(), buffer, size);
|
2021-04-16 21:50:15 +02:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
void Provider::write(u64 offset, const void *buffer, size_t size) {
|
2021-12-09 21:10:24 +01:00
|
|
|
this->writeRaw(offset - this->getBaseAddress(), buffer, size);
|
2022-08-08 21:23:52 +02:00
|
|
|
this->markDirty();
|
2021-04-16 21:50:15 +02:00
|
|
|
}
|
|
|
|
|
2023-04-06 17:36:28 +02:00
|
|
|
void Provider::save() {
|
|
|
|
EventManager::post<EventProviderSaved>(this);
|
|
|
|
}
|
2022-03-27 00:01:28 +01:00
|
|
|
void Provider::saveAs(const std::fs::path &path) {
|
2023-03-17 08:15:43 +01:00
|
|
|
wolv::io::File file(path, wolv::io::File::Mode::Create);
|
|
|
|
|
|
|
|
if (file.isValid()) {
|
|
|
|
std::vector<u8> buffer(std::min<size_t>(0xFF'FFFF, this->getActualSize()), 0x00);
|
2023-03-17 08:38:38 +01:00
|
|
|
size_t bufferSize = 0;
|
2023-03-17 08:15:43 +01:00
|
|
|
|
|
|
|
for (u64 offset = 0; offset < this->getActualSize(); offset += bufferSize) {
|
2023-03-17 08:38:38 +01:00
|
|
|
bufferSize = buffer.size();
|
|
|
|
|
|
|
|
auto [region, valid] = this->getRegionValidity(offset + this->getBaseAddress());
|
|
|
|
if (!valid)
|
|
|
|
offset = region.getEndAddress() + 1;
|
|
|
|
|
|
|
|
auto [newRegion, newValid] = this->getRegionValidity(offset + this->getBaseAddress());
|
2023-03-17 09:17:44 +01:00
|
|
|
bufferSize = std::min<size_t>(bufferSize, (newRegion.getEndAddress() - offset) + 1);
|
|
|
|
bufferSize = std::min<size_t>(bufferSize, this->getActualSize() - offset);
|
2023-03-17 08:15:43 +01:00
|
|
|
|
|
|
|
this->read(offset + this->getBaseAddress(), buffer.data(), bufferSize, true);
|
2023-03-23 11:23:07 +01:00
|
|
|
file.writeBuffer(buffer.data(), bufferSize);
|
2023-03-17 08:15:43 +01:00
|
|
|
}
|
2023-04-06 17:36:28 +02:00
|
|
|
|
2023-06-03 23:57:41 +02:00
|
|
|
for (auto &[patchAddress, patch] : getPatches()) {
|
|
|
|
file.seek(patchAddress - this->getBaseAddress());
|
|
|
|
file.writeBuffer(&patch, 1);
|
|
|
|
}
|
|
|
|
|
2023-04-06 17:36:28 +02:00
|
|
|
EventManager::post<EventProviderSaved>(this);
|
2023-03-17 08:15:43 +01:00
|
|
|
}
|
2022-03-27 00:01:28 +01:00
|
|
|
}
|
2021-08-21 13:53:50 +02:00
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
void Provider::resize(size_t newSize) {
|
2022-08-08 21:23:52 +02:00
|
|
|
hex::unused(newSize);
|
|
|
|
|
|
|
|
this->markDirty();
|
2022-03-27 00:01:28 +01:00
|
|
|
}
|
2022-01-20 23:24:26 +01:00
|
|
|
|
|
|
|
void Provider::insert(u64 offset, size_t size) {
|
|
|
|
auto &patches = getPatches();
|
|
|
|
|
|
|
|
std::vector<std::pair<u64, u8>> patchesToMove;
|
|
|
|
|
|
|
|
for (auto &[address, value] : patches) {
|
|
|
|
if (address > offset)
|
|
|
|
patchesToMove.emplace_back(address, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &[address, value] : patchesToMove)
|
|
|
|
patches.erase(address);
|
|
|
|
for (const auto &[address, value] : patchesToMove)
|
2022-03-27 00:01:28 +01:00
|
|
|
patches.insert({ address + size, value });
|
2022-08-08 21:23:52 +02:00
|
|
|
|
|
|
|
this->markDirty();
|
2022-01-20 23:24:26 +01:00
|
|
|
}
|
2021-07-27 21:07:36 +02:00
|
|
|
|
2022-06-17 14:21:56 +02:00
|
|
|
void Provider::remove(u64 offset, size_t size) {
|
|
|
|
auto &patches = getPatches();
|
|
|
|
|
|
|
|
std::vector<std::pair<u64, u8>> patchesToMove;
|
|
|
|
|
|
|
|
for (auto &[address, value] : patches) {
|
|
|
|
if (address > offset)
|
|
|
|
patchesToMove.emplace_back(address, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &[address, value] : patchesToMove)
|
|
|
|
patches.erase(address);
|
|
|
|
for (const auto &[address, value] : patchesToMove)
|
|
|
|
patches.insert({ address - size, value });
|
2022-08-08 21:23:52 +02:00
|
|
|
|
|
|
|
this->markDirty();
|
2022-06-17 14:21:56 +02:00
|
|
|
}
|
|
|
|
|
2021-03-21 14:50:47 +01:00
|
|
|
void Provider::applyOverlays(u64 offset, void *buffer, size_t size) {
|
|
|
|
for (auto &overlay : this->m_overlays) {
|
|
|
|
auto overlayOffset = overlay->getAddress();
|
2022-02-01 22:09:44 +01:00
|
|
|
auto overlaySize = overlay->getSize();
|
2021-03-21 14:50:47 +01:00
|
|
|
|
2022-01-22 22:37:52 +01:00
|
|
|
i128 overlapMin = std::max(offset, overlayOffset);
|
|
|
|
i128 overlapMax = std::min(offset + size, overlayOffset + overlaySize);
|
2022-01-24 20:53:17 +01:00
|
|
|
if (overlapMax > overlapMin)
|
|
|
|
std::memcpy(static_cast<u8 *>(buffer) + std::max<i128>(0, overlapMin - offset), overlay->getData().data() + std::max<i128>(0, overlapMin - overlayOffset), overlapMax - overlapMin);
|
2021-03-21 14:50:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
std::map<u64, u8> &Provider::getPatches() {
|
2023-04-12 19:21:48 +02:00
|
|
|
return *this->m_currPatches;
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
const std::map<u64, u8> &Provider::getPatches() const {
|
2023-04-12 19:21:48 +02:00
|
|
|
return *this->m_currPatches;
|
2021-09-21 02:29:54 +02:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
void Provider::applyPatches() {
|
2023-06-03 23:57:41 +02:00
|
|
|
if (!this->isWritable())
|
|
|
|
return;
|
|
|
|
|
2023-07-27 02:05:35 +02:00
|
|
|
this->m_patches.emplace_back();
|
|
|
|
|
|
|
|
for (auto &[patchAddress, patch] : getPatches()) {
|
|
|
|
u8 value = 0x00;
|
|
|
|
this->readRaw(patchAddress - this->getBaseAddress(), &value, 1);
|
|
|
|
this->m_patches.back().insert({ patchAddress, value });
|
|
|
|
}
|
|
|
|
|
2021-12-22 13:36:26 +01:00
|
|
|
for (auto &[patchAddress, patch] : getPatches()) {
|
|
|
|
this->writeRaw(patchAddress - this->getBaseAddress(), &patch, 1);
|
|
|
|
}
|
2023-02-17 10:26:09 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
this->markDirty();
|
2022-09-28 15:01:43 +02:00
|
|
|
|
|
|
|
this->m_patches.emplace_back();
|
2023-07-27 02:05:35 +02:00
|
|
|
this->m_currPatches = std::prev(this->m_patches.end());
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
2021-01-30 22:39:06 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
Overlay *Provider::newOverlay() {
|
2023-09-03 10:26:25 +02:00
|
|
|
return this->m_overlays.emplace_back(std::make_unique<Overlay>()).get();
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Provider::deleteOverlay(Overlay *overlay) {
|
2023-08-25 23:54:39 +02:00
|
|
|
this->m_overlays.remove_if([overlay](const auto &item) {
|
|
|
|
return item.get() == overlay;
|
|
|
|
});
|
2021-01-30 22:39:06 +01:00
|
|
|
}
|
|
|
|
|
2023-08-25 23:54:39 +02:00
|
|
|
const std::list<std::unique_ptr<Overlay>> &Provider::getOverlays() {
|
2021-01-30 22:39:06 +01:00
|
|
|
return this->m_overlays;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-23 11:34:30 +02:00
|
|
|
size_t Provider::getPageSize() const {
|
|
|
|
return this->m_pageSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Provider::setPageSize(size_t pageSize) {
|
|
|
|
if (pageSize > MaxPageSize)
|
|
|
|
pageSize = MaxPageSize;
|
|
|
|
|
|
|
|
this->m_pageSize = pageSize;
|
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
u32 Provider::getPageCount() const {
|
2023-05-23 11:34:30 +02:00
|
|
|
return (this->getActualSize() / this->getPageSize()) + (this->getActualSize() % this->getPageSize() != 0 ? 1 : 0);
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 Provider::getCurrentPage() const {
|
|
|
|
return this->m_currPage;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Provider::setCurrentPage(u32 page) {
|
|
|
|
if (page < getPageCount())
|
|
|
|
this->m_currPage = page;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-11 13:50:04 +01:00
|
|
|
void Provider::setBaseAddress(u64 address) {
|
|
|
|
this->m_baseAddress = address;
|
2022-08-08 21:23:52 +02:00
|
|
|
this->markDirty();
|
2021-01-11 13:50:04 +01:00
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
u64 Provider::getBaseAddress() const {
|
2021-12-09 21:10:24 +01:00
|
|
|
return this->m_baseAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 Provider::getCurrentPageAddress() const {
|
2023-05-23 11:34:30 +02:00
|
|
|
return this->getPageSize() * this->getCurrentPage();
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
size_t Provider::getSize() const {
|
2023-05-23 11:34:30 +02:00
|
|
|
return std::min(this->getActualSize() - this->getPageSize() * this->m_currPage, this->getPageSize());
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
std::optional<u32> Provider::getPageOfAddress(u64 address) const {
|
2023-05-23 11:34:30 +02:00
|
|
|
u32 page = std::floor((address - this->getBaseAddress()) / double(this->getPageSize()));
|
2020-12-22 18:10:01 +01:00
|
|
|
|
|
|
|
if (page >= this->getPageCount())
|
2022-01-30 12:43:43 +01:00
|
|
|
return std::nullopt;
|
2020-12-22 18:10:01 +01:00
|
|
|
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
2023-09-08 21:59:27 +02:00
|
|
|
std::vector<Provider::Description> Provider::getDataDescription() const {
|
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
2022-01-09 21:27:59 +01:00
|
|
|
void Provider::addPatch(u64 offset, const void *buffer, size_t size, bool createUndo) {
|
2023-04-13 16:12:00 +02:00
|
|
|
if (createUndo) {
|
|
|
|
// Delete all patches after the current one if a modification is made while
|
|
|
|
// the current patch list is not at the end of the undo stack
|
|
|
|
if (std::next(this->m_currPatches) != this->m_patches.end())
|
|
|
|
this->m_patches.erase(std::next(this->m_currPatches), this->m_patches.end());
|
|
|
|
|
2022-01-09 21:27:59 +01:00
|
|
|
createUndoPoint();
|
2023-04-13 16:12:00 +02:00
|
|
|
}
|
2021-03-26 21:43:24 +01:00
|
|
|
|
2022-01-30 11:58:32 +01:00
|
|
|
for (u64 i = 0; i < size; i++) {
|
2022-02-01 22:09:44 +01:00
|
|
|
u8 patch = reinterpret_cast<const u8 *>(buffer)[i];
|
2022-01-30 11:58:32 +01:00
|
|
|
u8 originalValue = 0x00;
|
2023-07-27 02:05:35 +02:00
|
|
|
this->readRaw((offset + i) - this->getBaseAddress(), &originalValue, sizeof(u8));
|
2022-01-30 11:58:32 +01:00
|
|
|
|
|
|
|
if (patch == originalValue)
|
|
|
|
getPatches().erase(offset + i);
|
|
|
|
else
|
|
|
|
getPatches()[offset + i] = patch;
|
2023-04-06 17:36:28 +02:00
|
|
|
|
|
|
|
EventManager::post<EventPatchCreated>(offset, originalValue, patch);
|
2022-01-30 11:58:32 +01:00
|
|
|
}
|
2022-08-08 21:23:52 +02:00
|
|
|
|
|
|
|
this->markDirty();
|
2023-04-06 17:36:28 +02:00
|
|
|
|
2021-03-26 21:43:24 +01:00
|
|
|
}
|
|
|
|
|
2022-01-09 21:27:59 +01:00
|
|
|
void Provider::createUndoPoint() {
|
|
|
|
this->m_patches.push_back(getPatches());
|
2023-04-12 19:21:48 +02:00
|
|
|
this->m_currPatches = std::prev(this->m_patches.end());
|
2022-01-09 21:27:59 +01:00
|
|
|
}
|
|
|
|
|
2021-03-26 21:43:24 +01:00
|
|
|
void Provider::undo() {
|
|
|
|
if (canUndo())
|
2023-04-12 19:21:48 +02:00
|
|
|
this->m_currPatches--;
|
2021-03-26 21:43:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Provider::redo() {
|
|
|
|
if (canRedo())
|
2023-04-12 19:21:48 +02:00
|
|
|
this->m_currPatches++;
|
2021-03-26 21:43:24 +01:00
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
bool Provider::canUndo() const {
|
2023-04-12 19:21:48 +02:00
|
|
|
return this->m_currPatches != this->m_patches.begin();
|
2021-03-26 21:43:24 +01:00
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
bool Provider::canRedo() const {
|
2023-04-12 19:21:48 +02:00
|
|
|
return std::next(this->m_currPatches) != this->m_patches.end();
|
2021-03-26 21:43:24 +01:00
|
|
|
}
|
|
|
|
|
2022-08-12 15:11:27 +02:00
|
|
|
bool Provider::hasFilePicker() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Provider::handleFilePicker() {
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-12 00:41:44 +01:00
|
|
|
|
|
|
|
bool Provider::hasLoadInterface() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Provider::hasInterface() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-16 16:48:15 +01:00
|
|
|
bool Provider::drawLoadInterface() {
|
|
|
|
return true;
|
2021-12-12 00:41:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Provider::drawInterface() {
|
|
|
|
}
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
nlohmann::json Provider::storeSettings(nlohmann::json settings) const {
|
2022-08-14 10:07:45 +02:00
|
|
|
settings["displayName"] = this->getName();
|
|
|
|
settings["type"] = this->getTypeName();
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
settings["baseAddress"] = this->m_baseAddress;
|
|
|
|
settings["currPage"] = this->m_currPage;
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Provider::loadSettings(const nlohmann::json &settings) {
|
|
|
|
this->m_baseAddress = settings["baseAddress"];
|
|
|
|
this->m_currPage = settings["currPage"];
|
|
|
|
}
|
|
|
|
|
2022-08-10 09:26:48 +02:00
|
|
|
std::pair<Region, bool> Provider::getRegionValidity(u64 address) const {
|
2022-08-17 16:28:44 +02:00
|
|
|
if ((address - this->getBaseAddress()) > this->getActualSize())
|
2022-08-10 09:26:48 +02:00
|
|
|
return { Region::Invalid(), false };
|
|
|
|
|
|
|
|
bool insideValidRegion = false;
|
|
|
|
|
|
|
|
std::optional<u64> nextRegionAddress;
|
|
|
|
for (const auto &overlay : this->m_overlays) {
|
|
|
|
Region overlayRegion = { overlay->getAddress(), overlay->getSize() };
|
|
|
|
if (!nextRegionAddress.has_value() || overlay->getAddress() < nextRegionAddress) {
|
|
|
|
nextRegionAddress = overlayRegion.getStartAddress();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Region { address, 1 }.overlaps(overlayRegion)) {
|
|
|
|
insideValidRegion = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &[patchAddress, value] : this->m_patches.back()) {
|
|
|
|
if (!nextRegionAddress.has_value() || patchAddress < nextRegionAddress)
|
|
|
|
nextRegionAddress = patchAddress;
|
|
|
|
|
|
|
|
if (address == patchAddress)
|
|
|
|
insideValidRegion = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nextRegionAddress.has_value())
|
2022-08-14 22:38:01 +02:00
|
|
|
return { Region::Invalid(), false };
|
2022-08-10 09:26:48 +02:00
|
|
|
else
|
|
|
|
return { Region { address, *nextRegionAddress - address }, insideValidRegion };
|
|
|
|
}
|
|
|
|
|
2022-09-22 09:05:09 +02:00
|
|
|
|
|
|
|
u32 Provider::getID() const {
|
|
|
|
return this->m_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Provider::setID(u32 id) {
|
|
|
|
this->m_id = id;
|
|
|
|
if (id > s_idCounter)
|
|
|
|
s_idCounter = id + 1;
|
|
|
|
}
|
|
|
|
|
2023-01-24 09:07:11 +01:00
|
|
|
|
|
|
|
[[nodiscard]] std::variant<std::string, i128> Provider::queryInformation(const std::string &category, const std::string &) {
|
|
|
|
if (category == "mime")
|
|
|
|
return magic::getMIMEType(this);
|
|
|
|
else if (category == "description")
|
|
|
|
return magic::getDescription(this);
|
|
|
|
else if (category == "provider_type")
|
|
|
|
return this->getTypeName();
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-05 20:49:57 +02:00
|
|
|
[[nodiscard]] bool Provider::isDumpable() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-22 13:36:26 +01:00
|
|
|
}
|