Пример #1
0
        public static ArraySegment <byte> GetBytes(byte value)
        {
            var result = ImmutableArrayPool <byte> .Allocate(1);

            result.Array[result.Offset] = value;

            return(result);
        }
Пример #2
0
        public static ArraySegment <byte> GetBytes(string value, Encoding encoding)
        {
            var length = encoding.GetByteCount(value);
            var result = ImmutableArrayPool <byte> .Allocate(length);

            encoding.GetBytes(value, 0, value.Length, result.Array, result.Offset);

            return(result);
        }
Пример #3
0
        public unsafe static ArraySegment <byte> GetBytes(ulong value)
        {
            var result = ImmutableArrayPool <byte> .Allocate(8);

            fixed(byte *pBuffer = result.Array)
            * (ulong *)(pBuffer + result.Offset) = value;

            return(result);
        }
Пример #4
0
        private unsafe static void DeserializeKeys(ref DeserializationContext context, out HashSet <TangleKey> output)
        {
            if (context.SourceLength < 4)
            {
                throw new InvalidDataException();
            }

            int count = *(int *)(context.Source);

            output = new HashSet <TangleKey>();
            uint offset = 4;

            for (int i = 0; i < count; i++)
            {
                if (context.SourceLength < (offset + 6))
                {
                    throw new InvalidDataException();
                }

                int    keyLength = *(int *)(context.Source + offset);
                ushort keyType   = *(ushort *)(context.Source + offset + 4);

                offset += 6;
                if (context.SourceLength < (offset + keyLength))
                {
                    throw new InvalidDataException();
                }

                var keyBytes = ImmutableArrayPool <byte> .Allocate(keyLength);

                Unsafe.ReadBytes(context.Source, offset, keyBytes.Array, keyBytes.Offset, (uint)keyBytes.Count);

                output.Add(new TangleKey(keyBytes, keyType));
                offset += (uint)keyLength;
            }
        }