From a166bf4ed81820f557dc347725295d4946edb009 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 12 Nov 2020 21:21:11 +0100 Subject: [PATCH] Improved libmagic usage. Now supports additional databases --- source/views/view_information.cpp | 56 +++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/source/views/view_information.cpp b/source/views/view_information.cpp index 2ca7b3d96..99984f429 100644 --- a/source/views/view_information.cpp +++ b/source/views/view_information.cpp @@ -4,13 +4,20 @@ #include "utils.hpp" -#include -#include #include #include +#include +#include +#include + #include -#include + +#if defined(__EMX__) || defined (WIN32) +#define MAGIC_PATH_SEPARATOR ";" +#else +#define MAGIC_PATH_SEPARATOR ":" +#endif namespace hex { @@ -71,20 +78,43 @@ namespace hex { std::vector buffer(this->m_dataProvider->getSize(), 0x00); this->m_dataProvider->read(0x00, buffer.data(), buffer.size()); + this->m_fileDescription.clear(); + this->m_mimeType.clear(); + + std::string magicFiles; + + for (const auto &entry : std::filesystem::directory_iterator("magic")) { + if (entry.is_regular_file() && entry.path().extension() == ".mgc") + magicFiles += entry.path().string() + MAGIC_PATH_SEPARATOR; + } + magicFiles.pop_back(); + { magic_t cookie = magic_open(MAGIC_NONE); - magic_load(cookie, "magic"); + if (magic_load(cookie, magicFiles.c_str()) == -1) + goto skip_description; + this->m_fileDescription = magic_buffer(cookie, buffer.data(), buffer.size()); + + skip_description: + magic_close(cookie); } + { magic_t cookie = magic_open(MAGIC_MIME); - magic_load(cookie, "magic"); + if (magic_load(cookie, magicFiles.c_str()) == -1) + goto skip_mime; + this->m_mimeType = magic_buffer(cookie, buffer.data(), buffer.size()); + + skip_mime: + magic_close(cookie); } + this->m_shouldInvalidate = false; } } @@ -115,13 +145,17 @@ namespace hex { ImGui::Separator(); ImGui::NewLine(); - ImGui::TextUnformatted("Description:"); - ImGui::TextWrapped("%s", this->m_fileDescription.c_str()); - ImGui::NewLine(); - ImGui::TextUnformatted("MIME Type:"); - ImGui::TextWrapped("%s", this->m_mimeType.c_str()); + if (!this->m_fileDescription.empty()) { + ImGui::TextUnformatted("Description:"); + ImGui::TextWrapped("%s", this->m_fileDescription.c_str()); + ImGui::NewLine(); + } - ImGui::NewLine(); + if (!this->m_mimeType.empty()) { + ImGui::TextUnformatted("MIME Type:"); + ImGui::TextWrapped("%s", this->m_mimeType.c_str()); + ImGui::NewLine(); + } } ImGui::EndChild();