From e9b2ad6d628b866b2cb825687dd069904894bafc Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 11 Aug 2019 21:02:44 +0100 Subject: [PATCH] Add initial BinaryExprOrderTransformer --- .../dev/openrs2/deob/ast/AstDeobfuscator.java | 4 +- .../transform/BinaryExprOrderTransformer.java | 48 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.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 36bd452e..0c9c38a9 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 @@ -13,6 +13,7 @@ import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeS 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; import dev.openrs2.deob.ast.transform.Transformer; @@ -21,7 +22,8 @@ public final class AstDeobfuscator { private static final ImmutableList TRANSFORMERS = ImmutableList.of( new AddSubTransformer(), new ComplementTransformer(), - new IfElseTransformer() + new IfElseTransformer(), + new BinaryExprOrderTransformer() ); public static void main(String[] args) { 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 new file mode 100644 index 00000000..dc5c4618 --- /dev/null +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.java @@ -0,0 +1,48 @@ +package dev.openrs2.deob.ast.transform; + +import java.util.Optional; + +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.expr.BinaryExpr; + +public final class BinaryExprOrderTransformer extends Transformer { + private static Optional flip(BinaryExpr.Operator op) { + // TODO(gpe): handle the PLUS operator (we can't flip it if the type of the expression is String) + switch (op) { + case MULTIPLY: + case EQUALS: + case NOT_EQUALS: + case BINARY_AND: + case BINARY_OR: + case XOR: + case OR: + case AND: + return Optional.of(op); + case GREATER: + return Optional.of(BinaryExpr.Operator.LESS); + case GREATER_EQUALS: + return Optional.of(BinaryExpr.Operator.LESS_EQUALS); + case LESS: + return Optional.of(BinaryExpr.Operator.GREATER); + case LESS_EQUALS: + return Optional.of(BinaryExpr.Operator.GREATER_EQUALS); + default: + return Optional.empty(); + } + } + + @Override + public void transform(CompilationUnit unit) { + unit.findAll(BinaryExpr.class).forEach(expr -> { + flip(expr.getOperator()).ifPresent(op -> { + var left = expr.getLeft(); + var right = expr.getRight(); + if (left.isLiteralExpr() && !right.isLiteralExpr()) { + expr.setOperator(op); + expr.setLeft(right); + expr.setRight(left); + } + }); + }); + } +}