File Coverage

blib/lib/XDR/Encode.pm
Criterion Covered Total %
statement 48 49 97.9
branch 10 14 71.4
condition n/a
subroutine 13 14 92.8
pod 0 8 0.0
total 71 85 83.5


line stmt bran cond sub pod time code
1             # Encode.pm - build XDR strings from Perl objects
2             # Copyright (C) 2000 Mountain View Data, Inc.
3             # Written by Gordon Matzigkeit , 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:
32             # Date: 2000-12-14
33             # License: GPL]
34             # [clemency]
35              
36              
37 3     3   1890 use strict;
  3         4  
  3         102  
38 3     3   17 use Carp;
  3         4  
  3         246  
39              
40             BEGIN
41             {
42 3     3   23 use Exporter ();
  3         9  
  3         71  
43 3     3   14 use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS);
  3         4  
  3         546  
44 3     3   1072 @ISA = qw(Exporter);
45 3         20 $EXPORT_TAGS{packet} = [qw(&call_packet &reply_packet &packet)];
46 3         13 $EXPORT_TAGS{simple} = [qw(&opaque &unsigned &void)];
47 3         5 $EXPORT_TAGS{all} = [@{$EXPORT_TAGS{packet}}, @{$EXPORT_TAGS{simple}},
  3         9  
  3         27  
48             '&record', '&opaque_auth'];
49 3         382 Exporter::export_ok_tags ('all');
50             }
51              
52 3     3   1719 use XDR qw(:msg_type :auth_flavor RPCVERS AUTH_NULL MSG_ACCEPTED);
  3         7  
  3         2514  
53              
54              
55             # Encode a generic packet.
56             my $global_xid = 0;
57             sub packet
58             {
59 13     13 0 19 my ($msg_type, $contents, $xid) = @_;
60 13 50       36 $xid = $global_xid ++ if (! defined $xid);
61 13         21 return unsigned ($xid) . unsigned ($msg_type) . $contents;
62             }
63              
64              
65             # Return an RPC record.
66             sub record
67             {
68 1     1 0 38 my ($data) = @_;
69 1         5 my ($len) = length ($data) | (1 << 31);
70 1         7 return unsigned ($len) . $data;
71             }
72              
73              
74             # Encode an unsigned integer.
75             sub unsigned
76             {
77 114     114 0 137 my ($data) = @_;
78 114 50       411 confess "Non-numeric data for pack" if ($data !~ /^\d+$/);
79 114         578 pack ('N', $data);
80             }
81              
82              
83             sub opaque_auth
84             {
85 18     18 0 448 my ($flavor, $body) = @_;
86 18 50       51 $body = '' if (! defined $body);
87 18         32 unsigned ($flavor) . opaque ($body);
88             }
89              
90              
91             sub opaque
92             {
93 27     27 0 167 my ($data) = @_;
94 27         38 my ($len) = length ($data);
95              
96             # Align to int boundaries.
97 27         38 my $dribble = $len & 3;
98 27 100       66 if ($dribble)
99             {
100 7         66 $data .= "\0" x (4 - $dribble);
101             }
102              
103 27         50 return unsigned ($len) . $data;
104             }
105              
106              
107             sub void
108             {
109             # This isn't undef so we don't get warnings.
110 0     0 0 0 return '';
111             }
112              
113              
114             # Construct a call packet.
115             sub call_packet
116             {
117 5     5 0 19 my ($xid, $proc, $args, $vers, $prog, $rpcvers) = @_;
118              
119 5 100       19 $rpcvers = RPCVERS if (! defined $rpcvers);
120 5         21 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             {
135 8     8 0 17 my ($xid, $status, $reason, $args) = @_;
136            
137 8         9 my ($verf);
138 8 100       25 $args = '' if (! defined $args);
139 8 50       25 $verf = opaque_auth (AUTH_NULL) if ($status == MSG_ACCEPTED);
140 8         20 return packet (REPLY,
141             $verf .
142             unsigned ($status) .
143             unsigned ($reason) .
144             $args,
145             $xid);
146             }
147              
148              
149             1;