Esempio n. 1
0
        ///<summary>Reads an AMQP "table" definition from the reader.</summary>
        ///<remarks>
        /// Supports the AMQP 0-8/0-9 standard entry types S, I, D, T
        /// and F, as well as the QPid-0-8 specific b, d, f, l, s, t,
        /// x and V types and the AMQP 0-9-1 A type.
        ///</remarks>
        /// <returns>A <seealso cref="System.Collections.Generic.Dictionary{TKey,TValue}"/>.</returns>
        public static int ReadDictionary(ReadOnlySpan <byte> span, out Dictionary <string, object> valueDictionary)
        {
            long tableLength = NetworkOrderDeserializer.ReadUInt32(span);

            if (tableLength == 0)
            {
                valueDictionary = null;
                return(4);
            }

            span            = span.Slice(4);
            valueDictionary = new Dictionary <string, object>();
            int bytesRead = 0;

            while (bytesRead < tableLength)
            {
                bytesRead += ReadShortstr(span.Slice(bytesRead), out string key);
                object value = ReadFieldValue(span.Slice(bytesRead), out int valueBytesRead);
                bytesRead += valueBytesRead;

                valueDictionary[key] = value;
            }

            return(4 + bytesRead);
        }
Esempio n. 2
0
        public static decimal ReadDecimal(ReadOnlyMemory <byte> memory)
        {
            byte scale            = memory.Span[0];
            uint unsignedMantissa = NetworkOrderDeserializer.ReadUInt32(memory.Slice(1));

            return(AmqpToDecimal(scale, unsignedMantissa));
        }
        public static decimal ReadDecimal(ReadOnlySpan <byte> span)
        {
            byte scale            = span[0];
            uint unsignedMantissa = NetworkOrderDeserializer.ReadUInt32(span.Slice(1));

            return(AmqpToDecimal(scale, unsignedMantissa));
        }
Esempio n. 4
0
        public uint ReadLong()
        {
            uint result = NetworkOrderDeserializer.ReadUInt32(_memory.Slice(_memoryOffset));

            _memoryOffset += 4;
            return(result);
        }
Esempio n. 5
0
        public uint ReadLong()
        {
            uint result = NetworkOrderDeserializer.ReadUInt32(Span);

            _offset += 4;
            return(result);
        }
Esempio n. 6
0
        ///<summary>Reads an AMQP "table" definition from the reader.</summary>
        ///<remarks>
        /// Supports the AMQP 0-8/0-9 standard entry types S, I, D, T
        /// and F, as well as the QPid-0-8 specific b, d, f, l, s, t,
        /// x and V types and the AMQP 0-9-1 A type.
        ///</remarks>
        /// <returns>A <seealso cref="System.Collections.Generic.Dictionary{TKey,TValue}"/>.</returns>
        public static Dictionary <string, object> ReadTable(ReadOnlySpan <byte> span, out int bytesRead)
        {
            bytesRead = 4;
            long tableLength = NetworkOrderDeserializer.ReadUInt32(span);

            if (tableLength == 0)
            {
                return(null);
            }

            Dictionary <string, object> table = new Dictionary <string, object>();

            while ((bytesRead - 4) < tableLength)
            {
                string key = ReadShortstr(span.Slice(bytesRead), out int keyBytesRead);
                bytesRead += keyBytesRead;
                object value = ReadFieldValue(span.Slice(bytesRead), out int valueBytesRead);
                bytesRead += valueBytesRead;

                if (!table.ContainsKey(key))
                {
                    table[key] = value;
                }
            }

            return(table);
        }
        public uint ReadLong()
        {
            ClearBits();
            uint result = NetworkOrderDeserializer.ReadUInt32(_memory.Slice(_memoryOffset).Span);

            _memoryOffset += 4;
            return(result);
        }
        public static byte[] ReadLongstr(ReadOnlyMemory <byte> memory)
        {
            int byteCount = (int)NetworkOrderDeserializer.ReadUInt32(memory);

            if (byteCount > int.MaxValue)
            {
                throw new SyntaxError($"Long string too long; byte length={byteCount}, max={int.MaxValue}");
            }

            return(memory.Slice(4, byteCount).ToArray());
        }
Esempio n. 9
0
        public static byte[] ReadLongstr(ReadOnlySpan <byte> span)
        {
            uint byteCount = NetworkOrderDeserializer.ReadUInt32(span);

            if (byteCount > int.MaxValue)
            {
                throw new SyntaxErrorException($"Long string too long; byte length={byteCount}, max={int.MaxValue}");
            }

            return(span.Slice(4, (int)byteCount).ToArray());
        }
Esempio n. 10
0
        public static int ReadLongstr(ReadOnlySpan <byte> span, out byte[] value)
        {
            uint byteCount = NetworkOrderDeserializer.ReadUInt32(span);

            if (byteCount > int.MaxValue)
            {
                value = null;
                return(ThrowSyntaxErrorException(byteCount));
            }

            value = span.Slice(4, (int)byteCount).ToArray();
            return(4 + value.Length);
        }
Esempio n. 11
0
        public static decimal ReadDecimal(ReadOnlySpan <byte> span)
        {
            byte scale = span[0];

            if (scale > 28)
            {
                ThrowInvalidDecimalScale(scale);
            }

            uint unsignedMantissa = NetworkOrderDeserializer.ReadUInt32(span.Slice(1));
            var  data             = new DecimalData(((uint)(scale << 16)) | unsignedMantissa & 0x80000000, 0, unsignedMantissa & 0x7FFFFFFF, 0);

            return(Unsafe.As <DecimalData, decimal>(ref data));
        }
        public static IList ReadArray(ReadOnlyMemory <byte> memory, out int bytesRead)
        {
            IList array       = new List <object>();
            long  arrayLength = NetworkOrderDeserializer.ReadUInt32(memory);

            bytesRead = 4;
            while (bytesRead - 4 < arrayLength)
            {
                object value = ReadFieldValue(memory.Slice(bytesRead), out int fieldValueBytesRead);
                bytesRead += fieldValueBytesRead;
                array.Add(value);
            }

            return(array);
        }
Esempio n. 13
0
        public static IList ReadArray(ReadOnlySpan <byte> span, out int bytesRead)
        {
            bytesRead = 4;
            long arrayLength = NetworkOrderDeserializer.ReadUInt32(span);

            if (arrayLength == 0)
            {
                return(null);
            }
            List <object> array = new List <object>();

            while (bytesRead - 4 < arrayLength)
            {
                array.Add(ReadFieldValue(span.Slice(bytesRead), out int fieldValueBytesRead));
                bytesRead += fieldValueBytesRead;
            }

            return(array);
        }
        ///<summary>Reads an AMQP "table" definition from the reader.</summary>
        ///<remarks>
        /// Supports the AMQP 0-8/0-9 standard entry types S, I, D, T
        /// and F, as well as the QPid-0-8 specific b, d, f, l, s, t,
        /// x and V types and the AMQP 0-9-1 A type.
        ///</remarks>
        /// <returns>A <seealso cref="System.Collections.Generic.IDictionary{TKey,TValue}"/>.</returns>
        public static IDictionary <string, object> ReadTable(ReadOnlyMemory <byte> memory, out int bytesRead)
        {
            IDictionary <string, object> table = new Dictionary <string, object>();
            long tableLength = NetworkOrderDeserializer.ReadUInt32(memory);

            bytesRead = 4;
            while ((bytesRead - 4) < tableLength)
            {
                string key = ReadShortstr(memory.Slice(bytesRead), out int keyBytesRead);
                bytesRead += keyBytesRead;
                object value = ReadFieldValue(memory.Slice(bytesRead), out int valueBytesRead);
                bytesRead += valueBytesRead;

                if (!table.ContainsKey(key))
                {
                    table[key] = value;
                }
            }

            return(table);
        }
        public static decimal ReadDecimal(ReadOnlySpan <byte> span)
        {
            byte scale = span[0];

            if (scale > 28)
            {
                throw new SyntaxErrorException($"Unrepresentable AMQP decimal table field: scale={scale}");
            }

            uint unsignedMantissa = NetworkOrderDeserializer.ReadUInt32(span.Slice(1));

            return(new decimal(
                       // The low 32 bits of a 96-bit integer
                       lo: (int)(unsignedMantissa & 0x7FFFFFFF),
                       // The middle 32 bits of a 96-bit integer.
                       mid: 0,
                       // The high 32 bits of a 96-bit integer.
                       hi: 0,
                       isNegative: (unsignedMantissa & 0x80000000) != 0,
                       // A power of 10 ranging from 0 to 28.
                       scale: scale));
        }
 public void TestReadUInt32()
 {
     Assert.Equal(0x89ABCDEF, NetworkOrderDeserializer.ReadUInt32(new byte[] { 0x89, 0xAB, 0xCD, 0xEF }.AsSpan()));
 }
Esempio n. 17
0
        public static object ReadFieldValue(ReadOnlySpan <byte> span, out int bytesRead)
        {
            bytesRead = 1;
            switch ((char)span[0])
            {
            case 'S':
                bytesRead += ReadLongstr(span.Slice(1), out var bytes);
                return(bytes);

            case 'I':
                bytesRead += 4;
                return(NetworkOrderDeserializer.ReadInt32(span.Slice(1)));

            case 'i':
                bytesRead += 4;
                return(NetworkOrderDeserializer.ReadUInt32(span.Slice(1)));

            case 'D':
                bytesRead += 5;
                return(ReadDecimal(span.Slice(1)));

            case 'T':
                bytesRead += ReadTimestamp(span.Slice(1), out var timestamp);
                return(timestamp);

            case 'F':
                bytesRead += ReadDictionary(span.Slice(1), out var dictionary);
                return(dictionary);

            case 'A':
                IList arrayResult = ReadArray(span.Slice(1), out int arrayBytesRead);
                bytesRead += arrayBytesRead;
                return(arrayResult);

            case 'B':
                bytesRead += 1;
                return(span[1]);

            case 'b':
                bytesRead += 1;
                return((sbyte)span[1]);

            case 'd':
                bytesRead += 8;
                return(NetworkOrderDeserializer.ReadDouble(span.Slice(1)));

            case 'f':
                bytesRead += 4;
                return(NetworkOrderDeserializer.ReadSingle(span.Slice(1)));

            case 'l':
                bytesRead += 8;
                return(NetworkOrderDeserializer.ReadInt64(span.Slice(1)));

            case 's':
                bytesRead += 2;
                return(NetworkOrderDeserializer.ReadInt16(span.Slice(1)));

            case 't':
                bytesRead += 1;
                return(span[1] != 0);

            case 'x':
                bytesRead += ReadLongstr(span.Slice(1), out var binaryTableResult);
                return(new BinaryTableValue(binaryTableResult));

            case 'V':
                return(null);

            default:
                throw new SyntaxErrorException($"Unrecognised type in table: {(char)span[0]}");
            }
        }
        public static object ReadFieldValue(ReadOnlyMemory <byte> memory, out int bytesRead)
        {
            bytesRead = 1;
            ReadOnlyMemory <byte> slice = memory.Slice(1);

            switch ((char)memory.Span[0])
            {
            case 'S':
                byte[] result = ReadLongstr(slice);
                bytesRead += result.Length + 4;
                return(result);

            case 'I':
                bytesRead += 4;
                return(NetworkOrderDeserializer.ReadInt32(slice));

            case 'i':
                bytesRead += 4;
                return(NetworkOrderDeserializer.ReadUInt32(slice));

            case 'D':
                bytesRead += 5;
                return(ReadDecimal(slice));

            case 'T':
                bytesRead += 8;
                return(ReadTimestamp(slice));

            case 'F':
                IDictionary <string, object> tableResult = ReadTable(slice, out int tableBytesRead);
                bytesRead += tableBytesRead;
                return(tableResult);

            case 'A':
                IList arrayResult = ReadArray(slice, out int arrayBytesRead);
                bytesRead += arrayBytesRead;
                return(arrayResult);

            case 'B':
                bytesRead += 1;
                return(slice.Span[0]);

            case 'b':
                bytesRead += 1;
                return((sbyte)slice.Span[0]);

            case 'd':
                bytesRead += 8;
                return(NetworkOrderDeserializer.ReadDouble(slice));

            case 'f':
                bytesRead += 4;
                return(NetworkOrderDeserializer.ReadSingle(slice));

            case 'l':
                bytesRead += 8;
                return(NetworkOrderDeserializer.ReadInt64(slice));

            case 's':
                bytesRead += 2;
                return(NetworkOrderDeserializer.ReadInt16(slice));

            case 't':
                bytesRead += 1;
                return(slice.Span[0] != 0);

            case 'x':
                byte[] binaryTableResult = ReadLongstr(slice);
                bytesRead += binaryTableResult.Length + 4;
                return(new BinaryTableValue(binaryTableResult));

            case 'V':
                return(null);

            default:
                throw new SyntaxError($"Unrecognised type in table: {(char)memory.Span[0]}");
            }
        }
Esempio n. 19
0
 public static int ReadLong(ReadOnlySpan <byte> span, out uint value)
 {
     value = NetworkOrderDeserializer.ReadUInt32(span);
     return(4);
 }