Skip to content

Commit

Permalink
Use snapshot common compress to avoid reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
yujincheng08 committed Sep 15, 2024
1 parent 953facb commit b3c9c9d
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 46 deletions.
14 changes: 0 additions & 14 deletions app/apk/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,6 @@
int mActivityHandlesConfigFlags;
}

-keepclassmembernames class org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream {
private org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream$CurrentEntry entry;
private void closeCopiedEntry(boolean);
private final org.apache.commons.compress.archivers.zip.StreamCompressor streamCompressor;
}

-keepclassmembernames class org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream$CurrentEntry {
private boolean hasWritten;
}

-keepclassmembernames class org.apache.commons.compress.archivers.zip.StreamCompressor {
void reset();
}

# main
-keep,allowoptimization public class com.topjohnwu.magisk.signing.SignBoot {
public static void main(java.lang.String[]);
Expand Down
5 changes: 4 additions & 1 deletion app/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ dependencies {
api(libs.timber)
api(libs.markwon.core)
implementation(libs.bcpkix)
implementation(libs.commons.compress)
implementation(libs.commons.io)
implementation(libs.commons.codec)
implementation(libs.commons.lang)
implementation(files("libs/commons-compress-1.27.2-SNAPSHOT.jar"))

api(libs.libsu.core)
api(libs.libsu.service)
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import com.topjohnwu.magisk.core.isRunningAsStub
import com.topjohnwu.magisk.core.ktx.cachedFile
import com.topjohnwu.magisk.core.ktx.copyAll
import com.topjohnwu.magisk.core.ktx.copyAndClose
import com.topjohnwu.magisk.core.ktx.reflectField
import com.topjohnwu.magisk.core.ktx.reflectMethod
import com.topjohnwu.magisk.core.ktx.set
import com.topjohnwu.magisk.core.ktx.withStreams
import com.topjohnwu.magisk.core.ktx.writeTo
Expand Down Expand Up @@ -324,35 +322,17 @@ class DownloadEngine(
zout.putArchiveEntry(ZipArchiveEntry("META-INF/com/google/android/updater-script"))
zout.write("#MAGISK\n".toByteArray())

val entryField = zout.javaClass.reflectField("entry")
val hasWrittenField = entryField.type.reflectField("hasWritten")
val closeEntryMethod = zout.javaClass.reflectMethod("closeCopiedEntry", Boolean::class.java)
val streamCompressorField = zout.javaClass.reflectField("streamCompressor")
val resetMethod = streamCompressorField.type.reflectMethod("reset")

for (entry in zin) {
val path = entry.name
if (path.isNotEmpty() && !path.startsWith("META-INF")) {
val method = entry.method
val size = entry.size
// Force STORED method to avoid decompression for better performance
if (entry.method != ZipEntry.STORED) {
if (!entry.isDirectory) {
// Force STORED method to avoid decompression for better performance
val archiveEntry = ZipArchiveEntry(entry)
entry.method = ZipEntry.STORED
entry.size = entry.compressedSize
}
val archiveEntry = ZipArchiveEntry(path)
archiveEntry.method = method
archiveEntry.crc = entry.crc
zout.putArchiveEntry(archiveEntry)
if (!entry.isDirectory) {
archiveEntry.method = ZipEntry.STORED
zin.copyAll(zout)
hasWrittenField.set(entryField.get(zout), true)
archiveEntry.compressedSize = entry.compressedSize
archiveEntry.size = size
archiveEntry.method = method
closeEntryMethod(zout, false)
resetMethod(streamCompressorField.get(zout))
zout.addRawArchiveEntry(archiveEntry, zin)
} else {
zout.putArchiveEntry(entry)
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions app/core/src/main/java/com/topjohnwu/magisk/core/ktx/XJVM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import java.lang.reflect.Field
import java.lang.reflect.Method
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Collections
Expand Down Expand Up @@ -87,9 +86,6 @@ fun <K, V> MutableMap<K, V>.synchronized(): MutableMap<K, V> = Collections.synch
fun Class<*>.reflectField(name: String): Field =
getDeclaredField(name).apply { isAccessible = true }

fun Class<*>.reflectMethod(name: String, vararg types: Class<*>): Method =
getDeclaredMethod(name, *types).apply { isAccessible = true }

inline fun <T, R> Flow<T>.concurrentMap(crossinline transform: suspend (T) -> R): Flow<R> {
return flatMapMerge { value ->
flow { emit(transform(value)) }
Expand Down
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ room = "2.6.1"

[libraries]
bcpkix = { module = "org.bouncycastle:bcpkix-jdk18on", version = "1.78.1" }
commons-compress = { module = "org.apache.commons:commons-compress", version = "1.27.1" }
commons-io = { module = "commons-io:commons-io", version = "2.16.1" }
commons-codec = { module = "commons-codec:commons-codec", version = "1.17.1" }
commons-lang = { module = "org.apache.commons:commons-lang3", version = "3.17.0" }
retrofit = { module = "com.squareup.retrofit2:retrofit", version.ref = "retrofit" }
retrofit-moshi = { module = "com.squareup.retrofit2:converter-moshi", version.ref = "retrofit" }
retrofit-scalars = { module = "com.squareup.retrofit2:converter-scalars", version.ref = "retrofit" }
Expand Down

0 comments on commit b3c9c9d

Please sign in to comment.