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

            return(instruction);
        }
        public MSILInstruction DisassembleNextInstruction()
        {
            int currentOffset = (int)(reader.BaseStream.Position - ilOffset);

            MSILOpCode opcode = ReadNextOpCode();

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

            return(new MSILInstruction(currentOffset, opcode, rawoperand, operand));
        }
Esempio n. 6
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));
        }
Esempio 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);
        }
Esempio n. 8
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;
        }
Esempio 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;
        }
Esempio 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) };
 }
Esempio n. 11
0
 public static MSILInstruction Create(MSILOpCode opcode, object operand)
 {
     MSILInstruction instruction = new MSILInstruction()
     {
         OpCode = opcode,
         Operand = operand,
     };
     return instruction;
 }
Esempio n. 12
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);
        }
Esempio n. 13
0
        private object ConvertToOperand(int instructionOffset, MSILOpCode opcode, byte[] rawoperand)
        {
            try
            {
                switch (opcode.OperandType)
                {
                    case OperandType.Argument:
                        return GetParameter(BitConverter.ToInt32(rawoperand, 0));
                    case OperandType.ShortArgument:
                        return GetParameter(rawoperand[0]);

                    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:
                        int metadata = BitConverter.ToInt32(rawoperand, 0);
                        try
                        {
                            object operand = tokenresolver.ResolveMember(metadata);

                            if ((operand is TypeSpecification) && (operand as TypeSpecification).OriginalType is GenericParamReference)
                            {
                                GenericParamReference paramRef = (operand as TypeSpecification).OriginalType as GenericParamReference;
                                if (paramRef.IsMethodVar)
                                {
                                    if (MethodBody.Method.GenericParameters != null && MethodBody.Method.GenericParameters.Length > 0)
                                        operand = MethodBody.Method.GenericParameters[paramRef.Index];
                                }
                                else
                                {
                                    if (MethodBody.Method.DeclaringType.GenericParameters != null && MethodBody.Method.DeclaringType.GenericParameters.Length > 0)
                                        operand = MethodBody.Method.DeclaringType.GenericParameters[paramRef.Index];
                                }
                            }
                            return operand;
                        }
                        catch { return new TypeReference() { name = "TOKEN:" + metadata.ToString("X8") }; }

                    case OperandType.ShortVariable:
                        return GetVariable(ASMGlobals.ByteToSByte(rawoperand[0]));
                    case OperandType.Variable:
                        return GetVariable(BitConverter.ToInt32(rawoperand, 0));
                    case OperandType.Signature:
                        return BitConverter.ToInt32(rawoperand, 0);
                    case OperandType.String:
                        return tokenresolver.ResolveString(BitConverter.ToInt32(rawoperand, 0));
                }
            }
            catch { }

            return null;
        }