codice:
public struct PHP_Serializer
{
// Andrea Giamamarchi [2006/02/10]
/**
* public namespace struct
* cast implicitly unserialized variables
*
* PHP_UnSerialized_Object(object variable);
*
* @param Object generic object unserialized compatible with php
* [int,bool,double,null,string,Hashtable(array),SortedList(Object)]
* @return Mixed one of valid implicit casting types, Null variable returns string "null" with ToString()
*
*/
public struct PHP_UnSerialized_Object
{
object __value;
public PHP_UnSerialized_Object(object __value)
{
this.__value = __value;
}
public static implicit operator System.Int32(PHP_UnSerialized_Object php_serializer_object)
{
return (System.Int32)php_serializer_object.__value;
}
public static implicit operator System.Double(PHP_UnSerialized_Object php_serializer_object)
{
return (System.Double)php_serializer_object.__value;
}
public static implicit operator System.Boolean(PHP_UnSerialized_Object php_serializer_object)
{
return (System.Boolean)php_serializer_object.__value;
}
public static implicit operator System.String(PHP_UnSerialized_Object php_serializer_object)
{
return (System.String)php_serializer_object.__value;
}
public static implicit operator System.Collections.Hashtable(PHP_UnSerialized_Object php_serializer_object)
{
return (System.Collections.Hashtable)php_serializer_object.__value;
}
public static implicit operator System.Collections.SortedList(PHP_UnSerialized_Object php_serializer_object)
{
return (System.Collections.SortedList)php_serializer_object.__value;
}
public override string ToString()
{
System.String result;
if (__value == null)
result = "null";
else
result = __value.ToString();
return result;
}
}
/** private variables */
private System.Int32 __c; // string position, where indexOf and substrings should start
private System.String __s; // string to unserialize
/**
* public method serialize
* accepts a generic object and returns a PHP serialize
* rappresentation.
*
* this.serialize(what:Object):String
*
* @param Object generic object to serialize compatible with php
* [int,bool,double,null,string,Hashtable(array),SortedList(Object)]
* @return String php compatible rappresentation string of serialized value
*/
public System.String serialize(System.Object what)
{
System.String __s;
if (what is System.Int32)
__s = __serializeInt32((System.Int32)what);
else if (what is System.Double)
__s = __serializeDouble((System.Double)what);
else if (what is System.Boolean)
__s = __serializeBoolean((System.Boolean)what);
else if (what is System.String)
__s = __serializeString((System.String)what);
else if (what is System.Collections.Hashtable)
__s = __serializeHashtable((System.Collections.Hashtable)what);
else if (what is System.Collections.SortedList)
__s = __serializeSortedList((System.Collections.SortedList)what);
else if (what == null)
__s = "N;";
else
__s = "";
return __s;
}
/**
* public method serialize
* accepts a php serialized string and returns a generic object that should be a
* [int,bool,double,string,Hashtable(from array),SortedList(from Object *),null]
* * Object will contains a key called __name__ that is the name of original serialized Object
*
* this.unserialize(what:String):Object
*
* @param String php serialized rappresentation of a variable
* @return Object C# compatible variable [int,double,bool,Hashtable,SortedList,null,string]
*/
public PHP_UnSerialized_Object unserialize(System.String what)
{
__c = 0;
__s = what;
return new PHP_UnSerialized_Object(__switchFunction(__s.Substring(__c, 1)));
}
/** list all private methods */
private System.String __serializeString(System.String s)
{
return "s:" + s.Length + ":\"" + s + "\";";
}
private System.String __serializeBoolean(System.Boolean b)
{
return "b:" + (b == false ? "0" : "1") + ";";
}
private System.String __serializeInt32(System.Int32 i)
{
return "i:" + i + ";";
}
private System.String __serializeDouble(System.Double d)
{
return "d:" + d.ToString().Replace(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator, ".") + ";";
}
private System.String __serializeHashtable(System.Collections.Hashtable a)
{
return "a:" + a.Count + "{" + __serializeCollections(a) + "}";
}
private System.String __serializeSortedList(System.Collections.SortedList O)
{
return "O:" + O["__name__"].ToString().Length + ":\"" + O["__name__"] + "\":" + (O.Count - 1) + ":{" + __serializeCollections(O) + "}";
}
private System.String __serializeCollections(System.Collections.Hashtable a)
{
System.String __s = "";
foreach (System.Collections.DictionaryEntry r in a)
__s += serialize(r.Key) + "" + serialize(r.Value);
return __s;
}
private System.String __serializeCollections(System.Collections.SortedList O)
{
System.String k, v, __s = "";
foreach (System.Collections.DictionaryEntry r in O)
{
k = r.Key.ToString();
v = serialize(r.Value);
if (k != "__name__")
{
k = serialize(k);
if (k != "" && v != "")
__s += k + "" + v;
}
}
return __s;
}
private System.String __unserializeString()
{
System.Int32 sli, sli2;
System.String sls;
__c += 2;
sls = __s.Substring(__c, (__s.IndexOf(':', __c) - __c));
sli = System.Convert.ToInt32(sls);
sli2 = __c + sls.Length + 2;
__c = sli2 + sli + 2;
return __s.Substring(sli2, sli);
}
private System.Int32 __unserializeInt32()
{
System.Int32 sli = __s.IndexOf(';', (__c + 1)) - 2;
System.Int32 tmp = System.Convert.ToInt32(__s.Substring((__c + 2), (sli - __c)));
__c = sli + 3;
return tmp;
}
private System.Double __unserializeDouble()
{
System.Int32 sli = __s.IndexOf(';', (__c + 1)) - 2;
System.Double tmp = System.Convert.ToDouble(__s.Substring((__c + 2), (sli - __c)).Replace(".", System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator));
__c = sli + 3;
return tmp;
}
private System.Boolean __unserializeBoolean()
{
System.Boolean tmp = (__s.Substring(__c + 2, 1) == "1" ? true : false);
__c += 4;
return tmp;
}
private System.Int32 __uGetKforWhile()
{
System.Int32 a, k;
__c += 2;
a = __s.IndexOf(':', __c);
k = System.Convert.ToInt32(__s.Substring(__c, (a - __c))) + 1;
__c = a + 2;
return k;
}
private System.Collections.SortedList __unserializeCollections(System.Collections.SortedList tmp)
{
System.Int32 k = __uGetKforWhile();
while (--k > 0)
tmp[__switchFunction(__s.Substring(__c, 1)).ToString()] = __switchFunction(__s.Substring(__c, 1));
return tmp;
}
private System.Collections.Hashtable __unserializeCollections(System.Collections.Hashtable tmp)
{
System.Int32 k = __uGetKforWhile();
while (--k > 0)
tmp[__switchFunction(__s.Substring(__c, 1))] = __switchFunction(__s.Substring(__c, 1));
return tmp;
}
private System.Collections.Hashtable __unserializeHashtable()
{
System.Collections.Hashtable tmp = __unserializeCollections(new System.Collections.Hashtable());
++__c;
return tmp;
}
private System.Collections.SortedList __unserializeSortedList()
{
System.String tmp, a, o;
System.Collections.SortedList result;
tmp = "s" + __s.Substring(++__c, (__s.IndexOf(':', (__c + 3)) - __c)) + ";";
a = tmp.Substring(2, (tmp.IndexOf(':', 2) - 2));
o = tmp.Substring((a.Length + 4), System.Convert.ToInt32(a));
__c += (tmp.Length - 3);
result = __unserializeCollections(new System.Collections.SortedList());
result["__name__"] = o;
++__c;
return result;
}
private System.Object __switchFunction(System.String what)
{
System.Object result;
switch (what)
{
case "O":
result = __unserializeSortedList();
break;
case "a":
result = __unserializeHashtable();
break;
case "s":
result = __unserializeString();
break;
case "i":
result = __unserializeInt32();
break;
case "d":
result = __unserializeDouble();
break;
case "b":
result = __unserializeBoolean();
break;
case "N":
__c += 2;
result = null;
break;
default:
result = new System.Object();
break;
}
return result;
}
}