Пример #1
0
        /// <inheritdoc />
        public override void Write(IBinaryStreamWriter writer)
        {
            ulong start = writer.Offset;

            writer.WriteUInt16((ushort)Magic);
            writer.WriteByte(MajorLinkerVersion);
            writer.WriteByte(MinorLinkerVersion);
            writer.WriteUInt32(SizeOfCode);
            writer.WriteUInt32(SizeOfInitializedData);
            writer.WriteUInt32(SizeOfUninitializedData);
            writer.WriteUInt32(AddressOfEntrypoint);
            writer.WriteUInt32(BaseOfCode);

            switch (Magic)
            {
            case OptionalHeaderMagic.Pe32:
                writer.WriteUInt32(BaseOfData);
                writer.WriteUInt32((uint)ImageBase);
                break;

            case OptionalHeaderMagic.Pe32Plus:
                writer.WriteUInt64(ImageBase);
                break;

            default:
                throw new BadImageFormatException("Unrecognized or unsupported optional header format.");
            }

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

            if (Magic == OptionalHeaderMagic.Pe32)
            {
                writer.WriteUInt32((uint)SizeOfStackReserve);
                writer.WriteUInt32((uint)SizeOfStackCommit);
                writer.WriteUInt32((uint)SizeOfHeapReserve);
                writer.WriteUInt32((uint)SizeOfHeapCommit);
            }
            else
            {
                writer.WriteUInt64(SizeOfStackReserve);
                writer.WriteUInt64(SizeOfStackCommit);
                writer.WriteUInt64(SizeOfHeapReserve);
                writer.WriteUInt64(SizeOfHeapCommit);
            }

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

            foreach (var directory in DataDirectories)
            {
                directory.Write(writer);
            }

            writer.WriteZeroes((int)(GetPhysicalSize() - (writer.Offset - start)));
        }
Пример #2
0
        /// <summary>
        /// Aligns the writer to a specified boundary.
        /// </summary>
        /// <param name="writer">The writer to align.</param>
        /// <param name="align">The boundary to use.</param>
        public static void Align(this IBinaryStreamWriter writer, uint align)
        {
            ulong currentPosition = writer.Offset;

            writer.WriteZeroes((int)(currentPosition.Align(align) - writer.Offset));
        }
Пример #3
0
 /// <summary>
 /// Aligns the writer to a specified boundary.
 /// </summary>
 /// <param name="writer">The writer to align.</param>
 /// <param name="align">The boundary to use.</param>
 public static void Align(this IBinaryStreamWriter writer, int align)
 {
     align--;
     writer.WriteZeroes((((int)writer.Position + align) & ~align) - (int)writer.Position);
 }