Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any "Pos" function to find bytes?

var
  FileBuff: TBytes;
  Pattern: TBytes;
begin
  FileBuff := filetobytes(filename);
  Result := CompareMem(@Pattern[0], @FileBuff[0], Length(Pattern));
end;

Is there any function such as

Result := Pos(@Pattern[0], @FileBuff[0]);
like image 417
user Avatar asked Feb 10 '11 16:02

user


1 Answers

I think this does it:

function BytePos(const Pattern: TBytes; const Buffer: PByte; const BufLen: cardinal): PByte;
var
  PatternLength: cardinal;
  i: cardinal;
  j: cardinal;
  OK: boolean;
begin
  result := nil;
  PatternLength := length(Pattern);
  if PatternLength > BufLen then Exit;
  if PatternLength = 0 then Exit(Buffer);
  for i := 0 to BufLen - PatternLength do
    if PByte(Buffer + i)^ = Pattern[0] then
    begin
      OK := true;
      for j := 1 to PatternLength - 1 do
        if PByte(Buffer + i + j)^ <> Pattern[j] then
        begin
          OK := false;
          break
        end;
      if OK then
        Exit(Buffer + i);
    end;
end;
like image 108
Andreas Rejbrand Avatar answered Oct 13 '22 23:10

Andreas Rejbrand