BigTIFF.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. package Image::ExifTool::BigTIFF;
  11. use strict;
  12. use vars qw($VERSION);
  13. use Image::ExifTool qw(:DataAccess :Utils);
  14. use Image::ExifTool::Exif;
  15. $VERSION = '1.06';
  16. my $maxOffset = 0x7fffffff; # currently supported maximum data offset/size
  17. #------------------------------------------------------------------------------
  18. # Process Big IFD directory
  19. # Inputs: 0) ExifTool object ref, 1) dirInfo ref, 2) tag table ref
  20. # Returns: 1 on success, otherwise returns 0 and sets a Warning
  21. sub ProcessBigIFD($$$)
  22. {
  23. my ($et, $dirInfo, $tagTablePtr) = @_;
  24. my $raf = $$dirInfo{RAF};
  25. my $verbose = $$et{OPTIONS}{Verbose};
  26. my $htmlDump = $$et{HTML_DUMP};
  27. my $dirName = $$dirInfo{DirName};
  28. my $dirStart = $$dirInfo{DirStart};
  29. $verbose = -1 if $htmlDump; # mix htmlDump into verbose so we can test for both at once
  30. # loop through IFD chain
  31. for (;;) {
  32. if ($dirStart > $maxOffset and not $et->Options('LargeFileSupport')) {
  33. $et->Warn('Huge offsets not supported (LargeFileSupport not set)');
  34. last;
  35. }
  36. unless ($raf->Seek($dirStart, 0)) {
  37. $et->Warn("Bad $dirName offset");
  38. return 0;
  39. }
  40. my ($dirBuff, $index);
  41. unless ($raf->Read($dirBuff, 8) == 8) {
  42. $et->Warn("Truncated $dirName count");
  43. return 0;
  44. }
  45. my $numEntries = Image::ExifTool::Get64u(\$dirBuff, 0);
  46. $verbose > 0 and $et->VerboseDir($dirName, $numEntries);
  47. my $bsize = $numEntries * 20;
  48. if ($bsize > $maxOffset) {
  49. $et->Warn('Huge directory counts not yet supported');
  50. last;
  51. }
  52. my $bufPos = $raf->Tell();
  53. unless ($raf->Read($dirBuff, $bsize) == $bsize) {
  54. $et->Warn("Truncated $dirName directory");
  55. return 0;
  56. }
  57. my $nextIFD;
  58. $raf->Read($nextIFD, 8) == 8 or undef $nextIFD; # try to read next IFD pointer
  59. if ($htmlDump) {
  60. $et->HDump($bufPos-8, 8, "$dirName entries", "Entry count: $numEntries");
  61. if (defined $nextIFD) {
  62. my $tip = sprintf("Offset: 0x%.8x", Image::ExifTool::Get64u(\$nextIFD, 0));
  63. $et->HDump($bufPos + 20 * $numEntries, 8, "Next IFD", $tip, 0);
  64. }
  65. }
  66. # loop through all entries in this BigTIFF IFD
  67. for ($index=0; $index<$numEntries; ++$index) {
  68. my $entry = 20 * $index;
  69. my $tagID = Get16u(\$dirBuff, $entry);
  70. my $format = Get16u(\$dirBuff, $entry+2);
  71. my $count = Image::ExifTool::Get64u(\$dirBuff, $entry+4);
  72. my $formatSize = $Image::ExifTool::Exif::formatSize[$format];
  73. unless (defined $formatSize) {
  74. $et->HDump($bufPos+$entry,20,"[invalid IFD entry]",
  75. "Bad format value: $format", 1);
  76. # warn unless the IFD was just padded with zeros
  77. $et->Warn(sprintf("Unknown format ($format) for $dirName tag 0x%x",$tagID));
  78. return 0; # assume corrupted IFD
  79. }
  80. my $formatStr = $Image::ExifTool::Exif::formatName[$format];
  81. my $size = $count * $formatSize;
  82. my $tagInfo = $et->GetTagInfo($tagTablePtr, $tagID);
  83. next unless defined $tagInfo or $verbose;
  84. my $valuePtr = $entry + 12;
  85. my ($valBuff, $valBase, $rational);
  86. if ($size > 8) {
  87. if ($size > $maxOffset) {
  88. $et->Warn("Can't handle $dirName entry $index (huge size)");
  89. next;
  90. }
  91. $valuePtr = Image::ExifTool::Get64u(\$dirBuff, $valuePtr);
  92. if ($valuePtr > $maxOffset and not $et->Options('LargeFileSupport')) {
  93. $et->Warn("Can't handle $dirName entry $index (LargeFileSupport not set)");
  94. next;
  95. }
  96. unless ($raf->Seek($valuePtr, 0) and $raf->Read($valBuff, $size) == $size) {
  97. $et->Warn("Error reading $dirName entry $index");
  98. next;
  99. }
  100. $valBase = 0;
  101. } else {
  102. $valBuff = substr($dirBuff, $valuePtr, $size);
  103. $valBase = $bufPos;
  104. }
  105. if (defined $tagInfo and not $tagInfo) {
  106. # GetTagInfo() required the value for a Condition
  107. $tagInfo = $et->GetTagInfo($tagTablePtr, $tagID, \$valBuff);
  108. }
  109. my $val = ReadValue(\$valBuff, 0, $formatStr, $count, $size, \$rational);
  110. if ($htmlDump) {
  111. my $tval = $val;
  112. # show numerator/denominator separately for rational numbers
  113. $tval .= " ($rational)" if defined $rational;
  114. my ($tagName, $colName);
  115. if ($tagID == 0x927c and $dirName eq 'ExifIFD') {
  116. $tagName = 'MakerNotes';
  117. } elsif ($tagInfo) {
  118. $tagName = $$tagInfo{Name};
  119. } else {
  120. $tagName = sprintf("Tag 0x%.4x",$tagID);
  121. }
  122. my $dname = sprintf("$dirName-%.2d", $index);
  123. # build our tool tip
  124. my $tip = sprintf("Tag ID: 0x%.4x\n", $tagID) .
  125. "Format: $formatStr\[$count]\nSize: $size bytes\n";
  126. if ($size > 8) {
  127. $tip .= sprintf("Value offset: 0x%.8x\n", $valuePtr);
  128. $colName = "<span class=H>$tagName</span>";
  129. } else {
  130. $colName = $tagName;
  131. }
  132. $tval = substr($tval,0,28) . '[...]' if length($tval) > 32;
  133. if ($formatStr =~ /^(string|undef|binary)/) {
  134. # translate non-printable characters
  135. $tval =~ tr/\x00-\x1f\x7f-\xff/./;
  136. } elsif ($tagInfo and Image::ExifTool::IsInt($tval)) {
  137. if ($$tagInfo{IsOffset}) {
  138. $tval = sprintf('0x%.4x', $tval);
  139. } elsif ($$tagInfo{PrintHex}) {
  140. $tval = sprintf('0x%x', $tval);
  141. }
  142. }
  143. $tip .= "Value: $tval";
  144. $et->HDump($entry+$bufPos, 20, "$dname $colName", $tip, 1);
  145. if ($size > 8) {
  146. # add value data block
  147. my $flg = ($tagInfo and $$tagInfo{SubDirectory} and $$tagInfo{MakerNotes}) ? 4 : 0;
  148. $et->HDump($valuePtr,$size,"$tagName value",'SAME', $flg);
  149. }
  150. }
  151. if ($tagInfo and $$tagInfo{SubIFD}) {
  152. # process all SubIFD's as BigTIFF
  153. $verbose > 0 and $et->VerboseInfo($tagID, $tagInfo,
  154. Table => $tagTablePtr,
  155. Index => $index,
  156. Value => $val,
  157. DataPt => \$valBuff,
  158. DataPos => $valBase + $valuePtr,
  159. Start => 0,
  160. Size => $size,
  161. Format => $formatStr,
  162. Count => $count,
  163. );
  164. my @offsets = split ' ', $val;
  165. my $i;
  166. for ($i=0; $i<scalar(@offsets); ++$i) {
  167. my $subdirName = $$tagInfo{Name};
  168. $subdirName .= $i if $i;
  169. my %subdirInfo = (
  170. RAF => $raf,
  171. DataPos => 0,
  172. DirStart => $offsets[$i],
  173. DirName => $subdirName,
  174. Parent => $dirInfo,
  175. );
  176. $et->ProcessDirectory(\%subdirInfo, $tagTablePtr, \&ProcessBigIFD);
  177. }
  178. } else {
  179. my $tagKey = $et->HandleTag($tagTablePtr, $tagID, $val,
  180. Index => $index,
  181. DataPt => \$valBuff,
  182. DataPos => $valBase + $valuePtr,
  183. Start => 0,
  184. Size => $size,
  185. Format => $formatStr,
  186. TagInfo => $tagInfo,
  187. RAF => $raf,
  188. );
  189. $tagKey and $et->SetGroup($tagKey, $dirName);
  190. }
  191. }
  192. last unless $dirName =~ /^(IFD|SubIFD)(\d*)$/;
  193. $dirName = $1 . (($2 || 0) + 1);
  194. defined $nextIFD or $et->Warn("Bad $dirName pointer"), return 0;
  195. $dirStart = Image::ExifTool::Get64u(\$nextIFD, 0);
  196. $dirStart or last;
  197. }
  198. return 1;
  199. }
  200. #------------------------------------------------------------------------------
  201. # Extract meta information from a BigTIFF image
  202. # Inputs: 0) ExifTool object reference, 1) dirInfo reference
  203. # Returns: 1 on success, 0 if this wasn't a valid BigTIFF image
  204. sub ProcessBTF($$)
  205. {
  206. my ($et, $dirInfo) = @_;
  207. my $raf = $$dirInfo{RAF};
  208. my $buff;
  209. return 0 unless $raf->Read($buff, 16) == 16;
  210. return 0 unless $buff =~ /^(MM\0\x2b\0\x08\0\0|II\x2b\0\x08\0\0\0)/;
  211. if ($$dirInfo{OutFile}) {
  212. $et->Error('ExifTool does not support writing of BigTIFF images');
  213. return 1;
  214. }
  215. $et->SetFileType('BTF'); # set the FileType tag
  216. SetByteOrder(substr($buff, 0, 2));
  217. my $offset = Image::ExifTool::Get64u(\$buff, 8);
  218. if ($$et{HTML_DUMP}) {
  219. my $o = (GetByteOrder() eq 'II') ? 'Little' : 'Big';
  220. $et->HDump(0, 8, "BigTIFF header", "Byte order: $o endian", 0);
  221. $et->HDump(8, 8, "IFD0 pointer", sprintf("Offset: 0x%.8x",$offset), 0);
  222. }
  223. my %dirInfo = (
  224. RAF => $raf,
  225. DataPos => 0,
  226. DirStart => $offset,
  227. DirName => 'IFD0',
  228. Parent => 'BigTIFF',
  229. );
  230. my $tagTablePtr = GetTagTable('Image::ExifTool::Exif::Main');
  231. $et->ProcessDirectory(\%dirInfo, $tagTablePtr, \&ProcessBigIFD);
  232. return 1;
  233. }
  234. 1; # end
  235. __END__
  236. =head1 NAME
  237. Image::ExifTool::BigTIFF - Read Big TIFF meta information
  238. =head1 SYNOPSIS
  239. This module is used by Image::ExifTool
  240. =head1 DESCRIPTION
  241. This module contains routines required by Image::ExifTool to read meta
  242. information in BigTIFF images.
  243. =head1 AUTHOR
  244. Copyright 2003-2016, Phil Harvey (phil at owl.phy.queensu.ca)
  245. This library is free software; you can redistribute it and/or modify it
  246. under the same terms as Perl itself.
  247. =head1 REFERENCES
  248. =over 4
  249. =item L<http://www.awaresystems.be/imaging/tiff/bigtiff.html>
  250. =back
  251. =head1 SEE ALSO
  252. L<Image::ExifTool::TagNames/EXIF Tags>,
  253. L<Image::ExifTool(3pm)|Image::ExifTool>
  254. =cut