lib/File/FormatIdentification/Pronom.pm

filter: Statementstmt 48.1% 269 / 559 Branchbran 46.0% 58 / 126 Conditioncond 59.2% 32 / 54 MC/DCmcdc 33.3% 10 / 30 Subroutinesub 58.7% 27 / 46 Podpod 0.0% 0 / 20 totaltotal 47.4% 396 / 835 CC 101 SCAR 35.2
CC
101
Cov
49%
_prepare_statistics
167.8
print_statistics
90.0
uniq_signature_ids_by_priority
42.0
CRAP 33.9
↑ Summary lib/File/FormatIdentification/Regex.pm »
1 package File::FormatIdentification::Pronom;
2  
3 1 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use List::Util qw( none first );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::FormatIdentification::Regex;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use v5.21; # special regex syntax introduced with 5.21 needed!
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $VERSION = '0.06'; # VERSION
16  
17 # ABSTRACT Perl extension for parsing PRONOM-Signatures using DROID-Signature file
18  
19 # Preloaded methods go here.
20 # flattens a regex-structure to a regex-string, expects a signature-pattern and a list of regex-structures
21 # returns regex
22 #
23 1 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 sub _flatten_rx_recursive ($$$@) {
26 35355 ▶ my $regex = shift;
Subroutine _flatten_rx_recursive: called CC=11 SCAR=25.0
27 35355 my $lastpos = shift;
28 35355 my $open_brackets = shift;
29 35355 my @rx_groups = @_;
30 35355 my $rx = shift @rx_groups;
31  
32 #use Data::Printer;
33 #say "_flatten_rx_recursive";
34 #p( @rx_groups );
35 #p( $rx );
36 35355 my $bracket_symbol = "(";
37 35355 ▶ if ( !defined $regex ) { confess; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $regex) 0 35355 35355
38  
39 35355 ▶ if ( !defined $rx ) { # do nothing
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rx) { } 16551 18804 35355
40 16551 ▶ while ( $open_brackets > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($open_brackets > 0) 2661 16551 19212
41 2661 $regex .= ")";
42 2661 $open_brackets--;
43 }
44 }
45 else {
46 18804 my $pos_diff = $rx->{position} - $lastpos;
47 18804 my $local_regex = $rx->{regex};
48 18804 ▶ if ( !defined $local_regex ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $local_regex) 0 18804 18804
49 0 $local_regex = '';
50 }
51 18804 ▶ if ( 0 == $pos_diff ) {
Branch: 3 branches
BranchTrueFalseTotal
if (0 == $pos_diff) { } 5757 13047 18804
elsif ($pos_diff > 0) { } 13047 0 13047
elsif ($pos_diff < 0) { } 0 0 0
52  
53 # TODO:
54 5757 File::FormatIdentification::Regex::simplify_two_or_combined_regex(
55 $regex, $local_regex );
56 5757 $regex =
57 &_flatten_rx_recursive( "$regex|$local_regex", $lastpos,
58 $open_brackets, @rx_groups );
59 }
60 elsif ( $pos_diff > 0 ) { # is deeper
61 # look a head, if same pos found, then use bracket, otherwise not
62 13047 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @rx_groups > 0 and $rx_groups[0]{'position'} == $rx->{'position'} or $pos_diff > 1) { } 2661 10386 13047
Condition83%5/6
scalar @rx_groups > 0 and $rx_groups[0]{"position"} == $rx->{"position"} or $pos_diff > 1
l!l&&r!l&&!r
2661010386
scalar @rx_groups > 0 and $rx_groups[0]{"position"} == $rx->{"position"}
!ll&&!rl&&r
762027662661
MC/DC66%2/3
scalar @rx_groups > 0 and $rx_groups[0]{"position"} == $rx->{"position"} or $pos_diff > 1
scalar @rx_groups > 0 $rx_groups[0]{"position"} == $rx->{"position"} $pos_diff > 1
Truth table60%3/5
A and B or C
A= scalar @rx_groups > 0B= $rx_groups[0]{"position"} == $rx->{"position"}C= $pos_diff > 1
ABCresult
11X1
0X11
1011
0X00
1000
63 (
64 scalar @rx_groups > 0
65 && ( $rx_groups[0]->{position} == $rx->{position} )
66 )
67 || $pos_diff > 1
68 )
69 { # use (
70 $regex = &_flatten_rx_recursive(
71 "$regex" . ( $bracket_symbol x $pos_diff ) . $local_regex,
72 2661 $rx->{position}, $open_brackets += $pos_diff, @rx_groups );
73 }
74 else {
75 $regex = &_flatten_rx_recursive(
76 "$regex$local_regex", $rx->{position},
77 10386 $open_brackets, @rx_groups
78 );
79 } ## end else [ if ( scalar @rx_groups...)]
80 }
81 elsif ( $pos_diff < 0 ) { # is higher
82 $regex = &_flatten_rx_recursive(
83 "$regex)$local_regex",
84 $rx->{position},
85 0 $open_brackets - 1, #($rx->{position} - $lastpos),
86 @rx_groups
87 );
88 }
89 else {
90 0 confess
91 "FL: pos=$rx->{position} lastpos=$lastpos regex='$regex' open=$open_brackets\n";
92 }
93 }
94 35355 return $regex;
95 } ## end sub _flatten_rx_recursive ($$$@)
96 1 ▶use warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 sub _flatten_rx ($@) {
99 16551 ▶ my $regex = shift;
Subroutine _flatten_rx: called CC=1 SCAR=0.0
100 16551 my @rx_groups = @_;
101  
102 #say "calling flatten_rx with regex=$regex quality=$quality";
103 #use Data::Printer;
104 #p( @rx_groups );
105 16551 $regex = _flatten_rx_recursive( $regex, 0, 0, @rx_groups );
106 16551 return $regex;
107 } ## end sub _flatten_rx ($@)
108  
109 # expands pattern of form "FFFB[10:EB]" to FFFB10, FFFB11, ... FFFBEB
110 sub _expand_pattern ($) {
111 18804 ▶ my $pattern = $_[0];
Subroutine _expand_pattern: called CC=1 SCAR=0.0
112 18804 $pattern =~ s/(?<=\[)!/^/g;
113 18804 $pattern =~ s/(?<=[0-9A-F]{2}):(?=[0-9A-F]{2})\]/-]/g;
114 18804 $pattern =~ s/([0-9A-F]{2})/\\x{$1}/g;
115  
116 # substitute hex with printable ASCII-Output
117 18804 $pattern =~ s#\\x\{(3[0-9]|[46][1-9A-F]|[57][0-9A])\}#chr( hex($1) );#egs;
118 18804 return $pattern;
119 } ## end sub _expand_pattern ($)
120  
121 # expands offsets min,max to regex ".{$min,$max}" and uses workarounds if $min or $max exceeds 32766
122 sub _expand_offsets($$) {
123 18804 ▶ my $minoffset = shift;
Subroutine _expand_offsets: called CC=31 SCAR=36.1
124 18804 my $maxoffset = shift;
125 18804 my $byte =
126 '.'; # HINT: needs the character set modifier "aa" in $foo=~m/$regex/aa
127 #my $byte = '[\x00-\xff]';
128 18804 my $offset_expanded = "";
129 18804 ▶ if ( ( ( not defined $minoffset ) || ( length($minoffset) == 0 ) )
Branch: 2 branches
BranchTrueFalseTotal
if (!defined($minoffset) || length $minoffset == 0 and !defined($maxoffset) || length $maxoffset == 0) { } 111 18693 18804
elsif (defined $minoffset and length $minoffset > 0 and defined $maxoffset and length $maxoffset > 0 and $minoffset == $maxoffset) { } 15243 3450 18693
Condition55%5/9
!defined($minoffset) || length $minoffset == 0 and !defined($maxoffset) || length $maxoffset == 0
!ll&&!rl&&r
186930111
not defined $minoffset or length $minoffset == 0
l!l&&r!l&&!r
111018693
not defined $maxoffset or length $maxoffset == 0
l!l&&r!l&&!r
11100
MC/DC25%1/4
!defined($minoffset) || length $minoffset == 0 and !defined($maxoffset) || length $maxoffset == 0
not defined $minoffset length $minoffset == 0 not defined $maxoffset length $maxoffset == 0
Truth table28%2/7
not(A) or B and not(C) or D
A= not defined $minoffsetB= length $minoffset == 0C= not defined $maxoffsetD= length $maxoffset == 0
ABCDresult
00XX0
1X000
01000
1X1X1
1X011
011X1
01011
130 && ( ( not defined $maxoffset ) || ( length($maxoffset) == 0 ) ) )
131 {
132 111 $offset_expanded = "";
133 }
134 ▶ elsif (( defined $minoffset )
Condition66%8/12
defined $minoffset and length $minoffset > 0 and defined $maxoffset and length $maxoffset > 0 and $minoffset == $maxoffset
!ll&&!rl&&r
1137231315243
defined $minoffset and length $minoffset > 0 and defined $maxoffset and length $maxoffset > 0
!ll&&!rl&&r
1137017556
defined $minoffset and length $minoffset > 0 and defined $maxoffset
!ll&&!rl&&r
0113717556
defined $minoffset and length $minoffset > 0
!ll&&!rl&&r
0018693
MC/DC40%2/5
defined $minoffset and length $minoffset > 0 and defined $maxoffset and length $maxoffset > 0 and $minoffset == $maxoffset
defined $minoffset length $minoffset > 0 defined $maxoffset length $maxoffset > 0 $minoffset == $maxoffset
Truth table50%3/6
A and B and C and D and E
A= defined $minoffsetB= length $minoffset > 0C= defined $maxoffsetD= length $maxoffset > 0E= $minoffset == $maxoffset
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
135 && ( length($minoffset) > 0 )
136 && ( defined $maxoffset )
137 && ( length($maxoffset) > 0 )
138 && ( $minoffset == $maxoffset ) )
139 {
140 15243 ▶ if ( $minoffset > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minoffset > 0) 3507 11736 15243
141 3507 my $maxloops = int( $maxoffset / 32766 );
142 3507 my $maxresidual = $maxoffset % 32766;
143 3507 ▶ for ( my $i = 0 ; $i < $maxloops ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $maxloops) 3 3507 3510
144 3 $offset_expanded .= $byte . "{32766}";
145 }
146 3507 $offset_expanded .= $byte . "{$maxresidual}";
147 } ## end if ( $minoffset > 0 )
148 }
149 else {
150  
151 # workaround, because perl quantifier limits,
152 # calc How many repetitions we need! Both offsets should be less than 32766
153 #TODO: check if this comes from Droid or is calculated
154  
155 3450 my $mintmp = 0;
156 3450 my $maxtmp = 0;
157 3450 ▶ if ( defined $minoffset && ( length($minoffset) > 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $minoffset and length $minoffset > 0) 3450 0 3450
Condition33%1/3
defined $minoffset and length $minoffset > 0
!ll&&!rl&&r
003450
MC/DC0%0/2
defined $minoffset and length $minoffset > 0
defined $minoffset length $minoffset > 0
Truth table33%1/3
A and B
A= defined $minoffsetB= length $minoffset > 0
ABresult
0X0
100
111
158 3450 $mintmp = $minoffset;
159 }
160 3450 ▶ if ( defined $maxoffset && ( length($maxoffset) > 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maxoffset and length $maxoffset > 0) 2313 1137 3450
Condition66%2/3
defined $maxoffset and length $maxoffset > 0
!ll&&!rl&&r
113702313
MC/DC50%1/2
defined $maxoffset and length $maxoffset > 0
defined $maxoffset length $maxoffset > 0
Truth table66%2/3
A and B
A= defined $maxoffsetB= length $maxoffset > 0
ABresult
0X0
100
111
161 2313 $maxtmp = $maxoffset;
162 }
163  
164 3450 my $maxloops;
165 3450 ▶ if ( $maxtmp >= $mintmp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxtmp >= $mintmp) { } 3408 42 3450
166 3408 $maxloops = int( $maxtmp / 32766 );
167 }
168 else {
169 42 $maxloops = int( $mintmp / 32766 );
170 }
171 3450 my $maxresidual = $maxtmp % 32766;
172 3450 my $minresidual = $mintmp % 32766;
173  
174 #say "\tMaxloops=$maxloops maxres = $maxresidual minres=$minresidual";
175 3450 my @offsets;
176 3450 my $minstr = "";
177 3450 my $maxstr = "";
178 3450 ▶ if ( defined $minoffset && length($minoffset) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $minoffset and length $minoffset > 0) 3450 0 3450
Condition33%1/3
defined $minoffset and length $minoffset > 0
!ll&&!rl&&r
003450
MC/DC0%0/2
defined $minoffset and length $minoffset > 0
defined $minoffset length $minoffset > 0
Truth table33%1/3
A and B
A= defined $minoffsetB= length $minoffset > 0
ABresult
0X0
100
111
179 3450 $minstr = $minresidual;
180 3450 $mintmp = $mintmp - $minresidual;
181 }
182  
183 3450 ▶ for ( my $i = 0 ; $i <= $maxloops ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $maxloops) 4437 3450 7887
184  
185 # loop, so we assure the special handling of residuals
186 4437 ▶ if ( $maxtmp > $maxresidual ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($maxtmp > $maxresidual) { } 1134 3303 4437
elsif ($maxtmp < 0) { } 0 3303 3303
187 1134 $maxstr = 32766;
188 }
189 elsif ( $maxtmp < 0 ) {
190 0 $maxstr = 0;
191 }
192 else {
193 3303 $maxstr = $maxresidual;
194 }
195 4437 ▶ if ( $mintmp > $minresidual ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mintmp > $minresidual) { } 0 4437 4437
elsif ($mintmp < 0) { } 279 4158 4437
196 0 $minstr = 32766;
197 }
198 elsif ( $mintmp < 0 ) {
199 279 $minstr = 0;
200 }
201 else {
202 4158 $minstr = $minresidual;
203 }
204 #### handle residuals
205 4437 ▶ if ( $i == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i == 0) { } 3450 987 4437
elsif ($i == $maxloops) { } 147 840 987
206 3450 $minstr = $minresidual;
207 3450 $mintmp = $mintmp - $minresidual;
208 }
209 elsif ( $i == $maxloops ) {
210 147 $maxstr = $maxresidual;
211 147 $maxtmp = $maxtmp - $maxresidual;
212 }
213  
214 # mark offsets
215 4437 my $tmp;
216 4437 $tmp->{minoffset} = $minstr;
217 4437 $tmp->{maxoffset} = $maxstr;
218 4437 push @offsets, $tmp;
219 } ## end for ( my $i = 0 ; $i <=...)
220 my @filtered = map {
221 3450 ▶ if ( !defined $maxoffset || length($maxoffset) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $maxoffset or length $maxoffset == 0) 1137 3300 4437
Condition66%2/3
not defined $maxoffset or length $maxoffset == 0
l!l&&r!l&&!r
113703300
MC/DC50%1/2
not defined $maxoffset or length $maxoffset == 0
not defined $maxoffset length $maxoffset == 0
Truth table66%2/3
not(A) or B
A= not defined $maxoffsetB= length $maxoffset == 0
ABresult
1X1
011
000
222 1137 $_->{maxoffset} = "";
223 }
224 4437 ▶ if ( !defined $minoffset || length($minoffset) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $minoffset or length $minoffset == 0) 0 4437 4437
Condition33%1/3
not defined $minoffset or length $minoffset == 0
l!l&&r!l&&!r
004437
MC/DC0%0/2
not defined $minoffset or length $minoffset == 0
not defined $minoffset length $minoffset == 0
Truth table33%1/3
not(A) or B
A= not defined $minoffsetB= length $minoffset == 0
ABresult
1X1
011
000
225 0 $_->{minoffset} = "";
226 }
227 4437 $_;
228 } @offsets;
229 3450 foreach my $tmp (@filtered) {
230  
231 # ? at the end - means non-greedy
232 #$offset_expanded .= $byte."{" . $tmp->{minoffset} . "," . $tmp->{maxoffset} . "}?";
233 $offset_expanded .=
234 4437 $byte . "{" . $tmp->{minoffset} . "," . $tmp->{maxoffset} . "}";
235 } ## end foreach my $tmp (@filtered)
236 } ## end else [ if ( ( ( not defined $minoffset...)))]
237  
238 #say "DEBUG: minoffset='$minoffset' maxoffset='$maxoffset' --> offset_expanded='$offset_expanded'";
239  
240 # minimization steps
241 18804 $offset_expanded =~ s#{0,}#*#g;
242 18804 $offset_expanded =~ s#{1,}#+#g;
243 18804 $offset_expanded =~ s#{0,1}#?#g;
244 18804 return $offset_expanded;
245 } ## end sub _expand_offsets($$)
246  
247 # got XPath-object and returns a regex-structure as hashref
248 sub _parse_fragments ($) {
249 12993 ▶ my $fq = shift;
Subroutine _parse_fragments: called CC=3 SCAR=11.0
250 12993 my $position = $fq->getAttribute('Position');
251 12993 my $minoffset = $fq->getAttribute('MinOffset');
252 12993 my $maxoffset = $fq->getAttribute('MaxOffset');
253 12993 my $rx = $fq->textContent;
254 12993 my $expanded = _expand_pattern($rx);
255 12993 my $ret;
256 12993 $ret->{position} = $position;
257 12993 $ret->{direction} = "left";
258 12993 $ret->{regex} = "";
259  
260 12993 my ($offset_expanded) = _expand_offsets( $minoffset, $maxoffset );
261  
262 12993 ▶ if ( $fq->localname eq "LeftFragment" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fq->localname eq 'LeftFragment') { } 4788 8205 12993
elsif ($fq->localname eq 'RightFragment') { } 8205 0 8205
263 4788 $ret->{direction} = "left";
264 4788 $ret->{regex} = "($expanded)$offset_expanded";
265 }
266 elsif ( $fq->localname eq "RightFragment" ) {
267 8205 $ret->{direction} = "right";
268 8205 $ret->{regex} = "$offset_expanded($expanded)";
269 }
270  
271 #say "pF: rx=$rx expanded=$expanded offset=$offset_expanded";
272 12993 return $ret;
273 } ## end sub _parse_fragments ($)
274  
275 # got XPath-object and search direction and returns a regex-structure as hashref
276 sub _parse_subsequence ($$) {
277 5811 ▶ my $ssq = shift;
Subroutine _parse_subsequence: called CC=5 SCAR=16.2
278 5811 my $dir = shift;
279 5811 my $position = $ssq->getAttribute('Position');
280 5811 my $minoffset = $ssq->getAttribute('SubSeqMinOffset');
281 5811 my $maxoffset = $ssq->getAttribute('SubSeqMaxOffset');
282  
283 5811 my $rx = $ssq->getElementsByTagName('Sequence')->get_node(1)->textContent;
284  
285 5811 my @lnodes = $ssq->getElementsByTagName('LeftFragment');
286 5811 my @rnodes = $ssq->getElementsByTagName('RightFragment');
287 5811 my @lrx_fragments = map { _parse_fragments($_) } @lnodes;
288 5811 my @rrx_fragments = map { _parse_fragments($_) } @rnodes;
289 5811 my $lregex = _flatten_rx( "", @lrx_fragments );
290 5811 my $rregex = _flatten_rx( "", @rrx_fragments );
291 5811 my $expanded = _expand_pattern($rx);
292  
293 #if ( length($minoffset) > 0
294 # && length($maxoffset) > 0
295 # && $minoffset > $maxoffset ) {
296 # confess(
297 #"parse_subsequence: Maxoffset=$maxoffset < Minoffset=$minoffset! regex= '$rx'"
298 # );
299 # } ## end if ( length($minoffset...))
300  
301 5811 my $offset_expanded = _expand_offsets( $minoffset, $maxoffset );
302 5811 my $prefix;
303 my $suffix;
304 5811 my $ret;
305 5811 my $regex;
306 5811 ▶ if ( !defined $dir || length($dir) == 0 ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $dir or length $dir == 0) { } 420 5391 5811
elsif ($dir eq 'BOFoffset') { } 4782 609 5391
elsif ($dir eq 'EOFoffset') { } 609 0 609
Condition66%2/3
not defined $dir or length $dir == 0
l!l&&r!l&&!r
42005391
MC/DC50%1/2
not defined $dir or length $dir == 0
not defined $dir length $dir == 0
Truth table66%2/3
not(A) or B
A= not defined $dirB= length $dir == 0
ABresult
1X1
011
000
307 420 $regex = join( "", $lregex, $expanded, $rregex );
308 }
309 elsif ( $dir eq "BOFoffset" ) {
310 4782 $regex =
311 join( "", $offset_expanded, "(", $lregex, $expanded, $rregex, ")" );
312 }
313 elsif ( $dir eq "EOFoffset" ) {
314 609 $regex =
315 join( "", "(", $lregex, $expanded, $rregex, ")", $offset_expanded );
316 }
317 else {
318 0 warn "unknown reference '$dir' found\n";
319 0 $regex = join( "", $lregex, $expanded, $rregex );
320 }
321 $ret->{regex} =
322 5811 File::FormatIdentification::Regex::peep_hole_optimizer($regex);
323 5811 $ret->{position} = $position;
324  
325 5811 return $ret;
326 } ## end sub _parse_subsequence ($$)
327  
328 # got XPath-object and returns regex-string
329 sub _parse_bytesequence ($) {
330 4929 ▶ my $bsq = shift;
Subroutine _parse_bytesequence: called CC=5 SCAR=16.2
331  
332 #say "rx_groups in parse_byte_sequence:";
333 4929 my $reference = $bsq->getAttribute('Reference');
334 ; # if BOFoffset -> anchored begin of file, EOFofset -> end of file
335 4929 my @nodes = $bsq->getElementsByTagName('SubSequence');
336 4929 my @rx_groups = map { _parse_subsequence( $_, $reference ) } @nodes;
337 4929 my $expanded = "";
338 4929 my $regex_flattened = _flatten_rx( $expanded, @rx_groups );
339  
340 #my $ro = Regexp::Optimizer->new;
341 #my $ro = Regexp::Assemble->new;
342 #$ro->add( $regex_flattened);
343 #$regex_flattened = $ro->as_string($regex_flattened);
344 #$regex_flattened = $ro->re;
345 4929 my $regex;
346 4929 ▶ if ( !defined $reference || 0 == length($reference) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $reference or 0 == length $reference) { } 255 4674 4929
elsif ($reference eq 'BOFoffset') { } 4065 609 4674
elsif ($reference eq 'EOFoffset') { } 609 0 609
Condition66%2/3
not defined $reference or 0 == length $reference
l!l&&r!l&&!r
25504674
MC/DC50%1/2
not defined $reference or 0 == length $reference
not defined $reference 0 == length $reference
Truth table66%2/3
not(A) or B
A= not defined $referenceB= 0 == length $reference
ABresult
1X1
011
000
347 255 $regex = "$regex_flattened";
348 }
349 elsif ( $reference eq "BOFoffset" ) {
350 4065 $regex = "\\A$regex_flattened";
351 }
352 elsif ( $reference eq "EOFoffset" ) {
353 609 $regex = "$regex_flattened\\Z";
354 }
355 else {
356 0 warn "unknown reference '$reference' found\n";
357 0 $regex = "$regex_flattened";
358 }
359  
360 1 ▶ use Regexp::Optimizer;
Subroutine BEGIN: called CC=1 SCAR=0.0
361 4929 my $ro = Regexp::Optimizer->new;
362  
363 #say "regex='$regex'";
364 #$regex = $ro->as_string( $regex );
365 4929 return $regex;
366 } ## end sub _parse_bytesequence ($)
367  
368 # ($%signatures, $%internal) = parse_signaturefile( $file )
369 sub _parse_signaturefile($) {
370 3 ▶ my $pronomfile = shift;
Subroutine _parse_signaturefile: called CC=5 SCAR=16.1
371 3 my %signatures;
372  
373 # hash{internalid}->{regex} = $regex
374 # ->{signature} = $signature
375 my %internal_signatures;
376  
377 3 my $dom = XML::LibXML->load_xml( location => $pronomfile );
378 3 $dom->indexElements();
379 3 my $xp = XML::LibXML::XPathContext->new($dom);
380 3 $xp->registerNs( 'droid',
381 'http://www.nationalarchives.gov.uk/pronom/SignatureFile' );
382  
383 # find Fileformats
384 #my $tmp = $xp->find('/*[local-name() = "FFSignatureFile"]')->get_node(1);
385 #say "E:", $tmp->nodeName;
386 #say "EXISTS:", $xp->exists('/droid:FFSignatureFile');
387 #say "EXISTS2", $xp->exists('/droid:FFSignatureFile/droid:FileFormatCollection/droid:FileFormat');
388  
389 3 my $fmts = $xp->find(
390 '/*[local-name() = "FFSignatureFile"]/*[local-name() = "FileFormatCollection"]/*[local-name() = "FileFormat"]'
391 );
392 3 foreach my $fmt ( $fmts->get_nodelist() ) {
393 4752 my $id = $fmt->getAttribute('ID');
394 4752 my $mimetype = $fmt->getAttribute('MIMEtype');
395 4752 my $name = $fmt->getAttribute('Name');
396 4752 my $puid = $fmt->getAttribute('PUID');
397 4752 my $version = $fmt->getAttribute('Version');
398 #
399  
400 ##
401 my @extensions =
402 4752 map { $_->textContent() } $fmt->getElementsByTagName('Extension');
403 my @internalsignatures =
404 4752 map { $_->textContent() }
405 $fmt->getElementsByTagName('InternalSignatureID');
406 4752 my @haspriorityover = map { $_->textContent() }
407 $fmt->getElementsByTagName('HasPriorityOverFileFormatID');
408 4752 $signatures{$id}->{mimetype} = $mimetype;
409 4752 $signatures{$id}->{name} = $name;
410 4752 $signatures{$id}->{puid} = $puid;
411 4752 $signatures{$id}->{version} = $version; # optional
412 4752 $signatures{$id}->{extensions} = \@extensions;
413 4752 $signatures{$id}->{internal_signatures} = \@internalsignatures;
414  
415 4752 foreach my $prio (@haspriorityover) {
416 2229 $signatures{$id}->{priorityover}->{$prio} = 1;
417 }
418  
419 4752 foreach my $internal (@internalsignatures) {
420 4029 $internal_signatures{$internal}->{signature} = $id;
421 }
422 } ## end foreach my $fmt ( $fmts->get_nodelist...)
423  
424 # find InternalSignatures
425 3 my $sigs =
426 $xp->find(
427 '/*[local-name() = "FFSignatureFile"]/*[local-name() = "InternalSignatureCollection"]/*[local-name() = "InternalSignature"]'
428 );
429  
430 3 foreach my $sig ( $sigs->get_nodelist() ) {
431  
432 4083 my $id = $sig->getAttribute('ID');
433 4083 my $specificity = $sig->getAttribute('Specificity');
434 4083 $internal_signatures{$id}->{specificity} = $specificity;
435  
436 #p( $sig->toString() );
437 4083 my @nodes = $sig->getElementsByTagName('ByteSequence');
438  
439 #p( @nodes );
440 4083 my @rx_groups = map { _parse_bytesequence($_) } @nodes;
441 my @rx_quality =
442 4083 map { File::FormatIdentification::Regex::calc_quality($_); }
443 @rx_groups;
444  
445 4083 $internal_signatures{$id}->{regex} = \@rx_groups;
446 4083 $internal_signatures{$id}->{quality} = \@rx_quality;
447 } ## end foreach my $sig ( $sigs->get_nodelist...)
448  
449 3 return ( \%signatures, \%internal_signatures );
450 } ## end sub _parse_signaturefile($)
451  
452 sub uniq_signature_ids_by_priority {
453 0 ▶ my $self = shift;
Subroutine uniq_signature_ids_by_priority: not called CC=6 SCAR=37.4
Pod undocumented
454 0 my @signatures = @_;
455 0 my %found_signature_ids;
456  
457 # which PUIDs are in list?
458 0 foreach my $signatureid (@signatures) {
459 0 ▶ if ( defined $signatureid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $signatureid) 0 0 0
460 0 $found_signature_ids{$signatureid} = 1;
461 }
462 }
463  
464 # remove all signatures when actual signature has priority over
465 0 foreach my $signatureid ( keys %found_signature_ids ) {
466 0 foreach my $priority_over_sid (
467 0 keys %{ $self->{signatures}->{$signatureid}->{priorityover} } )
468 {
469 0 ▶ if ( exists $found_signature_ids{$priority_over_sid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $found_signature_ids{$priority_over_sid}) 0 0 0
470 0 delete $found_signature_ids{$priority_over_sid};
471 }
472 } ## end foreach my $priority_over_sid...
473 } ## end foreach my $signatureid ( keys...)
474  
475 # reduce list to all signatures with correct priority
476 my @result =
477 0 grep { defined $found_signature_ids{ $_->{signature} } } @signatures;
478 0 return @result;
479 } ## end sub uniq_signature_ids_by_priority
480  
481 has 'droid_signature_filename' => (
482 is => 'ro',
483 required => 1,
484 reader => 'get_droid_signature_filename',
485 trigger => sub {
486 4 ▶ my $self = shift;
Subroutine __ANON__: called CC=7 SCAR=19.7
487  
488 #say "TRIGGER";
489 4 my $yaml_file = $self->get_droid_signature_filename() . ".yaml";
490 4 ▶ if ( $self->{auto_load} && -e $yaml_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'auto_load'} and -e $yaml_file) { } 1 3 4
Condition66%2/3
$self->{"auto_load"} and -e $yaml_file
!ll&&!rl&&r
031
MC/DC50%1/2
$self->{"auto_load"} and -e $yaml_file
$self->{"auto_load"} -e $yaml_file
Truth table66%2/3
A and B
A= $self->{"auto_load"}B= -e $yaml_file
ABresult
0X0
100
111
491 1 $self->load_from_yamlfile($yaml_file);
492 }
493 else {
494 my ( $signatures, $internal_signatures ) =
495 3 _parse_signaturefile( $self->{droid_signature_filename} );
496 3 $self->{signatures} = $signatures;
497 3 $self->{internal_signatures} = $internal_signatures;
498  
499 #die;
500 3 ▶ if ( $self->{auto_store} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"auto_store"}) 2 1 3
501 2 $self->save_as_yamlfile($yaml_file);
502 }
503 } ## end else [ if ( $self->{auto_load...})]
504 4 foreach my $s ( keys %{ $self->{signatures} } ) {
505 6336 my $puid = $self->{signatures}->{$s}->{puid};
506 6336 ▶ if ( defined $puid && length($puid) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $puid and length $puid > 0) 6336 0 6336
Condition33%1/3
defined $puid and length $puid > 0
!ll&&!rl&&r
006336
MC/DC0%0/2
defined $puid and length $puid > 0
defined $puid length $puid > 0
Truth table33%1/3
A and B
A= defined $puidB= length $puid > 0
ABresult
0X0
100
111
507 6336 $self->{puids}->{$puid} = $s;
508 }
509 }
510 }
511 1 );
512  
513 sub save_as_yamlfile {
514 2 ▶ my $self = shift;
Subroutine save_as_yamlfile: called CC=1 SCAR=0.0
Pod undocumented
515 2 my $filename = shift;
516 2 my @res;
517 2 push @res, $self->{signatures};
518 2 push @res, $self->{internal_signatures};
519 2 YAML::XS::DumpFile( "$filename", @res );
520 2 return;
521 } ## end sub save_as_yamlfile
522  
523 sub load_from_yamlfile {
524 1 ▶ my $self = shift;
Subroutine load_from_yamlfile: called CC=1 SCAR=0.0
Pod undocumented
525 1 my $filename = shift;
526 1 my ( $sig, $int ) = YAML::XS::LoadFile($filename);
527 1 $self->{signatures} = $sig;
528 1 $self->{internal_signatures} = $int;
529 1 return;
530 } ## end sub load_from_yamlfile
531  
532 1 has 'auto_store' => (
533 is => 'ro',
534 default => 1,
535 );
536  
537 1 has 'auto_load' => (
538 is => 'ro',
539 default => 1,
540 );
541  
542 sub get_all_signature_ids {
543 0 ▶ my $self = shift;
Subroutine get_all_signature_ids: not called CC=1 SCAR=6.9
Pod undocumented
544 0 my @sigs = sort { $a <=> $b } keys %{ $self->{signatures} };
545 0 return @sigs;
546 }
547  
548 sub get_signature_id_by_puid {
549 0 ▶ my $self = shift;
Subroutine get_signature_id_by_puid: not called CC=1 SCAR=6.9
Pod undocumented
550 0 my $puid = shift;
551 0 my $sig = $self->{puids}->{$puid};
552 0 return $sig;
553 }
554  
555 sub get_internal_ids_by_puid {
556 0 ▶ my $self = shift;
Subroutine get_internal_ids_by_puid: not called CC=2 SCAR=17.9
Pod undocumented
557 0 my $puid = shift;
558 0 my $sig = $self->get_signature_id_by_puid($puid);
559 0 my @ids = ();
560 0 ▶ if ( defined $sig ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sig) 0 0 0
561 0 @ids = grep { defined $_ }
562 0 @{ $self->{signatures}->{$sig}->{internal_signatures} };
563 }
564 0 return @ids;
565 }
566  
567 sub get_file_endings_by_puid {
568 0 ▶ my $self = shift;
Subroutine get_file_endings_by_puid: not called CC=2 SCAR=17.9
Pod undocumented
569 0 my $puid = shift;
570 0 my $sig = $self->get_signature_id_by_puid($puid);
571 0 my @endings = ();
572 0 ▶ if ( defined $sig ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sig) 0 0 0
573 0 @endings = $self->{signatures}->{$sig}->{extensions};
574 }
575 0 return @endings;
576 }
577  
578 sub get_all_internal_ids {
579 0 ▶ my $self = shift;
Subroutine get_all_internal_ids: not called CC=3 SCAR=24.8
Pod undocumented
580 0 my @ids = sort { $a <=> $b } keys %{ $self->{internal_signatures} };
581 0 foreach my $id (@ids) {
582 0 ▶ if ( !defined $id ) { confess("$id not defined") }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 0 0
583 }
584 0 return @ids;
585 }
586  
587 sub get_all_puids {
588 0 ▶ my $self = shift;
Subroutine get_all_puids: not called CC=1 SCAR=6.9
Pod undocumented
589 my @ids =
590 0 sort grep { defined $_ }
591 0 map { $self->{signatures}->{$_}->{puid}; }
592 0 grep { defined $_ } $self->get_all_signature_ids();
593 0 return @ids;
594 }
595  
596 sub get_regular_expressions_by_internal_id {
597 0 ▶ my $self = shift;
Subroutine get_regular_expressions_by_internal_id: not called CC=2 SCAR=17.9
Pod undocumented
598 0 my $internalid = shift;
599 0 ▶ if ( !defined $internalid ) { confess("internalid must exists!"); }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $internalid) 0 0 0
600 0 return @{ $self->{internal_signatures}->{$internalid}->{regex} };
601 }
602  
603 sub get_all_regular_expressions {
604 0 ▶ my $self = shift;
Subroutine get_all_regular_expressions: not called CC=2 SCAR=17.9
Pod undocumented
605 0 my @ids = $self->get_all_internal_ids();
606 0 my @regexes = ();
607 0 foreach my $id (@ids) {
608 0 my @rx = $self->get_regular_expressions_by_internal_id($id);
609 0 push @regexes, @rx;
610 }
611 0 my @ret = sort @regexes;
612 0 return @ret;
613 }
614  
615 sub get_qualities_by_internal_id {
616 0 ▶ my $self = shift;
Subroutine get_qualities_by_internal_id: not called CC=3 SCAR=24.8
Pod undocumented
617 0 my $internalid = shift;
618 0 ▶ if ( !defined $internalid ) { confess("internalid must exists!"); }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $internalid) 0 0 0
619 0 my $value = $self->{internal_signatures}->{$internalid}->{quality};
620 0 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
621 0 return @{$value};
622 }
623 0 return;
624 }
625  
626 sub get_signature_id_by_internal_id {
627 0 ▶ my $self = shift;
Subroutine get_signature_id_by_internal_id: not called CC=2 SCAR=17.9
Pod undocumented
628 0 my $internalid = shift;
629 0 ▶ if ( !defined $internalid ) { confess("internalid must exists!"); }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $internalid) 0 0 0
630 0 return $self->{internal_signatures}->{$internalid}->{signature};
631 }
632  
633 sub get_name_by_signature_id {
634 0 ▶ my $self = shift;
Subroutine get_name_by_signature_id: not called CC=1 SCAR=6.9
Pod undocumented
635 0 my $signature = shift;
636 0 return $self->{signatures}->{$signature}->{name};
637 }
638  
639 sub get_puid_by_signature_id {
640 0 ▶ my $self = shift;
Subroutine get_puid_by_signature_id: not called CC=1 SCAR=6.9
Pod undocumented
641 0 my $signature = shift;
642 0 return $self->{signatures}->{$signature}->{puid};
643 }
644  
645 sub get_puid_by_internal_id {
646 0 ▶ my $self = shift;
Subroutine get_puid_by_internal_id: not called CC=2 SCAR=17.9
Pod undocumented
647 0 my $internalid = shift;
648 0 ▶ if ( !defined $internalid ) { confess("internalid must exists!"); }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $internalid) 0 0 0
649 0 my $signature = $self->get_signature_id_by_internal_id($internalid);
650 0 return $self->get_puid_by_signature_id($signature);
651 }
652  
653 sub get_quality_sorted_internal_ids {
654 0 ▶ my $self = shift;
Subroutine get_quality_sorted_internal_ids: not called CC=3 SCAR=24.8
Pod undocumented
655 my @ids = sort {
656  
657 # sort by regexes
658 0 my @a_rxq = @{ $self->{internal_signatures}->{$a}->{quality} };
659 0 my @b_rxq = @{ $self->{internal_signatures}->{$b}->{quality} };
660 0 my $aq = 0;
661 0 foreach my $as (@a_rxq) { $aq += $as; }
662 0 my $bq = 0;
663 0 foreach my $bs (@b_rxq) { $bq += $bs; }
664  
665 #use Data::Printer;
666 #p( $a );
667 #p( $aq );
668 0 $aq <=> $bq;
669 } $self->get_all_internal_ids();
670 0 return @ids;
671 }
672  
673 sub get_combined_regex_by_puid {
674 0 ▶ my $self = shift;
Subroutine get_combined_regex_by_puid: not called CC=1 SCAR=6.9
Pod undocumented
675 0 my $puid = shift;
676 0 my @internals = $self->get_internal_ids_by_puid($puid);
677  
678 #use Data::Printer;
679 #p( $puid );
680 #p( @internals );
681 my @regexes = map {
682 0 my @regexes_per_internal =
683 $self->get_regular_expressions_by_internal_id($_);
684 0 my $combined =
685 File::FormatIdentification::Regex::and_combine(@regexes_per_internal);
686  
687 #p( $combined );
688 0 $combined;
689 } @internals;
690 0 my $result = File::FormatIdentification::Regex::or_combine(@regexes);
691  
692 #p( $result );
693 0 return $result;
694 }
695  
696 sub _prepare_statistics {
697 0 ▶ my $self = shift;
Subroutine _prepare_statistics: not called CC=13 SCAR=51.2
698 0 my $results;
699  
700 # count of PUIDs
701 # count of internal ids (IDs per PUID)
702 # count of regexes
703 # count of file endings only
704 # count of internal ids without PUID
705 # larges and shortest regex
706 # complex and simple regex
707 # common regexes
708 #say "stat";
709 0 my @puids = $self->get_all_puids();
710 0 my $puids = scalar(@puids);
711 0 my @internals = $self->get_all_internal_ids();
712 0 my $internals = scalar(@internals);
713 0 my $regexes = 0;
714 0 my $fileendingsonly = 0;
715 0 my @fileendingsonly = ();
716 0 my $fileendings = 0;
717 0 my $int_per_puid = 0;
718 0 my $internal_without_puid = 0;
719 0 my @internal_without_puid = ();
720 0 my @quality_sorted_internal_ids = $self->get_quality_sorted_internal_ids();
721 0 my %uniq_regexes;
722  
723 0 foreach my $internalid (@internals) {
724 0 my @regexes =
725 $self->get_regular_expressions_by_internal_id($internalid);
726 0 foreach my $rx (@regexes) {
727 0 my @tmp = ();
728 0 ▶ if ( exists $uniq_regexes{$rx} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $uniq_regexes{$rx}) 0 0 0
729 0 @tmp = @{ $uniq_regexes{$rx} };
730 }
731 0 push @tmp, $internalid;
732 0 $uniq_regexes{$rx} = \@tmp;
733 }
734  
735 0 $regexes += scalar(@regexes);
736 0 my $sigid = $self->get_signature_id_by_internal_id($internalid);
737 0 ▶ if ( !defined $sigid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sigid) 0 0 0
738 0 $internal_without_puid++;
739 0 push @internal_without_puid, $internalid;
740 }
741 }
742 0 foreach my $puid (@puids) {
743 0 my @ints = $self->get_internal_ids_by_puid($puid);
744 0 my @fileendings = $self->get_file_endings_by_puid($puid);
745 0 ▶ if ( 0 == scalar(@ints) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == scalar @ints) { } 0 0 0
746 0 $fileendingsonly++;
747 0 push @fileendingsonly, $puid;
748 }
749 else {
750 0 $fileendings += scalar(@fileendings);
751 0 $int_per_puid += scalar(@ints);
752 }
753 }
754 0 foreach my $i (@quality_sorted_internal_ids) {
755 0 my $regex =
756 join( "#", $self->get_regular_expressions_by_internal_id($i) );
757 0 my $quality = join( " ", $self->get_qualities_by_internal_id($i) );
758  
759 }
760  
761 0 $results->{filename} = $self->get_droid_signature_filename();
762 0 $results->{count_of_puids} = $puids;
763 0 $results->{count_of_internal_ids} = $internals;
764 0 $results->{count_of_regular_expressions} = $regexes;
765 0 $results->{count_of_fileendings} = $fileendings;
766 0 $results->{count_of_puid_with_fileendings_only} = $fileendingsonly;
767 0 $results->{puids_with_fileendings_only} = \@fileendingsonly;
768 0 $results->{count_of_orphaned_internal_ids} = $internal_without_puid;
769 0 $results->{internal_ids_without_puids} = \@internal_without_puid;
770 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
771  
772 0 ▶ for ( my $i = 0 ; $i <= 4 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 4) 0 0 0
773 0 my $best_quality_internal = pop @quality_sorted_internal_ids;
774 0 ▶ if ( defined $best_quality_internal ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $best_quality_internal) 0 0 0
775 0 my $best_quality = join( ";",
776 $self->get_qualities_by_internal_id($best_quality_internal) );
777 0 my $best_puid =
778 $self->get_puid_by_internal_id($best_quality_internal);
779 0 my $best_name =
780 $self->get_name_by_signature_id(
781 $self->get_signature_id_by_internal_id($best_quality_internal)
782 );
783 0 my $best_regex = $self->get_combined_regex_by_puid($best_puid);
784 $results->{nth_best_quality}->[$i]->{internal_id} =
785 0 $best_quality_internal;
786 0 $results->{nth_best_quality}->[$i]->{puid} = $best_puid;
787 0 $results->{nth_best_quality}->[$i]->{name} = $best_name;
788 0 $results->{nth_best_quality}->[$i]->{quality} = $best_quality;
789 0 $results->{nth_best_quality}->[$i]->{combined_regex} = $best_regex;
790 }
791 }
792 0 ▶ for ( my $i = 0 ; $i <= 4 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 4) 0 0 0
793 0 my $worst_quality_internal = shift @quality_sorted_internal_ids;
794 0 ▶ if ( defined $worst_quality_internal ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $worst_quality_internal) 0 0 0
795 0 my $worst_quality = join( ";",
796 $self->get_qualities_by_internal_id($worst_quality_internal) );
797 0 my $worst_puid =
798 $self->get_puid_by_internal_id($worst_quality_internal);
799 0 my $worst_name =
800 $self->get_name_by_signature_id(
801 $self->get_signature_id_by_internal_id($worst_quality_internal)
802 );
803 0 my $worst_regex = $self->get_combined_regex_by_puid($worst_puid);
804 $results->{nth_worst_quality}->[$i]->{internal_id} =
805 0 $worst_quality_internal;
806 0 $results->{nth_worst_quality}->[$i]->{puid} = $worst_puid;
807 0 $results->{nth_worst_quality}->[$i]->{name} = $worst_name;
808 0 $results->{nth_worst_quality}->[$i]->{quality} = $worst_quality;
809 $results->{nth_worst_quality}->[$i]->{combined_regex} =
810 0 $worst_regex;
811 }
812 }
813 my @multiple_used_regex = grep {
814 0 my $tmp = $uniq_regexes{$_};
815 0 my @tmp = @{$tmp};
816 0 scalar(@tmp) > 1
817 } sort keys %uniq_regexes;
818 0 $results->{count_of_multiple_used_regex} = scalar(@multiple_used_regex);
819 0 ▶ for ( my $i = 0 ; $i <= $#multiple_used_regex ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#multiple_used_regex) 0 0 0
820 $results->{multiple_used_regex}->[$i]->{regex} =
821 0 $multiple_used_regex[$i];
822 0 my @ids = join( ",", @{ $uniq_regexes{ $multiple_used_regex[$i] } } );
823 0 $results->{multiple_used_regex}->[$i]->{internal_ids} = \@ids;
824 }
825 0 return $results;
826 }
827  
828 sub print_csv_statistics {
829 0 ▶ my $self = shift;
Subroutine print_csv_statistics: not called CC=4 SCAR=30.0
Pod undocumented
830 0 my $csv_file = shift;
831 0 my $results = $self->_prepare_statistics();
832 0 my $version = $results->{filename};
833 0 $version =~ s/DROID_SignatureFile_V(\d+)\.xml/$1/;
834 0 $results->{version} = $version;
835 0 $results->{best_quality_puid} = $results->{nth_best_quality}->[0]->{puid};
836 $results->{best_quality_internal_id} =
837 0 $results->{nth_best_quality}->[0]->{internal_id};
838 $results->{best_quality_quality} =
839 0 $results->{nth_best_quality}->[0]->{quality};
840 $results->{best_quality_combined_regex} =
841 0 $results->{nth_best_quality}->[0]->{combined_regex};
842 0 $results->{worst_quality_puid} = $results->{nth_worst_quality}->[0]->{puid};
843 $results->{worst_quality_internal_id} =
844 0 $results->{nth_worst_quality}->[0]->{internal_id};
845 $results->{worst_quality_quality} =
846 0 $results->{nth_worst_quality}->[0]->{quality};
847 $results->{worst_quality_combined_regex} =
848 0 $results->{nth_worst_quality}->[0]->{combined_regex};
849  
850 0 my @headers =
851 qw(version filename count_of_puids count_of_internal_ids count_of_regular_expressions count_of_fileendings count_of_puid_with_fileendings_only count_of_orphaned_internal_ids count_of_multiple_used_regex best_quality_puid best_quality_internal_id best_quality_quality best_quality_combined_regex worst_quality_puid worst_quality_internal_id worst_quality_quality worst_quality_combined_regex);
852 0 my $file_exists = (-e $csv_file);
853 0 ▶ open (my $FH, ">>", "$csv_file") or croak "Can't open file '$csv_file', $0";
Branch: 1 branch
BranchTrueFalseTotal
open my $FH, ">>", "$csv_file" or croak("Can't open file '${csv_file}', $0") 0 0 0
854 0 ▶ if (not $file_exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file_exists) 0 0 0
855 0 say $FH "#", join( ",", @headers );
856 }
857 say $FH join(
858 ",",
859 map {
860 0 my $result = $results->{$_};
861 0 ▶ if ( !defined $result ) { $result = ""; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 0 0 0
862 0 "\"$result\"";
863 } @headers
864 );
865 0 close ($FH);
866 0 return;
867 }
868  
869 sub print_statistics {
870 0 ▶ my $self = shift;
Subroutine print_statistics: not called CC=9 SCAR=45.0
Pod undocumented
871 0 my $verbose = shift;
872 0 my $results = $self->_prepare_statistics();
873  
874 0 say "Statistics of file $results->{filename}";
875 0 say "=======================================";
876 0 say "";
877 0 say "Countings";
878 0 say "---------------------------------------";
879 0 say "Count of PUIDs: $results->{count_of_puids}";
880 0 say
881 " internal IDs: $results->{count_of_internal_ids}";
882 0 say
883 " regular expressions: $results->{count_of_regular_expressions}";
884 0 say
885 " file endings: $results->{count_of_fileendings}";
886 0 say
887 " PUIDs with file endings only: $results->{count_of_puid_with_fileendings_only}";
888  
889 0 ▶ if ( defined $verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $verbose) 0 0 0
890 say " (",
891 0 join( ", ", sort @{ $results->{puids_with_fileendings_only} } ), ")";
892 }
893 say
894 0 " orphaned internal IDs: $results->{count_of_orphaned_internal_ids}";
895 0 ▶ if ( defined $verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $verbose) 0 0 0
896 say " (",
897 0 join( ", ", sort {$a <=> $b} @{ $results->{internal_ids_without_puids} } ), ")";
898 }
899 0 say "";
900 0 say "Quality of internal IDs";
901 0 say "---------------------------------------";
902  
903 0 my $nth = 1;
904 0 foreach my $n ( @{ $results->{nth_best_quality} } ) {
905 0 say
906 "$nth-best quality internal ID (PUID, name): $n->{internal_id} ($n->{puid}, $n->{name}) -> $n->{quality}";
907 0 ▶ if ( defined $verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $verbose) 0 0 0
908 0 say " combined regex: ", $n->{combined_regex};
909 }
910 0 $nth++;
911 }
912 0 say "";
913 0 $nth = 1;
914 0 foreach my $n ( @{ $results->{nth_worst_quality} } ) {
915 0 say
916 "$nth-worst quality internal ID (PUID, name): $n->{internal_id} ($n->{puid}, $n->{name}) -> $n->{quality}";
917 0 ▶ if ( defined $verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $verbose) 0 0 0
918 0 say " combined regex: ", $n->{combined_regex};
919 }
920 0 $nth++;
921 }
922 0 say "";
923  
924 0 say "";
925 0 say "Regular expressions";
926 0 say "---------------------------------------";
927 0 say
928 "Count of multiple used regular expressions: $results->{count_of_multiple_used_regex}";
929 0 ▶ if ( defined $verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $verbose) 0 0 0
930 0 ▶ for ( my $i = 0 ; $i < $results->{count_of_multiple_used_regex} ; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $results->{"count_of_multiple_used_regex"}) 0 0 0
931 {
932 0 say " common regex group no $i:";
933 say " regex='"
934 0 . $results->{multiple_used_regex}->[$i]->{regex} . "'";
935 say " internal IDs: ",
936 0 join( ",", @{ $results->{multiple_used_regex}->[$i]->{internal_ids} } );
937 }
938 }
939 0 say "";
940  
941 #my @rx = $self->get_all_regular_expressions();
942 #use Data::Printer;
943 #p( %uniq_regexes );
944 0 return;
945 }
946  
947 1 1;
948  
949 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
950 1 __PACKAGE__->meta->make_immutable;
951 1 1;
952  
953 __END__
954  
955 =pod
956  
957 =encoding UTF-8
958  
959 =head1 NAME
960  
961 File::FormatIdentification::Pronom
962  
963 =head1 VERSION
964  
965 version 0.06
966  
967 =head1 SYNOPSIS
968  
969 use File::FormatIdentification::Pronom;
970 my $pronomfile = "Droid-Signature.xml";
971 my ( $signatures, $internals ) = parse_signaturefile($pronomfile);
972  
973 =head1 DESCRIPTION
974  
975 The module allows to handle Droid signatures. Droid is a utility which
976 uses the PRONOM database to identify file formats.
977  
978 See https://www.nationalarchives.gov.uk/PRONOM/ for details.
979  
980 With this module you could:
981  
982 =over
983  
984 =item convert Droid signatures to Perl regular expressions
985  
986 =item analyze files and display which/where pattern of Droid signature matches via tag-files for wxHexEditor
987  
988 =item calc statistics about Droid signatures
989  
990 =back
991  
992 The module is in early alpha state and should not be used in production.
993  
994 =head2 Examples
995  
996 =head3 Colorize wxHexeditor fields
997  
998 See example file F<bin/pronom2wxhexeditor.pl>. This colorizes the hex-blob to check PRONOM pattern matches for a given file.
999  
1000 =head3 Identify file
1001  
1002 There are better tools for the job, but as a proof of concept certainly not bad: Identifying the file type of a file.
1003  
1004 my $pronom = File::FormatIdentification::Pronom->new(
1005 "droid_signature_filename" => $pronomfile
1006 );
1007 # .. $filestream is a scalar representing a file
1008 foreach my $internalid ( $pronom->get_all_internal_ids() ) {
1009 my $sig = $pronom->get_signature_id_by_internal_id($internalid);
1010 next unless defined $sig;
1011 my @regexes = $pronom->get_regular_expressions_by_internal_id($internalid);
1012 if ( all {$filestream =~ m/$_/saa} @regexes ) {
1013 my $puid = $pronom->get_puid_by_signature_id($sig);
1014 my $name = $pronom->get_name_by_signature_id($sig);
1015 my $quality = $pronom->get_qualities_by_internal_id($internalid);
1016 say "$binaryfile identified as $name with PUID $puid (regex quality $quality)";
1017 }
1018 }
1019  
1020 See example file F<bin/pronomidentify.pl> for a full working script.
1021  
1022 =head3 Get PRONOM Statistics
1023  
1024 To get a feeling for which signatures need to be revised in PRONOM, or why which file formats are difficult to recognize,
1025 you can get detailed statistics for given signature files.
1026  
1027 In the blog entry under L<https://kulturreste.blogspot.com/2018/10/heres-tool-make-it-work.html> the statistic report is presented in more detail.
1028  
1029 =head2 EXPORT
1030  
1031 None by default.
1032  
1033 =head1 NAME
1034  
1035 File::FormatIdentification::Pronom - Perl extension for parsing PRONOM-Signatures using DROID-Signature file
1036  
1037 =head1 SEE ALSO
1038  
1039 L<File::FormatIdentification::Regex>
1040  
1041 =head1 AUTHOR
1042  
1043 Andreas Romeyke L<pause@andreas-romeyke.de>
1044  
1045 =head1 COPYRIGHT AND LICENSE
1046  
1047 Copyright (C) 2018/19/20 by Andreas Romeyke
1048  
1049 This library is free software; you can redistribute it and/or modify
1050 it under the same terms as Perl itself, either Perl version 5.24.1 or,
1051 at your option, any later version of Perl 5 you may have available.
1052  
1053 The droid-signature file in t/ is from L<https://www.nationalarchives.gov.uk/PRONOM/Default.aspx>
1054 and without guarantee, it does not look like it is legally protected. If there are any legal claims,
1055 please let me know that I can remove them from the distribution.
1056  
1057 =head1 BUGS
1058  
1059 =over
1060  
1061 =item Some droid recipes results in PCREs which are greedy and therefore the running
1062 time could be exponential with size of binary file.
1063  
1064 =back
1065  
1066 =head1 CONTRIBUTING
1067  
1068 Please feel free to send me comments and patches to my email address. You can clone the modules
1069 from L<https://art1pirat.spdns.org/art1/File-FormatIdentification-Pronom> and send me merge requests.
1070  
1071 =head1 AUTHOR
1072  
1073 Andreas Romeyke <pause@andreas-romeyke.de>
1074  
1075 =head1 COPYRIGHT AND LICENSE
1076  
1077 This software is copyright (c) 2018 by Andreas Romeyke.
1078  
1079 This is free software; you can redistribute it and/or modify it under
1080 the same terms as the Perl 5 programming language system itself.
1081  
1082 =cut
↑ Summary lib/File/FormatIdentification/Regex.pm »