summaryrefslogtreecommitdiff
path: root/hexfind.lpr
blob: 4d129d08d0e97d5e151de34c0536fe8dafea7cdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
program hexfind;

uses
  math;

type
  tByteArray = array of byte;

procedure loadFile(out ar: tByteArray; nam: string);
var
  f: file;
begin
  assignfile(f,nam);
  reset(f,1);
  setlength(ar,filesize(f));
  blockread(f,ar[0],length(ar));
  closefile(f);
end;

function inttohex(b: byte): string;
const
  h2s = '0123456789abcdef';
begin
  result:=
    h2s[1 + ((b and $f0) shr 4)]
    + h2s[1 + (b and $f)];
end;

var
  haystack,needle: tByteArray;
  iHay,iNee,i:     longint;

const
  matchLen = 10;

begin
  if paramcount<>2 then begin
    writeln('usage: '+paramstr(0)+' haystack needle');
    halt(1);
  end;

  loadFile(haystack,paramstr(1));
  loadFile(needle,paramstr(2));

  for iHay:=1 to length(haystack)-matchlen do
    for iNee:=0 to min(length(needle)-1,length(haystack)-iHay) do
      if haystack[length(haystack)-iHay-iNee] <> needle[length(needle)-1-iNee] then begin
        if iNee<matchLen then
          break;

        write('<');
        for i:=0 to length(haystack)-iHay-iNee do
          write(' '+inttohex(haystack[i]));
        writeln;

        write('>');
        for i:=0 to length(needle)-1-iNee do
          write(' '+inttohex(needle[i]));
        writeln;

        for i:=0 to 79 do
          write('-');
        writeln;

        write('<');
        for i:=length(haystack)-iHay+1 to length(haystack)-1 do
          write(' '+inttohex(haystack[i]));
        writeln;

        setlength(haystack,0);
        setlength(needle,0);
        halt;
      end;

  setlength(haystack,0);
  setlength(needle,0);
end.