File Coverage

lib/Archive/CAR/Utils.pm
Criterion Covered Total %
statement 45 47 95.7
branch 6 10 60.0
condition 2 3 66.6
subroutine 7 7 100.0
pod 4 4 100.0
total 64 71 90.1


line stmt bran cond sub pod time code
1 2     2   30 use v5.40;
  2         9  
2             #
3             package Archive::CAR::Utils v0.0.4 {
4 2     2   52 use Exporter 'import';
  2         3  
  2         470  
5             our @EXPORT_OK = qw[encode_varint decode_varint decode_cid systell];
6             #
7 151     151 1 208 sub systell ($fh) {
  151         194  
  151         193  
8 151         462 return tell($fh);
9             }
10              
11 21     21 1 26 sub encode_varint ($val) {
  21         29  
  21         26  
12 21         81 my $out = '';
13 21         62 while ( $val >= 0x80 ) {
14 5         14 $out .= chr( ( $val & 0x7f ) | 0x80 );
15 5         13 $val >>= 7;
16             }
17 21         38 $out .= chr($val);
18 21         53 return $out;
19             }
20              
21 133     133 1 155 sub decode_varint ( $str_or_fh, $offset //= 0 ) {
  133         155  
  133         183  
  133         151  
22 2     2   15 use Scalar::Util qw[openhandle];
  2         3  
  2         784  
23              
24             # Handles both scalar strings and filehandles
25 133         245 my $is_fh = openhandle($str_or_fh);
26 133         234 my ( $val, $shift, $bytes_read ) = ( 0, 0, 0 );
27 133         153 my $initial_offset = $offset;
28 133         154 while (1) {
29 139         159 my $byte_val;
30 139 50       217 if ($is_fh) {
31 139         153 my $byte_char;
32 139         413 my $bytes_read_now = read( $str_or_fh, $byte_char, 1 );
33 139 100 66     470 return ( undef, 0 ) unless defined $bytes_read_now && $bytes_read_now == 1;
34 136         178 $byte_val = ord($byte_char);
35             }
36             else {
37 0 0       0 return ( undef, 0 ) unless $offset < length($str_or_fh);
38 0         0 $byte_val = ord( substr( $str_or_fh, $offset++, 1 ) );
39             }
40 136         163 $bytes_read++;
41 136         192 $val |= ( $byte_val & 0x7f ) << $shift;
42 136 100       416 return ( $val, $bytes_read ) unless $byte_val & 0x80;
43 6         10 $shift += 7;
44 6 50       26 return ( undef, $bytes_read ) if $shift >= 64;
45             }
46             }
47              
48 28     28 1 41 sub decode_cid ($fh) {
  28         31  
  28         42  
49 28         143 require Archive::CAR::CID;
50 28         77 return Archive::CAR::CID->decode($fh);
51             }
52             };
53             #
54             1;