Skip to content

Commit 8a2ed66

Browse files
committed
Use SequenceEqual
Remove unsafe and msvcrt.dll PInvoke See https://stackoverflow.com/a/48599119
1 parent 501b14c commit 8a2ed66

2 files changed

Lines changed: 4 additions & 60 deletions

File tree

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,13 @@
1-
using System.Diagnostics;
1+

22
using System.Runtime.CompilerServices;
3-
using System.Runtime.InteropServices;
43

54
namespace PngDecoder.Extension;
65
internal static class GenericHelper
76
{
8-
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
9-
private static extern int memcmp(IntPtr a1, IntPtr a2, uint count);
7+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8+
internal static bool Equal(ReadOnlySpan<byte> data1, ReadOnlySpan<byte> data2) =>
9+
data1.SequenceEqual(data2);
1010

11-
internal static unsafe bool Equal(byte[] data1, byte[] data2)
12-
{
13-
if (data1 is null || data2 is null || data1.Length != data2.Length)
14-
return false;
15-
16-
fixed (byte* p1 = data1, p2 = data2)
17-
return memcmp((IntPtr)p1, (IntPtr)p2, (uint)data1.Length * sizeof(byte)) == 0;
18-
}
19-
20-
21-
internal static unsafe bool Equal(ReadOnlySpan<byte> data1, ReadOnlySpan<byte> data2)
22-
{
23-
if (data1.Length != data2.Length)
24-
return false;
25-
26-
fixed (byte* p1 = data1, p2 = data2)
27-
return memcmp((IntPtr)p1, (IntPtr)p2, (uint)data1.Length * sizeof(byte)) == 0;
28-
}
29-
30-
31-
32-
internal static byte[] Tobytes(this ValueType @struct)
33-
{
34-
var result = new byte[Marshal.SizeOf(@struct)];
35-
Unsafe.As<byte, ValueType>(ref result[0]) = @struct;
36-
return result;
37-
}
38-
39-
internal static T ToStruct<T>(this byte[] @bytes) where T : struct =>
40-
Unsafe.As<byte, T>(ref @bytes[0]);
4111
internal static T ToStruct<T>(this Span<byte> @bytes) where T : struct =>
4212
Unsafe.As<byte, T>(ref @bytes[0]);
43-
44-
internal static T FromStream<T>(this Stream stream,
45-
long skip = 0,
46-
SeekOrigin origin = SeekOrigin.Current) where T : struct
47-
{
48-
var size = Marshal.SizeOf(typeof(T));
49-
Span<byte> resultAsByte = stackalloc byte[size];
50-
stream.Seek(skip, origin);
51-
52-
var read = stream.Read(resultAsByte);
53-
Debug.Assert(read == size);
54-
55-
return resultAsByte.ToStruct<T>();
56-
}
57-
58-
internal static byte ReadByteNotMove(this Stream stream, long index)
59-
{
60-
var current = stream.Position;
61-
62-
Span<byte> result = stackalloc byte[1];
63-
stream.Seek(index, SeekOrigin.Current);
64-
stream.Read(result);
65-
stream.Seek(current, SeekOrigin.Begin);
66-
return result[0];
67-
}
6813
}

Src/PngDecoder/PngDecoder.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7-
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
87
</PropertyGroup>
98

109
</Project>

0 commit comments

Comments
 (0)