note 125263 deleted from function.serialize by cmb
| From: | cmb@php.net | Date: | Wed, 20 Jan 2021 08:58:20 +0000 |
| Subject: | note 125263 deleted from function.serialize by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-216201@lists.php.net to get a copy of this message | ||
Note Submitter: Rohan
----
I have also written some code for importing serialized PHP data into PERL and then writing it back
into PHP. I think the similar library posted above is actually more robust for a few select cases,
but mine is more compact and a little easier to follow. I'd really like comments if anyone
finds this useful or has improvements. Please credit me if you use my code.
sub unserialize {
my($raw) = $_[0];
my(@rawList, $type, $len, $body);
# Split raw data into fields
@rawList = split(/:/, $raw, 3);
# Get datatype field
$type = $rawList[0];
# Basically a switch operation on datatype possibilities
if ($type eq 'N;') {
# ... NULL datatype
return 0;
} elsif ($type eq 'i' or $type eq 'd' or $type eq 'b') {
# ... Integer, Float and Boolean datatypes
$body = $rawList[1];
chomp($body);
return substr($body, 0, -1);
} elsif ($type eq 's') {
# ... String datatype
$len = $rawList[1];
$body = $rawList[2];
chomp($body);
return substr($body, 1, $len);
} elsif ($type eq 'a') {
# ... Use a recursive solution for Arrays
my ($keyMatch, $valMatch, %assoc);
$keyMatch = 'i:\d+;|s:\d+:".*?";';
$valMatch = 'N;|b:[01];|i:\d+;|d:\d+.\d+;|s:\d+:".*?";|a:\d+:{.*?}';
$len = $rawList[1];
$body = $rawList[2];
chomp($body);
$body = substr($body, 1, -1);
while ($body =~ /^($keyMatch)($valMatch)($keyMatch|$)(.*)/ogs) {
$assoc{unserialize($1)} = unserialize($2);
$body = $3 . $4;
}
# Return reference to hash; allows multi-layer arrays
return \%assoc;
} elsif ($type eq 'O') {
# ... Use a recursive solution for Objects
my ($obj, @objList, $className, $objLen, $objBody, $objAssoc);
my %object;
$obj = $rawList[2];
@objList = split(/:/, $obj, 3);
$className = substr($objList[0], 1, -1);
$objLen = $objList[1];
$objBody = $objList[2];
# A little hacky; plunders the Array unserialize logic.
$objAssoc = unserialize("a:$objLen:$objBody");
# We must distinguish Objects from Arrays in the internal PERL
# representation. We do this by using an undef hash key
# 'OBJECT'. No PHP array should return an undef hash key.
# Therefore the test exists($hash{'OBJECT'}) combined with
# not(defined($hash{'OBJECT'})) should work to determine if
# the hash returned is an array or an object.
$object{'OBJECT'} = undef;
$object{'name'} = $className;
$object{'len'} = $objLen;
$object{'assoc'} = $objAssoc;
return \%object;
}
}
# serialize()
# Takes a PERL scalar or properly formed (i.e. generated by unserialize() or
# organized accordingly) referenced hash and converts it into a PHP format
# serialized data string. Uses recursive algorithms for arrays and objects.
sub serialize {
my($input) = $_[0];
if ($input =~ /^\d+$/) {
# ... Integer datatype
return "i:$input;";
} elsif ($input =~ /^\d+\.\d+$/) {
# ... Float datatype
return "d:$input;";
} elsif (ref($input) eq 'HASH') {
# ... Object and Array datatypes.
# See serialize() above for explanation of the test used here to
# distinguish Object from Array.
if (exists(${$input}{'OBJECT'}) and
not defined(${$input}{'OBJECT'})) {
# ... Use a recursive solution for Objects
my($className, $objLen, $objAssoc);
$className = ${$input}{'name'};
$objLen = ${$input}{'len'};
# A little hacky; plunders the Array serialize logic
$objAssoc = substr(serialize(${$input}{'assoc'}), 4);
return "O:3:\"$className\":$objLen:$objAssoc";
} else {
# ... Use a recursive solution for Arrays
my($key, $value, $serKey, $serVal, $hashLen);
my($serHash) = '';
while (($key, $value) = each(%{$input})) {
$serKey = serialize($key);
$serVal = serialize($value);
$serHash = $serHash . $serKey . $serVal;
}
$hashLen = scalar(keys(%{$input}));
return "a:$hashLen:{$serHash}";
}
} else {
# ... String datatype
my $len = length($input);
return "s:$len:\"$input\";";
}
}
If you have any problem then you can contact me by going here:
https://www.darkhackerworld.com/p/contact-me.html