| 1 | 26 | ▶ | use v5.40; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 2 | 26 | ▶ | use feature 'class'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 26 | ▶ | no warnings 'experimental::class'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 26 | ▶ | use Net::BitTorrent::Emitter; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||
| 6 | 26 | class Net::BitTorrent::Protocol::BEP53 v2.1.1 : isa(Net::BitTorrent::Emitter) { | |||||||||||||||||||||||||||||||
| 7 | 26 | ▶ | use URI::Escape qw[uri_unescape uri_escape]; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||
| 9 | 26 | field $infohash_v1 : reader : param = undef; | |||||||||||||||||||||||||||||||
| 10 | 26 | field $infohash_v2 : reader : param = undef; | |||||||||||||||||||||||||||||||
| 11 | 26 | field $trackers : reader : param = []; | |||||||||||||||||||||||||||||||
| 12 | 26 | field $name : reader : param = undef; | |||||||||||||||||||||||||||||||
| 13 | 26 | field $nodes : reader : param = []; # DHT bootstrap nodes (x.pe) | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||||||
| 16 | 15 | ▶ | sub parse ( $class, $uri ) { | ||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=13 SCAR=27.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 17 | 15 | ▶ | die 'Not a magnet URI' unless $uri =~ /^magnet:\?/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 18 | 15 | my %params; | |||||||||||||||||||||||||||||||
| 19 | 15 | my $query = substr( $uri, 8 ); | |||||||||||||||||||||||||||||||
| 20 | 15 | for my $pair ( split( /[&;]/, $query ) ) { | |||||||||||||||||||||||||||||||
| 21 | 21 | my ( $key, $val ) = split( /=/, $pair, 2 ); | |||||||||||||||||||||||||||||||
| 22 | 21 | ▶ | next unless defined $key && defined $val; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $key and defined $val
MC/DC0%0/2
defined $key and defined $val
defined $key
defined $val
Truth table33%1/3
A and B
A= defined $keyB= defined $val
| |||||||||||||||||||||||||||||||||
| 23 | 21 | $val = uri_unescape($val); | |||||||||||||||||||||||||||||||
| 24 | 21 | push @{ $params{$key} }, $val; | |||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||
| 26 | 15 | my ( $v1, $v2 ); | |||||||||||||||||||||||||||||||
| 27 | 15 | ▶ | for my $xt ( @{ $params{xt} // [] } ) { | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$params{"xt"} // []
MC/DC0%0/1
$params{"xt"} // []
$params{"xt"}
Truth table50%1/2
A
A= $params{"xt"}
| |||||||||||||||||||||||||||||||||
| 28 | 17 | ▶ | if ( $xt =~ /^urn:btih:([a-fA-F0-9]{40})$/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 29 | 14 | $v1 = pack( 'H*', $1 ); | |||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | elsif ( $xt =~ /^urn:btih:([a-zA-Z2-7]{32})$/ ) { | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | # Base32 encoded (v1) | ||||||||||||||||||||||||||||||||
| 34 | 0 | $v1 = _decode_base32($1); | |||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||
| 36 | elsif ( $xt =~ /^urn:btmh:1220([a-fA-F0-9]{64})$/ ) { | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | # BEP 53 v2 (multihash SHA-256) | ||||||||||||||||||||||||||||||||
| 39 | 3 | $v2 = pack( 'H*', $1 ); | |||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||
| 42 | return $class->new( | ||||||||||||||||||||||||||||||||
| 43 | infohash_v1 => $v1, | ||||||||||||||||||||||||||||||||
| 44 | infohash_v2 => $v2, | ||||||||||||||||||||||||||||||||
| 45 | trackers => ( $params{tr} // [] ), | ||||||||||||||||||||||||||||||||
| 46 | name => ( $params{dn}[0] // undef ), | ||||||||||||||||||||||||||||||||
| 47 | 15 | ▶ | nodes => ( $params{'x.pe'} // [] ), | ||||||||||||||||||||||||||||||
|
Condition83%5/6
$params{"tr"} // []
$params{"dn"}[0] // undef
$params{"x.pe"} // []
MC/DC0%0/1
$params{"tr"} // []
$params{"tr"}
MC/DC100%1/1
$params{"dn"}[0] // undef
$params{"dn"}[0]
MC/DC100%1/1
$params{"x.pe"} // []
$params{"x.pe"}
Truth table50%1/2
A
A= $params{"tr"}
Truth table100%2/2
A
A= $params{"dn"}[0]
Truth table100%2/2
A
A= $params{"x.pe"}
| |||||||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 3 | ▶ | sub _decode_base32 ($str) { | ||||||||||||||||||||||||||||||
|
Subroutine
_decode_base32: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 52 | 3 | $str = uc($str); | |||||||||||||||||||||||||||||||
| 53 | 3 | my $alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; | |||||||||||||||||||||||||||||||
| 54 | 3 | my %map; | |||||||||||||||||||||||||||||||
| 55 | 3 | @map{ split //, $alphabet } = 0 .. 31; | |||||||||||||||||||||||||||||||
| 56 | 3 | my $buffer = 0; | |||||||||||||||||||||||||||||||
| 57 | 3 | my $bits = 0; | |||||||||||||||||||||||||||||||
| 58 | 3 | my $res = ''; | |||||||||||||||||||||||||||||||
| 59 | 3 | for my $char ( split //, $str ) { | |||||||||||||||||||||||||||||||
| 60 | 56 | ▶ | next unless exists $map{$char}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | 56 | $buffer = ( $buffer << 5 ) | $map{$char}; | |||||||||||||||||||||||||||||||
| 62 | 56 | $bits += 5; | |||||||||||||||||||||||||||||||
| 63 | 56 | ▶ | if ( $bits >= 8 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 64 | 35 | $bits -= 8; | |||||||||||||||||||||||||||||||
| 65 | 35 | $res .= chr( ( $buffer >> $bits ) & 0xFF ); | |||||||||||||||||||||||||||||||
| 66 | 35 | $buffer &= ( 1 << $bits ) - 1; | |||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | 3 | return $res; | |||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 2 | ▶ | method to_string () { | ||||||||||||||||||||||||||||||
|
Subroutine
to_string: called CC=6 SCAR=18.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 73 | 2 | my @pairs; | |||||||||||||||||||||||||||||||
| 74 | 2 | ▶ | push @pairs, 'xt=urn:btih:' . unpack( 'H*', $infohash_v1 ) if $infohash_v1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | 2 | ▶ | push @pairs, 'xt=urn:btmh:1220' . unpack( 'H*', $infohash_v2 ) if $infohash_v2; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 76 | 2 | ▶ | push @pairs, 'dn=' . uri_escape($name) if defined $name; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 77 | 2 | push @pairs, 'tr=' . uri_escape($_) for @$trackers; | |||||||||||||||||||||||||||||||
| 78 | 2 | push @pairs, 'x.pe=' . uri_escape($_) for @$nodes; | |||||||||||||||||||||||||||||||
| 79 | 2 | 'magnet:?' . join( '&', @pairs ); | |||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | }; | ||||||||||||||||||||||||||||||||
| 82 | # | ||||||||||||||||||||||||||||||||
| 83 | 26 | 1; | |||||||||||||||||||||||||||||||