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   22 use v5.40;
  2         12  
2             #
3             package Archive::CAR::Utils v0.0.3 {
4 2     2   12 use Exporter 'import';
  2         21  
  2         368  
5             our @EXPORT_OK = qw[encode_varint decode_varint decode_cid systell];
6             #
7 154     154 1 138 sub systell ($fh) {
  154         115  
  154         127  
8 154         317 return tell($fh);
9             }
10              
11 21     21 1 19 sub encode_varint ($val) {
  21         17  
  21         21  
12 21         19 my $out = '';
13 21         39 while ( $val >= 0x80 ) {
14 5         13 $out .= chr( ( $val & 0x7f ) | 0x80 );
15 5         10 $val >>= 7;
16             }
17 21         32 $out .= chr($val);
18 21         58 return $out;
19             }
20              
21 133     133 1 135 sub decode_varint ( $str_or_fh, $offset //= 0 ) {
  133         112  
  133         117  
  133         91  
22 2     2   12 use Scalar::Util qw[openhandle];
  2         3  
  2         741  
23              
24             # Handles both scalar strings and filehandles
25 133         153 my $is_fh = openhandle($str_or_fh);
26 133         148 my ( $val, $shift, $bytes_read ) = ( 0, 0, 0 );
27 133         114 my $initial_offset = $offset;
28 133         121 while (1) {
29 139         106 my $byte_val;
30 139 50       139 if ($is_fh) {
31 139         103 my $byte_char;
32 139         288 my $bytes_read_now = read( $str_or_fh, $byte_char, 1 );
33 139 100 66     318 return ( undef, 0 ) unless defined $bytes_read_now && $bytes_read_now == 1;
34 136         130 $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         104 $bytes_read++;
41 136         126 $val |= ( $byte_val & 0x7f ) << $shift;
42 136 100       294 return ( $val, $bytes_read ) unless $byte_val & 0x80;
43 6         8 $shift += 7;
44 6 50       15 return ( undef, $bytes_read ) if $shift >= 64;
45             }
46             }
47              
48 36     36 1 29 sub decode_cid ($fh) {
  36         27  
  36         28  
49 36         555 require Archive::CAR::CID;
50 36         90 return Archive::CAR::CID->decode($fh);
51             }
52             };
53             #
54             1;