/usr/share/perl5/PHP/Serialization.pm is in libphp-serialization-perl 0.34-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | package PHP::Serialization;
use strict;
use warnings;
use Exporter ();
use Scalar::Util qw/blessed/;
use Carp qw(croak confess carp);
use bytes;
use vars qw/$VERSION @ISA @EXPORT_OK/;
$VERSION = '0.34';
@ISA = qw(Exporter);
@EXPORT_OK = qw(unserialize serialize);
=head1 NAME
PHP::Serialization - simple flexible means of converting the output of PHP's serialize() into the equivalent Perl memory structure, and vice versa.
=head1 SYNOPSIS
use PHP::Serialization qw(serialize unserialize);
my $encoded = serialize({ a => 1, b => 2});
my $hashref = unserialize($encoded);
=cut
=head1 DESCRIPTION
Provides a simple, quick means of serializing perl memory structures (including object data!) into a format that PHP can deserialize() and access, and vice versa.
NOTE: Converts PHP arrays into Perl Arrays when the PHP array used exclusively numeric indexes, and into Perl Hashes then the PHP array did not.
=cut
sub new {
my ($class) = shift;
my $self = bless {}, blessed($class) ? blessed($class) : $class;
return $self;
}
=head1 FUNCTIONS
Exportable functions..
=cut
=head2 serialize($var,[optional $asString,[optional $sortHashes]])
Serializes the memory structure pointed to by $var, and returns a scalar value of encoded data.
If the optional $asString is true, $var will be encoded as string if it is double or float.
If the optional $sortHashes is true, all hashes will be sorted before serialization.
NOTE: Will recursively encode objects, hashes, arrays, etc.
SEE ALSO: ->encode()
=cut
sub serialize {
return __PACKAGE__->new->encode(@_);
}
=head2 unserialize($encoded,[optional CLASS])
Deserializes the encoded data in $encoded, and returns a value (be it a hashref, arrayref, scalar, etc)
representing the data structure serialized in $encoded_string.
If the optional CLASS is specified, any objects are blessed into CLASS::$serialized_class. Otherwise, O
bjects are blessed into PHP::Serialization::Object::$serialized_class. (which has no methods)
SEE ALSO: ->decode()
=cut
sub unserialize {
return __PACKAGE__->new->decode(@_);
}
=head1 METHODS
Functionality available if using the object interface..
=cut
=head2 decode($encoded_string,[optional CLASS])
Deserializes the encoded data in $encoded, and returns a value (be it a hashref, arrayref, scalar, etc)
representing the data structure serialized in $encoded_string.
If the optional CLASS is specified, any objects are blessed into CLASS::$serialized_class. Otherwise,
Objects are blessed into PHP::Serialization::Object::$serialized_class. (which has no methods)
SEE ALSO: unserialize()
=cut
my $sorthash;
sub decode {
my ($self, $string, $class, $shash) = @_;
$sorthash=$shash if defined($shash);
my $cursor = 0;
$self->{string} = \$string;
$self->{cursor} = \$cursor;
$self->{strlen} = length($string);
if ( defined $class ) {
$self->{class} = $class;
}
else {
$self->{class} = 'PHP::Serialization::Object';
}
# Ok, start parsing...
my @values = $self->_parse();
# Ok, we SHOULD only have one value..
if ( $#values == -1 ) {
# Oops, none...
return;
}
elsif ( $#values == 0 ) {
# Ok, return our one value..
return $values[0];
}
else {
# Ok, return a reference to the list.
return \@values;
}
} # End of decode sub.
my %type_table = (
O => 'object',
s => 'scalar',
a => 'array',
i => 'integer',
d => 'float',
b => 'boolean',
N => 'undef',
);
sub _parse_array {
my $self = shift;
my $elemcount = shift;
my $cursor = $self->{cursor};
my $string = $self->{string};
my $strlen = $self->{strlen};
confess("No cursor") unless $cursor;
confess("No string") unless $string;
confess("No strlen") unless $strlen;
my @elems = ();
my @shash_arr = ('some') if (($sorthash) and (ref($sorthash) eq 'HASH'));
$self->_skipchar('{');
foreach my $i (1..$elemcount*2) {
push(@elems,$self->_parse_elem);
if (($i % 2) and (@shash_arr)) {
$shash_arr[0]= ((($i-1)/2) eq $elems[$#elems])? 'array' : 'hash' unless ($shash_arr[0] eq 'hash');
push(@shash_arr,$elems[$#elems]);
}
}
$self->_skipchar('}');
push(@elems,\@shash_arr) if (@shash_arr);
return @elems;
}
sub _parse_elem {
my $self = shift;
my $cursor = $self->{cursor};
my $string = $self->{string};
my $strlen = $self->{strlen};
my @elems;
my $type_c = $self->_readchar();
my $type = $type_table{$type_c};
if (!defined $type) {
croak("ERROR: Unknown type $type_c.");
}
if ( $type eq 'object' ) {
$self->_skipchar(':');
# Ok, get our name count...
my $namelen = $self->_readnum();
$self->_skipchar(':');
# Ok, get our object name...
$self->_skipchar('"');
my $name = $self->_readstr($namelen);
$self->_skipchar('"');
# Ok, our sub elements...
$self->_skipchar(':');
my $elemcount = $self->_readnum();
$self->_skipchar(':');
my %value = $self->_parse_array($elemcount);
# TODO: Call wakeup
# TODO: Support for objecttypes
return bless(\%value, $self->{class} . '::' . $name);
} elsif ( $type eq 'array' ) {
$self->_skipchar(':');
# Ok, our sub elements...
my $elemcount = $self->_readnum();
$self->_skipchar(':');
my @values = $self->_parse_array($elemcount);
# If every other key is not numeric, map to a hash..
my $subtype = 'array';
my @newlist;
my @shash_arr=@{pop(@values)} if (ref($sorthash) eq 'HASH');
foreach ( 0..$#values ) {
if ( ($_ % 2) ) {
push(@newlist, $values[$_]);
next;
} elsif (($_ / 2) ne $values[$_]) {
$subtype = 'hash';
last;
}
if ( $values[$_] !~ /^\d+$/ ) {
$subtype = 'hash';
last;
}
}
if ( $subtype eq 'array' ) {
# Ok, remap...
return \@newlist;
} else {
# Ok, force into hash..
my %hash = @values;
${$sorthash}{\%hash}=@shash_arr if ((ref($sorthash) eq 'HASH') and @shash_arr and (shift(@shash_arr) ne 'array'));
return \%hash;
}
}
elsif ( $type eq 'scalar' ) {
$self->_skipchar(':');
# Ok, get our string size count...
my $strlen = $self->_readnum;
$self->_skipchar(':');
$self->_skipchar('"');
my $string = $self->_readstr($strlen);
$self->_skipchar('"');
$self->_skipchar(';');
return $string;
}
elsif ( $type eq 'integer' || $type eq 'float' ) {
$self->_skipchar(':');
# Ok, read the value..
my $val = $self->_readnum;
if ( $type eq 'integer' ) { $val = int($val); }
$self->_skipchar(';');
return $val;
}
elsif ( $type eq 'boolean' ) {
$self->_skipchar(':');
# Ok, read our boolen value..
my $bool = $self->_readchar;
$self->_skipchar;
if ($bool eq '0') {
$bool = undef;
}
return $bool;
}
elsif ( $type eq 'undef' ) {
$self->_skipchar(';');
return undef;
}
else {
confess "Unknown element type '$type' found! (cursor $$cursor)";
}
}
sub _parse {
my ($self) = @_;
my $cursor = $self->{cursor};
my $string = $self->{string};
my $strlen = $self->{strlen};
confess("No cursor") unless $cursor;
confess("No string") unless $string;
confess("No strlen") unless $strlen;
my @elems;
push(@elems,$self->_parse_elem);
# warn if we have unused chars
if ($$cursor != $strlen) {
carp("WARN: Unused characters in string after $$cursor.");
}
return @elems;
} # End of decode.
sub _readstr {
my ($self, $length) = @_;
my $string = $self->{string};
my $cursor = $self->{cursor};
if ($$cursor + $length > length($$string)) {
croak("ERROR: Read past end of string. Want $length after $$cursor. (".$$string.")");
}
my $str = substr($$string, $$cursor, $length);
$$cursor += $length;
return $str;
}
sub _readchar {
my ($self) = @_;
return $self->_readstr(1);
}
sub _readnum {
# Reads in a character at a time until we run out of numbers to read...
my ($self) = @_;
my $cursor = $self->{cursor};
my $string;
while ( 1 ) {
my $char = $self->_readchar;
if ( $char !~ /^[\d\.-]+$/ ) {
$$cursor--;
last;
}
$string .= $char;
} # End of while.
return $string;
} # End of readnum
sub _skipchar {
my $self = shift;
my $want = shift;
my $c = $self->_readchar();
if (($want)&&($c ne $want)) {
my $cursor = $self->{cursor};
my $str = $self->{string};
croak("ERROR: Wrong char $c, expected $want at position ".$$cursor." (".$$str.")");
}
print "_skipchar: WRONG char $c ($want)\n" if (($want)&&($c ne $want));
# ${$$self{cursor}}++;
} # Move our cursor one bytes ahead...
=head2 encode($reference,[optional $asString,[optional $sortHashes]])
Serializes the memory structure pointed to by $reference, and returns a scalar value of encoded data.
If the optional $asString is true, $reference will be encoded as string if it is double or float.
If the optional $sortHashes is true, all hashes will be sorted before serialization.
NOTE: Will recursively encode objects, hashes, arrays, etc.
SEE ALSO: serialize()
=cut
sub encode {
my ($self, $val, $iskey, $shash) = @_;
$iskey=0 unless defined $iskey;
$sorthash=$shash if defined $shash;
if ( ! defined $val ) {
return $self->_encode('null', $val);
}
elsif ( blessed $val ) {
return $self->_encode('obj', $val);
}
elsif ( ! ref($val) ) {
if ( $val =~ /^-?(?:[0-9]|[1-9]\d{1,10})$/ && abs($val) < 2**31 ) {
return $self->_encode('int', $val);
}
elsif ( $val =~ /^-?\d+\.\d*$/ && !$iskey) {
return $self->_encode('float', $val);
}
else {
return $self->_encode('string', $val);
}
}
else {
my $type = ref($val);
if ($type eq 'HASH' || $type eq 'ARRAY' ) {
return $self->_sort_hash_encode($val) if (($sorthash) and ($type eq 'HASH'));
return $self->_encode('array', $val);
}
else {
confess "I can't serialize data of type '$type'!";
}
}
}
sub _sort_hash_encode {
my ($self, $val) = @_;
my $buffer = '';
my @hsort = ((ref($sorthash) eq 'HASH') and (ref(${$sorthash}{$val}) eq 'ARRAY')) ? ${$sorthash}{$val} : sort keys %{$val};
$buffer .= sprintf('a:%d:',scalar(@hsort)) . '{';
for (@hsort) {
$buffer .= $self->encode($_,1);
$buffer .= $self->encode($$val{$_});
}
$buffer .= '}';
return $buffer;
}
sub _encode {
my ($self, $type, $val) = @_;
my $buffer = '';
if ( $type eq 'null' ) {
$buffer .= 'N;';
}
elsif ( $type eq 'int' ) {
$buffer .= sprintf('i:%d;', $val);
}
elsif ( $type eq 'float' ) {
$buffer .= sprintf('d:%s;', $val);
}
elsif ( $type eq 'string' ) {
$buffer .= sprintf('s:%d:"%s";', length($val), $val);
}
elsif ( $type eq 'array' ) {
if ( ref($val) eq 'ARRAY' ) {
$buffer .= sprintf('a:%d:',($#{$val}+1)) . '{';
map { # Ewww
$buffer .= $self->encode($_);
$buffer .= $self->encode($$val[$_]);
} 0..$#{$val};
$buffer .= '}';
}
else {
$buffer .= sprintf('a:%d:',scalar(keys(%{$val}))) . '{';
while ( my ($key, $value) = each(%{$val}) ) {
$buffer .= $self->encode($key,1);
$buffer .= $self->encode($value);
}
$buffer .= '}';
}
}
elsif ( $type eq 'obj' ) {
my $class = ref($val);
$class =~ /(\w+)$/;
my $subclass = $1;
$buffer .= sprintf('O:%d:"%s":%d:', length($subclass), $subclass, scalar(keys %{$val})) . '{';
foreach ( %{$val} ) {
$buffer .= $self->encode($_);
}
$buffer .= '}';
}
else {
confess "Unknown encode type!";
}
return $buffer;
}
=head1 TODO
Support diffrent object types
=head1 AUTHOR INFORMATION
Copyright (c) 2003 Jesse Brown <jbrown@cpan.org>. All rights reserved. This program is free software;
you can redistribute it and/or modify it under the same terms as Perl itself.
Various patches contributed by assorted authors on rt.cpan.org (as detailed in Changes file).
Currently maintained by Tomas Doran <bobtfish@bobtfish.net>.
Rewritten to solve all known bugs by Bjørn-Olav Strand <bolav@cpan.org>
=cut
package PHP::Serialization::Object;
1;
|