| 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
Condition100%3/3
length $ref and $ref =~ /^([-\+][1-9])?\d*$/u
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
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2979 | ▶ | return join( '', 'l', ( map { bencode($_) } @{$ref} ), 'e' ) if ref $ref eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2943 | ▶ | return join( '', 'd', ( map { length($_) . ':' . $_ . bencode( $ref->{$_} ) } sort { $a cmp $b } keys %{$ref} ), 'e' ) if ref $ref eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 7275 | my $return; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 7275 | ▶ | if ( $string =~ s[^(0+|[1-9]\d*):][] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 5279 | ▶ | $return = '' if $size =~ m[^0+$]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 5279 | $return .= substr( $string, 0, $size, '' ); | |||||||||||||||||||||||||||||||||||||||||
| 25 | 5279 | ▶ | return if length $return < $size; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 5278 | ▶ | return $k ? ( $return, $string ) : $return; # byte string | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$int =~ /^-0/u or $int =~ /^0\d+/u
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
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 416 | ▶ | return $k ? ( $int, $string ) : $int; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | elsif ( $string =~ s[^l][]s ) { # list without greedy capture | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 312 | ▶ | while ( $string and $string !~ s[^e][]s ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$string and not $string =~ s/^e//su
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$string and not $string =~ s/^e//su
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 3107 | ▶ | die 'malformed dictionary' if defined $pkey && defined $key && $pkey gt $key; # BEP52 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $pkey and defined $key and $pkey gt $key
defined $pkey and defined $key
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
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 3104 | ▶ | $return->{$key} = $value if defined $key; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 3104 | ▶ | $pkey = $key if defined $key; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 1142 | ▶ | return $k ? ( \%$return, $string ) : \%$return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 13 | return; | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 47 | 1; | |||||||||||||||||||||||||||||||||||||||||