mirror of
https://github.com/Thealexbarney/LibHac.git
synced 2024-11-14 10:49:41 +01:00
Add some "unchecked" blocks
Try to identify places where integer overflow is expected and mark them as "unchecked"
This commit is contained in:
parent
085511660d
commit
d116903892
9 changed files with 53 additions and 39 deletions
|
@ -566,7 +566,7 @@ public static class ExternalKeyReader
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static bool IsValidNameChar(char c)
|
||||
{
|
||||
return (c | 0x20u) - 'a' <= 'z' - 'a' || (uint)(c - '0') <= 9 || c == '_';
|
||||
return unchecked((c | 0x20u) - 'a' <= 'z' - 'a' || (uint)(c - '0') <= 9 || c == '_');
|
||||
}
|
||||
|
||||
private static bool TryGetKeyInfo(out SpecificKeyInfo info, List<KeyInfo> keyList, ReadOnlySpan<char> keyName)
|
||||
|
@ -584,4 +584,4 @@ public static class ExternalKeyReader
|
|||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -147,7 +147,7 @@ public ref struct U8StringBuilder
|
|||
// Remove possible sign extension if needed
|
||||
if (mask == 'x' || mask == 'X')
|
||||
{
|
||||
value &= (long)mask;
|
||||
value &= unchecked((long)mask);
|
||||
}
|
||||
|
||||
int bytesWritten;
|
||||
|
|
|
@ -31,8 +31,8 @@ public struct AesCtrModeNi
|
|||
ref Vector128<byte> inBlock = ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(input));
|
||||
ref Vector128<byte> outBlock = ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(output));
|
||||
|
||||
Vector128<byte> byteSwapMask = Vector128.Create((ulong)0x706050403020100, 0x8090A0B0C0D0E0F).AsByte();
|
||||
var inc = Vector128.Create((ulong)0, 1);
|
||||
Vector128<byte> byteSwapMask = Vector128.Create(0x706050403020100ul, 0x8090A0B0C0D0E0Ful).AsByte();
|
||||
var inc = Vector128.Create(0ul, 1ul);
|
||||
|
||||
Vector128<byte> iv = Iv;
|
||||
Vector128<ulong> bSwappedIv = Ssse3.Shuffle(iv, byteSwapMask).AsUInt64();
|
||||
|
@ -122,4 +122,4 @@ public struct AesCtrModeNi
|
|||
|
||||
Unsafe.ReadUnaligned<Vector128<byte>>(ref counter[0]);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -155,12 +155,15 @@ public struct AesXtsMode
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void Gf128Mul(ref Buffer16 buffer)
|
||||
{
|
||||
Span<ulong> b = buffer.AsSpan<ulong>();
|
||||
unchecked
|
||||
{
|
||||
Span<ulong> b = buffer.AsSpan<ulong>();
|
||||
|
||||
ulong tt = (ulong)((long)b[1] >> 63) & 0x87;
|
||||
ulong tt = (ulong)((long)b[1] >> 63) & 0x87;
|
||||
|
||||
b[1] = (b[1] << 1) | (b[0] >> 63);
|
||||
b[0] = (b[0] << 1) ^ tt;
|
||||
b[1] = (b[1] << 1) | (b[0] >> 63);
|
||||
b[0] = (b[0] << 1) ^ tt;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
@ -173,4 +176,4 @@ public struct AesXtsMode
|
|||
outputS[0] = input1S[0] ^ input2S[0];
|
||||
outputS[1] = input1S[1] ^ input2S[1];
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2166,7 +2166,7 @@ internal class SaveDataFileSystemService : ISaveDataTransferCoreInterface, ISave
|
|||
|
||||
private bool IsStaticSaveDataIdValueRange(ulong id)
|
||||
{
|
||||
return (long)id < 0;
|
||||
return unchecked((long)id) < 0;
|
||||
}
|
||||
|
||||
private void ModifySaveDataExtraData(ref SaveDataExtraData currentExtraData, in SaveDataExtraData extraData,
|
||||
|
|
|
@ -289,7 +289,9 @@ public readonly struct Result : IEquatable<Result>
|
|||
/// The <see cref="Result"/> representing the start of this result range.
|
||||
/// If returning a <see cref="Result"/> from a function, use <see cref="Log"/> instead.
|
||||
/// </summary>
|
||||
public Result Value => new Result((BaseType)_value);
|
||||
// The conversion to int won't remove the part of the value containing the description end, but that doesn't
|
||||
// matter because the constructor for Result will take care of that.
|
||||
public Result Value => new Result(unchecked((BaseType)_value));
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the range of this <see cref="Result.Base"/> includes the provided <see cref="Result"/>.
|
||||
|
@ -307,7 +309,7 @@ public readonly struct Result : IEquatable<Result>
|
|||
}
|
||||
|
||||
return result.Module == Module &&
|
||||
result.Description - DescriptionRangeStart <= DescriptionRangeEnd - DescriptionRangeStart;
|
||||
unchecked(result.Description - DescriptionRangeStart <= DescriptionRangeEnd - DescriptionRangeStart);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -356,7 +358,7 @@ public readonly struct Result : IEquatable<Result>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static BaseType GetBitsValueLong(ulong value, int bitsOffset, int bitsCount)
|
||||
{
|
||||
return (BaseType)(value >> bitsOffset) & ~(~default(BaseType) << bitsCount);
|
||||
return unchecked((BaseType)(value >> bitsOffset)) & ~(~default(BaseType) << bitsCount);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
@ -391,7 +393,7 @@ public readonly struct Result : IEquatable<Result>
|
|||
public BaseType DescriptionRangeStart => GetBitsValueLong(_value, DescriptionBitsOffset, DescriptionBitsCount);
|
||||
public BaseType DescriptionRangeEnd => GetBitsValueLong(_value, DescriptionEndBitsOffset, DescriptionBitsCount);
|
||||
|
||||
private Result Value => new Result((BaseType)_value);
|
||||
private Result Value => new Result(unchecked((BaseType)_value));
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the range of this <see cref="Result.Base"/> includes the provided <see cref="Result"/>.
|
||||
|
@ -409,7 +411,7 @@ public readonly struct Result : IEquatable<Result>
|
|||
}
|
||||
|
||||
return result.Module == Module &&
|
||||
result.Description - DescriptionRangeStart <= DescriptionRangeEnd - DescriptionRangeStart;
|
||||
unchecked(result.Description - DescriptionRangeStart <= DescriptionRangeEnd - DescriptionRangeStart);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -174,7 +174,7 @@ public class Aes128XtsTransform
|
|||
|
||||
for (int i = 2; i < bufL.Length; i += 2)
|
||||
{
|
||||
ulong tt = (ulong)((long)a >> 63) & 0x87;
|
||||
ulong tt = unchecked((ulong)((long)a >> 63) & 0x87);
|
||||
|
||||
a = (a << 1) | (b >> 63);
|
||||
b = (b << 1) ^ tt;
|
||||
|
@ -193,7 +193,7 @@ public class Aes128XtsTransform
|
|||
{
|
||||
for (int i = 0xF; i >= 0; i--)
|
||||
{
|
||||
value[i] = (byte)sector;
|
||||
value[i] = unchecked((byte)sector);
|
||||
sector >>= 8;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -64,21 +64,27 @@ internal static class HexConverter
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void ToBytesBuffer(byte value, Span<byte> buffer, int startingIndex = 0, Casing casing = Casing.Upper)
|
||||
{
|
||||
uint difference = ((value & 0xF0U) << 4) + (value & 0x0FU) - 0x8989U;
|
||||
uint packedResult = ((((uint)(-(int)difference) & 0x7070U) >> 4) + difference + 0xB9B9U) | (uint)casing;
|
||||
unchecked
|
||||
{
|
||||
uint difference = ((value & 0xF0U) << 4) + (value & 0x0FU) - 0x8989U;
|
||||
uint packedResult = ((((uint)(-(int)difference) & 0x7070U) >> 4) + difference + 0xB9B9U) | (uint)casing;
|
||||
|
||||
buffer[startingIndex + 1] = (byte)packedResult;
|
||||
buffer[startingIndex] = (byte)(packedResult >> 8);
|
||||
buffer[startingIndex + 1] = (byte)packedResult;
|
||||
buffer[startingIndex] = (byte)(packedResult >> 8);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void ToCharsBuffer(byte value, Span<char> buffer, int startingIndex = 0, Casing casing = Casing.Upper)
|
||||
{
|
||||
uint difference = ((value & 0xF0U) << 4) + (value & 0x0FU) - 0x8989U;
|
||||
uint packedResult = ((((uint)(-(int)difference) & 0x7070U) >> 4) + difference + 0xB9B9U) | (uint)casing;
|
||||
unchecked
|
||||
{
|
||||
uint difference = ((value & 0xF0U) << 4) + (value & 0x0FU) - 0x8989U;
|
||||
uint packedResult = ((((uint)(-(int)difference) & 0x7070U) >> 4) + difference + 0xB9B9U) | (uint)casing;
|
||||
|
||||
buffer[startingIndex + 1] = (char)(packedResult & 0xFF);
|
||||
buffer[startingIndex] = (char)(packedResult >> 8);
|
||||
buffer[startingIndex + 1] = (char)(packedResult & 0xFF);
|
||||
buffer[startingIndex] = (char)(packedResult >> 8);
|
||||
}
|
||||
}
|
||||
|
||||
public static void EncodeToUtf16(ReadOnlySpan<byte> bytes, Span<char> chars, Casing casing = Casing.Upper)
|
||||
|
@ -182,13 +188,16 @@ internal static class HexConverter
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int FromLowerChar(int c)
|
||||
{
|
||||
if ((uint)(c - '0') <= '9' - '0')
|
||||
return c - '0';
|
||||
unchecked
|
||||
{
|
||||
if ((uint)(c - '0') <= '9' - '0')
|
||||
return c - '0';
|
||||
|
||||
if ((uint)(c - 'a') <= 'f' - 'a')
|
||||
return c - 'a' + 10;
|
||||
if ((uint)(c - 'a') <= 'f' - 'a')
|
||||
return c - 'a' + 10;
|
||||
|
||||
return 0xFF;
|
||||
return 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
@ -200,13 +209,13 @@ internal static class HexConverter
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool IsHexUpperChar(int c)
|
||||
{
|
||||
return (uint)(c - '0') <= 9 || (uint)(c - 'A') <= ('F' - 'A');
|
||||
return unchecked((uint)(c - '0') <= 9 || (uint)(c - 'A') <= ('F' - 'A'));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool IsHexLowerChar(int c)
|
||||
{
|
||||
return (uint)(c - '0') <= 9 || (uint)(c - 'a') <= ('f' - 'a');
|
||||
return unchecked((uint)(c - '0') <= 9 || (uint)(c - 'a') <= ('f' - 'a'));
|
||||
}
|
||||
|
||||
/// <summary>Map from an ASCII char to its hex value, e.g. arr['b'] == 11. 0xFF means it's not a hex digit.</summary>
|
||||
|
@ -229,4 +238,4 @@ internal static class HexConverter
|
|||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 239
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF // 255
|
||||
};
|
||||
}
|
||||
}
|
|
@ -159,7 +159,7 @@ public static class StringUtils
|
|||
|
||||
private static byte ToLowerAsciiInvariant(byte c)
|
||||
{
|
||||
if ((uint)(c - 'A') <= 'Z' - 'A')
|
||||
if (unchecked((uint)(c - 'A') <= 'Z' - 'A'))
|
||||
{
|
||||
c = (byte)(c | 0x20);
|
||||
}
|
||||
|
@ -240,12 +240,12 @@ public static class StringUtils
|
|||
|
||||
public static bool IsAlpha(byte c)
|
||||
{
|
||||
return (c | 0x20u) - (byte)'a' <= 'z' - 'a';
|
||||
return unchecked((c | 0x20u) - (byte)'a' <= 'z' - 'a');
|
||||
}
|
||||
|
||||
public static bool IsDigit(byte c)
|
||||
{
|
||||
return (uint)(c - (byte)'0') <= 9;
|
||||
return unchecked((uint)(c - (byte)'0') <= 9);
|
||||
}
|
||||
|
||||
public static bool IsHexDigit(byte c)
|
||||
|
@ -316,4 +316,4 @@ public static class StringUtils
|
|||
{
|
||||
return HexConverter.ToString(bytes);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue