Beispiel #1
0
        public double ReadDouble()
        {
            SkipSemanticTag();
            CborReaderHeader header = GetHeader();

            switch (header.MajorType)
            {
            case CborMajorType.PositiveInteger:
                return(ReadInteger());

            case CborMajorType.NegativeInteger:
                return(-1L - (long)ReadInteger());

            case CborMajorType.Primitive:
            {
                switch (header.Primitive)
                {
                case CborPrimitive.HalfFloat:
                    return((double)InternalReadHalf());

                case CborPrimitive.SingleFloat:
                    return(InternalReadSingle());

                case CborPrimitive.DoubleFloat:
                    return(InternalReadDouble());

                default:
                    throw new CborException($"Invalid primitive {header.Primitive}");
                }
            }

            default:
                throw new CborException($"Invalid major type {header.MajorType}");
            }
        }
Beispiel #2
0
        public void SkipDataItem()
        {
            CborReaderHeader header = GetHeader();

            switch (header.MajorType)
            {
            case CborMajorType.PositiveInteger:
            case CborMajorType.NegativeInteger:
                ReadInteger();
                break;

            case CborMajorType.ByteString:
            case CborMajorType.TextString:
                ReadSizeAndBytes();
                break;

            case CborMajorType.Array:
                SkipArray();
                break;

            case CborMajorType.Map:
                SkipMap();
                break;

            case CborMajorType.SemanticTag:
                _state = CborReaderState.Data;
                break;

            case CborMajorType.Primitive:
                switch (header.Primitive)
                {
                case CborPrimitive.False:
                case CborPrimitive.True:
                case CborPrimitive.Null:
                case CborPrimitive.Undefined:
                case CborPrimitive.SimpleValue:
                case CborPrimitive.Break:
                    _state = CborReaderState.Data;
                    break;

                case CborPrimitive.HalfFloat:
                    Advance(2);
                    break;

                case CborPrimitive.SingleFloat:
                    Advance(4);
                    break;

                case CborPrimitive.DoubleFloat:
                    Advance(8);
                    break;

                case CborPrimitive.DecimalFloat:
                    Advance(16);
                    break;
                }
                break;
            }
        }
Beispiel #3
0
 public CborReader(ReadOnlySpan <byte> buffer)
 {
     _buffer             = buffer;
     _currentPos         = 0;
     _state              = CborReaderState.Start;
     _header             = new CborReaderHeader();
     _remainingItemCount = 0;
 }
Beispiel #4
0
 public void ReturnToBookmark(CborReaderBookmark bookmark)
 {
     _buffer             = bookmark.buffer;
     _currentPos         = bookmark.currentPos;
     _state              = bookmark.state;
     _header             = bookmark.header;
     _remainingItemCount = bookmark.remainingItemCount;
 }
Beispiel #5
0
        private ulong ReadUnsigned(ulong maxValue)
        {
            CborReaderHeader header = GetHeader();

            switch (header.MajorType)
            {
            case CborMajorType.PositiveInteger:
                return(ReadInteger(maxValue));

            default:
                throw new CborException($"Invalid major type {header.MajorType}");
            }
        }
Beispiel #6
0
        private long ReadSigned(long maxValue)
        {
            CborReaderHeader header = GetHeader();

            switch (header.MajorType)
            {
            case CborMajorType.PositiveInteger:
                return((long)ReadInteger((ulong)maxValue));

            case CborMajorType.NegativeInteger:
                return(-1L - (long)ReadInteger((ulong)maxValue));

            default:
                throw new CborException($"Invalid major type {header.MajorType}");
            }
        }
Beispiel #7
0
        private ulong ReadInteger(ulong maxValue = ulong.MaxValue)
        {
            CborReaderHeader header = GetHeader();

            ulong value;

            switch (header.AdditionalValue)
            {
            // 8 bits
            case 24:
                value = ReadBytes(1)[0];
                break;

            // 16 bits
            case 25:
                value = BinaryPrimitives.ReadUInt16BigEndian(ReadBytes(2));
                break;

            // 32 bits
            case 26:
                value = BinaryPrimitives.ReadUInt32BigEndian(ReadBytes(4));
                break;

            // 64 bits
            case 27:
                value = BinaryPrimitives.ReadUInt64BigEndian(ReadBytes(8));
                break;

            case 28:
            case 29:
            case 30:
            case 31:
                throw BuildException($"Unexpected additional value {header.AdditionalValue}");

            default:
                value  = header.AdditionalValue;
                _state = CborReaderState.Data;
                break;
            }

            if (value > maxValue)
            {
                throw BuildException("Invalid signed integer");
            }

            return(value);
        }
Beispiel #8
0
        public CborDataItemType GetCurrentDataItemType()
        {
            SkipSemanticTag();
            CborReaderHeader header = GetHeader();

            switch (header.MajorType)
            {
            case CborMajorType.PositiveInteger:
                return(CborDataItemType.Unsigned);

            case CborMajorType.NegativeInteger:
                return(CborDataItemType.Signed);

            case CborMajorType.ByteString:
                return(CborDataItemType.ByteString);

            case CborMajorType.TextString:
                return(CborDataItemType.String);

            case CborMajorType.Array:
                return(CborDataItemType.Array);

            case CborMajorType.Map:
                return(CborDataItemType.Map);

            case CborMajorType.SemanticTag:
                Advance();
                return(GetCurrentDataItemType());

            case CborMajorType.Primitive:
                switch (header.Primitive)
                {
                case CborPrimitive.True:
                case CborPrimitive.False:
                    return(CborDataItemType.Boolean);

                case CborPrimitive.Null:
                case CborPrimitive.Undefined:
                    return(CborDataItemType.Null);

                case CborPrimitive.HalfFloat:
                case CborPrimitive.SingleFloat:
                    return(CborDataItemType.Single);

                case CborPrimitive.DoubleFloat:
                    return(CborDataItemType.Double);

                case CborPrimitive.DecimalFloat:
                    return(CborDataItemType.Decimal);

                case CborPrimitive.Break:
                    return(CborDataItemType.Break);

                default:
                    throw BuildException("Primitive not supported");
                }

            default:
                throw BuildException("Major type not supported");
            }
        }