From bda89a354a853e4e130ac6fce37bf2c2b4124c03 Mon Sep 17 00:00:00 2001 From: DI20ID Date: Sun, 31 Oct 2021 14:50:19 +0100 Subject: [PATCH 01/11] test --- .../PatternScanALittleBitNaiveFor1.cs | 63 +++++++++++++++++++ PatternScanBench/Program.cs | 11 ++++ 2 files changed, 74 insertions(+) create mode 100644 PatternScanBench/Implementations/PatternScanALittleBitNaiveFor1.cs diff --git a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor1.cs b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor1.cs new file mode 100644 index 0000000..21323dd --- /dev/null +++ b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor1.cs @@ -0,0 +1,63 @@ +using System.Collections.Generic; + +namespace PatternScanBench.Implementations +{ + /// + /// Pattern scan implementation 'ALittleBitNaiveFor' - by DI20ID + /// https://github.com/DI20ID + /// + internal class PatternScanALittleBitNaiveFor1 + { + /// + /// Returns address of pattern using 'ALittleBitNaiveFor' implementation by DI20ID. Can match 0. + /// + /// The byte array to scan. + /// The byte pattern to look for, wildcard positions are replaced by 0. + /// A string that determines how pattern should be matched, 'x' is match, '?' acts as wildcard. + /// -1 if pattern is not found. + internal static long FindPattern(in byte[] cbMemory, in byte[] cbPattern, string szMask) + { + long ix; + int iy; + Dictionary not0PatternBytesDic = new Dictionary(); + List not0PatternBytesList = new List(); + List not0PatternBytesIndexList = new List(); + + int dataLength = cbMemory.Length - cbPattern.Length; + + for (iy = cbPattern.Length - 1; iy > -1; iy--) + { + if (szMask[iy] == 'x') + { + not0PatternBytesList.Add(cbPattern[iy]); + not0PatternBytesIndexList.Add(iy); + } + } + + byte[] not0PatternBytesArray = not0PatternBytesList.ToArray(); + int not0PatternBytesL = not0PatternBytesArray.Length; + int[] not0PatternBytesIndexArray = not0PatternBytesIndexList.ToArray(); + + for (ix = 0; ix < dataLength; ix++) + { + if (not0PatternBytesArray[not0PatternBytesL - 1] != cbMemory[ix]) continue; + bool check = true; + + for (iy = not0PatternBytesL - 1; iy > -1; iy--) + { + if (not0PatternBytesArray[iy] == cbMemory[ix + not0PatternBytesIndexArray[iy]]) + continue; + check = false; + break; + } + + if (check) + { + return ix; + } + } + + return -1; + } + } +} diff --git a/PatternScanBench/Program.cs b/PatternScanBench/Program.cs index e9aeddf..b6fb3fe 100644 --- a/PatternScanBench/Program.cs +++ b/PatternScanBench/Program.cs @@ -46,6 +46,17 @@ public void ALittleBitNaiveFor() } } + [Benchmark(Description = "ALittleBitNaiveFor1 by DI20ID")] + public void ALittleBitNaiveFor1() + { + foreach (MemoryPattern pattern in MemoryPatterns) + { + long result = PatternScanALittleBitNaiveFor1.FindPattern(in CbMemory, in pattern.CbPattern, pattern.SzMask); + if (result != pattern.ExpectedAddress) + throw new Exception("Pattern not found..."); + } + } + [Benchmark(Description = "Exodia by mrexodia")] public void Exodia() { From 895d93f3051d04ca1a2227a3cf132813e0dc656b Mon Sep 17 00:00:00 2001 From: DI20ID Date: Thu, 4 Nov 2021 00:00:25 +0100 Subject: [PATCH 02/11] Delete PatternScanALittleBitNaiveFor1.cs --- .../PatternScanALittleBitNaiveFor1.cs | 63 ------------------- 1 file changed, 63 deletions(-) delete mode 100644 PatternScanBench/Implementations/PatternScanALittleBitNaiveFor1.cs diff --git a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor1.cs b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor1.cs deleted file mode 100644 index 21323dd..0000000 --- a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor1.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System.Collections.Generic; - -namespace PatternScanBench.Implementations -{ - /// - /// Pattern scan implementation 'ALittleBitNaiveFor' - by DI20ID - /// https://github.com/DI20ID - /// - internal class PatternScanALittleBitNaiveFor1 - { - /// - /// Returns address of pattern using 'ALittleBitNaiveFor' implementation by DI20ID. Can match 0. - /// - /// The byte array to scan. - /// The byte pattern to look for, wildcard positions are replaced by 0. - /// A string that determines how pattern should be matched, 'x' is match, '?' acts as wildcard. - /// -1 if pattern is not found. - internal static long FindPattern(in byte[] cbMemory, in byte[] cbPattern, string szMask) - { - long ix; - int iy; - Dictionary not0PatternBytesDic = new Dictionary(); - List not0PatternBytesList = new List(); - List not0PatternBytesIndexList = new List(); - - int dataLength = cbMemory.Length - cbPattern.Length; - - for (iy = cbPattern.Length - 1; iy > -1; iy--) - { - if (szMask[iy] == 'x') - { - not0PatternBytesList.Add(cbPattern[iy]); - not0PatternBytesIndexList.Add(iy); - } - } - - byte[] not0PatternBytesArray = not0PatternBytesList.ToArray(); - int not0PatternBytesL = not0PatternBytesArray.Length; - int[] not0PatternBytesIndexArray = not0PatternBytesIndexList.ToArray(); - - for (ix = 0; ix < dataLength; ix++) - { - if (not0PatternBytesArray[not0PatternBytesL - 1] != cbMemory[ix]) continue; - bool check = true; - - for (iy = not0PatternBytesL - 1; iy > -1; iy--) - { - if (not0PatternBytesArray[iy] == cbMemory[ix + not0PatternBytesIndexArray[iy]]) - continue; - check = false; - break; - } - - if (check) - { - return ix; - } - } - - return -1; - } - } -} From f514b37d522ff54017b205678f06e1bd1f760b15 Mon Sep 17 00:00:00 2001 From: DI20ID Date: Thu, 4 Nov 2021 00:01:51 +0100 Subject: [PATCH 03/11] optimization --- .../PatternScanALittleBitNaiveFor.cs | 68 ++++++++++++------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs index 44024a2..6b249b3 100644 --- a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs +++ b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Runtime.CompilerServices; namespace PatternScanBench.Implementations { @@ -17,46 +18,61 @@ internal class PatternScanALittleBitNaiveFor /// -1 if pattern is not found. internal static long FindPattern(in byte[] cbMemory, in byte[] cbPattern, string szMask) { - long ix; - int iy; + int cbMemoryL = cbMemory.Length; + int cbPatternL = cbPattern.Length; + int[] cbPatternIndexes = new int[cbPatternL+1]; + int[] tcbPatternIndexes = new int[cbPatternL]; - List not0PatternBytesList = new List(); - List not0PatternBytesIndexList = new List(); + ref byte PcbMemory = ref cbMemory[0]; + ref byte PcbPattern = ref cbPattern[0]; + ref int PcbPatternIndexes = ref cbPatternIndexes[0]; + ref int tPcbPatternIndexes = ref tcbPatternIndexes[0]; - int dataLength = cbMemory.Length - cbPattern.Length; + - for (iy = cbPattern.Length - 1; iy > -1; iy--) + int tcbPatternL= 0; + int l = 0; + for (int i = 0; i < cbPatternL; i++) { - if (szMask[iy] == 'x') + l++; + if(szMask[i] == 'x') { - not0PatternBytesList.Add(cbPattern[iy]); - not0PatternBytesIndexList.Add(iy); + tcbPatternL++; + PcbPatternIndexes = l; + PcbPatternIndexes = ref Unsafe.Add(ref PcbPatternIndexes, 1); + l = 0; } } - byte[] not0PatternBytesArray = not0PatternBytesList.ToArray(); - int not0PatternBytesL = not0PatternBytesArray.Length; - int[] not0PatternBytesIndexArray = not0PatternBytesIndexList.ToArray(); + PcbPatternIndexes = ref cbPatternIndexes[0]; - for (ix = 0; ix < dataLength; ix++) + for (int i = 0; i < cbMemoryL; i++, PcbMemory = ref Unsafe.Add(ref PcbMemory, 1)) { - if (not0PatternBytesArray[not0PatternBytesL - 1] != cbMemory[ix]) continue; - bool check = true; - - for (iy = not0PatternBytesArray.Length - 1; iy > -1; iy--) + //if(i == 0x198A9A) + //{ + // int k = 10; + //} + if(PcbMemory == PcbPattern) { - if (not0PatternBytesArray[iy] == cbMemory[ix + not0PatternBytesIndexArray[iy]]) - continue; - check = false; - break; - } + ref byte xPcbMemory = ref PcbMemory; + ref byte xPcbPattern = ref PcbPattern; + ref int xPcbPatternIndexes = ref PcbPatternIndexes; + bool check = true; - if (check) - { - return ix; + for (int j = 0; j < tcbPatternL; j++, xPcbPatternIndexes = ref Unsafe.Add(ref xPcbPatternIndexes, 1), xPcbMemory = ref Unsafe.Add(ref xPcbMemory, xPcbPatternIndexes), xPcbPattern = ref Unsafe.Add(ref xPcbPattern, xPcbPatternIndexes)) + { + if(xPcbMemory != xPcbPattern) + { + check = false; + break; + } + if(j == tcbPatternL -1) + { + if (check) return i; + } + } } } - return -1; } } From 28470e5b81955885b9ab4ef8521dcd31d0e15af2 Mon Sep 17 00:00:00 2001 From: DI20ID Date: Thu, 4 Nov 2021 00:07:43 +0100 Subject: [PATCH 04/11] delete --- .../PatternScanALittleBitNaiveFor1.cs | 63 ------------------- 1 file changed, 63 deletions(-) delete mode 100644 PatternScanBench/Implementations/PatternScanALittleBitNaiveFor1.cs diff --git a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor1.cs b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor1.cs deleted file mode 100644 index 21323dd..0000000 --- a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor1.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System.Collections.Generic; - -namespace PatternScanBench.Implementations -{ - /// - /// Pattern scan implementation 'ALittleBitNaiveFor' - by DI20ID - /// https://github.com/DI20ID - /// - internal class PatternScanALittleBitNaiveFor1 - { - /// - /// Returns address of pattern using 'ALittleBitNaiveFor' implementation by DI20ID. Can match 0. - /// - /// The byte array to scan. - /// The byte pattern to look for, wildcard positions are replaced by 0. - /// A string that determines how pattern should be matched, 'x' is match, '?' acts as wildcard. - /// -1 if pattern is not found. - internal static long FindPattern(in byte[] cbMemory, in byte[] cbPattern, string szMask) - { - long ix; - int iy; - Dictionary not0PatternBytesDic = new Dictionary(); - List not0PatternBytesList = new List(); - List not0PatternBytesIndexList = new List(); - - int dataLength = cbMemory.Length - cbPattern.Length; - - for (iy = cbPattern.Length - 1; iy > -1; iy--) - { - if (szMask[iy] == 'x') - { - not0PatternBytesList.Add(cbPattern[iy]); - not0PatternBytesIndexList.Add(iy); - } - } - - byte[] not0PatternBytesArray = not0PatternBytesList.ToArray(); - int not0PatternBytesL = not0PatternBytesArray.Length; - int[] not0PatternBytesIndexArray = not0PatternBytesIndexList.ToArray(); - - for (ix = 0; ix < dataLength; ix++) - { - if (not0PatternBytesArray[not0PatternBytesL - 1] != cbMemory[ix]) continue; - bool check = true; - - for (iy = not0PatternBytesL - 1; iy > -1; iy--) - { - if (not0PatternBytesArray[iy] == cbMemory[ix + not0PatternBytesIndexArray[iy]]) - continue; - check = false; - break; - } - - if (check) - { - return ix; - } - } - - return -1; - } - } -} From 6f086458df5c2cb09a6123c3c1578b396a4beeb3 Mon Sep 17 00:00:00 2001 From: DI20ID Date: Thu, 4 Nov 2021 01:27:22 +0100 Subject: [PATCH 05/11] optimization --- .../PatternScanALittleBitNaiveFor.cs | 13 +++++++++---- PatternScanBench/Program.cs | 6 +++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs index 6b249b3..7976024 100644 --- a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs +++ b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs @@ -22,26 +22,31 @@ internal static long FindPattern(in byte[] cbMemory, in byte[] cbPattern, string int cbPatternL = cbPattern.Length; int[] cbPatternIndexes = new int[cbPatternL+1]; int[] tcbPatternIndexes = new int[cbPatternL]; + char[] bMask = szMask.ToCharArray(); + int tcbPatternL = 0; + int l = 0; ref byte PcbMemory = ref cbMemory[0]; ref byte PcbPattern = ref cbPattern[0]; ref int PcbPatternIndexes = ref cbPatternIndexes[0]; ref int tPcbPatternIndexes = ref tcbPatternIndexes[0]; + ref char PbMask = ref bMask[0]; + + + - - int tcbPatternL= 0; - int l = 0; for (int i = 0; i < cbPatternL; i++) { l++; - if(szMask[i] == 'x') + if(PbMask == 120) { tcbPatternL++; PcbPatternIndexes = l; PcbPatternIndexes = ref Unsafe.Add(ref PcbPatternIndexes, 1); l = 0; } + PbMask = ref Unsafe.Add(ref PbMask, 1); } PcbPatternIndexes = ref cbPatternIndexes[0]; diff --git a/PatternScanBench/Program.cs b/PatternScanBench/Program.cs index e862616..d9eb360 100644 --- a/PatternScanBench/Program.cs +++ b/PatternScanBench/Program.cs @@ -305,7 +305,11 @@ internal static void Main(string[] args) if (IntPtr.Size != 8) throw new PlatformNotSupportedException("Supports x64 only"); - + ////////////////////////////////////////////////////////////////////////// + //Benchmark b = new Benchmark(); + //b.ALittleBitNaiveFor(); + //return; + /////////////////////////////////////////////////////////////////// Spinner spinner = new(); spinner.Start(); From f47e358a94ad80fc3686a227c4e63bfee3f6ea76 Mon Sep 17 00:00:00 2001 From: DI20ID Date: Thu, 4 Nov 2021 15:54:42 +0100 Subject: [PATCH 06/11] Update PatternScanALittleBitNaiveFor.cs "while" experiment --- .../PatternScanALittleBitNaiveFor.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs index 7976024..c677473 100644 --- a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs +++ b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs @@ -82,3 +82,36 @@ internal static long FindPattern(in byte[] cbMemory, in byte[] cbPattern, string } } } +//{ +// if (PcbMemory == PcbPattern) +// { +// ref byte xPcbMemory = ref PcbMemory; +// ref byte xPcbPattern = ref PcbPattern; +// ref int xPcbPatternIndexes = ref PcbPatternIndexes; +// +// +// while (xPcbMemory == xPcbPattern) +// { +// int m = (int)Unsafe.ByteOffset(ref PcbMemory, ref xPcbMemory); +// +// //int n = (int)Unsafe.ByteOffset(ref cbMemory[0], ref xPcbMemory); +// //if (n == 0x198A9A) +// //{ +// // int k = 10; +// //} +// if (m == cbPatternL - 1) +// { +// int n = (int)Unsafe.ByteOffset(ref cbMemory[0], ref PcbMemory); +// return n; +// } +// xPcbPatternIndexes = ref Unsafe.Add(ref xPcbPatternIndexes, 1); +// xPcbMemory = ref Unsafe.Add(ref xPcbMemory, xPcbPatternIndexes); xPcbPattern = ref Unsafe.Add(ref xPcbPattern, xPcbPatternIndexes); +// +// +// } +// } +// +// PcbMemory = ref Unsafe.Add(ref PcbMemory, 1); +// +//} + From 7f2e0354cfdc69d66eb8051932afd2cf240f653d Mon Sep 17 00:00:00 2001 From: DI20ID Date: Thu, 4 Nov 2021 15:55:42 +0100 Subject: [PATCH 07/11] Update PatternScanALittleBitNaiveFor.cs --- .../PatternScanALittleBitNaiveFor.cs | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs index c677473..f923da0 100644 --- a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs +++ b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs @@ -82,36 +82,36 @@ internal static long FindPattern(in byte[] cbMemory, in byte[] cbPattern, string } } } -//{ -// if (PcbMemory == PcbPattern) -// { -// ref byte xPcbMemory = ref PcbMemory; -// ref byte xPcbPattern = ref PcbPattern; -// ref int xPcbPatternIndexes = ref PcbPatternIndexes; -// -// -// while (xPcbMemory == xPcbPattern) -// { -// int m = (int)Unsafe.ByteOffset(ref PcbMemory, ref xPcbMemory); -// -// //int n = (int)Unsafe.ByteOffset(ref cbMemory[0], ref xPcbMemory); -// //if (n == 0x198A9A) -// //{ -// // int k = 10; -// //} -// if (m == cbPatternL - 1) -// { -// int n = (int)Unsafe.ByteOffset(ref cbMemory[0], ref PcbMemory); -// return n; -// } -// xPcbPatternIndexes = ref Unsafe.Add(ref xPcbPatternIndexes, 1); -// xPcbMemory = ref Unsafe.Add(ref xPcbMemory, xPcbPatternIndexes); xPcbPattern = ref Unsafe.Add(ref xPcbPattern, xPcbPatternIndexes); -// -// -// } -// } -// -// PcbMemory = ref Unsafe.Add(ref PcbMemory, 1); -// -//} - + //while (true) + //{ + // if (PcbMemory == PcbPattern) + // { + // ref byte xPcbMemory = ref PcbMemory; + // ref byte xPcbPattern = ref PcbPattern; + // ref int xPcbPatternIndexes = ref PcbPatternIndexes; + // + // + // while (xPcbMemory == xPcbPattern) + // { + // int m = (int)Unsafe.ByteOffset(ref PcbMemory, ref xPcbMemory); + // + // //int n = (int)Unsafe.ByteOffset(ref cbMemory[0], ref xPcbMemory); + // //if (n == 0x198A9A) + // //{ + // // int k = 10; + // //} + // if (m == cbPatternL - 1) + // { + // int n = (int)Unsafe.ByteOffset(ref cbMemory[0], ref PcbMemory); + // return n; + // } + // xPcbPatternIndexes = ref Unsafe.Add(ref xPcbPatternIndexes, 1); + // xPcbMemory = ref Unsafe.Add(ref xPcbMemory, xPcbPatternIndexes); xPcbPattern = ref Unsafe.Add(ref xPcbPattern, xPcbPatternIndexes); + // + // + // } + // } + // + // PcbMemory = ref Unsafe.Add(ref PcbMemory, 1); + // + //} From 1bc5b0b99ee592956bf112179f10bd2ca3f90d90 Mon Sep 17 00:00:00 2001 From: DI20ID Date: Thu, 4 Nov 2021 20:33:06 +0100 Subject: [PATCH 08/11] ehehuhuhuhu --- .../PatternScanALittleBitNaiveFor.cs | 66 +++++++------------ 1 file changed, 25 insertions(+), 41 deletions(-) diff --git a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs index f923da0..fb8b93c 100644 --- a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs +++ b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs @@ -21,7 +21,7 @@ internal static long FindPattern(in byte[] cbMemory, in byte[] cbPattern, string int cbMemoryL = cbMemory.Length; int cbPatternL = cbPattern.Length; int[] cbPatternIndexes = new int[cbPatternL+1]; - int[] tcbPatternIndexes = new int[cbPatternL]; + char[] bMask = szMask.ToCharArray(); int tcbPatternL = 0; int l = 0; @@ -29,13 +29,9 @@ internal static long FindPattern(in byte[] cbMemory, in byte[] cbPattern, string ref byte PcbMemory = ref cbMemory[0]; ref byte PcbPattern = ref cbPattern[0]; ref int PcbPatternIndexes = ref cbPatternIndexes[0]; - ref int tPcbPatternIndexes = ref tcbPatternIndexes[0]; + ref char PbMask = ref bMask[0]; - - - - for (int i = 0; i < cbPatternL; i++) { l++; @@ -63,7 +59,7 @@ internal static long FindPattern(in byte[] cbMemory, in byte[] cbPattern, string ref byte xPcbPattern = ref PcbPattern; ref int xPcbPatternIndexes = ref PcbPatternIndexes; bool check = true; - + for (int j = 0; j < tcbPatternL; j++, xPcbPatternIndexes = ref Unsafe.Add(ref xPcbPatternIndexes, 1), xPcbMemory = ref Unsafe.Add(ref xPcbMemory, xPcbPatternIndexes), xPcbPattern = ref Unsafe.Add(ref xPcbPattern, xPcbPatternIndexes)) { if(xPcbMemory != xPcbPattern) @@ -71,47 +67,35 @@ internal static long FindPattern(in byte[] cbMemory, in byte[] cbPattern, string check = false; break; } - if(j == tcbPatternL -1) + else if(j == tcbPatternL -1) { if (check) return i; } } } } + return -1; } } } - //while (true) - //{ - // if (PcbMemory == PcbPattern) - // { - // ref byte xPcbMemory = ref PcbMemory; - // ref byte xPcbPattern = ref PcbPattern; - // ref int xPcbPatternIndexes = ref PcbPatternIndexes; - // - // - // while (xPcbMemory == xPcbPattern) - // { - // int m = (int)Unsafe.ByteOffset(ref PcbMemory, ref xPcbMemory); - // - // //int n = (int)Unsafe.ByteOffset(ref cbMemory[0], ref xPcbMemory); - // //if (n == 0x198A9A) - // //{ - // // int k = 10; - // //} - // if (m == cbPatternL - 1) - // { - // int n = (int)Unsafe.ByteOffset(ref cbMemory[0], ref PcbMemory); - // return n; - // } - // xPcbPatternIndexes = ref Unsafe.Add(ref xPcbPatternIndexes, 1); - // xPcbMemory = ref Unsafe.Add(ref xPcbMemory, xPcbPatternIndexes); xPcbPattern = ref Unsafe.Add(ref xPcbPattern, xPcbPatternIndexes); - // - // - // } - // } - // - // PcbMemory = ref Unsafe.Add(ref PcbMemory, 1); - // - //} +//while (true) +//{ +// if (PcbMemory == PcbPattern) +// { +// ref byte xPcbMemory = ref PcbMemory; +// ref byte xPcbPattern = ref PcbPattern; +// ref int xPcbPatternIndexes = ref PcbPatternIndexes; +// while (xPcbMemory == xPcbPattern) +// { +// if ((int)Unsafe.ByteOffset(ref PcbMemory, ref xPcbMemory) == cbPatternL - 1) +// { +// int n = (int)Unsafe.ByteOffset(ref cbMemory[0], ref PcbMemory); +// return n; +// } +// xPcbPatternIndexes = ref Unsafe.Add(ref xPcbPatternIndexes, 1); +// xPcbMemory = ref Unsafe.Add(ref xPcbMemory, xPcbPatternIndexes); xPcbPattern = ref Unsafe.Add(ref xPcbPattern, xPcbPatternIndexes); +// } +// } +// PcbMemory = ref Unsafe.Add(ref PcbMemory, 1); +//} From 6ecc5b89350348eca75d791d526c6087c852424d Mon Sep 17 00:00:00 2001 From: DI20ID Date: Fri, 5 Nov 2021 12:33:58 +0100 Subject: [PATCH 09/11] experiments --- .../PatternScanALittleBitNaiveFor.cs | 41 ++++++++++++++----- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs index fb8b93c..6726e39 100644 --- a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs +++ b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs @@ -79,23 +79,44 @@ internal static long FindPattern(in byte[] cbMemory, in byte[] cbPattern, string } } } -//while (true) +//for (int i = 0; i < cbPatternL; i++) //{ +// PbbMask = (byte)PbMask; +// l++; +// if (PbbMask == 120) +// { +// +// iPcbPatternIndexes = l; +// iPcbPatternIndexes = ref Unsafe.Add(ref iPcbPatternIndexes, 1); +// l = 0; +// } +// PbMask = ref Unsafe.Add(ref PbMask, 1); +// PbbMask = ref Unsafe.Add(ref PbbMask, 1); +//} +// +// +//for (int i = 0; i < cbMemoryL; i++, PcbMemory = ref Unsafe.Add(ref PcbMemory, 1)) +//{ +// //if(i == 0x198A9A) +// //{ +// // int k = 10; +// //} // if (PcbMemory == PcbPattern) // { // ref byte xPcbMemory = ref PcbMemory; // ref byte xPcbPattern = ref PcbPattern; // ref int xPcbPatternIndexes = ref PcbPatternIndexes; -// while (xPcbMemory == xPcbPattern) +// +// while (true) // { -// if ((int)Unsafe.ByteOffset(ref PcbMemory, ref xPcbMemory) == cbPatternL - 1) -// { -// int n = (int)Unsafe.ByteOffset(ref cbMemory[0], ref PcbMemory); -// return n; -// } -// xPcbPatternIndexes = ref Unsafe.Add(ref xPcbPatternIndexes, 1); -// xPcbMemory = ref Unsafe.Add(ref xPcbMemory, xPcbPatternIndexes); xPcbPattern = ref Unsafe.Add(ref xPcbPattern, xPcbPatternIndexes); +// xPcbPatternIndexes = ref Unsafe.Add(ref xPcbPatternIndexes, 1); Unsafe.WriteUnaligned(ref xPcbMemory, xPcbPattern); +// xPcbMemory = ref Unsafe.Add(ref xPcbMemory, xPcbPatternIndexes); +// xPcbPattern = ref Unsafe.Add(ref xPcbPattern, xPcbPatternIndexes); +// if (xPcbMemory != xPcbPattern) +// break; +// else if ((int)Unsafe.ByteOffset(ref PcbMemory, ref xPcbMemory) == cbPatternL - 1) +// return i; // } // } -// PcbMemory = ref Unsafe.Add(ref PcbMemory, 1); //} + From 90e016886e44d5f2ee30a83c444466e92f0324de Mon Sep 17 00:00:00 2001 From: DI20ID Date: Fri, 5 Nov 2021 19:17:19 +0100 Subject: [PATCH 10/11] optimization --- .../PatternScanALittleBitNaiveFor.cs | 99 +++++-------------- 1 file changed, 25 insertions(+), 74 deletions(-) diff --git a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs index 6726e39..40b6f64 100644 --- a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs +++ b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs @@ -19,104 +19,55 @@ internal class PatternScanALittleBitNaiveFor internal static long FindPattern(in byte[] cbMemory, in byte[] cbPattern, string szMask) { int cbMemoryL = cbMemory.Length; - int cbPatternL = cbPattern.Length; - int[] cbPatternIndexes = new int[cbPatternL+1]; - - char[] bMask = szMask.ToCharArray(); - int tcbPatternL = 0; - int l = 0; - ref byte PcbMemory = ref cbMemory[0]; + + int cbPatternL = cbPattern.Length; ref byte PcbPattern = ref cbPattern[0]; + int[] cbPatternIndexes = new int[cbPatternL + 1]; ref int PcbPatternIndexes = ref cbPatternIndexes[0]; - + ref int iPcbPatternIndexes = ref cbPatternIndexes[0]; + + char[] bMask = szMask.ToCharArray(); + byte[] bbMask = new byte[cbPatternL]; ref char PbMask = ref bMask[0]; + ref byte PbbMask = ref bbMask[0]; + int l = 0; for (int i = 0; i < cbPatternL; i++) { + PbbMask = (byte)PbMask; l++; - if(PbMask == 120) + if (PbbMask == 120) { - tcbPatternL++; - PcbPatternIndexes = l; - PcbPatternIndexes = ref Unsafe.Add(ref PcbPatternIndexes, 1); - l = 0; + iPcbPatternIndexes = l; + iPcbPatternIndexes = ref Unsafe.Add(ref iPcbPatternIndexes, 1); + l = 0; } PbMask = ref Unsafe.Add(ref PbMask, 1); + PbbMask = ref Unsafe.Add(ref PbbMask, 1); } - PcbPatternIndexes = ref cbPatternIndexes[0]; - for (int i = 0; i < cbMemoryL; i++, PcbMemory = ref Unsafe.Add(ref PcbMemory, 1)) { - //if(i == 0x198A9A) - //{ - // int k = 10; - //} - if(PcbMemory == PcbPattern) + if (PcbMemory == PcbPattern) { ref byte xPcbMemory = ref PcbMemory; ref byte xPcbPattern = ref PcbPattern; ref int xPcbPatternIndexes = ref PcbPatternIndexes; - bool check = true; - - for (int j = 0; j < tcbPatternL; j++, xPcbPatternIndexes = ref Unsafe.Add(ref xPcbPatternIndexes, 1), xPcbMemory = ref Unsafe.Add(ref xPcbMemory, xPcbPatternIndexes), xPcbPattern = ref Unsafe.Add(ref xPcbPattern, xPcbPatternIndexes)) + + while (true) { - if(xPcbMemory != xPcbPattern) - { - check = false; + xPcbPatternIndexes = ref Unsafe.Add(ref xPcbPatternIndexes, 1); + xPcbMemory = ref Unsafe.Add(ref xPcbMemory, xPcbPatternIndexes); + xPcbPattern = ref Unsafe.Add(ref xPcbPattern, xPcbPatternIndexes); + if (xPcbMemory != xPcbPattern) break; - } - else if(j == tcbPatternL -1) - { - if (check) return i; - } + else if ((int)Unsafe.ByteOffset(ref PcbMemory, ref xPcbMemory) == cbPatternL - 1) + return i; } } } - return -1; } } -} -//for (int i = 0; i < cbPatternL; i++) -//{ -// PbbMask = (byte)PbMask; -// l++; -// if (PbbMask == 120) -// { -// -// iPcbPatternIndexes = l; -// iPcbPatternIndexes = ref Unsafe.Add(ref iPcbPatternIndexes, 1); -// l = 0; -// } -// PbMask = ref Unsafe.Add(ref PbMask, 1); -// PbbMask = ref Unsafe.Add(ref PbbMask, 1); -//} -// -// -//for (int i = 0; i < cbMemoryL; i++, PcbMemory = ref Unsafe.Add(ref PcbMemory, 1)) -//{ -// //if(i == 0x198A9A) -// //{ -// // int k = 10; -// //} -// if (PcbMemory == PcbPattern) -// { -// ref byte xPcbMemory = ref PcbMemory; -// ref byte xPcbPattern = ref PcbPattern; -// ref int xPcbPatternIndexes = ref PcbPatternIndexes; -// -// while (true) -// { -// xPcbPatternIndexes = ref Unsafe.Add(ref xPcbPatternIndexes, 1); Unsafe.WriteUnaligned(ref xPcbMemory, xPcbPattern); -// xPcbMemory = ref Unsafe.Add(ref xPcbMemory, xPcbPatternIndexes); -// xPcbPattern = ref Unsafe.Add(ref xPcbPattern, xPcbPatternIndexes); -// if (xPcbMemory != xPcbPattern) -// break; -// else if ((int)Unsafe.ByteOffset(ref PcbMemory, ref xPcbMemory) == cbPatternL - 1) -// return i; -// } -// } -//} - +} \ No newline at end of file From 115f3c162dd2b9c591bbdf7b1af761c9c454137d Mon Sep 17 00:00:00 2001 From: DI20ID Date: Sat, 6 Nov 2021 14:25:55 +0100 Subject: [PATCH 11/11] optimazation --- .../PatternScanALittleBitNaiveFor.cs | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs index 40b6f64..ad4beed 100644 --- a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs +++ b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs @@ -21,7 +21,7 @@ internal static long FindPattern(in byte[] cbMemory, in byte[] cbPattern, string int cbMemoryL = cbMemory.Length; ref byte PcbMemory = ref cbMemory[0]; - int cbPatternL = cbPattern.Length; + int cbPatternL = cbPattern.Length - 1; ref byte PcbPattern = ref cbPattern[0]; int[] cbPatternIndexes = new int[cbPatternL + 1]; ref int PcbPatternIndexes = ref cbPatternIndexes[0]; @@ -32,16 +32,14 @@ internal static long FindPattern(in byte[] cbMemory, in byte[] cbPattern, string ref char PbMask = ref bMask[0]; ref byte PbbMask = ref bbMask[0]; - int l = 0; - for (int i = 0; i < cbPatternL; i++) + for (int i = 0; i <= cbPatternL; i++) { PbbMask = (byte)PbMask; - l++; + iPcbPatternIndexes++; if (PbbMask == 120) { - iPcbPatternIndexes = l; iPcbPatternIndexes = ref Unsafe.Add(ref iPcbPatternIndexes, 1); - l = 0; + iPcbPatternIndexes = 0; } PbMask = ref Unsafe.Add(ref PbMask, 1); PbbMask = ref Unsafe.Add(ref PbbMask, 1); @@ -51,19 +49,22 @@ internal static long FindPattern(in byte[] cbMemory, in byte[] cbPattern, string { if (PcbMemory == PcbPattern) { + if (Unsafe.Add(ref PcbMemory, cbPatternL) == Unsafe.Add(ref PcbPattern, cbPatternL)) + { ref byte xPcbMemory = ref PcbMemory; ref byte xPcbPattern = ref PcbPattern; ref int xPcbPatternIndexes = ref PcbPatternIndexes; - while (true) - { - xPcbPatternIndexes = ref Unsafe.Add(ref xPcbPatternIndexes, 1); - xPcbMemory = ref Unsafe.Add(ref xPcbMemory, xPcbPatternIndexes); - xPcbPattern = ref Unsafe.Add(ref xPcbPattern, xPcbPatternIndexes); - if (xPcbMemory != xPcbPattern) - break; - else if ((int)Unsafe.ByteOffset(ref PcbMemory, ref xPcbMemory) == cbPatternL - 1) - return i; + while (true) + { + xPcbPatternIndexes = ref Unsafe.Add(ref xPcbPatternIndexes, 1); + xPcbMemory = ref Unsafe.Add(ref xPcbMemory, xPcbPatternIndexes); + xPcbPattern = ref Unsafe.Add(ref xPcbPattern, xPcbPatternIndexes); + if (xPcbMemory != xPcbPattern) + break; + else if ((int)Unsafe.ByteOffset(ref PcbMemory, ref xPcbMemory) == cbPatternL) + return i; + } } } }