public static ImportDirectory Parse(BinaryReader reader) { ImportDirectory importDirectory = new ImportDirectory(); ImageImportDescriptor descriptor = ImageImportDescriptor.Parse(reader); while (descriptor.NameRVA != 0) { importDirectory.Descriptors.Add(descriptor); descriptor = ImageImportDescriptor.Parse(reader); } return(importDirectory); }
public static List <string> GetDependencies(string path) { List <string> result = new List <string>(); PortableExecutableInfo peInfo = new PortableExecutableInfo(path); ImportDirectory dir = peInfo.ImportDirectory; if (dir != null) { BinaryReader reader = GetBinaryReader(path); foreach (ImageImportDescriptor desc in dir.Descriptors) { uint fileNameOffset = peInfo.GetOffsetFromRVA(desc.NameRVA); reader.BaseStream.Seek(fileNameOffset, SeekOrigin.Begin); string fileName = BinaryReaderUtils.ReadNullTerminatedAsciiString(reader); result.Add(fileName); } reader.Close(); } return(result); }
public void Parse(BinaryReader reader) { m_dosHeader = DosHeader.Parse(reader); int dosStubSize = (int)(m_dosHeader.CoffHeaderOffset - reader.BaseStream.Position); m_dosStubBytes = reader.ReadBytes(dosStubSize); m_coffHeader = CoffHeader.Parse(reader); m_peHeaderOffset = (uint)reader.BaseStream.Position; m_peHeader = PEHeader.Parse(reader); for (int i = 0; i < m_coffHeader.NumberOfSections; i++) { m_sectionHeaders.Add(PESectionHeader.Parse(reader)); } int fillerSize = (int)(m_sectionHeaders[0].PointerToRawData - reader.BaseStream.Position); m_filler = reader.ReadBytes(fillerSize); for (int i = 0; i < m_coffHeader.NumberOfSections; i++) { byte[] sectionBytes = reader.ReadBytes((int)m_sectionHeaders[i].SizeOfRawData); m_sections.Add(sectionBytes); } int remainingByteCount = (int)(reader.BaseStream.Length - reader.BaseStream.Position); m_remainingBytes = reader.ReadBytes(remainingByteCount); // file ends here // Parse Import Directory: PEDataDirectory importDirectoryEntry = m_peHeader.DataDirectories[(int)DataDirectoryName.Import]; if (importDirectoryEntry.VirtualAddress > 0) { uint importDirectoryFileOffset = GetOffsetFromRVA(importDirectoryEntry.VirtualAddress); reader.BaseStream.Seek(importDirectoryFileOffset, SeekOrigin.Begin); m_importDirectory = ImportDirectory.Parse(reader); } }