/// <summary> /// Constructor /// </summary> /// <param name="reader">PE file reader pointing to the start of this section</param> /// <param name="totalSize">Total size of this optional header (from the file header)</param> /// <param name="verify">Verify section</param> /// <exception cref="BadImageFormatException">Thrown if verification fails</exception> public ImageOptionalHeader32(ref DataReader reader, uint totalSize, bool verify) { if (totalSize < 0x60) { throw new BadImageFormatException("Invalid optional header size"); } if (verify && (ulong)reader.Position + totalSize > reader.Length) { throw new BadImageFormatException("Invalid optional header size"); } SetStartOffset(ref reader); magic = reader.ReadUInt16(); majorLinkerVersion = reader.ReadByte(); minorLinkerVersion = reader.ReadByte(); sizeOfCode = reader.ReadUInt32(); sizeOfInitializedData = reader.ReadUInt32(); sizeOfUninitializedData = reader.ReadUInt32(); addressOfEntryPoint = (RVA)reader.ReadUInt32(); baseOfCode = (RVA)reader.ReadUInt32(); baseOfData = (RVA)reader.ReadUInt32(); imageBase = reader.ReadUInt32(); sectionAlignment = reader.ReadUInt32(); fileAlignment = reader.ReadUInt32(); majorOperatingSystemVersion = reader.ReadUInt16(); minorOperatingSystemVersion = reader.ReadUInt16(); majorImageVersion = reader.ReadUInt16(); minorImageVersion = reader.ReadUInt16(); majorSubsystemVersion = reader.ReadUInt16(); minorSubsystemVersion = reader.ReadUInt16(); win32VersionValue = reader.ReadUInt32(); sizeOfImage = reader.ReadUInt32(); sizeOfHeaders = reader.ReadUInt32(); checkSum = reader.ReadUInt32(); subsystem = (Subsystem)reader.ReadUInt16(); dllCharacteristics = (DllCharacteristics)reader.ReadUInt16(); sizeOfStackReserve = reader.ReadUInt32(); sizeOfStackCommit = reader.ReadUInt32(); sizeOfHeapReserve = reader.ReadUInt32(); sizeOfHeapCommit = reader.ReadUInt32(); loaderFlags = reader.ReadUInt32(); numberOfRvaAndSizes = reader.ReadUInt32(); for (int i = 0; i < dataDirectories.Length; i++) { uint len = reader.Position - (uint)startOffset; if (len + 8 <= totalSize) { dataDirectories[i] = new ImageDataDirectory(ref reader, verify); } else { dataDirectories[i] = new ImageDataDirectory(); } } reader.Position = (uint)startOffset + totalSize; SetEndoffset(ref reader); }
/// <summary> /// Constructor /// </summary> /// <param name="reader">PE file reader pointing to the start of this section</param> /// <param name="totalSize">Total size of this optional header (from the file header)</param> /// <param name="verify">Verify section</param> /// <exception cref="BadImageFormatException">Thrown if verification fails</exception> public ImageOptionalHeader32(IImageStream reader, uint totalSize, bool verify) { if (totalSize < 0x60) { throw new BadImageFormatException("Invalid optional header size"); } if (verify && reader.Position + totalSize > reader.Length) { throw new BadImageFormatException("Invalid optional header size"); } SetStartOffset(reader); Magic = reader.ReadUInt16(); MajorLinkerVersion = reader.ReadByte(); MinorLinkerVersion = reader.ReadByte(); SizeOfCode = reader.ReadUInt32(); SizeOfInitializedData = reader.ReadUInt32(); SizeOfUninitializedData = reader.ReadUInt32(); AddressOfEntryPoint = (RVA)reader.ReadUInt32(); BaseOfCode = (RVA)reader.ReadUInt32(); BaseOfData = (RVA)reader.ReadUInt32(); imageBase = reader.ReadUInt32(); SectionAlignment = reader.ReadUInt32(); FileAlignment = reader.ReadUInt32(); MajorOperatingSystemVersion = reader.ReadUInt16(); MinorOperatingSystemVersion = reader.ReadUInt16(); MajorImageVersion = reader.ReadUInt16(); MinorImageVersion = reader.ReadUInt16(); MajorSubsystemVersion = reader.ReadUInt16(); MinorSubsystemVersion = reader.ReadUInt16(); Win32VersionValue = reader.ReadUInt32(); SizeOfImage = reader.ReadUInt32(); SizeOfHeaders = reader.ReadUInt32(); CheckSum = reader.ReadUInt32(); Subsystem = (Subsystem)reader.ReadUInt16(); DllCharacteristics = (DllCharacteristics)reader.ReadUInt16(); sizeOfStackReserve = reader.ReadUInt32(); sizeOfStackCommit = reader.ReadUInt32(); sizeOfHeapReserve = reader.ReadUInt32(); sizeOfHeapCommit = reader.ReadUInt32(); LoaderFlags = reader.ReadUInt32(); NumberOfRvaAndSizes = reader.ReadUInt32(); for (var i = 0; i < DataDirectories.Length; i++) { var len = (uint)(reader.Position - startOffset); if (len + 8 <= totalSize) { DataDirectories[i] = new ImageDataDirectory(reader, verify); } else { DataDirectories[i] = new ImageDataDirectory(); } } reader.Position = (long)startOffset + totalSize; SetEndoffset(reader); }
/// <summary> /// Constructor /// </summary> /// <param name="reader">PE file reader pointing to the start of this section</param> /// <param name="verify">Verify section</param> /// <exception cref="BadImageFormatException">Thrown if verification fails</exception> public ImageCor20Header(IImageStream reader, bool verify) { SetStartOffset(reader); this.cb = reader.ReadUInt32(); if (verify && this.cb < 0x48) throw new BadImageFormatException("Invalid IMAGE_COR20_HEADER.cb value"); this.majorRuntimeVersion = reader.ReadUInt16(); this.minorRuntimeVersion = reader.ReadUInt16(); this.metaData = new ImageDataDirectory(reader, verify); this.flags = (ComImageFlags)reader.ReadUInt32(); this.entryPointToken_or_RVA = reader.ReadUInt32(); this.resources = new ImageDataDirectory(reader, verify); this.strongNameSignature = new ImageDataDirectory(reader, verify); this.codeManagerTable = new ImageDataDirectory(reader, verify); this.vtableFixups = new ImageDataDirectory(reader, verify); this.exportAddressTableJumps = new ImageDataDirectory(reader, verify); this.managedNativeHeader = new ImageDataDirectory(reader, verify); SetEndoffset(reader); }
static void WriteDataDirectory(BinaryWriter writer, ImageDataDirectory dataDir) { writer.Write((uint)dataDir.VirtualAddress); writer.Write(dataDir.Size); }
/// <summary> /// Constructor /// </summary> /// <param name="reader">PE file reader pointing to the start of this section</param> /// <param name="totalSize">Total size of this optional header (from the file header)</param> /// <param name="verify">Verify section</param> /// <exception cref="BadImageFormatException">Thrown if verification fails</exception> public ImageOptionalHeader32(IImageStream reader, uint totalSize, bool verify) { if (totalSize < 0x60) throw new BadImageFormatException("Invalid optional header size"); if (verify && reader.Position + totalSize > reader.Length) throw new BadImageFormatException("Invalid optional header size"); SetStartOffset(reader); this.magic = reader.ReadUInt16(); this.majorLinkerVersion = reader.ReadByte(); this.minorLinkerVersion = reader.ReadByte(); this.sizeOfCode = reader.ReadUInt32(); this.sizeOfInitializedData = reader.ReadUInt32(); this.sizeOfUninitializedData = reader.ReadUInt32(); this.addressOfEntryPoint = (RVA)reader.ReadUInt32(); this.baseOfCode = (RVA)reader.ReadUInt32(); this.baseOfData = (RVA)reader.ReadUInt32(); this.imageBase = reader.ReadUInt32(); this.sectionAlignment = reader.ReadUInt32(); this.fileAlignment = reader.ReadUInt32(); this.majorOperatingSystemVersion = reader.ReadUInt16(); this.minorOperatingSystemVersion = reader.ReadUInt16(); this.majorImageVersion = reader.ReadUInt16(); this.minorImageVersion = reader.ReadUInt16(); this.majorSubsystemVersion = reader.ReadUInt16(); this.minorSubsystemVersion = reader.ReadUInt16(); this.win32VersionValue = reader.ReadUInt32(); this.sizeOfImage = reader.ReadUInt32(); this.sizeOfHeaders = reader.ReadUInt32(); this.checkSum = reader.ReadUInt32(); this.subsystem = (Subsystem)reader.ReadUInt16(); this.dllCharacteristics = (DllCharacteristics)reader.ReadUInt16(); this.sizeOfStackReserve = reader.ReadUInt32(); this.sizeOfStackCommit = reader.ReadUInt32(); this.sizeOfHeapReserve = reader.ReadUInt32(); this.sizeOfHeapCommit = reader.ReadUInt32(); this.loaderFlags = reader.ReadUInt32(); this.numberOfRvaAndSizes = reader.ReadUInt32(); for (int i = 0; i < dataDirectories.Length; i++) { uint len = (uint)(reader.Position - startOffset); if (len + 8 <= totalSize) dataDirectories[i] = new ImageDataDirectory(reader, verify); else dataDirectories[i] = new ImageDataDirectory(); } reader.Position = (long)startOffset + totalSize; SetEndoffset(reader); }