Replace Utilities with modern .NET function

This commit is contained in:
Deterous
2023-11-01 23:21:31 +13:00
parent be713935b6
commit 894b344b2e
2 changed files with 3 additions and 61 deletions
+3 -3
View File
@@ -195,7 +195,7 @@ namespace LibIRD
if (discKeyStr.Length != 32) if (discKeyStr.Length != 32)
throw new InvalidDataException("Unexpected Disc Key in .getkey.log"); throw new InvalidDataException("Unexpected Disc Key in .getkey.log");
// Convert Disc Key to byte array // Convert Disc Key to byte array
discKey = Utilities.HexToBytes(discKeyStr); discKey = Convert.FromHexString(discKeyStr);
// Read Disc ID // Read Disc ID
byte[] discID; byte[] discID;
@@ -210,7 +210,7 @@ namespace LibIRD
// Replace X's in Disc ID with 00000001 // Replace X's in Disc ID with 00000001
discIDStr = discIDStr[..24] + "00000001"; discIDStr = discIDStr[..24] + "00000001";
// Convert Disc ID to byte array // Convert Disc ID to byte array
discID = Utilities.HexToBytes(discIDStr); discID = Convert.FromHexString(discIDStr);
// Look for PIC in log // Look for PIC in log
byte[] discPIC; byte[] discPIC;
@@ -225,7 +225,7 @@ namespace LibIRD
if (discPICStr.Length != 256) if (discPICStr.Length != 256)
throw new InvalidDataException("Unexpected PIC in .getkey.log"); throw new InvalidDataException("Unexpected PIC in .getkey.log");
// Convert PIC to byte array // Convert PIC to byte array
discPIC = Utilities.HexToBytes(discPICStr[..230]); discPIC = Convert.FromHexString(discPICStr[..230]);
// Double check for warnings in .getkey.log // Double check for warnings in .getkey.log
while ((line = sr.ReadLine()) != null && line.Trim().StartsWith("WARNING") == false && line.Trim().StartsWith("SUCCESS") == false) while ((line = sr.ReadLine()) != null && line.Trim().StartsWith("WARNING") == false && line.Trim().StartsWith("SUCCESS") == false)
-58
View File
@@ -1,58 +0,0 @@
using System;
namespace LibIRD
{
internal class Utilities
{
// Helper function to convert a hex string to a byte array
// Original source: https://codereview.stackexchange.com/a/53846
private static int HexToInt(char c)
{
return c switch
{
'0' => 0,
'1' => 1,
'2' => 2,
'3' => 3,
'4' => 4,
'5' => 5,
'6' => 6,
'7' => 7,
'8' => 8,
'9' => 9,
'a' or 'A' => 10,
'b' or 'B' => 11,
'c' or 'C' => 12,
'd' or 'D' => 13,
'e' or 'E' => 14,
'f' or 'F' => 15,
_ => throw new FormatException("Unrecognized hex char " + c),
};
}
private static readonly byte[,] ByteLookup = new byte[,]
{
// low nibble
{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
// high nibble
{0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0}
};
internal static byte[] HexToBytes(string input)
{
var result = new byte[(input.Length + 1) >> 1];
int lastcell = result.Length - 1;
int lastchar = input.Length - 1;
// count up in characters, but inside the loop will
// reference from the end of the input/output.
for (int i = 0; i < input.Length; i++)
{
// i >> 1 - (i / 2) gives the result byte offset from the end
// i & 1 - 1 if it is high-nibble, 0 for low-nibble.
result[lastcell - (i >> 1)] |= ByteLookup[i & 1, HexToInt(input[lastchar - i])];
}
return result;
}
}
}