X-Git-Url: http://git.grml.org/?a=blobdiff_plain;f=usr_bin%2Fbincompare.pl;fp=usr_bin%2Fbincompare.pl;h=767b8a397e31233ffce9c093e7c44695617b0e6f;hb=1b8dcad8b9da618cb6c0e22a762cafb08e262640;hp=0000000000000000000000000000000000000000;hpb=608ca3eeeacd74efa85ad44f5585d1b1b60c8d85;p=grml-scripts-core.git diff --git a/usr_bin/bincompare.pl b/usr_bin/bincompare.pl new file mode 100755 index 0000000..767b8a3 --- /dev/null +++ b/usr_bin/bincompare.pl @@ -0,0 +1,68 @@ +#!/usr/bin/perl +# Filename: bincompare.pl +# Purpose: Binary File Similarity Checking +# Authors: (C) Copyright 2004 Diomidis Spinellis +# Bug-Reports: see http://grml.org/bugs/ +# License: See notes by author (Diomidis Spinellis). +################################################################################ +# See http://www.dmst.aueb.gr/dds/blog/20040319/index.html +# +# Original notes: +# +# (C) Copyright 2004 Diomidis Spinellis +# +# Permission to use, copy, and distribute this software and its +# documentation for any purpose and without fee is hereby granted, +# provided that the above copyright notice appear in all copies and that +# both that copyright notice and this permission notice appear in +# supporting documentation. +# +# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED +# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF +# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + +# Return the entropy of the file passed as the argument +sub +entropy +{ + my($file) = @_; + my($i, $l); + + # File length + $l = `wc -c $file`; + # File information (approximation) + $i = `bzip2 -c $file | wc -c`; + print STDERR "$0: warning file size exceeds bzip2 block size\n" if ($l > 900 * 1024); + return ($i / $l); +} + + +# Return the entropy of the two files passed as arguments +sub +entropy2 +{ + my($file1, $file2) = @_; + my($oldrs) = ($/); + my($tmp) = ("/tmp/entropy.$$"); + + undef($/); + open(IN, $file1) || die "read from $file1: $!\n"; + binmode(IN); + open(OUT, ">$tmp") || die "write to $tmp: $!\n"; + print OUT ; + open(IN, $file2) || die "read from $file2: $!\n"; + binmode(IN); + print OUT ; + close(IN); + close(OUT); + my($e) = (entropy($tmp)); + unlink($tmp); + return ($e); +} + +$#ARGV == 1 || die "Usage $0: file1 file2\n"; + +printf("%.3g - Entropy of $ARGV[0]\n", $e0 = entropy($ARGV[0])); +printf("%.3g - Entropy of $ARGV[1]\n", $e1 = entropy($ARGV[1])); +printf("%.3g - Combined predicted entropy\n", ($e0 + $e1) / 2); +printf("%.3g - Combined actual entropy\n", entropy2($ARGV[0], $ARGV[1]));