From 3db50a690c409bfd8a81173c1e4451c89a49fa40 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 30 Jun 2022 19:39:06 +0200 Subject: [PATCH] fix: Various issues with UTF-8 paths --- lib/libimhex/include/hex/helpers/fs.hpp | 2 ++ lib/libimhex/source/helpers/fs.cpp | 16 ++++++++++++++++ lib/libimhex/source/helpers/magic.cpp | 5 +++-- .../builtin/include/content/views/view_yara.hpp | 2 +- .../source/content/views/view_pattern_editor.cpp | 2 +- .../builtin/source/content/views/view_yara.cpp | 8 ++++---- 6 files changed, 27 insertions(+), 8 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/fs.hpp b/lib/libimhex/include/hex/helpers/fs.hpp index 0693760aa..ccd7873f1 100644 --- a/lib/libimhex/include/hex/helpers/fs.hpp +++ b/lib/libimhex/include/hex/helpers/fs.hpp @@ -67,6 +67,8 @@ namespace hex::fs { bool isPathWritable(const std::fs::path &path); + std::fs::path toShortPath(const std::fs::path &path); + enum class DialogMode { Open, diff --git a/lib/libimhex/source/helpers/fs.cpp b/lib/libimhex/source/helpers/fs.cpp index b03947d3f..81d6f8421 100644 --- a/lib/libimhex/source/helpers/fs.cpp +++ b/lib/libimhex/source/helpers/fs.cpp @@ -313,4 +313,20 @@ namespace hex::fs { return result; } + std::fs::path toShortPath(const std::fs::path &path) { + #if defined(OS_WINDOWS) + size_t size = GetShortPathNameW(path.c_str(), nullptr, 0) * sizeof(TCHAR); + if (size == 0) + return path; + + std::wstring newPath(size, 0x00); + GetShortPathNameW(path.c_str(), newPath.data(), newPath.size()); + + return newPath; + #else + return path; + #endif + } + + } diff --git a/lib/libimhex/source/helpers/magic.cpp b/lib/libimhex/source/helpers/magic.cpp index cde19afa3..6539b4dfa 100644 --- a/lib/libimhex/source/helpers/magic.cpp +++ b/lib/libimhex/source/helpers/magic.cpp @@ -26,8 +26,9 @@ namespace hex::magic { std::error_code error; for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Magic)) { for (const auto &entry : std::fs::directory_iterator(dir, error)) { - if (entry.is_regular_file() && ((sourceFiles && entry.path().extension().empty()) || (!sourceFiles && entry.path().extension() == ".mgc"))) - magicFiles += entry.path().string() + MAGIC_PATH_SEPARATOR; + if (entry.is_regular_file() && ((sourceFiles && entry.path().extension().empty()) || (!sourceFiles && entry.path().extension() == ".mgc"))) { + magicFiles += fs::toShortPath(entry.path()).string() + MAGIC_PATH_SEPARATOR; + } } } diff --git a/plugins/builtin/include/content/views/view_yara.hpp b/plugins/builtin/include/content/views/view_yara.hpp index 4fa039d8a..7fec699ad 100644 --- a/plugins/builtin/include/content/views/view_yara.hpp +++ b/plugins/builtin/include/content/views/view_yara.hpp @@ -26,7 +26,7 @@ namespace hex::plugin::builtin { u32 tooltipId; }; - std::vector> m_rules; + std::vector> m_rules; std::vector m_matches; u32 m_selectedRule = 0; bool m_matching = false; diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index 8cf17b2d0..fd3149e8c 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -133,7 +133,7 @@ namespace hex::plugin::builtin { if (!entry.is_regular_file()) continue; - fs::File file(entry.path().string(), fs::File::Mode::Read); + fs::File file(entry.path(), fs::File::Mode::Read); if (!file.isValid()) continue; diff --git a/plugins/builtin/source/content/views/view_yara.cpp b/plugins/builtin/source/content/views/view_yara.cpp index 45cc25196..0f8bd1eeb 100644 --- a/plugins/builtin/source/content/views/view_yara.cpp +++ b/plugins/builtin/source/content/views/view_yara.cpp @@ -46,10 +46,10 @@ namespace hex::plugin::builtin { } else { ImGui::BeginDisabled(this->m_matching); { - if (ImGui::BeginCombo("hex.builtin.view.yara.header.rules"_lang, this->m_rules[this->m_selectedRule].first.c_str())) { + if (ImGui::BeginCombo("hex.builtin.view.yara.header.rules"_lang, this->m_rules[this->m_selectedRule].first.string().c_str())) { for (u32 i = 0; i < this->m_rules.size(); i++) { const bool selected = (this->m_selectedRule == i); - if (ImGui::Selectable(this->m_rules[i].first.c_str(), selected)) + if (ImGui::Selectable(this->m_rules[i].first.string().c_str(), selected)) this->m_selectedRule = i; if (selected) @@ -162,7 +162,7 @@ namespace hex::plugin::builtin { std::error_code error; for (const auto &entry : std::fs::recursive_directory_iterator(path, error)) { if (entry.is_regular_file() && entry.path().extension() == ".yar") { - this->m_rules.emplace_back(std::fs::relative(entry.path(), std::fs::path(path)).string(), entry.path().string()); + this->m_rules.emplace_back(std::fs::relative(entry.path(), path), entry.path()); } } } @@ -207,7 +207,7 @@ namespace hex::plugin::builtin { delete[] ptr; }, - this->m_rules[this->m_selectedRule].second.data()); + fs::toShortPath(this->m_rules[this->m_selectedRule].second).string().data()); fs::File file(this->m_rules[this->m_selectedRule].second, fs::File::Mode::Read);