line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package File::LibMagic::FFI; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
17051
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
65
|
|
4
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
46
|
|
5
|
2
|
|
|
2
|
|
21
|
use v5.10; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
78
|
|
6
|
2
|
|
|
2
|
|
918
|
use FFI::Raw; |
|
2
|
|
|
|
|
9151
|
|
|
2
|
|
|
|
|
52
|
|
7
|
2
|
|
|
2
|
|
817
|
use FFI::Util qw( scalar_to_buffer ); |
|
2
|
|
|
|
|
7830
|
|
|
2
|
|
|
|
|
9
|
|
8
|
2
|
|
|
2
|
|
993
|
use FFI::CheckLib; |
|
2
|
|
|
|
|
2021
|
|
|
2
|
|
|
|
|
133
|
|
9
|
|
|
|
|
|
|
use constant { |
10
|
2
|
|
|
|
|
6
|
_lib => find_lib( lib => "magic" ), |
11
|
|
|
|
|
|
|
MAGIC_NONE => 0x000000, |
12
|
|
|
|
|
|
|
MAGIC_MIME => 0x000410, #MIME_TYPE | MIME_ENCODING, |
13
|
2
|
|
|
2
|
|
9
|
}; |
|
2
|
|
|
|
|
2
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# ABSTRACT: Determine MIME types of data or files using libmagic |
16
|
|
|
|
|
|
|
our $VERSION = '0.02'; # VERSION |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
use constant { |
20
|
|
|
|
|
|
|
|
21
|
2
|
|
|
|
|
1367
|
_open => FFI::Raw->new( |
22
|
|
|
|
|
|
|
_lib, 'magic_open', |
23
|
|
|
|
|
|
|
FFI::Raw::ptr, |
24
|
|
|
|
|
|
|
FFI::Raw::int, |
25
|
|
|
|
|
|
|
), |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
#_error => FFI::Raw->new( |
28
|
|
|
|
|
|
|
# _lib, 'magic_error', |
29
|
|
|
|
|
|
|
# FFI::Raw::str, |
30
|
|
|
|
|
|
|
# FFI::Raw::ptr, |
31
|
|
|
|
|
|
|
#), |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
_load => FFI::Raw->new( |
34
|
|
|
|
|
|
|
_lib, 'magic_load', |
35
|
|
|
|
|
|
|
FFI::Raw::int, |
36
|
|
|
|
|
|
|
FFI::Raw::ptr, FFI::Raw::str, |
37
|
|
|
|
|
|
|
), |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
_file => FFI::Raw->new( |
40
|
|
|
|
|
|
|
_lib, 'magic_file', |
41
|
|
|
|
|
|
|
FFI::Raw::str, |
42
|
|
|
|
|
|
|
FFI::Raw::ptr, FFI::Raw::str, |
43
|
|
|
|
|
|
|
), |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
#_setflags => FFI::Raw->new( |
46
|
|
|
|
|
|
|
# _lib, 'magic_setflags', |
47
|
|
|
|
|
|
|
# FFI::Raw::void, |
48
|
|
|
|
|
|
|
# FFI::Raw::ptr, FFI::Raw::int, |
49
|
|
|
|
|
|
|
#), |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
_buffer => FFI::Raw->new( |
52
|
|
|
|
|
|
|
_lib, 'magic_buffer', |
53
|
|
|
|
|
|
|
FFI::Raw::str, |
54
|
|
|
|
|
|
|
FFI::Raw::ptr, FFI::Raw::ptr, FFI::Raw::int, |
55
|
|
|
|
|
|
|
), |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
#_check => FFI::Raw->new( |
58
|
|
|
|
|
|
|
# _lib, 'magic_check', |
59
|
|
|
|
|
|
|
# FFI::Raw::int, |
60
|
|
|
|
|
|
|
# FFI::Raw::ptr, FFI::Raw::str, |
61
|
|
|
|
|
|
|
#), |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
#_compile => FFI::Raw->new( |
64
|
|
|
|
|
|
|
# _lib, 'magic_compile', |
65
|
|
|
|
|
|
|
# FFI::Raw::int, |
66
|
|
|
|
|
|
|
# FFI::Raw::ptr, FFI::Raw::str, |
67
|
|
|
|
|
|
|
#), |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
_close => FFI::Raw->new( |
70
|
|
|
|
|
|
|
_lib, 'magic_close', |
71
|
|
|
|
|
|
|
FFI::Raw::void, |
72
|
|
|
|
|
|
|
FFI::Raw::ptr, |
73
|
|
|
|
|
|
|
), |
74
|
2
|
|
|
2
|
|
9264
|
}; |
|
2
|
|
|
|
|
4
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub new |
78
|
|
|
|
|
|
|
{ |
79
|
3
|
|
|
3
|
1
|
1850
|
my($class, $magic_file) = @_; |
80
|
3
|
|
|
|
|
13
|
return bless { magic_file => $magic_file }, $class; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub _mime_handle |
84
|
|
|
|
|
|
|
{ |
85
|
10
|
|
|
10
|
|
19
|
my($self) = @_; |
86
|
10
|
|
66
|
|
|
1007
|
return $self->{mime_handle} ||= do { |
87
|
2
|
|
|
|
|
25
|
my $handle = _open->call(MAGIC_MIME); |
88
|
2
|
|
|
|
|
281
|
_load->call($handle, $self->{magic_file}); |
89
|
2
|
|
|
|
|
2102
|
$handle; |
90
|
|
|
|
|
|
|
}; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub _describe_handle |
94
|
|
|
|
|
|
|
{ |
95
|
10
|
|
|
10
|
|
14
|
my($self) = @_; |
96
|
10
|
|
66
|
|
|
921
|
return $self->{describe_handle} ||= do { |
97
|
2
|
|
|
|
|
16
|
my $handle = _open->call(MAGIC_NONE); |
98
|
2
|
|
|
|
|
205
|
_load->call($handle, $self->{magic_file}); |
99
|
2
|
|
|
|
|
1296
|
$handle; |
100
|
|
|
|
|
|
|
}; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub DESTROY |
104
|
|
|
|
|
|
|
{ |
105
|
3
|
|
|
3
|
|
1035
|
my($self) = @_; |
106
|
3
|
50
|
|
|
|
15
|
_close->call($self->{magic_handle}) if defined $self->{magic_handle}; |
107
|
3
|
100
|
|
|
|
162
|
_close->call($self->{mime_handle}) if defined $self->{mime_handle}; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub checktype_contents |
112
|
|
|
|
|
|
|
{ |
113
|
6
|
100
|
|
6
|
1
|
4570
|
_buffer->call($_[0]->_mime_handle, scalar_to_buffer(ref $_[1] ? ${$_[1]} : $_[1])); |
|
2
|
|
|
|
|
7
|
|
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub checktype_filename |
118
|
|
|
|
|
|
|
{ |
119
|
4
|
|
|
4
|
1
|
1943
|
_file->call($_[0]->_mime_handle, $_[1]); |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub describe_contents |
124
|
|
|
|
|
|
|
{ |
125
|
6
|
100
|
|
6
|
1
|
5942
|
_buffer->call($_[0]->_describe_handle, scalar_to_buffer(ref $_[1] ? ${$_[1]} : $_[1])); |
|
2
|
|
|
|
|
7
|
|
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub describe_filename |
130
|
|
|
|
|
|
|
{ |
131
|
4
|
|
|
4
|
1
|
15
|
_file->call($_[0]->_describe_handle, $_[1]); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
1; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
__END__ |