From e43fac9f55cfeebacd869352bfb090b7d8d063c1 Mon Sep 17 00:00:00 2001 From: jeff Date: Sat, 30 Aug 2014 11:41:17 -0400 Subject: got a decent class matcher working --- src/cuchaz/enigma/analysis/JarClassIterator.java | 72 +++++++++++++++--------- 1 file changed, 45 insertions(+), 27 deletions(-) (limited to 'src/cuchaz/enigma/analysis/JarClassIterator.java') diff --git a/src/cuchaz/enigma/analysis/JarClassIterator.java b/src/cuchaz/enigma/analysis/JarClassIterator.java index 6c9f124..10ae805 100644 --- a/src/cuchaz/enigma/analysis/JarClassIterator.java +++ b/src/cuchaz/enigma/analysis/JarClassIterator.java @@ -67,33 +67,7 @@ public class JarClassIterator implements Iterator JarEntry entry = m_iter.next(); try { - // read the class into a buffer - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - byte[] buf = new byte[Constants.KiB]; - int totalNumBytesRead = 0; - InputStream in = m_jar.getInputStream( entry ); - while( in.available() > 0 ) - { - int numBytesRead = in.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!" ); - } - } - - // get a javassist handle for the class - String className = Descriptor.toJavaName( getClassEntry( entry ).getName() ); - ClassPool classPool = new ClassPool(); - classPool.insertClassPath( new ByteArrayClassPath( className, bos.toByteArray() ) ); - return classPool.get( className ); + return getClass( m_jar, entry ); } catch( IOException | NotFoundException ex ) { @@ -136,6 +110,50 @@ public class JarClassIterator implements Iterator }; } + public static CtClass getClass( JarFile jar, ClassEntry classEntry ) + { + try + { + return getClass( jar, new JarEntry( classEntry.getName() + ".class" ) ); + } + catch( IOException | NotFoundException ex ) + { + throw new Error( "Unable to load class: " + classEntry.getName() ); + } + } + + private static CtClass getClass( JarFile jar, JarEntry entry ) + throws IOException, NotFoundException + { + // read the class into a buffer + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + byte[] buf = new byte[Constants.KiB]; + int totalNumBytesRead = 0; + InputStream in = jar.getInputStream( entry ); + while( in.available() > 0 ) + { + int numBytesRead = in.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!" ); + } + } + + // get a javassist handle for the class + String className = Descriptor.toJavaName( getClassEntry( entry ).getName() ); + ClassPool classPool = new ClassPool(); + classPool.insertClassPath( new ByteArrayClassPath( className, bos.toByteArray() ) ); + return classPool.get( className ); + } + private static ClassEntry getClassEntry( JarEntry entry ) { return new ClassEntry( entry.getName().substring( 0, entry.getName().length() - ".class".length() ) ); -- cgit v1.2.3