2021-09-06 16:15:05 +02:00
|
|
|
#include <hex/helpers/magic.hpp>
|
|
|
|
|
|
|
|
#include <hex/helpers/utils.hpp>
|
2022-03-04 11:36:37 +01:00
|
|
|
#include <hex/helpers/fs.hpp>
|
2021-09-06 16:15:05 +02:00
|
|
|
|
|
|
|
#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;
|
2022-03-04 11:36:37 +01:00
|
|
|
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Magic)) {
|
|
|
|
for (const auto &entry : std::fs::directory_iterator(dir, error)) {
|
2022-06-30 19:39:06 +02:00
|
|
|
if (entry.is_regular_file() && ((sourceFiles && entry.path().extension().empty()) || (!sourceFiles && entry.path().extension() == ".mgc"))) {
|
2022-10-04 09:10:58 +02:00
|
|
|
magicFiles += hex::toUTF8String(fs::toShortPath(entry.path())) + MAGIC_PATH_SEPARATOR;
|
2022-06-30 19:39:06 +02:00
|
|
|
}
|
2021-09-06 16:15:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error)
|
2022-01-30 12:43:43 +01:00
|
|
|
return std::nullopt;
|
2021-09-06 16:15:05 +02:00
|
|
|
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); };
|
|
|
|
|
2021-09-09 01:56:48 +02:00
|
|
|
if (magic_load(ctx, magicFiles->c_str()) == 0)
|
2021-09-06 16:15:05 +02:00
|
|
|
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);
|
2021-12-09 21:10:24 +01:00
|
|
|
provider->read(provider->getBaseAddress(), buffer.data(), buffer.size());
|
2021-09-06 16:15:05 +02:00
|
|
|
|
|
|
|
return getDescription(buffer);
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
std::string getMIMEType(const std::vector<u8> &data) {
|
2021-09-06 16:15:05 +02:00
|
|
|
auto magicFiles = getMagicFiles();
|
|
|
|
|
|
|
|
if (magicFiles.has_value()) {
|
2021-09-26 21:18:25 +02:00
|
|
|
magic_t ctx = magic_open(MAGIC_MIME_TYPE);
|
2021-09-06 16:15:05 +02:00
|
|
|
ON_SCOPE_EXIT { magic_close(ctx); };
|
|
|
|
|
2021-09-09 01:56:48 +02:00
|
|
|
if (magic_load(ctx, magicFiles->c_str()) == 0)
|
2021-09-06 16:15:05 +02:00
|
|
|
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);
|
2021-12-09 21:10:24 +01:00
|
|
|
provider->read(provider->getBaseAddress(), buffer.data(), buffer.size());
|
2021-09-06 16:15:05 +02:00
|
|
|
|
|
|
|
return getMIMEType(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|