/*
Media File XStream, Network file stream server supporting XBMSP
Copyright (C) 2004 j3g
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
This project can be found on SourceForge.
http://sourceforge.net/projects/mfxstream
*/
using System;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Windows.Forms.Design;
using System.Drawing.Design;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
namespace MediaStream{
/// <summary>
///
/// </summary>
[DefaultPropertyAttribute("FlattenSubFoldersDefault")]
public class CSettings
{
#region Private attributes
private static XmlDocument m_xmlSettings;
private static String m_strPath = GetAppPath + "\\Settings.xml";
private static Boolean m_blnSaving;
private static DateTime m_dtLastLoad = new DateTime(1, 1, 1);
#endregion Private attributes
#region Static and Non static properties
[CategoryAttribute("MFXStream Server"), DescriptionAttribute("Server port number.")]
public Int16 PortNumber { get { return (Convert.ToInt16(GetXmlValue("Settings/PortNumber", "1400"))); } set { SetXmlValue("Settings/PortNumber", Convert.ToString(value)); } }
public static Int16 GetPortNumber { get { return (Convert.ToInt16(GetXmlValue("Settings/PortNumber", "1400"))); } set { SetXmlValue("Settings/PortNumber", Convert.ToString(value)); } }
[CategoryAttribute("MFXStream Server"), DescriptionAttribute("Server Maximum simultaneous client connections.")]
public Int32 MaxClientConnections { get { return (Convert.ToInt32(GetXmlValue("Settings/MaxClientConnections", "10"))); } set { SetXmlValue("Settings/MaxClientConnections", Convert.ToString(value)); } }
public static Int32 GetMaxClientConnections { get { return (Convert.ToInt32(GetXmlValue("Settings/MaxClientConnections", "10"))); } set { SetXmlValue("Settings/MaxClientConnections", Convert.ToString(value)); } }
[CategoryAttribute("MFXStream Server"), DescriptionAttribute("Server Reciving buffer size per client.")]
public Int32 ClientBufferSize { get { return (Convert.ToInt32(GetXmlValue("Settings/ClientBufferSize", "1024"))); } set { SetXmlValue("Settings/ClientBufferSize", Convert.ToString(value)); } }
public static Int32 GetClientBufferSize { get { return (Convert.ToInt32(GetXmlValue("Settings/ClientBufferSize", "1024"))); } set { SetXmlValue("Settings/ClientBufferSize", Convert.ToString(value)); } }
[CategoryAttribute("MFXStream Server"), DescriptionAttribute("Use user authenication.")]
public Boolean Authenication { get { return ("1" == GetXmlValue("Settings/Authenication", "0") ? true : false); } set { SetXmlValue("Settings/Authenication", (true == value ? "1" : "0")); } }
public static Boolean GetAuthenication { get { return ("1" == GetXmlValue("Settings/Authenication", "0") ? true : false); } set { SetXmlValue("Settings/Authenication", (true == value ? "1" : "0")); } }
[CategoryAttribute("MFXStream Server"), DescriptionAttribute("IPAddress for server, Default is the first IPAddress in the computers IPAddress-list."), TypeConverter(typeof(CIPAddressConverter))]
public String ServerIPAddress
{
get
{
String strIPAddress = GetXmlValue("Settings/ServerIPAddress", "Default");
IPAddress [] ipAddressList = GetServerIPAddressList;
if ("Default" != strIPAddress)
{
for (Int16 i = 0; i < ipAddressList.Length; i++)
{
if (ipAddressList[i].ToString().Split(':')[0] == strIPAddress)
{
return (strIPAddress);
}
}
strIPAddress = "Default";
}
return (strIPAddress);
}
set { SetXmlValue("Settings/ServerIPAddress", value); }
}
public static String GetServerIPAddress
{
get
{
String strIPAddress = GetXmlValue("Settings/ServerIPAddress", "Default");
IPAddress [] ipAddressList = GetServerIPAddressList;
if ("Default" == strIPAddress)
{
strIPAddress = ipAddressList[0].ToString().Split(':')[0];
}
else
{
for (Int16 i = 0; i < ipAddressList.Length; i++)
{
if (ipAddressList[i].ToString().Split(':')[0] == strIPAddress)
{
return (strIPAddress);
}
}
strIPAddress = ipAddressList[0].ToString().Split(':')[0];
}
return (strIPAddress);
}
}
public static IPAddress [] GetServerIPAddressList { get { return(Dns.GetHostByName(Dns.GetHostName()).AddressList); } }
[CategoryAttribute("MFXStream Server"), DescriptionAttribute("Path to sharesfile."), Editor ( typeof(FileNameEditor), typeof(UITypeEditor) )]
public String SharesFilePath { get { return (GetXmlValue("Settings/SharesFilePath", "MFXShares.xml")); } set { SetXmlValue("Settings/SharesFilePath", value); } }
public static String GetSharesFilePath
{
get
{
String strPath = GetXmlValue("Settings/SharesFilePath", "MFXShares.xml");
if (!System.IO.Path.IsPathRooted(strPath))
strPath = GetAppPath + "\\" + strPath;
return (strPath);
}
set
{
SetXmlValue("Settings/SharesFilePath", value);
}
}
[CategoryAttribute("MFXStream Loging"), DescriptionAttribute("Path to logfile."), Editor ( typeof(FileNameEditor), typeof(UITypeEditor) )]
public String LogFileName { get { return (GetXmlValue("Settings/LogFile/LogFileName", "MFXStream.log")); } set { SetXmlValue("Settings/LogFile/LogFileName", value); } }
public static String GetLogFileName { get { return (GetXmlValue("Settings/LogFile/LogFileName", "MFXStream.log")); } set { SetXmlValue("Settings/LogFile/LogFileName", value); } }
[CategoryAttribute("MFXStream Loging"), DescriptionAttribute("Loging level.")] //, EnumConverter(typeof(LogLevel))]
public LogLevel LogLevel { get { return ((LogLevel) Convert.ToInt16(GetXmlValue("Settings/LogFile/LogLevel","0"))); } set { SetXmlValue("Settings/LogFile/LogLevel", Convert.ToString((int) value)); } }
public static LogLevel GetLogLevel { get { return ((LogLevel) Convert.ToInt16(GetXmlValue("Settings/LogFile/LogLevel","0"))); } set { SetXmlValue("Settings/LogFile/LogLevel", Convert.ToString((int) value)); } }
[CategoryAttribute("MFXStream Client"), DescriptionAttribute("If server is to start automaticaly.")]
public Boolean StartServer { get { return ("1" == GetXmlValue("Settings/Client/StartServer", "0") ? true : false); } set { SetXmlValue("Settings/Client/StartServer", (value ? "1" : "0")); } }
public static Boolean GetStartServer { get { return ("1" == GetXmlValue("Settings/Client/StartServer", "0") ? true : false); } set { SetXmlValue("Settings/Client/StartServer", (value ? "1" : "0")); } }
[CategoryAttribute("MFXStream Client"), DescriptionAttribute("Merge MountPoints default, true = On, false = Off.")]
public Boolean MergeMountPointDefault { get { return ("1" == GetXmlValue("Settings/Client/MergeMountPointDefault", "0") ? true : false); } set { SetXmlValue("Settings/Client/MergeMountPointDefault", (value ? "1" : "0")); } }
public static Boolean GetMergeMountPointDefault { get { return ("1" == GetXmlValue("Settings/Client/MergeMountPointDefault", "0") ? true : false); } set { SetXmlValue("Settings/Client/MergeMountPointDefault", (value ? "1" : "0")); } }
[CategoryAttribute("MFXStream Client"), DescriptionAttribute("Show Subfolders default, true = On, false = Off.")]
public Boolean ShowSubFoldersDefault { get { return ("1" == GetXmlValue("Settings/Client/ShowSubFoldersDefault", "0") ? true : false); } set { SetXmlValue("Settings/Client/ShowSubFoldersDefault", (value ? "1" : "0")); } }
public static Boolean GetShowSubFoldersDefault { get { return ("1" == GetXmlValue("Settings/Client/ShowSubFoldersDefault", "0") ? true : false); } set { SetXmlValue("Settings/Client/ShowSubFoldersDefault", (value ? "1" : "0")); } }
[CategoryAttribute("MFXStream Client"), DescriptionAttribute("Flattens Subfolders default, true = On, false = Off.")]
public Boolean FlattenSubFoldersDefault { get { return ("1" == GetXmlValue("Settings/Client/FlattenSubFoldersDefault", "0") ? true : false); } set { SetXmlValue("Settings/Client/FlattenSubFoldersDefault", (value ? "1" : "0")); } }
public static Boolean GetFlattenSubFoldersDefault { get { return ("1" == GetXmlValue("Settings/Client/FlattenSubFoldersDefault", "0") ? true : false); } set { SetXmlValue("Settings/Client/FlattenSubFoldersDefault", (value ? "1" : "0")); } }
[BrowsableAttribute(false)]
public static String GetFtpIPAddress { get { return (GetXmlValue("Settings/FTPXbox/IPAddress", "")); } set { SetXmlValue("Settings/FTPXbox/IPAddress", value); } }
[BrowsableAttribute(false)]
public static String GetFtpUser { get { return (GetXmlValue("Settings/FTPXbox/User", "")); } set { SetXmlValue("Settings/FTPXbox/User", value); } }
[BrowsableAttribute(false)]
public static String GetFtpPrefix { get { return (GetXmlValue("Settings/FTPXbox/Prefix", "")); } set { SetXmlValue("Settings/FTPXbox/Prefix", value); } }
[BrowsableAttribute(false)]
public static String GetFtpPath { get { return (GetXmlValue("Settings/FTPXbox/Path", "")); } set { SetXmlValue("Settings/FTPXbox/Path", value); } }
[BrowsableAttribute(false)]
public static String GetFtpFileName { get { return (GetXmlValue("Settings/FTPXbox/FileName", "")); } set { SetXmlValue("Settings/FTPXbox/FileName", value); } }
[BrowsableAttribute(false)]
public static String GetFtpPassword { get { return (Decrypt(GetXmlValue("Settings/FTPXbox/Password", ""))); } set { SetXmlValue("Settings/FTPXbox/Password", Encrypt(value)); } }
[BrowsableAttribute(false)]
public static Boolean GetFtpRemoveOld { get { return ("1" == GetXmlValue("Settings/FTPXbox/RemoveOld", "0") ? true : false); } set { SetXmlValue("Settings/FTPXbox/RemoveOld", (value ? "1" : "0")); } }
[BrowsableAttribute(false)]
public static int SharedFileCount { get { return (int.Parse(GetXmlValue("Settings/SharedFileCount", "0"))); } set { SetXmlValue("Settings/SharedFileCount", value.ToString()); } }
[BrowsableAttribute(false)]
public static int SharedFolderCount { get { return (int.Parse(GetXmlValue("Settings/SharedFolderCount", "0"))); } set { SetXmlValue("Settings/SharedFolderCount", value.ToString()); } }
[BrowsableAttribute(false)]
public static String GetAppPath
{
get
{
return (System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Substring(6));
}
}
[BrowsableAttribute(false)]
public static DateTime GetSharesFileLastWriteTime
{
get
{
if (File.Exists(GetSharesFilePath))
{
return (File.GetLastWriteTime(GetSharesFilePath));
}
else
{
return (new DateTime(1,1,1));
}
}
}
#endregion Static and Non static properties
#region Settings Functions
static CSettings()
{
LoadSettings();
}
private static String GetXmlValue(String strXPath, String strDefaultValue)
{
try
{
LoadSettings();
XmlNode xmlNode = m_xmlSettings.SelectSingleNode(strXPath);
if (null == xmlNode)
{
xmlNode = CreateXPath(strXPath);
xmlNode.InnerText = strDefaultValue;
}
return (xmlNode.InnerText);
}
catch
{
return (strDefaultValue);
}
}
private static void SetXmlValue(String strXPath, String strValue)
{
try
{
LoadSettings();
XmlNode xmlNode = m_xmlSettings.SelectSingleNode(strXPath);
xmlNode.InnerText = strValue;
SaveSettings();
}
catch
{
CreateXPath(strXPath).InnerText = strValue;
SaveSettings();
}
}
private static void LoadSettings()
{
try
{
DateTime dtLastWrite;
dtLastWrite = System.IO.File.GetLastWriteTime(m_strPath);
if (m_dtLastLoad < dtLastWrite)
{
m_xmlSettings = new XmlDocument();
XmlTextReader trFile = new XmlTextReader(TextReader.Synchronized(File.OpenText(m_strPath)));
m_xmlSettings.Load(trFile);
trFile.Close();
trFile = null;
m_dtLastLoad = DateTime.Now;
}
}
catch
{
m_xmlSettings = new XmlDocument();
m_xmlSettings.LoadXml("<Settings>\n</Settings>\n");
}
}
private static void SaveSettings()
{
try
{
XmlTextWriter twFile = new XmlTextWriter(TextWriter.Synchronized(File.CreateText(m_strPath)));
twFile.Formatting = Formatting.Indented;
twFile.IndentChar = '\t';
twFile.Indentation = 1;
m_xmlSettings.Save(twFile);
twFile.Flush();
twFile.Close();
twFile = null;
}
catch (Exception pException)
{
throw(new Exception("Could not save settings file due to: " + pException.Message));
}
}
private static XmlNode CreateXPath(String strXPath)
{
String [] arrXPath = strXPath.Split('/');
XmlNode xmlNode = null, xmlParent = m_xmlSettings;
for (Int16 i = 0; i < arrXPath.Length; i++)
{
xmlNode = xmlParent.SelectSingleNode(arrXPath[i]);
if (null == xmlNode)
{
xmlNode = m_xmlSettings.CreateNode(XmlNodeType.Element, arrXPath[i], "");
xmlParent.AppendChild(xmlNode);
}
xmlParent = xmlNode;
}
return (xmlNode);
}
public static Boolean IsFileExtensionValid(XBMCBookmarkType xbType, String File)
{
FileInfo fi;
DirectoryInfo di = new DirectoryInfo(File);
if (di.Exists)
{
return (true);
}
else
{
fi = new FileInfo(File);
}
String strExtensions = GetXmlValue("Settings/" + xbType.ToString() + "extensions", "").ToLower();
if (0 == strExtensions.Length) return (true);
return ((0 <= strExtensions.IndexOf(fi.Extension.ToLower())));
}
public static string GetValideFileExtensions()
{
XBMCBookmarkType xbType;
String strExtensions;
xbType = XBMCBookmarkType.Video;
strExtensions = GetXmlValue("Settings/" + xbType.ToString() + "extensions", "").ToLower();
xbType = XBMCBookmarkType.Pictures;
strExtensions += GetXmlValue("Settings/" + xbType.ToString() + "extensions", "").ToLower();
xbType = XBMCBookmarkType.Music;
strExtensions += GetXmlValue("Settings/" + xbType.ToString() + "extensions", "").ToLower();
return(strExtensions);
}
public static void SetFileExtensions(XBMCBookmarkType xbType, String FileExtensions)
{
SetXmlValue("Settings/" + xbType.ToString() + "extensions", FileExtensions);
}
public static String GetFileExtensions(XBMCBookmarkType xbType)
{
return (GetXmlValue("Settings/" + xbType.ToString() + "extensions", ""));
}
#endregion Settings Functions
#region Shares Functions
public static XmlDocument LoadSharesXMLDom()
{
try
{
while (m_blnSaving) ;
XmlTextReader trFile = new XmlTextReader(TextReader.Synchronized(File.OpenText(GetSharesFilePath)));
XmlDocument xmlDom = new XmlDocument();
xmlDom.Load(trFile);
trFile.Close();
trFile = null;
return (xmlDom);
}
catch (Exception pException)
{
throw(new Exception("Could not load settings file due to: " + pException.Message));
}
}
public static void SaveSharesXMLDom(XmlDocument SharesXML)
{
try
{
m_blnSaving = true;
XmlTextWriter twFile = new XmlTextWriter(TextWriter.Synchronized(File.CreateText(GetSharesFilePath)));
twFile.Formatting = Formatting.Indented;
twFile.IndentChar = ' ';
twFile.QuoteChar = '\'';
twFile.Indentation = 4;
SharesXML.Save(twFile);
twFile.Flush();
twFile.Close();
twFile = null;
m_blnSaving = false;
}
catch (Exception pException)
{
throw(new Exception("Could not save settings file due to: " + pException.Message));
}
}
#endregion Shares Functions
#region Users Functions
public static void AddUser(String UserName, String Description, String Password)
{
try
{
LoadSettings();
XmlNode xmlUser = m_xmlSettings.SelectSingleNode("Settings/Users");
if (null == xmlUser)
xmlUser = CreateXPath("Settings/Users");
xmlUser = xmlUser.AppendChild(m_xmlSettings.CreateNode(XmlNodeType.Element, "User", ""));
xmlUser.Attributes.Append((XmlAttribute) m_xmlSettings.CreateNode(XmlNodeType.Attribute, "name",""));
xmlUser.Attributes.GetNamedItem("name").InnerText = UserName;
XmlNode xmlNode;
xmlNode = xmlUser.AppendChild(m_xmlSettings.CreateNode(XmlNodeType.Element, "Description", ""));
xmlNode.InnerText = Description;
xmlNode = xmlUser.AppendChild(m_xmlSettings.CreateNode(XmlNodeType.Element, "PW", ""));
xmlNode.InnerText = Encrypt(Password);
SaveSettings();
}
catch {}
}
public static void SetUser(String UserName, String Description, String Password)
{
try
{
LoadSettings();
Boolean blnChanged = false;
XmlNode xmlUser = m_xmlSettings.SelectSingleNode("Settings/Users/User[@name='" + UserName + "']");
if (null == xmlUser)
{
AddUser(UserName, Description, Password);
return;
}
if (xmlUser.SelectSingleNode("Description").InnerText != Description)
{
xmlUser.SelectSingleNode("Description").InnerText = Description;
blnChanged = true;
}
if (xmlUser.SelectSingleNode("PW").InnerText != Decrypt(Password))
{
xmlUser.SelectSingleNode("PW").InnerText = Encrypt(Password);
blnChanged = true;
}
if (blnChanged)
SaveSettings();
}
catch {}
}
public static void SetUser(String UserName, String Description)
{
try
{
LoadSettings();
XmlNode xmlUser = m_xmlSettings.SelectSingleNode("Settings/Users/User[@name='" + UserName + "']");
if (null == xmlUser) return;
if (xmlUser.SelectSingleNode("Description").InnerText != Description)
{
xmlUser.SelectSingleNode("Description").InnerText = Description;
SaveSettings();
}
}
catch {}
}
public static void DeleteUser(String UserName)
{
try
{
LoadSettings();
XmlNode xmlUser = m_xmlSettings.SelectSingleNode("Settings/Users/User[@name='" + UserName + "']");
if (null == xmlUser) return;
XmlNode xmlParent = xmlUser.ParentNode;
xmlParent.RemoveChild(xmlUser);
SaveSettings();
}
catch {}
}
public static Boolean CheckUser(String UserName, String Password)
{
try
{
LoadSettings();
if (Password == Decrypt(m_xmlSettings.SelectSingleNode("Settings/Users/User[@name = '" + UserName +"']/PW").InnerText))
return (true);
return (false);
}
catch
{
return (false);
}
}
public static CUser [] GetUsers()
{
try
{
LoadSettings();
XmlNodeList xmlUserList = m_xmlSettings.SelectNodes("Settings/Users/User");
if (null == xmlUserList)
return (null);
CUser [] xsUsers = new CUser[xmlUserList.Count];
Int16 i = 0;
foreach (XmlNode xmlNode in xmlUserList)
{
xsUsers[i++] = new CUser(xmlNode.Attributes.GetNamedItem("name").InnerText, xmlNode.SelectSingleNode("Description").InnerText);
}
return (xsUsers);
}
catch
{
return (null);
}
}
#endregion Users Functions
#region Trusts Functions
public static void AddTrust(String TrustAddress, String Description)
{
try
{
LoadSettings();
XmlNode xmlTrust = m_xmlSettings.SelectSingleNode("Settings/Trusts");
if (null == xmlTrust)
xmlTrust = CreateXPath("Settings/Trusts");
xmlTrust = xmlTrust.AppendChild(m_xmlSettings.CreateNode(XmlNodeType.Element, "Trust", ""));
xmlTrust.Attributes.Append((XmlAttribute) m_xmlSettings.CreateNode(XmlNodeType.Attribute, "name",""));
xmlTrust.Attributes.GetNamedItem("name").InnerText = TrustAddress;
XmlNode xmlNode;
xmlNode = xmlTrust.AppendChild(m_xmlSettings.CreateNode(XmlNodeType.Element, "Description", ""));
xmlNode.InnerText = Description;
SaveSettings();
}
catch {}
}
public static void SetTrust(String NewTrustAddress, String OldTrustAddress, String Description)
{
try
{
LoadSettings();
Boolean blnChanged = false;
XmlNode xmlTrust = m_xmlSettings.SelectSingleNode("Settings/Trusts/Trust[@name='" + OldTrustAddress + "']");
if (null == xmlTrust)
{
AddTrust(NewTrustAddress, Description);
return;
}
if (NewTrustAddress != OldTrustAddress && 0 < NewTrustAddress.Length)
{
xmlTrust.Attributes.GetNamedItem("name").InnerText = NewTrustAddress;
blnChanged = true;
}
if (xmlTrust.SelectSingleNode("Description").InnerText != Description)
{
xmlTrust.SelectSingleNode("Description").InnerText = Description;
blnChanged = true;
}
if (blnChanged)
SaveSettings();
}
catch {}
}
public static void DeleteTrust(String TrustAddress)
{
try
{
LoadSettings();
XmlNode xmlTrust = m_xmlSettings.SelectSingleNode("Settings/Trusts/Trust[@name='" + TrustAddress + "']");
if (null == xmlTrust) return;
XmlNode xmlParent = xmlTrust.ParentNode;
xmlParent.RemoveChild(xmlTrust);
SaveSettings();
}
catch {}
}
public static Boolean CheckTrust(String Address)
{
try
{
LoadSettings();
XmlNode xmlNode = m_xmlSettings.SelectSingleNode("Settings/Trusts/Trust[@name = '" + Address +"']");
if (null != xmlNode)
return (true);
return (false);
}
catch
{
return (false);
}
}
public static CTrust [] GetTrusts()
{
try
{
LoadSettings();
XmlNodeList xmlTrustList = m_xmlSettings.SelectNodes("Settings/Trusts/Trust");
if (null == xmlTrustList)
return (null);
CTrust [] xsTrusts = new CTrust[xmlTrustList.Count];
Int16 i = 0;
foreach (XmlNode xmlNode in xmlTrustList)
{
xsTrusts[i++] = new CTrust(xmlNode.Attributes.GetNamedItem("name").InnerText, xmlNode.SelectSingleNode("Description").InnerText);
}
return (xsTrusts);
}
catch
{
return (null);
}
}
#endregion Trusts Functions
#region Crypto Functions
private static Byte [] ConvertStrToHex(String str)
{
Byte [] pbReturn = new Byte[str.Length / 2];
int bHex1, bHex2;
Int32 nCnt = 0;
for (Int32 i = 0; i <pbReturn.Length; i++)
{
bHex1 = CharToHex(str[nCnt++]);
bHex2 = CharToHex(str[nCnt++]);
bHex1 = (bHex1 << 4) & 0xF0;
bHex2 = bHex2 & (Byte) 0x0F;
pbReturn[i] = (Byte) (bHex1 | bHex2);
}
return (pbReturn);
}
private static int CharToHex(Char chChar)
{
switch(chChar)
{
case '0': return (0);
case '1': return (1);
case '2': return (2);
case '3': return (3);
case '4': return (4);
case '5': return (5);
case '6': return (6);
case '7': return (7);
case '8': return (8);
case '9': return (9);
case 'A': return (10);
case 'B': return (11);
case 'C': return (12);
case 'D': return (13);
case 'E': return (14);
case 'F': return (15);
}
return (0);
}
private static String ConvertHexToStr(Byte [] pb)
{
String strReturn = "";
int bHex;
for (Int32 i = 0; i < pb.Length; i++)
{
bHex = pb[i]; bHex >>= 4;
strReturn += HexToChar(bHex);
bHex = pb[i]; bHex &= 0x0F;
strReturn += HexToChar(bHex);
}
return (strReturn);
}
private static Char HexToChar(int bHex)
{
switch(bHex)
{
case 0 : return ('0');
case 1 : return ('1');
case 2 : return ('2');
case 3 : return ('3');
case 4 : return ('4');
case 5 : return ('5');
case 6 : return ('6');
case 7 : return ('7');
case 8 : return ('8');
case 9 : return ('9');
case 10 : return ('A');
case 11 : return ('B');
case 12 : return ('C');
case 13 : return ('D');
case 14 : return ('E');
case 15 : return ('F');
}
return ('0');
}
private static Byte [] GetCryptoKey()
{
String strKey = GetXmlValue("Settings/Encryption/Key", "");
if (0 == strKey.Length)
{
RijndaelManaged xsRijndael = new RijndaelManaged();
xsRijndael.GenerateKey();
strKey = ConvertHexToStr(xsRijndael.Key);
SetXmlValue("Settings/Encryption/Key", strKey);
}
return (ConvertStrToHex(strKey));
}
private static Byte [] GetCryptoIV()
{
String strIV = GetXmlValue("Settings/Encryption/IV", "");
if (0 == strIV.Length)
{
RijndaelManaged xsRijndael = new RijndaelManaged();
xsRijndael.GenerateIV();
strIV = ConvertHexToStr(xsRijndael.IV);
SetXmlValue("Settings/Encryption/IV", strIV);
}
return (ConvertStrToHex(strIV));
}
private static String Encrypt(String Text)
{
RijndaelManaged xsRijndael = new RijndaelManaged();
ICryptoTransform encryptor = xsRijndael.CreateEncryptor(GetCryptoKey(), GetCryptoIV());
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
Byte [] pbText = new Byte[Text.Length + 2];
pbText[0] = 254;
pbText[1] = 255;
for (Int32 i = 2; i < Text.Length + 2; i++)
pbText[i] = (Byte) Text[i - 2];
csEncrypt.Write(pbText, 0, pbText.Length);
csEncrypt.FlushFinalBlock();
Byte [] pbEncrypt = msEncrypt.ToArray();
return (ConvertHexToStr(pbEncrypt));
}
private static String Decrypt(String Message)
{
String strReturn = "";
try
{
Byte [] pbMessage = ConvertStrToHex(Message);
Byte [] pbText = new Byte[pbMessage.Length];
RijndaelManaged xsRijndael = new RijndaelManaged();
ICryptoTransform decryptor = xsRijndael.CreateDecryptor(GetCryptoKey(), GetCryptoIV());
MemoryStream msDecrypt = new MemoryStream(pbMessage);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
csDecrypt.Read(pbText, 0, pbText.Length);
if (255 == pbText[1] && 254 == pbText[0])
{
Int32 i = 2;
while (i < pbText.Length && 0 != pbText[i])
strReturn += (Char) pbText[i++];
}
else
{
strReturn = Message;
}
}
catch
{
strReturn = Message;
}
return (strReturn);
}
#endregion Crypto Functions
}
public enum XBMCBookmarkType
{
Pictures = 0,
Video = 1,
Music = 2
}
public class CUser
{
private String m_strDescription;
public String GetDescription { get { return(m_strDescription); } set { m_strDescription = value; } }
private String m_strUserName;
public String GetUserName { get { return(m_strUserName); } set { m_strUserName = value; } }
public CUser(String UserName, String Description)
{
m_strDescription = Description;
m_strUserName = UserName;
}
}
public class CTrust
{
private String m_strDescription;
public String GetDescription { get { return(m_strDescription); } set { m_strDescription = value; } }
private String m_strTrustAddress;
public String GetTrustAddress { get { return(m_strTrustAddress); } set { m_strTrustAddress = value; } }
public CTrust(String TrustAddress, String Description)
{
m_strDescription = Description;
m_strTrustAddress = TrustAddress;
}
}
public class CIPAddressConverter : StringConverter
{
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return (true);
}
public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
IPAddress [] ipAddressList = CSettings.GetServerIPAddressList;
String [] strIPAddresses = new String[ipAddressList.Length + 1];
strIPAddresses[0] = "Default";
for (Int16 i = 0; i < ipAddressList.Length; i++)
{
strIPAddresses[i + 1] = ipAddressList[i].ToString().Split(':')[0];
}
return new StandardValuesCollection(strIPAddresses);
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return (false);
}
}
}
|