Skip to content

Commit

Permalink
fix: give ClassWriter a ClassReader for symtable
Browse files Browse the repository at this point in the history
removed SafeClassWriter as it was unused
  • Loading branch information
Sculas committed Mar 23, 2022
1 parent cb4ee20 commit e8f6973
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 150 deletions.
19 changes: 10 additions & 9 deletions src/main/kotlin/app/revanced/patcher/util/Io.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package app.revanced.patcher.util

import app.revanced.patcher.writer.SafeClassWriter
import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.tree.ClassNode
Expand Down Expand Up @@ -50,21 +49,21 @@ internal class Io(
fun saveAsJar() {
val jis = ZipInputStream(bufferedInputStream)
val jos = ZipOutputStream(output)
val classReaders = mutableMapOf<String, ClassReader>()

// first write all non .class zip entries from the original input stream to the output stream
// we read it first to close the input stream as fast as possible
// TODO(oSumAtrIX): There is currently no way to remove non .class files.
lateinit var zipEntry: ZipEntry
while (jis.nextEntry.also { if (it != null) zipEntry = it } != null) {
// skip all class files because we added them in the loop above
// TODO(oSumAtrIX): Check for zipEntry.isDirectory
if (zipEntry.name.endsWith(".class")) continue
if (zipEntry.name.endsWith(".class")) {
classReaders[zipEntry.name] = ClassReader(jis.readBytes())
continue
}

// create a new zipEntry and write the contents of the zipEntry to the output stream
// create a new zipEntry and write the contents of the zipEntry to the output stream and close it
jos.putNextEntry(ZipEntry(zipEntry))
jos.write(jis.readBytes())

// close the newly created zipEntry
jos.closeEntry()
}

Expand All @@ -76,10 +75,12 @@ internal class Io(
// now write all the patched classes to the output stream
for (patchedClass in classes) {
// create a new entry of the patched class
jos.putNextEntry(JarEntry(patchedClass.name + ".class"))
val name = patchedClass.name + ".class"
jos.putNextEntry(JarEntry(name))

// parse the patched class to a byte array and write it to the output stream
val cw: ClassWriter = SafeClassWriter(
val cw = ClassWriter(
classReaders[name]!!,
ClassWriter.COMPUTE_FRAMES or ClassWriter.COMPUTE_MAXS
)
patchedClass.accept(cw)
Expand Down
140 changes: 0 additions & 140 deletions src/main/kotlin/app/revanced/patcher/writer/SafeClassWriter.kt

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/kotlin/app/revanced/patcher/ReaderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ internal class ReaderTest {
@Test
fun `read jar containing multiple classes`() {
val testData = PatcherTest::class.java.getResourceAsStream("/test2.jar")!!
Patcher(testData, ByteArrayOutputStream(), PatcherTest.testSignatures) // reusing test sigs from PatcherTest
Patcher(testData, ByteArrayOutputStream(), PatcherTest.testSignatures).save() // reusing test sigs from PatcherTest
}
}

0 comments on commit e8f6973

Please sign in to comment.