MimeInfo/Magic.pm

filter: Statementstmt 87.9% 131 / 149 Branchbran 63.2% 62 / 98 Conditioncond 30.4% 7 / 23 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 87.5% 14 / 16 Podpod 100% 3 / 3 totaltotal 72.2% 219 / 303 CC 66 SCAR 27.9
CC
66
Cov
74%
_hash_magic
32.6
_escape_bytes
20.0
_check_rule
15.3
CRAP 16.2
« MimeInfo.pm ↑ Summary
1 package File::MimeInfo::Magic;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Fcntl 'SEEK_SET';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use File::BaseDir qw/data_files/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 require File::MimeInfo;
8 4 require Exporter;
9  
10 4 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
11 4 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 for (qw/extensions describe globs inodetype default/) {
13 20 *{$_} = \&{"File::MimeInfo::$_"};
14 }
15 }
16  
17 4 our @ISA = qw(Exporter File::MimeInfo);
18 4 our @EXPORT = qw(mimetype);
19 4 our @EXPORT_OK = qw(extensions describe globs inodetype magic);
20 4 our $VERSION = '0.37';
21 4 our $DEBUG;
22  
23 4 our $_hashed = 0;
24 4 our $max_buffer = 32;
25 4 our (@magic_80, @magic);
26 # @magic_80 and @magic are used to store the parse tree of magic data
27 # @magic_80 contains magic rules with priority 80 and higher, @magic the rest
28 # $max_buffer contains the maximum number of chars to be buffered from a non-seekable
29 # filehandle in order to do magic mimetyping
30  
31 8 ▶sub mimetype {
Subroutine mimetype: called CC=11 SCAR=26.9
Pod documented
32 8 my $file = pop;
33 8 ▶ croak 'subroutine "mimetype" needs a filename as argument' unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 8 8
34  
35 8 ▶ return magic($file) || default($file) if ref $file;
Branch: 1 branch
BranchTrueFalseTotal
if ref $file 0 8 8
Condition0%0/3
magic($file) || default($file)
l!l&&r!l&&!r
000
MC/DC0%0/2
magic($file) || default($file)
magic($file) default($file)
Truth table0%0/3
A || B
A= magic($file)B= default($file)
ABresult
1X1
011
000
36 8 ▶ return &File::MimeInfo::mimetype($file) unless -s $file and -r _;
Branch: 1 branch
BranchTrueFalseTotal
unless -s $file and -r _ 0 8 8
Condition33%1/3
-s $file and -r _
!ll&&!rl&&r
008
MC/DC0%0/2
-s $file and -r _
-s $file -r _
Truth table33%1/3
A and B
A= -s $fileB= -r _
ABresult
0X0
100
111
37  
38 8 my ($mimet, $fh);
39 8 ▶ return $mimet if $mimet = inodetype($file);
Branch: 1 branch
BranchTrueFalseTotal
if $mimet = inodetype($file) 0 8 8
40  
41 8 ($mimet, $fh) = _magic($file, \@magic_80); # high priority rules
42 8 ▶ return $mimet if $mimet;
Branch: 1 branch
BranchTrueFalseTotal
if $mimet 3 5 8
43  
44 5 ▶ return $mimet if $mimet = globs($file);
Branch: 1 branch
BranchTrueFalseTotal
if $mimet = globs($file) 0 5 5
45  
46 5 ($mimet, $fh) = _magic($fh, \@magic); # lower priority rules
47 5 ▶ close $fh if ref $fh;
Branch: 1 branch
BranchTrueFalseTotal
if ref $fh 5 0 5
48  
49 5 ▶ return $mimet if $mimet;
Branch: 1 branch
BranchTrueFalseTotal
if $mimet 3 2 5
50 2 return default($file);
51 }
52  
53 8 ▶sub magic {
Subroutine magic: called CC=6 SCAR=19.9
Pod documented
54 8 my $file = pop;
55 8 ▶ croak 'subroutine "magic" needs a filename as argument' unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 8 8
56 8 ▶ return undef unless ref($file) || -s $file;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file or -s $file 0 8 8
Condition33%1/3
ref $file or -s $file
l!l&&r!l&&!r
080
MC/DC0%0/2
ref $file or -s $file
ref $file -s $file
Truth table33%1/3
A or B
A= ref $fileB= -s $file
ABresult
1X1
011
000
57 8 ▶ print STDERR "> Checking all magic rules\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::MimeInfo::Magic::DEBUG 0 8 8
58  
59 8 my ($mimet, $fh) = _magic($file, \@magic_80, \@magic);
60 8 ▶ close $fh unless ref $file;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file 8 0 8
61  
62 8 return $mimet;
63 }
64  
65 21 ▶sub _magic {
Subroutine _magic: called CC=8 SCAR=20.8
66 21 my ($file, @rules) = @_;
67 21 ▶ _rehash() unless $_hashed;
Branch: 1 branch
BranchTrueFalseTotal
unless $File::MimeInfo::Magic::_hashed 1 20 21
68  
69 21 my $fh;
70 21 ▶ unless (ref $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 5 16 21
71 16 ▶ open $fh, '<', $file or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or return undef 16 0 16
72 16 binmode $fh;
73 }
74 5 else { $fh = $file }
75  
76 21 for my $type (map @$_, @rules) {
77 62 for (2..$#$type) {
78 212 ▶ next unless _check_rule($$type[$_], $fh, 0);
Branch: 1 branch
BranchTrueFalseTotal
unless _check_rule($type->[$_], $fh, 0) 200 12 212
79 12 ▶ close $fh unless ref $file;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file 9 3 12
80 12 return ($$type[1], $fh);
81 }
82 }
83 9 return (undef, $fh);
84 }
85  
86 216 ▶sub _check_rule {
Subroutine _check_rule: called CC=10 SCAR=27.3
87 216 my ($ref, $fh, $lev) = @_;
88 216 my $line;
89  
90 # Read
91 216 ▶ if (ref $fh eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq 'GLOB') { } 216 0 216
92 216 seek($fh, $$ref[0], SEEK_SET); # seek offset
93 216 read($fh, $line, $$ref[1]); # read max length
94 }
95 else { # allowing for IO::Something
96 0 $fh->seek($$ref[0], SEEK_SET); # seek offset
97 0 $fh->read($line, $$ref[1]); # read max length
98 }
99  
100 # Match regex
101 216 ▶ $line = unpack 'b*', $line if $$ref[2]; # unpack to bits if using mask
Branch: 1 branch
BranchTrueFalseTotal
if $ref->[2] 16 200 216
102 216 ▶ return undef unless $line =~ $$ref[3]; # match regex
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /$ref->[3]/ 200 16 216
103 16 ▶ print STDERR '>', '>'x$lev, ' Value "', _escape_bytes($2),
Branch: 1 branch
BranchTrueFalseTotal
if $File::MimeInfo::Magic::DEBUG 0 16 16
104 '" at offset ', $$ref[1]+length($1),
105 " matches at $$ref[4]\n"
106 if $DEBUG;
107 16 ▶ return 1 unless $#$ref > 4;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref->$#* > 4 12 4 16
108  
109 # Check nested rules and recurs
110 4 for (5..$#$ref) {
111 4 ▶ return 1 if _check_rule($$ref[$_], $fh, $lev+1);
Branch: 1 branch
BranchTrueFalseTotal
if _check_rule($ref->[$_], $fh, $lev + 1) 4 0 4
112 }
113 0 ▶ print STDERR "> Failed nested rules\n" if $DEBUG && ! $lev;
Branch: 1 branch
BranchTrueFalseTotal
if $File::MimeInfo::Magic::DEBUG and not $lev 0 0 0
Condition0%0/3
$File::MimeInfo::Magic::DEBUG and not $lev
!ll&&!rl&&r
000
MC/DC0%0/2
$File::MimeInfo::Magic::DEBUG and not $lev
$File::MimeInfo::Magic::DEBUG not $lev
Truth table0%0/3
A and not(B)
A= $File::MimeInfo::Magic::DEBUGB= not $lev
ABresult
0X0
100
111
114 0 return 0;
115 }
116  
117 0 ▶sub rehash {
Subroutine rehash: not called CC=1 SCAR=6.9
Pod documented
118 0 &File::MimeInfo::rehash();
119 0 &_rehash();
120 #use Data::Dumper;
121 #print Dumper \@magic_80, \@magic;
122 }
123  
124 1 ▶sub _rehash {
Subroutine _rehash: called CC=6 SCAR=18.4
125 1 local $_; # limit scope of $_ ... :S
126 1 ($max_buffer, @magic_80, @magic) = (32); # clear data
127 my @magicfiles = @File::MimeInfo::DIRS
128 1 ▶ ? ( grep {-e $_ && -r $_}
Branch: 1 branch
BranchTrueFalseTotal
@File::MimeInfo::DIRS ? : 1 0 1
Condition33%1/3
-e $_ && -r $_
!ll&&!rl&&r
001
MC/DC0%0/2
-e $_ && -r $_
-e $_ -r $_
Truth table33%1/3
A && B
A= -e $_B= -r $_
ABresult
0X0
100
111
129 map "$_/magic", @File::MimeInfo::DIRS )
130 : ( reverse data_files('mime/magic') ) ;
131 1 my @done;
132 1 for my $file (@magicfiles) {
133 0 ▶ next if grep {$file eq $_} @done;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$file eq $_;} @done 0 1 1
134 1 _hash_magic($file);
135 1 push @done, $file;
136 }
137 1 @magic = sort {$$b[0] <=> $$a[0]} @magic;
138 1 ▶ while ($magic[0][0] >= 80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($File::MimeInfo::Magic::magic[0][0] >= 80) 2 1 3
139 2 push @magic_80, shift @magic;
140 }
141 1 $_hashed = 1;
142 }
143  
144 1 ▶sub _hash_magic {
Subroutine _hash_magic: called CC=23 SCAR=34.8
145 1 my $file = shift;
146  
147 1 ▶ open MAGIC, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open MAGIC, "<", $file or croak("Could not open file '${file}' for reading") 1 0 1
148 or croak "Could not open file '$file' for reading";
149 1 binmode MAGIC;
150 1 ▶ <MAGIC> eq "MIME-Magic\x00\n"
Branch: 1 branch
BranchTrueFalseTotal
readline MAGIC eq "MIME-Magic\0\n" or carp("Magic file '${file}' doesn't seem to be a magic file") 1 0 1
151 or carp "Magic file '$file' doesn't seem to be a magic file";
152 1 my $line = 1;
153 1 ▶ while (<MAGIC>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline MAGIC)) 37 1 38
154 37 $line++;
155  
156 37 ▶ if (/^\[(\d+):(.*?)\]\n$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\[(\d+):(.*?)\]\n$/) 6 31 37
157 6 push @magic, [$1,$2];
158 6 next;
159 }
160  
161 s/^(\d*)>(\d+)=(.{2})//s
162 31 ▶ || warn "$file line $line skipped\n" && next;
Branch: 1 branch
BranchTrueFalseTotal
s/^(\d*)>(\d+)=(.{2})//s or warn "$file line $line skipped\n" && (next) 31 0 31
Condition0%0/2
"$file line $line skipped\n" && (next)
!ll
00
MC/DC0%0/1
"$file line $line skipped\n" && (next)
"$file line $line skipped\n"
Truth table0%0/2
A
A= "$file line $line skipped\n"
Aresult
00
11
163 31 my ($i, $o, $l) = ($1, $2, unpack 'n', $3);
164 # indent, offset, value length
165 31 ▶ while (length($_) <= $l) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ <= $l) 0 31 31
166 0 $_ .= <MAGIC>;
167 0 $line++;
168 }
169  
170 31 my $v = substr $_, 0, $l, ''; # value
171  
172 31 ▶ /^(?:&(.{$l}))?(?:~(\d+))?(?:\+(\d+))?\n$/s
Branch: 1 branch
BranchTrueFalseTotal
/^(?:&(.{$l}))?(?:~(\d+))?(?:\+(\d+))?\n$/s or warn "$file line $line skipped\n" && (next) 31 0 31
Condition0%0/2
"$file line $line skipped\n" && (next)
!ll
00
MC/DC0%0/1
"$file line $line skipped\n" && (next)
"$file line $line skipped\n"
Truth table0%0/2
A
A= "$file line $line skipped\n"
Aresult
00
11
173 || warn "$file line $line skipped\n" && next;
174 31 ▶ my ($m, $w, $r) = ($1, $2 || 1, $3 || 1);
Condition100%4/4
$2 || 1
l!l
526
$3 || 1
l!l
229
MC/DC100%1/1
$2 || 1
$2
MC/DC100%1/1
$3 || 1
$3
Truth table100%2/2
A
A= $2
Aresult
11
00
Truth table100%2/2
A
A= $3
Aresult
11
00
175 # mask, word size, range
176 31 my $mdef = defined $m;
177  
178 # possible big endian to little endian conversion
179 # as a bonus perl also takes care of weird endian cases
180 31 ▶ if ( $w != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w != 1) 5 26 31
181 5 my ( $utpl, $ptpl );
182 5 ▶ if ( 2 == $w ) {
Branch: 2 branches
BranchTrueFalseTotal
if (2 == $w) { } 5 0 5
elsif (4 == $w) { } 0 0 0
183 5 $v = pack 'S', unpack 'n', $v;
184 5 ▶ $m = pack 'S', unpack 'n', $m if $mdef;
Branch: 1 branch
BranchTrueFalseTotal
if $mdef 0 5 5
185 }
186 elsif ( 4 == $w ) {
187 0 $v = pack 'L', unpack 'N', $v;
188 0 ▶ $m = pack 'L', unpack 'N', $m if $mdef;
Branch: 1 branch
BranchTrueFalseTotal
if $mdef 0 0 0
189 }
190 else {
191 0 warn "Unsupported word size: $w octets ".
192 " at $file line $line\n"
193 }
194 }
195  
196 31 my $end = $o + $l + $r - 1;
197 31 ▶ $max_buffer = $end if $max_buffer < $end;
Branch: 1 branch
BranchTrueFalseTotal
if $File::MimeInfo::Magic::max_buffer < $end 2 29 31
198 31 ▶ my $ref = $i ? _find_branch($i) : $magic[-1];
Branch: 1 branch
BranchTrueFalseTotal
$i ? : 4 27 31
199 31 $r--; # 1-based => 0-based range for regex
200 31 ▶ $r *= 8 if $mdef; # bytes => bits for matching a mask
Branch: 1 branch
BranchTrueFalseTotal
if $mdef 1 30 31
201 31 ▶ my $reg = '^'
Branch: 2 branches
BranchTrueFalseTotal
$r ? : 2 29 31
$mdef ? : 1 30 31
202 . ( $r ? "(.{0,$r}?)" : '()' )
203 . ( $mdef ? '('. _mask_regex($v, $m) .')'
204 : '('. quotemeta($v) .')' ) ;
205 31 push @$ref, [
206 $o, $end, # offset, offset+length+range
207 $mdef, # boolean for mask
208 qr/$reg/sm, # the regex to match
209 undef # debug data
210 ];
211 31 ▶ $$ref[-1][-1] = "$file line $line" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::MimeInfo::Magic::DEBUG 0 31 31
212 }
213 1 close MAGIC;
214 }
215  
216 4 ▶sub _find_branch { # finds last branch of tree of rules
Subroutine _find_branch: called CC=2 SCAR=6.9
217 4 my $i = shift;
218 4 my $ref = $magic[-1];
219 4 for (1..$i) { $ref = $$ref[-1] }
220 4 return $ref;
221 }
222  
223 1 ▶sub _mask_regex { # build regex based on mask
Subroutine _mask_regex: called CC=3 SCAR=11.0
224 1 my ($v, $m) = @_;
225 1 my @v = split '', unpack "b*", $v;
226 1 my @m = split '', unpack "b*", $m;
227 1 my $re = '';
228 1 for (0 .. $#m) {
229 64 ▶ $re .= $m[$_] ? $v[$_] : '.' ;
Branch: 1 branch
BranchTrueFalseTotal
$m[$_] ? : 56 8 64
230 # If $mask = 1 than ($input && $mask) will be same as $input
231 # If $mask = 0 than ($input && $mask) is always 0
232 # But $mask = 0 only makes sense if $value = 0
233 # So if $mask = 0 we ignore that bit of $input
234 }
235 1 return $re;
236 }
237  
238 0 ▶sub _escape_bytes { # used for debug output
Subroutine _escape_bytes: not called CC=4 SCAR=30.0
239 0 my $string = shift;
240 0 ▶ if ($string =~ /[\x00-\x1F\x7F]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /[\x00-\x1F\x7F]/) 0 0 0
241 $string = join '', map {
242 0 my $o = ord($_);
243 0 ▶ ($o < 32) ? '^' . chr($o + 64) :
Branch: 2 branches
BranchTrueFalseTotal
$o < 32 ? : 0 0 0
$o == 127 ? : 0 0 0
244 ($o == 127) ? '^?' : $_ ;
245 } split '', $string;
246 }
247 0 return $string;
248 }
249  
250 4 1;
251  
252 __END__
« MimeInfo.pm ↑ Summary