예제 #1
0
        /// <summary>
        /// Writes the PE file to the provided output stream.
        /// </summary>
        /// <param name="writer">The output stream to write to.</param>
        public void Write(IBinaryStreamWriter writer)
        {
            UpdateHeaders();

            // Dos header.
            DosHeader.Write(writer);

            // NT headers
            writer.FileOffset = DosHeader.NextHeaderOffset;

            writer.WriteUInt32(ValidPESignature);
            FileHeader.Write(writer);
            OptionalHeader.Write(writer);

            // Section headers.
            writer.FileOffset = OptionalHeader.FileOffset + FileHeader.SizeOfOptionalHeader;
            foreach (var section in Sections)
            {
                section.CreateHeader().Write(writer);
            }

            // Data between section headers and sections.
            ExtraSectionData?.Write(writer);

            // Sections.

            writer.FileOffset = OptionalHeader.SizeOfHeaders;
            foreach (var section in Sections)
            {
                writer.FileOffset = section.FileOffset;
                section.Contents.Write(writer);
                writer.Align(OptionalHeader.FileAlignment);
            }
        }
예제 #2
0
        internal void Write(PeWriter wtr)
        {
            wtr.SetPosition(0);
            if (t == PeFileType.Image)
            {
                dos.Write(wtr);

                wtr.SetPosition(dos.PEHeaderOffset);

                wtr.Write(sign);

                pe.Write(wtr);

                op.Write(wtr);

                sects.Write(wtr);

                certs.Save(wtr, op.DataDirectories[DataDirectoryType.Certificate].Address.Value);
            }
            else if (t == PeFileType.Object)
            {
                pe.Write(wtr);

                sects.Write(wtr);
            }
        }