summaryrefslogtreecommitdiff
path: root/hexfind.lpr
diff options
context:
space:
mode:
Diffstat (limited to 'hexfind.lpr')
-rw-r--r--hexfind.lpr78
1 files changed, 78 insertions, 0 deletions
diff --git a/hexfind.lpr b/hexfind.lpr
new file mode 100644
index 0000000..4d129d0
--- /dev/null
+++ b/hexfind.lpr
@@ -0,0 +1,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.
+