feat: Added Base64 provider

This commit is contained in:
WerWolv
2023-12-26 23:42:22 +01:00
parent 96fe608d60
commit 83fa024fab
15 changed files with 95 additions and 68 deletions

View File

@@ -73,37 +73,6 @@ namespace hex::plugin::builtin {
// Import
namespace {
void importBase64() {
fs::openFileBrowser(fs::DialogMode::Open, {}, [](const auto &path) {
wolv::io::File inputFile(path, wolv::io::File::Mode::Read);
if (!inputFile.isValid()) {
ui::ToastError::open("hex.builtin.menu.file.import.base64.popup.open_error"_lang);
return;
}
auto base64 = inputFile.readVector();
if (!base64.empty()) {
auto data = crypt::decode64(base64);
if (data.empty())
ui::ToastError::open("hex.builtin.menu.file.import.base64.popup.import_error"_lang);
else {
fs::openFileBrowser(fs::DialogMode::Save, {}, [&data](const std::fs::path &path) {
wolv::io::File outputFile(path, wolv::io::File::Mode::Create);
if (!outputFile.isValid())
ui::ToastError::open("hex.builtin.menu.file.import.base64.popup.import_error"_lang);
outputFile.writeVector(data);
});
}
} else {
ui::ToastError::open("hex.builtin.popup.file_open_error"_lang);
}
});
}
void importIPSPatch() {
fs::openFileBrowser(fs::DialogMode::Open, {}, [](const auto &path) {
TaskManager::createTask("hex.ui.common.processing", TaskManager::NoProgress, [path](auto &task) {
@@ -422,14 +391,6 @@ namespace hex::plugin::builtin {
/* Import */
{
/* Base 64 */
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.file", "hex.builtin.menu.file.import", "hex.builtin.menu.file.import.base64" }, 2050,
Shortcut::None,
importBase64,
noRunningTaskAndWritableProvider);
ContentRegistry::Interface::addMenuItemSeparator({ "hex.builtin.menu.file", "hex.builtin.menu.file.import" }, 2100);
/* IPS */
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.file", "hex.builtin.menu.file.import", "hex.builtin.menu.file.import.ips"}, 2150,
Shortcut::None,

View File

@@ -9,6 +9,7 @@
#include "content/providers/memory_file_provider.hpp"
#include "content/providers/view_provider.hpp"
#include <content/providers/process_memory_provider.hpp>
#include <content/providers/base64_provider.hpp>
#include <popups/popup_notification.hpp>
#include "content/helpers/notification.hpp"
@@ -32,6 +33,7 @@ namespace hex::plugin::builtin {
ContentRegistry::Provider::add<GDBProvider>();
ContentRegistry::Provider::add<IntelHexProvider>();
ContentRegistry::Provider::add<MotorolaSRECProvider>();
ContentRegistry::Provider::add<Base64Provider>();
ContentRegistry::Provider::add<MemoryFileProvider>(false);
ContentRegistry::Provider::add<ViewProvider>(false);

View File

@@ -0,0 +1,65 @@
#include <content/providers/base64_provider.hpp>
#include <hex/helpers/crypto.hpp>
#include <hex/helpers/utils.hpp>
namespace hex::plugin::builtin {
void Base64Provider::readRaw(u64 offset, void *buffer, size_t size) {
const u64 base64Offset = 4 * (offset / 3);
const u64 base64Size = hex::alignTo<u64>(4 * (size / 3), 4) + 4;
std::vector<u8> bytes(base64Size);
FileProvider::readRaw(base64Offset, bytes.data(), bytes.size());
auto decoded = crypt::decode64(bytes);
if (decoded.empty())
return;
u64 startOffset = offset % 3;
std::memcpy(buffer, decoded.data() + startOffset, std::min<size_t>(decoded.size() - startOffset, size));
}
void Base64Provider::writeRaw(u64 offset, const void *buffer, size_t size) {
const u64 base64Offset = 4 * (offset / 3);
const u64 base64Size = hex::alignTo<u64>(4 * (size / 3), 4) + 4;
std::vector<u8> bytes(base64Size);
FileProvider::readRaw(base64Offset, bytes.data(), bytes.size());
auto decoded = crypt::decode64(bytes);
if (decoded.empty())
return;
u64 startOffset = offset % 3;
std::memcpy(decoded.data() + startOffset, buffer, std::min<size_t>(decoded.size() - startOffset, size));
auto encoded = crypt::encode64(decoded);
if (encoded.empty())
return;
FileProvider::writeRaw(base64Offset, encoded.data(), encoded.size());
}
void Base64Provider::resizeRaw(u64 newSize) {
u64 newFileLength = 4 * (newSize / 3);
FileProvider::resizeRaw(newFileLength);
}
void Base64Provider::insertRaw(u64 offset, u64 size) {
u64 newFileLength = 4 * ((getActualSize() + size) / 3);
FileProvider::insertRaw(4 * (offset / 3), newFileLength);
constexpr static auto NullByte = 0x00;
for (u64 i = 0; i < size; i++)
writeRaw(offset + i, &NullByte, 1);
}
void Base64Provider::removeRaw(u64 offset, u64 size) {
u64 newFileLength = 4 * ((getActualSize() - size) / 3);
FileProvider::removeRaw(4 * (offset / 3), newFileLength);
}
}

View File

@@ -43,8 +43,7 @@ namespace hex::plugin::builtin {
}
void FileProvider::readRaw(u64 offset, void *buffer, size_t size) {
auto actualSize = this->getActualSize();
if (actualSize == 0 || (offset + size) > actualSize || buffer == nullptr || size == 0)
if (m_fileSize == 0 || (offset + size) > m_fileSize || buffer == nullptr || size == 0)
return;
std::memcpy(buffer, m_file.getMapping() + offset, size);