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>
|
2023-07-30 21:36:48 +02:00
|
|
|
#include <hex/helpers/logger.hpp>
|
2021-09-06 16:15:05 +02:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
#include <wolv/utils/guards.hpp>
|
2023-03-12 18:43:05 +01:00
|
|
|
#include <wolv/utils/string.hpp>
|
2023-03-12 18:27:29 +01:00
|
|
|
|
2021-09-06 16:15:05 +02:00
|
|
|
#include <hex/providers/provider.hpp>
|
|
|
|
|
|
|
|
#include <filesystem>
|
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <magic.h>
|
2023-07-30 21:36:48 +02:00
|
|
|
#include <unistd.h>
|
2021-09-06 16:15:05 +02:00
|
|
|
|
|
|
|
#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)) {
|
2023-10-29 19:43:40 +01:00
|
|
|
for (const auto &entry : std::fs::directory_iterator(dir, error)) {
|
2023-05-06 10:09:23 +02:00
|
|
|
auto path = std::fs::absolute(entry.path());
|
|
|
|
|
2023-10-29 19:43:40 +01:00
|
|
|
if (sourceFiles) {
|
|
|
|
if (path.extension().empty() || entry.is_directory())
|
|
|
|
magicFiles += wolv::util::toUTF8String(path) + MAGIC_PATH_SEPARATOR;
|
|
|
|
} else {
|
|
|
|
if (path.extension() == ".mgc")
|
|
|
|
magicFiles += wolv::util::toUTF8String(path) + MAGIC_PATH_SEPARATOR;
|
2022-06-30 19:39:06 +02:00
|
|
|
}
|
2021-09-06 16:15:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-29 19:43:40 +01:00
|
|
|
if (!magicFiles.empty())
|
|
|
|
magicFiles.pop_back();
|
|
|
|
|
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() {
|
2023-10-29 19:43:40 +01:00
|
|
|
magic_t ctx = magic_open(MAGIC_CHECK);
|
2021-09-06 16:15:05 +02:00
|
|
|
ON_SCOPE_EXIT { magic_close(ctx); };
|
|
|
|
|
|
|
|
auto magicFiles = getMagicFiles(true);
|
|
|
|
|
|
|
|
if (!magicFiles.has_value())
|
|
|
|
return false;
|
|
|
|
|
2023-11-24 11:28:37 +01:00
|
|
|
if (magicFiles->empty())
|
|
|
|
return true;
|
|
|
|
|
2023-07-30 21:36:48 +02:00
|
|
|
std::array<char, 1024> cwd = { 0x00 };
|
|
|
|
if (getcwd(cwd.data(), cwd.size()) == nullptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::optional<std::fs::path> magicFolder;
|
|
|
|
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Magic)) {
|
|
|
|
if (std::fs::exists(dir) && fs::isPathWritable(dir)) {
|
|
|
|
magicFolder = dir;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!magicFolder.has_value()) {
|
|
|
|
log::error("Could not find a writable magic folder");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chdir(wolv::util::toUTF8String(*magicFolder).c_str()) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto result = magic_compile(ctx, magicFiles->c_str()) == 0;
|
2023-10-29 19:43:40 +01:00
|
|
|
if (!result) {
|
|
|
|
log::error("Failed to compile magic files \"{}\": {}", *magicFiles, magic_error(ctx));
|
|
|
|
}
|
2023-07-30 21:36:48 +02:00
|
|
|
|
|
|
|
if (chdir(cwd.data()) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return result;
|
2021-09-06 16:15:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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); };
|
|
|
|
|
2023-01-04 14:03:09 +01:00
|
|
|
if (magic_load(ctx, magicFiles->c_str()) == 0) {
|
|
|
|
if (auto result = magic_buffer(ctx, data.data(), data.size()); result != nullptr)
|
|
|
|
return result;
|
|
|
|
}
|
2021-09-06 16:15:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getDescription(prv::Provider *provider, size_t size) {
|
2023-12-07 13:02:12 +01:00
|
|
|
std::vector<u8> buffer(std::min<u64>(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); };
|
|
|
|
|
2023-01-04 14:03:09 +01:00
|
|
|
if (magic_load(ctx, magicFiles->c_str()) == 0) {
|
|
|
|
if (auto result = magic_buffer(ctx, data.data(), data.size()); result != nullptr)
|
|
|
|
return result;
|
|
|
|
}
|
2021-09-06 16:15:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getMIMEType(prv::Provider *provider, size_t size) {
|
2023-12-07 13:02:12 +01:00
|
|
|
std::vector<u8> buffer(std::min<u64>(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);
|
|
|
|
}
|
|
|
|
|
2023-01-09 08:38:19 +01:00
|
|
|
bool isValidMIMEType(const std::string &mimeType) {
|
|
|
|
// MIME types always contain a slash
|
|
|
|
if (!mimeType.contains("/"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// The MIME type "application/octet-stream" is a fallback type for arbitrary binary data.
|
|
|
|
// Specifying this in a pattern would make it get suggested for every single unknown binary that's being loaded.
|
|
|
|
// We don't want that, so we ignore it here
|
|
|
|
if (mimeType == "application/octet-stream")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-06 16:15:05 +02:00
|
|
|
}
|