sys: Cleanup libmagic mess

This commit is contained in:
WerWolv
2021-09-06 16:15:05 +02:00
parent f29febdc86
commit 6879cf765f
8 changed files with 157 additions and 80 deletions

View File

@@ -3,6 +3,7 @@
#include <hex/providers/provider.hpp>
#include <hex/helpers/paths.hpp>
#include <hex/helpers/fmt.hpp>
#include <hex/helpers/literals.hpp>
#include <cstring>
#include <cmath>
@@ -12,13 +13,15 @@
#include <thread>
#include <vector>
#include <magic.h>
#include <hex/helpers/magic.hpp>
#include <imgui_imhex_extensions.h>
#include <implot.h>
namespace hex {
using namespace hex::literals;
ViewInformation::ViewInformation() : View("hex.view.information.name") {
EventManager::subscribe<EventDataChanged>(this, [this]() {
this->m_dataValid = false;
@@ -93,47 +96,9 @@ namespace hex {
}
{
std::vector<u8> buffer(provider->getSize(), 0x00);
provider->readRelative(0x00, buffer.data(), buffer.size());
this->m_fileDescription.clear();
this->m_mimeType.clear();
std::string magicFiles;
std::error_code error;
for (const auto &dir : hex::getPath(ImHexPath::Magic)) {
for (const auto &entry : std::filesystem::directory_iterator(dir, error)) {
if (entry.is_regular_file() && entry.path().extension() == ".mgc")
magicFiles += entry.path().string() + MAGIC_PATH_SEPARATOR;
}
}
if (!error) {
magicFiles.pop_back();
{
magic_t cookie = magic_open(MAGIC_NONE);
if (magic_load(cookie, magicFiles.c_str()) != -1)
this->m_fileDescription = magic_buffer(cookie, buffer.data(), buffer.size());
else
this->m_fileDescription = "";
magic_close(cookie);
}
{
magic_t cookie = magic_open(MAGIC_MIME);
if (magic_load(cookie, magicFiles.c_str()) != -1)
this->m_mimeType = magic_buffer(cookie, buffer.data(), buffer.size());
else
this->m_mimeType = "";
magic_close(cookie);
}
this->m_dataValid = true;
}
this->m_fileDescription = magic::getDescription(provider);
this->m_mimeType = magic::getMIMEType(provider);
this->m_dataValid = true;
}
this->m_analyzing = false;

View File

@@ -5,7 +5,8 @@
#include <hex/lang/pattern_data.hpp>
#include <hex/helpers/paths.hpp>
#include <magic.h>
#include <hex/helpers/magic.hpp>
#include <hex/helpers/literals.hpp>
#include <imgui_imhex_extensions.h>
@@ -13,6 +14,8 @@
namespace hex {
using namespace hex::literals;
static const TextEditor::LanguageDefinition& PatternLanguage() {
static bool initialized = false;
static TextEditor::LanguageDefinition langDef;
@@ -101,37 +104,12 @@ namespace hex {
return;
lang::Preprocessor preprocessor;
std::string magicFiles;
std::error_code error;
for (const auto &dir : hex::getPath(ImHexPath::Magic)) {
if (!std::filesystem::is_directory(dir))
continue;
for (const auto &entry : std::filesystem::directory_iterator(dir, error)) {
if (entry.is_regular_file() && entry.path().extension() == ".mgc")
magicFiles += entry.path().string() + MAGIC_PATH_SEPARATOR;
}
}
if (error)
return;
auto provider = SharedData::currentProvider;
if (provider == nullptr)
return;
std::vector<u8> buffer(std::min(provider->getSize(), size_t(0xFFFF)), 0x00);
provider->readRelative(0, buffer.data(), buffer.size());
std::string mimeType;
magic_t cookie = magic_open(MAGIC_MIME_TYPE);
if (magic_load(cookie, magicFiles.c_str()) != -1)
mimeType = magic_buffer(cookie, buffer.data(), buffer.size());
magic_close(cookie);
std::string mimeType = magic::getMIMEType(provider);
bool foundCorrectType = false;
preprocessor.addPragmaHandler("MIME", [&mimeType, &foundCorrectType](std::string value) {