From d8c3f4e31cd5fb26d5352b578ee866e5ad18986e Mon Sep 17 00:00:00 2001 From: Desetude Date: Sun, 26 Jan 2020 20:56:52 +0000 Subject: [PATCH] Flip > and >= in for loop conditions to < and <= respectively Signed-off-by: Graham --- .../dev/openrs2/deob/ast/AstDeobfuscator.kt | 2 ++ .../transform/ForLoopConditionTransformer.kt | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt index be62dd1b..b8d6b372 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.kt @@ -14,6 +14,7 @@ import dev.openrs2.deob.ast.transform.BinaryExprOrderTransformer import dev.openrs2.deob.ast.transform.BitMaskTransformer import dev.openrs2.deob.ast.transform.ComplementTransformer import dev.openrs2.deob.ast.transform.EncloseTransformer +import dev.openrs2.deob.ast.transform.ForLoopConditionTransformer import dev.openrs2.deob.ast.transform.IfElseTransformer import dev.openrs2.deob.ast.transform.IncrementTransformer import dev.openrs2.deob.ast.transform.NegativeLiteralTransformer @@ -89,6 +90,7 @@ class AstDeobfuscator(private val modules: List) { ValueOfTransformer(), NewInstanceTransformer(), IncrementTransformer(), + ForLoopConditionTransformer(), EncloseTransformer() ) } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt new file mode 100644 index 00000000..51fd3338 --- /dev/null +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt @@ -0,0 +1,28 @@ +package dev.openrs2.deob.ast.transform + +import com.github.javaparser.ast.CompilationUnit +import com.github.javaparser.ast.Node +import com.github.javaparser.ast.expr.BinaryExpr +import com.github.javaparser.ast.stmt.ForStmt +import dev.openrs2.deob.ast.util.walk + +class ForLoopConditionTransformer : Transformer() { + override fun transform(unit: CompilationUnit) { + unit.walk(Node.TreeTraversal.POSTORDER) { stmt: ForStmt -> + stmt.compare.ifPresent { compare -> + if (!compare.isBinaryExpr) { + return@ifPresent + } + + val expr = compare.asBinaryExpr() + val flipped = when (expr.operator) { + BinaryExpr.Operator.GREATER -> BinaryExpr.Operator.LESS + BinaryExpr.Operator.GREATER_EQUALS -> BinaryExpr.Operator.LESS_EQUALS + else -> return@ifPresent + } + + stmt.setCompare(BinaryExpr(expr.right, expr.left, flipped)) + } + } + } +}