diff options
| author | 2014-07-27 22:33:21 -0400 | |
|---|---|---|
| committer | 2014-07-27 22:33:21 -0400 | |
| commit | d7321b5b0d38c575e54c770f7aa18dacbacab3c8 (patch) | |
| tree | ef4b3e0f83b1fe89125c2674fec023871e70c0d8 /src/cuchaz/enigma/Deobfuscator.java | |
| parent | made gui responsive to caret position and show identifier info (diff) | |
| download | enigma-fork-d7321b5b0d38c575e54c770f7aa18dacbacab3c8.tar.gz enigma-fork-d7321b5b0d38c575e54c770f7aa18dacbacab3c8.tar.xz enigma-fork-d7321b5b0d38c575e54c770f7aa18dacbacab3c8.zip | |
added identifier renaming capability
copied some code over from M3L to handle the heavy bytecode magic.
It's ok... M3L will eventually depend on Enigma.
Completely restructured the mappings though. This way is better. =)
Diffstat (limited to 'src/cuchaz/enigma/Deobfuscator.java')
| -rw-r--r-- | src/cuchaz/enigma/Deobfuscator.java | 115 |
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 @@ | |||
| 11 | package cuchaz.enigma; | 11 | package cuchaz.enigma; |
| 12 | 12 | ||
| 13 | import java.io.File; | 13 | import java.io.File; |
| 14 | import java.io.FileInputStream; | ||
| 14 | import java.io.IOException; | 15 | import java.io.IOException; |
| 16 | import java.io.InputStream; | ||
| 15 | import java.io.StringWriter; | 17 | import java.io.StringWriter; |
| 16 | import java.util.ArrayList; | 18 | import java.util.ArrayList; |
| 17 | import java.util.Collections; | 19 | import java.util.Collections; |
| @@ -21,16 +23,27 @@ import java.util.List; | |||
| 21 | import java.util.jar.JarEntry; | 23 | import java.util.jar.JarEntry; |
| 22 | import java.util.jar.JarFile; | 24 | import java.util.jar.JarFile; |
| 23 | 25 | ||
| 24 | import com.strobel.assembler.metadata.JarTypeLoader; | ||
| 25 | import com.strobel.decompiler.Decompiler; | 26 | import com.strobel.decompiler.Decompiler; |
| 26 | import com.strobel.decompiler.DecompilerSettings; | 27 | import com.strobel.decompiler.DecompilerSettings; |
| 27 | import com.strobel.decompiler.PlainTextOutput; | 28 | import com.strobel.decompiler.PlainTextOutput; |
| 28 | 29 | ||
| 30 | import cuchaz.enigma.mapping.Ancestries; | ||
| 31 | import cuchaz.enigma.mapping.ArgumentEntry; | ||
| 32 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 33 | import cuchaz.enigma.mapping.Entry; | ||
| 34 | import cuchaz.enigma.mapping.FieldEntry; | ||
| 35 | import cuchaz.enigma.mapping.MethodEntry; | ||
| 36 | import cuchaz.enigma.mapping.TranslationDirection; | ||
| 37 | import cuchaz.enigma.mapping.TranslationMappings; | ||
| 38 | import cuchaz.enigma.mapping.Translator; | ||
| 39 | |||
| 29 | public class Deobfuscator | 40 | public 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 | } |