Image/ExifTool/BigTIFF.pm

filter: Statementstmt 49.6% 79 / 159 Branchbran 27.1% 25 / 92 Conditioncond 14.2% 5 / 35 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 2 totaltotal 36.3% 115 / 316 CC 59 SCAR 67.3
CC
59
Cov
38%
ProcessBigIFD
995.2
ProcessBTF
8.0
BEGIN
1.0
CRAP 840.5
« Image/ExifTool/EXE.pm ↑ Summary Image/ExifTool/Sony.pm »
1 #------------------------------------------------------------------------------
2 # File: BigTIFF.pm
3 #
4 # Description: Read Big TIFF meta information
5 #
6 # Revisions: 07/03/2007 - P. Harvey Created
7 #
8 # References: 1) http://www.awaresystems.be/imaging/tiff/bigtiff.html
9 #------------------------------------------------------------------------------
10  
11 package Image::ExifTool::BigTIFF;
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 1 ▶use Image::ExifTool::Exif;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 $VERSION = '1.08';
19  
20 1 my $maxOffset = 0x7fffffff; # currently supported maximum data offset/size
21  
22 #------------------------------------------------------------------------------
23 # Process Big IFD directory
24 # Inputs: 0) ExifTool object ref, 1) dirInfo ref, 2) tag table ref
25 # Returns: 1 on success, otherwise returns 0 and sets a Warning
26 sub ProcessBigIFD($$$)
27 1 ▶{
Subroutine ProcessBigIFD: called CC=54 SCAR=69.0
Pod undocumented
28 1 my ($et, $dirInfo, $tagTablePtr) = @_;
29 1 my $raf = $$dirInfo{RAF};
30 1 my $verbose = $$et{OPTIONS}{Verbose};
31 1 my $htmlDump = $$et{HTML_DUMP};
32 1 my $dirName = $$dirInfo{DirName};
33 1 my $dirStart = $$dirInfo{DirStart};
34 1 my ($offName, $nextOffName);
35  
36 1 ▶ if ($htmlDump) {
Branch: 1 branch
BranchTrueFalseTotal
if ($htmlDump) 0 1 1
37 0 $verbose = -1; # mix htmlDump into verbose so we can test for both at once
38 0 $offName = $$dirInfo{OffsetName};
39 }
40  
41 # loop through IFD chain
42 1 for (;;) {
43 1 ▶ if ($dirStart > $maxOffset and not $et->Options('LargeFileSupport')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dirStart > $maxOffset and not $et->Options("LargeFileSupport")) 0 1 1
Condition33%1/3
$dirStart > $maxOffset and not $et->Options("LargeFileSupport")
!ll&&!rl&&r
100
MC/DC0%0/2
$dirStart > $maxOffset and not $et->Options("LargeFileSupport")
$dirStart > $maxOffset not $et->Options("LargeFileSupport")
Truth table33%1/3
A and not(B)
A= $dirStart > $maxOffsetB= not $et->Options("LargeFileSupport")
ABresult
0X0
100
111
44 0 $et->Warn('Huge offsets not supported (LargeFileSupport not set)');
45 0 last;
46 }
47 1 ▶ unless ($raf->Seek($dirStart, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($raf->Seek($dirStart, 0)) 0 1 1
48 0 $et->Warn("Bad $dirName offset");
49 0 return 0;
50 }
51 1 my ($dirBuff, $index);
52 1 ▶ unless ($raf->Read($dirBuff, 8) == 8) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($raf->Read($dirBuff, 8) == 8) 0 1 1
53 0 $et->Warn("Truncated $dirName count");
54 0 return 0;
55 }
56 1 my $numEntries = Image::ExifTool::Get64u(\$dirBuff, 0);
57 1 ▶ $verbose > 0 and $et->VerboseDir($dirName, $numEntries);
Branch: 1 branch
BranchTrueFalseTotal
$verbose > 0 and $et->VerboseDir($dirName, $numEntries) 0 1 1
58 1 my $bsize = $numEntries * 20;
59 1 ▶ if ($bsize > $maxOffset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bsize > $maxOffset) 0 1 1
60 0 $et->Warn('Huge directory counts not yet supported');
61 0 last;
62 }
63 1 my $bufPos = $raf->Tell();
64 1 ▶ unless ($raf->Read($dirBuff, $bsize) == $bsize) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($raf->Read($dirBuff, $bsize) == $bsize) 0 1 1
65 0 $et->Warn("Truncated $dirName directory");
66 0 return 0;
67 }
68 1 my $nextIFD;
69 1 ▶ $raf->Read($nextIFD, 8) == 8 or undef $nextIFD; # try to read next IFD pointer
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($nextIFD, 8) == 8 or undef $nextIFD 1 0 1
70 1 ▶ if ($htmlDump) {
Branch: 1 branch
BranchTrueFalseTotal
if ($htmlDump) 0 1 1
71 0 $et->HDump($bufPos-8, 8, "$dirName entries", "Entry count: $numEntries", undef, $offName);
72 0 ▶ if (defined $nextIFD) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nextIFD) 0 0 0
73 0 my $off = Image::ExifTool::Get64u(\$nextIFD, 0);
74 0 my $tip = sprintf("Offset: 0x%.8x", $off);
75 0 my $id = $offName;
76 0 ▶ ($nextOffName, $id) = Image::ExifTool::Exif::NextOffsetName($et, $id) if $off;
Branch: 1 branch
BranchTrueFalseTotal
if $off 0 0 0
77 0 $et->HDump($bufPos + 20 * $numEntries, 8, "Next IFD", $tip, 0, $id);
78 }
79 }
80 # loop through all entries in this BigTIFF IFD
81 1 ▶ for ($index=0; $index<$numEntries; ++$index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < $numEntries) 8 1 9
82 8 my $entry = 20 * $index;
83 8 my $tagID = Get16u(\$dirBuff, $entry);
84 8 my $format = Get16u(\$dirBuff, $entry+2);
85 8 my $count = Image::ExifTool::Get64u(\$dirBuff, $entry+4);
86 8 my $formatSize = $Image::ExifTool::Exif::formatSize[$format];
87 8 ▶ unless (defined $formatSize) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $formatSize) 0 8 8
88 0 $et->HDump($bufPos+$entry,20,"[invalid IFD entry]",
89 "Bad format value: $format", 1, $offName);
90 # warn unless the IFD was just padded with zeros
91 0 $et->Warn(sprintf("Unknown format ($format) for $dirName tag 0x%x",$tagID));
92 0 return 0; # assume corrupted IFD
93 }
94 8 my $formatStr = $Image::ExifTool::Exif::formatName[$format];
95 8 my $size = $count * $formatSize;
96 8 my $tagInfo = $et->GetTagInfo($tagTablePtr, $tagID);
97 8 ▶ next unless defined $tagInfo or $verbose;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tagInfo or $verbose 0 8 8
Condition33%1/3
defined $tagInfo or $verbose
l!l&&r!l&&!r
800
MC/DC0%0/2
defined $tagInfo or $verbose
defined $tagInfo $verbose
Truth table33%1/3
A or B
A= defined $tagInfoB= $verbose
ABresult
1X1
011
000
98 8 my $valuePtr = $entry + 12;
99 8 my ($valBuff, $valBase, $rational, $subOffName);
100 8 ▶ if ($size > 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > 8) { } 0 8 8
101 0 ▶ if ($size > $maxOffset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > $maxOffset) 0 0 0
102 0 $et->Warn("Can't handle $dirName entry $index (huge size)");
103 0 next;
104 }
105 0 $valuePtr = Image::ExifTool::Get64u(\$dirBuff, $valuePtr);
106 0 ▶ if ($valuePtr > $maxOffset and not $et->Options('LargeFileSupport')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valuePtr > $maxOffset and not $et->Options("LargeFileSupport")) 0 0 0
Condition0%0/3
$valuePtr > $maxOffset and not $et->Options("LargeFileSupport")
!ll&&!rl&&r
000
MC/DC0%0/2
$valuePtr > $maxOffset and not $et->Options("LargeFileSupport")
$valuePtr > $maxOffset not $et->Options("LargeFileSupport")
Truth table0%0/3
A and not(B)
A= $valuePtr > $maxOffsetB= not $et->Options("LargeFileSupport")
ABresult
0X0
100
111
107 0 $et->Warn("Can't handle $dirName entry $index (LargeFileSupport not set)");
108 0 next;
109 }
110 0 ▶ unless ($raf->Seek($valuePtr, 0) and $raf->Read($valBuff, $size) == $size) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($raf->Seek($valuePtr, 0) and $raf->Read($valBuff, $size) == $size) 0 0 0
Condition0%0/3
$raf->Seek($valuePtr, 0) and $raf->Read($valBuff, $size) == $size
!ll&&!rl&&r
000
MC/DC0%0/2
$raf->Seek($valuePtr, 0) and $raf->Read($valBuff, $size) == $size
$raf->Seek($valuePtr, 0) $raf->Read($valBuff, $size) == $size
Truth table0%0/3
A and B
A= $raf->Seek($valuePtr, 0)B= $raf->Read($valBuff, $size) == $size
ABresult
0X0
100
111
111 0 $et->Warn("Error reading $dirName entry $index");
112 0 next;
113 }
114 0 $valBase = 0;
115 } else {
116 8 $valBuff = substr($dirBuff, $valuePtr, $size);
117 8 $valBase = $bufPos;
118 }
119 8 ▶ if (defined $tagInfo and not $tagInfo) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tagInfo and not $tagInfo) 0 8 8
Condition33%1/3
defined $tagInfo and not $tagInfo
!ll&&!rl&&r
080
MC/DC0%0/2
defined $tagInfo and not $tagInfo
defined $tagInfo not $tagInfo
Truth table33%1/3
A and not(B)
A= defined $tagInfoB= not $tagInfo
ABresult
0X0
100
111
120 # GetTagInfo() required the value for a Condition
121 0 $tagInfo = $et->GetTagInfo($tagTablePtr, $tagID, \$valBuff);
122 }
123 8 my $val = ReadValue(\$valBuff, 0, $formatStr, $count, $size, \$rational);
124 8 ▶ if ($htmlDump) {
Branch: 1 branch
BranchTrueFalseTotal
if ($htmlDump) 0 8 8
125 0 my $tval = $val;
126 # show numerator/denominator separately for rational numbers
127 0 ▶ $tval .= " ($rational)" if defined $rational;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rational 0 0 0
128 0 my ($tagName, $colName);
129 0 ▶ if ($tagID == 0x927c and $dirName eq 'ExifIFD') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tagID == 37500 and $dirName eq 'ExifIFD') { } 0 0 0
elsif ($tagInfo) { } 0 0 0
Condition0%0/3
$tagID == 37500 and $dirName eq "ExifIFD"
!ll&&!rl&&r
000
MC/DC0%0/2
$tagID == 37500 and $dirName eq "ExifIFD"
$tagID == 37500 $dirName eq "ExifIFD"
Truth table0%0/3
A and B
A= $tagID == 37500B= $dirName eq "ExifIFD"
ABresult
0X0
100
111
130 0 $tagName = 'MakerNotes';
131 } elsif ($tagInfo) {
132 0 $tagName = $$tagInfo{Name};
133 } else {
134 0 $tagName = sprintf("Tag 0x%.4x",$tagID);
135 }
136 0 my $dname = sprintf("$dirName-%.2d", $index);
137 # build our tool tip
138 0 my $tip = sprintf("Tag ID: 0x%.4x\n", $tagID) .
139 "Format: $formatStr\[$count]\nSize: $size bytes\n";
140 0 ▶ if ($size > 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > 8) { } 0 0 0
141 0 $tip .= sprintf("Value offset: 0x%.8x\n", $valuePtr);
142 0 $colName = "<span class=H>$tagName</span>";
143 } else {
144 0 $colName = $tagName;
145 }
146 0 ▶ $tval = substr($tval,0,28) . '[...]' if length($tval) > 32;
Branch: 1 branch
BranchTrueFalseTotal
if length $tval > 32 0 0 0
147 0 ▶ if ($formatStr =~ /^(string|undef|binary)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($formatStr =~ /^(string|undef|binary)/) { } 0 0 0
elsif ($tagInfo and &Image::ExifTool::IsInt($tval)) { } 0 0 0
148 # translate non-printable characters
149 0 $tval =~ tr/\x00-\x1f\x7f-\xff/./;
150 ▶ } elsif ($tagInfo and Image::ExifTool::IsInt($tval)) {
Condition0%0/3
$tagInfo and &Image::ExifTool::IsInt($tval)
!ll&&!rl&&r
000
MC/DC0%0/2
$tagInfo and &Image::ExifTool::IsInt($tval)
$tagInfo &Image::ExifTool::IsInt($tval)
Truth table0%0/3
A and B
A= $tagInfoB= &Image::ExifTool::IsInt($tval)
ABresult
0X0
100
111
151 0 ▶ if ($$tagInfo{IsOffset}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tagInfo->{'IsOffset'}) { } 0 0 0
elsif ($tagInfo->{'PrintHex'}) { } 0 0 0
152 0 $tval = sprintf('0x%.4x', $tval);
153 } elsif ($$tagInfo{PrintHex}) {
154 0 $tval = sprintf('0x%x', $tval);
155 }
156 }
157 0 $tip .= "Value: $tval";
158 0 my ($id, $sid);
159 0 ▶ if ($tagInfo and $$tagInfo{SubIFD}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagInfo and $tagInfo->{'SubIFD'}) { } 0 0 0
Condition0%0/3
$tagInfo and $tagInfo->{"SubIFD"}
!ll&&!rl&&r
000
MC/DC0%0/2
$tagInfo and $tagInfo->{"SubIFD"}
$tagInfo $tagInfo->{"SubIFD"}
Truth table0%0/3
A and B
A= $tagInfoB= $tagInfo->{"SubIFD"}
ABresult
0X0
100
111
160 0 ($subOffName, $id, $sid) = Image::ExifTool::Exif::NextOffsetName($et, $offName);
161 } else {
162 0 $id = $offName;
163 }
164 0 $et->HDump($entry+$bufPos, 20, "$dname $colName", $tip, 1, $id);
165 0 ▶ if ($size > 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > 8) 0 0 0
166 # add value data block
167 0 ▶ my $flg = ($tagInfo and $$tagInfo{SubDirectory} and $$tagInfo{MakerNotes}) ? 4 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$tagInfo && $tagInfo->{'SubDirectory'} && $tagInfo->{'MakerNotes'} ? : 0 0 0
Condition0%0/6
$tagInfo and $tagInfo->{"SubDirectory"} and $tagInfo->{"MakerNotes"}
!ll&&!rl&&r
000
$tagInfo and $tagInfo->{"SubDirectory"}
!ll&&!rl&&r
000
MC/DC0%0/3
$tagInfo and $tagInfo->{"SubDirectory"} and $tagInfo->{"MakerNotes"}
$tagInfo $tagInfo->{"SubDirectory"} $tagInfo->{"MakerNotes"}
Truth table0%0/4
A and B and C
A= $tagInfoB= $tagInfo->{"SubDirectory"}C= $tagInfo->{"MakerNotes"}
ABCresult
0XX0
10X0
1100
1111
168 0 $et->HDump($valuePtr,$size,"$tagName value",'SAME', $flg, $sid);
169 }
170 }
171 8 ▶ if ($tagInfo and $$tagInfo{SubIFD}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagInfo and $tagInfo->{'SubIFD'}) { } 0 8 8
Condition33%1/3
$tagInfo and $tagInfo->{"SubIFD"}
!ll&&!rl&&r
080
MC/DC0%0/2
$tagInfo and $tagInfo->{"SubIFD"}
$tagInfo $tagInfo->{"SubIFD"}
Truth table33%1/3
A and B
A= $tagInfoB= $tagInfo->{"SubIFD"}
ABresult
0X0
100
111
172 # process all SubIFD's as BigTIFF
173 0 ▶ $verbose > 0 and $et->VerboseInfo($tagID, $tagInfo,
Branch: 1 branch
BranchTrueFalseTotal
$verbose > 0 and $et->VerboseInfo($tagID, $tagInfo, "Table", $tagTablePtr, "Index", $index, "Value", $val, "DataPt", \$valBuff, "DataPos", $valBase + $valuePtr, "Start", 0, "Size", $size, "Format", $formatStr, "Count", $count) 0 0 0
174 Table => $tagTablePtr,
175 Index => $index,
176 Value => $val,
177 DataPt => \$valBuff,
178 DataPos => $valBase + $valuePtr,
179 Start => 0,
180 Size => $size,
181 Format => $formatStr,
182 Count => $count,
183 );
184 0 my @offsets = split ' ', $val;
185 0 my $i;
186 0 ▶ for ($i=0; $i<scalar(@offsets); ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @offsets) 0 0 0
187 0 my $subdirName = $$tagInfo{Name};
188 0 ▶ $subdirName .= $i if $i;
Branch: 1 branch
BranchTrueFalseTotal
if $i 0 0 0
189 0 my %subdirInfo = (
190 RAF => $raf,
191 DataPos => 0,
192 DirStart => $offsets[$i],
193 DirName => $subdirName,
194 Parent => $dirName,
195 OffsetName => $subOffName,
196 );
197 0 $et->ProcessDirectory(\%subdirInfo, $tagTablePtr, \&ProcessBigIFD);
198 }
199 } else {
200 8 my $tagKey = $et->HandleTag($tagTablePtr, $tagID, $val,
201 Index => $index,
202 DataPt => \$valBuff,
203 DataPos => $valBase + $valuePtr,
204 Start => 0,
205 Size => $size,
206 Format => $formatStr,
207 TagInfo => $tagInfo,
208 RAF => $raf,
209 );
210 8 ▶ $tagKey and $et->SetGroup($tagKey, $dirName);
Branch: 1 branch
BranchTrueFalseTotal
$tagKey and $et->SetGroup($tagKey, $dirName) 8 0 8
211 }
212 }
213 1 ▶ last unless $dirName =~ /^(IFD|SubIFD)(\d*)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $dirName =~ /^(IFD|SubIFD)(\d*)$/ 0 1 1
214 1 ▶ $dirName = $1 . (($2 || 0) + 1);
Condition50%1/2
$2 || 0
l!l
01
MC/DC0%0/1
$2 || 0
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
215 1 ▶ defined $nextIFD or $et->Warn("Bad $dirName pointer"), return 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $nextIFD or $et->Warn("Bad $dirName pointer"), (return 0) 1 0 1
216 1 $dirStart = Image::ExifTool::Get64u(\$nextIFD, 0);
217 1 ▶ $dirStart or last;
Branch: 1 branch
BranchTrueFalseTotal
$dirStart or last 0 1 1
218 0 $offName = $nextOffName;
219 # protect against infinite loop
220 0 ▶ if ($$et{PROCESSED}{$dirStart}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($et->{'PROCESSED'}{$dirStart}) { } 0 0 0
221 0 $et->Warn("$dirName pointer references previous $$et{PROCESSED}{$dirStart} directory");
222 0 last;
223 } else {
224 0 $$et{PROCESSED}{$dirStart} = $dirName;
225 }
226 }
227 1 return 1;
228 }
229  
230 #------------------------------------------------------------------------------
231 # Extract meta information from a BigTIFF image
232 # Inputs: 0) ExifTool object reference, 1) dirInfo reference
233 # Returns: 1 on success, 0 if this wasn't a valid BigTIFF image
234 sub ProcessBTF($$)
235 1 ▶{
Subroutine ProcessBTF: called CC=6 SCAR=20.8
Pod undocumented
236 1 my ($et, $dirInfo) = @_;
237 1 my $raf = $$dirInfo{RAF};
238 1 my $buff;
239  
240 1 ▶ return 0 unless $raf->Read($buff, 16) == 16;
Branch: 1 branch
BranchTrueFalseTotal
unless $raf->Read($buff, 16) == 16 0 1 1
241 1 ▶ return 0 unless $buff =~ /^(MM\0\x2b\0\x08\0\0|II\x2b\0\x08\0\0\0)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $buff =~ /^(MM\0\x2b\0\x08\0\0|II\x2b\0\x08\0\0\0)/ 0 1 1
242 1 ▶ if ($$dirInfo{OutFile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dirInfo->{"OutFile"}) 0 1 1
243 0 $et->Error('ExifTool does not support writing of BigTIFF images');
244 0 return 1;
245 }
246 1 $et->SetFileType('BTF'); # set the FileType tag
247 1 SetByteOrder(substr($buff, 0, 2));
248 1 my $offset = Image::ExifTool::Get64u(\$buff, 8);
249 1 ▶ if ($$et{HTML_DUMP}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($et->{"HTML_DUMP"}) 0 1 1
250 0 ▶ my $o = (GetByteOrder() eq 'II') ? 'Little' : 'Big';
Branch: 1 branch
BranchTrueFalseTotal
&GetByteOrder() eq 'II' ? : 0 0 0
251 0 $et->HDump(0, 8, "BigTIFF header", "Byte order: $o endian", 0);
252 0 $et->HDump(8, 8, "IFD0 pointer", sprintf("Offset: 0x%.8x",$offset), 0);
253 }
254 1 my %dirInfo = (
255 RAF => $raf,
256 DataPos => 0,
257 DirStart => $offset,
258 DirName => 'IFD0',
259 Parent => 'BigTIFF',
260 );
261 1 my $tagTablePtr = GetTagTable('Image::ExifTool::Exif::Main');
262 1 $et->ProcessDirectory(\%dirInfo, $tagTablePtr, \&ProcessBigIFD);
263 1 return 1;
264 }
265  
266 1 1; # end
267  
268 __END__
« Image/ExifTool/EXE.pm ↑ Summary Image/ExifTool/Sony.pm »