Skip to content

Commit

Permalink
[MapleLib] "okay GPT, optimise this shit for me"
Browse files Browse the repository at this point in the history
  • Loading branch information
lastbattle committed Dec 12, 2022
1 parent dfffdbd commit 90b606e
Show file tree
Hide file tree
Showing 20 changed files with 482 additions and 511 deletions.
30 changes: 7 additions & 23 deletions MapleLib/Helpers/ByteUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,16 @@ public static class ByteUtils
{
public static bool CompareBytearrays(byte[] a, byte[] b)
{
if (a.Length != b.Length)
return false;
int i = 0;
foreach (byte c in a)
{
if (c != b[i])
return false;
i++;
}
return true;
return a.Length == b.Length && a.SequenceEqual(b);
}

public static byte[] IntegerToLittleEndian(int data)
{
byte[] b = new byte[4];
b[0] = (byte)data;
b[1] = (byte)(((uint)data >> 8) & 0xFF);
b[2] = (byte)(((uint)data >> 16) & 0xFF);
b[3] = (byte)(((uint)data >> 24) & 0xFF);
byte[] b = BitConverter.GetBytes(data);
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(b);
}
return b;
}

Expand Down Expand Up @@ -90,14 +81,7 @@ public static string BytesToHex(byte[] bytes, string header = "")
/// <returns>true if <paramref name="c"/>is a hexadecimal digit; otherwise, false.</returns>
public static bool IsHexDigit(char c)
{
if (('0' <= c && c <= '9') ||
('A' <= c && c <= 'F') ||
('a' <= c && c <= 'f') ||
c == '*') //we allow wildcards
{
return true;
}
return false;
return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f') || (c == '*');
}

/// <summary>
Expand Down
37 changes: 35 additions & 2 deletions MapleLib/MapleCryptoLib/MapleCrypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.*/

using System;
using System.Numerics;

namespace MapleLib.MapleCryptoLib
{
Expand Down Expand Up @@ -224,7 +225,39 @@ public static byte[] MultiplyBytes(byte[] input, int count, int mult)
}
return ret;
}
#endregion

}
public static byte[] MultiplyBytes_SIMD(byte[] input, int count, int mult)
{
byte[] ret = new byte[count * mult];
int simdWidth = Vector<byte>.Count;

// Process input in blocks of simdWidth elements
int blockCount = count / simdWidth;
for (int i = 0; i < blockCount; i++)
{
// Load simdWidth elements from input into a vector
Vector<byte> vec = new Vector<byte>(input, i * simdWidth);

// Replicate the vector mult times and store it in the output
for (int j = 0; j < mult; j++)
{
vec.CopyTo(ret, (i * simdWidth * mult) + (j * simdWidth));
}
}

// Process any remaining elements
int remainder = count % simdWidth;
if (remainder > 0)
{
for (int x = 0; x < ret.Length; x++)
{
ret[x] = input[x % count];
}
}

return ret;
}
#endregion

}
}
66 changes: 31 additions & 35 deletions MapleLib/MapleCryptoLib/MapleCustomEncryption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.*/

using System.Numerics;

namespace MapleLib.MapleCryptoLib
{
/// <summary>
Expand Down Expand Up @@ -106,40 +108,34 @@ public static void Decrypt(byte[] data)
}
}

/// <summary>
/// Rolls a byte left
/// </summary>
/// <param name="val">input byte to roll</param>
/// <param name="num">amount of bits to roll</param>
/// <returns>The left rolled byte</returns>
public static byte rol(byte val, int num)
{
int highbit;
for (int i = 0; i < num; i++)
{
highbit = ((val & 0x80) != 0 ? 1 : 0);
val <<= 1;
val |= (byte)highbit;
}
return val;
}
/// <summary>
/// Rolls a byte left
/// </summary>
/// <param name="val">input byte to roll</param>
/// <param name="num">amount of bits to roll</param>
/// <returns>The left rolled byte</returns>
public static byte rol(byte val, int num)
{
for (int i = 0; i < num; i++)
{
val = (byte)((val << 1) | ((val >> 7) & 1));
}
return val;
}

/// <summary>
/// Rolls a byte right
/// </summary>
/// <param name="val">input byte to roll</param>
/// <param name="num">amount of bits to roll</param>
/// <returns>The right rolled byte</returns>
public static byte ror(byte val, int num)
{
int lowbit;
for (int i = 0; i < num; i++)
{
lowbit = ((val & 1) != 0 ? 1 : 0);
val >>= 1;
val |= (byte)(lowbit << 7);
}
return val;
}
}
/// <summary>
/// Rolls a byte right
/// </summary>
/// <param name="val">input byte to roll</param>
/// <param name="num">amount of bits to roll</param>
/// <returns>The right rolled byte</returns>
public static byte ror(byte val, int num)
{
for (int i = 0; i < num; i++)
{
val = (byte)((val >> 1) | ((val & 1) << 7));
}
return val;
}
}
}
43 changes: 16 additions & 27 deletions MapleLib/PacketLib/HexEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,12 @@ public class HexEncoding
/// <returns>Char is a hex digit</returns>
public static bool IsHexDigit(Char c)
{
int numChar;
int numA = Convert.ToInt32('A');
int num1 = Convert.ToInt32('0');
c = Char.ToUpper(c);
numChar = Convert.ToInt32(c);
if (numChar >= numA && numChar < (numA + 6))
return true;
if (numChar >= num1 && numChar < (num1 + 10))
return true;
return false;
int numChar = Convert.ToInt32(c);

return (numChar >= numA && numChar < (numA + 6)) || (numChar >= num1 && numChar < (num1 + 10));
}

/// <summary>
Expand Down Expand Up @@ -84,9 +80,9 @@ public static byte[] GetBytes(string hexString)
int j = 0;
for (int i = 0; i < bytes.Length; i++)
{
hex = new String(new Char[] { newString[j], newString[j + 1] });
hex = new string(new Char[] { newString[j], newString[j + 1] });
bytes[i] = HexToByte(hex);
j = j + 2;
j += 2;
}
return bytes;
}
Expand All @@ -96,22 +92,15 @@ public static byte[] GetBytes(string hexString)
/// </summary>
/// <param name="bytes">Bytes to convert to ASCII</param>
/// <returns>The byte array as an ASCII string</returns>
public static String ToStringFromAscii(byte[] bytes)
{
char[] ret = new char[bytes.Length];
for (int x = 0; x < bytes.Length; x++)
{
if (bytes[x] < 32 && bytes[x] >= 0)
{
ret[x] = '.';
}
else
{
int chr = ((short)bytes[x]) & 0xFF;
ret[x] = (char)chr;
}
}
return new String(ret);
}
}
public static string ToStringFromAscii(byte[] bytes)
{
char[] ret = new char[bytes.Length];
for (int x = 0; x < bytes.Length; x++)
{
// Use a ternary operator to avoid an if statement
ret[x] = (bytes[x] < 32 && bytes[x] >= 0) ? '.' : (char)((short)bytes[x] & 0xFF);
}
return new string(ret);
}
}
}
8 changes: 8 additions & 0 deletions MapleLib/PacketLib/HexTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,13 @@ public static String ToString(this PacketWriter writer)
}
return hexed.ToString();
}

public static string ByteArrayToString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 3);
foreach (byte b in ba)
hex.AppendFormat("{0:x2} ", b);
return hex.ToString();
}
}
}

0 comments on commit 90b606e

Please sign in to comment.