Binary.pm

filter: Statementstmt 14.6% 95 / 650 Branchbran 0.4% 1 / 228 Conditioncond 0.0% 0 / 89 MC/DCmcdc 0.0% 0 / 51 Subroutinesub 22.4% 29 / 129 Podpod 0.0% 0 / 63 totaltotal 10.3% 125 / 1210 CC 165 SCAR 46.7
CC
165
Cov
10%
init
871.1
next_member
90.0
dispatch_class
72.0
CRAP 107.2
« Binary/FixedFormat.pm ↑ Summary Binary/FixedFormat/Variants.pm »
1 package Parse::Binary;
2 1 $Parse::Binary::VERSION = '0.11';
3  
4 1 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Parse::Binary::FixedFormat;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Parse::Binary - Unpack binary data structures into object hierarchies
13  
14 =head1 VERSION
15  
16 This document describes version 0.11 of Parse::Binary, released
17 January 25, 2009.
18  
19 =head1 SYNOPSIS
20  
21 # This class represents a Win32 F<.ico> file:
22  
23 package IconFile;
24 use base 'Parse::Binary';
25 use constant FORMAT => (
26 Magic => 'a2',
27 Type => 'v',
28 Count => 'v',
29 'Icon' => [ 'a16', '{$Count}', 1 ],
30 Data => 'a*',
31 );
32  
33 # An individual icon resource:
34  
35 package Icon;
36 use base 'Parse::Binary';
37 use constant FORMAT => (
38 Width => 'C',
39 Height => 'C',
40 ColorCount => 'C',
41 Reserved => 'C',
42 Planes => 'v',
43 BitCount => 'v',
44 ImageSize => 'V',
45 ImageOffset => 'v',
46 );
47 sub Data {
48 my ($self) = @_;
49 return $self->parent->substr($self->ImageOffset, $self->ImageSize);
50 }
51  
52 # Simple F<.ico> file dumper that uses them:
53  
54 use IconFile;
55 my $icon_file = IconFile->new('input.ico');
56 foreach my $icon ($icon_file->members) {
57 print "Dimension: ", $icon->Width, "x", $icon->Height, $/;
58 print "Colors: ", 2 ** $icon->BitCount, $/;
59 print "Image Size: ", $icon->ImageSize, " bytes", $/;
60 print "Actual Size: ", length($icon->Data), " bytes", $/, $/;
61 }
62 $icon_file->write('output.ico'); # save as another .ico file
63  
64 =head1 DESCRIPTION
65  
66 This module makes parsing binary data structures much easier, by serving
67 as a base class for classes that represents the binary data, which may
68 contain objects of other classes to represent parts of itself.
69  
70 Documentation is unfortunately a bit lacking at this moment. Please read
71 the tests and source code of L<Parse::AFP> and L<Win32::Exe> for examples
72 of using this module.
73  
74 =cut
75  
76 1 ▶use constant PROPERTIES => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
77 %struct $filename $size $parent @siblings %children
78 $output $lazy $iterator $iterated
79 1 );
80 1 ▶use constant ENCODED_FIELDS => ( 'Data' );
Subroutine BEGIN: called CC=1 SCAR=0.0
81 1 ▶use constant FORMAT => ( Data => 'a*' );
Subroutine BEGIN: called CC=1 SCAR=0.0
82 1 ▶use constant SUBFORMAT => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
83 1 ▶use constant DEFAULT_ARGS => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
84 1 ▶use constant DELEGATE_SUBS => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
85 1 ▶use constant DISPATCH_TABLE => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 1 ▶use constant DISPATCH_FIELD => undef;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 1 ▶use constant BASE_CLASS => undef;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 1 ▶use constant ENCODING => undef;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 1 ▶use constant PADDING => undef;
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 1 ▶unless (eval { require Scalar::Util; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require Scalar::Util; 1 } }) 0 1 1
93 0 ▶ *Scalar::Util::weaken = sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
94 0 ▶ *Scalar::Util::blessed = sub { UNIVERSAL::can($_[0], 'can') };
Subroutine __ANON__: not called CC=1 SCAR=6.9
95 }
96  
97 ### Constructors ###
98  
99 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=37.5
Pod undocumented
100 0 my ($self, $input, $attr) = @_;
101  
102 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
103 0 my $class = $self->class;
104 0 ▶ $class->init unless ${"$class\::init_done"};
Branch: 1 branch
BranchTrueFalseTotal
unless ${"${class}::init_done";} 0 0 0
105  
106 0 ▶ $attr ||= {};
Condition0%0/2
$attr ||= {}
l!l
00
MC/DC0%0/1
$attr ||= {}
$attr
Truth table0%0/2
A
A= $attr
Aresult
11
00
107 0 ▶ $attr->{filename} ||= $input unless ref $input;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $input 0 0 0
Condition0%0/3
$attr->{"filename"} ||= $input
l!l&&r!l&&!r
000
MC/DC0%0/2
$attr->{"filename"} ||= $input
$attr->{"filename"} $input
Truth table0%0/3
A ||= B
A= $attr->{"filename"}B= $input
ABresult
1X1
011
000
108  
109 0 my $obj = $class->spawn;
110 0 %$obj = (%$obj, %$attr);
111  
112 0 my $data = $obj->read_data($input);
113 0 $obj->load($data, $attr);
114  
115 0 ▶ if ($obj->{lazy}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($obj->{'lazy'}) { } 0 0 0
elsif (not $obj->{'iterator'}) { } 0 0 0
116 0 $obj->{lazy} = $obj;
117 }
118 elsif (!$obj->{iterator}) {
119 0 $obj->make_members;
120 }
121  
122 0 return $obj;
123 }
124  
125 0 ▶sub dispatch_field {
Subroutine dispatch_field: not called CC=1 SCAR=6.9
Pod undocumented
126 0 return undef;
127 }
128  
129 1 ▶use vars qw(%HasMembers %DefaultArgs);
Subroutine BEGIN: called CC=1 SCAR=0.0
130 1 ▶use vars qw(%Fields %MemberFields %MemberClass %Packer %Parser %FieldPackFormat);
Subroutine BEGIN: called CC=1 SCAR=0.0
131 1 ▶use vars qw(%DispatchField %DispatchTable);
Subroutine BEGIN: called CC=1 SCAR=0.0
132  
133 0 ▶sub init {
Subroutine init: not called CC=30 SCAR=67.7
Pod undocumented
134 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
135 0 ▶ return if ${"$_[0]\::init_done"};
Branch: 1 branch
BranchTrueFalseTotal
if ${"$_[0]::init_done";} 0 0 0
136  
137 0 my $class = shift;
138  
139 0 ▶ *{"$class\::class"} = sub { ref($_[0]) || $_[0] };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] or $_[0] 0 0 0
140 0 *{"$class\::is_type"} = \&is_type;
141  
142 0 foreach my $item ($class->PROPERTIES) {
143 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
144 0 my ($sigil, $name) = split(//, $item, 2);
145 0 *{"$class\::$name"} =
146 0 ▶ ($sigil eq '$') ? sub { $_[0]{$name} } :
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 0 ▶ ($sigil eq '@') ? sub { wantarray ? @{$_[0]{$name}||=[]} : ($_[0]{$name}||=[]) } :
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
Condition0%0/4
$_[0]{$name} ||= []
l!l
00
$_[0]{$name} ||= []
l!l
00
MC/DC0%0/1
$_[0]{$name} ||= []
$_[0]{$name}
MC/DC0%0/1
$_[0]{$name} ||= []
$_[0]{$name}
Truth table0%0/2
A
A= $_[0]{$name}
Aresult
11
00
Truth table0%0/2
A
A= $_[0]{$name}
Aresult
11
00
148 0 ▶ ($sigil eq '%') ? sub { $_[0]{$name}||={} } :
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[0]{$name} ||= {}
l!l
00
MC/DC0%0/1
$_[0]{$name} ||= {}
$_[0]{$name}
Truth table0%0/2
A
A= $_[0]{$name}
Aresult
11
00
149 0 ▶ die "Unknown sigil: $sigil";
Branch: 3 branches
BranchTrueFalseTotal
$sigil eq '$' ? : 0 0 0
$sigil eq '@' ? : 0 0 0
$sigil eq '%' ? : 0 0 0
150 0 *{"$class\::set_$name"} =
151 0 ▶ ($sigil eq '$') ? sub { $_[0]->{$name} = $_[1] } :
Subroutine __ANON__: not called CC=1 SCAR=6.9
152 0 ▶ ($sigil eq '@') ? sub { @{$_[0]->{$name}||=$_[1]||[]} = @{$_[1]||[]} } :
Subroutine __ANON__: not called CC=4 SCAR=30.0
Condition0%0/7
$_[1] || []
l!l
00
$_[0]{$name} ||= $_[1] || []
l!l&&r!l&&!r
000
$_[1] || []
l!l
00
MC/DC0%0/2
$_[0]{$name} ||= $_[1] || []
$_[0]{$name} $_[1]
Truth table0%0/3
A ||= B
A= $_[0]{$name}B= $_[1]
ABresult
1X1
011
000
153 0 ▶ ($sigil eq '%') ? sub { %{$_[0]->{$name}||=$_[1]||{}} = %{$_[1]||{}} } :
Subroutine __ANON__: not called CC=4 SCAR=30.0
Condition0%0/7
$_[1] || {}
l!l
00
$_[0]{$name} ||= $_[1] || {}
l!l&&r!l&&!r
000
$_[1] || {}
l!l
00
MC/DC0%0/2
$_[0]{$name} ||= $_[1] || {}
$_[0]{$name} $_[1]
Truth table0%0/3
A ||= B
A= $_[0]{$name}B= $_[1]
ABresult
1X1
011
000
154 0 ▶ die "Unknown sigil: $sigil";
Branch: 3 branches
BranchTrueFalseTotal
$sigil eq '$' ? : 0 0 0
$sigil eq '@' ? : 0 0 0
$sigil eq '%' ? : 0 0 0
155 }
156  
157 0 my @args = $class->default_args;
158 0 *{"$class\::default_args"} = \@args;
159 0 ▶ *{"$class\::default_args"} = sub { @args };
Subroutine __ANON__: not called CC=1 SCAR=6.9
160 0 my $delegate_subs = $class->delegate_subs;
161 0 ▶ if (defined(&{"$class\::DELEGATE_SUBS"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &{"${class}::DELEGATE_SUBS";}) 0 0 0
162 0 $delegate_subs = { $class->DELEGATE_SUBS };
163 }
164 0 ▶ *{"$class\::delegate_subs"} = sub { $delegate_subs };
Subroutine __ANON__: not called CC=1 SCAR=6.9
165 0 ▶ while (my ($subclass, $methods) = each %$delegate_subs) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$delegate_subs) 0 0 0
166 0 ▶ $methods = [ $methods ] unless ref $methods;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $methods 0 0 0
167 0 foreach my $method (grep length, @$methods) {
168 0 ▶ *{"$class\::$method"} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
169 0 goto &{$_[0]->require_class($subclass)->can($method)};
170 0 };
171 }
172 }
173 0 my $dispatch_table = $class->dispatch_table;
174 0 ▶ if (defined(&{"$class\::DISPATCH_TABLE"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &{"${class}::DISPATCH_TABLE";}) 0 0 0
175 0 $dispatch_table = { $class->DISPATCH_TABLE };
176 }
177 0 $DispatchTable{$class} = $dispatch_table;
178 0 ▶ *{"$class\::dispatch_table"} = sub { $dispatch_table };
Subroutine __ANON__: not called CC=1 SCAR=6.9
179  
180 0 my $dispatch_field = undef;
181 0 ▶ if (defined(&{"$class\::DISPATCH_FIELD"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &{"${class}::DISPATCH_FIELD";}) 0 0 0
182 0 $dispatch_field = $class->DISPATCH_FIELD;
183 }
184 0 $DispatchField{$class} = $dispatch_field;
185 0 ▶ *{"$class\::dispatch_field"} = sub { $dispatch_field };
Subroutine __ANON__: not called CC=1 SCAR=6.9
186  
187 0 my @format = $class->format_list;
188 0 ▶ if (my @subformat = $class->subformat_list) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@subformat) = $class->subformat_list) 0 0 0
189 0 my @new_format;
190 0 ▶ while (my ($field, $format) = splice(@format, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@format, 0, 2)) 0 0 0
191 0 ▶ if ($field eq 'Data') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq 'Data') { } 0 0 0
192 0 push @new_format, @subformat;
193 }
194 else {
195 0 push @new_format, ($field => $format);
196 }
197 }
198 0 @format = @new_format;
199 }
200 0 my @format_list = @format;
201 0 ▶ *{"$class\::format_list"} = sub { @format_list };
Subroutine __ANON__: not called CC=1 SCAR=6.9
202  
203 0 my (@fields, @formats, @pack_formats, $underscore_count);
204 0 my (%field_format, %field_pack_format);
205 0 my (%field_parser, %field_packer, %field_length);
206 0 my (@member_fields, %member_class);
207 0 ▶ while (my ($field, $format) = splice(@format, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@format, 0, 2)) 0 0 0
208 0 ▶ if ($field eq '_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq "_") 0 0 0
209 # "we don't care" fields
210 0 $underscore_count++;
211 0 $field = "_${underscore_count}_$class";
212 0 $field =~ s/:/_/g;
213 }
214  
215 0 ▶ if (ref $format) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $format) { } 0 0 0
216 0 $member_class{$field} = $class->classname($field);
217 0 $field =~ s/:/_/g;
218 0 $member_class{$field} = $class->classname($field);
219 0 $class->require($member_class{$field});
220 0 push @member_fields, $field;
221 }
222 else {
223 0 $format = [ $format ];
224 }
225  
226 0 push @fields, $field;
227  
228 0 my $string = join(':', $field, @$format);
229 0 $field_format{$field} = [ @$format ];
230 0 ▶ if (!grep /\{/, @$format) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /\{/, @$format) 0 0 0
231 0 $field_length{$field} = length(pack($format->[0], 0));
232 0 $field_parser{$field} = Parse::Binary::FixedFormat->new( [ $string ] );
233 }
234 0 push @formats, $string;
235  
236 0 s/\s*X\s*//g for @$format;
237 0 my $pack_string = join(':', $field, @$format);
238 0 $field_pack_format{$field} = [ @$format ];
239 0 $field_packer{$field} = Parse::Binary::FixedFormat->new( [ $pack_string ] );
240 0 push @pack_formats, $pack_string;
241 }
242  
243 0 my $parser = $class->make_formatter(@formats);
244 0 my $packer = $class->make_formatter(@pack_formats);
245  
246 0 $Packer{$class} = $packer;
247 0 $Parser{$class} = $parser;
248 0 $Fields{$class} = \@fields;
249 0 ▶ $HasMembers{$class} = @member_fields ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@member_fields ? : 0 0 0
250 0 $DefaultArgs{$class} = \@args;
251 0 $MemberClass{$class} = \%member_class;
252 0 $MemberFields{$class} = \@member_fields;
253 0 ▶ $FieldPackFormat{$class} = { map { ref($_) ? $_->[0] : $_ } %field_pack_format };
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
254  
255 0 *{"$class\::fields"} = \@fields;
256 0 *{"$class\::member_fields"} = \@member_fields;
257 0 ▶ *{"$class\::has_members"} = @member_fields ? sub { 1 } : sub { 0 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@member_fields ? : 0 0 0
258 0 ▶ *{"$class\::fields"} = sub { @fields };
Subroutine __ANON__: not called CC=1 SCAR=6.9
259 0 ▶ *{"$class\::formats"} = sub { @formats };
Subroutine __ANON__: not called CC=1 SCAR=6.9
260 0 ▶ *{"$class\::member_fields"} = sub { @member_fields };
Subroutine __ANON__: not called CC=1 SCAR=6.9
261 0 ▶ *{"$class\::member_class"} = sub { $member_class{$_[1]} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
262 0 ▶ *{"$class\::pack_formats"} = sub { @pack_formats };
Subroutine __ANON__: not called CC=1 SCAR=6.9
263 0 ▶ *{"$class\::field_format"} = sub { $field_format{$_[1]}[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
264 0 ▶ *{"$class\::field_pack_format"} = sub { $field_pack_format{$_[1]}[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
265 0 ▶ *{"$class\::field_length"} = sub { $field_length{$_[1]} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
266  
267 0 ▶ *{"$class\::parser"} = sub { $parser };
Subroutine __ANON__: not called CC=1 SCAR=6.9
268 0 ▶ *{"$class\::packer"} = sub { $packer };
Subroutine __ANON__: not called CC=1 SCAR=6.9
269 0 ▶ *{"$class\::field_parser"} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
270 0 my ($self, $field) = @_;
271 0 ▶ $field_parser{$field} || do {
Branch: 1 branch
BranchTrueFalseTotal
$field_parser{$field} or do { "Parse::Binary::FixedFormat"->new([$self->eval_format($self->{"struct"}, join(":", $field, @{$field_format{$field};}))]) } 0 0 0
272 Parse::Binary::FixedFormat->new( [
273 $self->eval_format(
274 $self->{struct},
275 0 join(':', $field, @{$field_format{$field}}),
276 ),
277 ] );
278 };
279 0 };
280  
281 0 ▶ *{"$class\::field_packer"} = sub { $field_packer{$_[1]} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
282 0 ▶ *{"$class\::has_field"} = sub { $field_packer{$_[1]} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
283  
284 0 my %enc_fields = map { ($_ => 1) } $class->ENCODED_FIELDS;
285  
286 0 foreach my $field (@fields) {
287 0 ▶ next if defined &{"$class\::$field"};
Branch: 1 branch
BranchTrueFalseTotal
if defined &{"${class}::$field";} 0 0 0
288  
289 0 ▶ if ($enc_fields{$field} and my $encoding = $class->ENCODING) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc_fields{$field} and my $encoding = $class->ENCODING) 0 0 0
Condition0%0/3
$enc_fields{$field} and my $encoding = $class->ENCODING
!ll&&!rl&&r
000
MC/DC0%0/2
$enc_fields{$field} and my $encoding = $class->ENCODING
$enc_fields{$field} my $encoding = $class->ENCODING
Truth table0%0/3
A and B
A= $enc_fields{$field}B= my $encoding = $class->ENCODING
ABresult
0X0
100
111
290 0 require Encode;
291  
292 0 ▶ *{"$class\::$field"} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
293 0 my ($self) = @_;
294 0 return Encode::decode($encoding => $self->{struct}{$field});
295 0 };
296  
297 0 ▶ *{"$class\::Set$field"} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
298 0 my ($self, $data) = @_;
299 0 $self->{struct}{$field} = Encode::encode($encoding => $data);
300 0 };
301 0 next;
302 }
303  
304 0 ▶ *{"$class\::$field"} = sub { $_[0]->{struct}{$field} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
305 0 ▶ *{"$class\::Set$field"} = sub { $_[0]->{struct}{$field} = $_[1] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
306 }
307  
308 0 ${"$class\::init_done"} = 1;
309 }
310  
311 0 ▶sub initialize {
Subroutine initialize: not called CC=1 SCAR=6.9
Pod undocumented
312 0 return 1;
313 }
314  
315 ### Miscellanous ###
316  
317 0 ▶sub field {
Subroutine field: not called CC=1 SCAR=6.9
Pod undocumented
318 0 my ($self, $field) = @_;
319 0 return $self->{struct}{$field};
320 }
321  
322 0 ▶sub set_field {
Subroutine set_field: not called CC=1 SCAR=6.9
Pod undocumented
323 0 my ($self, $field, $data) = @_;
324 0 $self->{struct}{$field} = $data;
325 }
326  
327 0 ▶sub classname {
Subroutine classname: not called CC=4 SCAR=30.0
Pod undocumented
328 0 my ($self, $class) = @_;
329 0 ▶ return undef unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 0 0
330  
331 0 $class =~ s/__/::/g;
332  
333 0 ▶ my $base_class = $self->BASE_CLASS or return $class;
Branch: 1 branch
BranchTrueFalseTotal
my $base_class = $self->BASE_CLASS or return $class 0 0 0
334 0 ▶ return $base_class if $class eq '::BASE::';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "::BASE::" 0 0 0
335  
336 0 return "$base_class\::$class";
337 }
338  
339 0 ▶sub member_fields {
Subroutine member_fields: not called CC=1 SCAR=6.9
Pod undocumented
340 0 return ();
341 }
342  
343 0 ▶sub dispatch_class {
Subroutine dispatch_class: not called CC=8 SCAR=42.8
Pod undocumented
344 0 my ($self, $field) = @_;
345 0 my $table = $DispatchTable{ref $self};
346 0 ▶ my $class = exists($table->{$field}) ? $table->{$field} : $table->{'*'};
Branch: 1 branch
BranchTrueFalseTotal
exists $table->{$field} ? : 0 0 0
347  
348 0 ▶ $class = &$class($self, $field) if UNIVERSAL::isa($class, 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($class, "CODE") 0 0 0
349 0 ▶ defined $class or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $class or return 0 0 0
350  
351 0 ▶ if (my $members = $self->{parent}{callback_members}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $members = $self->{"parent"}{"callback_members"}) 0 0 0
352 0 ▶ return unless $members->{$class};
Branch: 1 branch
BranchTrueFalseTotal
unless $members->{$class} 0 0 0
353 }
354 0 ▶ my $subclass = $self->classname($class) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $subclass = $self->classname($class) or return 0 0 0
355 0 ▶ return if $subclass eq $class;
Branch: 1 branch
BranchTrueFalseTotal
if $subclass eq $class 0 0 0
356 0 return $subclass;
357 }
358  
359 0 ▶sub require {
Subroutine require: not called CC=4 SCAR=30.0
Pod undocumented
360 0 my ($class, $module) = @_;
361 0 ▶ return unless defined $module;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $module 0 0 0
362  
363 0 my $file = "$module.pm";
364 0 $file =~ s{::}{/}g;
365  
366 0 ▶ return $module if (eval { require $file; 1 });
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require $file; 1 } } 0 0 0
367 0 ▶ die $@ unless $@ =~ /^Can't locate /;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /^Can't locate / 0 0 0
368 0 return;
369 }
370  
371 0 ▶sub require_class {
Subroutine require_class: not called CC=1 SCAR=6.9
Pod undocumented
372 0 my ($class, $subclass) = @_;
373 0 return $class->require($class->classname($subclass));
374 }
375  
376 0 ▶sub format_list {
Subroutine format_list: not called CC=1 SCAR=6.9
Pod undocumented
377 0 my ($self) = @_;
378 0 return $self->FORMAT;
379 }
380  
381 0 ▶sub subformat_list {
Subroutine subformat_list: not called CC=2 SCAR=17.9
Pod undocumented
382 0 my ($self) = @_;
383 0 ▶ $self->SUBFORMAT ? $self->SUBFORMAT : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->SUBFORMAT ? : 0 0 0
384 }
385  
386 0 ▶sub default_args {
Subroutine default_args: not called CC=2 SCAR=17.9
Pod undocumented
387 0 my ($self) = @_;
388 0 ▶ $self->DEFAULT_ARGS ? $self->DEFAULT_ARGS : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->DEFAULT_ARGS ? : 0 0 0
389 }
390  
391 0 ▶sub dispatch_table {
Subroutine dispatch_table: not called CC=2 SCAR=17.9
Pod undocumented
392 0 my ($self) = @_;
393 0 ▶ $self->DISPATCH_TABLE ? { $self->DISPATCH_TABLE } : {};
Branch: 1 branch
BranchTrueFalseTotal
$self->DISPATCH_TABLE ? : 0 0 0
394 }
395  
396 0 ▶sub delegate_subs {
Subroutine delegate_subs: not called CC=2 SCAR=17.9
Pod undocumented
397 0 my ($self) = @_;
398 0 ▶ $self->DELEGATE_SUBS ? { $self->DELEGATE_SUBS } : {};
Branch: 1 branch
BranchTrueFalseTotal
$self->DELEGATE_SUBS ? : 0 0 0
399 }
400  
401 0 ▶sub class {
Subroutine class: not called CC=2 SCAR=17.9
Pod undocumented
402 0 my ($self) = @_;
403 0 ▶ return(ref($self) || $self);
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
404 }
405  
406 0 ▶sub make_formatter {
Subroutine make_formatter: not called CC=1 SCAR=6.9
Pod undocumented
407 0 my ($self, @formats) = @_;
408 0 return Parse::Binary::FixedFormat->new( $self->make_format(@formats) );
409 }
410  
411 0 ▶sub make_format {
Subroutine make_format: not called CC=4 SCAR=30.0
Pod undocumented
412 0 my ($self, @formats) = @_;
413 0 ▶ return \@formats unless grep /\{/, @formats;
Branch: 1 branch
BranchTrueFalseTotal
unless grep /\{/, @formats 0 0 0
414  
415 0 my @prefix;
416 0 foreach my $format (@formats) {
417 0 ▶ last if $format =~ /\{/;
Branch: 1 branch
BranchTrueFalseTotal
if $format =~ /\{/ 0 0 0
418 0 push @prefix, $format;
419 }
420 return {
421 0 ▶ Chooser => sub { $self->chooser(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
422 0 Formats => [ \@prefix, \@formats ],
423 };
424 }
425  
426 0 ▶sub chooser {
Subroutine chooser: not called CC=1 SCAR=6.9
Pod undocumented
427 0 my ($self, $rec, $obj, $mode) = @_;
428 0 my $idx = @{$obj->{Layouts}};
429 0 my @format = $self->eval_format($rec, @{$obj->{Formats}[1]});
430 0 $obj->{Layouts}[$idx] = $self->make_formatter(@format);
431 0 return $idx;
432 }
433  
434 0 ▶sub eval_format {
Subroutine eval_format: not called CC=6 SCAR=37.4
Pod undocumented
435 0 my ($self, $rec, @format) = @_;
436 0 foreach my $key (sort keys %$rec) {
437 0 s/\$$key\b/$rec->{$key}/ for @format;
438 }
439 0 ▶ !/\$/ and s/\{(.*?)\}/$1/eeg for @format;
Branch: 1 branch
BranchTrueFalseTotal
/\$/ or s/\{(.*?)\}/$1;/gee 0 0 0
440 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
441 0 return @format;
442 }
443  
444 0 ▶sub padding {
Subroutine padding: not called CC=1 SCAR=6.9
Pod undocumented
445 0 return '';
446 }
447  
448 0 ▶sub load_struct {
Subroutine load_struct: not called CC=1 SCAR=6.9
Pod undocumented
449 0 my ($self, $data) = @_;
450 0 $self->{struct} = $Parser{ref $self}->unformat($$data . $self->padding, $self->{lazy}, $self);
451 }
452  
453 0 ▶sub load_size {
Subroutine load_size: not called CC=1 SCAR=6.9
Pod undocumented
454 0 my ($self, $data) = @_;
455 0 $self->{size} = length($$data);
456 0 return 1;
457 }
458  
459 0 ▶sub lazy_load {
Subroutine lazy_load: not called CC=3 SCAR=24.8
Pod undocumented
460 0 my ($self) = @_;
461 0 ▶ ref(my $sub = $self->{lazy}) or return;
Branch: 1 branch
BranchTrueFalseTotal
ref(my $sub = $self->{"lazy"}) or return 0 0 0
462 0 $self->{lazy} = 1;
463 0 ▶ $self->make_members unless $self->{iterator};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"iterator"} 0 0 0
464 }
465  
466 1 my %DispatchClass;
467 0 ▶sub load {
Subroutine load: not called CC=7 SCAR=37.5
Pod undocumented
468 0 my ($self, $data, $attr) = @_;
469 0 ▶ return $self unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
470  
471 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
472 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
473 0 ▶ $class->init unless ${"$class\::init_done"};
Branch: 1 branch
BranchTrueFalseTotal
unless ${"${class}::init_done";} 0 0 0
474  
475 0 $self->load_struct($data);
476 0 $self->load_size($data);
477  
478 0 ▶ if (my $field = $DispatchField{$class}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $field = $Parse::Binary::DispatchField{$class}) 0 0 0
479 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (my $subclass = ($DispatchClass{$class}{$self->{"struct"}{$field}} ||= $self->dispatch_class($self->{"struct"}{$field}))) 0 0 0
Condition0%0/3
$DispatchClass{$class}{$self->{"struct"}{$field}} ||= $self->dispatch_class($self->{"struct"}{$field})
l!l&&r!l&&!r
000
MC/DC0%0/2
$DispatchClass{$class}{$self->{"struct"}{$field}} ||= $self->dispatch_class($self->{"struct"}{$field})
$DispatchClass{$class}{$self->{"struct"}{$field}} $self->dispatch_class($self->{"struct"}{$field})
Truth table0%0/3
A ||= B
A= $DispatchClass{$class}{$self->{"struct"}{$field}}B= $self->dispatch_class($self->{"struct"}{$field})
ABresult
1X1
011
000
480 my $subclass = $DispatchClass{$class}{ $self->{struct}{$field} }
481 ||= $self->dispatch_class( $self->{struct}{$field})
482 ) {
483 0 $self->require($subclass);
484 0 bless($self, $subclass);
485 0 $self->load($data, $attr);
486 }
487 }
488  
489 0 return $self;
490 }
491  
492 1 my (%classname, %fill_cache);
493 0 ▶sub spawn {
Subroutine spawn: not called CC=5 SCAR=30.9
Pod undocumented
494 0 my ($self, %args) = @_;
495 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
496  
497 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
498  
499 0 ▶ if (my $subclass = delete($args{Class})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subclass = delete $args{"Class"}) 0 0 0
500 0 ▶ $class = $classname{$subclass} ||= do {
Condition0%0/3
$classname{$subclass} ||= do { my $name = $self->classname($subclass); $self->require($name); $name->init; $name }
l!l&&r!l&&!r
000
MC/DC0%0/2
$classname{$subclass} ||= do { my $name = $self->classname($subclass); $self->require($name); $name->init; $name }
$classname{$subclass} do { my $name = $self->classname($subclass); $self->require($name); $name->init; $name }
Truth table0%0/3
A ||= B
A= $classname{$subclass}B= do { my $name = $self->classname($subclass); $self->require($name); $name->init; $name }
ABresult
1X1
011
000
501 0 my $name = $self->classname($subclass);
502 0 $self->require($name);
503 0 $name->init;
504 0 $name;
505 };
506 }
507  
508 bless({
509 struct => {
510 %args,
511 0 @{ $DefaultArgs{$class} },
512 0 ▶ %{ $fill_cache{$class} ||= $class->fill_in },
Condition0%0/3
$fill_cache{$class} ||= $class->fill_in
l!l&&r!l&&!r
000
MC/DC0%0/2
$fill_cache{$class} ||= $class->fill_in
$fill_cache{$class} $class->fill_in
Truth table0%0/3
A ||= B
A= $fill_cache{$class}B= $class->fill_in
ABresult
1X1
011
000
513 },
514 }, $class);
515 }
516  
517 0 ▶sub fill_in {
Subroutine fill_in: not called CC=6 SCAR=37.4
Pod undocumented
518 0 my $class = shift;
519 0 my $entries = {};
520  
521 0 foreach my $super_class ($class->superclasses) {
522 0 ▶ my $field = $DispatchField{$super_class} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $field = $Parse::Binary::DispatchField{$super_class} or next 0 0 0
523 0 ▶ my $table = $DispatchTable{$super_class} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $table = $Parse::Binary::DispatchTable{$super_class} or next 0 0 0
524 0 foreach my $code (reverse sort keys %$table) {
525 0 ▶ $class->is_type($table->{$code}) or next;
Branch: 1 branch
BranchTrueFalseTotal
$class->is_type($table->{$code}) or next 0 0 0
526 0 $entries->{$field} = $code;
527 0 last;
528 }
529 }
530  
531 0 return $entries;
532 }
533  
534 0 ▶sub spawn_sibling {
Subroutine spawn_sibling: not called CC=2 SCAR=17.9
Pod undocumented
535 0 my ($self, %args) = @_;
536 0 ▶ my $parent = $self->{parent} or die "$self has no parent";
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $self->{"parent"} or die "$self has no parent" 0 0 0
537  
538 0 my $obj = $self->spawn(%args);
539 0 @{$obj}{qw( lazy parent output siblings )} =
540 0 @{$self}{qw( lazy parent output siblings )};
541 0 $obj->{size} = length($obj->dump);
542 0 $obj->refresh_parent;
543 0 $obj->initialize;
544  
545 0 return $obj;
546 }
547  
548 0 ▶sub sibling_index {
Subroutine sibling_index: not called CC=5 SCAR=34.0
Pod undocumented
549 0 my ($self, $obj) = @_;
550 0 ▶ $obj ||= $self;
Condition0%0/3
$obj ||= $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj ||= $self
$obj $self
Truth table0%0/3
A ||= B
A= $objB= $self
ABresult
1X1
011
000
551  
552 0 my @siblings = @{$self->{siblings}};
553 0 ▶ foreach my $index (($obj->{index}||0) .. $#siblings) {
Condition0%0/2
$obj->{'index'} || 0
l!l
00
MC/DC0%0/1
$obj->{'index'} || 0
$obj->{'index'}
Truth table0%0/2
A
A= $obj->{'index'}
Aresult
11
00
554 0 ▶ return $index if $obj == $siblings[$index];
Branch: 1 branch
BranchTrueFalseTotal
if $obj == $siblings[$index] 0 0 0
555 }
556  
557 0 return undef;
558 }
559  
560 0 ▶sub gone {
Subroutine gone: not called CC=2 SCAR=17.9
Pod undocumented
561 0 my ($self, $obj) = @_;
562 0 ▶ $self->{parent}{struct}{Data} .= ($obj || $self)->dump;
Condition0%0/3
$obj || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj || $self
$obj $self
Truth table0%0/3
A || B
A= $objB= $self
ABresult
1X1
011
000
563 }
564  
565 0 ▶sub prepend_obj {
Subroutine prepend_obj: not called CC=3 SCAR=24.8
Pod undocumented
566 0 my ($self, %args) = @_;
567 0 ▶ if ($self->{lazy}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lazy"}) 0 0 0
568 0 my $obj = $self->spawn(%args);
569 0 $self->gone($obj);
570 0 return;
571 }
572 0 my $obj = $self->spawn_sibling(%args);
573 0 my $siblings = $self->{siblings};
574 0 ▶ my $index = $self->{index} ? $self->{index}++ : $self->sibling_index;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'index'} ? : 0 0 0
575 0 $obj->{index} = $index;
576  
577 0 splice(@$siblings, $index, 0, $obj);
578 0 return $obj;
579 }
580  
581 0 ▶sub append_obj {
Subroutine append_obj: not called CC=2 SCAR=17.9
Pod undocumented
582 0 my ($self, %args) = @_;
583 0 my $obj = $self->spawn_sibling(%args);
584  
585 0 @{$self->{siblings}} = (
586 0 ▶ map { $_, (($_ == $self) ? $obj : ()) } @{$self->{siblings}}
Branch: 1 branch
BranchTrueFalseTotal
$_ == $self ? : 0 0 0
587 );
588 0 return $obj;
589 }
590  
591 0 ▶sub remove {
Subroutine remove: not called CC=1 SCAR=6.9
Pod undocumented
592 0 my ($self, %args) = @_;
593 0 my $siblings = $self->{siblings};
594 0 splice(@$siblings, $self->sibling_index, 1, undef);
595  
596 0 Scalar::Util::weaken($self->{parent});
597 0 Scalar::Util::weaken($self);
598 }
599  
600 0 ▶sub read_data {
Subroutine read_data: not called CC=4 SCAR=30.0
Pod undocumented
601 0 my ($self, $data) = @_;
602 0 ▶ return undef unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
603 0 ▶ return \($data->dump) if UNIVERSAL::can($data, 'dump');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can($data, "dump") 0 0 0
604 0 ▶ return $data if UNIVERSAL::isa($data, 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($data, "SCALAR") 0 0 0
605 0 return \($self->read_file($data));
606 }
607  
608 0 ▶sub read_file {
Subroutine read_file: not called CC=2 SCAR=17.9
Pod undocumented
609 0 my ($self, $file) = @_;
610  
611 0 local *FH; local $/;
612 0 ▶ open FH, "< $file" or die "Cannot open $file for reading: $!";
Branch: 1 branch
BranchTrueFalseTotal
open FH, "< $file" or die "Cannot open $file for reading: $!" 0 0 0
613 0 binmode(FH);
614  
615 0 return scalar <FH>;
616 }
617  
618 0 ▶sub make_members {
Subroutine make_members: not called CC=3 SCAR=24.8
Pod undocumented
619 0 my ($self) = @_;
620  
621 0 ▶ $HasMembers{ref $self} or return;
Branch: 1 branch
BranchTrueFalseTotal
$Parse::Binary::HasMembers{ref $self} or return 0 0 0
622 0 %{$self->{children}} = ();
623  
624 0 foreach my $field (@{$MemberFields{ref $self}}) {
625 my ($format) = $self->eval_format(
626 $self->{struct},
627 0 $FieldPackFormat{ref $self}{$field},
628 );
629  
630 my $members = [ map {
631 0 $self->new_member( $field, \pack($format, @$_) )
632 } $self->validate_memberdata($field) ];
633 0 $self->set_field_children( $field, $members );
634 }
635 }
636  
637 0 ▶sub set_members {
Subroutine set_members: not called CC=1 SCAR=6.9
Pod undocumented
638 0 my ($self, $field, $members) = @_;
639 0 $field =~ s/:/_/g;
640 $self->set_field_children(
641 $field,
642 0 [ map { $self->new_member( $field, $_ ) } @$members ],
643 );
644 }
645  
646 0 ▶sub set_field_children {
Subroutine set_field_children: not called CC=1 SCAR=6.9
Pod undocumented
647 0 my ($self, $field, $data) = @_;
648 0 my $children = $self->field_children($field);
649 0 @$children = @$data;
650 0 return $children;
651 }
652  
653 0 ▶sub field_children {
Subroutine field_children: not called CC=3 SCAR=24.8
Pod undocumented
654 0 my ($self, $field) = @_;
655 0 ▶ my $children = ($self->{children}{$field} ||= []);
Condition0%0/2
$self->{"children"}{$field} ||= []
l!l
00
MC/DC0%0/1
$self->{"children"}{$field} ||= []
$self->{"children"}{$field}
Truth table0%0/2
A
A= $self->{"children"}{$field}
Aresult
11
00
656 # $_->lazy_load for @$children;
657 0 ▶ return(wantarray ? @$children : $children);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
658 }
659  
660 0 ▶sub validate_memberdata {
Subroutine validate_memberdata: not called CC=2 SCAR=17.9
Pod undocumented
661 0 my ($self, $field) = @_;
662 0 ▶ return @{$self->{struct}{$field}||[]};
Condition0%0/2
$self->{'struct'}{$field} || []
l!l
00
MC/DC0%0/1
$self->{'struct'}{$field} || []
$self->{'struct'}{$field}
Truth table0%0/2
A
A= $self->{'struct'}{$field}
Aresult
11
00
663 }
664  
665 0 ▶sub first_member {
Subroutine first_member: not called CC=5 SCAR=28.8
Pod undocumented
666 0 my ($self, $type) = @_;
667 0 $self->lazy_load;
668  
669 0 ▶ return undef unless $HasMembers{ref $self};
Branch: 1 branch
BranchTrueFalseTotal
unless $Parse::Binary::HasMembers{ref $self} 0 0 0
670  
671 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
672 0 foreach my $field (@{$MemberFields{ref $self}}) {
673 0 foreach my $member ($self->field_children($field)) {
674 0 ▶ return $member if $member->is_type($type);
Branch: 1 branch
BranchTrueFalseTotal
if $member->is_type($type) 0 0 0
675 }
676 }
677 0 return undef;
678 }
679  
680 0 ▶sub next_member {
Subroutine next_member: not called CC=9 SCAR=45.0
Pod undocumented
681 0 my ($self, $type) = @_;
682 0 ▶ return undef unless $HasMembers{ref $self};
Branch: 1 branch
BranchTrueFalseTotal
unless $Parse::Binary::HasMembers{ref $self} 0 0 0
683  
684 0 ▶ if ($self->{lazy} and !$self->{iterated}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lazy"} and not $self->{"iterated"}) 0 0 0
Condition0%0/3
$self->{"lazy"} and not $self->{"iterated"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"lazy"} and not $self->{"iterated"}
$self->{"lazy"} not $self->{"iterated"}
Truth table0%0/3
A and not(B)
A= $self->{"lazy"}B= not $self->{"iterated"}
ABresult
0X0
100
111
685 0 ▶ if (ref($self->{lazy})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"lazy"}) 0 0 0
686 0 %{$self->{children}} = ();
687 0 $self->{iterator} = $self->make_next_member;
688 0 $self->lazy_load;
689 }
690  
691 0 ▶ while (my $member = &{$self->{iterator}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $member = &{$self->{"iterator"};}) 0 0 0
692 0 ▶ return $member if $member->is_type($type);
Branch: 1 branch
BranchTrueFalseTotal
if $member->is_type($type) 0 0 0
693 }
694 0 $self->{iterated} = 1;
695 0 return;
696 }
697  
698 0 ▶ $self->{_next_member}{$type} ||= $self->members($type);
Condition0%0/3
$self->{"_next_member"}{$type} ||= $self->members($type)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_next_member"}{$type} ||= $self->members($type)
$self->{"_next_member"}{$type} $self->members($type)
Truth table0%0/3
A ||= B
A= $self->{"_next_member"}{$type}B= $self->members($type)
ABresult
1X1
011
000
699  
700 0 shift(@{$self->{_next_member}{$type}})
701 0 ▶ || undef($self->{_next_member}{$type});
Branch: 1 branch
BranchTrueFalseTotal
shift @{$$self{"_next_member"}{$type};} or undef $self->{"_next_member"}{$type} 0 0 0
702 }
703  
704 0 ▶sub make_next_member {
Subroutine make_next_member: not called CC=1 SCAR=6.9
Pod undocumented
705 0 my $self = shift;
706 0 my $class = ref($self);
707 0 my ($field_idx, $item_idx, $format) = (0, 0, undef);
708 0 my @fields = @{$MemberFields{$class}};
709 0 my $struct = $self->{struct};
710 0 my $formats = $FieldPackFormat{$class};
711  
712 0 ▶ sub { LOOP: {
Subroutine __ANON__: not called CC=6 SCAR=37.4
713 0 ▶ my $field = $fields[$field_idx] or return;
Branch: 1 branch
BranchTrueFalseTotal
my $field = $fields[$field_idx] or return 0 0 0
714  
715 0 my $items = $struct->{$field};
716 0 ▶ if ($item_idx > $#$items) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item_idx > $items->$#*) 0 0 0
717 0 $field_idx++;
718 0 $item_idx = 0;
719 0 undef $format;
720 0 redo;
721 }
722  
723 0 ▶ $format ||= ($self->eval_format( $struct, $formats->{$field} ))[0];
Condition0%0/3
$format ||= ($self->eval_format($struct, $formats->{$field}))[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$format ||= ($self->eval_format($struct, $formats->{$field}))[0]
$format ($self->eval_format($struct, $formats->{$field}))[0]
Truth table0%0/3
A ||= B
A= $formatB= ($self->eval_format($struct, $formats->{$field}))[0]
ABresult
1X1
011
000
724  
725 0 my $item = $items->[$item_idx++];
726 0 ▶ $item = $item->($self, $items) if UNIVERSAL::isa($item, 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($item, "CODE") 0 0 0
727 0 ▶ $self->valid_memberdata($item) or redo;
Branch: 1 branch
BranchTrueFalseTotal
$self->valid_memberdata($item) or redo 0 0 0
728  
729 0 my $member = $self->new_member( $field, \pack($format, @$item) );
730 0 $member->{index} = (push @{$self->{children}{$field}}, $member) - 1;
731 0 return $member;
732 0 } };
733 }
734  
735 0 ▶sub members {
Subroutine members: not called CC=3 SCAR=20.0
Pod undocumented
736 0 my ($self, $type) = @_;
737 0 $self->lazy_load;
738  
739 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
740 my @members = map {
741 0 ▶ grep { $type ? $_->is_type($type) : 1 } $self->field_children($_)
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 0 0 0
742 0 } @{$MemberFields{ref $self}};
743 0 ▶ wantarray ? @members : \@members;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
744 }
745  
746 0 ▶sub members_recursive {
Subroutine members_recursive: not called CC=3 SCAR=24.8
Pod undocumented
747 0 my ($self, $type) = @_;
748 my @members = (
749 ( $self->is_type($type) ? $self : () ),
750 0 ▶ map { $_->members_recursive($type) } $self->members
Branch: 1 branch
BranchTrueFalseTotal
$self->is_type($type) ? : 0 0 0
751 );
752 0 ▶ wantarray ? @members : \@members;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
753 }
754  
755 0 ▶sub new_member {
Subroutine new_member: not called CC=2 SCAR=17.9
Pod undocumented
756 0 my ($self, $field, $data) = @_;
757 my $obj = $MemberClass{ref $self}{$field}->new(
758 0 $data, { lazy => $self->{lazy}, parent => $self }
759 );
760  
761 0 $obj->{output} = $self->{output};
762 0 ▶ $obj->{siblings} = $self->{children}{$field}||=[];
Condition0%0/2
$self->{"children"}{$field} ||= []
l!l
00
MC/DC0%0/1
$self->{"children"}{$field} ||= []
$self->{"children"}{$field}
Truth table0%0/2
A
A= $self->{"children"}{$field}
Aresult
11
00
763 0 $obj->initialize;
764  
765 0 return $obj;
766 }
767  
768 0 ▶sub valid_memberdata {
Subroutine valid_memberdata: not called CC=1 SCAR=6.9
Pod undocumented
769 0 length($_[-1][0])
770 }
771  
772 0 ▶sub dump_members {
Subroutine dump_members: not called CC=1 SCAR=6.9
Pod undocumented
773 0 my ($self) = @_;
774 0 return $Packer{ref $self}->format($self->{struct});
775 }
776  
777 0 ▶sub dump {
Subroutine dump: not called CC=2 SCAR=17.9
Pod undocumented
778 0 my ($self) = @_;
779 0 ▶ return $self->dump_members if $HasMembers{ref $self};
Branch: 1 branch
BranchTrueFalseTotal
if $Parse::Binary::HasMembers{ref $self} 0 0 0
780 0 return $Packer{ref $self}->format($self->{struct});
781 }
782  
783 0 ▶sub write {
Subroutine write: not called CC=6 SCAR=37.4
Pod undocumented
784 0 my ($self, $file) = @_;
785  
786 0 ▶ if (ref($file)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $file) { } 0 0 0
elsif (not defined $file and my $fh = $self->{'output'}) { } 0 0 0
787 0 $$file = $self->dump;
788 }
789 ▶ elsif (!defined($file) and my $fh = $self->{output}) {
Condition0%0/3
not defined $file and my $fh = $self->{"output"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $file and my $fh = $self->{"output"}
not defined $file my $fh = $self->{"output"}
Truth table0%0/3
not(A) and B
A= not defined $fileB= my $fh = $self->{"output"}
ABresult
0X0
100
111
790 0 print $fh $self->dump;
791 }
792 else {
793 0 ▶ $file = $self->{filename} unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
794 0 ▶ $self->write_file($file, $self->dump) if defined $file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file 0 0 0
795 }
796 }
797  
798 0 ▶sub write_file {
Subroutine write_file: not called CC=2 SCAR=17.9
Pod undocumented
799 0 my ($self, $file, $data) = @_;
800 0 local *FH;
801 0 ▶ open FH, "> $file" or die "Cannot open $file for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open FH, "> $file" or die "Cannot open $file for writing: $!" 0 0 0
802 0 binmode(FH);
803 0 print FH $data;
804 };
805  
806 0 ▶sub superclasses {
Subroutine superclasses: not called CC=1 SCAR=1.7
Pod undocumented
807 0 my ($self) = @_;
808 0 my $class = $self->class;
809  
810 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
811 0 return @{"$class\::ISA"};
812 }
813  
814 1 my %type_cache;
815 0 ▶sub is_type {
Subroutine is_type: not called CC=7 SCAR=37.1
Pod undocumented
816 0 my ($self, $type) = @_;
817 0 ▶ return 1 unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 0 0
818  
819 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
820  
821 0 ▶ if (exists $type_cache{$class}{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $type_cache{$class}{$type}) 0 0 0
822 0 return $type_cache{$class}{$type};
823 }
824  
825 0 $type_cache{$class}{$type} = 1;
826  
827  
828 0 $type =~ s/__/::/g;
829 0 $type =~ s/[^\w:]//g;
830 0 ▶ return 1 if ($class =~ /::$type$/);
Branch: 1 branch
BranchTrueFalseTotal
if $class =~ /::$type$/ 0 0 0
831  
832 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
833 0 foreach my $super_class ($class->superclasses) {
834 0 ▶ return 1 if $super_class->is_type($type);
Branch: 1 branch
BranchTrueFalseTotal
if $super_class->is_type($type) 0 0 0
835 };
836  
837 0 $type_cache{$class}{$type} = 0;
838 }
839  
840 0 ▶sub refresh {
Subroutine refresh: not called CC=3 SCAR=24.8
Pod undocumented
841 0 my ($self) = @_;
842  
843 0 foreach my $field (@{$MemberFields{ref $self}}) {
844 0 my $parser = $self->field_parser($field);
845 0 my $padding = $self->padding;
846  
847 0 ▶ local $SIG{__WARN__} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
848 0 @{$self->{struct}{$field}} = map {
849 0 $parser->unformat( $_->dump . $padding, 0, $self)->{$field}[0]
850 0 ▶ } grep defined, @{$self->{children}{$field}||[]};
Condition0%0/2
$self->{'children'}{$field} || []
l!l
00
MC/DC0%0/1
$self->{'children'}{$field} || []
$self->{'children'}{$field}
Truth table0%0/2
A
A= $self->{'children'}{$field}
Aresult
11
00
851  
852 0 $self->validate_memberdata;
853 }
854  
855 0 $self->refresh_parent;
856 }
857  
858 0 ▶sub refresh_parent {
Subroutine refresh_parent: not called CC=4 SCAR=30.0
Pod undocumented
859 0 my ($self) = @_;
860 0 ▶ my $parent = $self->{parent} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $self->{"parent"} or return 0 0 0
861 0 ▶ $parent->refresh unless !Scalar::Util::blessed($parent) or $parent->{lazy};
Branch: 1 branch
BranchTrueFalseTotal
unless not builtin::blessed($parent) or $parent->{"lazy"} 0 0 0
Condition0%0/3
not builtin::blessed($parent) or $parent->{"lazy"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not builtin::blessed($parent) or $parent->{"lazy"}
not builtin::blessed($parent) $parent->{"lazy"}
Truth table0%0/3
not(A) or B
A= not builtin::blessed($parent)B= $parent->{"lazy"}
ABresult
1X1
011
000
862 }
863  
864 0 ▶sub first_parent {
Subroutine first_parent: not called CC=3 SCAR=24.8
Pod undocumented
865 0 my ($self, $type) = @_;
866 0 ▶ return $self if $self->is_type($type);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_type($type) 0 0 0
867 0 ▶ my $parent = $self->{parent} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $self->{"parent"} or return 0 0 0
868 0 return $parent->first_parent($type);
869 }
870  
871 0 ▶sub substr {
Subroutine substr: not called CC=4 SCAR=30.0
Pod undocumented
872 0 my $self = shift;
873 0 my $data = $self->Data;
874 0 my $offset = shift(@_) - ($self->{size} - length($data));
875 0 ▶ my $length = @_ ? shift(@_) : (length($data) - $offset);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
876 0 my $replace = shift;
877  
878 # XXX - Check for "substr outside string"
879 0 ▶ return if $offset > length($data);
Branch: 1 branch
BranchTrueFalseTotal
if $offset > length $data 0 0 0
880  
881 # Fetch a range
882 0 ▶ return substr($data, $offset, $length) if !defined $replace;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $replace 0 0 0
883  
884 # Substitute a range
885 0 substr($data, $offset, $length, $replace);
886 0 $self->{struct}{Data} = $data;
887 }
888  
889 0 ▶sub set_output_file {
Subroutine set_output_file: not called CC=2 SCAR=17.9
Pod undocumented
890 0 my ($self, $file) = @_;
891  
892 0 ▶ open my $fh, '>', $file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or die $! 0 0 0
893 0 binmode($fh);
894 0 $self->{output} = $fh;
895 }
896  
897 1 my %callback_map;
898 0 ▶sub callback {
Subroutine callback: not called CC=6 SCAR=37.4
Pod undocumented
899 0 my $self = shift;
900 0 ▶ my $pkg = shift || caller;
Condition0%0/3
shift() || caller
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || caller
shift() caller
Truth table0%0/3
A || B
A= shift()B= caller
ABresult
1X1
011
000
901 0 ▶ my $types = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $types = shift() or return 0 0 0
902  
903 0 ▶ my $map = $callback_map{"@$types"} ||= $self->callback_map($pkg, $types);
Condition0%0/3
$callback_map{join $", @$types} ||= $self->callback_map($pkg, $types)
l!l&&r!l&&!r
000
MC/DC0%0/2
$callback_map{join $", @$types} ||= $self->callback_map($pkg, $types)
$callback_map{join $", @$types} $self->callback_map($pkg, $types)
Truth table0%0/3
A ||= B
A= $callback_map{join $", @$types}B= $self->callback_map($pkg, $types)
ABresult
1X1
011
000
904 0 ▶ my $sub = $map->{ref $self} || $map->{'*'} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $$map{ref $self} || $map->{"*"} or return 0 0 0
Condition0%0/3
$$map{ref $self} || $map->{'*'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$$map{ref $self} || $map->{'*'}
$$map{ref $self} $map->{'*'}
Truth table0%0/3
A || B
A= $$map{ref $self}B= $map->{'*'}
ABresult
1X1
011
000
905 0 unshift @_, $self;
906 0 goto &$sub;
907 }
908  
909 0 ▶sub callback_map {
Subroutine callback_map: not called CC=4 SCAR=26.3
Pod undocumented
910 0 my ($self, $pkg, $types) = @_;
911 0 my %map;
912 0 my $base = $self->BASE_CLASS;
913 0 foreach my $type (map "$_", @$types) {
914 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
915 0 my $method = $type;
916 0 $method =~ s/::/_/g;
917 0 $method =~ s/\*/__/g;
918  
919 0 ▶ defined &{"$pkg\::$method"} or next;
Branch: 1 branch
BranchTrueFalseTotal
defined &{"${pkg}::$method";} or next 0 0 0
920  
921 0 ▶ $type = "$base\::$type" unless $type eq '*';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "*" 0 0 0
922 0 $map{$type} = \&{"$pkg\::$method"};
923 }
924 0 return \%map;
925 }
926  
927 0 ▶sub callback_members {
Subroutine callback_members: not called CC=2 SCAR=17.9
Pod undocumented
928 0 my $self = shift;
929 0 $self->{callback_members} = { map { ($_ => 1) } @{$_[0]} };
930  
931 0 ▶ while (my $member = $self->next_member) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $member = $self->next_member) 0 0 0
932 0 $member->callback(scalar caller, @_);
933 }
934 }
935  
936 0 ▶sub done {
Subroutine done: not called CC=2 SCAR=17.9
Pod undocumented
937 0 my $self = shift;
938 0 ▶ return unless $self->{lazy};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"lazy"} 0 0 0
939 0 $self->write;
940 0 $self->remove;
941 }
942  
943 1 1;
944  
945 __END__
« Binary/FixedFormat.pm ↑ Summary Binary/FixedFormat/Variants.pm »