Ejemplo n.º 1
0
        internal void Load(Stream s)
        {
            s.AssumeArgNotNull("s");

            RVA = s.ReadUIntLE();
            Size = s.ReadUIntLE();
        }
Ejemplo n.º 2
0
        internal void Load(Stream stream, long size)
        {
            stream.AssumeArgNotNull("stream");
            size.AssumeArgGte(s_minSize, "size");

            using(var subStream = new SubStream(stream, size, "Invalid meta-data root."))
            {
                subStream.ReadUIntLE().AssumeEquals(
                    0x424A5342U,
                    "Incorrect meta-data signature."
                );

                MajorVersion = subStream.ReadUShortLE();
                MinorVersion = subStream.ReadUShortLE();
                Reserved = subStream.ReadUIntLE();
                var versionLen =
                    (int)(
                        subStream.ReadUIntLE().AssumeLte(
                            256,
                            "CLR Version string too long"
                        ));

                Util.Assume(
                    versionLen % 4 == 0,
                    "Mis-algned version string length in CLR Header."
                );

                Version =
                    Encoding.UTF8.GetString(
                        subStream.ReadBytes(versionLen)
                    ).TrimEnd('\0');

                Flags = subStream.ReadUShortLE();

                var numberOfStreams = subStream.ReadUShortLE();
                var streamHeaders = new StreamHeader[numberOfStreams];

                var b = new StringBuilder();

                for (var i = 0; i < numberOfStreams; ++i)
                {
                    streamHeaders[i] = new StreamHeader();
                    streamHeaders[i].Load(subStream, b);
                }

                StreamHeaders =
                    new ReadOnlyDictionary<string,StreamHeader>(
                        streamHeaders.ToDictionary(h=>h.Name)
                    );
            }
        }
Ejemplo n.º 3
0
        internal void Load(Stream s)
        {
            s.AssumeArgNotNull("s");

            HeaderSize = s.ReadUIntLE();
            MajorRuntimeVersion = s.ReadUShortLE();
            MinorRuntimeVersion = s.ReadUShortLE();
            MetaDataPointer.Load(s);
            Flags = (ClrHeaderFlags) s.ReadUIntLE();
            EntryPointToken = s.ReadUIntLE();
            Resources.Load(s);
            StrongNameSignature.Load(s);
            CodeManagerTable.Load(s);
            VTableFixups.Load(s);
            ExportAddressTableJumps.Load(s);
            ManagedNativeHeader.Load(s);
        }
Ejemplo n.º 4
0
        internal void Load(Stream s)
        {
            s.AssumeArgNotNull("s");

            MachineType = ((MachineType)s.ReadUShortLE()).AssumeDefined();
            NumberOfSections = s.ReadUShortLE().AssumeGt(0, "Empty section table detected. All valid PE files should contain at least one section.");
            TimeDateStamp = s.ReadUIntLE();
            PointerToSymbolTable = s.ReadUIntLE().AssumeEquals(0u);
            NumberOfSymbols = s.ReadUIntLE().AssumeEquals(0u);
            SizeOfOptionalHeader = s.ReadUShortLE();
            Characteristics = (FileCharacteristics)s.ReadUShortLE();

            Util.Assume(SizeOfOptionalHeader >= 92, "Unexpected optional header size");

            var optHeaderStart = s.Position;

            ImageType = ((ImageType)s.ReadUShortLE()).AssumeDefined();

            if (ImageType == ImageType.PE32PlusFile)
            {
                Util.Assume(SizeOfOptionalHeader >= 108, "Unexpected optional header size");
            }

            MajorLinkerVersion = s.ReadByteOrThrow();
            MinorLinkerVersion = s.ReadByteOrThrow();
            SizeOfCode = s.ReadUIntLE();
            SizeOfInitializedData = s.ReadUIntLE();
            SizeOfUnInitializedData = s.ReadUIntLE();
            EntryPointRVA = s.ReadUIntLE();
            BaseOfCode = s.ReadUIntLE();
            if (ImageType == ImageType.PEFile)
            {
                BaseOfData = s.ReadUIntLE();
                ImageBase = s.ReadUIntLE();
            }
            else
            {
                ImageBase = s.ReadULongLE();
            }

            SectionAlignment = s.ReadUIntLE();
            FileAlignment = s.ReadUIntLE();
            MajorOSVersion = s.ReadUShortLE();
            MinorOSVersion = s.ReadUShortLE();
            MajorImageVersion = s.ReadUShortLE();
            MinorImageVersion = s.ReadUShortLE();
            MajorSubsystemVersion = s.ReadUShortLE();
            MinorSubsystemVersion = s.ReadUShortLE();
            Win32VersionValue = s.ReadUIntLE();
            SizeOfImage = s.ReadUIntLE();
            SizeOfHeaders = s.ReadUIntLE();
            CheckSum = s.ReadUIntLE();
            Subsystem = ((WindowsSubsystem) s.ReadUShortLE()).AssumeDefined();
            DllCharacteristics = (DllCharacteristics) s.ReadUShortLE();

            if (ImageType == ImageType.PEFile)
            {
                SizeOfStackReserve = s.ReadUIntLE();
                SizeOfStackCommit = s.ReadUIntLE();
                SizeOfHeapReserve = s.ReadUIntLE();
                SizeOfHeapCommit = s.ReadUIntLE();
            }
            else
            {
                SizeOfStackReserve = s.ReadULongLE();
                SizeOfStackCommit = s.ReadULongLE();
                SizeOfHeapReserve = s.ReadULongLE();
                SizeOfHeapCommit = s.ReadULongLE();
            }
            LoaderFlags = s.ReadUIntLE();

            var dataDirCount = s.ReadUIntLE();

            Util.Assume(dataDirCount * 8 <= SizeOfOptionalHeader - (s.Position - optHeaderStart), "Inconsistent header fields");

            if (dataDirCount != 0)
            {
                var dirs = new BlockReference[dataDirCount];

                for (int i = 0; i < dirs.Length; ++i)
                {
                    dirs[i] = new BlockReference(s);
                }

                DataDirectories = new ReadOnlyCollection<BlockReference>(dirs);
            }
        }