// Use this method whenever using a MetadataToken, otherwise you'll be off by 1! internal void SeekToMDToken(MetadataToken token) { if (token.Index == 0) throw new BadImageFormatException(String.Format(CultureInfo.CurrentCulture, Res.NilMetadataToken, token)); SeekToRowOfTable(token.Table, token.Index - 1); }
// See the comments in Equals - we're doing string comparisons here // to compare full type names. public static bool Equals(MiniAssembly assemblyA, PEFileReader peFileB, MetadataToken assemblyRefB) { System.Diagnostics.Contracts.Contract.Requires(assemblyA != null); System.Diagnostics.Contracts.Contract.Requires(peFileB != null); System.Diagnostics.Contracts.Contract.Requires(assemblyRefB.Table == MDTables.Tables.AssemblyRef); String nameA, nameRefB; if (assemblyA.IsReflectionAssembly) nameA = AppDomain.CurrentDomain.ApplyPolicy(assemblyA._reflectionAssembly.FullName); else { AssemblyInfo assemblyInfoA = new AssemblyInfo(); assemblyA._peFile.GetAssemblyInfo(ref assemblyInfoA); nameA = AppDomain.CurrentDomain.ApplyPolicy(assemblyInfoA.ToString()); } AssemblyInfo assemblyInfoB = ReadAssemblyRef(peFileB, assemblyRefB); nameRefB = AppDomain.CurrentDomain.ApplyPolicy(assemblyInfoB.ToString()); return Utils.AssemblyRefEqualsDef(nameRefB, nameA); }
private static AssemblyInfo ReadAssemblyRef(PEFileReader peFile, MetadataToken assemblyRef) { System.Diagnostics.Contracts.Contract.Requires(peFile != null); System.Diagnostics.Contracts.Contract.Requires(assemblyRef.Table == MDTables.Tables.AssemblyRef); MDTables metaData = peFile.MetaData; BinaryReader B = metaData.B; metaData.SeekToMDToken(assemblyRef); UInt16 major = B.ReadUInt16(); UInt16 minor = B.ReadUInt16(); UInt16 build = B.ReadUInt16(); UInt16 revision = B.ReadUInt16(); Version v = new Version(major, minor, build, revision); UInt32 assemblyFlags = B.ReadUInt32(); byte[] publicKey = metaData.ReadBlob(); String simpleName = metaData.ReadString(); String culture = metaData.ReadString(); if ((culture != null) && (culture.Length == 0)) culture = null; return new AssemblyInfo(v, assemblyFlags, publicKey, simpleName, culture); }
public MiniAssembly ResolveAssemblyRef(MetadataToken token, bool throwOnError) { System.Diagnostics.Contracts.Contract.Requires(token.Table == MDTables.Tables.AssemblyRef); PEFileReader peFile = this.PEFileReader; MDTables metaData = peFile.MetaData; metaData.SeekToMDToken(token); peFile.B.ReadUInt64(); // Skip 4 parts of the version number. peFile.B.ReadUInt32(); // AssemblyFlags byte[] publicKeyOrToken = metaData.ReadBlob(); // Public key or token String assemblySimpleName = metaData.ReadString(); // simple name String cultureName = metaData.ReadString(); // assembly culture if (!String.IsNullOrEmpty(cultureName)) throw new BadImageFormatException(Res.UnexpectedlyLoadingASatellite, FullName); if (assemblySimpleName == "mscorlib" && (cultureName.Length == 0 || cultureName == "neutral")) return new MiniAssembly(typeof(Object).Assembly); MiniAssembly loadedAssembly = Open(assemblySimpleName, _dependencyDirs, throwOnError); if (loadedAssembly != null) { // Check whether the reference to the assembly matches what we actually loaded. // We don't respect the "throwOnError" parameter here because if someone does // violate this, they've either severely messed up their deployment, or they're // attempting a security exploit. System.Reflection.AssemblyName loadedAssemblyName = new System.Reflection.AssemblyName(loadedAssembly.FullName); if (!Utils.PublicKeyMatches(loadedAssemblyName, publicKeyOrToken)) { throw new FileLoadException(String.Format(CultureInfo.CurrentCulture, Res.AssemblyLoadRefDefMismatch, assemblySimpleName, publicKeyOrToken, loadedAssemblyName.GetPublicKeyToken())); } if (!String.IsNullOrEmpty(loadedAssemblyName.CultureInfo.Name)) { throw new FileLoadException(String.Format(CultureInfo.CurrentCulture, Res.AssemblyLoadRefDefMismatch, assemblySimpleName, String.Empty, loadedAssemblyName.CultureInfo.Name)); } } return loadedAssembly; }