我打开文件把文件分块读取,大小是1000,然后在这1000字节里面查找我要找的hex值比如查找7E 97 98 99 9A 26,找到就输出,但是我的代码执行很慢,不知道有没有更好的解决方法
for int:=0 to 1000 do
begin
//MessageBox(0,Pchar(Inttohex(integer(p^),0)+Inttohex(integer(p[1]),0)),'',mb_ok);
NewReada:= inttohex(Cardinal(p^),0) + inttohex(Cardinal(p[1]),0) +
inttohex(Cardinal(p[2]),0) + inttohex(Cardinal(p[3]),0) +
inttohex(Cardinal(p[4]),0) + inttohex(Cardinal(p[5]),0); inc(p);
end;//这样很慢,要转换成hex来匹配,然后把读取的这6个hex值(7E 97 98 99 9A 26)放到hash里面去查找,找到就输出。
for int:=0 to 1000 do
begin
//MessageBox(0,Pchar(Inttohex(integer(p^),0)+Inttohex(integer(p[1]),0)),'',mb_ok);
NewReada:= inttohex(Cardinal(p^),0) + inttohex(Cardinal(p[1]),0) +
inttohex(Cardinal(p[2]),0) + inttohex(Cardinal(p[3]),0) +
inttohex(Cardinal(p[4]),0) + inttohex(Cardinal(p[5]),0); inc(p);
end;//这样很慢,要转换成hex来匹配,然后把读取的这6个hex值(7E 97 98 99 9A 26)放到hash里面去查找,找到就输出。
NewReada:= inttohex(Cardinal(p^),0) + inttohex(Cardinal(p[1]),0) +
inttohex(Cardinal(p[2]),0) + inttohex(Cardinal(p[3]),0) +
inttohex(Cardinal(p[4]),0) + inttohex(Cardinal(p[5]),0);for int:=0 to 1000-6 do
begin
//处理NewReada
//MessageBox(0,Pchar(Inttohex(integer(p^),0)+Inttohex(integer(p[1]),0)),'',mb_ok);
inc(p);
NewReada1:=PChar(Integer(NewReada)+2);
NewReada2:=inttohex(Cardinal(p[5]),0);
...//这里能释放NewReada占用的内存最好
NewReada:=NewReada1+NewReada2;不是最优,但应该要快些,但是内存占用也要大些没运行,可能有错
一个字节一个字节都需要转换,肯定慢。
用kmp,自己写过的一个东西,
先把要找的转换为字符串function KMP(const ASource, ASub: string; StartPos: Integer = 1): Integer;
//字符串搜索
var
F: array of Integer;
I, J, SrcLen, SubLen: Integer;
begin
Result := 0;
SubLen := Length(ASub);
SetLength(f, SubLen + 1);
SrcLen := Length(ASource);
F[0] := 0;
F[1] := 0; I := 1; J := 0;
FillChar(F[0], SizeOf(Integer) * (SubLen + 1), 0);
while I < SubLen do
if (J = 0) or (ASub[I] = ASub[J]) then
begin
Inc(I);
Inc(J);
F[I] := J;
end else
J := F[J]; J := 1;
I := StartPos;
while (I <= SrcLen) and (J <= SubLen) do
begin
if ASource[I] = ASub[J] then
begin
Inc(I);
Inc(J);
end
else
if J = 1 then
Inc(I)
else
J := F[J - 1] + 1;
if J > SubLen then
begin
Result := I - SubLen;
break;
end;
end;
end;function FindStrInFile(Str, FileName: string): integer;
//在文件中搜索字符串过程
var
FFileHandle: THandle;
FMapHandle: THandle;
FFileSize: Integer;
PData: pointer;
Size: Integer;
Buffer: TBytes;
s: string;
Encoding: TEncoding;
begin
FFileHandle := FileOpen(filename, fmOpenRead);
FFileSize := GetFileSize(FFileHandle, nil);
FMapHandle := CreateFileMapping(FFileHandle, nil, PAGE_READONLY, 0, FFileSize, nil);
CloseHandle(FFileHandle);
PData := MapViewOfFile(FMapHandle, FILE_MAP_READ, 0, 0, FFileSize);
if PData <> nil then
begin
SetLength(Buffer, FFileSize);
Move(PData^, Buffer[0], FFileSize);
end;
UnmapViewOfFile(PData);
CloseHandle(FMapHandle);
Encoding := nil;
Size := TEncoding.GetBufferEncoding(Buffer, Encoding);
s := Encoding.GetString(Buffer, Size, Length(Buffer) - Size);
result := KMP(s, str, 1);
end;
for int:=0 to 1000 do
begin
Move(p^,NewReadaTemp,6);//、很慢 p是我读取1000字节的指针
NewReada:= inttohex(NewReadaTemp[0],0) + inttohex(NewReadaTemp[1],0)+
inttohex(NewReadaTemp[2],0)+ inttohex(NewReadaTemp[3],0)+
inttohex(NewReadaTemp[4],0)+inttohex(NewReadaTemp[5],0);
//MessageBox(0,pchar(NewReada),'',mb_ok);exit;
//showmessage(format('%s',[NewReadaTemp]);exit;
Tmpa:=ScanReada.Values[NewReada];//将特征转换成整数
if (Tmpa<>'') then
begin
MEssageBox(0,PAnsiChar(Tmpa),'',MB_OK);break;exit;
end;
inc(p);
end;
并且如果你查找的字符串正好被分割了,你会查不到。
unsigned long CKsScanner::ScanCode(PVOID pAddr, DWORD dwSize)
{
#ifdef __KSSUPERSWORD_H__
// 请使用时依情况改变
CListCtrl& m_list=((CScannerDialog*)theApp.m_pMainWnd)->m_list;
#endif
char* p = NULL;
DWORD dwScore = 0;
DWORD dwLimit =(DWORD)pAddr + dwSize ;
for(p = (char*)pAddr; (DWORD)p < (DWORD)pAddr + dwSize; p++)
{
HASHTYPE hash;
if((DWORD)p + sizeof(HASHTYPE) >=dwLimit) // 大过了??想想怎么办...
{
// 调出下一页
char szTemp[0x50];
memset(szTemp, 0, 0x50);
memcpy(szTemp, p, (char*)dwLimit - p); // 先复制前半部分
memcpy((char*)((DWORD)szTemp + dwLimit - (DWORD)p),pBuffer[nCur?0:1], 5);
//MessageBox(0,szTemp,"",MB_OK);
hash = *(HASHTYPE*)szTemp;
// 完全匹配.加!
dwScore += q->dwScore;
#ifdef __KSSUPERSWORD_H__
// 显示信息,依情况添加
char sz[16]="";
wsprintf(sz,"%d",q->dwScore );
m_list.InsertItem(m_list.GetItemCount(),sz);
wsprintf(sz,"0x%08lX",(DWORD)((DWORD)p - (DWORD)pBuffer[nCur] + m_CurBase));
m_list.SetItemText(m_list.GetItemCount()-1,1,sz);
m_list.SetItemText(m_list.GetItemCount()-1,2,strPath);
m_list.SetItemText(m_list.GetItemCount()-1,3,q->pInfo);
#endif
if(q->bBreak)
goto out;
}
lend:
q = q->pNext;
}
}
out: return dwScore;
}