DBus/Marshal.pm

filter: Statementstmt 91.8% 158 / 172 Branchbran 85.0% 51 / 60 Conditioncond 56.2% 9 / 16 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 84.2% 16 / 19 Podpod 0.0% 0 / 6 totaltotal 84.0% 237 / 282 CC 40 SCAR 21.0
CC
40
Cov
88%
_marshal
14.1
_unmarshal_sct
11.5
_marshal_array
10.0
CRAP 8.2
« DBus/Client/AnyEvent.pm ↑ Summary DBus/Message.pm »
1 package Protocol::DBus::Marshal;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Protocol::DBus::Pack ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Protocol::DBus::Signature ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 our $_ENDIAN_PACK;
10  
11 # Set this to get actual Perl filehandles in the
12 # message body. XXX FIXME This is a very hacky way to do it!
13 8 our $FILEHANDLES;
14  
15 # XXX FIXME Hackety-hack …
16 8 our $PRESERVE_VARIANT_SIGNATURES;
17  
18 # for testing
19 8 our $DICT_CANONICAL;
20  
21 8 our @_MARSHAL_FDS;
22  
23 # sig, data (array ref)
24 37 ▶sub marshal_le {
Subroutine marshal_le: called CC=1 SCAR=0.0
Pod undocumented
25 37 local $_ENDIAN_PACK = '<';
26 37 local @_MARSHAL_FDS;
27 37 return( _marshal(@_[0, 1]), \@_MARSHAL_FDS );
28 }
29  
30 # buf, buf offset, sig
31 41 ▶sub unmarshal_le {
Subroutine unmarshal_le: called CC=1 SCAR=0.0
Pod undocumented
32 41 local $_ENDIAN_PACK = '<';
33 41 return _unmarshal(@_);
34 }
35  
36 4 ▶sub marshal_be {
Subroutine marshal_be: called CC=1 SCAR=0.0
Pod undocumented
37 4 local $_ENDIAN_PACK = '>';
38 4 local @_MARSHAL_FDS;
39 4 return( _marshal(@_[0, 1]), \@_MARSHAL_FDS );
40 }
41  
42 1 ▶sub unmarshal_be {
Subroutine unmarshal_be: called CC=1 SCAR=0.0
Pod undocumented
43 1 local $_ENDIAN_PACK = '>';
44 1 return _unmarshal(@_);
45 }
46  
47 #----------------------------------------------------------------------
48  
49 231 ▶sub _marshal {
Subroutine _marshal: called CC=14 SCAR=26.5
50 231 my ($sig, $data, $buf_sr, $_data_are_not_list) = @_;
51  
52 231 ▶ $buf_sr ||= \do { my $v = q<> };
Condition100%2/2
$buf_sr ||= \do { my $v = "" }
l!l
19041
MC/DC100%1/1
$buf_sr ||= \do { my $v = "" }
$buf_sr
Truth table100%2/2
A
A= $buf_sr
Aresult
11
00
53  
54 231 my @scts = Protocol::DBus::Signature::split($sig);
55  
56 231 for my $si ( 0 .. $#scts ) {
57 296 my $sct = $scts[$si];
58  
59 296 ▶ my $datum = $_data_are_not_list ? $data : $data->[$si];
Branch: 1 branch
BranchTrueFalseTotal
$_data_are_not_list ? : 179 117 296
60  
61 296 ▶ if (!defined $datum) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $datum) 0 296 296
62 0 die "Undefined datum (SCT=“$sct”)!";
63 }
64  
65 # Arrays
66 296 ▶ if (index($sct, 'a') == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (index($sct, 'a') == 0) { } 12 284 296
elsif (index($sct, '(') == 0) { } 11 273 284
elsif ($sct eq 'v') { } 47 226 273
67 12 _marshal_array( $sct, $datum, $buf_sr);
68 }
69  
70 # Structs are given as arrays.
71 elsif (index($sct, '(') == 0) {
72 11 Protocol::DBus::Pack::align_str($$buf_sr, 8);
73  
74 11 my $struct_sig = substr($sig, 1, -1);
75  
76 11 _marshal( $struct_sig, $datum, $buf_sr );
77 }
78  
79 # Variants are given as two-member arrays.
80 elsif ($sct eq 'v') {
81 47 _marshal( g => $datum->[0], $buf_sr, 1 );
82 47 _marshal( $datum->[0], $datum->[1], $buf_sr, 1 );
83 }
84  
85 # Anything else is a basic type.
86 else {
87 226 ▶ if ($sct eq 'h') {
Branch: 3 branches
BranchTrueFalseTotal
if ($sct eq 'h') { } 3 223 226
elsif ($sct eq 'o') { } 11 212 223
elsif ($sct eq 's') { } 41 171 212
88 3 my $fd = fileno($datum);
89 3 ▶ die "fileno($datum) returned undef!" if !defined $fd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fd 0 3 3
90  
91 3 my ($idx) = grep { $_MARSHAL_FDS[$_] == $fd } 0 .. $#_MARSHAL_FDS;
92  
93 3 ▶ if (!defined $idx) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $idx) 2 1 3
94 2 $idx = @_MARSHAL_FDS;
95 2 push @_MARSHAL_FDS, $fd;
96 }
97  
98 3 $datum = $idx;
99 }
100 elsif ($sct eq 'o') {
101 11 ▶ $datum =~ m<\A/(?:[A-Za-z0-9_]+(?:/[A-Za-z0-9_]+)*)?\z> or do {
Branch: 1 branch
BranchTrueFalseTotal
$datum =~ m[\A/(?:[A-Za-z0-9_]+(?:/[A-Za-z0-9_]+)*)?\z] or do { die "Invalid object path: \342\200\234$datum\342\200\235" } 11 0 11
102 0 die "Invalid object path: “$datum”";
103 };
104 }
105 elsif ($sct eq 's') {
106 41 utf8::encode($datum);
107 }
108  
109 226 Protocol::DBus::Pack::align_str($$buf_sr, Protocol::DBus::Pack::ALIGNMENT()->{$sct});
110  
111 226 my ($pack) = _get_pack_template($sct);
112  
113 226 $pack = "($pack)$_ENDIAN_PACK";
114 226 $$buf_sr .= pack( $pack, $datum );
115 }
116 }
117  
118 231 return $buf_sr;
119 }
120  
121 12 ▶sub _marshal_array {
Subroutine _marshal_array: called CC=10 SCAR=23.0
122 12 my ($sct, $data, $buf_sr) = @_;
123  
124 12 Protocol::DBus::Pack::align_str($$buf_sr, 4);
125  
126 # We’ll fill this in with the length below.
127 12 $$buf_sr .= "\0\0\0\0";
128  
129 12 my $array_start = length $$buf_sr;
130  
131 # Per the spec, array lengths do NOT include alignment bytes
132 # after the length. This only affects 8-byte-aligned types.
133 12 my $compensate_align8;
134  
135 12 substr($sct, 0, 1, q<>); # chop off the leading “a”
136  
137 12 ▶ if ($array_start % 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($array_start % 8) 3 9 12
138 3 $compensate_align8 = (0 == index($sct, '('));
139 3 ▶ $compensate_align8 ||= (0 == index($sct, '{'));
Condition100%3/3
$compensate_align8 ||= 0 == index($sct, "{")
l!l&&r!l&&!r
111
MC/DC100%2/2
$compensate_align8 ||= 0 == index($sct, "{")
$compensate_align8 0 == index($sct, "{")
Truth table100%3/3
A ||= B
A= $compensate_align8B= 0 == index($sct, "{")
ABresult
1X1
011
000
140 3 ▶ $compensate_align8 ||= ((Protocol::DBus::Pack::ALIGNMENT()->{$sct} || 0) == 8);
Condition60%3/5
$compensate_align8 ||= ({"b" => 4,"d" => 8,"g" => 1,"h" => 4,"i" => 4,"n" => 2,"o" => 4,"q" => 2,"s" => 4,"t" => 8,"u" => 4,"x" => 8,"y" => 1}->{$sct} || 0) == 8
l!l&&r!l&&!r
210
{b => 4, d => 8, g => 1, h => 4, i => 4, n => 2, o => 4, q => 2, s => 4, t => 8, u => 4, x => 8, y => 1}->{$sct} || 0
l!l
10
MC/DC0%0/2
$compensate_align8 ||= ({"b" => 4,"d" => 8,"g" => 1,"h" => 4,"i" => 4,"n" => 2,"o" => 4,"q" => 2,"s" => 4,"t" => 8,"u" => 4,"x" => 8,"y" => 1}->{$sct} || 0) == 8
$compensate_align8 {b => 4, d => 8, g => 1, h => 4, i => 4, n => 2, o => 4, q => 2, s => 4, t => 8, u => 4, x => 8, y => 1}->{$sct}
Truth table66%2/3
A ||= B
A= $compensate_align8B= {b => 4, d => 8, g => 1, h => 4, i => 4, n => 2, o => 4, q => 2, s => 4, t => 8, u => 4, x => 8, y => 1}->{$sct}
ABresult
1X1
011
000
141 }
142  
143 # DICT_ENTRY arrays are given as plain Perl hashes
144 12 ▶ if (0 == index($sct, '{')) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == index($sct, '{')) { } 9 3 12
145 9 my $key_sig = substr($sct, 1, 1);
146 9 my $value_sig = substr($sct, 2, -1);
147  
148 9 ▶ for my $key ( $DICT_CANONICAL ? (sort keys %$data) : keys %$data ) {
Branch: 1 branch
BranchTrueFalseTotal
$Protocol::DBus::Marshal::DICT_CANONICAL ? : 2 7 9
149 38 Protocol::DBus::Pack::align_str($$buf_sr, 8);
150 38 _marshal($key_sig, $key, $buf_sr, 1);
151 38 _marshal( $value_sig, $data->{$key}, $buf_sr, 1);
152 }
153 }
154  
155 # Any other array is given as an array.
156 else {
157 3 for my $item ( @$data ) {
158 9 _marshal($sct, $item, $buf_sr, 1);
159 }
160 }
161  
162 12 my $array_len = length($$buf_sr) - $array_start;
163 12 ▶ $array_len -= 4 if $compensate_align8;
Branch: 1 branch
BranchTrueFalseTotal
if $compensate_align8 3 9 12
164  
165 12 substr( $$buf_sr, $array_start - 4, 4, pack("L$_ENDIAN_PACK", $array_len) );
166 }
167  
168 #----------------------------------------------------------------------
169  
170 78 ▶sub _unmarshal {
Subroutine _unmarshal: called CC=2 SCAR=6.9
171 78 my ($buf_sr, $buf_offset, $sig) = @_;
172  
173 78 my @items;
174  
175 78 my $buf_start = $buf_offset;
176 78 my $sig_offset = 0;
177  
178 78 ▶ while ($sig_offset < length($sig)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig_offset < length $sig) 278 78 356
179 278 my $next_sct_len = Protocol::DBus::Signature::get_sct_length($sig, $sig_offset);
180  
181 278 my ($item, $item_length) = _unmarshal_sct(
182 $buf_sr,
183 $buf_offset,
184 substr( $sig, $sig_offset, $next_sct_len ),
185 );
186  
187 278 push @items, $item;
188  
189 278 $buf_offset += $item_length;
190 278 $sig_offset += $next_sct_len;
191 }
192  
193 78 return (\@items, $buf_offset - $buf_start);
194 }
195  
196 0 ▶sub unmarshal_sct_le {
Subroutine unmarshal_sct_le: not called CC=1 SCAR=6.9
Pod undocumented
197 0 return _unmarshal_sct(@_);
198 }
199  
200 0 ▶sub unmarshal_sct_be {
Subroutine unmarshal_sct_be: not called CC=1 SCAR=6.9
Pod undocumented
201 0 return _unmarshal_sct(@_);
202 }
203  
204 # SCT = “single complete type”.
205 # Returns the value plus its marshaled length.
206 1872 ▶sub _unmarshal_sct {
Subroutine _unmarshal_sct: called CC=11 SCAR=24.4
207 1872 my ($buf_sr, $buf_offset, $sct_sig) = @_;
208  
209 1872 my $buf_start = $buf_offset;
210  
211 1872 ▶ if (substr($sct_sig, 0, 1) eq 'a') {
Branch: 3 branches
BranchTrueFalseTotal
if (substr($sct_sig, 0, 1) eq 'a') { } 116 1756 1872
elsif (substr($sct_sig, 0, 1) eq '(') { } 36 1720 1756
elsif (substr($sct_sig, 0, 1) eq 'v') { } 193 1527 1720
212 116 Protocol::DBus::Pack::align($buf_offset, 4);
213  
214 116 my $array_len = unpack "\@$buf_offset L$_ENDIAN_PACK", $$buf_sr;
215 116 $buf_offset += 4; #uint32 length
216  
217 116 my $obj;
218  
219 # We parse arrays of DICT_ENTRY into a hash.
220 116 ▶ if (substr($sct_sig, 1, 1) eq '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($sct_sig, 1, 1) eq '{') { } 52 64 116
221  
222 # The key is always a basic type, so just one letter.
223 52 my $key_type = substr($sct_sig, 2, 1);
224  
225 # The value can be any SCT.
226 52 my $value_type = substr( $sct_sig, 3, Protocol::DBus::Signature::get_sct_length($sct_sig, 3) );
227  
228 # Do this here rather than in
229 # _unmarshal_to_hashref() to avoid
230 # the creation of an intermediate length.
231 52 Protocol::DBus::Pack::align($buf_offset, 8);
232  
233 52 $obj = _unmarshal_to_hashref($buf_sr, $buf_offset, $array_len, $key_type, $value_type);
234 52 $buf_offset += $array_len;
235 }
236  
237 # Anything else we parse normally.
238 else {
239 64 my $array_sig = substr( $sct_sig, 1, Protocol::DBus::Signature::get_sct_length($sct_sig, 1) );
240  
241 64 my @array_items;
242 64 $obj = bless \@array_items, 'Protocol::DBus::Type::Array';
243  
244 # If the array contents are 8-byte-aligned, then the array will
245 # actually be 4 bytes longer than this. But it doesn’t appear we
246 # need to care about that since _unmarshal_sct() accounts for that.
247 64 my $array_end = $buf_offset + $array_len;
248  
249 64 ▶ while ($buf_offset < $array_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf_offset < $array_end) 838 64 902
250 838 my ($item, $item_length) = _unmarshal_sct($buf_sr, $buf_offset, $array_sig);
251  
252 838 $buf_offset += $item_length;
253  
254 838 push @array_items, $item;
255 }
256 }
257  
258 116 return ($obj, $buf_offset - $buf_start);
259 }
260 elsif (substr($sct_sig, 0, 1) eq '(') {
261 36 return _unmarshal_struct(@_);
262 }
263 elsif (substr($sct_sig, 0, 1) eq 'v') {
264 193 return _unmarshal_variant(@_);
265 }
266  
267 1527 my ($pack_tmpl, $is_string) = _get_pack_template($sct_sig);
268  
269 1527 Protocol::DBus::Pack::align($buf_offset, Protocol::DBus::Pack::ALIGNMENT()->{$sct_sig});
270  
271 1527 my $val = unpack("\@$buf_offset ($pack_tmpl)$_ENDIAN_PACK", $$buf_sr);
272  
273 1527 my $strlen;
274  
275 1527 ▶ if ($FILEHANDLES && $sct_sig eq 'h') {
Branch: 2 branches
BranchTrueFalseTotal
if ($Protocol::DBus::Marshal::FILEHANDLES and $sct_sig eq 'h') { } 0 1527 1527
elsif ($is_string) { } 428 1099 1527
Condition33%1/3
$Protocol::DBus::Marshal::FILEHANDLES and $sct_sig eq "h"
!ll&&!rl&&r
152700
MC/DC0%0/2
$Protocol::DBus::Marshal::FILEHANDLES and $sct_sig eq "h"
$Protocol::DBus::Marshal::FILEHANDLES $sct_sig eq "h"
Truth table33%1/3
A and B
A= $Protocol::DBus::Marshal::FILEHANDLESB= $sct_sig eq "h"
ABresult
0X0
100
111
276 0 ▶ $val = $FILEHANDLES->[$val] || do {
Condition0%0/3
$Protocol::DBus::Marshal::FILEHANDLES->[$val] || do { warn "UNIX_FD ($val) received that doesn\342\200\231t refer to a received file descriptor!\n"; $val }
l!l&&r!l&&!r
000
MC/DC0%0/2
$Protocol::DBus::Marshal::FILEHANDLES->[$val] || do { warn "UNIX_FD ($val) received that doesn\342\200\231t refer to a received file descriptor!\n"; $val }
$Protocol::DBus::Marshal::FILEHANDLES->[$val] do { warn "UNIX_FD ($val) received that doesn\342\200\231t refer to a received file descriptor!\n"; $val }
Truth table0%0/3
A || B
A= $Protocol::DBus::Marshal::FILEHANDLES->[$val]B= do { warn "UNIX_FD ($val) received that doesn\342\200\231t refer to a received file descriptor!\n"; $val }
ABresult
1X1
011
000
277 0 warn "UNIX_FD ($val) received that doesn’t refer to a received file descriptor!\n";
278 0 $val;
279 };
280 }
281 elsif ($is_string) {
282 428 $strlen = length $val;
283 428 utf8::decode($val);
284 }
285  
286 1527 ▶ return ($val, $buf_offset - $buf_start + Protocol::DBus::Pack::WIDTH()->{$sct_sig} + ($is_string ? $strlen : 0));
Branch: 1 branch
BranchTrueFalseTotal
$is_string ? : 428 1099 1527
287 }
288  
289 193 ▶sub _unmarshal_variant {
Subroutine _unmarshal_variant: called CC=3 SCAR=11.2
290 193 my ($buf_sr, $buf_offset) = @_;
291  
292 193 my $buf_start = $buf_offset;
293  
294 193 my ($sig, $len) = _unmarshal_sct( $buf_sr, $buf_offset, 'g' );
295  
296 193 ▶ die sprintf("No sig ($len bytes?) from “%s”?", substr($$buf_sr, $buf_offset)) if !length $sig;
Branch: 1 branch
BranchTrueFalseTotal
unless length $sig 0 193 193
297  
298 193 $buf_offset += $len;
299  
300 193 (my $val, $len) = _unmarshal_sct( $buf_sr, $buf_offset, $sig );
301  
302 return(
303 193 ▶ $PRESERVE_VARIANT_SIGNATURES ? bless( [ $sig => $val ], 'Protocol::DBus::Type::Variant' ) : $val,
Branch: 1 branch
BranchTrueFalseTotal
$Protocol::DBus::Marshal::PRESERVE_VARIANT_SIGNATURES ? : 0 193 193
304 $len + $buf_offset - $buf_start,
305 );
306 }
307  
308 1753 ▶sub _get_pack_template {
Subroutine _get_pack_template: called CC=3 SCAR=11.1
309 1753 my ($sct_sig) = @_;
310  
311 1753 my ($is_string, $pack_tmpl);
312 1753 ▶ if ( $pack_tmpl = Protocol::DBus::Pack::STRING()->{$sct_sig} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pack_tmpl = {g => 'C/a x', o => 'L/a x', s => 'L/a x'}->{$sct_sig}) { } 533 1220 1753
313 533 $is_string = 1;
314 }
315 else {
316 1220 ▶ $pack_tmpl = Protocol::DBus::Pack::NUMERIC()->{$sct_sig} or do {
Branch: 1 branch
BranchTrueFalseTotal
$pack_tmpl = {"b" => "L","d" => "d","h" => "L","i" => "l","n" => "s","q" => "S","t" => "Q","u" => "L","x" => "q","y" => "C"}->{$sct_sig} or do { die "No basic type template for type \342\200\234$sct_sig\342\200\235!" } 1220 0 1220
317 0 die "No basic type template for type “$sct_sig”!";
318 };
319  
320 1220 if (!Protocol::DBus::Pack::CAN_64()) {
321 if ($pack_tmpl eq 'q') {
322 $pack_tmpl = ( $_ENDIAN_PACK eq '>' ) ? 'x4 l' : 'l x4';
323 }
324 elsif ($pack_tmpl eq 'Q') {
325 $pack_tmpl = ( $_ENDIAN_PACK eq '>' ) ? 'x4 L' : 'L x4';
326 }
327 }
328 }
329  
330 1753 return ($pack_tmpl, $is_string);
331 }
332  
333 52 ▶sub _unmarshal_to_hashref {
Subroutine _unmarshal_to_hashref: called CC=2 SCAR=6.9
334 52 my ($buf_sr, $buf_offset, $array_len, $key_type, $value_type) = @_;
335  
336 52 my %items;
337 52 my $obj = bless \%items, 'Protocol::DBus::Type::Dict';
338  
339 # NB: We already align()ed this.
340  
341 52 my $end_offset = $buf_offset + $array_len;
342  
343 52 ▶ while ($buf_offset < $end_offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf_offset < $end_offset) 185 52 237
344 185 Protocol::DBus::Pack::align($buf_offset, 8);
345  
346 185 my ($key, $len_in_buf) = _unmarshal_sct($buf_sr, $buf_offset, $key_type);
347  
348 185 $buf_offset += $len_in_buf;
349  
350 185 (my $val, $len_in_buf) = _unmarshal_sct($buf_sr, $buf_offset, $value_type);
351  
352 185 $buf_offset += $len_in_buf;
353  
354 185 $items{$key} = $val;
355 }
356  
357 # We don’t need to return the length.
358 52 return $obj;
359 }
360  
361 36 ▶sub _unmarshal_struct {
Subroutine _unmarshal_struct: called CC=1 SCAR=0.0
362 36 my ($buf_sr, $buf_offset, $sct_sig) = @_;
363  
364 # Remove “()” and just parse as a series of types.
365 36 chop $sct_sig;
366 36 substr( $sct_sig, 0, 1, q<> );
367  
368 36 my $buf_start = $buf_offset;
369  
370 36 Protocol::DBus::Pack::align($buf_offset, 8);
371  
372 36 my ($items_ar, $len) = _unmarshal($buf_sr, $buf_offset, $sct_sig);
373 36 bless $items_ar, 'Protocol::DBus::Type::Struct';
374  
375 36 return ($items_ar, ($buf_offset - $buf_start) + $len);
376 }
377  
378 0 ▶sub _add_uint32_variant_length {
Subroutine _add_uint32_variant_length: not called CC=2 SCAR=17.9
379 0 my ($buf_sr, $buf_offset_sr) = @_;
380  
381 0 Protocol::DBus::Pack::align( $$buf_offset_sr, 4 );
382  
383 0 ▶ my $array_len = unpack(
Branch: 1 branch
BranchTrueFalseTotal
$Protocol::DBus::Marshal::_ENDIAN_PACK eq '<' ? : 0 0 0
384 "\@$$buf_offset_sr " . ($_ENDIAN_PACK eq '<' ? 'V' : 'N'),
385 $$buf_sr,
386 );
387  
388 0 $$buf_offset_sr += 4;
389 0 $$buf_offset_sr += $array_len;
390  
391 0 return;
392 }
393  
394 8 1;
« DBus/Client/AnyEvent.pm ↑ Summary DBus/Message.pm »