| 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::v1 v0.0.4 { | |||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Archive::CAR::Utils qw[systell]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Codec::CBOR; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Archive::CAR::CID; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | 2 | field $header : reader; | |||||||||||||||||||||||||||
| 11 | 2 | field $roots : reader; | |||||||||||||||||||||||||||
| 12 | 2 | field $blocks : reader; | |||||||||||||||||||||||||||
| 13 | 2 | field $codec; | |||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||
| 15 | 10 | ▶ | ADJUST { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 10 | $codec = Codec::CBOR->new(); | |||||||||||||||||||||||||||
| 17 | $codec->add_tag_handler( | ||||||||||||||||||||||||||||
| 18 | 42 => sub ($data) { | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Codec::CBOR returns raw bytes for Major Type 2 | ||||||||||||||||||||||||||||
| 21 | return Archive::CAR::CID->from_raw( substr( $data, 1 ) ) if substr( $data, 0, 1 ) eq "\x00"; | ||||||||||||||||||||||||||||
| 22 | return Archive::CAR::CID->from_raw($data); | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | 10 | ); | |||||||||||||||||||||||||||
| 25 | $codec->add_class_handler( | ||||||||||||||||||||||||||||
| 26 | 'Archive::CAR::CID' => sub ( $codec_obj, $item ) { | ||||||||||||||||||||||||||||
| 27 | my $cid_raw = $item->raw; | ||||||||||||||||||||||||||||
| 28 | return pack( 'C', 0xd8 ) . pack( 'C', 42 ) . $codec_obj->_encode_bytes( "\x00" . $cid_raw ); | ||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | 10 | ); | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 3 | ▶ | method version () {1} | ||||||||||||||||||||||||||
|
Subroutine
version: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | method to_file ($filename) { Archive::CAR->write( $filename, $self->roots, $self->blocks, 1 ) } | ||||||||||||||||||||||||||
|
Subroutine
to_file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 6 | ▶ | method read ( $fh, $limit //= undef ) { | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 6 | my $data_start = systell($fh); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # Header | ||||||||||||||||||||||||||||
| 39 | 6 | my ($header_len) = Archive::CAR::Utils::decode_varint($fh); | |||||||||||||||||||||||||||
| 40 | 6 | ▶ | return undef unless defined $header_len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 6 | my $header_raw; | |||||||||||||||||||||||||||
| 42 | 6 | read( $fh, $header_raw, $header_len ); | |||||||||||||||||||||||||||
| 43 | 6 | $header = $codec->decode($header_raw); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # Ensure roots are CID objects. | ||||||||||||||||||||||||||||
| 46 | # If they were decoded as Tag 42 with our handler, they already are. | ||||||||||||||||||||||||||||
| 47 | 6 | ▶ | $roots = $header->{roots} if $header->{roots}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 6 | my @blocks_list; | |||||||||||||||||||||||||||
| 49 | 6 | ▶ | while ( !defined $limit || systell($fh) < $data_start + $limit ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $limit or systell($fh) < $data_start + $limit
MC/DC100%2/2
not defined $limit or systell($fh) < $data_start + $limit
not defined $limit
systell($fh) < $data_start + $limit
Truth table100%3/3
not(A) or B
A= not defined $limitB= systell($fh) < $data_start + $limit
| |||||||||||||||||||||||||||||
| 50 | 31 | my $record_start = systell($fh); | |||||||||||||||||||||||||||
| 51 | 31 | my ( $block_len, $varint_len ) = Archive::CAR::Utils::decode_varint($fh); | |||||||||||||||||||||||||||
| 52 | 31 | ▶ | last unless defined $block_len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 28 | my $cid = Archive::CAR::Utils::decode_cid($fh); | |||||||||||||||||||||||||||
| 54 | 28 | ▶ | last unless defined $cid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 28 | my $cid_len = length( $cid->raw ); | |||||||||||||||||||||||||||
| 56 | 28 | my $data_len = $block_len - $cid_len; | |||||||||||||||||||||||||||
| 57 | 28 | ▶ | last if $data_len < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 28 | my $data; | |||||||||||||||||||||||||||
| 59 | 28 | read( $fh, $data, $data_len ); | |||||||||||||||||||||||||||
| 60 | 28 | push @blocks_list, | |||||||||||||||||||||||||||
| 61 | { | ||||||||||||||||||||||||||||
| 62 | cid => $cid, | ||||||||||||||||||||||||||||
| 63 | data => $data, | ||||||||||||||||||||||||||||
| 64 | offset => $record_start, | ||||||||||||||||||||||||||||
| 65 | length => $block_len + $varint_len, | ||||||||||||||||||||||||||||
| 66 | blockOffset => systell($fh) - $data_len, | ||||||||||||||||||||||||||||
| 67 | blockLength => $data_len | ||||||||||||||||||||||||||||
| 68 | }; | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 6 | $blocks = \@blocks_list; | |||||||||||||||||||||||||||
| 71 | 6 | return $self; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 4 | ▶ | method write ( $fh, $roots, $blocks ) { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # Write header | ||||||||||||||||||||||||||||
| 77 | # Transform CID objects to Tag 42 for the header via class handler | ||||||||||||||||||||||||||||
| 78 | 4 | my $header_data = { version => 1, roots => $roots, }; | |||||||||||||||||||||||||||
| 79 | 4 | my $header_encoded = $codec->encode($header_data); | |||||||||||||||||||||||||||
| 80 | 4 | print {$fh} Archive::CAR::Utils::encode_varint( length($header_encoded) ); | |||||||||||||||||||||||||||
| 81 | 4 | print {$fh} $header_encoded; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Write blocks | ||||||||||||||||||||||||||||
| 84 | 4 | for my $block (@$blocks) { | |||||||||||||||||||||||||||
| 85 | 15 | my $cid_raw = $block->{cid}->raw; | |||||||||||||||||||||||||||
| 86 | 15 | my $data = $block->{data}; | |||||||||||||||||||||||||||
| 87 | 15 | my $block_len = length($cid_raw) + length($data); | |||||||||||||||||||||||||||
| 88 | 15 | print {$fh} Archive::CAR::Utils::encode_varint($block_len); | |||||||||||||||||||||||||||
| 89 | 15 | print {$fh} $cid_raw; | |||||||||||||||||||||||||||
| 90 | 15 | print {$fh} $data; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | }; | ||||||||||||||||||||||||||||
| 94 | # | ||||||||||||||||||||||||||||
| 95 | 2 | 1; | |||||||||||||||||||||||||||