diff --git a/lib/libimhex/include/hex/helpers/utils.hpp b/lib/libimhex/include/hex/helpers/utils.hpp index 1bbe17996..42fb38aec 100644 --- a/lib/libimhex/include/hex/helpers/utils.hpp +++ b/lib/libimhex/include/hex/helpers/utils.hpp @@ -305,4 +305,6 @@ namespace hex { [[nodiscard]] std::string generateHexView(u64 offset, u64 size, prv::Provider *provider); [[nodiscard]] std::string generateHexView(u64 offset, const std::vector &data); + [[nodiscard]] std::string formatSystemError(i32 error); + } diff --git a/lib/libimhex/source/api/plugin_manager.cpp b/lib/libimhex/source/api/plugin_manager.cpp index 3d5d1a11e..01d626277 100644 --- a/lib/libimhex/source/api/plugin_manager.cpp +++ b/lib/libimhex/source/api/plugin_manager.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -24,7 +25,7 @@ namespace hex { m_handle = uintptr_t(LoadLibraryW(path.c_str())); if (m_handle == uintptr_t(INVALID_HANDLE_VALUE) || m_handle == 0) { - log::error("Loading plugin '{}' failed: {} {}!", wolv::util::toUTF8String(path.filename()), ::GetLastError(), std::system_category().message(::GetLastError())); + log::error("Loading plugin '{}' failed: {} {}!", wolv::util::toUTF8String(path.filename()), ::GetLastError(), hex::formatSystemError(::GetLastError())); return; } #else @@ -91,7 +92,7 @@ namespace hex { #if defined(OS_WINDOWS) if (m_handle != 0) if (FreeLibrary(HMODULE(m_handle)) == FALSE) { - log::error("Error when unloading plugin '{}': {}!", wolv::util::toUTF8String(m_path.filename()), std::system_category().message(::GetLastError())); + log::error("Error when unloading plugin '{}': {}!", wolv::util::toUTF8String(m_path.filename()), hex::formatSystemError(::GetLastError())); } #else if (m_handle != 0) diff --git a/lib/libimhex/source/helpers/utils.cpp b/lib/libimhex/source/helpers/utils.cpp index ef5def35c..d1c9d7813 100644 --- a/lib/libimhex/source/helpers/utils.cpp +++ b/lib/libimhex/source/helpers/utils.cpp @@ -750,4 +750,14 @@ namespace hex { return generateHexViewImpl(offset, data.begin(), data.end()); } + std::string formatSystemError(i32 error) { + auto message = std::system_category().message(error); + + #if defined(OS_WINDOWS) + return hex::utf16ToUtf8(hex::utf8ToUtf16(message)); + #else + return message; + #endif + } + } \ No newline at end of file diff --git a/plugins/builtin/source/content/providers/disk_provider.cpp b/plugins/builtin/source/content/providers/disk_provider.cpp index 79cf9794b..db94a75f6 100644 --- a/plugins/builtin/source/content/providers/disk_provider.cpp +++ b/plugins/builtin/source/content/providers/disk_provider.cpp @@ -48,15 +48,11 @@ namespace hex::plugin::builtin { bool DiskProvider::isAvailable() const { -#if defined(OS_WINDOWS) - - return m_diskHandle != INVALID_HANDLE_VALUE; - -#else - - return m_diskHandle != -1; - -#endif + #if defined(OS_WINDOWS) + return m_diskHandle != INVALID_HANDLE_VALUE; + #else + return m_diskHandle != -1; + #endif } bool DiskProvider::isReadable() const { @@ -146,9 +142,9 @@ namespace hex::plugin::builtin { m_readable = true; m_writable = true; -#if defined(OS_WINDOWS) + #if defined(OS_WINDOWS) - const auto &path = m_path.native(); + const auto &path = m_path.native(); m_diskHandle = CreateFileW(path.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); if (m_diskHandle == INVALID_HANDLE_VALUE) { @@ -156,7 +152,7 @@ namespace hex::plugin::builtin { m_writable = false; if (m_diskHandle == INVALID_HANDLE_VALUE) { - this->setErrorMessage(std::system_category().message(::GetLastError())); + this->setErrorMessage(hex::formatSystemError(::GetLastError())); return false; } } @@ -180,7 +176,7 @@ namespace hex::plugin::builtin { } if (m_diskHandle == nullptr || m_diskHandle == INVALID_HANDLE_VALUE) { - this->setErrorMessage(std::system_category().message(::GetLastError())); + this->setErrorMessage(hex::formatSystemError(::GetLastError())); m_readable = false; m_diskHandle = nullptr; CloseHandle(m_diskHandle); @@ -188,59 +184,59 @@ namespace hex::plugin::builtin { return false; } -#else + #else - const auto &path = m_path.native(); + const auto &path = m_path.native(); - m_diskHandle = ::open(path.c_str(), O_RDWR); - if (m_diskHandle == -1) { - this->setErrorMessage(hex::format("hex.builtin.provider.disk.error.read_rw"_lang, path, ::strerror(errno))); - log::warn(this->getErrorMessage()); - m_diskHandle = ::open(path.c_str(), O_RDONLY); - m_writable = false; - } + m_diskHandle = ::open(path.c_str(), O_RDWR); + if (m_diskHandle == -1) { + this->setErrorMessage(hex::format("hex.builtin.provider.disk.error.read_rw"_lang, path, ::strerror(errno))); + log::warn(this->getErrorMessage()); + m_diskHandle = ::open(path.c_str(), O_RDONLY); + m_writable = false; + } - if (m_diskHandle == -1) { - this->setErrorMessage(hex::format("hex.builtin.provider.disk.error.read_ro"_lang, path, ::strerror(errno))); - log::warn(this->getErrorMessage()); - m_readable = false; - return false; - } + if (m_diskHandle == -1) { + this->setErrorMessage(hex::format("hex.builtin.provider.disk.error.read_ro"_lang, path, ::strerror(errno))); + log::warn(this->getErrorMessage()); + m_readable = false; + return false; + } - u64 diskSize = 0; - blkdev_get_size(m_diskHandle, &diskSize); - m_diskSize = diskSize; - blkdev_get_sector_size(m_diskHandle, reinterpret_cast(&m_sectorSize)); + u64 diskSize = 0; + blkdev_get_size(m_diskHandle, &diskSize); + m_diskSize = diskSize; + blkdev_get_sector_size(m_diskHandle, reinterpret_cast(&m_sectorSize)); - m_sectorBuffer.resize(m_sectorSize); + m_sectorBuffer.resize(m_sectorSize); -#endif + #endif return true; } void DiskProvider::close() { -#if defined(OS_WINDOWS) + #if defined(OS_WINDOWS) - if (m_diskHandle != INVALID_HANDLE_VALUE) - ::CloseHandle(m_diskHandle); + if (m_diskHandle != INVALID_HANDLE_VALUE) + ::CloseHandle(m_diskHandle); - m_diskHandle = INVALID_HANDLE_VALUE; + m_diskHandle = INVALID_HANDLE_VALUE; -#else + #else - if (m_diskHandle != -1) - ::close(m_diskHandle); + if (m_diskHandle != -1) + ::close(m_diskHandle); - m_diskHandle = -1; + m_diskHandle = -1; -#endif + #endif } void DiskProvider::readRaw(u64 offset, void *buffer, size_t size) { -#if defined(OS_WINDOWS) + #if defined(OS_WINDOWS) - DWORD bytesRead = 0; + DWORD bytesRead = 0; u64 startOffset = offset; @@ -261,36 +257,36 @@ namespace hex::plugin::builtin { offset += m_sectorSize; } -#else + #else - u64 startOffset = offset; + u64 startOffset = offset; - while (size > 0) { - u64 seekPosition = offset - (offset % m_sectorSize); + while (size > 0) { + u64 seekPosition = offset - (offset % m_sectorSize); - if (m_sectorBufferAddress != seekPosition || m_sectorBufferAddress == 0) { - ::lseek(m_diskHandle, seekPosition, SEEK_SET); - if (::read(m_diskHandle, m_sectorBuffer.data(), m_sectorBuffer.size()) == -1) - break; + if (m_sectorBufferAddress != seekPosition || m_sectorBufferAddress == 0) { + ::lseek(m_diskHandle, seekPosition, SEEK_SET); + if (::read(m_diskHandle, m_sectorBuffer.data(), m_sectorBuffer.size()) == -1) + break; - m_sectorBufferAddress = seekPosition; + m_sectorBufferAddress = seekPosition; + } + + std::memcpy(reinterpret_cast(buffer) + (offset - startOffset), + m_sectorBuffer.data() + (offset & (m_sectorSize - 1)), + std::min(m_sectorSize, size)); + + size = std::max(static_cast(size) - m_sectorSize, 0); + offset += m_sectorSize; } - std::memcpy(reinterpret_cast(buffer) + (offset - startOffset), - m_sectorBuffer.data() + (offset & (m_sectorSize - 1)), - std::min(m_sectorSize, size)); - - size = std::max(static_cast(size) - m_sectorSize, 0); - offset += m_sectorSize; - } - -#endif + #endif } void DiskProvider::writeRaw(u64 offset, const void *buffer, size_t size) { -#if defined(OS_WINDOWS) + #if defined(OS_WINDOWS) - DWORD bytesWritten = 0; + DWORD bytesWritten = 0; u64 startOffset = offset; @@ -311,33 +307,36 @@ namespace hex::plugin::builtin { ::SetFilePointer(m_diskHandle, seekPosition.LowPart, &seekPosition.HighPart, FILE_BEGIN); ::WriteFile(m_diskHandle, modifiedSectorBuffer.data(), modifiedSectorBuffer.size(), &bytesWritten, nullptr); + //Print last error + log::error("{}", hex::formatSystemError(::GetLastError())); + offset += currSize; size -= currSize; } -#else + #else - u64 startOffset = offset; + u64 startOffset = offset; - std::vector modifiedSectorBuffer; - modifiedSectorBuffer.resize(m_sectorSize); + std::vector modifiedSectorBuffer; + modifiedSectorBuffer.resize(m_sectorSize); - while (size > 0) { - u64 sectorBase = offset - (offset % m_sectorSize); - size_t currSize = std::min(size, m_sectorSize); + while (size > 0) { + u64 sectorBase = offset - (offset % m_sectorSize); + size_t currSize = std::min(size, m_sectorSize); - this->readRaw(sectorBase, modifiedSectorBuffer.data(), modifiedSectorBuffer.size()); - std::memcpy(modifiedSectorBuffer.data() + ((offset - sectorBase) % m_sectorSize), reinterpret_cast(buffer) + (startOffset - offset), currSize); + this->readRaw(sectorBase, modifiedSectorBuffer.data(), modifiedSectorBuffer.size()); + std::memcpy(modifiedSectorBuffer.data() + ((offset - sectorBase) % m_sectorSize), reinterpret_cast(buffer) + (startOffset - offset), currSize); - ::lseek(m_diskHandle, sectorBase, SEEK_SET); - if (::write(m_diskHandle, modifiedSectorBuffer.data(), modifiedSectorBuffer.size()) < 0) - break; + ::lseek(m_diskHandle, sectorBase, SEEK_SET); + if (::write(m_diskHandle, modifiedSectorBuffer.data(), modifiedSectorBuffer.size()) < 0) + break; - offset += currSize; - size -= currSize; - } + offset += currSize; + size -= currSize; + } -#endif + #endif } u64 DiskProvider::getActualSize() const {