SharpDisasm.Tests/OpTableGeneration.cs
/// <summary>
/// Parse udis86 optable.xml file and return list of instruction definitions
/// </summary>
/// <param name="xml"></param>
/// <returns></returns>
Parse udis86 optable.xml file and return list of instruction definitions
Parse udis86 optable . xml file and return list of instruction definitions
private static IEnumerable<UdInsnDef> ParseOptableXml(string xml)
            {
                XDocument doc = XDocument.Parse(xml);
                XNode topNode = doc.FirstNode;
                while (topNode != null && ((topNode as XElement) == null || (topNode as XElement).Name != "x86optable"))
                    topNode = topNode.NextNode;
                if (topNode as XElement != null)
                {
                    foreach (var instruction in (topNode as XElement).Elements())
                    {
                        if (String.IsNullOrEmpty(instruction.Name.ToString()))
                            continue;
                        if (instruction.Name != "instruction")
                        {
                            Error("Invalid instruction node in XML - " + instruction.Name);
                            continue;
                        }
                        string mnemonic = (from n in instruction.Elements(XName.Get("mnemonic", ""))
                                           select n.Value).FirstOrDefault();
                        string vendor = String.Empty;
                        string[] cpuid = new string[] { };
                        foreach (var node in instruction.Elements())
                        {
                            if (node.Name == "vendor")
                                vendor = node.Value;
                            else if (node.Name == "cpuid")
                                cpuid = node.Value.Split(' ');
                        }
                        foreach (var node in instruction.Elements())
                        {
                            if (node.Name == "def")
                            {
                                string[] pfx = new string[] { };
                                string[] opc = new string[] { };
                                string[] opr = new string[] { };
                                string localVendor = null;
                                string[] localCpuid = null;
                                foreach (var def in node.Elements())
                                {
                                    if (def.Name == "pfx")
                                        pfx = def.Value.Split(' ');
                                    else if (def.Name == "opc")
                                        opc = def.Value.Split(' ');
                                    else if (def.Name == "opr")
                                        opr = def.Value.Split(' ');
                                    else if (def.Name == "mode")
                                        pfx = pfx.Concat(def.Value.Split(' ')).ToArray();
                                    else if (def.Name == "cpuid")
                                        localCpuid = def.Value.Split(' ');
                                    else if (def.Name == "vendor")
                                        localVendor = def.Value;
                                }
                                yield return new UdInsnDef(mnemonic, pfx, opc, opr, localVendor ?? vendor, localCpuid ?? cpuid);
                            }
                        }
                    }
                }
            }
private static IEnumerable < UdInsnDef > ParseOptableXml ( string xml ) { XDocument doc = XDocument . Parse ( xml ) ; XNode topNode = doc . FirstNode ; while ( topNode != null && ( ( topNode as XElement ) == null || ( topNode as XElement ) . Name != " " ) ) topNode = topNode . NextNode ; if ( topNode as XElement != null ) { foreach ( var instruction in ( topNode as XElement ) . Elements ( ) ) { if ( String . IsNullOrEmpty ( instruction . Name . ToString ( ) ) ) continue ; if ( instruction . Name != " " ) { Error ( " " + instruction . Name ) ; continue ; } string mnemonic = ( from n in instruction . Elements ( XName . Get ( " " , " " ) ) select n . Value ) . FirstOrDefault ( ) ; string vendor = String . Empty ; string [ ] cpuid = new string [ ] { } ; foreach ( var node in instruction . Elements ( ) ) { if ( node . Name == " " ) vendor = node . Value ; else if ( node . Name == " " ) cpuid = node . Value . Split ( ' ' ) ; } foreach ( var node in instruction . Elements ( ) ) { if ( node . Name == " " ) { string [ ] pfx = new string [ ] { } ; string [ ] opc = new string [ ] { } ; string [ ] opr = new string [ ] { } ; string localVendor = null ; string [ ] localCpuid = null ; foreach ( var def in node . Elements ( ) ) { if ( def . Name == " " ) pfx = def . Value . Split ( ' ' ) ; else if ( def . Name == " " ) opc = def . Value . Split ( ' ' ) ; else if ( def . Name == " " ) opr = def . Value . Split ( ' ' ) ; else if ( def . Name == " " ) pfx = pfx . Concat ( def . Value . Split ( ' ' ) ) . ToArray ( ) ; else if ( def . Name == " " ) localCpuid = def . Value . Split ( ' ' ) ; else if ( def . Name == " " ) localVendor = def . Value ; } yield return new UdInsnDef ( mnemonic , pfx , opc , opr , localVendor ?? vendor , localCpuid ?? cpuid ) ; } } } } }
Parse udis86 optable.xml file and return list of instruction definitions
Parse udis86 optable . xml file and return list of instruction definitions
None
xml
string
None