From 3db2927add005cab398e8fbd3799a1893cf711dd Mon Sep 17 00:00:00 2001 From: rob thijssen Date: Wed, 6 May 2026 13:44:24 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20phase=200+1=20=E2=80=94=20workspace=20s?= =?UTF-8?q?caffold=20and=20protocol=20primitives?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Phase 0: Cargo workspace with ericrfb (lib) and ericrfb-proxy (bin) crates, .envrc for RUST_LOG, workspace dependency pins. Phase 1: ericrfb/src/proto.rs implements all wire primitives: - read/write helpers matching h.java (u8, i8, u16-BE, i16-BE, i32-BE) - varint reader/writer matching aw.int() (1-3 bytes, Tight lengths) - modified-UTF-8 string reader matching h.byte() - RectHeader { x: u16, y: u16, w: u16, h: u16, encoding: i32 } - RGB332 → RGBA compile-time LUT (256 entries) 20 tests including proptest varint roundtrip over [0, 2^22). Co-Authored-By: Claude Opus 4.6 (1M context) --- .envrc | 1 + .gitignore | 1 + Cargo.lock | 1207 ++++++++++++++++++++++++++++++ Cargo.toml | 13 + crates/ericrfb-proxy/Cargo.toml | 12 + crates/ericrfb-proxy/src/main.rs | 3 + crates/ericrfb/Cargo.toml | 10 + crates/ericrfb/src/lib.rs | 1 + crates/ericrfb/src/proto.rs | 450 +++++++++++ 9 files changed, 1698 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 crates/ericrfb-proxy/Cargo.toml create mode 100644 crates/ericrfb-proxy/src/main.rs create mode 100644 crates/ericrfb/Cargo.toml create mode 100644 crates/ericrfb/src/lib.rs create mode 100644 crates/ericrfb/src/proto.rs diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..16fb2e9 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +export RUST_LOG=ericrfb=debug,ericrfb_proxy=debug diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..66db399 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,1207 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + +[[package]] +name = "anyhow" +version = "1.0.102" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c" + +[[package]] +name = "async-trait" +version = "0.1.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "axum" +version = "0.7.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edca88bc138befd0323b20752846e6587272d3b03b0343c8ea28a6f819e6e71f" +dependencies = [ + "async-trait", + "axum-core", + "bytes", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "axum-core" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09f2bd6146b97ae3359fa0cc6d6b376d9539582c7b4220f041a33ec24c226199" +dependencies = [ + "async-trait", + "bytes", + "futures-util", + "http", + "http-body", + "http-body-util", + "mime", + "pin-project-lite", + "rustversion", + "sync_wrapper", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "bit-set" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08807e080ed7f9d5433fa9b275196cfc35414f66a0c79d864dc51a0d825231a3" +dependencies = [ + "bit-vec", +] + +[[package]] +name = "bit-vec" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7" + +[[package]] +name = "bitflags" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3" + +[[package]] +name = "bytes" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33" + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "ericrfb" +version = "0.1.0" +dependencies = [ + "proptest", + "thiserror", +] + +[[package]] +name = "ericrfb-proxy" +version = "0.1.0" +dependencies = [ + "anyhow", + "axum", + "ericrfb", + "tokio", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "fastrand" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f1f227452a390804cdb637b74a86990f2a7d7ba4b7d5693aac9b4dd6defd8d6" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures-channel" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" + +[[package]] +name = "futures-task" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393" + +[[package]] +name = "futures-util" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "slab", +] + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "libc", + "r-efi 5.3.0", + "wasip2", +] + +[[package]] +name = "getrandom" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de51e6874e94e7bf76d726fc5d13ba782deca734ff60d5bb2fb2607c7406555" +dependencies = [ + "cfg-if", + "libc", + "r-efi 6.0.0", + "wasip2", + "wasip3", +] + +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "foldhash", +] + +[[package]] +name = "hashbrown" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f467dd6dccf739c208452f8014c75c18bb8301b050ad1cfb27153803edb0f51" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "http" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3ba2a386d7f85a81f119ad7498ebe444d2e22c2af0b86b069416ace48b3311a" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "hyper" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6299f016b246a94207e63da54dbe807655bf9e00044f73ded42c3ac5305fbcca" +dependencies = [ + "atomic-waker", + "bytes", + "futures-channel", + "futures-core", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", +] + +[[package]] +name = "hyper-util" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96547c2556ec9d12fb1578c4eaf448b04993e7fb79cbaad930a656880a6bdfa0" +dependencies = [ + "bytes", + "http", + "http-body", + "hyper", + "pin-project-lite", + "tokio", + "tower-service", +] + +[[package]] +name = "id-arena" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" + +[[package]] +name = "indexmap" +version = "2.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" +dependencies = [ + "equivalent", + "hashbrown 0.17.0", + "serde", + "serde_core", +] + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "leb128fmt" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" + +[[package]] +name = "libc" +version = "0.2.186" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" + +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" + +[[package]] +name = "matchers" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" +dependencies = [ + "regex-automata", +] + +[[package]] +name = "matchit" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94" + +[[package]] +name = "memchr" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "mio" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50b7e5b27aa02a74bac8c3f23f448f8d87ff11f92d3aac1a6ed369ee08cc56c1" +dependencies = [ + "libc", + "wasi", + "windows-sys", +] + +[[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "prettyplease" +version = "0.2.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" +dependencies = [ + "proc-macro2", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "proptest" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b45fcc2344c680f5025fe57779faef368840d0bd1f42f216291f0dc4ace4744" +dependencies = [ + "bit-set", + "bit-vec", + "bitflags", + "num-traits", + "rand", + "rand_chacha", + "rand_xorshift", + "regex-syntax", + "rusty-fork", + "tempfile", + "unarray", +] + +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + +[[package]] +name = "quote" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "rand" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c5af06bb1b7d3216d91932aed5265164bf384dc89cd6ba05cf59a35f5f76ea" +dependencies = [ + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", +] + +[[package]] +name = "rand_xorshift" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "513962919efc330f829edb2535844d1b912b0fbe2ca165d613e4e8788bb05a5a" +dependencies = [ + "rand_core", +] + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags", +] + +[[package]] +name = "regex-automata" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a" + +[[package]] +name = "rustix" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "rusty-fork" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc6bf79ff24e648f6da1f8d1f011e9cac26491b619e6b9280f2b47f1774e6ee2" +dependencies = [ + "fnv", + "quick-error", + "tempfile", + "wait-timeout", +] + +[[package]] +name = "ryu" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "semver" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.149" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "serde_path_to_error" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a9ff822e371bb5403e391ecd83e182e0e77ba7f6fe0160b795797109d1b457" +dependencies = [ + "itoa", + "serde", + "serde_core", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "socket2" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" + +[[package]] +name = "tempfile" +version = "3.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" +dependencies = [ + "fastrand", + "getrandom 0.4.2", + "once_cell", + "rustix", + "windows-sys", +] + +[[package]] +name = "thiserror" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "thread_local" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "tokio" +version = "1.52.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "110a78583f19d5cdb2c5ccf321d1290344e71313c6c37d43520d386027d18386" +dependencies = [ + "bytes", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys", +] + +[[package]] +name = "tokio-macros" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "385a6cb71ab9ab790c5fe8d67f1645e6c450a7ce006a33de03daa956cf70a496" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tower" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper", + "tokio", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "log", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex-automata", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", +] + +[[package]] +name = "unarray" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94" + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + +[[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + +[[package]] +name = "wait-timeout" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ac3b126d3914f9849036f826e054cbabdc8519970b8998ddaf3b5bd3c65f11" +dependencies = [ + "libc", +] + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasip2" +version = "1.0.3+wasi-0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20064672db26d7cdc89c7798c48a0fdfac8213434a1186e5ef29fd560ae223d6" +dependencies = [ + "wit-bindgen 0.57.1", +] + +[[package]] +name = "wasip3" +version = "0.4.0+wasi-0.3.0-rc-2026-01-06" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5428f8bf88ea5ddc08faddef2ac4a67e390b88186c703ce6dbd955e1c145aca5" +dependencies = [ + "wit-bindgen 0.51.0", +] + +[[package]] +name = "wasm-encoder" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "990065f2fe63003fe337b932cfb5e3b80e0b4d0f5ff650e6985b1048f62c8319" +dependencies = [ + "leb128fmt", + "wasmparser", +] + +[[package]] +name = "wasm-metadata" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0e353e6a2fbdc176932bbaab493762eb1255a7900fe0fea1a2f96c296cc909" +dependencies = [ + "anyhow", + "indexmap", + "wasm-encoder", + "wasmparser", +] + +[[package]] +name = "wasmparser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe" +dependencies = [ + "bitflags", + "hashbrown 0.15.5", + "indexmap", + "semver", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "wit-bindgen" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5" +dependencies = [ + "wit-bindgen-rust-macro", +] + +[[package]] +name = "wit-bindgen" +version = "0.57.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + +[[package]] +name = "wit-bindgen-core" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea61de684c3ea68cb082b7a88508a8b27fcc8b797d738bfc99a82facf1d752dc" +dependencies = [ + "anyhow", + "heck", + "wit-parser", +] + +[[package]] +name = "wit-bindgen-rust" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7c566e0f4b284dd6561c786d9cb0142da491f46a9fbed79ea69cdad5db17f21" +dependencies = [ + "anyhow", + "heck", + "indexmap", + "prettyplease", + "syn", + "wasm-metadata", + "wit-bindgen-core", + "wit-component", +] + +[[package]] +name = "wit-bindgen-rust-macro" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c0f9bfd77e6a48eccf51359e3ae77140a7f50b1e2ebfe62422d8afdaffab17a" +dependencies = [ + "anyhow", + "prettyplease", + "proc-macro2", + "quote", + "syn", + "wit-bindgen-core", + "wit-bindgen-rust", +] + +[[package]] +name = "wit-component" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2" +dependencies = [ + "anyhow", + "bitflags", + "indexmap", + "log", + "serde", + "serde_derive", + "serde_json", + "wasm-encoder", + "wasm-metadata", + "wasmparser", + "wit-parser", +] + +[[package]] +name = "wit-parser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc8ac4bc1dc3381b7f59c34f00b67e18f910c2c0f50015669dde7def656a736" +dependencies = [ + "anyhow", + "id-arena", + "indexmap", + "log", + "semver", + "serde", + "serde_derive", + "serde_json", + "unicode-xid", + "wasmparser", +] + +[[package]] +name = "zerocopy" +version = "0.8.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eed437bf9d6692032087e337407a86f04cd8d6a16a37199ed57949d415bd68e9" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70e3cd084b1788766f53af483dd21f93881ff30d7320490ec3ef7526d203bad4" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zmij" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..b2d0bc7 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[workspace] +resolver = "2" +members = ["crates/ericrfb", "crates/ericrfb-proxy"] + +[workspace.dependencies] +tokio = { version = "1", features = ["full"] } +axum = "0.7" +bytes = "1" +flate2 = "1" +tracing = "0.1" +tracing-subscriber = { version = "0.3", features = ["env-filter"] } +anyhow = "1" +thiserror = "2" diff --git a/crates/ericrfb-proxy/Cargo.toml b/crates/ericrfb-proxy/Cargo.toml new file mode 100644 index 0000000..baf3319 --- /dev/null +++ b/crates/ericrfb-proxy/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "ericrfb-proxy" +version = "0.1.0" +edition = "2024" + +[dependencies] +ericrfb = { path = "../ericrfb" } +tokio.workspace = true +axum.workspace = true +tracing.workspace = true +tracing-subscriber.workspace = true +anyhow.workspace = true diff --git a/crates/ericrfb-proxy/src/main.rs b/crates/ericrfb-proxy/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/crates/ericrfb-proxy/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/crates/ericrfb/Cargo.toml b/crates/ericrfb/Cargo.toml new file mode 100644 index 0000000..e772218 --- /dev/null +++ b/crates/ericrfb/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "ericrfb" +version = "0.1.0" +edition = "2024" + +[dependencies] +thiserror.workspace = true + +[dev-dependencies] +proptest = "1" diff --git a/crates/ericrfb/src/lib.rs b/crates/ericrfb/src/lib.rs new file mode 100644 index 0000000..febacec --- /dev/null +++ b/crates/ericrfb/src/lib.rs @@ -0,0 +1 @@ +pub mod proto; diff --git a/crates/ericrfb/src/proto.rs b/crates/ericrfb/src/proto.rs new file mode 100644 index 0000000..cb1076b --- /dev/null +++ b/crates/ericrfb/src/proto.rs @@ -0,0 +1,450 @@ +use std::io::{self, Read, Write}; + +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum ProtoError { + #[error("i/o error: {0}")] + Io(#[from] io::Error), + #[error("unexpected end of stream")] + UnexpectedEof, + #[error("invalid modified-UTF-8: {0}")] + InvalidUtf8(String), +} + +pub type Result = std::result::Result; + +// --------------------------------------------------------------------------- +// Read primitives — mirrors h.java +// --------------------------------------------------------------------------- + +/// Read 1 byte as unsigned (h.new, line 106). +pub fn read_u8(r: &mut impl Read) -> Result { + let mut buf = [0u8; 1]; + r.read_exact(&mut buf)?; + Ok(buf[0]) +} + +/// Read 1 byte as signed (h.try, line 91). +pub fn read_i8(r: &mut impl Read) -> Result { + Ok(read_u8(r)? as i8) +} + +/// Read 2 bytes big-endian as unsigned (h.int, line 138). +pub fn read_u16_be(r: &mut impl Read) -> Result { + let mut buf = [0u8; 2]; + r.read_exact(&mut buf)?; + Ok(u16::from_be_bytes(buf)) +} + +/// Read 2 bytes big-endian as signed (h.char, line 121). +pub fn read_i16_be(r: &mut impl Read) -> Result { + let mut buf = [0u8; 2]; + r.read_exact(&mut buf)?; + Ok(i16::from_be_bytes(buf)) +} + +/// Read 4 bytes big-endian as signed (h.do, line 172). +pub fn read_i32_be(r: &mut impl Read) -> Result { + let mut buf = [0u8; 4]; + r.read_exact(&mut buf)?; + Ok(i32::from_be_bytes(buf)) +} + +/// Read exactly `n` bytes into a new vec. +pub fn read_exact(r: &mut impl Read, n: usize) -> Result> { + let mut buf = vec![0u8; n]; + r.read_exact(&mut buf)?; + Ok(buf) +} + +// --------------------------------------------------------------------------- +// Varint — aw.int(), line 484 +// +// 1–3 byte variable-length integer, top bit = continuation. +// Byte 0: value bits [6:0] +// Byte 1 (if bit 7 of byte 0 set): value bits [13:7] +// Byte 2 (if bit 7 of byte 1 set): value bits [21:14] (all 8 bits used) +// +// Maximum representable value: (0x7F) | (0x7F << 7) | (0xFF << 14) +// = 127 + 16256 + 4177920 = 4194303 = 0x3FFFFF +// --------------------------------------------------------------------------- + +/// Read a 1–3 byte varint (aw.int, line 484). Used for Tight compressed-stream +/// lengths, NOT for rectangle header coords. +pub fn read_varint(r: &mut impl Read) -> Result { + let b0 = read_u8(r)? as u32; + let mut val = b0 & 0x7F; + if b0 & 0x80 != 0 { + let b1 = read_u8(r)? as u32; + val |= (b1 & 0x7F) << 7; + if b1 & 0x80 != 0 { + let b2 = read_u8(r)? as u32; + val |= (b2 & 0xFF) << 14; + } + } + Ok(val) +} + +/// Write a value as a 1–3 byte varint. Panics if `val > 0x3FFFFF`. +pub fn write_varint(w: &mut impl Write, val: u32) -> Result<()> { + assert!(val <= 0x3F_FFFF, "varint overflow: {val}"); + if val < 0x80 { + w.write_all(&[val as u8])?; + } else if val < 0x4000 { + w.write_all(&[(val & 0x7F) as u8 | 0x80, ((val >> 7) & 0x7F) as u8])?; + } else { + w.write_all(&[ + (val & 0x7F) as u8 | 0x80, + ((val >> 7) & 0x7F) as u8 | 0x80, + ((val >> 14) & 0xFF) as u8, + ])?; + } + Ok(()) +} + +// --------------------------------------------------------------------------- +// Modified-UTF-8 string — h.byte(), line 188 +// +// Java's modified-UTF-8: u16 byte-length prefix, then encoded bytes. +// Encoding matches standard UTF-8 for BMP codepoints except: +// - U+0000 is encoded as [0xC0, 0x80] (2 bytes, not 1) +// - Supplementary characters use surrogate pairs +// We accept standard UTF-8 as well for robustness. +// --------------------------------------------------------------------------- + +/// Read a modified-UTF-8 string (h.byte, line 188). +/// Format: u16-BE length prefix + `length` bytes of Java modified-UTF-8. +pub fn read_modified_utf8(r: &mut impl Read) -> Result { + let len = read_u16_be(r)? as usize; + let data = read_exact(r, len)?; + decode_modified_utf8(&data) +} + +fn decode_modified_utf8(data: &[u8]) -> Result { + let mut out = String::with_capacity(data.len()); + let mut i = 0; + while i < data.len() { + let b = data[i]; + match b >> 4 { + // Single byte: 0x01..=0x7F (standard ASCII, but NOT 0x00) + 0..=7 => { + out.push(b as char); + i += 1; + } + // Two-byte sequence: 110xxxxx 10xxxxxx + 12 | 13 => { + if i + 1 >= data.len() { + return Err(ProtoError::InvalidUtf8( + "truncated 2-byte sequence".into(), + )); + } + let b2 = data[i + 1]; + if b2 & 0xC0 != 0x80 { + return Err(ProtoError::InvalidUtf8( + "invalid continuation byte".into(), + )); + } + let cp = ((b as u32 & 0x1F) << 6) | (b2 as u32 & 0x3F); + out.push(char::from_u32(cp).unwrap_or('\u{FFFD}')); + i += 2; + } + // Three-byte sequence: 1110xxxx 10xxxxxx 10xxxxxx + 14 => { + if i + 2 >= data.len() { + return Err(ProtoError::InvalidUtf8( + "truncated 3-byte sequence".into(), + )); + } + let b2 = data[i + 1]; + let b3 = data[i + 2]; + if (b2 & 0xC0 != 0x80) || (b3 & 0xC0 != 0x80) { + return Err(ProtoError::InvalidUtf8( + "invalid continuation byte".into(), + )); + } + let cp = + ((b as u32 & 0x0F) << 12) | ((b2 as u32 & 0x3F) << 6) | (b3 as u32 & 0x3F); + out.push(char::from_u32(cp).unwrap_or('\u{FFFD}')); + i += 3; + } + _ => { + return Err(ProtoError::InvalidUtf8(format!( + "invalid leading byte: 0x{b:02X}" + ))); + } + } + } + Ok(out) +} + +// --------------------------------------------------------------------------- +// Rectangle header — aw.f(), line 468 +// +// 4 × u16-BE coords + 1 × i32-BE encoding = 12 bytes fixed. +// Encoding is i32 because standard RFB uses negative pseudo-encoding IDs +// (e.g. -250). +// --------------------------------------------------------------------------- + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct RectHeader { + pub x: u16, + pub y: u16, + pub w: u16, + pub h: u16, + pub encoding: i32, +} + +impl RectHeader { + pub fn read_from(r: &mut impl Read) -> Result { + Ok(Self { + x: read_u16_be(r)?, + y: read_u16_be(r)?, + w: read_u16_be(r)?, + h: read_u16_be(r)?, + encoding: read_i32_be(r)?, + }) + } +} + +// --------------------------------------------------------------------------- +// Write helpers — for client-to-server messages +// --------------------------------------------------------------------------- + +pub fn write_u8(w: &mut impl Write, v: u8) -> Result<()> { + w.write_all(&[v])?; + Ok(()) +} + +pub fn write_u16_be(w: &mut impl Write, v: u16) -> Result<()> { + w.write_all(&v.to_be_bytes())?; + Ok(()) +} + +pub fn write_i32_be(w: &mut impl Write, v: i32) -> Result<()> { + w.write_all(&v.to_be_bytes())?; + Ok(()) +} + +// --------------------------------------------------------------------------- +// RGB332 lookup table — ByteColorRFBRenderer constructor, lines 57-66 +// +// DirectColorModel(8, 7, 56, 192): +// red mask = 0b_0000_0111 (bits 0-2), shift to 8-bit: v * 255 / 7 +// green mask = 0b_0011_1000 (bits 3-5), shift to 8-bit: v * 255 / 7 +// blue mask = 0b_1100_0000 (bits 6-7), shift to 8-bit: v * 255 / 3 +// --------------------------------------------------------------------------- + +/// Expand an 8bpp RGB332 index to an RGBA pixel (alpha = 0xFF). +pub const fn rgb332_to_rgba(idx: u8) -> [u8; 4] { + let r3 = idx & 0x07; + let g3 = (idx >> 3) & 0x07; + let b2 = (idx >> 6) & 0x03; + [ + (r3 as u16 * 255 / 7) as u8, + (g3 as u16 * 255 / 7) as u8, + (b2 as u16 * 255 / 3) as u8, + 0xFF, + ] +} + +/// Precomputed RGB332 → RGBA lookup table (256 entries, 4 bytes each). +pub const RGB332_LUT: [[u8; 4]; 256] = { + let mut lut = [[0u8; 4]; 256]; + let mut i = 0u16; + while i < 256 { + lut[i as usize] = rgb332_to_rgba(i as u8); + i += 1; + } + lut +}; + +// --------------------------------------------------------------------------- +// Tests +// --------------------------------------------------------------------------- + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Cursor; + + // -- read primitives -- + + #[test] + fn test_read_u8() { + let mut c = Cursor::new([0x42u8]); + assert_eq!(read_u8(&mut c).unwrap(), 0x42); + } + + #[test] + fn test_read_i8() { + let mut c = Cursor::new([0xFEu8]); // -2 as i8 + assert_eq!(read_i8(&mut c).unwrap(), -2); + } + + #[test] + fn test_read_u16_be() { + let mut c = Cursor::new([0x01u8, 0x00]); + assert_eq!(read_u16_be(&mut c).unwrap(), 256); + } + + #[test] + fn test_read_i16_be() { + let mut c = Cursor::new([0xFF, 0xFE]); // -2 as i16 + assert_eq!(read_i16_be(&mut c).unwrap(), -2); + } + + #[test] + fn test_read_i32_be() { + let mut c = Cursor::new([0xFF, 0xFF, 0xFF, 0x06]); // -250 as i32 + assert_eq!(read_i32_be(&mut c).unwrap(), -250); + } + + // -- varint -- + + #[test] + fn test_varint_single_byte() { + // Values 0..=127 are encoded as a single byte + let mut c = Cursor::new([0x00]); + assert_eq!(read_varint(&mut c).unwrap(), 0); + + let mut c = Cursor::new([0x7F]); + assert_eq!(read_varint(&mut c).unwrap(), 127); + } + + #[test] + fn test_varint_two_bytes() { + // 128 = 0x80: byte0 = (128 & 0x7F) | 0x80 = 0x80, byte1 = 128 >> 7 = 1 + let mut c = Cursor::new([0x80, 0x01]); + assert_eq!(read_varint(&mut c).unwrap(), 128); + + // 16383 = 0x3FFF: byte0 = 0xFF, byte1 = 0x7F + let mut c = Cursor::new([0xFF, 0x7F]); + assert_eq!(read_varint(&mut c).unwrap(), 16383); + } + + #[test] + fn test_varint_three_bytes() { + // 16384 = 0x4000: byte0 = 0x80, byte1 = 0x80, byte2 = 0x01 + let mut c = Cursor::new([0x80, 0x80, 0x01]); + assert_eq!(read_varint(&mut c).unwrap(), 16384); + + // max: 0x3FFFFF = 4194303 + let mut c = Cursor::new([0xFF, 0xFF, 0xFF]); + assert_eq!(read_varint(&mut c).unwrap(), 0x3F_FFFF); + } + + #[test] + fn test_varint_roundtrip() { + for val in [0, 1, 127, 128, 255, 16383, 16384, 100_000, 0x3F_FFFF] { + let mut buf = Vec::new(); + write_varint(&mut buf, val).unwrap(); + let mut c = Cursor::new(&buf); + assert_eq!(read_varint(&mut c).unwrap(), val, "roundtrip failed for {val}"); + } + } + + // -- modified UTF-8 -- + + #[test] + fn test_read_modified_utf8_ascii() { + // u16 length = 5, then "hello" + let data = [0x00, 0x05, b'h', b'e', b'l', b'l', b'o']; + let mut c = Cursor::new(&data[..]); + assert_eq!(read_modified_utf8(&mut c).unwrap(), "hello"); + } + + #[test] + fn test_read_modified_utf8_null() { + // Java modified-UTF-8 encodes U+0000 as [0xC0, 0x80] + let data = [0x00, 0x02, 0xC0, 0x80]; + let mut c = Cursor::new(&data[..]); + assert_eq!(read_modified_utf8(&mut c).unwrap(), "\0"); + } + + #[test] + fn test_read_modified_utf8_multibyte() { + // U+00E9 (é) = [0xC3, 0xA9] in UTF-8 + let data = [0x00, 0x02, 0xC3, 0xA9]; + let mut c = Cursor::new(&data[..]); + assert_eq!(read_modified_utf8(&mut c).unwrap(), "é"); + } + + // -- rect header -- + + #[test] + fn test_rect_header() { + // x=10, y=20, w=640, h=480, encoding=7 (Tight) + let mut data = Vec::new(); + data.extend_from_slice(&10u16.to_be_bytes()); + data.extend_from_slice(&20u16.to_be_bytes()); + data.extend_from_slice(&640u16.to_be_bytes()); + data.extend_from_slice(&480u16.to_be_bytes()); + data.extend_from_slice(&7i32.to_be_bytes()); + + let mut c = Cursor::new(&data[..]); + let hdr = RectHeader::read_from(&mut c).unwrap(); + assert_eq!(hdr, RectHeader { x: 10, y: 20, w: 640, h: 480, encoding: 7 }); + } + + #[test] + fn test_rect_header_negative_encoding() { + // encoding = -250 (pseudo-encoding) + let mut data = Vec::new(); + data.extend_from_slice(&0u16.to_be_bytes()); + data.extend_from_slice(&0u16.to_be_bytes()); + data.extend_from_slice(&0u16.to_be_bytes()); + data.extend_from_slice(&0u16.to_be_bytes()); + data.extend_from_slice(&(-250i32).to_be_bytes()); + + let mut c = Cursor::new(&data[..]); + let hdr = RectHeader::read_from(&mut c).unwrap(); + assert_eq!(hdr.encoding, -250); + } + + // -- proptest -- + + use proptest::prelude::*; + + proptest! { + #[test] + fn prop_varint_roundtrip(val in 0u32..=0x3F_FFFF) { + let mut buf = Vec::new(); + write_varint(&mut buf, val).unwrap(); + let mut c = Cursor::new(&buf); + let decoded = read_varint(&mut c).unwrap(); + prop_assert_eq!(decoded, val); + } + } + + // -- RGB332 LUT -- + + #[test] + fn test_rgb332_black() { + assert_eq!(RGB332_LUT[0x00], [0, 0, 0, 255]); + } + + #[test] + fn test_rgb332_white() { + // 0xFF = r=7, g=7, b=3 → [255, 255, 255, 255] + assert_eq!(RGB332_LUT[0xFF], [255, 255, 255, 255]); + } + + #[test] + fn test_rgb332_pure_red() { + // pure red = r=7, g=0, b=0 → 0x07 + assert_eq!(RGB332_LUT[0x07], [255, 0, 0, 255]); + } + + #[test] + fn test_rgb332_pure_green() { + // pure green = r=0, g=7, b=0 → 0x38 + assert_eq!(RGB332_LUT[0x38], [0, 255, 0, 255]); + } + + #[test] + fn test_rgb332_pure_blue() { + // pure blue = r=0, g=0, b=3 → 0xC0 + assert_eq!(RGB332_LUT[0xC0], [0, 0, 255, 255]); + } +}