XDR/Encode.pm

filter: Statementstmt 98.0% 50 / 51 Branchbran 71.4% 10 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 8 totaltotal 83.9% 73 / 87 CC 8 SCAR 6.0
CC
8
Cov
92%
reply_packet
3.0
packet
2.0
unsigned
2.0
CRAP 1.8
« XDR/Decode.pm ↑ Summary XDR/RPC/Call.pm »
1 # Encode.pm - build XDR strings from Perl objects
2 # Copyright (C) 2000 Mountain View Data, Inc.
3 # Written by Gordon Matzigkeit <gord@fig.org>, 2000-12-14
4 #
5 # This file is part of Perl XDR.
6 #
7 # Perl XDR is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # Perl XDR is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 # USA
21  
22 package XDR::Encode;
23 # [guilt]
24 # [maint
25 # File: Encode.pm
26 # Summary: build XDR strings from Perl objects
27 # Package: Perl XDR
28 # Owner: Mountain View Data, Inc.
29 # Years: 2000
30 # Author: Gordon Matzigkeit
31 # Contact: <gord@fig.org>
32 # Date: 2000-12-14
33 # License: GPL]
34 # [clemency]
35  
36  
37 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 BEGIN
41 3 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 ▶ use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 @ISA = qw(Exporter);
45 3 $EXPORT_TAGS{packet} = [qw(&call_packet &reply_packet &packet)];
46 3 $EXPORT_TAGS{simple} = [qw(&opaque &unsigned &void)];
47 3 $EXPORT_TAGS{all} = [@{$EXPORT_TAGS{packet}}, @{$EXPORT_TAGS{simple}},
48 '&record', '&opaque_auth'];
49 3 Exporter::export_ok_tags ('all');
50 }
51  
52 3 ▶use XDR qw(:msg_type :auth_flavor RPCVERS AUTH_NULL MSG_ACCEPTED);
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54  
55 # Encode a generic packet.
56 3 my $global_xid = 0;
57 sub packet
58 13 ▶{
Subroutine packet: called CC=2 SCAR=7.1
Pod undocumented
59 13 my ($msg_type, $contents, $xid) = @_;
60 13 ▶ $xid = $global_xid ++ if (! defined $xid);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xid 0 13 13
61 13 return unsigned ($xid) . unsigned ($msg_type) . $contents;
62 }
63  
64  
65 # Return an RPC record.
66 sub record
67 1 ▶{
Subroutine record: called CC=1 SCAR=0.0
Pod undocumented
68 1 my ($data) = @_;
69 1 my ($len) = length ($data) | (1 << 31);
70 1 return unsigned ($len) . $data;
71 }
72  
73  
74 # Encode an unsigned integer.
75 sub unsigned
76 114 ▶{
Subroutine unsigned: called CC=2 SCAR=7.1
Pod undocumented
77 114 my ($data) = @_;
78 114 ▶ confess "Non-numeric data for pack" if ($data !~ /^\d+$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $data =~ /^\d+$/ 0 114 114
79 114 pack ('N', $data);
80 }
81  
82  
83 sub opaque_auth
84 18 ▶{
Subroutine opaque_auth: called CC=2 SCAR=7.1
Pod undocumented
85 18 my ($flavor, $body) = @_;
86 18 ▶ $body = '' if (! defined $body);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 18 0 18
87 18 unsigned ($flavor) . opaque ($body);
88 }
89  
90  
91 sub opaque
92 27 ▶{
Subroutine opaque: called CC=2 SCAR=6.9
Pod undocumented
93 27 my ($data) = @_;
94 27 my ($len) = length ($data);
95  
96 # Align to int boundaries.
97 27 my $dribble = $len & 3;
98 27 ▶ if ($dribble)
Branch: 1 branch
BranchTrueFalseTotal
if ($dribble) 7 20 27
99 {
100 7 $data .= "\0" x (4 - $dribble);
101 }
102  
103 27 return unsigned ($len) . $data;
104 }
105  
106  
107 sub void
108 0 ▶{
Subroutine void: not called CC=1 SCAR=6.9
Pod undocumented
109 # This isn't undef so we don't get warnings.
110 0 return '';
111 }
112  
113  
114 # Construct a call packet.
115 sub call_packet
116 5 ▶{
Subroutine call_packet: called CC=2 SCAR=6.9
Pod undocumented
117 5 my ($xid, $proc, $args, $vers, $prog, $rpcvers) = @_;
118  
119 5 ▶ $rpcvers = RPCVERS if (! defined $rpcvers);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rpcvers 4 1 5
120 5 return packet (CALL,
121 unsigned ($rpcvers) . # rpcvers
122 unsigned ($prog) . # prog
123 unsigned ($vers) . # vers
124 unsigned ($proc) . # proc
125 opaque_auth (AUTH_NULL) . # cred
126 opaque_auth (AUTH_NULL) . # verf
127 $args,
128 $xid);
129 }
130  
131  
132 # Construct a reply packet.
133 sub reply_packet
134 8 ▶{
Subroutine reply_packet: called CC=3 SCAR=11.0
Pod undocumented
135 8 my ($xid, $status, $reason, $args) = @_;
136
137 8 my ($verf);
138 8 ▶ $args = '' if (! defined $args);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 5 3 8
139 8 ▶ $verf = opaque_auth (AUTH_NULL) if ($status == MSG_ACCEPTED);
Branch: 1 branch
BranchTrueFalseTotal
if $status == 0 8 0 8
140 8 return packet (REPLY,
141 $verf .
142 unsigned ($status) .
143 unsigned ($reason) .
144 $args,
145 $xid);
146 }
147  
148  
149 3 1;
« XDR/Decode.pm ↑ Summary XDR/RPC/Call.pm »