110 lines
4.1 KiB
C#
Raw Normal View History

2019-06-12 21:56:55 +02:00
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace Capnp
{
/// <summary>
/// Supports the deserialization of Cap'n Proto messages from a stream (see https://capnproto.org/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>
2019-07-11 21:44:42 +02:00
/// <exception cref="InvalidDataException">Encountered invalid framing data, too many or too large segments</exception>
2019-06-12 21:56:55 +02:00
/// <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))
{
2019-06-22 18:43:30 -04:00
return reader.ReadWireFrame();
}
}
2019-06-12 21:56:55 +02:00
/// <summary>
/// Deserializes the next Cap'n Proto message from given stream.
/// </summary>
/// <param name="reader">The stream to read from</param>
/// <returns>The message</returns>
2019-06-22 18:43:30 -04:00
public static WireFrame ReadWireFrame(this BinaryReader reader)
{
uint scount = reader.ReadUInt32();
if (scount++ == uint.MaxValue)
{
throw new InvalidDataException("Encountered invalid framing data");
}
2019-07-11 21:44:42 +02:00
// 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.");
}
2019-06-22 18:43:30 -04:00
var buffers = new Memory<ulong>[scount];
2019-06-12 21:56:55 +02:00
2019-06-22 18:43:30 -04:00
for (uint i = 0; i < scount; i++)
{
uint size = reader.ReadUInt32();
2019-07-11 21:44:42 +02:00
if (size == 0)
{
throw new EndOfStreamException("Stream closed");
}
2019-07-11 21:44:42 +02:00
if (size >= SecurityOptions.TraversalLimit)
{
throw new InvalidDataException("Too large segment. Probably invalid data. Try increasing the traversal limit.");
}
2019-06-22 18:43:30 -04:00
buffers[i] = new Memory<ulong>(new ulong[size]);
}
if ((scount & 1) == 0)
{
// Padding
reader.ReadUInt32();
}
FillBuffersFromFrames(buffers, scount, reader);
return new WireFrame(buffers);
}
static void FillBuffersFromFrames(Memory<ulong>[] buffers, uint segmentCount, BinaryReader reader)
2019-06-22 18:43:30 -04:00
{
for (uint i = 0; i < segmentCount; i++)
{
#if NETSTANDARD2_0
2019-06-22 18:43:30 -04:00
var buffer = MemoryMarshal.Cast<ulong, byte>(buffers[i].Span.ToArray());
var tmpBuffer = reader.ReadBytes(buffer.Length);
if (tmpBuffer.Length != buffer.Length)
2019-06-12 21:56:55 +02:00
{
2019-06-22 18:43:30 -04:00
throw new InvalidDataException("Expected more bytes according to framing header");
2019-06-12 21:56:55 +02:00
}
2019-06-22 19:46:52 -04:00
// Fastest way to do this without /unsafe
2019-06-22 18:43:30 -04:00
for (int j = 0; j < buffers[i].Length; j++)
2019-06-12 21:56:55 +02:00
{
2019-06-22 18:43:30 -04:00
var value = BitConverter.ToUInt64(tmpBuffer, j*8);
buffers[i].Span[j] = value;
2019-06-12 21:56:55 +02:00
}
#else
var buffer = MemoryMarshal.Cast<ulong, byte>(buffers[i].Span);
reader.Read(buffer);
#endif
2019-06-12 21:56:55 +02:00
}
}
}
}