Parsi Coders
Seek bytes and return offset - نسخه قابل چاپ

+- Parsi Coders (http://parsicoders.com)
+-- انجمن: Software Development Programming (http://parsicoders.com/forumdisplay.php?fid=37)
+--- انجمن: Pascal/Delphi (http://parsicoders.com/forumdisplay.php?fid=45)
+---- انجمن: Delphi (http://parsicoders.com/forumdisplay.php?fid=69)
+---- موضوع: Seek bytes and return offset (/showthread.php?tid=1735)



Seek bytes and return offset - Amin_Mansouri - 01-30-2012

کد:
{


Protocol.
}

program Project2;

{$APPTYPE CONSOLE}

uses
  Windows;


function CompareMem(P1, P2: Pointer; Length: Integer): Boolean; assembler;
(* ***** BEGIN LICENSE BLOCK *****
*
* The function CompareMem is licensed under the CodeGear license terms.
*
* The initial developer of the original code is Fastcode
*
* Portions created by the initial developer are Copyright (C) 2002-2004
* the initial developer. All Rights Reserved.
*
* Contributor(s): Aleksandr Sharahov
*
* ***** END LICENSE BLOCK ***** *
*************************************************
              Ripped from sysutils
*************************************************)
asm
   add   eax, ecx
   add   edx, ecx
   xor   ecx, -1
   add   eax, -8
   add   edx, -8
   add   ecx, 9
   push  ebx
   jg    @Dword
   mov   ebx, [eax+ecx]
   cmp   ebx, [edx+ecx]
   jne   @Ret0
   lea   ebx, [eax+ecx]
   add   ecx, 4
   and   ebx, 3
   sub   ecx, ebx
   jg    @Dword
@DwordLoop:
   mov   ebx, [eax+ecx]
   cmp   ebx, [edx+ecx]
   jne   @Ret0
   mov   ebx, [eax+ecx+4]
   cmp   ebx, [edx+ecx+4]
   jne   @Ret0
   add   ecx, 8
   jg    @Dword
   mov   ebx, [eax+ecx]
   cmp   ebx, [edx+ecx]
   jne   @Ret0
   mov   ebx, [eax+ecx+4]
   cmp   ebx, [edx+ecx+4]
   jne   @Ret0
   add   ecx, 8
   jle   @DwordLoop
@Dword:
   cmp   ecx, 4
   jg    @Word
   mov   ebx, [eax+ecx]
   cmp   ebx, [edx+ecx]
   jne   @Ret0
   add   ecx, 4
@Word:
   cmp   ecx, 6
   jg    @Byte
   movzx ebx, word ptr [eax+ecx]
   cmp   bx, [edx+ecx]
   jne   @Ret0
   add   ecx, 2
@Byte:
   cmp   ecx, 7
   jg    @Ret1
   movzx ebx, byte ptr [eax+7]
   cmp   bl, [edx+7]
   jne   @Ret0
@Ret1:
   mov   eax, 1
   pop   ebx
   ret
@Ret0:
        XOR     EAX,EAX
   pop   ebx
end;


const
  HexTbl: array[0..15] of Char = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');

function IntToHex(val: DWORD; minLen: Integer): string;
begin
  Result := '';
  while val<>0 do
  begin
    Result := HexTbl[val and $F] + Result;
    val := val shr 4;
  end;

  while Length(Result)<minLen do
    Result := '0' + Result;
end;

Function SeekBytes (lpFileName :PChar;  lpOldBytes :PByte;  dwLength  :DWORD;
                    dwOffset  :DWORD) :DWORD;
var
  dwFileSize            :DWORD;
  hFile                 :Longword;
  lpBuffer              :PByte;
  dwNumberOfBytesRead   :DWORD;
begin
Result  := 0;
if dwLength = 0 then exit;
  hFile :=  CreateFile(lpFileName,GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    if hFile <> INVALID_HANDLE_VALUE then
    begin
      SetFilePointer(hFile, 0, nil, FILE_BEGIN);
      dwFileSize  :=  GetFileSize(hFile,nil);
      lpBuffer    :=  VirtualAlloc(nil, dwLength, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
      FillChar(lpBuffer^,dwLength,0);
      if lpBuffer <>  nil then
      begin
        dwNumberOfBytesRead := 0;
        Repeat
          SetFilePointer(hFile, dwOffSet, nil, FILE_BEGIN);
          ReadFile(hFile,lpBuffer^,dwLength,dwNumberOfBytesRead,nil);
            if CompareMem(lpBuffer,lpOldBytes,dwLength) = True then
            begin
              Result := dwoffset;
              Break;
            end;
          Inc(dwOffset);
        Until dwOffset >= dwFileSize;
      end;
      VirtualFree(lpBuffer,dwLength,MEM_RELEASE);
    end;
  CloseHandle(hFile);
end;

var
  SearchFor :Array[0..2] of byte = ($00,$00,$00);
  Offset  :DWORD;
begin
  Offset  :=  SeekBytes('C:\server.txt',@(SearchFor),SizeOf(SearchFor), 0);
  Writeln('Offset : ',IntToHex(Offset ,8));
  Readln;

end.