#!/usr/bin/perl -l

#based on something i found on 
#http://www.ale.org/archive/ale/ale-2004-11/msg00759.html
#
#syntax:
#
#bingrep.pl [-s] <hexstring> filename
#e.g.
#bingrep.pl 00f3fe99 binary_file.dat
#
#with -s, bingrep will exit after the first match.
#
#~Felix (niteling@uvic.ca).

BEGIN{$/=\1024}

if ($ARGV[0] eq "-s") {
	$stop = 1;
	shift @ARGV;
}

$hex = $ARGV[0];
die "hex string must have even length and exist!"
	if ((length $hex) % 2 or 0 == length $hex);
$filename = $ARGV[1];

$hex =~ s/(..)/\\x\1/g;

open(FH,$filename) or die "cannot open file";

while(<FH>) {
	if (/$hex/) {
		print "$filename: hit byte ", ($.-1) * 1024 + $-[0] ;
		exit if $stop;
	}
}
