Beispiel #1
0
 private void CheckNewMode(HexDumpMode mode)
 {
     if (mode != _currentMode)
     {
         Flush();
         if (mode == HexDumpMode.BytesOut)
         {
             _logWriter.WriteLine(">>> Sending now");
         }
         else
         {
             _logWriter.WriteLine("<<< receiving now");
         }
         _currentMode = mode;
     }
 }
Beispiel #2
0
 public void DumpBytes(HexDumpMode mode, byte[] data)
 {
     if (data == null)
     {
         throw new ArgumentNullException(nameof(data));
     }
     if (data.Length == 0)
     {
         return;
     }
     CheckNewMode(mode);
     for (var i = 0; i < data.Length; i++)
     {
         _linebuffer[_bufferpos++] = data[i];
         CheckAutoFlush();
     }
     CheckAutoFlush();
 }
Beispiel #3
0
 public ParseCommand()
     : base("Parser", "parse", "Parse a packet log file")
 {
     Options = new OptionSet
     {
         $"Usage: {Program.Name} {Name} PKTFILE [OPTIONS]",
         string.Empty,
         "Available options:",
         string.Empty,
         {
             "o|output=",
             $"Specify output file (defaults to input file name with extension changed to `{ParsedExtension}`)",
             o => _output = o
         },
         {
             "r|regex=",
             "Add a game message name regex to filter packets by (uses Perl 5 regex syntax)",
             (string r) => _regexes.Add(new Regex(r, FilterRegexOptions))
         },
         {
             "e|header",
             $"Enable/disable printing the packet log header before parsing (defaults to `{_header}`)",
             e => _header = e != null
         },
         {
             "s|stats",
             $"Enable/disable printing parsing and analysis statistics before exiting (defaults to `{_stats}`)",
             s => _stats = s != null
         },
         {
             "u|summary",
             $"Enable/disable printing a summary of known and unknown packets before exiting (defaults to `{_summary}`)",
             u => _summary = u != null
         },
         {
             "x|hex-dump=",
             $"Specify hex dump mode (defaults to `{_hex}`)",
             (HexDumpMode x) => _hex = x
         },
         {
             "p|parse",
             $"Enable/disable parsing of known packets (defaults to `{_parse}`)",
             p => _parse = p != null
         },
         {
             "z|backend=",
             $"Specify packet serializer backend (defaults to `{_backend}`)",
             (PacketSerializerBackend z) => _backend = z
         },
         {
             "t|roundtrips=",
             $"Specify the number of times to roundtrip parsed packets (defaults to `{_roundtrips}`)",
             (int t) => _roundtrips = t
         },
         {
             "a|analyze=",
             $"Specify analysis mode (defaults to `{_analysis}`)",
             (AnalysisMode a) => _analysis = a
         },
         {
             "m|min-string-length=",
             $"Specify minimum string length for analysis (defaults to `{_length}`)",
             (int m) => _length = m
         },
         {
             "w|allow-white-space",
             $"Enable/disable showing potential strings consisting only of white space (defaults to `{_whiteSpace}`)",
             w => _whiteSpace = w != null
         },
         {
             "c|allow-control-chars",
             $"Enable/disable showing potential strings containing control characters (defaults to `{_control}`)",
             c => _control = c != null
         },
         string.Empty,
         "Hex dump modes:",
         string.Empty,
         $"  {HexDumpMode.None}: Don't do hex dumps",
         $"  {HexDumpMode.Unknown}: Do hex dumps for packets with unknown structure (default)",
         $"  {HexDumpMode.All}: Do hex dumps for all packets",
         string.Empty,
         "Packet serializer backends:",
         string.Empty,
         $"  {PacketSerializerBackend.Reflection}: Reflection-based serializer (default)",
         $"  {PacketSerializerBackend.Compiler}: Runtime-compiled serializer",
         string.Empty,
         "Analysis modes:",
         string.Empty,
         $"  {AnalysisMode.None}: Don't perform analysis (default)",
         $"  {AnalysisMode.Unknown}: Analyze packets with unknown structure",
         $"  {AnalysisMode.All}: Analyze all packets",
     };
 }
Beispiel #4
0
        static bool HandleArguments(ref string[] args)
        {
            var asm     = Assembly.GetExecutingAssembly();
            var name    = asm.GetName().Name;
            var version = false;
            var help    = false;
            var set     = new OptionSet
            {
                $"This is {name}, part of the {nameof(Alkahest)} project.",
                "",
                "Usage:",
                "",
                $"  {name} [options...] [--] <file>",
                "",
                "General",
                {
                    "h|?|help",
                    "Print version and exit.",
                    h => help = h != null
                },
                {
                    "v|version",
                    "Print help and exit.",
                    v => version = v != null
                },
                {
                    "o|output",
                    "Specify output text file.",
                    o => _output = o
                },
                {
                    "r|regex=",
                    "Add an opcode regex to filter packets by. Uses Perl 5 regex syntax.",
                    (string ar) => _regexes.Add(ar)
                },
                {
                    "e|header",
                    "Output packet log header information before parsing.",
                    e => _header = e != null
                },
                {
                    "s|stats",
                    "Output parsing and analysis statistics before exiting.",
                    s => _stats = s != null
                },
                {
                    "u|summary",
                    "Output a summary of known and unknown packets before exiting.",
                    u => _summary = u != null
                },
                "Parsing",
                {
                    "x|hex-dump=",
                    "Specify hex dump mode.",
                    (HexDumpMode x) => _hex = x
                },
                {
                    "p|parse",
                    "Enable/disable parsing of known packets.",
                    p => _parse = p != null
                },
                {
                    "z|backend=",
                    "Specify packet serializer backend.",
                    (PacketSerializerBackend z) => _backend = z
                },
                {
                    "t|roundtrips=",
                    "Specify the number of times to roundtrip parsed packets.",
                    (int t) => _roundtrips = t
                },
                "Analysis",
                {
                    "a|analyze=",
                    "Specify analysis mode.",
                    (AnalysisMode a) => _analysis = a
                },
                {
                    "m|min-string-length=",
                    "Specify minimum string length.",
                    (int m) => _length = m
                },
                {
                    "w|allow-white-space",
                    "Enable/disable showing strings consisting only of white space.",
                    w => _whiteSpace = w != null
                },
                {
                    "c|allow-control-chars",
                    "Enable/disable showing strings containing control characters.",
                    c => _control = c != null
                },
                "",
                "Hex dump modes:",
                "",
                $"  {HexDumpMode.None}: Don't do hex dumps.",
                $"  {HexDumpMode.Unknown}: Do hex dumps for packets with unknown structure.",
                $"  {HexDumpMode.All}: Do hex dumps for all packets.",
                "",
                "Packet serializer backends:",
                "",
                $"  {PacketSerializerBackend.Reflection}: Reflection-based serializer (default).",
                $"  {PacketSerializerBackend.Compiler}: Runtime-compiled serializer (default).",
                "",
                "Analysis modes:",
                "",
                $"  {AnalysisMode.None}: Don't perform analysis (default).",
                $"  {AnalysisMode.Unknown}: Analyze packets with unknown structure.",
                $"  {AnalysisMode.All}: Analyze all packets."
            };

            args = set.Parse(args).ToArray();

            if (version)
            {
                Console.WriteLine("{0} {1}", name,
                                  asm.GetCustomAttribute <AssemblyInformationalVersionAttribute>()
                                  .InformationalVersion);
                return(false);
            }

            if (help)
            {
                set.WriteOptionDescriptions(Console.Out);
                return(false);
            }

            return(true);
        }
Beispiel #5
0
        static bool HandleArguments(ref string[] args)
        {
            var version = false;
            var help    = false;
            var set     = new OptionSet
            {
                "General",
                {
                    "h|?|help",
                    "Print version and exit.",
                    h => help = h != null
                },
                {
                    "v|version",
                    "Print help and exit.",
                    v => version = v != null
                },
                {
                    "o|output",
                    "Specify output text file.",
                    o => _output = o
                },
                {
                    "r|regex=",
                    "Add an opcode regex to filter packets by. Uses Perl 5 regex syntax.",
                    (string ar) => _regexes.Add(ar)
                },
                {
                    "s|stats",
                    "Output parsing and analysis statistics before exiting.",
                    s => _stats = s != null
                },
                "Parsing",
                {
                    "x|hex-dump=",
                    "Specify hex dump mode.",
                    (HexDumpMode x) => _hex = x
                },
                {
                    "p|parse",
                    "Enable/disable parsing of known packets.",
                    p => _parse = p != null
                },
                {
                    "t|roundtrips=",
                    "Specify the number of times to roundtrip parsed packets.",
                    (int t) => _roundtrips = t
                },
                "Analysis",
                {
                    "a|analyze=",
                    "Specify analysis mode.",
                    (AnalysisMode a) => _analysis = a
                },
                {
                    "m|min-string-length",
                    "Specify minimum string length.",
                    (int m) => _length = m
                },
                {
                    "w|allow-white-space",
                    "Enable/disable showing strings consisting only of white space.",
                    w => _whiteSpace = w != null
                },
                {
                    "c|allow-control-chars",
                    "Enable/disable showing strings containing control characters.",
                    c => _control = c != null
                }
            };

            args = set.Parse(args).ToArray();

            if (version)
            {
                ShowVersion();
                return(false);
            }

            if (help)
            {
                ShowHelp(set);
                return(false);
            }

            return(true);
        }
Beispiel #6
0
 public void DumpByte(HexDumpMode mode, byte data)
 {
     CheckNewMode(mode);
     _linebuffer[_bufferpos++] = data;
     CheckAutoFlush();
 }