lib/File/FormatIdentification/RandomSampling.pm

filter: Statementstmt 98.4% 127 / 129 Branchbran 89.2% 25 / 28 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 15 / 15 Podpod 88.8% 8 / 9 totaltotal 96.7% 180 / 186 CC 24 SCAR 13.1
CC
24
Cov
97%
calc_type
5.3
update_bytegram
5.0
is_text
5.0
CRAP 3.7
« blib/lib/File/FormatIdentification/RandomSampling/Model.pm ↑ Summary blib/script/crazy_fast_image_scan.pl »
1 package File::FormatIdentification::RandomSampling;
2 # ABSTRACT: methods to identify content of device o media files using random sampling
3 3 our $VERSION = '0.006'; # VERSION:
4 # (c) 2020/2021 by Andreas Romeyke
5 # licensed via GPL v3.0 or later
6  
7  
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 has 'bytegram' => (
14 is => 'rw',
15 isa => 'ArrayRef',
16 1 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
17 3 );
18  
19  
20  
21  
22 8 ▶sub init_bytegrams {
Subroutine init_bytegrams: called CC=1 SCAR=0.0
Pod documented
23 8 my $self = shift;
24 8 my $bytegram_ref = $self->{'bytegram'};
25 8 $bytegram_ref->[0] = [(0) x 256]; # onegram
26 8 $bytegram_ref->[1] = [(0) x 65536]; #bigram
27 8 return 1;
28 }
29  
30 1 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
31 1 my $self = shift;
32 1 $self->init_bytegrams();
33 1 return 1;
34 }
35  
36  
37  
38 8 ▶sub update_bytegram {
Subroutine update_bytegram: called CC=5 SCAR=16.1
Pod documented
39 8 my $self = shift;
40 8 my $buffer = shift;
41 8 ▶ if (defined $buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $buffer) 7 1 8
42 7 my $bytegram_ref = $self->{'bytegram'};
43 7 my @bytes = unpack "C*", $buffer;
44 7 my @words = unpack "S*", $buffer;
45 # my @bytes = map{ ord($_)} split //, $buffer;
46 7 ▶ if (scalar @bytes > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @bytes > 0) 7 0 7
47 7 my @onegram = @{$bytegram_ref->[0]};
48 7 my @bigram = @{$bytegram_ref->[1]};
49 7 foreach my $byte (@bytes) {
50 2562 $onegram[$byte]++;
51 }
52 7 foreach my $word (@words) {
53 1280 $bigram[$word]++;
54 }
55 7 $bytegram_ref->[0] = \@onegram;
56 7 $bytegram_ref->[1] = \@bigram;
57 }
58 }
59 8 return 1;
60 }
61  
62  
63 5 ▶sub calc_histogram { # use only the most significant first 8 entries
Subroutine calc_histogram: called CC=3 SCAR=11.0
Pod documented
64 5 my $self = shift;
65 5 my $bytegram_ref = $self->{'bytegram'};
66 5 my @bytes_sorted = sort {$bytegram_ref->[0]->[$b] <=> $bytegram_ref->[0]->[$a]} (0..255);
67 5 my @words_sorted = sort {$bytegram_ref->[1]->[$b] <=> $bytegram_ref->[1]->[$a]} (0 .. 65535);
68 # show only 8 most onegrame bytes
69 5 my @bytes_truncated = @bytes_sorted[0..7];
70 5 my @words_truncated = @words_sorted[0..7];
71 5 my %histogram;
72 5 foreach my $byte (@bytes_truncated) {
73 40 push @{$histogram{onegram}}, $byte; #$bytegram_ref->[0]->[$byte];
74 }
75 5 foreach my $word (@words_truncated) {
76 40 push @{$histogram{bigram}}, $word; #$bytegram_ref->[1]->[$word];
77 }
78 5 return \%histogram;
79 }
80  
81  
82 5 ▶sub is_uniform {
Subroutine is_uniform: called CC=4 SCAR=13.9
Pod documented
83 5 my $self = shift;
84 #say "is_uniform?";
85 5 my $bytegram_ref = $self->{'bytegram'};
86 5 my $sum = 0;
87 5 my $n = 0;
88 5 my @unigram = @{$bytegram_ref->[0]};
89 5 foreach my $byte (0 .. 255) {
90 1280 ▶ if ($unigram[$byte] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unigram[$byte] > 0) 285 995 1280
91 285 $n += $unigram[$byte];
92 285 $sum += ($unigram[$byte] * $byte);
93 }
94 }
95 5 ▶ if ($n == 0) { return;}
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0) 2 3 5
96 3 my $expected = (256)/2;
97 3 my $mean = ($sum/$n);
98 #say "expected=$expected, sum=$sum, mean=$mean";
99 3 return (abs($expected - $mean) < 4);
100 }
101  
102  
103 7 ▶sub is_empty {
Subroutine is_empty: called CC=4 SCAR=13.9
Pod documented
104 7 my $self = shift;
105 #say "is_empty?";
106 7 my $bytegram_ref = $self->{'bytegram'};
107 7 my $sum = 0;
108 7 my $n = 0;
109 7 my @unigram = @{$bytegram_ref->[0]};
110 7 foreach my $byte (0 .. 255) {
111 1792 ▶ if ($unigram[$byte] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unigram[$byte] > 0) 314 1478 1792
112 314 $n += $unigram[$byte];
113 314 $sum += ($unigram[$byte] * $byte);
114 }
115 }
116 7 ▶ if ($n == 0) { return;}
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0) 2 5 7
117 5 my $expected = 0;
118 5 my $mean = ($sum/$n);
119 # say "expected=$expected, mean=$mean";
120 5 my $criteria = abs($expected - $mean) < 4;
121 5 return ( $criteria);
122 }
123  
124  
125 6 ▶sub is_text {
Subroutine is_text: called CC=5 SCAR=16.1
Pod documented
126 6 my $self = shift;
127 #say "is_text?";
128 6 my $bytegram_ref = $self->{'bytegram'};
129 # many Bytes in range 32 .. 173
130 6 my $printable = 0;
131 6 my $non_printable = 0;
132 6 my @unigram = @{$bytegram_ref->[0]};
133 6 foreach my $byte (0 .. 255) {
134 #say "bytegram[$byte] = ". $bytegram_ref->[0]->[$byte];
135 1536 ▶ if ($unigram[$byte] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unigram[$byte] > 0) 313 1223 1536
136 313 ▶ if (($byte >= 32) && ($byte <= 173)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byte >= 32 and $byte <= 173) { } 196 117 313
Condition100%3/3
$byte >= 32 and $byte <= 173
!ll&&!rl&&r
3582196
MC/DC100%2/2
$byte >= 32 and $byte <= 173
$byte >= 32 $byte <= 173
Truth table100%3/3
A and B
A= $byte >= 32B= $byte <= 173
ABresult
0X0
100
111
137 196 $printable += ($unigram[$byte]);
138 }
139 else {
140 117 $non_printable += ($unigram[$byte]);
141 }
142 }
143 }
144 6 my $ratio = $printable / ($printable + $non_printable + 1); # +1 to avoid division by zero
145 #say "ratio text = $ratio (print=$printable, nonprint=$non_printable";
146 6 return ($ratio > 0.9);
147 }
148  
149  
150 4 ▶sub is_video { # quicktime
Subroutine is_video: called CC=4 SCAR=13.9
Pod documented
151 4 my $self = shift;
152 #say "is_video?";
153 4 my $bytegram_ref = $self->{'bytegram'};
154 # many Bytes with 0x6d, ratio > 1/256 per read Byte
155 4 my $mp_indicator = 0;
156 4 my $other = 0;
157 4 my @unigram = @{$bytegram_ref->[0]};
158 # MPEG-TS: Synchrobyte = 0x47 5times with distance of 188bytes
159 # MP4/Quicktime: Atom 'mvhd'
160 # General: 0x6d value
161 4 foreach my $byte ( 0 .. 255) {
162 1024 ▶ if ($unigram[$byte] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unigram[$byte] > 0) 257 767 1024
163 257 ▶ if ($byte != 0x6d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byte != 109) { } 256 1 257
164 256 $other += $unigram[$byte];
165 } else { # $byte = 0x6d
166 1 $mp_indicator += $unigram[$byte];
167 }
168 }
169 }
170 4 my $ratio = $mp_indicator / ($mp_indicator + $other + 1); # +1 to avoid division by zero
171 #say "ratio=$ratio ($mp_indicator / ".($mp_indicator + $other + 1).") 47=", chr(0x47);
172 4 return ($ratio > 2/256);
173 }
174  
175  
176  
177 3 ▶sub calc_type {
Subroutine calc_type: called CC=5 SCAR=16.6
Pod documented
178 3 my $self = shift;
179 3 my $buffer = shift;
180  
181 3 $self->init_bytegrams();
182 3 $self->update_bytegram($buffer);
183  
184 3 ▶ if ($self->is_empty()) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->is_empty) { } 1 2 3
elsif ($self->is_text) { } 1 1 2
elsif ($self->is_video) { } 0 1 1
elsif ($self->is_uniform) { } 0 1 1
185 1 return "empty";
186 }
187 elsif ($self->is_text()) {
188 1 return "text";
189 }
190 elsif ($self->is_video()) {
191 0 return "video/audio";
192 }
193 elsif ($self->is_uniform()) {
194 0 return "random/encrypted/compressed";
195 }
196 1 return "undef";
197 }
198  
199 3 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
200  
201 3 __PACKAGE__->meta->make_immutable;
202  
203 3 1;
204  
205 __END__
« blib/lib/File/FormatIdentification/RandomSampling/Model.pm ↑ Summary blib/script/crazy_fast_image_scan.pl »