2020-11-11 09:18:35 +01:00
|
|
|
#include "providers/file_provider.hpp"
|
|
|
|
|
2020-11-15 16:06:10 +01:00
|
|
|
#include <time.h>
|
2021-01-03 15:09:12 +01:00
|
|
|
#include <cstring>
|
2020-11-15 16:06:10 +01:00
|
|
|
|
2020-11-30 00:03:12 +01:00
|
|
|
#include "helpers/project_file_handler.hpp"
|
2020-11-15 16:06:10 +01:00
|
|
|
|
2021-01-06 17:22:12 +01:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
#include <locale>
|
|
|
|
#include <codecvt>
|
|
|
|
#endif
|
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
namespace hex::prv {
|
|
|
|
|
2020-11-15 16:06:10 +01:00
|
|
|
FileProvider::FileProvider(std::string_view path) : Provider(), m_path(path) {
|
|
|
|
this->m_fileStatsValid = stat(path.data(), &this->m_fileStats) == 0;
|
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
this->m_readable = true;
|
|
|
|
this->m_writable = true;
|
|
|
|
|
2021-01-03 15:00:16 +01:00
|
|
|
#if defined(OS_WINDOWS)
|
2021-01-06 17:22:12 +01:00
|
|
|
std::wstring widePath;
|
|
|
|
{
|
2021-01-11 13:50:04 +01:00
|
|
|
auto length = path.length() + 1;
|
|
|
|
auto wideLength = MultiByteToWideChar(CP_UTF8, 0, path.data(), length, 0, 0);
|
|
|
|
wchar_t* buffer = new wchar_t[wideLength];
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, path.data(), length, buffer, wideLength);
|
|
|
|
widePath = buffer;
|
|
|
|
delete[] buffer;
|
2021-01-06 17:22:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-03 15:00:16 +01:00
|
|
|
LARGE_INTEGER fileSize = { 0 };
|
2021-01-06 17:22:12 +01:00
|
|
|
this->m_file = reinterpret_cast<HANDLE>(CreateFileW(widePath.data(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr));
|
2021-01-03 15:00:16 +01:00
|
|
|
|
|
|
|
GetFileSizeEx(this->m_file, &fileSize);
|
|
|
|
this->m_fileSize = fileSize.QuadPart;
|
2021-01-06 17:22:12 +01:00
|
|
|
CloseHandle(this->m_file);
|
2021-01-03 15:00:16 +01:00
|
|
|
|
2021-01-06 17:22:12 +01:00
|
|
|
this->m_file = reinterpret_cast<HANDLE>(CreateFileW(widePath.data(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr));
|
2021-01-03 15:00:16 +01:00
|
|
|
if (this->m_file == nullptr || this->m_file == INVALID_HANDLE_VALUE) {
|
2021-01-06 17:22:12 +01:00
|
|
|
this->m_file = reinterpret_cast<HANDLE>(CreateFileW(widePath.data(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr));
|
2020-11-11 09:18:35 +01:00
|
|
|
this->m_writable = false;
|
|
|
|
}
|
2020-11-30 00:03:12 +01:00
|
|
|
|
2021-01-03 15:00:16 +01:00
|
|
|
ScopeExit fileCleanup([this]{
|
|
|
|
this->m_readable = false;
|
|
|
|
this->m_file = nullptr;
|
|
|
|
CloseHandle(this->m_file);
|
|
|
|
});
|
|
|
|
if (this->m_file == nullptr || this->m_file == INVALID_HANDLE_VALUE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-06 17:22:12 +01:00
|
|
|
this->m_mapping = CreateFileMapping(this->m_file, nullptr, PAGE_READWRITE, fileSize.HighPart, fileSize.LowPart, nullptr);
|
|
|
|
if (this->m_mapping == nullptr || this->m_mapping == INVALID_HANDLE_VALUE) {
|
2021-01-03 15:00:16 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopeExit mappingCleanup([this]{
|
|
|
|
this->m_readable = false;
|
|
|
|
this->m_mapping = nullptr;
|
|
|
|
CloseHandle(this->m_mapping);
|
|
|
|
});
|
|
|
|
|
|
|
|
this->m_mappedFile = MapViewOfFile(this->m_mapping, FILE_MAP_ALL_ACCESS, 0, 0, this->m_fileSize);
|
|
|
|
if (this->m_mappedFile == nullptr) {
|
|
|
|
this->m_readable = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fileCleanup.release();
|
|
|
|
mappingCleanup.release();
|
|
|
|
|
|
|
|
ProjectFile::setFilePath(path);
|
|
|
|
|
|
|
|
#else
|
|
|
|
this->m_file = open(path.data(), O_RDWR);
|
|
|
|
if (this->m_file == -1) {
|
|
|
|
this->m_file = open(path.data(), O_RDONLY);
|
|
|
|
this->m_writable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->m_file == -1) {
|
|
|
|
this->m_readable = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->m_fileSize = this->m_fileStats.st_size;
|
|
|
|
|
|
|
|
this->m_mappedFile = mmap(nullptr, this->m_fileSize, PROT_READ | PROT_WRITE, MAP_PRIVATE, this->m_file, 0);
|
|
|
|
|
|
|
|
#endif
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FileProvider::~FileProvider() {
|
2021-01-03 15:00:16 +01:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
if (this->m_mappedFile != nullptr)
|
|
|
|
UnmapViewOfFile(this->m_mappedFile);
|
|
|
|
if (this->m_mapping != nullptr)
|
|
|
|
CloseHandle(this->m_mapping);
|
2020-11-11 09:18:35 +01:00
|
|
|
if (this->m_file != nullptr)
|
2021-01-03 15:00:16 +01:00
|
|
|
CloseHandle(this->m_file);
|
|
|
|
#else
|
|
|
|
munmap(this->m_mappedFile, this->m_fileSize);
|
|
|
|
close(this->m_file);
|
|
|
|
#endif
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileProvider::isAvailable() {
|
2021-01-03 15:00:16 +01:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
return this->m_file != nullptr && this->m_mapping != nullptr && this->m_mappedFile != nullptr;
|
|
|
|
#else
|
|
|
|
return this->m_file != -1 && this->m_mappedFile != nullptr;
|
|
|
|
#endif
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileProvider::isReadable() {
|
2020-11-11 10:47:02 +01:00
|
|
|
return isAvailable() && this->m_readable;
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileProvider::isWritable() {
|
2020-11-11 10:47:02 +01:00
|
|
|
return isAvailable() && this->m_writable;
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileProvider::read(u64 offset, void *buffer, size_t size) {
|
|
|
|
if ((offset + size) > this->getSize() || buffer == nullptr || size == 0)
|
|
|
|
return;
|
|
|
|
|
2021-01-11 13:50:04 +01:00
|
|
|
std::memcpy(buffer, reinterpret_cast<u8*>(this->m_mappedFile) + PageSize * this->m_currPage + offset, size);
|
2020-11-27 09:09:48 +01:00
|
|
|
|
2021-01-03 15:00:16 +01:00
|
|
|
for (u64 i = 0; i < size; i++)
|
2020-11-27 09:09:48 +01:00
|
|
|
if (this->m_patches.back().contains(offset + i))
|
2021-01-11 13:50:04 +01:00
|
|
|
reinterpret_cast<u8*>(buffer)[i] = this->m_patches.back()[offset + PageSize * this->m_currPage + i];
|
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-01-11 13:50:04 +01:00
|
|
|
if ((offset + size) > this->getSize() || buffer == nullptr || size == 0)
|
2020-11-11 09:18:35 +01:00
|
|
|
return;
|
|
|
|
|
2020-11-27 09:09:48 +01:00
|
|
|
this->m_patches.push_back(this->m_patches.back());
|
|
|
|
|
|
|
|
for (u64 i = 0; i < size; i++)
|
2021-01-11 13:50:04 +01:00
|
|
|
this->m_patches.back()[offset + this->getBaseAddress() + i] = reinterpret_cast<const u8*>(buffer)[i];
|
2020-11-27 09:09:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileProvider::readRaw(u64 offset, void *buffer, size_t size) {
|
|
|
|
if ((offset + size) > this->getSize() || buffer == nullptr || size == 0)
|
|
|
|
return;
|
|
|
|
|
2021-01-11 13:50:04 +01:00
|
|
|
std::memcpy(buffer, reinterpret_cast<u8*>(this->m_mappedFile) + PageSize * this->m_currPage + 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-01-11 13:50:04 +01:00
|
|
|
if ((offset + size) > this->getSize() || buffer == nullptr || size == 0)
|
2020-11-27 09:09:48 +01:00
|
|
|
return;
|
|
|
|
|
2021-01-11 13:50:04 +01:00
|
|
|
std::memcpy(reinterpret_cast<u8*>(this->m_mappedFile) + PageSize * this->m_currPage + offset, buffer, size);
|
2020-11-27 09:09:48 +01:00
|
|
|
}
|
2020-11-22 19:43:35 +01:00
|
|
|
size_t FileProvider::getActualSize() {
|
2021-01-03 15:00:16 +01:00
|
|
|
return this->m_fileSize;
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
|
2020-11-15 16:06:10 +01:00
|
|
|
std::vector<std::pair<std::string, std::string>> FileProvider::getDataInformation() {
|
|
|
|
std::vector<std::pair<std::string, std::string>> result;
|
|
|
|
|
2021-02-22 13:07:25 +01:00
|
|
|
result.emplace_back("hex.builtin.provider.file.path"_lang, this->m_path);
|
|
|
|
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) {
|
2021-02-22 13:07:25 +01:00
|
|
|
result.emplace_back("hex.builtin.provider.file.creation"_lang, ctime(&this->m_fileStats.st_ctime));
|
|
|
|
result.emplace_back("hex.builtin.provider.file.access"_lang, ctime(&this->m_fileStats.st_atime));
|
|
|
|
result.emplace_back("hex.builtin.provider.file.modification"_lang, ctime(&this->m_fileStats.st_mtime));
|
2020-11-15 16:06:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|