lib/File/FormatIdentification/RandomSampling.pm

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