lib/Net/BitTorrent/Protocol/BEP03/Bencode.pm

filter: Statementstmt 100% 56 / 56 Branchbran 93.4% 43 / 46 Conditioncond 94.4% 17 / 18 MC/DCmcdc 90.9% 10 / 11 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 96.4% 135 / 140 CC 34 SCAR 30.4
CC
34
Cov
97%
bdecode
28.1
bencode
7.0
BEGIN
1.0
CRAP 21.0
« lib/Net/BitTorrent/Torrent.pm ↑ Summary lib/Net/BitTorrent/Tracker/WebSeed.pm »
1 47 package Net::BitTorrent::Protocol::BEP03::Bencode v2.1.1 {
2 47 ▶ use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 47 ▶ use Exporter qw[import];
Subroutine BEGIN: called CC=1 SCAR=0.0
4 47 our %EXPORT_TAGS = ( all => [ our @EXPORT_OK = qw[bencode bdecode] ], bencode => [] );
5 #
6 47 ▶ use constant MAX_BDECODE_DEPTH => 100;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 47 ▶ use constant MAX_STRING_SIZE => 64 * 1024 * 1024; # 64MB
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9637 ▶ sub bencode ( $ref //= return ) {
Subroutine bencode: called CC=7 SCAR=19.5
Pod documented
10 9636 ▶ return ( ( ( length $ref ) && $ref =~ m[^([-\+][1-9])?\d*$] ) ? ( 'i' . $ref . 'e' ) : ( length($ref) . ':' . $ref ) ) if !ref $ref;
Branch: 2 branches
BranchTrueFalseTotal
unless ref $ref 6657 2979 9636
length $ref && $ref =~ /^([-\+][1-9])?\d*$/u ? : 550 6107 6657
Condition100%3/3
length $ref and $ref =~ /^([-\+][1-9])?\d*$/u
!ll&&!rl&&r
36104550
MC/DC100%2/2
length $ref and $ref =~ /^([-\+][1-9])?\d*$/u
length $ref $ref =~ /^([-\+][1-9])?\d*$/u
Truth table100%3/3
A and B
A= length $refB= $ref =~ /^([-\+][1-9])?\d*$/u
ABresult
0X0
100
111
11 2979 ▶ return join( '', 'l', ( map { bencode($_) } @{$ref} ), 'e' ) if ref $ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $ref eq "ARRAY" 36 2943 2979
12 2943 ▶ return join( '', 'd', ( map { length($_) . ':' . $_ . bencode( $ref->{$_} ) } sort { $a cmp $b } keys %{$ref} ), 'e' ) if ref $ref eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $ref eq "HASH" 2941 2 2943
13 2 return '';
14 }
15  
16 7277 ▶ sub bdecode( $string //= return, $k //= 0, $depth //= 0 ) {
Subroutine bdecode: called CC=28 SCAR=33.4
Pod documented
17 47 ▶ no warnings 'recursion'; # Let me deal with it.
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7277 ▶ die 'bencode nesting depth limit exceeded (max ' . MAX_BDECODE_DEPTH . ' levels)' if $depth > MAX_BDECODE_DEPTH;
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 100 2 7275 7277
19 7275 my $return;
20 7275 ▶ if ( $string =~ s[^(0+|[1-9]\d*):][] ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($string =~ s/^(0+|[1-9]\d*)://u) { } 5284 1991 7275
elsif ($string =~ s/^i([-\+]?\d+)e//u) { } 416 1575 1991
elsif ($string =~ s/^l//su) { } 312 1263 1575
elsif ($string =~ s/^d//su) { } 1250 13 1263
21 5284 my $size = $1;
22 5284 ▶ die "bencode string too large ($size bytes, max " . MAX_STRING_SIZE . ')' if $size > MAX_STRING_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $size > 67108864 5 5279 5284
23 5279 ▶ $return = '' if $size =~ m[^0+$];
Branch: 1 branch
BranchTrueFalseTotal
if $size =~ /^0+$/u 63 5216 5279
24 5279 $return .= substr( $string, 0, $size, '' );
25 5279 ▶ return if length $return < $size;
Branch: 1 branch
BranchTrueFalseTotal
if length $return < $size 1 5278 5279
26 5278 ▶ return $k ? ( $return, $string ) : $return; # byte string
Branch: 1 branch
BranchTrueFalseTotal
$k ? : 5270 8 5278
27 }
28 elsif ( $string =~ s[^i([-\+]?\d+)e][] ) { # integer
29 416 my $int = $1;
30 416 ▶ $int = () if $int =~ m[^-0] || $int =~ m[^0\d+];
Branch: 1 branch
BranchTrueFalseTotal
if $int =~ /^-0/u or $int =~ /^0\d+/u 2 414 416
Condition100%3/3
$int =~ /^-0/u or $int =~ /^0\d+/u
l!l&&r!l&&!r
11414
MC/DC100%2/2
$int =~ /^-0/u or $int =~ /^0\d+/u
$int =~ /^-0/u $int =~ /^0\d+/u
Truth table100%3/3
A or B
A= $int =~ /^-0/uB= $int =~ /^0\d+/u
ABresult
1X1
011
000
31 416 ▶ return $k ? ( $int, $string ) : $int;
Branch: 1 branch
BranchTrueFalseTotal
$k ? : 408 8 416
32 }
33 elsif ( $string =~ s[^l][]s ) { # list without greedy capture
34 312 ▶ while ( $string and $string !~ s[^e][]s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string and not $string =~ s/^e//su) 346 207 553
Condition100%3/3
$string and not $string =~ s/^e//su
!ll&&!rl&&r
1206346
MC/DC100%2/2
$string and not $string =~ s/^e//su
$string not $string =~ s/^e//su
Truth table100%3/3
A and not(B)
A= $stringB= not $string =~ s/^e//su
ABresult
0X0
100
111
35 346 ( my ($piece), $string ) = bdecode( $string, 1, $depth + 1 );
36 241 push @$return, $piece;
37 }
38 207 ▶ return $k ? ( \@$return, $string ) : \@$return;
Branch: 1 branch
BranchTrueFalseTotal
$k ? : 198 9 207
39 }
40 elsif ( $string =~ s[^d][]s ) { # dictionary without greedy capture
41 1250 my $pkey;
42 1250 ▶ while ( $string and $string !~ s[^e][]s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string and not $string =~ s/^e//su) 3212 1142 4354
Condition100%3/3
$string and not $string =~ s/^e//su
!ll&&!rl&&r
611363212
MC/DC100%2/2
$string and not $string =~ s/^e//su
$string not $string =~ s/^e//su
Truth table100%3/3
A and not(B)
A= $stringB= not $string =~ s/^e//su
ABresult
0X0
100
111
43 3212 my ( $key, $value );
44 3212 ( $key, $string ) = bdecode( $string, 1, $depth + 1 );
45 3210 ▶ ( $value, $string ) = bdecode( $string, 1, $depth + 1 ) if $string;
Branch: 1 branch
BranchTrueFalseTotal
if $string 3210 0 3210
46 3107 ▶ die 'malformed dictionary' if defined $pkey && defined $key && $pkey gt $key; # BEP52
Branch: 1 branch
BranchTrueFalseTotal
if defined $pkey and defined $key and $pkey gt $key 3 3104 3107
Condition83%5/6
defined $pkey and defined $key and $pkey gt $key
!ll&&!rl&&r
113819663
defined $pkey and defined $key
!ll&&!rl&&r
113801969
MC/DC66%2/3
defined $pkey and defined $key and $pkey gt $key
defined $pkey defined $key $pkey gt $key
Truth table75%3/4
A and B and C
A= defined $pkeyB= defined $keyC= $pkey gt $key
ABCresult
0XX0
10X0
1100
1111
47 3104 ▶ $return->{$key} = $value if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 3104 0 3104
48 3104 ▶ $pkey = $key if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 3104 0 3104
49 }
50 1142 ▶ return $k ? ( \%$return, $string ) : \%$return;
Branch: 1 branch
BranchTrueFalseTotal
$k ? : 710 432 1142
51 }
52 13 return;
53 }
54 };
55 47 1;
« lib/Net/BitTorrent/Torrent.pm ↑ Summary lib/Net/BitTorrent/Tracker/WebSeed.pm »