Files
zeekay 2aa8261f86 rebrand: complete capnp → ZAP across all C# source/build files
Finishes the in-flight capnp→ZAP rebrand on this branch. The prior commits
renamed identifiers/namespaces/files/projects (Capnp.Net.Runtime→Zap.Net.Runtime,
CapnpC→ZapC, .capnp→.zap) but left the human-readable brand name "Cap'n Proto"
throughout doc-comments, .csproj/.nuspec descriptions, README, MSBuild .xaml
rule display names, and the generated XML doc. This completes brand neutrality:

- Replace 154 "Cap'n Proto" / 4 "Cap'n-Proto" / 1 "Cap'n'p" prose occurrences
  with "ZAP" across 32 first-party files (runtime, generator, zapc-csharp,
  MSBuild integration, chocolatey nuspecs, README, docs, XML doc).
- Revert the LICENSE provenance URL to the real upstream fork
  (c80k/capnproto-dotnetcore) — upstream-fork license headers must stay.
- Canonicalize project/repo URLs to github.com/zap-proto/cs (were fabricated
  c80k/zap-dotnetcore) in csproj/nuspec/VERIFICATION/README badges.
- Canonicalize docs URLs zap-protocol(.github.io|/zap-cs|/zap-cpp-core)
  → zap-proto.dev / github.com/zap-proto/{cs,cpp-core}.
- Collapse redundant "zap \"ZAP\"" PackageTags to "zap".

Verified consistent (no toolchain on host): every .sln/.csproj ProjectReference
resolves; all namespaces are Zap*/ZapC*; MSBuild task chain
(GenerateZapFileCodeBehindTask, ZapFiles ItemGroup, _ZapcCsharp_TaskAssembly)
is uniformly renamed; props/targets/xaml/coverage/chocolatey carry no capnp.

Deliberately preserved as upstream-derived test data (not first-party source):
upstream Cap'n Proto schema fixtures (rpc.zap, rpc-twoparty.zap, json.zap,
rpc-csharp.zap), golden compiler-error outputs (errors.txt), and the
machine-generated DO-NOT-EDIT test.zap.{c++,h}. Their "Cap'n Proto" strings are
the literal output of the external schema compiler the compat test matches.
2026-06-19 23:46:57 -07:00

125 lines
4.9 KiB
C#

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace Zap
{
/// <summary>
/// Supports the deserialization of ZAP messages from a stream (see https://zap-proto.dev/encoding.html#serialization-over-a-stream).
/// Packing and compression cannot be handled yet.
/// </summary>
public static class Framing
{
/// <summary>
/// Deserializes a message from given stream.
/// </summary>
/// <param name="stream">The stream to read from</param>
/// <returns>The deserialized message</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
/// <exception cref="ArgumentException">The stream does not support reading, is null, or is already closed.</exception>
/// <exception cref="EndOfStreamException">The end of the stream is reached.</exception>
/// <exception cref="ObjectDisposedException">The stream is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
/// <exception cref="InvalidDataException">Encountered invalid framing data, too many or too large segments</exception>
/// <exception cref="OutOfMemoryException">Too many or too large segments, probably due to invalid framing data.</exception>
public static WireFrame ReadSegments(Stream stream)
{
using (var reader = new BinaryReader(stream, Encoding.Default, true))
{
return reader.ReadWireFrame();
}
}
/// <summary>
/// Deserializes the next ZAP message from given stream.
/// </summary>
/// <param name="reader">The stream to read from</param>
/// <returns>The message</returns>
public static WireFrame ReadWireFrame(this BinaryReader reader)
{
uint scount = reader.ReadUInt32();
if (scount++ == uint.MaxValue)
{
throw new InvalidDataException("Encountered invalid framing data");
}
// Cannot have more segments than the traversal limit
if (scount >= SecurityOptions.TraversalLimit)
{
throw new InvalidDataException("Too many segments. Probably invalid data. Try increasing the traversal limit.");
}
var buffers = new Memory<ulong>[scount];
for (uint i = 0; i < scount; i++)
{
uint size = reader.ReadUInt32();
if (size == 0)
{
throw new EndOfStreamException("Stream closed");
}
if (size >= SecurityOptions.TraversalLimit)
{
throw new InvalidDataException("Too large segment. Probably invalid data. Try increasing the traversal limit.");
}
buffers[i] = new Memory<ulong>(new ulong[size]);
}
if ((scount & 1) == 0)
{
// Padding
reader.ReadUInt32();
}
FillBuffersFromFrames(buffers, scount, reader);
return new WireFrame(buffers);
}
static InvalidDataException StreamClosed()
=> new InvalidDataException("Prematurely reached end of stream. Expected more bytes according to framing header.");
static void FillBuffersFromFrames(Memory<ulong>[] buffers, uint segmentCount, BinaryReader reader)
{
for (uint i = 0; i < segmentCount; i++)
{
#if NETSTANDARD2_0
var buffer = MemoryMarshal.Cast<ulong, byte>(buffers[i].Span.ToArray());
var tmpBuffer = reader.ReadBytes(buffer.Length);
if (tmpBuffer.Length != buffer.Length)
{
// Note w.r.t. issue #37: If there are temporarily less bytes available,
// this will NOT cause ReadBytes to return a shorter buffer.
// Only if the end of the stream is reached will we enter this branch. And this will be an error condition,
// since it would mean that the connection was closed in the middle of a frame transfer.
throw StreamClosed();
}
// Fastest way to do this without /unsafe
for (int j = 0; j < buffers[i].Length; j++)
{
var value = BitConverter.ToUInt64(tmpBuffer, j*8);
buffers[i].Span[j] = value;
}
#else
var buffer = MemoryMarshal.Cast<ulong, byte>(buffers[i].Span);
do
{
int obtained = reader.Read(buffer);
if (obtained == 0)
throw StreamClosed();
buffer = buffer.Slice(obtained);
}
while (buffer.Length > 0);
#endif
}
}
}
}