summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar jeff2014-08-02 16:45:32 -0400
committerGravatar jeff2014-08-02 16:45:32 -0400
commitada041979ecf3dfd4543f3c250fcc78ad594866c (patch)
treeaf96a76ae6e1fbc9932de271defb6f4bdc42e565
parentfixed bug with save mappings menu (diff)
downloadenigma-fork-ada041979ecf3dfd4543f3c250fcc78ad594866c.tar.gz
enigma-fork-ada041979ecf3dfd4543f3c250fcc78ad594866c.tar.xz
enigma-fork-ada041979ecf3dfd4543f3c250fcc78ad594866c.zip
started working on method parameter renaming
-rw-r--r--src/cuchaz/enigma/Deobfuscator.java4
-rw-r--r--src/cuchaz/enigma/TranslatingTypeLoader.java10
-rw-r--r--src/cuchaz/enigma/bytecode/MethodParameterWriter.java35
-rw-r--r--src/cuchaz/enigma/bytecode/MethodParametersAttribute.java80
4 files changed, 123 insertions, 6 deletions
diff --git a/src/cuchaz/enigma/Deobfuscator.java b/src/cuchaz/enigma/Deobfuscator.java
index 619eebf..edc29e1 100644
--- a/src/cuchaz/enigma/Deobfuscator.java
+++ b/src/cuchaz/enigma/Deobfuscator.java
@@ -95,8 +95,8 @@ public class Deobfuscator
95 // update decompiler options 95 // update decompiler options
96 m_settings.setTypeLoader( new TranslatingTypeLoader( 96 m_settings.setTypeLoader( new TranslatingTypeLoader(
97 m_jar, 97 m_jar,
98 m_mappings.getTranslator( m_ancestries, TranslationDirection.Deobfuscating ), 98 m_mappings.getTranslator( m_ancestries, TranslationDirection.Obfuscating ),
99 m_mappings.getTranslator( m_ancestries, TranslationDirection.Obfuscating ) 99 m_mappings.getTranslator( m_ancestries, TranslationDirection.Deobfuscating )
100 ) ); 100 ) );
101 } 101 }
102 102
diff --git a/src/cuchaz/enigma/TranslatingTypeLoader.java b/src/cuchaz/enigma/TranslatingTypeLoader.java
index 872f486..cd36e8d 100644
--- a/src/cuchaz/enigma/TranslatingTypeLoader.java
+++ b/src/cuchaz/enigma/TranslatingTypeLoader.java
@@ -23,19 +23,20 @@ import com.strobel.assembler.metadata.Buffer;
23import com.strobel.assembler.metadata.ITypeLoader; 23import com.strobel.assembler.metadata.ITypeLoader;
24 24
25import cuchaz.enigma.bytecode.ClassTranslator; 25import cuchaz.enigma.bytecode.ClassTranslator;
26import cuchaz.enigma.bytecode.MethodParameterWriter;
26import cuchaz.enigma.mapping.Translator; 27import cuchaz.enigma.mapping.Translator;
27 28
28public class TranslatingTypeLoader implements ITypeLoader 29public class TranslatingTypeLoader implements ITypeLoader
29{ 30{
30 private JarFile m_jar; 31 private JarFile m_jar;
31 private ClassTranslator m_classTranslator;
32 private Translator m_obfuscatingTranslator; 32 private Translator m_obfuscatingTranslator;
33 private Translator m_deobfuscatingTranslator;
33 34
34 public TranslatingTypeLoader( JarFile jar, Translator deobfuscatingTranslator, Translator obfuscatingTranslator ) 35 public TranslatingTypeLoader( JarFile jar, Translator obfuscatingTranslator, Translator deobfuscatingTranslator )
35 { 36 {
36 m_jar = jar; 37 m_jar = jar;
37 m_classTranslator = new ClassTranslator( deobfuscatingTranslator );
38 m_obfuscatingTranslator = obfuscatingTranslator; 38 m_obfuscatingTranslator = obfuscatingTranslator;
39 m_deobfuscatingTranslator = deobfuscatingTranslator;
39 } 40 }
40 41
41 @Override 42 @Override
@@ -69,7 +70,8 @@ public class TranslatingTypeLoader implements ITypeLoader
69 try 70 try
70 { 71 {
71 CtClass c = classPool.get( name ); 72 CtClass c = classPool.get( name );
72 m_classTranslator.translate( c ); 73 new ClassTranslator( m_deobfuscatingTranslator ).translate( c );
74 new MethodParameterWriter( m_deobfuscatingTranslator ).writeMethodArguments( c );
73 buf = c.toBytecode(); 75 buf = c.toBytecode();
74 } 76 }
75 catch( Exception ex ) 77 catch( Exception ex )
diff --git a/src/cuchaz/enigma/bytecode/MethodParameterWriter.java b/src/cuchaz/enigma/bytecode/MethodParameterWriter.java
new file mode 100644
index 0000000..1e5d1f0
--- /dev/null
+++ b/src/cuchaz/enigma/bytecode/MethodParameterWriter.java
@@ -0,0 +1,35 @@
1/*******************************************************************************
2 * Copyright (c) 2014 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Public License v3.0
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/gpl.html
7 *
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11package cuchaz.enigma.bytecode;
12
13import javassist.CtBehavior;
14import javassist.CtClass;
15import javassist.bytecode.AttributeInfo;
16import cuchaz.enigma.mapping.Translator;
17
18public class MethodParameterWriter
19{
20 private Translator m_translator;
21
22 public MethodParameterWriter( Translator translator )
23 {
24 m_translator = translator;
25 }
26
27 public void writeMethodArguments( CtClass c )
28 {
29 // Procyon will read method arguments from the "MethodParameters" attribute, so write those
30 for( CtBehavior behavior : c.getDeclaredBehaviors() )
31 {
32 AttributeInfo attribute = behavior.getMethodInfo().getAttribute( "MethodParameter" );
33 }
34 }
35}
diff --git a/src/cuchaz/enigma/bytecode/MethodParametersAttribute.java b/src/cuchaz/enigma/bytecode/MethodParametersAttribute.java
new file mode 100644
index 0000000..0b29403
--- /dev/null
+++ b/src/cuchaz/enigma/bytecode/MethodParametersAttribute.java
@@ -0,0 +1,80 @@
1/*******************************************************************************
2 * Copyright (c) 2014 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Public License v3.0
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/gpl.html
7 *
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11package cuchaz.enigma.bytecode;
12
13import java.io.ByteArrayOutputStream;
14import java.io.DataOutputStream;
15import java.io.IOException;
16import java.util.List;
17
18import javassist.bytecode.AttributeInfo;
19import javassist.bytecode.ConstPool;
20
21public class MethodParametersAttribute extends AttributeInfo
22{
23 public MethodParametersAttribute( ConstPool pool, int attributeNameIndex, List<Integer> parameterNameIndices )
24 {
25 super( pool, "MethodParameters", writeStruct( attributeNameIndex, parameterNameIndices ) );
26 }
27
28 private static byte[] writeStruct( int attributeNameIndex, List<Integer> parameterNameIndices )
29 {
30 // JVM Spec says the struct looks like this:
31 // http://cr.openjdk.java.net/~mr/se/8/java-se-8-fr-spec-01/java-se-8-jvms-fr-diffs.pdf
32 // uint16 name_index -> points to UTF8 entry in constant pool that says "MethodParameters"
33 // uint32 length -> length of this struct, minus 6 bytes (ie, length of num_params and parameter array)
34 // uint8 num_params
35 // for each param:
36 // uint16 name_index -> points to UTF8 entry in constant pool, or 0 for no entry
37 // uint16 access_flags -> don't care, just set to 0
38
39 ByteArrayOutputStream buf = new ByteArrayOutputStream();
40 DataOutputStream out = new DataOutputStream( buf );
41
42 // NOTE: java hates unsigned integers, so we have to be careful here
43 // the writeShort(), writeByte() methods will read 16,8 low-order bits from the int argument
44 // as long as the int argument is in range of the unsigned short/byte type, it will be written as an unsigned short/byte
45 // if the int is out of range, the byte stream won't look the way we want and weird things will happen
46 final int SIZEOF_UINT16 = 2;
47 final int MAX_UINT8 = ( 1 << 8 ) - 1;
48 final int MAX_UINT16 = ( 1 << 16 ) - 1;
49 final long MAX_UINT32 = ( 1 << 32 ) - 1;
50
51 try
52 {
53 assert( attributeNameIndex >= 0 && attributeNameIndex <= MAX_UINT16 );
54 out.writeShort( attributeNameIndex );
55
56 long length = SIZEOF_UINT16 + parameterNameIndices.size()*( SIZEOF_UINT16 + SIZEOF_UINT16 );
57 assert( length >= 0 && length <= MAX_UINT32 );
58 out.writeInt( (int)length );
59
60 assert( parameterNameIndices.size() >= 0 && parameterNameIndices.size() <= MAX_UINT8 );
61 out.writeByte( parameterNameIndices.size() );
62
63 for( Integer index : parameterNameIndices )
64 {
65 assert( index >= 0 && index <= MAX_UINT16 );
66 out.writeShort( index );
67
68 // just write 0 for the access flags
69 out.writeShort( 0 );
70 }
71
72 out.close();
73 return buf.toByteArray();
74 }
75 catch( IOException ex )
76 {
77 throw new Error( ex );
78 }
79 }
80}