diff --git a/TACTLib/Core/BLTEDecoder.cs b/TACTLib/Core/BLTEDecoder.cs index f4346a7..9dfc92c 100644 --- a/TACTLib/Core/BLTEDecoder.cs +++ b/TACTLib/Core/BLTEDecoder.cs @@ -92,7 +92,8 @@ public static unsafe byte[] Decode(ClientHandler client, Span src) { private static void HandleDataBlock(ClientHandler client, Span inputData, Span outputData, int blockIndex) { - switch ((char)SpanHelper.ReadByte(ref inputData)) { + var blockType = (char)SpanHelper.ReadByte(ref inputData); + switch (blockType) { case 'E': // E (encrypted) var decrypted = Decrypt(client, inputData, blockIndex); HandleDataBlock(client, decrypted, outputData, blockIndex); @@ -107,15 +108,15 @@ private static void HandleDataBlock(ClientHandler client, Span inputData, case 'F': // F (frame, recursive) throw new BLTEDecoderException(null, "DecoderFrame not implemented"); default: - throw new BLTEDecoderException(null, "unknown BLTE block type {0} (0x{1:X2})!", (char) inputData[0], inputData[0]); + throw new BLTEDecoderException(null, $"unknown BLTE block type {blockType} (0x{(byte)blockType:X2})!"); } } private static Span Decrypt(ClientHandler client, Span data, int blockIndex) { var keyNameSize = SpanHelper.ReadByte(ref data); - if (keyNameSize == 0 || keyNameSize != 8) - throw new BLTEDecoderException(null, "keyNameSize == 0 || keyNameSize != 8"); + if (keyNameSize != 8) + throw new BLTEDecoderException(null, $"keyNameSize != 8. got {keyNameSize}"); var keyNameData = SpanHelper.Advance(ref data, keyNameSize); var keyName = BinaryPrimitives.ReadUInt64LittleEndian(keyNameData); @@ -131,7 +132,7 @@ private static Span Decrypt(ClientHandler client, Span data, int blo var encType = SpanHelper.ReadByte(ref data); if (encType != EncryptionSalsa20 && encType != EncryptionArc4) // 'S' or 'A' - throw new BLTEDecoderException(null, "encType != ENCRYPTION_SALSA20 && encType != ENCRYPTION_ARC4"); + throw new BLTEDecoderException(null, $"encType != ENCRYPTION_SALSA20 && encType != ENCRYPTION_ARC4. got {(char)encType}"); // expand to 8 bytes Span iv = stackalloc byte[8]; @@ -149,7 +150,7 @@ private static Span Decrypt(ClientHandler client, Span data, int blo return data; } else { - throw new BLTEDecoderException(null, $"encType {encType} not implemented"); + throw new BLTEDecoderException(null, $"encType {(char)encType} not implemented"); } } diff --git a/TACTLib/Core/Salsa20.cs b/TACTLib/Core/Salsa20.cs index 125be98..6008c70 100644 --- a/TACTLib/Core/Salsa20.cs +++ b/TACTLib/Core/Salsa20.cs @@ -1,8 +1,8 @@ using System; using System.Numerics; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; using System.Security.Cryptography; +using CommunityToolkit.HighPerformance; namespace TACTLib.Core { @@ -52,7 +52,7 @@ public Salsa20(ReadOnlySpan key, ReadOnlySpan iv) { public void Transform(ReadOnlySpan input, Span output) { var hashOutput = new UIntArray(); - var hashOutputBytes = MemoryMarshal.Cast(hashOutput); + var hashOutputBytes = ((ReadOnlySpan)hashOutput).AsBytes(); while (input.Length > 0) { Hash(ref hashOutput, ref m_state);