fix: ZipAligner not correctly calculating the file offset

This commit is contained in:
oSumAtrIX 2022-06-12 00:52:14 +02:00
parent 0d7581ad75
commit 2975a47d0f
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
3 changed files with 12 additions and 24 deletions

View File

@ -2,6 +2,7 @@ package app.revanced.utils.signing.align
import app.revanced.utils.signing.align.stream.MultiOutputStream import app.revanced.utils.signing.align.stream.MultiOutputStream
import app.revanced.utils.signing.align.stream.PeekingFakeStream import app.revanced.utils.signing.align.stream.PeekingFakeStream
import java.io.BufferedOutputStream
import java.io.File import java.io.File
import java.util.* import java.util.*
import java.util.zip.ZipEntry import java.util.zip.ZipEntry
@ -18,7 +19,7 @@ internal object ZipAligner {
val peekingFakeStream = PeekingFakeStream() val peekingFakeStream = PeekingFakeStream()
val fakeOutputStream = ZipOutputStream(peekingFakeStream) val fakeOutputStream = ZipOutputStream(peekingFakeStream)
// real // real
val zipOutputStream = ZipOutputStream(output.outputStream()) val zipOutputStream = ZipOutputStream(BufferedOutputStream(output.outputStream()))
val multiOutputStream = MultiOutputStream( val multiOutputStream = MultiOutputStream(
listOf( listOf(
@ -40,8 +41,6 @@ internal object ZipAligner {
val newOffset = fileOffset + bias val newOffset = fileOffset + bias
padding = ((alignment - (newOffset % alignment)) % alignment).toInt() padding = ((alignment - (newOffset % alignment)) % alignment).toInt()
// fake, used to add the padding, because we add it to real as well in the extra field
peekingFakeStream.seek(padding.toLong())
// real // real
entry.extra = if (entry.extra == null) ByteArray(padding) entry.extra = if (entry.extra == null) ByteArray(padding)
else Arrays.copyOf(entry.extra, entry.extra.size + padding) else Arrays.copyOf(entry.extra, entry.extra.size + padding)

View File

@ -5,21 +5,16 @@ import java.io.OutputStream
internal class MultiOutputStream( internal class MultiOutputStream(
private val streams: Iterable<OutputStream>, private val streams: Iterable<OutputStream>,
) : OutputStream() { ) : OutputStream() {
override fun write(b: ByteArray, off: Int, len: Int) { override fun write(b: ByteArray, off: Int, len: Int) = streams.forEach {
streams.forEach { it.write(b, off, len)
it.write(b, off, len)
}
} }
override fun write(b: ByteArray) { override fun write(b: ByteArray) = streams.forEach {
streams.forEach { it.write(b)
it.write(b)
}
} }
override fun write(b: Int) { override fun write(b: Int) = streams.forEach {
streams.forEach { it.write(b)
it.write(b)
}
} }
} }

View File

@ -2,16 +2,10 @@ package app.revanced.utils.signing.align.stream
import java.io.OutputStream import java.io.OutputStream
internal class PeekingFakeStream : OutputStream() { internal class PeekingFakeStream : OutputStream() {
private var numberOfBytes: Long = 0 private var numberOfBytes: Long = 0
fun seek(n: Long) { fun peek() = numberOfBytes
numberOfBytes += n
}
fun peek(): Long {
return numberOfBytes
}
override fun write(b: Int) { override fun write(b: Int) {
numberOfBytes++ numberOfBytes++
@ -22,6 +16,6 @@ internal class PeekingFakeStream : OutputStream() {
} }
override fun write(b: ByteArray, offset: Int, len: Int) { override fun write(b: ByteArray, offset: Int, len: Int) {
numberOfBytes += len - offset numberOfBytes += len
} }
} }