From fa4b15d7d2e4315e7e07aeb397034f5e87b919fa Mon Sep 17 00:00:00 2001 From: huiyadanli Date: Tue, 24 Dec 2019 00:16:06 +0800 Subject: [PATCH] =?UTF-8?q?[+]=2016=E8=BF=9B=E5=88=B6=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E9=80=9A=E9=85=8D=E7=AC=A6=E6=9F=A5=E6=89=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Matcher/FuzzyMatcher.cs | 105 ++++++++++++++++++ .../RevokeMsgPatcher.Assistant.csproj | 13 ++- 2 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 RevokeMsgPatcher.Assistant/Matcher/FuzzyMatcher.cs diff --git a/RevokeMsgPatcher.Assistant/Matcher/FuzzyMatcher.cs b/RevokeMsgPatcher.Assistant/Matcher/FuzzyMatcher.cs new file mode 100644 index 0000000..3ecf417 --- /dev/null +++ b/RevokeMsgPatcher.Assistant/Matcher/FuzzyMatcher.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace RevokeMsgPatcher.Assistant.Matcher +{ + /// + /// 对16进制数据进行模糊查找 + /// 通配符的方式 + /// + public class FuzzyMatcher + { + public const byte wildcard = 0x3F; // 通配符 + + /// + /// 模糊匹配所有 + /// + /// 被查找对象 + /// 查找串 + /// + public static int[] MatchAll(byte[] content, byte[] pattern) + { + byte[] head = GetHead(pattern); + int[] indexs = BoyerMooreMatcher.MatchAll(content, head); + // 头串和查找串相同则直接返回,不同则继续判断是否符合查询串 + if (head.Length == pattern.Length) + { + return indexs; + } + else + { + List res = new List(); + foreach (int index in indexs) + { + if (IsEqual(content, index, pattern)) + { + res.Add(index); + } + } + if (res.Count > 0) + { + return res.ToArray(); + } + else + { + return null; + } + } + } + + /// + /// 获取头串 + /// + /// 完整查找串 + /// + private static byte[] GetHead(byte[] whole) + { + int len = whole.Length; + for (int i = 0; i < whole.Length; i++) + { + if (whole[i] == wildcard) + { + len = i; + break; + } + } + if (len == 0) + { + throw new Exception("不正确的通配符位置!"); + } + return whole.Take(len).ToArray(); + } + + /// + /// 确认整个查找串是否匹配 + /// + /// 被查找对象 + /// 头串匹配位置 + /// 完整查找串 + /// + private static bool IsEqual(byte[] content, int start, byte[] whole) + { + int i = 0; + for (i = 0; i < whole.Length; i++) + { + if (whole[i] == wildcard) + { + continue; + } + if (content[start + i] != whole[i]) + { + break; + } + } + if (i == whole.Length) + { + return true; + } + else + { + return false; + } + } + } +} diff --git a/RevokeMsgPatcher.Assistant/RevokeMsgPatcher.Assistant.csproj b/RevokeMsgPatcher.Assistant/RevokeMsgPatcher.Assistant.csproj index 00c5ac4..a61f64c 100644 --- a/RevokeMsgPatcher.Assistant/RevokeMsgPatcher.Assistant.csproj +++ b/RevokeMsgPatcher.Assistant/RevokeMsgPatcher.Assistant.csproj @@ -69,6 +69,7 @@ + @@ -97,12 +98,6 @@ - - - {977bf781-ced8-4389-9404-0fa08fdf21df} - RevokeMsgPatcher - - False @@ -110,5 +105,11 @@ false + + + {977bf781-ced8-4389-9404-0fa08fdf21df} + RevokeMsgPatcher + + \ No newline at end of file