Esempio n. 1
0
        private void exportAllStep2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog d = new FolderBrowserDialog();

            if (d.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string path = d.SelectedPath + "\\";
                pb1.Maximum = Functions.Count();
                pb1.Value   = 0;
                for (int i = 0; i < Functions.Count; i++)
                {
                    XEXFunction f = Functions[i];
                    string      s = "";
                    f = Decompiler.Step1(AsmCodeLines, f, out s);
                    f = Decompiler.Step2(AsmCodeLines, f, ref s);
                    File.WriteAllText(path + f.Name.Split('\t')[0] + ".c", s);
                    if (i % 77 == 0)
                    {
                        pb1.Value   = i;
                        status.Text = "Decompiling(" + (i + 1) + "/" + Functions.Count + ")...";
                        Application.DoEvents();
                    }
                }
                pb1.Value   = 0;
                status.Text = "Ready";
                Application.DoEvents();
                MessageBox.Show("Done");
            }
        }
Esempio n. 2
0
        private void step2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int n = listBox1.SelectedIndex;

            if (n == -1)
            {
                return;
            }
            XEXFunction f = Functions[n];
            string      s = rtb2.Text;

            f            = Decompiler.Step2(AsmCodeLines, f, ref s);
            Functions[n] = f;
            rtb3.Text    = s;
        }
Esempio n. 3
0
        public static XEXFunction Step1(List <string> AsmCode, XEXFunction f, out string code)
        {
            StringBuilder sb    = new StringBuilder();
            List <string> lines = new List <string>();

            for (int i = f.AsmCodeStart; i <= f.AsmCodeEnd; i++)
            {
                lines.Add(AsmCode[i]);
            }
            sb.AppendLine("void " + f.Name + "()");
            sb.AppendLine("{");
            sb.AppendLine("\t_asm");
            sb.AppendLine("\t{");
            //skip header
            int start = 0;

            for (int i = 0; i < lines.Count; i++)
            {
                if (lines[i].StartsWith(f.Name))
                {
                    start = i + 1;
                    break;
                }
            }
            while (lines[start].Trim().StartsWith("#") || lines[start].Trim() == "")
            {
                start++;
            }
            //insert asm
            for (int i = start; i < lines.Count - 1; i++)
            {
                sb.Append(SimplePseudoC(lines[i]));
            }
            sb.AppendLine("\t}");
            sb.AppendLine("}");
            string result = sb.ToString();

            result = result.Replace(_asmstartend, "");
            code   = result;
            return(f);
        }
Esempio n. 4
0
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int n = listBox1.SelectedIndex;

            if (n == -1)
            {
                return;
            }
            XEXFunction f = Functions[n];

            rtb1.Text = "";
            StringBuilder sb = new StringBuilder();

            for (int i = f.AsmCodeStart; i <= f.AsmCodeEnd; i++)
            {
                sb.AppendLine(AsmCodeLines[i]);
            }
            rtb1.Text = sb.ToString();
            if (midStepToolStripMenuItem.Checked)
            {
                treeView1.Nodes.Clear();
                List <string> code = new List <string>();
                for (int i = f.AsmCodeStart; i <= f.AsmCodeEnd; i++)
                {
                    code.Add(AsmCodeLines[i]);
                }
                treeView1.Nodes.Add(Decompiler.MidStep(code));
            }
            if (step1ToolStripMenuItem.Checked)
            {
                string s = "";
                f         = Decompiler.Step1(AsmCodeLines, f, out s);
                rtb2.Text = s;
                if (step2ToolStripMenuItem.Checked)
                {
                    f         = Decompiler.Step2(AsmCodeLines, f, ref s);
                    rtb3.Text = s;
                }
            }
        }
Esempio n. 5
0
        private void exportSingleStep1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int n = listBox1.SelectedIndex;

            if (n == -1)
            {
                return;
            }
            XEXFunction f = Functions[n];
            string      s = "";

            f = Decompiler.Step1(AsmCodeLines, f, out s);
            SaveFileDialog d = new SaveFileDialog();

            d.Filter   = "*.c|*.c";
            d.FileName = f.Name + ".c";
            if (d.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                File.WriteAllText(d.FileName, s);
                MessageBox.Show("Done");
            }
        }
Esempio n. 6
0
        public static XEXFunction Step2(List <string> AsmCode, XEXFunction f, ref string code)
        {
            StringReader  sr    = new StringReader(code);
            List <string> lines = new List <string>();
            string        line;

            while ((line = sr.ReadLine()) != null)
            {
                lines.Add(line);
            }
            // simple if(){...} search
            for (int i = 0; i < lines.Count; i++)
            {
                if (lines[i].StartsWith("\t\t"))
                {
                    string[] parts = lines[i].Trim().Split('\t');
                    if (parts.Length > 0)
                    {
                        switch (parts[0].Trim().ToLower())
                        {
                        case "bne":
                        case "beq":
                            List <string> temp  = new List <string>();
                            string[]      part2 = parts[1].Trim().Split(',');
                            if (part2.Length == 2)
                            {
                                int pos = -1;
                                for (int j = i + 1; j < lines.Count; j++)
                                {
                                    if (lines[j].Trim().Contains(part2[1].Trim()))
                                    {
                                        pos = j;
                                        break;
                                    }
                                }
                                if (pos != -1)
                                {
                                    for (int j = 0; j < i; j++)
                                    {
                                        temp.Add(lines[j]);
                                    }
                                    temp.Add("\t}");
                                    string compare = parts[0].Trim().ToLower();
                                    switch (compare)
                                    {
                                    case "bne":
                                        temp.Add("\tif(" + part2[0].Trim() + " != 0)");
                                        break;

                                    case "beq":
                                        temp.Add("\tif(" + part2[0].Trim() + " == 0)");
                                        break;
                                    }
                                    temp.Add("\t{");
                                    temp.Add("\t_asm");
                                    temp.Add("\t{");
                                    for (int j = i + 1; j < pos; j++)
                                    {
                                        temp.Add(lines[j]);
                                    }
                                    temp.Add("\t}");
                                    temp.Add("\t}");
                                    temp.Add("\t_asm");
                                    temp.Add("\t{");
                                    for (int j = pos; j < lines.Count; j++)
                                    {
                                        temp.Add(lines[j]);
                                    }
                                    lines = temp;
                                }
                            }
                            break;
                        }
                    }
                }
            }
            StringBuilder sb = new StringBuilder();

            foreach (string l in lines)
            {
                sb.AppendLine(l);
            }
            string result = sb.ToString();

            result = result.Replace(_asmstartend, "");
            //tabbing in ->
            sr    = new StringReader(result);
            lines = new List <string>();
            while ((line = sr.ReadLine()) != null)
            {
                lines.Add(line);
            }
            for (int i = 0; i < lines.Count; i++)
            {
                if (lines[i].Trim().StartsWith("if("))
                {
                    int start = i + 2;
                    int end   = start;
                    int depth = 1;
                    for (int j = start; j < lines.Count; j++)
                    {
                        if (lines[j].Trim().StartsWith("{"))
                        {
                            depth++;
                        }
                        if (lines[j].Trim().StartsWith("}"))
                        {
                            depth--;
                        }
                        if (depth == 0)
                        {
                            end = j;
                            break;
                        }
                    }
                    for (int j = start; j < end; j++)
                    {
                        if (!lines[j].StartsWith("loc"))
                        {
                            lines[j] = '\t' + lines[j];
                        }
                    }
                }
            }
            sb = new StringBuilder();
            foreach (string l in lines)
            {
                sb.AppendLine(l);
            }
            code = sb.ToString();
            return(f);
        }
Esempio n. 7
0
 private void openASMToolStripMenuItem_Click(object sender, EventArgs e)
 {
     OpenFileDialog d = new OpenFileDialog();
     d.Filter = "*.asm|*.asm";
     if (d.ShowDialog() == System.Windows.Forms.DialogResult.OK)
     {
         status.Text = "Counting Assembler Code Lines...";
         Application.DoEvents();
         int lineCount = File.ReadLines(d.FileName).Count();
         status.Text = "Loading Assembler Code Lines...";
         Application.DoEvents();
         pb1.Maximum = lineCount;
         pb1.Value = 0;
         int counter = 0;
         StreamReader file = new StreamReader(d.FileName);
         AsmCodeLines = new List<string>();
         string line;
         while ((line = file.ReadLine()) != null)
         {
             AsmCodeLines.Add(line);
             if ((counter++) % 1000 == 0)
             {
                 Application.DoEvents();
                 pb1.Value = counter;
             }
         }
         pb1.Value = 0;
         status.Text = "Finding Functions...";
         Application.DoEvents();
         Functions = new List<XEXFunction>();
         counter = 0;
         XEXFunction func = new XEXFunction();
         while (counter < AsmCodeLines.Count)
         {
             line = AsmCodeLines[counter];
             if (line.StartsWith("# =============== S U B\tR O U T\tI N E ======================================="))
             {
                 AsmCodeLines[counter] = AsmCodeLines[counter].Replace("\t", " ");
                 func.AsmCodeStart = counter;
             }
             else if (line.StartsWith("# End of function"))
             {
                 func.AsmCodeEnd = counter;
                 func.Name = line.Substring(18, line.Length - 18).Trim();
                 Functions.Add(func);
                 func = new XEXFunction();
             }
             if (counter++ % 1000 == 0)
             {
                 status.Text = "Functions found : " + Functions.Count();
                 pb1.Value = counter;
                 Application.DoEvents();
             }
         }
         pb1.Value = 0;
         listBox1.Items.Clear();
         counter = 0;
         foreach (XEXFunction f in Functions)
             listBox1.Items.Add((counter++).ToString("d8") + " : " + f.Name + " (" + (f.AsmCodeEnd - f.AsmCodeStart + 1) + " lines)");
         status.Text = "Ready";
     }
 }
Esempio n. 8
0
        private void openASMToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog d = new OpenFileDialog();

            d.Filter = "*.asm|*.asm";
            if (d.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                status.Text = "Counting Assembler Code Lines...";
                Application.DoEvents();
                int lineCount = File.ReadLines(d.FileName).Count();
                status.Text = "Loading Assembler Code Lines...";
                Application.DoEvents();
                pb1.Maximum = lineCount;
                pb1.Value   = 0;
                int          counter = 0;
                StreamReader file    = new StreamReader(d.FileName);
                AsmCodeLines = new List <string>();
                string line;
                while ((line = file.ReadLine()) != null)
                {
                    AsmCodeLines.Add(line);
                    if ((counter++) % 1000 == 0)
                    {
                        Application.DoEvents();
                        pb1.Value = counter;
                    }
                }
                pb1.Value   = 0;
                status.Text = "Finding Functions...";
                Application.DoEvents();
                Functions = new List <XEXFunction>();
                counter   = 0;
                XEXFunction func = new XEXFunction();
                while (counter < AsmCodeLines.Count)
                {
                    line = AsmCodeLines[counter];
                    if (line.StartsWith("# =============== S U B\tR O U T\tI N E ======================================="))
                    {
                        AsmCodeLines[counter] = AsmCodeLines[counter].Replace("\t", " ");
                        func.AsmCodeStart     = counter;
                    }
                    else if (line.StartsWith("# End of function"))
                    {
                        func.AsmCodeEnd = counter;
                        func.Name       = line.Substring(18, line.Length - 18).Trim();
                        Functions.Add(func);
                        func = new XEXFunction();
                    }
                    if (counter++ % 1000 == 0)
                    {
                        status.Text = "Functions found : " + Functions.Count();
                        pb1.Value   = counter;
                        Application.DoEvents();
                    }
                }
                pb1.Value = 0;
                listBox1.Items.Clear();
                counter = 0;
                foreach (XEXFunction f in Functions)
                {
                    listBox1.Items.Add((counter++).ToString("d8") + " : " + f.Name + " (" + (f.AsmCodeEnd - f.AsmCodeStart + 1) + " lines)");
                }
                status.Text = "Ready";
            }
        }
Esempio n. 9
0
 public static XEXFunction Step2(List<string> AsmCode, XEXFunction f, ref string code)
 {
     StringReader sr = new StringReader(code);
     List<string> lines = new List<string>();
     string line;
     while ((line = sr.ReadLine()) != null)
         lines.Add(line);
     // simple if(){...} search
     for (int i = 0; i < lines.Count; i++)
         if (lines[i].StartsWith("\t\t"))
         {
             string[] parts = lines[i].Trim().Split('\t');
             if (parts.Length > 0)
                 switch (parts[0].Trim().ToLower())
                 {
                     case "bne":
                     case "beq":
                         List<string> temp = new List<string>();
                         string[] part2 = parts[1].Trim().Split(',');
                         if (part2.Length == 2)
                         {
                             int pos = -1;
                             for (int j = i + 1; j < lines.Count; j++)
                                 if (lines[j].Trim().Contains(part2[1].Trim()))
                                 {
                                     pos = j;
                                     break;
                                 }
                             if (pos != -1)
                             {
                                 for (int j = 0; j < i; j++)
                                     temp.Add(lines[j]);
                                 temp.Add("\t}");
                                 string compare = parts[0].Trim().ToLower();
                                 switch (compare)
                                 {
                                     case "bne":
                                         temp.Add("\tif(" + part2[0].Trim() + " != 0)");
                                         break;
                                     case "beq":
                                         temp.Add("\tif(" + part2[0].Trim() + " == 0)");
                                         break;
                                 }
                                 temp.Add("\t{");
                                 temp.Add("\t_asm");
                                 temp.Add("\t{");
                                 for (int j = i + 1; j < pos; j++)
                                     temp.Add(lines[j]);
                                 temp.Add("\t}");
                                 temp.Add("\t}");
                                 temp.Add("\t_asm");
                                 temp.Add("\t{");
                                 for (int j = pos; j < lines.Count; j++)
                                     temp.Add(lines[j]);
                                 lines = temp;
                             }
                         }
                         break;
                 }
         }
     StringBuilder sb = new StringBuilder();
     foreach (string l in lines)
         sb.AppendLine(l);
     string result = sb.ToString();
     result = result.Replace(_asmstartend, "");
     //tabbing in ->
     sr = new StringReader(result);
     lines = new List<string>();
     while ((line = sr.ReadLine()) != null)
         lines.Add(line);
     for(int i=0;i<lines.Count;i++)
         if (lines[i].Trim().StartsWith("if("))
         {
             int start = i + 2;
             int end = start;
             int depth = 1;
             for (int j = start; j < lines.Count; j++)
             {
                 if (lines[j].Trim().StartsWith("{"))
                     depth++;
                 if (lines[j].Trim().StartsWith("}"))
                     depth--;
                 if (depth == 0)
                 {
                     end = j;
                     break;
                 }
             }
             for (int j = start; j < end; j++)
                 if (!lines[j].StartsWith("loc"))
                     lines[j] = '\t' + lines[j];
         }
     sb = new StringBuilder();
     foreach (string l in lines)
         sb.AppendLine(l);
     code = sb.ToString();
     return f;
 }
Esempio n. 10
0
 public static XEXFunction Step1(List<string> AsmCode, XEXFunction f, out string code)
 {
     StringBuilder sb = new StringBuilder();
     List<string> lines = new List<string>();
     for (int i = f.AsmCodeStart; i <= f.AsmCodeEnd; i++)
         lines.Add(AsmCode[i]);
     sb.AppendLine("void " + f.Name + "()");
     sb.AppendLine("{");
     sb.AppendLine("\t_asm");
     sb.AppendLine("\t{");
     //skip header
     int start = 0;
     for(int i=0;i<lines.Count;i++)
         if (lines[i].StartsWith(f.Name))
         {
             start = i+1;
             break;
         }
     while (lines[start].Trim().StartsWith("#") || lines[start].Trim() == "")
         start++;
     //insert asm
     for (int i = start; i < lines.Count - 1; i++)
         sb.Append(SimplePseudoC(lines[i]));
     sb.AppendLine("\t}");
     sb.AppendLine("}");
     string result = sb.ToString();
     result = result.Replace(_asmstartend, "");
     code = result;
     return f;
 }