Пример #1
0
        public void Sr_ReadArray()
        {
            var rdr = new LeImageReader(new byte[] {
                0x4A, 0x4B,             // signature
                0x08, 0x00, 0x00, 0x00, // pointer to directory
                0xFF, 0xFF,             // padding
                0x10, 0,                // Directory slot 0
                0x13, 0,                // Directory slot 1
                0x16, 0,                // Directory slot 2
                0x19, 0,                // Directory slot 3
                0x61, 0x62, 0x00,
                0x63, 0x64, 0x00,
                0x65, 0x66, 0x00,
                0x65, 0x78, 0x00,
            });
            var test = new TestStruct6();
            var sr   = new StructureReader(test);

            sr.Read(rdr);

            Assert.IsNotNull(test.directory);
            Assert.IsNotNull(test.directory.sections);
            Assert.AreEqual(4, test.directory.sections.Length);
            Assert.AreEqual("ab", test.directory.sections[0].name);
            Assert.AreEqual("cd", test.directory.sections[1].name);
            Assert.AreEqual("ef", test.directory.sections[2].name);
            Assert.AreEqual("ex", test.directory.sections[3].name);
        }
Пример #2
0
        public DescriptorStructure(PsdReader reader, bool hasVersion)
        {
            if (hasVersion == true)
            {
                this.version = reader.ReadInt32();
            }

            this.Add("Name", reader.ReadString());
            this.Add("ClassID", reader.ReadKey());

            int count = reader.ReadInt32();

            for (int i = 0; i < count; i++)
            {
                string key    = reader.ReadKey();
                string osType = reader.ReadType();
                if (key == "EngineData")
                {
                    this.Add(key.Trim(), new StructureEngineData(reader));
                }
                else
                {
                    object value = StructureReader.Read(osType, reader);
                    this.Add(key.Trim(), value);
                }
            }
        }
Пример #3
0
        private bool TryLoadHeader(BeImageReader rdr, out PrgHeader hdr)
        {
            var sr = new StructureReader <PrgHeader>(rdr);

            hdr = sr.Read();
            return(hdr.Magic == 0x601A);
        }
Пример #4
0
        public override Program Load(Address addrLoad)
        {
            var ext       = new StructureReader <SOM_Header>(MakeReader(0));
            var somHeader = ext.Read();

            if (somHeader.aux_header_location == 0)
            {
                throw new BadImageFormatException();
            }

            var spaces    = ReadSpaces(somHeader.space_location, somHeader.space_total, somHeader.space_strings_location);
            var subspaces = ReadSubspaces(somHeader.subspace_location, somHeader.subspace_total, somHeader.space_strings_location);

            var rdr       = MakeReader(somHeader.aux_header_location);
            var auxReader = new StructureReader <aux_id>(rdr);
            var aux       = auxReader.Read();

            switch (aux.type)
            {
            case aux_id_type.exec_aux_header:
                var program = LoadExecSegments(rdr);
                SetProgramOptions(somHeader, program);
                return(program);

            default:
                throw new BadImageFormatException();
            }
        }
Пример #5
0
        public void Sr_ReadLeInt32_Field()
        {
            var rdr  = new LeImageReader(new byte[] { 0x34, 0x12, 0xAB, 0xCD, 0x78, 0x56, 0x34, 0x12 }, 0);
            var test = new TestStruct2();
            var sr   = new StructureReader(test);

            sr.Read(rdr);

            Assert.AreEqual((int)0x12345678, test.lField);
        }
Пример #6
0
        public void Sr_ReadLeUInt16_Field()
        {
            var rdr  = new LeImageReader(new byte[] { 0x34, 0x12 }, 0);
            var test = new TestStruct();
            var sr   = new StructureReader(test);

            sr.Read(rdr);

            Assert.AreEqual((ushort)0x1234, test.usField);
        }
Пример #7
0
        private List <(string, SpaceDictionaryRecord)> ReadSpaces(uint space_location, uint count, uint uStrings)
        {
            var spaces = new List <(string, SpaceDictionaryRecord)>();
            var rdr    = new StructureReader <SpaceDictionaryRecord>(MakeReader(space_location));

            for (; count > 0; --count)
            {
                var space = rdr.Read();
                var name  = ReadString(space.name, uStrings);
                spaces.Add((name, space));
            }
            return(spaces);
        }
Пример #8
0
        public override Program Load(Address?addrLoad)
        {
            var sr  = new StructureReader <MorphosHeader>(new BeImageReader(this.RawImage));
            var hdr = sr.Read();
            var embeddedElfImage = new byte[this.RawImage.Length - hdr.ElfOffset];

            //$PERF: this is a prime candidate for Span<T>
            Array.Copy(this.RawImage, hdr.ElfOffset, embeddedElfImage, 0, embeddedElfImage.Length);
            this.elfLdr = new ElfImageLoader(this.Services, this.Filename, embeddedElfImage);
            var program = elfLdr.Load(addrLoad);

            return(program);
        }
Пример #9
0
        private bool TryLoadHeader(BeImageReader rdr, out PrgHeader hdr)
        {
            var sr = new StructureReader <PrgHeader>(rdr);
            var h  = sr.Read();

            if (h.Magic != 0x601A)
            {
                hdr = default(PrgHeader);
                return(false);
            }
            hdr = h;
            return(true);
        }
Пример #10
0
        private List <(string?, SubspaceDictionaryRecord)> ReadSubspaces(uint subspace_location, uint count, uint uStrings)
        {
            var subspaces = new List <(string?, SubspaceDictionaryRecord)>();
            var rdr       = new StructureReader <SubspaceDictionaryRecord>(MakeReader(subspace_location));

            for (; count > 0; --count)
            {
                var subspace = rdr.Read();
                var name     = ReadString(subspace.name, uStrings);
                var access   = subspace.attributes >> 25;
                subspaces.Add((name, subspace));
            }
            return(subspaces);
        }
Пример #11
0
        public void Sr_ReadStructure()
        {
            var rdr = new LeImageReader(new byte[] {
                0x4A, 0x4B,             // signature
                0x08, 0x00, 0x00, 0x00, // pointer to struct
                0xFF, 0xFF,             // padding
                0x34, 0x12,             // structure.
            });
            var test = new TestStruct5();
            var sr   = new StructureReader(test);

            sr.Read(rdr);

            Assert.IsNotNull(test.extra);
            Assert.AreEqual((ushort)0x1234, test.extra.sig);
        }
Пример #12
0
        public void Sr_ReadLeInt32_String()
        {
            var rdr = new LeImageReader(new byte[] {
                0x34, 0x12,
                0xAB, 0xCD,
                0x48, 0x69, 0x00,
                0x42, 0x79, 0x65, 0x21, 0x00
            });
            var test = new TestStruct4();
            var sr   = new StructureReader(test);

            sr.Read(rdr);

            Assert.AreEqual("Hi", test.sField04);
            Assert.AreEqual("Bye!", test.sFieldnn);
        }
Пример #13
0
        public override Program Load(Address addrLoad)
        {
            var cfgSvc = Services.RequireService <IConfigurationService>();

            this.arch = cfgSvc.GetArchitecture("x86-protected-32");
            var rdr = new LeImageReader(RawImage, this.lfaNew);

            var hdrReader = new StructureReader <LXHeader>(rdr);

            this.hdr = hdrReader.Read();

            LoadModuleTable();
            var leSegs = LoadSegmentTable();

            var segments = MakeSegmentMap(addrLoad, leSegs);
            var platform = MakePlatform();

            return(new Program(segments, arch, platform));
        }
Пример #14
0
        private Program LoadExecSegments(BeImageReader rdr)
        {
            var segments   = new List <ImageSegment>();
            var execAuxRdr = new StructureReader <SOM_Exec_aux_hdr>(rdr);
            var execAux    = execAuxRdr.Read();

            var cfgSvc = Services.RequireService <IConfigurationService>();
            var arch   = cfgSvc.GetArchitecture("paRisc");

            var dlHeaderRdr = ReadDynamicLibraryInfo(execAux, arch);

            var textBytes = new byte[execAux.exec_tsize];
            var textAddr  = Address.Ptr32(execAux.exec_tmem);

            Array.Copy(RawImage, (int)execAux.exec_tfile, textBytes, 0, textBytes.Length);
            var textSeg = new ImageSegment(
                ".text",
                new MemoryArea(textAddr, textBytes),
                AccessMode.ReadExecute);

            segments.Add(textSeg);

            var dataBytes = new byte[execAux.exec_dsize];
            var dataAddr  = Address.Ptr32(execAux.exec_tmem);

            Array.Copy(RawImage, (int)execAux.exec_dfile, dataBytes, 0, dataBytes.Length);
            var dataSeg = new ImageSegment(
                ".data",
                new MemoryArea(dataAddr, dataBytes),
                AccessMode.ReadWrite);

            segments.Add(dataSeg);

            var segmap = new SegmentMap(
                segments.Min(s => s.Address),
                segments.ToArray());
            var platform = cfgSvc.GetEnvironment("hpux").Load(Services, arch);

            return(new Program(segmap, arch, platform));
        }
Пример #15
0
        Segment[] LoadSegmentTable()
        {
            var objectTableEntries     = new ObjectTableEntry[hdr.obj_no];
            var objectPageTableEntries = new ObjectPageTableEntry[hdr.module_pages_no];

            var rdr = new LeImageReader(RawImage, hdr.obj_table_off + lfaNew);
            var objTblEntryReader = new StructureReader <ObjectTableEntry>(rdr);

            for (int i = 0; i < hdr.obj_no; i++)
            {
                objectTableEntries[i] = objTblEntryReader.Read();
            }

            rdr = new LeImageReader(RawImage, hdr.obj_page_table_off + lfaNew);

            if (hdr.signature == SIGNATURE16)
            {
                var objPageTblEntry16Reader = new StructureReader <ObjectPageTableEntry16>(rdr);

                for (int i = 0; i < hdr.module_pages_no; i++)
                {
                    ObjectPageTableEntry16 page16 = objPageTblEntry16Reader.Read();

                    int pageNo = (page16.High << 8) + page16.Low;

                    objectPageTableEntries[i] = new ObjectPageTableEntry
                    {
                        DataSize       = (ushort)hdr.page_size,
                        Flags          = (PageTableAttributes)page16.Flags,
                        PageDataOffset = (uint)((pageNo - 1) * hdr.page_size)
                    };
                }
            }
            else
            {
                var objPageTblEntryReader = new StructureReader <ObjectPageTableEntry>(rdr);
                for (int i = 0; i < hdr.module_pages_no; i++)
                {
                    objectPageTableEntries[i] = objPageTblEntryReader.Read();
                }
            }

            int debugSections   = 0;
            int winrsrcSections = 0;

            if (hdr.debug_info_len > 0)
            {
                debugSections = 1;
            }
            if (hdr.win_res_len > 0)
            {
                winrsrcSections = 1;
            }

            Segment[] sections = new Segment[objectTableEntries.Length + debugSections + winrsrcSections];
            for (int i = 0; i < objectTableEntries.Length; i++)
            {
                sections[i] = new Segment {
                    Flags = objectTableEntries[i].ObjectFlags
                };
                if (objectTableEntries[i].ObjectFlags.HasFlag(ObjectFlags.Resource))
                {
                    sections[i].Name = ".rsrc";
                }
                else if (objectTableEntries[i].ObjectFlags.HasFlag(ObjectFlags.Executable))
                {
                    sections[i].Name = ".text";
                }
                else if (!objectTableEntries[i].ObjectFlags.HasFlag(ObjectFlags.Writable))
                {
                    sections[i].Name = ".rodata";
                }
                else if (new LeImageReader(objectTableEntries[i].Name).ReadCString(PrimitiveType.Char, Encoding.ASCII).ToString().ToLower() == "bss")
                {
                    sections[i].Name = ".bss";
                }
                else if (!string.IsNullOrWhiteSpace(new LeImageReader(objectTableEntries[i].Name).ReadCString(PrimitiveType.Char, Encoding.ASCII).ToString().Trim()))
                {
                    sections[i].Name = new LeImageReader(objectTableEntries[i].Name).ReadCString(PrimitiveType.Char, Encoding.ASCII).ToString().Trim();
                }
                else
                {
                    sections[i].Name = ".data";
                }

                if (objectTableEntries[i].PageTableEntries == 0 ||
                    objectTableEntries[i].PageTableIndex > objectPageTableEntries.Length)
                {
                    sections[i].DataLength = objectTableEntries[i].VirtualSize;
                    continue;
                }

                int shift = (int)(hdr.signature == SIGNATURE16 ? 0 : hdr.page_off_shift);

                if (objectPageTableEntries[objectTableEntries[i].PageTableIndex - 1]
                    .Flags.HasFlag(PageTableAttributes.IteratedDataPage))
                {
                    sections[i].DataOffset =
                        (objectPageTableEntries[objectTableEntries[i].PageTableIndex - 1].PageDataOffset << shift) +
                        hdr.obj_iter_pages_off;
                }
                else if (objectPageTableEntries[objectTableEntries[i].PageTableIndex - 1]
                         .Flags.HasFlag(PageTableAttributes.LegalPhysicalPage))
                {
                    sections[i].DataOffset =
                        (objectPageTableEntries[objectTableEntries[i].PageTableIndex - 1].PageDataOffset << shift) +
                        hdr.data_pages_off;
                }
                else
                {
                    sections[i].DataOffset = 0;
                }

                sections[i].DataLength = 0;
                for (int j = 0; j < objectTableEntries[i].PageTableEntries; j++)
                {
                    sections[i].DataLength += objectPageTableEntries[j + objectTableEntries[i].PageTableIndex - 1].DataSize;
                }

                if (sections[i].DataOffset + sections[i].DataLength > RawImage.Length)
                {
                    sections[i].DataLength = (uint)RawImage.Length - sections[i].DataOffset;
                }

                sections[i].BaseAddress = objectTableEntries[i].RelocationBaseAddress;
            }

            if (winrsrcSections > 0)
            {
                sections[sections.Length - debugSections - winrsrcSections] = new Segment
                {
                    Name       = ".rsrc",
                    DataLength = hdr.win_res_len,
                    DataOffset = hdr.win_res_off
                }
            }
            ;

            if (debugSections > 0)
            {
                sections[sections.Length - debugSections] = new Segment
                {
                    Name       = ".debug",
                    DataLength = hdr.debug_info_len,
                    DataOffset = hdr.debug_info_off
                }
            }
            ;

            return(sections);
        }