public UInt32 Index; // 1 to N, not 0 to N-1. public MetadataToken(MDTables.Tables table, UInt32 index) { System.Diagnostics.Contracts.Contract.Requires((index & 0xFF000000U) == 0); Table = table; Index = index; }
/* * private static UInt32 PadTo4(UInt32 RVA) * { * UInt32 Cleared = RVA & ~0x3U; * return (RVA == Cleared) ? RVA : RVA + 0x4U; * } */ internal void InitMetaData() { if (B == null) { throw new ObjectDisposedException(null); } if (parsedMetaData) { return; } SeekToRVA(MetaDataDDRVA); UInt32 MetaDataRVA = B.ReadUInt32(); UInt32 MetaDataSize = B.ReadUInt32(); if ((MetaDataRVA == 0) || (MetaDataSize == 0)) { throw new BadImageFormatException(); } MetadataRoot = RVAToFileAddress(MetaDataRVA); UInt32 VersionLengthRVA = MetaDataRVA + 12; UInt32 VersionStringRVA = VersionLengthRVA + 4; SeekToRVA(VersionLengthRVA); UInt32 VersionLength = B.ReadUInt32(); String Version = ReadUTF8String(VersionLength); UInt32 FlagsRVA = VersionStringRVA + VersionLength; UInt32 NStreamsRVA = FlagsRVA + 2; SeekToRVA(NStreamsRVA); UInt16 NStreams = B.ReadUInt16(); Streams = new StreamDesc[NStreams]; for (int i = 0; i < NStreams; i++) { Streams[i] = new StreamDesc(B, MetadataRoot); } StreamDesc stringStream, blobStream, TablesStream; if (!FindStream("#~", out TablesStream)) { if (!FindStream("#-", out TablesStream)) { throw new BadImageFormatException(); } } if (!FindStream("#Strings", out stringStream)) { throw new BadImageFormatException(); } if (!FindStream("#Blob", out blobStream)) { throw new BadImageFormatException(); } TablesStream.SeekTo(B, 0); metaData = new MDTables(B, stringStream, blobStream); // metaData.DumpStringHeap(); parsedMetaData = true; }
private static MetadataToken FindTypeDef(PEFileReader peFile, MDTables mdScope, String typeName, String nameSpace) { System.Diagnostics.Contracts.Contract.Requires(typeName != null); uint numTypeDefs = mdScope.RowsInTable(MDTables.Tables.TypeDef); for (uint i = 0; i < numTypeDefs; i++) { mdScope.SeekToRowOfTable(MDTables.Tables.TypeDef, i); peFile.B.ReadUInt32(); // TypeAttributes String rowTypeName = mdScope.ReadString(); if (!String.Equals(typeName, rowTypeName)) continue; String rowNameSpace= mdScope.ReadString(); if (!String.Equals(nameSpace, rowNameSpace)) continue; return new MetadataToken(MDTables.Tables.TypeDef, i + 1); } throw new TypeLoadException(String.Format(CultureInfo.CurrentCulture, Res.CantFindTypeName, nameSpace, typeName)); }
/* private static UInt32 PadTo4(UInt32 RVA) { UInt32 Cleared = RVA & ~0x3U; return (RVA == Cleared) ? RVA : RVA + 0x4U; } */ internal void InitMetaData() { if (B == null) throw new ObjectDisposedException(null); if (parsedMetaData) return; SeekToRVA(MetaDataDDRVA); UInt32 MetaDataRVA = B.ReadUInt32(); UInt32 MetaDataSize = B.ReadUInt32(); if ((MetaDataRVA == 0) || (MetaDataSize == 0)) throw new BadImageFormatException(); MetadataRoot = RVAToFileAddress(MetaDataRVA); UInt32 VersionLengthRVA = MetaDataRVA + 12; UInt32 VersionStringRVA = VersionLengthRVA + 4; SeekToRVA(VersionLengthRVA); UInt32 VersionLength = B.ReadUInt32(); String Version = ReadUTF8String(VersionLength); UInt32 FlagsRVA = VersionStringRVA + VersionLength; UInt32 NStreamsRVA = FlagsRVA + 2; SeekToRVA(NStreamsRVA); UInt16 NStreams = B.ReadUInt16(); Streams = new StreamDesc[NStreams]; for (int i = 0; i < NStreams; i++) Streams[i] = new StreamDesc(B, MetadataRoot); StreamDesc stringStream, blobStream, TablesStream; if (!FindStream("#~", out TablesStream)) if (!FindStream("#-", out TablesStream)) throw new BadImageFormatException(); if (!FindStream("#Strings", out stringStream)) throw new BadImageFormatException(); if (!FindStream("#Blob", out blobStream)) throw new BadImageFormatException(); TablesStream.SeekTo(B, 0); metaData = new MDTables(B, stringStream, blobStream); // metaData.DumpStringHeap(); parsedMetaData = true; }