Example #1
0
        public static bool TryReadHeader(NdrBuffer original, out RpcHeader header)
        {
            if (original == null)
            {
                throw new ArgumentNullException(nameof(original));
            }

            header = new RpcHeader();

            if (original.BytesAvailable < HeaderLength)
            {
                return(false);
            }

            var buffer = original.Read(HeaderLength);

            if (buffer.Length < HeaderLength)
            {
                return(false);
            }

            header.Version = buffer[0];

            if (header.Version != PROTOCOL_VERSION)
            {
                return(false);
            }

            header.Endian = (EndianType)buffer[1];

            if (header.Endian != EndianType.Big && header.Endian != EndianType.Little)
            {
                return(false);
            }

            header.CommonHeaderLength = BinaryPrimitives.ReadInt16LittleEndian(buffer.Slice(2, 2));

            if (header.CommonHeaderLength != COMMON_HEADER_BYTES)
            {
                return(false);
            }

            header.Filler = BinaryPrimitives.ReadInt32LittleEndian(buffer.Slice(4, 4));

            Debug.Assert(header.Filler == ExpectedFiller);

            header.ObjectBufferLength = BinaryPrimitives.ReadInt32LittleEndian(buffer.Slice(8, 4));

            Debug.Assert(header.ObjectBufferLength == original.BytesAvailable);

            header.ConstructedTypeFiller = BinaryPrimitives.ReadInt32LittleEndian(buffer.Slice(12, 4));

            Debug.Assert(header.ConstructedTypeFiller == 0);

            return(true);
        }
Example #2
0
        internal void MarshalObject(INdrStruct thing)
        {
            RpcHeader.WriteHeader(this);

            if (WriteDeferred(thing))
            {
                WriteStruct(thing);
            }

            var typeLength = Offset - RpcHeader.HeaderLength;

            BinaryPrimitives.WriteInt32LittleEndian(backingBuffer.Span.Slice(8, 4), typeLength);
        }
Example #3
0
        public void UnmarshalObject(INdrStruct thing)
        {
            if (!RpcHeader.TryReadHeader(this, out RpcHeader header))
            {
                throw new InvalidDataException("Expecting a header but got something unknown");
            }

            if (header.Endian != EndianType.Little)
            {
                throw new InvalidDataException("Expecting little endian encoding");
            }

            if (header.ObjectBufferLength > BytesAvailable)
            {
                throw new InvalidDataException($"Expected length ({header.ObjectBufferLength} bytes) is greater than available bytes ({BytesAvailable} bytes)");
            }

            ReadReferentStruct(thing);
        }