2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/providers/provider.hpp>
|
2020-12-22 18:10:01 +01:00
|
|
|
|
|
|
|
#include <hex.hpp>
|
2021-12-12 00:41:44 +01:00
|
|
|
#include <hex/api/event.hpp>
|
2022-02-27 23:25:39 +01:00
|
|
|
#include <hex/pattern_language/patterns/pattern.hpp>
|
|
|
|
#include <hex/pattern_language/pattern_language.hpp>
|
|
|
|
|
|
|
|
#include <hex/ui/view.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>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace hex::prv {
|
|
|
|
|
|
|
|
Provider::Provider() {
|
|
|
|
this->m_patches.emplace_back();
|
2022-02-27 23:25:39 +01:00
|
|
|
this->m_patternLanguageRuntime = std::make_unique<pl::PatternLanguage>();
|
2021-12-12 00:41:44 +01:00
|
|
|
|
|
|
|
if (this->hasLoadInterface())
|
|
|
|
EventManager::post<RequestOpenPopup>(View::toWindowName("hex.builtin.view.provider_settings.load_popup"));
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 01:14:05 +01:00
|
|
|
Provider::~Provider() {
|
|
|
|
for (auto &overlay : this->m_overlays)
|
|
|
|
this->deleteOverlay(overlay);
|
|
|
|
}
|
|
|
|
|
2021-03-21 14:50:47 +01:00
|
|
|
void Provider::read(u64 offset, void *buffer, size_t size, bool 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);
|
2021-04-16 21:50:15 +02:00
|
|
|
}
|
|
|
|
|
2021-08-21 13:53:50 +02:00
|
|
|
void Provider::save() { }
|
2022-03-04 11:36:37 +01:00
|
|
|
void Provider::saveAs(const std::fs::path &path) { }
|
2021-08-21 13:53:50 +02:00
|
|
|
|
2022-01-20 23:24:26 +01:00
|
|
|
void Provider::resize(size_t newSize) { }
|
|
|
|
|
|
|
|
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)
|
|
|
|
patches.insert({ address + offset, value });
|
|
|
|
}
|
2021-07-27 21:07:36 +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() {
|
2022-01-09 21:27:59 +01:00
|
|
|
auto iter = this->m_patches.end();
|
|
|
|
for (auto i = 0; i < this->m_patchTreeOffset + 1; i++)
|
|
|
|
iter--;
|
|
|
|
|
|
|
|
return *(iter);
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
const std::map<u64, u8> &Provider::getPatches() const {
|
2022-01-09 21:27:59 +01:00
|
|
|
auto iter = this->m_patches.end();
|
|
|
|
for (auto i = 0; i < this->m_patchTreeOffset + 1; i++)
|
|
|
|
iter--;
|
|
|
|
|
|
|
|
return *(iter);
|
2021-09-21 02:29:54 +02:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
void Provider::applyPatches() {
|
2021-12-22 13:36:26 +01:00
|
|
|
for (auto &[patchAddress, patch] : getPatches()) {
|
|
|
|
this->writeRaw(patchAddress - this->getBaseAddress(), &patch, 1);
|
|
|
|
}
|
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() {
|
2021-01-30 22:39:06 +01:00
|
|
|
return this->m_overlays.emplace_back(new Overlay());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Provider::deleteOverlay(Overlay *overlay) {
|
|
|
|
this->m_overlays.erase(std::find(this->m_overlays.begin(), this->m_overlays.end(), overlay));
|
|
|
|
delete overlay;
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
const std::list<Overlay *> &Provider::getOverlays() {
|
2021-01-30 22:39:06 +01:00
|
|
|
return this->m_overlays;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
u32 Provider::getPageCount() const {
|
2020-12-22 18:10:01 +01:00
|
|
|
return std::ceil(this->getActualSize() / double(PageSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return PageSize * this->getCurrentPage();
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
size_t Provider::getSize() const {
|
2020-12-22 18:10:01 +01:00
|
|
|
return std::min(this->getActualSize() - PageSize * this->m_currPage, PageSize);
|
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
std::optional<u32> Provider::getPageOfAddress(u64 address) const {
|
2021-04-16 21:50:15 +02:00
|
|
|
u32 page = std::floor((address - this->getBaseAddress()) / double(PageSize));
|
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;
|
|
|
|
}
|
|
|
|
|
2022-01-09 21:27:59 +01:00
|
|
|
void Provider::addPatch(u64 offset, const void *buffer, size_t size, bool createUndo) {
|
2021-03-26 21:43:24 +01:00
|
|
|
if (this->m_patchTreeOffset > 0) {
|
2022-01-09 21:27:59 +01:00
|
|
|
auto iter = this->m_patches.end();
|
|
|
|
for (auto i = 0; i < this->m_patchTreeOffset; i++)
|
|
|
|
iter--;
|
|
|
|
|
|
|
|
this->m_patches.erase(iter, this->m_patches.end());
|
2021-03-26 21:43:24 +01:00
|
|
|
this->m_patchTreeOffset = 0;
|
|
|
|
}
|
|
|
|
|
2022-01-09 21:27:59 +01:00
|
|
|
if (createUndo)
|
|
|
|
createUndoPoint();
|
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;
|
|
|
|
this->readRaw(offset + i, &originalValue, sizeof(u8));
|
|
|
|
|
|
|
|
if (patch == originalValue)
|
|
|
|
getPatches().erase(offset + i);
|
|
|
|
else
|
|
|
|
getPatches()[offset + i] = patch;
|
|
|
|
}
|
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());
|
|
|
|
}
|
|
|
|
|
2021-03-26 21:43:24 +01:00
|
|
|
void Provider::undo() {
|
|
|
|
if (canUndo())
|
|
|
|
this->m_patchTreeOffset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Provider::redo() {
|
|
|
|
if (canRedo())
|
|
|
|
this->m_patchTreeOffset--;
|
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
bool Provider::canUndo() const {
|
2021-03-26 21:43:24 +01:00
|
|
|
return this->m_patchTreeOffset < this->m_patches.size() - 1;
|
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
bool Provider::canRedo() const {
|
2021-03-26 21:43:24 +01:00
|
|
|
return this->m_patchTreeOffset > 0;
|
|
|
|
}
|
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
|
|
|
|
bool Provider::hasLoadInterface() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Provider::hasInterface() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Provider::drawLoadInterface() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Provider::drawInterface() {
|
|
|
|
}
|
|
|
|
|
2021-12-22 13:36:26 +01:00
|
|
|
}
|