diff --git a/plugins/builtin/include/content/command_line_interface.hpp b/plugins/builtin/include/content/command_line_interface.hpp index 50ad1120d..c2381f1a6 100644 --- a/plugins/builtin/include/content/command_line_interface.hpp +++ b/plugins/builtin/include/content/command_line_interface.hpp @@ -17,6 +17,7 @@ namespace hex::plugin::builtin { void handleDecodeCommand(const std::vector &args); void handleMagicCommand(const std::vector &args); void handlePatternLanguageCommand(const std::vector &args); + void handleHexdumpCommand(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 41800f4db..d55d291de 100644 --- a/plugins/builtin/source/content/command_line_interface.cpp +++ b/plugins/builtin/source/content/command_line_interface.cpp @@ -1,5 +1,6 @@ #include "content/command_line_interface.hpp" +#include #include #include @@ -11,6 +12,7 @@ #include #include +#include #include #include @@ -274,6 +276,32 @@ namespace hex::plugin::builtin { std::exit(pl::cli::executeCommandLineInterface(processedArgs)); } + void handleHexdumpCommand(const std::vector &args) { + if (args.size() < 1 || args.size() > 3) { + log::println("usage: imhex --hexdump "); + std::exit(EXIT_FAILURE); + } + + std::fs::path filePath = reinterpret_cast(args[0].data()); + + FileProvider provider; + + provider.setPath(filePath); + if (!provider.open()) { + log::println("Failed to open file '{}'", args[0]); + std::exit(EXIT_FAILURE); + } + + u64 startAddress = args.size() >= 2 ? std::stoull(args[1], nullptr, 0) : 0x00; + u64 size = args.size() >= 3 ? std::stoull(args[2], nullptr, 0) : provider.getActualSize(); + + size = std::min(size, provider.getActualSize()); + + log::print("{}", hex::generateHexView(startAddress, size - startAddress, &provider)); + + std::exit(EXIT_SUCCESS); + } + 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 163a38c3c..6748bd61d 100644 --- a/plugins/builtin/source/plugin_builtin.cpp +++ b/plugins/builtin/source/plugin_builtin.cpp @@ -66,6 +66,7 @@ IMHEX_PLUGIN_SUBCOMMANDS() { { "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 }, + { "hexdump", "Generate a hex dump of the provided file", hex::plugin::builtin::handleHexdumpCommand }, }; IMHEX_PLUGIN_SETUP("Built-in", "WerWolv", "Default ImHex functionality") {