From 12d3af48dd5a5a52edce8945abadf86f47fa0e18 Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 12 Aug 2019 19:36:10 +0100 Subject: [PATCH] Add TernaryTransformer --- .../dev/openrs2/deob/ast/AstDeobfuscator.java | 2 ++ .../deob/ast/transform/IfElseTransformer.java | 9 ++----- .../ast/transform/TernaryTransformer.java | 25 +++++++++++++++++++ .../dev/openrs2/deob/ast/util/ExprUtils.java | 4 +++ 4 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.java diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.java index 397b407939..9f1e1ed720 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.java +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.java @@ -17,6 +17,7 @@ import dev.openrs2.deob.ast.transform.BinaryExprOrderTransformer; import dev.openrs2.deob.ast.transform.ComplementTransformer; import dev.openrs2.deob.ast.transform.IfElseTransformer; import dev.openrs2.deob.ast.transform.NegativeLiteralTransformer; +import dev.openrs2.deob.ast.transform.TernaryTransformer; import dev.openrs2.deob.ast.transform.Transformer; public final class AstDeobfuscator { @@ -24,6 +25,7 @@ public final class AstDeobfuscator { new NegativeLiteralTransformer(), new ComplementTransformer(), new IfElseTransformer(), + new TernaryTransformer(), new BinaryExprOrderTransformer(), new AddSubTransformer() ); diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.java index 75bd0913a7..bc179a0831 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.java +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.java @@ -2,10 +2,9 @@ package dev.openrs2.deob.ast.transform; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.NodeList; -import com.github.javaparser.ast.expr.Expression; -import com.github.javaparser.ast.expr.UnaryExpr; import com.github.javaparser.ast.stmt.IfStmt; import com.github.javaparser.ast.stmt.Statement; +import dev.openrs2.deob.ast.util.ExprUtils; import dev.openrs2.deob.ast.visitor.NegateExprVisitor; public final class IfElseTransformer extends Transformer { @@ -36,10 +35,6 @@ public final class IfElseTransformer extends Transformer { throw new IllegalArgumentException(); } - private static boolean isNot(Expression expr) { - return expr.isUnaryExpr() && expr.asUnaryExpr().getOperator() == UnaryExpr.Operator.LOGICAL_COMPLEMENT; - } - @Override public void transform(CompilationUnit unit) { unit.findAll(IfStmt.class).forEach(stmt -> { @@ -63,7 +58,7 @@ public final class IfElseTransformer extends Transformer { * Prefer if (a) over if (!a). We don't swap != as it makes * checking bitwise flags look worse. */ - if (isNot(condition)) { + if (ExprUtils.isNot(condition)) { stmt.setCondition(condition.accept(new NegateExprVisitor(), null)); stmt.setThenStmt(elseStmt); stmt.setElseStmt(thenStmt); diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.java new file mode 100644 index 0000000000..d1335b096b --- /dev/null +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.java @@ -0,0 +1,25 @@ +package dev.openrs2.deob.ast.transform; + +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.expr.ConditionalExpr; +import dev.openrs2.deob.ast.util.ExprUtils; +import dev.openrs2.deob.ast.visitor.NegateExprVisitor; + +public final class TernaryTransformer extends Transformer { + @Override + public void transform(CompilationUnit unit) { + unit.findAll(ConditionalExpr.class).forEach(expr -> { + var condition = expr.getCondition(); + if (!ExprUtils.isNot(condition)) { + return; + } + + var thenExpr = expr.getThenExpr(); + var elseExpr = expr.getElseExpr(); + + expr.setCondition(condition.accept(new NegateExprVisitor(), null)); + expr.setThenExpr(elseExpr.clone()); + expr.setElseExpr(thenExpr.clone()); + }); + } +} diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.java index bb00484d50..d21d8e3345 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.java +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.java @@ -14,6 +14,10 @@ public final class ExprUtils { return new LongLiteralExpr(Long.toString(value).concat("L")); } + public static boolean isNot(Expression expr) { + return expr.isUnaryExpr() && expr.asUnaryExpr().getOperator() == UnaryExpr.Operator.LOGICAL_COMPLEMENT; + } + public static Expression negate(Expression expr) { if (expr.isUnaryExpr() && expr.asUnaryExpr().getOperator() == UnaryExpr.Operator.MINUS) { return expr.asUnaryExpr().getExpression().clone();