Files

127 lines
3.7 KiB
C#
Raw Permalink Normal View History

2024-01-14 16:50:53 +01:00
/*
2024-09-22 10:13:19 +02:00
* Copyright (c) Gustave Monce and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
2024-01-14 16:50:53 +01:00
using System.Diagnostics.CodeAnalysis;
2024-01-17 23:46:46 +01:00
namespace Img2Ffu.Writer.Flashing
2024-01-14 16:50:53 +01:00
{
2024-01-17 20:05:27 +01:00
public readonly struct ByteArrayKey
2024-01-14 16:50:53 +01:00
{
public readonly byte[] Bytes;
private readonly int _hashCode;
public override bool Equals(object obj)
{
2024-01-17 20:05:27 +01:00
ByteArrayKey other = (ByteArrayKey)obj;
2024-01-14 16:50:53 +01:00
return Compare(Bytes, other.Bytes);
}
public override int GetHashCode()
{
return _hashCode;
}
2024-10-05 21:18:48 +02:00
private static int GetHashCode([NotNull] Memory<byte> bytes)
2024-01-14 16:50:53 +01:00
{
unchecked
{
2024-01-17 20:05:27 +01:00
int hash = 17;
for (int i = 0; i < bytes.Length; i++)
2024-01-14 16:50:53 +01:00
{
2024-10-05 21:18:48 +02:00
hash = (hash * 23) + bytes.Span[i];
2024-01-14 16:50:53 +01:00
}
return hash;
}
}
public ByteArrayKey(byte[] bytes)
{
Bytes = bytes;
_hashCode = GetHashCode(bytes);
}
public static ByteArrayKey Create(byte[] bytes)
{
return new ByteArrayKey(bytes);
}
public static unsafe bool Compare(byte[] a1, byte[] a2)
{
if (a1 == null || a2 == null || a1.Length != a2.Length)
2024-01-17 20:05:27 +01:00
{
2024-01-14 16:50:53 +01:00
return false;
2024-01-17 20:05:27 +01:00
}
2024-01-14 16:50:53 +01:00
fixed (byte* p1 = a1, p2 = a2)
{
byte* x1 = p1, x2 = p2;
2024-01-17 20:05:27 +01:00
int l = a1.Length;
for (int i = 0; i < l / 8; i++, x1 += 8, x2 += 8)
{
2024-01-14 16:50:53 +01:00
if (*(long*)x1 != *(long*)x2)
2024-01-17 20:05:27 +01:00
{
2024-01-14 16:50:53 +01:00
return false;
2024-01-17 20:05:27 +01:00
}
}
2024-01-14 16:50:53 +01:00
if ((l & 4) != 0)
{
if (*(int*)x1 != *(int*)x2)
2024-01-17 20:05:27 +01:00
{
2024-01-14 16:50:53 +01:00
return false;
2024-01-17 20:05:27 +01:00
}
2024-01-14 16:50:53 +01:00
x1 += 4;
x2 += 4;
}
if ((l & 2) != 0)
{
if (*(short*)x1 != *(short*)x2)
2024-01-17 20:05:27 +01:00
{
2024-01-14 16:50:53 +01:00
return false;
2024-01-17 20:05:27 +01:00
}
2024-01-14 16:50:53 +01:00
x1 += 2;
x2 += 2;
}
if ((l & 1) != 0)
2024-01-17 20:05:27 +01:00
{
2024-01-14 16:50:53 +01:00
if (*x1 != *x2)
2024-01-17 20:05:27 +01:00
{
2024-01-14 16:50:53 +01:00
return false;
2024-01-17 20:05:27 +01:00
}
}
2024-01-14 16:50:53 +01:00
return true;
}
}
public static bool operator ==(ByteArrayKey left, ByteArrayKey right)
{
return left.Equals(right);
}
public static bool operator !=(ByteArrayKey left, ByteArrayKey right)
{
return !(left == right);
}
}
}