예제 #1
0
        public CLRSigFieldSig(CLRSignatureParser parser)
        {
            if (parser.NextByte() != 0x6)
                throw new ParseFailedException("Malformed FieldSig");
            parser.ConsumeByte();

            CustomMods = CLRSigType.ReadCustomMods(parser);
            Type = CLRSigType.Parse(parser, false);
        }
예제 #2
0
        public static CLRSigType Parse(CLRSignatureParser parser, bool permitVoid)
        {
            ElementType eType = (ElementType)parser.NextByte();
            parser.ConsumeByte();

            switch (eType)
            {
                case ElementType.BOOLEAN:
                case ElementType.CHAR:
                case ElementType.I1:
                case ElementType.U1:
                case ElementType.I2:
                case ElementType.U2:
                case ElementType.I4:
                case ElementType.U4:
                case ElementType.I8:
                case ElementType.U8:
                case ElementType.R4:
                case ElementType.R8:
                case ElementType.I:
                case ElementType.U:
                case ElementType.OBJECT:
                case ElementType.STRING:
                    return new CLRSigTypeSimple(eType);
                case ElementType.VOID:
                    if (!permitVoid)
                        throw new ParseFailedException("Unexpected void type");
                    return new CLRSigTypeSimple(eType);
                case ElementType.ARRAY:
                    return new CLRSigTypeArray(eType, parser);
                case ElementType.CLASS:
                case ElementType.VALUETYPE:
                    return new CLRSigTypeStructured(eType, parser);
                case ElementType.FNPTR:
                    return new CLRSigTypeFunctionPointer(eType, parser);
                case ElementType.GENERICINST:
                    return new CLRSigTypeGenericInstantiation(eType, parser);
                case ElementType.MVAR:
                case ElementType.VAR:
                    return new CLRSigTypeVarOrMVar(eType, parser);
                case ElementType.PTR:
                    return new CLRSigTypePointer(eType, parser);
                case ElementType.SZARRAY:
                    return new CLRSigTypeSZArray(eType, parser);
                default:
                    throw new ParseFailedException("Unexpected sig type");
            }
        }
예제 #3
0
        public CLRSigCustomMod(CLRSignatureParser parser)
        {
            byte token = parser.NextByte();
            parser.ConsumeByte();

            if (token == 0x1f)
                IsOptional = false;
            else if (token == 0x20)
                IsOptional = true;
            else
                throw new ParseFailedException("Strange custom mod token");

            IndexedType = parser.ReadTypeDefOrRefOrSpecEncoded();

            throw new NotSupportedException("Custom modifiers are not supported");
        }
예제 #4
0
        public CLRSigMethodDefOrRefSig(CLRSignatureParser parser, Kind allowedKind)
        {
            byte baseByte = parser.NextByte();
            parser.ConsumeByte();

            CLRSignatureParser.Token callingConvention = (CLRSignatureParser.Token)(baseByte & 0x0f);

            if ((baseByte & (byte)CLRSignatureParser.Token.HASTHIS) != 0)
            {
                HasThis = true;
                if ((baseByte & (byte)CLRSignatureParser.Token.EXPLICITTHIS) != 0)
                    ExplicitThis = true;
            }

            if ((baseByte & (int)CLRSignatureParser.Token.GENERIC) != 0)
            {
                if (allowedKind != Kind.Def && allowedKind != Kind.DefOrRef && allowedKind != Kind.Ref)
                    throw new ParseFailedException("Invalid method signature");
                allowedKind = Kind.Def;
                NumGenericParameters = parser.ReadCompressedUInt();
            }

            switch (callingConvention)
            {
                case CLRSignatureParser.Token.DEFAULT:
                    if (allowedKind != Kind.Def && allowedKind != Kind.DefOrRef && allowedKind != Kind.Ref)
                        throw new ParseFailedException("Invalid method signature");
                    allowedKind = Kind.Def;
                    CallingConvention = CallingConventionType.Default;
                    break;
                case CLRSignatureParser.Token.VARARG:
                    CallingConvention = CallingConventionType.VarArg;
                    break;
                case CLRSignatureParser.Token.C:
                    if (allowedKind != Kind.StandAlone)
                        throw new ParseFailedException("Invalid method signature");
                    CallingConvention = CallingConventionType.C;
                    break;
                case CLRSignatureParser.Token.STDCALL:
                    if (allowedKind != Kind.StandAlone)
                        throw new ParseFailedException("Invalid method signature");
                    CallingConvention = CallingConventionType.StdCall;
                    break;
                case CLRSignatureParser.Token.THISCALL:
                    if (allowedKind != Kind.StandAlone)
                        throw new ParseFailedException("Invalid method signature");
                    CallingConvention = CallingConventionType.ThisCall;
                    break;
                case CLRSignatureParser.Token.FASTCALL:
                    if (allowedKind != Kind.StandAlone)
                        throw new ParseFailedException("Invalid method signature");
                    CallingConvention = CallingConventionType.FastCall;
                    break;
                default:
                    throw new ParseFailedException("Unexpected signature token");
            }
            uint paramCount = parser.ReadCompressedUInt();

            // Return type
            RetType = new CLRSigRetType(parser);

            ParamTypes = new CLRSigParamType[paramCount];

            bool allowSentinel = (allowedKind == Kind.StandAlone) && (CallingConvention == CallingConventionType.C || CallingConvention == CallingConventionType.VarArg);

            // Parameter types
            for (uint i = 0; i < paramCount; i++)
                ParamTypes[i] = new CLRSigParamType(parser, allowSentinel);
        }
예제 #5
0
        public CLRSigTypeGenericInstantiation(ElementType type, CLRSignatureParser parser)
        {
            BasicType = type;

            ElementType instType = (ElementType)parser.NextByte();
            parser.ConsumeByte();
            if (instType == ElementType.CLASS)
                InstantiationType = InstType.Class;
            else if (instType == ElementType.VALUETYPE)
                InstantiationType = InstType.ValueType;
            else
                throw new ParseFailedException("Unexpected instantiation type");
            GenericType = parser.ReadTypeDefOrRefOrSpecEncoded();
            uint genArgCount = parser.ReadCompressedUInt();

            ArgTypes = new CLRSigType[genArgCount];
            for (uint i = 0; i < genArgCount; i++)
                ArgTypes[i] = CLRSigType.Parse(parser, false);
        }
예제 #6
0
        public static void ReadCustomModsAndConstraints(CLRSignatureParser parser, out CLRSigCustomMod[] outCustomMods, out CLRSigConstraint[] outConstraints)
        {
            List<CLRSigCustomMod> customMods = new List<CLRSigCustomMod>();
            List<CLRSigConstraint> constraints = new List<CLRSigConstraint>();

            byte nextToken = parser.NextByte();
            while (true)
            {
                if (nextToken == (byte)ElementType.PINNED)
                {
                    constraints.Add(new CLRSigConstraint(CLRSigConstraint.ConstraintTypeEnum.Pinned));
                    parser.ConsumeByte();
                }
                else if (nextToken == (byte)ElementType.CMOD_OPT || nextToken == (byte)ElementType.CMOD_REQD)
                {
                    customMods.Add(new CLRSigCustomMod(parser));
                    nextToken = parser.NextByte();
                }
                else
                    break;
            }
            outCustomMods = customMods.ToArray();
            outConstraints = constraints.ToArray();
        }