Пример #1
0
        static void WriteDosHeader(DosHeader dosHeader, BinaryStreamWriter writer)
        {
            writer.WriteUInt16((ushort)MZSignature.MZ);
            writer.WriteUInt16(dosHeader.cblp);
            writer.WriteUInt16(dosHeader.cp);
            writer.WriteUInt16(dosHeader.crlc);
            writer.WriteUInt16(dosHeader.cparhdr);
            writer.WriteUInt16(dosHeader.minalloc);
            writer.WriteUInt16(dosHeader.maxalloc);
            writer.WriteUInt16(dosHeader.ss);
            writer.WriteUInt16(dosHeader.sp);
            writer.WriteUInt16(dosHeader.csum);
            writer.WriteUInt16(dosHeader.ip);
            writer.WriteUInt16(dosHeader.cs);
            writer.WriteUInt16(dosHeader.lfarlc);
            writer.WriteUInt16(dosHeader.ovno);

            writer.WriteUInt64(dosHeader.res1);

            writer.WriteUInt16(dosHeader.oemid);
            writer.WriteUInt16(dosHeader.oeminfo);

            writer.WriteUInt32(dosHeader.ReservedNumber0);
            writer.WriteUInt32(dosHeader.ReservedNumber1);
            writer.WriteUInt32(dosHeader.ReservedNumber2);
            writer.WriteUInt32(dosHeader.ReservedNumber3);
            writer.WriteUInt32(dosHeader.ReservedNumber4);
            writer.WriteUInt32(dosHeader.lfanew);
        }
Пример #2
0
        static void WriteOptionalHeader(OptionalHeader optionalHeader, BinaryStreamWriter writer)
        {
            writer.WriteUInt16((ushort)optionalHeader.PEMagic);

            writer.WriteByte(optionalHeader.MajorLinkerVersion);
            writer.WriteByte(optionalHeader.MinorLinkerVersion);
            writer.WriteUInt32(optionalHeader.SizeOfCode);
            writer.WriteUInt32(optionalHeader.SizeOfInitializedData);
            writer.WriteUInt32(optionalHeader.SizeOfUninitializedData);
            writer.WriteUInt32(optionalHeader.AddressOfEntryPoint);
            writer.WriteUInt32(optionalHeader.BaseOfCode);

            if (optionalHeader.PEMagic == PEMagic.NT32)
            {
                writer.WriteUInt32(optionalHeader.BaseOfData);
                writer.WriteUInt32(checked((uint)optionalHeader.ImageBase));
            }
            else
            {
                writer.WriteUInt64(optionalHeader.ImageBase);
            }

            writer.WriteUInt32(optionalHeader.SectionAlignment);
            writer.WriteUInt32(optionalHeader.FileAlignment);
            writer.WriteUInt16(optionalHeader.MajorOperatingSystemVersion);
            writer.WriteUInt16(optionalHeader.MinorOperatingSystemVersion);
            writer.WriteUInt16(optionalHeader.MajorImageVersion);
            writer.WriteUInt16(optionalHeader.MinorImageVersion);
            writer.WriteUInt16(optionalHeader.MajorSubsystemVersion);
            writer.WriteUInt16(optionalHeader.MinorSubsystemVersion);
            writer.WriteUInt32(optionalHeader.Win32VersionValue);
            writer.WriteUInt32(optionalHeader.SizeOfImage);
            writer.WriteUInt32(optionalHeader.SizeOfHeaders);
            writer.WriteUInt32(optionalHeader.CheckSum);
            writer.WriteUInt16((ushort)optionalHeader.Subsystem);
            writer.WriteUInt16((ushort)optionalHeader.DllCharacteristics);

            if (optionalHeader.PEMagic == PEMagic.NT32)
            {
                writer.WriteUInt32(checked((uint)optionalHeader.SizeOfStackReserve));
                writer.WriteUInt32(checked((uint)optionalHeader.SizeOfStackCommit));
                writer.WriteUInt32(checked((uint)optionalHeader.SizeOfHeapReserve));
                writer.WriteUInt32(checked((uint)optionalHeader.SizeOfHeapCommit));
            }
            else
            {
                writer.WriteUInt64(optionalHeader.SizeOfStackReserve);
                writer.WriteUInt64(optionalHeader.SizeOfStackCommit);
                writer.WriteUInt64(optionalHeader.SizeOfHeapReserve);
                writer.WriteUInt64(optionalHeader.SizeOfHeapCommit);
            }

            writer.WriteUInt32(optionalHeader.LoaderFlags);
            writer.WriteUInt32(optionalHeader.NumberOfRvaAndSizes);

            for (int i = 0; i < optionalHeader.DataDirectories.Length; i++)
            {
                writer.WriteUInt32(optionalHeader.DataDirectories[i].VirtualAddress);
                writer.WriteUInt32(optionalHeader.DataDirectories[i].Size);
            }
        }