Exemplo n.º 1
0
        public bool Read()
        {
            var pos = file.Span.Start;

            DosHeader = PeDosHeaderDataImpl.TryCreate(file, pos);
            if (DosHeader == null)
            {
                return(false);
            }

            uint ntHeaderOffset = DosHeader.Lfanew.Data.ReadValue();

            pos = file.Span.Start + ntHeaderOffset;
            if (pos + 4 > file.Span.End)
            {
                return(false);
            }
            if (file.Buffer.ReadUInt32(pos) != 0x4550)
            {
                return(false);
            }
            pos       += 4;
            FileHeader = PeFileHeaderDataImpl.TryCreate(file, pos);
            if (FileHeader == null)
            {
                return(false);
            }
            pos = FileHeader.Span.End;
            uint sizeOfOptionalHeader = FileHeader.SizeOfOptionalHeader.Data.ReadValue();

            OptionalHeader = CreateOptionalHeader(pos, sizeOfOptionalHeader);
            if (OptionalHeader == null)
            {
                return(false);
            }
            pos = OptionalHeader.Span.Span.Start + sizeOfOptionalHeader;
            int sects = FileHeader.NumberOfSections.Data.ReadValue();

            Sections = CreateSections(pos, sects);
            if (Sections == null)
            {
                return(false);
            }

            var headers = new ImageSectionHeader[Sections.FieldCount];

            for (int i = 0; i < headers.Length; i++)
            {
                var h = Sections[i].Data;
                headers[i] = new ImageSectionHeader(h.VirtualSize.Data.ReadValue(), h.VirtualAddress.Data.ReadValue(), h.SizeOfRawData.Data.ReadValue(), h.PointerToRawData.Data.ReadValue());
            }
            SectionHeaders = headers;

            if (initFileLayout)
            {
                IsFileLayout = GuessIsFileLayout();
            }
            return(true);
        }
Exemplo n.º 2
0
        public bool Read()
        {
            var pos = file.Span.Start;

            DosHeader = PeDosHeaderDataImpl.TryCreate(file, pos);
            if (DosHeader == null)
            {
                return(false);
            }

            uint ntHeaderOffset = DosHeader.Lfanew.Data.ReadValue();

            pos = file.Span.Start + ntHeaderOffset;
            if (pos + 4 > file.Span.End)
            {
                return(false);
            }
            // Mono only checks the low 2 bytes
            if ((ushort)file.Buffer.ReadUInt32(pos) != 0x4550)
            {
                return(false);
            }
            pos       += 4;
            FileHeader = PeFileHeaderDataImpl.TryCreate(file, pos);
            if (FileHeader == null)
            {
                return(false);
            }
            pos = FileHeader.Span.End;
            uint sizeOfOptionalHeader = FileHeader.SizeOfOptionalHeader.Data.ReadValue();

            OptionalHeader = CreateOptionalHeader(pos, sizeOfOptionalHeader);
            if (OptionalHeader == null)
            {
                return(false);
            }
            pos = OptionalHeader.Span.Span.Start + sizeOfOptionalHeader;
            int sects = FileHeader.NumberOfSections.Data.ReadValue();

            if (sects > 0 && file.Span.Contains(pos + 0x28 - 1))
            {
                // Mono doesn't verify the section count
                uint firstSectionOffset = file.Buffer.ReadUInt32(pos + 0x14);
                sects = Math.Min(sects, (int)((firstSectionOffset - pos).ToUInt64() / 0x28));
            }
            Sections = CreateSections(pos, sects);
            if (Sections == null)
            {
                return(false);
            }

            var headers = new ImageSectionHeader[Sections.FieldCount];

            for (int i = 0; i < headers.Length; i++)
            {
                var h = Sections[i].Data;
                headers[i] = new ImageSectionHeader(h.VirtualSize.Data.ReadValue(), h.VirtualAddress.Data.ReadValue(), h.SizeOfRawData.Data.ReadValue(), h.PointerToRawData.Data.ReadValue());
            }
            SectionHeaders = headers;

            if (initFileLayout)
            {
                IsFileLayout = GuessIsFileLayout();
            }
            return(true);
        }