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
|
|
|
}
|
|
|
|
|
2024-01-25 20:53:08 +01:00
|
|
|
std::string getDescription(const std::vector<u8> &data, bool firstEntryOnly) {
|
2024-02-01 22:35:24 +01:00
|
|
|
if (data.empty()) return "";
|
|
|
|
|
2021-09-06 16:15:05 +02:00
|
|
|
auto magicFiles = getMagicFiles();
|
|
|
|
|
|
|
|
if (magicFiles.has_value()) {
|
2024-02-02 23:22:25 +01:00
|
|
|
magic_t ctx = magic_open(firstEntryOnly ? MAGIC_NONE : MAGIC_CONTINUE);
|
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) {
|
2024-05-19 10:21:02 +02:00
|
|
|
if (auto description = magic_buffer(ctx, data.data(), data.size()); description != nullptr) {
|
|
|
|
auto result = wolv::util::replaceStrings(description, "\\012-", "\n-");
|
|
|
|
if (result.ends_with("- data"))
|
|
|
|
result = result.substr(0, result.size() - 6);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2023-01-04 14:03:09 +01:00
|
|
|
}
|
2021-09-06 16:15:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2024-02-21 00:06:52 +01:00
|
|
|
std::string getDescription(prv::Provider *provider, u64 address, size_t size, bool firstEntryOnly) {
|
2023-12-07 13:02:12 +01:00
|
|
|
std::vector<u8> buffer(std::min<u64>(provider->getSize(), size), 0x00);
|
2024-02-24 23:53:34 +01:00
|
|
|
provider->read(address, buffer.data(), buffer.size());
|
2021-09-06 16:15:05 +02:00
|
|
|
|
2024-01-25 20:53:08 +01:00
|
|
|
return getDescription(buffer, firstEntryOnly);
|
2021-09-06 16:15:05 +02:00
|
|
|
}
|
|
|
|
|
2024-01-25 20:53:08 +01:00
|
|
|
std::string getMIMEType(const std::vector<u8> &data, bool firstEntryOnly) {
|
2024-02-01 22:35:24 +01:00
|
|
|
if (data.empty()) return "";
|
|
|
|
|
2021-09-06 16:15:05 +02:00
|
|
|
auto magicFiles = getMagicFiles();
|
|
|
|
|
|
|
|
if (magicFiles.has_value()) {
|
2024-02-02 23:22:25 +01:00
|
|
|
magic_t ctx = magic_open(MAGIC_MIME_TYPE | (firstEntryOnly ? MAGIC_NONE : MAGIC_CONTINUE));
|
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) {
|
2024-05-19 10:21:02 +02:00
|
|
|
if (auto mimeType = magic_buffer(ctx, data.data(), data.size()); mimeType != nullptr) {
|
|
|
|
auto result = wolv::util::replaceStrings(mimeType, "\\012-", "\n-");
|
|
|
|
if (result.ends_with("- application/octet-stream"))
|
|
|
|
result = result.substr(0, result.size() - 26);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2023-01-04 14:03:09 +01:00
|
|
|
}
|
2021-09-06 16:15:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2024-02-21 00:06:52 +01:00
|
|
|
std::string getMIMEType(prv::Provider *provider, u64 address, size_t size, bool firstEntryOnly) {
|
2024-02-01 22:35:24 +01:00
|
|
|
std::vector<u8> buffer(std::min<u64>(provider->getSize(), size), 0x00);
|
2024-02-24 23:53:34 +01:00
|
|
|
provider->read(address, buffer.data(), buffer.size());
|
2024-02-01 22:35:24 +01:00
|
|
|
|
|
|
|
return getMIMEType(buffer, firstEntryOnly);
|
|
|
|
}
|
|
|
|
|
2024-02-21 00:06:52 +01:00
|
|
|
std::string getExtensions(prv::Provider *provider, u64 address, size_t size, bool firstEntryOnly) {
|
2023-12-07 13:02:12 +01:00
|
|
|
std::vector<u8> buffer(std::min<u64>(provider->getSize(), size), 0x00);
|
2024-02-24 23:53:34 +01:00
|
|
|
provider->read(address, buffer.data(), buffer.size());
|
2021-09-06 16:15:05 +02:00
|
|
|
|
2024-01-25 20:53:08 +01:00
|
|
|
return getExtensions(buffer, firstEntryOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getExtensions(const std::vector<u8> &data, bool firstEntryOnly) {
|
2024-02-01 22:35:24 +01:00
|
|
|
if (data.empty()) return "";
|
|
|
|
|
2024-01-25 20:53:08 +01:00
|
|
|
auto magicFiles = getMagicFiles();
|
|
|
|
|
|
|
|
if (magicFiles.has_value()) {
|
|
|
|
magic_t ctx = magic_open(MAGIC_EXTENSION | (firstEntryOnly ? MAGIC_NONE : MAGIC_CONTINUE));
|
|
|
|
ON_SCOPE_EXIT { magic_close(ctx); };
|
|
|
|
|
|
|
|
if (magic_load(ctx, magicFiles->c_str()) == 0) {
|
2024-05-19 10:21:02 +02:00
|
|
|
if (auto extension = magic_buffer(ctx, data.data(), data.size()); extension != nullptr) {
|
|
|
|
auto result = wolv::util::replaceStrings(extension, "\\012-", "\n-");
|
|
|
|
if (result.ends_with("- ???"))
|
|
|
|
result = result.substr(0, result.size() - 5);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2024-01-25 20:53:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2024-02-21 00:06:52 +01:00
|
|
|
std::string getAppleCreatorType(prv::Provider *provider, u64 address, size_t size, bool firstEntryOnly) {
|
2024-01-25 20:53:08 +01:00
|
|
|
std::vector<u8> buffer(std::min<u64>(provider->getSize(), size), 0x00);
|
2024-02-24 23:53:34 +01:00
|
|
|
provider->read(address, buffer.data(), buffer.size());
|
2024-01-25 20:53:08 +01:00
|
|
|
|
|
|
|
return getAppleCreatorType(buffer, firstEntryOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getAppleCreatorType(const std::vector<u8> &data, bool firstEntryOnly) {
|
2024-02-01 22:35:24 +01:00
|
|
|
if (data.empty()) return "";
|
|
|
|
|
2024-01-25 20:53:08 +01:00
|
|
|
auto magicFiles = getMagicFiles();
|
|
|
|
|
|
|
|
if (magicFiles.has_value()) {
|
|
|
|
magic_t ctx = magic_open(MAGIC_APPLE | (firstEntryOnly ? MAGIC_NONE : MAGIC_CONTINUE));
|
|
|
|
ON_SCOPE_EXIT { magic_close(ctx); };
|
|
|
|
|
|
|
|
if (magic_load(ctx, magicFiles->c_str()) == 0) {
|
|
|
|
if (auto result = magic_buffer(ctx, data.data(), data.size()); result != nullptr)
|
|
|
|
return wolv::util::replaceStrings(result, "\\012-", "\n-");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|