A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.
fix(x11): consume the zlib flush tail at the end of each ZRLE rect
ExTv91e85dd8fd4a
diff --git a/app/src/main/java/com/excp/podroid/x11/ZrleDecoder.kt b/app/src/main/java/com/excp/podroid/x11/ZrleDecoder.ktindex 9c2b5ff..22237ba 100644--- a/app/src/main/java/com/excp/podroid/x11/ZrleDecoder.kt+++ b/app/src/main/java/com/excp/podroid/x11/ZrleDecoder.kt@@ -108,6 +108,10 @@ class ZrleDecoder {}ty += 64}+ // The tiles can finish before this rect's compressed bytes are all read: a+ // SYNC_FLUSH block ends with bytes that inflate to nothing (00 00 FF FF).+ // Consume them so the socket stays aligned on the next rect header.+ zi.consumeRest()}private fun decodeTile(zi: ZInput, tx: Int, ty: Int, tw: Int, th: Int, target: IntArray, stride: Int) {@@ -229,12 +233,7 @@ class ZrleDecoder {// Feed the next compressed chunk on demand, bounded by this rect's// remaining budget. Each chunk is fully consumed before its buffer// is reused, and we never read past compLen.- if (inf.needsInput() && remaining > 0) {- val nIn = minOf(remaining, inputScratch.size)- inputStream!!.readFully(inputScratch, 0, nIn)- inf.setInput(inputScratch, 0, nIn)- remaining -= nIn- }+ if (inf.needsInput() && remaining > 0) feedChunk()val n = inf.inflate(buf)if (n > 0) { pos = 0; avail = n }// n == 0 with needsInput means all input was consumed; if finished() is false@@ -246,6 +245,22 @@ class ZrleDecoder {}}+ private fun feedChunk() {+ val nIn = minOf(remaining, inputScratch.size)+ inputStream!!.readFully(inputScratch, 0, nIn)+ inf.setInput(inputScratch, 0, nIn)+ remaining -= nIn+ }++ /** Reads and inflates this rect's unread compressed bytes, which must yield no pixels. */+ fun consumeRest() {+ while (remaining > 0) {+ if (inf.needsInput()) feedChunk()+ if (inf.inflate(buf) > 0) throw IOException("ZRLE: rect data longer than its tiles")+ if (!inf.needsInput()) throw IOException("ZRLE: inflater made no progress")+ }+ }+fun readByte(): Int {fill()return buf[pos++].toInt().also { avail-- } and 0xFFdiff --git a/app/src/test/java/com/excp/podroid/x11/ZrleDecoderTest.kt b/app/src/test/java/com/excp/podroid/x11/ZrleDecoderTest.ktindex 49cf281..9ec9ffe 100644--- a/app/src/test/java/com/excp/podroid/x11/ZrleDecoderTest.kt+++ b/app/src/test/java/com/excp/podroid/x11/ZrleDecoderTest.kt@@ -94,6 +94,76 @@ class ZrleDecoderTest {org.junit.Assert.assertArrayEquals(src, target)}+ /**+ * Real Xvnc output: every rect is one SYNC_FLUSH segment of a single session-long+ * zlib stream, so each block ends with a few bytes (the empty stored block,+ * 00 00 FF FF) that produce no output. When those bytes are the only thing left+ * for the last 4096-byte input chunk, the tiles finish before that chunk is read,+ * and the unread tail is then parsed as the next rect header.+ */+ @Test fun `rect whose last input chunk is only the sync flush tail leaves stream aligned`() {+ // One SYNC_FLUSH segment of [def]'s stream, with the 4-byte length prefix.+ // The output buffer is large enough for one deflate() call, so each rect gets+ // exactly one flush, as a server does.+ fun syncRect(def: Deflater, plain: ByteArray): ByteArray {+ def.setInput(plain)+ val out = java.io.ByteArrayOutputStream(); val buf = ByteArray(65536)+ while (true) {+ val n = def.deflate(buf, 0, buf.size, Deflater.SYNC_FLUSH)+ out.write(buf, 0, n)+ if (n < buf.size) break+ }+ val z = out.toByteArray()+ return java.nio.ByteBuffer.allocate(4).putInt(z.size).array() + z+ }+ // One 64x64 plain-RLE tile: `singles` one-pixel runs of pseudo-random colors,+ // then one run covering the rest. Varying `singles` moves the compressed+ // length a few bytes at a time.+ var seed = 0x2468ACE1+ val colors = IntArray(64 * 64) {+ seed = seed xor (seed shl 13); seed = seed xor (seed ushr 17); seed = seed xor (seed shl 5)+ (0xFF shl 24) or (seed and 0xFFFFFF)+ }+ fun rleTile(singles: Int): ByteArray {+ val plain = java.io.ByteArrayOutputStream()+ plain.write(128) // plain RLE+ for (i in 0 until singles) { plain.write(cpixel(colors[i])); plain.write(0) }+ plain.write(cpixel(colors[singles]))+ var rest = 64 * 64 - singles - 1 // run length minus one+ while (rest >= 255) { plain.write(255); rest -= 255 }+ plain.write(rest)+ return plain.toByteArray()+ }+ // Find a tile whose compressed block is k*4096 + 4 bytes, so the decoder's+ // last input chunk holds only the 4-byte stored-block tail. Probing uses a+ // throwaway Deflater with the same settings (first segment of a stream).+ var singles = -1+ for (n in 1 until 64 * 64) {+ val probe = Deflater(Deflater.DEFAULT_COMPRESSION, /*nowrap=*/false)+ val len = syncRect(probe, rleTile(n)).size - 4+ probe.end()+ if (len > 4096 && len % 4096 == 4) { singles = n; break }+ }+ assertTrue("no tile gives compLen % 4096 == 4", singles > 0)+ val expected1 = IntArray(64 * 64) { colors[minOf(it, singles)] }++ val def = Deflater(Deflater.DEFAULT_COMPRESSION, /*nowrap=*/false)+ val rect1 = syncRect(def, rleTile(singles))+ assertEquals(4, java.nio.ByteBuffer.wrap(rect1, 0, 4).int % 4096)+ val green = 0xFF00FF00.toInt()+ val rect2 = syncRect(def, byteArrayOf(1) + cpixel(green))++ val din = DataInputStream(ByteArrayInputStream(rect1 + rect2))+ val dec = ZrleDecoder()+ val t1 = IntArray(64 * 64)+ dec.decode(din, 0, 0, 64, 64, t1, 64)+ org.junit.Assert.assertArrayEquals(expected1, t1)+ val t2 = IntArray(4)+ dec.decode(din, 0, 0, 2, 2, t2, 2)+ assertEquals(green, t2[0]); assertEquals(green, t2[3])+ assertEquals(0, din.available())+ }+/** High: a packed-palette tile whose index exceeds the palette size → IOException, not AIOOBE. */@Test(expected = java.io.IOException::class)fun `packed palette index out of range throws IOException`() {