public CLRSigFieldSig(CLRSignatureParser parser) { if (parser.NextByte() != 0x6) throw new ParseFailedException("Malformed FieldSig"); parser.ConsumeByte(); CustomMods = CLRSigType.ReadCustomMods(parser); Type = CLRSigType.Parse(parser, false); }
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"); } }
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"); }
public CLRSigTypeSpec(CLRSignatureParser parser) { byte token = parser.NextByte(); // VAR and MVAR aren't permitted by the spec, but that appears to be an error if (token != (byte)CLRSigType.ElementType.PTR && token != (byte)CLRSigType.ElementType.FNPTR && token != (byte)CLRSigType.ElementType.ARRAY && token != (byte)CLRSigType.ElementType.SZARRAY && token != (byte)CLRSigType.ElementType.GENERICINST && token != (byte)CLRSigType.ElementType.MVAR && token != (byte)CLRSigType.ElementType.VAR) throw new ParseFailedException("Invalid sig type for type spec"); Type = CLRSigType.Parse(parser, false); }
public static string ReadUTF8String(CLRSignatureParser parser) { byte firstByte = parser.NextByte(); if (firstByte == 0xff) return null; else { uint stringLengthBytes = parser.ReadCompressedUInt(); if (stringLengthBytes == 0) return ""; else { byte[] utf8chars = new byte[stringLengthBytes]; parser.ReadBytes(utf8chars, stringLengthBytes); return System.Text.Encoding.UTF8.GetString(utf8chars); } } }
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); }
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); }
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(); }
public static CLRSigCustomMod[] ReadCustomMods(CLRSignatureParser parser) { List<CLRSigCustomMod> customMods = new List<CLRSigCustomMod>(); byte nextToken = parser.NextByte(); while (nextToken == (byte)ElementType.CMOD_OPT || nextToken == (byte)ElementType.CMOD_REQD) { customMods.Add(new CLRSigCustomMod(parser)); nextToken = parser.NextByte(); throw new NotImplementedException(); } return customMods.ToArray(); }