| 1 | package Digest::ManberHash; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Digest::ManberHash - a Perl package to calculate Manber Hashes | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | use Digest::ManberHash; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | $instance = Digest::ManberHash::new($maskbits, $prime, $charcount); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | $hash1 = $instance->DoHash($filename1); | ||||||||||||||||||||||||||||
| 14 | $hash2 = $instance->DoHash($filename2); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | $similarity = $instance->Compare($hash1, $hash2); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head2 Initialization | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | Use C<Digest::ManberHash::new>. | ||||||||||||||||||||||||||||
| 23 | Parameters: | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =over 4 | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =item maskbits | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | range 1 .. 30, default 11. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =item prime | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | range 3 .. 65537, default 7. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =item charcount | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | range 8 .. 32768, default 64. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =back | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | For a detailed description please read http://citeseer.nj.nec.com/manber94finding.html. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head2 Calculating hashes | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | $hash = $instance->DoHash($filename); | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | This gives an object, which has an hash of hash values stored within. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head2 Comparing hashes | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | $similarity = $instance->Compare($hash1, $hash2); | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | This gives an value of 0.0 .. 1.0, depending on the similariness. | ||||||||||||||||||||||||||||
| 56 | Help wanted: The calculation could do better than now!! | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =cut | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 62 | 1 | require DynaLoader; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | our @ISA = qw(Exporter DynaLoader); | |||||||||||||||||||||||||||
| 65 | # Items to export into callers namespace by default. Note: do not export | ||||||||||||||||||||||||||||
| 66 | # names by default without a very good reason. Use EXPORT_OK instead. | ||||||||||||||||||||||||||||
| 67 | # Do not simply export all your public functions/methods/constants. | ||||||||||||||||||||||||||||
| 68 | 1 | our @EXPORT = qw( | |||||||||||||||||||||||||||
| 69 | HashFile | ||||||||||||||||||||||||||||
| 70 | new | ||||||||||||||||||||||||||||
| 71 | Compare | ||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||
| 73 | 1 | our $VERSION = '0.7'; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub new | ||||||||||||||||||||||||||||
| 77 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 1 | my($class, $maskbits, $prime, $charcount)=@_; | |||||||||||||||||||||||||||
| 79 | 1 | my($x,%a); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | ▶ | $prime||=7; | ||||||||||||||||||||||||||
|
Condition50%1/2
$prime ||= 7
MC/DC0%0/1
$prime ||= 7
$prime
Truth table50%1/2
A
A= $prime
| |||||||||||||||||||||||||||||
| 82 | 1 | ▶ | $maskbits||=11; | ||||||||||||||||||||||||||
|
Condition50%1/2
$maskbits ||= 11
MC/DC0%0/1
$maskbits ||= 11
$maskbits
Truth table50%1/2
A
A= $maskbits
| |||||||||||||||||||||||||||||
| 83 | 1 | ▶ | $charcount||=64; | ||||||||||||||||||||||||||
|
Condition50%1/2
$charcount ||= 64
MC/DC0%0/1
$charcount ||= 64
$charcount
Truth table50%1/2
A
A= $charcount
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | $x=Init($prime,$maskbits,$charcount); | |||||||||||||||||||||||||||
| 86 | 1 | %a=( "settings" => $x ); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1 | bless \%a; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub DoHash | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
DoHash: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | 0 | my($self,$filename)=@_; | |||||||||||||||||||||||||||
| 94 | 0 | my($e,$f,%a,%b); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | %b=(); | |||||||||||||||||||||||||||
| 97 | 0 | ManberHash($self->{"settings"}, $filename, \%b ); | |||||||||||||||||||||||||||
| 98 | 0 | %a= ( "data" => \%b, "base" => $self); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | while (($e, $f) = each(%b)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | { | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | $self->{"max"}{$e}=$f if $self->{"max"}{$e} < $f; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | bless \%a; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub Compare | ||||||||||||||||||||||||||||
| 109 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
Compare: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 110 | 0 | my($self,$file1,$file2)=@_; | |||||||||||||||||||||||||||
| 111 | 0 | my(%keys,$a,$k,$c,$v,$m); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | #return 0 if (ref($self) !~ /^HASH/); | ||||||||||||||||||||||||||||
| 114 | die if $self ne $file1->{"base"} || | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | $self ne $file2->{"base"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self ne $file1->{"base"} or $self ne $file2->{"base"}
MC/DC0%0/2
$self ne $file1->{"base"} or $self ne $file2->{"base"}
$self ne $file1->{"base"}
$self ne $file2->{"base"}
Truth table0%0/3
A or B
A= $self ne $file1->{"base"}B= $self ne $file2->{"base"}
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | %keys=map { $_,1; } (keys %{$file1->{"data"}}, keys %{$file2->{"data"}}); | |||||||||||||||||||||||||||
| 119 | 0 | $c=$a=$m=0; | |||||||||||||||||||||||||||
| 120 | 0 | for $k (keys %keys) | |||||||||||||||||||||||||||
| 121 | { | ||||||||||||||||||||||||||||
| 122 | 0 | $v = ($file1->{"data"}->{$k} - $file2->{"data"}->{$k}); | |||||||||||||||||||||||||||
| 123 | # $m += $self->{"max"}{$k} * $self->{"max"}{$k}; | ||||||||||||||||||||||||||||
| 124 | 0 | $a += $v*$v; | |||||||||||||||||||||||||||
| 125 | 0 | $c++; | |||||||||||||||||||||||||||
| 126 | # print "$k = ",$self->{$k}," - ",$other->{$k},"($c, $a)\n"; | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | return 0 if !$c; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | # 1 - 6*$a/($c*$c*$c - $c); | ||||||||||||||||||||||||||||
| 131 | # 1-sqrt($a)/$c; | ||||||||||||||||||||||||||||
| 132 | 0 | 1/(1.0+$a); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 1 | bootstrap Digest::ManberHash $VERSION; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # Preloaded methods go here. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # Autoload methods go after __END__, and are processed by the autosplit program. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 1 | 1; | |||||||||||||||||||||||||||
| 142 | __END__ | ||||||||||||||||||||||||||||