Depend on console-tools; drop runit
[grml-scripts.git] / usr_bin / bincompare.pl
1 #!/usr/bin/perl
2 # Filename:      bincompare.pl
3 # Purpose:       Binary File Similarity Checking
4 # Authors:       (C) Copyright 2004 Diomidis Spinellis
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       See notes by author (Diomidis Spinellis).
7 # Latest change: Sam Okt 16 22:54:03 CEST 2004 [mika]
8 ################################################################################
9 # See http://www.dmst.aueb.gr/dds/blog/20040319/index.html
10 #
11 # Original notes:
12
13 # (C) Copyright 2004 Diomidis Spinellis
14 #
15 # Permission to use, copy, and distribute this software and its
16 # documentation for any purpose and without fee is hereby granted,
17 # provided that the above copyright notice appear in all copies and that
18 # both that copyright notice and this permission notice appear in
19 # supporting documentation.
20 #
21 # THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
22 # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
23 # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24
25 # Return the entropy of the file passed as the argument
26 sub
27 entropy
28 {
29         my($file) = @_;
30         my($i, $l);
31
32         # File length
33         $l = `wc -c $file`;
34         # File information (approximation)
35         $i = `bzip2 -c $file | wc -c`;
36         print STDERR "$0: warning file size exceeds bzip2 block size\n" if ($l > 900 * 1024);
37         return ($i / $l);
38 }
39
40
41 # Return the entropy of the two files passed as arguments
42 sub
43 entropy2
44 {
45         my($file1, $file2) = @_;
46         my($oldrs) = ($/);
47         my($tmp) = ("/tmp/entropy.$$");
48
49         undef($/);
50         open(IN, $file1) || die "read from $file1: $!\n";
51         binmode(IN);
52         open(OUT, ">$tmp") || die "write to $tmp: $!\n";
53         print OUT <IN>;
54         open(IN, $file2) || die "read from $file2: $!\n";
55         binmode(IN);
56         print OUT <IN>;
57         close(IN);
58         close(OUT);
59         my($e) = (entropy($tmp));
60         unlink($tmp);
61         return ($e);
62 }
63
64 $#ARGV == 1 || die "Usage $0: file1 file2\n";
65
66 printf("%.3g - Entropy of $ARGV[0]\n", $e0 = entropy($ARGV[0]));
67 printf("%.3g - Entropy of $ARGV[1]\n", $e1 = entropy($ARGV[1]));
68 printf("%.3g - Combined predicted entropy\n", ($e0 + $e1) / 2);
69 printf("%.3g - Combined actual entropy\n", entropy2($ARGV[0], $ARGV[1]));