diff options
| author | 2014-08-30 11:41:17 -0400 | |
|---|---|---|
| committer | 2014-08-30 11:41:17 -0400 | |
| commit | e43fac9f55cfeebacd869352bfb090b7d8d063c1 (patch) | |
| tree | dd4b01daa04dbdcecc765c7270e18bdf1b63d97f /src/cuchaz/enigma/analysis/JarClassIterator.java | |
| parent | working on version conversion (diff) | |
| download | enigma-fork-e43fac9f55cfeebacd869352bfb090b7d8d063c1.tar.gz enigma-fork-e43fac9f55cfeebacd869352bfb090b7d8d063c1.tar.xz enigma-fork-e43fac9f55cfeebacd869352bfb090b7d8d063c1.zip | |
got a decent class matcher working
Diffstat (limited to 'src/cuchaz/enigma/analysis/JarClassIterator.java')
| -rw-r--r-- | src/cuchaz/enigma/analysis/JarClassIterator.java | 72 |
1 files changed, 45 insertions, 27 deletions
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<CtClass> | |||
| 67 | JarEntry entry = m_iter.next(); | 67 | JarEntry entry = m_iter.next(); |
| 68 | try | 68 | try |
| 69 | { | 69 | { |
| 70 | // read the class into a buffer | 70 | return getClass( m_jar, entry ); |
| 71 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
| 72 | byte[] buf = new byte[Constants.KiB]; | ||
| 73 | int totalNumBytesRead = 0; | ||
| 74 | InputStream in = m_jar.getInputStream( entry ); | ||
| 75 | while( in.available() > 0 ) | ||
| 76 | { | ||
| 77 | int numBytesRead = in.read( buf ); | ||
| 78 | if( numBytesRead < 0 ) | ||
| 79 | { | ||
| 80 | break; | ||
| 81 | } | ||
| 82 | bos.write( buf, 0, numBytesRead ); | ||
| 83 | |||
| 84 | // sanity checking | ||
| 85 | totalNumBytesRead += numBytesRead; | ||
| 86 | if( totalNumBytesRead > Constants.MiB ) | ||
| 87 | { | ||
| 88 | throw new Error( "Class file " + entry.getName() + " larger than 1 MiB! Something is wrong!" ); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | // get a javassist handle for the class | ||
| 93 | String className = Descriptor.toJavaName( getClassEntry( entry ).getName() ); | ||
| 94 | ClassPool classPool = new ClassPool(); | ||
| 95 | classPool.insertClassPath( new ByteArrayClassPath( className, bos.toByteArray() ) ); | ||
| 96 | return classPool.get( className ); | ||
| 97 | } | 71 | } |
| 98 | catch( IOException | NotFoundException ex ) | 72 | catch( IOException | NotFoundException ex ) |
| 99 | { | 73 | { |
| @@ -136,6 +110,50 @@ public class JarClassIterator implements Iterator<CtClass> | |||
| 136 | }; | 110 | }; |
| 137 | } | 111 | } |
| 138 | 112 | ||
| 113 | public static CtClass getClass( JarFile jar, ClassEntry classEntry ) | ||
| 114 | { | ||
| 115 | try | ||
| 116 | { | ||
| 117 | return getClass( jar, new JarEntry( classEntry.getName() + ".class" ) ); | ||
| 118 | } | ||
| 119 | catch( IOException | NotFoundException ex ) | ||
| 120 | { | ||
| 121 | throw new Error( "Unable to load class: " + classEntry.getName() ); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | private static CtClass getClass( JarFile jar, JarEntry entry ) | ||
| 126 | throws IOException, NotFoundException | ||
| 127 | { | ||
| 128 | // read the class into a buffer | ||
| 129 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
| 130 | byte[] buf = new byte[Constants.KiB]; | ||
| 131 | int totalNumBytesRead = 0; | ||
| 132 | InputStream in = jar.getInputStream( entry ); | ||
| 133 | while( in.available() > 0 ) | ||
| 134 | { | ||
| 135 | int numBytesRead = in.read( buf ); | ||
| 136 | if( numBytesRead < 0 ) | ||
| 137 | { | ||
| 138 | break; | ||
| 139 | } | ||
| 140 | bos.write( buf, 0, numBytesRead ); | ||
| 141 | |||
| 142 | // sanity checking | ||
| 143 | totalNumBytesRead += numBytesRead; | ||
| 144 | if( totalNumBytesRead > Constants.MiB ) | ||
| 145 | { | ||
| 146 | throw new Error( "Class file " + entry.getName() + " larger than 1 MiB! Something is wrong!" ); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | // get a javassist handle for the class | ||
| 151 | String className = Descriptor.toJavaName( getClassEntry( entry ).getName() ); | ||
| 152 | ClassPool classPool = new ClassPool(); | ||
| 153 | classPool.insertClassPath( new ByteArrayClassPath( className, bos.toByteArray() ) ); | ||
| 154 | return classPool.get( className ); | ||
| 155 | } | ||
| 156 | |||
| 139 | private static ClassEntry getClassEntry( JarEntry entry ) | 157 | private static ClassEntry getClassEntry( JarEntry entry ) |
| 140 | { | 158 | { |
| 141 | return new ClassEntry( entry.getName().substring( 0, entry.getName().length() - ".class".length() ) ); | 159 | return new ClassEntry( entry.getName().substring( 0, entry.getName().length() - ".class".length() ) ); |