From 2975a47d0f682a92da7b3ed455f5078298b0cbaa Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sun, 12 Jun 2022 00:52:14 +0200 Subject: [PATCH] fix: `ZipAligner` not correctly calculating the file offset --- .../align/{Aligner.kt => ZipAligner.kt} | 5 ++--- .../signing/align/stream/MultiOutputStream.kt | 19 +++++++------------ .../signing/align/stream/PeekingFakeStream.kt | 12 +++--------- 3 files changed, 12 insertions(+), 24 deletions(-) rename src/main/kotlin/app/revanced/utils/signing/align/{Aligner.kt => ZipAligner.kt} (89%) diff --git a/src/main/kotlin/app/revanced/utils/signing/align/Aligner.kt b/src/main/kotlin/app/revanced/utils/signing/align/ZipAligner.kt similarity index 89% rename from src/main/kotlin/app/revanced/utils/signing/align/Aligner.kt rename to src/main/kotlin/app/revanced/utils/signing/align/ZipAligner.kt index d7ab747f..2c441e16 100644 --- a/src/main/kotlin/app/revanced/utils/signing/align/Aligner.kt +++ b/src/main/kotlin/app/revanced/utils/signing/align/ZipAligner.kt @@ -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.PeekingFakeStream +import java.io.BufferedOutputStream import java.io.File import java.util.* import java.util.zip.ZipEntry @@ -18,7 +19,7 @@ internal object ZipAligner { val peekingFakeStream = PeekingFakeStream() val fakeOutputStream = ZipOutputStream(peekingFakeStream) // real - val zipOutputStream = ZipOutputStream(output.outputStream()) + val zipOutputStream = ZipOutputStream(BufferedOutputStream(output.outputStream())) val multiOutputStream = MultiOutputStream( listOf( @@ -40,8 +41,6 @@ internal object ZipAligner { val newOffset = fileOffset + bias 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 entry.extra = if (entry.extra == null) ByteArray(padding) else Arrays.copyOf(entry.extra, entry.extra.size + padding) diff --git a/src/main/kotlin/app/revanced/utils/signing/align/stream/MultiOutputStream.kt b/src/main/kotlin/app/revanced/utils/signing/align/stream/MultiOutputStream.kt index 02f8307c..1ab80a95 100644 --- a/src/main/kotlin/app/revanced/utils/signing/align/stream/MultiOutputStream.kt +++ b/src/main/kotlin/app/revanced/utils/signing/align/stream/MultiOutputStream.kt @@ -5,21 +5,16 @@ import java.io.OutputStream internal class MultiOutputStream( private val streams: Iterable, ) : OutputStream() { - override fun write(b: ByteArray, off: Int, len: Int) { - streams.forEach { - it.write(b, off, len) - } + override fun write(b: ByteArray, off: Int, len: Int) = streams.forEach { + it.write(b, off, len) } - override fun write(b: ByteArray) { - streams.forEach { - it.write(b) - } + override fun write(b: ByteArray) = streams.forEach { + it.write(b) } - override fun write(b: Int) { - streams.forEach { - it.write(b) - } + override fun write(b: Int) = streams.forEach { + it.write(b) } + } \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/utils/signing/align/stream/PeekingFakeStream.kt b/src/main/kotlin/app/revanced/utils/signing/align/stream/PeekingFakeStream.kt index 9f4893d4..06ee49e7 100644 --- a/src/main/kotlin/app/revanced/utils/signing/align/stream/PeekingFakeStream.kt +++ b/src/main/kotlin/app/revanced/utils/signing/align/stream/PeekingFakeStream.kt @@ -2,16 +2,10 @@ package app.revanced.utils.signing.align.stream import java.io.OutputStream -internal class PeekingFakeStream : OutputStream() { +internal class PeekingFakeStream : OutputStream() { private var numberOfBytes: Long = 0 - fun seek(n: Long) { - numberOfBytes += n - } - - fun peek(): Long { - return numberOfBytes - } + fun peek() = numberOfBytes override fun write(b: Int) { numberOfBytes++ @@ -22,6 +16,6 @@ internal class PeekingFakeStream : OutputStream() { } override fun write(b: ByteArray, offset: Int, len: Int) { - numberOfBytes += len - offset + numberOfBytes += len } } \ No newline at end of file