所以我使用以下代碼從記憶體中讀取:
public static extern bool ReadProcessMemory(IntPtr handle, IntPtr baseAddress, [Out] byte[] buffer, int size, out IntPtr numberOfBytesRead);
它使用上面的代碼將記憶體十六進制代碼輸出為位元組陣列:
buffer[0] = 01;
buffer[1] = 2D;
buffer[2] = F2;
我想在某個十六進制代碼的某個范圍內搜索某個十六進制陣列。為此,我想使用“用于模式搜索的 KMP 演算法”。
目前我正在使用以下代碼來實作:
byte[] moduleBytes = {0};
IntPtr bytesRead;
ReadProcessMemory(process.Handle, baseAddress, moduleBytes, moduleBytes.Length, out bytesRead);
string buffer = "";
foreach (byte bytesfrommemory in moduleBytes)
{
buffer = bytesfrommemory.ToString("X");;
}
//algorithm
string data = buffer;
int[] value = SearchString(data, pattern);
foreach (int entry in value)
{
Console.WriteLine(entry); //Outputs the offset where it found the code
}
問題在于回圈遍歷每個位元組以將其添加到緩沖區字串需要 1000 個位元組。有沒有更快的方法將位元組陣列從陣列“轉換”為字串而不實際轉換它,因為我仍然需要原始位元組陣列作為字串?
我使用以下代碼進行了嘗試,但它將其轉換為不同的內容:
char[] characters = moduleBytes.Select(o => (char)o).ToArray();
string buffer = new string(characters);
謝謝你的幫助 :)
uj5u.com熱心網友回復:
Alexei 的評論是正確的;您正在將位元組轉換為字串以運行搜索演算法,該演算法將字串轉換為字符陣列(即位元組陣列,即您開始使用的陣列)以完成其作業。使用 KMP 在位元組陣列中查找位元組陣列與在字串中查找字串相同
為了證明我的觀點,我考慮了一個適用于字串的 KMP 實作。我在 Geeks For Geeks 找到了一個并將其從處理字串轉換為處理位元組,實際上只是編輯方法呼叫中的型別;string 有一個 Length 并且可以像陣列一樣被索引,byte array 有一個 Length 并且可以被索引,因為它是一個陣列等 - 不再需要使這個版本作業:
// C# program for implementation of KMP pattern
// searching algorithm
using System;
public class GFG {
void KMPSearch(byte[] pat, byte[] txt)
{
int M = pat.Length;
int N = txt.Length;
// create lps[] that will hold the longest
// prefix suffix values for pattern
int[] lps = new int[M];
int j = 0; // index for pat[]
// Preprocess the pattern (calculate lps[]
// array)
computeLPSArray(pat, M, lps);
int i = 0; // index for txt[]
while (i < N) {
if (pat[j] == txt[i]) {
j ;
i ;
}
if (j == M) {
Console.Write("Found pattern "
"at index " (i - j));
j = lps[j - 1];
}
// mismatch after j matches
else if (i < N && pat[j] != txt[i]) {
// Do not match lps[0..lps[j-1]] characters,
// they will match anyway
if (j != 0)
j = lps[j - 1];
else
i = i 1;
}
}
}
void computeLPSArray(byte[] pat, int M, int[] lps)
{
// length of the previous longest prefix suffix
int len = 0;
int i = 1;
lps[0] = 0; // lps[0] is always 0
// the loop calculates lps[i] for i = 1 to M-1
while (i < M) {
if (pat[i] == pat[len]) {
len ;
lps[i] = len;
i ;
}
else // (pat[i] != pat[len])
{
// This is tricky. Consider the example.
// AAACAAAA and i = 7. The idea is similar
// to search step.
if (len != 0) {
len = lps[len - 1];
// Also, note that we do not increment
// i here
}
else // if (len == 0)
{
lps[i] = len;
i ;
}
}
}
}
// Driver program to test above function
public static void Main()
{
string txt = System.Text.Encoding.ASCII.GetBytes("ABABDABACDABABCABAB");
string pat = System.Text.Encoding.ASCII.GetBytes("ABABCABAB");
new GFG().KMPSearch(pat, txt);
}
}
// This code has been contributed by Amit Khandelwal.
最大的作業(鍵入的鍵數最多)是System.Text.Encoding.ASCII.GetBytes用來獲取一對位元組陣列以供輸入 ??
帶走點;根本不轉換您的記憶體位元組 - 將您的搜索詞轉換為位元組并在記憶體位元組中搜索它
免責宣告:我提供零保證,即這是 KMP 的正確實作,可以充分滿足您的用例。它似乎有效,但我只是指出您不需要轉換為字串并再次回傳來操作從根本上可以以與搜索字串完全相同的方式搜索位元組的代碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/340386.html
上一篇:為樹中的節點添加顏色
