Add AddSubTransformer

master
Graham 5 years ago
parent 0b1d46eec4
commit eadea9231c
  1. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.java
  2. 51
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.java
  3. 8
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.java
  4. 14
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.java
  5. 14
      deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.java
  6. 13
      deob-ast/src/main/java/dev/openrs2/deob/ast/util/TypeUtils.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) {

@ -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));
}
});
}
}

@ -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<BinaryExpr.Operator> 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;
}

@ -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));
}
});
}

@ -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 */
}

@ -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 */
}
}
Loading…
Cancel
Save