Пример #1
0
        private long WriteFile()
        {
            Listener.OnWriterEvent(this, ModuleWriterEvent.BeginWritePdb);
            WritePdbFile();
            Listener.OnWriterEvent(this, ModuleWriterEvent.EndWritePdb);

            Listener.OnWriterEvent(this, ModuleWriterEvent.BeginCalculateRvasAndFileOffsets);
            var chunks = new List <IChunk>();

            chunks.Add(PEHeaders);
            foreach (var section in sections)
            {
                chunks.Add(section);
            }
            PEHeaders.PESections = sections;
            CalculateRvasAndFileOffsets(chunks, 0, 0, PEHeaders.FileAlignment, PEHeaders.SectionAlignment);
            Listener.OnWriterEvent(this, ModuleWriterEvent.EndCalculateRvasAndFileOffsets);

            InitializeChunkProperties();

            Listener.OnWriterEvent(this, ModuleWriterEvent.BeginWriteChunks);
            var writer = new BinaryWriter(destStream);

            WriteChunks(writer, chunks, 0, PEHeaders.FileAlignment);
            var imageLength = writer.BaseStream.Position - destStreamBaseOffset;

            Listener.OnWriterEvent(this, ModuleWriterEvent.EndWriteChunks);

            Listener.OnWriterEvent(this, ModuleWriterEvent.BeginStrongNameSign);
            if (Options.StrongNameKey != null)
            {
                StrongNameSign((long)strongNameSignature.FileOffset);
            }
            Listener.OnWriterEvent(this, ModuleWriterEvent.EndStrongNameSign);

            Listener.OnWriterEvent(this, ModuleWriterEvent.BeginWritePEChecksum);
            if (Options.AddCheckSum)
            {
                PEHeaders.WriteCheckSum(writer, imageLength);
            }
            Listener.OnWriterEvent(this, ModuleWriterEvent.EndWritePEChecksum);

            return(imageLength);
        }
Пример #2
0
        long WriteFile()
        {
            managedExportsWriter.AddExportedMethods(metaData.ExportedMethods, GetTimeDateStamp());
            if (managedExportsWriter.HasExports)
            {
                needStartupStub = true;
            }

            Listener.OnWriterEvent(this, ModuleWriterEvent.BeginWritePdb);
            WritePdbFile();
            Listener.OnWriterEvent(this, ModuleWriterEvent.EndWritePdb);

            Listener.OnWriterEvent(this, ModuleWriterEvent.BeginCalculateRvasAndFileOffsets);
            var chunks = new List <IChunk>();

            chunks.Add(peHeaders);
            if (!managedExportsWriter.HasExports)
            {
                sections.Remove(sdataSection);
            }
            if (!(relocDirectory.NeedsRelocSection || managedExportsWriter.HasExports || needStartupStub))
            {
                sections.Remove(relocSection);
            }

            importAddressTable.Enable = needStartupStub;
            importDirectory.Enable    = needStartupStub;
            startupStub.Enable        = needStartupStub;

            foreach (var section in sections)
            {
                chunks.Add(section);
            }
            peHeaders.PESections = sections;
            CalculateRvasAndFileOffsets(chunks, 0, 0, peHeaders.FileAlignment, peHeaders.SectionAlignment);
            Listener.OnWriterEvent(this, ModuleWriterEvent.EndCalculateRvasAndFileOffsets);

            InitializeChunkProperties();

            Listener.OnWriterEvent(this, ModuleWriterEvent.BeginWriteChunks);
            var writer = new BinaryWriter(destStream);

            WriteChunks(writer, chunks, 0, peHeaders.FileAlignment);
            long imageLength = writer.BaseStream.Position - destStreamBaseOffset;

            Listener.OnWriterEvent(this, ModuleWriterEvent.EndWriteChunks);

            Listener.OnWriterEvent(this, ModuleWriterEvent.BeginStrongNameSign);
            if (Options.StrongNameKey != null)
            {
                StrongNameSign((long)strongNameSignature.FileOffset);
            }
            Listener.OnWriterEvent(this, ModuleWriterEvent.EndStrongNameSign);

            Listener.OnWriterEvent(this, ModuleWriterEvent.BeginWritePEChecksum);
            if (Options.AddCheckSum)
            {
                peHeaders.WriteCheckSum(writer, imageLength);
            }
            Listener.OnWriterEvent(this, ModuleWriterEvent.EndWritePEChecksum);

            return(imageLength);
        }