public static CoffHeader Parse(BinaryReader reader) { uint signature = reader.ReadUInt32(); if (NTSignature != signature) { throw new Exception("Invalid COFF header signature"); } CoffHeader header = new CoffHeader(); header.Machine = (CoffMachine)reader.ReadUInt16(); header.NumberOfSections = reader.ReadUInt16(); header.TimeDateStamp = reader.ReadUInt32(); header.PointerToSymbolTable = reader.ReadUInt32(); header.NumberOfSymbols = reader.ReadUInt32(); header.SizeOfOptionalHeader = reader.ReadUInt16(); header.Characteristics = (CoffFlags)reader.ReadUInt16(); return(header); }
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); } }