# packet-codec ## Goal The `DATA` packet and its framing as Swift value types in `MWBCore`, byte-exact with `docs/protocol.md` §1: the 32/64-byte layout, the `PackageType` numbering and big/small rule, the ASCII machine name, and the magic + checksum framing that overwrites bytes 1–3. This is the foundation every other lane reads bytes through; it has no I/O and no crypto. ## Assumptions - **Little-endian, natural alignment, no padding**, because the Windows side marshals an explicit-layout struct (`DATA.cs:135-149`). All offsets are taken from `docs/protocol.md` §1.1, not recomputed. - **`Type` is one byte on the wire.** The Swift type stores `PackageType` as `UInt8` at offset 0; `Framing.open` zeroes bytes 1–3 exactly as `ProcessReceivedDataEx` does, so a decoded packet's bytes 1–3 are always 0 and re-sealing is deterministic. - **The checksum covers bytes 2..31 only, even on 64-byte packets.** We reproduce this, and pin it with a test that corrupts byte 40 of a 64-byte packet and asserts it is *not* detected — the encryption layer, not the checksum, is what protects the name half. - **Unknown type values decode as `.unknown(UInt8)`** rather than throwing, so a newer Windows build cannot desynchronise our framing. Size for an unknown value follows the big/small rule (`0x80` bit ⇒ 64, else 32), which is what Windows would do. - **Machine names are ASCII, ≤ 32, space-padded.** Non-ASCII input is an error at construction time; on decode, bytes are trimmed of trailing spaces and NULs (Windows trims spaces; NUL tolerance costs nothing). - The magic number is a parameter of `Framing`, not computed here (that is cipher-stream's `magicNumber(for:)`). ## Handshakes ### Promised (topo.md handshake 1, to session and every later consumer) ```swift public enum PackageType: RawRepresentable { case hi, hello, … , unknown(UInt8); var rawValue: UInt8; var isBig: Bool } public struct Packet { public var type: PackageType; public var id: Int32; public var src: UInt32; public var des: UInt32 public var mouse: MouseData // X, Y, wheelDelta, flags at 16..31 public var keyboard: KeyboardData// dateTime at 16, vk at 24, flags at 28 public var nonce: (UInt32, UInt32, UInt32, UInt32) public var postAction: UInt32 public var machineName: String? // nil on small packets public var payload48: Data // bytes 16..63 for clipboard chunks public init(type:) // zeroed buffer of the right size public init?(bytes: Data) // 32 or 64 bytes, no framing check public var bytes: Data // 32 or 64 } public enum Framing { public static func seal(_ p: Packet, magic: UInt32) -> Data public static func open(_ bytes: Data, magic: UInt32) -> Packet // type == .invalid on bad framing } ``` Exact spelling may change during the lane; the README is updated in the same change and session codes against the README. ### Consumed - Nothing. Foundation only. ## Status / TODO - [ ] `Sources/MWBCore/Packet.swift`: `PackageType` (every value in protocol §1.2), `isBig`, `Packet` with union views. - [ ] `Sources/MWBCore/Framing.swift`: `seal` / `open` per §1.3. - [ ] `Tests/MWBCoreTests/PacketTests.swift`: offsets pinned per field, big/small over all 256 values, name padding/trim, non-ASCII refused. - [ ] `Tests/MWBCoreTests/FramingTests.swift`: hand-assembled golden bytes for Handshake / Mouse / Keyboard / Matrix(slot 4, both flags) / ClipboardText chunk; round-trip property test; bytes 2..31 corruption detected; byte 40 corruption *not* detected. - [ ] `docs/protocol.md`: add any offset or rule learned; keep `[transcribed]` marks until interop-vectors. ## Work log ## QA log ## Review