From 295eceece371b516e771de93b6127bf728999483 Mon Sep 17 00:00:00 2001 From: jeff Date: Sat, 26 Jul 2014 19:24:00 -0400 Subject: initial commit so far source analysis is working. =) --- src/cuchaz/enigma/ClassFile.java | 45 ++++ src/cuchaz/enigma/Deobfuscator.java | 109 +++++++++ src/cuchaz/enigma/Main.java | 63 ++++++ src/cuchaz/enigma/Util.java | 65 ++++++ src/cuchaz/enigma/analysis/Analyzer.java | 252 +++++++++++++++++++++ src/cuchaz/enigma/analysis/ClassNameIndex.java | 19 ++ .../enigma/analysis/JavaSourceFromString.java | 31 +++ src/cuchaz/enigma/analysis/Lexer.java | 41 ++++ src/cuchaz/enigma/analysis/SourceIndex.java | 57 +++++ src/cuchaz/enigma/analysis/SourcedAst.java | 111 +++++++++ src/cuchaz/enigma/gui/BoxHighlightPainter.java | 54 +++++ src/cuchaz/enigma/gui/ClassSelectionHandler.java | 18 ++ src/cuchaz/enigma/gui/Gui.java | 163 +++++++++++++ .../gui/ObfuscatedClassListCellRenderer.java | 40 ++++ src/cuchaz/enigma/gui/SourceFormatter.java | 62 +++++ src/cuchaz/enigma/mapping/ArgumentEntry.java | 88 +++++++ src/cuchaz/enigma/mapping/ClassEntry.java | 67 ++++++ src/cuchaz/enigma/mapping/FieldEntry.java | 76 +++++++ src/cuchaz/enigma/mapping/MethodEntry.java | 88 +++++++ 19 files changed, 1449 insertions(+) create mode 100644 src/cuchaz/enigma/ClassFile.java create mode 100644 src/cuchaz/enigma/Deobfuscator.java create mode 100644 src/cuchaz/enigma/Main.java create mode 100644 src/cuchaz/enigma/Util.java create mode 100644 src/cuchaz/enigma/analysis/Analyzer.java create mode 100644 src/cuchaz/enigma/analysis/ClassNameIndex.java create mode 100644 src/cuchaz/enigma/analysis/JavaSourceFromString.java create mode 100644 src/cuchaz/enigma/analysis/Lexer.java create mode 100644 src/cuchaz/enigma/analysis/SourceIndex.java create mode 100644 src/cuchaz/enigma/analysis/SourcedAst.java create mode 100644 src/cuchaz/enigma/gui/BoxHighlightPainter.java create mode 100644 src/cuchaz/enigma/gui/ClassSelectionHandler.java create mode 100644 src/cuchaz/enigma/gui/Gui.java create mode 100644 src/cuchaz/enigma/gui/ObfuscatedClassListCellRenderer.java create mode 100644 src/cuchaz/enigma/gui/SourceFormatter.java create mode 100644 src/cuchaz/enigma/mapping/ArgumentEntry.java create mode 100644 src/cuchaz/enigma/mapping/ClassEntry.java create mode 100644 src/cuchaz/enigma/mapping/FieldEntry.java create mode 100644 src/cuchaz/enigma/mapping/MethodEntry.java (limited to 'src') diff --git a/src/cuchaz/enigma/ClassFile.java b/src/cuchaz/enigma/ClassFile.java new file mode 100644 index 00000000..221a119e --- /dev/null +++ b/src/cuchaz/enigma/ClassFile.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * 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; + +import java.util.regex.Pattern; + +public class ClassFile +{ + private static Pattern m_obfuscatedClassPattern; + + static + { + m_obfuscatedClassPattern = Pattern.compile( "^[a-z]+$" ); + } + + private String m_name; + + public ClassFile( String name ) + { + m_name = name; + } + + public String getName( ) + { + return m_name; + } + + public boolean isObfuscated( ) + { + return m_obfuscatedClassPattern.matcher( m_name ).matches(); + } + + public String getPath( ) + { + return m_name.replace( ".", "/" ) + ".class"; + } +} diff --git a/src/cuchaz/enigma/Deobfuscator.java b/src/cuchaz/enigma/Deobfuscator.java new file mode 100644 index 00000000..83a21719 --- /dev/null +++ b/src/cuchaz/enigma/Deobfuscator.java @@ -0,0 +1,109 @@ +/******************************************************************************* + * 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; + +import java.io.File; +import java.io.IOException; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.Enumeration; +import java.util.List; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +import com.strobel.assembler.metadata.JarTypeLoader; +import com.strobel.decompiler.Decompiler; +import com.strobel.decompiler.DecompilerSettings; +import com.strobel.decompiler.PlainTextOutput; + +public class Deobfuscator +{ + private File m_file; + private JarFile m_jar; + private DecompilerSettings m_settings; + + private static Comparator m_obfuscatedClassSorter; + + static + { + m_obfuscatedClassSorter = new Comparator( ) + { + @Override + public int compare( ClassFile a, ClassFile b ) + { + if( a.getName().length() != b.getName().length() ) + { + return a.getName().length() - b.getName().length(); + } + + return a.getName().compareTo( b.getName() ); + } + }; + } + + public Deobfuscator( File file ) + throws IOException + { + m_file = file; + m_jar = new JarFile( m_file ); + m_settings = DecompilerSettings.javaDefaults(); + m_settings.setTypeLoader( new JarTypeLoader( m_jar ) ); + m_settings.setForceExplicitImports( true ); + m_settings.setShowSyntheticMembers( true ); + } + + public List getObfuscatedClasses( ) + { + List classes = new ArrayList(); + Enumeration entries = m_jar.entries(); + while( entries.hasMoreElements() ) + { + JarEntry entry = entries.nextElement(); + + // get the class name + String className = toClassName( entry.getName() ); + if( className == null ) + { + continue; + } + + ClassFile classFile = new ClassFile( className ); + if( classFile.isObfuscated() ) + { + classes.add( classFile ); + } + } + Collections.sort( classes, m_obfuscatedClassSorter ); + return classes; + } + + // TODO: could go somewhere more generic + private static String toClassName( String fileName ) + { + final String suffix = ".class"; + + if( !fileName.endsWith( suffix ) ) + { + return null; + } + + return fileName.substring( 0, fileName.length() - suffix.length() ).replace( "/", "." ); + } + + public String getSource( final ClassFile classFile ) + { + StringWriter buf = new StringWriter(); + Decompiler.decompile( classFile.getName(), new PlainTextOutput( buf ), m_settings ); + return buf.toString(); + } +} diff --git a/src/cuchaz/enigma/Main.java b/src/cuchaz/enigma/Main.java new file mode 100644 index 00000000..e08c16ed --- /dev/null +++ b/src/cuchaz/enigma/Main.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * 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; + +import java.io.File; + +import cuchaz.enigma.analysis.Analyzer; +import cuchaz.enigma.analysis.SourceIndex; +import cuchaz.enigma.gui.ClassSelectionHandler; +import cuchaz.enigma.gui.Gui; + +public class Main +{ + public static void main( String[] args ) + throws Exception + { + startGui(); + } + + private static void startGui( ) + throws Exception + { + final Gui gui = new Gui(); + + // settings + final File jarFile = new File( "/home/jeff/.minecraft/versions/1.7.10/1.7.10.jar" ); + gui.setTitle( jarFile.getName() ); + + // init the deobfuscator + final Deobfuscator deobfuscator = new Deobfuscator( jarFile ); + gui.setObfClasses( deobfuscator.getObfuscatedClasses() ); + + // handle events + gui.setClassSelectionHandler( new ClassSelectionHandler( ) + { + @Override + public void classSelected( final ClassFile classFile ) + { + gui.setSource( "(deobfuscating...)" ); + + // run the deobfuscator in a separate thread so we don't block the GUI event queue + new Thread( ) + { + @Override + public void run( ) + { + String source = deobfuscator.getSource( classFile ); + SourceIndex index = Analyzer.analyze( classFile.getName(), source ); + gui.setSource( source, index ); + } + }.start(); + } + } ); + } +} diff --git a/src/cuchaz/enigma/Util.java b/src/cuchaz/enigma/Util.java new file mode 100644 index 00000000..c51eb621 --- /dev/null +++ b/src/cuchaz/enigma/Util.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * 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; + +import java.io.Closeable; +import java.io.IOException; +import java.util.jar.JarFile; + + +public class Util +{ + public static int combineHashesOrdered( Object ... objs ) + { + final int prime = 67; + int result = 1; + for( Object obj : objs ) + { + result *= prime; + if( obj != null ) + { + result += obj.hashCode(); + } + } + return result; + } + + public static void closeQuietly( Closeable closeable ) + { + if( closeable != null ) + { + try + { + closeable.close(); + } + catch( IOException ex ) + { + // just ignore any further exceptions + } + } + } + + public static void closeQuietly( JarFile jarFile ) + { + // silly library should implement Closeable... + if( jarFile != null ) + { + try + { + jarFile.close(); + } + catch( IOException ex ) + { + // just ignore any further exceptions + } + } + } +} diff --git a/src/cuchaz/enigma/analysis/Analyzer.java b/src/cuchaz/enigma/analysis/Analyzer.java new file mode 100644 index 00000000..1cdabe75 --- /dev/null +++ b/src/cuchaz/enigma/analysis/Analyzer.java @@ -0,0 +1,252 @@ +/******************************************************************************* + * 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.analysis; + +import java.io.IOException; +import java.util.Arrays; + +import javax.tools.JavaCompiler; +import javax.tools.StandardJavaFileManager; +import javax.tools.ToolProvider; + +import jsyntaxpane.Token; +import jsyntaxpane.TokenType; + +import com.sun.source.tree.ArrayTypeTree; +import com.sun.source.tree.ClassTree; +import com.sun.source.tree.CompilationUnitTree; +import com.sun.source.tree.IdentifierTree; +import com.sun.source.tree.MethodTree; +import com.sun.source.tree.PrimitiveTypeTree; +import com.sun.source.tree.Tree; +import com.sun.source.tree.Tree.Kind; +import com.sun.source.tree.VariableTree; +import com.sun.source.util.JavacTask; +import com.sun.source.util.TreeScanner; +import com.sun.source.util.Trees; + +import cuchaz.enigma.mapping.ArgumentEntry; +import cuchaz.enigma.mapping.ClassEntry; +import cuchaz.enigma.mapping.FieldEntry; +import cuchaz.enigma.mapping.MethodEntry; + +class TreeVisitor extends TreeScanner +{ + private SourceIndex m_index; + + public TreeVisitor( SourceIndex index ) + { + m_index = index; + } + + @Override + public CompilationUnitTree visitClass( ClassTree classTree, SourcedAst ast ) + { + ClassEntry classEntry = indexClass( classTree, ast ); + + // look at the class members + for( Tree memberTree : classTree.getMembers() ) + { + if( memberTree.getKind() == Kind.VARIABLE ) + { + indexField( (VariableTree)memberTree, ast, classEntry ); + } + else if( memberTree.getKind() == Kind.METHOD ) + { + MethodTree methodTree = (MethodTree)memberTree; + MethodEntry methodEntry = indexMethod( methodTree, ast, classEntry ); + + // look at method arguments + int argNum = 0; + for( VariableTree variableTree : methodTree.getParameters() ) + { + indexArgument( variableTree, ast, methodEntry, argNum++ ); + } + } + } + + return super.visitClass( classTree, ast ); + } + + private ClassEntry indexClass( ClassTree classTree, SourcedAst ast ) + { + // build the entry + ClassEntry entry = new ClassEntry( ast.getFullClassName( classTree.getSimpleName().toString() ) ); + + // lex the source at this tree node + for( Token token : new Lexer( ast.getSource( classTree ).toString() ) ) + { + // scan until we get the first identifier + if( token.type == TokenType.IDENTIFIER ) + { + m_index.add( entry, offsetToken( token, ast.getStart( classTree ) ) ); + break; + } + } + + return entry; + } + + private FieldEntry indexField( VariableTree variableTree, SourcedAst ast, ClassEntry classEntry ) + { + // build the entry + FieldEntry entry = new FieldEntry( classEntry, variableTree.getName().toString() ); + + // lex the source at this tree node + Lexer lexer = new Lexer( ast.getSource( variableTree ).toString() ); + for( Token token : lexer ) + { + // scan until we find an identifier that matches the field name + if( token.type == TokenType.IDENTIFIER && lexer.getText( token ).equals( entry.getName() ) ) + { + m_index.add( entry, offsetToken( token, ast.getStart( variableTree ) ) ); + break; + } + } + + return entry; + } + + private MethodEntry indexMethod( MethodTree methodTree, SourcedAst ast, ClassEntry classEntry ) + { + // build the entry + StringBuilder signature = new StringBuilder(); + signature.append( "(" ); + for( VariableTree variableTree : methodTree.getParameters() ) + { + signature.append( toJvmType( variableTree.getType(), ast ) ); + } + signature.append( ")" ); + if( methodTree.getReturnType() != null ) + { + signature.append( toJvmType( methodTree.getReturnType(), ast ) ); + } + else + { + signature.append( "V" ); + } + MethodEntry entry = new MethodEntry( classEntry, methodTree.getName().toString(), signature.toString() ); + + // lex the source at this tree node + Lexer lexer = new Lexer( ast.getSource( methodTree ).toString() ); + for( Token token : lexer ) + { + // scan until we find an identifier that matches the method name + if( token.type == TokenType.IDENTIFIER && lexer.getText( token ).equals( entry.getName() ) ) + { + m_index.add( entry, offsetToken( token, ast.getStart( methodTree ) ) ); + break; + } + } + + return entry; + } + + private void indexArgument( VariableTree variableTree, SourcedAst ast, MethodEntry methodEntry, int index ) + { + System.out.println( "\tFound argument: " + variableTree.getName() ); + + // build the entry + ArgumentEntry entry = new ArgumentEntry( methodEntry, index, variableTree.getName().toString() ); + + // lex the source at this tree node + Lexer lexer = new Lexer( ast.getSource( variableTree ).toString() ); + for( Token token : lexer ) + { + // scan until we find an identifier that matches the variable name + if( token.type == TokenType.IDENTIFIER && lexer.getText( token ).equals( entry.getName() ) ) + { + m_index.add( entry, offsetToken( token, ast.getStart( variableTree ) ) ); + break; + } + } + } + + private Token offsetToken( Token in, int offset ) + { + return new Token( in.type, in.start + offset, in.length ); + } + + private String toJvmType( Tree tree, SourcedAst ast ) + { + switch( tree.getKind() ) + { + case PRIMITIVE_TYPE: + { + PrimitiveTypeTree primitiveTypeTree = (PrimitiveTypeTree)tree; + switch( primitiveTypeTree.getPrimitiveTypeKind() ) + { + case BOOLEAN: return "Z"; + case BYTE: return "B"; + case CHAR: return "C"; + case DOUBLE: return "D"; + case FLOAT: return "F"; + case INT: return "I"; + case LONG: return "J"; + case SHORT: return "S"; + case VOID: return "V"; + + default: + throw new Error( "Unsupported primitive type: " + primitiveTypeTree.getPrimitiveTypeKind() ); + } + } + + case IDENTIFIER: + { + IdentifierTree identifierTree = (IdentifierTree)tree; + String className = identifierTree.getName().toString(); + className = ast.getFullClassName( className ); + return "L" + className.replace( ".", "/" ) + ";"; + } + + case ARRAY_TYPE: + { + ArrayTypeTree arrayTree = (ArrayTypeTree)tree; + return "[" + toJvmType( arrayTree.getType(), ast ); + } + + + default: + throw new Error( "Unsupported type kind: " + tree.getKind() ); + } + } +} + +public class Analyzer +{ + public static SourceIndex analyze( String className, String source ) + { + SourceIndex index = new SourceIndex(); + SourcedAst ast = getAst( className, source ); + ast.visit( new TreeVisitor( index ) ); + return index; + } + + private static SourcedAst getAst( String className, String source ) + { + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null ); + JavaSourceFromString unit = new JavaSourceFromString( className, source ); + JavacTask task = (JavacTask)compiler.getTask( null, fileManager, null, null, null, Arrays.asList( unit ) ); + + try + { + return new SourcedAst( + task.parse().iterator().next(), + Trees.instance( task ) + ); + } + catch( IOException ex ) + { + throw new Error( ex ); + } + } +} diff --git a/src/cuchaz/enigma/analysis/ClassNameIndex.java b/src/cuchaz/enigma/analysis/ClassNameIndex.java new file mode 100644 index 00000000..ea3e2cae --- /dev/null +++ b/src/cuchaz/enigma/analysis/ClassNameIndex.java @@ -0,0 +1,19 @@ +/******************************************************************************* + * 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.analysis; + +import com.sun.source.tree.CompilationUnitTree; +import com.sun.source.util.TreeScanner; + +public class ClassNameIndex extends TreeScanner +{ + +} diff --git a/src/cuchaz/enigma/analysis/JavaSourceFromString.java b/src/cuchaz/enigma/analysis/JavaSourceFromString.java new file mode 100644 index 00000000..cf5c4c27 --- /dev/null +++ b/src/cuchaz/enigma/analysis/JavaSourceFromString.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * 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.analysis; + +import java.net.URI; + +import javax.tools.SimpleJavaFileObject; + +public class JavaSourceFromString extends SimpleJavaFileObject +{ + private final String m_source; + + JavaSourceFromString( String name, String source ) + { + super( URI.create( "string:///" + name.replace( '.', '/' ) + Kind.SOURCE.extension ), Kind.SOURCE ); + m_source = source; + } + + public CharSequence getCharContent( boolean ignoreEncodingErrors ) + { + return m_source; + } +} diff --git a/src/cuchaz/enigma/analysis/Lexer.java b/src/cuchaz/enigma/analysis/Lexer.java new file mode 100644 index 00000000..acb52bf8 --- /dev/null +++ b/src/cuchaz/enigma/analysis/Lexer.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * 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.analysis; + +import java.util.Iterator; + +import jsyntaxpane.SyntaxDocument; +import jsyntaxpane.Token; +import jsyntaxpane.lexers.JavaLexer; + +public class Lexer implements Iterable +{ + private SyntaxDocument m_doc; + private Iterator m_iter; + + public Lexer( String source ) + { + m_doc = new SyntaxDocument( new JavaLexer() ); + m_doc.append( source ); + m_iter = m_doc.getTokens( 0, m_doc.getLength() ); + } + + @Override + public Iterator iterator( ) + { + return m_iter; + } + + public String getText( Token token ) + { + return token.getString( m_doc ); + } +} diff --git a/src/cuchaz/enigma/analysis/SourceIndex.java b/src/cuchaz/enigma/analysis/SourceIndex.java new file mode 100644 index 00000000..a4b5329b --- /dev/null +++ b/src/cuchaz/enigma/analysis/SourceIndex.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * 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.analysis; + +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import jsyntaxpane.Token; + +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; + +public class SourceIndex implements Iterable> +{ + private BiMap m_entryToToken; + private BiMap m_tokenToEntry; + + public SourceIndex( ) + { + m_entryToToken = HashBiMap.create(); + m_tokenToEntry = m_entryToToken.inverse(); + } + + public void add( Object entry, Token token ) + { + m_entryToToken.put( entry, token ); + } + + public Iterator> iterator( ) + { + return m_entryToToken.entrySet().iterator(); + } + + public Set tokens( ) + { + return m_entryToToken.values(); + } + + public Object getEntry( Token token ) + { + return m_tokenToEntry.get( token ); + } + + public Object getToken( Object entry ) + { + return m_entryToToken.get( entry ); + } +} diff --git a/src/cuchaz/enigma/analysis/SourcedAst.java b/src/cuchaz/enigma/analysis/SourcedAst.java new file mode 100644 index 00000000..04c6f03b --- /dev/null +++ b/src/cuchaz/enigma/analysis/SourcedAst.java @@ -0,0 +1,111 @@ +/******************************************************************************* + * 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.analysis; + +import java.io.IOException; +import java.util.HashMap; + +import com.google.common.collect.Maps; +import com.sun.source.tree.CompilationUnitTree; +import com.sun.source.tree.ImportTree; +import com.sun.source.tree.Tree; +import com.sun.source.util.SourcePositions; +import com.sun.source.util.Trees; + +public class SourcedAst +{ + private CompilationUnitTree m_tree; + private Trees m_trees; + private SourcePositions m_positions; + private HashMap m_classNameIndex; + + public SourcedAst( CompilationUnitTree tree, Trees trees ) + { + m_tree = tree; + m_trees = trees; + m_positions = m_trees.getSourcePositions(); + m_classNameIndex = Maps.newHashMap(); + + // index all the class names + for( ImportTree importTree : m_tree.getImports() ) + { + // ignore static imports for now + if( importTree.isStatic() ) + { + continue; + } + + // get the full and simple class names + String fullName = importTree.getQualifiedIdentifier().toString(); + String simpleName = fullName; + String[] parts = fullName.split( "\\." ); + if( parts.length > 0 ) + { + simpleName = parts[parts.length - 1]; + } + + m_classNameIndex.put( simpleName, fullName ); + } + } + + public int getStart( Tree node ) + { + return (int)m_positions.getStartPosition( m_tree, node ); + } + + public int getEnd( Tree node ) + { + return (int)m_positions.getEndPosition( m_tree, node ); + } + + public int getLine( Tree node ) + { + return getLine( getStart( node ) ); + } + + public int getLine( int pos ) + { + return (int)m_tree.getLineMap().getLineNumber( pos ); + } + + public CharSequence getSource( ) + { + try + { + return m_tree.getSourceFile().getCharContent( true ); + } + catch( IOException ex ) + { + throw new Error( ex ); + } + } + + public CharSequence getSource( Tree node ) + { + return getSource().subSequence( getStart( node ), getEnd( node ) ); + } + + public void visit( TreeVisitor visitor ) + { + m_tree.accept( visitor, this ); + } + + public String getFullClassName( String simpleClassName ) + { + String fullClassName = m_classNameIndex.get( simpleClassName ); + if( fullClassName == null ) + { + // no mapping was found, the name is probably already fully-qualified + fullClassName = simpleClassName; + } + return fullClassName; + } +} diff --git a/src/cuchaz/enigma/gui/BoxHighlightPainter.java b/src/cuchaz/enigma/gui/BoxHighlightPainter.java new file mode 100644 index 00000000..22db28b7 --- /dev/null +++ b/src/cuchaz/enigma/gui/BoxHighlightPainter.java @@ -0,0 +1,54 @@ +/******************************************************************************* + * 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.gui; + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Rectangle; +import java.awt.Shape; + +import javax.swing.text.BadLocationException; +import javax.swing.text.Highlighter; +import javax.swing.text.JTextComponent; + +public class BoxHighlightPainter implements Highlighter.HighlightPainter +{ + private static final Color FillColor = new Color( 230, 230, 230 ); + private static final Color BorderColor = new Color( 100, 100, 100 ); + + @Override + public void paint( Graphics g, int start, int end, Shape shape, JTextComponent text ) + { + try + { + // determine the bounds of the text + Rectangle bounds = text.getUI().modelToView( text, start ).union( text.getUI().modelToView( text, end ) ); + + // adjust the box so it looks nice + bounds.x -= 2; + bounds.width += 2; + bounds.y += 1; + bounds.height -= 2; + + // fill the area + g.setColor( FillColor ); + g.fillRoundRect( bounds.x, bounds.y, bounds.width, bounds.height, 4, 4 ); + + // draw a box around the area + g.setColor( BorderColor ); + g.drawRoundRect( bounds.x, bounds.y, bounds.width, bounds.height, 4, 4 ); + } + catch( BadLocationException ex ) + { + throw new Error( ex ); + } + } +} diff --git a/src/cuchaz/enigma/gui/ClassSelectionHandler.java b/src/cuchaz/enigma/gui/ClassSelectionHandler.java new file mode 100644 index 00000000..a50cf6a3 --- /dev/null +++ b/src/cuchaz/enigma/gui/ClassSelectionHandler.java @@ -0,0 +1,18 @@ +/******************************************************************************* + * 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.gui; + +import cuchaz.enigma.ClassFile; + +public interface ClassSelectionHandler +{ + void classSelected( ClassFile classFile ); +} diff --git a/src/cuchaz/enigma/gui/Gui.java b/src/cuchaz/enigma/gui/Gui.java new file mode 100644 index 00000000..e0d53d83 --- /dev/null +++ b/src/cuchaz/enigma/gui/Gui.java @@ -0,0 +1,163 @@ +/******************************************************************************* + * 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.gui; + +import java.awt.BorderLayout; +import java.awt.Container; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.List; +import java.util.Vector; + +import javax.swing.JEditorPane; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JSplitPane; +import javax.swing.ListSelectionModel; +import javax.swing.WindowConstants; +import javax.swing.text.BadLocationException; + +import jsyntaxpane.DefaultSyntaxKit; +import jsyntaxpane.Token; +import cuchaz.enigma.ClassFile; +import cuchaz.enigma.analysis.SourceIndex; + +public class Gui +{ + private static final String Name = "Enigma"; + + // controls + private JFrame m_frame; + private JList m_obfClasses; + private JList m_deobfClasses; + private JEditorPane m_editor; + + // handlers + private ClassSelectionHandler m_classSelectionHandler; + + private BoxHighlightPainter m_highlightPainter; + + public Gui( ) + { + // init frame + m_frame = new JFrame( Name ); + final Container pane = m_frame.getContentPane(); + pane.setLayout( new BorderLayout() ); + + // init obfuscated classes list + m_obfClasses = new JList(); + m_obfClasses.setSelectionMode( ListSelectionModel.SINGLE_SELECTION ); + m_obfClasses.setLayoutOrientation( JList.VERTICAL ); + m_obfClasses.setCellRenderer( new ObfuscatedClassListCellRenderer() ); + m_obfClasses.addMouseListener( new MouseAdapter() + { + public void mouseClicked( MouseEvent event ) + { + if( event.getClickCount() == 2 ) + { + if( m_classSelectionHandler != null ) + { + ClassFile selected = m_obfClasses.getSelectedValue(); + if( selected != null ) + { + m_classSelectionHandler.classSelected( selected ); + } + } + } + } + } ); + JScrollPane obfScroller = new JScrollPane( m_obfClasses ); + JPanel obfPanel = new JPanel(); + obfPanel.setLayout( new BorderLayout() ); + obfPanel.add( new JLabel( "Obfuscated Classes" ), BorderLayout.NORTH ); + obfPanel.add( obfScroller, BorderLayout.CENTER ); + + // init deobfuscated classes list + m_deobfClasses = new JList(); + m_obfClasses.setSelectionMode( ListSelectionModel.SINGLE_SELECTION ); + m_obfClasses.setLayoutOrientation( JList.VERTICAL ); + JScrollPane deobfScroller = new JScrollPane( m_deobfClasses ); + JPanel deobfPanel = new JPanel(); + deobfPanel.setLayout( new BorderLayout() ); + deobfPanel.add( new JLabel( "De-obfuscated Classes" ), BorderLayout.NORTH ); + deobfPanel.add( deobfScroller, BorderLayout.CENTER ); + + // init editor + DefaultSyntaxKit.initKit(); + m_editor = new JEditorPane(); + m_editor.setEditable( false ); + JScrollPane sourceScroller = new JScrollPane( m_editor ); + m_editor.setContentType( "text/java" ); + + // layout controls + JSplitPane splitLeft = new JSplitPane( JSplitPane.VERTICAL_SPLIT, true, obfPanel, deobfPanel ); + JSplitPane splitMain = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, true, splitLeft, sourceScroller ); + pane.add( splitMain, BorderLayout.CENTER ); + + // show the frame + pane.doLayout(); + m_frame.setSize( 800, 600 ); + m_frame.setVisible( true ); + m_frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE ); + + // init handlers + m_classSelectionHandler = null; + + m_highlightPainter = new BoxHighlightPainter(); + } + + public void setTitle( String title ) + { + m_frame.setTitle( Name + " - " + title ); + } + + public void setObfClasses( List classes ) + { + m_obfClasses.setListData( new Vector( classes ) ); + } + + public void setSource( String source ) + { + setSource( source, null ); + } + + public void setSource( String source, SourceIndex index ) + { + m_editor.setText( source ); + + // remove any old highlighters + m_editor.getHighlighter().removeAllHighlights();; + + if( index != null ) + { + // color things based on the index + for( Token token : index.tokens() ) + { + try + { + m_editor.getHighlighter().addHighlight( token.start, token.end(), m_highlightPainter ); + } + catch( BadLocationException ex ) + { + throw new Error( ex ); + } + } + } + } + + public void setClassSelectionHandler( ClassSelectionHandler val ) + { + m_classSelectionHandler = val; + } +} diff --git a/src/cuchaz/enigma/gui/ObfuscatedClassListCellRenderer.java b/src/cuchaz/enigma/gui/ObfuscatedClassListCellRenderer.java new file mode 100644 index 00000000..0badb3b9 --- /dev/null +++ b/src/cuchaz/enigma/gui/ObfuscatedClassListCellRenderer.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * 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.gui; + +import java.awt.Component; + +import javax.swing.DefaultListCellRenderer; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.ListCellRenderer; + +import cuchaz.enigma.ClassFile; + +public class ObfuscatedClassListCellRenderer implements ListCellRenderer +{ + private DefaultListCellRenderer m_defaultRenderer; + + public ObfuscatedClassListCellRenderer( ) + { + m_defaultRenderer = new DefaultListCellRenderer(); + } + + @Override + public Component getListCellRendererComponent( JList list, ClassFile classFile, int index, boolean isSelected, boolean hasFocus ) + { + JLabel label = (JLabel)m_defaultRenderer.getListCellRendererComponent( list, classFile, index, isSelected, hasFocus ); + + label.setText( classFile.getName() ); + + return label; + } +} diff --git a/src/cuchaz/enigma/gui/SourceFormatter.java b/src/cuchaz/enigma/gui/SourceFormatter.java new file mode 100644 index 00000000..f3878405 --- /dev/null +++ b/src/cuchaz/enigma/gui/SourceFormatter.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * 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.gui; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; + +public class SourceFormatter +{ + public static String format( String in ) + { + return collapseNewlines( in ); + } + + private static String collapseNewlines( String in ) + { + StringBuffer buf = new StringBuffer(); + int numBlankLines = 0; + + BufferedReader reader = new BufferedReader( new StringReader( in ) ); + String line = null; + try + { + while( ( line = reader.readLine() ) != null ) + { + // how blank lines is this? + boolean isBlank = line.trim().length() == 0; + if( isBlank ) + { + numBlankLines++; + + // stop printing blank lines after the first one + if( numBlankLines < 2 ) + { + buf.append( line ); + buf.append( "\n" ); + } + } + else + { + numBlankLines = 0; + buf.append( line ); + buf.append( "\n" ); + } + } + } + catch( IOException ex ) + { + // StringReader will never throw an IOExecption here... + } + return buf.toString(); + } +} diff --git a/src/cuchaz/enigma/mapping/ArgumentEntry.java b/src/cuchaz/enigma/mapping/ArgumentEntry.java new file mode 100644 index 00000000..6c108d7c --- /dev/null +++ b/src/cuchaz/enigma/mapping/ArgumentEntry.java @@ -0,0 +1,88 @@ +/******************************************************************************* + * 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.mapping; + +import java.io.Serializable; + +import cuchaz.enigma.Util; + +public class ArgumentEntry implements Serializable +{ + private static final long serialVersionUID = 4472172468162696006L; + + private MethodEntry m_methodEntry; + private int m_index; + private String m_name; + + public ArgumentEntry( MethodEntry methodEntry, int index, String name ) + { + if( methodEntry == null ) + { + throw new IllegalArgumentException( "Method cannot be null!" ); + } + if( index < 0 ) + { + throw new IllegalArgumentException( "Index must be non-negative!" ); + } + if( name == null ) + { + throw new IllegalArgumentException( "Argument name cannot be null!" ); + } + + m_methodEntry = methodEntry; + m_index = index; + m_name = name; + } + + public MethodEntry getMethodEntry( ) + { + return m_methodEntry; + } + + public int getIndex( ) + { + return m_index; + } + + public String getName( ) + { + return m_name; + } + + @Override + public int hashCode( ) + { + return Util.combineHashesOrdered( m_methodEntry, Integer.valueOf( m_index ).hashCode(), m_name.hashCode() ); + } + + @Override + public boolean equals( Object other ) + { + if( other instanceof ArgumentEntry ) + { + return equals( (ArgumentEntry)other ); + } + return false; + } + + public boolean equals( ArgumentEntry other ) + { + return m_methodEntry.equals( other.m_methodEntry ) + && m_index == other.m_index + && m_name.equals( other.m_name ); + } + + @Override + public String toString( ) + { + return m_methodEntry.toString() + "(" + m_index + ":" + m_name + ")"; + } +} diff --git a/src/cuchaz/enigma/mapping/ClassEntry.java b/src/cuchaz/enigma/mapping/ClassEntry.java new file mode 100644 index 00000000..7e78d4c0 --- /dev/null +++ b/src/cuchaz/enigma/mapping/ClassEntry.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * 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.mapping; + +import java.io.Serializable; + + +public class ClassEntry implements Serializable +{ + private static final long serialVersionUID = 4235460580973955811L; + + private String m_name; + + public ClassEntry( String className ) + { + if( className == null ) + { + throw new IllegalArgumentException( "Class name cannot be null!" ); + } + if( className.contains( "." ) ) + { + throw new IllegalArgumentException( "Class name must be in JVM format. ie, path/to/package/class$inner" ); + } + + m_name = className; + } + + public String getName( ) + { + return m_name; + } + + @Override + public int hashCode( ) + { + return m_name.hashCode(); + } + + @Override + public boolean equals( Object other ) + { + if( other instanceof ClassEntry ) + { + return equals( (ClassEntry)other ); + } + return false; + } + + public boolean equals( ClassEntry other ) + { + return m_name.equals( other.m_name ); + } + + @Override + public String toString( ) + { + return m_name; + } +} diff --git a/src/cuchaz/enigma/mapping/FieldEntry.java b/src/cuchaz/enigma/mapping/FieldEntry.java new file mode 100644 index 00000000..15a93520 --- /dev/null +++ b/src/cuchaz/enigma/mapping/FieldEntry.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * 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.mapping; + +import java.io.Serializable; + +import cuchaz.enigma.Util; + +public class FieldEntry implements Serializable +{ + private static final long serialVersionUID = 3004663582802885451L; + + private ClassEntry m_classEntry; + private String m_name; + + public FieldEntry( ClassEntry classEntry, String name ) + { + if( classEntry == null ) + { + throw new IllegalArgumentException( "Class cannot be null!" ); + } + if( name == null ) + { + throw new IllegalArgumentException( "Field name cannot be null!" ); + } + + m_classEntry = classEntry; + m_name = name; + } + + public ClassEntry getClassEntry( ) + { + return m_classEntry; + } + + public String getName( ) + { + return m_name; + } + + @Override + public int hashCode( ) + { + return Util.combineHashesOrdered( m_classEntry, m_name ); + } + + @Override + public boolean equals( Object other ) + { + if( other instanceof FieldEntry ) + { + return equals( (FieldEntry)other ); + } + return false; + } + + public boolean equals( FieldEntry other ) + { + return m_classEntry.equals( other.m_classEntry ) + && m_name.equals( other.m_name ); + } + + @Override + public String toString( ) + { + return m_classEntry.getName() + "." + m_name; + } +} diff --git a/src/cuchaz/enigma/mapping/MethodEntry.java b/src/cuchaz/enigma/mapping/MethodEntry.java new file mode 100644 index 00000000..e71a4664 --- /dev/null +++ b/src/cuchaz/enigma/mapping/MethodEntry.java @@ -0,0 +1,88 @@ +/******************************************************************************* + * 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.mapping; + +import java.io.Serializable; + +import cuchaz.enigma.Util; + +public class MethodEntry implements Serializable +{ + private static final long serialVersionUID = 4770915224467247458L; + + private ClassEntry m_classEntry; + private String m_name; + private String m_signature; + + public MethodEntry( ClassEntry classEntry, String name, String signature ) + { + if( classEntry == null ) + { + throw new IllegalArgumentException( "Class cannot be null!" ); + } + if( name == null ) + { + throw new IllegalArgumentException( "Method name cannot be null!" ); + } + if( signature == null ) + { + throw new IllegalArgumentException( "Method signature cannot be null!" ); + } + + m_classEntry = classEntry; + m_name = name; + m_signature = signature; + } + + public ClassEntry getClassEntry( ) + { + return m_classEntry; + } + + public String getName( ) + { + return m_name; + } + + public String getSignature( ) + { + return m_signature; + } + + @Override + public int hashCode( ) + { + return Util.combineHashesOrdered( m_classEntry, m_name, m_signature ); + } + + @Override + public boolean equals( Object other ) + { + if( other instanceof MethodEntry ) + { + return equals( (MethodEntry)other ); + } + return false; + } + + public boolean equals( MethodEntry other ) + { + return m_classEntry.equals( other.m_classEntry ) + && m_name.equals( other.m_name ) + && m_signature.equals( other.m_signature ); + } + + @Override + public String toString( ) + { + return m_classEntry.getName() + "." + m_name + ":" + m_signature; + } +} -- cgit v1.2.3