blib/lib/Crypt/Rhash.pm

filter: Statementstmt 95.2% 202 / 212 Branchbran 54.3% 25 / 46 Conditioncond 50.0% 11 / 22 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 98.3% 59 / 60 Podpod 87.5% 14 / 16 totaltotal 85.6% 315 / 368 CC 34 SCAR 20.3
CC
34
Cov
85%
AUTOLOAD
56.0
new
11.9
update_fd
9.2
CRAP 7.6
↑ Summary _rhash_torrent.c »
1 package Crypt::Rhash;
2  
3 3 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 require Exporter;
8 3 our @ISA = (qw(Exporter));
9  
10 # possible tags for export
11 3 our %EXPORT_TAGS = (
12 Functions => [qw(raw2hex raw2base32 raw2base64)],
13 Constants => [qw(RHASH_CRC32 RHASH_CRC32C RHASH_MD4 RHASH_MD5
14 RHASH_SHA1 RHASH_TIGER RHASH_TTH RHASH_BTIH RHASH_ED2K
15 RHASH_AICH RHASH_WHIRLPOOL RHASH_RIPEMD160 RHASH_GOST94
16 RHASH_GOST94_CRYPTOPRO RHASH_GOST12_256 RHASH_GOST12_512
17 RHASH_SHA224 RHASH_SHA256 RHASH_SHA384 RHASH_SHA512
18 RHASH_SHA3_224 RHASH_SHA3_256 RHASH_SHA3_384 RHASH_SHA3_512
19 RHASH_BLAKE2S RHASH_BLAKE2B RHASH_EDONR256 RHASH_EDONR512
20 RHASH_HAS160 RHASH_SNEFRU128 RHASH_SNEFRU256 RHASH_ALL)]
21 );
22  
23 3 Exporter::export_tags( );
24 3 Exporter::export_ok_tags( qw(Functions Constants) );
25  
26 3 our $VERSION = '1.05';
27  
28 3 require XSLoader;
29 3 XSLoader::load('Crypt::Rhash', $VERSION);
30  
31 ##############################################################################
32 # identifiers of hash functions
33 3 ▶use constant RHASH_CRC32 => 0x01;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use constant RHASH_MD4 => 0x02;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use constant RHASH_MD5 => 0x04;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use constant RHASH_SHA1 => 0x08;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use constant RHASH_TIGER => 0x10;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 ▶use constant RHASH_TTH => 0x20;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use constant RHASH_BTIH => 0x40;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use constant RHASH_ED2K => 0x80;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use constant RHASH_AICH => 0x100;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶use constant RHASH_WHIRLPOOL => 0x200;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 ▶use constant RHASH_RIPEMD160 => 0x400;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 ▶use constant RHASH_GOST94 => 0x800;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 3 ▶use constant RHASH_GOST94_CRYPTOPRO => 0x1000;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 3 ▶use constant RHASH_HAS160 => 0x2000;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 3 ▶use constant RHASH_GOST12_256 => 0x4000;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 3 ▶use constant RHASH_GOST12_512 => 0x8000;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 3 ▶use constant RHASH_SHA224 => 0x10000;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 3 ▶use constant RHASH_SHA256 => 0x20000;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 3 ▶use constant RHASH_SHA384 => 0x40000;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 3 ▶use constant RHASH_SHA512 => 0x80000;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 3 ▶use constant RHASH_EDONR256 => 0x100000;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 3 ▶use constant RHASH_EDONR512 => 0x200000;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 3 ▶use constant RHASH_SHA3_224 => 0x0400000;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 3 ▶use constant RHASH_SHA3_256 => 0x0800000;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 3 ▶use constant RHASH_SHA3_384 => 0x1000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 3 ▶use constant RHASH_SHA3_512 => 0x2000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 3 ▶use constant RHASH_CRC32C => 0x4000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 3 ▶use constant RHASH_SNEFRU128 => 0x8000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 3 ▶use constant RHASH_SNEFRU256 => 0x10000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 3 ▶use constant RHASH_BLAKE2S => 0x20000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 3 ▶use constant RHASH_BLAKE2B => 0x40000000;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 3 ▶use constant RHASH_ALL => 0x7FFFFFFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 ##############################################################################
67 # Rhash class methods
68  
69 # Rhash object constructor
70 sub new($$@)
71 5 ▶{
Subroutine new: called CC=10 SCAR=24.8
Pod undocumented
72 5 my ($class, @hash_ids) = @_;
73  
74 # validate @hash_ids to be an array of hash identifiers
75 5 ▶ scalar(@_) > 0 or die "hash_id not specified\n";
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ > 0 or die "hash_id not specified\n" 5 0 5
76 5 for my $id (@hash_ids) {
77 7 ▶ if (ref($id) || !scalar($id) || (scalar($id) & RHASH_ALL) != $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $id or not scalar $id or (scalar $id & 2147483647) != $id) 0 7 7
Condition33%2/6
ref $id or not scalar $id or (scalar $id & 2147483647) != $id
l!l&&r!l&&!r
007
ref $id or not scalar $id
l!l&&r!l&&!r
007
MC/DC0%0/3
ref $id or not scalar $id or (scalar $id & 2147483647) != $id
ref $id not scalar $id (scalar $id & 2147483647) != $id
Truth table25%1/4
A or not(B) or C
A= ref $idB= not scalar $idC= (scalar $id & 2147483647) != $id
ABCresult
1XX1
01X1
0011
0000
78 0 die "bad hash_id = " . scalar($id) . " " . ref($id) . "\n";
79 }
80 }
81 # handle legacy initialization by a single bitmask
82 5 ▶ if (scalar(@hash_ids) == 1 && ($hash_ids[0] & ($hash_ids[0] - 1)) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @hash_ids == 1 and ($hash_ids[0] & $hash_ids[0] - 1) != 0) 2 3 5
Condition100%3/3
scalar @hash_ids == 1 and ($hash_ids[0] & $hash_ids[0] - 1) != 0
!ll&&!rl&&r
212
MC/DC100%2/2
scalar @hash_ids == 1 and ($hash_ids[0] & $hash_ids[0] - 1) != 0
scalar @hash_ids == 1 ($hash_ids[0] & $hash_ids[0] - 1) != 0
Truth table100%3/3
A and B
A= scalar @hash_ids == 1B= ($hash_ids[0] & $hash_ids[0] - 1) != 0
ABresult
0X0
100
111
83 # Split the bit mask into the array of single bits
84 2 ▶ for (my $mask = shift(@hash_ids); $mask; $mask = $mask & ($mask - 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mask) 62 2 64
85 62 push(@hash_ids, $mask & -$mask);
86 }
87 }
88 5 ▶ my $context = rhash_init_multi_wrapper(\@hash_ids) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $context = &rhash_init_multi_wrapper(\@hash_ids) or return undef 5 0 5
89 5 my $self = {
90 context => $context,
91 };
92 5 return bless $self, $class;
93 }
94  
95 # destructor
96 sub DESTROY($)
97 3 ▶{
Subroutine DESTROY: called CC=2 SCAR=7.2
98 3 my $self = shift;
99 # the 'if' added as workaround for perl 'global destruction' bug
100 # ($self->{context} can disappear on global destruction)
101 3 ▶ rhash_free($self->{context}) if $self->{context};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"context"} 3 0 3
102 }
103  
104 sub update($$)
105 4 ▶{
Subroutine update: called CC=1 SCAR=0.0
Pod documented
106 4 my $self = shift;
107 4 my $message = shift;
108 4 rhash_update($self->{context}, $message);
109 4 return $self;
110 }
111  
112 sub update_fd($$;$$)
113 3 ▶{
Subroutine update_fd: called CC=9 SCAR=22.1
Pod documented
114 3 my ($self, $fd, $start, $size) = @_;
115 3 my $res = 0;
116 3 my $num = 0;
117  
118 3 binmode($fd);
119 3 ▶ if(defined($start)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start) 1 2 3
120 1 ▶ seek($fd, scalar($start), 0) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
seek $fd, scalar $start, 0 or return undef 1 0 1
121 }
122  
123 3 my $data;
124 3 ▶ if(defined($size)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size) { } 1 2 3
125 1 ▶ for(my $left = scalar($size); $left > 0; $left -= 8192) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left > 0) 1 1 2
126 1 ▶ ($res = read($fd, $data,
Branch: 2 branches
BranchTrueFalseTotal
$res = read($fd, $data, $left < 8192 ? $left : 8192) or last 1 0 1
$left < 8192 ? : 1 0 1
127 ($left < 8192 ? $left : 8192))) || last;
128 1 rhash_update($self->{context}, $data);
129 1 $num += $res;
130 }
131 } else {
132 2 ▶ while( ($res = read($fd, $data, 8192)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res = read($fd, $data, 8192)) 2 2 4
133 2 rhash_update($self->{context}, $data);
134 2 $num += $res;
135 }
136 }
137  
138 3 ▶ return (defined($res) ? $num : undef); # return undef on read error
Branch: 1 branch
BranchTrueFalseTotal
defined $res ? : 3 0 3
139 }
140  
141 sub update_file($$;$$)
142 2 ▶{
Subroutine update_file: called CC=2 SCAR=7.0
Pod documented
143 2 my ($self, $file, $start, $size) = @_;
144 2 ▶ open(my $fd, "<", $file) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fd, "<", $file or return undef 2 0 2
145 2 my $res = $self->update_fd($fd, $start, $size);
146 2 close($fd);
147 2 return $res;
148 }
149  
150 sub final($)
151 3 ▶{
Subroutine final: called CC=1 SCAR=0.0
Pod documented
152 3 my $self = shift;
153 3 rhash_final($self->{context});
154 3 return $self;
155 }
156  
157 sub reset($)
158 3 ▶{
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
159 3 my $self = shift;
160 3 rhash_reset($self->{context});
161 3 return $self;
162 }
163  
164 sub hashed_length($)
165 2 ▶{
Subroutine hashed_length: called CC=1 SCAR=0.0
Pod documented
166 2 my $self = shift;
167 2 return rhash_get_hashed_length($self->{context});
168 }
169  
170 sub hash_id($)
171 1 ▶{
Subroutine hash_id: called CC=1 SCAR=0.0
Pod documented
172 1 my $self = shift;
173 1 return rhash_get_hash_id($self->{context});
174 }
175  
176 ##############################################################################
177 # Message digest formatting functions
178  
179 # printing constants
180 3 ▶use constant RHPR_DEFAULT => 0x0;
Subroutine BEGIN: called CC=1 SCAR=0.0
181 3 ▶use constant RHPR_RAW => 0x1;
Subroutine BEGIN: called CC=1 SCAR=0.0
182 3 ▶use constant RHPR_HEX => 0x2;
Subroutine BEGIN: called CC=1 SCAR=0.0
183 3 ▶use constant RHPR_BASE32 => 0x3;
Subroutine BEGIN: called CC=1 SCAR=0.0
184 3 ▶use constant RHPR_BASE64 => 0x4;
Subroutine BEGIN: called CC=1 SCAR=0.0
185 3 ▶use constant RHPR_UPPERCASE => 0x8;
Subroutine BEGIN: called CC=1 SCAR=0.0
186 3 ▶use constant RHPR_REVERSE => 0x10;
Subroutine BEGIN: called CC=1 SCAR=0.0
187  
188 sub hash($;$$)
189 43 ▶{
Subroutine hash: called CC=3 SCAR=11.0
Pod documented
190 43 my $self = shift;
191 43 ▶ my $hash_id = scalar(shift) || 0;
Condition100%2/2
scalar shift() || 0
l!l
385
MC/DC100%1/1
scalar shift() || 0
scalar shift()
Truth table100%2/2
A
A= scalar shift()
Aresult
11
00
192 43 ▶ my $print_flags = scalar(shift) || RHPR_DEFAULT;
Condition100%2/2
scalar shift() || 0
l!l
637
MC/DC100%1/1
scalar shift() || 0
scalar shift()
Truth table100%2/2
A
A= scalar shift()
Aresult
11
00
193 43 return rhash_print_wrapper($self->{context}, $hash_id, $print_flags);
194 }
195  
196 sub hash_base32($;$)
197 1 ▶{
Subroutine hash_base32: called CC=1 SCAR=0.0
Pod documented
198 1 hash($_[0], $_[1], RHPR_BASE32);
199 }
200  
201 sub hash_base64($;$)
202 1 ▶{
Subroutine hash_base64: called CC=1 SCAR=0.0
Pod documented
203 1 hash($_[0], $_[1], RHPR_BASE64);
204 }
205  
206 sub hash_hex($;$)
207 2 ▶{
Subroutine hash_hex: called CC=1 SCAR=0.0
Pod documented
208 2 hash($_[0], $_[1], RHPR_HEX);
209 }
210  
211 sub hash_rhex($;$)
212 1 ▶{
Subroutine hash_rhex: called CC=1 SCAR=0.0
Pod documented
213 1 hash($_[0], $_[1], RHPR_HEX | RHPR_REVERSE);
214 }
215  
216 sub hash_raw($;$)
217 1 ▶{
Subroutine hash_raw: called CC=1 SCAR=0.0
Pod undocumented
218 1 hash($_[0], $_[1], RHPR_RAW);
219 }
220  
221 sub magnet_link($;$@)
222 4 ▶{
Subroutine magnet_link: called CC=6 SCAR=19.6
Pod documented
223 4 my ($self, $filename, @hash_ids) = @_;
224 4 my $hash_mask = 0;
225 4 ▶ if (scalar(@hash_ids)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @hash_ids) { } 2 2 4
226 2 for my $id (@hash_ids) {
227 5 ▶ if (ref($id) || !scalar($id) || (scalar($id) & RHASH_ALL) != $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $id or not scalar $id or (scalar $id & 2147483647) != $id) 0 5 5
Condition33%2/6
ref $id or not scalar $id or (scalar $id & 2147483647) != $id
l!l&&r!l&&!r
005
ref $id or not scalar $id
l!l&&r!l&&!r
005
MC/DC0%0/3
ref $id or not scalar $id or (scalar $id & 2147483647) != $id
ref $id not scalar $id (scalar $id & 2147483647) != $id
Truth table25%1/4
A or not(B) or C
A= ref $idB= not scalar $idC= (scalar $id & 2147483647) != $id
ABCresult
1XX1
01X1
0011
0000
228 0 die "bad hash_id = " . scalar($id) . " " . ref($id) . "\n";
229 }
230 5 $hash_mask = $hash_mask | scalar($id);
231 }
232 } else {
233 2 $hash_mask = undef;
234 }
235 4 return rhash_print_magnet_wrapper($self->{context}, $filename, $hash_mask);
236 }
237  
238 3 our $AUTOLOAD;
239  
240 # report error if a script called unexisting method/field
241 sub AUTOLOAD
242 0 ▶{
Subroutine AUTOLOAD: not called CC=7 SCAR=40.3
243 0 my ($self, $field, $type, $pkg) = ($_[0], $AUTOLOAD, undef, __PACKAGE__);
244 0 $field =~ s/.*://;
245 0 ▶ die "function $field does not exist\n" if $field =~ /^(rhash_|raw2)/;
Branch: 1 branch
BranchTrueFalseTotal
if $field =~ /^(rhash_|raw2)/ 0 0 0
246 0 ▶ die "no arguments specified to $field()\n" if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
247 0 ▶ die "the $field() argument is undefined\n" if !defined $self;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 0 0
248  
249 0 ▶ ($type = ref($self)) && $type eq $pkg || die "the $field() argument is not a $pkg reference\n";
Branch: 1 branch
BranchTrueFalseTotal
$type = ref $self and $type eq $pkg or die "the $field() argument is not a $pkg reference\n" 0 0 0
Condition0%0/3
$type = ref $self and $type eq $pkg
!ll&&!rl&&r
000
MC/DC0%0/2
$type = ref $self and $type eq $pkg
$type = ref $self $type eq $pkg
Truth table0%0/3
A and B
A= $type = ref $selfB= $type eq $pkg
ABresult
0X0
100
111
250 0 ▶ my $text = (exists $self->{$field} ? "is not accessible" : "does not exist");
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$field} ? : 0 0 0
251 0 die "the method $field() $text in the class $pkg\n";
252 }
253  
254 # static functions
255  
256 sub msg($$)
257 1 ▶{
Subroutine msg: called CC=2 SCAR=7.1
Pod documented
258 1 my ($hash_id, $msg) = @_;
259 1 my $raw = rhash_msg_wrapper($hash_id, $msg); # get a binary message digest
260 1 ▶ return (is_base32($hash_id) ? raw2base32($raw) : raw2hex($raw));
Branch: 1 branch
BranchTrueFalseTotal
&is_base32($hash_id) ? : 0 1 1
261 }
262  
263 3 1;
264 __END__
↑ Summary _rhash_torrent.c »