2020-11-10 21:31:04 +01:00
|
|
|
#include "views/view_hexeditor.hpp"
|
|
|
|
|
2020-11-11 10:47:02 +01:00
|
|
|
#include "providers/provider.hpp"
|
2020-11-11 09:18:35 +01:00
|
|
|
#include "providers/file_provider.hpp"
|
|
|
|
|
2020-11-11 14:42:01 +01:00
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
2020-11-28 21:55:52 +01:00
|
|
|
#include "helpers/crypto.hpp"
|
|
|
|
#include "helpers/patches.hpp"
|
2020-11-30 00:03:12 +01:00
|
|
|
#include "helpers/project_file_handler.hpp"
|
2020-12-01 02:21:40 +01:00
|
|
|
#include "helpers/loader_script_handler.hpp"
|
2020-11-24 02:00:22 +01:00
|
|
|
|
|
|
|
#undef __STRICT_ANSI__
|
|
|
|
#include <cstdio>
|
|
|
|
|
2020-11-10 21:31:04 +01:00
|
|
|
namespace hex {
|
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
ViewHexEditor::ViewHexEditor(std::vector<lang::PatternData*> &patternData)
|
|
|
|
: View("Hex Editor"), m_patternData(patternData) {
|
2020-11-10 21:31:04 +01:00
|
|
|
|
|
|
|
this->m_memoryEditor.ReadFn = [](const ImU8 *data, size_t off) -> ImU8 {
|
2020-12-27 15:39:06 +01:00
|
|
|
auto provider = prv::Provider::getCurrentProvider();
|
|
|
|
if (!provider->isAvailable() || !provider->isReadable())
|
2020-11-10 21:31:04 +01:00
|
|
|
return 0x00;
|
|
|
|
|
|
|
|
ImU8 byte;
|
2020-12-27 15:39:06 +01:00
|
|
|
provider->read(off, &byte, sizeof(ImU8));
|
2020-11-10 21:31:04 +01:00
|
|
|
|
|
|
|
return byte;
|
|
|
|
};
|
|
|
|
|
|
|
|
this->m_memoryEditor.WriteFn = [](ImU8 *data, size_t off, ImU8 d) -> void {
|
2020-12-27 15:39:06 +01:00
|
|
|
auto provider = prv::Provider::getCurrentProvider();
|
|
|
|
if (!provider->isAvailable() || !provider->isWritable())
|
2020-11-10 21:31:04 +01:00
|
|
|
return;
|
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
provider->write(off, &d, sizeof(ImU8));
|
|
|
|
View::postEvent(Events::DataChanged);
|
2020-11-30 00:03:12 +01:00
|
|
|
ProjectFile::markDirty();
|
2020-11-10 21:31:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
this->m_memoryEditor.HighlightFn = [](const ImU8 *data, size_t off, bool next) -> bool {
|
|
|
|
ViewHexEditor *_this = (ViewHexEditor *) data;
|
|
|
|
|
2021-01-03 02:37:37 +01:00
|
|
|
std::optional<u32> currColor, prevColor;
|
|
|
|
if (_this->m_highlightedBytes.contains(off))
|
|
|
|
currColor = _this->m_highlightedBytes[off];
|
|
|
|
if (_this->m_highlightedBytes.contains(off - 1))
|
|
|
|
prevColor = _this->m_highlightedBytes[off - 1];
|
|
|
|
|
|
|
|
if (next && prevColor != currColor) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currColor.has_value()) {
|
|
|
|
_this->m_memoryEditor.HighlightColor = currColor.value();
|
|
|
|
return true;
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
|
2020-11-19 22:34:56 +01:00
|
|
|
_this->m_memoryEditor.HighlightColor = 0x60C08080;
|
2020-11-10 21:31:04 +01:00
|
|
|
return false;
|
|
|
|
};
|
2020-11-17 13:58:50 +01:00
|
|
|
|
|
|
|
View::subscribeEvent(Events::FileDropped, [this](const void *userData) {
|
|
|
|
auto filePath = static_cast<const char*>(userData);
|
|
|
|
|
|
|
|
if (filePath != nullptr)
|
|
|
|
this->openFile(filePath);
|
|
|
|
});
|
2020-11-23 13:10:14 +01:00
|
|
|
|
|
|
|
View::subscribeEvent(Events::SelectionChangeRequest, [this](const void *userData) {
|
|
|
|
const Region ®ion = *reinterpret_cast<const Region*>(userData);
|
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
auto provider = prv::Provider::getCurrentProvider();
|
|
|
|
auto page = provider->getPageOfAddress(region.address);
|
2020-11-23 13:10:14 +01:00
|
|
|
if (!page.has_value())
|
|
|
|
return;
|
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
provider->setCurrentPage(page.value());
|
2020-11-23 13:10:14 +01:00
|
|
|
this->m_memoryEditor.GotoAddr = region.address;
|
|
|
|
this->m_memoryEditor.DataPreviewAddr = region.address;
|
|
|
|
this->m_memoryEditor.DataPreviewAddrEnd = region.address + region.size - 1;
|
2020-11-28 15:53:11 +01:00
|
|
|
View::postEvent(Events::RegionSelected, ®ion);
|
2020-11-23 13:10:14 +01:00
|
|
|
});
|
2020-11-30 00:03:12 +01:00
|
|
|
|
|
|
|
View::subscribeEvent(Events::ProjectFileLoad, [this](const void *userData) {
|
|
|
|
this->openFile(ProjectFile::getFilePath());
|
|
|
|
});
|
|
|
|
|
|
|
|
View::subscribeEvent(Events::WindowClosing, [this](const void *userData) {
|
|
|
|
auto window = const_cast<GLFWwindow*>(static_cast<const GLFWwindow*>(userData));
|
|
|
|
|
|
|
|
if (ProjectFile::hasUnsavedChanges()) {
|
|
|
|
glfwSetWindowShouldClose(window, GLFW_FALSE);
|
2020-12-07 00:42:52 +01:00
|
|
|
this->getWindowOpenState() = true;
|
2020-11-30 00:03:12 +01:00
|
|
|
View::doLater([] { ImGui::OpenPopup("Save Changes"); });
|
|
|
|
}
|
|
|
|
});
|
2021-01-03 02:37:37 +01:00
|
|
|
|
|
|
|
View::subscribeEvent(Events::PatternChanged, [this](const void *userData) {
|
|
|
|
this->m_highlightedBytes.clear();
|
|
|
|
|
|
|
|
for (const auto &pattern : this->m_patternData)
|
|
|
|
this->m_highlightedBytes.merge(pattern->getHighlightedAddresses());
|
|
|
|
});
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
|
2020-11-12 09:38:52 +01:00
|
|
|
ViewHexEditor::~ViewHexEditor() {
|
2020-12-27 15:39:06 +01:00
|
|
|
|
2020-11-12 09:38:52 +01:00
|
|
|
}
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
void ViewHexEditor::drawContent() {
|
2020-12-27 15:39:06 +01:00
|
|
|
auto provider = prv::Provider::getCurrentProvider();
|
|
|
|
|
|
|
|
size_t dataSize = (provider == nullptr || !provider->isReadable()) ? 0x00 : provider->getSize();
|
2020-11-12 09:38:52 +01:00
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
this->m_memoryEditor.DrawWindow("Hex Editor", &this->getWindowOpenState(), this, dataSize, dataSize == 0 ? 0x00 : provider->getBaseAddress());
|
2020-11-12 09:38:52 +01:00
|
|
|
|
|
|
|
if (dataSize != 0x00) {
|
2020-11-22 19:43:35 +01:00
|
|
|
ImGui::Begin("Hex Editor");
|
|
|
|
ImGui::SameLine();
|
2020-12-21 15:20:57 +01:00
|
|
|
ImGui::Spacing();
|
|
|
|
ImGui::SameLine();
|
2020-12-27 15:39:06 +01:00
|
|
|
ImGui::Text("Page %d / %d", provider->getCurrentPage() + 1, provider->getPageCount());
|
2020-11-22 19:43:35 +01:00
|
|
|
ImGui::SameLine();
|
|
|
|
|
|
|
|
if (ImGui::ArrowButton("prevPage", ImGuiDir_Left)) {
|
2020-12-27 15:39:06 +01:00
|
|
|
provider->setCurrentPage(provider->getCurrentPage() - 1);
|
2020-11-22 19:43:35 +01:00
|
|
|
|
2020-11-28 15:53:11 +01:00
|
|
|
Region dataPreview = { std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd), 1 };
|
|
|
|
View::postEvent(Events::RegionSelected, &dataPreview);
|
2020-11-22 19:43:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
|
|
|
|
if (ImGui::ArrowButton("nextPage", ImGuiDir_Right)) {
|
2020-12-27 15:39:06 +01:00
|
|
|
provider->setCurrentPage(provider->getCurrentPage() + 1);
|
2020-11-22 19:43:35 +01:00
|
|
|
|
2020-11-28 15:53:11 +01:00
|
|
|
Region dataPreview = { std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd), 1 };
|
|
|
|
View::postEvent(Events::RegionSelected, &dataPreview);
|
2020-11-22 19:43:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::End();
|
|
|
|
|
2020-11-12 09:38:52 +01:00
|
|
|
this->drawSearchPopup();
|
|
|
|
this->drawGotoPopup();
|
|
|
|
}
|
|
|
|
|
2020-11-24 02:59:49 +01:00
|
|
|
|
2020-12-11 14:24:42 +01:00
|
|
|
if (ImGui::BeginPopupModal("Save Changes", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
|
|
|
|
constexpr auto Message = "You have unsaved changes made to your Project.\nAre you sure you want to exit?";
|
2020-11-30 00:03:12 +01:00
|
|
|
ImGui::NewLine();
|
2020-12-11 14:24:42 +01:00
|
|
|
ImGui::TextUnformatted(Message);
|
|
|
|
ImGui::NewLine();
|
|
|
|
|
|
|
|
confirmButtons("Yes", "No", [] { std::exit(0); }, [] { ImGui::CloseCurrentPopup(); });
|
2020-11-30 00:03:12 +01:00
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
|
|
|
|
2020-12-11 14:24:42 +01:00
|
|
|
if (ImGui::BeginPopupModal("Load File with Loader Script", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
|
2020-12-01 02:21:40 +01:00
|
|
|
constexpr auto Message = "Load a file using a Python loader script.";
|
2020-12-11 14:24:42 +01:00
|
|
|
ImGui::SetCursorPosX(10);
|
|
|
|
ImGui::TextWrapped(Message);
|
2020-12-01 02:21:40 +01:00
|
|
|
|
|
|
|
ImGui::NewLine();
|
|
|
|
ImGui::InputText("##nolabel", this->m_loaderScriptScriptPath.data(), this->m_loaderScriptScriptPath.length(), ImGuiInputTextFlags_ReadOnly);
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::Button("Script"))
|
|
|
|
ImGui::OpenPopup("Loader Script: Open Script");
|
|
|
|
ImGui::InputText("##nolabel", this->m_loaderScriptFilePath.data(), this->m_loaderScriptFilePath.length(), ImGuiInputTextFlags_ReadOnly);
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::Button("File"))
|
|
|
|
ImGui::OpenPopup("Loader Script: Open File");
|
|
|
|
|
|
|
|
|
|
|
|
if (this->m_fileBrowser.showFileDialog("Loader Script: Open Script", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, ImVec2(0, 0), ".py")) {
|
|
|
|
this->m_loaderScriptScriptPath = this->m_fileBrowser.selected_path;
|
|
|
|
}
|
|
|
|
if (this->m_fileBrowser.showFileDialog("Loader Script: Open File", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN)) {
|
|
|
|
this->m_loaderScriptFilePath = this->m_fileBrowser.selected_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::NewLine();
|
|
|
|
|
2020-12-11 14:24:42 +01:00
|
|
|
confirmButtons("Load", "Cancel",
|
2020-12-27 15:39:06 +01:00
|
|
|
[this, &provider] {
|
2020-12-11 14:24:42 +01:00
|
|
|
if (!this->m_loaderScriptScriptPath.empty() && !this->m_loaderScriptFilePath.empty()) {
|
|
|
|
this->openFile(this->m_loaderScriptFilePath);
|
|
|
|
LoaderScript::setFilePath(this->m_loaderScriptFilePath);
|
2020-12-27 15:39:06 +01:00
|
|
|
LoaderScript::setDataProvider(provider);
|
2020-12-11 14:24:42 +01:00
|
|
|
LoaderScript::processFile(this->m_loaderScriptScriptPath);
|
|
|
|
ImGui::CloseCurrentPopup();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[] {
|
2020-12-01 02:21:40 +01:00
|
|
|
ImGui::CloseCurrentPopup();
|
|
|
|
}
|
2020-12-11 14:24:42 +01:00
|
|
|
);
|
|
|
|
|
2020-12-01 02:21:40 +01:00
|
|
|
ImGui::EndPopup();
|
2020-12-11 14:24:42 +01:00
|
|
|
|
2020-12-01 02:21:40 +01:00
|
|
|
}
|
|
|
|
|
2020-11-24 02:59:49 +01:00
|
|
|
|
2020-11-17 15:38:24 +01:00
|
|
|
if (this->m_fileBrowser.showFileDialog("Open File", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN)) {
|
|
|
|
this->openFile(this->m_fileBrowser.selected_path);
|
2020-11-12 21:20:51 +01:00
|
|
|
}
|
|
|
|
|
2020-11-24 02:00:22 +01:00
|
|
|
if (this->m_fileBrowser.showFileDialog("Open Base64 File", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN)) {
|
|
|
|
std::vector<u8> base64;
|
|
|
|
this->loadFromFile(this->m_fileBrowser.selected_path, base64);
|
|
|
|
|
|
|
|
if (!base64.empty()) {
|
2020-11-27 09:09:48 +01:00
|
|
|
this->m_dataToSave = decode64(base64);
|
2020-11-27 13:45:56 +01:00
|
|
|
|
|
|
|
if (this->m_dataToSave.empty())
|
|
|
|
View::showErrorPopup("File is not in a valid Base64 format!");
|
|
|
|
else
|
|
|
|
ImGui::OpenPopup("Save Data");
|
|
|
|
} else View::showErrorPopup("Failed to open file!");
|
2020-11-24 02:00:22 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-30 00:03:12 +01:00
|
|
|
|
|
|
|
if (this->m_fileBrowser.showFileDialog("Open Project", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, ImVec2(0, 0), ".hexproj")) {
|
|
|
|
ProjectFile::load(this->m_fileBrowser.selected_path);
|
|
|
|
View::postEvent(Events::ProjectFileLoad);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->m_fileBrowser.showFileDialog("Save Project", imgui_addons::ImGuiFileBrowser::DialogMode::SAVE, ImVec2(0, 0), ".hexproj")) {
|
|
|
|
ProjectFile::store(this->m_fileBrowser.selected_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-27 13:45:56 +01:00
|
|
|
if (this->m_fileBrowser.showFileDialog("Export File", imgui_addons::ImGuiFileBrowser::DialogMode::SAVE)) {
|
2020-11-27 09:09:48 +01:00
|
|
|
this->saveToFile(this->m_fileBrowser.selected_path, this->m_dataToSave);
|
2020-11-24 02:00:22 +01:00
|
|
|
}
|
|
|
|
|
2020-11-29 15:09:36 +01:00
|
|
|
if (this->m_fileBrowser.showFileDialog("Apply IPS Patch", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN)) {
|
|
|
|
auto patchData = hex::readFile(this->m_fileBrowser.selected_path);
|
|
|
|
auto patch = hex::loadIPSPatch(patchData);
|
|
|
|
|
|
|
|
for (auto &[address, value] : patch) {
|
2020-12-27 15:39:06 +01:00
|
|
|
provider->write(address, &value, 1);
|
2020-11-29 15:09:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->m_fileBrowser.showFileDialog("Apply IPS32 Patch", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN)) {
|
|
|
|
auto patchData = hex::readFile(this->m_fileBrowser.selected_path);
|
|
|
|
auto patch = hex::loadIPS32Patch(patchData);
|
|
|
|
|
|
|
|
for (auto &[address, value] : patch) {
|
2020-12-27 15:39:06 +01:00
|
|
|
provider->write(address, &value, 1);
|
2020-11-29 15:09:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 13:45:56 +01:00
|
|
|
if (this->m_fileBrowser.showFileDialog("Save As", imgui_addons::ImGuiFileBrowser::DialogMode::SAVE)) {
|
|
|
|
FILE *file = fopen(this->m_fileBrowser.selected_path.c_str(), "wb");
|
|
|
|
|
|
|
|
if (file != nullptr) {
|
|
|
|
std::vector<u8> buffer(0xFF'FFFF, 0x00);
|
|
|
|
size_t bufferSize = buffer.size();
|
|
|
|
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
for (u64 offset = 0; offset < provider->getActualSize(); offset += bufferSize) {
|
|
|
|
if (bufferSize > provider->getActualSize() - offset)
|
|
|
|
bufferSize = provider->getActualSize() - offset;
|
2020-11-27 13:45:56 +01:00
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
provider->read(offset, buffer.data(), bufferSize);
|
2020-11-27 13:45:56 +01:00
|
|
|
fwrite(buffer.data(), 1, bufferSize, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
}
|
2020-11-24 02:00:22 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
void ViewHexEditor::drawMenu() {
|
2020-12-27 15:39:06 +01:00
|
|
|
auto provider = prv::Provider::getCurrentProvider();
|
2020-11-24 02:00:22 +01:00
|
|
|
|
|
|
|
if (ImGui::BeginMenu("File")) {
|
|
|
|
if (ImGui::MenuItem("Open File...", "CTRL + O")) {
|
2020-11-29 01:17:54 +01:00
|
|
|
this->getWindowOpenState() = true;
|
2020-11-24 02:00:22 +01:00
|
|
|
View::doLater([]{ ImGui::OpenPopup("Open File"); });
|
|
|
|
}
|
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
if (ImGui::MenuItem("Save", "CTRL + S", false, provider != nullptr && provider->isWritable())) {
|
|
|
|
for (const auto &[address, value] : provider->getPatches())
|
|
|
|
provider->writeRaw(address, &value, sizeof(u8));
|
2020-11-27 13:45:56 +01:00
|
|
|
}
|
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
if (ImGui::MenuItem("Save As...", "CTRL + SHIFT + S", false, provider != nullptr && provider->isWritable())) {
|
2020-11-27 13:45:56 +01:00
|
|
|
View::doLater([]{ ImGui::OpenPopup("Save As"); });
|
|
|
|
}
|
|
|
|
|
2020-11-30 00:03:12 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
|
|
|
|
if (ImGui::MenuItem("Open Project", "")) {
|
|
|
|
this->getWindowOpenState() = true;
|
|
|
|
View::doLater([]{ ImGui::OpenPopup("Open Project"); });
|
|
|
|
}
|
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
if (ImGui::MenuItem("Save Project", "", false, provider != nullptr && provider->isWritable())) {
|
2020-11-30 00:03:12 +01:00
|
|
|
View::postEvent(Events::ProjectFileStore);
|
|
|
|
|
|
|
|
if (ProjectFile::getProjectFilePath() == "")
|
|
|
|
View::doLater([] { ImGui::OpenPopup("Save Project"); });
|
|
|
|
else
|
|
|
|
ProjectFile::store();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::Separator();
|
|
|
|
|
2020-11-24 02:00:22 +01:00
|
|
|
if (ImGui::BeginMenu("Import...")) {
|
|
|
|
if (ImGui::MenuItem("Base64 File")) {
|
2020-11-29 01:17:54 +01:00
|
|
|
this->getWindowOpenState() = true;
|
2020-11-24 02:00:22 +01:00
|
|
|
View::doLater([]{ ImGui::OpenPopup("Open Base64 File"); });
|
|
|
|
}
|
|
|
|
|
2020-11-29 15:09:36 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
|
|
|
|
if (ImGui::MenuItem("IPS Patch")) {
|
|
|
|
this->getWindowOpenState() = true;
|
|
|
|
View::doLater([]{ ImGui::OpenPopup("Apply IPS Patch"); });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::MenuItem("IPS32 Patch")) {
|
|
|
|
this->getWindowOpenState() = true;
|
|
|
|
View::doLater([]{ ImGui::OpenPopup("Apply IPS32 Patch"); });
|
|
|
|
}
|
|
|
|
|
2020-12-01 02:21:40 +01:00
|
|
|
if (ImGui::MenuItem("File with Loader Script")) {
|
|
|
|
this->getWindowOpenState() = true;
|
|
|
|
this->m_loaderScriptFilePath.clear();
|
|
|
|
this->m_loaderScriptScriptPath.clear();
|
|
|
|
View::doLater([]{ ImGui::OpenPopup("Load File with Loader Script"); });
|
|
|
|
}
|
|
|
|
|
2020-11-24 02:00:22 +01:00
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
if (ImGui::BeginMenu("Export...", provider != nullptr && provider->isWritable())) {
|
2020-11-27 09:09:48 +01:00
|
|
|
if (ImGui::MenuItem("IPS Patch")) {
|
2020-12-27 15:39:06 +01:00
|
|
|
Patches patches = provider->getPatches();
|
2020-11-27 09:09:48 +01:00
|
|
|
if (!patches.contains(0x00454F45) && patches.contains(0x00454F46)) {
|
|
|
|
u8 value = 0;
|
2020-12-27 15:39:06 +01:00
|
|
|
provider->read(0x00454F45, &value, sizeof(u8));
|
2020-11-27 09:09:48 +01:00
|
|
|
patches[0x00454F45] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->m_dataToSave = generateIPSPatch(patches);
|
2020-11-27 13:45:56 +01:00
|
|
|
View::doLater([]{ ImGui::OpenPopup("Export File"); });
|
2020-11-27 09:09:48 +01:00
|
|
|
}
|
|
|
|
if (ImGui::MenuItem("IPS32 Patch")) {
|
2020-12-27 15:39:06 +01:00
|
|
|
Patches patches = provider->getPatches();
|
2020-11-27 09:09:48 +01:00
|
|
|
if (!patches.contains(0x00454F45) && patches.contains(0x45454F46)) {
|
|
|
|
u8 value = 0;
|
2020-12-27 15:39:06 +01:00
|
|
|
provider->read(0x45454F45, &value, sizeof(u8));
|
2020-11-27 09:09:48 +01:00
|
|
|
patches[0x45454F45] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->m_dataToSave = generateIPS32Patch(patches);
|
2020-11-27 13:45:56 +01:00
|
|
|
View::doLater([]{ ImGui::OpenPopup("Export File"); });
|
2020-11-27 09:09:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
|
|
|
|
2020-11-27 13:45:56 +01:00
|
|
|
|
|
|
|
|
2020-11-24 02:00:22 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
|
|
|
|
if (ImGui::MenuItem("Search", "CTRL + F")) {
|
2020-11-29 01:17:54 +01:00
|
|
|
this->getWindowOpenState() = true;
|
2020-11-24 02:00:22 +01:00
|
|
|
View::doLater([]{ ImGui::OpenPopup("Search"); });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::MenuItem("Goto", "CTRL + G")) {
|
2020-11-29 01:17:54 +01:00
|
|
|
this->getWindowOpenState() = true;
|
2020-11-24 02:00:22 +01:00
|
|
|
View::doLater([]{ ImGui::OpenPopup("Goto"); });
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::BeginMenu("Edit")) {
|
|
|
|
if (ImGui::BeginMenu("Copy as...", this->m_memoryEditor.DataPreviewAddr != -1 && this->m_memoryEditor.DataPreviewAddrEnd != -1)) {
|
|
|
|
if (ImGui::MenuItem("Bytes", "CTRL + ALT + C"))
|
|
|
|
this->copyBytes();
|
|
|
|
if (ImGui::MenuItem("Hex String", "CTRL + SHIFT + C"))
|
|
|
|
this->copyString();
|
|
|
|
|
|
|
|
ImGui::Separator();
|
|
|
|
|
|
|
|
if (ImGui::MenuItem("C Array"))
|
|
|
|
this->copyLanguageArray(Language::C);
|
|
|
|
if (ImGui::MenuItem("C++ Array"))
|
|
|
|
this->copyLanguageArray(Language::Cpp);
|
|
|
|
if (ImGui::MenuItem("C# Array"))
|
|
|
|
this->copyLanguageArray(Language::CSharp);
|
|
|
|
if (ImGui::MenuItem("Rust Array"))
|
|
|
|
this->copyLanguageArray(Language::Rust);
|
|
|
|
if (ImGui::MenuItem("Python Array"))
|
|
|
|
this->copyLanguageArray(Language::Python);
|
|
|
|
if (ImGui::MenuItem("Java Array"))
|
|
|
|
this->copyLanguageArray(Language::Java);
|
|
|
|
if (ImGui::MenuItem("JavaScript Array"))
|
|
|
|
this->copyLanguageArray(Language::JavaScript);
|
|
|
|
|
|
|
|
ImGui::Separator();
|
|
|
|
|
|
|
|
if (ImGui::MenuItem("Editor View"))
|
|
|
|
this->copyHexView();
|
|
|
|
if (ImGui::MenuItem("HTML"))
|
|
|
|
this->copyHexViewHTML();
|
|
|
|
|
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
|
|
|
|
2020-11-28 00:33:26 +01:00
|
|
|
if (ImGui::MenuItem("Create bookmark", nullptr, false, this->m_memoryEditor.DataPreviewAddr != -1 && this->m_memoryEditor.DataPreviewAddrEnd != -1)) {
|
|
|
|
size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
|
|
|
|
size_t end = std::max(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
|
2020-12-01 02:21:40 +01:00
|
|
|
Bookmark bookmark = { start, end - start + 1, { }, { } };
|
2020-11-28 00:33:26 +01:00
|
|
|
|
2020-12-01 02:21:40 +01:00
|
|
|
View::postEvent(Events::AddBookmark, &bookmark);
|
2020-11-28 00:33:26 +01:00
|
|
|
}
|
2020-11-24 02:00:22 +01:00
|
|
|
|
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ViewHexEditor::handleShortcut(int key, int mods) {
|
2020-11-27 13:45:56 +01:00
|
|
|
if (mods == GLFW_MOD_CONTROL && key == GLFW_KEY_S) {
|
2020-12-27 15:39:06 +01:00
|
|
|
auto provider = prv::Provider::getCurrentProvider();
|
|
|
|
for (const auto &[address, value] : provider->getPatches())
|
|
|
|
provider->writeRaw(address, &value, sizeof(u8));
|
2020-11-27 13:45:56 +01:00
|
|
|
return true;
|
2020-11-30 00:03:12 +01:00
|
|
|
} else if (mods == (GLFW_MOD_CONTROL | GLFW_MOD_SHIFT) && key == GLFW_KEY_S) {
|
2020-11-27 13:45:56 +01:00
|
|
|
ImGui::OpenPopup("Save As");
|
|
|
|
return true;
|
|
|
|
} else if (mods == GLFW_MOD_CONTROL && key == GLFW_KEY_F) {
|
2020-11-24 02:00:22 +01:00
|
|
|
ImGui::OpenPopup("Search");
|
|
|
|
return true;
|
|
|
|
} else if (mods == GLFW_MOD_CONTROL && key == GLFW_KEY_G) {
|
|
|
|
ImGui::OpenPopup("Goto");
|
|
|
|
return true;
|
|
|
|
} else if (mods == GLFW_MOD_CONTROL && key == GLFW_KEY_O) {
|
|
|
|
ImGui::OpenPopup("Open File");
|
|
|
|
return true;
|
|
|
|
} else if (mods == (GLFW_MOD_CONTROL | GLFW_MOD_ALT) && key == GLFW_KEY_C) {
|
|
|
|
this->copyBytes();
|
|
|
|
return true;
|
|
|
|
} else if (mods == (GLFW_MOD_CONTROL | GLFW_MOD_SHIFT) && key == GLFW_KEY_C) {
|
|
|
|
this->copyString();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2020-11-12 09:38:52 +01:00
|
|
|
}
|
|
|
|
|
2020-11-24 02:00:22 +01:00
|
|
|
|
2020-11-17 13:58:50 +01:00
|
|
|
void ViewHexEditor::openFile(std::string path) {
|
2020-12-27 15:39:06 +01:00
|
|
|
auto& provider = prv::Provider::getCurrentProvider();
|
|
|
|
|
|
|
|
if (provider != nullptr)
|
|
|
|
delete provider;
|
2020-11-17 13:58:50 +01:00
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
provider = new prv::FileProvider(path);
|
|
|
|
this->m_memoryEditor.ReadOnly = !provider->isWritable();
|
2020-11-30 00:03:12 +01:00
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
if (provider->isAvailable())
|
2020-11-30 00:03:12 +01:00
|
|
|
ProjectFile::setFilePath(path);
|
|
|
|
|
2020-12-05 22:10:03 +01:00
|
|
|
this->getWindowOpenState() = true;
|
|
|
|
|
2020-11-27 13:45:27 +01:00
|
|
|
View::postEvent(Events::FileLoaded);
|
2020-11-17 13:58:50 +01:00
|
|
|
View::postEvent(Events::DataChanged);
|
2020-11-30 00:03:12 +01:00
|
|
|
ProjectFile::markDirty();
|
2020-11-17 13:58:50 +01:00
|
|
|
}
|
|
|
|
|
2020-11-24 02:00:22 +01:00
|
|
|
bool ViewHexEditor::saveToFile(std::string path, const std::vector<u8>& data) {
|
|
|
|
FILE *file = fopen(path.c_str(), "wb");
|
|
|
|
|
|
|
|
if (file == nullptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
fwrite(data.data(), 1, data.size(), file);
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ViewHexEditor::loadFromFile(std::string path, std::vector<u8>& data) {
|
|
|
|
FILE *file = fopen(path.c_str(), "rb");
|
|
|
|
|
|
|
|
if (file == nullptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
size_t size = ftello64(file);
|
|
|
|
rewind(file);
|
|
|
|
|
|
|
|
data.resize(size);
|
|
|
|
fread(data.data(), 1, data.size(), file);
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-16 00:07:42 +01:00
|
|
|
void ViewHexEditor::copyBytes() {
|
2020-12-27 15:39:06 +01:00
|
|
|
auto provider = prv::Provider::getCurrentProvider();
|
|
|
|
|
2020-11-17 02:36:12 +01:00
|
|
|
size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
|
|
|
|
size_t end = std::max(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
|
|
|
|
|
|
|
|
size_t copySize = (end - start) + 1;
|
2020-11-16 00:07:42 +01:00
|
|
|
|
|
|
|
std::vector<u8> buffer(copySize, 0x00);
|
2020-12-27 15:39:06 +01:00
|
|
|
provider->read(start, buffer.data(), buffer.size());
|
2020-11-16 00:07:42 +01:00
|
|
|
|
|
|
|
std::string str;
|
|
|
|
for (const auto &byte : buffer)
|
2020-11-20 15:15:43 +01:00
|
|
|
str += hex::format("%02X ", byte);
|
2020-11-16 00:07:42 +01:00
|
|
|
str.pop_back();
|
|
|
|
|
|
|
|
ImGui::SetClipboardText(str.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewHexEditor::copyString() {
|
2020-12-27 15:39:06 +01:00
|
|
|
auto provider = prv::Provider::getCurrentProvider();
|
|
|
|
|
2020-11-17 02:36:12 +01:00
|
|
|
size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
|
|
|
|
size_t end = std::max(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
|
|
|
|
|
|
|
|
size_t copySize = (end - start) + 1;
|
2020-11-16 00:07:42 +01:00
|
|
|
|
2020-11-23 03:00:46 +01:00
|
|
|
std::string buffer(copySize, 0x00);
|
2020-11-16 00:07:42 +01:00
|
|
|
buffer.reserve(copySize + 1);
|
2020-12-27 15:39:06 +01:00
|
|
|
provider->read(start, buffer.data(), copySize);
|
2020-11-16 00:07:42 +01:00
|
|
|
|
|
|
|
ImGui::SetClipboardText(buffer.c_str());
|
|
|
|
}
|
|
|
|
|
2020-11-20 11:57:14 +01:00
|
|
|
void ViewHexEditor::copyLanguageArray(Language language) {
|
2020-12-27 15:39:06 +01:00
|
|
|
auto provider = prv::Provider::getCurrentProvider();
|
|
|
|
|
2020-11-20 11:57:14 +01:00
|
|
|
size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
|
|
|
|
size_t end = std::max(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
|
|
|
|
|
|
|
|
size_t copySize = (end - start) + 1;
|
|
|
|
|
|
|
|
std::vector<u8> buffer(copySize, 0x00);
|
2020-12-27 15:39:06 +01:00
|
|
|
provider->read(start, buffer.data(), buffer.size());
|
2020-11-20 11:57:14 +01:00
|
|
|
|
|
|
|
std::string str;
|
|
|
|
switch (language) {
|
|
|
|
case Language::C:
|
|
|
|
str += "const unsigned char data[" + std::to_string(buffer.size()) + "] = { ";
|
|
|
|
|
|
|
|
for (const auto &byte : buffer)
|
2020-11-20 15:15:43 +01:00
|
|
|
str += hex::format("0x%02X, ", byte);
|
2020-11-20 11:57:14 +01:00
|
|
|
|
|
|
|
// Remove trailing comma
|
|
|
|
str.pop_back();
|
|
|
|
str.pop_back();
|
|
|
|
|
|
|
|
str += " };";
|
|
|
|
break;
|
|
|
|
case Language::Cpp:
|
2020-11-20 11:58:40 +01:00
|
|
|
str += "constexpr std::array<unsigned char, " + std::to_string(buffer.size()) + "> data = { ";
|
2020-11-20 11:57:14 +01:00
|
|
|
|
|
|
|
for (const auto &byte : buffer)
|
2020-11-20 15:15:43 +01:00
|
|
|
str += hex::format("0x%02X, ", byte);
|
2020-11-20 11:57:14 +01:00
|
|
|
|
|
|
|
// Remove trailing comma
|
|
|
|
str.pop_back();
|
|
|
|
str.pop_back();
|
|
|
|
|
|
|
|
str += " };";
|
|
|
|
break;
|
|
|
|
case Language::Java:
|
|
|
|
str += "final byte[] data = { ";
|
|
|
|
|
|
|
|
for (const auto &byte : buffer)
|
2020-11-20 15:15:43 +01:00
|
|
|
str += hex::format("0x%02X, ", byte);
|
2020-11-20 11:57:14 +01:00
|
|
|
|
|
|
|
// Remove trailing comma
|
|
|
|
str.pop_back();
|
|
|
|
str.pop_back();
|
|
|
|
|
|
|
|
str += " };";
|
|
|
|
break;
|
|
|
|
case Language::CSharp:
|
|
|
|
str += "const byte[] data = { ";
|
|
|
|
|
|
|
|
for (const auto &byte : buffer)
|
2020-11-20 15:15:43 +01:00
|
|
|
str += hex::format("0x%02X, ", byte);
|
2020-11-20 11:57:14 +01:00
|
|
|
|
|
|
|
// Remove trailing comma
|
|
|
|
str.pop_back();
|
|
|
|
str.pop_back();
|
|
|
|
|
|
|
|
str += " };";
|
|
|
|
break;
|
|
|
|
case Language::Rust:
|
|
|
|
str += "let data: [u8, " + std::to_string(buffer.size()) + "] = [ ";
|
|
|
|
|
|
|
|
for (const auto &byte : buffer)
|
2020-11-20 15:15:43 +01:00
|
|
|
str += hex::format("0x%02X, ", byte);
|
2020-11-20 11:57:14 +01:00
|
|
|
|
|
|
|
// Remove trailing comma
|
|
|
|
str.pop_back();
|
|
|
|
str.pop_back();
|
|
|
|
|
|
|
|
str += " ];";
|
|
|
|
break;
|
|
|
|
case Language::Python:
|
|
|
|
str += "data = bytes([ ";
|
|
|
|
|
|
|
|
for (const auto &byte : buffer)
|
2020-11-20 15:15:43 +01:00
|
|
|
str += hex::format("0x%02X, ", byte);
|
2020-11-20 11:57:14 +01:00
|
|
|
|
|
|
|
// Remove trailing comma
|
|
|
|
str.pop_back();
|
|
|
|
str.pop_back();
|
|
|
|
|
|
|
|
str += " ]);";
|
|
|
|
break;
|
|
|
|
case Language::JavaScript:
|
|
|
|
str += "const data = new Uint8Array([ ";
|
|
|
|
|
|
|
|
for (const auto &byte : buffer)
|
2020-11-20 15:15:43 +01:00
|
|
|
str += hex::format("0x%02X, ", byte);
|
2020-11-20 11:57:14 +01:00
|
|
|
|
|
|
|
// Remove trailing comma
|
|
|
|
str.pop_back();
|
|
|
|
str.pop_back();
|
|
|
|
|
|
|
|
str += " ]);";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::SetClipboardText(str.c_str());
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:25:55 +01:00
|
|
|
void ViewHexEditor::copyHexView() {
|
2020-12-27 15:39:06 +01:00
|
|
|
auto provider = prv::Provider::getCurrentProvider();
|
|
|
|
|
2020-11-20 13:25:55 +01:00
|
|
|
size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
|
|
|
|
size_t end = std::max(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
|
|
|
|
|
|
|
|
size_t copySize = (end - start) + 1;
|
|
|
|
|
|
|
|
std::vector<u8> buffer(copySize, 0x00);
|
2020-12-27 15:39:06 +01:00
|
|
|
provider->read(start, buffer.data(), buffer.size());
|
2020-11-20 13:25:55 +01:00
|
|
|
|
|
|
|
std::string str = "Hex View 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n\n";
|
|
|
|
|
|
|
|
|
|
|
|
for (u32 col = start >> 4; col <= (end >> 4); col++) {
|
2020-11-20 15:15:43 +01:00
|
|
|
str += hex::format("%08lX ", col << 4);
|
2020-11-20 13:25:55 +01:00
|
|
|
for (u64 i = 0 ; i < 16; i++) {
|
|
|
|
|
|
|
|
if (col == (start >> 4) && i < (start & 0xF) || col == (end >> 4) && i > (end & 0xF))
|
|
|
|
str += " ";
|
|
|
|
else
|
2020-11-20 15:15:43 +01:00
|
|
|
str += hex::format("%02lX ", buffer[((col << 4) - start) + i]);
|
2020-11-20 13:25:55 +01:00
|
|
|
|
|
|
|
if ((i & 0xF) == 0x7)
|
|
|
|
str += " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
str += " ";
|
|
|
|
|
|
|
|
for (u64 i = 0 ; i < 16; i++) {
|
|
|
|
|
|
|
|
if (col == (start >> 4) && i < (start & 0xF) || col == (end >> 4) && i > (end & 0xF))
|
|
|
|
str += " ";
|
|
|
|
else {
|
|
|
|
u8 c = buffer[((col << 4) - start) + i];
|
|
|
|
char displayChar = (c < 32 || c >= 128) ? '.' : c;
|
|
|
|
str += hex::format("%c", displayChar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
str += "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ImGui::SetClipboardText(str.c_str());
|
|
|
|
}
|
|
|
|
|
2020-11-20 15:15:43 +01:00
|
|
|
void ViewHexEditor::copyHexViewHTML() {
|
2020-12-27 15:39:06 +01:00
|
|
|
auto provider = prv::Provider::getCurrentProvider();
|
|
|
|
|
2020-11-20 15:15:43 +01:00
|
|
|
size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
|
|
|
|
size_t end = std::max(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
|
|
|
|
|
|
|
|
size_t copySize = (end - start) + 1;
|
|
|
|
|
|
|
|
std::vector<u8> buffer(copySize, 0x00);
|
2020-12-27 15:39:06 +01:00
|
|
|
provider->read(start, buffer.data(), buffer.size());
|
2020-11-20 15:15:43 +01:00
|
|
|
|
|
|
|
std::string str =
|
|
|
|
R"(
|
|
|
|
<div>
|
|
|
|
<style type="text/css">
|
|
|
|
.offsetheader { color:#0000A0; line-height:200% }
|
|
|
|
.offsetcolumn { color:#0000A0 }
|
|
|
|
.hexcolumn { color:#000000 }
|
|
|
|
.textcolumn { color:#000000 }
|
|
|
|
</style>
|
|
|
|
|
|
|
|
<code>
|
|
|
|
<span class="offsetheader">Hex View  00 01 02 03 04 05 06 07  08 09 0A 0B 0C 0D 0E 0F</span><br/>
|
|
|
|
)";
|
|
|
|
|
|
|
|
|
|
|
|
for (u32 col = start >> 4; col <= (end >> 4); col++) {
|
|
|
|
str += hex::format(" <span class=\"offsetcolumn\">%08lX</span>  <span class=\"hexcolumn\">", col << 4);
|
|
|
|
for (u64 i = 0 ; i < 16; i++) {
|
|
|
|
|
|
|
|
if (col == (start >> 4) && i < (start & 0xF) || col == (end >> 4) && i > (end & 0xF))
|
|
|
|
str += "   ";
|
|
|
|
else
|
|
|
|
str += hex::format("%02lX ", buffer[((col << 4) - start) + i]);
|
|
|
|
|
|
|
|
if ((i & 0xF) == 0x7)
|
|
|
|
str += " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
str += "</span>  <span class=\"textcolumn\">";
|
|
|
|
|
|
|
|
for (u64 i = 0 ; i < 16; i++) {
|
|
|
|
|
|
|
|
if (col == (start >> 4) && i < (start & 0xF) || col == (end >> 4) && i > (end & 0xF))
|
|
|
|
str += " ";
|
|
|
|
else {
|
|
|
|
u8 c = buffer[((col << 4) - start) + i];
|
|
|
|
char displayChar = (c < 32 || c >= 128) ? '.' : c;
|
|
|
|
str += hex::format("%c", displayChar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
str += "</span><br/>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
str +=
|
|
|
|
R"(
|
|
|
|
</code>
|
|
|
|
</div>
|
|
|
|
)";
|
|
|
|
|
|
|
|
|
|
|
|
ImGui::SetClipboardText(str.c_str());
|
|
|
|
}
|
|
|
|
|
2020-11-15 23:04:46 +01:00
|
|
|
static std::vector<std::pair<u64, u64>> findString(prv::Provider* &provider, std::string string) {
|
|
|
|
std::vector<std::pair<u64, u64>> results;
|
|
|
|
|
|
|
|
u32 foundCharacters = 0;
|
|
|
|
|
|
|
|
std::vector<u8> buffer(1024, 0x00);
|
|
|
|
size_t dataSize = provider->getSize();
|
|
|
|
for (u64 offset = 0; offset < dataSize; offset += 1024) {
|
2020-12-06 13:48:56 +01:00
|
|
|
size_t usedBufferSize = std::min(u64(buffer.size()), dataSize - offset);
|
2020-11-15 23:04:46 +01:00
|
|
|
provider->read(offset, buffer.data(), usedBufferSize);
|
|
|
|
|
|
|
|
for (u64 i = 0; i < usedBufferSize; i++) {
|
|
|
|
if (buffer[i] == string[foundCharacters])
|
|
|
|
foundCharacters++;
|
|
|
|
else
|
|
|
|
foundCharacters = 0;
|
|
|
|
|
|
|
|
if (foundCharacters == string.size()) {
|
|
|
|
results.emplace_back(offset + i - foundCharacters + 1, offset + i + 1);
|
|
|
|
foundCharacters = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::vector<std::pair<u64, u64>> findHex(prv::Provider* &provider, std::string string) {
|
|
|
|
std::vector<std::pair<u64, u64>> results;
|
|
|
|
|
|
|
|
if ((string.size() % 2) == 1)
|
|
|
|
string = "0" + string;
|
|
|
|
|
|
|
|
std::vector<u8> hex;
|
|
|
|
hex.reserve(string.size() / 2);
|
|
|
|
|
|
|
|
for (u32 i = 0; i < string.size(); i += 2) {
|
|
|
|
char byte[3] = { string[i], string[i + 1], 0 };
|
|
|
|
hex.push_back(strtoul(byte, nullptr, 16));
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 foundCharacters = 0;
|
|
|
|
|
|
|
|
std::vector<u8> buffer(1024, 0x00);
|
|
|
|
size_t dataSize = provider->getSize();
|
|
|
|
for (u64 offset = 0; offset < dataSize; offset += 1024) {
|
2020-12-06 13:48:56 +01:00
|
|
|
size_t usedBufferSize = std::min(u64(buffer.size()), dataSize - offset);
|
2020-11-15 23:04:46 +01:00
|
|
|
provider->read(offset, buffer.data(), usedBufferSize);
|
|
|
|
|
|
|
|
for (u64 i = 0; i < usedBufferSize; i++) {
|
|
|
|
if (buffer[i] == hex[foundCharacters])
|
|
|
|
foundCharacters++;
|
|
|
|
else
|
|
|
|
foundCharacters = 0;
|
|
|
|
|
|
|
|
if (foundCharacters == hex.size()) {
|
|
|
|
results.emplace_back(offset + i - foundCharacters + 1, offset + i + 1);
|
|
|
|
foundCharacters = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-12 09:38:52 +01:00
|
|
|
void ViewHexEditor::drawSearchPopup() {
|
2020-11-15 23:04:46 +01:00
|
|
|
static auto InputCallback = [](ImGuiInputTextCallbackData* data) -> int {
|
|
|
|
auto _this = static_cast<ViewHexEditor*>(data->UserData);
|
2020-12-27 15:39:06 +01:00
|
|
|
auto provider = prv::Provider::getCurrentProvider();
|
2020-11-12 09:38:52 +01:00
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
*_this->m_lastSearchBuffer = _this->m_searchFunction(provider, data->Buf);
|
2020-11-15 23:04:46 +01:00
|
|
|
_this->m_lastSearchIndex = 0;
|
2020-11-11 14:42:01 +01:00
|
|
|
|
2020-11-15 23:04:46 +01:00
|
|
|
if (_this->m_lastSearchBuffer->size() > 0)
|
|
|
|
_this->m_memoryEditor.GotoAddrAndHighlight((*_this->m_lastSearchBuffer)[0].first, (*_this->m_lastSearchBuffer)[0].second);
|
2020-11-11 14:42:01 +01:00
|
|
|
|
2020-11-15 23:04:46 +01:00
|
|
|
return 0;
|
|
|
|
};
|
2020-11-11 14:42:01 +01:00
|
|
|
|
2020-11-15 23:04:46 +01:00
|
|
|
static auto Find = [this](char *buffer) {
|
2020-12-27 15:39:06 +01:00
|
|
|
auto provider = prv::Provider::getCurrentProvider();
|
|
|
|
|
|
|
|
*this->m_lastSearchBuffer = this->m_searchFunction(provider, buffer);
|
2020-11-15 23:04:46 +01:00
|
|
|
this->m_lastSearchIndex = 0;
|
2020-11-11 14:42:01 +01:00
|
|
|
|
2020-11-15 23:04:46 +01:00
|
|
|
if (this->m_lastSearchBuffer->size() > 0)
|
|
|
|
this->m_memoryEditor.GotoAddrAndHighlight((*this->m_lastSearchBuffer)[0].first, (*this->m_lastSearchBuffer)[0].second);
|
|
|
|
};
|
2020-11-11 14:42:01 +01:00
|
|
|
|
2020-11-15 23:04:46 +01:00
|
|
|
static auto FindNext = [this]() {
|
|
|
|
if (this->m_lastSearchBuffer->size() > 0) {
|
|
|
|
++this->m_lastSearchIndex %= this->m_lastSearchBuffer->size();
|
|
|
|
this->m_memoryEditor.GotoAddrAndHighlight((*this->m_lastSearchBuffer)[this->m_lastSearchIndex].first,
|
|
|
|
(*this->m_lastSearchBuffer)[this->m_lastSearchIndex].second);
|
2020-11-11 14:42:01 +01:00
|
|
|
}
|
2020-11-15 23:04:46 +01:00
|
|
|
};
|
2020-11-11 14:42:01 +01:00
|
|
|
|
2020-11-15 23:04:46 +01:00
|
|
|
static auto FindPrevious = [this]() {
|
|
|
|
if (this->m_lastSearchBuffer->size() > 0) {
|
|
|
|
this->m_lastSearchIndex--;
|
2020-11-11 14:42:01 +01:00
|
|
|
|
2020-11-15 23:04:46 +01:00
|
|
|
if (this->m_lastSearchIndex < 0)
|
|
|
|
this->m_lastSearchIndex = this->m_lastSearchBuffer->size() - 1;
|
|
|
|
|
|
|
|
this->m_lastSearchIndex %= this->m_lastSearchBuffer->size();
|
|
|
|
|
|
|
|
this->m_memoryEditor.GotoAddrAndHighlight((*this->m_lastSearchBuffer)[this->m_lastSearchIndex].first,
|
|
|
|
(*this->m_lastSearchBuffer)[this->m_lastSearchIndex].second);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (ImGui::BeginPopup("Search")) {
|
|
|
|
ImGui::TextUnformatted("Search");
|
|
|
|
if (ImGui::BeginTabBar("searchTabs")) {
|
|
|
|
char *currBuffer;
|
|
|
|
if (ImGui::BeginTabItem("String")) {
|
|
|
|
this->m_searchFunction = findString;
|
|
|
|
this->m_lastSearchBuffer = &this->m_lastStringSearch;
|
|
|
|
currBuffer = this->m_searchStringBuffer;
|
|
|
|
|
|
|
|
ImGui::InputText("##nolabel", currBuffer, 0xFFFF, ImGuiInputTextFlags_CallbackCompletion,
|
|
|
|
InputCallback, this);
|
|
|
|
ImGui::EndTabItem();
|
2020-11-11 14:42:01 +01:00
|
|
|
}
|
|
|
|
|
2020-11-15 23:04:46 +01:00
|
|
|
if (ImGui::BeginTabItem("Hex")) {
|
|
|
|
this->m_searchFunction = findHex;
|
|
|
|
this->m_lastSearchBuffer = &this->m_lastHexSearch;
|
|
|
|
currBuffer = this->m_searchHexBuffer;
|
|
|
|
|
|
|
|
ImGui::InputText("##nolabel", currBuffer, 0xFFFF,
|
|
|
|
ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CallbackCompletion,
|
|
|
|
InputCallback, this);
|
|
|
|
ImGui::EndTabItem();
|
|
|
|
}
|
2020-11-11 14:42:01 +01:00
|
|
|
|
2020-11-15 23:04:46 +01:00
|
|
|
if (ImGui::Button("Find"))
|
|
|
|
Find(currBuffer);
|
2020-11-11 14:42:01 +01:00
|
|
|
|
2020-11-15 23:04:46 +01:00
|
|
|
if (this->m_lastSearchBuffer->size() > 0) {
|
|
|
|
if ((ImGui::Button("Find Next")))
|
|
|
|
FindNext();
|
2020-11-11 14:42:01 +01:00
|
|
|
|
2020-11-15 23:04:46 +01:00
|
|
|
ImGui::SameLine();
|
2020-11-11 14:42:01 +01:00
|
|
|
|
2020-11-15 23:04:46 +01:00
|
|
|
if ((ImGui::Button("Find Prev")))
|
|
|
|
FindPrevious();
|
2020-11-11 14:42:01 +01:00
|
|
|
}
|
2020-11-15 23:04:46 +01:00
|
|
|
|
|
|
|
ImGui::EndTabBar();
|
2020-11-11 14:42:01 +01:00
|
|
|
}
|
|
|
|
|
2020-11-15 23:04:46 +01:00
|
|
|
|
2020-11-11 14:42:01 +01:00
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
|
2020-11-12 09:38:52 +01:00
|
|
|
void ViewHexEditor::drawGotoPopup() {
|
2020-12-27 15:39:06 +01:00
|
|
|
auto provider = prv::Provider::getCurrentProvider();
|
|
|
|
|
2020-11-12 09:38:52 +01:00
|
|
|
if (ImGui::BeginPopup("Goto")) {
|
|
|
|
ImGui::TextUnformatted("Goto");
|
2020-11-19 23:24:34 +01:00
|
|
|
if (ImGui::BeginTabBar("gotoTabs")) {
|
|
|
|
s64 newOffset = 0;
|
|
|
|
if (ImGui::BeginTabItem("Begin")) {
|
|
|
|
ImGui::InputScalar("##nolabel", ImGuiDataType_U64, &this->m_gotoAddress, nullptr, nullptr, "%llx", ImGuiInputTextFlags_CharsHexadecimal);
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
if (this->m_gotoAddress >= provider->getActualSize())
|
|
|
|
this->m_gotoAddress = provider->getActualSize() - 1;
|
2020-11-10 21:31:04 +01:00
|
|
|
|
2020-11-19 23:24:34 +01:00
|
|
|
newOffset = this->m_gotoAddress;
|
|
|
|
|
|
|
|
ImGui::EndTabItem();
|
|
|
|
}
|
|
|
|
if (ImGui::BeginTabItem("Current")) {
|
|
|
|
ImGui::InputScalar("##nolabel", ImGuiDataType_S64, &this->m_gotoAddress, nullptr, nullptr, "%llx", ImGuiInputTextFlags_CharsHexadecimal);
|
|
|
|
|
|
|
|
if (this->m_memoryEditor.DataPreviewAddr == -1 || this->m_memoryEditor.DataPreviewAddrEnd == -1) {
|
|
|
|
this->m_memoryEditor.DataPreviewAddr = 0;
|
|
|
|
this->m_memoryEditor.DataPreviewAddrEnd = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
s64 currHighlightStart = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
|
|
|
|
|
|
|
|
newOffset = this->m_gotoAddress + currHighlightStart;
|
2020-12-27 15:39:06 +01:00
|
|
|
if (newOffset >= provider->getActualSize()) {
|
|
|
|
newOffset = provider->getActualSize() - 1;
|
|
|
|
this->m_gotoAddress = (provider->getActualSize() - 1) - currHighlightStart;
|
2020-11-19 23:24:34 +01:00
|
|
|
} else if (newOffset < 0) {
|
|
|
|
newOffset = 0;
|
|
|
|
this->m_gotoAddress = -currHighlightStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndTabItem();
|
|
|
|
}
|
|
|
|
if (ImGui::BeginTabItem("End")) {
|
|
|
|
ImGui::InputScalar("##nolabel", ImGuiDataType_U64, &this->m_gotoAddress, nullptr, nullptr, "%llx", ImGuiInputTextFlags_CharsHexadecimal);
|
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
if (this->m_gotoAddress >= provider->getActualSize())
|
|
|
|
this->m_gotoAddress = provider->getActualSize() - 1;
|
2020-11-19 23:24:34 +01:00
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
newOffset = (provider->getActualSize() - 1) - this->m_gotoAddress;
|
2020-11-19 23:24:34 +01:00
|
|
|
|
|
|
|
ImGui::EndTabItem();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::Button("Goto")) {
|
2020-12-27 15:39:06 +01:00
|
|
|
provider->setCurrentPage(std::floor(newOffset / double(prv::Provider::PageSize)));
|
2020-11-19 23:24:34 +01:00
|
|
|
this->m_memoryEditor.GotoAddr = newOffset;
|
|
|
|
this->m_memoryEditor.DataPreviewAddr = newOffset;
|
|
|
|
this->m_memoryEditor.DataPreviewAddrEnd = newOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndTabBar();
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|
|
|
|
|
2020-11-12 09:38:52 +01:00
|
|
|
ImGui::EndPopup();
|
2020-11-11 14:42:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 21:31:04 +01:00
|
|
|
}
|