lib/Archive/Lha/Header/Utils.pm

filter: Statementstmt 76.9% 60 / 78 Branchbran 61.6% 37 / 60 Conditioncond 48.2% 14 / 29 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 53.3% 8 / 15 Podpod n/a totaltotal 61.5% 120 / 195 CC 40 SCAR 29.1
CC
40
Cov
66%
_os_id
27.9
_extended_header_buf
21.4
_dostime2utime_pp
20.0
CRAP 18.3
« lib/Archive/Lha/Header/Level0.pm ↑ Summary lib/Archive/Lha/Stream.pm »
1 package Archive::Lha::Header::Utils;
2  
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use Time::Local ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 eval { require Archive::Lha::Decode::Base }; # bootstrap XS functions
8 18 ▶use Exporter::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 18 our @EXPORT = qw(
11 _int _short _checksum _dostime2utime _dostime_fields _os_id _extended_header _extended_header_buf
12 );
13  
14 # Prefer the XS implementations (installed by the bootstrap above);
15 # fall back to the pure-Perl versions when the XS is unavailable.
16 18 ▶*_checksum = \&_checksum_pp unless defined &_checksum;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &Archive::Lha::Header::Utils::_checksum 0 18 18
17 18 ▶*_dostime2utime = \&_dostime2utime_pp unless defined &_dostime2utime;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &Archive::Lha::Header::Utils::_dostime2utime 0 18 18
18  
19 0 ▶sub _int { unpack 'V', ( pack 'aaaa', @_ ) }
Subroutine _int: not called CC=1 SCAR=6.9
20 0 ▶sub _short { unpack 'v', ( pack 'aa', @_ ) }
Subroutine _short: not called CC=1 SCAR=6.9
21  
22 # Fast variants that operate directly on a raw buffer + offset
23 0 ▶sub _int_at { unpack 'V', substr($_[0], $_[1], 4) }
Subroutine _int_at: not called CC=1 SCAR=6.9
24 0 ▶sub _short_at { unpack 'v', substr($_[0], $_[1], 2) }
Subroutine _short_at: not called CC=1 SCAR=6.9
25  
26 # Sum of all bytes from offset to end of buffer, truncated to one byte
27 0 ▶sub _checksum_pp {
Subroutine _checksum_pp: not called CC=2 SCAR=17.9
28 0 my $sum = 0;
29 0 $sum += $_ for unpack 'C*', substr($_[0], $_[1]);
30 0 return $sum & 0xFF;
31 }
32  
33 # Decode DOS timestamp into (sec, min, hour, mday, mon_0based, year_since_1900)
34 388 ▶sub _dostime_fields {
Subroutine _dostime_fields: called CC=3 SCAR=11.0
35 388 ▶ return (0) x 6 unless @_ && $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless @_ and $_[0] 4 384 388
Condition66%2/3
@_ and $_[0]
!ll&&!rl&&r
04384
MC/DC50%1/2
@_ and $_[0]
@_ $_[0]
Truth table66%2/3
A and B
A= @_B= $_[0]
ABresult
0X0
100
111
36 384 my $v = $_[0];
37 return (
38 384 ($v & 0x1F) * 2,
39 ($v >> 5) & 0x3F,
40 ($v >> 11) & 0x1F,
41 ($v >> 16) & 0x1F,
42 (($v >> 21) & 0x0F) - 1,
43 (($v >> 25) & 0x7F) + 80,
44 );
45 }
46  
47 0 ▶sub _dostime2utime_pp {
Subroutine _dostime2utime_pp: not called CC=4 SCAR=30.0
48 0 ▶ return 0 unless @_ && $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless @_ and $_[0] 0 0 0
Condition0%0/3
@_ and $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and $_[0]
@_ $_[0]
Truth table0%0/3
A and B
A= @_B= $_[0]
ABresult
0X0
100
111
49 0 my @t = _dostime_fields($_[0]);
50 # DOS timestamps are local time, like the XS mktime. Pass a full year
51 # to avoid Time::Local's two-digit-year heuristic.
52 0 $t[5] += 1900;
53 0 ▶ eval { Time::Local::timelocal(@t) } // 0;
Condition0%0/2
eval { do { Time::Local::timelocal(@t) } } // 0
l!l
00
MC/DC0%0/1
eval { do { Time::Local::timelocal(@t) } } // 0
eval { do { Time::Local::timelocal(@t) } }
Truth table0%0/2
A
A= eval { do { Time::Local::timelocal(@t) } }
Aresult
11
00
54 }
55  
56 296 ▶sub _os_id {
Subroutine _os_id: called CC=19 SCAR=33.3
57 296 ▶ my $hex = ref $_[0] ? ord($_[0]) : ord(substr($_[0], 0, 1));
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 296 296
58  
59 296 ▶ return [ M => 'MS-DOS' ] if $hex == 0x4D;
Branch: 1 branch
BranchTrueFalseTotal
if $hex == 77 51 245 296
60 245 ▶ return [ w => 'WinNT' ] if $hex == 0x57;
Branch: 1 branch
BranchTrueFalseTotal
if $hex == 87 0 245 245
61 245 ▶ return [ w => 'Win95' ] if $hex == 0x77;
Branch: 1 branch
BranchTrueFalseTotal
if $hex == 119 0 245 245
62 245 ▶ return [ g => 'generic' ] if $hex == 0x00;
Branch: 1 branch
BranchTrueFalseTotal
if $hex == 0 0 245 245
63 245 ▶ return [ U => 'UNIX' ] if $hex == 0x55;
Branch: 1 branch
BranchTrueFalseTotal
if $hex == 85 10 235 245
64 235 ▶ return [ m => 'Macintosh' ] if $hex == 0x6D;
Branch: 1 branch
BranchTrueFalseTotal
if $hex == 109 0 235 235
65 235 ▶ return [ J => 'Java VM' ] if $hex == 0x4A;
Branch: 1 branch
BranchTrueFalseTotal
if $hex == 74 0 235 235
66 235 ▶ return [ 2 => 'OS/2' ] if $hex == 0x32;
Branch: 1 branch
BranchTrueFalseTotal
if $hex == 50 0 235 235
67 235 ▶ return [ 9 => 'OS/9' ] if $hex == 0x39;
Branch: 1 branch
BranchTrueFalseTotal
if $hex == 57 0 235 235
68 235 ▶ return [ K => 'OS/68K' ] if $hex == 0x4B;
Branch: 1 branch
BranchTrueFalseTotal
if $hex == 75 0 235 235
69 235 ▶ return [ 3 => 'OS/386' ] if $hex == 0x33;
Branch: 1 branch
BranchTrueFalseTotal
if $hex == 51 0 235 235
70 235 ▶ return [ H => 'Human68K' ] if $hex == 0x48;
Branch: 1 branch
BranchTrueFalseTotal
if $hex == 72 0 235 235
71 235 ▶ return [ C => 'CP/M' ] if $hex == 0x43;
Branch: 1 branch
BranchTrueFalseTotal
if $hex == 67 0 235 235
72 235 ▶ return [ F => 'FLEX' ] if $hex == 0x46;
Branch: 1 branch
BranchTrueFalseTotal
if $hex == 70 0 235 235
73 235 ▶ return [ R => 'Runser' ] if $hex == 0x52;
Branch: 1 branch
BranchTrueFalseTotal
if $hex == 82 0 235 235
74 235 ▶ return [ T => 'TownsOS' ] if $hex == 0x54;
Branch: 1 branch
BranchTrueFalseTotal
if $hex == 84 0 235 235
75 235 ▶ return [ X => 'XOSK' ] if $hex == 0x58;
Branch: 1 branch
BranchTrueFalseTotal
if $hex == 88 0 235 235
76 235 return [ a => 'Amiga' ];
77 }
78  
79 # Legacy: called with a list of single chars
80 0 ▶sub _extended_header {
Subroutine _extended_header: not called CC=1 SCAR=6.9
81 0 my $buf = join '', @_;
82 0 return _extended_header_buf($buf, 0, length($buf));
83 }
84  
85 # Fast: called with a raw buffer, offset, and length
86 750 ▶sub _extended_header_buf {
Subroutine _extended_header_buf: called CC=16 SCAR=30.6
87 750 my ($buf, $from, $len) = @_;
88 750 my $to = $from + $len;
89 750 my $next = unpack 'v', substr($buf, $to - 2, 2);
90  
91 750 my %hash;
92 750 my $type = ord(substr($buf, $from, 1));
93  
94 750 ▶ if ( $type == 0x00 ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($type == 0) { } 288 462 750
elsif ($type == 1) { } 57 405 462
elsif ($type == 2) { } 287 118 405
elsif ($type == 80) { } 10 108 118
elsif ($type == 81) { } 10 98 108
elsif ($type == 82) { } 0 98 98
elsif ($type == 84) { } 0 98 98
elsif ($type == 57 or $type == 63 or $type == 64 or $type == 65 or $type == 66 or $type == 70 or $type == 125 or $type == 126) { } 98 0 98
95 288 $hash{additional_crc} = unpack 'v', substr($buf, $from + 1, 2);
96 }
97 elsif ( $type == 0x01 ) {
98 57 my $name = substr($buf, $from + 1, $len - 3);
99 57 $name =~ s/\0.*//s;
100 57 $hash{filename} = $name;
101 }
102 elsif ( $type == 0x02 ) {
103 287 my $dir = substr($buf, $from + 1, $len - 3);
104 287 $dir =~ s/\0.*//s;
105 287 $hash{directory} = $dir;
106 }
107 elsif ( $type == 0x50 ) {
108 10 $hash{unix_perm} = unpack 'v', substr($buf, $from + 1, 2);
109 }
110 elsif ( $type == 0x51 ) {
111 10 $hash{unix_gid} = unpack 'v', substr($buf, $from + 1, 2);
112 10 $hash{unix_uid} = unpack 'v', substr($buf, $from + 3, 2);
113 }
114 elsif ( $type == 0x52 ) {
115 0 $hash{unix_group} = substr($buf, $from + 1, $len - 3);
116 }
117 elsif ( $type == 0x54 ) {
118 0 $hash{timestamp} = unpack 'V', substr($buf, $from + 1, 4);
119 0 $hash{timestamp_is_unix} = 1;
120 }
121 ▶ elsif ( $type == 0x39 || $type == 0x3F || $type == 0x40 || $type == 0x41
Condition57%12/21
$type == 57 or $type == 63 or $type == 64 or $type == 65 or $type == 66 or $type == 70 or $type == 125 or $type == 126
l!l&&r!l&&!r
9800
$type == 57 or $type == 63 or $type == 64 or $type == 65 or $type == 66 or $type == 70 or $type == 125
l!l&&r!l&&!r
9800
$type == 57 or $type == 63 or $type == 64 or $type == 65 or $type == 66 or $type == 70
l!l&&r!l&&!r
51470
$type == 57 or $type == 63 or $type == 64 or $type == 65 or $type == 66
l!l&&r!l&&!r
51047
$type == 57 or $type == 63 or $type == 64 or $type == 65
l!l&&r!l&&!r
44747
$type == 57 or $type == 63 or $type == 64
l!l&&r!l&&!r
0494
$type == 57 or $type == 63
l!l&&r!l&&!r
0098
MC/DC0%0/8
$type == 57 or $type == 63 or $type == 64 or $type == 65 or $type == 66 or $type == 70 or $type == 125 or $type == 126
$type == 57 $type == 63 $type == 64 $type == 65 $type == 66 $type == 70 $type == 125 $type == 126
Truth table33%3/9
A or B or C or D or E or F or G or H
A= $type == 57B= $type == 63C= $type == 64D= $type == 65E= $type == 66F= $type == 70G= $type == 125H= $type == 126
ABCDEFGHresult
1XXXXXXX1
01XXXXXX1
001XXXXX1
0001XXXX1
00001XXX1
000001XX1
0000001X1
000000011
000000000
122 || $type == 0x42 || $type == 0x46 || $type == 0x7D || $type == 0x7E ) {
123 # known but ignored header types
124 }
125 else {
126 0 warn sprintf "Unknown extended header type: %02x\n", $type;
127 }
128  
129 750 return ($next, \%hash);
130 }
131  
132 18 1;
133  
134 __END__
« lib/Archive/Lha/Header/Level0.pm ↑ Summary lib/Archive/Lha/Stream.pm »