コード例 #1
0
        DmdCustomAttributeData[] Read(SecurityAction action)
        {
            try {
                if (reader.Position >= reader.Length)
                {
                    return(Array.Empty <DmdCustomAttributeData>());
                }

                if (reader.ReadByte() == '.')
                {
                    return(ReadBinaryFormat(action));
                }
                reader.Position--;
                return(ReadXmlFormat(action));
            }
            catch (TypeNameParserException) {
            }
            catch (CABlobParserException) {
            }
            catch (ResolveException) {
            }
            catch (IOException) {
            }
            return(Array.Empty <DmdCustomAttributeData>());
        }
コード例 #2
0
ファイル: DmdSignatureReader.cs プロジェクト: xi4oyu/dnSpy
 public static (DmdType fieldType, DmdMethodSignature methodSignature, bool containedGenericParams) ReadMethodSignatureOrFieldType(DmdModule module, DmdDataStream reader, IList <DmdType> genericTypeArguments, IList <DmdType> genericMethodArguments, bool resolve)
 {
     try {
         using (var sigReader = new DmdSignatureReader(module, reader, genericTypeArguments, genericMethodArguments, resolve)) {
             var flags = (DmdSignatureCallingConvention)reader.ReadByte();
             if ((flags & DmdSignatureCallingConvention.Mask) == DmdSignatureCallingConvention.Field)
             {
                 var fieldType = sigReader.ReadType().type;
                 return(fieldType, null, sigReader.containedGenericParams);
             }
             else
             {
                 sigReader.ReadMethodSignature(flags, out var genericParameterCount, out var returnType, out var parameterTypes, out var varArgsParameterTypes);
                 if ((flags & DmdSignatureCallingConvention.Mask) != DmdSignatureCallingConvention.Property)
                 {
                     var methodSignature = new DmdMethodSignature(flags, genericParameterCount, returnType, parameterTypes, varArgsParameterTypes);
                     return(null, methodSignature, sigReader.containedGenericParams);
                 }
             }
         }
     }
     catch (IOException) {
     }
     catch (OutOfMemoryException) {
     }
     return(module.AppDomain.System_Void, null, false);
 }
コード例 #3
0
        bool ReadHeader(out int localSignatureMetadataToken, out int maxStackSize, out bool initLocals, out int codeSize, out bool hasExceptionHandlers)
        {
            byte b = reader.ReadByte();

            switch (b & 7)
            {
            case 2:
            case 6:
                localSignatureMetadataToken = 0;
                maxStackSize         = 8;
                initLocals           = false;
                codeSize             = b >> 2;
                hasExceptionHandlers = false;
                return(true);

            case 3:
                uint flags      = (ushort)((reader.ReadByte() << 8) | b);
                int  headerSize = (int)(flags >> 12);
                initLocals   = (flags & 0x10) != 0;
                maxStackSize = reader.ReadUInt16();
                codeSize     = reader.ReadInt32();
                localSignatureMetadataToken = reader.ReadInt32();
                hasExceptionHandlers        = (flags & 8) != 0;

                reader.Position += -12 + headerSize * 4;
                if (headerSize < 3)
                {
                    hasExceptionHandlers = false;
                }
                return(true);

            default:
                localSignatureMetadataToken = 0;
                maxStackSize         = 0;
                initLocals           = false;
                codeSize             = 0;
                hasExceptionHandlers = false;
                return(false);
            }
        }
コード例 #4
0
ファイル: DmdMarshalBlobReader.cs プロジェクト: zyc9012/dnSpy
        DmdMarshalType Read()
        {
            const int DEFAULT = 0;

            try {
                var           nativeType = (UnmanagedType)reader.ReadByte();
                UnmanagedType nt;
                int           size;
                switch (nativeType)
                {
                case UnmanagedType.ByValTStr:
                    size = CanRead ? (int)reader.ReadCompressedUInt32() : DEFAULT;
                    return(DmdMarshalType.CreateFixedSysString(size));

                case UnmanagedType.SafeArray:
                    var vt      = CanRead ? (VarEnum)reader.ReadCompressedUInt32() : DEFAULT;
                    var udtName = CanRead ? ReadUTF8String() : null;
                    var udtRef  = udtName == null ? null : DmdTypeNameParser.Parse(module, udtName, genericTypeArguments);
                    return(DmdMarshalType.CreateSafeArray(vt, udtRef));

                case UnmanagedType.ByValArray:
                    size = CanRead ? (int)reader.ReadCompressedUInt32() : DEFAULT;
                    nt   = CanRead ? (UnmanagedType)reader.ReadCompressedUInt32() : DEFAULT;
                    return(DmdMarshalType.CreateFixedArray(size, nt));

                case UnmanagedType.LPArray:
                    nt = CanRead ? (UnmanagedType)reader.ReadCompressedUInt32() : DEFAULT;
                    int paramNum = CanRead ? (int)reader.ReadCompressedUInt32() : DEFAULT;
                    size = CanRead ? (int)reader.ReadCompressedUInt32() : DEFAULT;
                    bool      hasFlags = CanRead;
                    int       flags    = hasFlags ? (int)reader.ReadCompressedUInt32() : DEFAULT;
                    const int ntaSizeParamIndexSpecified = 1;
                    if (hasFlags && (flags & ntaSizeParamIndexSpecified) == 0)
                    {
                        paramNum = 0;
                    }
                    return(DmdMarshalType.CreateArray(nt, (short)paramNum, size));

                case UnmanagedType.CustomMarshaler:
                    var guid              = ReadUTF8String();
                    var nativeTypeName    = ReadUTF8String();
                    var custMarshalerName = ReadUTF8String();
                    var cmRef             = DmdTypeNameParser.Parse(module, custMarshalerName, genericTypeArguments);
                    var cookie            = ReadUTF8String();
                    return(DmdMarshalType.CreateCustomMarshaler(custMarshalerName, cmRef, cookie));

                case UnmanagedType.IUnknown:
                case UnmanagedType.IDispatch:
                case UnmanagedType.Interface:
                    int iidParamIndex = CanRead ? (int)reader.ReadCompressedUInt32() : DEFAULT;
                    return(DmdMarshalType.CreateInterface(nativeType, iidParamIndex));

                default:
                    return(DmdMarshalType.Create(nativeType));
                }
            }
            catch (ArgumentException) {
            }
            catch (IOException) {
            }
            return(null);
        }
コード例 #5
0
		object ReadValue(SerializationType etype, DmdType argType, out DmdType realArgType) {
			if (!IncrementRecursionCounter())
				throw new CABlobParserException("Stack overflow");

			object result;
			switch (etype) {
			case SerializationType.Boolean:
				realArgType = module.AppDomain.System_Boolean;
				result = reader.ReadByte() != 0;
				break;

			case SerializationType.Char:
				realArgType = module.AppDomain.System_Char;
				result = (char)reader.ReadUInt16();
				break;

			case SerializationType.I1:
				realArgType = module.AppDomain.System_SByte;
				result = reader.ReadSByte();
				break;

			case SerializationType.U1:
				realArgType = module.AppDomain.System_Byte;
				result = reader.ReadByte();
				break;

			case SerializationType.I2:
				realArgType = module.AppDomain.System_Int16;
				result = reader.ReadInt16();
				break;

			case SerializationType.U2:
				realArgType = module.AppDomain.System_UInt16;
				result = reader.ReadUInt16();
				break;

			case SerializationType.I4:
				realArgType = module.AppDomain.System_Int32;
				result = reader.ReadInt32();
				break;

			case SerializationType.U4:
				realArgType = module.AppDomain.System_UInt32;
				result = reader.ReadUInt32();
				break;

			case SerializationType.I8:
				realArgType = module.AppDomain.System_Int64;
				result = reader.ReadInt64();
				break;

			case SerializationType.U8:
				realArgType = module.AppDomain.System_UInt64;
				result = reader.ReadUInt64();
				break;

			case SerializationType.R4:
				realArgType = module.AppDomain.System_Single;
				result = reader.ReadSingle();
				break;

			case SerializationType.R8:
				realArgType = module.AppDomain.System_Double;
				result = reader.ReadDouble();
				break;

			case SerializationType.String:
				realArgType = module.AppDomain.System_String;
				result = ReadUTF8String();
				break;

			// It's ET.ValueType if it's eg. a ctor enum arg type
			case (SerializationType)DMD.ElementType.ValueType:
				if ((object)argType == null)
					throw new CABlobParserException("Invalid element type");
				realArgType = argType;
				result = ReadEnumValue(GetEnumUnderlyingType(argType));
				break;

			// It's ET.Object if it's a ctor object arg type
			case (SerializationType)DMD.ElementType.Object:
			case SerializationType.TaggedObject:
				realArgType = ReadFieldOrPropType();
				if (realArgType.IsSZArray)
					result = ReadArrayArgument(realArgType);
				else
					result = ReadValue(ToSerializationType(realArgType), realArgType, out var tmpType);
				break;

			// It's ET.Class if it's eg. a ctor System.Type arg type
			case (SerializationType)DMD.ElementType.Class:
				if (argType == module.AppDomain.System_Type) {
					result = ReadValue(SerializationType.Type, argType, out realArgType);
					break;
				}
				else if (argType == module.AppDomain.System_String) {
					result = ReadValue(SerializationType.String, argType, out realArgType);
					break;
				}
				else if (argType == module.AppDomain.System_Object) {
					result = ReadValue(SerializationType.TaggedObject, argType, out realArgType);
					break;
				}

				// Assume it's an enum that couldn't be resolved
				realArgType = argType;
				result = ReadEnumValue(null);
				break;

			case SerializationType.Type:
				realArgType = argType;
				result = ReadType(true);
				break;

			case SerializationType.Enum:
				realArgType = ReadType(false);
				result = ReadEnumValue(GetEnumUnderlyingType(realArgType));
				break;

			default:
				throw new CABlobParserException("Invalid element type");
			}

			DecrementRecursionCounter();
			return result;
		}