From eadea9231c1a8805064d0946b286c5bab6b2cb8e Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 12 Aug 2019 19:19:00 +0100 Subject: [PATCH] Add AddSubTransformer --- .../dev/openrs2/deob/ast/AstDeobfuscator.java | 4 +- .../deob/ast/transform/AddSubTransformer.java | 51 +++++++++++++++++++ .../transform/BinaryExprOrderTransformer.java | 8 +-- .../transform/NegativeLiteralTransformer.java | 14 +---- .../dev/openrs2/deob/ast/util/ExprUtils.java | 14 +++++ .../dev/openrs2/deob/ast/util/TypeUtils.java | 13 +++++ 6 files changed, 84 insertions(+), 20 deletions(-) create mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.java create mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/util/TypeUtils.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 7a87bdd5ba..397b407939 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 @@ -12,6 +12,7 @@ import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeS import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; import com.github.javaparser.utils.SourceRoot; import com.google.common.collect.ImmutableList; +import dev.openrs2.deob.ast.transform.AddSubTransformer; import dev.openrs2.deob.ast.transform.BinaryExprOrderTransformer; import dev.openrs2.deob.ast.transform.ComplementTransformer; import dev.openrs2.deob.ast.transform.IfElseTransformer; @@ -23,7 +24,8 @@ public final class AstDeobfuscator { new NegativeLiteralTransformer(), new ComplementTransformer(), new IfElseTransformer(), - new BinaryExprOrderTransformer() + new BinaryExprOrderTransformer(), + new AddSubTransformer() ); public static void main(String[] args) { diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.java new file mode 100644 index 0000000000..48526d47b7 --- /dev/null +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.java @@ -0,0 +1,51 @@ +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.expr.Expression; +import com.github.javaparser.ast.expr.UnaryExpr; +import dev.openrs2.deob.ast.util.ExprUtils; +import dev.openrs2.deob.ast.util.NodeUtils; +import dev.openrs2.deob.ast.util.TypeUtils; + +public final class AddSubTransformer extends Transformer { + private static boolean isNegative(Expression expr) { + if (expr.isUnaryExpr()) { + return expr.asUnaryExpr().getOperator() == UnaryExpr.Operator.MINUS; + } else if (expr.isIntegerLiteralExpr()) { + return expr.asIntegerLiteralExpr().asInt() < 0; + } else if (expr.isLongLiteralExpr()) { + return expr.asLongLiteralExpr().asLong() < 0; + } else { + return false; + } + } + + @Override + public void transform(CompilationUnit unit) { + NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, BinaryExpr.class, expr -> { + var op = expr.getOperator(); + var left = expr.getLeft(); + var right = expr.getRight(); + + var type = expr.calculateResolvedType(); + if (op == BinaryExpr.Operator.PLUS && TypeUtils.isString(type)) { + return; + } + + if (op == BinaryExpr.Operator.PLUS && isNegative(right)) { + /* x + -y => x - y */ + expr.setOperator(BinaryExpr.Operator.MINUS); + expr.setRight(ExprUtils.negate(right)); + } else if (op == BinaryExpr.Operator.PLUS && isNegative(left)) { + /* -x + y => y - x */ + // TODO(gpe): check for side effects before applying this transform + } else if (op == BinaryExpr.Operator.MINUS && isNegative(right)) { + /* x - -y => x + y */ + expr.setOperator(BinaryExpr.Operator.PLUS); + expr.setRight(ExprUtils.negate(right)); + } + }); + } +} diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.java index c138afa0ba..2211a92820 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.java +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.java @@ -5,8 +5,8 @@ import java.util.Optional; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.Node; import com.github.javaparser.ast.expr.BinaryExpr; -import com.github.javaparser.resolution.types.ResolvedType; import dev.openrs2.deob.ast.util.NodeUtils; +import dev.openrs2.deob.ast.util.TypeUtils; public final class BinaryExprOrderTransformer extends Transformer { private static Optional flip(BinaryExpr.Operator op) { @@ -34,16 +34,12 @@ public final class BinaryExprOrderTransformer extends Transformer { } } - private static boolean isString(ResolvedType type) { - return type.isReferenceType() && type.asReferenceType().getQualifiedName().equals("java.lang.String"); - } - @Override public void transform(CompilationUnit unit) { NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, BinaryExpr.class, expr -> { flip(expr.getOperator()).ifPresent(op -> { var type = expr.calculateResolvedType(); - if (op == BinaryExpr.Operator.PLUS && isString(type)) { + if (op == BinaryExpr.Operator.PLUS && TypeUtils.isString(type)) { return; } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.java index 0a0a12e44e..d2ac9ab257 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.java +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.java @@ -1,22 +1,10 @@ package dev.openrs2.deob.ast.transform; import com.github.javaparser.ast.CompilationUnit; -import com.github.javaparser.ast.expr.Expression; -import com.github.javaparser.ast.expr.IntegerLiteralExpr; import com.github.javaparser.ast.expr.UnaryExpr; import dev.openrs2.deob.ast.util.ExprUtils; public final class NegativeLiteralTransformer extends Transformer { - private static Expression negate(Expression expr) { - if (expr.isIntegerLiteralExpr()) { - return new IntegerLiteralExpr(-expr.asIntegerLiteralExpr().asInt()); - } else if (expr.isLongLiteralExpr()) { - return ExprUtils.createLong(-expr.asLongLiteralExpr().asLong()); - } else { - throw new IllegalArgumentException(); - } - } - @Override public void transform(CompilationUnit unit) { unit.findAll(UnaryExpr.class).forEach(expr -> { @@ -29,7 +17,7 @@ public final class NegativeLiteralTransformer extends Transformer { if (op == UnaryExpr.Operator.PLUS) { expr.replace(operand); } else if (op == UnaryExpr.Operator.MINUS) { - expr.replace(negate(operand)); + expr.replace(ExprUtils.negate(operand)); } }); } 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 a4409d1686..bb00484d50 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 @@ -1,7 +1,9 @@ package dev.openrs2.deob.ast.util; import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.expr.IntegerLiteralExpr; import com.github.javaparser.ast.expr.LongLiteralExpr; +import com.github.javaparser.ast.expr.UnaryExpr; public final class ExprUtils { public static boolean isIntegerOrLongLiteral(Expression expr) { @@ -12,6 +14,18 @@ public final class ExprUtils { return new LongLiteralExpr(Long.toString(value).concat("L")); } + public static Expression negate(Expression expr) { + if (expr.isUnaryExpr() && expr.asUnaryExpr().getOperator() == UnaryExpr.Operator.MINUS) { + return expr.asUnaryExpr().getExpression().clone(); + } else if (expr.isIntegerLiteralExpr()) { + return new IntegerLiteralExpr(-expr.asIntegerLiteralExpr().asInt()); + } else if (expr.isLongLiteralExpr()) { + return createLong(-expr.asLongLiteralExpr().asLong()); + } else { + throw new IllegalArgumentException(); + } + } + private ExprUtils() { /* empty */ } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/util/TypeUtils.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/util/TypeUtils.java new file mode 100644 index 0000000000..9ecb47e76e --- /dev/null +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/util/TypeUtils.java @@ -0,0 +1,13 @@ +package dev.openrs2.deob.ast.util; + +import com.github.javaparser.resolution.types.ResolvedType; + +public final class TypeUtils { + public static boolean isString(ResolvedType type) { + return type.isReferenceType() && type.asReferenceType().getQualifiedName().equals("java.lang.String"); + } + + private TypeUtils() { + /* empty */ + } +}