SSI/File/Type.pm

filter: Statementstmt 30.7% 158 / 514 Branchbran 16.0% 53 / 330 Conditioncond 15.5% 14 / 90 MC/DCmcdc 0.0% 0 / 53 Subroutinesub 56.4% 22 / 39 Podpod 75.0% 15 / 20 totaltotal 25.0% 262 / 1046 CC 204 SCAR 57.4
CC
204
Cov
24%
_magic_match_str
992.0
parse_magic_line
702.0
_magic_match
319.1
CRAP 310.4
« SSI.pm ↑ Summary SSI/URI.pm »
1 ##----------------------------------------------------------------------------
2 ## Apache2 Server Side Include Parser - ~/lib/Apache2/SSI/File/Type.pm
3 ## Version v0.1.3
4 ## Copyright(c) 2024 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/03/27
7 ## Modified 2025/03/22
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package Apache2::SSI::File::Type;
14 BEGIN
15 9 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶ use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶ use vars qw( $VERSION $TEMPLATES $ESC $MAGIC_DATA $MAGIC_DATA_SOURCE );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 ▶ use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 9 ▶ use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 9 ▶ use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 9 ▶ use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 9 ▶ use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 9 ▶ use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 9 ▶ use URI::file;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 9 our $VERSION = 'v0.1.3';
29 # Translation of type in magic file to unpack template and byte count
30 9 our $TEMPLATES =
31 {
32 'byte' => [ 'c', 1 ],
33 'ubyte' => [ 'C', 1 ],
34 'char' => [ 'c', 1 ],
35 'uchar' => [ 'C', 1 ],
36 'short' => [ 's', 2 ],
37 'ushort' => [ 'S', 2 ],
38 'long' => [ 'l', 4 ],
39 'ulong' => [ 'L', 4 ],
40 'date' => [ 'l', 4 ],
41 'ubeshort' => [ 'n', 2 ],
42 'beshort' => [ [ 'n', 'S', 's' ], 2 ],
43 'ubelong' => [ 'N', 4 ],
44 'belong' => [ [ 'N', 'I', 'i' ], 4 ],
45 'bedate' => [ 'N', 4 ],
46 'uleshort' => [ 'v', 2 ],
47 'leshort' => [ [ 'v', 'S', 's' ], 2 ],
48 'ulelong' => [ 'V', 4 ],
49 'lelong' => [ [ 'V', 'I', 'i' ], 4 ],
50 'ledate' => [ 'V', 4 ],
51 'string' => undef(),
52 };
53
54 # For letter escapes in magic file
55 9 our $ESC =
56 {
57 'n' => "\n",
58 'r' => "\r",
59 'b' => "\b",
60 't' => "\t",
61 'f' => "\f"
62 };
63 # Cache
64 9 our $MAGIC_DATA = [];
65 # Keep a record of the source data file, if any, so we can re-use this cached data instead of re-reading from it
66 9 our $MAGIC_DATA_SOURCE = '';
67 };
68  
69 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 sub init
73 1 ▶{
Subroutine init: called CC=17 SCAR=41.4
Pod documented
74 1 my $self = shift( @_ );
75 1 my $file;
76 1 ▶ $file = shift( @_ ) if( @_ % 2 );
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 1 1
77 1 my $opts = $self->_get_args_as_hash( @_ );
78 1 ▶ $opts->{magic} = $file if( length( $file ) );
Branch: 1 branch
BranchTrueFalseTotal
if length $file 0 1 1
79 1 $self->{follow_links} = 1;
80 1 $self->{check_magic} = 0;
81 # If there is an error or file is empty, it returns undef instead of application/octet-stream
82 1 $self->{error_returns_undef} = 0;
83 # Default to returns text/plain. If not, it will return an empty string and leave the caller to set the default mime-type.
84 1 $self->{default_type} = 'text/plain';
85 1 $self->{_init_strict_use_sub} = 1;
86 1 $self->SUPER::init( @_ );
87 1 $self->{magic} = {};
88 1 $self->{magic_data} = [];
89 my $load_json_data = sub
90 1 ▶ {
Subroutine __ANON__: called CC=4 SCAR=14.2
91 1 ▶ my $json_file = shift( @_ ) || return;
Condition50%1/2
shift @_ || (return)
l!l
10
MC/DC0%0/1
shift @_ || (return)
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
92 1 ▶ my $io = IO::File->new( "<$json_file" ) ||
Condition50%1/2
'IO::File'->new("<$json_file") || (return $self->error(qq[Unable to open our own json magic file "$json_file": $!]))
l!l
10
MC/DC0%0/1
'IO::File'->new("<$json_file") || (return $self->error(qq[Unable to open our own json magic file "$json_file": $!]))
'IO::File'->new("<$json_file")
Truth table50%1/2
A
A= 'IO::File'->new("<$json_file")
Aresult
11
00
93 return( $self->error( "Unable to open our own json magic file \"$json_file\": $!" ) );
94 1 local $/;
95 1 my $buf = scalar( <$io> );
96 1 $io->close;
97 1 local $@;
98 # try-catch
99 my $rv = eval
100 1 {
101 1 my $j = JSON->new->relaxed->allow_nonref;
102 1 $MAGIC_DATA = $self->{magic_data} = $j->decode( $buf );
103 1 return(1);
104 };
105 1 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
106 {
107 0 return( $self->error( "An error occured while trying to json decode ", length( $buf ), " bytes of json data: $@" ) );
108 }
109 1 return( $rv );
110 1 };
111
112 1 ▶ if( $opts->{magic} )
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'magic'}) { } 0 1 1
elsif ($Apache2::SSI::File::Type::MAGIC_DATA and scalar @$Apache2::SSI::File::Type::MAGIC_DATA) { } 0 1 1
113 {
114 0 $file = $opts->{magic};
115 0 my $file_abs = URI::file->new_abs( $file )->file( $^O );
116 0 ▶ if( $file_abs eq $MAGIC_DATA_SOURCE && scalar( @$MAGIC_DATA ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($file_abs eq $Apache2::SSI::File::Type::MAGIC_DATA_SOURCE and scalar @$Apache2::SSI::File::Type::MAGIC_DATA) { } 0 0 0
Condition0%0/2
$file_abs eq $Apache2::SSI::File::Type::MAGIC_DATA_SOURCE and scalar @$Apache2::SSI::File::Type::MAGIC_DATA
!ll
00
MC/DC0%0/1
$file_abs eq $Apache2::SSI::File::Type::MAGIC_DATA_SOURCE and scalar @$Apache2::SSI::File::Type::MAGIC_DATA
$file_abs eq $Apache2::SSI::File::Type::MAGIC_DATA_SOURCE
Truth table0%0/2
A
A= $file_abs eq $Apache2::SSI::File::Type::MAGIC_DATA_SOURCE
Aresult
00
11
117 {
118 0 $self->{magic_data} = $MAGIC_DATA;
119 }
120 else
121 {
122 0 my $checksum = Digest::MD5::md5_hex( $file_abs );
123 0 my $base = File::Basename::basename( $file );
124 0 my $path = File::Spec->catpath( File::Spec->tmpdir, $base . "_${checksum}.json" );
125 0 ▶ if( -e( $path ) && -s( $path ) )
Branch: 1 branch
BranchTrueFalseTotal
if (-e $path and -s $path) { } 0 0 0
Condition0%0/3
-e $path and -s $path
!ll&&!rl&&r
000
MC/DC0%0/2
-e $path and -s $path
-e $path -s $path
Truth table0%0/3
A and B
A= -e $pathB= -s $path
ABresult
0X0
100
111
126 {
127 0 ▶ $load_json_data->( $path ) || return;
Branch: 1 branch
BranchTrueFalseTotal
&$load_json_data($path) or return 0 0 0
128 }
129 else
130 {
131 0 ▶ return( $self->error( "Magic file provided \"$file\" does not exist." ) ) if( !-e( $file ) );
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 0 0
132 0 ▶ my $io = IO::File->new( "<$file" ) ||
Condition0%0/2
'IO::File'->new("<$file") || (return $self->error(qq[Unable to open magic file provided "$file": $!]))
l!l
00
MC/DC0%0/1
'IO::File'->new("<$file") || (return $self->error(qq[Unable to open magic file provided "$file": $!]))
'IO::File'->new("<$file")
Truth table0%0/2
A
A= 'IO::File'->new("<$file")
Aresult
11
00
133 return( $self->error( "Unable to open magic file provided \"$file\": $!" ) );
134 0 $io->binmode;
135 0 $self->parse_magic_file( $io );
136 0 $MAGIC_DATA = $self->{magic_data};
137 0 $io->close;
138 0 ▶ my $json = $self->as_json || return;
Condition0%0/2
$self->as_json || (return)
l!l
00
MC/DC0%0/1
$self->as_json || (return)
$self->as_json
Truth table0%0/2
A
A= $self->as_json
Aresult
11
00
139 0 ▶ my $fh = IO::File->new( ">$path" ) ||
Condition0%0/2
'IO::File'->new(">$path") || (return $self->error(qq[Unable to write to magic cache json data file "$path": $!]))
l!l
00
MC/DC0%0/1
'IO::File'->new(">$path") || (return $self->error(qq[Unable to write to magic cache json data file "$path": $!]))
'IO::File'->new(">$path")
Truth table0%0/2
A
A= 'IO::File'->new(">$path")
Aresult
11
00
140 return( $self->error( "Unable to write to magic cache json data file \"$path\": $!" ) );
141 0 $fh->binmode;
142 0 $fh->print( $json );
143 0 $fh->close;
144 }
145 0 $MAGIC_DATA_SOURCE = $file_abs;
146 }
147 }
148 ▶ elsif( $MAGIC_DATA && scalar( @$MAGIC_DATA ) )
Condition50%1/2
$Apache2::SSI::File::Type::MAGIC_DATA and scalar @$Apache2::SSI::File::Type::MAGIC_DATA
!ll
01
MC/DC0%0/1
$Apache2::SSI::File::Type::MAGIC_DATA and scalar @$Apache2::SSI::File::Type::MAGIC_DATA
$Apache2::SSI::File::Type::MAGIC_DATA
Truth table50%1/2
A
A= $Apache2::SSI::File::Type::MAGIC_DATA
Aresult
00
11
149 {
150 0 $self->{magic_data} = $MAGIC_DATA;
151 }
152 else
153 {
154 1 $file = __FILE__;
155 1 $file =~ s/\.pm/\.json/;
156 1 ▶ return( $self->error( "Apache2::SSI magic file \"$file\" does not exist." ) ) if( !-e( $file ) );
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 1 1
157 1 ▶ $load_json_data->( $file ) || return;
Branch: 1 branch
BranchTrueFalseTotal
&$load_json_data($file) or return 1 0 1
158 }
159
160 # From the BSD names.h, some tokens for hard-coded checks of different texts.
161 # This isn't rocket science. It's prone to failure so these checks are only a last resort.
162 $self->{SPECIALS} =
163 {
164 1 'message/rfc822' =>
165 [
166 '^Received:',
167 '^>From ',
168 '^From ',
169 '^To: ',
170 '^Return-Path: ',
171 '^Cc: ',
172 '^X-Mailer: '
173 ],
174 'message/news' =>
175 [
176 '^Newsgroups: ',
177 '^Path: ',
178 '^X-Newsreader: '
179 ],
180 'text/html' =>
181 [
182 '<html[^>]*>',
183 '<HTML[^>]*>',
184 '<head[^>]*>',
185 '<HEAD[^>]*>',
186 '<body[^>]*>',
187 '<BODY[^>]*>',
188 '<title[^>]*>',
189 '<TITLE[^>]*>',
190 '<h1[^>]*>',
191 '<H1[^>]*>',
192 ],
193 'text/x-roff' =>
194 [
195 "^\\.SH",
196 "^\\.PP",
197 "^\\.TH",
198 "^\\.BR",
199 "^\\.SS",
200 "^\\.TP",
201 "^\\.IR",
202 ],
203 };
204  
205 $self->{FILE_EXTS} =
206 {
207 1 qr/\.gz$/ => 'application/x-gzip',
208 qr/\.bz2$/ => 'application/x-bzip2',
209 qr/\.Z$/ => 'application/x-compress',
210 qr/\.txt$/ => 'text/plain',
211 qr/\.html$/ => 'text/html',
212 qr/\.htm$/ => 'text/html',
213 };
214 1 return( $self );
215 }
216  
217 sub as_json
218 0 ▶{
Subroutine as_json: not called CC=1 SCAR=6.9
Pod documented
219 0 my $self = shift( @_ );
220 0 my $data = $self->{magic_data};
221 0 my $j = JSON->new->relaxed->allow_nonref;
222 0 my $json = $j->pretty->encode( $data );
223 0 return( $json );
224 }
225  
226 sub check
227 0 ▶{
Subroutine check: not called CC=3 SCAR=24.8
Pod documented
228 0 my $self = shift( @_ );
229 0 my $file = shift( @_ );
230 0 my $prev = $self->check_magic;
231 0 $self->check_magic( 1 );
232 0 ▶ my $io = IO::File->new( "<$file" ) || return( $self->error( "Unable to open magic file \"$file\": $!" ) );
Condition0%0/2
'IO::File'->new("<$file") || (return $self->error(qq[Unable to open magic file "$file": $!]))
l!l
00
MC/DC0%0/1
'IO::File'->new("<$file") || (return $self->error(qq[Unable to open magic file "$file": $!]))
'IO::File'->new("<$file")
Truth table0%0/2
A
A= 'IO::File'->new("<$file")
Aresult
11
00
233 0 $io->binmode;
234 0 $self->{magic}->{io} = $io;
235 0 my $data = [];
236 0 ▶ while( !$io->eof() )
Branch: 1 branch
BranchTrueFalseTotal
unless ($io->eof) 0 0 0
237 {
238 0 $self->read_magic_entry( $data );
239 }
240 0 $io->close();
241 0 $self->dump( $data );
242 0 $self->check_magic( $prev );
243 0 return( $self );
244 }
245  
246 76 ▶sub check_magic { return( shift->_set_get_boolean( 'check_magic', @_ ) ); }
Subroutine check_magic: called CC=1 SCAR=0.0
Pod documented
247  
248 sub data
249 0 ▶{
Subroutine data: not called CC=6 SCAR=37.4
Pod documented
250 0 my $self = shift( @_ );
251 0 my $data = shift( @_ );
252 0 my $type = '';
253
254 0 ▶ if( length( $data ) <= 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (length $data <= 0) 0 0 0
255 {
256 0 ▶ return( $self->{default_type} ? 'application/octet-stream' : '' );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'default_type'} ? : 0 0 0
257 }
258
259 0 $type = $self->with_magic( $data );
260
261 # 4) Check if it's text or binary.
262 # If it's text, then do a bunch of searching for special tokens
263 0 ▶ if( !defined( $type ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 0 0
264 {
265 0 $type = $self->with_data( $data );
266 }
267 0 ▶ if( !defined( $type ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 0 0
268 {
269 0 ▶ $type = $self->{default_type} ? 'text/plain' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'default_type'} ? : 0 0 0
270 }
271 0 return( $type );
272 }
273  
274 0 ▶sub default_type { return( shift->_set_get_scalar( 'default_type', @_ ) ); }
Subroutine default_type: not called CC=1 SCAR=6.9
Pod documented
275  
276 # Recursively write the magic file to stderr.
277 # Numbers are written in decimal.
278 sub dump
279 0 ▶{
Subroutine dump: not called CC=12 SCAR=50.5
Pod documented
280 0 my $self = shift( @_ );
281 0 ▶ my $data = shift( @_ ) || $self->{magic_data};
Condition0%0/3
shift @_ || $self->{'magic_data'}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift @_ || $self->{'magic_data'}
shift @_ $self->{'magic_data'}
Truth table0%0/3
A || B
A= shift @_B= $self->{'magic_data'}
ABresult
1X1
011
000
282 0 my $depth = shift( @_ );
283 0 ▶ $data = [] unless( defined( $data ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
284 0 ▶ $depth = 0 unless( defined( $depth ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $depth 0 0 0
285 0 our $err = IO::File->new;
286 0 $err->autoflush( 1 );
287 0 ▶ $err->fdopen( fileno( STDERR ), 'w' ) || return( $self->error( "Cannot write to STDERR: $!" ) );
Branch: 1 branch
BranchTrueFalseTotal
$Apache2::SSI::File::Type::err->fdopen(fileno STDERR, "w") or return $self->error("Cannot write to STDERR: $!") 0 0 0
288 0 $err->binmode;
289  
290 0 foreach my $entry ( @$data )
291 {
292 # Delayed evaluation.
293 0 ▶ $entry = $self->parse_magic_line( @$entry ) if( scalar( @$entry ) == 3 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$entry == 3 0 0 0
294 0 ▶ next if( !defined( $entry ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry 0 0 0
295 0 my( $offtype, $offset, $numbytes, $type, $mask, $op, $testval, $template, $message, $subtests ) = @$entry;
296 0 $err->print( '>' x $depth );
297 0 ▶ if( $offtype == 1 )
Branch: 2 branches
BranchTrueFalseTotal
if ($offtype == 1) { } 0 0 0
elsif ($offtype == 2) { } 0 0 0
298 {
299 0 $offset->[2] =~ tr/c/b/;
300 0 $err->printf( "(%s.%s%s)", $offset->[0], $offset->[2], $offset->[3] );
301 }
302 elsif( $offtype == 2 )
303 {
304 0 $err->print( "&", $offset );
305 }
306 else
307 {
308 # offtype == 0
309 0 $err->print( $offset );
310 }
311 0 $err->print( "\t", $type );
312 0 ▶ if( $mask )
Branch: 1 branch
BranchTrueFalseTotal
if ($mask) 0 0 0
313 {
314 0 $err->print( "&", $mask );
315 }
316 0 $err->print( "\t", $op, $testval, "\t", $message, "\n" );
317
318 0 ▶ if( $subtests )
Branch: 1 branch
BranchTrueFalseTotal
if ($subtests) 0 0 0
319 {
320 0 $self->dump( $subtests, $depth + 1 );
321 }
322 }
323 }
324  
325 0 ▶sub error_returns_undef { return( shift->_set_get_boolean( 'error_returns_undef', @_ ) ); }
Subroutine error_returns_undef: not called CC=1 SCAR=6.9
Pod documented
326  
327 sub file
328 1 ▶{
Subroutine file: called CC=24 SCAR=53.3
Pod documented
329 1 my $self = shift( @_ );
330 # The description line. append info to this string
331 1 my $desc = '';
332 1 my $type = '';
333 # Iterate over each file explicitly so we can seek
334 my $file = shift( @_ ) || do
335 1 ▶ {
Condition33%1/3
shift @_ || do { if ($self->{'error_returns_undef'}) { return $self->error('Missing file arguement. Usage: $magic->file( $some_file_name )'); } else { $desc .= 'no file provided.'; return "x-system/x-error; $desc"; } }
l!l&&r!l&&!r
100
MC/DC0%0/2
shift @_ || do { if ($self->{'error_returns_undef'}) { return $self->error('Missing file arguement. Usage: $magic->file( $some_file_name )'); } else { $desc .= 'no file provided.'; return "x-system/x-error; $desc"; } }
shift @_ do { if ($self->{'error_returns_undef'}) { return $self->error('Missing file arguement. Usage: $magic->file( $some_file_name )'); } else { $desc .= 'no file provided.'; return "x-system/x-error; $desc"; } }
Truth table33%1/3
A || B
A= shift @_B= do { if ($self->{'error_returns_undef'}) { return $self->error('Missing file arguement. Usage: $magic->file( $some_file_name )'); } else { $desc .= 'no file provided.'; return "x-system/x-error; $desc"; } }
ABresult
1X1
011
000
336 0 ▶ if( $self->{error_returns_undef} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'error_returns_undef'}) { } 0 0 0
337 {
338 0 return( $self->error( "Missing file arguement. Usage: \$magic->file( \$some_file_name )" ) );
339 }
340 else
341 {
342 0 $desc .= "no file provided.";
343 0 return( "x-system/x-error; $desc" );
344 }
345 };
346
347 # No need to let everybody know what is our server file system
348 1 my $base_file = File::Basename::basename( $file );
349 # 0) Check existence
350 1 ▶ if( !-e( $file ) )
Branch: 2 branches
BranchTrueFalseTotal
if (not -e $file) { } 0 1 1
elsif (not -r $file) { } 0 1 1
351 {
352 0 ▶ if( $self->{error_returns_undef} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'error_returns_undef'}) { } 0 0 0
353 {
354 0 return( $self->error( "File $file does not exist." ) );
355 }
356 else
357 {
358 0 $desc .= "file '$file' does not exist.";
359 0 return( "x-system/x-error; $desc" );
360 }
361 }
362 # 1) Check permission
363 elsif( !-r( $file ) )
364 {
365 0 ▶ if( $self->{error_returns_undef} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'error_returns_undef'}) { } 0 0 0
366 {
367 0 return( $self->error( "Unable to read file '$file'; lacking permission" ) );
368 }
369 else
370 {
371 0 $desc .= "unable to read '$base_file': Permission denied.";
372 0 return( "x-system/x-error; $desc" );
373 }
374 }
375
376 # 2) Check for various special files first
377 1 ▶ if( $self->follow_links )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->follow_links) { } 1 0 1
378 {
379 1 CORE::stat( $file );
380 }
381 else
382 {
383 0 CORE::lstat( $file );
384 }
385 # Avoid doing many useless redondant system stat, use '_'. See perlfunc man page
386 1 ▶ if( !-f( _ ) || -z( _ ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not -f _ or -z _) 0 1 1
Condition33%1/3
not -f _ or -z _
l!l&&r!l&&!r
001
MC/DC0%0/2
not -f _ or -z _
not -f _ -z _
Truth table33%1/3
not(A) or B
A= not -f _B= -z _
ABresult
1X1
011
000
387 {
388 0 ▶ if( !$self->follow_links && -l( _ ) )
Branch: 7 branches
BranchTrueFalseTotal
if (not $self->follow_links and -l _) { } 0 0 0
elsif (-d _) { } 0 0 0
elsif (-p _) { } 0 0 0
elsif (-S _) { } 0 0 0
elsif (-b _) { } 0 0 0
elsif (-c _) { } 0 0 0
elsif (-z _) { } 0 0 0
Condition0%0/3
not $self->follow_links and -l _
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->follow_links and -l _
not $self->follow_links -l _
Truth table0%0/3
not(A) and B
A= not $self->follow_linksB= -l _
ABresult
0X0
100
111
389 {
390 #$desc .= " symbolic link to ". readlink( $file );
391 0 return( 'application/x-link' );
392 }
393 0 elsif( -d( _ ) ) { return( 'application/x-directory' ); }
394 # Named pipe
395 0 elsif( -p( _ ) ) { return( 'application/x-pipe' ); }
396 0 elsif( -S( _ ) ) { return( 'application/x-socket' ); }
397 # Block special file
398 0 elsif( -b( _ ) ) { return( 'application/x-block' ); }
399 # Character special file
400 0 elsif( -c( _ ) ) { return( 'application/x-character' ); }
401 0 elsif( -z( _ ) ) { return( 'application/x-empty' ); }
402 else
403 {
404 0 ▶ return( $self->{default_type} ? $self->{default_type} : 'application/x-unknown' );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'default_type'} ? : 0 0 0
405 }
406 }
407
408 # Current file handle. or undef if check_magic (-c option) is true.
409 1 my $io;
410 $io = IO::File->new( "<$file" ) || do
411 1 ▶ {
Condition33%1/3
'IO::File'->new("<$file") || do { if ($self->{'error_returns_undef'}) { return $self->error("Unable to open file '${file}': $!"); } else { return "x-system/x-error; ${base_file}: $!"; } }
l!l&&r!l&&!r
100
MC/DC0%0/2
'IO::File'->new("<$file") || do { if ($self->{'error_returns_undef'}) { return $self->error("Unable to open file '${file}': $!"); } else { return "x-system/x-error; ${base_file}: $!"; } }
'IO::File'->new("<$file") do { if ($self->{'error_returns_undef'}) { return $self->error("Unable to open file '${file}': $!"); } else { return "x-system/x-error; ${base_file}: $!"; } }
Truth table33%1/3
A || B
A= 'IO::File'->new("<$file")B= do { if ($self->{'error_returns_undef'}) { return $self->error("Unable to open file '${file}': $!"); } else { return "x-system/x-error; ${base_file}: $!"; } }
ABresult
1X1
011
000
412 0 ▶ if( $self->{error_returns_undef} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'error_returns_undef'}) { } 0 0 0
413 {
414 0 return( $self->error( "Unable to open file '$file': $!" ) );
415 }
416 else
417 {
418 0 return( "x-system/x-error; $base_file: $!" );
419 }
420 };
421 1 $io->binmode;
422
423 # 3) Check for script
424 # if( ( -x( $file ) || ( $^O =~ /^(dos|mswin32|NetWare|symbian|win32)$/i && $file =~ /\.(?:pl|cgi)$/ ) ) &&
425 # if( ( -x( $file ) || $file =~ /\.(?:cgi|pl|t)$/ ) &&
426 # -T( _ ) )
427 1 my $default;
428 1 ▶ if( -x( $file ) && -T( _ ) )
Branch: 1 branch
BranchTrueFalseTotal
if (-x $file and -T _) 1 0 1
Condition33%1/3
-x $file and -T _
!ll&&!rl&&r
001
MC/DC0%0/2
-x $file and -T _
-x $file -T _
Truth table33%1/3
A and B
A= -x $fileB= -T _
ABresult
0X0
100
111
429 {
430 # Note, some magic files include elaborate attempts to match #! header lines
431 # and return pretty responses but this slows down matching and is unnecessary.
432 1 my $line1 = $io->getline;
433 1 ▶ if( $line1 =~ /^\#![[:blank:]\h]*(\S+)/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($line1 =~ /^\#![[:blank:]\h]*(\S+)/) 1 0 1
434 {
435 # Returns the binary name, without file path
436 1 my $bin_name = File::Basename::basename( $1 );
437 #$desc .= " executable $bin_name script text";
438 # $io->close;
439 # return( "text/x-${bin_name}" );
440 1 $default = "text/x-${bin_name}";
441 }
442 }
443 1 my $out = $self->handle( $io, $desc, { default => $default } );
444 1 $io->close;
445 1 return( $out );
446 }
447  
448 1 ▶sub follow_links { return( shift->_set_get_boolean( 'follow_links', @_ ) ); }
Subroutine follow_links: called CC=1 SCAR=0.0
Pod documented
449  
450 sub handle
451 1 ▶{
Subroutine handle: called CC=15 SCAR=33.4
Pod documented
452 1 my $self = shift( @_ );
453 1 my $io = shift( @_ );
454 1 my $desc = shift( @_ );
455 1 my $opts = $self->_get_args_as_hash( @_ );
456 1 ▶ $opts->{default} = $self->default_type if( !length( $opts->{default} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $opts->{"default"} 0 1 1
457 1 my $type = '';
458
459 # 3) Iterate over each magic entry.
460 1 my $match_found = 0;
461 1 ▶ for( my $m = 0; $m <= $#{ $self->{magic_data} }; $m++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($m <= $#{$$self{"magic_data"};}) 87 0 87
462 {
463 # Check if the m-th magic entry matches and if it does, then $desc will contain
464 # an updated description
465 87 my $test;
466 87 ▶ if( ( $test = $self->_magic_match( $self->{magic_data}->[$m], \$desc, $io ) ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($test = $self->_magic_match($self->{'magic_data'}[$m], \$desc, $io)) { } 1 86 87
elsif (not defined $test) { } 11 75 86
467 {
468 1 ▶ if( defined( $desc ) && $desc ne '' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $desc and $desc ne "") 1 0 1
Condition33%1/3
defined $desc and $desc ne ""
!ll&&!rl&&r
001
MC/DC0%0/2
defined $desc and $desc ne ""
defined $desc $desc ne ""
Truth table33%1/3
A and B
A= defined $descB= $desc ne ""
ABresult
0X0
100
111
469 {
470 1 $match_found = 1;
471 1 $type = $desc;
472 1 last;
473 }
474 }
475 elsif( !defined( $test ) )
476 {
477 11 ▶ warnings::warn( "Error occurred while checking for match: ", $self->error ) if( warnings::enabled() && $self->debug );
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled() and $self->debug 0 11 11
Condition33%1/3
warnings::enabled() and $self->debug
!ll&&!rl&&r
0110
MC/DC0%0/2
warnings::enabled() and $self->debug
warnings::enabled() $self->debug
Truth table33%1/3
A and B
A= warnings::enabled()B= $self->debug
ABresult
0X0
100
111
478 }
479
480 # Read another entry from the magic file if we've exhausted all the entries
481 # already buffered. read_magic_entry will add to the end of the array
482 # if there are more.
483 86 ▶ if( $m == $#{ $self->{magic_data} } &&
Branch: 1 branch
BranchTrueFalseTotal
if ($m == $#{$$self{"magic_data"};} and $self->{"magic"}{"io"} and not $self->{"magic"}{"io"}->eof) 0 86 86
Condition33%2/6
$m == $#{$$self{"magic_data"};} and $self->{"magic"}{"io"} and not $self->{"magic"}{"io"}->eof
!ll&&!rl&&r
8600
$m == $#{$$self{"magic_data"};} and $self->{"magic"}{"io"}
!ll&&!rl&&r
8600
MC/DC0%0/3
$m == $#{$$self{"magic_data"};} and $self->{"magic"}{"io"} and not $self->{"magic"}{"io"}->eof
$m == $#{$$self{"magic_data"};} $self->{"magic"}{"io"} not $self->{"magic"}{"io"}->eof
Truth table25%1/4
A and B and not(C)
A= $m == $#{$$self{"magic_data"};}B= $self->{"magic"}{"io"}C= not $self->{"magic"}{"io"}->eof
ABCresult
0XX0
10X0
1100
1111
484 $self->{magic}->{io} &&
485 !$self->{magic}->{io}->eof )
486 {
487 0 $self->read_magic_entry();
488 }
489 }
490
491 # 4) Check if it's text or binary.
492 # if It's text, then do a bunch of searching for special tokens
493 1 ▶ if( !$match_found )
Branch: 1 branch
BranchTrueFalseTotal
unless ($match_found) 0 1 1
494 {
495 0 my $data = '';
496 0 $io->seek( 0, 0 );
497 0 $io->read( $data, 0x8564 );
498 0 $type = $self->with_data( $data );
499 }
500 1 ▶ if( !defined( $type ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 1 1
501 {
502 0 ▶ $type = $opts->{default} ? $opts->{default} : '';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'default'} ? : 0 0 0
503 }
504 1 return( $type );
505 }
506  
507 sub parse_magic_file
508 0 ▶{
Subroutine parse_magic_file: not called CC=2 SCAR=17.9
Pod documented
509 0 my $self = shift( @_ );
510 0 my $io = shift( @_ );
511 # Initialize values
512 0 $self->{magic}->{io} = $io;
513 0 $self->{magic}->{buffer} = undef();
514 0 $self->{magic}->{count} = 0;
515 0 ▶ while( !$io->eof() )
Branch: 1 branch
BranchTrueFalseTotal
unless ($io->eof) 0 0 0
516 {
517 0 $self->read_magic_entry();
518 }
519 0 seek( $io, 0, 0 );
520 }
521  
522 # parse_magic_line( $line, $line_num, $subtests )
523 #
524 # Parses the match info out of $line. Returns a reference to an array.
525 #
526 # Format is:
527 #
528 # [ offset, bytes, type, mask, operator, testval, template, sprintf, subtests ]
529 # 0 1 2 3 4 5 6 7 8
530 #
531 # subtests is an array like @$data.
532 sub parse_magic_line
533 0 ▶{
Subroutine parse_magic_line: not called CC=26 SCAR=65.5
Pod undocumented
534 0 my $self = shift( @_ );
535 0 my( $line, $line_num, $subtests ) = @_;
536 0 my( $offtype, $offset, $numbytes, $type, $mask, $operator, $testval, $template, $message );
537
538 # This would be easier if escaped whitespace wasn't allowed.
539
540 # Grab the offset and type. offset can either be a decimal, oct, or hex offset or
541 # an indirect offset specified in parenthesis like (x[.[bsl]][+-][y]), or a relative
542 # offset specified by &. offtype : 0 = absolute, 1 = indirect, 2 = relative
543 0 ▶ if( $line =~ s/^>*([&\(]?[a-flsx\.\+\-\d]+\)?)[[:blank:]\h]+(\S+)[[:blank:]\h]+// )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/^>*([&\(]?[a-flsx\.\+\-\d]+\)?)[[:blank:]\h]+(\S+)[[:blank:]\h]+//) { } 0 0 0
544 {
545 0 ( $offset, $type ) = ( $1, $2 );
546 0 ▶ if( $offset =~ /^\(/ )
Branch: 2 branches
BranchTrueFalseTotal
if ($offset =~ /^\(/) { } 0 0 0
elsif ($offset =~ /^&/o) { } 0 0 0
547 {
548 # Indirect offset.
549 0 $offtype = 1;
550 # Store as a reference [ offset1 type template offset2 ]
551 0 my( $o1, $type, $o2 );
552 0 ▶ if( ( $o1, $type, $o2 ) = ( $offset =~ /\((\d+)(\.[bsl])?([\+\-]?\d+)?\)/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if (() = $offset =~ /\((\d+)(\.[bsl])?([\+\-]?\d+)?\)/) { } 0 0 0
553 {
554 0 ▶ $o1 = oct( $o1 ) if( $o1 =~ /^0/o );
Branch: 1 branch
BranchTrueFalseTotal
if $o1 =~ /^0/o 0 0 0
555 0 ▶ $o2 = oct( $o2 ) if( $o2 =~ /^0/o );
Branch: 1 branch
BranchTrueFalseTotal
if $o2 =~ /^0/o 0 0 0
556
557 0 $type =~ s/\.//;
558 # Default to long
559 0 ▶ $type = 'l' if( $type eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "" 0 0 0
560 # Type will be template for unpack
561 0 $type =~ tr/b/c/;
562 # Number of bytes
563 0 my $sz = $type;
564 0 $sz =~ tr/csl/124/;
565
566 0 $offset = [ $o1, $sz, $type, int( $o2 ) ];
567 }
568 else
569 {
570 0 return( $self->error( "Bad indirect offset at line $line_num. '$offset'" ) );
571 }
572 }
573 elsif( $offset =~ /^&/o )
574 {
575 # Relative offset
576 0 $offtype = 2;
577
578 0 $offset = substr( $offset, 1 );
579 0 ▶ $offset = oct( $offset ) if( $offset =~ /^0/o );
Branch: 1 branch
BranchTrueFalseTotal
if $offset =~ /^0/o 0 0 0
580 }
581 else
582 {
583 # Mormal absolute offset
584 0 $offtype = 0;
585
586 # Convert if needed
587 0 ▶ $offset = oct( $offset ) if( $offset =~ /^0/o );
Branch: 1 branch
BranchTrueFalseTotal
if $offset =~ /^0/o 0 0 0
588 }
589 }
590 else
591 {
592 0 return( $self->error( "Bad Offset/Type at line $line_num. '$line'" ) );
593 }
594
595 # Check for & operator on type
596 0 ▶ if( $type =~ s/&(.*)// )
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ s/&(.*)//) 0 0 0
597 {
598 0 $mask = $1;
599 # Convert if needed
600 0 ▶ $mask = oct( $mask ) if( $mask =~ /^0/o );
Branch: 1 branch
BranchTrueFalseTotal
if $mask =~ /^0/o 0 0 0
601 }
602
603 # Check if type is valid
604 0 ▶ if( !exists( $TEMPLATES->{ $type } ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Apache2::SSI::File::Type::TEMPLATES->{$type}) 0 0 0
605 {
606 0 return( $self->error( "Invalid type '$type' at line $line_num" ) );
607 }
608
609 # Take everything after the first non-escaped space
610 0 ▶ if( $line =~ s/([^\\])\s+(.*)/$1/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/([^\\])\s+(.*)/$1/) { } 0 0 0
611 {
612 0 $message = $2;
613 }
614 else
615 {
616 0 return( $self->error( "Missing or invalid test condition/message at line $line_num" ) );
617 }
618
619 # Remove the return if it is still there
620 0 $line =~ s/\n$//o;
621  
622 # Get the operator. If 'x', must be alone. Default is '='.
623 0 ▶ if( $line =~ s/^([><&^=!])//o )
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ s/^([><&^=!])//o) { } 0 0 0
elsif ($line eq 'x') { } 0 0 0
624 {
625 0 $operator = $1;
626 }
627 elsif( $line eq 'x' )
628 {
629 0 $operator = 'x';
630 }
631 else
632 {
633 0 $operator = '=';
634 }
635
636 0 ▶ if( $type eq 'string' )
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'string') { } 0 0 0
637 {
638 0 $testval = $line;
639
640 # Do octal/hex conversion
641 0 $testval =~ s/\\([x0-7][0-7]?[0-7]?)/chr( oct( $1 ) )/eg;
642
643 # Do single char escapes
644 0 ▶ $testval =~ s/\\(.)/$ESC->{ $1 }||$1/eg;
Condition0%0/3
$Apache2::SSI::File::Type::ESC->{$1} || $1
l!l&&r!l&&!r
000
MC/DC0%0/2
$Apache2::SSI::File::Type::ESC->{$1} || $1
$Apache2::SSI::File::Type::ESC->{$1} $1
Truth table0%0/3
A || B
A= $Apache2::SSI::File::Type::ESC->{$1}B= $1
ABresult
1X1
011
000
645
646 # Put the number of bytes to read in numbytes.
647 # '0' means read to \0 or \n.
648 0 ▶ if( $operator =~ /[>x]/o )
Branch: 3 branches
BranchTrueFalseTotal
if ($operator =~ /[>x]/o) { } 0 0 0
elsif ($operator =~ /[=<]/o) { } 0 0 0
elsif ($operator eq '!') { } 0 0 0
649 {
650 0 $numbytes = 0;
651 }
652 elsif( $operator =~ /[=<]/o )
653 {
654 0 $numbytes = length( $testval );
655 }
656 elsif( $operator eq '!' )
657 {
658 # Annoying special case. ! operator only applies to numerics so put it back.
659 0 $testval = $operator . $testval;
660 0 $numbytes = length( $testval );
661 0 $operator = '=';
662 }
663 else
664 {
665 # There's a bug in my magic file where there's a line that says
666 # "0 string ^!<arc..." and the BSD file program treats the argument
667 # like a numeric. To minimize hassles, complain about bad ops only if -c is set.
668 0 return( $self->error( "Invalid operator '$operator' for type 'string' at line $line_num." ) );
669 }
670 }
671 else
672 {
673 # Numeric
674 0 ▶ if( $operator ne 'x' )
Branch: 1 branch
BranchTrueFalseTotal
if ($operator ne "x") 0 0 0
675 {
676 # This conversion is very forgiving. Tt's faster and it doesn't complain
677 # about bugs in popular magic files, but it will silently turn a string into zero.
678 0 ▶ if( $line =~ /^0/o )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^0/o) { } 0 0 0
679 {
680 0 $testval = oct( $line );
681 }
682 else
683 {
684 0 $testval = int( $line );
685 }
686 }
687
688 0 ( $template, $numbytes ) = @{$TEMPLATES->{ $type }};
689
690 # Unset coercion of $unsigned unless we're doing order comparison
691 0 ▶ if( ref( $template ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $template) 0 0 0
692 {
693 0 ▶ $template = $template->[0] unless( $operator eq '>' || $operator eq '<' );
Branch: 1 branch
BranchTrueFalseTotal
unless $operator eq ">" or $operator eq "<" 0 0 0
Condition0%0/3
$operator eq ">" or $operator eq "<"
l!l&&r!l&&!r
000
MC/DC0%0/2
$operator eq ">" or $operator eq "<"
$operator eq ">" $operator eq "<"
Truth table0%0/3
A or B
A= $operator eq ">"B= $operator eq "<"
ABresult
1X1
011
000
694 }
695 }
696 0 return( [ $offtype, $offset, $numbytes, $type, $mask, $operator, $testval, $template, $message, $subtests ] );
697 }
698  
699 # read_magic_entry( $magic_data, $depth )
700 #
701 # Reads the next entry from the magic file and stores it as a ref to an array at the
702 # end of @$magic_data.
703 #
704 # $magic = { filehandle, last buffered line, line count }
705 #
706 # This is called recursively with increasing $depth to read in sub-clauses
707 #
708 # Returns the depth of the current buffered line.
709 sub read_magic_entry
710 0 ▶{
Subroutine read_magic_entry: not called CC=14 SCAR=53.5
Pod undocumented
711 0 my $self = shift( @_ );
712 0 ▶ my $data = shift( @_ ) || $self->{magic_data};
Condition0%0/3
shift @_ || $self->{'magic_data'}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift @_ || $self->{'magic_data'}
shift @_ $self->{'magic_data'}
Truth table0%0/3
A || B
A= shift @_B= $self->{'magic_data'}
ABresult
1X1
011
000
713 0 my $depth = shift( @_ );
714 0 my $magic = $self->{magic};
715  
716 0 my $io = $magic->{io};
717 # A ref to an array containing a magic line's components
718 0 my $entry = [];
719 0 my $line = '';
720  
721 # Buffered last line
722 0 $line = $magic->{buffer};
723 0 while(1)
724 {
725 0 ▶ $line = '' if( !defined( $line ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
726 0 ▶ if( $line =~ /^\#/ || $line =~ /^[[:blank:]\h]*$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\#/ or $line =~ /^[[:blank:]\h]*$/) 0 0 0
Condition0%0/3
$line =~ /^\#/ or $line =~ /^[[:blank:]\h]*$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /^\#/ or $line =~ /^[[:blank:]\h]*$/
$line =~ /^\#/ $line =~ /^[[:blank:]\h]*$/
Truth table0%0/3
A or B
A= $line =~ /^\#/B= $line =~ /^[[:blank:]\h]*$/
ABresult
1X1
011
000
727 {
728 0 ▶ last if( $io->eof );
Branch: 1 branch
BranchTrueFalseTotal
if $io->eof 0 0 0
729 0 $line = <$io>;
730 0 $magic->{count}++;
731 0 next;
732 }
733  
734 0 my $this_depth = ( $line =~ /^(>+)/ )[0];
735 0 ▶ $this_depth = '' if( !defined( $this_depth ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this_depth 0 0 0
736 0 ▶ $depth = 0 if( !defined( $depth ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $depth 0 0 0
737  
738 0 ▶ if( length( $this_depth ) > $depth )
Branch: 3 branches
BranchTrueFalseTotal
if (length $this_depth > $depth) { } 0 0 0
elsif (length $this_depth < $depth) { } 0 0 0
elsif (@$entry) { } 0 0 0
739 {
740 0 $magic->{buffer} = $line;
741
742 # Call ourselves recursively. will return the depth of the entry following
743 # the nested group.
744 0 ▶ if( $self->read_magic_entry( $entry->[2], $depth + 1 ) < $depth ||
Branch: 1 branch
BranchTrueFalseTotal
if ($self->read_magic_entry($entry->[2], $depth + 1) < $depth or $io->eof) 0 0 0
Condition0%0/3
$self->read_magic_entry($entry->[2], $depth + 1) < $depth or $io->eof
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->read_magic_entry($entry->[2], $depth + 1) < $depth or $io->eof
$self->read_magic_entry($entry->[2], $depth + 1) < $depth $io->eof
Truth table0%0/3
A or B
A= $self->read_magic_entry($entry->[2], $depth + 1) < $depthB= $io->eof
ABresult
1X1
011
000
745 $io->eof )
746 {
747 0 return;
748 }
749 0 $line = $magic->{buffer};
750 }
751 elsif( length( $this_depth ) < $depth )
752 {
753 0 $magic->{buffer} = $line;
754 0 return( length( $this_depth ) );
755 }
756 elsif( @$entry )
757 {
758 # Already have an entry. This is not a continuation. Save this line for the
759 # next call and exit.
760 0 $magic->{buffer} = $line;
761 0 return( length( $this_depth ) );
762 }
763 else
764 {
765 # We're here if the number of '>' is the same as the current depth and we
766 # haven't read a magic line yet.
767  
768 # Create temp entry later, if we ever get around to evaluating this condition,
769 # we'll replace @$entry with the results from parse_magic_line.
770 0 $entry = [ $line , $magic->{count}, [] ];
771  
772 # Add to list
773 0 push( @$data, $entry );
774  
775 # Read the next line
776 0 ▶ last if( $io->eof() );
Branch: 1 branch
BranchTrueFalseTotal
if $io->eof 0 0 0
777 0 $line = <$io>;
778 0 my $tmp = $line;
779 0 $tmp =~ s/\n$//gs;
780 0 $magic->{count}++;
781 }
782 # print( STDERR "$line" );
783 }
784 }
785  
786 sub with_data
787 0 ▶{
Subroutine with_data: not called CC=6 SCAR=37.4
Pod documented
788 0 my $self = shift( @_ );
789 0 my $data = shift( @_ );
790 0 my $type = undef();
791
792 0 ▶ return if( length( $data ) <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if length $data <= 0 0 0 0
793
794 # Truncate data
795 0 $data = substr( $data, 0, 0x8564 );
796
797 0 ▶ if( _is_binary( $data ) )
Branch: 1 branch
BranchTrueFalseTotal
if (_is_binary($data)) { } 0 0 0
798 {
799 0 $type = 'application/octet-stream';
800 }
801 else
802 {
803 # In BSD's version, there's an effort to search from more specific to less,
804 # but I don't do that.
805 0 my( $token, %val );
806 0 foreach my $type ( keys( %{$self->{SPECIALS}} ) )
807 {
808 0 my $token = '(' . ( join( '|', sort{ length( $a ) <=> length( $b ) } @{$self->{SPECIALS}->{ $type } } ) ) . ')';
809 0 my $tdata = $data;
810 0 ▶ if( $tdata =~ /$token/mg )
Branch: 1 branch
BranchTrueFalseTotal
if ($tdata =~ /$token/gm) 0 0 0
811 {
812 0 $val{ $type } = pos( $tdata );
813 }
814 }
815 # Search latest match
816 0 ▶ if( scalar( keys( %val ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %val) 0 0 0
817 {
818 0 my @skeys = sort{ $val{ $a } <=> $val{ $b } } keys( %val );
819 0 $type = $skeys[0];
820 }
821
822 # ALLDONE:
823 # $type = 'text/plain' if( !defined( $type ) );
824 }
825 # $type = 'text/plain' if( !defined( $type ) );
826 0 return( $type );
827 }
828  
829 sub with_filename
830 0 ▶{
Subroutine with_filename: not called CC=6 SCAR=37.4
Pod documented
831 0 my $self = shift( @_ );
832 0 my $fname = shift( @_ );
833 0 my $type = '';
834
835 0 my $file = $fname;
836 0 $fname =~ s/^.*\///;
837 0 for my $regex ( keys( %{$self->{FILE_EXTS}} ) )
838 {
839 0 ▶ if( $fname =~ /$regex/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($fname =~ /$regex/i) 0 0 0
840 {
841 0 ▶ if( ( defined( $type ) && $type !~ /;/ ) ||
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and not $type =~ /;/ or not defined $type) 0 0 0
Condition0%0/6
defined $type and not $type =~ /;/ or not defined $type
l!l&&r!l&&!r
000
defined $type and not $type =~ /;/
!ll&&!rl&&r
000
MC/DC0%0/3
defined $type and not $type =~ /;/ or not defined $type
defined $type not $type =~ /;/ not defined $type
Truth table0%0/5
A and not(B) or not(C)
A= defined $typeB= not $type =~ /;/C= not defined $type
ABCresult
11X1
0X11
1011
0X00
1000
842 !defined( $type ) )
843 {
844 # has no x-type param
845 0 $type = $self->{FILE_EXTS}->{ $regex };
846 }
847 }
848 }
849 0 return( $type );
850 }
851  
852 sub with_magic
853 0 ▶{
Subroutine with_magic: not called CC=8 SCAR=42.8
Pod documented
854 0 my $self = shift( @_ );
855 0 my $data = shift( @_ );
856 0 my $desc = '';
857 0 my $type = '';
858
859 0 ▶ return( 'application/octet-stream' ) if( length( $data ) <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if length $data <= 0 0 0 0
860
861 # 3) Iterate over each magic entry.
862 0 ▶ for( my $m = 0; $m <= $#{ $self->{magic_data} }; $m++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($m <= $#{$$self{"magic_data"};}) 0 0 0
863 {
864 # Check if the m-th magic entry matches and if it does, then $desc will contain
865 # an updated description
866 0 ▶ if( $self->_magic_match_str( $self->{magic_data}->[ $m ], \$desc, $data ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_magic_match_str($self->{"magic_data"}[$m], \$desc, $data)) 0 0 0
867 {
868 0 ▶ if( defined( $desc ) && $desc ne '' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $desc and $desc ne "") 0 0 0
Condition0%0/3
defined $desc and $desc ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $desc and $desc ne ""
defined $desc $desc ne ""
Truth table0%0/3
A and B
A= defined $descB= $desc ne ""
ABresult
0X0
100
111
869 {
870 0 $type = $desc;
871 0 last;
872 }
873 }
874
875 # Read another entry from the magic file if we've exhausted all the entries
876 # already buffered. read_magic_entry will add to the end of the array if
877 # there are more.
878 0 ▶ if( $m == $#{ $self->{magic_data} } && !$self->{magic}->{io}->eof() )
Branch: 1 branch
BranchTrueFalseTotal
if ($m == $#{$$self{"magic_data"};} and not $self->{"magic"}{"io"}->eof) 0 0 0
Condition0%0/3
$m == $#{$$self{"magic_data"};} and not $self->{"magic"}{"io"}->eof
!ll&&!rl&&r
000
MC/DC0%0/2
$m == $#{$$self{"magic_data"};} and not $self->{"magic"}{"io"}->eof
$m == $#{$$self{"magic_data"};} not $self->{"magic"}{"io"}->eof
Truth table0%0/3
A and not(B)
A= $m == $#{$$self{"magic_data"};}B= not $self->{"magic"}{"io"}->eof
ABresult
0X0
100
111
879 {
880 0 $self->read_magic_entry();
881 }
882 }
883 0 return( $type );
884 }
885  
886 sub _is_binary
887 0 ▶{
Subroutine _is_binary: not called CC=3 SCAR=24.8
888 0 my( $data ) = @_;
889 0 my $len = length( $data );
890 # Exclude TAB, ESC, nl, cr
891 0 my $count = ( $data =~ tr/[\x00-\x08\x0b-\x0c\x0e-\x1a\x1c-\x1f]// );
892 # No contents
893 0 ▶ return( 1 ) if( $len <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $len <= 0 0 0 0
894 # Binary
895 0 ▶ return( 1 ) if( ( $count / $len ) > 0.1 );
Branch: 1 branch
BranchTrueFalseTotal
if $count / $len > "0.1" 0 0 0
896 0 return( 0 );
897 }
898  
899 # Compare the magic item with the filehandle.
900 # If success, print info and return true, otherwise return undef.
901 #
902 # This is called recursively if an item has subitems.
903 sub _magic_match
904 87 ▶{
Subroutine _magic_match: called CC=42 SCAR=57.7
905 87 my $self = shift( @_ );
906 # $io is the file handle of the file being inspected
907 87 my( $item, $p_desc, $io ) = @_;
908
909 # $item could be undef if we ran into troubles while reading the entry.
910 87 ▶ return unless( defined( $item ) && ref( $item // '' ) eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item and ref($item // "") eq "ARRAY" 0 87 87
Condition40%2/5
defined $item and ref($item // "") eq "ARRAY"
!ll&&!rl&&r
0087
$item // ""
l!l
870
MC/DC0%0/2
defined $item and ref($item // "") eq "ARRAY"
defined $item $item
Truth table33%1/3
A and B
A= defined $itemB= $item
ABresult
0X0
100
111
911
912 # Delayed evaluation. If this is our first time considering this item, then parse out
913 # its structure. @$item is just the raw string, line number, and subtests until we
914 # need the real info. This saves time otherwise wasted parsing unused subtests.
915 87 ▶ $item = $self->parse_magic_line( @$item ) if( @$item == 3 );
Branch: 1 branch
BranchTrueFalseTotal
if @$item == 3 0 87 87
916
917 # $io is not defined if -c. That way we always return false for every item which
918 # allows reading/checking the entire magic file.
919 87 ▶ return( $self->error( "File handle is not defined." ) ) unless( defined( $io ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $io 0 87 87
920 # return unless( defined( fileno( $io ) ) );
921 # return unless( Scalar::Util::openhandle( $io ) );
922
923 87 my( $offtype, $offset, $numbytes, $type, $mask, $op, $testval, $template, $message, $subtests ) = @$item;
924 87 $self->{trick}++;
925 87 ▶ if( $self->{trick} > 186 && $self->{trick} < 192 )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"trick"} > 186 and $self->{"trick"} < 192) 0 87 87
Condition33%1/3
$self->{"trick"} > 186 and $self->{"trick"} < 192
!ll&&!rl&&r
8700
MC/DC0%0/2
$self->{"trick"} > 186 and $self->{"trick"} < 192
$self->{"trick"} > 186 $self->{"trick"} < 192
Truth table33%1/3
A and B
A= $self->{"trick"} > 186B= $self->{"trick"} < 192
ABresult
0X0
100
111
926 {
927 0 my $c = -1;
928 }
929 # Bytes from file
930 87 my $data = '';
931  
932 # Set to true if match
933 87 my $match = 0;
934
935 # offset = [ off1, sz, template, off2 ] for indirect offset
936 87 ▶ if( $offtype == 1 )
Branch: 2 branches
BranchTrueFalseTotal
if ($offtype == 1) { } 0 87 87
elsif ($offtype == 2) { } 0 87 87
937 {
938 0 my( $off1, $sz, $template, $off2 ) = @$offset;
939 0 ▶ $io->seek( $off1, 0 ) || return( $self->error( "Unable to seek to offset $off1 in file" ) );
Branch: 1 branch
BranchTrueFalseTotal
$io->seek($off1, 0) or return $self->error("Unable to seek to offset $off1 in file") 0 0 0
940 # return( $self->error( "Unable to read $sz bytes of data from file. Buffer is only ", length( $data ), " bytes." ) ) if( $io->read( $data, $sz ) != $sz );
941 0 ▶ return if( $io->read( $data, $sz ) != $sz );
Branch: 1 branch
BranchTrueFalseTotal
if $io->read($data, $sz) != $sz 0 0 0
942 0 $off2 += unpack( $template, $data );
943 0 ▶ $io->seek( $off2, 0 ) || return( $self->error( "Unable to seek to offset $off2 in file." ) );
Branch: 1 branch
BranchTrueFalseTotal
$io->seek($off2, 0) or return $self->error("Unable to seek to offset $off2 in file.") 0 0 0
944 }
945 elsif( $offtype == 2 )
946 {
947 # Relative offsets from previous seek
948 0 ▶ $io->seek( $offset, 1 ) || return( $self->error( "Unable to seek to offset $offset in file" ) );
Branch: 1 branch
BranchTrueFalseTotal
$io->seek($offset, 1) or return $self->error("Unable to seek to offset $offset in file") 0 0 0
949 }
950 else
951 {
952 # Absolute offset
953 87 ▶ $io->seek( $offset, 0 ) || return( $self->error( "Unable to seek to offset $offset in file" ) );
Branch: 1 branch
BranchTrueFalseTotal
$io->seek($offset, 0) or return $self->error("Unable to seek to offset $offset in file") 87 0 87
954 }
955
956 87 ▶ if( $type eq 'string' )
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'string') { } 78 9 87
957 {
958 # Read the length of the match string unless the comparison is
959 # '>' ($numbytes == 0), in which case read to the next null or "\n".
960 # (that's what BSD's file does)
961 78 ▶ if( $numbytes > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($numbytes > 0) { } 78 0 78
962 {
963 # return( $self->error( "Unable to read $numbytes bytes of data from file. Buffer is only ", length( $data ), " bytes." ) ) if( $io->read( $data, $numbytes ) != $numbytes );
964 78 ▶ return if( $io->read( $data, $numbytes ) != $numbytes );
Branch: 1 branch
BranchTrueFalseTotal
if $io->read($data, $numbytes) != $numbytes 11 67 78
965 }
966 else
967 {
968 0 my $ch = $io->getc();
969 0 ▶ while( defined( $ch ) && $ch ne "\0" && $ch ne "\n" )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ch and $ch ne "\0" and $ch ne "\n") 0 0 0
Condition0%0/6
defined $ch and $ch ne "\0" and $ch ne "\n"
!ll&&!rl&&r
000
defined $ch and $ch ne "\0"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $ch and $ch ne "\0" and $ch ne "\n"
defined $ch $ch ne "\0" $ch ne "\n"
Truth table0%0/4
A and B and C
A= defined $chB= $ch ne "\0"C= $ch ne "\n"
ABCresult
0XX0
10X0
1100
1111
970 {
971 0 $data .= $ch;
972 0 $ch = $io->getc();
973 }
974 }
975
976 # Now do the comparison
977 67 ▶ if( $op eq '=' )
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq '=') { } 67 0 67
elsif ($op eq '<') { } 0 0 0
elsif ($op eq '>') { } 0 0 0
978 {
979 67 $match = ( $data eq $testval );
980 }
981 elsif( $op eq '<' )
982 {
983 0 $match = ( $data lt $testval );
984 }
985 elsif( $op eq '>' )
986 {
987 0 $match = ( $data gt $testval );
988 }
989 # Else bogus op, but don't die, just skip
990 67 ▶ if( $self->check_magic )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check_magic) 0 67 67
991 {
992 0 print( STDERR "STRING: $data $op $testval => $match\n" );
993 }
994 }
995 else
996 {
997 # Numeric
998 # Read up to 4 bytes
999 # return( $self->error( "Unable to read $numbytes bytes of data from file. Buffer is only ", length( $data ), " bytes." ) ) if( $io->read( $data, $numbytes ) != $numbytes );
1000 9 ▶ return if( $io->read( $data, $numbytes ) != $numbytes );
Branch: 1 branch
BranchTrueFalseTotal
if $io->read($data, $numbytes) != $numbytes 0 9 9
1001
1002 # If template is a ref to an array of 3 letters, then this is an endian number
1003 # which must be first unpacked into an unsigned and then coerced into a signed.
1004 # Is there a better way?
1005 9 ▶ if( ref( $template ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $template) { } 0 9 9
1006 {
1007 0 $data = unpack( $template->[2], pack( $template->[1], unpack( $template->[0], $data ) ) );
1008 }
1009 else
1010 {
1011 9 $data = unpack( $template, $data );
1012 }
1013
1014 # If mask
1015 9 ▶ if( defined( $mask ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mask) 2 7 9
1016 {
1017 2 $data &= $mask;
1018 }
1019
1020 # Now do the check
1021 9 ▶ if( $op eq '=' )
Branch: 7 branches
BranchTrueFalseTotal
if ($op eq '=') { } 8 1 9
elsif ($op eq 'x') { } 0 1 1
elsif ($op eq '!') { } 0 1 1
elsif ($op eq '&') { } 1 0 1
elsif ($op eq '^') { } 0 0 0
elsif ($op eq '<') { } 0 0 0
elsif ($op eq '>') { } 0 0 0
1022 {
1023 8 $match = ( $data == $testval );
1024 }
1025 elsif( $op eq 'x' )
1026 {
1027 0 $match = 1;
1028 }
1029 elsif( $op eq '!' )
1030 {
1031 0 $match = ( $data != $testval );
1032 }
1033 elsif( $op eq '&' )
1034 {
1035 1 $match = ( ( $data & $testval ) == $testval );
1036 }
1037 elsif( $op eq '^' )
1038 {
1039 0 $match = ( ( ~$data & $testval ) == $testval );
1040 }
1041 elsif( $op eq '<' )
1042 {
1043 0 $match = ( $data < $testval );
1044 }
1045 elsif( $op eq '>' )
1046 {
1047 0 $match = ( $data > $testval );
1048 }
1049 # Else bogus entry that we're ignoring
1050 9 ▶ if( $self->check_magic )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check_magic) 0 9 9
1051 {
1052 0 print( STDERR "NUMERIC: $data $op $testval => $match\n" );
1053 }
1054 }
1055
1056 76 ▶ if( $match )
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 1 75 76
1057 {
1058 # It's pretty common to find "\b" in the message, but sprintf doesn't insert a
1059 # backspace. If it's at the beginning (typical) then don't include separator space.
1060 1 ▶ if( $message =~ s/^\\b// )
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ s/^\\b//) { } 0 1 1
1061 {
1062 0 ▶ $$p_desc .= ( index( $message, '%s' ) != -1 ? sprintf( $message, $data ) : $message );
Branch: 1 branch
BranchTrueFalseTotal
index($message, '%s') != -1 ? : 0 0 0
1063 }
1064 else
1065 {
1066 # $$p_desc .= ' ' . sprintf( $message, $data ) if( $message );
1067 1 ▶ $$p_desc .= ( index( $message, '%s' ) != -1 ? sprintf( $message, $data ) : $message ) if( $message );
Branch: 2 branches
BranchTrueFalseTotal
if $message 1 0 1
index($message, '%s') != -1 ? : 0 1 1
1068 }
1069
1070 1 foreach my $subtest ( @$subtests )
1071 {
1072 0 $self->_magic_match( $subtest, $p_desc, $io );
1073 }
1074 1 return( 1 );
1075 }
1076 }
1077  
1078 sub _magic_match_str
1079 0 ▶{
Subroutine _magic_match_str: not called CC=31 SCAR=69.0
1080 0 my $self = shift( @_ );
1081 0 my( $item, $p_desc, $str ) = @_;
1082 0 my $origstr = $str;
1083
1084 # Delayed evaluation. If this is our first time considering this item, then parse out
1085 # its structure. @$item is just the raw string, line number, and subtests until we
1086 # need the real info. This saves time otherwise wasted parsing unused subtests.
1087 0 ▶ $item = $self->parse_magic_line( @$item ) if( @$item == 3 );
Branch: 1 branch
BranchTrueFalseTotal
if @$item == 3 0 0 0
1088
1089 # $item could be undef if we ran into troubles while reading the entry.
1090 0 ▶ return unless( defined( $item ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item 0 0 0
1091
1092 # $fh is not be defined if -c. That way we always return false for every item which
1093 # allows reading/checking the entire magic file.
1094 0 ▶ return unless( defined( $str ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 0 0
1095 0 ▶ return if( $str eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if $str eq "" 0 0 0
1096
1097 0 my( $offtype, $offset, $numbytes, $type, $mask, $op, $testval, $template, $message, $subtests ) = @$item;
1098 0 ▶ return unless( defined( $op ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $op 0 0 0
1099
1100 # Bytes from file
1101 0 my $data = '';
1102
1103 # Set to true if match
1104 0 my $match = 0;
1105
1106 # offset = [ off1, sz, template, off2 ] for indirect offset
1107 0 ▶ if( $offtype == 1 )
Branch: 2 branches
BranchTrueFalseTotal
if ($offtype == 1) { } 0 0 0
elsif ($offtype == 2) { } 0 0 0
1108 {
1109 0 my( $off1, $sz, $template, $off2 ) = @$offset;
1110 0 ▶ return if( length( $str ) < $off1 );
Branch: 1 branch
BranchTrueFalseTotal
if length $str < $off1 0 0 0
1111 0 $data = pack( "a$sz", $str );
1112 0 $off2 += unpack( $template, $data );
1113 0 ▶ return if( length( $str ) < $off2 );
Branch: 1 branch
BranchTrueFalseTotal
if length $str < $off2 0 0 0
1114 }
1115 elsif( $offtype == 2 )
1116 {
1117 # Unable to handle relative offsets from previous seek
1118 0 return;
1119 }
1120 else
1121 {
1122 # Absolute offset
1123 0 ▶ return if( $offset > length( $str ) );
Branch: 1 branch
BranchTrueFalseTotal
if $offset > length $str 0 0 0
1124 0 $str = substr( $str, $offset );
1125 }
1126
1127 0 ▶ if( $type eq 'string' )
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'string') { } 0 0 0
1128 {
1129 # Read the length of the match string unless the comparison is
1130 # '>' ($numbytes == 0), in which case read to the next null or "\n".
1131 # (that's what BSD's file does)
1132 0 ▶ if( $numbytes > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($numbytes > 0) { } 0 0 0
1133 {
1134 0 $data = pack( "a$numbytes", $str );
1135 }
1136 else
1137 {
1138 0 $str =~ /^(.*)\0|$/;
1139 0 $data = $1;
1140 }
1141  
1142 # Now do the comparison
1143 0 ▶ if( $op eq '=' )
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq '=') { } 0 0 0
elsif ($op eq '<') { } 0 0 0
elsif ($op eq '>') { } 0 0 0
1144 {
1145 0 $match = ( $data eq $testval );
1146 }
1147 elsif( $op eq '<' )
1148 {
1149 0 $match = ( $data lt $testval );
1150 }
1151 elsif( $op eq '>' )
1152 {
1153 0 $match = ( $data gt $testval );
1154 }
1155 # Else bogus op, but don't die, just skip
1156
1157 0 ▶ if( $self->check_magic )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check_magic) 0 0 0
1158 {
1159 0 print( STDERR "STRING: $data $op $testval => $match\n" );
1160 }
1161 }
1162 else
1163 {
1164 # Numeric
1165 # Read up to 4 bytes
1166 0 $data = substr( $str, 0, 4 );
1167
1168 # If template is a ref to an array of 3 letters, then this is an endian number
1169 # which must be first unpacked into an unsigned and then coerced into a signed.
1170 # Is there a better way?
1171 0 ▶ if( ref( $template ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $template) { } 0 0 0
1172 {
1173 0 $data = unpack( $template->[2], pack( $template->[1], unpack( $template->[0], $data ) ) );
1174 }
1175 else
1176 {
1177 0 $data = unpack( $template, $data );
1178 }
1179
1180 # If mask
1181 0 ▶ if( defined( $mask ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mask) 0 0 0
1182 {
1183 0 $data &= $mask;
1184 }
1185
1186 # Now do the check
1187 0 ▶ if( $op eq '=' )
Branch: 7 branches
BranchTrueFalseTotal
if ($op eq '=') { } 0 0 0
elsif ($op eq 'x') { } 0 0 0
elsif ($op eq '!') { } 0 0 0
elsif ($op eq '&') { } 0 0 0
elsif ($op eq '^') { } 0 0 0
elsif ($op eq '<') { } 0 0 0
elsif ($op eq '>') { } 0 0 0
1188 {
1189 0 $match = ( $data == $testval );
1190 }
1191 elsif( $op eq 'x' )
1192 {
1193 0 $match = 1;
1194 }
1195 elsif( $op eq '!' )
1196 {
1197 0 $match = ( $data != $testval );
1198 }
1199 elsif( $op eq '&' )
1200 {
1201 0 $match = ( ( $data & $testval ) == $testval );
1202 }
1203 elsif( $op eq '^' )
1204 {
1205 0 $match = ( ( ~$data & $testval ) == $testval );
1206 }
1207 elsif( $op eq '<' )
1208 {
1209 0 $match = ( $data < $testval );
1210 }
1211 elsif( $op eq '>' )
1212 {
1213 0 $match = ( $data > $testval );
1214 }
1215 # else bogus entry that we're ignoring
1216 0 ▶ if( $self->check_magic )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check_magic) 0 0 0
1217 {
1218 0 print( STDERR "NUMERIC: $data $op $testval => $match\n" );
1219 }
1220 }
1221
1222 0 ▶ if( $match )
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 0 0 0
1223 {
1224 # It's pretty common to find "\b" in the message, but sprintf doesn't insert a
1225 # backspace. If it's at the beginning (typical) then don't include separator space.
1226 0 ▶ if( $message =~ s/^\\b// )
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ s/^\\b//) { } 0 0 0
1227 {
1228 0 $$p_desc .= sprintf( $message, $data );
1229 }
1230 else
1231 {
1232 # $$p_desc .= ' ' . sprintf( $message, $data ) if( $message );
1233 0 ▶ $$p_desc .= sprintf( $message, $data ) if( $message );
Branch: 1 branch
BranchTrueFalseTotal
if $message 0 0 0
1234 }
1235 0 foreach my $subtest ( @$subtests )
1236 {
1237 # Finish evaluation when matched.
1238 0 $self->_magic_match_str( $subtest, $p_desc, $origstr );
1239 }
1240 0 return( 1 );
1241 }
1242 }
1243  
1244 # Obsolete routines
1245 sub add_specials
1246 0 ▶{
Subroutine add_specials: not called CC=1 SCAR=6.9
Pod undocumented
1247 0 my $self = shift( @_ );
1248 0 my $type = shift( @_ );
1249 0 $self->{SPECIALS}->{ $type } = [ @_ ];
1250 0 return( $self );
1251 }
1252  
1253 sub add_file_exts
1254 0 ▶{
Subroutine add_file_exts: not called CC=1 SCAR=6.9
Pod undocumented
1255 0 my $self = shift( @_ );
1256 0 my $filepat = shift( @_ );
1257 0 my $type = shift( @_ );
1258 0 $self->{FILE_EXTS}->{ $filepat } = $type;
1259 0 return( $self );
1260 }
1261  
1262 sub add_magic_entry
1263 0 ▶{
Subroutine add_magic_entry: not called CC=1 SCAR=6.9
Pod undocumented
1264 0 my $self = shift( @_ );
1265 0 my $entry = shift( @_ );
1266 0 unshift( @{$self->{magic_data}}, [ $entry, -1, [] ] );
1267 0 return( $self );
1268 }
1269  
1270 9 1;
1271 # NOTE: POD
1272 __END__
« SSI.pm ↑ Summary SSI/URI.pm »