2021-09-03 02:34:40 +02:00
|
|
|
#include "views/view_store.hpp"
|
2021-08-29 11:10:48 +02:00
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
#include <imgui.h>
|
|
|
|
#define IMGUI_DEFINE_MATH_OPERATORS
|
|
|
|
#include <imgui_internal.h>
|
|
|
|
|
|
|
|
#include <hex/helpers/utils.hpp>
|
|
|
|
#include <hex/helpers/crypto.hpp>
|
|
|
|
#include <hex/helpers/logger.hpp>
|
2021-09-08 17:22:54 +02:00
|
|
|
#include <hex/helpers/magic.hpp>
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <filesystem>
|
2021-09-08 17:22:54 +02:00
|
|
|
#include <functional>
|
2021-09-03 02:34:40 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
2021-09-23 22:56:49 +02:00
|
|
|
#include <microtar.h>
|
|
|
|
#include <hex/helpers/file.hpp>
|
2021-08-29 11:10:48 +02:00
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
using namespace std::literals::string_literals;
|
|
|
|
using namespace std::literals::chrono_literals;
|
|
|
|
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
|
|
|
ViewStore::ViewStore() : View("hex.view.store.name") {
|
|
|
|
this->refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
ViewStore::~ViewStore() { }
|
|
|
|
|
|
|
|
void ViewStore::drawStore() {
|
2021-09-08 17:22:54 +02:00
|
|
|
ImGui::Header("hex.view.store.desc"_lang, true);
|
2021-09-03 02:34:40 +02:00
|
|
|
|
2021-09-08 17:22:54 +02:00
|
|
|
if (ImGui::Button("hex.view.store.reload"_lang)) {
|
2021-09-03 02:34:40 +02:00
|
|
|
this->refresh();
|
|
|
|
}
|
|
|
|
|
2021-09-23 22:56:49 +02:00
|
|
|
auto drawTab = [this](auto title, ImHexPath pathType, auto &content, std::function<void(const StoreEntry&)> downloadDoneCallback) {
|
2021-09-03 02:34:40 +02:00
|
|
|
if (ImGui::BeginTabItem(title)) {
|
2021-09-08 17:22:54 +02:00
|
|
|
if (ImGui::BeginTable("##pattern_language", 3, ImGuiTableFlags_ScrollY | ImGuiTableFlags_Borders | ImGuiTableFlags_SizingStretchSame | ImGuiTableFlags_RowBg)) {
|
2021-09-03 02:34:40 +02:00
|
|
|
ImGui::TableSetupScrollFreeze(0, 1);
|
2021-09-09 12:58:44 +02:00
|
|
|
ImGui::TableSetupColumn("hex.view.store.row.name"_lang, ImGuiTableColumnFlags_WidthFixed);
|
|
|
|
ImGui::TableSetupColumn("hex.view.store.row.description"_lang, ImGuiTableColumnFlags_None);
|
2021-09-08 17:22:54 +02:00
|
|
|
ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed);
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
ImGui::TableHeadersRow();
|
|
|
|
|
2021-09-09 01:57:11 +02:00
|
|
|
u32 id = 1;
|
2021-09-03 02:34:40 +02:00
|
|
|
for (auto &entry : content) {
|
2021-09-08 17:22:54 +02:00
|
|
|
ImGui::TableNextRow(ImGuiTableRowFlags_None, ImGui::GetTextLineHeight() + 4.0F * ImGui::GetStyle().FramePadding.y);
|
2021-09-03 02:34:40 +02:00
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextUnformatted(entry.name.c_str());
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextUnformatted(entry.description.c_str());
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
2021-09-09 01:57:11 +02:00
|
|
|
ImGui::PushID(id);
|
2021-09-03 02:34:40 +02:00
|
|
|
ImGui::BeginDisabled(this->m_download.valid() && this->m_download.wait_for(0s) != std::future_status::ready);
|
|
|
|
{
|
|
|
|
if (entry.downloading) {
|
2021-09-08 17:22:54 +02:00
|
|
|
ImGui::TextSpinner("");
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
if (this->m_download.valid() && this->m_download.wait_for(0s) == std::future_status::ready) {
|
|
|
|
entry.downloading = false;
|
|
|
|
|
|
|
|
auto response = this->m_download.get();
|
|
|
|
if (response.code == 200) {
|
|
|
|
entry.installed = true;
|
|
|
|
entry.hasUpdate = false;
|
2021-09-23 22:56:49 +02:00
|
|
|
|
|
|
|
if (entry.isFolder) {
|
|
|
|
mtar_t ctx;
|
|
|
|
mtar_open(&ctx, this->m_downloadPath.string().c_str(), "r");
|
|
|
|
|
|
|
|
mtar_header_t header;
|
|
|
|
auto extractBasePath = this->m_downloadPath.parent_path() / this->m_downloadPath.stem();
|
|
|
|
while (mtar_read_header(&ctx, &header) != MTAR_ENULLRECORD) {
|
|
|
|
auto filePath = extractBasePath / fs::path(header.name);
|
|
|
|
fs::create_directories(filePath.parent_path());
|
|
|
|
File outputFile(filePath.string(), File::Mode::Create);
|
|
|
|
|
|
|
|
std::vector<u8> buffer(0x10000);
|
|
|
|
for (u64 offset = 0; offset < header.size; offset += buffer.size()) {
|
|
|
|
auto readSize = std::min(buffer.size(), header.size - offset);
|
|
|
|
mtar_read_data(&ctx, buffer.data(), readSize);
|
|
|
|
|
|
|
|
buffer.resize(readSize);
|
|
|
|
outputFile.write(buffer);
|
|
|
|
}
|
|
|
|
mtar_next(&ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
mtar_finalize(&ctx);
|
|
|
|
mtar_close(&ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadDoneCallback(entry);
|
2021-09-03 02:34:40 +02:00
|
|
|
} else
|
|
|
|
log::error("Download failed!");
|
2021-08-29 11:10:48 +02:00
|
|
|
|
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
this->m_download = { };
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (entry.hasUpdate) {
|
2021-09-08 17:22:54 +02:00
|
|
|
if (ImGui::Button("hex.view.store.update"_lang)) {
|
2021-09-03 02:34:40 +02:00
|
|
|
this->download(pathType, entry.fileName, entry.link, true);
|
|
|
|
entry.downloading = true;
|
|
|
|
}
|
|
|
|
} else if (!entry.installed) {
|
2021-09-08 17:22:54 +02:00
|
|
|
if (ImGui::Button("hex.view.store.download"_lang)) {
|
2021-09-03 02:34:40 +02:00
|
|
|
this->download(pathType, entry.fileName, entry.link, false);
|
|
|
|
entry.downloading = true;
|
|
|
|
}
|
|
|
|
} else {
|
2021-09-08 17:22:54 +02:00
|
|
|
if (ImGui::Button("hex.view.store.remove"_lang)) {
|
2021-09-03 02:34:40 +02:00
|
|
|
this->remove(pathType, entry.fileName);
|
|
|
|
entry.installed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::EndDisabled();
|
2021-09-09 01:57:11 +02:00
|
|
|
ImGui::PopID();
|
|
|
|
id++;
|
2021-09-03 02:34:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndTable();
|
2021-08-29 11:10:48 +02:00
|
|
|
}
|
2021-09-03 02:34:40 +02:00
|
|
|
ImGui::EndTabItem();
|
2021-08-29 11:10:48 +02:00
|
|
|
}
|
2021-09-03 02:34:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (ImGui::BeginTabBar("storeTabs")) {
|
2021-09-23 22:56:49 +02:00
|
|
|
auto extractTar = []{
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
drawTab("hex.view.store.tab.patterns"_lang, ImHexPath::Patterns, this->m_patterns, [](auto){});
|
|
|
|
drawTab("hex.view.store.tab.libraries"_lang, ImHexPath::PatternsInclude, this->m_includes, [](auto){});
|
|
|
|
drawTab("hex.view.store.tab.magics"_lang, ImHexPath::Magic, this->m_magics, [](auto){
|
2021-09-08 17:22:54 +02:00
|
|
|
magic::compile();
|
|
|
|
});
|
2021-09-23 22:56:49 +02:00
|
|
|
drawTab("hex.view.store.tab.constants"_lang, ImHexPath::Constants, this->m_constants, [](auto){});
|
|
|
|
drawTab("hex.view.store.tab.yara"_lang, ImHexPath::Yara, this->m_yara, [](auto){});
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
ImGui::EndTabBar();
|
2021-08-29 11:10:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
void ViewStore::refresh() {
|
|
|
|
this->m_patterns.clear();
|
|
|
|
this->m_includes.clear();
|
|
|
|
this->m_magics.clear();
|
2021-09-10 21:16:08 +02:00
|
|
|
this->m_constants.clear();
|
2021-09-23 22:56:49 +02:00
|
|
|
this->m_yara.clear();
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
this->m_apiRequest = this->m_net.getString(ImHexApiURL + "/store"s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewStore::parseResponse() {
|
|
|
|
auto response = this->m_apiRequest.get();
|
2021-09-08 17:22:54 +02:00
|
|
|
if (response.code == 200) {
|
2021-09-03 02:34:40 +02:00
|
|
|
auto json = nlohmann::json::parse(response.body);
|
|
|
|
|
|
|
|
auto parseStoreEntries = [](auto storeJson, const std::string &name, ImHexPath pathType, std::vector<StoreEntry> &results) {
|
|
|
|
// Check if the response handles the type of files
|
|
|
|
if (storeJson.contains(name)) {
|
|
|
|
|
|
|
|
for (auto &entry : storeJson[name]) {
|
|
|
|
|
|
|
|
// Check if entry is valid
|
2021-09-23 22:56:49 +02:00
|
|
|
if (entry.contains("name") && entry.contains("desc") && entry.contains("file") && entry.contains("url") && entry.contains("hash") && entry.contains("folder")) {
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
// Parse entry
|
2021-09-23 22:56:49 +02:00
|
|
|
StoreEntry storeEntry = { entry["name"], entry["desc"], entry["file"], entry["url"], entry["hash"], entry["folder"],false, false, false };
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
// Check if file is installed already or has an update available
|
2021-09-09 12:58:44 +02:00
|
|
|
for (const auto &folder : hex::getPath(pathType)) {
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
auto path = folder / fs::path(storeEntry.fileName);
|
|
|
|
|
|
|
|
if (fs::exists(path)) {
|
|
|
|
storeEntry.installed = true;
|
2021-08-29 11:10:48 +02:00
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
std::ifstream file(path, std::ios::in | std::ios::binary);
|
|
|
|
std::vector<u8> data(fs::file_size(path), 0x00);
|
|
|
|
file.read(reinterpret_cast<char*>(data.data()), data.size());
|
|
|
|
|
|
|
|
auto fileHash = crypt::sha256(data);
|
|
|
|
|
|
|
|
// Compare installed file hash with hash of repo file
|
|
|
|
if (std::vector(fileHash.begin(), fileHash.end()) != crypt::decode16(storeEntry.hash))
|
|
|
|
storeEntry.hasUpdate = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
results.push_back(storeEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-08 17:22:54 +02:00
|
|
|
parseStoreEntries(json, "patterns", ImHexPath::Patterns, this->m_patterns);
|
2021-09-03 02:34:40 +02:00
|
|
|
parseStoreEntries(json, "includes", ImHexPath::PatternsInclude, this->m_includes);
|
|
|
|
parseStoreEntries(json, "magic", ImHexPath::Magic, this->m_magics);
|
2021-09-09 12:58:44 +02:00
|
|
|
parseStoreEntries(json, "constants", ImHexPath::Constants, this->m_constants);
|
2021-09-23 22:56:49 +02:00
|
|
|
parseStoreEntries(json, "yara", ImHexPath::Yara, this->m_yara);
|
2021-09-09 12:58:44 +02:00
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
}
|
2021-09-08 17:22:54 +02:00
|
|
|
this->m_apiRequest = { };
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewStore::drawContent() {
|
|
|
|
ImGui::SetNextWindowSizeConstraints(ImVec2(600, 400) * SharedData::globalScale, ImVec2(FLT_MAX, FLT_MAX));
|
|
|
|
if (ImGui::BeginPopupModal(View::toWindowName("hex.view.store.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_AlwaysAutoResize)) {
|
|
|
|
if (this->m_apiRequest.valid()) {
|
|
|
|
if (this->m_apiRequest.wait_for(0s) != std::future_status::ready)
|
2021-09-08 17:22:54 +02:00
|
|
|
ImGui::TextSpinner("hex.view.store.loading"_lang);
|
2021-09-03 02:34:40 +02:00
|
|
|
else
|
|
|
|
this->parseResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
this->drawStore();
|
|
|
|
|
|
|
|
ImGui::EndPopup();
|
|
|
|
} else {
|
|
|
|
this->getWindowOpenState() = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewStore::drawMenu() {
|
|
|
|
if (ImGui::BeginMenu("hex.menu.help"_lang)) {
|
|
|
|
if (ImGui::MenuItem("hex.view.store.name"_lang)) {
|
|
|
|
View::doLater([]{ ImGui::OpenPopup(View::toWindowName("hex.view.store.name").c_str()); });
|
|
|
|
this->getWindowOpenState() = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ViewStore::download(ImHexPath pathType, const std::string &fileName, const std::string &url, bool update) {
|
|
|
|
if (!update) {
|
2021-09-23 22:56:49 +02:00
|
|
|
this->m_downloadPath = hex::getPath(pathType).front() / fs::path(fileName);
|
|
|
|
this->m_download = this->m_net.downloadFile(url, this->m_downloadPath);
|
2021-09-03 02:34:40 +02:00
|
|
|
} else {
|
|
|
|
for (const auto &path : hex::getPath(pathType)) {
|
|
|
|
auto fullPath = path / fs::path(fileName);
|
|
|
|
if (fs::exists(fullPath)) {
|
2021-09-23 22:56:49 +02:00
|
|
|
this->m_downloadPath = fullPath;
|
2021-09-03 02:34:40 +02:00
|
|
|
this->m_download = this->m_net.downloadFile(url, fullPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewStore::remove(ImHexPath pathType, const std::string &fileName) {
|
|
|
|
for (const auto &path : hex::getPath(pathType))
|
|
|
|
fs::remove(path / fs::path(fileName));
|
2021-08-29 11:10:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|