| 1 | #------------------------------------------------------------------------------ | ||
| 2 | # File: HtmlDump.pm | ||
| 3 | # | ||
| 4 | # Description: Dump information in hex to HTML page | ||
| 5 | # | ||
| 6 | # Revisions: 12/05/2005 - P. Harvey Created | ||
| 7 | #------------------------------------------------------------------------------ | ||
| 8 | |||
| 9 | package Image::ExifTool::HtmlDump; | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use vars qw($VERSION); | ||
| 13 | use Image::ExifTool; # only for FinishTiffDump() | ||
| 14 | use Image::ExifTool::HTML qw(EscapeHTML); | ||
| 15 | |||
| 16 | $VERSION = '1.42'; | ||
| 17 | |||
| 18 | sub DumpTable($$$;$$$$$$); | ||
| 19 | sub Open($$$;@); | ||
| 20 | sub Write($@); | ||
| 21 | |||
| 22 | my ($bkgStart, $bkgEnd, @bkgSpan); | ||
| 23 | |||
| 24 | my $htmlHeader1 = <<_END_PART_1_; | ||
| 25 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" | ||
| 26 | "http://www.w3.org/TR/1998/REC-html40-19980424/loose.dtd"> | ||
| 27 | <html> | ||
| 28 | <head> | ||
| 29 | <title> | ||
| 30 | _END_PART_1_ | ||
| 31 | |||
| 32 | # Note: Don't change font-weight style because it can affect line height | ||
| 33 | my $htmlHeader2 = <<_END_PART_2_; | ||
| 34 | </title> | ||
| 35 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
| 36 | <style type="text/css"> | ||
| 37 | <!-- | ||
| 38 | /* character style ID's */ | ||
| 39 | .D { color: #000000 } /* default color */ | ||
| 40 | .V { color: #ff0000 } /* duplicate block 1 */ | ||
| 41 | .W { color: #004400 } /* normal block 1 */ | ||
| 42 | .X { color: #ff4488 } /* duplicate block 2 */ | ||
| 43 | .Y { color: #448844 } /* normal block 2 */ | ||
| 44 | .U { color: #cc8844 } /* unused data block */ | ||
| 45 | .H { color: #0000ff } /* highlighted tag name */ | ||
| 46 | .F { color: #aa00dd } /* actual offset differs */ | ||
| 47 | .M { text-decoration: underline } /* maker notes data */ | ||
| 48 | .tt { /* tooltip text */ | ||
| 49 | visibility: hidden; | ||
| 50 | position: absolute; | ||
| 51 | white-space: nowrap; | ||
| 52 | top: 0; | ||
| 53 | left: 0; | ||
| 54 | font-family: Verdana, sans-serif; | ||
| 55 | font-size: .7em; | ||
| 56 | padding: 2px 4px; | ||
| 57 | border: 1px solid gray; | ||
| 58 | z-index: 3; | ||
| 59 | } | ||
| 60 | .tb { /* tooltip background */ | ||
| 61 | visibility: hidden; | ||
| 62 | position: absolute; | ||
| 63 | background: #ffffdd; | ||
| 64 | zoom: 1; | ||
| 65 | -moz-opacity: 0.8; | ||
| 66 | -khtml-opacity: 0.8; | ||
| 67 | -ms-filter: 'progid:DXImageTransform.Microsoft.Alpha(Opacity=80)'; | ||
| 68 | filter: alpha(opacity=80); | ||
| 69 | opacity: 0.8; | ||
| 70 | z-index: 2; | ||
| 71 | } | ||
| 72 | /* table styles */ | ||
| 73 | table.dump { | ||
| 74 | border-top: 1px solid gray; | ||
| 75 | border-bottom: 1px solid gray; | ||
| 76 | } | ||
| 77 | table.dump td { padding: .2em .3em } | ||
| 78 | td.c2 { | ||
| 79 | border-left: 1px solid gray; | ||
| 80 | border-right: 1px solid gray; | ||
| 81 | } | ||
| 82 | pre { margin: 0 } | ||
| 83 | table { font-size: .9em } | ||
| 84 | body { color: black; background: white } | ||
| 85 | --> | ||
| 86 | </style> | ||
| 87 | <script language="JavaScript" type="text/JavaScript"> | ||
| 88 | <!-- Begin | ||
| 89 | // tooltip positioning constants | ||
| 90 | var TMAR = 4; // top/left margins | ||
| 91 | var BMAR = 16; // bottom/right margins (scrollbars may overhang inner dimensions) | ||
| 92 | var XOFF = 10; // x offset from cursor | ||
| 93 | var YOFF = 40; // y offset | ||
| 94 | var YMIN = 10; // minimum y offset | ||
| 95 | var YTOP = 20; // y offset when above cursor | ||
| 96 | // common variables | ||
| 97 | var safari1 = navigator.userAgent.indexOf("Safari/312.6") >= 0; | ||
| 98 | var ie6 = navigator.userAgent.toLowerCase().indexOf('msie 6') >= 0; | ||
| 99 | var mspan = new Array; | ||
| 100 | var clicked = 0; | ||
| 101 | var hlist, tt, tb, firstOutEvt, lastInEvt; | ||
| 102 | |||
| 103 | function GetElementsByClass(classname, tagname) { | ||
| 104 | var found = new Array(); | ||
| 105 | var list = document.getElementsByTagName(tagname); | ||
| 106 | var len = list.length; | ||
| 107 | for (var i=0, j=0; i<len; ++i) { | ||
| 108 | var classes = list[i].className.split(' '); | ||
| 109 | for (var k=0; k<classes.length; ++k) { | ||
| 110 | if (classes[k] == classname) { | ||
| 111 | found[j++] = list[i]; | ||
| 112 | break; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | } | ||
| 116 | return found; | ||
| 117 | } | ||
| 118 | |||
| 119 | // click mouse | ||
| 120 | function doClick(e) | ||
| 121 | { | ||
| 122 | if (!clicked) { | ||
| 123 | firstOutEvt = lastInEvt = undefined; | ||
| 124 | high(e, 2); | ||
| 125 | if (hlist) clicked = 1; | ||
| 126 | } else { | ||
| 127 | clicked = 0; | ||
| 128 | if (firstOutEvt) high(firstOutEvt, 0); | ||
| 129 | if (lastInEvt) high(lastInEvt, 1); | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | // move tooltip | ||
| 134 | function move(e) | ||
| 135 | { | ||
| 136 | if (!tt) return; | ||
| 137 | if (ie6 && (tt.style.top == '' || tt.style.top == 0) && | ||
| 138 | (tt.style.left == '' || tt.style.left == 0)) | ||
| 139 | { | ||
| 140 | tt.style.width = tt.offsetWidth + 'px'; | ||
| 141 | tt.style.height = tt.offsetHeight + 'px'; | ||
| 142 | } | ||
| 143 | var w, h; | ||
| 144 | // browser inconsistencies make getting window size more complex than it should be, | ||
| 145 | // and even then we don't know if it is smaller due to scrollbar width | ||
| 146 | if (typeof(window.innerWidth) == 'number') { | ||
| 147 | w = window.innerWidth; | ||
| 148 | h = window.innerHeight; | ||
| 149 | } else if (document.documentElement && document.documentElement.clientWidth) { | ||
| 150 | w = document.documentElement.clientWidth; | ||
| 151 | h = document.documentElement.clientHeight; | ||
| 152 | } else { | ||
| 153 | w = document.body.clientWidth; | ||
| 154 | h = document.body.clientHeight; | ||
| 155 | } | ||
| 156 | var x = e.clientX + XOFF; | ||
| 157 | var y = e.clientY + YOFF; | ||
| 158 | if (safari1) { // patch for people still using OS X 10.3.9 | ||
| 159 | x -= document.body.scrollLeft + document.documentElement.scrollLeft; | ||
| 160 | y -= document.body.scrollTop + document.documentElement.scrollTop; | ||
| 161 | } | ||
| 162 | var mx = w - BMAR - tt.offsetWidth; | ||
| 163 | var my = h - BMAR - tt.offsetHeight; | ||
| 164 | if (y > my + YOFF - YMIN) y = e.clientY - YTOP - tt.offsetHeight; | ||
| 165 | if (x > mx) x = mx; | ||
| 166 | if (y > my) y = my; | ||
| 167 | if (x < TMAR) x = TMAR; | ||
| 168 | if (y < TMAR) y = TMAR; | ||
| 169 | x += document.body.scrollLeft + document.documentElement.scrollLeft; | ||
| 170 | y += document.body.scrollTop + document.documentElement.scrollTop; | ||
| 171 | tb.style.width = tt.offsetWidth + 'px'; | ||
| 172 | tb.style.height = tt.offsetHeight + 'px'; | ||
| 173 | tt.style.top = tb.style.top = y + 'px'; | ||
| 174 | tt.style.left = tb.style.left = x + 'px'; | ||
| 175 | tt.style.visibility = tb.style.visibility = 'visible'; | ||
| 176 | } | ||
| 177 | |||
| 178 | // highlight/unhighlight text | ||
| 179 | function high(e,on) { | ||
| 180 | if (on) { | ||
| 181 | lastInEvt = e; | ||
| 182 | } else { | ||
| 183 | if (!firstOutEvt) firstOutEvt = e; | ||
| 184 | } | ||
| 185 | if (clicked) return; | ||
| 186 | var targ; | ||
| 187 | if (e.target) targ = e.target; | ||
| 188 | else if (e.srcElement) targ = e.srcElement; | ||
| 189 | if (targ.nodeType == 3) targ = targ.parentNode; // defeat Safari bug | ||
| 190 | if (!targ.name) targ = targ.parentNode; // go up another level if necessary | ||
| 191 | if (targ.name && document.getElementsByName) { | ||
| 192 | // un-highlight current objects | ||
| 193 | if (hlist) { | ||
| 194 | for (var i=0; i<hlist.length; ++i) { | ||
| 195 | for (var j=0; j<hlist[i].length; ++j) { | ||
| 196 | hlist[i][j].style.background = 'transparent'; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | hlist = null; | ||
| 200 | } | ||
| 201 | if (tt) { | ||
| 202 | // hide old tooltip | ||
| 203 | tt.style.visibility = tb.style.visibility = 'hidden'; | ||
| 204 | tt = null; | ||
| 205 | } | ||
| 206 | if (on) { | ||
| 207 | if (targ.name.substring(0,1) == 't') { | ||
| 208 | // show our tooltip (ID is different than name to avoid confusing IE) | ||
| 209 | tt = document.getElementById('p' + targ.name.substring(1)); | ||
| 210 | if (tt) { | ||
| 211 | tb = document.getElementById('tb'); | ||
| 212 | move(e); | ||
| 213 | } | ||
| 214 | } | ||
| 215 | // highlight anchor elements with the same name | ||
| 216 | hlist = new Array; | ||
| 217 | hlist.push(document.getElementsByName(targ.name)); | ||
| 218 | // is this an IFD pointer? | ||
| 219 | var pos = targ.className.indexOf('Offset_'); | ||
| 220 | if (pos > 0) { | ||
| 221 | // add elements from this IFD to our highlight list | ||
| 222 | hlist.push(document.getElementsByClassName(targ.className.substr(pos+7))); | ||
| 223 | } | ||
| 224 | // use class name to highlight span elements if necessary | ||
| 225 | for (var i=0; i<mspan.length; ++i) { | ||
| 226 | if (mspan[i] != targ.name) continue; | ||
| 227 | // add these span elements to our highlight list | ||
| 228 | hlist.push(GetElementsByClass(targ.name, 'span')); | ||
| 229 | break; | ||
| 230 | } | ||
| 231 | for (var i=0; i<hlist.length; ++i) { | ||
| 232 | for (var j=0; j<hlist[i].length; ++j) { | ||
| 233 | hlist[i][j].style.background = on == 2 ? '#ffbbbb' : '#ffcc99'; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | } | ||
| 237 | } | ||
| 238 | } | ||
| 239 | _END_PART_2_ | ||
| 240 | |||
| 241 | my $htmlHeader3 = q[ | ||
| 242 | // End ---> | ||
| 243 | </script></head> | ||
| 244 | <body><noscript><b class=V>--> | ||
| 245 | Enable JavaScript for active highlighting and information tool tips! | ||
| 246 | </b></noscript> | ||
| 247 | <table class=dump cellspacing=0 cellpadding=2> | ||
| 248 | <tr><td valign='top'><pre>]; | ||
| 249 | |||
| 250 | my $preMouse = q(<pre onmouseover="high(event,1)" onmouseout="high(event,0)" onmousemove="move(event)" onmousedown="doClick(event)">); | ||
| 251 | |||
| 252 | #------------------------------------------------------------------------------ | ||
| 253 | # New - create new HtmlDump object | ||
| 254 | # Inputs: 0) reference to HtmlDump object or HtmlDump class name | ||
| 255 | sub new | ||
| 256 | { | ||
| 257 | local $_; | ||
| 258 | my $that = shift; | ||
| 259 | my $class = ref($that) || $that || 'Image::ExifTool::HtmlDump'; | ||
| 260 | return bless { Block => {}, TipNum => 0 }, $class; | ||
| 261 | } | ||
| 262 | |||
| 263 | #------------------------------------------------------------------------------ | ||
| 264 | # Add information to dump | ||
| 265 | # Inputs: 0) HTML dump hash ref, 1) absolute offset in file, 2) data size, | ||
| 266 | # 3) comment string, 4) tool tip (or SAME to use previous tip), | ||
| 267 | # 5) bit flags (see below), 6) IFD name | ||
| 268 | # Bits: 0x01 - print at start of line | ||
| 269 | # 0x02 - print red address | ||
| 270 | # 0x04 - maker notes data ('M'-class span) | ||
| 271 | # 0x08 - limit block length | ||
| 272 | # 0x10 - allow double references | ||
| 273 | # 0x100 - (reserved) | ||
| 274 | # Notes: Block will be shown in 'unused' color if comment string begins with '[' | ||
| 275 | sub Add($$$$;$$) | ||
| 276 | { | ||
| 277 | my ($self, $start, $size, $msg, $tip, $flag, $ifd) = @_; | ||
| 278 | my $block = $$self{Block}; | ||
| 279 | $$block{$start} or $$block{$start} = [ ]; | ||
| 280 | my $htip; | ||
| 281 | if ($tip and $tip eq 'SAME') { | ||
| 282 | $htip = ''; | ||
| 283 | } else { | ||
| 284 | # use message as first line of tip, and make bold unless in brackets | ||
| 285 | $htip = ($msg =~ /^[[(]/) ? $msg : "<b>$msg</b>"; | ||
| 286 | if (defined $tip) { | ||
| 287 | ($tip = EscapeHTML($tip)) =~ s/\n/<br>/g; # HTML-ize tooltip text | ||
| 288 | $htip .= '<br>' . $tip; | ||
| 289 | } | ||
| 290 | # add size if not already done | ||
| 291 | $htip .= "<br>($size bytes)" unless $htip =~ /<br>Size:/; | ||
| 292 | ++$self->{TipNum}; | ||
| 293 | } | ||
| 294 | push @{$$block{$start}}, [ $size, $msg, $htip, $flag, $self->{TipNum}, $ifd ]; | ||
| 295 | } | ||
| 296 | |||
| 297 | #------------------------------------------------------------------------------ | ||
| 298 | # Print dump information to HTML page | ||
| 299 | # Inputs: 0) Dump information hash reference, 1) source file RAF reference, | ||
| 300 | # 2) data pointer, 3) data position, 4) output file or scalar reference, | ||
| 301 | # 5) limit level (1-3), 6) title | ||
| 302 | # Returns: non-zero if useful output was generated, | ||
| 303 | # or -1 on error loading data and "ERROR" is set to offending data name | ||
| 304 | # Note: The "Error" member may be set externally to print a specific error | ||
| 305 | # message instead of doing the dump. | ||
| 306 | sub Print($$;$$$$$) | ||
| 307 | { | ||
| 308 | local $_; | ||
| 309 | my ($self, $raf, $dataPt, $dataPos, $outfile, $level, $title) = @_; | ||
| 310 | my ($i, $buff, $rtnVal, $limit, $err); | ||
| 311 | my $block = $$self{Block}; | ||
| 312 | $dataPos = 0 unless $dataPos; | ||
| 313 | $outfile = \*STDOUT unless ref $outfile; | ||
| 314 | $title = 'HtmlDump' unless $title; | ||
| 315 | $level or $level = 0; | ||
| 316 | my $tell = $raf->Tell(); | ||
| 317 | $raf->Seek(0,2) or $$self{ERROR} = 'Seek error', return -1; | ||
| 318 | my $fileLen = $raf->Tell(); | ||
| 319 | my $pos = 0; | ||
| 320 | my $dataEnd = $dataPos + ($dataPt ? length($$dataPt) : 0); | ||
| 321 | # initialize member variables | ||
| 322 | $$self{Open} = []; | ||
| 323 | $$self{Closed} = []; | ||
| 324 | $$self{TipList} = []; | ||
| 325 | $$self{MSpanList} = []; | ||
| 326 | $$self{Cols} = [ '', '', '', '' ]; # text columns | ||
| 327 | # set dump size limits (limits are 4x smaller if bit 0x08 set in flags) | ||
| 328 | if ($level <= 1) { | ||
| 329 | $limit = 1024; | ||
| 330 | } elsif ($level <= 2) { | ||
| 331 | $limit = 16384; | ||
| 332 | } else { | ||
| 333 | $limit = 256 * 1024 * 1024; # never dump bigger than 256 MB | ||
| 334 | } | ||
| 335 | $$self{Limit} = $limit; | ||
| 336 | # pre-initialize open/closed hashes for all columns | ||
| 337 | for ($i=0; $i<4; ++$i) { | ||
| 338 | $self->{Open}->[$i] = { ID => [ ], Element => { } }; | ||
| 339 | $self->{Closed}->[$i] = { ID => [ ], Element => { } }; | ||
| 340 | } | ||
| 341 | $bkgStart = $bkgEnd = 0; | ||
| 342 | undef @bkgSpan; | ||
| 343 | my $index = 0; # initialize tooltip index | ||
| 344 | my (@names, $wasUnused, @starts); | ||
| 345 | # only do dump if we didn't have a serious error | ||
| 346 | @starts = sort { $a <=> $b } keys %$block unless $$self{Error}; | ||
| 347 | for ($i=0; $i<=@starts; ++$i) { | ||
| 348 | my $start = $starts[$i]; | ||
| 349 | my $parmList; | ||
| 350 | if (defined $start) { | ||
| 351 | $parmList = $$block{$start}; | ||
| 352 | } elsif ($bkgEnd and $pos < $bkgEnd and not defined $wasUnused) { | ||
| 353 | $start = $bkgEnd; # finish last bkg block | ||
| 354 | } else { | ||
| 355 | last; | ||
| 356 | } | ||
| 357 | $start = $fileLen if $start > $fileLen; # handle case of bad start offset | ||
| 358 | my $len = $start - $pos; | ||
| 359 | if ($len > 0 and not $wasUnused) { | ||
| 360 | # we have a unused bytes before this data block | ||
| 361 | --$i; # dump the data block next time around | ||
| 362 | # split unused data into 2 blocks if it spans end of a bkg block | ||
| 363 | my ($nextBkgEnd, $bkg); | ||
| 364 | if (not defined $wasUnused and $bkgEnd) { | ||
| 365 | foreach $bkg (@bkgSpan) { | ||
| 366 | next if $pos >= $$bkg{End} + $dataPos or $pos + $len <= $$bkg{End} + $dataPos; | ||
| 367 | $nextBkgEnd = $$bkg{End} unless $nextBkgEnd and $nextBkgEnd < $$bkg{End}; | ||
| 368 | } | ||
| 369 | } | ||
| 370 | if ($nextBkgEnd) { | ||
| 371 | $start = $pos; | ||
| 372 | $len = $nextBkgEnd + $dataPos - $pos; | ||
| 373 | $wasUnused = 0; | ||
| 374 | } else { | ||
| 375 | $start = $pos; # dump the unused bytes now | ||
| 376 | $wasUnused = 1; # avoid re-dumping unused bytes if we get a read error | ||
| 377 | } | ||
| 378 | my $str = ($len > 1) ? "unused $len bytes" : 'pad byte'; | ||
| 379 | $parmList = [ [ $len, "[$str]", undef, 0x108 ] ]; | ||
| 380 | } else { | ||
| 381 | undef $wasUnused; | ||
| 382 | } | ||
| 383 | my $parms; | ||
| 384 | foreach $parms (@$parmList) { | ||
| 385 | my ($len, $msg, $tip, $flag, $tipNum, $ifd) = @$parms; | ||
| 386 | next unless $len > 0; | ||
| 387 | $flag = 0 unless defined $flag; | ||
| 388 | # generate same name for all blocks indexed by this tooltip | ||
| 389 | my $name; | ||
| 390 | $name = $names[$tipNum] if defined $tipNum; | ||
| 391 | my $idx = $index; | ||
| 392 | if ($name) { | ||
| 393 | # get index from existing ID | ||
| 394 | $idx = substr($name, 1); | ||
| 395 | } else { | ||
| 396 | $name = "t$index"; | ||
| 397 | $names[$tipNum] = $name if defined $tipNum; | ||
| 398 | ++$index; | ||
| 399 | } | ||
| 400 | if ($flag & 0x14) { | ||
| 401 | my $class = $flag & 0x04 ? "$name M" : $name; | ||
| 402 | $class .= " $ifd" if $ifd; | ||
| 403 | my %bkg = ( | ||
| 404 | Class => $class, | ||
| 405 | Start => $start - $dataPos, | ||
| 406 | End => $start - $dataPos + $len, | ||
| 407 | ); | ||
| 408 | push @bkgSpan, \%bkg; | ||
| 409 | $bkgStart = $bkg{Start} unless $bkgStart and $bkgStart < $bkg{Start}; | ||
| 410 | $bkgEnd = $bkg{End} unless $bkgEnd and $bkgEnd > $bkg{End}; | ||
| 411 | push @{$self->{MSpanList}}, $name; | ||
| 412 | next; | ||
| 413 | } | ||
| 414 | # loop until we read the value properly | ||
| 415 | my ($end, $try); | ||
| 416 | for ($try=0; $try<2; ++$try) { | ||
| 417 | $end = $start + $len; | ||
| 418 | # only load as much of the block as we are going to dump | ||
| 419 | # (read 32 more bytes than necessary just in case there | ||
| 420 | # is only one skipped line that we decide to print) | ||
| 421 | my $size = ($len > $limit + 32) ? $limit / 2 + 16 : $len; | ||
| 422 | if ($start >= $dataPos and $end <= $dataEnd) { | ||
| 423 | $buff = substr($$dataPt, $start-$dataPos, $size); | ||
| 424 | if ($len != $size) { | ||
| 425 | $buff .= substr($$dataPt, $start-$dataPos+$len-$size, $size); | ||
| 426 | } | ||
| 427 | } else { | ||
| 428 | $buff = ''; | ||
| 429 | if ($raf->Seek($start, 0) and $raf->Read($buff, $size) == $size) { | ||
| 430 | # read end of block | ||
| 431 | if ($len != $size) { | ||
| 432 | my $buf2 = ''; | ||
| 433 | unless ($raf->Seek($start+$len-$size, 0) and | ||
| 434 | $raf->Read($buf2, $size) == $size) | ||
| 435 | { | ||
| 436 | $err = $msg; | ||
| 437 | # reset $len to the actual length of available data | ||
| 438 | $len = $fileLen - $start; | ||
| 439 | $tip .= "<br>Error: Only $len bytes available!" if $tip; | ||
| 440 | next; | ||
| 441 | } | ||
| 442 | $buff .= $buf2; | ||
| 443 | undef $buf2; | ||
| 444 | } | ||
| 445 | } else { | ||
| 446 | $err = $msg; | ||
| 447 | $len = length $buff; | ||
| 448 | $tip .= "<br>Error: Only $len bytes available!" if $tip; | ||
| 449 | } | ||
| 450 | } | ||
| 451 | last; | ||
| 452 | } | ||
| 453 | $tip and $self->{TipList}->[$idx] = $tip; | ||
| 454 | next unless length $buff; | ||
| 455 | # set flag to continue this line if next block is contiguous | ||
| 456 | if ($i+1 < @starts and $parms eq $$parmList[-1] and | ||
| 457 | ($end == $starts[$i+1] or ($end < $starts[$i+1] and $end >= $pos))) | ||
| 458 | { | ||
| 459 | my $nextFlag = $block->{$starts[$i+1]}->[0]->[3] || 0; | ||
| 460 | $flag |= 0x100 unless $flag & 0x01 or $nextFlag & 0x01; | ||
| 461 | } | ||
| 462 | $self->DumpTable($start-$dataPos, \$buff, $msg, $name, | ||
| 463 | $flag, $len, $pos-$dataPos, $ifd); | ||
| 464 | undef $buff; | ||
| 465 | $pos = $end if $pos < $end; | ||
| 466 | } | ||
| 467 | } | ||
| 468 | $self->Open('',''); # close all open elements | ||
| 469 | $raf->Seek($tell,0); | ||
| 470 | |||
| 471 | # write output HTML file | ||
| 472 | Write($outfile, $htmlHeader1, $title); | ||
| 473 | if ($self->{Cols}->[0]) { | ||
| 474 | Write($outfile, $htmlHeader2); | ||
| 475 | my $mspan = \@{$$self{MSpanList}}; | ||
| 476 | for ($i=0; $i<@$mspan; ++$i) { | ||
| 477 | Write($outfile, qq(mspan[$i] = "$$mspan[$i]";\n)); | ||
| 478 | } | ||
| 479 | Write($outfile, $htmlHeader3, $self->{Cols}->[0]); | ||
| 480 | Write($outfile, '</pre></td><td valign="top">', | ||
| 481 | $preMouse, $self->{Cols}->[1]); | ||
| 482 | Write($outfile, '</pre></td><td class=c2 valign="top">', | ||
| 483 | $preMouse, $self->{Cols}->[2]); | ||
| 484 | Write($outfile, '</pre></td><td valign="top">', | ||
| 485 | $preMouse, $self->{Cols}->[3]); | ||
| 486 | Write($outfile, "</pre></td></tr></table>\n<div id=tb class=tb> </div>\n"); | ||
| 487 | my $tips = \@{$$self{TipList}}; | ||
| 488 | for ($i=0; $i<@$tips; ++$i) { | ||
| 489 | my $tip = $$tips[$i]; | ||
| 490 | Write($outfile, "<div id=p$i class=tt>$tip</div>\n") if defined $tip; | ||
| 491 | } | ||
| 492 | delete $$self{TipList}; | ||
| 493 | $rtnVal = 1; | ||
| 494 | } else { | ||
| 495 | my $err = $$self{Error} || 'No EXIF or TIFF information found in image'; | ||
| 496 | Write($outfile, "$title</title></head><body>\n$err\n"); | ||
| 497 | $rtnVal = 0; | ||
| 498 | } | ||
| 499 | Write($outfile, "</body></html>\n"); | ||
| 500 | for ($i=0; $i<4; ++$i) { | ||
| 501 | $self->{Cols}->[$i] = ''; # free memory | ||
| 502 | } | ||
| 503 | if ($err) { | ||
| 504 | $err =~ tr/()//d; | ||
| 505 | $$self{ERROR} = $err; | ||
| 506 | return -1; | ||
| 507 | } | ||
| 508 | return $rtnVal; | ||
| 509 | } | ||
| 510 | |||
| 511 | #------------------------------------------------------------------------------ | ||
| 512 | # Open or close a specified html element | ||
| 513 | # Inputs: 0) HtmlDump object ref, 1) element id, 2) element string, | ||
| 514 | # 3-N) list of column numbers (empty for all columns) | ||
| 515 | # - element id may be '' to close all elements | ||
| 516 | # - element string may be '' to close element by ID (or 0 to close without reopening) | ||
| 517 | # - element id and string may both be 1 to reopen temporarily closed elements | ||
| 518 | sub Open($$$;@) | ||
| 519 | { | ||
| 520 | my ($self, $id, $element, @colNums) = @_; | ||
| 521 | |||
| 522 | # loop through specified columns | ||
| 523 | @colNums or @colNums = (0 .. $#{$self->{Open}}); | ||
| 524 | my $col; | ||
| 525 | foreach $col (@colNums) { | ||
| 526 | # get information about open elements in this column | ||
| 527 | my $opHash = $self->{Open}->[$col]; | ||
| 528 | my $opElem = $$opHash{Element}; | ||
| 529 | if ($element) { | ||
| 530 | # next if already open | ||
| 531 | next if $$opElem{$id} and $$opElem{$id} eq $element; | ||
| 532 | } elsif ($id and not $$opElem{$id}) { | ||
| 533 | # next if already closed and nothing to reopen | ||
| 534 | next unless $element eq '' and @{$self->{Closed}->[$col]->{ID}}; | ||
| 535 | } | ||
| 536 | my $opID = $$opHash{ID}; | ||
| 537 | my $clHash = $self->{Closed}->[$col]; | ||
| 538 | my $clID = $$clHash{ID}; | ||
| 539 | my $clElem = $$clHash{Element}; | ||
| 540 | # get reference to output column list (use temp list if available) | ||
| 541 | my $cols = $$self{TmpCols} || $$self{Cols}; | ||
| 542 | # close everything down to this element if necessary | ||
| 543 | if ($$opElem{$id} or not $id) { | ||
| 544 | while (@$opID) { | ||
| 545 | my $tid = pop @$opID; | ||
| 546 | my $e = $$opElem{$tid}; | ||
| 547 | $e =~ s/^<(\S+).*/<\/$1>/s; | ||
| 548 | $$cols[$col] .= $e; | ||
| 549 | if ($id eq $tid or not $id) { | ||
| 550 | delete $$opElem{$tid}; | ||
| 551 | last if $id; | ||
| 552 | next; | ||
| 553 | } | ||
| 554 | # add this to the temporarily closed list | ||
| 555 | # (because we really didn't want to close it) | ||
| 556 | push @$clID, $tid; | ||
| 557 | $$clElem{$tid} = $$opElem{$tid}; | ||
| 558 | delete $$opElem{$tid}; | ||
| 559 | } | ||
| 560 | unless ($id) { | ||
| 561 | # forget all temporarily closed elements | ||
| 562 | $clID = $$clHash{ID} = [ ]; | ||
| 563 | $clElem = $$clHash{Element} = { }; | ||
| 564 | } | ||
| 565 | } elsif ($$clElem{$id}) { | ||
| 566 | # delete from the list of temporarily closed elements | ||
| 567 | delete $$clElem{$id}; | ||
| 568 | @$clID = grep !/^$id$/, @$clID; | ||
| 569 | } | ||
| 570 | next if $element eq '0'; # 0 = don't reopen temporarily closed elements | ||
| 571 | |||
| 572 | # re-open temporarily closed elements | ||
| 573 | while (@$clID) { | ||
| 574 | my $tid = pop @$clID; | ||
| 575 | $$cols[$col] .= $$clElem{$tid}; | ||
| 576 | push @$opID, $tid; | ||
| 577 | $$opElem{$tid} = $$clElem{$tid}; | ||
| 578 | delete $$clElem{$tid}; | ||
| 579 | } | ||
| 580 | # open specified element | ||
| 581 | if ($element and $element ne '1') { | ||
| 582 | $$cols[$col] .= $element; | ||
| 583 | push @$opID, $id; | ||
| 584 | $$opElem{$id} = $element; | ||
| 585 | } | ||
| 586 | } | ||
| 587 | } | ||
| 588 | |||
| 589 | #------------------------------------------------------------------------------ | ||
| 590 | # Dump a block of data in HTML table form | ||
| 591 | # Inputs: 0) HtmlDump object ref, 1) data position, 2) block pointer, | ||
| 592 | # 3) message, 4) object name, 5) flag, 6) full block length (actual | ||
| 593 | # data may be shorter), 7) data end position, 8) IFD name | ||
| 594 | sub DumpTable($$$;$$$$$$) | ||
| 595 | { | ||
| 596 | my ($self, $pos, $blockPt, $msg, $name, $flag, $len, $endPos, $ifd) = @_; | ||
| 597 | $len = length $$blockPt unless defined $len; | ||
| 598 | $endPos = 0 unless $endPos; | ||
| 599 | my ($f0, $dblRef, $id); | ||
| 600 | my $skipped = 0; | ||
| 601 | if (($endPos and $pos < $endPos) or $flag & 0x02) { | ||
| 602 | # display double-reference addresses in red | ||
| 603 | $f0 = "<span class=V>"; | ||
| 604 | $dblRef = 1 if $endPos and $pos < $endPos; | ||
| 605 | } else { | ||
| 606 | $f0 = ''; | ||
| 607 | } | ||
| 608 | my @c = ('','','',''); | ||
| 609 | $$self{TmpCols} = \@c; | ||
| 610 | if ($name) { | ||
| 611 | if ($msg and $msg =~ /^\[/) { | ||
| 612 | $id = 'U'; | ||
| 613 | } else { | ||
| 614 | if ($$self{A}) { | ||
| 615 | $id = 'X'; | ||
| 616 | $$self{A} = 0; | ||
| 617 | } else { | ||
| 618 | $id = 'V'; | ||
| 619 | $$self{A} = 1; | ||
| 620 | } | ||
| 621 | ++$id unless $dblRef; | ||
| 622 | } | ||
| 623 | my $class = $ifd ? "'$id $ifd'" : $id; | ||
| 624 | $name = "<a name=$name class=$class>"; | ||
| 625 | $msg and $msg = "$name$msg</a>"; | ||
| 626 | } else { | ||
| 627 | $name = ''; | ||
| 628 | } | ||
| 629 | # use base-relative offsets from now on | ||
| 630 | my $cols = 0; | ||
| 631 | my $p = $pos; | ||
| 632 | if ($$self{Cont}) { | ||
| 633 | $cols = $pos & 0x0f; | ||
| 634 | $c[1] .= ($cols == 8) ? ' ' : ' '; | ||
| 635 | } else { | ||
| 636 | my $addr = $pos < 0 ? sprintf("-%.4x",-$pos) : sprintf("%5.4x",$pos); | ||
| 637 | $self->Open('fgd', $f0, 0); | ||
| 638 | $self->Open('fgd', '', 3); | ||
| 639 | $c[0] .= "$addr"; | ||
| 640 | $p -= $pos & 0x0f unless $flag & 0x01; | ||
| 641 | if ($p < $pos) { | ||
| 642 | $self->Open('bkg', '', 1, 2); # don't underline white space | ||
| 643 | $cols = $pos - $p; | ||
| 644 | my $n = 3 * $cols; | ||
| 645 | ++$n if $cols > 7; | ||
| 646 | $c[1] .= ' ' x $n; | ||
| 647 | $c[2] .= ' ' x $cols; | ||
| 648 | $p = $pos; | ||
| 649 | } | ||
| 650 | } | ||
| 651 | # loop through each column of hex numbers | ||
| 652 | for (;;) { | ||
| 653 | my (@spanClass, @spanCont, $spanClose, $bkg); | ||
| 654 | if ($p >= $bkgStart and $p < $bkgEnd) { | ||
| 655 | foreach $bkg (@bkgSpan) { | ||
| 656 | next unless $p >= $$bkg{Start} and $p < $$bkg{End}; | ||
| 657 | push @spanClass, $$bkg{Class}; | ||
| 658 | if ($p + 1 == $$bkg{End}) { | ||
| 659 | $spanClose = 1; | ||
| 660 | } else { | ||
| 661 | push @spanCont, $$bkg{Class}; # this span continues | ||
| 662 | } | ||
| 663 | } | ||
| 664 | $self->Open('bkg', @spanClass ? "<span class='@spanClass'>" : '', 1, 2); | ||
| 665 | } else { | ||
| 666 | $self->Open('bkg', '', 1, 2); | ||
| 667 | } | ||
| 668 | $self->Open('a', $name, 1, 2); | ||
| 669 | my $ch = substr($$blockPt,$p-$pos-$skipped,1); | ||
| 670 | $c[1] .= sprintf("%.2x", ord($ch)); | ||
| 671 | # make the character HTML-friendly | ||
| 672 | $ch =~ tr/\x00-\x1f\x7f-\xff/./; | ||
| 673 | $ch =~ s/&/&/g; | ||
| 674 | $ch =~ s/>/>/g; | ||
| 675 | $ch =~ s/</</g; | ||
| 676 | $c[2] .= $ch; | ||
| 677 | ++$p; | ||
| 678 | ++$cols; | ||
| 679 | # close necessary elements | ||
| 680 | if ($spanClose) { | ||
| 681 | my $spanCont = @spanCont ? "<span class='@spanCont'>" : ''; | ||
| 682 | # close without reopening if closing anchor later | ||
| 683 | my $arg = ($p - $pos >= $len) ? 0 : $spanCont; | ||
| 684 | $self->Open('bkg', $arg, 1, 2); | ||
| 685 | } | ||
| 686 | if ($dblRef and $p >= $endPos) { | ||
| 687 | $dblRef = 0; | ||
| 688 | ++$id; | ||
| 689 | my $class = $ifd ? "'$id $ifd'" : $id; | ||
| 690 | $name =~ s/class=\w\b/class=$class/; | ||
| 691 | $f0 = ''; | ||
| 692 | $self->Open('fgd', $f0, 0); | ||
| 693 | } | ||
| 694 | if ($p - $pos >= $len) { | ||
| 695 | $self->Open('a', '', 1, 2); # close our anchor | ||
| 696 | last; | ||
| 697 | } | ||
| 698 | if ($cols < 16) { | ||
| 699 | $c[1] .= ($cols == 8 ? ' ' : ' '); | ||
| 700 | next; | ||
| 701 | } elsif ($flag & 0x01 and $cols < $len) { | ||
| 702 | $c[1] .= ' '; | ||
| 703 | next; # put it all on one line | ||
| 704 | } | ||
| 705 | unless ($$self{Msg}) { | ||
| 706 | $c[3] .= $msg; | ||
| 707 | $msg = ''; | ||
| 708 | } | ||
| 709 | $_ .= "\n" foreach @c; # add CR to all lines | ||
| 710 | $$self{Msg} = 0; | ||
| 711 | # limit data length if specified | ||
| 712 | if ($$self{Limit}) { | ||
| 713 | my $div = ($flag & 0x08) ? 4 : 1; | ||
| 714 | my $lim = $$self{Limit} / (2 * $div) - 16; | ||
| 715 | if ($p - $pos > $lim and $len - $p + $pos > $lim) { | ||
| 716 | my $n = ($len - $p + $pos - $lim) & ~0x0f; | ||
| 717 | if ($n > 16) { # (no use just cutting out one line) | ||
| 718 | $self->Open('bkg', '', 1, 2); # no underline | ||
| 719 | my $note = sprintf "[snip %d lines]", $n / 16; | ||
| 720 | $note = (' ' x (24-length($note)/2)) . $note; | ||
| 721 | $c[0] .= " ...\n"; | ||
| 722 | $c[1] .= $note . (' ' x (48-length($note))) . "\n"; | ||
| 723 | $c[2] .= " [snip] \n"; | ||
| 724 | $c[3] .= "\n"; | ||
| 725 | $p += $n; | ||
| 726 | $skipped += $len - length $$blockPt; | ||
| 727 | } | ||
| 728 | } | ||
| 729 | } | ||
| 730 | $c[0] .= ($p < 0 ? sprintf("-%.4x",-$p) : sprintf("%5.4x",$p)); | ||
| 731 | $cols = 0; | ||
| 732 | } | ||
| 733 | if ($msg) { | ||
| 734 | $msg = " $msg" if $$self{Msg}; | ||
| 735 | $c[3] .= $msg; | ||
| 736 | } | ||
| 737 | if ($flag & 0x100 and $cols < 16) { # continue on same line? | ||
| 738 | $$self{Cont} = 1; | ||
| 739 | $$self{Msg} = 1 if $msg; | ||
| 740 | } else { | ||
| 741 | $_ .= "\n" foreach @c; | ||
| 742 | $$self{Msg} = 0; | ||
| 743 | $$self{Cont} = 0; | ||
| 744 | } | ||
| 745 | # add temporary column data to our real columns | ||
| 746 | my $i; | ||
| 747 | for ($i=0; $i<4; ++$i) { | ||
| 748 | $self->{Cols}->[$i] .= $c[$i]; | ||
| 749 | } | ||
| 750 | delete $$self{TmpCols}; | ||
| 751 | } | ||
| 752 | |||
| 753 | #------------------------------------------------------------------------------ | ||
| 754 | # Finish dumping of TIFF image data | ||
| 755 | # Inputs: 0) HtmlDump object ref, 1) ExifTool object ref, 2) length of file | ||
| 756 | # (this really belongs in Image::ExifTool::Exif, but is placed here so it | ||
| 757 | # is only compiled when needed) | ||
| 758 | sub FinishTiffDump($$$) | ||
| 759 | { | ||
| 760 | my ($self, $et, $size) = @_; | ||
| 761 | my ($tag, $key, $start, $blockInfo, $i); | ||
| 762 | |||
| 763 | # list of all indirectly referenced TIFF data tags | ||
| 764 | my %offsetPair = ( | ||
| 765 | StripOffsets => 'StripByteCounts', | ||
| 766 | TileOffsets => 'TileByteCounts', | ||
| 767 | FreeOffsets => 'FreeByteCounts', | ||
| 768 | ThumbnailOffset => 'ThumbnailLength', | ||
| 769 | PreviewImageStart => 'PreviewImageLength', | ||
| 770 | JpgFromRawStart => 'JpgFromRawLength', | ||
| 771 | OtherImageStart => 'OtherImageLength', | ||
| 772 | PreviewJXLStart => 'PreviewJXLLength', | ||
| 773 | ImageOffset => 'ImageByteCount', | ||
| 774 | AlphaOffset => 'AlphaByteCount', | ||
| 775 | MPImageStart => 'MPImageLength', | ||
| 776 | IDCPreviewStart => 'IDCPreviewLength', | ||
| 777 | SamsungRawPointersOffset => 'SamsungRawPointersLength', | ||
| 778 | ); | ||
| 779 | |||
| 780 | # add TIFF data to html dump | ||
| 781 | foreach $tag (keys %offsetPair) { | ||
| 782 | my $info = $et->GetInfo($tag); | ||
| 783 | next unless %$info; | ||
| 784 | # Panasonic hack: StripOffsets is not valid for Panasonic RW2 files, | ||
| 785 | # and StripRowBytes is not valid for some RAW images | ||
| 786 | if ($tag eq 'StripOffsets' and $$et{TAG_INFO}{$tag}{PanasonicHack}) { | ||
| 787 | # use RawDataOffset instead if available since it is valid in RW2 | ||
| 788 | my $info2 = $et->GetInfo('RawDataOffset'); | ||
| 789 | $info2 = $info unless %$info2; | ||
| 790 | my @keys = keys %$info2; | ||
| 791 | my $offset = $$info2{$keys[0]}; | ||
| 792 | my $raf = $$et{RAF}; | ||
| 793 | # ignore StripByteCounts and assume raw data runs to the end of file | ||
| 794 | if (@keys == 1 and $offset =~ /^\d+$/ and $raf) { | ||
| 795 | my $pos = $raf->Tell(); | ||
| 796 | $raf->Seek(0, 2); # seek to end | ||
| 797 | my $len = $raf->Tell() - $offset; | ||
| 798 | $raf->Seek($pos, 0); | ||
| 799 | if ($len > 0) { | ||
| 800 | $self->Add($offset, $len, "(Panasonic raw data)", "Size: $len bytes", 0x08); | ||
| 801 | next; | ||
| 802 | } | ||
| 803 | } | ||
| 804 | } | ||
| 805 | # loop through all offsets tags | ||
| 806 | foreach $key (keys %$info) { | ||
| 807 | my $name = Image::ExifTool::GetTagName($key); | ||
| 808 | my $grp1 = $et->GetGroup($key, 1); | ||
| 809 | my $info2 = $et->GetInfo($offsetPair{$tag}, { Group1 => $grp1 }); | ||
| 810 | my $key2 = $offsetPair{$tag}; | ||
| 811 | $key2 .= $1 if $key =~ /( .*)/; # use same instance number as $tag | ||
| 812 | next unless $$info2{$key2}; | ||
| 813 | my $offsets = $$info{$key}; | ||
| 814 | my $byteCounts = $$info2{$key2}; | ||
| 815 | # ignore primary MPImage (this is the whole JPEG) | ||
| 816 | next if $tag eq 'MPImageStart' and $offsets eq '0'; | ||
| 817 | # (long lists may be SCALAR references) | ||
| 818 | my @offsets = split ' ', (ref $offsets ? $$offsets : $offsets); | ||
| 819 | my @byteCounts = split ' ', (ref $byteCounts ? $$byteCounts : $byteCounts); | ||
| 820 | my $num = scalar @offsets; | ||
| 821 | my $li = 0; | ||
| 822 | my $padBytes = 0; | ||
| 823 | for ($i=0; @offsets and @byteCounts; ++$i) { | ||
| 824 | my $offset = shift @offsets; | ||
| 825 | my $byteCount = shift @byteCounts; | ||
| 826 | my $end = $offset + $byteCount; | ||
| 827 | if (@offsets and @byteCounts) { | ||
| 828 | # show data as contiguous if only normal pad bytes between blocks | ||
| 829 | if ($end & 0x01 and $end + 1 == $offsets[0]) { | ||
| 830 | $end += 1; | ||
| 831 | ++$padBytes; # count them | ||
| 832 | } | ||
| 833 | if ($end == $offsets[0]) { | ||
| 834 | # combine these two blocks | ||
| 835 | $byteCounts[0] += $offsets[0] - $offset; | ||
| 836 | $offsets[0] = $offset; | ||
| 837 | next; | ||
| 838 | } | ||
| 839 | } | ||
| 840 | my $msg = $et->GetGroup($key, 1) . ':' . $tag; | ||
| 841 | $msg =~ s/(Offsets?|Start)$/ /; | ||
| 842 | if ($num > 1) { | ||
| 843 | $msg .= "$li-" if $li != $i; | ||
| 844 | $msg .= "$i "; | ||
| 845 | $li = $i + 1; | ||
| 846 | } | ||
| 847 | $msg .= "data"; | ||
| 848 | my $tip = "Size: $byteCount bytes"; | ||
| 849 | $tip .= ", incl. $padBytes pad bytes" if $padBytes; | ||
| 850 | $self->Add($offset, $byteCount, "($msg)", $tip, 0x08); | ||
| 851 | } | ||
| 852 | } | ||
| 853 | } | ||
| 854 | # find offset of last dumped information, and dump any unknown trailer | ||
| 855 | my $last = 0; | ||
| 856 | my $block = $$self{Block}; | ||
| 857 | foreach $start (keys %$block) { | ||
| 858 | foreach $blockInfo (@{$$block{$start}}) { | ||
| 859 | my $end = $start + $$blockInfo[0]; | ||
| 860 | $last = $end if $last < $end; | ||
| 861 | } | ||
| 862 | } | ||
| 863 | my $diff = $size - $last; | ||
| 864 | if ($diff > 0 and ($last or $et->Options('Unknown'))) { | ||
| 865 | if ($diff > 1 or $size & 0x01) { | ||
| 866 | $self->Add($last, $diff, "[unknown data]", "Size: $diff bytes", 0x08); | ||
| 867 | } else { | ||
| 868 | $self->Add($last, $diff, "[trailing pad byte]", undef, 0x08); | ||
| 869 | } | ||
| 870 | } | ||
| 871 | } | ||
| 872 | |||
| 873 | #------------------------------------------------------------------------------ | ||
| 874 | # utility routine to write to file or memory | ||
| 875 | # Inputs: 0) file or scalar reference, 1-N) list of stuff to write | ||
| 876 | # Returns: true on success | ||
| 877 | sub Write($@) | ||
| 878 | { | ||
| 879 | my $outfile = shift; | ||
| 880 | if (UNIVERSAL::isa($outfile,'GLOB')) { | ||
| 881 | return print $outfile @_; | ||
| 882 | } elsif (ref $outfile eq 'SCALAR') { | ||
| 883 | $$outfile .= join('', @_); | ||
| 884 | return 1; | ||
| 885 | } | ||
| 886 | return 0; | ||
| 887 | } | ||
| 888 | |||
| 889 | 1; # end | ||
| 890 | |||
| 891 | __END__ |