2021-12-07 22:47:41 +01:00
|
|
|
#include "content/providers/file_provider.hpp"
|
2020-11-11 09:18:35 +01:00
|
|
|
|
2021-01-03 15:09:12 +01:00
|
|
|
#include <cstring>
|
2020-11-15 16:06:10 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
#include <hex/api/imhex_api.hpp>
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/api/localization.hpp>
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/utils.hpp>
|
2021-09-08 15:18:24 +02:00
|
|
|
#include <hex/helpers/file.hpp>
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
2022-08-08 21:23:52 +02:00
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
2020-11-15 16:06:10 +01:00
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
namespace hex::plugin::builtin::prv {
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
bool FileProvider::isAvailable() const {
|
2022-09-26 11:49:35 +02:00
|
|
|
return this->m_mappedFile != nullptr;
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
bool FileProvider::isReadable() const {
|
2020-11-11 10:47:02 +01:00
|
|
|
return isAvailable() && this->m_readable;
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
bool FileProvider::isWritable() const {
|
2020-11-11 10:47:02 +01:00
|
|
|
return isAvailable() && this->m_writable;
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
bool FileProvider::isResizable() const {
|
2022-09-03 23:08:40 +02:00
|
|
|
return isAvailable() && isWritable();
|
2021-07-27 21:07:36 +02:00
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
bool FileProvider::isSavable() const {
|
2021-08-21 13:53:50 +02:00
|
|
|
return !this->getPatches().empty();
|
|
|
|
}
|
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
|
2021-03-21 14:50:47 +01:00
|
|
|
void FileProvider::read(u64 offset, void *buffer, size_t size, bool overlays) {
|
2021-12-09 21:10:24 +01:00
|
|
|
if ((offset - this->getBaseAddress()) > (this->getActualSize() - size) || buffer == nullptr || size == 0)
|
2020-11-11 09:18:35 +01:00
|
|
|
return;
|
|
|
|
|
2021-12-09 21:10:24 +01:00
|
|
|
this->readRaw(offset - this->getBaseAddress(), buffer, size);
|
2020-11-27 09:09:48 +01:00
|
|
|
|
2021-01-03 15:00:16 +01:00
|
|
|
for (u64 i = 0; i < size; i++)
|
2021-03-26 21:43:24 +01:00
|
|
|
if (getPatches().contains(offset + i))
|
2022-01-24 20:53:17 +01:00
|
|
|
reinterpret_cast<u8 *>(buffer)[i] = getPatches()[offset + PageSize * this->m_currPage + i];
|
2021-03-21 14:50:47 +01:00
|
|
|
|
|
|
|
if (overlays)
|
|
|
|
this->applyOverlays(offset, buffer, size);
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 13:44:52 +01:00
|
|
|
void FileProvider::write(u64 offset, const void *buffer, size_t size) {
|
2021-12-09 21:10:24 +01:00
|
|
|
if ((offset - this->getBaseAddress()) > (this->getActualSize() - size) || buffer == nullptr || size == 0)
|
2020-11-11 09:18:35 +01:00
|
|
|
return;
|
|
|
|
|
2022-02-15 21:50:02 +01:00
|
|
|
addPatch(offset, buffer, size, true);
|
2020-11-27 09:09:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileProvider::readRaw(u64 offset, void *buffer, size_t size) {
|
2022-09-26 11:49:35 +02:00
|
|
|
if ((offset + size) > this->getActualSize() || buffer == nullptr || size == 0)
|
2020-11-27 09:09:48 +01:00
|
|
|
return;
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
std::memcpy(buffer, reinterpret_cast<u8 *>(this->m_mappedFile) + offset, size);
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 13:44:52 +01:00
|
|
|
void FileProvider::writeRaw(u64 offset, const void *buffer, size_t size) {
|
2021-12-09 21:10:24 +01:00
|
|
|
if ((offset + size) > this->getActualSize() || buffer == nullptr || size == 0)
|
2020-11-27 09:09:48 +01:00
|
|
|
return;
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
std::memcpy(reinterpret_cast<u8 *>(this->m_mappedFile) + offset, buffer, size);
|
2020-11-27 09:09:48 +01:00
|
|
|
}
|
2021-07-27 21:07:36 +02:00
|
|
|
|
2021-08-21 13:53:50 +02:00
|
|
|
void FileProvider::save() {
|
|
|
|
this->applyPatches();
|
|
|
|
}
|
|
|
|
|
2022-03-04 11:36:37 +01:00
|
|
|
void FileProvider::saveAs(const std::fs::path &path) {
|
|
|
|
fs::File file(path, fs::File::Mode::Create);
|
2021-08-21 13:53:50 +02:00
|
|
|
|
2021-09-08 15:18:24 +02:00
|
|
|
if (file.isValid()) {
|
2021-10-07 22:51:16 +02:00
|
|
|
auto provider = ImHexApi::Provider::get();
|
|
|
|
|
|
|
|
std::vector<u8> buffer(std::min<size_t>(0xFF'FFFF, provider->getActualSize()), 0x00);
|
2021-08-21 13:53:50 +02:00
|
|
|
size_t bufferSize = buffer.size();
|
|
|
|
|
|
|
|
for (u64 offset = 0; offset < provider->getActualSize(); offset += bufferSize) {
|
|
|
|
if (bufferSize > provider->getActualSize() - offset)
|
|
|
|
bufferSize = provider->getActualSize() - offset;
|
|
|
|
|
2021-12-09 21:10:24 +01:00
|
|
|
provider->read(offset + this->getBaseAddress(), buffer.data(), bufferSize);
|
2021-09-08 15:18:24 +02:00
|
|
|
file.write(buffer);
|
2021-08-21 13:53:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 23:24:26 +01:00
|
|
|
void FileProvider::resize(size_t newSize) {
|
2021-07-27 22:46:37 +02:00
|
|
|
this->close();
|
2021-07-27 21:07:36 +02:00
|
|
|
|
|
|
|
{
|
2022-03-04 11:36:37 +01:00
|
|
|
fs::File file(this->m_path, fs::File::Mode::Write);
|
2022-01-20 23:24:26 +01:00
|
|
|
|
|
|
|
file.setSize(newSize);
|
|
|
|
this->m_fileSize = file.getSize();
|
2021-07-27 21:07:36 +02:00
|
|
|
}
|
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
(void)this->open();
|
2021-07-27 21:07:36 +02:00
|
|
|
}
|
|
|
|
|
2022-01-20 23:24:26 +01:00
|
|
|
void FileProvider::insert(u64 offset, size_t size) {
|
|
|
|
auto oldSize = this->getActualSize();
|
|
|
|
this->resize(oldSize + size);
|
|
|
|
|
|
|
|
std::vector<u8> buffer(0x1000);
|
|
|
|
const std::vector<u8> zeroBuffer(0x1000);
|
|
|
|
|
|
|
|
auto position = oldSize;
|
|
|
|
while (position > offset) {
|
2022-06-17 14:21:56 +02:00
|
|
|
const auto readSize = std::min<size_t>(position - offset, buffer.size());
|
2022-01-20 23:24:26 +01:00
|
|
|
|
|
|
|
position -= readSize;
|
|
|
|
|
|
|
|
this->readRaw(position, buffer.data(), readSize);
|
|
|
|
this->writeRaw(position, zeroBuffer.data(), readSize);
|
|
|
|
this->writeRaw(position + size, buffer.data(), readSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
Provider::insert(offset, size);
|
|
|
|
}
|
|
|
|
|
2022-06-17 14:21:56 +02:00
|
|
|
void FileProvider::remove(u64 offset, size_t size) {
|
|
|
|
auto oldSize = this->getActualSize();
|
|
|
|
this->resize(oldSize + size);
|
|
|
|
|
|
|
|
std::vector<u8> buffer(0x1000);
|
|
|
|
|
|
|
|
const auto newSize = oldSize - size;
|
|
|
|
auto position = offset;
|
|
|
|
while (position < newSize) {
|
|
|
|
const auto readSize = std::min<size_t>(newSize - position, buffer.size());
|
|
|
|
|
|
|
|
this->readRaw(position + size, buffer.data(), readSize);
|
|
|
|
this->writeRaw(position, buffer.data(), readSize);
|
|
|
|
|
|
|
|
position += readSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->resize(newSize);
|
|
|
|
|
|
|
|
Provider::insert(offset, size);
|
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
size_t FileProvider::getActualSize() const {
|
2021-01-03 15:00:16 +01:00
|
|
|
return this->m_fileSize;
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
std::string FileProvider::getName() const {
|
2022-10-04 09:10:58 +02:00
|
|
|
return hex::toUTF8String(this->m_path.filename());
|
2021-09-21 02:29:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::pair<std::string, std::string>> FileProvider::getDataInformation() const {
|
2020-11-15 16:06:10 +01:00
|
|
|
std::vector<std::pair<std::string, std::string>> result;
|
|
|
|
|
2022-10-04 09:10:58 +02:00
|
|
|
result.emplace_back("hex.builtin.provider.file.path"_lang, hex::toUTF8String(this->m_path));
|
2021-02-22 13:07:25 +01:00
|
|
|
result.emplace_back("hex.builtin.provider.file.size"_lang, hex::toByteString(this->getActualSize()));
|
2020-11-15 16:06:10 +01:00
|
|
|
|
|
|
|
if (this->m_fileStatsValid) {
|
2022-03-26 17:18:40 +01:00
|
|
|
result.emplace_back("hex.builtin.provider.file.creation"_lang, hex::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(this->m_fileStats.st_ctime)));
|
|
|
|
result.emplace_back("hex.builtin.provider.file.access"_lang, hex::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(this->m_fileStats.st_atime)));
|
|
|
|
result.emplace_back("hex.builtin.provider.file.modification"_lang, hex::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(this->m_fileStats.st_mtime)));
|
2020-11-15 16:06:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-08-12 15:11:27 +02:00
|
|
|
bool FileProvider::handleFilePicker() {
|
|
|
|
return fs::openFileBrowser(fs::DialogMode::Open, {}, [this](const auto &path) {
|
|
|
|
this->setPath(path);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-04 11:36:37 +01:00
|
|
|
void FileProvider::setPath(const std::fs::path &path) {
|
2021-12-07 22:47:41 +01:00
|
|
|
this->m_path = path;
|
2021-12-12 00:41:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileProvider::open() {
|
2021-07-27 21:07:36 +02:00
|
|
|
this->m_readable = true;
|
|
|
|
this->m_writable = true;
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
const auto &path = this->m_path.native();
|
2022-03-26 17:18:40 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
this->m_fileStatsValid = wstat(path.c_str(), &this->m_fileStats) == 0;
|
2021-07-27 21:07:36 +02:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
LARGE_INTEGER fileSize = {};
|
2022-09-26 11:49:35 +02:00
|
|
|
auto file = reinterpret_cast<HANDLE>(CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr));
|
2022-01-24 20:53:17 +01:00
|
|
|
|
2022-09-26 11:49:35 +02:00
|
|
|
GetFileSizeEx(file, &fileSize);
|
2022-08-08 21:23:52 +02:00
|
|
|
this->m_fileSize = fileSize.QuadPart;
|
2022-09-26 11:49:35 +02:00
|
|
|
CloseHandle(file);
|
2022-01-09 21:57:43 +01:00
|
|
|
|
2022-09-26 11:49:35 +02:00
|
|
|
file = reinterpret_cast<HANDLE>(CreateFileW(path.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr));
|
|
|
|
if (file == nullptr || file == INVALID_HANDLE_VALUE) {
|
|
|
|
file = reinterpret_cast<HANDLE>(CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr));
|
2022-08-08 21:23:52 +02:00
|
|
|
this->m_writable = false;
|
|
|
|
}
|
2022-01-09 21:57:43 +01:00
|
|
|
|
2022-09-26 11:49:35 +02:00
|
|
|
if (file == nullptr || file == INVALID_HANDLE_VALUE) {
|
2022-08-08 21:23:52 +02:00
|
|
|
return false;
|
2022-01-24 20:53:17 +01:00
|
|
|
}
|
2021-07-27 21:07:36 +02:00
|
|
|
|
2022-09-26 11:49:35 +02:00
|
|
|
ON_SCOPE_EXIT { CloseHandle(file); };
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
if (this->m_fileSize > 0) {
|
2022-09-26 11:49:35 +02:00
|
|
|
HANDLE mapping = CreateFileMapping(file, nullptr, PAGE_READWRITE, 0, 0, nullptr);
|
2022-09-19 23:07:44 +02:00
|
|
|
ON_SCOPE_EXIT { CloseHandle(mapping); };
|
2022-01-09 21:57:43 +01:00
|
|
|
|
2022-09-19 23:07:44 +02:00
|
|
|
if (mapping == nullptr || mapping == INVALID_HANDLE_VALUE) {
|
2022-09-26 11:49:35 +02:00
|
|
|
mapping = CreateFileMapping(file, nullptr, PAGE_READONLY, 0, 0, nullptr);
|
2022-01-16 01:51:31 +01:00
|
|
|
|
2022-09-19 23:07:44 +02:00
|
|
|
if (mapping == nullptr || mapping == INVALID_HANDLE_VALUE)
|
2022-08-08 21:23:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-01-09 21:57:43 +01:00
|
|
|
|
2022-09-19 23:07:44 +02:00
|
|
|
this->m_mappedFile = MapViewOfFile(mapping, FILE_MAP_ALL_ACCESS, 0, 0, this->m_fileSize);
|
2022-08-08 21:23:52 +02:00
|
|
|
if (this->m_mappedFile == nullptr) {
|
|
|
|
|
2022-09-19 23:07:44 +02:00
|
|
|
this->m_mappedFile = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, this->m_fileSize);
|
2022-08-08 21:23:52 +02:00
|
|
|
if (this->m_mappedFile == nullptr) {
|
|
|
|
this->m_readable = false;
|
2022-01-16 01:51:31 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-01-09 21:57:43 +01:00
|
|
|
}
|
2022-08-08 21:23:52 +02:00
|
|
|
} else if (!this->m_emptyFile) {
|
|
|
|
this->m_emptyFile = true;
|
|
|
|
this->resize(1);
|
|
|
|
} else {
|
|
|
|
return false;
|
2022-01-24 20:53:17 +01:00
|
|
|
}
|
2022-08-08 21:23:52 +02:00
|
|
|
#else
|
|
|
|
|
|
|
|
const auto &path = this->m_path.native();
|
|
|
|
this->m_fileStatsValid = stat(path.c_str(), &this->m_fileStats) == 0;
|
2021-07-27 21:07:36 +02:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
int mmapprot = PROT_READ | PROT_WRITE;
|
2021-07-27 21:07:36 +02:00
|
|
|
|
2022-09-26 11:49:35 +02:00
|
|
|
auto file = ::open(path.c_str(), O_RDWR);
|
|
|
|
if (file == -1) {
|
|
|
|
file = ::open(path.c_str(), O_RDONLY);
|
2022-08-08 21:23:52 +02:00
|
|
|
this->m_writable = false;
|
|
|
|
mmapprot &= ~(PROT_WRITE);
|
|
|
|
}
|
2022-03-26 17:18:40 +01:00
|
|
|
|
2022-09-26 11:49:35 +02:00
|
|
|
if (file == -1) {
|
2022-08-08 21:23:52 +02:00
|
|
|
this->m_readable = false;
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-16 02:29:19 +01:00
|
|
|
|
2022-09-28 15:02:55 +02:00
|
|
|
ON_SCOPE_EXIT { ::close(file); };
|
2021-07-27 21:07:36 +02:00
|
|
|
|
2022-09-26 11:49:35 +02:00
|
|
|
this->m_fileSize = this->m_fileStats.st_size;
|
2021-07-27 21:07:36 +02:00
|
|
|
|
2022-09-26 11:49:35 +02:00
|
|
|
this->m_mappedFile = ::mmap(nullptr, this->m_fileSize, mmapprot, MAP_SHARED, file, 0);
|
|
|
|
if (this->m_mappedFile == MAP_FAILED)
|
2022-08-08 21:23:52 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-09-04 11:16:20 +02:00
|
|
|
return true;
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileProvider::close() {
|
|
|
|
#if defined(OS_WINDOWS)
|
2021-07-27 21:07:36 +02:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
if (this->m_mappedFile != nullptr)
|
|
|
|
::UnmapViewOfFile(this->m_mappedFile);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2022-09-26 11:49:35 +02:00
|
|
|
if (this->m_mappedFile != nullptr)
|
|
|
|
::munmap(this->m_mappedFile, this->m_fileSize);
|
2021-07-27 21:07:36 +02:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
#endif
|
|
|
|
}
|
2021-12-12 00:41:44 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
void FileProvider::loadSettings(const nlohmann::json &settings) {
|
|
|
|
Provider::loadSettings(settings);
|
2022-04-17 16:57:30 +02:00
|
|
|
|
2022-10-04 09:10:58 +02:00
|
|
|
auto path = settings["path"].get<std::string>();
|
|
|
|
this->setPath(std::u8string(path.begin(), path.end()));
|
2021-07-27 21:07:36 +02:00
|
|
|
}
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
nlohmann::json FileProvider::storeSettings(nlohmann::json settings) const {
|
2022-10-04 09:10:58 +02:00
|
|
|
settings["path"] = hex::toUTF8String(this->m_path);
|
2022-08-08 21:23:52 +02:00
|
|
|
|
|
|
|
return Provider::storeSettings(settings);
|
2021-07-27 21:07:36 +02:00
|
|
|
}
|
|
|
|
|
2022-08-14 09:38:38 +02:00
|
|
|
std::pair<Region, bool> FileProvider::getRegionValidity(u64 address) const {
|
2022-08-17 16:28:44 +02:00
|
|
|
address -= this->getBaseAddress();
|
|
|
|
|
2022-08-14 09:38:38 +02:00
|
|
|
if (address < this->getActualSize())
|
2022-08-17 16:28:44 +02:00
|
|
|
return { Region { this->getBaseAddress() + address, this->getActualSize() - address }, true };
|
2022-08-14 09:38:38 +02:00
|
|
|
else
|
|
|
|
return { Region::Invalid(), false };
|
|
|
|
}
|
|
|
|
|
2021-12-22 13:36:26 +01:00
|
|
|
}
|