1
0
mirror of synced 2024-11-15 03:27:40 +01:00

patterns: Added find_sequence_in_range function (#448)

This commit is contained in:
Lukas Cone 2022-02-26 15:53:18 +01:00 committed by GitHub
parent e3fbb490df
commit 9737b9cd62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -106,12 +106,14 @@ namespace hex::plugin::builtin {
return u128(ctx->getProvider()->getActualSize()); return u128(ctx->getProvider()->getActualSize());
}); });
/* find_sequence(occurrence_index, bytes...) */ /* find_sequence_in_range(occurrence_index, start_offset, end_offset, bytes...) */
ContentRegistry::PatternLanguage::addFunction(nsStdMem, "find_sequence", ContentRegistry::PatternLanguage::MoreParametersThan | 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> { ContentRegistry::PatternLanguage::addFunction(nsStdMem, "find_sequence_in_range", ContentRegistry::PatternLanguage::MoreParametersThan | 3, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
auto occurrenceIndex = Token::literalToUnsigned(params[0]); auto occurrenceIndex = Token::literalToUnsigned(params[0]);
auto offsetFrom = Token::literalToUnsigned(params[1]);
auto offsetTo = Token::literalToUnsigned(params[2]);
std::vector<u8> sequence; std::vector<u8> sequence;
for (u32 i = 1; i < params.size(); i++) { for (u32 i = 3; i < params.size(); i++) {
auto byte = Token::literalToUnsigned(params[i]); auto byte = Token::literalToUnsigned(params[i]);
if (byte > 0xFF) if (byte > 0xFF)
@ -122,7 +124,9 @@ namespace hex::plugin::builtin {
std::vector<u8> bytes(sequence.size(), 0x00); std::vector<u8> bytes(sequence.size(), 0x00);
u32 occurrences = 0; u32 occurrences = 0;
for (u64 offset = 0; offset < ctx->getProvider()->getSize() - sequence.size(); offset++) { const u64 bufferSize = ctx->getProvider()->getSize();
const u64 endOffset = offsetTo <= offsetFrom ? bufferSize : std::min(bufferSize, u64(offsetTo));
for (u64 offset = offsetFrom; offset < endOffset - sequence.size(); offset++) {
ctx->getProvider()->read(offset, bytes.data(), bytes.size()); ctx->getProvider()->read(offset, bytes.data(), bytes.size());
if (bytes == sequence) { if (bytes == sequence) {
@ -249,7 +253,6 @@ namespace hex::plugin::builtin {
ContentRegistry::PatternLanguage::Namespace nsStdFile = { "builtin", "std", "file" }; ContentRegistry::PatternLanguage::Namespace nsStdFile = { "builtin", "std", "file" };
{ {
static u32 fileCounter = 0; static u32 fileCounter = 0;
static std::map<u32, File> openFiles; static std::map<u32, File> openFiles;
@ -509,5 +512,4 @@ namespace hex::plugin::builtin {
}); });
} }
} }
} }