summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/analysis
diff options
context:
space:
mode:
authorGravatar jeff2014-08-30 11:41:17 -0400
committerGravatar jeff2014-08-30 11:41:17 -0400
commite43fac9f55cfeebacd869352bfb090b7d8d063c1 (patch)
treedd4b01daa04dbdcecc765c7270e18bdf1b63d97f /src/cuchaz/enigma/analysis
parentworking on version conversion (diff)
downloadenigma-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')
-rw-r--r--src/cuchaz/enigma/analysis/JarClassIterator.java72
-rw-r--r--src/cuchaz/enigma/analysis/JarIndex.java34
2 files changed, 64 insertions, 42 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() ) );
diff --git a/src/cuchaz/enigma/analysis/JarIndex.java b/src/cuchaz/enigma/analysis/JarIndex.java
index deacf16..b479b69 100644
--- a/src/cuchaz/enigma/analysis/JarIndex.java
+++ b/src/cuchaz/enigma/analysis/JarIndex.java
@@ -95,7 +95,7 @@ public class JarIndex
95 // step 2: index method/field access 95 // step 2: index method/field access
96 for( CtClass c : JarClassIterator.classes( jar ) ) 96 for( CtClass c : JarClassIterator.classes( jar ) )
97 { 97 {
98 fixClass( c ); 98 ClassRenamer.moveAllClassesOutOfDefaultPackage( c, Constants.NonePackage );
99 ClassEntry classEntry = new ClassEntry( Descriptor.toJvmName( c.getName() ) ); 99 ClassEntry classEntry = new ClassEntry( Descriptor.toJvmName( c.getName() ) );
100 for( CtField field : c.getDeclaredFields() ) 100 for( CtField field : c.getDeclaredFields() )
101 { 101 {
@@ -112,7 +112,7 @@ public class JarIndex
112 // step 3: index the types, methods 112 // step 3: index the types, methods
113 for( CtClass c : JarClassIterator.classes( jar ) ) 113 for( CtClass c : JarClassIterator.classes( jar ) )
114 { 114 {
115 fixClass( c ); 115 ClassRenamer.moveAllClassesOutOfDefaultPackage( c, Constants.NonePackage );
116 String className = Descriptor.toJvmName( c.getName() ); 116 String className = Descriptor.toJvmName( c.getName() );
117 m_ancestries.addSuperclass( className, Descriptor.toJvmName( c.getClassFile().getSuperclass() ) ); 117 m_ancestries.addSuperclass( className, Descriptor.toJvmName( c.getClassFile().getSuperclass() ) );
118 for( String interfaceName : c.getClassFile().getInterfaces() ) 118 for( String interfaceName : c.getClassFile().getInterfaces() )
@@ -128,8 +128,7 @@ public class JarIndex
128 // step 4: index inner classes and anonymous classes 128 // step 4: index inner classes and anonymous classes
129 for( CtClass c : JarClassIterator.classes( jar ) ) 129 for( CtClass c : JarClassIterator.classes( jar ) )
130 { 130 {
131 fixClass( c ); 131 ClassRenamer.moveAllClassesOutOfDefaultPackage( c, Constants.NonePackage );
132
133 String outerClassName = findOuterClass( c ); 132 String outerClassName = findOuterClass( c );
134 if( outerClassName != null ) 133 if( outerClassName != null )
135 { 134 {
@@ -164,17 +163,6 @@ public class JarIndex
164 renameMethods( m_bridgeMethods ); 163 renameMethods( m_bridgeMethods );
165 } 164 }
166 165
167 private void fixClass( CtClass c )
168 {
169 ClassEntry classEntry = new ClassEntry( Descriptor.toJvmName( c.getName() ) );
170 if( classEntry.isInDefaultPackage() )
171 {
172 // move class out of default package
173 classEntry = new ClassEntry( Constants.NonePackage + "/" + classEntry.getName() );
174 ClassRenamer.moveAllClassesOutOfDefaultPackage( c, Constants.NonePackage );
175 }
176 }
177
178 private void indexBehavior( CtBehavior behavior ) 166 private void indexBehavior( CtBehavior behavior )
179 { 167 {
180 // get the method entry 168 // get the method entry
@@ -729,6 +717,22 @@ public class JarIndex
729 717
730 private void renameClasses( Map<String,String> renames ) 718 private void renameClasses( Map<String,String> renames )
731 { 719 {
720 // rename class entries
721 Set<ClassEntry> obfClassEntries = Sets.newHashSet();
722 for( ClassEntry classEntry : m_obfClassEntries )
723 {
724 if( renames.containsKey( classEntry.getName() ) )
725 {
726 obfClassEntries.add( new ClassEntry( renames.get( classEntry.getName() ) ) );
727 }
728 else
729 {
730 obfClassEntries.add( classEntry );
731 }
732 }
733 m_obfClassEntries = obfClassEntries;
734
735 // rename others
732 m_ancestries.renameClasses( renames ); 736 m_ancestries.renameClasses( renames );
733 renameClassesInMultimap( renames, m_methodImplementations ); 737 renameClassesInMultimap( renames, m_methodImplementations );
734 renameClassesInMultimap( renames, m_behaviorReferences ); 738 renameClassesInMultimap( renames, m_behaviorReferences );