From 4d818291288a8c911e149aa05a5a3437279ed494 Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 13 Feb 2016 23:38:58 -0500 Subject: [PATCH] Beginning of rename deob --- .../deob/deobfuscators/rename/Rename.java | 173 ++++++++++++++++++ .../deobfuscators/rename/MapStaticTest.java | 24 ++- 2 files changed, 194 insertions(+), 3 deletions(-) create mode 100644 src/main/java/net/runelite/deob/deobfuscators/rename/Rename.java diff --git a/src/main/java/net/runelite/deob/deobfuscators/rename/Rename.java b/src/main/java/net/runelite/deob/deobfuscators/rename/Rename.java new file mode 100644 index 0000000000..4884b5dadf --- /dev/null +++ b/src/main/java/net/runelite/deob/deobfuscators/rename/Rename.java @@ -0,0 +1,173 @@ +package net.runelite.deob.deobfuscators.rename; + +import com.google.common.collect.Multimap; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import net.runelite.deob.ClassFile; +import net.runelite.deob.ClassGroup; +import net.runelite.deob.Field; +import net.runelite.deob.Method; +import net.runelite.deob.util.JarUtil; + +public class Rename +{ + private static final int MAX_CLASSES = 250; + + private ClassGroup source, target; + private ParallelExecutorMapping mapping; + + public Rename(ClassGroup source, ClassGroup target) + { + this.source = source; + this.target = target; + } + + public ParallelExecutorMapping getMapping() + { + return mapping; + } + + public void run() + { + ParallelExecutorMapping finalm = new ParallelExecutorMapping(source, target); + + finalm.merge(staticMethodSignatureMapper()); + finalm.merge(methodSignatureMapper()); + + for (int i = -1; i < MAX_CLASSES; ++i) + { + ClassFile c1; + + if (i == -1) + { + c1 = source.findClass("client"); + } + else + { + c1 = source.findClass("class" + i); + } + + if (c1 == null) + continue; + + for (Method m : c1.getMethods().getMethods()) + { + if (!finalm.getMap().containsKey(m)) + System.out.println("missing " + m); + } + for (Field m : c1.getFields().getFields()) + { + if (!finalm.getMap().containsKey(m)) + System.out.println("missing " + m); + } + } + + mapping = finalm; + } + + private ParallelExecutorMapping methodSignatureMapper() + { + List pmes = new ArrayList<>(); + + for (int i = -1; i < MAX_CLASSES; ++i) + { + ClassFile c1, c2; + + if (i == -1) + { + c1 = source.findClass("client"); + c2 = target.findClass("client"); + } + else + { + c1 = source.findClass("class" + i); + c2 = target.findClass("class" + i); + } + + if (c1 == null || c2 == null) + continue; + + MethodSignatureMapper msm = new MethodSignatureMapper(); + msm.map(c1, c2); + + Multimap map = msm.getMap(); + for (Method m : map.keySet()) + { + Collection methods = map.get(m); + + for (Method other : methods) + { + HashMap all = new HashMap(); + map(all, pmes, m, other); + } + } + } + + ParallelExecutorMapping finalm = new ParallelExecutorMapping(source, target); + for (ParallelExecutorMapping pme : pmes) + finalm.merge(pme); + + return finalm; + } + + private ParallelExecutorMapping staticMethodSignatureMapper() + { + StaticMethodSignatureMapper smsm = new StaticMethodSignatureMapper(); + smsm.map(source, target); + + List pmes = new ArrayList<>(); + + for (Method m : smsm.getMap().keySet()) + { + Collection methods = smsm.getMap().get(m); + + for (Method other : methods) + { + HashMap all = new HashMap(); + map(all, pmes, m, other); + } + } + + ParallelExecutorMapping finalm = new ParallelExecutorMapping(source, target); + for (ParallelExecutorMapping pme : pmes) + finalm.merge(pme); + + return finalm; + } + + private void map(Map all, List result, Method m1, Method m2) + { + if (all.containsKey(m1)) + return; + all.put(m1, m2); + + assert (m1.getCode() == null) == (m2.getCode() == null); + + if (m1.getCode() == null) + return; + + ParallelExecutorMapping mappings = MappingExecutorUtil.map(m1, m2); + + if (mappings.getMap().isEmpty() && mappings.crashed) + return; + + mappings.map(m1, m2); + result.add(mappings); + + for (Map.Entry e : mappings.getMap().entrySet()) + { + if (e.getKey() instanceof Method) + { + Method n1 = (Method) e.getKey(), + n2 = (Method) e.getValue(); + + map(all, result, n1, n2); + } + } + } +} diff --git a/src/test/java/net/runelite/deob/deobfuscators/rename/MapStaticTest.java b/src/test/java/net/runelite/deob/deobfuscators/rename/MapStaticTest.java index 332299b83b..6bb3e5012c 100644 --- a/src/test/java/net/runelite/deob/deobfuscators/rename/MapStaticTest.java +++ b/src/test/java/net/runelite/deob/deobfuscators/rename/MapStaticTest.java @@ -403,8 +403,8 @@ public class MapStaticTest return; // XXX this is the packet stuff.. - if (m1.getName().equals("vmethod3096")) - return; +// if (m1.getName().equals("vmethod3096")) +// return; ParallelExecutorMapping mappings = MappingExecutorUtil.map(m1, m2); @@ -447,7 +447,25 @@ public class MapStaticTest public void printTotalObjects() throws Exception { ClassGroup group1 = JarUtil.loadJar(new File(JAR1)); - //ClassGroup group2 = JarUtil.loadJar(new File(JAR2)); + ClassGroup group2 = JarUtil.loadJar(new File(JAR2)); //print(group1); } + + @Test + public void testCore() throws Exception + { + ClassGroup group1 = JarUtil.loadJar(new File(JAR1)); + ClassGroup group2 = JarUtil.loadJar(new File(JAR2)); + + Rename rename = new Rename(group1, group2); + rename.run(); + + summary(rename.getMapping(), group1); + + String sg1 = print(group1), + sg2 = print(group2); + + System.out.println("GROUP 1 " + sg1); + System.out.println("GROUP 2 " + sg2); + } }