From 38a09facea848f7d726efbba4afe03999087d7a4 Mon Sep 17 00:00:00 2001 From: ghorsington Date: Fri, 7 Aug 2020 18:25:13 +0300 Subject: [PATCH] Annotate byte patterns; fix x64 calling convention to correct one --- .../Platforms/X64Patcher.cs | 20 ++++++------- .../Platforms/X86Patcher.cs | 30 +++++++++---------- src/MirrorInternalLogs/Util/BytePattern.cs | 6 +--- .../Util/StringExtensions.cs | 30 +++++++++++++++++++ 4 files changed, 56 insertions(+), 30 deletions(-) diff --git a/src/MirrorInternalLogs/Platforms/X64Patcher.cs b/src/MirrorInternalLogs/Platforms/X64Patcher.cs index 71e484b..4b30804 100644 --- a/src/MirrorInternalLogs/Platforms/X64Patcher.cs +++ b/src/MirrorInternalLogs/Platforms/X64Patcher.cs @@ -12,15 +12,15 @@ internal class X64Patcher : X86Patcher protected override BytePattern[] Patterns { get; } = { @" - 48 89 4C 24 08 - 48 89 54 24 10 - 4C 89 44 24 18 - 4C 89 4C 24 20 - 48 83 EC 28 - 48 8B D1 - 4C 8D 44 24 38 - B9 05 00 00 00 - E8 + 48 89 4C 24 08 ; mov QWORD PTR [rsp+0x8],rcx + 48 89 54 24 10 ; mov QWORD PTR [rsp+0x10],rdx + 4C 89 44 24 18 ; mov QWORD PTR [rsp+0x18],r8 + 4C 89 4C 24 20 ; mov QWORD PTR [rsp+0x20],r9 + 48 83 EC 28 ; sub rsp,0x28 + 48 8B D1 ; mov rdx,rcx + 4C 8D 44 24 38 ; lea r8,[rsp+0x38] + B9 05 00 00 00 ; mov ecx,0x5 + E8 ; call " }; @@ -39,7 +39,7 @@ private static void OnUnityLog(ulong type, IntPtr message, IntPtr args) original(type, message, args); } - [UnmanagedFunctionPointer(CallingConvention.FastCall)] + [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate void PrintFDelegate(ulong type, IntPtr pattern, IntPtr parts); } } \ No newline at end of file diff --git a/src/MirrorInternalLogs/Platforms/X86Patcher.cs b/src/MirrorInternalLogs/Platforms/X86Patcher.cs index cf4eed5..217a4d8 100644 --- a/src/MirrorInternalLogs/Platforms/X86Patcher.cs +++ b/src/MirrorInternalLogs/Platforms/X86Patcher.cs @@ -14,24 +14,24 @@ internal class X86Patcher : IPlatformPatcher { // New Unity @" - 55 - 8B EC - 8D 45 0C - 50 - FF 75 08 - 6A 05 - E8 + 55 ; push ebp + 8B EC ; mov ebp,esp + 8D 45 0C ; lea eax,[ebp+0xc] + 50 ; push eax + FF 75 08 ; push DWORD PTR [ebp+0x8] + 6A 05 ; push 0x5 + E8 ; call ", // Older Unity @" - 55 - 8B EC - 8B 4D 08 - 8D 45 0C - 50 - 51 - 6A 05 - E8 + 55 ; push ebp + 8B EC ; mov ebp,esp + 8B 4D 08 ; mov ecx,DWORD PTR [ebp+0x8] + 8D 45 0C ; lea eax,[ebp+0xc] + 50 ; push eax + 51 ; push ecx + 6A 05 ; push 0x5 + E8 ; call " }; diff --git a/src/MirrorInternalLogs/Util/BytePattern.cs b/src/MirrorInternalLogs/Util/BytePattern.cs index 64c9ed0..ff1a4bc 100644 --- a/src/MirrorInternalLogs/Util/BytePattern.cs +++ b/src/MirrorInternalLogs/Util/BytePattern.cs @@ -11,11 +11,7 @@ internal class BytePattern public BytePattern(string bytes) { - pattern = bytes.Replace("\t", " ") - .Replace("\r", " ") - .Replace("\n", " ") - .Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries) - .Select(p => byte.Parse(p.Trim(), NumberStyles.HexNumber)).ToArray(); + pattern = bytes.ParseHexBytes(); CreateJumpTable(); } diff --git a/src/MirrorInternalLogs/Util/StringExtensions.cs b/src/MirrorInternalLogs/Util/StringExtensions.cs index f06f137..dbf25bc 100644 --- a/src/MirrorInternalLogs/Util/StringExtensions.cs +++ b/src/MirrorInternalLogs/Util/StringExtensions.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Globalization; +using System.IO; using System.Linq; namespace MirrorInternalLogs.Util @@ -10,5 +12,33 @@ public static string Format(this string fmt, Dictionary> va { return vars.Aggregate(fmt, (str, kv) => str.Replace($"{{{kv.Key}}}", kv.Value())); } + + public static byte[] ParseHexBytes(this string str) + { + static bool IsHexChar(char lowerC) => '0' <= lowerC && lowerC <= '9' || 'a' <= lowerC && lowerC <= 'f'; + var result = new List(); + + var sr = new StringReader(str); + while (sr.Peek() > 0) + { + var c = char.ToLower((char) sr.Read()); + + if (char.IsWhiteSpace(c)) + continue; + if (c == ';') + { + sr.ReadLine(); + } + else if (IsHexChar(c) && sr.Peek() > 0) + { + var other = char.ToLower((char) sr.Peek()); + if (!IsHexChar(other)) continue; + sr.Read(); + result.Add(byte.Parse($"{c}{other}", NumberStyles.HexNumber)); + } + } + + return result.ToArray(); + } } } \ No newline at end of file