fix(core): Serialization issues (#350)

- [X] Fixes IPAddresses not having enough space
- [X] Streamlines some code
- [X] Fixes base escortables to not load destination tables in a dangerous way.
- [X] Fixes some bad assumptions about resizing buffers
- [X] Fixes little endian issue with circular buffer writer

This doesn't seem to address #349. That issue requires more investigation because I am not seeing where the issue would be.

Closes #347
Closes #322
This commit is contained in:
Kamron Batman 2020-12-13 13:15:48 -08:00 • committed by GitHub
parent 3acb1414dd
commit e17413af21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 186 additions and 222 deletions

View file

@ -88,7 +88,11 @@ namespace System.Buffers
{
if (!BinaryPrimitives.TryWriteInt16BigEndian(_first.Slice(Position), value))
{
// Not enough space. Split the spans
if (BitConverter.IsLittleEndian)
{
value = BinaryPrimitives.ReverseEndianness(value);
}
Write((byte)(value >> 8));
Write((byte)value);
}
@ -114,7 +118,11 @@ namespace System.Buffers
{
if (!BinaryPrimitives.TryWriteUInt16BigEndian(_first.Slice(Position), value))
{
// Not enough space. Split the spans
if (BitConverter.IsLittleEndian)
{
value = BinaryPrimitives.ReverseEndianness(value);
}
Write((byte)(value >> 8));
Write((byte)value);
}
@ -140,7 +148,11 @@ namespace System.Buffers
{
if (!BinaryPrimitives.TryWriteInt32BigEndian(_first.Slice(Position), value))
{
// Not enough space. Split the spans
if (BitConverter.IsLittleEndian)
{
value = BinaryPrimitives.ReverseEndianness(value);
}
Write((byte)(value >> 24));
Write((byte)(value >> 16));
Write((byte)(value >> 8));
@ -168,7 +180,11 @@ namespace System.Buffers
{
if (!BinaryPrimitives.TryWriteInt32LittleEndian(_first.Slice(Position), value))
{
// Not enough space. Split the spans
if (!BitConverter.IsLittleEndian)
{
value = BinaryPrimitives.ReverseEndianness(value);
}
Write((byte)(value >> 24));
Write((byte)(value >> 16));
Write((byte)(value >> 8));
@ -199,7 +215,11 @@ namespace System.Buffers
{
if (!BinaryPrimitives.TryWriteUInt32BigEndian(_first.Slice(Position), value))
{
// Not enough space. Split the spans
if (BitConverter.IsLittleEndian)
{
value = BinaryPrimitives.ReverseEndianness(value);
}
Write((byte)(value >> 24));
Write((byte)(value >> 16));
Write((byte)(value >> 8));
@ -220,6 +240,38 @@ namespace System.Buffers
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteLE(uint value)
{
if (Position < _first.Length)
{
if (!BinaryPrimitives.TryWriteUInt32LittleEndian(_first.Slice(Position), value))
{
if (!BitConverter.IsLittleEndian)
{
value = BinaryPrimitives.ReverseEndianness(value);
}
Write((byte)(value >> 24));
Write((byte)(value >> 16));
Write((byte)(value >> 8));
Write((byte)value);
}
else
{
Position += 4;
}
}
else if (BinaryPrimitives.TryWriteUInt32LittleEndian(_second.Slice(Position - _first.Length), value))
{
Position += 4;
}
else
{
throw new OutOfMemoryException();
}
}
/// <summary>
/// Writes a 8-byte signed integer value to the underlying stream.
/// </summary>
@ -230,7 +282,11 @@ namespace System.Buffers
{
if (!BinaryPrimitives.TryWriteInt64BigEndian(_first.Slice(Position), value))
{
// Not enough space. Split the spans
if (BitConverter.IsLittleEndian)
{
value = BinaryPrimitives.ReverseEndianness(value);
}
Write((byte)(value >> 56));
Write((byte)(value >> 48));
Write((byte)(value >> 40));
@ -239,6 +295,7 @@ namespace System.Buffers
Write((byte)(value >> 16));
Write((byte)(value >> 8));
Write((byte)value);
}
else
{
@ -265,7 +322,11 @@ namespace System.Buffers
{
if (!BinaryPrimitives.TryWriteUInt64BigEndian(_first.Slice(Position), value))
{
// Not enough space. Split the spans
if (BitConverter.IsLittleEndian)
{
value = BinaryPrimitives.ReverseEndianness(value);
}
Write((byte)(value >> 56));
Write((byte)(value >> 48));
Write((byte)(value >> 40));