Image/ExifTool/Fixup.pm

filter: Statementstmt 74.0% 120 / 162 Branchbran 54.2% 38 / 70 Conditioncond 66.6% 10 / 15 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 83.3% 10 / 12 Podpod 0.0% 0 / 9 totaltotal 65.8% 181 / 275 CC 58 SCAR 27.3
CC
58
Cov
68%
HasMarker
42.0
Dump
42.0
ApplyFixup
14.0
CRAP 15.3
« Image/ExifTool/PLUS.pm ↑ Summary Image/ExifTool/Lytro.pm »
1 #------------------------------------------------------------------------------
2 # File: Fixup.pm
3 #
4 # Description: Utility to handle pointer fixups
5 #
6 # Revisions: 01/19/2005 - P. Harvey Created
7 # 04/11/2005 - P. Harvey Allow fixups to be tagged with a marker,
8 # and add new marker-related routines
9 # 06/21/2006 - P. Harvey Patch to work with negative offsets
10 # 07/07/2006 - P. Harvey Added support for 16-bit pointers
11 # 02/19/2013 - P. Harvey Added IsEmpty()
12 #
13 # Data Members:
14 #
15 # Start - Position in data where a zero pointer points to.
16 # Shift - Amount to shift offsets (relative to Start).
17 # Fixups - List of Fixup object references to to shift relative to this Fixup.
18 # Pointers - Hash of references to fixup pointer arrays, keyed by ByteOrder
19 # string (with "2" added if pointer is 16-bit [default is 32-bit],
20 # plus "_$marker" suffix if tagged with a marker name).
21 #
22 # Procedure:
23 #
24 # 1. Create a Fixup object for each data block containing pointers
25 # 2. Call AddFixup with the offset of each pointer in the block
26 # - pointer is assumed int32u with the current byte order
27 # - may also be called with a fixup reference for contained blocks
28 # 3. Add the necessary pointer offset to $$fixup{Shift}
29 # 4. Add data size to $$fixup{Start} if data is added before the block
30 # - automatically also shifts pointers by this amount
31 # 5. Call ApplyFixup to apply the fixup to all pointers
32 # - resets Shift and Start to 0 after applying fixup
33 #------------------------------------------------------------------------------
34  
35 package Image::ExifTool::Fixup;
36  
37 61 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 61 ▶use Image::ExifTool qw(GetByteOrder SetByteOrder Get32u Get32s Set32u
Subroutine BEGIN: called CC=1 SCAR=0.0
39 61 Get16u Get16s Set16u);
40 61 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 61 $VERSION = '1.06';
43  
44 sub AddFixup($$;$$);
45 sub ApplyFixup($$);
46 sub Dump($;$);
47  
48 #------------------------------------------------------------------------------
49 # New - create new Fixup object
50 # Inputs: 0) reference to Fixup object or Fixup class name
51 sub new
52 1168 ▶{
Subroutine new: called CC=3 SCAR=11.4
Pod undocumented
53 1168 local $_;
54 1168 my $that = shift;
55 1168 ▶ my $class = ref($that) || $that || 'Image::ExifTool::Fixup';
Condition40%2/5
ref $that || $that || 'Image::ExifTool::Fixup'
l!l
11680
ref $that or $that
l!l&&r!l&&!r
011680
MC/DC0%0/2
ref $that || $that || 'Image::ExifTool::Fixup'
ref $that $that
Truth table33%1/3
A or B
A= ref $thatB= $that
ABresult
1X1
011
000
56 1168 my $self = bless {}, $class;
57  
58 # initialize required members
59 1168 $self->{Start} = 0;
60 1168 $self->{Shift} = 0;
61  
62 1168 return $self;
63 }
64  
65 #------------------------------------------------------------------------------
66 # Clone this object
67 # Inputs: 0) reference to Fixup object or Fixup class name
68 # Returns: reference to new Fixup object
69 sub Clone($)
70 6 ▶{
Subroutine Clone: called CC=5 SCAR=17.8
Pod undocumented
71 6 my $self = shift;
72 6 my $clone = Image::ExifTool::Fixup->new;
73 6 $clone->{Start} = $self->{Start};
74 6 $clone->{Shift} = $self->{Shift};
75 6 my $phash = $self->{Pointers};
76 6 ▶ if ($phash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($phash) 6 0 6
77 6 $clone->{Pointers} = { };
78 6 my $byteOrder;
79 6 foreach $byteOrder (keys %$phash) {
80 7 my @pointers = @{$phash->{$byteOrder}};
81 7 $clone->{Pointers}->{$byteOrder} = \@pointers;
82 }
83 }
84 6 ▶ if ($self->{Fixups}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Fixups"}) 0 6 6
85 0 $clone->{Fixups} = [ ];
86 0 my $subFixup;
87 0 foreach $subFixup (@{$self->{Fixups}}) {
88 0 push @{$clone->{Fixups}}, $subFixup->Clone();
89 }
90 }
91 6 return $clone;
92 }
93  
94 #------------------------------------------------------------------------------
95 # Add fixup pointer or another fixup object below this one
96 # Inputs: 0) Fixup object reference
97 # 1) Scalar for pointer offset, or reference to Fixup object
98 # 2) Optional marker name for the pointer
99 # 3) Optional pointer format ('int16u' or 'int32u', defaults to 'int32u')
100 # Notes: Byte ordering must be set properly for the pointer being added (must keep
101 # track of the byte order of each offset since MakerNotes may have different byte order!)
102 sub AddFixup($$;$$)
103 3417 ▶{
Subroutine AddFixup: called CC=9 SCAR=23.3
Pod undocumented
104 3417 my ($self, $pointer, $marker, $format) = @_;
105 3417 ▶ if (ref $pointer) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pointer) { } 559 2858 3417
106 559 ▶ $self->{Fixups} or $self->{Fixups} = [ ];
Branch: 1 branch
BranchTrueFalseTotal
$self->{"Fixups"} or $self->{"Fixups"} = [] 230 329 559
107 559 push @{$self->{Fixups}}, $pointer;
108 } else {
109 2858 my $byteOrder = GetByteOrder();
110 2858 ▶ if (defined $format) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $format) 0 2858 2858
111 0 ▶ if ($format eq 'int16u') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'int16u') { } 0 0 0
elsif ($format ne 'int32u') { } 0 0 0
112 0 $byteOrder .= '2';
113 } elsif ($format ne 'int32u') {
114 0 warn "Bad Fixup pointer format $format\n";
115 }
116 }
117 2858 ▶ $byteOrder .= "_$marker" if defined $marker;
Branch: 1 branch
BranchTrueFalseTotal
if defined $marker 105 2753 2858
118 2858 my $phash = $self->{Pointers};
119 2858 ▶ $phash or $phash = $self->{Pointers} = { };
Branch: 1 branch
BranchTrueFalseTotal
$phash or $phash = ($self->{"Pointers"} = {}) 2410 448 2858
120 2858 ▶ $phash->{$byteOrder} or $phash->{$byteOrder} = [ ];
Branch: 1 branch
BranchTrueFalseTotal
$phash->{$byteOrder} or $phash->{$byteOrder} = [] 2316 542 2858
121 2858 push @{$phash->{$byteOrder}}, $pointer;
122 }
123 }
124  
125 #------------------------------------------------------------------------------
126 # fix up pointer offsets
127 # Inputs: 0) Fixup object reference, 1) data reference
128 # Outputs: Collapses fixup hierarchy into linear lists of fixup pointers
129 sub ApplyFixup($$)
130 1402 ▶{
Subroutine ApplyFixup: called CC=14 SCAR=26.4
Pod undocumented
131 1402 my ($self, $dataPt) = @_;
132  
133 1402 my $start = $self->{Start};
134 1402 my $shift = $self->{Shift} + $start; # make shift relative to start
135 1402 my $phash = $self->{Pointers};
136  
137 # fix up pointers in this fixup
138 1402 ▶ if ($phash and ($start or $shift)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($phash and $start || $shift) 652 750 1402
Condition100%6/6
$phash and $start || $shift
!ll&&!rl&&r
626124652
$start or $shift
l!l&&r!l&&!r
506146124
MC/DC100%3/3
$phash and $start || $shift
$phash $start $shift
Truth table100%4/4
A and B or C
A= $phashB= $startC= $shift
ABCresult
0XX0
1000
11X1
1011
139 652 my $saveOrder = GetByteOrder(); # save original byte ordering
140 652 my ($byteOrder, $ptr);
141 652 foreach $byteOrder (keys %$phash) {
142 729 SetByteOrder(substr($byteOrder,0,2));
143 # apply the fixup offset shift (must get as signed integer
144 # to avoid overflow in case it was negative before)
145 729 ▶ my ($get, $set) = ($byteOrder =~ /^(II2|MM2)/) ?
Branch: 1 branch
BranchTrueFalseTotal
$byteOrder =~ /^(II2|MM2)/ ? : 0 729 729
146 (\&Get16s, \&Set16u) : (\&Get32s, \&Set32u);
147 729 foreach $ptr (@{$phash->{$byteOrder}}) {
148 7328 $ptr += $start; # update pointer to new start location
149 7328 ▶ next unless $shift;
Branch: 1 branch
BranchTrueFalseTotal
unless $shift 0 7328 7328
150 7328 &$set(&$get($dataPt, $ptr) + $shift, $dataPt, $ptr);
151 }
152 }
153 652 SetByteOrder($saveOrder); # restore original byte ordering
154 }
155 # recurse into contained fixups
156 1402 ▶ if ($self->{Fixups}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Fixups"}) 329 1073 1402
157 # create our pointer hash if it doesn't exist
158 329 ▶ $phash or $phash = $self->{Pointers} = { };
Branch: 1 branch
BranchTrueFalseTotal
$phash or $phash = ($self->{"Pointers"} = {}) 131 198 329
159 # loop through all contained fixups
160 329 my $subFixup;
161 329 foreach $subFixup (@{$self->{Fixups}}) {
162 # adjust the subfixup start and shift
163 559 $subFixup->{Start} += $start;
164 559 $subFixup->{Shift} += $shift - $start;
165 # recursively apply contained fixups
166 559 ApplyFixup($subFixup, $dataPt);
167 559 ▶ my $shash = $subFixup->{Pointers} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $shash = $subFixup->{"Pointers"} or next 479 80 559
168 # add all pointers to our collapsed lists
169 479 my $byteOrder;
170 479 foreach $byteOrder (keys %$shash) {
171 467 ▶ $phash->{$byteOrder} or $phash->{$byteOrder} = [ ];
Branch: 1 branch
BranchTrueFalseTotal
$phash->{$byteOrder} or $phash->{$byteOrder} = [] 281 186 467
172 467 push @{$phash->{$byteOrder}}, @{$shash->{$byteOrder}};
173 467 delete $shash->{$byteOrder};
174 }
175 479 delete $subFixup->{Pointers};
176 }
177 329 delete $self->{Fixups}; # remove our contained fixups
178 }
179 # reset our Start/Shift for the collapsed fixup
180 1402 $self->{Start} = $self->{Shift} = 0;
181 }
182  
183 #------------------------------------------------------------------------------
184 # Is this Fixup empty?
185 # Inputs: 0) Fixup object ref
186 # Returns: True if there are no offsets to fix
187 sub IsEmpty($)
188 13 ▶{
Subroutine IsEmpty: called CC=5 SCAR=16.5
Pod undocumented
189 13 my $self = shift;
190 13 my $phash = $self->{Pointers};
191 13 ▶ if ($phash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($phash) 13 0 13
192 13 my $key;
193 13 foreach $key (keys %$phash) {
194 12 ▶ next unless ref $$phash{$key} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $phash->{$key} eq "ARRAY" 0 12 12
195 12 ▶ return 0 if @{$$phash{$key}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$phash->{$key};} 12 0 12
196 }
197 }
198 1 return 1;
199 }
200  
201 #------------------------------------------------------------------------------
202 # Does specified marker exist?
203 # Inputs: 0) Fixup object reference, 1) marker name
204 # Returns: True if fixup contains specified marker name
205 sub HasMarker($$)
206 0 ▶{
Subroutine HasMarker: not called CC=6 SCAR=37.4
Pod undocumented
207 0 my ($self, $marker) = @_;
208 0 my $phash = $self->{Pointers};
209 0 ▶ return 0 unless $phash;
Branch: 1 branch
BranchTrueFalseTotal
unless $phash 0 0 0
210 0 ▶ return 1 if grep /_$marker$/, keys %$phash;
Branch: 1 branch
BranchTrueFalseTotal
if grep /_$marker$/, keys %$phash 0 0 0
211 0 ▶ return 0 unless $self->{Fixups};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Fixups"} 0 0 0
212 0 my $subFixup;
213 0 foreach $subFixup (@{$self->{Fixups}}) {
214 0 ▶ return 1 if $subFixup->HasMarker($marker);
Branch: 1 branch
BranchTrueFalseTotal
if $subFixup->HasMarker($marker) 0 0 0
215 }
216 0 return 0;
217 }
218  
219 #------------------------------------------------------------------------------
220 # Set all marker pointers to specified value
221 # Inputs: 0) Fixup object reference, 1) data reference
222 # 2) marker name, 3) pointer value, 4) offset to start of data
223 sub SetMarkerPointers($$$$;$)
224 131 ▶{
Subroutine SetMarkerPointers: called CC=9 SCAR=23.2
Pod undocumented
225 131 my ($self, $dataPt, $marker, $value, $startOffset) = @_;
226 131 ▶ my $start = $self->{Start} + ($startOffset || 0);
Condition50%1/2
$startOffset || 0
l!l
0131
MC/DC0%0/1
$startOffset || 0
$startOffset
Truth table50%1/2
A
A= $startOffset
Aresult
11
00
227 131 my $phash = $self->{Pointers};
228  
229 131 ▶ if ($phash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($phash) 125 6 131
230 125 my $saveOrder = GetByteOrder(); # save original byte ordering
231 125 my ($byteOrder, $ptr);
232 125 foreach $byteOrder (keys %$phash) {
233 222 ▶ next unless $byteOrder =~ /^(II|MM)(2?)_$marker$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $byteOrder =~ /^(II|MM)(2?)_$marker$/ 216 6 222
234 6 SetByteOrder($1);
235 6 ▶ my $set = $2 ? \&Set16u : \&Set32u;
Branch: 1 branch
BranchTrueFalseTotal
$2 ? : 0 6 6
236 6 foreach $ptr (@{$phash->{$byteOrder}}) {
237 7 &$set($value, $dataPt, $ptr + $start);
238 }
239 }
240 125 SetByteOrder($saveOrder); # restore original byte ordering
241 }
242 131 ▶ if ($self->{Fixups}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Fixups"}) 0 131 131
243 0 my $subFixup;
244 0 foreach $subFixup (@{$self->{Fixups}}) {
245 0 $subFixup->SetMarkerPointers($dataPt, $marker, $value, $start);
246 }
247 }
248 }
249  
250 #------------------------------------------------------------------------------
251 # Get pointer values for specified marker
252 # Inputs: 0) Fixup object reference, 1) data reference,
253 # 2) marker name, 3) offset to start of data
254 # Returns: List of marker pointers in list context, or first marker pointer otherwise
255 sub GetMarkerPointers($$$;$)
256 6 ▶{
Subroutine GetMarkerPointers: called CC=9 SCAR=24.6
Pod undocumented
257 6 my ($self, $dataPt, $marker, $startOffset) = @_;
258 6 ▶ my $start = $self->{Start} + ($startOffset || 0);
Condition50%1/2
$startOffset || 0
l!l
06
MC/DC0%0/1
$startOffset || 0
$startOffset
Truth table50%1/2
A
A= $startOffset
Aresult
11
00
259 6 my $phash = $self->{Pointers};
260 6 my @pointers;
261  
262 6 ▶ if ($phash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($phash) 6 0 6
263 6 my $saveOrder = GetByteOrder();
264 6 my ($byteOrder, $ptr);
265 6 foreach $byteOrder (grep /_$marker$/, keys %$phash) {
266 3 SetByteOrder(substr($byteOrder,0,2));
267 3 ▶ my $get = ($byteOrder =~ /^(II2|MM2)/) ? \&Get16u : \&Get32u;
Branch: 1 branch
BranchTrueFalseTotal
$byteOrder =~ /^(II2|MM2)/ ? : 0 3 3
268 3 foreach $ptr (@{$phash->{$byteOrder}}) {
269 9 push @pointers, &$get($dataPt, $ptr + $start);
270 }
271 }
272 6 SetByteOrder($saveOrder); # restore original byte ordering
273 }
274 6 ▶ if ($self->{Fixups}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Fixups"}) 0 6 6
275 0 my $subFixup;
276 0 foreach $subFixup (@{$self->{Fixups}}) {
277 0 push @pointers, $subFixup->GetMarkerPointers($dataPt, $marker, $start);
278 }
279 }
280 6 ▶ return @pointers if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 6 0 6
281 0 return $pointers[0];
282 }
283  
284 #------------------------------------------------------------------------------
285 # Dump fixup to console for debugging
286 # Inputs: 0) Fixup object reference, 1) optional initial indent string
287 sub Dump($;$)
288 0 ▶{
Subroutine Dump: not called CC=6 SCAR=37.4
Pod undocumented
289 0 my ($self, $indent) = @_;
290 0 ▶ $indent or $indent = '';
Branch: 1 branch
BranchTrueFalseTotal
$indent or $indent = "" 0 0 0
291 0 printf "${indent}Fixup start=0x%x shift=0x%x\n", $self->{Start}, $self->{Shift};
292 0 my $phash = $self->{Pointers};
293 0 ▶ if ($phash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($phash) 0 0 0
294 0 my $byteOrder;
295 0 foreach $byteOrder (sort keys %$phash) {
296 0 print "$indent $byteOrder: ", join(' ',@{$phash->{$byteOrder}}),"\n";
297 }
298 }
299 0 ▶ if ($self->{Fixups}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Fixups"}) 0 0 0
300 0 my $subFixup;
301 0 foreach $subFixup (@{$self->{Fixups}}) {
302 0 Dump($subFixup, $indent . ' ');
303 }
304 }
305 }
306  
307  
308 61 1; # end
309  
310 __END__
« Image/ExifTool/PLUS.pm ↑ Summary Image/ExifTool/Lytro.pm »