Exemple #1
0
        internal CborInitialByte(CborMajorType majorType, CborAdditionalInfo additionalInfo)
        {
            Debug.Assert((byte)majorType < 8, "CBOR Major Type is out of range");
            Debug.Assert((byte)additionalInfo < 32, "CBOR initial byte additional info is out of range");

            InitialByte = (byte)(((byte)majorType << 5) | (byte)additionalInfo);
        }
 private void WriteUnsignedInteger(CborMajorType type, ulong value)
 {
     if (value < (byte)CborAdditionalInfo.Additional8BitData)
     {
         EnsureWriteCapacity(1);
         WriteInitialByte(new CborInitialByte(type, (CborAdditionalInfo)value));
     }
     else if (value <= byte.MaxValue)
     {
         EnsureWriteCapacity(1 + sizeof(byte));
         WriteInitialByte(new CborInitialByte(type, CborAdditionalInfo.Additional8BitData));
         _buffer[_offset++] = (byte)value;
     }
     else if (value <= ushort.MaxValue)
     {
         EnsureWriteCapacity(1 + sizeof(ushort));
         WriteInitialByte(new CborInitialByte(type, CborAdditionalInfo.Additional16BitData));
         BinaryPrimitives.WriteUInt16BigEndian(_buffer.AsSpan(_offset), (ushort)value);
         _offset += sizeof(ushort);
     }
     else if (value <= uint.MaxValue)
     {
         EnsureWriteCapacity(1 + sizeof(uint));
         WriteInitialByte(new CborInitialByte(type, CborAdditionalInfo.Additional32BitData));
         BinaryPrimitives.WriteUInt32BigEndian(_buffer.AsSpan(_offset), (uint)value);
         _offset += sizeof(uint);
     }
     else
     {
         EnsureWriteCapacity(1 + sizeof(ulong));
         WriteInitialByte(new CborInitialByte(type, CborAdditionalInfo.Additional64BitData));
         BinaryPrimitives.WriteUInt64BigEndian(_buffer.AsSpan(_offset), value);
         _offset += sizeof(ulong);
     }
 }
Exemple #3
0
 private void WriteInteger(CborMajorType majorType, ulong value)
 {
     if (value <= 23)
     {
         WriteHeader(majorType, (byte)value);
     }
     else if (value <= byte.MaxValue)
     {
         WriteHeader(majorType, 24);
         WriteRawByte((byte)value);
     }
     else if (value <= ushort.MaxValue)
     {
         WriteHeader(majorType, 25);
         Span <byte> bytes = _bufferWriter.GetSpan(2);
         BinaryPrimitives.WriteUInt16BigEndian(bytes, (ushort)value);
         _bufferWriter.Advance(2);
     }
     else if (value <= uint.MaxValue)
     {
         WriteHeader(majorType, 26);
         Span <byte> bytes = _bufferWriter.GetSpan(4);
         BinaryPrimitives.WriteUInt32BigEndian(bytes, (uint)value);
         _bufferWriter.Advance(4);
     }
     else
     {
         WriteHeader(majorType, 27);
         Span <byte> bytes = _bufferWriter.GetSpan(8);
         BinaryPrimitives.WriteUInt64BigEndian(bytes, value);
         _bufferWriter.Advance(8);
     }
 }
Exemple #4
0
        internal static void Peek_SingleByteBuffer_ShouldReturnExpectedState(CborMajorType majorType, CborReaderState expectedResult)
        {
            ReadOnlyMemory <byte> buffer = new byte[] { (byte)((byte)majorType << 5) };
            var reader = new CborReader(buffer);

            Assert.Equal(expectedResult, reader.PeekState());
        }
Exemple #5
0
 private void Expect(CborMajorType majorType)
 {
     if (!Accept(majorType))
     {
         throw BuildException($"Expected major type {majorType} ({(byte)majorType})");
     }
 }
Exemple #6
0
        private void Encode(CborMajorType majorType, byte[] bytes)
        {
            byte ib = (byte)majorType;

            if (bytes.Length == 1)
            {
                _writer.Write((byte)(ib + 24));
            }
            else if (bytes.Length == 2)
            {
                _writer.Write((byte)(ib + 25));
            }
            else if (bytes.Length == 4)
            {
                _writer.Write((byte)(ib + 26));
            }
            else if (bytes.Length == 8)
            {
                _writer.Write((byte)(ib + 27));
            }
            else
            {
                throw new InvalidOperationException("Can not encode non primiative bytes");
            }

            if (BitConverter.IsLittleEndian)
            {
                _writer.Write(bytes.Reverse().ToArray());
            }
            else
            {
                _writer.Write(bytes);
            }
        }
Exemple #7
0
        private void EnqueueValue(CborMajorType majorType, ulong value, bool count = true)
        {
            if (value < 24)
            {
                _values.Enqueue(new CborSimpleToken(majorType, (byte)value));
            }
            else
            {
                byte[] bytes;
                if (value <= byte.MaxValue)
                {
                    bytes = new[] { (byte)value }
                }
                ;
                else if (value <= ushort.MaxValue)
                {
                    bytes = BitConverter.GetBytes((ushort)value);
                }
                else if (value <= uint.MaxValue)
                {
                    bytes = BitConverter.GetBytes((uint)value);
                }
                else
                {
                    bytes = BitConverter.GetBytes(value);
                }

                _values.Enqueue(new CborTokenValue(majorType, bytes));
            }
            if (count)
            {
                CountItem();
            }
        }
Exemple #8
0
        internal static void WriteByteStringLength(IncrementalHash hasher, ulong value)
        {
            const CborMajorType MajorType = CborMajorType.ByteString;
            CborInitialByte     initialByte;

            if (value < (byte)CborAdditionalInfo.Additional8BitData)
            {
                initialByte = new CborInitialByte(MajorType, (CborAdditionalInfo)value);
                hasher.AppendData(stackalloc byte[] { initialByte.InitialByte });
Exemple #9
0
 private void WriteSize(CborMajorType majorType, int size)
 {
     if (size >= 0)
     {
         WriteInteger(majorType, (ulong)size);
     }
     else
     {
         WriteHeader(majorType, INDEFINITE_LENGTH);
     }
 }
Exemple #10
0
        private CborMajorType GetCurrentMajorType()
        {
            ExpectLength(1);

            CborMajorType majorType = (CborMajorType)((_buffer[0] >> 5) & 0x07);

            if (majorType > CborMajorType.Max)
            {
                throw new CborException($"Invalid major type {majorType}");
            }

            return(majorType);
        }
Exemple #11
0
        public CborInitialByte Peek(CborMajorType expectedType)
        {
            if (_buffer.IsEmpty)
            {
                throw new InvalidOperationException("end of buffer");
            }

            var result = new CborInitialByte(_buffer[0]);

            if (expectedType != result.MajorType)
            {
                throw new InvalidOperationException("Data item major type mismatch.");
            }

            return(result);
        }
Exemple #12
0
        // Unsigned integer encoding https://tools.ietf.org/html/rfc7049#section-2.1
        private void WriteUnsignedInteger(CborMajorType type, ulong value)
        {
            EnsureCanWriteNewDataItem();

            if (value < 24)
            {
                EnsureWriteCapacity(1);
                _buffer[_offset++] = new CborInitialByte(type, (CborAdditionalInfo)value).InitialByte;
            }
            else if (value <= byte.MaxValue)
            {
                EnsureWriteCapacity(2);
                _buffer[_offset]     = new CborInitialByte(type, CborAdditionalInfo.UnsignedInteger8BitEncoding).InitialByte;
                _buffer[_offset + 1] = (byte)value;
                _offset += 2;
            }
            else if (value <= ushort.MaxValue)
            {
                EnsureWriteCapacity(3);
                _buffer[_offset] = new CborInitialByte(type, CborAdditionalInfo.UnsignedInteger16BitEncoding).InitialByte;
                BinaryPrimitives.WriteUInt16BigEndian(_buffer.AsSpan(_offset + 1), (ushort)value);
                _offset += 3;
            }
            else if (value <= uint.MaxValue)
            {
                EnsureWriteCapacity(5);
                _buffer[_offset] = new CborInitialByte(type, CborAdditionalInfo.UnsignedInteger32BitEncoding).InitialByte;
                BinaryPrimitives.WriteUInt32BigEndian(_buffer.AsSpan(_offset + 1), (uint)value);
                _offset += 5;
            }
            else
            {
                EnsureWriteCapacity(9);
                _buffer[_offset] = new CborInitialByte(type, CborAdditionalInfo.UnsignedInteger64BitEncoding).InitialByte;
                BinaryPrimitives.WriteUInt64BigEndian(_buffer.AsSpan(_offset + 1), value);
                _offset += 9;
            }

            _remainingDataItems--;
        }
        // perform an in-place conversion of an indefinite-length encoding into an equivalent definite-length
        private void PatchIndefiniteLengthCollection(CborMajorType majorType, int count)
        {
            Debug.Assert(majorType == CborMajorType.Array || majorType == CborMajorType.Map);

            int currentOffset = _offset;
            int bytesToShift  = GetIntegerEncodingLength((ulong)count) - 1;

            if (bytesToShift > 0)
            {
                // length encoding requires more than 1 byte, need to shift encoded elements to the right
                EnsureWriteCapacity(bytesToShift);

                ReadOnlySpan <byte> elementEncoding = _buffer.AsSpan(_frameOffset, currentOffset - _frameOffset);
                Span <byte>         target          = _buffer.AsSpan(_frameOffset + bytesToShift, currentOffset - _frameOffset);
                elementEncoding.CopyTo(target);
            }

            // rewind to the start of the collection and write a new initial byte
            _offset = _frameOffset - 1;
            WriteUnsignedInteger(majorType, (ulong)count);
            _offset = currentOffset + bytesToShift;
        }
Exemple #14
0
 private void Encode(CborMajorType majorType, UInt64 value)
 {
     if (value < 24)
     {
         EncodeSimpleType(majorType, (byte)value);
     }
     else if (value <= byte.MaxValue)
     {
         Encode(majorType, new byte[] { (byte)value });
     }
     else if (value <= ushort.MaxValue)
     {
         Encode(majorType, BitConverter.GetBytes((ushort)value));
     }
     else if (value <= uint.MaxValue)
     {
         Encode(majorType, BitConverter.GetBytes((uint)value));
     }
     else
     {
         Encode(majorType, BitConverter.GetBytes(value));
     }
 }
Exemple #15
0
        private void WriteHeader(CborMajorType majorType, byte additionalValue)
        {
            byte header = (byte)(((byte)majorType) << 5 | (additionalValue & 0x1f));

            WriteRawByte(header);
        }
Exemple #16
0
 public CborSimpleToken(CborMajorType majorType, byte simpleType)
     : base(majorType)
 {
     SimpleType = (CborSimpleType)simpleType;
 }
Exemple #17
0
 public CborSimpleToken(CborMajorType majorType, CborSimpleType simpleType)
     : base(majorType)
 {
     SimpleType = simpleType;
 }
Exemple #18
0
 public CborTokenValue(CborMajorType majorType, byte[] value)
     : base(majorType)
 {
     Value = value;
 }
Exemple #19
0
 public CborToken(CborMajorType majorType)
 {
     MajorType = majorType;
 }
 public ContainerContext(CborMajorType majorType)
 {
     MajorType = majorType;
 }
Exemple #21
0
 private void EncodeSimpleType(CborMajorType majorType, byte value)
 {
     _writer.Write((byte)((byte)majorType + (value & 0x1F)));
 }
Exemple #22
0
 private bool Accept(CborMajorType majorType)
 {
     return(GetHeader().MajorType == majorType);
 }
Exemple #23
0
 public CborCollectionToken(CborMajorType majorType)
     : base(majorType)
 {
 }