diff --git a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs index 6b249b3..ad4beed 100644 --- a/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs +++ b/PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs @@ -19,56 +19,51 @@ 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]; - int[] tcbPatternIndexes = new int[cbPatternL]; - ref byte PcbMemory = ref cbMemory[0]; + + int cbPatternL = cbPattern.Length - 1; ref byte PcbPattern = ref cbPattern[0]; + int[] cbPatternIndexes = new int[cbPatternL + 1]; ref int PcbPatternIndexes = ref cbPatternIndexes[0]; - ref int tPcbPatternIndexes = ref tcbPatternIndexes[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 tcbPatternL= 0; - int l = 0; - for (int i = 0; i < cbPatternL; i++) + for (int i = 0; i <= cbPatternL; i++) { - l++; - if(szMask[i] == 'x') + PbbMask = (byte)PbMask; + iPcbPatternIndexes++; + if (PbbMask == 120) { - tcbPatternL++; - PcbPatternIndexes = l; - PcbPatternIndexes = ref Unsafe.Add(ref PcbPatternIndexes, 1); - l = 0; + iPcbPatternIndexes = ref Unsafe.Add(ref iPcbPatternIndexes, 1); + iPcbPatternIndexes = 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) { + 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; - 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) - { - check = false; - break; - } - if(j == tcbPatternL -1) + while (true) { - if (check) return i; + 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; } } } @@ -76,4 +71,4 @@ internal static long FindPattern(in byte[] cbMemory, in byte[] cbPattern, string return -1; } } -} +} \ No newline at end of file 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();