2021-08-29 14:18:45 +02:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/shared_data.hpp>
|
|
|
|
#include <hex/helpers/fmt.hpp>
|
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
#include <hex/pattern_language/token.hpp>
|
2021-09-08 15:18:24 +02:00
|
|
|
#include <hex/pattern_language/log_console.hpp>
|
|
|
|
#include <hex/pattern_language/evaluator.hpp>
|
2021-09-21 21:29:18 +02:00
|
|
|
#include <hex/pattern_language/pattern_data.hpp>
|
2021-01-22 18:01:39 +01:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
#include <fmt/args.h>
|
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
namespace hex::plugin::builtin {
|
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
std::string format(pl::Evaluator *, auto params) {
|
|
|
|
auto format = pl::Token::literalToString(params[0], true);
|
|
|
|
std::string message;
|
|
|
|
|
|
|
|
fmt::dynamic_format_arg_store<fmt::format_context> formatArgs;
|
|
|
|
|
|
|
|
for (u32 i = 1; i < params.size(); i++) {
|
|
|
|
auto ¶m = params[i];
|
|
|
|
|
|
|
|
std::visit(overloaded {
|
|
|
|
[&](pl::PatternData* value) {
|
|
|
|
formatArgs.push_back(hex::format("{} {} @ 0x{:X}", value->getTypeName(), value->getVariableName(), value->getOffset()));
|
|
|
|
},
|
|
|
|
[&](auto &&value) {
|
|
|
|
formatArgs.push_back(value);
|
|
|
|
}
|
|
|
|
}, param);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return fmt::vformat(format, formatArgs);
|
|
|
|
} catch (fmt::format_error &error) {
|
|
|
|
hex::pl::LogConsole::abortEvaluation(hex::format("format error: {}", error.what()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
void registerPatternLanguageFunctions() {
|
2021-09-08 15:18:24 +02:00
|
|
|
using namespace hex::pl;
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-08-27 09:56:20 +02:00
|
|
|
ContentRegistry::PatternLanguageFunctions::Namespace nsStd = { "std" };
|
|
|
|
{
|
|
|
|
|
|
|
|
/* assert(condition, message) */
|
2021-09-21 21:29:18 +02:00
|
|
|
ContentRegistry::PatternLanguageFunctions::add(nsStd, "assert", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
|
|
|
auto condition = Token::literalToBoolean(params[0]);
|
|
|
|
auto message = std::get<std::string>(params[1]);
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
if (!condition)
|
|
|
|
LogConsole::abortEvaluation(hex::format("assertion failed \"{0}\"", message));
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
return std::nullopt;
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
|
|
|
|
2021-09-09 23:18:10 +02:00
|
|
|
/* assert_warn(condition, message) */
|
2021-09-21 21:29:18 +02:00
|
|
|
ContentRegistry::PatternLanguageFunctions::add(nsStd, "assert_warn", 2, [](auto *ctx, auto params) -> std::optional<Token::Literal> {
|
|
|
|
auto condition = Token::literalToBoolean(params[0]);
|
|
|
|
auto message = std::get<std::string>(params[1]);
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
if (!condition)
|
|
|
|
ctx->getConsole().log(LogConsole::Level::Warning, hex::format("assertion failed \"{0}\"", message));
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
return std::nullopt;
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
/* print(format, args...) */
|
|
|
|
ContentRegistry::PatternLanguageFunctions::add(nsStd, "print", ContentRegistry::PatternLanguageFunctions::MoreParametersThan | 0, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
|
|
|
ctx->getConsole().log(LogConsole::Level::Info, format(ctx, params));
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
return std::nullopt;
|
|
|
|
});
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
/* format(format, args...) */
|
|
|
|
ContentRegistry::PatternLanguageFunctions::add(nsStd, "format", ContentRegistry::PatternLanguageFunctions::MoreParametersThan | 0, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
|
|
|
return format(ctx, params);
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
|
|
|
|
2021-09-09 23:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ContentRegistry::PatternLanguageFunctions::Namespace nsStdMem = { "std", "mem" };
|
|
|
|
{
|
|
|
|
|
|
|
|
/* align_to(alignment, value) */
|
2021-09-21 21:29:18 +02:00
|
|
|
ContentRegistry::PatternLanguageFunctions::add(nsStdMem, "align_to", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
|
|
|
auto alignment = Token::literalToUnsigned(params[0]);
|
|
|
|
auto value = Token::literalToUnsigned(params[1]);
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
u128 remainder = value % alignment;
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
return remainder != 0 ? value + (alignment - remainder) : value;
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
|
|
|
|
2021-09-09 23:18:10 +02:00
|
|
|
/* base_address() */
|
2021-09-21 21:29:18 +02:00
|
|
|
ContentRegistry::PatternLanguageFunctions::add(nsStdMem, "base_address", ContentRegistry::PatternLanguageFunctions::NoParameters, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
|
|
|
return u128(ctx->getProvider()->getBaseAddress());
|
2021-09-09 23:18:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/* size() */
|
2021-09-21 21:29:18 +02:00
|
|
|
ContentRegistry::PatternLanguageFunctions::add(nsStdMem, "size", ContentRegistry::PatternLanguageFunctions::NoParameters, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
|
|
|
return u128(ctx->getProvider()->getActualSize());
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
2021-09-09 23:18:10 +02:00
|
|
|
|
|
|
|
/* find_sequence(occurrence_index, bytes...) */
|
2021-09-21 21:29:18 +02:00
|
|
|
ContentRegistry::PatternLanguageFunctions::add(nsStdMem, "find_sequence", ContentRegistry::PatternLanguageFunctions::MoreParametersThan | 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
|
|
|
auto occurrenceIndex = Token::literalToUnsigned(params[0]);
|
|
|
|
|
2021-09-09 23:18:10 +02:00
|
|
|
std::vector<u8> sequence;
|
|
|
|
for (u32 i = 1; i < params.size(); i++) {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto byte = Token::literalToUnsigned(params[i]);
|
|
|
|
|
|
|
|
if (byte > 0xFF)
|
|
|
|
LogConsole::abortEvaluation(hex::format("byte #{} value out of range: {} > 0xFF", i, u64(byte)));
|
|
|
|
|
|
|
|
sequence.push_back(u8(byte & 0xFF));
|
2021-09-09 23:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8> bytes(sequence.size(), 0x00);
|
|
|
|
u32 occurrences = 0;
|
2021-09-21 21:29:18 +02:00
|
|
|
for (u64 offset = 0; offset < ctx->getProvider()->getSize() - sequence.size(); offset++) {
|
|
|
|
ctx->getProvider()->read(offset, bytes.data(), bytes.size());
|
2021-09-09 23:18:10 +02:00
|
|
|
|
|
|
|
if (bytes == sequence) {
|
2021-09-21 21:29:18 +02:00
|
|
|
if (occurrences < occurrenceIndex) {
|
2021-09-09 23:18:10 +02:00
|
|
|
occurrences++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
return u128(offset);
|
2021-09-09 23:18:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
LogConsole::abortEvaluation("failed to find sequence");
|
2021-09-09 23:18:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/* read_unsigned(address, size) */
|
2021-09-21 21:29:18 +02:00
|
|
|
ContentRegistry::PatternLanguageFunctions::add(nsStdMem, "read_unsigned", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
|
|
|
auto address = Token::literalToUnsigned(params[0]);
|
|
|
|
auto size = Token::literalToUnsigned(params[1]);
|
|
|
|
|
|
|
|
if (size > 16)
|
|
|
|
LogConsole::abortEvaluation("read size out of range");
|
|
|
|
|
|
|
|
u128 result = 0;
|
|
|
|
ctx->getProvider()->read(address, &result, size);
|
|
|
|
|
|
|
|
return result;
|
2021-09-09 23:18:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/* read_signed(address, size) */
|
2021-09-21 21:29:18 +02:00
|
|
|
ContentRegistry::PatternLanguageFunctions::add(nsStdMem, "read_signed", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
|
|
|
auto address = Token::literalToUnsigned(params[0]);
|
|
|
|
auto size = Token::literalToUnsigned(params[1]);
|
|
|
|
|
|
|
|
if (size > 16)
|
|
|
|
LogConsole::abortEvaluation("read size out of range");
|
|
|
|
|
|
|
|
s128 value;
|
|
|
|
ctx->getProvider()->read(address, &value, size);
|
|
|
|
return hex::signExtend(size * 8, value);
|
2021-09-09 23:18:10 +02:00
|
|
|
});
|
|
|
|
|
2021-09-21 23:29:30 +02:00
|
|
|
/* read_string(address, size) */
|
|
|
|
ContentRegistry::PatternLanguageFunctions::add(nsStdMem, "read_string", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
|
|
|
auto address = Token::literalToUnsigned(params[0]);
|
|
|
|
auto size = Token::literalToUnsigned(params[1]);
|
|
|
|
|
|
|
|
std::string result(size, '\x00');
|
|
|
|
ctx->getProvider()->read(address, result.data(), size);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
2021-08-27 09:56:20 +02:00
|
|
|
}
|
|
|
|
|
2021-09-25 00:04:54 +02:00
|
|
|
ContentRegistry::PatternLanguageFunctions::Namespace nsStdString = { "std", "string" };
|
2021-08-27 09:56:20 +02:00
|
|
|
{
|
|
|
|
/* length(string) */
|
2021-09-25 00:04:54 +02:00
|
|
|
ContentRegistry::PatternLanguageFunctions::add(nsStdString, "length", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto string = Token::literalToString(params[0], false);
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
return u128(string.length());
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-08-27 09:56:20 +02:00
|
|
|
/* at(string, index) */
|
2021-09-25 00:04:54 +02:00
|
|
|
ContentRegistry::PatternLanguageFunctions::add(nsStdString, "at", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto string = Token::literalToString(params[0], false);
|
|
|
|
auto index = Token::literalToSigned(params[1]);
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
if (std::abs(index) >= string.length())
|
|
|
|
LogConsole::abortEvaluation("character index out of range");
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
if (index >= 0)
|
|
|
|
return char(string[index]);
|
|
|
|
else
|
|
|
|
return char(string[string.length() - -index]);
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
/* substr(string, pos, count) */
|
2021-09-25 00:04:54 +02:00
|
|
|
ContentRegistry::PatternLanguageFunctions::add(nsStdString, "substr", 3, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto string = Token::literalToString(params[0], false);
|
|
|
|
auto pos = Token::literalToUnsigned(params[1]);
|
|
|
|
auto size = Token::literalToUnsigned(params[2]);
|
|
|
|
|
2021-09-25 00:04:40 +02:00
|
|
|
if (pos > string.length())
|
2021-09-21 21:29:18 +02:00
|
|
|
LogConsole::abortEvaluation("character index out of range");
|
2021-03-02 13:55:43 +01:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
return string.substr(pos, size);
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
2021-09-21 21:29:18 +02:00
|
|
|
|
2021-08-27 09:56:20 +02:00
|
|
|
}
|
2021-01-22 18:01:39 +01:00
|
|
|
}
|
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
}
|