| 1 | 2 | ▶ | use v5.40; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use feature 'class'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | no warnings 'experimental::class'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | 2 | class Archive::CAR::CID v0.0.4 { | |||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Archive::CAR::Utils qw[systell]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | 2 | field $version : param : reader; | |||||||||||||||||||||||||||
| 9 | 2 | field $codec : param : reader; | |||||||||||||||||||||||||||
| 10 | 2 | field $hash : param : reader; | |||||||||||||||||||||||||||
| 11 | 2 | field $digest : param : reader; | |||||||||||||||||||||||||||
| 12 | 2 | field $raw : param : reader; | |||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||
| 14 | 7 | ▶ | method to_string() { | ||||||||||||||||||||||||||
|
Subroutine
to_string: called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Minimal string conversion for debugging/display | ||||||||||||||||||||||||||||
| 17 | # In a real IPFS context, this uses Multibase (base32 or base58) | ||||||||||||||||||||||||||||
| 18 | 7 | ▶ | return 'b' . $self->_encode_base32($raw) if $version == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 0 | return unpack( 'H*', $raw ); | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 7 | ▶ | method _encode_base32 ($data) { | ||||||||||||||||||||||||||
|
Subroutine
_encode_base32: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 23 | 7 | my @alphabet = split //, 'abcdefghijklmnopqrstuvwxyz234567'; | |||||||||||||||||||||||||||
| 24 | 7 | my $bits = ''; | |||||||||||||||||||||||||||
| 25 | 7 | for my $byte ( unpack 'C*', $data ) { | |||||||||||||||||||||||||||
| 26 | 252 | $bits .= sprintf '%08b', $byte; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | 7 | my $out = ''; | |||||||||||||||||||||||||||
| 29 | 7 | ▶ | while ( $bits =~ s/^([01]{5})// ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 399 | $out .= $alphabet[ oct "0b$1" ]; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 7 | ▶ | if ( length $bits ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 7 | $bits .= '0' x ( 5 - length $bits ); | |||||||||||||||||||||||||||
| 34 | 7 | $out .= $alphabet[ oct "0b$bits" ]; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 7 | return $out; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 38 | ▶ | sub decode ( $class, $fh ) { | ||||||||||||||||||||||||||
|
Subroutine
decode: called CC=13 SCAR=29.6
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 38 | my $pos_before = systell($fh); | |||||||||||||||||||||||||||
| 41 | 38 | my $first_byte; | |||||||||||||||||||||||||||
| 42 | 38 | my $fb_res = read( $fh, $first_byte, 1 ); | |||||||||||||||||||||||||||
| 43 | 38 | ▶ | return undef unless defined $fb_res && $fb_res == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $fb_res and $fb_res == 1
MC/DC0%0/2
defined $fb_res and $fb_res == 1
defined $fb_res
$fb_res == 1
Truth table33%1/3
A and B
A= defined $fb_resB= $fb_res == 1
| |||||||||||||||||||||||||||||
| 44 | 38 | my $fb = ord($first_byte); | |||||||||||||||||||||||||||
| 45 | 38 | ▶ | if ( $fb == 0x00 ) { # Optional leading zero | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | $pos_before = systell($fh); | |||||||||||||||||||||||||||
| 47 | 0 | $fb_res = read( $fh, $first_byte, 1 ); | |||||||||||||||||||||||||||
| 48 | 0 | ▶ | return undef unless defined $fb_res && $fb_res == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $fb_res and $fb_res == 1
MC/DC0%0/2
defined $fb_res and $fb_res == 1
defined $fb_res
$fb_res == 1
Truth table0%0/3
A and B
A= defined $fb_resB= $fb_res == 1
| |||||||||||||||||||||||||||||
| 49 | 0 | $fb = ord($first_byte); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 38 | ▶ | if ( $fb == 0x12 ) { # Likely CIDv0 in binary form | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 14 | my $second_byte; | |||||||||||||||||||||||||||
| 53 | 14 | read( $fh, $second_byte, 1 ); | |||||||||||||||||||||||||||
| 54 | 14 | ▶ | if ( defined $second_byte && ord($second_byte) == 0x20 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $second_byte and ord $second_byte == 32
MC/DC0%0/2
defined $second_byte and ord $second_byte == 32
defined $second_byte
ord $second_byte == 32
Truth table33%1/3
A and B
A= defined $second_byteB= ord $second_byte == 32
| |||||||||||||||||||||||||||||
| 55 | 14 | my $digest; | |||||||||||||||||||||||||||
| 56 | 14 | read( $fh, $digest, 32 ); | |||||||||||||||||||||||||||
| 57 | 14 | my $raw = chr(0x12) . chr(0x20) . $digest; | |||||||||||||||||||||||||||
| 58 | 14 | return $class->new( version => 0, codec => 0x70, hash => 0x12, digest => $digest, raw => $raw ); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 0 | seek( $fh, $pos_before + 1, 0 ); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 24 | seek( $fh, $pos_before, 0 ); | |||||||||||||||||||||||||||
| 63 | 24 | my ($version) = Archive::CAR::Utils::decode_varint($fh); | |||||||||||||||||||||||||||
| 64 | 24 | ▶ | return undef if !defined $version; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 24 | my ($codec) = Archive::CAR::Utils::decode_varint($fh); | |||||||||||||||||||||||||||
| 66 | 24 | ▶ | return undef unless defined $codec; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 24 | my ($mh_type) = Archive::CAR::Utils::decode_varint($fh); | |||||||||||||||||||||||||||
| 68 | 24 | ▶ | return undef unless defined $mh_type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 24 | my ($mh_len) = Archive::CAR::Utils::decode_varint($fh); | |||||||||||||||||||||||||||
| 70 | 24 | ▶ | return undef unless defined $mh_len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 24 | my $digest; | |||||||||||||||||||||||||||
| 72 | 24 | read( $fh, $digest, $mh_len ); | |||||||||||||||||||||||||||
| 73 | 24 | my $pos_after = systell($fh); | |||||||||||||||||||||||||||
| 74 | 24 | seek( $fh, $pos_before, 0 ); | |||||||||||||||||||||||||||
| 75 | 24 | my $raw; | |||||||||||||||||||||||||||
| 76 | 24 | read( $fh, $raw, $pos_after - $pos_before ); | |||||||||||||||||||||||||||
| 77 | 24 | seek( $fh, $pos_after, 0 ); | |||||||||||||||||||||||||||
| 78 | 24 | return $class->new( version => $version, codec => $codec, hash => $mh_type, digest => $digest, raw => $raw ); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 8 | ▶ | sub from_raw ( $class, $raw ) { | ||||||||||||||||||||||||||
|
Subroutine
from_raw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 8 | open my $fh, '<:raw', \$raw; | |||||||||||||||||||||||||||
| 83 | 8 | return $class->decode($fh); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | }; | ||||||||||||||||||||||||||||
| 86 | # | ||||||||||||||||||||||||||||
| 87 | 2 | 1; | |||||||||||||||||||||||||||