1
0
mirror of synced 2025-01-29 19:17:28 +01:00

feat: Added --pl and --magic command

This commit is contained in:
WerWolv 2023-07-17 10:43:29 +02:00
parent b9ec1a150d
commit a83ca3c228
6 changed files with 61 additions and 6 deletions

View File

@ -564,6 +564,7 @@ macro(addBundledLibraries)
pkg_search_module(CAPSTONE 4.0.2 REQUIRED capstone)
endif()
set(LIBPL_BUILD_CLI_AS_EXECUTABLE OFF)
add_subdirectory(${EXTERN_LIBS_FOLDER}/pattern_language EXCLUDE_FROM_ALL)
set_target_properties(libpl PROPERTIES POSITION_INDEPENDENT_CODE ON)

@ -1 +1 @@
Subproject commit d46faa68437bd174f54180f08c533f6112588100
Subproject commit 51e64e0e17fe1c7a767433af3ad88cbf9894c3a5

View File

@ -73,7 +73,7 @@ elseif (APPLE)
endif ()
target_link_libraries(libimhex PRIVATE ${FMT_LIBRARIES})
target_link_libraries(libimhex PUBLIC dl imgui ${NFD_LIBRARIES} magic ${CAPSTONE_LIBRARIES} LLVMDemangle microtar ${NLOHMANN_JSON_LIBRARIES} ${YARA_LIBRARIES} ${LIBCURL_LIBRARIES} ${MBEDTLS_LIBRARIES} ${LIBBACKTRACE_LIBRARIES} libpl libpl-gen ${MINIAUDIO_LIBRARIES} libwolv-utils libwolv-io libwolv-hash libwolv-net libwolv-containers)
target_link_libraries(libimhex PUBLIC dl imgui ${NFD_LIBRARIES} magic ${CAPSTONE_LIBRARIES} LLVMDemangle microtar ${NLOHMANN_JSON_LIBRARIES} ${YARA_LIBRARIES} ${LIBCURL_LIBRARIES} ${MBEDTLS_LIBRARIES} ${LIBBACKTRACE_LIBRARIES} plcli libpl libpl-gen ${MINIAUDIO_LIBRARIES} libwolv-utils libwolv-io libwolv-hash libwolv-net libwolv-containers)
if (DEFINED IMHEX_COMMIT_HASH_LONG AND DEFINED IMHEX_COMMIT_HASH_SHORT AND DEFINED IMHEX_COMMIT_BRANCH)

View File

@ -15,6 +15,8 @@ namespace hex::plugin::builtin {
void handleHashCommand(const std::vector<std::string> &args);
void handleEncodeCommand(const std::vector<std::string> &args);
void handleDecodeCommand(const std::vector<std::string> &args);
void handleMagicCommand(const std::vector<std::string> &args);
void handlePatternLanguageCommand(const std::vector<std::string> &args);
void registerCommandForwarders();

View File

@ -6,7 +6,9 @@
#include <hex/helpers/fmt.hpp>
#include <fmt/color.h>
#include <hex/helpers/magic.hpp>
#include <hex/helpers/crypto.hpp>
#include <hex/helpers/literals.hpp>
#include <romfs/romfs.hpp>
#include <hex/api/plugin_manager.hpp>
@ -16,8 +18,12 @@
#include "content/helpers/math_evaluator.hpp"
#include <pl/cli/cli.hpp>
namespace hex::plugin::builtin {
using namespace hex::literals;
void handleVersionCommand(const std::vector<std::string> &args) {
hex::unused(args);
@ -212,6 +218,50 @@ namespace hex::plugin::builtin {
std::exit(EXIT_SUCCESS);
}
void handleMagicCommand(const std::vector<std::string> &args) {
if (args.size() != 2) {
hex::print("usage: imhex --magic <operation> <file>");
std::exit(EXIT_FAILURE);
}
if (!magic::compile()) {
hex::print("Failed to compile magic database!");
std::exit(EXIT_FAILURE);
}
const auto &operation = args[0];
const auto &filePath = std::fs::path(args[1]);
wolv::io::File file(filePath, wolv::io::File::Mode::Read);
if (!file.isValid()) {
hex::print("Failed to open file: {}", wolv::util::toUTF8String(filePath));
std::exit(EXIT_FAILURE);
}
auto data = file.readVector(std::min(file.getSize(), 100_KiB));
if (operation == "mime") {
auto result = magic::getMIMEType(data);
hex::print("{}", result);
} else if (operation == "desc") {
auto result = magic::getDescription(data);
hex::print("{}", result);
} else {
hex::print("Unknown operation: {}", operation);
hex::print("Available operations: mime, desc");
std::exit(EXIT_FAILURE);
}
std::exit(EXIT_SUCCESS);
}
void handlePatternLanguageCommand(const std::vector<std::string> &args) {
auto processedArgs = args;
processedArgs.insert(processedArgs.begin(), "imhex");
std::exit(pl::cli::executeCommandLineInterface(processedArgs));
}
void registerCommandForwarders() {
hex::subcommands::registerSubCommand("open", [](const std::vector<std::string> &args){

View File

@ -55,10 +55,12 @@ IMHEX_PLUGIN_SUBCOMMANDS() {
{ "open", "Open files passed as argument. [default]", hex::plugin::builtin::handleOpenCommand },
{ "calc", "Evaluate a mathematical expression", hex::plugin::builtin::handleCalcCommand },
{ "hash", "Calculate the hash of a file", hex::plugin::builtin::handleHashCommand },
{ "encode", "Encode a string", hex::plugin::builtin::handleEncodeCommand },
{ "decode", "Decode a string", hex::plugin::builtin::handleDecodeCommand },
{ "calc", "Evaluate a mathematical expression", hex::plugin::builtin::handleCalcCommand },
{ "hash", "Calculate the hash of a file", hex::plugin::builtin::handleHashCommand },
{ "encode", "Encode a string", hex::plugin::builtin::handleEncodeCommand },
{ "decode", "Decode a string", hex::plugin::builtin::handleDecodeCommand },
{ "magic", "Identify file types", hex::plugin::builtin::handleMagicCommand },
{ "pl", "Interact with the pattern language", hex::plugin::builtin::handlePatternLanguageCommand },
};
IMHEX_PLUGIN_SETUP("Built-in", "WerWolv", "Default ImHex functionality") {