public static MethodImportDescriptor FromReader(DisReader reader)
 {
     return(new MethodImportDescriptor {
         Signature = reader.ReadInt32(),
         Name = reader.ReadString()
     });
 }
예제 #2
0
        public static ExportDescriptor FromReader(DisReader reader)
        {
            ExportDescriptor result = new ExportDescriptor();

            result.PC          = reader.ReadOP();
            result.FrameTypeId = reader.ReadOP();
            result.Signature   = reader.ReadInt32();
            result.Name        = reader.ReadString();

            return(result);
        }
예제 #3
0
        public static DataDescriptor FromReader(DisReader reader)
        {
            DataDescriptor result = new DataDescriptor {
                Code = 0
            };

            int code = reader.ReadByte();

            if (code == 0)
            {
                return(result);
            }

            int count = code & 0x0f;

            if (count == 0)
            {
                count = reader.ReadOP();
            }

            result.Code   = (code & 0xf0) >> 4;
            result.Count  = count;
            result.Offset = reader.ReadOP();

            int dataSize = 0;

            if (result.Code == INT8)
            {
                dataSize = 1;
            }
            else if (result.Code == INT32)
            {
                dataSize = 4;
            }
            else if (result.Code == FLOAT)
            {
                dataSize = 8;
            }
            else if (result.Code == STRING)
            {
                result.Data = Encoding.UTF8.GetBytes(reader.ReadString(count));
            }

            if (dataSize != 0 && count != 0)
            {
                result.Data = reader.ReadBytes(count * dataSize);

                // big endian to little endian
                result.Data = result.Data.Reverse().ToArray();
            }
            return(result);
        }
예제 #4
0
 public static ObjectFile FromFile(string path)
 {
     try
     {
         using (FileStream fs = File.OpenRead(path))
         {
             DisReader reader = new DisReader(fs);
             return(FromReader(reader));
         }
     }
     catch (IOException)
     {
         return(null);
     }
 }
        public static ModuleImportDescriptor FromReader(DisReader reader)
        {
            int size = reader.ReadOP();

            List <MethodImportDescriptor> imports = new List <MethodImportDescriptor> ();

            for (int i = 0; i < size; i++)
            {
                imports.Add(MethodImportDescriptor.FromReader(reader));
            }

            return(new ModuleImportDescriptor {
                Imports = imports.ToArray()
            });
        }
예제 #6
0
        public static Instruction FromReader(DisReader reader)
        {
            Instruction result = new Instruction();

            result.Opcode         = reader.ReadByte();
            result.AddressingMode = reader.ReadByte();

            int srcAddr    = result.AddressingMode & AddrMode.SOURCE_MASK;
            int destAddr   = result.AddressingMode & AddrMode.DESTINATION_MASK;
            int middleAddr = result.AddressingMode & AddrMode.MIDDLE_MASK;

            if (middleAddr == AddrMode.MIDDLE_NONE)
            {
                result.Middle = result.Destination;
            }
            else
            {
                result.Middle = reader.ReadOP();
            }

            if ((srcAddr == AddrMode.SOURCE_MPDOUBLEINDIRECT) ||
                (srcAddr == AddrMode.SOURCE_FPDOUBLEINDIRECT))
            {
                result.Source = (reader.ReadOP() << 16) | reader.ReadOP();
            }
            else if (srcAddr != AddrMode.SOURCE_NONE)
            {
                result.Source = reader.ReadOP();
            }

            if ((destAddr == AddrMode.DESTINATION_MPDOUBLEINDIRECT) ||
                (destAddr == AddrMode.DESTINATION_FPDOUBLEINDIRECT))
            {
                result.Destination = (reader.ReadOP() << 16) | reader.ReadOP();
            }
            else if (destAddr != AddrMode.DESTINATION_NONE)
            {
                result.Destination = reader.ReadOP();
            }

            return(result);
        }
예제 #7
0
        public static TypeDescriptor FromReader(DisReader reader)
        {
            TypeDescriptor result = new TypeDescriptor();

            result.Number = reader.ReadOP();
            result.Size   = reader.ReadOP();

            int nbytes = reader.ReadOP();

            result.PointerMap = new bool[nbytes * 8];

            if (nbytes != 0)
            {
                byte[] map = reader.ReadBytes(nbytes);
                for (int i = 0; i < nbytes * 8; i++)
                {
                    result.PointerMap[i] = ((map[i / 8] & (1 << (i % 8))) != 0);
                }
            }

            return(result);
        }
예제 #8
0
        public static ObjectFile FromReader(DisReader reader)
        {
            int magic = reader.ReadOP();

            if ((magic != XMAGIC) && (magic != SMAGIC))
            {
                throw new InvalidDataException("Invalid magic number");
            }

            if (magic == SMAGIC)
            {
                int    slength   = reader.ReadOP();
                byte[] signature = reader.ReadBytes(slength);
            }

            int runtimeFlag = reader.ReadOP();
            int stackExtent = reader.ReadOP();
            int codeSize    = reader.ReadOP();
            int dataSize    = reader.ReadOP();
            int typeSize    = reader.ReadOP();
            int linkSize    = reader.ReadOP();
            int entryPc     = reader.ReadOP();
            int entryType   = reader.ReadOP();

            ObjectFile result = new ObjectFile();

            result.ShareMP        = ((runtimeFlag & FLAG_SHAREMP) != 0);
            result.StackExtent    = stackExtent;
            result.EntryPC        = entryPc;
            result.EntryFrameType = entryType;

            result.Instructions = new Instruction[codeSize];
            for (int i = 0; i < codeSize; i++)
            {
                Instruction inst = Instruction.FromReader(reader);
                result.Instructions[i] = inst;
            }

            result.Types = new TypeDescriptor[typeSize];
            for (int i = 0; i < typeSize; i++)
            {
                result.Types[i] = TypeDescriptor.FromReader(reader);
            }


            List <DataDescriptor> data = new List <DataDescriptor> ();

            while (true)
            {
                DataDescriptor descriptor = DataDescriptor.FromReader(reader);
                if (descriptor.Code == 0)
                {
                    break;
                }

                data.Add(descriptor);
            }
            result.Data = data.ToArray();

            result.Name = reader.ReadString();

            result.Link = new ExportDescriptor[linkSize];
            for (int i = 0; i < linkSize; i++)
            {
                ExportDescriptor desc = ExportDescriptor.FromReader(reader);
                desc.FrameSize = result.Types[desc.FrameTypeId].Size;

                result.Link[i] = desc;
            }

            if (!reader.AtEndOfStream)
            {
                int importCount = reader.ReadOP();

                List <ModuleImportDescriptor> imports = new List <ModuleImportDescriptor> (importCount);
                for (int i = 0; i < importCount; i++)
                {
                    imports.Add(ModuleImportDescriptor.FromReader(reader));
                }

                result.Imports = imports.ToArray();
            }

            if (!reader.AtEndOfStream)
            {
                int handlerCount = reader.ReadOP();

                List <HandlerDescriptor> handlers = new List <HandlerDescriptor> (handlerCount);
                for (int i = 0; i < handlerCount; i++)
                {
                    handlers.Add(HandlerDescriptor.FromReader(reader));
                }

                result.Handlers = handlers.ToArray();
            }

            return(result);
        }
예제 #9
0
 public static HandlerDescriptor FromReader(DisReader reader)
 {
     return(new HandlerDescriptor());
 }