internal void Load(BuildModule module) { if (_module != null) { throw new InvalidOperationException(); } _module = module; if (_isNew) { if (_bufferLength == 0) { var image = _module.Image; _moduleTableOffset = BuildAssembly.RowSize * image.GetAssemblyCount(); _typeTableOffset = _moduleTableOffset + BuildModule.RowSize; _methodTableOffset = _typeTableOffset + (BuildType.RowSize * image.GetTypeDefCount()); _fieldTableOffset = _methodTableOffset + (BuildMethod.RowSize * image.GetMethodCount()); _propertyTableOffset = _fieldTableOffset + (BuildField.RowSize * image.GetFieldCount()); _eventTableOffset = _propertyTableOffset + (BuildProperty.RowSize * image.GetPropertyCount()); _resourceTableOffset = _eventTableOffset + (BuildEvent.RowSize * image.GetEventCount()); _bufferLength = _resourceTableOffset + (BuildResource.RowSize * image.GetManifestResourceCount()); } _buffer = new byte[_bufferLength]; _blobs = new BlobList(); _strings = new StringSerializer(); _signatures = new SignatureList(_strings); _objects = new ModuleObjectState(this); } else { using (var accessor = new StreamAccessor(new FileStream(_stateFilePath, FileMode.Open, FileAccess.Read, FileShare.None))) { _bufferLength = accessor.Read7BitEncodedInt(); _buffer = accessor.ReadBytes(_bufferLength); _objects = new ModuleObjectState(this, accessor); _blobs = new BlobList(accessor); var stringBlob = new Blob(accessor.ReadBytes(accessor.Read7BitEncodedInt())); StrongCryptoUtils.Decrypt(stringBlob.GetBuffer(), 0, stringBlob.Length); _strings = new StringSerializer(new BlobAccessor(stringBlob)); _signatures = new SignatureList(accessor, _strings); } } _strings.DelayWrite = true; _signatures.DelayWrite = true; }
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); } }