From 35b82f5d6ecf34ee5c5e4464535531105dd1d28b Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Sun, 19 Jan 2020 23:31:23 -0700 Subject: [PATCH] Misc tweaks (#111) - Use System.HashCode in GetHashCode methods - Update master key revisions. - Remove some unneeded utility functions - Suppress a spurious ReSharper warning --- src/LibHac/Common/Id128.cs | 5 +- src/LibHac/Common/Key128.cs | 5 +- src/LibHac/Fs/SaveDataStructs.cs | 9 +--- src/LibHac/Fs/Shim/Application.cs | 1 + src/LibHac/Ncm/ContentMetaKey.cs | 13 ++--- src/LibHac/Npdm/NpdmBinary.cs | 5 +- src/LibHac/Util.cs | 80 ++++++------------------------- 7 files changed, 24 insertions(+), 94 deletions(-) diff --git a/src/LibHac/Common/Id128.cs b/src/LibHac/Common/Id128.cs index 2cb598e6..9bb2eeec 100644 --- a/src/LibHac/Common/Id128.cs +++ b/src/LibHac/Common/Id128.cs @@ -44,10 +44,7 @@ namespace LibHac.Common public override int GetHashCode() { - unchecked - { - return (High.GetHashCode() * 397) ^ Low.GetHashCode(); - } + return HashCode.Combine(High, Low); } public int CompareTo(Id128 other) diff --git a/src/LibHac/Common/Key128.cs b/src/LibHac/Common/Key128.cs index 6febda1a..10123b8d 100644 --- a/src/LibHac/Common/Key128.cs +++ b/src/LibHac/Common/Key128.cs @@ -36,10 +36,7 @@ namespace LibHac.Common public override int GetHashCode() { - int hashCode = -1653217991; - hashCode = hashCode * -1521134295 + _dummy1.GetHashCode(); - hashCode = hashCode * -1521134295 + _dummy2.GetHashCode(); - return hashCode; + return HashCode.Combine(_dummy1, _dummy2); } public static bool operator ==(Key128 left, Key128 right) => left.Equals(right); diff --git a/src/LibHac/Fs/SaveDataStructs.cs b/src/LibHac/Fs/SaveDataStructs.cs index 4565289b..fd62cfde 100644 --- a/src/LibHac/Fs/SaveDataStructs.cs +++ b/src/LibHac/Fs/SaveDataStructs.cs @@ -33,14 +33,7 @@ namespace LibHac.Fs public override int GetHashCode() { // ReSharper disable NonReadonlyMemberInGetHashCode - int hashCode = 487790375; - hashCode = hashCode * -1521134295 + TitleId.GetHashCode(); - hashCode = hashCode * -1521134295 + Type.GetHashCode(); - hashCode = hashCode * -1521134295 + UserId.GetHashCode(); - hashCode = hashCode * -1521134295 + SaveDataId.GetHashCode(); - hashCode = hashCode * -1521134295 + Rank.GetHashCode(); - hashCode = hashCode * -1521134295 + Index.GetHashCode(); - return hashCode; + return HashCode.Combine(TitleId, Type, UserId, SaveDataId, Rank, Index); // ReSharper restore NonReadonlyMemberInGetHashCode } diff --git a/src/LibHac/Fs/Shim/Application.cs b/src/LibHac/Fs/Shim/Application.cs index bfaa23c4..109aa08e 100644 --- a/src/LibHac/Fs/Shim/Application.cs +++ b/src/LibHac/Fs/Shim/Application.cs @@ -35,6 +35,7 @@ namespace LibHac.Fs.Shim static Result Run(FileSystemClient fs, U8Span mountName, U8Span path) { + // ReSharper disable once VariableHidesOuterVariable Result rc = MountHelpers.CheckMountName(mountName); if (rc.IsFailure()) return rc; diff --git a/src/LibHac/Ncm/ContentMetaKey.cs b/src/LibHac/Ncm/ContentMetaKey.cs index bdbcbce2..4bf9e125 100644 --- a/src/LibHac/Ncm/ContentMetaKey.cs +++ b/src/LibHac/Ncm/ContentMetaKey.cs @@ -50,16 +50,9 @@ namespace LibHac.Ncm public override int GetHashCode() { - unchecked - { - // ReSharper disable NonReadonlyMemberInGetHashCode - int hashCode = TitleId.GetHashCode(); - hashCode = (hashCode * 397) ^ (int)Version; - hashCode = (hashCode * 397) ^ Type.GetHashCode(); - hashCode = (hashCode * 397) ^ Attributes.GetHashCode(); - return hashCode; - // ReSharper restore NonReadonlyMemberInGetHashCode - } + // ReSharper disable NonReadonlyMemberInGetHashCode + return HashCode.Combine(TitleId, Version, Type, Attributes); + // ReSharper restore NonReadonlyMemberInGetHashCode } public int CompareTo(ContentMetaKey other) diff --git a/src/LibHac/Npdm/NpdmBinary.cs b/src/LibHac/Npdm/NpdmBinary.cs index df21ff3d..941ec422 100644 --- a/src/LibHac/Npdm/NpdmBinary.cs +++ b/src/LibHac/Npdm/NpdmBinary.cs @@ -1,5 +1,6 @@ // ReSharper disable UnusedVariable using System; +using System.Buffers.Binary; using System.IO; namespace LibHac.Npdm @@ -53,10 +54,10 @@ namespace LibHac.Npdm reader.ReadInt32(); //System resource size (max size as of 5.x: 534773760). - SystemResourceSize = Util.Swap32(reader.ReadInt32()); + SystemResourceSize = BinaryPrimitives.ReverseEndianness(reader.ReadInt32()); //ProcessCategory (0: regular title, 1: kernel built-in). Should be 0 here. - ProcessCategory = Util.Swap32(reader.ReadInt32()); + ProcessCategory = BinaryPrimitives.ReverseEndianness(reader.ReadInt32()); //Main entrypoint stack size. MainEntrypointStackSize = reader.ReadInt32(); diff --git a/src/LibHac/Util.cs b/src/LibHac/Util.cs index b774f85f..cd41f537 100644 --- a/src/LibHac/Util.cs +++ b/src/LibHac/Util.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.IO; using System.Numerics; using System.Runtime.CompilerServices; @@ -471,7 +470,6 @@ namespace LibHac public static void MemDump(this StringBuilder sb, string prefix, byte[] data) { - int max = 32; int remaining = data.Length; bool first = true; @@ -501,21 +499,21 @@ namespace LibHac } } - public static string GetKeyRevisionSummary(int revision) + public static string GetKeyRevisionSummary(int revision) => revision switch { - switch (revision) - { - case 0: return "1.0.0-2.3.0"; - case 1: return "3.0.0"; - case 2: return "3.0.1-3.0.2"; - case 3: return "4.0.0-4.1.0"; - case 4: return "5.0.0-5.1.0"; - case 5: return "6.0.0-6.0.1"; - case 6: return "6.2.0"; - case 7: return "7.0.0-8.0.1"; - default: return "Unknown"; - } - } + 0 => "1.0.0-2.3.0", + 1 => "3.0.0", + 2 => "3.0.1-3.0.2", + 3 => "4.0.0-4.1.0", + 4 => "5.0.0-5.1.0", + 5 => "6.0.0-6.0.1", + 6 => "6.2.0", + 7 => "7.0.0-8.0.1", + 8 => "8.1.0-8.1.1", + 9 => "9.0.0-9.0.1", + 0xA => "9.1.0-", + _ => "Unknown" + }; public static bool IsSubRange(long startIndex, long subLength, long length) { @@ -523,16 +521,6 @@ namespace LibHac return !isOutOfRange; } - public static int Swap32(int value) - { - uint uintVal = (uint)value; - - return (int)(((uintVal >> 24) & 0x000000ff) | - ((uintVal >> 8) & 0x0000ff00) | - ((uintVal << 8) & 0x00ff0000) | - ((uintVal << 24) & 0xff000000)); - } - public static int GetMasterKeyRevision(int keyGeneration) { if (keyGeneration == 0) return 0; @@ -540,44 +528,4 @@ namespace LibHac return keyGeneration - 1; } } - - public class ByteArray128BitComparer : EqualityComparer - { - public override bool Equals(byte[] first, byte[] second) - { - if (first == null || second == null) - { - // null == null returns true. - // non-null == null returns false. - return first == second; - } - if (ReferenceEquals(first, second)) - { - return true; - } - if (first.Length != second.Length) - { - return false; - } - - return Util.ArraysEqual(first, second); - } - - public override int GetHashCode(byte[] obj) - { - if (obj == null) - { - throw new ArgumentNullException(nameof(obj)); - } - if (obj.Length != 16) - { - throw new ArgumentException("Length must be 16 bytes"); - } - - ulong hi = BitConverter.ToUInt64(obj, 0); - ulong lo = BitConverter.ToUInt64(obj, 8); - - return (hi.GetHashCode() * 397) ^ lo.GetHashCode(); - } - } }