예제 #1
0
        internal void WriteJaggedArray(NameInfo memberNameInfo, NameInfo arrayNameInfo, WriteObjectInfo objectInfo, NameInfo arrayElemTypeNameInfo, int length, int lowerBound)
        {
            InternalWriteItemNull();
            BinaryArrayTypeEnum binaryArrayTypeEnum;
            var lengthA = new int[1];

            lengthA[0] = length;
            int[]  lowerBoundA     = null;
            object typeInformation = null;
            int    assemId         = 0;

            if (lowerBound == 0)
            {
                binaryArrayTypeEnum = BinaryArrayTypeEnum.Jagged;
            }
            else
            {
                binaryArrayTypeEnum = BinaryArrayTypeEnum.JaggedOffset;
                lowerBoundA         = new int[1];
                lowerBoundA[0]      = lowerBound;
            }

            BinaryTypeEnum binaryTypeEnum = BinaryTypeConverter.GetBinaryTypeInfo(arrayElemTypeNameInfo._type, objectInfo, arrayElemTypeNameInfo.NIname, _objectWriter, out typeInformation, out assemId);

            if (_binaryArray == null)
            {
                _binaryArray = new BinaryArray();
            }
            _binaryArray.Set((int)arrayNameInfo._objectId, 1, lengthA, lowerBoundA, binaryTypeEnum, typeInformation, binaryArrayTypeEnum, assemId);

            _binaryArray.Write(this);
        }
예제 #2
0
        internal void WriteRectangleArray(NameInfo memberNameInfo, NameInfo arrayNameInfo, WriteObjectInfo objectInfo, NameInfo arrayElemTypeNameInfo, int rank, int[] lengthA, int[] lowerBoundA)
        {
            InternalWriteItemNull();

            BinaryArrayTypeEnum binaryArrayTypeEnum = BinaryArrayTypeEnum.Rectangular;
            object         typeInformation          = null;
            int            assemId        = 0;
            BinaryTypeEnum binaryTypeEnum = BinaryTypeConverter.GetBinaryTypeInfo(arrayElemTypeNameInfo._type, objectInfo, arrayElemTypeNameInfo.NIname, _objectWriter, out typeInformation, out assemId);

            if (_binaryArray == null)
            {
                _binaryArray = new BinaryArray();
            }

            for (int i = 0; i < rank; i++)
            {
                if (lowerBoundA[i] != 0)
                {
                    binaryArrayTypeEnum = BinaryArrayTypeEnum.RectangularOffset;
                    break;
                }
            }

            _binaryArray.Set((int)arrayNameInfo._objectId, rank, lengthA, lowerBoundA, binaryTypeEnum, typeInformation, binaryArrayTypeEnum, assemId);
            _binaryArray.Write(this);
        }
예제 #3
0
        public void Read(BinaryParser input)
        {
            // binaryHeaderEnum has already been read
            _objectId         = input.ReadInt32();
            _name             = input.ReadString();
            _numMembers       = input.ReadInt32();
            _memberNames      = new string[_numMembers];
            _binaryTypeEnumA  = new BinaryTypeEnum[_numMembers];
            _typeInformationA = new object[_numMembers];
            _memberAssemIds   = new int[_numMembers];
            for (int i = 0; i < _numMembers; i++)
            {
                _memberNames[i] = input.ReadString();
            }
            for (int i = 0; i < _numMembers; i++)
            {
                _binaryTypeEnumA[i] = (BinaryTypeEnum)input.ReadByte();
            }
            for (int i = 0; i < _numMembers; i++)
            {
                if (_binaryTypeEnumA[i] != BinaryTypeEnum.ObjectUrt && _binaryTypeEnumA[i] != BinaryTypeEnum.ObjectUser)
                {
                    _typeInformationA[i] = BinaryTypeConverter.ReadTypeInfo(_binaryTypeEnumA[i], input, out _memberAssemIds[i]);
                }
                else
                {
                    BinaryTypeConverter.ReadTypeInfo(_binaryTypeEnumA[i], input, out _memberAssemIds[i]);
                }
            }

            if (_binaryHeaderEnum == BinaryHeaderEnum.ObjectWithMapTypedAssemId)
            {
                _assemId = input.ReadInt32();
            }
        }
예제 #4
0
        public void Write(BinaryFormatterWriter output)
        {
            Debug.Assert(_name != null && _memberNames != null && _binaryTypeEnumA != null && _typeInformationA != null && _memberAssemIds != null);
            output.WriteByte((byte)_binaryHeaderEnum);
            output.WriteInt32(_objectId);
            output.WriteString(_name);

            output.WriteInt32(_numMembers);
            for (int i = 0; i < _numMembers; i++)
            {
                output.WriteString(_memberNames[i]);
            }
            for (int i = 0; i < _numMembers; i++)
            {
                output.WriteByte((byte)_binaryTypeEnumA[i]);
            }
            for (int i = 0; i < _numMembers; i++)
            {
                BinaryTypeConverter.WriteTypeInfo(_binaryTypeEnumA[i], _typeInformationA[i], _memberAssemIds[i], output);
            }

            if (_assemId > 0)
            {
                output.WriteInt32(_assemId);
            }
        }
예제 #5
0
파일: ObjectMap.cs 프로젝트: z77ma/runtime
        internal ObjectMap(string objectName, string[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, object?[] typeInformationA, int[] memberAssemIds, ObjectReader objectReader, int objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
        {
            _objectName       = objectName;
            _memberNames      = memberNames;
            _binaryTypeEnumA  = binaryTypeEnumA;
            _typeInformationA = typeInformationA;
            _objectReader     = objectReader;
            _objectId         = objectId;
            _assemblyInfo     = assemblyInfo;

            if (assemblyInfo == null)
            {
                throw new SerializationException(SR.Format(SR.Serialization_Assembly, objectName));
            }

            _objectType  = objectReader.GetType(assemblyInfo, objectName);
            _memberTypes = new Type[memberNames.Length];

            for (int i = 0; i < memberNames.Length; i++)
            {
                BinaryTypeConverter.TypeFromInfo(
                    binaryTypeEnumA[i], typeInformationA[i], objectReader, (BinaryAssemblyInfo?)assemIdToAssemblyTable[memberAssemIds[i]],
                    out _, out _, out Type? type, out _);
                _memberTypes[i] = type;
            }

            _objectInfo = objectReader.CreateReadObjectInfo(_objectType, memberNames, null);
            if (!_objectInfo._isSi)
            {
                _objectInfo.GetMemberTypes(memberNames, _objectInfo._objectType);  // Check version match
            }
        }
예제 #6
0
        internal void WriteSingleArray(NameInfo memberNameInfo, NameInfo arrayNameInfo, WriteObjectInfo objectInfo, NameInfo arrayElemTypeNameInfo, int length, int lowerBound, Array array)
        {
            InternalWriteItemNull();
            BinaryArrayTypeEnum binaryArrayTypeEnum;
            var lengthA = new int[1];

            lengthA[0] = length;
            int[]  lowerBoundA     = null;
            object typeInformation = null;

            if (lowerBound == 0)
            {
                binaryArrayTypeEnum = BinaryArrayTypeEnum.Single;
            }
            else
            {
                binaryArrayTypeEnum = BinaryArrayTypeEnum.SingleOffset;
                lowerBoundA         = new int[1];
                lowerBoundA[0]      = lowerBound;
            }

            int            assemId;
            BinaryTypeEnum binaryTypeEnum = BinaryTypeConverter.GetBinaryTypeInfo(
                arrayElemTypeNameInfo._type, objectInfo, arrayElemTypeNameInfo.NIname, _objectWriter, out typeInformation, out assemId);

            if (_binaryArray == null)
            {
                _binaryArray = new BinaryArray();
            }
            _binaryArray.Set((int)arrayNameInfo._objectId, 1, lengthA, lowerBoundA, binaryTypeEnum, typeInformation, binaryArrayTypeEnum, assemId);

            _binaryArray.Write(this);

            if (Converter.IsWriteAsByteArray(arrayElemTypeNameInfo._primitiveTypeEnum) && (lowerBound == 0))
            {
                //array is written out as an array of bytes
                if (arrayElemTypeNameInfo._primitiveTypeEnum == InternalPrimitiveTypeE.Byte)
                {
                    WriteBytes((byte[])array);
                }
                else if (arrayElemTypeNameInfo._primitiveTypeEnum == InternalPrimitiveTypeE.Char)
                {
                    WriteChars((char[])array);
                }
                else
                {
                    WriteArrayAsBytes(array, Converter.TypeLength(arrayElemTypeNameInfo._primitiveTypeEnum));
                }
            }
        }
예제 #7
0
        public void Write(BinaryFormatterWriter output)
        {
            Debug.Assert(_lengthA != null);
            switch (_binaryHeaderEnum)
            {
            case BinaryHeaderEnum.ArraySinglePrimitive:
                output.WriteByte((byte)_binaryHeaderEnum);
                output.WriteInt32(_objectId);
                output.WriteInt32(_lengthA[0]);
                output.WriteByte((byte)((InternalPrimitiveTypeE)_typeInformation !));
                break;

            case BinaryHeaderEnum.ArraySingleString:
                output.WriteByte((byte)_binaryHeaderEnum);
                output.WriteInt32(_objectId);
                output.WriteInt32(_lengthA[0]);
                break;

            case BinaryHeaderEnum.ArraySingleObject:
                output.WriteByte((byte)_binaryHeaderEnum);
                output.WriteInt32(_objectId);
                output.WriteInt32(_lengthA[0]);
                break;

            default:
                output.WriteByte((byte)_binaryHeaderEnum);
                output.WriteInt32(_objectId);
                output.WriteByte((byte)_binaryArrayTypeEnum);
                output.WriteInt32(_rank);
                for (int i = 0; i < _rank; i++)
                {
                    output.WriteInt32(_lengthA[i]);
                }
                if ((_binaryArrayTypeEnum == BinaryArrayTypeEnum.SingleOffset) ||
                    (_binaryArrayTypeEnum == BinaryArrayTypeEnum.JaggedOffset) ||
                    (_binaryArrayTypeEnum == BinaryArrayTypeEnum.RectangularOffset))
                {
                    Debug.Assert(_lowerBoundA != null);
                    for (int i = 0; i < _rank; i++)
                    {
                        output.WriteInt32(_lowerBoundA[i]);
                    }
                }
                output.WriteByte((byte)_binaryTypeEnum);
                BinaryTypeConverter.WriteTypeInfo(_binaryTypeEnum, _typeInformation, _assemId, output);
                break;
            }
        }
예제 #8
0
        internal void WriteObject(NameInfo nameInfo, NameInfo typeNameInfo, int numMembers, string[] memberNames, Type[] memberTypes, WriteObjectInfo[] memberObjectInfos)
        {
            InternalWriteItemNull();
            int assemId;
            int objectId = (int)nameInfo._objectId;

            string objectName = objectId < 0 ?
                                objectName = typeNameInfo.NIname :         // Nested Object
                                             objectName = nameInfo.NIname; // Non-Nested

            if (_objectMapTable == null)
            {
                _objectMapTable = new Dictionary <string, ObjectMapInfo>();
            }

            ObjectMapInfo objectMapInfo;

            if (_objectMapTable.TryGetValue(objectName, out objectMapInfo) &&
                objectMapInfo.IsCompatible(numMembers, memberNames, memberTypes))
            {
                // Object
                if (_binaryObject == null)
                {
                    _binaryObject = new BinaryObject();
                }

                _binaryObject.Set(objectId, objectMapInfo._objectId);
                _binaryObject.Write(this);
            }
            else if (!typeNameInfo._transmitTypeOnObject)
            {
                // ObjectWithMap
                if (_binaryObjectWithMap == null)
                {
                    _binaryObjectWithMap = new BinaryObjectWithMap();
                }

                // BCL types are not placed into table
                assemId = (int)typeNameInfo._assemId;
                _binaryObjectWithMap.Set(objectId, objectName, numMembers, memberNames, assemId);

                _binaryObjectWithMap.Write(this);
                if (objectMapInfo == null)
                {
                    _objectMapTable.Add(objectName, new ObjectMapInfo(objectId, numMembers, memberNames, memberTypes));
                }
            }
            else
            {
                // ObjectWithMapTyped
                var binaryTypeEnumA  = new BinaryTypeEnum[numMembers];
                var typeInformationA = new object[numMembers];
                var assemIdA         = new int[numMembers];
                for (int i = 0; i < numMembers; i++)
                {
                    object typeInformation = null;
                    binaryTypeEnumA[i]  = BinaryTypeConverter.GetBinaryTypeInfo(memberTypes[i], memberObjectInfos[i], null, _objectWriter, out typeInformation, out assemId);
                    typeInformationA[i] = typeInformation;
                    assemIdA[i]         = assemId;
                }

                if (_binaryObjectWithMapTyped == null)
                {
                    _binaryObjectWithMapTyped = new BinaryObjectWithMapTyped();
                }

                // BCL types are not placed in table
                assemId = (int)typeNameInfo._assemId;
                _binaryObjectWithMapTyped.Set(objectId, objectName, numMembers, memberNames, binaryTypeEnumA, typeInformationA, assemIdA, assemId);
                _binaryObjectWithMapTyped.Write(this);
                if (objectMapInfo == null)
                {
                    _objectMapTable.Add(objectName, new ObjectMapInfo(objectId, numMembers, memberNames, memberTypes));
                }
            }
        }
예제 #9
0
        public void Read(BinaryParser input)
        {
            switch (_binaryHeaderEnum)
            {
            case BinaryHeaderEnum.ArraySinglePrimitive:
                _objectId            = input.ReadInt32();
                _lengthA             = new int[1];
                _lengthA[0]          = input.ReadInt32();
                _binaryArrayTypeEnum = BinaryArrayTypeEnum.Single;
                _rank            = 1;
                _lowerBoundA     = new int[_rank];
                _binaryTypeEnum  = BinaryTypeEnum.Primitive;
                _typeInformation = (InternalPrimitiveTypeE)input.ReadByte();
                break;

            case BinaryHeaderEnum.ArraySingleString:
                _objectId            = input.ReadInt32();
                _lengthA             = new int[1];
                _lengthA[0]          = input.ReadInt32();
                _binaryArrayTypeEnum = BinaryArrayTypeEnum.Single;
                _rank            = 1;
                _lowerBoundA     = new int[_rank];
                _binaryTypeEnum  = BinaryTypeEnum.String;
                _typeInformation = null;
                break;

            case BinaryHeaderEnum.ArraySingleObject:
                _objectId            = input.ReadInt32();
                _lengthA             = new int[1];
                _lengthA[0]          = input.ReadInt32();
                _binaryArrayTypeEnum = BinaryArrayTypeEnum.Single;
                _rank            = 1;
                _lowerBoundA     = new int[_rank];
                _binaryTypeEnum  = BinaryTypeEnum.Object;
                _typeInformation = null;
                break;

            default:
                _objectId            = input.ReadInt32();
                _binaryArrayTypeEnum = (BinaryArrayTypeEnum)input.ReadByte();
                _rank        = input.ReadInt32();
                _lengthA     = new int[_rank];
                _lowerBoundA = new int[_rank];
                for (int i = 0; i < _rank; i++)
                {
                    _lengthA[i] = input.ReadInt32();
                }
                if ((_binaryArrayTypeEnum == BinaryArrayTypeEnum.SingleOffset) ||
                    (_binaryArrayTypeEnum == BinaryArrayTypeEnum.JaggedOffset) ||
                    (_binaryArrayTypeEnum == BinaryArrayTypeEnum.RectangularOffset))
                {
                    for (int i = 0; i < _rank; i++)
                    {
                        _lowerBoundA[i] = input.ReadInt32();
                    }
                }
                _binaryTypeEnum  = (BinaryTypeEnum)input.ReadByte();
                _typeInformation = BinaryTypeConverter.ReadTypeInfo(_binaryTypeEnum, input, out _assemId);
                break;
            }
        }
예제 #10
0
        private void ReadArray(BinaryHeaderEnum binaryHeaderEnum)
        {
            BinaryAssemblyInfo assemblyInfo = null;
            BinaryArray        record       = new BinaryArray(binaryHeaderEnum);

            record.Read(this);

            if (record._binaryTypeEnum == BinaryTypeEnum.ObjectUser)
            {
                if (record._assemId < 1)
                {
                    throw new SerializationException(SR.Format(SR.Serialization_AssemblyId, record._typeInformation));
                }
                assemblyInfo = (BinaryAssemblyInfo)AssemIdToAssemblyTable[record._assemId];
            }
            else
            {
                assemblyInfo = SystemAssemblyInfo; //Urt assembly
            }

            ObjectProgress op = GetOp();
            ParseRecord    pr = op._pr;

            op._objectTypeEnum  = InternalObjectTypeE.Array;
            op._binaryTypeEnum  = record._binaryTypeEnum;
            op._typeInformation = record._typeInformation;

            ObjectProgress objectOp = (ObjectProgress)_stack.PeekPeek();

            if ((objectOp == null) || (record._objectId > 0))
            {
                // Non-Nested Object
                op._name            = "System.Array";
                pr._parseTypeEnum   = InternalParseTypeE.Object;
                op._memberValueEnum = InternalMemberValueE.Empty;
            }
            else
            {
                // Nested Object
                pr._parseTypeEnum   = InternalParseTypeE.Member;
                pr._memberValueEnum = InternalMemberValueE.Nested;
                op._memberValueEnum = InternalMemberValueE.Nested;

                switch (objectOp._objectTypeEnum)
                {
                case InternalObjectTypeE.Object:
                    pr._name           = objectOp._name;
                    pr._memberTypeEnum = InternalMemberTypeE.Field;
                    op._memberTypeEnum = InternalMemberTypeE.Field;
                    pr._keyDt          = objectOp._name;
                    pr._dtType         = objectOp._dtType;
                    break;

                case InternalObjectTypeE.Array:
                    pr._memberTypeEnum = InternalMemberTypeE.Item;
                    op._memberTypeEnum = InternalMemberTypeE.Item;
                    break;

                default:
                    throw new SerializationException(SR.Format(SR.Serialization_ObjectTypeEnum, objectOp._objectTypeEnum.ToString()));
                }
            }

            pr._objectId = _objectReader.GetId(record._objectId);
            if (pr._objectId == _topId)
            {
                pr._objectPositionEnum = InternalObjectPositionE.Top;
            }
            else if ((_headerId > 0) && (pr._objectId == _headerId))
            {
                pr._objectPositionEnum = InternalObjectPositionE.Headers; // Headers are an array of header objects
            }
            else
            {
                pr._objectPositionEnum = InternalObjectPositionE.Child;
            }

            pr._objectTypeEnum = InternalObjectTypeE.Array;

            BinaryTypeConverter.TypeFromInfo(record._binaryTypeEnum, record._typeInformation, _objectReader, assemblyInfo,
                                             out pr._arrayElementTypeCode, out pr._arrayElementTypeString,
                                             out pr._arrayElementType, out pr._isArrayVariant);

            pr._dtTypeCode = InternalPrimitiveTypeE.Invalid;

            pr._rank        = record._rank;
            pr._lengthA     = record._lengthA;
            pr._lowerBoundA = record._lowerBoundA;
            bool isPrimitiveArray = false;

            switch (record._binaryArrayTypeEnum)
            {
            case BinaryArrayTypeEnum.Single:
            case BinaryArrayTypeEnum.SingleOffset:
                op._numItems      = record._lengthA[0];
                pr._arrayTypeEnum = InternalArrayTypeE.Single;
                if (Converter.IsWriteAsByteArray(pr._arrayElementTypeCode) &&
                    (record._lowerBoundA[0] == 0))
                {
                    isPrimitiveArray = true;
                    ReadArrayAsBytes(pr);
                }
                break;

            case BinaryArrayTypeEnum.Jagged:
            case BinaryArrayTypeEnum.JaggedOffset:
                op._numItems      = record._lengthA[0];
                pr._arrayTypeEnum = InternalArrayTypeE.Jagged;
                break;

            case BinaryArrayTypeEnum.Rectangular:
            case BinaryArrayTypeEnum.RectangularOffset:
                int arrayLength = 1;
                for (int i = 0; i < record._rank; i++)
                {
                    arrayLength = arrayLength * record._lengthA[i];
                }
                op._numItems      = arrayLength;
                pr._arrayTypeEnum = InternalArrayTypeE.Rectangular;
                break;

            default:
                throw new SerializationException(SR.Format(SR.Serialization_ArrayType, record._binaryArrayTypeEnum.ToString()));
            }

            if (!isPrimitiveArray)
            {
                _stack.Push(op);
            }
            else
            {
                PutOp(op);
            }

            _objectReader.Parse(pr);

            if (isPrimitiveArray)
            {
                pr._parseTypeEnum = InternalParseTypeE.ObjectEnd;
                _objectReader.Parse(pr);
            }
        }
예제 #11
0
        internal void WriteObject(NameInfo nameInfo, NameInfo?typeNameInfo, int numMembers, string[] memberNames, Type[] memberTypes, WriteObjectInfo[] memberObjectInfos)
        {
            InternalWriteItemNull();
            int assemId;
            int objectId = (int)nameInfo._objectId;

            Debug.Assert(typeNameInfo != null);       // Explicitly called with null. Potential bug, but closed as Won't Fix: https://github.com/dotnet/runtime/issues/31402
            string?objectName = objectId < 0 ?
                                typeNameInfo.NIname : // Nested Object
                                nameInfo.NIname;      // Non-Nested

            if (_objectMapTable == null)
            {
                _objectMapTable = new Dictionary <string, ObjectMapInfo>();
            }

            Debug.Assert(objectName != null);
            if (_objectMapTable.TryGetValue(objectName, out ObjectMapInfo? objectMapInfo) &&
                objectMapInfo.IsCompatible(numMembers, memberNames, memberTypes))
            {
                // Object
                if (_binaryObject == null)
                {
                    _binaryObject = new BinaryObject();
                }

                _binaryObject.Set(objectId, objectMapInfo._objectId);
                _binaryObject.Write(this);
            }
            else if (!typeNameInfo._transmitTypeOnObject)
            {
                // ObjectWithMap
                if (_binaryObjectWithMap == null)
                {
                    _binaryObjectWithMap = new BinaryObjectWithMap();
                }

                // BCL types are not placed into table
                assemId = (int)typeNameInfo._assemId;
                _binaryObjectWithMap.Set(objectId, objectName, numMembers, memberNames, assemId);

                _binaryObjectWithMap.Write(this);
                if (objectMapInfo == null)
                {
                    _objectMapTable.Add(objectName, new ObjectMapInfo(objectId, numMembers, memberNames, memberTypes));
                }
            }
            else
            {
                // ObjectWithMapTyped
                var binaryTypeEnumA  = new BinaryTypeEnum[numMembers];
                var typeInformationA = new object?[numMembers];
                var assemIdA         = new int[numMembers];
                for (int i = 0; i < numMembers; i++)
                {
                    object?typeInformation;
                    binaryTypeEnumA[i]  = BinaryTypeConverter.GetBinaryTypeInfo(memberTypes[i], memberObjectInfos[i], null, _objectWriter, out typeInformation, out assemId);
                    typeInformationA[i] = typeInformation;
                    assemIdA[i]         = assemId;
                }

                if (_binaryObjectWithMapTyped == null)
                {
                    _binaryObjectWithMapTyped = new BinaryObjectWithMapTyped();
                }

                // BCL types are not placed in table
                assemId = (int)typeNameInfo._assemId;
                _binaryObjectWithMapTyped.Set(objectId, objectName, numMembers, memberNames, binaryTypeEnumA, typeInformationA, assemIdA, assemId);
                _binaryObjectWithMapTyped.Write(this);
                if (objectMapInfo == null)
                {
                    _objectMapTable.Add(objectName, new ObjectMapInfo(objectId, numMembers, memberNames, memberTypes));
                }
            }
        }