lib/Module/Generic/File/Magic.pm

filter: Statementstmt 55.6% 233 / 419 Branchbran 34.6% 77 / 222 Conditioncond 21.0% 27 / 128 MC/DCmcdc 8.1% 6 / 74 Subroutinesub 66.6% 36 / 54 Podpod 100% 26 / 26 totaltotal 43.8% 405 / 923 CC 166 SCAR 33.9
CC
166
Cov
44%
list
90.0
_file_backend_path
90.0
_open_cookie
90.0
CRAP 29.7
« lib/Module/Generic/HeaderValue.pm ↑ Summary lib/Module/Generic/Tie.pm »
1 ##----------------------------------------------------------------------------
2 ## Module Generic - ~/lib/Module/Generic/File/Magic.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2026/03/07
7 ## Modified 2026/03/28
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 Module::Generic::File::Magic;
14 BEGIN
15 3 ▶{
Subroutine BEGIN: called CC=3 SCAR=11.0
16 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 warnings::register_categories( 'Module::Generic' );
19 3 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶ use vars qw( $VERSION $BACKEND @EXPORT_OK %EXPORT_TAGS %LIBMAGIC_FLAGS );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶ use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶ use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶ use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶ use Scalar::Util qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 our $VERSION = 'v0.2.0';
26  
27 # $BACKEND is set by the XS BOOT block at load time:
28 # "xs" - libmagic loaded successfully via dlopen
29 # "json" - libmagic absent; JSON backend + file(1) fallback available
30 # Pre-set so syntax checks without the compiled .so do not die.
31 3 ▶ $BACKEND //= 'json';
Condition50%1/2
$Module::Generic::File::Magic::BACKEND //= "json"
l!l
30
MC/DC0%0/1
$Module::Generic::File::Magic::BACKEND //= "json"
$Module::Generic::File::Magic::BACKEND
Truth table50%1/2
A
A= $Module::Generic::File::Magic::BACKEND
Aresult
11
00
32  
33 # NOTE: libmagic flag constants - mirrored from <magic.h>
34 3 %LIBMAGIC_FLAGS = (
35 MAGIC_NONE => 0x0000000,
36 MAGIC_DEBUG => 0x0000001,
37 MAGIC_SYMLINK => 0x0000002,
38 MAGIC_COMPRESS => 0x0000004,
39 MAGIC_DEVICES => 0x0000008,
40 MAGIC_MIME_TYPE => 0x0000010,
41 MAGIC_CONTINUE => 0x0000020,
42 MAGIC_CHECK => 0x0000040,
43 MAGIC_PRESERVE_ATIME => 0x0000080,
44 MAGIC_RAW => 0x0000100,
45 MAGIC_ERROR => 0x0000200,
46 MAGIC_MIME_ENCODING => 0x0000400,
47 MAGIC_MIME => ( 0x0000010 | 0x0000400 ),
48 MAGIC_APPLE => 0x0000800,
49 MAGIC_EXTENSION => 0x1000000,
50 MAGIC_COMPRESS_TRANSP => 0x2000000,
51 MAGIC_NO_CHECK_COMPRESS => 0x0001000,
52 MAGIC_NO_CHECK_TAR => 0x0002000,
53 MAGIC_NO_CHECK_SOFT => 0x0004000,
54 MAGIC_NO_CHECK_APPTYPE => 0x0008000,
55 MAGIC_NO_CHECK_ELF => 0x0010000,
56 MAGIC_NO_CHECK_TEXT => 0x0020000,
57 MAGIC_NO_CHECK_CDF => 0x0040000,
58 MAGIC_NO_CHECK_TOKENS => 0x0100000,
59 MAGIC_NO_CHECK_ENCODING => 0x0200000,
60 );
61  
62 3 @EXPORT_OK = (
63 keys( %LIBMAGIC_FLAGS ),
64 qw(
65 magic_from_buffer
66 magic_from_file
67 magic_mime_encoding
68 magic_mime_type
69 )
70 );
71 3 $EXPORT_TAGS{all} = [@EXPORT_OK];
72 3 $EXPORT_TAGS{flags} = [keys( %LIBMAGIC_FLAGS )];
73 3 $EXPORT_TAGS{functions} = [qw(
74 magic_from_buffer
75 magic_from_file
76 magic_mime_encoding
77 magic_mime_type
78 )];
79  
80 # Generate constant subs at compile time
81 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
82 3 foreach my $name ( keys( %LIBMAGIC_FLAGS ) )
83 {
84 75 my $val = $LIBMAGIC_FLAGS{$name};
85 0 ▶ *{"Module::Generic::File::Magic::${name}"} = sub () { $val };
Subroutine __ANON__: not called CC=1 SCAR=0.4
86 }
87 };
88  
89 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 # We attempt to load the XS shared library (Generic.so, shared with Module::Generic).
93 # MODULE = Module::Generic in the .xs means the bootstrap symbol is boot_Module__Generic,
94 # which XSLoader resolves by loading auto/Module/Generic/Generic.so.
95 # PACKAGE = Module::Generic::File::Magic means all XS functions land in our namespace.
96 # If the .so is absent, XSLoader croaks - we catch that and stay on the pure-Perl backends.
97 # When the .so is present, its BOOT block sets $BACKEND to "xs".
98 # The .so is shared with Module::Generic which may have already loaded it.
99 # We guard against double-loading by checking if a known Magic XS function
100 # is already installed. If Module::Generic loaded the .so first, the BOOT
101 # block already ran and all magic_* functions are in our namespace.
102 3 require XSLoader;
103 3 ▶if( !$Module::Generic::XS_LOADED )
Branch: 1 branch
BranchTrueFalseTotal
unless ($Module::Generic::XS_LOADED) 0 3 3
104 {
105 eval
106 0 {
107 0 ▶ XSLoader::load( 'Module::Generic', $Module::Generic::VERSION // $VERSION );
Condition0%0/3
$Module::Generic::VERSION // $Module::Generic::File::Magic::VERSION
l!l&&r!l&&!r
000
MC/DC0%0/2
$Module::Generic::VERSION // $Module::Generic::File::Magic::VERSION
$Module::Generic::VERSION $Module::Generic::File::Magic::VERSION
Truth table0%0/3
A // B
A= $Module::Generic::VERSIONB= $Module::Generic::File::Magic::VERSION
ABresult
1X1
011
000
108 0 $Module::Generic::XS_LOADED = 1;
109 };
110 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
111 {
112 # .so absent or failed to load - stay on the pure-Perl backend
113 0 ▶ $BACKEND //= 'json';
Condition0%0/2
$Module::Generic::File::Magic::BACKEND //= "json"
l!l
00
MC/DC0%0/1
$Module::Generic::File::Magic::BACKEND //= "json"
$Module::Generic::File::Magic::BACKEND
Truth table0%0/2
A
A= $Module::Generic::File::Magic::BACKEND
Aresult
11
00
114 }
115 else
116 {
117 # Ensure $BACKEND is initialised in this package's symbol table.
118 # When Module::Generic loads the .so first, the BOOT block runs before
119 # Magic.pm is compiled, so the assignment lands in a not-yet-existing
120 # stash and DynaLoader warns "used only once". Reading it here after
121 # load ensures the symbol is live in our package.
122 0 ▶ $BACKEND //= 'json';
Condition0%0/2
$Module::Generic::File::Magic::BACKEND //= "json"
l!l
00
MC/DC0%0/1
$Module::Generic::File::Magic::BACKEND //= "json"
$Module::Generic::File::Magic::BACKEND
Truth table0%0/2
A
A= $Module::Generic::File::Magic::BACKEND
Aresult
11
00
123 }
124 }
125  
126 # NOTE: Package-level backend caches (populated lazily, shared by all instances)
127  
128 # JSON magic database: loaded on first use, cached for the process lifetime
129 3 my $_json_db = undef; # arrayref of magic entries once loaded
130 3 my $_json_error = undef; # error message if loading failed
131  
132 # Cached path to the file(1) command
133 3 my $_file_cmd = undef; # string path or '' (not found) once searched
134  
135  
136 sub init
137 4 ▶{
Subroutine init: called CC=4 SCAR=14.3
Pod documented
138 4 my $self = shift( @_ );
139 4 ▶ $self->{flags} = MAGIC_NONE unless( defined( $self->{flags} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"flags"} 4 0 4
140 4 ▶ $self->{magic_db} = undef unless( exists( $self->{magic_db} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"magic_db"} 4 0 4
141 # Max bytes to read from a buffer/file for pure-Perl backends.
142 # Default 512; covers all signatures in the JSON database.
143 4 ▶ $self->{max_read} = 512 unless( defined( $self->{max_read} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"max_read"} 4 0 4
144 # Internal xs state
145 4 $self->{_cookie} = undef;
146 4 $self->{_init_flags} = undef;
147 4 return( $self->SUPER::init( @_ ) );
148 }
149  
150 # Returns the active backend: "xs", "json", or "file"
151 1 ▶sub backend : method { return( $BACKEND ) }
Subroutine backend: called CC=1 SCAR=0.0
Pod documented
152  
153 1 ▶sub check : method
Subroutine check: called CC=9 SCAR=40.9
Pod documented
154 {
155 1 my $self = shift( @_ );
156 1 my $file = shift( @_ );
157 1 ▶ return( $self->error( "check() is only available with the xs backend." ) )
Branch: 1 branch
BranchTrueFalseTotal
unless $Module::Generic::File::Magic::BACKEND eq "xs" 1 0 1
158 unless( $BACKEND eq 'xs' );
159 0 ▶ $file = undef unless( defined( $file ) && length( $file ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and length $file 0 0 0
Condition0%0/3
defined $file and length $file
!ll&&!rl&&r
000
MC/DC0%0/2
defined $file and length $file
defined $file length $file
Truth table0%0/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
160 0 ▶ if( defined( $file ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 0 0 0
161 {
162 0 ▶ return( $self->error( "File not found: $file" ) ) unless( -e $file );
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 0 0
163 }
164 0 ▶ my $cookie = $self->_open_cookie || return( $self->pass_error );
Condition0%0/2
$self->_open_cookie || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_open_cookie || (return $self->pass_error)
$self->_open_cookie
Truth table0%0/2
A
A= $self->_open_cookie
Aresult
11
00
165 0 ▶ return( $self->error( "magic_check is not available in this version of libmagic." ) )
Branch: 1 branch
BranchTrueFalseTotal
unless magic_has_check() 0 0 0
166 unless( Module::Generic::File::Magic::magic_has_check() );
167 0 my $rc = Module::Generic::File::Magic::magic_check( $cookie, $file );
168 0 ▶ return( $self->error( sprintf( "magic_check failed: %s", $self->_magic_error_str ) ) ) if( $rc != 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $rc != 0 0 0 0
169 0 return( $self );
170 }
171  
172 0 ▶sub close : method
Subroutine close: not called CC=3 SCAR=24.8
Pod documented
173 {
174 0 my $self = shift( @_ );
175 0 ▶ if( $BACKEND eq 'xs' && defined( $self->{_cookie} ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($Module::Generic::File::Magic::BACKEND eq "xs" and defined $self->{"_cookie"}) 0 0 0
Condition0%0/3
$Module::Generic::File::Magic::BACKEND eq "xs" and defined $self->{"_cookie"}
!ll&&!rl&&r
000
MC/DC0%0/2
$Module::Generic::File::Magic::BACKEND eq "xs" and defined $self->{"_cookie"}
$Module::Generic::File::Magic::BACKEND eq "xs" defined $self->{"_cookie"}
Truth table0%0/3
A and B
A= $Module::Generic::File::Magic::BACKEND eq "xs"B= defined $self->{"_cookie"}
ABresult
0X0
100
111
176 {
177 0 Module::Generic::File::Magic::magic_close( $self->{_cookie} );
178 0 $self->{_cookie} = undef;
179 0 $self->{_init_flags} = undef;
180 }
181 0 return( $self );
182 }
183  
184 1 ▶sub compile : method
Subroutine compile: called CC=8 SCAR=38.3
Pod documented
185 {
186 1 my $self = shift( @_ );
187 1 my $file = shift( @_ );
188 1 ▶ return( $self->error( "compile() is only available with the xs backend." ) )
Branch: 1 branch
BranchTrueFalseTotal
unless $Module::Generic::File::Magic::BACKEND eq "xs" 1 0 1
189 unless( $BACKEND eq 'xs' );
190 0 ▶ return( $self->error( "A file path is required." ) )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and length $file 0 0 0
Condition0%0/3
defined $file and length $file
!ll&&!rl&&r
000
MC/DC0%0/2
defined $file and length $file
defined $file length $file
Truth table0%0/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
191 unless( defined( $file ) && length( $file ) );
192 0 ▶ return( $self->error( "File not found: $file" ) ) unless( -e $file );
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 0 0
193 0 ▶ my $cookie = $self->_open_cookie || return( $self->pass_error );
Condition0%0/2
$self->_open_cookie || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_open_cookie || (return $self->pass_error)
$self->_open_cookie
Truth table0%0/2
A
A= $self->_open_cookie
Aresult
11
00
194 0 ▶ return( $self->error( "magic_compile is not available in this version of libmagic." ) )
Branch: 1 branch
BranchTrueFalseTotal
unless magic_has_compile() 0 0 0
195 unless( Module::Generic::File::Magic::magic_has_compile() );
196 0 my $rc = Module::Generic::File::Magic::magic_compile( $cookie, $file );
197 0 ▶ return( $self->error( sprintf( "magic_compile failed: %s", $self->_magic_error_str ) ) ) if( $rc != 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $rc != 0 0 0 0
198 0 return( $self );
199 }
200  
201 16 ▶sub flags : method
Subroutine flags: called CC=10 SCAR=32.1
Pod documented
202 {
203 16 my $self = shift( @_ );
204 16 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 14 2 16
205 {
206 14 my $val = shift( @_ );
207 14 ▶ return( $self->error( "flags must be an integer bitmask." ) )
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($val) 0 14 14
208 unless( looks_like_number( $val ) );
209 14 $self->{flags} = int( $val );
210 14 ▶ if( $BACKEND eq 'xs' && defined( $self->{_cookie} ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($Module::Generic::File::Magic::BACKEND eq "xs" and defined $self->{"_cookie"}) 0 14 14
Condition33%1/3
$Module::Generic::File::Magic::BACKEND eq "xs" and defined $self->{"_cookie"}
!ll&&!rl&&r
1400
MC/DC0%0/2
$Module::Generic::File::Magic::BACKEND eq "xs" and defined $self->{"_cookie"}
$Module::Generic::File::Magic::BACKEND eq "xs" defined $self->{"_cookie"}
Truth table33%1/3
A and B
A= $Module::Generic::File::Magic::BACKEND eq "xs"B= defined $self->{"_cookie"}
ABresult
0X0
100
111
211 {
212 0 my $rc = Module::Generic::File::Magic::magic_setflags( $self->{_cookie}, $self->{flags} );
213 0 ▶ if( $rc == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($rc == -1) 0 0 0
214 {
215 0 return( $self->error( sprintf( "magic_setflags failed: %s", $self->_magic_error_str ) ) );
216 }
217 0 $self->{_init_flags} = $self->{flags};
218 }
219 14 return( $self );
220 }
221 2 ▶ if( $BACKEND eq 'xs' && defined( $self->{_cookie} )
Branch: 1 branch
BranchTrueFalseTotal
if ($Module::Generic::File::Magic::BACKEND eq "xs" and defined $self->{"_cookie"} and magic_has_getflags()) 0 2 2
Condition33%2/6
$Module::Generic::File::Magic::BACKEND eq "xs" and defined $self->{"_cookie"} and magic_has_getflags()
!ll&&!rl&&r
200
$Module::Generic::File::Magic::BACKEND eq "xs" and defined $self->{"_cookie"}
!ll&&!rl&&r
200
MC/DC0%0/3
$Module::Generic::File::Magic::BACKEND eq "xs" and defined $self->{"_cookie"} and magic_has_getflags()
$Module::Generic::File::Magic::BACKEND eq "xs" defined $self->{"_cookie"} magic_has_getflags()
Truth table25%1/4
A and B and C
A= $Module::Generic::File::Magic::BACKEND eq "xs"B= defined $self->{"_cookie"}C= magic_has_getflags()
ABCresult
0XX0
10X0
1100
1111
222 && Module::Generic::File::Magic::magic_has_getflags() )
223 {
224 0 my $live = Module::Generic::File::Magic::magic_getflags( $self->{_cookie} );
225 0 ▶ return( $live ) if( defined( $live ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $live 0 0 0
226 }
227 2 return( $self->{flags} );
228 }
229  
230 7 ▶sub from_buffer : method
Subroutine from_buffer: called CC=11 SCAR=38.8
Pod documented
231 {
232 7 my $self = shift( @_ );
233 7 my $buffer = shift( @_ );
234 7 ▶ return( $self->error( "A buffer (scalar) is required." ) )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $buffer 0 7 7
235 unless( defined( $buffer ) );
236 7 ▶ if( utf8::is_utf8( $buffer ) )
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($buffer)) 1 6 7
237 {
238 1 ▶ utf8::downgrade( $buffer, 1 )
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($buffer, 1) or return $self->error("Buffer contains characters above U+00FF. Encode it explicitly (e.g. Encode::encode('UTF-8', \$buf)) before passing to from_buffer().") 0 1 1
239 or return( $self->error(
240 "Buffer contains characters above U+00FF. "
241 . "Encode it explicitly (e.g. Encode::encode('UTF-8', \$buf)) "
242 . "before passing to from_buffer()."
243 ) );
244 }
245 6 ▶ if( $BACKEND eq 'xs' )
Branch: 1 branch
BranchTrueFalseTotal
if ($Module::Generic::File::Magic::BACKEND eq 'xs') { } 0 6 6
246 {
247 0 ▶ my $cookie = $self->_open_cookie || return( $self->pass_error );
Condition0%0/2
$self->_open_cookie || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_open_cookie || (return $self->pass_error)
$self->_open_cookie
Truth table0%0/2
A
A= $self->_open_cookie
Aresult
11
00
248 0 my $result = Module::Generic::File::Magic::magic_buffer( $cookie, $buffer );
249 0 ▶ return( $self->error( sprintf( "magic_buffer failed: %s", $self->_magic_error_str ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 0 0
250 unless( defined( $result ) );
251 # magic_buffer() is unreliable for end-of-file anchored formats (e.g. ZIP)
252 # on some libmagic versions (5.39, 5.46). Fall back to magic_file() via a
253 # temp file when the result is the generic catch-all type.
254 0 ▶ if( $result eq 'application/octet-stream' )
Branch: 1 branch
BranchTrueFalseTotal
if ($result eq "application/octet-stream") 0 0 0
255 {
256 0 my $tmp = $self->new_tempfile( cleanup => 1 );
257 0 ▶ $tmp->unload( $buffer, { binmode => 'raw', autoflush => 1 }) ||
Branch: 1 branch
BranchTrueFalseTotal
$tmp->unload($buffer, {"binmode", "raw", "autoflush", 1}) or return $self->pass_error 0 0 0
258 return( $self->pass_error );
259 0 my $file_result = Module::Generic::File::Magic::magic_file( $cookie, "$tmp" );
260 0 ▶ $result = $file_result
Branch: 1 branch
BranchTrueFalseTotal
if defined $file_result and $file_result ne "application/octet-stream" 0 0 0
Condition0%0/3
defined $file_result and $file_result ne "application/octet-stream"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $file_result and $file_result ne "application/octet-stream"
defined $file_result $file_result ne "application/octet-stream"
Truth table0%0/3
A and B
A= defined $file_resultB= $file_result ne "application/octet-stream"
ABresult
0X0
100
111
261 if( defined( $file_result ) && $file_result ne 'application/octet-stream' );
262 }
263 0 return( $result );
264 }
265 else
266 {
267 6 return( $self->_pure_perl_detect_buffer( $buffer, $self->{flags} ) );
268 }
269 }
270  
271 11 ▶sub from_file : method
Subroutine from_file: called CC=8 SCAR=27.7
Pod documented
272 {
273 11 my $self = shift( @_ );
274 11 my $file = shift( @_ );
275 11 ▶ return( $self->error( "A file path is required." ) )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and length $file 0 11 11
Condition33%1/3
defined $file and length $file
!ll&&!rl&&r
0011
MC/DC0%0/2
defined $file and length $file
defined $file length $file
Truth table33%1/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
276 unless( defined( $file ) && length( $file ) );
277 11 ▶ return( $self->error( "File not found: $file" ) ) unless( -e $file );
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 1 10 11
278 10 ▶ if( $BACKEND eq 'xs' )
Branch: 1 branch
BranchTrueFalseTotal
if ($Module::Generic::File::Magic::BACKEND eq 'xs') { } 0 10 10
279 {
280 0 ▶ my $cookie = $self->_open_cookie || return( $self->pass_error );
Condition0%0/2
$self->_open_cookie || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_open_cookie || (return $self->pass_error)
$self->_open_cookie
Truth table0%0/2
A
A= $self->_open_cookie
Aresult
11
00
281 0 my $result = Module::Generic::File::Magic::magic_file( $cookie, $file );
282 0 ▶ return( $self->error( sprintf( "magic_file failed: %s", $self->_magic_error_str ) ) ) unless( defined( $result ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 0 0
283 0 return( $result );
284 }
285 else
286 {
287 # Read up to max_read bytes from the file
288 10 ▶ my $buffer = $self->_read_file_head( $file ) // return( $self->pass_error );
Condition50%1/2
$self->_read_file_head($file) // (return $self->pass_error)
l!l
100
MC/DC0%0/1
$self->_read_file_head($file) // (return $self->pass_error)
$self->_read_file_head($file)
Truth table50%1/2
A
A= $self->_read_file_head($file)
Aresult
11
00
289 10 return( $self->_pure_perl_detect_buffer( $buffer, $self->{flags}, $file ) );
290 }
291 }
292  
293 1 ▶sub from_filehandle : method
Subroutine from_filehandle: called CC=7 SCAR=28.0
Pod documented
294 {
295 1 my $self = shift( @_ );
296 1 my $fh = shift( @_ );
297 1 ▶ return( $self->error( "A filehandle is required." ) )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 1 1
298 unless( defined( $fh ) );
299 1 ▶ if( $BACKEND eq 'xs' )
Branch: 1 branch
BranchTrueFalseTotal
if ($Module::Generic::File::Magic::BACKEND eq 'xs') { } 0 1 1
300 {
301 0 my $fd = fileno( $fh );
302 0 ▶ return( $self->error(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fd 0 0 0
303 "Could not obtain a file descriptor from the supplied filehandle. "
304 . "In-memory filehandles are not supported by the xs backend."
305 ) ) unless( defined( $fd ) );
306 0 ▶ my $cookie = $self->_open_cookie || return( $self->pass_error );
Condition0%0/2
$self->_open_cookie || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_open_cookie || (return $self->pass_error)
$self->_open_cookie
Truth table0%0/2
A
A= $self->_open_cookie
Aresult
11
00
307 0 my $result = Module::Generic::File::Magic::magic_descriptor( $cookie, $fd );
308 0 ▶ return( $self->error( sprintf( "magic_descriptor failed: %s", $self->_magic_error_str ) ) ) unless( defined( $result ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 0 0
309 0 return( $result );
310 }
311 else
312 {
313 # Read from the filehandle
314 1 my $buf = '';
315 1 ▶ my $max = $self->{max_read} // 512;
Condition50%1/2
$self->{"max_read"} // 512
l!l
10
MC/DC0%0/1
$self->{"max_read"} // 512
$self->{"max_read"}
Truth table50%1/2
A
A= $self->{"max_read"}
Aresult
11
00
316 1 read( $fh, $buf, $max );
317 1 return( $self->_pure_perl_detect_buffer( $buf, $self->{flags} ) );
318 }
319 }
320  
321 0 ▶sub list : method
Subroutine list: not called CC=9 SCAR=45.0
Pod documented
322 {
323 0 my $self = shift( @_ );
324 0 my $file = shift( @_ );
325 0 ▶ return( $self->error( "list() is only available with the xs backend." ) )
Branch: 1 branch
BranchTrueFalseTotal
unless $Module::Generic::File::Magic::BACKEND eq "xs" 0 0 0
326 unless( $BACKEND eq 'xs' );
327 0 ▶ $file = undef unless( defined( $file ) && length( $file ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and length $file 0 0 0
Condition0%0/3
defined $file and length $file
!ll&&!rl&&r
000
MC/DC0%0/2
defined $file and length $file
defined $file length $file
Truth table0%0/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
328 0 ▶ if( defined( $file ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 0 0 0
329 {
330 0 ▶ return( $self->error( "File not found: $file" ) ) unless( -e $file );
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 0 0
331 }
332 0 ▶ my $cookie = $self->_open_cookie || return( $self->pass_error );
Condition0%0/2
$self->_open_cookie || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_open_cookie || (return $self->pass_error)
$self->_open_cookie
Truth table0%0/2
A
A= $self->_open_cookie
Aresult
11
00
333 0 ▶ return( $self->error( "magic_list is not available in this version of libmagic." ) )
Branch: 1 branch
BranchTrueFalseTotal
unless magic_has_list() 0 0 0
334 unless( Module::Generic::File::Magic::magic_has_list() );
335 0 my $rc = Module::Generic::File::Magic::magic_list( $cookie, $file );
336 0 ▶ return( $self->error( sprintf( "magic_list failed: %s", $self->_magic_error_str ) ) ) if( $rc != 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $rc != 0 0 0 0
337 0 return( $self );
338 }
339  
340 0 ▶sub magic_db : method
Subroutine magic_db: not called CC=5 SCAR=34.0
Pod documented
341 {
342 0 my $self = shift( @_ );
343 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
344 {
345 0 my $db = shift( @_ );
346 0 ▶ if( defined( $db ) && length( $db ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $db and length $db) { } 0 0 0
Condition0%0/3
defined $db and length $db
!ll&&!rl&&r
000
MC/DC0%0/2
defined $db and length $db
defined $db length $db
Truth table0%0/3
A and B
A= defined $dbB= length $db
ABresult
0X0
100
111
347 {
348 0 ▶ return( $self->error( "Magic database file not found: $db" ) )
Branch: 1 branch
BranchTrueFalseTotal
unless -e $db 0 0 0
349 unless( -e $db );
350 }
351 else
352 {
353 0 $db = undef;
354 }
355 0 $self->{magic_db} = $db;
356 0 $self->close;
357 0 return( $self );
358 }
359 0 return( $self->{magic_db} );
360 }
361  
362 4 ▶sub max_read : method
Subroutine max_read: called CC=4 SCAR=14.2
Pod documented
363 {
364 4 my $self = shift( @_ );
365 4 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 2 4
366 {
367 2 my $val = shift( @_ );
368 2 ▶ return( $self->error( "max_read must be a positive integer." ) )
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($val) and int $val > 0 0 2 2
Condition33%1/3
&looks_like_number($val) and int $val > 0
!ll&&!rl&&r
002
MC/DC0%0/2
&looks_like_number($val) and int $val > 0
&looks_like_number($val) int $val > 0
Truth table33%1/3
A and B
A= &looks_like_number($val)B= int $val > 0
ABresult
0X0
100
111
369 unless( looks_like_number( $val ) && int( $val ) > 0 );
370 2 $self->{max_read} = int( $val );
371 2 return( $self );
372 }
373 2 return( $self->{max_read} );
374 }
375  
376 # NOTE: mime_encoding_from_*
377 0 ▶sub mime_encoding_from_buffer : method
Subroutine mime_encoding_from_buffer: not called CC=1 SCAR=6.9
Pod documented
378 {
379 0 my $self = shift( @_ );
380 0 return( $self->_with_flags( MAGIC_MIME_ENCODING, 'from_buffer', @_ ) );
381 }
382  
383 1 ▶sub mime_encoding_from_file : method
Subroutine mime_encoding_from_file: called CC=1 SCAR=0.0
Pod documented
384 {
385 1 my $self = shift( @_ );
386 1 return( $self->_with_flags( MAGIC_MIME_ENCODING, 'from_file', @_ ) );
387 }
388  
389 0 ▶sub mime_encoding_from_filehandle : method
Subroutine mime_encoding_from_filehandle: not called CC=1 SCAR=6.9
Pod documented
390 {
391 0 my $self = shift( @_ );
392 0 return( $self->_with_flags( MAGIC_MIME_ENCODING, 'from_filehandle', @_ ) );
393 }
394  
395 # NOTE: mime_from_*
396 0 ▶sub mime_from_buffer : method
Subroutine mime_from_buffer: not called CC=1 SCAR=6.9
Pod documented
397 {
398 0 my $self = shift( @_ );
399 0 return( $self->_with_flags( MAGIC_MIME, 'from_buffer', @_ ) );
400 }
401  
402 2 ▶sub mime_from_file : method
Subroutine mime_from_file: called CC=1 SCAR=0.0
Pod documented
403 {
404 2 my $self = shift( @_ );
405 2 return( $self->_with_flags( MAGIC_MIME, 'from_file', @_ ) );
406 }
407  
408 0 ▶sub mime_from_filehandle : method
Subroutine mime_from_filehandle: not called CC=1 SCAR=6.9
Pod documented
409 {
410 0 my $self = shift( @_ );
411 0 return( $self->_with_flags( MAGIC_MIME, 'from_filehandle', @_ ) );
412 }
413  
414 # NOTE: mime_type_from_*
415 0 ▶sub mime_type_from_buffer : method
Subroutine mime_type_from_buffer: not called CC=1 SCAR=6.9
Pod documented
416 {
417 0 my $self = shift( @_ );
418 0 return( $self->_with_flags( MAGIC_MIME_TYPE, 'from_buffer', @_ ) );
419 }
420  
421 1 ▶sub mime_type_from_file : method
Subroutine mime_type_from_file: called CC=1 SCAR=0.0
Pod documented
422 {
423 1 my $self = shift( @_ );
424 1 return( $self->_with_flags( MAGIC_MIME_TYPE, 'from_file', @_ ) );
425 }
426  
427 0 ▶sub mime_type_from_filehandle : method
Subroutine mime_type_from_filehandle: not called CC=1 SCAR=6.9
Pod documented
428 {
429 0 my $self = shift( @_ );
430 0 return( $self->_with_flags( MAGIC_MIME_TYPE, 'from_filehandle', @_ ) );
431 }
432  
433 0 ▶sub version : method
Subroutine version: not called CC=4 SCAR=30.0
Pod documented
434 {
435 0 my $self = shift( @_ );
436 0 ▶ return( undef ) unless( $BACKEND eq 'xs' );
Branch: 1 branch
BranchTrueFalseTotal
unless $Module::Generic::File::Magic::BACKEND eq "xs" 0 0 0
437 0 ▶ return( undef ) unless( Module::Generic::File::Magic::magic_has_version() );
Branch: 1 branch
BranchTrueFalseTotal
unless magic_has_version() 0 0 0
438 0 my $v = Module::Generic::File::Magic::magic_version();
439 0 ▶ return( undef ) unless( defined( $v ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
440 0 return( sprintf( '%d.%02d', int( $v / 100 ), $v % 100 ) );
441 }
442  
443 sub DESTROY
444 4 ▶{
Subroutine DESTROY: called CC=4 SCAR=17.9
445 4 my $self = shift( @_ );
446 $self->close
447 4 ▶ if( defined( $self ) && ref( $self ) && defined( $self->{_cookie} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self and ref $self and defined $self->{"_cookie"} 0 4 4
Condition33%2/6
defined $self and ref $self and defined $self->{"_cookie"}
!ll&&!rl&&r
040
defined $self and ref $self
!ll&&!rl&&r
004
MC/DC0%0/3
defined $self and ref $self and defined $self->{"_cookie"}
defined $self ref $self defined $self->{"_cookie"}
Truth table25%1/4
A and B and C
A= defined $selfB= ref $selfC= defined $self->{"_cookie"}
ABCresult
0XX0
10X0
1100
1111
448 }
449  
450 # NOTE: Exportable procedural interface
451 sub magic_from_buffer
452 1 ▶{
Subroutine magic_from_buffer: called CC=3 SCAR=11.4
Pod documented
453 1 my $buffer = shift( @_ );
454 1 ▶ my $flags = @_ ? shift( @_ ) : MAGIC_NONE;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 0 1
455 1 ▶ my $obj = __PACKAGE__->new( flags => $flags )
Condition50%1/2
__PACKAGE__->new('flags', $flags) || die(__PACKAGE__->error . "\n")
l!l
10
MC/DC0%0/1
__PACKAGE__->new('flags', $flags) || die(__PACKAGE__->error . "\n")
__PACKAGE__->new('flags', $flags)
Truth table50%1/2
A
A= __PACKAGE__->new('flags', $flags)
Aresult
11
00
456 || die( __PACKAGE__->error . "\n" );
457 1 return( $obj->from_buffer( $buffer ) );
458 }
459  
460 sub magic_from_file
461 0 ▶{
Subroutine magic_from_file: not called CC=3 SCAR=24.8
Pod documented
462 0 my $file = shift( @_ );
463 0 ▶ my $flags = @_ ? shift( @_ ) : MAGIC_NONE;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
464 0 ▶ my $obj = __PACKAGE__->new( flags => $flags )
Condition0%0/2
__PACKAGE__->new('flags', $flags) || die(__PACKAGE__->error . "\n")
l!l
00
MC/DC0%0/1
__PACKAGE__->new('flags', $flags) || die(__PACKAGE__->error . "\n")
__PACKAGE__->new('flags', $flags)
Truth table0%0/2
A
A= __PACKAGE__->new('flags', $flags)
Aresult
11
00
465 || die( __PACKAGE__->error . "\n" );
466 0 return( $obj->from_file( $file ) );
467 }
468  
469 sub magic_mime_encoding
470 0 ▶{
Subroutine magic_mime_encoding: not called CC=2 SCAR=17.9
Pod documented
471 0 my $file = shift( @_ );
472 0 ▶ my $obj = __PACKAGE__->new( flags => MAGIC_MIME_ENCODING )
Condition0%0/2
__PACKAGE__->new('flags', 1024) || die(__PACKAGE__->error . "\n")
l!l
00
MC/DC0%0/1
__PACKAGE__->new('flags', 1024) || die(__PACKAGE__->error . "\n")
__PACKAGE__->new('flags', 1024)
Truth table0%0/2
A
A= __PACKAGE__->new('flags', 1024)
Aresult
11
00
473 || die( __PACKAGE__->error . "\n" );
474 0 return( $obj->from_file( $file ) );
475 }
476  
477 sub magic_mime_type
478 1 ▶{
Subroutine magic_mime_type: called CC=2 SCAR=7.1
Pod documented
479 1 my $file = shift( @_ );
480 1 ▶ my $obj = __PACKAGE__->new( flags => MAGIC_MIME_TYPE )
Condition50%1/2
__PACKAGE__->new('flags', 16) || die(__PACKAGE__->error . "\n")
l!l
10
MC/DC0%0/1
__PACKAGE__->new('flags', 16) || die(__PACKAGE__->error . "\n")
__PACKAGE__->new('flags', 16)
Truth table50%1/2
A
A= __PACKAGE__->new('flags', 16)
Aresult
11
00
481 || die( __PACKAGE__->error . "\n" );
482 1 return( $obj->from_file( $file ) );
483 }
484  
485 # _find_file_command()
486 # Locates the file(1) executable; result cached after first call.
487 sub _find_file_command
488 0 ▶{
Subroutine _find_file_command: not called CC=8 SCAR=42.8
489 0 my $self = shift( @_ );
490 # Return cached result: defined string = found path, empty string = not found
491 0 ▶ return( $_file_cmd ) if( defined( $_file_cmd ) && length( $_file_cmd ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $_file_cmd and length $_file_cmd 0 0 0
Condition0%0/3
defined $_file_cmd and length $_file_cmd
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_file_cmd and length $_file_cmd
defined $_file_cmd length $_file_cmd
Truth table0%0/3
A and B
A= defined $_file_cmdB= length $_file_cmd
ABresult
0X0
100
111
492 0 ▶ return( undef ) if( defined( $_file_cmd ) && !length( $_file_cmd ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $_file_cmd and not length $_file_cmd 0 0 0
Condition0%0/3
defined $_file_cmd and not length $_file_cmd
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_file_cmd and not length $_file_cmd
defined $_file_cmd not length $_file_cmd
Truth table0%0/3
A and not(B)
A= defined $_file_cmdB= not length $_file_cmd
ABresult
0X0
100
111
493 0 foreach my $candidate (
494 qw(
495 /usr/bin/file
496 /bin/file
497 /usr/local/bin/file
498 /opt/homebrew/bin/file
499 /opt/local/bin/file
500 )
501 )
502 {
503 0 ▶ if( -x $candidate )
Branch: 1 branch
BranchTrueFalseTotal
if (-x $candidate) 0 0 0
504 {
505 0 $_file_cmd = $candidate;
506 0 return( $_file_cmd );
507 }
508 }
509 0 require File::Which;
510 0 my $try_bin = File::Which::which( 'file' );
511 0 ▶ if( -x( $try_bin ) )
Branch: 1 branch
BranchTrueFalseTotal
if (-x $try_bin) 0 0 0
512 {
513 0 $_file_cmd = $try_bin;
514 0 return( $_file_cmd );
515 }
516 0 $_file_cmd = '';
517 0 return( undef );
518 }
519  
520 # _file_backend_path( $path, $flags )
521 # Invokes file(1) and parses its output.
522 sub _file_backend_path
523 0 ▶{
Subroutine _file_backend_path: not called CC=9 SCAR=45.0
524 0 my $self = shift( @_ );
525 0 my $path = shift( @_ );
526 0 ▶ my $flags = shift( @_ ) // MAGIC_NONE;
Condition0%0/2
shift @_ // 0
l!l
00
MC/DC0%0/1
shift @_ // 0
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
527  
528 0 my $file_cmd = $self->_find_file_command;
529 0 ▶ return( $self->error( "The file(1) command was not found. Install it with: sudo apt-get install file" ) )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file_cmd 0 0 0
530 unless( defined( $file_cmd ) );
531  
532 0 my $want_mime = ( $flags & ( MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING | MAGIC_MIME ) );
533 0 my @cmd = ( $file_cmd, '-b' );
534 0 ▶ push( @cmd, '-i' ) if( $want_mime );
Branch: 1 branch
BranchTrueFalseTotal
if $want_mime 0 0 0
535 0 ▶ push( @cmd, '-z' ) if( $flags & MAGIC_COMPRESS );
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 4 0 0 0
536 0 ▶ push( @cmd, '-L' ) if( $flags & MAGIC_SYMLINK );
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 2 0 0 0
537 0 push( @cmd, $path );
538  
539 0 my $output;
540 {
541 0 local $SIG{PIPE} = 'IGNORE';
542 0 ▶ open( my $fh, '-|', @cmd ) or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", @cmd or return $self->error("Could not run file(1): $!") 0 0 0
543 return( $self->error( "Could not run file(1): $!" ) );
544 0 $output = do { local $/; <$fh> };
545 0 close( $fh );
546 }
547 0 ▶ return( $self->error( "file(1) produced no output for: $path" ) )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output and length $output 0 0 0
Condition0%0/3
defined $output and length $output
!ll&&!rl&&r
000
MC/DC0%0/2
defined $output and length $output
defined $output length $output
Truth table0%0/3
A and B
A= defined $outputB= length $output
ABresult
0X0
100
111
548 unless( defined( $output ) && length( $output ) );
549 0 chomp( $output );
550 0 return( $self->_parse_file_output( $output, $flags ) );
551 }
552  
553 # _load_json_db()
554 # Loads and caches the magic.json database.
555 # Returns the arrayref on success, undef on failure.
556 sub _load_json_db
557 17 ▶{
Subroutine _load_json_db: called CC=12 SCAR=28.0
558 17 my $self = shift( @_ );
559 17 ▶ return( $_json_db ) if( defined( $_json_db ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $_json_db 15 2 17
560 2 ▶ return( undef ) if( defined( $_json_error ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $_json_error 0 2 2
561  
562 # Locate magic.json relative to this module's path
563 2 my $pm_path = $INC{'Module/Generic/File/Magic.pm'};
564 2 my $json_path;
565 2 ▶ if( defined( $pm_path ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pm_path) 2 0 2
566 {
567 # $pm_path is .../lib/Module/Generic/File/Magic.pm
568 # JSON is at .../lib/Module/Generic/File/magic.json
569 2 ( $json_path = $pm_path ) =~ s/Magic\.pm$/magic.json/;
570 }
571  
572 2 ▶ unless( defined( $json_path ) && -r( $json_path ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $json_path and -r $json_path) 0 2 2
Condition33%1/3
defined $json_path and -r $json_path
!ll&&!rl&&r
002
MC/DC0%0/2
defined $json_path and -r $json_path
defined $json_path -r $json_path
Truth table33%1/3
A and B
A= defined $json_pathB= -r $json_path
ABresult
0X0
100
111
573 {
574 0 ▶ $_json_error = "magic.json not found (looked near ${\($pm_path//'?')})";
Condition0%0/2
$pm_path // "?"
l!l
00
MC/DC0%0/1
$pm_path // "?"
$pm_path
Truth table0%0/2
A
A= $pm_path
Aresult
11
00
575 0 return( undef );
576 }
577  
578 2 local $@;
579 my $json_text = do
580 2 {
581 open( my $fh, '<:utf8', $json_path ) or
582 0 ▶ do{ $_json_error = "Cannot open $json_path: $!"; return( undef ) };
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:utf8", $json_path or do { $_json_error = "Cannot open ${json_path}: $!"; return undef } 2 0 2
583 2 local $/;
584 2 <$fh>;
585 };
586  
587 my $data = eval
588 2 {
589 2 require JSON;
590 2 JSON->new->utf8->decode( $json_text );
591 };
592 2 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
593 {
594 0 $_json_error = "Failed to parse magic.json: $@";
595 0 return( undef );
596 }
597  
598 # Pre-compile hex strings to byte strings for faster matching
599 2 foreach my $entry ( @$data )
600 {
601 1260 foreach my $m ( @{$entry->{matches}} )
602 {
603 2266 $m->{_bytes} = pack( 'H*', $m->{bytes} );
604 2266 ▶ $m->{_mask} = defined( $m->{mask} ) ? pack( 'H*', $m->{mask} ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $m->{'mask'} ? : 38 2228 2266
605 2266 _precompile_match( $m );
606 }
607 }
608  
609 2 $_json_db = $data;
610 2 return( $_json_db );
611 }
612  
613 # _magic_error_str()
614 sub _magic_error_str
615 0 ▶{
Subroutine _magic_error_str: not called CC=4 SCAR=30.0
616 0 my $self = shift( @_ );
617 0 ▶ return( '' ) unless( $BACKEND eq 'xs' && defined( $self->{_cookie} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $Module::Generic::File::Magic::BACKEND eq "xs" and defined $self->{"_cookie"} 0 0 0
Condition0%0/3
$Module::Generic::File::Magic::BACKEND eq "xs" and defined $self->{"_cookie"}
!ll&&!rl&&r
000
MC/DC0%0/2
$Module::Generic::File::Magic::BACKEND eq "xs" and defined $self->{"_cookie"}
$Module::Generic::File::Magic::BACKEND eq "xs" defined $self->{"_cookie"}
Truth table0%0/3
A and B
A= $Module::Generic::File::Magic::BACKEND eq "xs"B= defined $self->{"_cookie"}
ABresult
0X0
100
111
618 0 my $msg = Module::Generic::File::Magic::magic_error( $self->{_cookie} );
619 0 ▶ return( defined( $msg ) ? $msg : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $msg ? : 0 0 0
620 }
621  
622 # _match_bytes( $buf, $match_entry ) -> bool
623 # Tests a single match entry (and its sub-matches) against $buf.
624 sub _match_bytes
625 13711 ▶{
Subroutine _match_bytes: called CC=12 SCAR=24.9
626 13711 my( $buf, $m ) = @_;
627 13711 my $want = $m->{_bytes};
628 13711 my $mask = $m->{_mask};
629 13711 my $offset = $m->{offset};
630 13711 ▶ my $range = $m->{range} // 0;
Condition50%1/2
$m->{"range"} // 0
l!l
137110
MC/DC0%0/1
$m->{"range"} // 0
$m->{"range"}
Truth table50%1/2
A
A= $m->{"range"}
Aresult
11
00
631 13711 my $wlen = length( $want );
632 13711 my $blen = length( $buf );
633  
634 13711 my $end = $offset + $range;
635 13711 ▶ $end = $blen - $wlen if( $end > $blen - $wlen );
Branch: 1 branch
BranchTrueFalseTotal
if $end > $blen - $wlen 406 13305 13711
636  
637 13711 ▶ foreach( my $pos = $offset; $pos <= $end; $pos++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($pos <= $end) 296233 13700 309933
638 {
639 296233 ▶ last if( $pos + $wlen > $blen );
Branch: 1 branch
BranchTrueFalseTotal
if $pos + $wlen > $blen 0 296233 296233
640 296233 my $chunk = substr( $buf, $pos, $wlen );
641 296233 ▶ if( defined( $mask ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mask) { } 248 295985 296233
642 {
643 # Apply mask: (buf & mask) == (want & mask)
644 # Since want is already stored masked (if mask was applied at build time),
645 # we just need: (chunk & mask) == want
646 248 my $masked = $chunk & $mask;
647 248 ▶ next unless( $masked eq $want );
Branch: 1 branch
BranchTrueFalseTotal
unless $masked eq $want 240 8 248
648 }
649 else
650 {
651 295985 ▶ next unless( $chunk eq $want );
Branch: 1 branch
BranchTrueFalseTotal
unless $chunk eq $want 295888 97 295985
652 }
653 # Parent matched - now check AND sub-matches
654 105 ▶ if( $m->{and} )
Branch: 1 branch
BranchTrueFalseTotal
if ($m->{'and'}) { } 96 9 105
655 {
656 96 my $all_ok = 1;
657 96 foreach my $sub ( @{$m->{and}} )
658 {
659 96 ▶ unless( _match_bytes( $buf, $sub ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (_match_bytes($buf, $sub)) 94 2 96
660 {
661 94 $all_ok = 0;
662 94 last;
663 }
664 }
665 96 ▶ return(1) if( $all_ok );
Branch: 1 branch
BranchTrueFalseTotal
if $all_ok 2 94 96
666 }
667 else
668 {
669 9 return(1);
670 }
671 }
672 13700 return(0);
673 }
674  
675 # _open_cookie()
676 # Returns the current magic_t cookie, opening a new one if needed.
677 # Only called when $BACKEND eq "xs".
678 sub _open_cookie
679 0 ▶{
Subroutine _open_cookie: not called CC=9 SCAR=45.0
680 0 my $self = shift( @_ );
681 0 ▶ my $flags = $self->{flags} // MAGIC_NONE;
Condition0%0/2
$self->{"flags"} // 0
l!l
00
MC/DC0%0/1
$self->{"flags"} // 0
$self->{"flags"}
Truth table0%0/2
A
A= $self->{"flags"}
Aresult
11
00
682  
683 0 ▶ if( defined( $self->{_cookie} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_cookie"} and defined $self->{"_init_flags"} and $self->{"_init_flags"} == $flags) 0 0 0
Condition0%0/6
defined $self->{"_cookie"} and defined $self->{"_init_flags"} and $self->{"_init_flags"} == $flags
!ll&&!rl&&r
000
defined $self->{"_cookie"} and defined $self->{"_init_flags"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $self->{"_cookie"} and defined $self->{"_init_flags"} and $self->{"_init_flags"} == $flags
defined $self->{"_cookie"} defined $self->{"_init_flags"} $self->{"_init_flags"} == $flags
Truth table0%0/4
A and B and C
A= defined $self->{"_cookie"}B= defined $self->{"_init_flags"}C= $self->{"_init_flags"} == $flags
ABCresult
0XX0
10X0
1100
1111
684 defined( $self->{_init_flags} ) &&
685 $self->{_init_flags} == $flags
686 )
687 {
688 0 return( $self->{_cookie} );
689 }
690  
691 0 $self->close;
692  
693 0 my $cookie = Module::Generic::File::Magic::magic_open( $flags );
694 0 ▶ return( $self->error( "magic_open failed: could not allocate a magic cookie." ) )
Branch: 1 branch
BranchTrueFalseTotal
unless $cookie 0 0 0
695 unless( $cookie );
696  
697 0 my $db = $self->{magic_db};
698 0 ▶ my $rc = Module::Generic::File::Magic::magic_load( $cookie, defined( $db ) ? $db : undef );
Branch: 1 branch
BranchTrueFalseTotal
defined $db ? : 0 0 0
699 0 ▶ if( $rc != 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($rc != 0) 0 0 0
700 {
701 0 ▶ my $err = Module::Generic::File::Magic::magic_error( $cookie ) // 'unknown error';
Condition0%0/2
magic_error($cookie) // "unknown error"
l!l
00
MC/DC0%0/1
magic_error($cookie) // "unknown error"
magic_error($cookie)
Truth table0%0/2
A
A= magic_error($cookie)
Aresult
11
00
702 0 Module::Generic::File::Magic::magic_close( $cookie );
703 0 return( $self->error( "magic_load failed: $err" ) );
704 }
705  
706 0 $self->{_cookie} = $cookie;
707 0 $self->{_init_flags} = $flags;
708 0 return( $cookie );
709 }
710  
711 # _parse_file_output( $output, $flags ) -> string
712 # Parses the output of file(1) according to the requested flags.
713 sub _parse_file_output
714 0 ▶{
Subroutine _parse_file_output: not called CC=7 SCAR=40.3
715 0 my $self = shift( @_ );
716 0 my $output = shift( @_ );
717 0 ▶ my $flags = shift( @_ ) // MAGIC_NONE;
Condition0%0/2
shift @_ // 0
l!l
00
MC/DC0%0/1
shift @_ // 0
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
718  
719 0 ▶ my $is_mime_type = ( $flags & MAGIC_MIME_TYPE ) && !( $flags & MAGIC_MIME_ENCODING );
Condition0%0/3
$flags & 16 && !($flags & 1024)
!ll&&!rl&&r
000
MC/DC0%0/2
$flags & 16 && !($flags & 1024)
$flags & 16 !($flags & 1024)
Truth table0%0/3
A && not(B)
A= $flags & 16B= !($flags & 1024)
ABresult
0X0
100
111
720 0 ▶ my $is_mime_encoding = ( $flags & MAGIC_MIME_ENCODING ) && !( $flags & MAGIC_MIME_TYPE );
Condition0%0/3
$flags & 1024 && !($flags & 16)
!ll&&!rl&&r
000
MC/DC0%0/2
$flags & 1024 && !($flags & 16)
$flags & 1024 !($flags & 16)
Truth table0%0/3
A && not(B)
A= $flags & 1024B= !($flags & 16)
ABresult
0X0
100
111
721  
722 0 ▶ if( $is_mime_type )
Branch: 2 branches
BranchTrueFalseTotal
if ($is_mime_type) { } 0 0 0
elsif ($is_mime_encoding) { } 0 0 0
723 {
724 0 ( my $type = $output ) =~ s/\s*;.*$//;
725 0 return( $type );
726 }
727 elsif( $is_mime_encoding )
728 {
729 0 ▶ return( $1 ) if( $output =~ /charset=(\S+)/ );
Branch: 1 branch
BranchTrueFalseTotal
if $output =~ /charset=(\S+)/ 0 0 0
730 0 return( 'binary' );
731 }
732 0 return( $output );
733 }
734  
735 # _precompile_match( $m )
736 # Recursively pre-compiles hex strings in match entries to byte strings.
737 sub _precompile_match
738 3080 ▶{
Subroutine _precompile_match: called CC=4 SCAR=13.9
739 3080 my $m = shift( @_ );
740 3080 ▶ if( $m->{and} )
Branch: 1 branch
BranchTrueFalseTotal
if ($m->{"and"}) 580 2500 3080
741 {
742 580 foreach my $sub ( @{$m->{and}} )
743 {
744 814 $sub->{_bytes} = pack( 'H*', $sub->{bytes} );
745 814 ▶ $sub->{_mask} = defined( $sub->{mask} ) ? pack( 'H*', $sub->{mask} ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $sub->{'mask'} ? : 36 778 814
746 814 _precompile_match( $sub );
747 }
748 }
749 }
750  
751 # _pure_perl_detect_buffer( $buf, $flags, $filepath )
752 # Runs the JSON -> text-heuristic -> file(1) detection chain on a byte buffer.
753 # $filepath is optional and used only for file(1) fallback.
754 sub _pure_perl_detect_buffer
755 17 ▶{
Subroutine _pure_perl_detect_buffer: called CC=9 SCAR=30.7
756 17 my $self = shift( @_ );
757 17 my $buf = shift( @_ );
758 17 ▶ my $flags = shift( @_ ) // MAGIC_NONE;
Condition50%1/2
shift @_ // 0
l!l
170
MC/DC0%0/1
shift @_ // 0
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
759 17 my $filepath = shift( @_ ); # optional
760  
761 17 my $want_mime = ( $flags & ( MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING | MAGIC_MIME ) );
762  
763 # NOTE: Level 2: JSON database
764 17 my $mime_type = $self->_run_json_detection( $buf );
765  
766 # NOTE: Level 2.5: text heuristic
767 # If no binary signature matched, classify the buffer as text/plain or
768 # application/octet-stream based on byte content. This matches the
769 # behaviour of libmagic for unrecognised file types:
770 # - >85% printable ASCII (0x09/0x0A/0x0D/0x20–0x7E) -> text/plain
771 # - otherwise -> application/octet-stream
772 # We only apply this when $filepath is set (from_file) so that
773 # from_buffer() for unknown binary data still falls through to file(1).
774 17 ▶ unless( defined( $mime_type ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mime_type) 8 9 17
775 {
776 8 ▶ if( length( $buf ) > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf > 0) 8 0 8
777 {
778 8 my $printable = ( $buf =~ tr/
779  
780 -~// );
781 8 ▶ if( $printable / length( $buf ) > 0.85 )
Branch: 2 branches
BranchTrueFalseTotal
if ($printable / length($buf) > 0.85) { } 8 0 8
elsif (defined $filepath) { } 0 0 0
782 {
783 8 $mime_type = 'text/plain';
784 }
785 elsif( defined( $filepath ) )
786 {
787 0 $mime_type = 'application/octet-stream';
788 }
789 }
790 }
791  
792 # NOTE: Level 3: file(1) subprocess
793 17 ▶ unless( defined( $mime_type ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mime_type) 0 17 17
794 {
795 # Use the file path directly if available; otherwise write a temp file
796 0 my $path = $filepath;
797 0 my $tmp;
798 0 ▶ unless( defined( $path ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 0 0 0
799 {
800 eval
801 0 {
802 0 $tmp = File::Temp->new( UNLINK => 1, SUFFIX => '.bin' );
803 0 binmode( $tmp );
804 0 print( $tmp $buf );
805 0 $tmp->flush;
806 0 $path = $tmp->filename;
807 };
808 0 ▶ return( $self->error( "Could not create temporary file: $@" ) ) if( $@ );
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
809 }
810 0 return( $self->_file_backend_path( $path, $flags ) );
811 }
812  
813 # NOTE: Format result according to flags
814 17 return( $self->_format_mime_result( $mime_type, $flags ) );
815 }
816  
817 # _format_mime_result( $mime_type, $flags ) -> string
818 # Given a MIME type string, returns the appropriate representation
819 # for the requested flags (type only, encoding only, or full).
820 sub _format_mime_result
821 17 ▶{
Subroutine _format_mime_result: called CC=8 SCAR=20.9
822 17 my $self = shift( @_ );
823 17 my $mime_type = shift( @_ );
824 17 ▶ my $flags = shift( @_ ) // MAGIC_NONE;
Condition50%1/2
shift @_ // 0
l!l
170
MC/DC0%0/1
shift @_ // 0
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
825  
826 17 ▶ my $is_type = ( $flags & MAGIC_MIME_TYPE ) && !( $flags & MAGIC_MIME_ENCODING );
Condition100%3/3
$flags & 16 && !($flags & 1024)
!ll&&!rl&&r
1214
MC/DC100%2/2
$flags & 16 && !($flags & 1024)
$flags & 16 !($flags & 1024)
Truth table100%3/3
A && not(B)
A= $flags & 16B= !($flags & 1024)
ABresult
0X0
100
111
827 17 ▶ my $is_encoding = ( $flags & MAGIC_MIME_ENCODING ) && !( $flags & MAGIC_MIME_TYPE );
Condition100%3/3
$flags & 1024 && !($flags & 16)
!ll&&!rl&&r
1421
MC/DC100%2/2
$flags & 1024 && !($flags & 16)
$flags & 1024 !($flags & 16)
Truth table100%3/3
A && not(B)
A= $flags & 1024B= !($flags & 16)
ABresult
0X0
100
111
828 17 ▶ my $is_mime = ( $flags & MAGIC_MIME_TYPE ) && ( $flags & MAGIC_MIME_ENCODING );
Condition100%3/3
$flags & 16 && $flags & 1024
!ll&&!rl&&r
1142
MC/DC100%2/2
$flags & 16 && $flags & 1024
$flags & 16 $flags & 1024
Truth table100%3/3
A && B
A= $flags & 16B= $flags & 1024
ABresult
0X0
100
111
829  
830 # Determine charset for binary formats
831 17 my $charset = _charset_for_mime( $mime_type );
832  
833 17 ▶ if( $is_type )
Branch: 3 branches
BranchTrueFalseTotal
if ($is_type) { } 14 3 17
elsif ($is_encoding) { } 1 2 3
elsif ($is_mime) { } 2 0 2
834 {
835 14 return( $mime_type );
836 }
837 elsif( $is_encoding )
838 {
839 1 return( $charset );
840 }
841 elsif( $is_mime )
842 {
843 2 return( "$mime_type; charset=$charset" );
844 }
845 # MAGIC_NONE or other: return a brief textual description
846 # For the pure-Perl backends, we just return the MIME type as the description
847 # since we don't have libmagic's verbose text output
848 0 return( $mime_type );
849 }
850  
851 # _charset_for_mime( $mime_type ) -> charset string
852 # Returns a reasonable charset for a given MIME type.
853 # Text types default to binary (we don't do text encoding detection here;
854 # that requires reading the content which is better left to libmagic or file(1)).
855 sub _charset_for_mime
856 17 ▶{
Subroutine _charset_for_mime: called CC=4 SCAR=14.2
857 17 ▶ my $mime = shift( @_ ) // '';
Condition50%1/2
shift @_ // ""
l!l
170
MC/DC0%0/1
shift @_ // ""
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
858 17 ▶ return( 'binary' ) if( $mime =~ m{^(application|audio|video|image)/} );
Branch: 1 branch
BranchTrueFalseTotal
if $mime =~ m[^(application|audio|video|image)/] 9 8 17
859 8 ▶ return( 'binary' ) if( $mime =~ m{^application/} );
Branch: 1 branch
BranchTrueFalseTotal
if $mime =~ m[^application/] 0 8 8
860 # Text types - return 'binary' too; proper charset detection needs more work
861 8 return( 'binary' );
862 }
863  
864 # _read_file_head( $path ) -> buffer | undef
865 # Reads up to max_read bytes from the beginning of a file.
866 sub _read_file_head
867 10 ▶{
Subroutine _read_file_head: called CC=3 SCAR=11.1
868 10 my $self = shift( @_ );
869 10 my $path = shift( @_ );
870 10 ▶ my $max = $self->{max_read} // 512;
Condition50%1/2
$self->{"max_read"} // 512
l!l
100
MC/DC0%0/1
$self->{"max_read"} // 512
$self->{"max_read"}
Truth table50%1/2
A
A= $self->{"max_read"}
Aresult
11
00
871 10 ▶ open( my $fh, '<:raw', $path ) or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $path or return $self->error("Cannot read ${path}: $!") 10 0 10
872 return( $self->error( "Cannot read $path: $!" ) );
873 10 my $buf = '';
874 10 read( $fh, $buf, $max );
875 10 close( $fh );
876 10 return( $buf );
877 }
878  
879 # _run_json_detection( $buf ) -> mime_type | undef
880 # Tests the JSON magic database against $buf.
881 # Returns the MIME type of the first matching entry (highest priority first).
882 sub _run_json_detection
883 17 ▶{
Subroutine _run_json_detection: called CC=5 SCAR=16.1
884 17 my $self = shift( @_ );
885 17 my $buf = shift( @_ );
886  
887 17 my $db = $self->_load_json_db;
888 17 ▶ return( undef ) unless( defined( $db ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $db 0 17 17
889  
890 17 foreach my $entry ( @$db )
891 {
892 7939 foreach my $m ( @{$entry->{matches}} )
893 {
894 13615 ▶ if( _match_bytes( $buf, $m ) )
Branch: 1 branch
BranchTrueFalseTotal
if (_match_bytes($buf, $m)) 9 13606 13615
895 {
896 9 return( $entry->{mime} );
897 }
898 }
899 }
900 8 return( undef );
901 }
902  
903 # _with_flags( $flags, $method, @args )
904 # Temporarily overrides flags, runs $method, restores original state.
905 sub _with_flags
906 4 ▶{
Subroutine _with_flags: called CC=4 SCAR=14.1
907 4 my $self = shift( @_ );
908 4 my $flags = shift( @_ );
909 4 my $method = shift( @_ );
910  
911 4 my $orig_flags = $self->{flags};
912 4 ▶ $self->close if( $BACKEND eq 'xs' );
Branch: 1 branch
BranchTrueFalseTotal
if $Module::Generic::File::Magic::BACKEND eq "xs" 0 4 4
913 4 $self->{flags} = $flags;
914  
915 4 my $result = $self->$method( @_ );
916  
917 4 ▶ $self->close if( $BACKEND eq 'xs' );
Branch: 1 branch
BranchTrueFalseTotal
if $Module::Generic::File::Magic::BACKEND eq "xs" 0 4 4
918 4 $self->{flags} = $orig_flags;
919  
920 4 ▶ return( $self->pass_error ) unless( defined( $result ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 4 4
921 4 return( $result );
922 }
923  
924 3 1;
925 # NOTE: POD
926 __END__
« lib/Module/Generic/HeaderValue.pm ↑ Summary lib/Module/Generic/Tie.pm »