Skip to content

Commit

Permalink
Fixed overflow problem
Browse files Browse the repository at this point in the history
Added tests
  • Loading branch information
alan-null committed Jan 16, 2019
1 parent e45338c commit c20ba59
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 34 deletions.
36 changes: 18 additions & 18 deletions L2Net.PacketConverter.Core/Convert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,34 @@ namespace L2Net.PacketConverter.Core
{
public static class Convert
{
public static string FromByteToHex(string i)
public static string FromByteToHex(byte i)
{
return FromNoToHex(System.Convert.ToInt32(i), 1);
return FromNumberToHex(i, 1);
}

public static string FromInt16ToHex(int i)
public static string FromInt16ToHex(Int16 i)
{
return FromNoToHex(i, 2);
return FromNumberToHex(i, 2);
}

public static string FromInt32ToHex(int i)
public static string FromInt32ToHex(Int32 i)
{
return FromNoToHex(i, 4);
return FromNumberToHex(i, 4);
}

public static string FromInt64ToHex(long i)
{
return FromNoToHex(i, 8);
return FromNumberToHex(i, 8);
}

private static string FromNoToHex(long i, int size)
private static string FromNumberToHex(long i, int size)
{
return FromNoToHex(System.Convert.ToByte(i), size);
return FromNumberToHex(BitConverter.GetBytes(i), size);
}

private static string FromNoToHex(byte i, int size)
private static string FromNumberToHex(byte[] i, int size)
{
var a = new byte[size];
a[0] = i;
return BitConverter.ToString(a).NormalizeString();
return BitConverter.ToString(i.Take(size).ToArray()).NormalizeString();
}

public static byte FromHexToByte(string hex)
Expand All @@ -46,20 +44,22 @@ public static byte FromHexToByte(string hex)

public static int FromHexToInt16(string hex)
{
return BitConverter.ToInt16(FromHexToNo(hex, 4), 0);
return BitConverter.ToInt16(FromHexToBytes(hex, 4), 0) is Int16 n && n == -1 ? int.MaxValue : n;
}

public static int FromHexToInt32(string hex)
{
return BitConverter.ToInt32(FromHexToNo(hex, 8), 0);
return
BitConverter.ToInt32(FromHexToBytes(hex, 8), 0) is Int32 n && n == -1 ? Int32.MaxValue : n;
}

public static long FromHexToInt64(string hex)
{
return BitConverter.ToInt64(FromHexToNo(hex, 16), 0);
return
BitConverter.ToInt64(FromHexToBytes(hex, 16), 0) is Int64 n && n == -1 ? Int64.MaxValue : n;
}

public static byte[] FromHexToNo(string hex, int size)
public static byte[] FromHexToBytes(string hex, int size)
{
hex = hex.RemoveSeparators();
if (hex.Length < size)
Expand All @@ -71,7 +71,7 @@ public static byte[] FromHexToNo(string hex, int size)
}
hex = sb.ToString();
}
byte[] raw = new byte[size / 2];
byte[] raw = new byte[size];
for (int i = 0; i < raw.Length / 2; i++)
{
raw[i] = System.Convert.ToByte(hex.Substring(i * 2, 2), 16);
Expand Down
29 changes: 17 additions & 12 deletions L2Net.PacketConverter.Tests/ConvertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,33 @@ namespace L2Net.PacketConverter.Tests
[TestClass]
public class ConvertTests
{
[DataRow("52", "82")]
public void FromByteToHexTest(string expected, string input)
[DataRow("52", 82)]
public void FromByteToHexTest(string expected, byte input)
{
Assert.AreEqual(expected, Convert.FromByteToHex(input));
}

[TestMethod]
public void FromInt16ToHexTest()
[DataRow("52 00", 82)]
[DataRow("FF FF", 99999999)]
public void FromInt16ToHexTest(string expected, byte bytee)
{
Assert.AreEqual("52 00", Convert.FromInt16ToHex(82));
Assert.AreEqual(expected, Convert.FromInt16ToHex(bytee));
}

[TestMethod]
public void FromInt32ToHexTest()
[DataRow("52 00 00 00", 82)]
[DataRow("52 00 6F 00", 7274578)]
[DataRow("FF FF FF F7", 4294967295)]
public void FromInt32ToHexTest(string expected, int int32)
{
Assert.AreEqual("52 00 00 00", Convert.FromInt32ToHex(82));
Assert.AreEqual(expected, Convert.FromInt32ToHex(int32));
}

[TestMethod]
public void FromInt64ToHexTest()
[DataRow("52 00 00 00 00 00 00 00", 7274578)]
[DataRow("FF FF C0 6F F2 86 23 00", 9999999999999999)]
[DataRow("FF FF FF FF FF FF FF F7", 9223372036854775807)]
public void FromInt64ToHexTest(string expected, System.Int64 int64)
{
Assert.AreEqual("52 00 00 00 00 00 00 00", Convert.FromInt64ToHex(82));
Assert.AreEqual(expected, Convert.FromInt64ToHex(int64));
}

[DataTestMethod]
Expand All @@ -48,7 +53,7 @@ public void FromHexToInt16Test(int expected, string hex)

[DataTestMethod]
[DataRow(76, "4C")]
[DataRow(76, "4C 00 32 00 4E 00 65 00 74 00 52 00 6F 00 63 00 6B 00")]
[DataRow(3276876, "4C 00 32 00 4E 00 65 00 74 00 52 00 6F 00 63 00 6B 00")]
public void FromHexToInt32Test(int expected, string hex)
{
Assert.AreEqual(expected, Convert.FromHexToInt32(hex));
Expand Down
26 changes: 22 additions & 4 deletions L2Net.PacketConverter.UI/PacketConverterForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,20 @@ private void rtbxPlain_KeyUp(object sender, KeyEventArgs e)
switch (Mode)
{
case ConvertMode.Byte:
value = Convert.FromByteToHex(rtbxPlain.Text);
var bytee = GetIntSafe(rtbxPlain.Text, System.Convert.ToByte, byte.MaxValue);
value = Convert.FromByteToHex(bytee);
break;
case ConvertMode.Int16:
value = Convert.FromInt16ToHex(System.Convert.ToInt16(rtbxPlain.Text));
var int16 = GetIntSafe(rtbxPlain.Text, System.Convert.ToInt16, Int16.MaxValue);
value = Convert.FromInt16ToHex(int16);
break;
case ConvertMode.Int32:
value = Convert.FromInt32ToHex(System.Convert.ToInt32(rtbxPlain.Text));
var int32 = GetIntSafe(rtbxPlain.Text, System.Convert.ToInt32, int.MaxValue);
value = Convert.FromInt32ToHex(int32);
break;
case ConvertMode.Int64:
value = Convert.FromInt64ToHex(System.Convert.ToInt64(rtbxPlain.Text));
var int64 = GetIntSafe(rtbxPlain.Text, System.Convert.ToInt64, Int64.MaxValue);
value = Convert.FromInt64ToHex(int64);
break;
case ConvertMode.String:
value = Convert.FromStringToHex(rtbxPlain.Text);
Expand All @@ -78,6 +82,20 @@ private void rtbxPlain_KeyUp(object sender, KeyEventArgs e)
LastSender = sender as RichTextBox;
}

private T GetIntSafe<T>(string text, Func<string, T> fun, T fallback)
{
T int16;
try
{
int16 = fun.Invoke(text);
}
catch (OverflowException)
{
int16 = fallback;
}
return int16;
}

private void rbtn_CheckedChanged(object sender, EventArgs e)
{
switch ((sender as RadioButton).Name)
Expand Down

0 comments on commit c20ba59

Please sign in to comment.