From ada041979ecf3dfd4543f3c250fcc78ad594866c Mon Sep 17 00:00:00 2001 From: jeff Date: Sat, 2 Aug 2014 16:45:32 -0400 Subject: started working on method parameter renaming --- src/cuchaz/enigma/Deobfuscator.java | 4 +- src/cuchaz/enigma/TranslatingTypeLoader.java | 10 +-- .../enigma/bytecode/MethodParameterWriter.java | 35 ++++++++++ .../enigma/bytecode/MethodParametersAttribute.java | 80 ++++++++++++++++++++++ 4 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 src/cuchaz/enigma/bytecode/MethodParameterWriter.java create mode 100644 src/cuchaz/enigma/bytecode/MethodParametersAttribute.java 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 // update decompiler options m_settings.setTypeLoader( new TranslatingTypeLoader( m_jar, - m_mappings.getTranslator( m_ancestries, TranslationDirection.Deobfuscating ), - m_mappings.getTranslator( m_ancestries, TranslationDirection.Obfuscating ) + m_mappings.getTranslator( m_ancestries, TranslationDirection.Obfuscating ), + m_mappings.getTranslator( m_ancestries, TranslationDirection.Deobfuscating ) ) ); } 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; import com.strobel.assembler.metadata.ITypeLoader; import cuchaz.enigma.bytecode.ClassTranslator; +import cuchaz.enigma.bytecode.MethodParameterWriter; import cuchaz.enigma.mapping.Translator; public class TranslatingTypeLoader implements ITypeLoader { private JarFile m_jar; - private ClassTranslator m_classTranslator; private Translator m_obfuscatingTranslator; + private Translator m_deobfuscatingTranslator; - public TranslatingTypeLoader( JarFile jar, Translator deobfuscatingTranslator, Translator obfuscatingTranslator ) + public TranslatingTypeLoader( JarFile jar, Translator obfuscatingTranslator, Translator deobfuscatingTranslator ) { m_jar = jar; - m_classTranslator = new ClassTranslator( deobfuscatingTranslator ); m_obfuscatingTranslator = obfuscatingTranslator; + m_deobfuscatingTranslator = deobfuscatingTranslator; } @Override @@ -69,7 +70,8 @@ public class TranslatingTypeLoader implements ITypeLoader try { CtClass c = classPool.get( name ); - m_classTranslator.translate( c ); + new ClassTranslator( m_deobfuscatingTranslator ).translate( c ); + new MethodParameterWriter( m_deobfuscatingTranslator ).writeMethodArguments( c ); buf = c.toBytecode(); } 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 @@ +/******************************************************************************* + * Copyright (c) 2014 Jeff Martin. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Public License v3.0 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/gpl.html + * + * Contributors: + * Jeff Martin - initial API and implementation + ******************************************************************************/ +package cuchaz.enigma.bytecode; + +import javassist.CtBehavior; +import javassist.CtClass; +import javassist.bytecode.AttributeInfo; +import cuchaz.enigma.mapping.Translator; + +public class MethodParameterWriter +{ + private Translator m_translator; + + public MethodParameterWriter( Translator translator ) + { + m_translator = translator; + } + + public void writeMethodArguments( CtClass c ) + { + // Procyon will read method arguments from the "MethodParameters" attribute, so write those + for( CtBehavior behavior : c.getDeclaredBehaviors() ) + { + AttributeInfo attribute = behavior.getMethodInfo().getAttribute( "MethodParameter" ); + } + } +} 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 @@ +/******************************************************************************* + * Copyright (c) 2014 Jeff Martin. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Public License v3.0 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/gpl.html + * + * Contributors: + * Jeff Martin - initial API and implementation + ******************************************************************************/ +package cuchaz.enigma.bytecode; + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.List; + +import javassist.bytecode.AttributeInfo; +import javassist.bytecode.ConstPool; + +public class MethodParametersAttribute extends AttributeInfo +{ + public MethodParametersAttribute( ConstPool pool, int attributeNameIndex, List parameterNameIndices ) + { + super( pool, "MethodParameters", writeStruct( attributeNameIndex, parameterNameIndices ) ); + } + + private static byte[] writeStruct( int attributeNameIndex, List parameterNameIndices ) + { + // JVM Spec says the struct looks like this: + // http://cr.openjdk.java.net/~mr/se/8/java-se-8-fr-spec-01/java-se-8-jvms-fr-diffs.pdf + // uint16 name_index -> points to UTF8 entry in constant pool that says "MethodParameters" + // uint32 length -> length of this struct, minus 6 bytes (ie, length of num_params and parameter array) + // uint8 num_params + // for each param: + // uint16 name_index -> points to UTF8 entry in constant pool, or 0 for no entry + // uint16 access_flags -> don't care, just set to 0 + + ByteArrayOutputStream buf = new ByteArrayOutputStream(); + DataOutputStream out = new DataOutputStream( buf ); + + // NOTE: java hates unsigned integers, so we have to be careful here + // the writeShort(), writeByte() methods will read 16,8 low-order bits from the int argument + // as long as the int argument is in range of the unsigned short/byte type, it will be written as an unsigned short/byte + // if the int is out of range, the byte stream won't look the way we want and weird things will happen + final int SIZEOF_UINT16 = 2; + final int MAX_UINT8 = ( 1 << 8 ) - 1; + final int MAX_UINT16 = ( 1 << 16 ) - 1; + final long MAX_UINT32 = ( 1 << 32 ) - 1; + + try + { + assert( attributeNameIndex >= 0 && attributeNameIndex <= MAX_UINT16 ); + out.writeShort( attributeNameIndex ); + + long length = SIZEOF_UINT16 + parameterNameIndices.size()*( SIZEOF_UINT16 + SIZEOF_UINT16 ); + assert( length >= 0 && length <= MAX_UINT32 ); + out.writeInt( (int)length ); + + assert( parameterNameIndices.size() >= 0 && parameterNameIndices.size() <= MAX_UINT8 ); + out.writeByte( parameterNameIndices.size() ); + + for( Integer index : parameterNameIndices ) + { + assert( index >= 0 && index <= MAX_UINT16 ); + out.writeShort( index ); + + // just write 0 for the access flags + out.writeShort( 0 ); + } + + out.close(); + return buf.toByteArray(); + } + catch( IOException ex ) + { + throw new Error( ex ); + } + } +} -- cgit v1.2.3