tools/plha

filter: Statementstmt 61.1% 206 / 337 Branchbran 39.2% 77 / 196 Conditioncond 29.8% 17 / 57 MC/DCmcdc 8.8% 3 / 34 Subroutinesub 77.7% 35 / 45 Podpod n/a totaltotal 50.5% 338 / 669 CC 119 SCAR 38.7
CC
119
Cov
51%
_extract_lhasa
210.0
_main_lhasa
111.7
__ANON__
90.0
CRAP 48.0
↑ Summary lib/Archive/Lha/Header/Level0.pm »
1 #!/usr/bin/perl
2 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 14 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Getopt::Long qw( GetOptionsFromArray );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use lib File::Spec->catfile($FindBin::Bin, '..', 'lib');
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 14 ▶use Archive::Lha::Decode;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 14 ▶use Archive::Lha::Header;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 14 ▶use Archive::Lha::Header::Utils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 14 ▶use Archive::Lha::Stream::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 14 ▶use POSIX qw( strftime setlocale LC_TIME );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 14 ▶use Time::Moment;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # Charset options: -fc (from charset) and -tc (to charset)
23 14 my $opt_from_charset;
24 my $opt_to_charset;
25 14 my $opt_use_locale;
26  
27 # Parse --use-locale before anything else so setlocale runs at startup
28 14 Getopt::Long::GetOptionsFromArray(\@ARGV,
29 'use-locale' => \$opt_use_locale,
30 'from-charset|fc=s' => \$opt_from_charset,
31 'to-charset|tc=s' => \$opt_to_charset,
32 );
33 14 ▶setlocale(LC_TIME, 'C') unless $opt_use_locale;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt_use_locale 14 0 14
34  
35 # Return display name for a header, respecting -fc/-tc options.
36 # Without options, pathname() auto-detects from the OS field.
37 224 ▶sub _display_name {
Subroutine _display_name: called CC=2 SCAR=6.9
38 224 my ($header) = @_;
39 224 ▶ return $header->pathname( $opt_from_charset, $opt_to_charset // 'UTF-8' );
Condition100%2/2
$opt_to_charset // "UTF-8"
l!l
98126
MC/DC100%1/1
$opt_to_charset // "UTF-8"
$opt_to_charset
Truth table100%2/2
A
A= $opt_to_charset
Aresult
11
00
40 }
41  
42 my $controller = +{
43 0 ▶ d => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
44 0 ▶ my $fname = shift or usage();
Branch: 1 branch
BranchTrueFalseTotal
my $fname = shift() or usage() 0 0 0
45 0 my $stream = open_archive($fname);
46 0 ▶ while ( defined( my $level = $stream->search_header ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $level = $stream->search_header)) 0 0 0
47 0 my $header = Archive::Lha::Header->new(
48 level => $level,
49 stream => $stream
50 );
51 0 $stream->seek( $header->{next_header} );
52 0 print Dumper($header);
53 }
54  
55 },
56 1 ▶ l => sub {
Subroutine __ANON__: called CC=10 SCAR=26.2
57 1 ▶ my $fname = shift or usage();
Branch: 1 branch
BranchTrueFalseTotal
my $fname = shift() or usage() 1 0 1
58 1 my $stream = open_archive($fname);
59 1 ▶ while ( defined( my $level = $stream->search_header ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $level = $stream->search_header)) 3 1 4
60 3 my $header = Archive::Lha::Header->new(
61 level => $level,
62 stream => $stream
63 );
64 3 $stream->seek( $header->{next_header} );
65 3 my $fullname = _display_name($header);
66 3 ▶ $fullname = '' if $fullname eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $fullname eq "." 0 3 3
67 3 ▶ my $has_path = ($fullname =~ m{/} && !_is_directory($header));
Condition33%1/3
$fullname =~ m[/] && !_is_directory($header)
!ll&&!rl&&r
003
MC/DC0%0/2
$fullname =~ m[/] && !_is_directory($header)
$fullname =~ m[/] !_is_directory($header)
Truth table33%1/3
A && not(B)
A= $fullname =~ m[/]B= !_is_directory($header)
ABresult
0X0
100
111
68 # l shows filename only (no path), + prefix if file has a path component
69 3 ▶ my $name = $has_path ? (split m{/}, $fullname)[-1] : $fullname;
Branch: 1 branch
BranchTrueFalseTotal
$has_path ? : 3 0 3
70 3 ▶ $name .= '/' if _is_directory($header) && $name ne '' && $name !~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
if _is_directory($header) and $name ne "" and not $name =~ m[/$] 0 3 3
Condition33%2/6
_is_directory($header) and $name ne "" and not $name =~ m[/$]
!ll&&!rl&&r
300
_is_directory($header) and $name ne ""
!ll&&!rl&&r
300
MC/DC0%0/3
_is_directory($header) and $name ne "" and not $name =~ m[/$]
_is_directory($header) $name ne "" not $name =~ m[/$]
Truth table25%1/4
A and B and not(C)
A= _is_directory($header)B= $name ne ""C= not $name =~ m[/$]
ABCresult
0XX0
10X0
1100
1111
71 3 ▶ my $prefix = $has_path ? '+' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$has_path ? : 3 0 3
72 3 printf "%s%s\n", $prefix, $name;
73 }
74 },
75 5 ▶ v => sub {
Subroutine __ANON__: called CC=8 SCAR=20.9
76 5 my $contents = '';
77 5 ▶ my $fname = shift or usage();
Branch: 1 branch
BranchTrueFalseTotal
my $fname = shift() or usage() 5 0 5
78 5 my $stream = open_archive($fname);
79 5 my $totals = { original_size => 0, encoded_size => 0, count => 0 };
80 5 print "Original Packed Ratio Date Time Name\n";
81 5 print "-------- ------- ----- --------- -------- -------------\n";
82 5 ▶ while ( defined( my $level = $stream->search_header ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $level = $stream->search_header)) 204 5 209
83 204 my $header = Archive::Lha::Header->new(
84 level => $level,
85 stream => $stream
86 );
87 204 $stream->seek( $header->{next_header} );
88  
89 204 $totals->{original_size} += $header->{original_size};
90 204 $totals->{encoded_size} += $header->{encoded_size};
91 204 $totals->{count} += 1;
92 printf "%8d %7d%5.1f%% %s %s %s\n",
93 $header->{original_size},
94 $header->{encoded_size},
95 204 ▶ (($header->{encoded_size} && $header->{original_size}) ? 100 * ($header->{original_size} - $header->{encoded_size}) / $header->{original_size} : 0),
Branch: 1 branch
BranchTrueFalseTotal
$header->{'encoded_size'} && $header->{'original_size'} ? : 202 2 204
Condition66%2/3
$header->{"encoded_size"} and $header->{"original_size"}
!ll&&!rl&&r
20202
MC/DC50%1/2
$header->{"encoded_size"} and $header->{"original_size"}
$header->{"encoded_size"} $header->{"original_size"}
Truth table66%2/3
A and B
A= $header->{"encoded_size"}B= $header->{"original_size"}
ABresult
0X0
100
111
96 _header_date($header),
97 _header_time($header),
98 _display_name($header);
99 204 ▶ printf ": %s\n", $header->{comment} if $header->{comment};
Branch: 1 branch
BranchTrueFalseTotal
if $header->{"comment"} 0 204 204
100 }
101 5 print "-------- ------- ----- --------- --------\n";
102 printf "%8d %7d%5.1f%% %s %s %s\n",
103 $totals->{original_size},
104 $totals->{encoded_size},
105 (($totals->{encoded_size} && $totals->{original_size}) ? 100 * ($totals->{original_size} - $totals->{encoded_size}) / $totals->{original_size} : 0),
106 strftime("%d-%b-%y", localtime((stat($fname))[9])),
107 strftime("%T", localtime((stat($fname))[9])),
108 5 ▶ (sprintf(" %d files", $totals->{count}));
Branch: 1 branch
BranchTrueFalseTotal
$totals->{'encoded_size'} && $totals->{'original_size'} ? : 4 1 5
Condition66%2/3
$totals->{"encoded_size"} and $totals->{"original_size"}
!ll&&!rl&&r
104
MC/DC50%1/2
$totals->{"encoded_size"} and $totals->{"original_size"}
$totals->{"encoded_size"} $totals->{"original_size"}
Truth table66%2/3
A and B
A= $totals->{"encoded_size"}B= $totals->{"original_size"}
ABresult
0X0
100
111
109 },
110 1 ▶ vv => sub {
Subroutine __ANON__: called CC=10 SCAR=24.6
111 1 ▶ my $fname = shift or usage();
Branch: 1 branch
BranchTrueFalseTotal
my $fname = shift() or usage() 1 0 1
112 1 my $stream = open_archive($fname);
113 1 my $totals = { original_size => 0, encoded_size => 0, count => 0 };
114 1 print "Original Packed Ratio Date Time Atts Method CRC L OS\n";
115 1 print "-------- ------- ----- --------- -------- -------- ------ ---- -----\n";
116 1 ▶ while ( defined( my $level = $stream->search_header ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $level = $stream->search_header)) 3 1 4
117 3 my $header = Archive::Lha::Header->new(
118 level => $level,
119 stream => $stream
120 );
121 3 $stream->seek( $header->{next_header} );
122  
123 3 $totals->{original_size} += $header->{original_size};
124 3 $totals->{encoded_size} += $header->{encoded_size};
125 3 $totals->{count} += 1;
126  
127 3 my $name = _display_name($header);
128 3 ▶ my $os_char = uc($header->{os}[0] // '?');
Condition50%1/2
$header->{"os"}[0] // "?"
l!l
30
MC/DC0%0/1
$header->{"os"}[0] // "?"
$header->{"os"}[0]
Truth table50%1/2
A
A= $header->{"os"}[0]
Aresult
11
00
129 3 ▶ my $hdr_level = ref($header) =~ /Level(\d)/ ? $1 : '?';
Branch: 1 branch
BranchTrueFalseTotal
ref($header) =~ /Level(\d)/ ? : 3 0 3
130  
131 3 printf "%s\n", $name;
132 printf "%8d %7d%5.1f%% %s %s ----rwed -%s- %04X %s %s\n",
133 $header->{original_size},
134 $header->{encoded_size},
135 (($header->{encoded_size} && $header->{original_size}) ? 100 * ($header->{original_size} - $header->{encoded_size}) / $header->{original_size} : 0),
136 _header_date($header),
137 _header_time($header),
138 $header->{method},
139 $header->{crc16},
140 3 ▶ $hdr_level,
Branch: 1 branch
BranchTrueFalseTotal
$header->{'encoded_size'} && $header->{'original_size'} ? : 3 0 3
Condition33%1/3
$header->{"encoded_size"} and $header->{"original_size"}
!ll&&!rl&&r
003
MC/DC0%0/2
$header->{"encoded_size"} and $header->{"original_size"}
$header->{"encoded_size"} $header->{"original_size"}
Truth table33%1/3
A and B
A= $header->{"encoded_size"}B= $header->{"original_size"}
ABresult
0X0
100
111
141 $os_char;
142 3 ▶ printf ": %s\n", $header->{comment} if $header->{comment};
Branch: 1 branch
BranchTrueFalseTotal
if $header->{"comment"} 0 3 3
143 }
144 1 print "-------- ------- ----- --------- --------\n";
145 printf "%8d %7d%5.1f%% %s %s %s\n",
146 $totals->{original_size},
147 $totals->{encoded_size},
148 (($totals->{encoded_size} && $totals->{original_size}) ? 100 * ($totals->{original_size} - $totals->{encoded_size}) / $totals->{original_size} : 0),
149 strftime("%d-%b-%y", localtime((stat($fname))[9])),
150 strftime("%T", localtime((stat($fname))[9])),
151 1 ▶ (sprintf(" %d files", $totals->{count}));
Branch: 1 branch
BranchTrueFalseTotal
$totals->{'encoded_size'} && $totals->{'original_size'} ? : 1 0 1
Condition33%1/3
$totals->{"encoded_size"} and $totals->{"original_size"}
!ll&&!rl&&r
001
MC/DC0%0/2
$totals->{"encoded_size"} and $totals->{"original_size"}
$totals->{"encoded_size"} $totals->{"original_size"}
Truth table33%1/3
A and B
A= $totals->{"encoded_size"}B= $totals->{"original_size"}
ABresult
0X0
100
111
152 },
153 0 ▶ t => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
154 0 ▶ my $fname = shift or usage();
Branch: 1 branch
BranchTrueFalseTotal
my $fname = shift() or usage() 0 0 0
155 0 printf "Testing integrity of archive '%s':\n", $fname;
156 0 my $stream = open_archive($fname);
157 0 my $totals = { original_size => 0, encoded_size => 0, count => 0 };
158 0 ▶ while ( defined( my $level = $stream->search_header ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $level = $stream->search_header)) 0 0 0
159 0 my $header = Archive::Lha::Header->new(
160 level => $level,
161 stream => $stream
162 );
163 0 $stream->seek( $header->data_top );
164 0 _decode_entry($header, $stream);
165 0 $stream->seek( $header->{next_header} );
166  
167 0 $totals->{original_size} += $header->{original_size};
168 0 $totals->{encoded_size} += $header->{encoded_size};
169 0 $totals->{count} += 1;
170 0 printf " Testing: (%8d/%8d) %s\n", $header->{original_size}, $header->{original_size}, _display_name($header);
171 }
172 0 my $error = undef;
173 0 ▶ if ($totals->{count}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($totals->{'count'}) { } 0 0 0
174 0 ▶ if (!$error) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 0 0 0
175 0 printf "%d files tested, all files OK\n", $totals->{count};
176 }
177 } else {
178 0 $error = 1;
179 0 printf "No files tested.\n";
180 }
181 0 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 0 0 0
182 0 printf "\nOperation not entirely successful\n\n";
183 } else {
184 0 printf "\nOperation succesful\n\n";
185 }
186 },
187 0 ▶ x => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
188 0 ▶ my $fname = shift or usage();
Branch: 1 branch
BranchTrueFalseTotal
my $fname = shift() or usage() 0 0 0
189 0 my %target;
190 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
191 0 %target = map { $_ => 1 } @_;
192 }
193 0 my $stream = open_archive($fname);
194 0 ▶ while ( defined( my $level = $stream->search_header ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $level = $stream->search_header)) 0 0 0
195 0 my $header = Archive::Lha::Header->new(
196 level => $level,
197 stream => $stream
198 );
199 0 ▶ if ( %target and !$target{$header->pathname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%target and not $target{$header->pathname}) 0 0 0
Condition0%0/3
%target and not $target{$header->pathname}
!ll&&!rl&&r
000
MC/DC0%0/2
%target and not $target{$header->pathname}
%target not $target{$header->pathname}
Truth table0%0/3
A and not(B)
A= %targetB= not $target{$header->pathname}
ABresult
0X0
100
111
200 0 $stream->seek( $header->next_header );
201 0 next;
202 }
203 0 $stream->seek( $header->data_top );
204  
205 0 ▶ if (_is_directory($header)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_directory($header)) 0 0 0
206 0 ▶ mkpath $header->pathname unless -d $header->pathname;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $header->pathname 0 0 0
207 0 $stream->seek( $header->{next_header} );
208 0 next;
209 }
210  
211 0 my ($decoded, $crc) = _decode_entry($header, $stream);
212 0 $stream->seek( $header->{next_header} );
213 0 ▶ die "crc mismatch" if $crc != $header->crc16;
Branch: 1 branch
BranchTrueFalseTotal
if $crc != $header->crc16 0 0 0
214  
215 0 write_all($header->pathname, $decoded);
216 }
217 },
218 14 };
219  
220 14 ▶my $PROGNAME = $ENV{PLHASA} ? 'plhasa' : basename($0);
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'PLHASA'} ? : 6 8 14
221  
222 14 &main;exit;
223  
224 14 ▶sub main {
Subroutine main: called CC=2 SCAR=6.9
225 14 ▶ if ($PROGNAME eq 'plhasa') {
Branch: 1 branch
BranchTrueFalseTotal
if ($PROGNAME eq 'plhasa') { } 6 8 14
226 6 _main_lhasa();
227 } else {
228 8 _main_plha();
229 }
230 }
231  
232 8 ▶sub _main_plha {
Subroutine _main_plha: called CC=4 SCAR=14.0
233 8 GetOptionsFromArray(\@ARGV,
234 'from-charset|fc=s' => \$opt_from_charset,
235 'to-charset|tc=s' => \$opt_to_charset,
236 'use-locale' => \$opt_use_locale,
237 );
238 8 ▶ my $cmd = shift @ARGV or usage();
Branch: 1 branch
BranchTrueFalseTotal
my $cmd = shift @ARGV or usage() 8 0 8
239 8 ▶ my $file = shift @ARGV or usage();
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift @ARGV or usage() 8 0 8
240 8 check_magic($file);
241 8 ▶ if ( !exists $controller->{$cmd} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $controller->{$cmd}) 1 7 8
242 1 usage("Unknown command: $cmd");
243 }
244 7 $controller->{$cmd}->($file, @ARGV);
245 }
246  
247 # lhasa-compatible argument parsing:
248 # [-]{lvtxep[q{num}][finv]}[w=<dir>] archive_file [file...]
249 6 ▶sub _main_lhasa {
Subroutine _main_lhasa: called CC=20 SCAR=47.2
250 6 ▶ my $arg = shift @ARGV or usage_lhasa();
Branch: 1 branch
BranchTrueFalseTotal
my $arg = shift @ARGV or usage_lhasa() 6 0 6
251 6 $arg =~ s/^-//; # strip optional leading dash
252  
253 # extract command letter (first char)
254 6 ▶ my ($cmd_char) = $arg =~ /^([lvtxep])/i or usage_lhasa();
Branch: 1 branch
BranchTrueFalseTotal
my($cmd_char) = $arg =~ /^([lvtxep])/i or usage_lhasa() 6 0 6
255 6 my $flags = substr($arg, 1); # everything after command char
256  
257 # parse options from flags string
258 6 my %opts = (quiet => 0, verbose => 0, force => 0, ignore_path => 0, dry_run => 0, extract_dir => undef);
259 6 ▶ while (length $flags) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $flags) 0 6 6
260 0 ▶ if ($flags =~ s/^q(\d*)//) {
Branch: 6 branches
BranchTrueFalseTotal
if ($flags =~ s/^q(\d*)//) { } 0 0 0
elsif ($flags =~ s/^w=([^\s]+)//) { } 0 0 0
elsif ($flags =~ s/^f//) { } 0 0 0
elsif ($flags =~ s/^i//) { } 0 0 0
elsif ($flags =~ s/^n//) { } 0 0 0
elsif ($flags =~ s/^v//) { } 0 0 0
261 0 ▶ $opts{quiet} = length($1) ? int($1) : 1;
Branch: 1 branch
BranchTrueFalseTotal
length $1 ? : 0 0 0
262 } elsif ($flags =~ s/^w=([^\s]+)//) {
263 0 $opts{extract_dir} = $1;
264 } elsif ($flags =~ s/^f//) {
265 0 $opts{force} = 1;
266 } elsif ($flags =~ s/^i//) {
267 0 $opts{ignore_path} = 1;
268 } elsif ($flags =~ s/^n//) {
269 0 $opts{dry_run} = 1;
270 } elsif ($flags =~ s/^v//) {
271 0 $opts{verbose} = 1;
272 } else {
273 0 $flags = substr($flags, 1); # skip unknown flag
274 }
275 }
276  
277 # also allow w=<dir> as a separate argument
278 6 ▶ if (@ARGV && $ARGV[0] =~ /^w=(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV and $ARGV[0] =~ /^w=(.+)/) 0 6 6
Condition33%1/3
@ARGV and $ARGV[0] =~ /^w=(.+)/
!ll&&!rl&&r
060
MC/DC0%0/2
@ARGV and $ARGV[0] =~ /^w=(.+)/
@ARGV $ARGV[0] =~ /^w=(.+)/
Truth table33%1/3
A and B
A= @ARGVB= $ARGV[0] =~ /^w=(.+)/
ABresult
0X0
100
111
279 0 $opts{extract_dir} = $1;
280 0 shift @ARGV;
281 }
282  
283 6 ▶ my $file = shift @ARGV or usage_lhasa();
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift @ARGV or usage_lhasa() 6 0 6
284 6 check_magic($file);
285  
286 6 my $cmd = lc $cmd_char;
287 6 ▶ $cmd = 'x' if $cmd eq 'e';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "e" 0 6 6
288  
289 6 ▶ if ($cmd eq 'p') {
Branch: 5 branches
BranchTrueFalseTotal
if ($cmd eq 'p') { } 0 6 6
elsif ($cmd eq 'x') { } 0 6 6
elsif ($cmd eq 'l') { } 2 4 6
elsif ($cmd eq 'v') { } 4 0 4
elsif ($cmd eq 't') { } 0 0 0
290 0 _print_to_stdout($file, \%opts, @ARGV);
291 } elsif ($cmd eq 'x') {
292 0 _extract_lhasa($file, \%opts, @ARGV);
293 } elsif ($cmd eq 'l') {
294 2 _list_lhasa($file, 'l');
295 } elsif ($cmd eq 'v') {
296 4 _list_lhasa($file, 'lv');
297 } elsif ($cmd eq 't') {
298 0 $controller->{t}->($file);
299 } else {
300 0 usage_lhasa();
301 }
302 }
303  
304 0 ▶sub _print_to_stdout {
Subroutine _print_to_stdout: not called CC=5 SCAR=34.0
305 0 my ($fname, $opts, @targets) = @_;
306 0 my %target = map { $_ => 1 } @targets;
307 0 my $stream = open_archive($fname);
308 0 ▶ while ( defined( my $level = $stream->search_header ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $level = $stream->search_header)) 0 0 0
309 0 my $header = Archive::Lha::Header->new( level => $level, stream => $stream );
310 0 ▶ if (%target && !$target{$header->pathname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (%target and not $target{$header->pathname}) 0 0 0
Condition0%0/3
%target and not $target{$header->pathname}
!ll&&!rl&&r
000
MC/DC0%0/2
%target and not $target{$header->pathname}
%target not $target{$header->pathname}
Truth table0%0/3
A and not(B)
A= %targetB= not $target{$header->pathname}
ABresult
0X0
100
111
311 0 $stream->seek( $header->{next_header} );
312 0 next;
313 }
314 0 ▶ next if _is_directory($header);
Branch: 1 branch
BranchTrueFalseTotal
if _is_directory($header) 0 0 0
315 0 $stream->seek( $header->data_top );
316 0 my ($decoded) = _decode_entry($header, $stream);
317 0 $stream->seek( $header->{next_header} );
318 0 print $decoded;
319 }
320 }
321  
322 0 ▶sub _extract_lhasa {
Subroutine _extract_lhasa: not called CC=14 SCAR=53.5
323 0 my ($fname, $opts, @targets) = @_;
324 0 my %target = map { $_ => 1 } @targets;
325 0 my $stream = open_archive($fname);
326 0 ▶ while ( defined( my $level = $stream->search_header ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $level = $stream->search_header)) 0 0 0
327 0 my $header = Archive::Lha::Header->new( level => $level, stream => $stream );
328 0 my $pathname = $header->pathname;
329 0 ▶ $pathname =~ s{.*/}{} if $opts->{ignore_path};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"ignore_path"} 0 0 0
330 0 ▶ if (%target && !$target{$pathname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (%target and not $target{$pathname}) 0 0 0
Condition0%0/3
%target and not $target{$pathname}
!ll&&!rl&&r
000
MC/DC0%0/2
%target and not $target{$pathname}
%target not $target{$pathname}
Truth table0%0/3
A and not(B)
A= %targetB= not $target{$pathname}
ABresult
0X0
100
111
331 0 $stream->seek( $header->{next_header} );
332 0 next;
333 }
334 $pathname = File::Spec->catfile($opts->{extract_dir}, $pathname)
335 0 ▶ if $opts->{extract_dir};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"extract_dir"} 0 0 0
336  
337 0 ▶ if (_is_directory($header)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_directory($header)) 0 0 0
338 0 ▶ mkpath $pathname unless -d $pathname || $opts->{dry_run};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $pathname or $opts->{"dry_run"} 0 0 0
Condition0%0/3
-d $pathname or $opts->{"dry_run"}
l!l&&r!l&&!r
000
MC/DC0%0/2
-d $pathname or $opts->{"dry_run"}
-d $pathname $opts->{"dry_run"}
Truth table0%0/3
A or B
A= -d $pathnameB= $opts->{"dry_run"}
ABresult
1X1
011
000
339 0 $stream->seek( $header->{next_header} );
340 0 next;
341 }
342  
343 0 $stream->seek( $header->data_top );
344 0 my ($decoded, $crc) = _decode_entry($header, $stream);
345 0 $stream->seek( $header->{next_header} );
346 0 ▶ die "crc mismatch for " . $header->pathname if $crc != $header->crc16;
Branch: 1 branch
BranchTrueFalseTotal
if $crc != $header->crc16 0 0 0
347  
348 0 ▶ unless ($opts->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"dry_run"}) 0 0 0
349 0 ▶ if (-e $pathname && !$opts->{force}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $pathname and not $opts->{"force"}) 0 0 0
Condition0%0/3
-e $pathname and not $opts->{"force"}
!ll&&!rl&&r
000
MC/DC0%0/2
-e $pathname and not $opts->{"force"}
-e $pathname not $opts->{"force"}
Truth table0%0/3
A and not(B)
A= -e $pathnameB= not $opts->{"force"}
ABresult
0X0
100
111
350 0 print STDERR "$pathname already exists, skipping (use -f to force)\n";
351 0 next;
352 }
353 0 write_all($pathname, $decoded);
354 }
355 0 ▶ printf " %s\n", $pathname if $opts->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"verbose"} 0 0 0
356 }
357 }
358  
359 0 ▶sub usage_lhasa {
Subroutine usage_lhasa: not called CC=1 SCAR=6.9
360 0 die "plhasa -- Perl LHA tool (lhasa-compatible)\n" .
361 "usage: plhasa [-]{lvtxep[q{num}][finv]}[w=<dir>] archive_file [file...]\n" .
362 "commands: options:\n" .
363 " l List (terse) f Force overwrite (no prompt)\n" .
364 " v Verbose list i Ignore directory path\n" .
365 " t Test file CRC in archive n Perform dry run\n" .
366 " x,e Extract from archive q{num} Quiet mode\n" .
367 " p Print to stdout from archive v Verbose\n" .
368 " w=<dir> Specify extract directory\n";
369 }
370  
371 1 ▶sub usage {
Subroutine usage: called CC=2 SCAR=7.4
372 1 my ($msg) = @_;
373 1 my $text = "Usage: $0 [options] (l|v|vv|x|t|d) archive (files)\n" .
374 " l - list contents (LhA terse format, filename only)\n" .
375 " v - list archive verbose (LhA v format)\n" .
376 " vv - list archive full (LhA vv format)\n" .
377 " x - extract archive\n" .
378 " t - test file\n" .
379 " d - dump each header\n" .
380 " -fc, --from-charset <charset> source encoding for filenames (default: auto-detect)\n" .
381 " -tc, --to-charset <charset> output encoding for filenames (default: UTF-8)\n" .
382 " --use-locale use system locale for month names (default: English)\n";
383 1 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) 1 0 1
384 1 die "$msg\n$text";
385 }
386 0 die $text;
387 }
388  
389 207 ▶sub _header_date {
Subroutine _header_date: called CC=2 SCAR=6.9
390 207 my ($h) = @_;
391 return $h->{timestamp_is_unix}
392 ? strftime("%d-%b-%y", localtime($h->{timestamp}))
393 207 ▶ : strftime("%d-%b-%y", Archive::Lha::Header::Utils::_dostime_fields($h->{timestamp}));
Branch: 1 branch
BranchTrueFalseTotal
$h->{'timestamp_is_unix'} ? : 13 194 207
394 }
395  
396 207 ▶sub _header_time {
Subroutine _header_time: called CC=2 SCAR=6.9
397 207 my ($h) = @_;
398 return $h->{timestamp_is_unix}
399 ? strftime("%T", localtime($h->{timestamp}))
400 207 ▶ : strftime("%T", Archive::Lha::Header::Utils::_dostime_fields($h->{timestamp}));
Branch: 1 branch
BranchTrueFalseTotal
$h->{'timestamp_is_unix'} ? : 13 194 207
401 }
402  
403 # ls-style date from Unix epoch (stat mtime etc)
404 # Note: avoid %e (space-padded day) — not supported on Windows MSVC runtime.
405 # Use %d and strip the leading zero manually instead.
406 20 ▶sub _ls_stamp {
Subroutine _ls_stamp: called CC=2 SCAR=6.9
407 20 my ($epoch) = @_;
408 20 my $six_months = 6 * 30 * 86400;
409 20 my @t = localtime($epoch);
410 20 my $day = sprintf "%2d", $t[3]; # space-pad day
411 20 ▶ if (abs(time - $epoch) < $six_months) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs time - $epoch < $six_months) 6 14 20
412 6 return strftime("%b", @t) . " $day " . strftime("%H:%M", @t);
413 }
414 14 return strftime("%b", @t) . " $day " . strftime("%Y", @t);
415 }
416  
417 # ls-style date from a header (handles both DOS and Unix timestamps)
418 14 ▶sub _ls_stamp_header {
Subroutine _ls_stamp_header: called CC=2 SCAR=6.9
419 14 my ($header) = @_;
420 my $epoch = $header->{timestamp_is_unix}
421 ? $header->{timestamp}
422 14 ▶ : Archive::Lha::Header::Utils::_dostime2utime($header->{timestamp});
Branch: 1 branch
BranchTrueFalseTotal
$header->{'timestamp_is_unix'} ? : 12 2 14
423 14 return _ls_stamp($epoch);
424 }
425  
426 # Lhasa-compatible listing (l = terse, lv = verbose with method+crc)
427 6 ▶sub _list_lhasa {
Subroutine _list_lhasa: called CC=16 SCAR=28.3
428 6 my ($fname, $mode) = @_;
429 6 my $stream = open_archive($fname);
430 6 my $totals = { original_size => 0, encoded_size => 0, count => 0 };
431  
432 6 ▶ if ($mode eq 'lv') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'lv') { } 4 2 6
433 4 printf " PERMSSN UID GID PACKED SIZE RATIO METHOD CRC STAMP NAME\n";
434 4 printf "---------- ----------- ------- ------- ------ ---------- ------------ -------------\n";
435 } else {
436 2 printf " PERMSSN UID GID SIZE RATIO STAMP NAME\n";
437 2 printf "---------- ----------- ------- ------ ------------ --------------------\n";
438 }
439  
440 6 ▶ while ( defined( my $level = $stream->search_header ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $level = $stream->search_header)) 14 6 20
441 14 my $header = Archive::Lha::Header->new(
442 level => $level,
443 stream => $stream
444 );
445 # Skip to next header - no need to decode for listing
446 14 $stream->seek( $header->{next_header} );
447  
448 14 $totals->{original_size} += $header->{original_size};
449 14 $totals->{encoded_size} += $header->{encoded_size};
450 14 $totals->{count} += 1;
451  
452 14 my $stamp = _ls_stamp_header($header);
453 14 my $name = _display_name($header);
454 14 ▶ $name = '' if $name eq '.'; # empty root directory
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "." 0 14 14
455 14 ▶ $name .= '/' if _is_directory($header) && $name ne '' && $name !~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
if _is_directory($header) and $name ne "" and not $name =~ m[/$] 0 14 14
Condition33%2/6
_is_directory($header) and $name ne "" and not $name =~ m[/$]
!ll&&!rl&&r
1400
_is_directory($header) and $name ne ""
!ll&&!rl&&r
1400
MC/DC0%0/3
_is_directory($header) and $name ne "" and not $name =~ m[/$]
_is_directory($header) $name ne "" not $name =~ m[/$]
Truth table25%1/4
A and B and not(C)
A= _is_directory($header)B= $name ne ""C= not $name =~ m[/$]
ABCresult
0XX0
10X0
1100
1111
456 14 ▶ $name = _fix_msdos_allcaps($name) if ($header->{os}[0] // '') eq 'M';
Branch: 1 branch
BranchTrueFalseTotal
if ($header->{"os"}[0] // "") eq "M" 12 2 14
Condition50%1/2
$header->{"os"}[0] // ""
l!l
140
MC/DC0%0/1
$header->{"os"}[0] // ""
$header->{"os"}[0]
Truth table50%1/2
A
A= $header->{"os"}[0]
Aresult
11
00
457 14 my $prefix = _lhasa_prefix($header);
458 my $ratio_str = _is_directory($header) ? '******'
459 : sprintf("%5.1f%%", $header->{original_size}
460 14 ▶ ? unpack('f', pack('f', 100 * $header->{encoded_size} / $header->{original_size})) : 100);
Branch: 2 branches
BranchTrueFalseTotal
_is_directory($header) ? : 0 14 14
$header->{'original_size'} ? : 14 0 14
461  
462 14 ▶ if ($mode eq 'lv') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'lv') { } 10 4 14
463 printf "%s%7d %7d %s -%s- %04x %s %s\n",
464 $prefix,
465 $header->{encoded_size},
466 $header->{original_size},
467 $ratio_str,
468 $header->{method},
469 $header->{crc16},
470 10 $stamp,
471 $name;
472 } else {
473 printf "%s%7d %s %s %s\n",
474 $prefix,
475 $header->{original_size},
476 4 $ratio_str,
477 $stamp,
478 $name;
479 }
480 }
481  
482 6 ▶ if ($mode eq 'lv') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'lv') { } 4 2 6
483 4 printf "---------- ----------- ------- ------- ------ ---------- ------------ -------------\n";
484 } else {
485 2 printf "---------- ----------- ------- ------ ------------ --------------------\n";
486 }
487  
488 my $ratio = $totals->{original_size}
489 6 ▶ ? unpack('f', pack('f', 100 * $totals->{encoded_size} / $totals->{original_size})) : 100;
Branch: 1 branch
BranchTrueFalseTotal
$totals->{'original_size'} ? : 6 0 6
490 6 my $stamp = _ls_stamp((stat($fname))[9]);
491  
492 # PERMSSN (10) + sep (1) + UID/GID (11) + sep (1) = 23 chars for prefix
493 # " Total " (PERMSSN 10) + " " (sep) + "%5d files" (UID/GID 11) + " " (sep) = 23
494 6 ▶ my $file_str = $totals->{count} == 1 ? 'file ' : 'files';
Branch: 1 branch
BranchTrueFalseTotal
$totals->{'count'} == 1 ? : 2 4 6
495 6 my $prefix = " Total " . sprintf(" %5d %s ", $totals->{count}, $file_str);
496  
497 6 ▶ if ($mode eq 'lv') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'lv') { } 4 2 6
498 printf "%s%7d %7d %5.1f%% %s\n",
499 $prefix,
500 $totals->{encoded_size},
501 $totals->{original_size},
502 4 $ratio,
503 $stamp;
504 } else {
505 printf "%s%7d %5.1f%% %s\n",
506 $prefix,
507 $totals->{original_size},
508 2 $ratio,
509 $stamp;
510 }
511 }
512  
513 34 ▶sub _is_directory { $_[0]->{method} eq 'lhd' }
Subroutine _is_directory: called CC=1 SCAR=0.0
514  
515 # MS-DOS archives store filenames in all-caps. Lhasa detects per-file
516 # all-caps paths and converts to lowercase. Match that behavior.
517 12 ▶sub _fix_msdos_allcaps {
Subroutine _fix_msdos_allcaps: called CC=2 SCAR=8.1
518 12 my ($name) = @_;
519 12 ▶ return $name if $name =~ /[a-z]/; # has lowercase = not all-caps
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /[a-z]/ 12 0 12
520 0 return lc $name;
521 }
522  
523 # Format permission/owner prefix like lhasa does
524 14 ▶sub _lhasa_prefix {
Subroutine _lhasa_prefix: called CC=10 SCAR=42.2
525 14 my ($header) = @_;
526 14 ▶ if (defined $header->{unix_perm}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $header->{"unix_perm"}) 0 14 14
527 0 my $perm = $header->{unix_perm};
528 0 ▶ my $type = _is_directory($header) ? 'd' : '-';
Branch: 1 branch
BranchTrueFalseTotal
_is_directory($header) ? : 0 0 0
529 0 my $str = $type;
530 0 for my $shift (6, 3, 0) {
531 0 my $bits = ($perm >> $shift) & 7;
532 0 ▶ $str .= ($bits & 4) ? 'r' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$bits & 4 ? : 0 0 0
533 0 ▶ $str .= ($bits & 2) ? 'w' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$bits & 2 ? : 0 0 0
534 0 ▶ $str .= ($bits & 1) ? 'x' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$bits & 1 ? : 0 0 0
535 }
536 0 ▶ my $uid = $header->{unix_uid} // 0;
Condition0%0/2
$header->{"unix_uid"} // 0
l!l
00
MC/DC0%0/1
$header->{"unix_uid"} // 0
$header->{"unix_uid"}
Truth table0%0/2
A
A= $header->{"unix_uid"}
Aresult
11
00
537 0 ▶ my $gid = $header->{unix_gid} // 0;
Condition0%0/2
$header->{"unix_gid"} // 0
l!l
00
MC/DC0%0/1
$header->{"unix_gid"} // 0
$header->{"unix_gid"}
Truth table0%0/2
A
A= $header->{"unix_gid"}
Aresult
11
00
538 # PERMSSN(10) + sep(1) + UID/GID(%5d/%-5d = 11) + sep(1) = 23
539 0 return sprintf "%s %5d/%-5d ", $str, $uid, $gid;
540 }
541 14 ▶ return sprintf "%-23s", '[' . ($header->{os}[1] // 'generic') . ']';
Condition50%1/2
$header->{"os"}[1] // "generic"
l!l
140
MC/DC0%0/1
$header->{"os"}[1] // "generic"
$header->{"os"}[1]
Truth table50%1/2
A
A= $header->{"os"}[1]
Aresult
11
00
542 }
543  
544 0 ▶sub _decode_entry {
Subroutine _decode_entry: not called CC=2 SCAR=17.9
545 0 my ($header, $stream) = @_;
546 0 ▶ return ('', 0) if _is_directory($header);
Branch: 1 branch
BranchTrueFalseTotal
if _is_directory($header) 0 0 0
547 0 my $decoded = '';
548 my $decoder = Archive::Lha::Decode->new(
549 header => $header,
550 0 ▶ read => sub { $stream->read(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
551 0 ▶ write => sub { $decoded .= join '', @_ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
552 0 );
553 0 my $crc = $decoder->decode;
554 0 return ($decoded, $crc);
555 }
556  
557 13 ▶sub open_archive {
Subroutine open_archive: called CC=2 SCAR=7.1
558 13 my $fname = shift;
559 13 ▶ die "fname missing" unless $fname;
Branch: 1 branch
BranchTrueFalseTotal
unless $fname 0 13 13
560 13 Archive::Lha::Stream::File->new(file => $fname);
561 }
562  
563 0 ▶sub write_all {
Subroutine write_all: not called CC=3 SCAR=24.8
564 0 my ($fname, $data) = @_;
565 0 my $dir = dirname($fname);
566 0 ▶ mkpath $dir unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
567 0 ▶ open my $fh, '>:raw', $fname or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $fname or die $! 0 0 0
568 0 binmode $fh;
569 0 print $fh $data;
570 0 close $fh;
571 }
572  
573 14 ▶sub check_magic {
Subroutine check_magic: called CC=4 SCAR=13.9
574 14 my $fname = shift;
575 14 ▶ open my $fh, '<:raw', $fname or die "Cannot open $fname: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $fname or die "Cannot open ${fname}: $!" 14 0 14
576 14 binmode $fh;
577 14 my $magic;
578 14 my $chars = read($fh, $magic, 5);
579 14 my ($signature) = unpack("x2a3", $magic);
580 14 ▶ die 'Does not look like an LHa file' unless $signature eq "-lh";
Branch: 1 branch
BranchTrueFalseTotal
unless $signature eq "-lh" 0 14 14
581  
582 # Check for truncation: last byte of a well-formed LHA archive is 0x00
583 14 seek $fh, -1, 2;
584 14 my $last_byte;
585 14 read $fh, $last_byte, 1;
586 14 ▶ if ( ord($last_byte) != 0x00 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ord $last_byte != 0) 1 13 14
587 1 warn "WARNING: Archive may be truncated or corrupt (last byte is not 0x00)\n";
588 }
589 14 close $fh;
590 }
591  
592 __END__
↑ Summary lib/Archive/Lha/Header/Level0.pm »