Exemple #1
0
        public string DisplayFromConstantsTable(int start, int end, string tablename)
        {
            var returnstring = "";

            if (ConstantsStruct == null)

                return "";

            for (var ii = 1; ii < ConstantsStruct.Length; ii++)
            {
                var consttablename = GetHasConstTable(ConstantsStruct[ii].parent);

                var constindex = GetHasConstValue(ConstantsStruct[ii].parent);

                if (consttablename == tablename)
                {
                    if (start == constindex)
                    {
                        if (consttablename == "Property" || consttablename == "FieldDef")

                            returnstring = " = ";

                        else

                            returnstring = "  = ";

                        var value = ConstantsStruct[ii].value;

                        if (ConstantsStruct[ii].dtype == 2)
                        {
                            var val = BitConverter.ToBoolean(blob, ConstantsStruct[ii].value + 1);

                            if (val)

                                returnstring = returnstring + GetType(ConstantsStruct[ii].dtype) + "(true)" + "\r\n";

                            else

                                returnstring = returnstring + GetType(ConstantsStruct[ii].dtype) + "(false)" + "\r\n";
                        }

                        if (ConstantsStruct[ii].dtype == 0x03)
                        {
                            returnstring = returnstring + "char(0x";

                            var val = BitConverter.ToInt16(blob, value + 1);

                            returnstring = returnstring + val.ToString("X4");

                            returnstring = returnstring + ")\r\n";
                        }

                        if (ConstantsStruct[ii].dtype == 0x04 || ConstantsStruct[ii].dtype == 0x05)
                        {
                            int val = blob[ConstantsStruct[ii].value + 1];

                            returnstring = returnstring + GetType(0x04) + "(0x" + val.ToString("X2") + ")" + "\r\n";
                        }

                        if (ConstantsStruct[ii].dtype == 0x06 || ConstantsStruct[ii].dtype == 0x07)
                        {
                            var val = BitConverter.ToInt16(blob, ConstantsStruct[ii].value + 1);

                            //if ( val == -1)

                            //returnstring = returnstring +  GetType(0x06) + "(0xFFFF)" + "\r\n";

                            //else

                            returnstring = returnstring + GetType(0x06) + "(0x" + val.ToString("X4") + ")" + "\r\n";
                        }

                        if (ConstantsStruct[ii].dtype == 0x08 || ConstantsStruct[ii].dtype == 0x09)
                        {
                            var val = BitConverter.ToInt32(blob, ConstantsStruct[ii].value + 1);

                            returnstring = returnstring + GetType(0x08) + "(0x" + val.ToString("X8") + ")" + "\r\n";
                        }

                        if (ConstantsStruct[ii].dtype == 0x0a || ConstantsStruct[ii].dtype == 0x0b)
                        {
                            var val = BitConverter.ToInt64(blob, ConstantsStruct[ii].value + 1);

                            returnstring = returnstring + GetType(0x0a) + "(0x" + val.ToString("X") + ")" + "\r\n";
                        }

                        if (ConstantsStruct[ii].dtype == 12)
                        {
                            returnstring = returnstring + "float32(";

                            var singlevalue = BitConverter.ToSingle(blob, value + 1);

                            var val = BitConverter.ToInt32(blob, value + 1);

                            var s = singlevalue.ToString();

                            var decimalpoint = s.IndexOf(".");

                            if (singlevalue == 1.5e+009)

                                returnstring = returnstring + "1.5e+009";

                            else if (decimalpoint == -1 && !(s.IndexOf("Infinity") != -1 || s.IndexOf("NaN") != -1))

                                returnstring = returnstring + s + ".";

                            else

                                returnstring = returnstring + "0x" + val.ToString("X8");

                            returnstring = returnstring + ")\r\n";

                            //Console.WriteLine(returnstring);
                        }

                        if (ConstantsStruct[ii].dtype == 13)
                        {
                            returnstring = returnstring + "float64(";

                            var val = BitConverter.ToDouble(blob, value + 1);

                            //int dummy = BitConverter.ToInt32(blob , value+1);

                            var s1 = new IldasmString();

                            string ss = s1.ReturnStringForR8(val, 1);

                            returnstring = returnstring + ss + "\r\n";
                        }

                        if (ConstantsStruct[ii].dtype == 0xe)
                        {
                            int len = blob[ConstantsStruct[ii].value];

                            if (len >= 1)
                            {
                                returnstring = returnstring + "\"";

                                for (var jj = 1; jj < len; jj++)
                                {
                                    if (blob[ConstantsStruct[ii].value + jj] != 0)

                                        returnstring = returnstring + (char)blob[ConstantsStruct[ii].value + jj];
                                }

                                returnstring = returnstring + "\"\r\n";
                            }

                            else

                                returnstring = returnstring + "\"\"" + "\r\n";
                        }

                        if (ConstantsStruct[ii].dtype == 0x12)
                        {
                            returnstring = returnstring + "nullref" + "\r\n";
                        }
                    }
                }
            }

            return returnstring;
        }
Exemple #2
0
        public void DisplayR8(double token, byte[] codearray, int codeindex)
        {
            var b1 = codearray[codeindex + 1];

            var b2 = codearray[codeindex + 2];

            var b3 = codearray[codeindex + 3];

            var b4 = codearray[codeindex + 4];

            var b5 = codearray[codeindex + 5];

            var b6 = codearray[codeindex + 6];

            var b7 = codearray[codeindex + 7];

            var b8 = codearray[codeindex + 8];

            var doubles = token.ToString();

            var pos = doubles.IndexOf("E-");

            var d2 = 0;

            if (pos != -1)
            {
                var d1 = doubles.Substring(pos + 2);

                d2 = Convert.ToInt32(d1);
            }

            //Console.WriteLine(".......{0} {1}" , doubles , token);

            if (doubles.IndexOf("Infinity") != -1 || doubles.IndexOf("NaN") != -1 || d2 >= 308)

                Console.Write("({0} {1} {2} {3} {4} {5} {6} {7})", b1.ToString("X2"), b2.ToString("X2"), b3.ToString("X2"),
                              b4.ToString("X2"), b5.ToString("X2"), b6.ToString("X2"), b7.ToString("X2"), b8.ToString("X2"));

            else if (token == 0 && b8 == 0x80)

                Console.Write("-0.0");

            else if (token == 0 && b8 == 0x00)

                Console.Write("0.0");

            else
            {
                var s1 = new IldasmString();

                string ss = s1.ReturnStringForR8(token, 0);

                Console.Write(ss);
            }
        }
Exemple #3
0
        public int DecodeILInstrcution2(OpCode opcode, int codeindex, byte[] codearray, int methodindex, string strings)
        {
            var sizeofinstructiondata = 0;

            if (opcode.OperandType == OperandType.ShortInlineR)
            {
                Console.Write(strings);

                var b1 = codearray[codeindex + 1];

                var b2 = codearray[codeindex + 2];

                var b3 = codearray[codeindex + 3];

                var b4 = codearray[codeindex + 4];

                //int int32value = BitConverter.ToInt32(codearray, codeindex+1);

                var singlevalue = BitConverter.ToSingle(codearray, codeindex + 1);

                var s = singlevalue.ToString();

                Console.Write("{0}{1}{2}{3}", b1.ToString("X2"), b2.ToString("X2"), b3.ToString("X2"), b4.ToString("X2"));

                Console.Write(CreateSpaces(8));

                Console.Write(" */ {0} {1} ", opcode.Name, CreateSpaces(3));

                if (singlevalue == 0.015625)

                    Console.Write("1.5625e-002");

                else if (singlevalue == 1.5e+009)

                    Console.Write("1.5e+009");

                else if (singlevalue == 1.5e+008)

                    Console.Write("1.5e+008");

                else if (singlevalue == 0 && b4 == 0x80)

                    Console.Write("-0.0");

                else if (singlevalue == 0)

                    Console.Write("0.0");

                else if (s.EndsWith(".5") || s.EndsWith(".25") || s.EndsWith(".75") || s.EndsWith("0625") ||
                         s.EndsWith("125") || s.EndsWith(".375"))

                    Console.Write(s);

                else
                {
                    //Console.WriteLine(".......A");

                    var s4 = new IldasmString();

                    string ss = s4.ReturnStringForR4(singlevalue);

                    if (ss == "")

                        Console.Write("({0} {1} {2} {3})", b1.ToString("X2"), b2.ToString("X2"), b3.ToString("X2"),
                                      b4.ToString("X2"));

                    else

                        Console.Write(ss);
                }

                sizeofinstructiondata = 5;
            }

            if (opcode.OperandType == OperandType.InlineR)
            {
                Console.Write(strings);

                var token = BitConverter.ToDouble(codearray, codeindex + 1);

                var b1 = codearray[codeindex + 1];

                var b2 = codearray[codeindex + 2];

                var b3 = codearray[codeindex + 3];

                var b4 = codearray[codeindex + 4];

                var b5 = codearray[codeindex + 5];

                var b6 = codearray[codeindex + 6];

                var b7 = codearray[codeindex + 7];

                var b8 = codearray[codeindex + 8];

                Console.Write("{0}{1}{2}{3}{4}{5}{6}{7}", b1.ToString("X2"), b2.ToString("X2"), b3.ToString("X2"),
                              b4.ToString("X2"), b5.ToString("X2"), b6.ToString("X2"), b7.ToString("X2"), b8.ToString("X2"));

                Console.Write(" */ {0} {1} ", opcode.Name, CreateSpaces(3));

                DisplayR8(token, codearray, codeindex);

                sizeofinstructiondata = 9;
            }

            if (opcode.OperandType == OperandType.InlineString)
            {
                Console.Write(strings);

                var token = BitConverter.ToInt32(codearray, codeindex + 1);

                token = token & 0x00ffffff;

                int howmanybytes, uncompressedbyte5;

                howmanybytes = CorSigUncompressData(us, token, out uncompressedbyte5);

                var count = uncompressedbyte5;

                //int count = uncompressedbyte5 - 1;

                var stringarray = new byte[count];

                var startingpt = token + howmanybytes;

                Array.Copy(us, startingpt, stringarray, 0, count);

                var makearray = IsMakeArray(stringarray);

                if (makearray && count != 0)
                {
                    Console.Write("(70){0}       */ {1}      ", token.ToString("X6"), opcode.Name);

                    Console.Write("bytearray (");

                    DisplayFormattedColumns(token, 62 + (spacesforrest + 2 + spacesfortry + spacesfornested), false, true);
                }

                else
                {
                    var str = GetNameU(token, false, 0);

                    Console.Write("(70){0}       */ {1}      ", token.ToString("X6"), opcode.Name);

                    Console.Write("\"");

                    string returnstring;

                    returnstring = DisplayStringMethod(str, spacesforrest + 2 + spacesfortry + spacesfornested, 0, false);

                    Console.Write(returnstring);

                    Console.Write("\" /* 70{0} */", token.ToString("X6"));
                }

                sizeofinstructiondata = 5;
            }

            if (codearray.Length - 1 == codeindex && opcode.OperandType == OperandType.InlineMethod)
            {
                Console.Write(strings);

                Console.Write("(00)02301B       */ call       0x2301b ");

                return 1;
            }

            if (opcode.OperandType == OperandType.InlineNone)
            {
                Console.Write(strings);

                Console.Write("{0}*/ {1}", CreateSpaces(17), opcode.Name);

                if (opcode.Name == "ret" && codeindex != (codearray.Length - 1))

                    Console.WriteLine();

                if (opcode.Name == "throw" && codeindex != (codearray.Length - 1))

                    Console.WriteLine();

                sizeofinstructiondata = 1;
            }

            if (opcode.OperandType == OperandType.InlineI)
            {
                Console.Write(strings);

                var token = BitConverter.ToInt32(codearray, codeindex + 1);

                var b1 = codearray[codeindex + 1];

                var b2 = codearray[codeindex + 2];

                var b3 = codearray[codeindex + 3];

                var b4 = codearray[codeindex + 4];

                Console.Write("{0}{1}{2}{3}", b1.ToString("X2"), b2.ToString("X2"), b3.ToString("X2"), b4.ToString("X2"));

                Console.Write(CreateSpaces(8));

                Console.Write(" */ {0} {1} 0x{2}", opcode.Name, CreateSpaces(3), token.ToString("x"));

                sizeofinstructiondata = 5;
            }

            if (opcode.OperandType == OperandType.InlineI8)
            {
                Console.Write(strings);

                var token = BitConverter.ToInt64(codearray, codeindex + 1);

                var b1 = codearray[codeindex + 1];

                var b2 = codearray[codeindex + 2];

                var b3 = codearray[codeindex + 3];

                var b4 = codearray[codeindex + 4];

                var b5 = codearray[codeindex + 5];

                var b6 = codearray[codeindex + 6];

                var b7 = codearray[codeindex + 7];

                var b8 = codearray[codeindex + 8];

                Console.Write("{0}{1}{2}{3}{4}{5}{6}{7}", b1.ToString("X2"), b2.ToString("X2"), b3.ToString("X2"),
                              b4.ToString("X2"), b5.ToString("X2"), b6.ToString("X2"), b7.ToString("X2"), b8.ToString("X2"));

                Console.Write(" */ {0} {1} 0x{2}", opcode.Name, CreateSpaces(3), token.ToString("x"));

                sizeofinstructiondata = 9;
            }

            if (opcode.OperandType == OperandType.ShortInlineI)
            {
                Console.Write(strings);

                int token = codearray[codeindex + 1];

                Console.Write("{0}", token.ToString("X2"));

                Console.Write(CreateSpaces(14));

                if (token >= 128)

                    token = token - 256;

                if (opcode.Name == "unaligned.")

                    Console.Write(" */ {0} {1}", opcode.Name, token);

                else

                    Console.Write(" */ {0}   {1}", opcode.Name, token);

                sizeofinstructiondata = 2;
            }

            if (opcode.OperandType == OperandType.InlineBrTarget)
            {
                Console.Write(strings);

                var token = BitConverter.ToInt32(codearray, codeindex + 1);

                var returnstring = "";

                var b1 = codearray[codeindex + 1];

                var b2 = codearray[codeindex + 2];

                var b3 = codearray[codeindex + 3];

                var b4 = codearray[codeindex + 4];

                Console.Write("{0}{1}{2}{3}", b1.ToString("X2"), b2.ToString("X2"), b3.ToString("X2"), b4.ToString("X2"));

                Console.Write(CreateSpaces(9) + "*/ ");

                token = token + codeindex + 5;

                var len = opcode.Name.Length;

                returnstring = opcode.Name + " " + CreateSpaces(9 - len) + " IL_" + token.ToString("x4");

                Console.Write(returnstring);

                if (!(codearray.Length == codeindex + 5))

                    Console.WriteLine();

                sizeofinstructiondata = 5;
            }

            if (opcode.OperandType == OperandType.ShortInlineBrTarget)
            {
                Console.Write(strings);

                int token = codearray[codeindex + 1];

                var returnstring = "";

                Console.Write("{0}", token.ToString("X2"));

                Console.Write(CreateSpaces(15) + "*/ ");

                if (token >= 128)
                {
                    token = 0xff - token;

                    token = codeindex + 1 - token;
                }

                else

                    token = token + codeindex + 2;

                var len = opcode.Name.Length;

                returnstring = opcode.Name + " " + CreateSpaces(9 - len) + " IL_" + token.ToString("x4");

                Console.Write(returnstring);

                if (!(codearray.Length == codeindex + 2))

                    Console.WriteLine();

                sizeofinstructiondata = 2;
            }

            if (opcode.OperandType == OperandType.InlineSwitch)
            {
                Console.Write(strings);

                var token = BitConverter.ToInt32(codearray, codeindex + 1);

                var b1 = codearray[codeindex + 1];

                var b2 = codearray[codeindex + 2];

                var b3 = codearray[codeindex + 3];

                var b4 = codearray[codeindex + 4];

                Console.Write("{0}{1}{2}{3}", b1.ToString("X2"), b2.ToString("X2"), b3.ToString("X2"), b4.ToString("X2"));

                Console.Write(CreateSpaces(9));

                Console.Write("*/ switch     ( ");

                if (token == 0)

                    Console.Write(")");

                else

                    for (var ii = 1; ii <= token; ii++)
                    {
                        Console.Write("\r\n" + CreateSpaces(spacesforrest + 2 + spacesfortry + spacesfornested));

                        Console.Write(CreateSpaces(10) + "/*      | ");

                        var index = BitConverter.ToInt32(codearray, codeindex + 1 + ii * 4);

                        b1 = codearray[codeindex + 1 + ii * 4];

                        b2 = codearray[codeindex + 2 + ii * 4];

                        b3 = codearray[codeindex + 3 + ii * 4];

                        b4 = codearray[codeindex + 4 + ii * 4];

                        Console.Write("{0}{1}{2}{3}", b1.ToString("X2"), b2.ToString("X2"), b3.ToString("X2"),
                                      b4.ToString("X2"));

                        var tot = codeindex + index + 1 + token * 4 + 4;

                        Console.Write(CreateSpaces(9) + "*/" + CreateSpaces(13) + "IL_{0}", tot.ToString("x4"));

                        if (ii != token)

                            Console.Write(",");

                        else

                            Console.Write(")");
                    }

                sizeofinstructiondata = 5 + token * 4;
            }

            if (opcode.OperandType == OperandType.ShortInlineVar)
            {
                Console.Write(strings);

                int token = codearray[codeindex + 1];

                Console.Write("{0}", token.ToString("X2"));

                Console.Write(CreateSpaces(15) + "*/ {0}", opcode.Name);

                var len = opcode.Name.Length;

                if (opcode.Name == "ldarg.s" || opcode.Name == "ldarga.s" || opcode.Name == "starg.s")
                {
                    var methodattributeflags = GetMethodAttribute(MethodStruct[methodindex].flags, methodindex);

                    var sname = "";

                    if (token > methoddefparamcount[methodindex] && methodattributeflags.IndexOf("static") != -1)

                        sname = token + " // ERROR: invalid arg index (>=" + (methoddefparamcount[methodindex] + 1) + ")";

                    else if (token > methoddefparamcount[methodindex] + 1 && methodattributeflags.IndexOf("instance") != -1)

                        sname = token + " // ERROR: invalid arg index (>=" + (methoddefparamcount[methodindex] + 1) + ")";

                    else if (token == 0 && methodattributeflags.IndexOf("instance") != -1)

                        sname = "0";

                    else
                    {
                        if (methodattributeflags.IndexOf("static") != -1)

                            token++;

                        sname = GetParamNameForMethod(methodindex, 0, token);
                    }

                    Console.Write("{0}{1}", CreateSpaces(11 - len), sname);
                }

                else

                    Console.Write("{0}V_{1}", CreateSpaces(11 - len), token);

                sizeofinstructiondata = 2;
            }

            if (opcode.OperandType == OperandType.InlineVar)
            {
                Console.Write(strings);

                var token = (ushort)BitConverter.ToInt16(codearray, codeindex + 1);

                var b1 = codearray[codeindex + 1];

                var b2 = codearray[codeindex + 2];

                Console.Write("{0}{1}", b1.ToString("X2"), b2.ToString("X2"));

                Console.Write(CreateSpaces(13) + "*/ {0}", opcode.Name);

                var len = opcode.Name.Length;

                if (opcode.Name == "ldarg" || opcode.Name == "ldarga" || opcode.Name == "starg")
                {
                    var methodattributeflags = GetMethodAttribute(MethodStruct[methodindex].flags, methodindex);

                    var sname = "";

                    if (token > methoddefparamcount[methodindex] && methodattributeflags.IndexOf("static") != -1)

                        sname = token + " // ERROR: invalid arg index (>=" + (methoddefparamcount[methodindex] + 1) + ")";

                    else if (token > methoddefparamcount[methodindex] + 1 && methodattributeflags.IndexOf("instance") != -1)

                        sname = token + " // ERROR: invalid arg index (>=" + (methoddefparamcount[methodindex] + 1) + ")";

                    else if (token == 0 && methodattributeflags.IndexOf("instance") != -1)

                        sname = "0";

                    else
                    {
                        if (methodattributeflags.IndexOf("static") != -1)

                            token++;

                        sname = GetParamNameForMethod(methodindex, 0, token);
                    }

                    Console.Write("{0}{1}", CreateSpaces(11 - len), sname);
                }

                else

                    Console.Write("{0}V_{1}", CreateSpaces(11 - len), token);

                sizeofinstructiondata = 3;
            }

            if (opcode.OperandType == OperandType.InlineField || opcode.OperandType == OperandType.InlineMethod ||
                opcode.OperandType == OperandType.InlineType || opcode.OperandType == OperandType.InlineTok ||
                opcode.OperandType == OperandType.InlineSig)
            {
                var token = BitConverter.ToInt32(codearray, codeindex + 1);

                int seriouserror;

                DecodeTokenIL(token, opcode, methodindex, strings, out seriouserror);

                var table = (int)(token & 0xff000000);

                table = table >> 24;

                token = token & 0x00ffffff;

                if (table == 0x0a)
                {
                    var b = HasCustomAttribute("MemberRef", token);

                    if (b)
                    {
                        Console.WriteLine();

                        DisplayCustomAttribute("MemberRef", token, 2 + spacesforrest + spacesfornested);
                    }
                }

                if (seriouserror == 1)

                    sizeofinstructiondata = codearray.Length + 1;

                else

                    sizeofinstructiondata = 5;
            }

            return sizeofinstructiondata;
        }