line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MogileFS::Checksum; |
2
|
21
|
|
|
21
|
|
86
|
use strict; |
|
21
|
|
|
|
|
26
|
|
|
21
|
|
|
|
|
610
|
|
3
|
21
|
|
|
21
|
|
79
|
use warnings; |
|
21
|
|
|
|
|
33
|
|
|
21
|
|
|
|
|
691
|
|
4
|
21
|
|
|
21
|
|
91
|
use overload '""' => \&as_string; |
|
21
|
|
|
|
|
24
|
|
|
21
|
|
|
|
|
215
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
my %TYPE = ( |
7
|
|
|
|
|
|
|
"MD5" => { type => 1, bytelen => 128 / 8 }, |
8
|
|
|
|
|
|
|
"SHA-1" => { type => 2, bytelen => 160 / 8 }, |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# see POD for rationale below |
11
|
|
|
|
|
|
|
# "SHA-224" => { type => 3, bytelen => 224 / 8 }, |
12
|
|
|
|
|
|
|
# "SHA-256" => { type => 4, bytelen => 256 / 8 }, |
13
|
|
|
|
|
|
|
# "SHA-384" => { type => 5, bytelen => 384 / 8 }, |
14
|
|
|
|
|
|
|
# "SHA-512" => { type => 6, bytelen => 512 / 8 }, |
15
|
|
|
|
|
|
|
); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our %NAME2TYPE = map { $_ => $TYPE{$_}->{type} } keys(%TYPE); |
18
|
|
|
|
|
|
|
our %TYPE2NAME = map { $NAME2TYPE{$_} => $_ } keys(%NAME2TYPE); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub valid_alg { |
21
|
0
|
|
|
0
|
0
|
|
my ($class, $alg) = @_; |
22
|
|
|
|
|
|
|
|
23
|
0
|
0
|
|
|
|
|
defined($alg) && defined($TYPE{$alg}); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub new { |
27
|
0
|
|
|
0
|
0
|
|
my ($class, $row) = @_; |
28
|
0
|
|
|
|
|
|
my $self = bless { |
29
|
|
|
|
|
|
|
fidid => $row->{fid}, |
30
|
|
|
|
|
|
|
checksum => $row->{checksum}, |
31
|
|
|
|
|
|
|
hashtype => $row->{hashtype} |
32
|
|
|
|
|
|
|
}, $class; |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
return $self; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# $string = "MD5:d41d8cd98f00b204e9800998ecf8427e" |
38
|
|
|
|
|
|
|
sub from_string { |
39
|
0
|
|
|
0
|
0
|
|
my ($class, $fidid, $string) = @_; |
40
|
0
|
0
|
|
|
|
|
$string =~ /\A([\w-]+):([a-fA-F0-9]{32,128})\z/ or |
41
|
|
|
|
|
|
|
die "invalid checksum string"; |
42
|
0
|
|
|
|
|
|
my $hashname = $1; |
43
|
0
|
|
|
|
|
|
my $hexdigest = $2; |
44
|
0
|
0
|
|
|
|
|
my $ref = $TYPE{$hashname} or |
45
|
|
|
|
|
|
|
die "invalid checksum name ($hashname) from $string"; |
46
|
0
|
|
|
|
|
|
my $checksum = pack("H*", $hexdigest); |
47
|
0
|
|
|
|
|
|
my $len = length($checksum); |
48
|
0
|
0
|
|
|
|
|
$len == $ref->{bytelen} or |
49
|
|
|
|
|
|
|
die "invalid checksum length=$len (expected $ref->{bytelen})"; |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
bless { |
52
|
|
|
|
|
|
|
fidid => $fidid, |
53
|
|
|
|
|
|
|
checksum => $checksum, |
54
|
|
|
|
|
|
|
hashtype => $NAME2TYPE{$hashname}, |
55
|
|
|
|
|
|
|
}, $class; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub hashname { |
59
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
60
|
0
|
|
|
|
|
|
my $type = $self->{hashtype}; |
61
|
0
|
0
|
|
|
|
|
my $name = $TYPE2NAME{$type} or die "hashtype=$type unknown"; |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
return $name; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub save { |
67
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
68
|
0
|
|
|
|
|
|
my $sto = Mgd::get_store(); |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
$sto->set_checksum($self->{fidid}, $self->{hashtype}, $self->{checksum}); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub maybe_save { |
74
|
0
|
|
|
0
|
0
|
|
my ($self, $dmid, $classid) = @_; |
75
|
0
|
|
|
|
|
|
my $class = eval { Mgd::class_factory()->get_by_id($dmid, $classid) }; |
|
0
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# $class may be undef as it could've been deleted between |
78
|
|
|
|
|
|
|
# create_open and create_close, we've never verified this before... |
79
|
|
|
|
|
|
|
# class->{hashtype} is also undef, as we allow create_close callers |
80
|
|
|
|
|
|
|
# to specify a hash regardless of class. |
81
|
0
|
0
|
0
|
|
|
|
if ($class && defined($class->{hashtype}) && $self->{hashtype} eq $class->{hashtype}) { |
|
|
|
0
|
|
|
|
|
82
|
0
|
|
|
|
|
|
$self->save; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub hexdigest { |
87
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
88
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
|
unpack("H*", $self->{checksum}); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub as_string { |
93
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
94
|
0
|
|
|
|
|
|
my $name = $self->hashname; |
95
|
0
|
|
|
|
|
|
my $hexdigest = $self->hexdigest; |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
"Checksum[f=$self->{fidid};$name=$hexdigest]" |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub info { |
101
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
$self->hashname . ':' . $self->hexdigest; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
1; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
__END__ |