diff --git a/cmake/build_helpers.cmake b/cmake/build_helpers.cmake index 9a222b26a..57d227a5f 100644 --- a/cmake/build_helpers.cmake +++ b/cmake/build_helpers.cmake @@ -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) diff --git a/lib/external/pattern_language b/lib/external/pattern_language index d46faa684..51e64e0e1 160000 --- a/lib/external/pattern_language +++ b/lib/external/pattern_language @@ -1 +1 @@ -Subproject commit d46faa68437bd174f54180f08c533f6112588100 +Subproject commit 51e64e0e17fe1c7a767433af3ad88cbf9894c3a5 diff --git a/lib/libimhex/CMakeLists.txt b/lib/libimhex/CMakeLists.txt index ebd5e4c5a..e26d06e08 100644 --- a/lib/libimhex/CMakeLists.txt +++ b/lib/libimhex/CMakeLists.txt @@ -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) diff --git a/plugins/builtin/include/content/command_line_interface.hpp b/plugins/builtin/include/content/command_line_interface.hpp index 3db0f45ee..50ad1120d 100644 --- a/plugins/builtin/include/content/command_line_interface.hpp +++ b/plugins/builtin/include/content/command_line_interface.hpp @@ -15,6 +15,8 @@ namespace hex::plugin::builtin { void handleHashCommand(const std::vector &args); void handleEncodeCommand(const std::vector &args); void handleDecodeCommand(const std::vector &args); + void handleMagicCommand(const std::vector &args); + void handlePatternLanguageCommand(const std::vector &args); void registerCommandForwarders(); diff --git a/plugins/builtin/source/content/command_line_interface.cpp b/plugins/builtin/source/content/command_line_interface.cpp index 6d981956a..81d2722bc 100644 --- a/plugins/builtin/source/content/command_line_interface.cpp +++ b/plugins/builtin/source/content/command_line_interface.cpp @@ -6,7 +6,9 @@ #include #include +#include #include +#include #include #include @@ -16,8 +18,12 @@ #include "content/helpers/math_evaluator.hpp" +#include + namespace hex::plugin::builtin { + using namespace hex::literals; + void handleVersionCommand(const std::vector &args) { hex::unused(args); @@ -212,6 +218,50 @@ namespace hex::plugin::builtin { std::exit(EXIT_SUCCESS); } + void handleMagicCommand(const std::vector &args) { + if (args.size() != 2) { + hex::print("usage: imhex --magic "); + 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 &args) { + auto processedArgs = args; + processedArgs.insert(processedArgs.begin(), "imhex"); + + std::exit(pl::cli::executeCommandLineInterface(processedArgs)); + } + void registerCommandForwarders() { hex::subcommands::registerSubCommand("open", [](const std::vector &args){ diff --git a/plugins/builtin/source/plugin_builtin.cpp b/plugins/builtin/source/plugin_builtin.cpp index 5f10b5043..be1a5841d 100644 --- a/plugins/builtin/source/plugin_builtin.cpp +++ b/plugins/builtin/source/plugin_builtin.cpp @@ -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") {