Files
mxaccess/src/MxNativeCodec/NmxItemControlMessage.cs
T
Joseph Doherty fe2a6db786
rust / build / test / clippy / fmt (push) Has been cancelled
Initial project state: .NET reference, design, Rust port (M0+M1), evidence
Layout:
- src/                    .NET 10 x64 reference: MxNativeCodec, MxNativeClient,
                          MxAsbClient, probes, tests, harnesses. Executable spec.
- design/                 Architectural plan for the Rust port (M0–M6), error
                          model, protocol invariants, risks (R1–R16), adversarial
                          review log (review.md).
- rust/                   Rust workspace. M0 skeleton + M1 codec parity.
                          mxaccess-codec: 215 unit tests + 2 cross-implementation
                          parity tests (byte-identical against .NET reference).
                          Other crates are M0 stubs awaiting M2+.
- captures/               Frida + netsh + pcap evidence per CLAUDE.md
                          ("captures are evidence, not throwaway logs").
- analysis/               Decompiled C# (frida/proxy/decompiled-*),
                          Ghidra exports for native DLLs (`exports/` only —
                          working state at `projects/` and AVEVA's input
                          binaries at `input/` are gitignored).
- docs/                   Reverse-engineering reference docs.
- tools/                  Setup-LiveProbeEnv.ps1 (Infisical credential fetcher),
                          Compute-Crc.ps1 (.NET parity helper).
- .github/workflows/      Rust CI: fmt + build + test + clippy on Windows.
- LICENSE                 MIT (Joseph Doherty, 2026).

Verified:
- cargo test --workspace → 217 passed (215 unit + 2 .NET parity), 0 failed
- cargo clippy --workspace -- -D warnings → clean
- cargo fmt --all -- --check → clean
- cargo publish --dry-run -p mxaccess-codec → packages cleanly

Excluded from history (see .gitignore):
- **/bin, **/obj, **/target — build artifacts
- analysis/ghidra/projects/ — Ghidra working state (regenerable)
- analysis/ghidra/input/ — AVEVA proprietary DLLs (vendor IP)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 06:21:00 -04:00

155 lines
5.6 KiB
C#

using System.Buffers.Binary;
namespace MxNativeCodec;
public enum NmxItemControlCommand : byte
{
Advise = 0x1f,
AdviseSupervisory = 0x1f,
UnAdvise = 0x21,
}
public sealed record NmxItemControlMessage(
NmxItemControlCommand Command,
Guid ItemCorrelationId,
ushort ObjectId,
ushort ObjectSignature,
short PrimitiveId,
short AttributeId,
short PropertyId,
ushort AttributeSignature,
short AttributeIndex,
uint Tail)
{
private const ushort Version = 1;
private const int HeaderLength = 3;
private const int GuidLength = 16;
private const int AdviseExtraLength = 2;
private const int PayloadLength = 18;
public static int GetEncodedLength(NmxItemControlCommand command)
{
return HeaderLength
+ GuidLength
+ (command == NmxItemControlCommand.AdviseSupervisory ? AdviseExtraLength : 0)
+ PayloadLength;
}
public static NmxItemControlMessage Parse(ReadOnlySpan<byte> body)
{
if (body.Length < HeaderLength + GuidLength + PayloadLength)
{
throw new ArgumentException("NMX item-control body is too short.", nameof(body));
}
var command = (NmxItemControlCommand)body[0];
if (command is not (NmxItemControlCommand.AdviseSupervisory or NmxItemControlCommand.UnAdvise))
{
throw new ArgumentException($"Unsupported item-control command 0x{body[0]:X2}.", nameof(body));
}
ushort version = BinaryPrimitives.ReadUInt16LittleEndian(body.Slice(1, sizeof(ushort)));
if (version != Version)
{
throw new ArgumentException($"Unsupported item-control version {version}.", nameof(body));
}
int expectedLength = GetEncodedLength(command);
if (body.Length != expectedLength)
{
throw new ArgumentException($"Unexpected item-control body length {body.Length}; expected {expectedLength}.", nameof(body));
}
int offset = HeaderLength;
var itemCorrelationId = new Guid(body.Slice(offset, GuidLength));
offset += GuidLength;
if (command == NmxItemControlCommand.AdviseSupervisory)
{
offset += AdviseExtraLength;
}
return new NmxItemControlMessage(
command,
itemCorrelationId,
BinaryPrimitives.ReadUInt16LittleEndian(body.Slice(offset, sizeof(ushort))),
BinaryPrimitives.ReadUInt16LittleEndian(body.Slice(offset + 2, sizeof(ushort))),
BinaryPrimitives.ReadInt16LittleEndian(body.Slice(offset + 4, sizeof(short))),
BinaryPrimitives.ReadInt16LittleEndian(body.Slice(offset + 6, sizeof(short))),
BinaryPrimitives.ReadInt16LittleEndian(body.Slice(offset + 8, sizeof(short))),
BinaryPrimitives.ReadUInt16LittleEndian(body.Slice(offset + 10, sizeof(ushort))),
BinaryPrimitives.ReadInt16LittleEndian(body.Slice(offset + 12, sizeof(short))),
BinaryPrimitives.ReadUInt32LittleEndian(body.Slice(offset + 14, sizeof(uint))));
}
public static NmxItemControlMessage FromReferenceHandle(
NmxItemControlCommand command,
Guid itemCorrelationId,
MxReferenceHandle referenceHandle,
uint tail = 3)
{
return new NmxItemControlMessage(
command,
itemCorrelationId,
referenceHandle.ObjectId,
referenceHandle.ObjectSignature,
referenceHandle.PrimitiveId,
referenceHandle.AttributeId,
referenceHandle.PropertyId,
referenceHandle.AttributeSignature,
referenceHandle.AttributeIndex,
tail);
}
public MxReferenceHandle ToReferenceHandle(
byte galaxyId = 1,
ushort platformId = 1,
ushort engineId = 1)
{
return new MxReferenceHandle(
galaxyId,
platformId,
engineId,
ObjectId,
ObjectSignature,
PrimitiveId,
AttributeId,
PropertyId,
AttributeSignature,
AttributeIndex);
}
public byte[] Encode()
{
byte[] body = new byte[GetEncodedLength(Command)];
body[0] = (byte)Command;
BinaryPrimitives.WriteUInt16LittleEndian(body.AsSpan(1, sizeof(ushort)), Version);
int offset = HeaderLength;
ItemCorrelationId.TryWriteBytes(body.AsSpan(offset, GuidLength));
offset += GuidLength;
if (Command == NmxItemControlCommand.AdviseSupervisory)
{
offset += AdviseExtraLength;
}
BinaryPrimitives.WriteUInt16LittleEndian(body.AsSpan(offset, sizeof(ushort)), ObjectId);
BinaryPrimitives.WriteUInt16LittleEndian(body.AsSpan(offset + 2, sizeof(ushort)), ObjectSignature);
BinaryPrimitives.WriteInt16LittleEndian(body.AsSpan(offset + 4, sizeof(short)), PrimitiveId);
BinaryPrimitives.WriteInt16LittleEndian(body.AsSpan(offset + 6, sizeof(short)), AttributeId);
BinaryPrimitives.WriteInt16LittleEndian(body.AsSpan(offset + 8, sizeof(short)), PropertyId);
BinaryPrimitives.WriteUInt16LittleEndian(body.AsSpan(offset + 10, sizeof(ushort)), AttributeSignature);
BinaryPrimitives.WriteInt16LittleEndian(body.AsSpan(offset + 12, sizeof(short)), AttributeIndex);
BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(offset + 14, sizeof(uint)), Tail);
return body;
}
public NmxItemControlMessage ToUnAdvise()
{
return this with { Command = NmxItemControlCommand.UnAdvise };
}
public NmxItemControlMessage ToAdviseSupervisory()
{
return this with { Command = NmxItemControlCommand.AdviseSupervisory };
}
}