From 603245ee6218668eb8eb39e63ecedce257b3ef35 Mon Sep 17 00:00:00 2001 From: jeff Date: Mon, 11 Aug 2014 23:18:10 -0400 Subject: refactor Ancestries into Ancestries and JarIndex --- src/cuchaz/enigma/analysis/Ancestries.java | 145 ----------------------------- 1 file changed, 145 deletions(-) (limited to 'src/cuchaz/enigma/analysis/Ancestries.java') diff --git a/src/cuchaz/enigma/analysis/Ancestries.java b/src/cuchaz/enigma/analysis/Ancestries.java index e6c8bbf..83c239c 100644 --- a/src/cuchaz/enigma/analysis/Ancestries.java +++ b/src/cuchaz/enigma/analysis/Ancestries.java @@ -10,110 +10,27 @@ ******************************************************************************/ package cuchaz.enigma.analysis; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; import java.io.Serializable; import java.util.ArrayList; -import java.util.Collection; import java.util.List; import java.util.Map; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; -import javassist.ByteArrayClassPath; -import javassist.ClassPool; -import javassist.CtClass; -import javassist.NotFoundException; import javassist.bytecode.Descriptor; -import javassist.bytecode.MethodInfo; -import com.google.common.collect.HashMultimap; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.google.common.collect.Multimap; - -import cuchaz.enigma.Constants; -import cuchaz.enigma.mapping.ClassEntry; -import cuchaz.enigma.mapping.MethodEntry; -import cuchaz.enigma.mapping.Translator; public class Ancestries implements Serializable { private static final long serialVersionUID = 738687982126844179L; private Map m_superclasses; - private Multimap m_methodImplementations; public Ancestries( ) { m_superclasses = Maps.newHashMap(); - m_methodImplementations = HashMultimap.create(); } - @SuppressWarnings( "unchecked" ) - public void readFromJar( InputStream in ) - throws IOException - { - ClassPool classPool = new ClassPool(); - - ZipInputStream zin = new ZipInputStream( in ); - ZipEntry entry; - while( ( entry = zin.getNextEntry() ) != null ) - { - // filter out non-classes - if( entry.isDirectory() || !entry.getName().endsWith( ".class" ) ) - { - continue; - } - - // read the class into a buffer - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - byte[] buf = new byte[Constants.KiB]; - int totalNumBytesRead = 0; - while( zin.available() > 0 ) - { - int numBytesRead = zin.read( buf ); - if( numBytesRead < 0 ) - { - break; - } - bos.write( buf, 0, numBytesRead ); - - // sanity checking - totalNumBytesRead += numBytesRead; - if( totalNumBytesRead > Constants.MiB ) - { - throw new Error( "Class file " + entry.getName() + " larger than 1 MiB! Something is wrong!" ); - } - } - - // determine the class name (ie chop off the ".class") - String className = Descriptor.toJavaName( entry.getName().substring( 0, entry.getName().length() - ".class".length() ) ); - - // get a javassist handle for the class - classPool.insertClassPath( new ByteArrayClassPath( className, bos.toByteArray() ) ); - try - { - CtClass c = classPool.get( className ); - addSuperclass( c.getName(), c.getClassFile().getSuperclass() ); - addMethodImplementations( c.getName(), (List)c.getClassFile().getMethods() ); - } - catch( NotFoundException ex ) - { - throw new Error( "Unable to load class: " + className ); - } - } - } - - private void addMethodImplementations( String name, List methods ) - { - for( MethodInfo method : methods ) - { - m_methodImplementations.put( name, getMethodKey( method.getName(), method.getDescriptor() ) ); - } - } - public void addSuperclass( String className, String superclassName ) { className = Descriptor.toJvmName( className ); @@ -165,71 +82,9 @@ public class Ancestries implements Serializable return subclasses; } - public boolean isMethodImplemented( MethodEntry methodEntry ) - { - return isMethodImplemented( methodEntry.getClassName(), methodEntry.getName(), methodEntry.getSignature() ); - } - - public boolean isMethodImplemented( String className, String methodName, String methodSignature ) - { - Collection implementations = m_methodImplementations.get( className ); - if( implementations == null ) - { - return false; - } - return implementations.contains( getMethodKey( methodName, methodSignature ) ); - } - - public ClassInheritanceTreeNode getClassInheritance( Translator deobfuscatingTranslator, ClassEntry obfClassEntry ) - { - // get the root node - List ancestry = getAncestry( obfClassEntry.getName() ); - ClassInheritanceTreeNode rootNode = new ClassInheritanceTreeNode( deobfuscatingTranslator, ancestry.get( ancestry.size() - 1 ) ); - - // expand all children recursively - rootNode.load( this, true ); - - return rootNode; - } - - public MethodInheritanceTreeNode getMethodInheritance( Translator deobfuscatingTranslator, MethodEntry obfMethodEntry ) - { - // travel to the ancestor implementation - String baseImplementationClassName = obfMethodEntry.getClassName(); - for( String ancestorClassName : getAncestry( obfMethodEntry.getClassName() ) ) - { - if( isMethodImplemented( ancestorClassName, obfMethodEntry.getName(), obfMethodEntry.getSignature() ) ) - { - baseImplementationClassName = ancestorClassName; - } - } - - // make a root node at the base - MethodEntry methodEntry = new MethodEntry( - new ClassEntry( baseImplementationClassName ), - obfMethodEntry.getName(), - obfMethodEntry.getSignature() - ); - MethodInheritanceTreeNode rootNode = new MethodInheritanceTreeNode( - deobfuscatingTranslator, - methodEntry, - isMethodImplemented( methodEntry ) - ); - - // expand the full tree - rootNode.load( this, true ); - - return rootNode; - } - private boolean isJre( String className ) { return className.startsWith( "java/" ) || className.startsWith( "javax/" ); } - - private String getMethodKey( String name, String signature ) - { - return name + signature; - } } -- cgit v1.2.3