A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.
feat(x11): fall back to Raw when a ZRLE session hits a decode error
ExTv04540ba43d9e
diff --git a/app/src/main/java/com/excp/podroid/ui/screens/x11/X11ViewModel.kt b/app/src/main/java/com/excp/podroid/ui/screens/x11/X11ViewModel.ktindex d782cf1..0ec0ef9 100644--- a/app/src/main/java/com/excp/podroid/ui/screens/x11/X11ViewModel.kt+++ b/app/src/main/java/com/excp/podroid/ui/screens/x11/X11ViewModel.kt@@ -14,6 +14,7 @@ import com.excp.podroid.engine.VmEngineimport com.excp.podroid.engine.VmStateimport com.excp.podroid.x11.AudioStreamerimport com.excp.podroid.x11.DamageTracker+import com.excp.podroid.x11.EncodingPolicyimport com.excp.podroid.x11.ResolutionModeimport com.excp.podroid.x11.ResolutionPolicyimport com.excp.podroid.x11.ResolutionPreset@@ -113,8 +114,9 @@ class X11ViewModel @Inject constructor(private var debugUpdates = 0Lprivate var debugCopyNanos = 0Lprivate var debugRxNanos = 0L- // Effective encoding for the current session (set once at connect() from- // x11-debug.conf, debug builds only); read cross-thread by the renderer.+ // Effective encoding for the current session (set at each session start from+ // x11-debug.conf and the Raw fallback, debug builds only); read cross-thread+ // by the renderer.@Volatile private var debugEncodingName = "raw"// Effective present mode for the current session (used by Task F3);// parsed here so the control file's format is settled before that lands.@@ -131,6 +133,9 @@ class X11ViewModel @Inject constructor(private var sessionJob: Job? = null@Volatile private var rfbOut: OutputStream? = null@Volatile private var rfbSocket: Socket? = null+ // Set once a ZRLE session fails with a protocol/decode error; every later+ // session in this ViewModel's lifetime advertises Raw only.+ @Volatile private var zrleDisabled = false// All post-handshake RFB output (pointer, key, SetDesktopSize, and the// recurring FramebufferUpdateRequest from the read loop) flows through this@@ -154,122 +159,144 @@ class X11ViewModel @Inject constructor(if (sessionJob?.isActive == true) return_connection.value = X11ConnectionState.ConnectingsessionJob = viewModelScope.launch(Dispatchers.IO) {- val sock = Socket()- try {- rfbSocket = sock- sock.connect(InetSocketAddress("127.0.0.1", X11Constants.VNC_PORT), 2000)- val rawInp = sock.getInputStream()- // Debug-only (BuildConfig.DEBUG): counts bytes read off the RFB- // socket for the X11Stats log; never wrapped in a release build.- val inp: InputStream = if (BuildConfig.DEBUG) {- CountingInputStream(rawInp) { n -> synchronized(debugStatsLock) { debugBytes += n } }- } else rawInp- val out = sock.getOutputStream()- rfbOut = out- // Each RFB session is a fresh zlib stream; reset the ZRLE inflater- // before the read loop so a reconnect doesn't feed a finished/leftover- // inflater (which yields corrupt output or DataFormatException).- zrle.reset()-- // Debug-only (BuildConfig.DEBUG): x11-debug.conf lets a developer- // flip the advertised encoding list (and, for Task F3, the present- // mode) without a rebuild. Release builds never read this file and- // always advertise VncClient.DEFAULT_ENCODINGS.- val encodings = if (BuildConfig.DEBUG) {- val cfg = X11DebugConfig.read(context)- debugEncodingName = cfg.encodingName- debugPresentHw = cfg.presentHw- Log.d("X11Stats", "debug-config encoding=${cfg.encodingName} present=${if (cfg.presentHw) "hw" else "sw"}")- cfg.encodings- } else {- VncClient.DEFAULT_ENCODINGS- }-- VncClient.handshake(inp, out)- VncClient.negotiatePixelFormat(out, encodings)- if (desiredW > 0) VncClient.requestDesktopSize(out, screenId, desiredW, desiredH)- VncClient.requestFramebufferUpdate(out, w = fbW, h = fbH, incremental = false)- _connection.value = X11ConnectionState.Connected- audio.start(viewModelScope)- var rxT0 = 0L- while (isActive) {- val upd = VncClient.readFramebufferUpdate(- inp, scratch, fbW, zrle,- onMessageStart = if (BuildConfig.DEBUG) { { rxT0 = System.nanoTime() } } else null,- )- if (BuildConfig.DEBUG) {- val rxElapsed = System.nanoTime() - rxT0- synchronized(debugStatsLock) { debugRxNanos += rxElapsed }- }- val ns = upd.newSize- if (ns != null && (ns.w != fbW || ns.h != fbH)) {- fbW = ns.w; fbH = ns.h- val fresh = IntArray(fbW * fbH)- // Swap the framebuffer and mark the whole new frame dirty in- // the same critical section, so a recomposition between resize- // and the next full frame can't blit stale damage rects (or a- // stale bounding box) against the new size.- synchronized(fbLock) { framebuffer = fresh; damageTracker.invalidateAll(fbW, fbH) }- scratch = IntArray(fbW * fbH)- _fbSize.value = ns- cursor.value = android.graphics.Point(fbW / 2, fbH / 2)- // Route through the serialized writer so this full-update- // request can't byte-interleave with a concurrent input- // write. Capture the just-resized dimensions explicitly.- val rw = fbW; val rh = fbH- submitRfb { VncClient.requestFramebufferUpdate(it, w = rw, h = rh, incremental = false) }- // Skip the rest of this iteration: the old code used- // return@let here, which only exited the let lambda and- // then fell through to overwrite lastDamage with rects- // measured against the OLD geometry (the exact race the- // synchronized block above prevents) and fire a spurious- // incremental request.- continue+ // Runs at most twice: a ZRLE session that fails with a protocol error+ // is torn down as usual and immediately retried once with Raw on a new+ // socket, without reporting Failed.+ var retryWithRaw: Boolean+ do {+ retryWithRaw = false+ var sessionUsedZrle = false+ val sock = Socket()+ try {+ rfbSocket = sock+ sock.connect(InetSocketAddress("127.0.0.1", X11Constants.VNC_PORT), 2000)+ val rawInp = sock.getInputStream()+ // Debug-only (BuildConfig.DEBUG): counts bytes read off the RFB+ // socket for the X11Stats log; never wrapped in a release build.+ val inp: InputStream = if (BuildConfig.DEBUG) {+ CountingInputStream(rawInp) { n -> synchronized(debugStatsLock) { debugBytes += n } }+ } else rawInp+ val out = sock.getOutputStream()+ rfbOut = out+ // Each RFB session is a fresh zlib stream; reset the ZRLE inflater+ // before the read loop so a reconnect doesn't feed a finished/leftover+ // inflater (which yields corrupt output or DataFormatException).+ zrle.reset()++ // Debug-only (BuildConfig.DEBUG): x11-debug.conf lets a developer+ // request ZRLE (and, for Task F3, the present mode) without a+ // rebuild. Release builds never read this file and never want ZRLE,+ // so they always advertise VncClient.DEFAULT_ENCODINGS.+ val wantZrle = if (BuildConfig.DEBUG) {+ val cfg = X11DebugConfig.read(context)+ debugPresentHw = cfg.presentHw+ Log.d("X11Stats", "debug-config encoding=${cfg.encodingName} present=${if (cfg.presentHw) "hw" else "sw"}")+ cfg.wantZrle+ } else {+ false}- val debugT0 = if (BuildConfig.DEBUG) System.nanoTime() else 0L- synchronized(fbLock) {- // scratch stays the authoritative full image (CopyRect reads- // from it), so only the damaged row-ranges need copying into- // the framebuffer the renderer reads.- for (r in upd.damage) {- for (row in 0 until r.h) {- val base = (r.y + row) * fbW + r.x- System.arraycopy(scratch, base, framebuffer, base, r.w)+ val encodings = EncodingPolicy.encodingsFor(wantZrle, zrleDisabled)+ val usesZrle = encodings === VncClient.ZRLE_ENCODINGS+ if (BuildConfig.DEBUG) debugEncodingName = if (usesZrle) "zrle" else "raw"++ VncClient.handshake(inp, out)+ VncClient.negotiatePixelFormat(out, encodings)+ sessionUsedZrle = usesZrle+ if (desiredW > 0) VncClient.requestDesktopSize(out, screenId, desiredW, desiredH)+ VncClient.requestFramebufferUpdate(out, w = fbW, h = fbH, incremental = false)+ _connection.value = X11ConnectionState.Connected+ audio.start(viewModelScope)+ var rxT0 = 0L+ while (isActive) {+ val upd = VncClient.readFramebufferUpdate(+ inp, scratch, fbW, zrle,+ onMessageStart = if (BuildConfig.DEBUG) { { rxT0 = System.nanoTime() } } else null,+ )+ if (BuildConfig.DEBUG) {+ val rxElapsed = System.nanoTime() - rxT0+ synchronized(debugStatsLock) { debugRxNanos += rxElapsed }+ }+ val ns = upd.newSize+ if (ns != null && (ns.w != fbW || ns.h != fbH)) {+ fbW = ns.w; fbH = ns.h+ val fresh = IntArray(fbW * fbH)+ // Swap the framebuffer and mark the whole new frame dirty in+ // the same critical section, so a recomposition between resize+ // and the next full frame can't blit stale damage rects (or a+ // stale bounding box) against the new size.+ synchronized(fbLock) { framebuffer = fresh; damageTracker.invalidateAll(fbW, fbH) }+ scratch = IntArray(fbW * fbH)+ _fbSize.value = ns+ cursor.value = android.graphics.Point(fbW / 2, fbH / 2)+ // Route through the serialized writer so this full-update+ // request can't byte-interleave with a concurrent input+ // write. Capture the just-resized dimensions explicitly.+ val rw = fbW; val rh = fbH+ submitRfb { VncClient.requestFramebufferUpdate(it, w = rw, h = rh, incremental = false) }+ // Skip the rest of this iteration: the old code used+ // return@let here, which only exited the let lambda and+ // then fell through to overwrite lastDamage with rects+ // measured against the OLD geometry (the exact race the+ // synchronized block above prevents) and fire a spurious+ // incremental request.+ continue+ }+ val debugT0 = if (BuildConfig.DEBUG) System.nanoTime() else 0L+ synchronized(fbLock) {+ // scratch stays the authoritative full image (CopyRect reads+ // from it), so only the damaged row-ranges need copying into+ // the framebuffer the renderer reads.+ for (r in upd.damage) {+ for (row in 0 until r.h) {+ val base = (r.y + row) * fbW + r.x+ System.arraycopy(scratch, base, framebuffer, base, r.w)+ }}+ damageTracker.add(upd.damage)}- damageTracker.add(upd.damage)+ if (BuildConfig.DEBUG) {+ val elapsed = System.nanoTime() - debugT0+ synchronized(debugStatsLock) { debugUpdates++; debugCopyNanos += elapsed }+ }+ onFrame?.invoke()+ // Same serialized path as input writes: queued FIFO behind any+ // in-flight pointer/key message rather than colliding with it on+ // the socket. Cadence is unchanged (one request per frame); the+ // next read() blocks until this flushes and the server responds.+ submitRfb { VncClient.requestFramebufferUpdate(it, w = fbW, h = fbH, incremental = true) }}- if (BuildConfig.DEBUG) {- val elapsed = System.nanoTime() - debugT0- synchronized(debugStatsLock) { debugUpdates++; debugCopyNanos += elapsed }+ } catch (e: Exception) {+ // A user-initiated disconnect() cancels this job and closes the+ // socket, which surfaces here as a SocketException; that is not a+ // failure, so only report Failed when the job is still active (a+ // genuine read/connect error). Cancellation flips isActive false+ // before the close lands, so the finally falls through to+ // Disconnected instead.+ if (isActive && EncodingPolicy.shouldFallBack(e, sessionUsedZrle)) {+ zrleDisabled = true+ retryWithRaw = true+ Log.w("X11ViewModel", "ZRLE session failed with a protocol error, reconnecting with Raw: ${e.message}")+ } else if (isActive) {+ _connection.value = X11ConnectionState.Failed(e.message ?: "unknown")+ }+ } finally {+ rfbOut = null+ rfbSocket = null+ // Close the socket on the serialized writer, queued AFTER any pending+ // RFB writes (e.g. the button-up from disconnect()), so a teardown+ // can't tear the socket down before a final message has flushed.+ viewModelScope.launch(rfbDispatcher) { runCatching { sock.close() } }+ audio.stop()+ if (retryWithRaw) {+ _connection.value = X11ConnectionState.Connecting+ } else if (_connection.value !is X11ConnectionState.Failed) {+ _connection.value = X11ConnectionState.Disconnected}- onFrame?.invoke()- // Same serialized path as input writes: queued FIFO behind any- // in-flight pointer/key message rather than colliding with it on- // the socket. Cadence is unchanged (one request per frame); the- // next read() blocks until this flushes and the server responds.- submitRfb { VncClient.requestFramebufferUpdate(it, w = fbW, h = fbH, incremental = true) }- }- } catch (e: Exception) {- // A user-initiated disconnect() cancels this job and closes the- // socket, which surfaces here as a SocketException; that is not a- // failure, so only report Failed when the job is still active (a- // genuine read/connect error). Cancellation flips isActive false- // before the close lands, so the finally falls through to- // Disconnected instead.- if (isActive) _connection.value = X11ConnectionState.Failed(e.message ?: "unknown")- } finally {- rfbOut = null- rfbSocket = null- // Close the socket on the serialized writer, queued AFTER any pending- // RFB writes (e.g. the button-up from disconnect()), so a teardown- // can't tear the socket down before a final message has flushed.- viewModelScope.launch(rfbDispatcher) { runCatching { sock.close() } }- audio.stop()- if (_connection.value !is X11ConnectionState.Failed) {- _connection.value = X11ConnectionState.Disconnected}- }+ } while (retryWithRaw && isActive)+ // A disconnect() that lands between the fallback decision and the retry+ // cancels the job before the new session starts; settle on Disconnected.+ if (retryWithRaw) _connection.value = X11ConnectionState.Disconnected}}@@ -421,7 +448,7 @@ class X11ViewModel @Inject constructor(* default for that key.*/private object X11DebugConfig {- class Config(val encodings: IntArray, val encodingName: String, val presentHw: Boolean)+ class Config(val wantZrle: Boolean, val encodingName: String, val presentHw: Boolean)fun read(context: Context): Config {var encodingName = "raw"@@ -442,7 +469,7 @@ private object X11DebugConfig {}val zrle = encodingName == "zrle"return Config(- encodings = if (zrle) VncClient.ZRLE_ENCODINGS else VncClient.DEFAULT_ENCODINGS,+ wantZrle = zrle,encodingName = if (zrle) "zrle" else "raw",presentHw = presentName == "hw",)diff --git a/app/src/main/java/com/excp/podroid/x11/VncClient.kt b/app/src/main/java/com/excp/podroid/x11/VncClient.ktindex b10020f..8eaa339 100644--- a/app/src/main/java/com/excp/podroid/x11/VncClient.kt+++ b/app/src/main/java/com/excp/podroid/x11/VncClient.kt@@ -13,6 +13,24 @@ import java.io.OutputStreamdata class VncServerInfo(val width: Int, val height: Int, val name: String)+/**+ * The server sent data this client cannot parse or decode (bad handshake field,+ * unexpected message type, out-of-bounds rect, unsupported encoding, corrupt+ * ZRLE/zlib data). Transport failures (EOF, socket errors, timeouts) are never+ * this type, so a normal disconnect can be told apart from a decode fault.+ */+class RfbProtocolException(message: String, cause: Throwable? = null) : java.io.IOException(message, cause)++/** Pure encoding choice and ZRLE-to-Raw fallback decision for an RFB session. */+object EncodingPolicy {+ fun encodingsFor(wantZrle: Boolean, zrleDisabled: Boolean): IntArray =+ if (wantZrle && !zrleDisabled) VncClient.ZRLE_ENCODINGS else VncClient.DEFAULT_ENCODINGS++ /** True only when the session advertised ZRLE and a protocol error is in [error]'s cause chain. */+ fun shouldFallBack(error: Throwable, sessionUsedZrle: Boolean): Boolean =+ sessionUsedZrle && generateSequence(error) { it.cause }.any { it is RfbProtocolException }+}+object VncClient {private const val PROTOCOL_VERSION = "RFB 003.008\n"private const val SEC_TYPE_NONE: Byte = 1@@ -23,14 +41,14 @@ object VncClient {* (the only ServerInit fields we need for v1 — pixel format is fixed* 32-bit BGRA via SetPixelFormat sent later by the caller).*- * Throws IOException on protocol mismatch.+ * Throws RfbProtocolException on protocol mismatch.*/fun handshake(inp: InputStream, out: OutputStream): VncServerInfo {val din = DataInputStream(inp)// 1. Read 12-byte version "RFB xxx.yyy\n"val serverVersion = ByteArray(12).also { din.readFully(it) }- require(serverVersion[0] == 'R'.code.toByte()) { "not RFB greeting" }+ if (serverVersion[0] != 'R'.code.toByte()) throw RfbProtocolException("not RFB greeting")// 2. Send our version (always 003.008)out.write(PROTOCOL_VERSION.toByteArray())@@ -38,9 +56,9 @@ object VncClient {// 3. Read security types. 0 => failure (not handled here)val numTypes = din.readUnsignedByte()- require(numTypes > 0) { "server reported zero security types" }+ if (numTypes <= 0) throw RfbProtocolException("server reported zero security types")val types = ByteArray(numTypes).also { din.readFully(it) }- require(types.any { it == SEC_TYPE_NONE }) { "server has no None auth" }+ if (types.none { it == SEC_TYPE_NONE }) throw RfbProtocolException("server has no None auth")// 4. Choose Noneout.write(byteArrayOf(SEC_TYPE_NONE))@@ -48,7 +66,7 @@ object VncClient {// 5. Read SecurityResult (4 bytes; 0 = OK)val secResult = din.readInt()- require(secResult == 0) { "security result $secResult" }+ if (secResult != 0) throw RfbProtocolException("security result $secResult")// 6. Send ClientInit (1 byte: shared = 1)out.write(byteArrayOf(1))@@ -59,7 +77,7 @@ object VncClient {val h = din.readUnsignedShort()skipFully(din, 16) // pixel format we'll overrideval nameLen = din.readInt()- require(nameLen in 0..(1 shl 20)) { "RFB name length $nameLen" }+ if (nameLen !in 0..(1 shl 20)) throw RfbProtocolException("RFB name length $nameLen")val name = ByteArray(nameLen).also { din.readFully(it) }.toString(Charsets.UTF_8)return VncServerInfo(w, h, name)@@ -88,10 +106,10 @@ object VncClient {* No-op for in-range rects.*/private fun requireInBounds(x: Int, y: Int, w: Int, h: Int, stride: Int, size: Int) {- if (stride <= 0) throw java.io.IOException("RFB: zero or negative stride $stride")+ if (stride <= 0) throw RfbProtocolException("RFB: zero or negative stride $stride")val rows = size / strideif (x < 0 || y < 0 || w < 0 || h < 0 || x + w > stride || y + h > rows)- throw java.io.IOException("RFB rect out of bounds: x=$x y=$y w=$w h=$h stride=$stride size=$size")+ throw RfbProtocolException("RFB rect out of bounds: x=$x y=$y w=$w h=$h stride=$stride size=$size")}private const val MSG_FRAMEBUFFER_UPDATE: Int = 0@@ -181,8 +199,8 @@ object VncClient {MSG_FRAMEBUFFER_UPDATE -> { onMessageStart?.invoke(); break }1 -> { skipFully(din, 1); din.readUnsignedShort(); val n = din.readUnsignedShort(); skipFully(din, n * 6) }2 -> { }- 3 -> { skipFully(din, 3); val len = din.readInt(); if (len in 0..(1 shl 20)) skipFully(din, len) else throw java.io.IOException("ServerCutText absurd length=$len") }- else -> throw java.io.IOException("unexpected RFB server msg type $msgType")+ 3 -> { skipFully(din, 3); val len = din.readInt(); if (len in 0..(1 shl 20)) skipFully(din, len) else throw RfbProtocolException("ServerCutText absurd length=$len") }+ else -> throw RfbProtocolException("unexpected RFB server msg type $msgType")}}skipFully(din, 1)@@ -199,7 +217,7 @@ object VncClient {ENC_EXTENDED_DESKTOP_SIZE -> { // -308: w/h are the new fb dimsval screens = din.readUnsignedByte(); skipFully(din, 3)skipFully(din, screens * 16) // we use a single-screen model; dims come from w/h- if (w <= 0 || h <= 0) throw java.io.IOException("RFB ExtendedDesktopSize: degenerate geometry w=$w h=$h")+ if (w <= 0 || h <= 0) throw RfbProtocolException("RFB ExtendedDesktopSize: degenerate geometry w=$w h=$h")newSize = VncSize(w, h)}ENC_RAW -> {@@ -235,7 +253,7 @@ object VncClient {zrle.decode(din, x, y, w, h, targetArgb, stride)damage.add(VncRect(x, y, w, h))}- else -> throw java.io.IOException("unsupported encoding $enc")+ else -> throw RfbProtocolException("unsupported encoding $enc")}}return RfbUpdate(newSize, damage)diff --git a/app/src/main/java/com/excp/podroid/x11/ZrleDecoder.kt b/app/src/main/java/com/excp/podroid/x11/ZrleDecoder.ktindex 22237ba..643c28a 100644--- a/app/src/main/java/com/excp/podroid/x11/ZrleDecoder.kt+++ b/app/src/main/java/com/excp/podroid/x11/ZrleDecoder.kt@@ -28,7 +28,7 @@package com.excp.podroid.x11import java.io.DataInputStream-import java.io.IOException+import java.util.zip.DataFormatExceptionimport java.util.zip.Inflaterclass ZrleDecoder {@@ -82,7 +82,7 @@ class ZrleDecoder {fun decode(din: DataInputStream, x: Int, y: Int, w: Int, h: Int, target: IntArray, stride: Int) {// Read and decompress the rect's zlib block.val compLen = din.readInt()- if (compLen < 0 || compLen > 64 * 1024 * 1024) throw IOException("ZRLE: absurd compressed length $compLen")+ if (compLen < 0 || compLen > 64 * 1024 * 1024) throw RfbProtocolException("ZRLE: absurd compressed length $compLen")// Stream the compressed block on demand. setInput() does not copy/append —// it holds inputScratch by reference and is consumed lazily by inflate(),@@ -96,22 +96,27 @@ class ZrleDecoder {// Wrap the inflater so tile-level code just calls readByte()/readBytes().val zi = ZInput(inflater)- // Tile loop: 64x64 tiles in row-major order.- var ty = 0- while (ty < h) {- val th = minOf(64, h - ty)- var tx = 0- while (tx < w) {- val tw = minOf(64, w - tx)- decodeTile(zi, x + tx, y + ty, tw, th, target, stride)- tx += 64+ try {+ // Tile loop: 64x64 tiles in row-major order.+ var ty = 0+ while (ty < h) {+ val th = minOf(64, h - ty)+ var tx = 0+ while (tx < w) {+ val tw = minOf(64, w - tx)+ decodeTile(zi, x + tx, y + ty, tw, th, target, stride)+ tx += 64+ }+ ty += 64}- 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()+ } catch (e: DataFormatException) {+ // Corrupt compressed data is a decode fault, not a transport failure.+ throw RfbProtocolException("ZRLE: corrupt zlib data: ${e.message}", e)}- // 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) {@@ -159,7 +164,7 @@ class ZrleDecoder {bitsInAccum -= bitsPerIndex// bitsPerIndex rounds up, so the index space can exceed n// when n is not a power of two.- if (idx >= n) throw IOException("ZRLE: packed palette index $idx >= $n")+ if (idx >= n) throw RfbProtocolException("ZRLE: packed palette index $idx >= $n")target[base + col] = palette[idx]col++}@@ -174,7 +179,7 @@ class ZrleDecoder {while (filled < total) {val color = zi.readCpixel()val runLen = zi.readRunLength()- if (filled + runLen > total) throw IOException("ZRLE: plain RLE run overruns tile ($filled+$runLen > $total)")+ if (filled + runLen > total) throw RfbProtocolException("ZRLE: plain RLE run overruns tile ($filled+$runLen > $total)")repeat(runLen) {val pos = filled + itval row = pos / tw; val col = pos % tw@@ -193,7 +198,7 @@ class ZrleDecoder {val indexByte = zi.readByte()if (indexByte and 0x80 == 0) {// Single pixel.- if (indexByte >= n) throw IOException("ZRLE: palette RLE index $indexByte >= $n")+ if (indexByte >= n) throw RfbProtocolException("ZRLE: palette RLE index $indexByte >= $n")val pos = filledval row = pos / tw; val col = pos % twtarget[(ty + row) * stride + (tx + col)] = palette[indexByte]@@ -201,10 +206,10 @@ class ZrleDecoder {} else {// Run of palette[index & 0x7F].val idx = indexByte and 0x7F- if (idx >= n) throw IOException("ZRLE: palette RLE index $idx >= $n")+ if (idx >= n) throw RfbProtocolException("ZRLE: palette RLE index $idx >= $n")val color = palette[idx]val runLen = zi.readRunLength()- if (filled + runLen > total) throw IOException("ZRLE: palette RLE run overruns tile ($filled+$runLen > $total)")+ if (filled + runLen > total) throw RfbProtocolException("ZRLE: palette RLE run overruns tile ($filled+$runLen > $total)")repeat(runLen) {val pos = filled + itval row = pos / tw; val col = pos % tw@@ -214,7 +219,7 @@ class ZrleDecoder {}}}- else -> throw IOException("ZRLE: unsupported subencoding $subenc")+ else -> throw RfbProtocolException("ZRLE: unsupported subencoding $subenc")}}@@ -229,7 +234,7 @@ class ZrleDecoder {private fun fill() {while (avail == 0) {- if (inf.finished()) throw IOException("ZRLE: inflater finished early")+ if (inf.finished()) throw RfbProtocolException("ZRLE: inflater finished early")// 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.@@ -238,10 +243,10 @@ class ZrleDecoder {if (n > 0) { pos = 0; avail = n }// n == 0 with needsInput means all input was consumed; if finished() is false// but no output and needsInput, the caller overfed or the stream is malformed.- else if (inf.needsInput()) throw IOException("ZRLE: inflater needs more input but none queued")+ else if (inf.needsInput()) throw RfbProtocolException("ZRLE: inflater needs more input but none queued")// n == 0, not finished, not needsInput → needsDictionary or a stuck// stream: bail rather than spin forever.- else throw IOException("ZRLE: inflater made no progress")+ else throw RfbProtocolException("ZRLE: inflater made no progress")}}@@ -256,8 +261,8 @@ class ZrleDecoder {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")+ if (inf.inflate(buf) > 0) throw RfbProtocolException("ZRLE: rect data longer than its tiles")+ if (!inf.needsInput()) throw RfbProtocolException("ZRLE: inflater made no progress")}}@@ -290,7 +295,7 @@ class ZrleDecoder {while (true) {val b = readByte()total += b- if (total > MAX_TILE_PIXELS) throw IOException("ZRLE: run length $total exceeds max tile size")+ if (total > MAX_TILE_PIXELS) throw RfbProtocolException("ZRLE: run length $total exceeds max tile size")if (b != 0xFF) break}return total + 1diff --git a/app/src/test/java/com/excp/podroid/x11/VncClientTest.kt b/app/src/test/java/com/excp/podroid/x11/VncClientTest.ktindex 4d5c3b5..d95ffa1 100644--- a/app/src/test/java/com/excp/podroid/x11/VncClientTest.kt+++ b/app/src/test/java/com/excp/podroid/x11/VncClientTest.kt@@ -6,6 +6,9 @@ package com.excp.podroid.x11import org.junit.Assert.assertEqualsimport org.junit.Assert.assertArrayEquals+import org.junit.Assert.assertFalse+import org.junit.Assert.assertThrows+import org.junit.Assert.assertTrueimport org.junit.Testimport java.io.ByteArrayInputStreamimport java.io.ByteArrayOutputStream@@ -220,6 +223,81 @@ class VncClientTest {VncClient.readFramebufferUpdate(java.io.ByteArrayInputStream(bos.toByteArray()), target, 1280, ZrleDecoder())}+ @Test fun `unsupported encoding is an RfbProtocolException`() {+ val bos = java.io.ByteArrayOutputStream()+ val d = java.io.DataOutputStream(bos)+ d.writeByte(0); d.writeByte(0); d.writeShort(1) // msg, pad, numRects+ d.writeShort(0); d.writeShort(0); d.writeShort(1); d.writeShort(1) // x=0,y=0,w=1,h=1+ d.writeInt(0x7FFFFFFF) // unknown encoding+ assertThrows(RfbProtocolException::class.java) {+ VncClient.readFramebufferUpdate(java.io.ByteArrayInputStream(bos.toByteArray()), IntArray(4), 2, ZrleDecoder())+ }+ }++ @Test fun `ZRLE rect with corrupt zlib data is an RfbProtocolException wrapping DataFormatException`() {+ // Valid zlib header (78 9C), then a deflate block header with BTYPE=11+ // (reserved), which the Inflater rejects with DataFormatException.+ val bos = java.io.ByteArrayOutputStream()+ val d = java.io.DataOutputStream(bos)+ d.writeByte(0); d.writeByte(0); d.writeShort(1) // msg, pad, numRects+ d.writeShort(0); d.writeShort(0); d.writeShort(2); d.writeShort(2) // x=0,y=0,w=2,h=2+ d.writeInt(16) // encoding = ZRLE+ d.writeInt(4)+ d.write(byteArrayOf(0x78, 0x9C.toByte(), 0xFF.toByte(), 0xFF.toByte()))+ val ex = assertThrows(RfbProtocolException::class.java) {+ VncClient.readFramebufferUpdate(java.io.ByteArrayInputStream(bos.toByteArray()), IntArray(4), 2, ZrleDecoder())+ }+ assertTrue("cause was ${ex.cause}", ex.cause is java.util.zip.DataFormatException)+ }++ @Test fun `stream truncated mid-rect is an EOFException, not an RfbProtocolException`() {+ // ZRLE rect announces 100 compressed bytes but the stream ends after 2.+ val zrle = java.io.ByteArrayOutputStream()+ java.io.DataOutputStream(zrle).apply {+ writeByte(0); writeByte(0); writeShort(1)+ writeShort(0); writeShort(0); writeShort(2); writeShort(2); writeInt(16)+ writeInt(100); write(byteArrayOf(0x78, 0x9C.toByte()))+ }+ val zrleEx = assertThrows(java.io.IOException::class.java) {+ VncClient.readFramebufferUpdate(java.io.ByteArrayInputStream(zrle.toByteArray()), IntArray(4), 2, ZrleDecoder())+ }+ assertTrue("was $zrleEx", zrleEx is java.io.EOFException)+ assertFalse(zrleEx is RfbProtocolException)++ // Raw rect 2x1 with only one of its two pixels on the wire.+ val raw = java.io.ByteArrayOutputStream()+ java.io.DataOutputStream(raw).apply {+ writeByte(0); writeByte(0); writeShort(1)+ writeShort(0); writeShort(0); writeShort(2); writeShort(1); writeInt(0)+ write(byteArrayOf(0, 0, 0, 0))+ }+ val rawEx = assertThrows(java.io.IOException::class.java) {+ VncClient.readFramebufferUpdate(java.io.ByteArrayInputStream(raw.toByteArray()), IntArray(2), 2, ZrleDecoder())+ }+ assertTrue("was $rawEx", rawEx is java.io.EOFException)+ assertFalse(rawEx is RfbProtocolException)+ }++ @Test fun `EncodingPolicy advertises ZRLE only when wanted and not disabled`() {+ assertArrayEquals(VncClient.ZRLE_ENCODINGS, EncodingPolicy.encodingsFor(wantZrle = true, zrleDisabled = false))+ assertArrayEquals(VncClient.DEFAULT_ENCODINGS, EncodingPolicy.encodingsFor(wantZrle = true, zrleDisabled = true))+ assertArrayEquals(VncClient.DEFAULT_ENCODINGS, EncodingPolicy.encodingsFor(wantZrle = false, zrleDisabled = false))+ assertArrayEquals(VncClient.DEFAULT_ENCODINGS, EncodingPolicy.encodingsFor(wantZrle = false, zrleDisabled = true))+ }++ @Test fun `EncodingPolicy falls back only for a protocol error in a ZRLE session`() {+ val protocol = RfbProtocolException("ZRLE: bad")+ val wrapped = java.io.IOException("outer", RuntimeException("mid", protocol))+ assertTrue(EncodingPolicy.shouldFallBack(protocol, sessionUsedZrle = true))+ assertTrue(EncodingPolicy.shouldFallBack(wrapped, sessionUsedZrle = true))+ assertFalse(EncodingPolicy.shouldFallBack(protocol, sessionUsedZrle = false))+ assertFalse(EncodingPolicy.shouldFallBack(wrapped, sessionUsedZrle = false))+ assertFalse(EncodingPolicy.shouldFallBack(java.io.IOException("reset"), sessionUsedZrle = true))+ assertFalse(EncodingPolicy.shouldFallBack(java.io.EOFException(), sessionUsedZrle = true))+ assertFalse(EncodingPolicy.shouldFallBack(java.net.SocketException("closed"), sessionUsedZrle = true))+ assertFalse(EncodingPolicy.shouldFallBack(java.net.SocketTimeoutException(), sessionUsedZrle = true))+ }+@Test fun `negotiatePixelFormat with default encodings is byte-identical to the old hardcoded SetEncodings`() {val out = java.io.ByteArrayOutputStream()VncClient.negotiatePixelFormat(out)diff --git a/app/src/test/java/com/excp/podroid/x11/ZrleDecoderTest.kt b/app/src/test/java/com/excp/podroid/x11/ZrleDecoderTest.ktindex 9ec9ffe..10898e3 100644--- a/app/src/test/java/com/excp/podroid/x11/ZrleDecoderTest.kt+++ b/app/src/test/java/com/excp/podroid/x11/ZrleDecoderTest.kt@@ -164,6 +164,74 @@ class ZrleDecoderTest {assertEquals(0, din.available())}+ // Subencoding 2 (1 bit per index) does not occur in the Xvnc captures under+ // resources/x11, so it is covered here. Width 10 leaves 6 padding bits per row.+ @Test fun `two colour packed palette tile decodes with row padding`() {+ val a = 0xFF102030.toInt(); val b = 0xFFA0B0C0.toInt()+ val w = 10; val h = 2+ val rows = arrayOf(+ intArrayOf(0, 1, 1, 0, 0, 0, 0, 0, 1, 1),+ intArrayOf(1, 0, 0, 0, 0, 0, 0, 0, 0, 1),+ )+ val plain = java.io.ByteArrayOutputStream()+ plain.write(2) // packed palette, 2 entries+ plain.write(cpixel(a)); plain.write(cpixel(b))+ for (r in rows) {+ var hi = 0; var lo = 0+ for (i in 0 until 8) hi = hi or (r[i] shl (7 - i))+ for (i in 8 until w) lo = lo or (r[i] shl (15 - i))+ plain.write(hi); plain.write(lo)+ }+ val target = IntArray(w * h)+ ZrleDecoder().decode(DataInputStream(ByteArrayInputStream(zrleRect(plain.toByteArray()))), 0, 0, w, h, target, w)+ val expected = IntArray(w * h) { if (rows[it / w][it % w] == 1) b else a }+ org.junit.Assert.assertArrayEquals(expected, target)+ }++ // Subencoding 128 (plain RLE) does not occur in the Xvnc captures either. Runs+ // cross row boundaries and use the multi-byte (0xFF continuation) run length.+ @Test fun `plain RLE tile with runs crossing rows decodes`() {+ val a = 0xFF0000FF.toInt(); val b = 0xFF00FF00.toInt(); val c = 0xFFFF0000.toInt()+ val w = 64; val h = 64+ val plain = java.io.ByteArrayOutputStream()+ plain.write(128)+ plain.write(cpixel(a)); plain.write(0) // run 1+ plain.write(cpixel(b)); plain.write(0xFF); plain.write(44) // run 255 + 44 + 1 = 300+ plain.write(cpixel(c)) // rest: 4096 - 301 = 3795+ var rest = w * h - 301 - 1+ while (rest >= 255) { plain.write(0xFF); rest -= 255 }+ plain.write(rest)+ val target = IntArray(w * h)+ ZrleDecoder().decode(DataInputStream(ByteArrayInputStream(zrleRect(plain.toByteArray()))), 0, 0, w, h, target, w)+ val expected = IntArray(w * h) { if (it == 0) a else if (it <= 300) b else c }+ org.junit.Assert.assertArrayEquals(expected, target)+ }++ @Test fun `corrupt zlib data is an RfbProtocolException wrapping DataFormatException`() {+ // Valid zlib header, then a deflate block with the reserved BTYPE=11.+ val rect = java.nio.ByteBuffer.allocate(4).putInt(4).array() ++ byteArrayOf(0x78, 0x9C.toByte(), 0xFF.toByte(), 0xFF.toByte())+ val ex = assertThrows(RfbProtocolException::class.java) {+ ZrleDecoder().decode(DataInputStream(ByteArrayInputStream(rect)), 0, 0, 2, 2, IntArray(4), 2)+ }+ assertTrue("cause was ${ex.cause}", ex.cause is java.util.zip.DataFormatException)+ }++ @Test fun `ZRLE content errors are RfbProtocolException`() {+ val plain = java.io.ByteArrayOutputStream()+ plain.write(128) // plain RLE, 2x2 tile, one run of 5 overruns it+ plain.write(cpixel(0xFFAABBCC.toInt()))+ plain.write(0x04)+ val din = DataInputStream(ByteArrayInputStream(zrleRect(plain.toByteArray())))+ assertThrows(RfbProtocolException::class.java) {+ ZrleDecoder().decode(din, 0, 0, 2, 2, IntArray(4), 2)+ }+ val unsupported = DataInputStream(ByteArrayInputStream(zrleRect(byteArrayOf(17))))+ assertThrows(RfbProtocolException::class.java) {+ ZrleDecoder().decode(unsupported, 0, 0, 1, 1, IntArray(1), 1)+ }+ }+/** 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`() {