Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 15th, 2007, 1:17 AM   #1
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
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;
	}
}
Pretty lengthy as you can tell. Now I'm pretty confident about the constructor when it is given a serialized string and it removes the keys (it also inserts brackets into the keys so as to indicate the nested structure of the array). However, I'm not so confident about the constructor when it is given keys and values to try and output a serialized string. It works with what I've tried, but there are instances where an error is generated which I just put in an @ to ignore. I was hoping someone with some free time might be able to offer an alternative. Thanks for any help.
__________________
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
grimpirate is offline   Reply With Quote
Old Nov 15th, 2007, 1:24 AM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 837
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Nov 15th, 2007, 2:16 AM   #3
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Ya sorry titanium I guess I didn't say exactly what the issue was, let me try a more concrete problem statement:
  1. Given a serialized array structure composed of zero or more nested arrays with numeric as well as string indexes, extract ONLY the string indexes out of the serialized structure.
  2. Given the separated structures from part 1 reintegrate them into the original serialized structure. Special aside: assuming you're given a shorter structure that contains the values, fill up array as available with the provided keys (i.e. having more keys than applicable key fields in the value string shouldn't be an issue).
I hope that states it better. I thought of regular expressions but honestly just wasn't sure how to proceed.
__________________
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
grimpirate is offline   Reply With Quote
Old Nov 19th, 2007, 5:01 PM   #4
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
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
grimpirate is offline   Reply With Quote
Old Nov 19th, 2007, 5:07 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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.
Sane is offline   Reply With Quote
Old Nov 19th, 2007, 9:27 PM   #6
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
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
grimpirate is offline   Reply With Quote
Old Nov 19th, 2007, 9:41 PM   #7
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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.
Sane is offline   Reply With Quote
Old Nov 20th, 2007, 4:14 AM   #8
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
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
grimpirate is offline   Reply With Quote
Old Nov 20th, 2007, 12:39 PM   #9
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Re: Messing with Serialization

I was talking about your solution. Not GrimBB.
Sane is offline   Reply With Quote
Old Nov 20th, 2007, 1:34 PM   #10
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:26 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC