/// <summary> /// Appends raw data to the stream. /// </summary> /// <param name="data">The data to append.</param> /// <returns>The index to the start of the data.</returns> /// <remarks> /// This method does not index the blob data. Calling <see cref="AppendRawData"/> or <see cref="GetBlobIndex(byte[])"/> /// on the same data will append the data a second time. /// </remarks> public uint AppendRawData(byte[] data) { uint offset = (uint)_rawStream.Length; _writer.WriteBytes(data, 0, data.Length); return(offset); }
private void CreateExeFile(string outputFile) { var peInfo = WindowsAssembly.FromFile(Path); using (var inputFs = new FileStream(Path, FileMode.Open)) using (var input = new ExtendedBinaryReader(inputFs, FileEncoding, Endianness.LittleEndian)) using (var outputFs = new FileStream(outputFile, FileMode.Create)) { var output = new BinaryStreamWriter(outputFs); var writingContext = new WritingContext(peInfo, new BinaryStreamWriter(outputFs)); var dosHeader = input.ReadBytes((int)peInfo.NtHeaders.StartOffset); output.WriteBytes(dosHeader); var ntHeader = peInfo.NtHeaders; ntHeader.FileHeader.NumberOfSections++; ntHeader.OptionalHeader.SizeOfImage += 0x00010000; ntHeader.Write(writingContext); var newSection = CreateTFSection(peInfo.SectionHeaders[peInfo.SectionHeaders.Count - 1], ntHeader.OptionalHeader.FileAlignment, ntHeader.OptionalHeader.SectionAlignment); peInfo.SectionHeaders.Add(newSection); foreach (var section in peInfo.SectionHeaders) { section.Write(writingContext); } foreach (var section in peInfo.SectionHeaders) { input.Seek(section.PointerToRawData, SeekOrigin.Begin); outputFs.Seek(section.PointerToRawData, SeekOrigin.Begin); var data = input.ReadBytes((int)section.SizeOfRawData); output.WriteBytes(data); } var bytes = new byte[0x00010000]; output.WriteBytes(bytes); } }
public override MetadataStream CreateStream() { using (var stream = new MemoryStream()) { var writer = new BinaryStreamWriter(stream); foreach (var guid in _guidOffsetMapping.Keys) { writer.WriteBytes(guid.ToByteArray()); } return(new GuidStream(new MemoryStreamReader(stream.ToArray()))); } }
public override MetadataStream CreateStream() { using (var stream = new MemoryStream()) { var writer = new BinaryStreamWriter(stream); writer.WriteByte(0); foreach (var value in _stringOffsetMapping.Keys) { writer.WriteCompressedUInt32((uint)Encoding.Unicode.GetByteCount(value) + 1); writer.WriteBytes(Encoding.Unicode.GetBytes(value)); writer.WriteByte(0); } return(new UserStringStream(new MemoryStreamReader(stream.ToArray()))); } }
public void WriteTo(BinaryStreamWriter writer) { WriteDosHeader(this.DosHeader, writer); if (this.DosStub != null) { writer.WriteBytes(this.DosStub, 0, this.DosStub.Length); } WritePEHeader(this.PEHeader, writer); WriteOptionalHeader(this.OptionalHeader, writer); foreach (var s in this.SectionHeaders) { WriteSectionHeader(writer, s); } }
public override MetadataStream CreateStream() { using (var stream = new MemoryStream()) { var writer = new BinaryStreamWriter(stream); writer.WriteByte(0); foreach (string value in _stringOffsetMapping.Keys) { writer.WriteBytes(Encoding.UTF8.GetBytes(value)); writer.WriteByte(0); } writer.WriteZeroes((int)(FileSegment.Align(_length, 4) - _length)); return(new StringStream(new MemoryStreamReader(stream.ToArray()))); } }
static void Main(string[] args) { var pe = new PEFile(); var stream = new MemoryStream(Properties.Resources.console_anycpu); var reader = new BinaryStreamReader(stream, new byte[1024]); pe.ReadFrom(reader); uint lowestPointerToRawData = uint.MaxValue; uint lowestVirtualAddress = uint.MaxValue; uint highestVirtualAddress = uint.MinValue; foreach (var s in pe.SectionHeaders) { lowestPointerToRawData = Math.Min(lowestPointerToRawData, s.PointerToRawData); lowestVirtualAddress = Math.Min(lowestVirtualAddress, s.VirtualAddress); highestVirtualAddress = Math.Max(highestVirtualAddress, s.VirtualAddress + (uint)s.VirtualSize); } byte[] allSectionContent = new byte[highestVirtualAddress - lowestVirtualAddress]; foreach (var s in pe.SectionHeaders) { reader.Position = s.PointerToRawData; reader.ReadBytes(allSectionContent, (int)(s.VirtualAddress - lowestVirtualAddress), (int)s.VirtualSize); } pe.PEHeader.NumberOfSections = 1; var singleSection = pe.SectionHeaders[0]; singleSection.VirtualSize = (uint)allSectionContent.Length; pe.SectionHeaders = new[] { singleSection }; using (var peFileStream = File.Create("console.anycpu.insane.exe")) { var writer = new BinaryStreamWriter(peFileStream); pe.WriteTo(writer); writer.Position = lowestPointerToRawData; writer.WriteBytes(allSectionContent, 0, allSectionContent.Length); } }
public void WriteForwardIterator(TypeReference type) { Write(CustomMetadataType.ForwardIterator, () => writer.WriteBytes(Encoding.Unicode.GetBytes(type.Name))); }