1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
package MSDP;
use strict;
use warnings;
use Carp;
use constant {
MSDP_VAR => chr(1),
MSDP_VAL => chr(2),
MSDP_TABLE_OPEN => chr(3),
MSDP_TABLE_CLOSE => chr(4),
MSDP_ARRAY_OPEN => chr(5),
MSDP_ARRAY_CLOSE => chr(6),
EOF => chr(0),
};
#######################################################################
# Public functions #
#######################################################################
sub new {
my $self = shift;
my $class = ref($self) || $self;
my $data = shift;
return bless { }, $class;
}
sub decode {
my $self = shift;
my ($data) = (@_);
$self->{'data'} = $data;
$self->{'current_pos'} = 0;
my %variables;
my $next_tok = $self->_get_next_token_or_eof();
while ($next_tok ne EOF) {
$self->_put_back($next_tok);
my ($name, $value) = $self->_get_var();
$variables{$name} = $value;
$next_tok = $self->_get_next_token_or_eof();
}
return \%variables;
}
sub encode {
my $self = shift;
my ($data) = (@_);
if (ref($data) ne 'HASH') {
Carp::croak("Invalid data type, expecting hash reference");
}
my $encoded = "";
while (my ($key, $value) = each(%$data)) {
$encoded .= _encode_var($key, $value);
}
return $encoded;
}
sub encode_command {
my $self = shift;
if (scalar @_ < 1) {
Carp::croak("Command not specified");
}
my $command = shift;
my $encoded = MSDP_VAR . $command;
foreach my $arg (@_) {
$encoded .= MSDP_VAL . _encode_value($arg);
}
return $encoded;
}
#######################################################################
# Private functions #
#######################################################################
#######################################################################
# Decoding #
#######################################################################
sub _get_var {
my $self = shift;
$self->_expect(MSDP_VAR);
my $name = $self->_get_textual_token();
$self->_expect(MSDP_VAL);
my $value = $self->_get_value();
# Handle the case of a multi-valued variable (which becomes an array)
# eg: MSDP_VAR variable MSDP_VAL value1 MSDP_VAL value2
my $token = $self->_get_next_token_or_eof();
while ($token eq MSDP_VAL) {
unless (ref($value)) {
$value = [ $value ];
}
my $next_value = $self->_get_value();
push(@$value, $next_value);
$token = $self->_get_next_token_or_eof();
}
$self->_put_back($token);
return ($name, $value);
}
sub _get_value {
my $self = shift;
my $token = $self->_get_next_token();
if ($token eq MSDP_ARRAY_OPEN) {
return $self->_get_array()
} elsif ($token eq MSDP_TABLE_OPEN) {
return $self->_get_table();
}
return $token;
}
sub _get_array {
my $self = shift;
my @values;
while (1) {
my $token = $self->_get_next_token();
if ($token eq MSDP_ARRAY_CLOSE) {
return \@values;
} elsif ($token eq MSDP_VAL) {
push (@values, $self->_get_value());
} else {
# FIXME: Better reporting
Carp::croak("Got unexpected token, expecting MSDP_VAL or MSDP_ARRAY_CLOSE");
}
}
}
sub _get_table {
my $self = shift;
my %values;
while (1) {
my $token = $self->_get_next_token();
if ($token eq MSDP_TABLE_CLOSE) {
return \%values;
} elsif ($token eq MSDP_VAR) {
$self->_put_back($token);
my ($name, $value) = $self->_get_var();
$values{$name} = $value;
} else {
# FIXME: Better reporting
Carp::croak("Got unexpected token, expecting MSDP_VAR or MSDP_TABLE_CLOSE");
}
}
}
sub _expect {
my $self = shift;
my ($expected_token) = @_;
if ($self->_get_next_token_or_eof() ne $expected_token) {
# FIXME: Describe better the problem: what we got, and what should
# have been found
Carp::croak("Got unexpected token");
}
}
sub _get_textual_token {
my $self = shift;
my $token = $self->_get_next_token();
if (_is_special_char($token)) {
# FIXME: Better reporting
Carp::croak("Expecting a string, got a control token");
}
return $token;
}
sub _get_next_token {
my $self = shift;
my $token = $self->_get_next_token_or_eof();
if ($token eq EOF) {
Carp::croak("Unexpected end of string");
}
return $token;
}
sub _put_back {
my $self = shift;
my ($token) = @_;
$self->{'next_token'} = $token;
}
sub _get_next_token_or_eof {
my $self = shift;
if (defined $self->{'next_token'}) {
my $token = $self->{'next_token'};
$self->{'next_token'} = undef;
return $token;
}
my $char = $self->_get_next_char();
if ($char eq EOF) {
return EOF;
}
if (_is_special_char($char)) {
return $char;
}
my $name = $char;
while (1) {
$char = $self->_get_next_char();
if ($char eq EOF) {
return $name;
}
if (_is_special_char($char)) {
--$self->{'current_pos'};
return $name;
}
$name .= $char;
}
}
sub _get_next_char {
my $self = shift;
if ($self->{'current_pos'} == length($self->{'data'})) {
return EOF;
}
my $char = substr($self->{'data'}, $self->{'current_pos'}++, 1);
return $char;
}
sub _is_special_char {
my ($char) = @_;
return (($char ge MSDP_VAR && $char le MSDP_ARRAY_CLOSE)
|| $char eq EOF);
}
#######################################################################
# Encoding #
#######################################################################
sub _encode_var {
my ($key, $value) = @_;
return MSDP_VAR . $key . MSDP_VAL . _encode_value($value);
}
sub _encode_value {
my ($value) = @_;
my $type = ref($value);
if ($type eq '') {
return $value;
} elsif ($type eq 'SCALAR') {
return $$value;
} elsif ($type eq 'ARRAY') {
return _encode_array($value);
} elsif ($type eq 'HASH') {
return _encode_table($value);
} else {
Carp::croak("Unsupported type $type");
}
}
sub _encode_array {
my ($array) = @_;
my $encoded = MSDP_ARRAY_OPEN;
foreach my $element (@$array) {
$encoded .= MSDP_VAL . _encode_value($element);
}
$encoded .= MSDP_ARRAY_CLOSE;
return $encoded;
}
sub _encode_table {
my ($hash) = @_;
my $encoded = MSDP_TABLE_OPEN;
while (my ($key, $value) = each(%$hash)) {
$encoded .= MSDP_VAR . $key . MSDP_VAL . _encode_value($value);
}
$encoded .= MSDP_TABLE_CLOSE;
return $encoded;
}
1;
|