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 SCAR 35.3
CC
34
Cov
97%
bdecode
28.1
bencode
7.0
BEGIN
1.0
CRAP 34.0
« lib/Net/BitTorrent/Torrent/Generator.pm ↑ Summary lib/Net/BitTorrent/Tracker/WebSeed.pm »
1 47 package Net::BitTorrent::Protocol::BEP03::Bencode v2.1.0 {
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 9373 ▶ sub bencode ( $ref //= return ) {
Subroutine bencode: called CC=7 SCAR=19.5
Pod documented
10 9372 ▶ return ( ( ( length $ref ) && $ref =~ m[^([-\+][1-9])?\d*$] ) ? ( 'i' . $ref . 'e' ) : ( length($ref) . ':' . $ref ) ) if !ref $ref;
Branch: 2 branches
BranchTrueFalseTotal
unless ref $ref 6473 2899 9372
length $ref && $ref =~ /^([-\+][1-9])?\d*$/u ? : 550 5923 6473
Condition100%3/3
length $ref and $ref =~ /^([-\+][1-9])?\d*$/u
!ll&&!rl&&r
35920550
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 2899 ▶ return join( '', 'l', ( map { bencode($_) } @{$ref} ), 'e' ) if ref $ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $ref eq "ARRAY" 36 2863 2899
12 2863 ▶ 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" 2861 2 2863
13 2 return '';
14 }
15  
16 7226 ▶ 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 7226 ▶ die 'bencode nesting depth limit exceeded (max ' . MAX_BDECODE_DEPTH . ' levels)' if $depth > MAX_BDECODE_DEPTH;
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 100 2 7224 7226
19 7224 my $return;
20 7224 ▶ if ( $string =~ s[^(0+|[1-9]\d*):][] ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($string =~ s/^(0+|[1-9]\d*)://u) { } 5238 1986 7224
elsif ($string =~ s/^i([-\+]?\d+)e//u) { } 410 1576 1986
elsif ($string =~ s/^l//su) { } 317 1259 1576
elsif ($string =~ s/^d//su) { } 1246 13 1259
21 5238 my $size = $1;
22 5238 ▶ die "bencode string too large ($size bytes, max " . MAX_STRING_SIZE . ')' if $size > MAX_STRING_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $size > 67108864 5 5233 5238
23 5233 ▶ $return = '' if $size =~ m[^0+$];
Branch: 1 branch
BranchTrueFalseTotal
if $size =~ /^0+$/u 63 5170 5233
24 5233 $return .= substr( $string, 0, $size, '' );
25 5233 ▶ return if length $return < $size;
Branch: 1 branch
BranchTrueFalseTotal
if length $return < $size 1 5232 5233
26 5232 ▶ return $k ? ( $return, $string ) : $return; # byte string
Branch: 1 branch
BranchTrueFalseTotal
$k ? : 5224 8 5232
27 }
28 elsif ( $string =~ s[^i([-\+]?\d+)e][] ) { # integer
29 410 my $int = $1;
30 410 ▶ $int = () if $int =~ m[^-0] || $int =~ m[^0\d+];
Branch: 1 branch
BranchTrueFalseTotal
if $int =~ /^-0/u or $int =~ /^0\d+/u 2 408 410
Condition100%3/3
$int =~ /^-0/u or $int =~ /^0\d+/u
l!l&&r!l&&!r
11408
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 410 ▶ return $k ? ( $int, $string ) : $int;
Branch: 1 branch
BranchTrueFalseTotal
$k ? : 402 8 410
32 }
33 elsif ( $string =~ s[^l][]s ) { # list without greedy capture
34 317 ▶ while ( $string and $string !~ s[^e][]s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string and not $string =~ s/^e//su) 351 212 563
Condition100%3/3
$string and not $string =~ s/^e//su
!ll&&!rl&&r
1211351
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 351 ( my ($piece), $string ) = bdecode( $string, 1, $depth + 1 );
36 246 push @$return, $piece;
37 }
38 212 ▶ return $k ? ( \@$return, $string ) : \@$return;
Branch: 1 branch
BranchTrueFalseTotal
$k ? : 203 9 212
39 }
40 elsif ( $string =~ s[^d][]s ) { # dictionary without greedy capture
41 1246 my $pkey;
42 1246 ▶ while ( $string and $string !~ s[^e][]s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string and not $string =~ s/^e//su) 3185 1138 4323
Condition100%3/3
$string and not $string =~ s/^e//su
!ll&&!rl&&r
611323185
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 3185 my ( $key, $value );
44 3185 ( $key, $string ) = bdecode( $string, 1, $depth + 1 );
45 3183 ▶ ( $value, $string ) = bdecode( $string, 1, $depth + 1 ) if $string;
Branch: 1 branch
BranchTrueFalseTotal
if $string 3183 0 3183
46 3080 ▶ 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 3077 3080
Condition83%5/6
defined $pkey and defined $key and $pkey gt $key
!ll&&!rl&&r
113419433
defined $pkey and defined $key
!ll&&!rl&&r
113401946
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 3077 ▶ $return->{$key} = $value if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 3077 0 3077
48 3077 ▶ $pkey = $key if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 3077 0 3077
49 }
50 1138 ▶ return $k ? ( \%$return, $string ) : \%$return;
Branch: 1 branch
BranchTrueFalseTotal
$k ? : 708 430 1138
51 }
52 13 return;
53 }
54 };
55 47 1;
« lib/Net/BitTorrent/Torrent/Generator.pm ↑ Summary lib/Net/BitTorrent/Tracker/WebSeed.pm »