summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/Deobfuscator.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/cuchaz/enigma/Deobfuscator.java')
-rw-r--r--src/cuchaz/enigma/Deobfuscator.java115
1 files changed, 113 insertions, 2 deletions
diff --git a/src/cuchaz/enigma/Deobfuscator.java b/src/cuchaz/enigma/Deobfuscator.java
index 97c5750..b1abd9e 100644
--- a/src/cuchaz/enigma/Deobfuscator.java
+++ b/src/cuchaz/enigma/Deobfuscator.java
@@ -11,7 +11,9 @@
11package cuchaz.enigma; 11package cuchaz.enigma;
12 12
13import java.io.File; 13import java.io.File;
14import java.io.FileInputStream;
14import java.io.IOException; 15import java.io.IOException;
16import java.io.InputStream;
15import java.io.StringWriter; 17import java.io.StringWriter;
16import java.util.ArrayList; 18import java.util.ArrayList;
17import java.util.Collections; 19import java.util.Collections;
@@ -21,16 +23,27 @@ import java.util.List;
21import java.util.jar.JarEntry; 23import java.util.jar.JarEntry;
22import java.util.jar.JarFile; 24import java.util.jar.JarFile;
23 25
24import com.strobel.assembler.metadata.JarTypeLoader;
25import com.strobel.decompiler.Decompiler; 26import com.strobel.decompiler.Decompiler;
26import com.strobel.decompiler.DecompilerSettings; 27import com.strobel.decompiler.DecompilerSettings;
27import com.strobel.decompiler.PlainTextOutput; 28import com.strobel.decompiler.PlainTextOutput;
28 29
30import cuchaz.enigma.mapping.Ancestries;
31import cuchaz.enigma.mapping.ArgumentEntry;
32import cuchaz.enigma.mapping.ClassEntry;
33import cuchaz.enigma.mapping.Entry;
34import cuchaz.enigma.mapping.FieldEntry;
35import cuchaz.enigma.mapping.MethodEntry;
36import cuchaz.enigma.mapping.TranslationDirection;
37import cuchaz.enigma.mapping.TranslationMappings;
38import cuchaz.enigma.mapping.Translator;
39
29public class Deobfuscator 40public class Deobfuscator
30{ 41{
31 private File m_file; 42 private File m_file;
32 private JarFile m_jar; 43 private JarFile m_jar;
33 private DecompilerSettings m_settings; 44 private DecompilerSettings m_settings;
45 private Ancestries m_ancestries;
46 private TranslationMappings m_mappings;
34 47
35 private static Comparator<ClassFile> m_obfuscatedClassSorter; 48 private static Comparator<ClassFile> m_obfuscatedClassSorter;
36 49
@@ -56,8 +69,30 @@ public class Deobfuscator
56 { 69 {
57 m_file = file; 70 m_file = file;
58 m_jar = new JarFile( m_file ); 71 m_jar = new JarFile( m_file );
72
73 // build the ancestries
74 InputStream jarIn = null;
75 try
76 {
77 m_ancestries = new Ancestries();
78 jarIn = new FileInputStream( m_file );
79 m_ancestries.readFromJar( jarIn );
80 }
81 finally
82 {
83 Util.closeQuietly( jarIn );
84 }
85
86 // init mappings
87 m_mappings = new TranslationMappings( m_ancestries );
88
89 // config the decompiler
59 m_settings = DecompilerSettings.javaDefaults(); 90 m_settings = DecompilerSettings.javaDefaults();
60 m_settings.setTypeLoader( new JarTypeLoader( m_jar ) ); 91 m_settings.setTypeLoader( new TranslatingTypeLoader(
92 m_jar,
93 m_mappings.getTranslator( TranslationDirection.Deobfuscating ),
94 m_mappings.getTranslator( TranslationDirection.Obfuscating )
95 ) );
61 m_settings.setForceExplicitImports( true ); 96 m_settings.setForceExplicitImports( true );
62 m_settings.setShowSyntheticMembers( true ); 97 m_settings.setShowSyntheticMembers( true );
63 } 98 }
@@ -111,4 +146,80 @@ public class Deobfuscator
111 Decompiler.decompile( classFile.getName(), new PlainTextOutput( buf ), m_settings ); 146 Decompiler.decompile( classFile.getName(), new PlainTextOutput( buf ), m_settings );
112 return buf.toString(); 147 return buf.toString();
113 } 148 }
149
150 // NOTE: these methods are a bit messy... oh well
151
152 public void rename( Entry entry, String newName )
153 {
154 if( entry instanceof ClassEntry )
155 {
156 m_mappings.setClassName( (ClassEntry)entry, newName );
157 }
158 else if( entry instanceof FieldEntry )
159 {
160 m_mappings.setFieldName( (FieldEntry)entry, newName );
161 }
162 else if( entry instanceof MethodEntry )
163 {
164 m_mappings.setMethodName( (MethodEntry)entry, newName );
165 }
166 else if( entry instanceof ArgumentEntry )
167 {
168 m_mappings.setArgumentName( (ArgumentEntry)entry, newName );
169 }
170 else
171 {
172 throw new Error( "Unknown entry type: " + entry.getClass().getName() );
173 }
174 }
175
176 public Entry obfuscate( Entry in )
177 {
178 Translator translator = m_mappings.getTranslator( TranslationDirection.Obfuscating );
179 if( in instanceof ClassEntry )
180 {
181 return translator.translateEntry( (ClassEntry)in );
182 }
183 else if( in instanceof FieldEntry )
184 {
185 return translator.translateEntry( (FieldEntry)in );
186 }
187 else if( in instanceof MethodEntry )
188 {
189 return translator.translateEntry( (MethodEntry)in );
190 }
191 else if( in instanceof ArgumentEntry )
192 {
193 return translator.translateEntry( (ArgumentEntry)in );
194 }
195 else
196 {
197 throw new Error( "Unknown entry type: " + in.getClass().getName() );
198 }
199 }
200
201 public Entry deobfuscate( Entry in )
202 {
203 Translator translator = m_mappings.getTranslator( TranslationDirection.Deobfuscating );
204 if( in instanceof ClassEntry )
205 {
206 return translator.translateEntry( (ClassEntry)in );
207 }
208 else if( in instanceof FieldEntry )
209 {
210 return translator.translateEntry( (FieldEntry)in );
211 }
212 else if( in instanceof MethodEntry )
213 {
214 return translator.translateEntry( (MethodEntry)in );
215 }
216 else if( in instanceof ArgumentEntry )
217 {
218 return translator.translateEntry( (ArgumentEntry)in );
219 }
220 else
221 {
222 throw new Error( "Unknown entry type: " + in.getClass().getName() );
223 }
224 }
114} 225}