Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Byte array support #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions MsgPack/MsgPackDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ internal unsafe byte ReadByte()
return v;
}

internal unsafe byte[] ReadByteArray(uint size)
{
byte* ptr = AdvancePointer(size);

byte[] array = new byte[size];
Marshal.Copy((IntPtr)ptr, array, 0, (int)size);

return array;
}

internal MsgPackCode ReadType() => (MsgPackCode)ReadByte();

internal byte ReadUInt8() => ReadByte();
Expand Down
36 changes: 36 additions & 0 deletions MsgPack/MsgPackDeserializerConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,42 @@ internal void SkipObjects(uint size)
SkipObject();
}

internal byte[] ConvertMsgPackedTypesToByteArray(uint size)
{
byte[] array = new byte[size];

for (uint i = 0; i < size; ++i)
array[i] = (byte)DeserializeAsUInt32();

return array;
}

public byte[] DeserializeAsByteArray()
{
MsgPackCode type = (MsgPackCode)ReadByte();
if (type >= MsgPackCode.FixArrayMin && type <= MsgPackCode.FixArrayMax)
return ConvertMsgPackedTypesToByteArray((byte)type % 16u);

switch (type)
{
case MsgPackCode.Bin8:
return ReadByteArray(ReadUInt8());
case MsgPackCode.Bin16:
return ReadByteArray(ReadUInt16());
case MsgPackCode.Bin32:
return ReadByteArray(ReadUInt32());
case MsgPackCode.Array16:
return ConvertMsgPackedTypesToByteArray(ReadUInt16());
case MsgPackCode.Array32:
return ConvertMsgPackedTypesToByteArray(ReadUInt16());
}

SkipObject(type);
throw new InvalidCastException(
$"MsgPack type {type} could not be deserialized into type {typeof(byte[])}"
);
}

public unsafe bool DeserializeAsBool()
{
MsgPackCode type = ReadType();
Expand Down
16 changes: 16 additions & 0 deletions MsgPack/MsgPackSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ public void Serialize(byte v)
Write(MsgPackCode.UInt8, v);
}

public void Serialize(byte[] v)
{
uint size = (uint)v.Length;

if (size <= byte.MaxValue)
Write(MsgPackCode.Bin8, unchecked((byte)size));
else if (size <= ushort.MaxValue)
WriteBigEndian(MsgPackCode.Bin16, unchecked((ushort)size));
else
WriteBigEndian(MsgPackCode.Bin32, size);

EnsureCapacity(size);
Array.Copy(v, 0, m_buffer, (int)m_position, size);
m_position += size;
}

public void Serialize(short v)
{
if (v < 0)
Expand Down