![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
King of Portal
|
Messing with Serialization
For a more detailed reason on why I'm doing this read here.
The idea behind this is that when PHP serializes an array it generates a string that contains all the information to recreate the array. However, I would like to be able to separate the array keys (keys that are exclusively strings) from the serialized string itself. So that I would have the keys in one string and the values of the array in another. I would also like to be able to recombine the two strings into the original serialized string. So here's the code I wrote up to do this: class gbSerializer {
var $serialString = '';
var $keys = '';
var $values = '';
function gbSerializer($keys, $values = null){
if(!isset($values)){
$this->serialString = $keys;
$count = 0;
do{
$lengthMarker = 0;
switch(substr($keys, 0, 1)){
case 'a':
$lengthMarker = strpos($keys, '{') + 1;
break;
case 's':
$lengthMarker = intval(substr($keys, 2, strpos($keys, ':', 2)));
$lengthMarker += strpos($keys, '"') + 3;
break;
case 'i':
case 'N':
case 'b':
$lengthMarker = strpos($keys, ';') + 1;
break;
case '}':
$lengthMarker++;
break;
}
$temp = substr($keys, 0, $lengthMarker);
$keys = substr($keys, $lengthMarker);
$iniChar = substr($temp, 0, 1);
switch($iniChar){
case 'a':
$count = 0;
$this->values .= $temp;
$this->keys .= '{';
break;
case '}':
$count = 0;
$this->values .= $temp;
$this->keys .= '}';
break;
default:
switch($count % 2){
case 0:
if(!strcmp($iniChar, 'i')){
$this->values .= $temp;
}else{
$this->keys .= $temp;
}
break;
case 1:
$this->values .= $temp;
break;
}
$count++;
break;
}
}while(strlen($keys) > 0);
}else{
$this->keys = $keys;
$this->values = $values;
do{
$lengthMarker = 0;
switch(substr($keys, 0, 1)){
case '{':
$keys = substr($keys, 1);
$lengthMarker = strpos($values, '{') + 1;
$this->serialString .= substr($values, 0, $lengthMarker);
$values = substr($values, $lengthMarker);
break;
case '}':
$lengthMarker++;
$this->serialString .= substr($values, 0, $lengthMarker);
$keys = substr($values, $lengthMarker);
$values = substr($values, $lengthMarker);
break;
default:
// will produce an error that can be ignored
$lengthMarker = intval(substr($keys, 2, @strpos($keys, ':', 2)));
$lengthMarker += strpos($keys, '"') + 3;
$temp = substr($keys, 0, $lengthMarker);
$this->serialString .= $temp;
$lengthMarker++;
$arrFlag = false;
switch(substr($keys, $lengthMarker, 1)){
case ':':
$lengthMarker--;
break;
case '}':
$lengthMarker++;
$arrFlag = true;
break;
}
$keys = substr($keys, $lengthMarker);
do{
$lengthMarker = 0;
switch(substr($values, 0, 1)){
case 'a':
$lengthMarker = strpos($values, '{') + 1;
break;
case 's':
$lengthMarker = intval(substr($values, 2, strpos($values, ':', 2)));
$lengthMarker += strpos($values, '"') + 3;
break;
case 'i':
case 'N':
case 'b':
$lengthMarker = strpos($values, ';') + 1;
break;
case '}':
$lengthMarker++;
$arrFlag = false;
break;
}
$this->serialString .= substr($values, 0, $lengthMarker);
$values = substr($values, $lengthMarker);
}while($arrFlag);
if(strlen($values) == 0 && strlen($keys) != 0){
$this->serialString = substr($this->serialString, 0, -1 * strlen($temp) - 1) . '}';
}
break;
}
}while(strlen($values) > 0);
}
}
function getKeys(){
return $this->keys;
}
function getValues(){
return $this->values;
}
function getSerial(){
return $this->serialString;
}
}
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#2 |
|
Expert Programmer
|
Re: Messing with Serialization
You could read the string much more easily with a regular expression.
Perhaps I don't fully understand the problem, but couldn't you just create separate arrays of the keys and values and use the built-in serialization functions to store them? That is, assuming the order of the objects is retained. Last edited by titaniumdecoy; Nov 15th, 2007 at 1:38 AM. |
|
|
|
|
|
#3 |
|
King of Portal
|
Ya sorry titanium I guess I didn't say exactly what the issue was, let me try a more concrete problem statement:
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#4 |
|
King of Portal
|
Re: Messing with Serialization
This is the latest refinement I made to the gbSerializer class and it works as I expect it to. Just curious if anyone could see a way I might refine it better.
class gbSerializer {
var $serialString = '';
var $keys = '';
var $values = '';
function gbSerializer($keys, $values = null){
if(!isset($values)){
$this->serialString = $keys;
$count = 0;
do{
$lengthMarker = 0;
$iniChar = substr($keys, 0, 1);
switch($iniChar){
case 'a':
$lengthMarker = strpos($keys, '{') + 1;
break;
case 's':
$lengthMarker = intval(substr($keys, 2, strpos($keys, ':', 2)));
$lengthMarker += strpos($keys, '"') + 3;
break;
case 'i':
case 'N':
case 'b':
$lengthMarker = strpos($keys, ';') + 1;
break;
case '}':
$lengthMarker++;
break;
}
$temp = substr($keys, 0, $lengthMarker);
$keys = substr($keys, $lengthMarker);
switch($iniChar){
case 'a':
$count = 0;
$this->values .= $temp;
$this->keys .= '{';
break;
case '}':
$count = 0;
$this->values .= $temp;
$this->keys .= '}';
break;
default:
if($count % 2){
$this->values .= $temp;
}else{
if(!strcmp($iniChar, 'i')){
$this->values .= $temp;
}else{
$this->keys .= $temp;
}
}
$count++;
break;
}
}while(strlen($keys) > 0);
}else{
$this->keys = $keys;
$this->values = $values;
$count = 0;
do{
$lengthMarker = 0;
if(!strcmp(substr($values, 0, 1), '}')){
$lengthMarker++;
$this->serialString .= substr($values, 0, $lengthMarker);
$values = substr($values, $lengthMarker);
}else{
switch(substr($keys, 0, 1)){
case '{':
$keys = substr($keys, 1);
$lengthMarker = strpos($values, '{') + 1;
$this->serialString .= substr($values, 0, $lengthMarker);
$values = substr($values, $lengthMarker);
$count = 0;
break;
default:
switch($count % 2){
case 0:
$count++;
$lengthMarker = intval(substr($keys, 2, strpos($keys, ':', 2)));
$lengthMarker += strpos($keys, '"') + 3;
$this->serialString .= substr($keys, 0, $lengthMarker);
$lengthMarker++;
switch(substr($keys, $lengthMarker, 1)){
case ':':
$lengthMarker--;
break;
case '}':
$lengthMarker++;
$count = -1;
break;
}
$keys = substr($keys, $lengthMarker);
break;
default:
if($count > -1){
$count++;
}
do{
$lengthMarker = 0;
switch(substr($values, 0, 1)){
case 'a':
$lengthMarker = strpos($values, '{') + 1;
break;
case 's':
$lengthMarker = intval(substr($values, 2, strpos($values, ':', 2)));
$lengthMarker += strpos($values, '"') + 3;
break;
case 'i':
case 'N':
case 'b':
$lengthMarker = strpos($values, ';') + 1;
break;
case '}':
$lengthMarker++;
$count = 0;
break;
}
$this->serialString .= substr($values, 0, $lengthMarker);
$values = substr($values, $lengthMarker);
}while($count < 0);
break;
}
break;
}
}
}while(strlen($values) > 0);
}
}
function getKeys(){
return $this->keys;
}
function getValues(){
return $this->values;
}
function getSerial(){
return $this->serialString;
}
}
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
Re: Messing with Serialization
Your solution seems so arbitrary. Have you tried to address the base problem of it all: file permissions? Since the new files default to a certain permission, have you tried chmodding them after creation? Have you tried setting .htaccess, contacting your server hosting's support, or looking for a synthetic-file-permissions solution that already exists?
Just saying. |
|
|
|
|
|
#6 |
|
King of Portal
|
Re: Messing with Serialization
Well here I'm not dealing with the problem of file permissions. I'm actually trying to find a way to reduce the amount of wasted bytes in the database. However, yes I have tried chmodding them to other permissions. Again the server treated them all as 777 even if it said something different in my FTP program. htaccess files were also not respected. This all happened on a free server and yes I wrote up on their forums and they said if you want privacy on your stuff you gotta pay. Solution was obviously to switch server and do what you're stating Sane. However, that would've meant I would've isolated everyone who wanted to store their files there. This solution on the other hand will work everywhere, because wherever there's a PHP engine it will parse out comments. So no risk of anything being seen.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
Re: Messing with Serialization
It seems innovative, I'll give you that. I'm just not so sure how many people would agree it's the most beneficial solution for reasons that I'm sure you could evaluate yourself. Either way, neat stuff. Get a sourceforge page up! See if others can contribute their ideas to make it a more fully-encomposing solution.
|
|
|
|
|
|
#8 |
|
King of Portal
|
Re: Messing with Serialization
Actually, GrimBB has a sourceforge page Sane. However, I found the sourceforge system very complicated. That's just me, it was complicated for me, I'm not trying to say the sourceforge system is complicated or is no good or anything. So I just removed the project source from there and basically just use sourceforge as a redirect to the GrimBB site. Interestingly enough I used the system to document like a bug or vulnerability or something, and suddenly the net was bombarded with GrimBB XSS Vulnerability security issues or something or other lol. The Brainstorming forum on my site is for those ideas. Of course I'd need actual traffic there but ay, that's why it's just my hobby ^_^
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
Re: Messing with Serialization
I was talking about your solution. Not GrimBB.
|
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
Re: Messing with Serialization
Hmm, I don't believe anyone has written a fully-functioning stand-alone relational database purely in .php before. That would be pretty nifty, and would have been perfect for GrimBB (instead of integrating a custom "database"). I should try that. Would be very fun.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Serialization | grimpirate | Software Design and Algorithms | 7 | Oct 31st, 2007 11:35 PM |
| how to transfer object using socket (tcp/ip) | amitpansuria | C++ | 4 | Aug 28th, 2007 5:44 AM |
| Binary Serialization vs. Byte Streams | kurifu | C# | 1 | Apr 7th, 2007 5:17 PM |