public static void Erase(NativeModuleWriter writer, ModuleDefMD module) { if (writer == null || module == null) return; var sects = (IList)origSects.GetValue(writer); var sections = new List<Tuple<uint, uint, byte[]>>(); var s = new MemoryStream(); foreach (var origSect in sects) { var oldChunk = (BinaryReaderChunk)chunk.GetValue(origSect); var sectHdr = (ImageSectionHeader)peSection.GetValue(origSect); s.SetLength(0); oldChunk.WriteTo(new BinaryWriter(s)); var buf = s.ToArray(); var newChunk = new BinaryReaderChunk(MemoryImageStream.Create(buf), oldChunk.GetVirtualSize()); newChunk.SetOffset(oldChunk.FileOffset, oldChunk.RVA); chunk.SetValue(origSect, newChunk); sections.Add(Tuple.Create( sectHdr.PointerToRawData, sectHdr.PointerToRawData + sectHdr.SizeOfRawData, buf)); } var md = module.MetaData; var row = md.TablesStream.MethodTable.Rows; for (uint i = 1; i <= row; i++) { var method = md.TablesStream.ReadMethodRow(i); var codeType = ((MethodImplAttributes)method.ImplFlags & MethodImplAttributes.CodeTypeMask); if (codeType == MethodImplAttributes.IL) Erase(sections, (uint)md.PEImage.ToFileOffset((RVA)method.RVA)); } var res = md.ImageCor20Header.Resources; if (res.Size > 0) Erase(sections, (uint)res.StartOffset, res.Size); Erase(sections, md.ImageCor20Header); Erase(sections, md.MetaDataHeader); foreach (var stream in md.AllStreams) Erase(sections, stream); }
/// <summary> /// Writes the mixed-mode module to a stream. /// </summary> /// <param name="dest">Destination stream</param> /// <param name="options">Writer options</param> public void NativeWrite(Stream dest, DNW.NativeModuleWriterOptions options) { var writer = new DNW.NativeModuleWriter(this, options ?? new DNW.NativeModuleWriterOptions(this)); writer.Write(dest); }
/// <summary> /// Writes the mixed-mode module to a file on disk. If the file exists, it will be overwritten. /// </summary> /// <param name="filename">Filename</param> /// <param name="options">Writer options</param> public void NativeWrite(string filename, DNW.NativeModuleWriterOptions options) { var writer = new DNW.NativeModuleWriter(this, options ?? new DNW.NativeModuleWriterOptions(this)); writer.Write(filename); }