mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-01 21:17:44 -05:00
sys: Cleanup libmagic mess
This commit is contained in:
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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user