From 0df6e46d1fd7ba98a64544dbe1c9864dde78a849 Mon Sep 17 00:00:00 2001 From: Mohamed Lemine Ould El-Hadj Date: Sun, 9 Oct 2022 22:43:41 -0400 Subject: [PATCH] fix: slightly better fuzzy scanning, succeeds if instructions are missing --- .../method/impl/MethodFingerprint.kt | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/app/revanced/patcher/fingerprint/method/impl/MethodFingerprint.kt b/src/main/kotlin/app/revanced/patcher/fingerprint/method/impl/MethodFingerprint.kt index bdb3aace..d0ce8e7b 100644 --- a/src/main/kotlin/app/revanced/patcher/fingerprint/method/impl/MethodFingerprint.kt +++ b/src/main/kotlin/app/revanced/patcher/fingerprint/method/impl/MethodFingerprint.kt @@ -158,18 +158,33 @@ abstract class MethodFingerprint( for (index in 0 until instructionLength) { var patternIndex = 0 var threshold = fingerprintFuzzyPatternScanThreshold + var offset = 0; - while (index + patternIndex < instructionLength) { + scan@ while (index + patternIndex < instructionLength) { val originalOpcode = instructions.elementAt(index + patternIndex).opcode - val patternOpcode = pattern.elementAt(patternIndex) + val patternOpcode = pattern.elementAt(patternIndex + offset) if (patternOpcode != null && patternOpcode.ordinal != originalOpcode.ordinal) { // reaching maximum threshold (0) means, // the pattern does not match to the current instructions if (threshold-- == 0) break + + // look ahead + for (depth in 1 until threshold + 2) { + val lookAheadIndex = patternIndex + offset + depth + if (lookAheadIndex < patternLength && index + lookAheadIndex < instructionLength) + { + val nextPatternOpcode = pattern.elementAt(lookAheadIndex) + if (nextPatternOpcode != null && nextPatternOpcode.ordinal == originalOpcode.ordinal) + { + offset+=depth; + continue@scan; + } + } + } } - if (patternIndex < patternLength - 1) { + if (patternIndex + offset < patternLength - 1) { // if the entire pattern has not been scanned yet // continue the scan patternIndex++