sys: Cleanup libmagic mess
This commit is contained in:
parent
f29febdc86
commit
6879cf765f
@ -99,9 +99,9 @@ set_target_properties(imhex PROPERTIES CXX_VISIBILITY_PRESET hidden)
|
||||
target_link_directories(imhex PRIVATE ${CAPSTONE_LIBRARY_DIRS} ${MAGIC_LIBRARY_DIRS})
|
||||
|
||||
if (WIN32)
|
||||
target_link_libraries(imhex magic ${CMAKE_DL_LIBS} capstone LLVMDemangle libimhex ${Python_LIBRARIES} wsock32 ws2_32 libyara Dwmapi.lib dl)
|
||||
target_link_libraries(imhex ${CMAKE_DL_LIBS} capstone LLVMDemangle libimhex ${Python_LIBRARIES} wsock32 ws2_32 libyara Dwmapi.lib dl)
|
||||
else ()
|
||||
target_link_libraries(imhex magic ${CMAKE_DL_LIBS} capstone LLVMDemangle libimhex ${Python_LIBRARIES} dl pthread libyara)
|
||||
target_link_libraries(imhex ${CMAKE_DL_LIBS} capstone LLVMDemangle libimhex ${Python_LIBRARIES} dl pthread libyara)
|
||||
endif ()
|
||||
|
||||
createPackage()
|
||||
|
@ -47,6 +47,7 @@ set(LIBIMHEX_SOURCES
|
||||
|
||||
source/helpers/utils.cpp
|
||||
source/helpers/paths.cpp
|
||||
source/helpers/magic.cpp
|
||||
source/helpers/shared_data.cpp
|
||||
source/helpers/crypto.cpp
|
||||
source/helpers/lang.cpp
|
||||
@ -93,9 +94,9 @@ target_link_directories(libimhex PUBLIC ${MBEDTLS_LIBRARY_DIR})
|
||||
|
||||
if (APPLE)
|
||||
find_library(FOUNDATION NAMES Foundation)
|
||||
target_link_libraries(libimhex PUBLIC imgui nlohmann_json mbedcrypto ${FOUNDATION} nfd fmt-header-only libcurl)
|
||||
target_link_libraries(libimhex PUBLIC imgui nlohmann_json mbedcrypto ${FOUNDATION} nfd fmt-header-only libcurl magic)
|
||||
else ()
|
||||
target_link_libraries(libimhex PUBLIC imgui nlohmann_json mbedcrypto nfd)
|
||||
target_link_libraries(libimhex PUBLIC imgui nlohmann_json mbedcrypto nfd magic)
|
||||
|
||||
if (NOT USE_SYSTEM_FMT)
|
||||
target_link_libraries(libimhex PUBLIC fmt-header-only)
|
||||
|
@ -23,10 +23,4 @@ using s128 = __int128_t;
|
||||
struct Region {
|
||||
u64 address;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
#ifdef OS_WINDOWS
|
||||
#define MAGIC_PATH_SEPARATOR ";"
|
||||
#else
|
||||
#define MAGIC_PATH_SEPARATOR ":"
|
||||
#endif
|
||||
};
|
23
plugins/libimhex/include/hex/helpers/literals.hpp
Normal file
23
plugins/libimhex/include/hex/helpers/literals.hpp
Normal file
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
namespace hex::literals {
|
||||
|
||||
/* Byte literals */
|
||||
|
||||
unsigned long long operator ""_Bytes(unsigned long long bytes) {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
unsigned long long operator ""_kiB(unsigned long long kiB) {
|
||||
return operator ""_Bytes(kiB * 1024);
|
||||
}
|
||||
|
||||
unsigned long long operator ""_MiB(unsigned long long MiB) {
|
||||
return operator ""_kiB(MiB * 1024);
|
||||
}
|
||||
|
||||
unsigned long long operator ""_GiB(unsigned long long GiB) {
|
||||
return operator ""_MiB(GiB * 1024);
|
||||
}
|
||||
|
||||
}
|
22
plugins/libimhex/include/hex/helpers/magic.hpp
Normal file
22
plugins/libimhex/include/hex/helpers/magic.hpp
Normal file
@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <hex.hpp>
|
||||
|
||||
#include <hex/helpers/literals.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace hex::prv { class Provider; }
|
||||
|
||||
namespace hex::magic {
|
||||
|
||||
using namespace hex::literals;
|
||||
|
||||
bool compile();
|
||||
std::string getDescription(const std::vector<u8> &data);
|
||||
std::string getDescription(prv::Provider *provider, size_t size = 5_MiB);
|
||||
std::string getMIMEType(const std::vector<u8> &data);
|
||||
std::string getMIMEType(prv::Provider *provider, size_t size = 5_MiB);
|
||||
|
||||
}
|
94
plugins/libimhex/source/helpers/magic.cpp
Normal file
94
plugins/libimhex/source/helpers/magic.cpp
Normal file
@ -0,0 +1,94 @@
|
||||
#include <hex/helpers/magic.hpp>
|
||||
|
||||
#include <hex/helpers/utils.hpp>
|
||||
#include <hex/helpers/paths.hpp>
|
||||
|
||||
#include <hex/providers/provider.hpp>
|
||||
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include <magic.h>
|
||||
|
||||
#if defined(OS_WINDOWS)
|
||||
#define MAGIC_PATH_SEPARATOR ";"
|
||||
#else
|
||||
#define MAGIC_PATH_SEPARATOR ":"
|
||||
#endif
|
||||
|
||||
|
||||
namespace hex::magic {
|
||||
|
||||
static std::optional<std::string> getMagicFiles(bool sourceFiles = false) {
|
||||
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() && ((sourceFiles && entry.path().extension().empty()) || (!sourceFiles && entry.path().extension() == ".mgc")))
|
||||
magicFiles += entry.path().string() + MAGIC_PATH_SEPARATOR;
|
||||
}
|
||||
}
|
||||
|
||||
if (error)
|
||||
return { };
|
||||
else
|
||||
return magicFiles;
|
||||
}
|
||||
|
||||
bool compile() {
|
||||
magic_t ctx = magic_open(MAGIC_NONE);
|
||||
ON_SCOPE_EXIT { magic_close(ctx); };
|
||||
|
||||
auto magicFiles = getMagicFiles(true);
|
||||
|
||||
if (!magicFiles.has_value())
|
||||
return false;
|
||||
|
||||
return magic_compile(ctx, magicFiles->c_str()) == 0;
|
||||
}
|
||||
|
||||
std::string getDescription(const std::vector<u8> &data) {
|
||||
auto magicFiles = getMagicFiles();
|
||||
|
||||
if (magicFiles.has_value()) {
|
||||
magic_t ctx = magic_open(MAGIC_NONE);
|
||||
ON_SCOPE_EXIT { magic_close(ctx); };
|
||||
|
||||
if (magic_load(ctx, magicFiles->c_str()))
|
||||
return magic_buffer(ctx, data.data(), data.size()) ?: "";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string getDescription(prv::Provider *provider, size_t size) {
|
||||
std::vector<u8> buffer(std::min(provider->getSize(), size), 0x00);
|
||||
provider->readRelative(0x00, buffer.data(), buffer.size());
|
||||
|
||||
return getDescription(buffer);
|
||||
}
|
||||
|
||||
std::string getMIMEType(const std::vector<u8> &data){
|
||||
auto magicFiles = getMagicFiles();
|
||||
|
||||
if (magicFiles.has_value()) {
|
||||
magic_t ctx = magic_open(MAGIC_MIME);
|
||||
ON_SCOPE_EXIT { magic_close(ctx); };
|
||||
|
||||
if (magic_load(ctx, magicFiles->c_str()))
|
||||
return magic_buffer(ctx, data.data(), data.size()) ?: "";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string getMIMEType(prv::Provider *provider, size_t size) {
|
||||
std::vector<u8> buffer(std::min(provider->getSize(), size), 0x00);
|
||||
provider->readRelative(0x00, buffer.data(), buffer.size());
|
||||
|
||||
return getMIMEType(buffer);
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user