DBus/Parser.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 14 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 147 SCAR 60.4
CC
20
Cov
0%
get_message
380.0
preserve_variant_signatures
6.0
new
2.0
CRAP 420.0
1 package Protocol::DBus::Parser;
2  
3 use strict;
4 use warnings;
5  
6 use Socket::MsgHdr ();
7  
8 use Protocol::DBus::Marshal ();
9 use Protocol::DBus::Message ();
10  
11 use constant _CHUNK_SIZE => 65536;
12  
13 use constant SINGLE_UNIX_FD_CMSGHDR => (0, 0, pack 'I!');
14  
15 use constant _LE_INIT_UNPACK => 'x4 V x4 V';
16 use constant _BE_INIT_UNPACK => 'x4 N x4 N';
17  
18 sub new {
19 my ($class, $socket) = @_;
20  
21 return bless { _s => $socket, _buf => q<> }, $class;
22 }
23  
24 sub get_message {
25 my ($self) = @_;
26  
27 my $msg;
28  
29 if (!$self->{'_bodysz'}) {
30 if (defined recv( $self->{'_s'}, my $peek, 16, Socket::MSG_PEEK() )) {
31 if ( 16 == length $peek ) {
32 @{$self}{'_bodysz', '_hdrsz'} = unpack(
33 (0 == index($peek, 'B')) ? _BE_INIT_UNPACK() : _LE_INIT_UNPACK(),
34 $peek,
35 );
36  
37 Protocol::DBus::Pack::align( $self->{'_hdrsz'}, 8 );
38  
39 $self->{'_msgsz'} = 16 + $self->{'_hdrsz'} + $self->{'_bodysz'};
40 }
41 }
42 elsif (!$!{'EAGAIN'} && !$!{'EWOULDBLOCK'}) {
43 die "recv(): $!";
44 }
45 }
46  
47 if (defined $self->{'_bodysz'} && !defined $self->{'_unix_fds'}) {
48 if (defined recv( $self->{'_s'}, my $full_hdr, 16 + $self->{'_hdrsz'}, Socket::MSG_PEEK() )) {
49 if ( length($full_hdr) == 16 + $self->{'_hdrsz'} ) {
50 my ($hdr) = Protocol::DBus::Message::Header::parse_simple(\$full_hdr);
51  
52 $self->{'_unix_fds'} = $hdr->[6]{ Protocol::DBus::Message::Header::FIELD()->{'UNIX_FDS'} } || 0;
53  
54 $self->{'_pending_unix_fds'} = $self->{'_unix_fds'};
55  
56 my $body_sig = $hdr->[6]{ Protocol::DBus::Message::Header::FIELD()->{'SIGNATURE'} };
57  
58 if ($hdr->[4]) {
59 die "No SIGNATURE header field!" if !defined $body_sig;
60 }
61 }
62 }
63 elsif (!$!{'EAGAIN'} && !$!{'EWOULDBLOCK'}) {
64 die "recv(): $!";
65 }
66 }
67  
68 if (defined $self->{'_unix_fds'}) {
69  
70 my $msg_buflen = $self->{'_msgsz'} - length $self->{'_buf'};
71 my $msg = Socket::MsgHdr->new(
72 buflen => $msg_buflen,
73 );
74  
75 # The unix FDs might arrive in a single control
76 # message, as individual control messages, or as
77 # some combination thereof. There is no way to know.
78 # So plan for the worst, and assume each unix FD is
79 # in its own control.
80 $msg->cmsghdr( (SINGLE_UNIX_FD_CMSGHDR()) x $self->{'_pending_unix_fds'} );
81  
82 my $got = Socket::MsgHdr::recvmsg( $self->{'_s'}, $msg );
83 if (defined $got) {
84  
85 if ($self->{'_pending_unix_fds'}) {
86 require Protocol::DBus::Parser::UnixFDs;
87 push @{ $self->{'_filehandles'} }, Protocol::DBus::Parser::UnixFDs::extract_from_msghdr($msg);
88 $self->{'_pending_unix_fds'} = $self->{'_unix_fds'} - @{ $self->{'_filehandles'} };
89 }
90  
91 $self->{'_buf'} .= $msg->buf();
92  
93 if ($got >= $msg_buflen) {
94 local $Protocol::DBus::Marshal::PRESERVE_VARIANT_SIGNATURES = 1 if $self->{'_preserve_variant_signatures'};
95  
96 # This clears out the buffer .. it should??
97 my $msg = Protocol::DBus::Message->parse( \$self->{'_buf'}, delete $self->{'_filehandles'} );
98  
99 die "Not enough bytes??" if !$msg;
100  
101 delete @{$self}{'_bodysz', '_unix_fds'};
102  
103 return $msg;
104 }
105 }
106 elsif (!$!{'EAGAIN'} && !$!{'EWOULDBLOCK'}) {
107 die "recv(): $!";
108 }
109 }
110  
111 return undef;
112 }
113  
114 sub preserve_variant_signatures {
115 my $self = shift;
116  
117 if (@_) {
118 $self->{'_preserve_variant_signatures'} = !!$_[0];
119 }
120  
121 return !!$self->{'_preserve_variant_signatures'};
122 }
123  
124 1;