protected override object CheckCctor(ref TypeDef type, MethodDef cctor) { var instrs = cctor.Body.Instructions; for (int i = 0; i < instrs.Count - 1; i++) { var ldci4 = instrs[i]; if (!ldci4.IsLdcI4()) { continue; } var call = instrs[i + 1]; if (call.OpCode.Code != Code.Call) { continue; } if (call.Operand != info.initMethod) { continue; } int offset = ldci4.GetLdcI4Value(); reader.Position = offset; uint rid = reader.ReadCompressedUInt32(); if (rid != type.Rid) { throw new ApplicationException("Invalid RID"); } return(string.Empty); // It's non-null } return(null); }
public uint ReadVariableLengthUInt32() { uint val; reader.ReadCompressedUInt32(out val); return(val); }
/// <summary> /// Reads a compressed <see cref="uint"/> from the current position in <paramref name="reader"/> /// </summary> /// <param name="reader">The reader</param> /// <returns>The value</returns> public static uint ReadCompressedUInt32(this IBinaryReader reader) { uint val; if (!reader.ReadCompressedUInt32(out val)) { throw new IOException("Could not read a compressed UInt32"); } return(val); }
/// <summary> /// Reads the new (.NET 2.0+) DeclSecurity blob format /// </summary> /// <returns></returns> private ThreadSafe.IList <SecurityAttribute> ReadBinaryFormat() { int numAttrs = (int)reader.ReadCompressedUInt32(); var list = ThreadSafeListCreator.Create <SecurityAttribute>(numAttrs); for (int i = 0; i < numAttrs; i++) { var name = ReadUTF8String(); // Use CA search rules. Some tools don't write the fully qualified name. var attrRef = TypeNameParser.ParseReflection(module, UTF8String.ToSystemStringOrEmpty(name), new CAAssemblyRefFinder(module), gpContext); int blobLength = (int)reader.ReadCompressedUInt32(); int numNamedArgs = (int)reader.ReadCompressedUInt32(); var namedArgs = CustomAttributeReader.ReadNamedArguments(module, reader, numNamedArgs, gpContext); if (namedArgs == null) { throw new ApplicationException("Could not read named arguments"); } list.Add(new SecurityAttribute(attrRef, namedArgs)); } return(list); }
public virtual bool Decrypt(IBinaryReader fileDataReader, DumpedMethod dm) { if (fileDataReader.ReadByte() != 0x2A) return false; // Not a RET methodsDataReader.Position = fileDataReader.ReadCompressedUInt32(); dm.mhCodeSize = methodsDataReader.ReadCompressedUInt32(); dm.code = methodsDataReader.ReadBytes((int)dm.mhCodeSize); if ((dm.mhFlags & 8) != 0) dm.extraSections = MethodBodyParser.ReadExtraSections(methodsDataReader); if (!DecryptCode(dm)) return false; return true; }
UTF8String ReadUTF8String() { if (reader.ReadByte() == 0xFF) { return(null); } reader.Position--; uint len; if (!reader.ReadCompressedUInt32(out len)) { throw new CABlobParserException("Could not read compressed UInt32"); } if (len == 0) { return(UTF8String.Empty); } return(new UTF8String(reader.ReadBytes((int)len))); }
public virtual bool Decrypt(IBinaryReader fileDataReader, DumpedMethod dm) { if (fileDataReader.ReadByte() != 0x2A) { return(false); // Not a RET } methodsDataReader.Position = fileDataReader.ReadCompressedUInt32(); dm.mhCodeSize = methodsDataReader.ReadCompressedUInt32(); dm.code = methodsDataReader.ReadBytes((int)dm.mhCodeSize); if ((dm.mhFlags & 8) != 0) { dm.extraSections = MethodBodyParser.ReadExtraSections(methodsDataReader); } if (!DecryptCode(dm)) { return(false); } return(true); }
PdbCustomDebugInfo ReadAsyncMethodSteppingInformationBlob() { if (bodyOpt == null) { return(null); } uint catchHandlerOffset = reader.ReadUInt32() - 1; Instruction catchHandler; if (catchHandlerOffset == uint.MaxValue) { catchHandler = null; } else { catchHandler = GetInstruction(catchHandlerOffset); Debug.Assert(catchHandler != null); if (catchHandler == null) { return(null); } } var asyncInfo = new PdbAsyncMethodSteppingInformationCustomDebugInfo(); asyncInfo.CatchHandler = catchHandler; while (reader.Position < reader.Length) { var yieldInstr = GetInstruction(reader.ReadUInt32()); Debug.Assert(yieldInstr != null); if (yieldInstr == null) { return(null); } uint resumeOffset = reader.ReadUInt32(); var moveNextRid = reader.ReadCompressedUInt32(); var moveNextToken = new MDToken(Table.Method, moveNextRid); MethodDef moveNextMethod; Instruction resumeInstr; if (gpContext.Method != null && moveNextToken == gpContext.Method.MDToken) { moveNextMethod = gpContext.Method; resumeInstr = GetInstruction(resumeOffset); } else { moveNextMethod = module.ResolveToken(moveNextToken, gpContext) as MethodDef; Debug.Assert(moveNextMethod != null); if (moveNextMethod == null) { return(null); } resumeInstr = GetInstruction(moveNextMethod, resumeOffset); } Debug.Assert(resumeInstr != null); if (resumeInstr == null) { return(null); } asyncInfo.AsyncStepInfos.Add(new PdbAsyncStepInfo(yieldInstr, moveNextMethod, resumeInstr)); } return(asyncInfo); }
T ReadSig <T>(T methodSig) where T : MethodBaseSig { if (methodSig.Generic) { uint count; if (!reader.ReadCompressedUInt32(out count)) { return(null); } methodSig.GenParamCount = count; } uint numParams; if (!reader.ReadCompressedUInt32(out numParams)) { return(null); } methodSig.RetType = ReadType(); var parameters = methodSig.Params; for (uint i = 0; i < numParams; i++) { var type = ReadType(); if (type is SentinelSig) { if (methodSig.ParamsAfterSentinel == null) { methodSig.ParamsAfterSentinel = parameters = ThreadSafeListCreator.Create <TypeSig>((int)(numParams - i)); } i--; } else { parameters.Add(type); } } return(methodSig); }
MarshalType Read() { MarshalType returnValue; try { var nativeType = (NativeType)reader.ReadByte(); NativeType nt; int size; switch (nativeType) { case NativeType.FixedSysString: size = CanRead() ? (int)reader.ReadCompressedUInt32() : -1; returnValue = new FixedSysStringMarshalType(size); break; case NativeType.SafeArray: var vt = CanRead() ? (VariantType)reader.ReadCompressedUInt32() : VariantType.NotInitialized; var udtName = CanRead() ? ReadUTF8String() : null; var udtRef = (object)udtName == null ? null : TypeNameParser.ParseReflection(module, UTF8String.ToSystemStringOrEmpty(udtName), null, gpContext); returnValue = new SafeArrayMarshalType(vt, udtRef); break; case NativeType.FixedArray: size = CanRead() ? (int)reader.ReadCompressedUInt32() : -1; nt = CanRead() ? (NativeType)reader.ReadCompressedUInt32() : NativeType.NotInitialized; returnValue = new FixedArrayMarshalType(size, nt); break; case NativeType.Array: nt = CanRead() ? (NativeType)reader.ReadCompressedUInt32() : NativeType.NotInitialized; int paramNum = CanRead() ? (int)reader.ReadCompressedUInt32() : -1; size = CanRead() ? (int)reader.ReadCompressedUInt32() : -1; int flags = CanRead() ? (int)reader.ReadCompressedUInt32() : -1; returnValue = new ArrayMarshalType(nt, paramNum, size, flags); break; case NativeType.CustomMarshaler: var guid = ReadUTF8String(); var nativeTypeName = ReadUTF8String(); var custMarshalerName = ReadUTF8String(); var cmRef = TypeNameParser.ParseReflection(module, UTF8String.ToSystemStringOrEmpty(custMarshalerName), new CAAssemblyRefFinder(module), gpContext); var cookie = ReadUTF8String(); returnValue = new CustomMarshalType(guid, nativeTypeName, cmRef, cookie); break; case NativeType.IUnknown: case NativeType.IDispatch: case NativeType.IntF: int iidParamIndex = CanRead() ? (int)reader.ReadCompressedUInt32() : -1; return(new InterfaceMarshalType(nativeType, iidParamIndex)); default: returnValue = new MarshalType(nativeType); break; } } catch { returnValue = new RawMarshalType(reader.ReadAllBytes()); } return(returnValue); }