public override void Write(double value)
 {
     Write(ByteConverter.GetBytes(value));
 }
Beispiel #2
0
        public uint offset;                              // offset to OSRel instructions

        public OSImportInfo(byte[] file, int address)
        {
            id     = ByteConverter.ToUInt32(file, address);
            offset = ByteConverter.ToUInt32(file, address + 4);
        }
Beispiel #3
0
        public static void CompactEXE(ref byte[] exefile)
        {
            if (ByteConverter.ToUInt16(exefile, 0) != 0x5A4D)
            {
                return;
            }
            int ptr = ByteConverter.ToInt32(exefile, 0x3c);

            if (ByteConverter.ToInt32(exefile, (int)ptr) != 0x4550)             //PE\0\0
            {
                return;
            }
            ptr += 4;
            UInt16 numsects = ByteConverter.ToUInt16(exefile, (int)ptr + 2);

            ptr += 0x14;
            int  PEHead    = ptr;
            uint imageBase = ByteConverter.ToUInt32(exefile, ptr + 28);

            byte[] result = new byte[ByteConverter.ToInt32(exefile, ptr + 0xe0 + ((int)SectOffs.Size * (numsects - 1)) + (int)SectOffs.FAddr) + ByteConverter.ToInt32(exefile, ptr + 0xe0 + ((int)SectOffs.Size * (numsects - 1)) + (int)SectOffs.FSize)];
            Array.Copy(exefile, result, ByteConverter.ToUInt32(exefile, ptr + 60));
            ptr += 0xe0;
            for (int i = 0; i < numsects; i++)
            {
                Array.Copy(exefile, ByteConverter.ToInt32(exefile, ptr + (int)SectOffs.VAddr), result, ByteConverter.ToInt32(exefile, ptr + (int)SectOffs.FAddr), ByteConverter.ToInt32(exefile, ptr + (int)SectOffs.FSize));
                ptr += (int)SectOffs.Size;
            }
            exefile = result;
        }
Beispiel #4
0
 public OSModuleLink(byte[] file, int address)
 {
     next = ByteConverter.ToUInt32(file, address);
     prev = ByteConverter.ToUInt32(file, address + 4);
 }
Beispiel #5
0
 public OSSectionInfo(byte[] file, int address)
 {
     offset = ByteConverter.ToUInt32(file, address);
     size   = ByteConverter.ToUInt32(file, address + 4);
 }
Beispiel #6
0
 public override short ReadInt16()
 {
     return(ByteConverter.ToInt16(ReadBytes(sizeof(short)), 0));
 }
Beispiel #7
0
        public static void CreateNewSection(ref byte[] exefile, string name, byte[] data, bool isCode)
        {
            int ptr = ByteConverter.ToInt32(exefile, 0x3c);

            ptr += 4;
            UInt16 numsects   = ByteConverter.ToUInt16(exefile, ptr + 2);
            int    sectnumptr = ptr + 2;

            ptr += 0x14;
            int PEHead = ptr;

            ptr += 0xe0;
            int sectptr = ptr;

            ptr += (int)SectOffs.Size * numsects;
            ByteConverter.GetBytes((ushort)(numsects + 1)).CopyTo(exefile, sectnumptr);
            Array.Clear(exefile, ptr, 8);
            Encoding.ASCII.GetBytes(name).CopyTo(exefile, ptr);
            UInt32 vaddr = HelperFunctions.Align(ByteConverter.ToUInt32(exefile, ptr - (int)SectOffs.Size + (int)SectOffs.VAddr) + ByteConverter.ToUInt32(exefile, ptr - (int)SectOffs.Size + (int)SectOffs.VSize));

            ByteConverter.GetBytes(vaddr).CopyTo(exefile, ptr + (int)SectOffs.VAddr);
            UInt32 faddr = HelperFunctions.Align(ByteConverter.ToUInt32(exefile, ptr - (int)SectOffs.Size + (int)SectOffs.FAddr) + ByteConverter.ToUInt32(exefile, ptr - (int)SectOffs.Size + (int)SectOffs.FSize));

            ByteConverter.GetBytes(faddr).CopyTo(exefile, ptr + (int)SectOffs.FAddr);
            ByteConverter.GetBytes(isCode ? 0x60000020 : 0xC0000040).CopyTo(exefile, ptr + (int)SectOffs.Flags);
            int diff = (int)HelperFunctions.Align((uint)data.Length);

            ByteConverter.GetBytes(diff).CopyTo(exefile, ptr + (int)SectOffs.VSize);
            ByteConverter.GetBytes(diff).CopyTo(exefile, ptr + (int)SectOffs.FSize);
            if (isCode)
            {
                ByteConverter.GetBytes(Convert.ToUInt32(ByteConverter.ToUInt32(exefile, PEHead + 4) + diff)).CopyTo(exefile, PEHead + 4);
            }
            else
            {
                ByteConverter.GetBytes(Convert.ToUInt32(ByteConverter.ToUInt32(exefile, PEHead + 8) + diff)).CopyTo(exefile, PEHead + 8);
            }
            ByteConverter.GetBytes(Convert.ToUInt32(ByteConverter.ToUInt32(exefile, PEHead + 0x38) + diff)).CopyTo(exefile, PEHead + 0x38);
            Array.Resize(ref exefile, exefile.Length + diff);
            data.CopyTo(exefile, vaddr);
        }
Beispiel #8
0
 public override ulong ReadUInt64()
 {
     return(ByteConverter.ToUInt64(ReadBytes(sizeof(ulong)), 0));
 }
Beispiel #9
0
        public static void FixRELPointers(byte[] file)
        {
            OSModuleHeader header = new OSModuleHeader(file, 0);

            OSSectionInfo[] sections = new OSSectionInfo[header.info.numSections];
            for (int i = 0; i < header.info.numSections; i++)
            {
                sections[i] = new OSSectionInfo(file, (int)header.info.sectionInfoOffset + (i * 8));
            }
            OSImportInfo[] imports = new OSImportInfo[header.impSize / 8];
            for (int i = 0; i < imports.Length; i++)
            {
                imports[i] = new OSImportInfo(file, (int)header.impOffset + (i * 8));
            }
            int reladdr = 0;

            for (int i = 0; i < imports.Length; i++)
            {
                if (imports[i].id == header.info.id)
                {
                    reladdr = (int)imports[i].offset;
                    break;
                }
            }
            OSRel rel      = new OSRel(file, reladdr);
            int   dataaddr = 0;

            unchecked
            {
                while (rel.type != (byte)RelocTypes.R_DOLPHIN_END)
                {
                    dataaddr += rel.offset;
                    uint sectionbase = (uint)(sections[rel.section].offset & ~1);
                    switch (rel.type)
                    {
                    case 0x01:
                        ByteConverter.GetBytes(rel.addend + sectionbase).CopyTo(file, dataaddr);
                        break;

                    case 0x02:
                        ByteConverter.GetBytes((ByteConverter.ToUInt32(file, dataaddr) & 0xFC000003) | ((rel.addend + sectionbase) & 0x3FFFFFC)).CopyTo(file, dataaddr);
                        break;

                    case 0x03:
                    case 0x04:
                        ByteConverter.GetBytes((ushort)(rel.addend + sectionbase)).CopyTo(file, dataaddr);
                        break;

                    case 0x05:
                        ByteConverter.GetBytes((ushort)((rel.addend + sectionbase) >> 16)).CopyTo(file, dataaddr);
                        break;

                    case 0x06:
                        ByteConverter.GetBytes((ushort)(((rel.addend + sectionbase) >> 16) + (((rel.addend + sectionbase) & 0x8000) == 0x8000 ? 1 : 0))).CopyTo(file, dataaddr);
                        break;

                    case 0x0A:
                        ByteConverter.GetBytes((uint)((ByteConverter.ToUInt32(file, dataaddr) & 0xFC000003) | (((rel.addend + sectionbase) - dataaddr) & 0x3FFFFFC))).CopyTo(file, dataaddr);
                        break;

                    case 0x00:
                    case (byte)RelocTypes.R_DOLPHIN_NOP:
                    case (byte)RelocTypes.R_DOLPHIN_END:
                        break;

                    case (byte)RelocTypes.R_DOLPHIN_SECTION:
                        dataaddr = (int)sectionbase;
                        break;

                    default:
                        throw new NotImplementedException();
                    }
                    reladdr += 8;
                    rel      = new OSRel(file, reladdr);
                }
            }
        }
Beispiel #10
0
 public override ushort ReadUInt16()
 {
     return(ByteConverter.ToUInt16(ReadBytes(sizeof(ushort)), 0));
 }
Beispiel #11
0
 public override uint ReadUInt32()
 {
     return(ByteConverter.ToUInt32(ReadBytes(sizeof(uint)), 0));
 }
Beispiel #12
0
 public override float ReadSingle()
 {
     return(ByteConverter.ToSingle(ReadBytes(sizeof(float)), 0));
 }
Beispiel #13
0
 public override long ReadInt64()
 {
     return(ByteConverter.ToInt64(ReadBytes(sizeof(long)), 0));
 }
Beispiel #14
0
 public override int ReadInt32()
 {
     return(ByteConverter.ToInt32(ReadBytes(sizeof(int)), 0));
 }
Beispiel #15
0
 public override void Write(float value)
 {
     Write(ByteConverter.GetBytes(value));
 }
Beispiel #16
0
        public static uint GetNewSectionAddress(byte[] exefile)
        {
            int ptr = ByteConverter.ToInt32(exefile, 0x3c);

            ptr += 4;
            UInt16 numsects = ByteConverter.ToUInt16(exefile, (int)ptr + 2);

            ptr += 0x14;
            ptr += 0xe0;
            ptr += (int)SectOffs.Size * (numsects - 1);
            return(HelperFunctions.Align(ByteConverter.ToUInt32(exefile, ptr + (int)SectOffs.VAddr) + ByteConverter.ToUInt32(exefile, ptr + (int)SectOffs.VSize)));
        }
Beispiel #17
0
 public override void Write(ulong value)
 {
     Write(ByteConverter.GetBytes(value));
 }
Beispiel #18
0
 public override double ReadDouble()
 {
     return(ByteConverter.ToDouble(ReadBytes(sizeof(double)), 0));
 }