mirror of https://github.com/quasar/Quasar.git
Restructured the code in 'FrmRegValueEditBinary
* Moved the convertion code in 'FrmRegValueEditBinary' to new class 'ByteConverter' * Converted if-statements in 'FrmRegValueEditBinary' to switch-cases
This commit is contained in:
parent
55ca59fa5a
commit
ea2584380a
|
@ -0,0 +1,136 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace xServer.Core.Utilities
|
||||
{
|
||||
public class ByteConverter
|
||||
{
|
||||
private static byte NULL_BYTE = byte.MinValue;
|
||||
|
||||
public static byte[] GetBytes(int value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
public static byte[] GetBytes(long value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
public static byte[] GetBytes(uint value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
public static byte[] GetBytes(ulong value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
public static byte[] GetBytes(string value)
|
||||
{
|
||||
return StringToBytes(value);
|
||||
}
|
||||
|
||||
public static byte[] GetBytes(string[] value)
|
||||
{
|
||||
return StringArrayToBytes(value);
|
||||
}
|
||||
|
||||
public static int ToInt32(byte[] bytes)
|
||||
{
|
||||
return BitConverter.ToInt32(bytes, 0);
|
||||
}
|
||||
|
||||
public static long ToInt64(byte[] bytes)
|
||||
{
|
||||
return BitConverter.ToInt64(bytes, 0);
|
||||
}
|
||||
|
||||
public static uint ToUInt32(byte[] bytes)
|
||||
{
|
||||
return BitConverter.ToUInt32(bytes, 0);
|
||||
}
|
||||
|
||||
public static ulong ToUInt64(byte[] bytes)
|
||||
{
|
||||
return BitConverter.ToUInt64(bytes, 0);
|
||||
}
|
||||
|
||||
public static string ToString(byte[] bytes)
|
||||
{
|
||||
return BytesToString(bytes);
|
||||
}
|
||||
|
||||
public static string[] ToStringArray(byte[] bytes)
|
||||
{
|
||||
return BytesToStringArray(bytes);
|
||||
}
|
||||
|
||||
private static byte[] GetNullBytes()
|
||||
{
|
||||
//Null bytes: 00 00
|
||||
return new byte[] { NULL_BYTE, NULL_BYTE };
|
||||
}
|
||||
|
||||
private static byte[] StringToBytes(string value)
|
||||
{
|
||||
byte[] bytes = new byte[value.Length * sizeof(char)];
|
||||
Buffer.BlockCopy(value.ToCharArray(), 0, bytes, 0, bytes.Length);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
private static byte[] StringArrayToBytes(string[] strings)
|
||||
{
|
||||
List<byte> bytes = new List<byte>();
|
||||
|
||||
foreach(string str in strings)
|
||||
{
|
||||
bytes.AddRange(StringToBytes(str));
|
||||
bytes.AddRange(GetNullBytes());
|
||||
}
|
||||
|
||||
return bytes.ToArray();
|
||||
}
|
||||
|
||||
private static string BytesToString(byte[] bytes)
|
||||
{
|
||||
int nrChars = (int)Math.Ceiling((float)bytes.Length / (float)sizeof(char));
|
||||
char[] chars = new char[nrChars];
|
||||
Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
|
||||
return new string(chars);
|
||||
}
|
||||
|
||||
private static string[] BytesToStringArray(byte[] bytes)
|
||||
{
|
||||
List<string> strings = new List<string>();
|
||||
|
||||
int i = 0;
|
||||
StringBuilder strBuilder = new StringBuilder(bytes.Length);
|
||||
while (i < bytes.Length)
|
||||
{
|
||||
//Holds the number of nulls (3 nulls indicated end of a string)
|
||||
int nullcount = 0;
|
||||
while (i < bytes.Length && nullcount < 3)
|
||||
{
|
||||
if (bytes[i] == NULL_BYTE)
|
||||
{
|
||||
nullcount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
strBuilder.Append(Convert.ToChar(bytes[i]));
|
||||
nullcount = 0;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
strings.Add(strBuilder.ToString());
|
||||
strBuilder.Clear();
|
||||
}
|
||||
|
||||
return strings.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,14 +1,15 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using Microsoft.Win32;
|
||||
using xServer.Core.Networking;
|
||||
using xServer.Core.Registry;
|
||||
using xServer.Core.Utilities;
|
||||
|
||||
namespace xServer.Forms
|
||||
{
|
||||
|
@ -36,37 +37,33 @@ namespace xServer.Forms
|
|||
|
||||
this.valueNameTxtBox.Text = RegValueHelper.GetName(value.Name);
|
||||
|
||||
if (value.Data == null){
|
||||
if(value.Data == null)
|
||||
{
|
||||
hexEditor.HexTable = new byte[] { };
|
||||
}
|
||||
else if (value.Kind == Microsoft.Win32.RegistryValueKind.Binary)
|
||||
{
|
||||
hexEditor.HexTable = (byte[])value.Data;
|
||||
}
|
||||
else if (value.Kind == Microsoft.Win32.RegistryValueKind.DWord)
|
||||
{
|
||||
hexEditor.HexTable = BitConverter.GetBytes((uint)(int)value.Data);
|
||||
}
|
||||
else if (value.Kind == Microsoft.Win32.RegistryValueKind.QWord)
|
||||
{
|
||||
hexEditor.HexTable = BitConverter.GetBytes((ulong)(long)value.Data);
|
||||
}
|
||||
else if (value.Kind == Microsoft.Win32.RegistryValueKind.String || value.Kind == Microsoft.Win32.RegistryValueKind.ExpandString)
|
||||
{
|
||||
//Convert string to bytes
|
||||
byte[] bytes = new byte[value.Data.ToString().Length * sizeof(char)];
|
||||
Buffer.BlockCopy(value.Data.ToString().ToCharArray(), 0, bytes, 0, bytes.Length);
|
||||
hexEditor.HexTable = bytes;
|
||||
}
|
||||
else if (value.Kind == Microsoft.Win32.RegistryValueKind.MultiString)
|
||||
{
|
||||
byte[] bytes = MultiStringToBytes((string[])value.Data);
|
||||
hexEditor.HexTable = bytes;
|
||||
else {
|
||||
switch(value.Kind)
|
||||
{
|
||||
case RegistryValueKind.Binary:
|
||||
hexEditor.HexTable = (byte[])value.Data;
|
||||
break;
|
||||
case RegistryValueKind.DWord:
|
||||
hexEditor.HexTable = ByteConverter.GetBytes((uint)(int)value.Data);
|
||||
break;
|
||||
case RegistryValueKind.QWord:
|
||||
hexEditor.HexTable = ByteConverter.GetBytes((ulong)(long)value.Data);
|
||||
break;
|
||||
case RegistryValueKind.MultiString:
|
||||
hexEditor.HexTable = ByteConverter.GetBytes((string[])value.Data);
|
||||
break;
|
||||
case RegistryValueKind.String:
|
||||
case RegistryValueKind.ExpandString:
|
||||
hexEditor.HexTable = ByteConverter.GetBytes(value.Data.ToString());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#region Help function
|
||||
|
||||
private object GetData()
|
||||
{
|
||||
byte[] bytes = hexEditor.HexTable;
|
||||
|
@ -74,128 +71,41 @@ namespace xServer.Forms
|
|||
{
|
||||
try
|
||||
{
|
||||
if (_value.Kind == Microsoft.Win32.RegistryValueKind.Binary)
|
||||
switch(_value.Kind)
|
||||
{
|
||||
return bytes;
|
||||
}
|
||||
else if (_value.Kind == Microsoft.Win32.RegistryValueKind.DWord)
|
||||
{
|
||||
uint unsignedValue = BitConverter.ToUInt32(hexEditor.HexTable, 0);
|
||||
return (int)unsignedValue;
|
||||
}
|
||||
else if (_value.Kind == Microsoft.Win32.RegistryValueKind.QWord)
|
||||
{
|
||||
ulong unsignedValue = BitConverter.ToUInt64(hexEditor.HexTable, 0);
|
||||
return (long)unsignedValue;
|
||||
}
|
||||
else if (_value.Kind == Microsoft.Win32.RegistryValueKind.String || _value.Kind == Microsoft.Win32.RegistryValueKind.ExpandString)
|
||||
{
|
||||
//Use ceiling function to make sure that the bytes fit in the char
|
||||
int nrChars = (int)Math.Ceiling((float)bytes.Length / (float)sizeof(char));
|
||||
char[] chars = new char[nrChars];
|
||||
Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
|
||||
return new string(chars);
|
||||
}
|
||||
else if (_value.Kind == Microsoft.Win32.RegistryValueKind.MultiString)
|
||||
{
|
||||
string[] strings = BytesToMultiString(hexEditor.HexTable);
|
||||
return strings;
|
||||
case RegistryValueKind.Binary:
|
||||
return bytes;
|
||||
case RegistryValueKind.DWord:
|
||||
return (int)ByteConverter.ToUInt32(bytes);
|
||||
case RegistryValueKind.QWord:
|
||||
return (long)ByteConverter.ToUInt64(bytes);
|
||||
case RegistryValueKind.MultiString:
|
||||
return ByteConverter.ToStringArray(bytes);
|
||||
case RegistryValueKind.String:
|
||||
case RegistryValueKind.ExpandString:
|
||||
return ByteConverter.ToString(bytes);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
string msg = INVALID_BINARY_ERROR;
|
||||
ShowWarning(msg, "Warning");
|
||||
return null;
|
||||
ShowWarning(INVALID_BINARY_ERROR, "Warning");
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Ok and Cancel button
|
||||
|
||||
private void okButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
object valueData = GetData();
|
||||
if (valueData != null)
|
||||
{
|
||||
new xServer.Core.Packets.ServerPackets.DoChangeRegistryValue(_keyPath, new RegValueData(_value.Name, _value.Kind, valueData)).Execute(_connectClient);
|
||||
}
|
||||
else
|
||||
{
|
||||
DialogResult = DialogResult.None;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Misc
|
||||
|
||||
private void ShowWarning(string msg, string caption)
|
||||
{
|
||||
MessageBox.Show(msg, caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the given string array to
|
||||
/// a byte array.
|
||||
/// </summary>
|
||||
/// <param name="strings"> string array</param>
|
||||
/// <returns></returns>
|
||||
private byte[] MultiStringToBytes(string[] strings)
|
||||
{
|
||||
List<byte> ret = new List<byte>();
|
||||
|
||||
foreach (string str in strings)
|
||||
{
|
||||
byte[] bytes = new byte[str.Length * sizeof(char)];
|
||||
Buffer.BlockCopy(str.ToString().ToCharArray(), 0, bytes, 0, bytes.Length);
|
||||
ret.AddRange(bytes);
|
||||
//Add nullbytes
|
||||
ret.Add(byte.MinValue);
|
||||
ret.Add(byte.MinValue);
|
||||
}
|
||||
return ret.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a array of bytes to
|
||||
/// a string array
|
||||
/// </summary>
|
||||
/// <param name="bytes"> array of bytes</param>
|
||||
/// <returns></returns>
|
||||
private string[] BytesToMultiString(byte[] bytes)
|
||||
{
|
||||
List<string> ret = new List<string>();
|
||||
|
||||
int i = 0;
|
||||
while (i < bytes.Length)
|
||||
{
|
||||
//Holds the number of nulls (3 nulls indicated end of a string)
|
||||
int nullcount = 0;
|
||||
string str = "";
|
||||
while (i < bytes.Length && nullcount < 3)
|
||||
{
|
||||
//Null byte
|
||||
if (bytes[i] == byte.MinValue)
|
||||
{
|
||||
nullcount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
str += Convert.ToChar(bytes[i]);
|
||||
nullcount = 0;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
ret.Add(str);
|
||||
}
|
||||
|
||||
return ret.ToArray();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
|
@ -186,6 +186,7 @@
|
|||
<Compile Include="Core\Registry\RegSeekerMatch.cs" />
|
||||
<Compile Include="Core\Registry\RegValueData.cs" />
|
||||
<Compile Include="Core\Registry\RegValueHelper.cs" />
|
||||
<Compile Include="Core\Utilities\ByteConverter.cs" />
|
||||
<Compile Include="Core\Utilities\FrameCounter.cs" />
|
||||
<Compile Include="Core\Helper\NativeMethodsHelper.cs" />
|
||||
<Compile Include="Core\Helper\PlatformHelper.cs" />
|
||||
|
|
Loading…
Reference in New Issue