From d528a3bd004e2fcd7cde8a03b9a57f93b1f7faee Mon Sep 17 00:00:00 2001 From: leaty Date: Mon, 16 Jan 2023 00:32:53 +0100 Subject: [PATCH] Fix rarely not finding pattern #4 --- src/mem.rs | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/mem.rs b/src/mem.rs index 95bc49a..cdf08cf 100644 --- a/src/mem.rs +++ b/src/mem.rs @@ -15,7 +15,6 @@ pub fn search( let find = pattern.len(); let mut chunk_size = CHUNK_SIZE; let mut pointer = region.0; - let mut criteria = pattern.iter(); let mut found = vec![]; let mut at = 0; @@ -28,26 +27,33 @@ pub fn search( // Read memory region one chunk at a time let chunk = read(pid, pointer, chunk_size)?; - // Try to find pattern - for (idx, mbyte) in chunk.iter().enumerate() { - if let Some(byte) = criteria.next().unwrap() { - // Found one - if byte == mbyte { - found.push(*mbyte); + // Go through chunk per byte, forward-match with pattern each time + for i in 0..chunk.len() { + chunk + .iter() + .skip(i) + .zip(pattern.iter().skip(found.len())) + .all(|(mbyte, cbyte)| { + match cbyte { + // Store matching bytes + // None "__" criteria always matches + Some(b) if b == mbyte => found.push(*mbyte), + None => found.push(*mbyte), + + // Doesn't match, reset + _ => { + found.clear(); + return false; + } + }; // Set "at" on first discovery if found.len() == 1 { - at = pointer + idx; + at = pointer + i; } - } else { - // Doesn't match, reset - found.clear(); - criteria = pattern.iter(); - } - } else { - // Skip (None) is still considered found - found.push(*mbyte); - } + + return true; + }); // Found what there is to find if found.len() == find {