From 9737b9cd6210dfaa6195b79b175cf4535ed1591f Mon Sep 17 00:00:00 2001 From: Lukas Cone Date: Sat, 26 Feb 2022 15:53:18 +0100 Subject: [PATCH] patterns: Added find_sequence_in_range function (#448) --- .../source/content/pl_builtin_functions.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/plugins/builtin/source/content/pl_builtin_functions.cpp b/plugins/builtin/source/content/pl_builtin_functions.cpp index 818e3204e..1ab77d037 100644 --- a/plugins/builtin/source/content/pl_builtin_functions.cpp +++ b/plugins/builtin/source/content/pl_builtin_functions.cpp @@ -106,12 +106,14 @@ namespace hex::plugin::builtin { return u128(ctx->getProvider()->getActualSize()); }); - /* find_sequence(occurrence_index, bytes...) */ - ContentRegistry::PatternLanguage::addFunction(nsStdMem, "find_sequence", ContentRegistry::PatternLanguage::MoreParametersThan | 1, [](Evaluator *ctx, auto params) -> std::optional { + /* find_sequence_in_range(occurrence_index, start_offset, end_offset, bytes...) */ + ContentRegistry::PatternLanguage::addFunction(nsStdMem, "find_sequence_in_range", ContentRegistry::PatternLanguage::MoreParametersThan | 3, [](Evaluator *ctx, auto params) -> std::optional { auto occurrenceIndex = Token::literalToUnsigned(params[0]); + auto offsetFrom = Token::literalToUnsigned(params[1]); + auto offsetTo = Token::literalToUnsigned(params[2]); std::vector sequence; - for (u32 i = 1; i < params.size(); i++) { + for (u32 i = 3; i < params.size(); i++) { auto byte = Token::literalToUnsigned(params[i]); if (byte > 0xFF) @@ -121,8 +123,10 @@ namespace hex::plugin::builtin { } std::vector bytes(sequence.size(), 0x00); - u32 occurrences = 0; - for (u64 offset = 0; offset < ctx->getProvider()->getSize() - sequence.size(); offset++) { + u32 occurrences = 0; + 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()); if (bytes == sequence) { @@ -249,7 +253,6 @@ namespace hex::plugin::builtin { ContentRegistry::PatternLanguage::Namespace nsStdFile = { "builtin", "std", "file" }; { - static u32 fileCounter = 0; static std::map openFiles; @@ -509,5 +512,4 @@ namespace hex::plugin::builtin { }); } } - }