Ejemplo n.º 1
0
 public static MSILInstruction Create(MSILOpCode opcode, uint metadataToken, NETHeader netHeader)
 {
     return(new MSILInstruction()
     {
         OpCode = opcode, OperandBytes = BitConverter.GetBytes(metadataToken), Operand = netHeader.TokenResolver.ResolveToken(metadataToken)
     });
 }
Ejemplo n.º 2
0
 internal MSILInstruction(int offset, MSILOpCode opcode, byte[] rawOperand, object operand)
 {
     Offset       = offset;
     OpCode       = opcode;
     Operand      = operand;
     OperandBytes = rawOperand;
 }
Ejemplo n.º 3
0
 internal MSILInstruction(int offset, MSILOpCode opcode, byte[] rawOperand, object operand)
 {
     Offset = offset;
     OpCode = opcode;
     Operand = operand;
     OperandBytes = rawOperand;
 }
Ejemplo n.º 4
0
        public static MSILInstruction Create(MSILOpCode opcode, object operand)
        {
            MSILInstruction instruction = new MSILInstruction()
            {
                OpCode  = opcode,
                Operand = operand,
            };

            return(instruction);
        }
Ejemplo n.º 5
0
        public MSILInstruction DisassembleNextInstruction()
        {
            int currentOffset = (int)(_reader.BaseStream.Position - _ilOffset);

            //if (currentOffset == 0x64)
            //    System.Diagnostics.Debugger.Break();

            MSILOpCode opcode = ReadNextOpCode();

            byte[] rawoperand = ReadRawOperand(opcode);
            object operand    = ConvertToOperand(currentOffset, opcode, rawoperand);

            return(new MSILInstruction(currentOffset, opcode, rawoperand, operand));
        }
Ejemplo n.º 6
0
        private byte[] ReadRawOperand(MSILOpCode opcode)
        {
            int size = opcode.Bytes.Length;

            switch (opcode.OperandType)
            {
            case OperandType.Int8:
            case OperandType.ShortArgument:
            case OperandType.ShortInstructionTarget:
            case OperandType.ShortVariable:
                return(_reader.ReadBytes(1));

            case OperandType.Argument:
            case OperandType.Variable:
                return(_reader.ReadBytes(2));

            case OperandType.Field:
            case OperandType.Int32:
            case OperandType.Float32:
            case OperandType.InstructionTarget:
            case OperandType.Method:
            case OperandType.Signature:
            case OperandType.String:
            case OperandType.Token:
            case OperandType.Type:
                return(_reader.ReadBytes(4));

            case OperandType.Float64:
            case OperandType.Int64:
                return(_reader.ReadBytes(8));

            case OperandType.InstructionTable:
                byte[] header  = _reader.ReadBytes(4);
                byte[] offsets = _reader.ReadBytes(BitConverter.ToInt32(header, 0) * sizeof(int));
                return(ASMGlobals.MergeBytes(header, offsets));
            }
            return(null);
        }
Ejemplo n.º 7
0
        private byte[] ReadRawOperand(MSILOpCode opcode)
        {
            int size = opcode.Bytes.Length;
            switch (opcode.OperandType)
            {
                case OperandType.Int8:
                case OperandType.ShortArgument:
                case OperandType.ShortInstructionTarget:
                case OperandType.ShortVariable:
                    return _reader.ReadBytes(1);

                case OperandType.Argument:
                case OperandType.Variable:
                    return _reader.ReadBytes(2);

                case OperandType.Field:
                case OperandType.Int32:
                case OperandType.Float32:
                case OperandType.InstructionTarget:
                case OperandType.Method:
                case OperandType.Signature:
                case OperandType.String:
                case OperandType.Token:
                case OperandType.Type:
                    return _reader.ReadBytes(4);

                case OperandType.Float64:
                case OperandType.Int64:
                    return _reader.ReadBytes(8);

                case OperandType.InstructionTable:
                    byte[] header = _reader.ReadBytes(4);
                    byte[] offsets = _reader.ReadBytes(BitConverter.ToInt32(header, 0) * sizeof(int));
                    return ASMGlobals.MergeBytes(header, offsets);
            }
            return null;
        }
Ejemplo n.º 8
0
        private object ConvertToOperand(int instructionOffset, MSILOpCode opcode, byte[] rawoperand)
        {
            try
            {
                switch (opcode.OperandType)
                {
                    case OperandType.Argument:
                        ParameterDefinition paramDef = GetParameter(BitConverter.ToInt16(rawoperand, 0));
                        if (paramDef == null)
                            return BitConverter.ToInt16(rawoperand, 0);
                        return paramDef;

                    case OperandType.ShortArgument:
                        paramDef = GetParameter(rawoperand[0]);
                        if (paramDef == null)
                            return rawoperand[0];
                        return paramDef;

                    case OperandType.Float32:
                        return BitConverter.ToSingle(rawoperand, 0);

                    case OperandType.Float64:
                        return BitConverter.ToDouble(rawoperand, 0);

                    case OperandType.InstructionTable:

                        int length = BitConverter.ToInt32(rawoperand, 0);
                        int[] offsets = new int[length];
                        int nextOffset = instructionOffset + (length * 4) + opcode.Bytes.Length + 4;
                        for (int i = 0; i < length; i++)
                        {
                            int index = (i + 1) * sizeof(int);
                            int roffset = BitConverter.ToInt32(rawoperand, index);
                            offsets[i] = roffset + nextOffset;
                        }

                        return offsets;

                    case OperandType.InstructionTarget:
                        return BitConverter.ToInt32(rawoperand, 0) + instructionOffset + opcode.Bytes.Length + sizeof(int);

                    case OperandType.ShortInstructionTarget:
                        return ASMGlobals.ByteToSByte(rawoperand[0]) + instructionOffset + opcode.Bytes.Length + sizeof(byte);

                    case OperandType.Int8:
                        return ASMGlobals.ByteToSByte(rawoperand[0]);

                    case OperandType.Int32:
                        return BitConverter.ToInt32(rawoperand, 0);

                    case OperandType.Int64:
                        return BitConverter.ToInt64(rawoperand, 0);

                    case OperandType.Token:
                    case OperandType.Field:
                    case OperandType.Method:
                    case OperandType.Type:
                        uint metadata = BitConverter.ToUInt32(rawoperand, 0);
                        try
                        {
                            object operand = TokenResolver.ResolveMember(metadata);

                            if (operand is ISpecification)
                                operand = (operand as ISpecification).TransformWith(MethodBody.Method);

                            return operand;
                        }
                        catch { return new TypeReference(string.Empty, "TOKEN:" + metadata.ToString("X8"), null); }

                    case OperandType.ShortVariable:
                        VariableDefinition varDef = GetVariable(rawoperand[0]);
                        if (varDef == null)
                            return rawoperand[0];
                        return varDef;

                    case OperandType.Variable:
                        varDef = GetVariable(BitConverter.ToInt16(rawoperand, 0));
                        if (varDef == null)
                            return rawoperand[0];
                        return varDef;

                    case OperandType.Signature:
                        return BitConverter.ToInt32(rawoperand, 0);

                    case OperandType.String:
                        return TokenResolver.ResolveString(BitConverter.ToUInt32(rawoperand, 0));
                }
            }
            catch { }

            return null;
        }
Ejemplo n.º 9
0
        private object ConvertToOperand(int instructionOffset, MSILOpCode opcode, byte[] rawoperand)
        {
            try
            {
                switch (opcode.OperandType)
                {
                case OperandType.Argument:
                    ParameterDefinition paramDef = GetParameter(BitConverter.ToInt16(rawoperand, 0));
                    if (paramDef == null)
                    {
                        return(BitConverter.ToInt16(rawoperand, 0));
                    }
                    return(paramDef);

                case OperandType.ShortArgument:
                    paramDef = GetParameter(rawoperand[0]);
                    if (paramDef == null)
                    {
                        return(rawoperand[0]);
                    }
                    return(paramDef);

                case OperandType.Float32:
                    return(BitConverter.ToSingle(rawoperand, 0));

                case OperandType.Float64:
                    return(BitConverter.ToDouble(rawoperand, 0));

                case OperandType.InstructionTable:

                    int   length     = BitConverter.ToInt32(rawoperand, 0);
                    int[] offsets    = new int[length];
                    int   nextOffset = instructionOffset + (length * 4) + opcode.Bytes.Length + 4;
                    for (int i = 0; i < length; i++)
                    {
                        int index   = (i + 1) * sizeof(int);
                        int roffset = BitConverter.ToInt32(rawoperand, index);
                        offsets[i] = roffset + nextOffset;
                    }

                    return(offsets);

                case OperandType.InstructionTarget:
                    return(BitConverter.ToInt32(rawoperand, 0) + instructionOffset + opcode.Bytes.Length + sizeof(int));

                case OperandType.ShortInstructionTarget:
                    return(ASMGlobals.ByteToSByte(rawoperand[0]) + instructionOffset + opcode.Bytes.Length + sizeof(byte));

                case OperandType.Int8:
                    return(ASMGlobals.ByteToSByte(rawoperand[0]));

                case OperandType.Int32:
                    return(BitConverter.ToInt32(rawoperand, 0));

                case OperandType.Int64:
                    return(BitConverter.ToInt64(rawoperand, 0));

                case OperandType.Token:
                case OperandType.Field:
                case OperandType.Method:
                case OperandType.Type:
                    uint metadata = BitConverter.ToUInt32(rawoperand, 0);
                    try
                    {
                        object operand = TokenResolver.ResolveMember(metadata);

                        if (operand is ISpecification)
                        {
                            operand = (operand as ISpecification).TransformWith(MethodBody.Method);
                        }

                        return(operand);
                    }
                    catch { return(new TypeReference(string.Empty, "TOKEN:" + metadata.ToString("X8"), null)); }

                case OperandType.ShortVariable:
                    VariableDefinition varDef = GetVariable(rawoperand[0]);
                    if (varDef == null)
                    {
                        return(rawoperand[0]);
                    }
                    return(varDef);

                case OperandType.Variable:
                    varDef = GetVariable(BitConverter.ToInt16(rawoperand, 0));
                    if (varDef == null)
                    {
                        return(rawoperand[0]);
                    }
                    return(varDef);

                case OperandType.Signature:
                    return(BitConverter.ToInt32(rawoperand, 0));

                case OperandType.String:
                    return(TokenResolver.ResolveString(BitConverter.ToUInt32(rawoperand, 0)));
                }
            }
            catch { }

            return(null);
        }
Ejemplo n.º 10
0
 public static MSILInstruction Create(MSILOpCode opcode, uint metadataToken, NETHeader netHeader)
 {
     return new MSILInstruction() { OpCode = opcode, OperandBytes = BitConverter.GetBytes(metadataToken), Operand = netHeader.TokenResolver.ResolveToken(metadataToken) };
 }
Ejemplo n.º 11
0
 public static MSILInstruction Create(MSILOpCode opcode, object operand)
 {
     MSILInstruction instruction = new MSILInstruction()
     {
         OpCode = opcode,
         Operand = operand,
     };
     return instruction;
 }