clipboard: docs/protocol.md §10.7

Answers the two open questions: the bulk connection's 64-byte DATA is raw,
not framed (Clipboard.cs:987, 996-997), and Foundation's .zlib is raw
DEFLATE (verified against Python zlib -15 both directions).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
lucioandClaude Opus 5 committed 2026-09-16 04:51:46 -07:00
1 parent 386c915861
commit 9eb05b522c
1 file changed
+113
+113
View File
@@ -938,3 +938,116 @@ sees them. One subsection per lane, appended, never rewritten.
`package.MachineName` (`:712-777`), which only exists on a big packet, so a
32-byte heartbeat would teach the peer nothing. `PackageType.isBig` already
lists them (§1.2).
### 10.7 clipboard (2026-09-16)
1. **The 64-byte `DATA` on the bulk connection is written raw, not framed.**
This was the lane's question (a). `Clipboard.ShakeHand` builds the package
and writes it with `enStream.Write(package.Bytes, 0, PACKAGE_SIZE_EX)`
(`Clipboard.cs:987`) — no `TcpSendData`, so no magic number and no checksum
in bytes 1–3 (§1.3). The reader is `deStream.ReadEx(buf, 0,
PACKAGE_SIZE_EX)` followed by `package.Bytes = buf` (`:996-997`), which
marshals the buffer straight into the struct, so `Type` is read as the full
**four-byte** `int` (`DATA.cs:27-28, 115-121`). Consequences:
- a sender must leave bytes 1–3 **zero**, or the acceptor's `Type` is not
69/79 and `ShakeHand` rejects the connection;
- a receiver must check those three bytes, not only byte 0 — our `Packet`
exposes byte 0 alone, so `BulkConnection.acceptHandshake` tests them
explicitly;
- there is no checksum on this connection at all. What authenticates it is
that the AES-256-CBC stream is derived from the shared key (§2.3) — the
wrong key turns the 64 bytes into noise whose type byte is not 69 or 79 —
plus the `MachinePool.ResolveID(name) == Src && IsConnectedTo(Src)` gate
(`Clipboard.cs:1009`). Both halves are required before a single byte of
payload is read or written (CLAUDE.md invariant 2).
2. **Foundation's `.zlib` is raw DEFLATE, byte-compatible with .NET's
`DeflateStream`.** This was question (b), and it is **[verified]**, not
transcribed. A UTF-16LE tag/SEP fixture compressed with
`(data as NSData).compressed(using: .zlib)` begins `ad 8e 3b 0a` — no zlib
header (`78 xx`) and no gzip header (`1f 8b`) — and Python
`zlib.decompress(bytes, -15)` inflates it to exactly the original, while
`zlib.decompress(bytes)` fails with "incorrect header check". The reverse
direction also holds: bytes from Python's `compressobj(9, DEFLATED, -15)`
inflate correctly through `NSData.decompressed(using: .zlib)`. So
`NSData.compressed(using: .zlib)` is what `Clipboard.cs:190-201` writes, and
`compression_stream` is not needed.
3. **Trailing zeros after the DEFLATE stream are normal and must be
tolerated**, from two independent directions: the in-band chunker
zero-fills its last 48-byte packet (`Clipboard.cs:281`), and the sender
ships `MemoryStream.GetBuffer()` (`:201`), which is the stream's
**capacity**, not its length — so a Windows payload is routinely longer
than the compressed data, in-band and on the bulk connection alike (the
bulk header's `<len>` is that same padded `data.Length`,
`SocketStuff.cs:1749`). Foundation's inflater ignores the tail;
**[verified]** with 13 and 137 zero bytes appended.
4. **`SendData`'s trailing padding is short by construction, and big text and
images over the bulk connection suffer for it on Windows.** Both senders
pad after the body so the CBC stream ends on a block boundary — a
`CryptoStream` in write mode holds back any final partial block, and
`Flush()` does not release it — but they compute the padding differently:
`SendFileEx` writes `PACKAGE_SIZE - (sentCount % PACKAGE_SIZE)` bytes
(`SocketStuff.cs:1832-1836`), which rounds the total up to 32, while
`SendData` writes `sentCount % PACKAGE_SIZE` bytes (`:1903-1907`). For a
body of `len` bytes the second gives `len + (len % 32)`, which is a multiple
of 16 only when `len ≡ 0 (mod 8)`; otherwise 2 to 14 bytes never leave the
sender and the receiver's `HasExpectedReceivedDataLength` check
(`Clipboard.cs:943-946`) fails with "Received incomplete file". Files are
unaffected. We pad the `SendFileEx` way for every payload
(`ClipboardCodec.trailingPadding`): the receiver reads exactly `<len>` bytes
and discards the rest either way, so over-padding is invisible on the wire
and our sends always arrive. **[transcribed]** — read from the source, not
observed against a running Windows build.
5. **The HTML flavour is CF_HTML on the wire.** `GetText(TextDataFormat.Html)`
(`FormHelper.cs:199`) and `DataFormats.Html` (`Clipboard.cs:1169`) are the
Windows clipboard's `HTML Format`, i.e. the `Version:0.9 / StartHTML: /
EndHTML: / StartFragment: / EndFragment:` header block followed by a full
document with `<!--StartFragment-->` markers, whose offsets count **UTF-8
bytes of the whole CF_HTML string**. macOS's `NSPasteboard.PasteboardType.
html` is bare markup, so the port wraps on send and unwraps on receive
(`ClipboardCodec.cfHTML(wrapping:)` / `htmlFragment(from:)`), and the wire
form is what `ClipboardText.html` always holds.
6. **A file lands in two different folders on Windows depending on the post
action, and neither is quite what §6.3 says.** `postAct == "desktop"` (the
drag-and-drop path) writes to `<Desktop>\MouseWithoutBorders\` and opens
that folder in Explorer (`Clipboard.cs:679-688, 840-848`); every other post
action — including the ordinary pull on a machine switch, whose `postAct` is
the literal string `"PackageType.MachineSwitched"` — writes to
`Common.GetMyStorageDir()`, which is
`<Desktop>\MouseWithoutBorders\ScreenCaptures\` (`Common.cs:1321-1327`,
`Clipboard.cs:726-727`), and puts the file on the clipboard as a file drop
list instead of revealing it (`:864-868`). The port writes every received
file to `~/Desktop/MouseWithoutBorders/`, puts its URL on the pasteboard
**and** reveals it in Finder: one folder is easier to explain than two, and
`ScreenCaptures` names something (screen capture to Paint) that is out of
scope (README D6).
7. **`Clipboard (69)` is a broadcast nobody is addressed by.** The receiver
does not test `Des` at all (`Receiver.cs:273-282`): any machine that hears
the beat records the sender as "the machine with big data" and starts its
own 30 s window. `MachineSwitched (77)`, by contrast, acts only when
`package.Des == Common.MachineID` (`:285`), and `ClipboardAsk (78)` only
when `package.Des == Common.MachineID` (`:318`).
8. **Who pulls and who pushes is decided by the packet type of the *connector's*
handshake, and the acceptor's own type is ignored.** The acceptor enters
`ShakeHand` with `clientPushData = true` and therefore always announces
`ClipboardPush (79)` (`SocketStuff.cs:1643`, `Clipboard.cs:975`); it then
overwrites that variable from what it received and sends when the connector
said `Clipboard (69)`, receives when it said `ClipboardPush (79)`
(`SocketStuff.cs:1655-1665`). The connector overwrites its own copy too and
then ignores it (`Clipboard.cs:477-484`). `PostAction` is likewise taken
from the received packet on both sides.
9. **The "100 interleaved foreign packets" abort is a property of the Windows
receive loop, not of the format.** `ReceiveClipboardDataUsingTCP` blocks
the connection's reader until `ClipboardDataEnd` arrives, dispatching any
other package inline and giving up after 100 of them
(`Clipboard.cs:308-373`). Our `Session` delivers packets one at a time and
never blocks, so there is nothing to interleave: `Reassembler` instead
abandons a payload that grows past the 1 MB in-band limit plus one chunk,
and `ClipboardBridge` drops a reassembly that has been idle for 30 s. The
bound that matters — memory — is kept; the counter is not.
10. **`MAX_CLIPBOARD_DATA_SIZE_CAN_BE_SENT_INSTANTLY_TCP` is compared against
the *compressed* length for text and the raw PNG for images**
(`Clipboard.cs:229-240`, after `LastClipboardData` is set at `:201` or
`:219`), and the comparison is `>`, so exactly 1 MB still goes in-band —
24 000 packets of it. The helper refuses to read text over 20 MB or an
image over 50 MB off the clipboard in the first place
(`FormHelper.cs:28-29, 206, 287`).