Misc tweaks (#111)

- Use System.HashCode in GetHashCode methods
- Update master key revisions.
- Remove some unneeded utility functions
- Suppress a spurious ReSharper warning
This commit is contained in:
Alex Barney 2020-01-19 23:31:23 -07:00 committed by GitHub
parent 1ae973a346
commit 35b82f5d6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 24 additions and 94 deletions

View file

@ -44,10 +44,7 @@ namespace LibHac.Common
public override int GetHashCode() public override int GetHashCode()
{ {
unchecked return HashCode.Combine(High, Low);
{
return (High.GetHashCode() * 397) ^ Low.GetHashCode();
}
} }
public int CompareTo(Id128 other) public int CompareTo(Id128 other)

View file

@ -36,10 +36,7 @@ namespace LibHac.Common
public override int GetHashCode() public override int GetHashCode()
{ {
int hashCode = -1653217991; return HashCode.Combine(_dummy1, _dummy2);
hashCode = hashCode * -1521134295 + _dummy1.GetHashCode();
hashCode = hashCode * -1521134295 + _dummy2.GetHashCode();
return hashCode;
} }
public static bool operator ==(Key128 left, Key128 right) => left.Equals(right); public static bool operator ==(Key128 left, Key128 right) => left.Equals(right);

View file

@ -33,14 +33,7 @@ namespace LibHac.Fs
public override int GetHashCode() public override int GetHashCode()
{ {
// ReSharper disable NonReadonlyMemberInGetHashCode // ReSharper disable NonReadonlyMemberInGetHashCode
int hashCode = 487790375; return HashCode.Combine(TitleId, Type, UserId, SaveDataId, Rank, Index);
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;
// ReSharper restore NonReadonlyMemberInGetHashCode // ReSharper restore NonReadonlyMemberInGetHashCode
} }

View file

@ -35,6 +35,7 @@ namespace LibHac.Fs.Shim
static Result Run(FileSystemClient fs, U8Span mountName, U8Span path) static Result Run(FileSystemClient fs, U8Span mountName, U8Span path)
{ {
// ReSharper disable once VariableHidesOuterVariable
Result rc = MountHelpers.CheckMountName(mountName); Result rc = MountHelpers.CheckMountName(mountName);
if (rc.IsFailure()) return rc; if (rc.IsFailure()) return rc;

View file

@ -50,16 +50,9 @@ namespace LibHac.Ncm
public override int GetHashCode() public override int GetHashCode()
{ {
unchecked // ReSharper disable NonReadonlyMemberInGetHashCode
{ return HashCode.Combine(TitleId, Version, Type, Attributes);
// ReSharper disable NonReadonlyMemberInGetHashCode // ReSharper restore 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
}
} }
public int CompareTo(ContentMetaKey other) public int CompareTo(ContentMetaKey other)

View file

@ -1,5 +1,6 @@
// ReSharper disable UnusedVariable // ReSharper disable UnusedVariable
using System; using System;
using System.Buffers.Binary;
using System.IO; using System.IO;
namespace LibHac.Npdm namespace LibHac.Npdm
@ -53,10 +54,10 @@ namespace LibHac.Npdm
reader.ReadInt32(); reader.ReadInt32();
//System resource size (max size as of 5.x: 534773760). //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 (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. //Main entrypoint stack size.
MainEntrypointStackSize = reader.ReadInt32(); MainEntrypointStackSize = reader.ReadInt32();

View file

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -471,7 +470,6 @@ namespace LibHac
public static void MemDump(this StringBuilder sb, string prefix, byte[] data) public static void MemDump(this StringBuilder sb, string prefix, byte[] data)
{ {
int max = 32; int max = 32;
int remaining = data.Length; int remaining = data.Length;
bool first = true; 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) 0 => "1.0.0-2.3.0",
{ 1 => "3.0.0",
case 0: return "1.0.0-2.3.0"; 2 => "3.0.1-3.0.2",
case 1: return "3.0.0"; 3 => "4.0.0-4.1.0",
case 2: return "3.0.1-3.0.2"; 4 => "5.0.0-5.1.0",
case 3: return "4.0.0-4.1.0"; 5 => "6.0.0-6.0.1",
case 4: return "5.0.0-5.1.0"; 6 => "6.2.0",
case 5: return "6.0.0-6.0.1"; 7 => "7.0.0-8.0.1",
case 6: return "6.2.0"; 8 => "8.1.0-8.1.1",
case 7: return "7.0.0-8.0.1"; 9 => "9.0.0-9.0.1",
default: return "Unknown"; 0xA => "9.1.0-",
} _ => "Unknown"
} };
public static bool IsSubRange(long startIndex, long subLength, long length) public static bool IsSubRange(long startIndex, long subLength, long length)
{ {
@ -523,16 +521,6 @@ namespace LibHac
return !isOutOfRange; 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) public static int GetMasterKeyRevision(int keyGeneration)
{ {
if (keyGeneration == 0) return 0; if (keyGeneration == 0) return 0;
@ -540,44 +528,4 @@ namespace LibHac
return keyGeneration - 1; return keyGeneration - 1;
} }
} }
public class ByteArray128BitComparer : EqualityComparer<byte[]>
{
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();
}
}
} }