using System;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace Cvv.Components
{
public class Serializer
{
public static readonly bool CanBinarySerialize;
static Serializer()
{
SecurityPermission permission = new SecurityPermission(SecurityPermissionFlag.SerializationFormatter);
try
{
permission.Demand();
CanBinarySerialize = true;
}
catch (SecurityException)
{
CanBinarySerialize = false;
}
}
private Serializer()
{
}
public static object ConvertFileToObject(string path, Type objectType)
{
object obj2 = null;
if ((path != null) && (path.Length > 0))
{
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
obj2 = new XmlSerializer(objectType).Deserialize(stream);
stream.Close();
}
}
return obj2;
}
public static void ConvertFromNameValueCollection(NameValueCollection nvc, ref string keys, ref string values)
{
ConvertFromNameValueCollection(nvc, ref keys, ref values, false);
}
public static void ConvertFromNameValueCollection(NameValueCollection nvc, ref string keys, ref string values, bool allowEmptyStrings)
{
if ((nvc != null) && (nvc.Count != 0))
{
StringBuilder builder = new StringBuilder();
StringBuilder builder2 = new StringBuilder();
int num = 0;
foreach (string text in nvc.AllKeys)
{
if (text.IndexOf(':') != -1)
{
throw new ArgumentException("ExtendedAttributes Key can not contain the character \":\"");
}
string text2 = nvc[text];
if ((allowEmptyStrings && (text2 != null)) || !TypeHelper.IsNullorEmpty(text2))
{
builder.AppendFormat("{0}:S:{1}:{2}:", text, num, text2.Length);
builder2.Append(text2);
num += text2.Length;
}
}
keys = builder.ToString();
values = builder2.ToString();
}
}
public static object ConvertSOAPToObject(string xml, Type objType)
{
MemoryStream serializationStream = null;
object obj2;
try
{
IFormatter formatter = new SoapFormatter();
serializationStream = new MemoryStream(Encoding.Default.GetBytes(xml));
obj2 = formatter.Deserialize(serializationStream);
}
finally
{
if (serializationStream != null)
{
serializationStream.Close();
}
}
return obj2;
}
public static byte[] ConvertToBytes(object objectToConvert)
{
byte[] buffer = null;
if (CanBinarySerialize)
{
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream serializationStream = new MemoryStream())
{
formatter.Serialize(serializationStream, objectToConvert);
serializationStream.Position = 0;
buffer = new byte[serializationStream.Length];
serializationStream.Read(buffer, 0, buffer.Length);
serializationStream.Close();
}
}
return buffer;
}
public static byte[] ConvertToBytes(string s)
{
return Convert.FromBase64String(s);
}
public static NameValueCollection ConvertToNameValueCollection(string keys, string values)
{
NameValueCollection values2 = new NameValueCollection();
if (((keys != null) && (values != null)) && ((keys.Length > 0) && (values.Length > 0)))
{
char[] separator = new char[] { ':' };
string[] textArray = keys.Split(separator);
for (int i = 0; i < (textArray.Length / 4); i++)
{
int startIndex = int.Parse(textArray[(i * 4) + 2], CultureInfo.InvariantCulture);
int length = int.Parse(textArray[(i * 4) + 3], CultureInfo.InvariantCulture);
string text = textArray[i * 4];
if (((textArray[(i * 4) + 1] == "S") && (startIndex >= 0)) && (values.Length >= (startIndex + length)))
{
values2[text] = values.Substring(startIndex, length);
}
}
}
return values2;
}
public static object ConvertToObject(byte[] byteArray)
{
object obj2 = null;
if ((CanBinarySerialize && (byteArray != null)) && (byteArray.Length > 0))
{
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream serializationStream = new MemoryStream())
{
serializationStream.Write(byteArray, 0, byteArray.Length);
serializationStream.Position = 0;
if (byteArray.Length > 4)
{
obj2 = formatter.Deserialize(serializationStream);
}
serializationStream.Close();
}
}
return obj2;
}
public static object ConvertToObject(string xml, Type objectType)
{
object obj2 = null;
if
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace Cvv.Components
{
public class Serializer
{
public static readonly bool CanBinarySerialize;
static Serializer()
{
SecurityPermission permission = new SecurityPermission(SecurityPermissionFlag.SerializationFormatter);
try
{
permission.Demand();
CanBinarySerialize = true;
}
catch (SecurityException)
{
CanBinarySerialize = false;
}
}
private Serializer()
{
}
public static object ConvertFileToObject(string path, Type objectType)
{
object obj2 = null;
if ((path != null) && (path.Length > 0))
{
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
obj2 = new XmlSerializer(objectType).Deserialize(stream);
stream.Close();
}
}
return obj2;
}
public static void ConvertFromNameValueCollection(NameValueCollection nvc, ref string keys, ref string values)
{
ConvertFromNameValueCollection(nvc, ref keys, ref values, false);
}
public static void ConvertFromNameValueCollection(NameValueCollection nvc, ref string keys, ref string values, bool allowEmptyStrings)
{
if ((nvc != null) && (nvc.Count != 0))
{
StringBuilder builder = new StringBuilder();
StringBuilder builder2 = new StringBuilder();
int num = 0;
foreach (string text in nvc.AllKeys)
{
if (text.IndexOf(':') != -1)
{
throw new ArgumentException("ExtendedAttributes Key can not contain the character \":\"");
}
string text2 = nvc[text];
if ((allowEmptyStrings && (text2 != null)) || !TypeHelper.IsNullorEmpty(text2))
{
builder.AppendFormat("{0}:S:{1}:{2}:", text, num, text2.Length);
builder2.Append(text2);
num += text2.Length;
}
}
keys = builder.ToString();
values = builder2.ToString();
}
}
public static object ConvertSOAPToObject(string xml, Type objType)
{
MemoryStream serializationStream = null;
object obj2;
try
{
IFormatter formatter = new SoapFormatter();
serializationStream = new MemoryStream(Encoding.Default.GetBytes(xml));
obj2 = formatter.Deserialize(serializationStream);
}
finally
{
if (serializationStream != null)
{
serializationStream.Close();
}
}
return obj2;
}
public static byte[] ConvertToBytes(object objectToConvert)
{
byte[] buffer = null;
if (CanBinarySerialize)
{
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream serializationStream = new MemoryStream())
{
formatter.Serialize(serializationStream, objectToConvert);
serializationStream.Position = 0;
buffer = new byte[serializationStream.Length];
serializationStream.Read(buffer, 0, buffer.Length);
serializationStream.Close();
}
}
return buffer;
}
public static byte[] ConvertToBytes(string s)
{
return Convert.FromBase64String(s);
}
public static NameValueCollection ConvertToNameValueCollection(string keys, string values)
{
NameValueCollection values2 = new NameValueCollection();
if (((keys != null) && (values != null)) && ((keys.Length > 0) && (values.Length > 0)))
{
char[] separator = new char[] { ':' };
string[] textArray = keys.Split(separator);
for (int i = 0; i < (textArray.Length / 4); i++)
{
int startIndex = int.Parse(textArray[(i * 4) + 2], CultureInfo.InvariantCulture);
int length = int.Parse(textArray[(i * 4) + 3], CultureInfo.InvariantCulture);
string text = textArray[i * 4];
if (((textArray[(i * 4) + 1] == "S") && (startIndex >= 0)) && (values.Length >= (startIndex + length)))
{
values2[text] = values.Substring(startIndex, length);
}
}
}
return values2;
}
public static object ConvertToObject(byte[] byteArray)
{
object obj2 = null;
if ((CanBinarySerialize && (byteArray != null)) && (byteArray.Length > 0))
{
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream serializationStream = new MemoryStream())
{
serializationStream.Write(byteArray, 0, byteArray.Length);
serializationStream.Position = 0;
if (byteArray.Length > 4)
{
obj2 = formatter.Deserialize(serializationStream);
}
serializationStream.Close();
}
}
return obj2;
}
public static object ConvertToObject(string xml, Type objectType)
{
object obj2 = null;
if