2023-07-16 23:46:41 +02:00
|
|
|
#include "content/command_line_interface.hpp"
|
|
|
|
|
2023-12-05 16:45:35 +01:00
|
|
|
#include <content/providers/file_provider.hpp>
|
2023-07-16 23:46:41 +02:00
|
|
|
#include <hex/api/imhex_api.hpp>
|
2023-11-18 14:50:43 +01:00
|
|
|
#include <hex/api/event_manager.hpp>
|
2023-07-16 23:46:41 +02:00
|
|
|
|
|
|
|
#include <hex/helpers/fmt.hpp>
|
|
|
|
|
2023-07-17 10:43:29 +02:00
|
|
|
#include <hex/helpers/magic.hpp>
|
2023-07-16 23:46:41 +02:00
|
|
|
#include <hex/helpers/crypto.hpp>
|
2023-07-17 10:43:29 +02:00
|
|
|
#include <hex/helpers/literals.hpp>
|
2024-03-10 15:58:57 +01:00
|
|
|
#include <hex/helpers/utils.hpp>
|
2023-07-16 23:46:41 +02:00
|
|
|
#include <romfs/romfs.hpp>
|
|
|
|
|
|
|
|
#include <hex/api/plugin_manager.hpp>
|
2024-01-22 12:53:07 +01:00
|
|
|
#include <hex/api/task_manager.hpp>
|
2023-07-16 23:46:41 +02:00
|
|
|
#include <hex/subcommands/subcommands.hpp>
|
|
|
|
|
|
|
|
#include <wolv/utils/string.hpp>
|
2023-12-01 14:07:10 +01:00
|
|
|
#include <wolv/math_eval/math_evaluator.hpp>
|
2023-07-16 23:46:41 +02:00
|
|
|
|
2023-07-17 10:43:29 +02:00
|
|
|
#include <pl/cli/cli.hpp>
|
2023-12-15 08:11:51 +01:00
|
|
|
#include <llvm/Demangle/Demangle.h>
|
2023-07-17 10:43:29 +02:00
|
|
|
|
2023-07-16 23:46:41 +02:00
|
|
|
namespace hex::plugin::builtin {
|
2023-07-17 10:43:29 +02:00
|
|
|
using namespace hex::literals;
|
|
|
|
|
2023-07-16 23:46:41 +02:00
|
|
|
void handleVersionCommand(const std::vector<std::string> &args) {
|
|
|
|
hex::unused(args);
|
|
|
|
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::print(std::string(romfs::get("logo.ans").string()),
|
2023-07-16 23:46:41 +02:00
|
|
|
ImHexApi::System::getImHexVersion(),
|
|
|
|
ImHexApi::System::getCommitBranch(), ImHexApi::System::getCommitHash(),
|
|
|
|
__DATE__, __TIME__,
|
|
|
|
ImHexApi::System::isPortableVersion() ? "Portable" : "Installed");
|
|
|
|
|
|
|
|
std::exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleHelpCommand(const std::vector<std::string> &args) {
|
|
|
|
hex::unused(args);
|
|
|
|
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::print(
|
2023-07-16 23:46:41 +02:00
|
|
|
"ImHex - A Hex Editor for Reverse Engineers, Programmers and people who value their retinas when working at 3 AM.\n"
|
|
|
|
"\n"
|
|
|
|
"usage: imhex [subcommand] [options]\n"
|
|
|
|
"Available subcommands:\n"
|
|
|
|
);
|
|
|
|
|
2024-03-14 18:24:31 +01:00
|
|
|
size_t longestLongCommand = 0, longestShortCommand = 0;
|
2023-07-16 23:46:41 +02:00
|
|
|
for (const auto &plugin : PluginManager::getPlugins()) {
|
|
|
|
for (const auto &subCommand : plugin.getSubCommands()) {
|
2024-03-14 18:24:31 +01:00
|
|
|
longestLongCommand = std::max(longestLongCommand, subCommand.commandLong.size());
|
|
|
|
longestShortCommand = std::max(longestShortCommand, subCommand.commandShort.size());
|
2023-07-16 23:46:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &plugin : PluginManager::getPlugins()) {
|
|
|
|
for (const auto &subCommand : plugin.getSubCommands()) {
|
2024-03-14 18:24:31 +01:00
|
|
|
hex::log::println(" "
|
|
|
|
"{}"
|
|
|
|
"{: <{}}"
|
|
|
|
"{}"
|
|
|
|
"{}"
|
|
|
|
"{: <{}}"
|
|
|
|
"{}",
|
|
|
|
subCommand.commandShort.empty() ? " " : "-",
|
|
|
|
subCommand.commandShort, longestShortCommand,
|
|
|
|
subCommand.commandShort.empty() ? " " : ", ",
|
|
|
|
subCommand.commandLong.empty() ? " " : "--",
|
|
|
|
subCommand.commandLong, longestLongCommand + 5,
|
|
|
|
subCommand.commandDescription
|
|
|
|
);
|
2023-07-16 23:46:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleOpenCommand(const std::vector<std::string> &args) {
|
|
|
|
if (args.empty()) {
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::println("No files provided to open.");
|
2023-07-16 23:46:41 +02:00
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> fullPaths;
|
|
|
|
bool doubleDashFound = false;
|
|
|
|
for (auto &arg : args) {
|
|
|
|
|
|
|
|
// Skip the first argument named `--`
|
|
|
|
if (arg == "--" && !doubleDashFound) {
|
|
|
|
doubleDashFound = true;
|
|
|
|
} else {
|
2023-10-16 10:59:17 +02:00
|
|
|
try {
|
|
|
|
auto path = std::filesystem::weakly_canonical(arg);
|
|
|
|
fullPaths.push_back(wolv::util::toUTF8String(path));
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
log::error("Failed to open file '{}'\n {}", arg, e.what());
|
|
|
|
}
|
2023-07-16 23:46:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hex::subcommands::forwardSubCommand("open", fullPaths);
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleCalcCommand(const std::vector<std::string> &args) {
|
|
|
|
if (args.empty()) {
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::println("No expression provided!");
|
|
|
|
hex::log::println("Example: imhex --calc \"5 * 7\"");
|
2023-07-16 23:46:41 +02:00
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2023-12-01 14:07:10 +01:00
|
|
|
wolv::math_eval::MathEvaluator<long double> evaluator;
|
2023-07-16 23:46:41 +02:00
|
|
|
|
|
|
|
auto input = hex::format("{}", fmt::join(args, " "));
|
|
|
|
auto result = evaluator.evaluate(input);
|
|
|
|
|
|
|
|
if (!result.has_value())
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::println("{}\n> '{}'", evaluator.getLastError().value(), input);
|
2023-07-16 23:46:41 +02:00
|
|
|
else
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::println("{}", result.value());
|
2023-07-16 23:46:41 +02:00
|
|
|
|
|
|
|
std::exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void handlePluginsCommand(const std::vector<std::string> &args) {
|
|
|
|
|
2024-01-22 12:53:07 +01:00
|
|
|
if (args.empty()) {
|
|
|
|
hex::log::println("Loaded plugins:");
|
2023-07-16 23:46:41 +02:00
|
|
|
|
2024-01-22 12:53:07 +01:00
|
|
|
for (const auto &plugin : PluginManager::getPlugins()) {
|
|
|
|
hex::log::print("- \033[1m{}\033[0m", plugin.getPluginName());
|
2023-07-16 23:46:41 +02:00
|
|
|
|
2024-01-22 12:53:07 +01:00
|
|
|
hex::log::println(" by {}", plugin.getPluginAuthor());
|
2023-07-16 23:46:41 +02:00
|
|
|
|
2024-01-22 12:53:07 +01:00
|
|
|
hex::log::println(" \033[2;3m{}\033[0m", plugin.getPluginDescription());
|
|
|
|
}
|
2023-07-16 23:46:41 +02:00
|
|
|
|
2024-01-22 12:53:07 +01:00
|
|
|
std::exit(EXIT_SUCCESS);
|
|
|
|
} else {
|
2024-01-22 23:35:00 +01:00
|
|
|
for (const auto &arg : args) {
|
|
|
|
PluginManager::addLoadPath(reinterpret_cast<const char8_t*>(arg.c_str()));
|
|
|
|
}
|
2024-01-22 12:53:07 +01:00
|
|
|
}
|
2023-07-16 23:46:41 +02:00
|
|
|
}
|
|
|
|
|
2024-02-01 10:58:45 +01:00
|
|
|
void handleLanguageCommand(const std::vector<std::string> &args) {
|
|
|
|
if (args.empty()) {
|
|
|
|
hex::log::println("usage: imhex --language <language>");
|
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImHexApi::System::impl::addInitArgument("language", args[0]);
|
|
|
|
}
|
|
|
|
|
2024-03-10 15:22:14 +01:00
|
|
|
void handleVerboseCommand(const std::vector<std::string> &) {
|
|
|
|
hex::log::enableDebugLogging();
|
|
|
|
}
|
|
|
|
|
2023-07-16 23:46:41 +02:00
|
|
|
void handleHashCommand(const std::vector<std::string> &args) {
|
|
|
|
if (args.size() != 2) {
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::println("usage: imhex --hash <algorithm> <file>");
|
|
|
|
hex::log::println("Available algorithms: md5, sha1, sha224, sha256, sha384, sha512");
|
2023-07-16 23:46:41 +02:00
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &algorithm = args[0];
|
|
|
|
const auto &filePath = std::fs::path(args[1]);
|
|
|
|
|
|
|
|
wolv::io::File file(filePath, wolv::io::File::Mode::Read);
|
|
|
|
if (!file.isValid()) {
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::println("Failed to open file: {}", wolv::util::toUTF8String(filePath));
|
2023-07-16 23:46:41 +02:00
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr static auto toVector = [](const auto &data) {
|
|
|
|
return std::vector<u8>(data.begin(), data.end());
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<u8> result;
|
|
|
|
if (algorithm == "md5") {
|
|
|
|
result = toVector(hex::crypt::md5(file.readVector()));
|
|
|
|
} else if (algorithm == "sha1") {
|
|
|
|
result = toVector(hex::crypt::sha1(file.readVector()));
|
|
|
|
} else if (algorithm == "sha224") {
|
|
|
|
result = toVector(hex::crypt::sha224(file.readVector()));
|
|
|
|
} else if (algorithm == "sha256") {
|
|
|
|
result = toVector(hex::crypt::sha256(file.readVector()));
|
|
|
|
} else if (algorithm == "sha384") {
|
|
|
|
result = toVector(hex::crypt::sha384(file.readVector()));
|
|
|
|
} else if (algorithm == "sha512") {
|
|
|
|
result = toVector(hex::crypt::sha512(file.readVector()));
|
|
|
|
} else {
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::println("Unknown algorithm: {}", algorithm);
|
|
|
|
hex::log::println("Available algorithms: md5, sha1, sha224, sha256, sha384, sha512");
|
2023-07-16 23:46:41 +02:00
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::println("{}({}) = {}", algorithm, wolv::util::toUTF8String(filePath.filename()), hex::crypt::encode16(result));
|
2023-07-16 23:46:41 +02:00
|
|
|
|
|
|
|
std::exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleEncodeCommand(const std::vector<std::string> &args) {
|
|
|
|
if (args.size() != 2) {
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::println("usage: imhex --encode <algorithm> <string>");
|
|
|
|
hex::log::println("Available algorithms: base64, hex");
|
2023-07-16 23:46:41 +02:00
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &algorithm = args[0];
|
|
|
|
const auto &data = std::vector<u8>(args[1].begin(), args[1].end());
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
if (algorithm == "base64") {
|
|
|
|
auto base64 = hex::crypt::encode64(data);
|
|
|
|
result = std::string(base64.begin(), base64.end());
|
|
|
|
} else if (algorithm == "hex") {
|
|
|
|
result = hex::crypt::encode16(data);
|
|
|
|
} else {
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::println("Unknown algorithm: {}", algorithm);
|
|
|
|
hex::log::println("Available algorithms: base64, hex");
|
2023-07-16 23:46:41 +02:00
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::println("encode_{}({}) = {}", algorithm, args[1], result);
|
2023-07-16 23:46:41 +02:00
|
|
|
std::exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleDecodeCommand(const std::vector<std::string> &args) {
|
|
|
|
if (args.size() != 2) {
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::println("usage: imhex --decode <algorithm> <string>");
|
|
|
|
hex::log::println("Available algorithms: base64, hex");
|
2023-07-16 23:46:41 +02:00
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &algorithm = args[0];
|
|
|
|
const auto &data = std::vector<u8>(args[1].begin(), args[1].end());
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
if (algorithm == "base64") {
|
|
|
|
auto base64 = hex::crypt::decode64(data);
|
|
|
|
result = std::string(base64.begin(), base64.end());
|
|
|
|
} else if (algorithm == "hex") {
|
|
|
|
auto base16 = hex::crypt::decode16(std::string(data.begin(), data.end()));
|
|
|
|
result = std::string(base16.begin(), base16.end());
|
|
|
|
} else {
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::println("Unknown algorithm: {}", algorithm);
|
|
|
|
hex::log::println("Available algorithms: base64, hex");
|
2023-07-16 23:46:41 +02:00
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::print("decode_{}({}) = {}", algorithm, args[1], result);
|
2023-07-16 23:46:41 +02:00
|
|
|
std::exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2023-07-17 10:43:29 +02:00
|
|
|
void handleMagicCommand(const std::vector<std::string> &args) {
|
|
|
|
if (args.size() != 2) {
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::println("usage: imhex --magic <operation> <file>");
|
|
|
|
hex::log::println("Available operations: mime, desc");
|
2023-07-17 10:43:29 +02:00
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!magic::compile()) {
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::print("Failed to compile magic database!");
|
2023-07-17 10:43:29 +02:00
|
|
|
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()) {
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::println("Failed to open file: {}", wolv::util::toUTF8String(filePath));
|
2023-07-17 10:43:29 +02:00
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2023-07-17 11:20:17 +02:00
|
|
|
auto data = file.readVector(std::min<size_t>(file.getSize(), 100_KiB));
|
2023-07-17 10:43:29 +02:00
|
|
|
|
|
|
|
if (operation == "mime") {
|
|
|
|
auto result = magic::getMIMEType(data);
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::println("{}", result);
|
2023-07-17 10:43:29 +02:00
|
|
|
} else if (operation == "desc") {
|
|
|
|
auto result = magic::getDescription(data);
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::println("{}", result);
|
2023-07-17 10:43:29 +02:00
|
|
|
} else {
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::log::println("Unknown operation: {}", operation);
|
|
|
|
hex::log::println("Available operations: mime, desc");
|
2023-07-17 10:43:29 +02:00
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void handlePatternLanguageCommand(const std::vector<std::string> &args) {
|
2023-08-02 20:30:13 +02:00
|
|
|
std::vector<std::string> processedArgs = args;
|
2023-12-27 16:33:49 +01:00
|
|
|
if (processedArgs.empty()) {
|
2023-08-02 20:30:13 +02:00
|
|
|
processedArgs.emplace_back("--help");
|
2023-12-27 16:33:49 +01:00
|
|
|
} else {
|
2023-09-03 12:25:00 +02:00
|
|
|
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::PatternsInclude))
|
|
|
|
processedArgs.emplace_back(hex::format("--includes={}", wolv::util::toUTF8String(path)));
|
|
|
|
}
|
2023-07-17 10:43:29 +02:00
|
|
|
|
|
|
|
std::exit(pl::cli::executeCommandLineInterface(processedArgs));
|
|
|
|
}
|
|
|
|
|
2023-12-05 16:45:35 +01:00
|
|
|
void handleHexdumpCommand(const std::vector<std::string> &args) {
|
|
|
|
if (args.size() < 1 || args.size() > 3) {
|
|
|
|
log::println("usage: imhex --hexdump <file> <offset> <size>");
|
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::fs::path filePath = reinterpret_cast<const char8_t*>(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();
|
|
|
|
|
2023-12-07 12:06:26 +01:00
|
|
|
size = std::min<u64>(size, provider.getActualSize());
|
2023-12-05 16:45:35 +01:00
|
|
|
|
|
|
|
log::print("{}", hex::generateHexView(startAddress, size - startAddress, &provider));
|
|
|
|
|
|
|
|
std::exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2023-12-15 08:11:51 +01:00
|
|
|
void handleDemangleCommand(const std::vector<std::string> &args) {
|
|
|
|
if (args.size() != 1) {
|
|
|
|
log::println("usage: imhex --demangle <identifier>");
|
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
log::println("{}", llvm::demangle(args[0]));
|
2024-02-11 13:46:06 +01:00
|
|
|
std::exit(EXIT_SUCCESS);
|
2023-12-15 08:11:51 +01:00
|
|
|
}
|
|
|
|
|
2023-07-16 23:46:41 +02:00
|
|
|
|
|
|
|
void registerCommandForwarders() {
|
|
|
|
hex::subcommands::registerSubCommand("open", [](const std::vector<std::string> &args){
|
|
|
|
for (auto &arg : args) {
|
2023-12-08 10:29:44 +01:00
|
|
|
RequestOpenFile::post(arg);
|
2023-07-16 23:46:41 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|