public static unsafe void WritePEChecksum(string filePath, uint checkSum) { using (var accessor = new StreamAccessor(new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite))) { // DOS DOSHeader dosHeader; fixed(byte *pBuff = accessor.ReadBytes(PEConstants.DosHeaderSize)) { dosHeader = *(DOSHeader *)pBuff; } if (dosHeader.Signature != PEConstants.DosSignature) { throw new BadImageFormatException(SR.DOSHeaderSignatureNotValid); } accessor.Position = dosHeader.Lfanew; // NT Signature if (accessor.ReadUInt32() != PEConstants.NTSignature) { throw new BadImageFormatException(SR.PESignatureNotValid); } // COFF accessor.ReadBytes(PEConstants.COFFHeaderSize); // PE ushort peMagic = accessor.ReadUInt16(); if (peMagic == PEConstants.PEMagic32) { accessor.ReadBytes(62); } else if (peMagic == 0x20b) { accessor.ReadBytes(62); } else { throw new BadImageFormatException(SR.PEHeaderSignatureNotValid); } accessor.Write((uint)checkSum); } }
private void ReadExtHeader(StreamAccessor accessor, out uint crc32, out int padding) { uint num = accessor.ReadUInt32(EncodingType.U4BE); if ((num != 6) && (num != 10)) { throw new FileFormatException("Extended Header Size は 6B 亦は 10B で在るべきです。"); } ushort num2 = accessor.ReadUInt16(EncodingType.I2BE); this.hascrc = (num2 & 0x8000) != 0; if (num != (this.hascrc ? ((long)10) : ((long)6))) { throw new FileFormatException(string.Format("Extended Header Size の大きさが不正です。\r\nCRC Checkを{0}場合には{1}B であるべきです。", this.hascrc ? "行う" : "行わない", this.hascrc ? "10" : "6")); } padding = accessor.ReadInt32(EncodingType.U4BE); crc32 = this.hascrc ? accessor.ReadUInt32(EncodingType.U4BE) : 0; }