Skip to content

Instantly share code, notes, and snippets.

@nlitsme
Last active August 29, 2015 14:08
Show Gist options
  • Save nlitsme/e4e7e783a81be7dc662a to your computer and use it in GitHub Desktop.
Save nlitsme/e4e7e783a81be7dc662a to your computer and use it in GitHub Desktop.
print different dwords between two files
use strict;
use warnings;
use IO::File;
sub readbin {
my $fn= shift;
my $fh= IO::File->new($fn, "r") or die "$fn: $!\n";
binmode $fh;
my $data;
$fh->read($data, -s $fn);
$fh->close();
return $data;
}
if (@ARGV!=2) {
die "Usage: cmpbin file1 file2\n";
}
my ($fn1, $fn2)= @ARGV;
my $f1= readbin($fn1);
my $f2= readbin($fn2);
for (my $ofs= 0 ; $ofs < length($f1) && $ofs < length($f2) ; $ofs+=4)
{
if (substr($f1,$ofs,4) ne substr($f2,$ofs,4)) {
my ($l,$r)= unpack("VV", substr($f1,$ofs,4).substr($f2,$ofs,4));
printf("%08lx: %08lx %08lx xor=%08x sub=%08x %s %s\n", $ofs, $l, $r, $l^$r, ($l-$r)&0xFFFFFFFF, disp(substr($f1,$ofs,4)), disp(substr($f2,$ofs,4)));
}
}
if (length($f1) < length($f2)) {
printf("%08x: EOF on %s\n", length($f1), $fn1);
}
elsif (length($f1) > length($f2)) {
printf("%08x: EOF on %s\n", length($f2), $fn2);
}
sub disp {
my $x= shift;
$x =~ s/[\x00-\x1f\x7f-\xff]/./g;
return $x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment