| 1 | # Decode.pm - objects to deserialize XDR strings | ||||||||||||||||||||||||||||
| 2 | # Copyright (C) 2000 Mountain View Data, Inc. | ||||||||||||||||||||||||||||
| 3 | # Written by Gordon Matzigkeit <gord@fig.org>, 2000-12-15 | ||||||||||||||||||||||||||||
| 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::Decode; | ||||||||||||||||||||||||||||
| 23 | # [guilt] | ||||||||||||||||||||||||||||
| 24 | # [maint | ||||||||||||||||||||||||||||
| 25 | # File: Decode.pm | ||||||||||||||||||||||||||||
| 26 | # Summary: objects to deserialize XDR strings | ||||||||||||||||||||||||||||
| 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-15 | ||||||||||||||||||||||||||||
| 33 | # License: GPL] | ||||||||||||||||||||||||||||
| 34 | # [clemency] | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Initialize a new decoding session. | ||||||||||||||||||||||||||||
| 41 | sub new | ||||||||||||||||||||||||||||
| 42 | 21 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 21 | my ($type, $buf) = @_; | |||||||||||||||||||||||||||
| 44 | 21 | bless { buffer => $buf, offset => 0 }, $type; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Append bytes to the decoding buffer. | ||||||||||||||||||||||||||||
| 49 | sub append | ||||||||||||||||||||||||||||
| 50 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
append: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | 1 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 52 | 1 | $self->{buffer} .= $data; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # Return the unconsumed buffer. | ||||||||||||||||||||||||||||
| 57 | sub buffer | ||||||||||||||||||||||||||||
| 58 | 17 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
buffer: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 17 | my ($self, $truncate) = @_; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 17 | my $ret = substr ($self->{buffer}, $self->{offset}); | |||||||||||||||||||||||||||
| 62 | 17 | ▶ | if ($truncate) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | { | ||||||||||||||||||||||||||||
| 64 | 16 | $self->{buffer} = ''; | |||||||||||||||||||||||||||
| 65 | 16 | $self->{offset} = 0; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 17 | return $ret; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Add an RPC record fragment to the buffer. Return the unused bytes, | ||||||||||||||||||||||||||||
| 72 | # if any. | ||||||||||||||||||||||||||||
| 73 | sub fragment | ||||||||||||||||||||||||||||
| 74 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
fragment: called CC=4 SCAR=15.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | 1 | my ($self, $rec) = @_; | |||||||||||||||||||||||||||
| 76 | 1 | my $dec = XDR::Decode->new ($rec); | |||||||||||||||||||||||||||
| 77 | 1 | my $len = $dec->unsigned; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1 | $self->append ($dec->inline ($len & ~(1 << 31))); | |||||||||||||||||||||||||||
| 80 | 1 | my $left = $dec->buffer; | |||||||||||||||||||||||||||
| 81 | 1 | my $leftlen = length $left; | |||||||||||||||||||||||||||
| 82 | 1 | ▶ | croak "$leftlen too many bytes in RPC record.\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$len >> 31 and $leftlen > 0
MC/DC0%0/2
$len >> 31 and $leftlen > 0
$len >> 31
$leftlen > 0
Truth table33%1/3
A and B
A= $len >> 31B= $leftlen > 0
| |||||||||||||||||||||||||||||
| 83 | if ($len >> 31 && $leftlen > 0); | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | ▶ | return $leftlen && $left; | ||||||||||||||||||||||||||
|
Condition33%1/3
$leftlen && $left
MC/DC0%0/2
$leftlen && $left
$leftlen
$left
Truth table33%1/3
A && B
A= $leftlenB= $left
| |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # Add a complete RPC record to the buffer. | ||||||||||||||||||||||||||||
| 90 | sub record | ||||||||||||||||||||||||||||
| 91 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
record: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 1 | my ($self, $rec) = @_; | |||||||||||||||||||||||||||
| 93 | 1 | my $remain; | |||||||||||||||||||||||||||
| 94 | do | ||||||||||||||||||||||||||||
| 95 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 1 | $remain = $self->fragment ($rec); | |||||||||||||||||||||||||||
| 97 | } while ($remain); | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Fetch N bytes from the buffer. | ||||||||||||||||||||||||||||
| 102 | sub inline | ||||||||||||||||||||||||||||
| 103 | 133 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
inline: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 104 | 133 | my ($self, $n) = @_; | |||||||||||||||||||||||||||
| 105 | 133 | my $left = length ($self->{buffer}) - $self->{offset}; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 133 | ▶ | croak "Need $n bytes, but only have $left remaining in XDR buffer.\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | if ($n > $left); | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # Take the slice they asked for. | ||||||||||||||||||||||||||||
| 111 | 133 | my $ret = substr ($self->{buffer}, $self->{offset}, $n); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # Advance the offset pointer. | ||||||||||||||||||||||||||||
| 114 | 133 | $self->{offset} += $n; | |||||||||||||||||||||||||||
| 115 | 133 | ▶ | if ($self->{offset} >= length $self->{buffer}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | { | ||||||||||||||||||||||||||||
| 117 | # Truncate the buffer to conserve memory. | ||||||||||||||||||||||||||||
| 118 | 12 | $self->{offset} = 0; | |||||||||||||||||||||||||||
| 119 | 12 | $self->{buffer} = ''; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 133 | return $ret; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # Decode nothing at all. | ||||||||||||||||||||||||||||
| 127 | sub void | ||||||||||||||||||||||||||||
| 128 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
void: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 129 | 1 | return ''; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # Decode an unsigned integer. | ||||||||||||||||||||||||||||
| 134 | sub unsigned | ||||||||||||||||||||||||||||
| 135 | 102 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
unsigned: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 136 | 102 | my $self = shift; | |||||||||||||||||||||||||||
| 137 | 102 | return unpack ('N', $self->inline (4)); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Decode a variable-length opaque. | ||||||||||||||||||||||||||||
| 142 | sub opaque | ||||||||||||||||||||||||||||
| 143 | 24 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
opaque: called CC=5 SCAR=16.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 144 | # opaque -> () | ||||||||||||||||||||||||||||
| 145 | # opaque[0] -> (0) | ||||||||||||||||||||||||||||
| 146 | # opaque[10] -> (10) | ||||||||||||||||||||||||||||
| 147 | # opaque<20> -> (0, 20) | ||||||||||||||||||||||||||||
| 148 | 24 | my ($self, $min, $max) = @_; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 24 | ▶ | $min = $self->unsigned () if (! defined $min); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 24 | ▶ | croak "Opaque length $min exceeds maximum $max." | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $max and $min > $max
MC/DC0%0/2
defined $max and $min > $max
defined $max
$min > $max
Truth table33%1/3
A and B
A= defined $maxB= $min > $max
| |||||||||||||||||||||||||||||
| 152 | if (defined $max && $min > $max); | ||||||||||||||||||||||||||||
| 153 | 24 | my $ret = $self->inline ($min); | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | # Strip the padded zeros. | ||||||||||||||||||||||||||||
| 156 | 24 | my $dribble = $min & 3; | |||||||||||||||||||||||||||
| 157 | 24 | ▶ | if ($dribble) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | { | ||||||||||||||||||||||||||||
| 159 | 5 | $self->inline (4 - $dribble); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 24 | return $ret; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | sub opaque_auth | ||||||||||||||||||||||||||||
| 167 | 17 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
opaque_auth: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 168 | 17 | my ($self) = @_; | |||||||||||||||||||||||||||
| 169 | 17 | return [ $self->unsigned, $self->opaque ]; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # Unpack an RPC buffer. | ||||||||||||||||||||||||||||
| 174 | 3 | ▶ | use XDR::RPC; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 175 | sub rpc | ||||||||||||||||||||||||||||
| 176 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
rpc: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 177 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 178 | 2 | return XDR::RPC->decode ($self); | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 3 | 1; | |||||||||||||||||||||||||||