public static unsafe int WriteRaw1(ref byte[] dst, int dstOffset, byte[] src) { MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); fixed(byte *pSrc = &src[0]) fixed(byte *pDst = &dst[dstOffset]) { *(byte *)pDst = *(byte *)pSrc; } return(src.Length); }
public static unsafe int WriteRaw9(ref byte[] dst, int dstOffset, byte[] src) { MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); fixed(byte *pSrc = &src[0]) fixed(byte *pDst = &dst[dstOffset]) { *(long *)(pDst + 0) = *(long *)(pSrc + 0); *(long *)(pDst + 1) = *(long *)(pSrc + 1); } return(src.Length); }
public unsafe int Serialize(ref byte[] bytes, int offset, T value, IFormatterResolver formatterResolver) { // Note: require byte header? maybe this is not valid msgpack-format. MessagePackBinary.EnsureCapacity(ref bytes, offset, size); fixed(void *p = &bytes[offset]) { UnsafeUtility.CopyStructureToPtr(ref value, p); } return(size); }
public int Serialize(ref byte[] bytes, int offset, CombGuid value, IFormatterResolver formatterResolver) { const byte _byteSize = 16; var buffer = value.GetByteArray(CombGuidSequentialSegmentType.Guid); MessagePackBinary.EnsureCapacity(ref bytes, offset, c_totalSize); bytes[offset] = MessagePackCode.Bin8; bytes[offset + 1] = _byteSize; Buffer.BlockCopy(buffer, 0, bytes, offset + 2, c_valueSize); return(c_totalSize); }
public static unsafe int WriteRaw13(ref byte[] dst, int dstOffset, byte[] src) { MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length); fixed (byte* pSrc = &src[0]) fixed (byte* pDst = &dst[dstOffset]) { *(int*)(pDst + 0) = *(int*)(pSrc + 0); *(int*)(pDst + 4) = *(int*)(pSrc + 4); *(int*)(pDst + 8) = *(int*)(pSrc + 8); *(int*)(pDst + 9) = *(int*)(pSrc + 9); } return src.Length; }
public int Serialize(ref byte[] bytes, int offset, Guid?value, IFormatterResolver formatterResolver) { if (value == null) { return(MessagePackBinary.WriteNil(ref bytes, offset)); } MessagePackBinary.EnsureCapacity(ref bytes, offset, 38); var guid = value.Value; bytes[offset] = MessagePackCode.Str8; bytes[offset + 1] = unchecked ((byte)36); new GuidBits(ref guid).Write(bytes, offset + 2); return(38); }
static void SerializeWithLengthPrefixExt <T>(Stream stream, T data, IFormatterResolver resolver) { const int ExtTypeCode = 111; // sample ext code byte[] buffer = null; var formatter = resolver.GetFormatter <T>(); var dataSize = formatter.Serialize(ref buffer, 0, data, resolver); var headerLength = MessagePackBinary.GetExtensionFormatHeaderLength(dataSize); MessagePackBinary.EnsureCapacity(ref buffer, 0, headerLength); Buffer.BlockCopy(buffer, 0, buffer, headerLength, dataSize); MessagePackBinary.WriteExtensionFormatHeader(ref buffer, 0, ExtTypeCode, dataSize); stream.Write(buffer, 0, dataSize + headerLength); }
public int Serialize(ref byte[] bytes, int offset, IPAddress value, IFormatterResolver formatterResolver) { if (value == null) { return(MessagePackBinary.WriteNil(ref bytes, offset)); } var addressBytes = value.GetAddressBytes(); var length = addressBytes.Length; var totalSize = length + 2; MessagePackBinary.EnsureCapacity(ref bytes, offset, totalSize); bytes[offset] = MessagePackCode.Bin8; bytes[offset + 1] = (byte)length; Buffer.BlockCopy(addressBytes, 0, bytes, offset + 2, length); return(totalSize); }
public int Serialize(ref byte[] bytes, int offset, Guid value, IFormatterResolver formatterResolver) { var buffer = value.ToByteArray(); MessagePackBinary.EnsureCapacity(ref bytes, offset, c_totalSize); bytes[offset] = MessagePackCode.Bin8; bytes[offset + 1] = c_valueSize; Buffer.BlockCopy(buffer, 0, bytes, offset + 2, c_valueSize); return(c_totalSize); //MessagePackBinary.EnsureCapacity(ref bytes, offset, 38); //bytes[offset] = MessagePackCode.Str8; //bytes[offset + 1] = unchecked((byte)36); //new GuidBits(ref value).Write(bytes, offset + 2); //return 38; }
// decimal underlying "flags, hi, lo, mid" fields are sequential and same layuout with .NET Framework and Mono(Unity) // But target machines must be same endian so restrict only for little endian. public unsafe int Serialize(ref byte[] bytes, int offset, Decimal value, IFormatterResolver formatterResolver) { if (!BitConverter.IsLittleEndian) { throw new Exception("BinaryGuidFormatter only allows on little endian env."); } MessagePackBinary.EnsureCapacity(ref bytes, offset, 18); fixed(byte *dst = &bytes[offset]) { var src = &value; dst[0] = MessagePackCode.Bin8; dst[1] = 16; *(Decimal *)(dst + 2) = *src; } return(18); }
// decimal underlying "flags, hi, lo, mid" fields are sequential and same layuout with .NET Framework and Mono(Unity) // But target machines must be same endian so restrict only for little endian. public unsafe int Serialize(ref byte[] bytes, int offset, Decimal value, IFormatterResolver formatterResolver) { if (!BitConverter.IsLittleEndian) { ThrowHelper.ThrowException_Decimal_Little_Endian(); } MessagePackBinary.EnsureCapacity(ref bytes, offset, 18); fixed(byte *dst = &bytes[offset]) { var src = &value; dst[0] = MessagePackCode.Bin8; dst[1] = 16; *(Decimal *)(dst + 2) = *src; } return(18); }
public int Serialize(ref byte[] bytes, int offset, T[] value, IFormatterResolver formatterResolver) { if (value == null) { return(MessagePackBinary.WriteNil(ref bytes, offset)); } var startOffset = offset; var byteLen = value.Length * StructLength; offset += MessagePackBinary.WriteExtensionFormatHeader(ref bytes, offset, TypeCode, byteLen); offset += MessagePackBinary.WriteInt32(ref bytes, offset, byteLen); // write original header(not array header) offset += MessagePackBinary.WriteBoolean(ref bytes, offset, BitConverter.IsLittleEndian); MessagePackBinary.EnsureCapacity(ref bytes, offset, byteLen); Unsafe.CopyBlockUnaligned(ref bytes[offset], ref Unsafe.As <T, byte>(ref value[0]), (uint)byteLen); offset += byteLen; return(offset - startOffset); }
public int Serialize(ref byte[] bytes, int offset, byte[] value, IFormatterResolver formatterResolver) { if (value == null) { return(MessagePackBinary.WriteNil(ref bytes, offset)); } var byteCount = value.Length; if (byteCount <= MessagePackRange.MaxFixStringLength) { MessagePackBinary.EnsureCapacity(ref bytes, offset, byteCount + 1); bytes[offset] = (byte)(MessagePackCode.MinFixStr | byteCount); Buffer.BlockCopy(value, 0, bytes, offset + 1, byteCount); return(byteCount + 1); } else if (byteCount <= ushort.MaxValue) { MessagePackBinary.EnsureCapacity(ref bytes, offset, byteCount + 3); bytes[offset] = MessagePackCode.Str16; bytes[offset + 1] = unchecked ((byte)(byteCount >> 8)); bytes[offset + 2] = unchecked ((byte)byteCount); Buffer.BlockCopy(value, 0, bytes, offset + 3, byteCount); return(byteCount + 3); } else { MessagePackBinary.EnsureCapacity(ref bytes, offset, byteCount + 5); bytes[offset] = MessagePackCode.Str32; bytes[offset + 1] = unchecked ((byte)(byteCount >> 24)); bytes[offset + 2] = unchecked ((byte)(byteCount >> 16)); bytes[offset + 3] = unchecked ((byte)(byteCount >> 8)); bytes[offset + 4] = unchecked ((byte)byteCount); Buffer.BlockCopy(value, 0, bytes, offset + 5, byteCount); return(byteCount + 5); } }
public int Serialize(ref byte[] bytes, int offset, LocalDate value, IFormatterResolver formatterResolver) { uint v = 0; unchecked { v = (uint)( ((value.Year - 1) << CalendarDayMonthBits) | ((value.Month - 1) << CalendarDayBits) | ((value.Day - 1) << CalendarBits) | 0 ); } MessagePackBinary.EnsureCapacity(ref bytes, offset, 6); bytes[offset] = MessagePackCode.FixExt4; bytes[offset + 1] = unchecked ((byte)NodatimeMessagePackExtensionTypeCode.LocalDate); bytes[offset + 2] = unchecked ((byte)(v >> 24)); bytes[offset + 3] = unchecked ((byte)(v >> 16)); bytes[offset + 4] = unchecked ((byte)(v >> 8)); bytes[offset + 5] = unchecked ((byte)v); return(6); }
public static int WriteBytesHeaderWithEnsureCount(ref byte[] dest, int dstOffset, int count) { if (count <= byte.MaxValue) { var size = 2; MessagePackBinary.EnsureCapacity(ref dest, dstOffset, size + count); dest[dstOffset] = MessagePackCode.Bin8; dest[dstOffset + 1] = (byte)count; return(size); } else if (count <= UInt16.MaxValue) { var size = 3; MessagePackBinary.EnsureCapacity(ref dest, dstOffset, size + count); unchecked { dest[dstOffset] = MessagePackCode.Bin16; dest[dstOffset + 1] = (byte)(count >> 8); dest[dstOffset + 2] = (byte)(count); } return(size); } else { var size = 5; MessagePackBinary.EnsureCapacity(ref dest, dstOffset, size + count); unchecked { dest[dstOffset] = MessagePackCode.Bin32; dest[dstOffset + 1] = (byte)(count >> 24); dest[dstOffset + 2] = (byte)(count >> 16); dest[dstOffset + 3] = (byte)(count >> 8); dest[dstOffset + 4] = (byte)(count); } return(size); } }
public int Serialize(ref byte[] bytes, int offset, TCollection value, IFormatterResolver formatterResolver) { if (value == null) { return(MessagePackBinary.WriteNil(ref bytes, offset)); } else { // Optimize iteration(array is fastest) var array = value as TElement[]; if (array != null) { var startOffset = offset; var formatter = formatterResolver.GetFormatterWithVerify <TElement>(); offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, array.Length); foreach (var item in array) { offset += formatter.Serialize(ref bytes, offset, item, formatterResolver); } return(offset - startOffset); } else { var startOffset = offset; var formatter = formatterResolver.GetFormatterWithVerify <TElement>(); // knows count or not. var seqCount = GetCount(value); if (seqCount != null) { offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, seqCount.Value); // Unity's foreach struct enumerator causes boxing so iterate manually. var e = GetSourceEnumerator(value); try { while (e.MoveNext()) { #if NETSTANDARD offset += formatter.Serialize(ref bytes, offset, e.Current, formatterResolver); #else offset += formatter.Serialize(ref bytes, (int)offset, (TElement)e.Current, (IFormatterResolver)formatterResolver); #endif } } finally { e.Dispose(); } return(offset - startOffset); } else { // write message first -> open header space -> write header var writeStarOffset = offset; var count = 0; var moveCount = 0; // count = 16 <= 65535, header len is "3" so choose default space. offset += 3; var e = GetSourceEnumerator(value); try { while (e.MoveNext()) { count++; #if NETSTANDARD var writeSize = formatter.Serialize(ref bytes, offset, e.Current, formatterResolver); #else var writeSize = formatter.Serialize(ref bytes, (int)offset, (TElement)e.Current, (IFormatterResolver)formatterResolver); #endif moveCount += writeSize; offset += writeSize; } } finally { e.Dispose(); } var headerLength = MessagePackBinary.GetArrayHeaderLength(count); if (headerLength != 3) { if (headerLength == 1) { offset -= 2; // 1 } else { offset += 2; // 5 } MessagePackBinary.EnsureCapacity(ref bytes, offset, headerLength); Buffer.BlockCopy(bytes, writeStarOffset + 3, bytes, writeStarOffset + headerLength, moveCount); } MessagePackBinary.WriteArrayHeader(ref bytes, writeStarOffset, count); return(offset - startOffset); } } } }
// Old spec does not exists str 8 format. public int Serialize(ref byte[] bytes, int offset, string value, IFormatterResolver formatterResolver) { if (value == null) { return(MessagePackBinary.WriteNil(ref bytes, offset)); } MessagePackBinary.EnsureCapacity(ref bytes, offset, StringEncoding.UTF8.GetMaxByteCount(value.Length) + 5); int useOffset; if (value.Length <= MessagePackRange.MaxFixStringLength) { useOffset = 1; } else if (value.Length <= ushort.MaxValue) { useOffset = 3; } else { useOffset = 5; } // skip length area var writeBeginOffset = offset + useOffset; var byteCount = StringEncoding.UTF8.GetBytes(value, 0, value.Length, bytes, writeBeginOffset); // move body and write prefix if (byteCount <= MessagePackRange.MaxFixStringLength) { if (useOffset != 1) { Buffer.BlockCopy(bytes, writeBeginOffset, bytes, offset + 1, byteCount); } bytes[offset] = (byte)(MessagePackCode.MinFixStr | byteCount); return(byteCount + 1); } else if (byteCount <= ushort.MaxValue) { if (useOffset != 3) { Buffer.BlockCopy(bytes, writeBeginOffset, bytes, offset + 3, byteCount); } bytes[offset] = MessagePackCode.Str16; bytes[offset + 1] = unchecked ((byte)(byteCount >> 8)); bytes[offset + 2] = unchecked ((byte)byteCount); return(byteCount + 3); } else { if (useOffset != 5) { Buffer.BlockCopy(bytes, writeBeginOffset, bytes, offset + 5, byteCount); } bytes[offset] = MessagePackCode.Str32; bytes[offset + 1] = unchecked ((byte)(byteCount >> 24)); bytes[offset + 2] = unchecked ((byte)(byteCount >> 16)); bytes[offset + 3] = unchecked ((byte)(byteCount >> 8)); bytes[offset + 4] = unchecked ((byte)byteCount); return(byteCount + 5); } }
int IInternalMemory.Serialize(ref byte[] bytes, int offset, IFormatterResolver resolver) { MessagePackBinary.EnsureCapacity(ref bytes, offset, RawMemory.Length); Buffer.BlockCopy(RawMemory, 0, bytes, offset, RawMemory.Length); return(RawMemory.Length); }