EXTERN_C  LPVOID   WINAPI Kmp(
LPVOID  lpAddress,
LPVOID  lpSignBuff,
INT     nBufferSize,
INT     nSignSize,
INT     nStart
)
{
INT  i = 0;
INT  k = nStart;
LPBYTE       lpSign  = (LPBYTE)lpSignBuff;
LPBYTE       lpBuffer    = (LPBYTE)lpAddress;  
LPDWORD  lpNextArray = NULL;
LPVOID       lpRel  = NULL;
if ( NULL == lpBuffer || NULL == lpSign )
{
return lpRel;
} lpNextArray = (LPDWORD)LocalAlloc(LPTR,nSignSize*sizeof(DWORD));
if (NULL == lpNextArray)
{
return lpRel;
} GetNextArray(lpSign,lpNextArray,nSignSize); while( i < nSignSize && k < nBufferSize)
{
if(lpBuffer[k] == lpSign[i] || -1 == i)
{
k++;
i++;
}
else
{
i = lpNextArray[i];
}
} if( i >= nSignSize) 

k -= nSignSize;
lpRel = (LPVOID)((LPBYTE)lpAddress + k);
if (lpRel == lpSign)
{
lpRel = NULL;
//过滤特征串.... 
} } if (NULL != lpNextArray)
{
LocalFree((HLOCAL)lpNextArray);
} return  lpRel;
}

解决方案 »

  1.   

    还是我来吧。
    function Kmp(lpAddress, lpSignBuff: Pointer;
      nBufferSize, nSignSize, nStart: Integer): Pointer;
    type
      TDWORDArray = array[0..(MaxLongInt div 4) - 1] of LongWord;
      PDWORDArray = ^TDWORDArray;
    var
      i, k: Integer;
      lpSign: PByteArray;
      lpBuffer: PByteArray;
      lpNextArray: PDWORDArray;
    begin
      Result := nil;
      if (lpAddress = nil) or (lpSignBuff = nil) then
        Exit;
      i := 0;
      k := nStart;
      lpSign := lpSignBuff;
      lpBuffer := lpAddress;
      lpNextArray := PDWORDArray(LocalAlloc(LPTR, nSignSize * sizeof(LongWord)));
      if lpNextArray = nil then Exit;
      try
        GetNextArray(lpSign, lpNextArray, nSignSize);
        while (i < nSignSize) and (k < nBufferSize) do
        begin
          if (lpBuffer[k] = lpSign[i]) or (i = -1) then
          begin
            Inc(i);
            Inc(k);
          end
          else
            i := lpNextArray[i];
        end;
        if  i >= nSignSize then
        begin
          Dec(k, nSignSize);
          Result := Pointer(LongWord(lpAddress) + k);
          if Result = lpSign then
          begin
            Result := nil;
            //过滤特征串....
          end;
        end;
      finally
        LocalFree(HLOCAL(lpNextArray));
      end;
    end;
      

  2.   

    更正:
    GetNextArray(lpSign, lpNextArray, nSignSize);
    为:
    GetNextArray(lpSign, PLongWord(lpNextArray), nSignSize);
      

  3.   

    上一个贴翻译中还有一个错误,已经修正(见原贴),这里干脆全部重新贴一遍,如果有错误,请告知:
    type
      TDWORDArray = array[0..(MaxLongInt div 4) - 1] of LongWord;
      PDWORDArray = ^TDWORDArray;procedure GetNextArray(lpSign: Pointer; lpNextBuffer: PDWORDArray; nSize: Integer);
    var
      i, k: Integer;
      lpSubBuffer: PByteArray;
    begin
      if (lpSign = nil) or (lpNextBuffer = nil) then Exit;
      i := 0;
      k := -1;
      lpSubBuffer := lpSign;
      lpNextBuffer[0] := LongWord(-1);
      while i < nSize - 1 do
      begin
        if (k = -1) or (lpSubBuffer[i] = lpSubBuffer[k]) then
        begin
          Inc(i);
          Inc(k);
          lpNextBuffer[i] := k;
        end
        else
          k := lpNextBuffer[k];
      end;
    end;function Kmp(lpAddress, lpSignBuff: Pointer;
      nBufferSize, nSignSize, nStart: Integer): Pointer;
    var
      i, k: Integer;
      lpSign: PByteArray;
      lpBuffer: PByteArray;
      lpNextArray: PDWORDArray;
    begin
      Result := nil;
      if (lpAddress = nil) or (lpSignBuff = nil) then
        Exit;
      i := 0;
      k := nStart;
      lpSign := lpSignBuff;
      lpBuffer := lpAddress;
      lpNextArray := PDWORDArray(LocalAlloc(LPTR, nSignSize * sizeof(LongWord)));
      if lpNextArray = nil then Exit;
      try
        GetNextArray(lpSign, lpNextArray, nSignSize);
        while (i < nSignSize) and (k < nBufferSize) do
        begin
          if (lpBuffer[k] = lpSign[i]) or (i = -1) then
          begin
            Inc(i);
            Inc(k);
          end
          else
            i := lpNextArray[i];
        end;
        if  i >= nSignSize then
        begin
          Dec(k, nSignSize);
          Result := Pointer(LongWord(lpAddress) + k);
          if Result = lpSign then
          begin
            Result := nil;
            //过滤特征串....
          end;
        end;
      finally
        LocalFree(HLOCAL(lpNextArray));
      end;
    end;
      

  4.   

    原来阿发伯不仅仅会玩gdi plus 啊。