Image/ExifTool/Torrent.pm

filter: Statementstmt 81.8% 113 / 138 Branchbran 60.7% 62 / 102 Conditioncond 38.4% 10 / 26 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 4 totaltotal 66.6% 194 / 291 CC 62 SCAR 38.1
CC
62
Cov
70%
ExtractTags
54.7
ReadBencode
54.1
ProcessTorrent
8.6
CRAP 45.1
« Image/ExifTool/VCard.pm ↑ Summary Image/ExifTool/ITC.pm »
1 #------------------------------------------------------------------------------
2 # File: Torrent.pm
3 #
4 # Description: Read information from BitTorrent file
5 #
6 # Revisions: 2013/08/27 - P. Harvey Created
7 #
8 # References: 1) https://wiki.theory.org/BitTorrentSpecification
9 #------------------------------------------------------------------------------
10  
11 package Image::ExifTool::Torrent;
12  
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Image::ExifTool qw(:DataAccess :Utils);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 $VERSION = '1.08';
18  
19 sub ReadBencode($$$);
20 sub ExtractTags($$$;$$@);
21  
22 # tags extracted from BitTorrent files
23 1 %Image::ExifTool::Torrent::Main = (
24 GROUPS => { 2 => 'Document' },
25 NOTES => q{
26 Below are tags commonly found in BitTorrent files. As well as these tags,
27 any other existing tags will be extracted. For convenience, list items are
28 expanded into individual tags with an index in the tag name, but only the
29 tags with index "1" are listed in the tables below. See
30 L<https://wiki.theory.org/BitTorrentSpecification> for the BitTorrent
31 specification.
32 },
33 'announce' => { },
34 'announce-list' => { Name => 'AnnounceList1' },
35 'comment' => { },
36 'created by' => { Name => 'Creator' }, # software used to create the torrent
37 'creation date' => {
38 Name => 'CreateDate',
39 Groups => { 2 => 'Time' },
40 ValueConv => 'ConvertUnixTime($val,1)',
41 PrintConv => '$self->ConvertDateTime($val)',
42 },
43 'encoding' => { },
44 'info' => {
45 SubDirectory => { TagTable => 'Image::ExifTool::Torrent::Info' },
46 Notes => 'extracted as a structure with the Struct option',
47 },
48 'url-list' => { Name => 'URLList1' },
49 );
50  
51 1 %Image::ExifTool::Torrent::Info = (
52 GROUPS => { 2 => 'Document' },
53 'file-duration' => { Name => 'File1Duration' },
54 'file-media' => { Name => 'File1Media' },
55 'files' => { SubDirectory => { TagTable => 'Image::ExifTool::Torrent::Files' } },
56 'length' => { },
57 'md5sum' => { Name => 'MD5Sum' },
58 'name' => { },
59 'name.utf-8' => { Name => 'NameUTF-8' },
60 'piece length' => { Name => 'PieceLength' },
61 'pieces' => {
62 Name => 'Pieces',
63 Notes => 'concatenation of 20-byte SHA-1 digests for each piece',
64 },
65 'private' => { },
66 'profiles' => { SubDirectory => { TagTable => 'Image::ExifTool::Torrent::Profiles' } },
67 );
68  
69 1 %Image::ExifTool::Torrent::Profiles = (
70 GROUPS => { 2 => 'Document' },
71 'width' => { Name => 'Profile1Width' },
72 'height' => { Name => 'Profile1Height' },
73 'acodec' => { Name => 'Profile1AudioCodec' },
74 'vcodec' => { Name => 'Profile1VideoCodec' },
75 );
76  
77 1 %Image::ExifTool::Torrent::Files = (
78 GROUPS => { 2 => 'Document' },
79 'length' => { Name => 'File1Length', PrintConv => 'ConvertFileSize($val)' },
80 'md5sum' => { Name => 'File1MD5Sum' },
81 'path' => { Name => 'File1Path', JoinPath => 1 },
82 'path.utf-8' => { Name => 'File1PathUTF-8', JoinPath => 1 },
83 );
84  
85 #------------------------------------------------------------------------------
86 # Read 64kB more data into buffer
87 # Inputs: 0) RAF ref, 1) buffer ref
88 # Returns: number of bytes read
89 # Notes: Sets BencodeEOF element of RAF on end of file
90 sub ReadMore($$)
91 1 ▶{
Subroutine ReadMore: called CC=3 SCAR=11.2
Pod undocumented
92 1 my ($raf, $dataPt) = @_;
93 1 my $buf2;
94 1 my $n = $raf->Read($buf2, 65536);
95 1 ▶ $$raf{BencodeEOF} = 1 if $n != 65536;
Branch: 1 branch
BranchTrueFalseTotal
if $n != 65536 1 0 1
96 1 ▶ $$dataPt = substr($$dataPt, pos($$dataPt)) . $buf2 if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 1 0 1
97 1 return $n;
98 }
99  
100 #------------------------------------------------------------------------------
101 # Read bencoded value
102 # Inputs: 0) ExifTool ref, 1) input file, 2) buffer (pos must be set to current position)
103 # Returns: HASH ref, ARRAY ref, SCALAR ref, SCALAR, or undef on error or end of data
104 # Notes: Sets BencodeError element of RAF on any error
105 sub ReadBencode($$$)
106 70 ▶{
Subroutine ReadBencode: called CC=26 SCAR=39.9
Pod undocumented
107 70 my ($et, $raf, $dataPt) = @_;
108  
109 # read more if necessary (keep a minimum of 64 bytes in the buffer)
110 70 my $pos = pos($$dataPt);
111 70 ▶ return undef unless defined $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 70 70
112 70 my $remaining = length($$dataPt) - $pos;
113 70 ▶ ReadMore($raf, $dataPt) if $remaining < 64 and not $$raf{BencodeEOF};
Branch: 1 branch
BranchTrueFalseTotal
if $remaining < 64 and not $raf->{"BencodeEOF"} 1 69 70
Condition100%3/3
$remaining < 64 and not $raf->{"BencodeEOF"}
!ll&&!rl&&r
6451
MC/DC100%2/2
$remaining < 64 and not $raf->{"BencodeEOF"}
$remaining < 64 not $raf->{"BencodeEOF"}
Truth table100%3/3
A and not(B)
A= $remaining < 64B= not $raf->{"BencodeEOF"}
ABresult
0X0
100
111
114  
115 # read next token
116 70 ▶ $$dataPt =~ /(.)/sg or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$$dataPt =~ /(.)/gs or return undef 70 0 70
117  
118 70 my $val;
119 70 my $tok = $1;
120 70 ▶ if ($tok eq 'i') { # integer
Branch: 5 branches
BranchTrueFalseTotal
if ($tok eq 'i') { } 6 64 70
elsif ($tok eq 'd') { } 6 58 64
elsif ($tok eq 'l') { } 8 50 58
elsif ($tok eq 'e') { } 14 36 50
elsif ($tok =~ /^\d$/ and $$dataPt =~ /\G(\d*):/g) { } 36 0 36
121 6 ▶ $$dataPt =~ /\G(-?\d+)e/g or return $val;
Branch: 1 branch
BranchTrueFalseTotal
$$dataPt =~ /\G(-?\d+)e/g or return $val 6 0 6
122 6 $val = $1;
123 } elsif ($tok eq 'd') { # dictionary
124 6 $val = { };
125 6 for (;;) {
126 26 my $k = ReadBencode($et, $raf, $dataPt);
127 26 ▶ last unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 6 20 26
128 # the key must be a byte string
129 20 ▶ if (ref $k) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $k) 0 20 20
130 0 ▶ ref $k ne 'SCALAR' and $$raf{BencodeError} = 'Bad dictionary key', last;
Branch: 1 branch
BranchTrueFalseTotal
ref $k ne "SCALAR" and $raf->{"BencodeError"} = "Bad dictionary key", last 0 0 0
131 0 $k = $$k;
132 }
133 20 my $v = ReadBencode($et, $raf, $dataPt);
134 20 ▶ last unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 20 20
135 20 $$val{$k} = $v;
136 }
137 } elsif ($tok eq 'l') { # list
138 8 $val = [ ];
139 8 for (;;) {
140 23 my $v = ReadBencode($et, $raf, $dataPt);
141 23 ▶ last unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 8 15 23
142 15 push @$val, $v;
143 }
144 } elsif ($tok eq 'e') { # end of dictionary or list
145 # return undef (no error)
146 ▶ } elsif ($tok =~ /^\d$/ and $$dataPt =~ /\G(\d*):/g) { # byte string
Condition33%1/3
$tok =~ /^\d$/ and $$dataPt =~ /\G(\d*):/g
!ll&&!rl&&r
0036
MC/DC0%0/2
$tok =~ /^\d$/ and $$dataPt =~ /\G(\d*):/g
$tok =~ /^\d$/ $$dataPt =~ /\G(\d*):/g
Truth table33%1/3
A and B
A= $tok =~ /^\d$/B= $$dataPt =~ /\G(\d*):/g
ABresult
0X0
100
111
147 36 my $len = $tok . $1;
148 36 my $more = $len - (length($$dataPt) - pos($$dataPt));
149 36 my $value;
150 36 ▶ if ($more <= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($more <= 0) { } 36 0 36
elsif ($more > 10000000) { } 0 0 0
151 36 $value = substr($$dataPt,pos($$dataPt),$len);
152 36 pos($$dataPt) = pos($$dataPt) + $len;
153 } elsif ($more > 10000000) {
154 # just skip over really long values
155 0 ▶ $val = \ "(Binary data $len bytes)" if $raf->Seek($more, 1);
Branch: 1 branch
BranchTrueFalseTotal
if $raf->Seek($more, 1) 0 0 0
156 } else {
157 # need to read more from file
158 0 my $buff;
159 0 my $n = $raf->Read($buff, $more);
160 0 ▶ if ($n == $more) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == $more) 0 0 0
161 0 $value = substr($$dataPt,pos($$dataPt)) . $buff;
162 0 $$dataPt = '';
163 0 pos($$dataPt) = 0;
164 }
165 }
166 36 ▶ if (defined $value) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $value) { } 36 0 36
elsif (not defined $val) { } 0 0 0
167 # return as binary data unless it is a reasonable-length ASCII string
168 36 ▶ if (length($value) > 256) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $value > 256) { } 0 36 36
elsif ($value =~ /[^\t\x20-\x7e]/) { } 1 35 36
169 0 $val = \$value;
170 } elsif ($value =~ /[^\t\x20-\x7e]/) {
171 1 ▶ if (Image::ExifTool::IsUTF8(\$value) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Image::ExifTool::IsUTF8(\$value) >= 0) { } 0 1 1
172 0 $val = $et->Decode($value, 'UTF8');
173 } else {
174 1 $val = \$value;
175 }
176 } else {
177 35 $val = $value;
178 }
179 } elsif (not defined $val) {
180 0 $$raf{BencodeError} = 'Truncated byte string';
181 }
182 } else {
183 0 $$raf{BencodeError} = 'Bad format';
184 }
185 70 return $val;
186 }
187  
188 #------------------------------------------------------------------------------
189 # Extract tags from dictionary hash
190 # Inputs: 0) ExifTool ref, 1) dictionary hash reference, 2) tag table ref,
191 # 3) parent hash ID, 4) parent hash name, 5-N) list indices
192 # Returns: number of tags extracted
193 sub ExtractTags($$$;$$@)
194 6 ▶{
Subroutine ExtractTags: called CC=29 SCAR=40.0
Pod undocumented
195 6 my ($et, $hashPtr, $tagTablePtr, $baseID, $baseName, @index) = @_;
196 6 my $count = 0;
197 6 my $tag;
198 6 foreach $tag (sort keys %$hashPtr) {
199 20 my $val = $$hashPtr{$tag};
200 20 my ($i, $j, @more);
201 20 ▶ for (; defined $val; $val = shift @more) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 27 20 47
202 27 ▶ my $id = defined $baseID ? "$baseID/$tag" : $tag;
Branch: 1 branch
BranchTrueFalseTotal
defined $baseID ? : 0 27 27
203 27 ▶ unless ($$tagTablePtr{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tagTablePtr->{$id}) 0 27 27
204 0 my $name = ucfirst $tag;
205 # capitalize all words in tag name and remove illegal characters
206 0 $name =~ s/[^-_a-zA-Z0-9]+(.?)/\U$1/g;
207 0 ▶ $name = "Tag$name" if length($name) < 2 or $name !~ /^[A-Z]/;
Branch: 1 branch
BranchTrueFalseTotal
if length $name < 2 or not $name =~ /^[A-Z]/ 0 0 0
Condition0%0/3
length $name < 2 or not $name =~ /^[A-Z]/
l!l&&r!l&&!r
000
MC/DC0%0/2
length $name < 2 or not $name =~ /^[A-Z]/
length $name < 2 not $name =~ /^[A-Z]/
Truth table0%0/3
A or not(B)
A= length $name < 2B= not $name =~ /^[A-Z]/
ABresult
1X1
011
000
208 0 ▶ $name = $baseName . $name if defined $baseName; # add base name if necessary
Branch: 1 branch
BranchTrueFalseTotal
if defined $baseName 0 0 0
209 0 AddTagToTable($tagTablePtr, $id, { Name => $name });
210 0 $et->VPrint(0, " [adding $id '${name}']\n");
211 }
212 27 ▶ my $tagInfo = $et->GetTagInfo($tagTablePtr, $id) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $tagInfo = $et->GetTagInfo($tagTablePtr, $id) or next 27 0 27
213 27 ▶ if (ref $val eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "ARRAY") 8 19 27
214 8 ▶ if ($$tagInfo{JoinPath}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagInfo->{'JoinPath'}) { } 4 4 8
215 4 ▶ $val = join '/', map { ref $_ ? '(Binary data)' : $_ } @$val;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 5 5
216 } else {
217 4 ▶ next unless @$val; # ignore empty arrays
Branch: 1 branch
BranchTrueFalseTotal
unless @$val 0 4 4
218 4 push @more, @$val;
219 4 ▶ next if ref $more[0] eq 'ARRAY'; # continue expanding nested lists
Branch: 1 branch
BranchTrueFalseTotal
if ref $more[0] eq "ARRAY" 1 3 4
220 3 $val = shift @more;
221 3 ▶ $i or $i = 0, push(@index, $i);
Branch: 1 branch
BranchTrueFalseTotal
$i or $i = 0, push(@index, $i) 0 3 3
222 }
223 }
224 26 ▶ $index[-1] = ++$i if defined $i;
Branch: 1 branch
BranchTrueFalseTotal
if defined $i 9 17 26
225 26 ▶ if (@index) {
Branch: 1 branch
BranchTrueFalseTotal
if (@index) 17 9 26
226 17 $id .= join '_', @index; # add instance number(s) to tag ID
227 17 ▶ unless ($$tagTablePtr{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tagTablePtr->{$id}) 17 0 17
228 17 my $name = $$tagInfo{Name};
229 # embed indices at position of '1' in tag name
230 17 my $n = ($name =~ tr/1/#/);
231 17 ▶ for ($j=0; $j<$n; ++$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $n) 13 17 30
232 13 ▶ my $idx = $index[$j] || '';
Condition50%1/2
$index[$j] || ''
l!l
130
MC/DC0%0/1
$index[$j] || ''
$index[$j]
Truth table50%1/2
A
A= $index[$j]
Aresult
11
00
233 13 $name =~ s/#/$idx/;
234 }
235 # put remaining indices at end of tag name
236 17 ▶ for (; $j<@index; ++$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @index) 4 17 21
237 4 ▶ $name .= '_' if $name =~ /\d$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\d$/ 0 4 4
238 4 $name .= $index[$j];
239 }
240 17 AddTagToTable($tagTablePtr, $id, { %$tagInfo, Name => $name });
241 }
242 17 ▶ $tagInfo = $et->GetTagInfo($tagTablePtr, $id) or next;
Branch: 1 branch
BranchTrueFalseTotal
$tagInfo = $et->GetTagInfo($tagTablePtr, $id) or next 17 0 17
243 }
244 26 ▶ if (ref $val eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'HASH') { } 5 21 26
245 5 ▶ if ($et->Options('Struct') and $tagInfo and $$tagInfo{Name} eq 'Info') {
Branch: 1 branch
BranchTrueFalseTotal
if ($et->Options("Struct") and $tagInfo and $tagInfo->{"Name"} eq "Info") 0 5 5
Condition33%2/6
$et->Options("Struct") and $tagInfo and $tagInfo->{"Name"} eq "Info"
!ll&&!rl&&r
500
$et->Options("Struct") and $tagInfo
!ll&&!rl&&r
500
MC/DC0%0/3
$et->Options("Struct") and $tagInfo and $tagInfo->{"Name"} eq "Info"
$et->Options("Struct") $tagInfo $tagInfo->{"Name"} eq "Info"
Truth table25%1/4
A and B and C
A= $et->Options("Struct")B= $tagInfoC= $tagInfo->{"Name"} eq "Info"
ABCresult
0XX0
10X0
1100
1111
246 0 $et->FoundTag($tagInfo, $val);
247 0 ++$count;
248 0 next;
249 }
250 # extract tags from this dictionary
251 5 my ($table, $rootID, $rootName);
252 5 ▶ if ($$tagInfo{SubDirectory}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagInfo->{'SubDirectory'}) { } 5 0 5
253 5 $table = GetTagTable($$tagInfo{SubDirectory}{TagTable});
254 } else {
255 0 $table = $tagTablePtr;
256 # use hash ID and Name as base for contained tags to avoid conflicts
257 0 $rootID = $id;
258 0 $rootName = $$tagInfo{Name};
259 }
260 5 $count += ExtractTags($et, $val, $table, $rootID, $rootName, @index);
261 } else {
262 # handle this simple tag value
263 21 $et->HandleTag($tagTablePtr, $id, $val);
264 21 ++$count;
265 }
266 }
267 20 ▶ pop @index if defined $i;
Branch: 1 branch
BranchTrueFalseTotal
if defined $i 3 17 20
268 }
269 6 return $count;
270 }
271  
272 #------------------------------------------------------------------------------
273 # Process BitTorrent file
274 # Inputs: 0) ExifTool object reference, 1) dirInfo reference (with RAF set)
275 # Returns: 1 on success, 0 if this wasn't a valid BitTorrent file
276 sub ProcessTorrent($$)
277 1 ▶{
Subroutine ProcessTorrent: called CC=7 SCAR=21.5
Pod undocumented
278 1 my ($et, $dirInfo) = @_;
279 1 my $success = 0;
280 1 my $raf = $$dirInfo{RAF};
281 1 my $buff = '';
282 1 pos($buff) = 0;
283 1 my $dict = ReadBencode($et, $raf, \$buff);
284 1 my $err = $$raf{BencodeError};
285 1 ▶ $et->Warn("Bencode error: $err") if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 1 1
286 1 ▶ if (ref $dict eq 'HASH' and ($$dict{announce} or $$dict{'created by'} or $$dict{info})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dict eq "HASH" and $dict->{"announce"} || $dict->{"created by"} || $dict->{"info"}) 1 0 1
Condition33%3/9
ref $dict eq "HASH" and $dict->{"announce"} || $dict->{"created by"} || $dict->{"info"}
!ll&&!rl&&r
001
$dict->{"announce"} or $dict->{"created by"} or $dict->{"info"}
l!l&&r!l&&!r
100
$dict->{"announce"} or $dict->{"created by"}
l!l&&r!l&&!r
100
MC/DC0%0/4
ref $dict eq "HASH" and $dict->{"announce"} || $dict->{"created by"} || $dict->{"info"}
ref $dict eq "HASH" $dict->{"announce"} $dict->{"created by"} $dict->{"info"}
Truth table20%1/5
A and B or C or D
A= ref $dict eq "HASH"B= $dict->{"announce"}C= $dict->{"created by"}D= $dict->{"info"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
287 1 $et->SetFileType();
288 1 my $tagTablePtr = GetTagTable('Image::ExifTool::Torrent::Main');
289 1 ▶ ExtractTags($et, $dict, $tagTablePtr) and $success = 1;
Branch: 1 branch
BranchTrueFalseTotal
&ExtractTags($et, $dict, $tagTablePtr) and $success = 1 1 0 1
290 }
291 1 return $success;
292 }
293  
294 1 1; # end
295  
296 __END__
« Image/ExifTool/VCard.pm ↑ Summary Image/ExifTool/ITC.pm »