Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 4 additions & 48 deletions Src/PngDecoder/Extension/GenericHelper.cs
Original file line number Diff line number Diff line change
@@ -1,60 +1,16 @@
using System.Diagnostics;

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

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

internal static unsafe bool Equal(byte[] data1, byte[] data2)
{
if (data1 is null || data2 is null || data1.Length != data2.Length)
return false;

fixed (byte* p1 = data1, p2 = data2)
return memcmp((IntPtr)p1, (IntPtr)p2, (uint)data1.Length * sizeof(byte)) == 0;
}


internal static unsafe bool Equal(ReadOnlySpan<byte> data1, ReadOnlySpan<byte> data2)
{
if (data1.Length != data2.Length)
return false;

fixed (byte* p1 = data1, p2 = data2)
return memcmp((IntPtr)p1, (IntPtr)p2, (uint)data1.Length * sizeof(byte)) == 0;
}



internal static byte[] Tobytes(this ValueType @struct)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like to have these. for farther implementation.

{
var result = new byte[Marshal.SizeOf(@struct)];
Unsafe.As<byte, ValueType>(ref result[0]) = @struct;
return result;
}

internal static T ToStruct<T>(this byte[] @bytes) where T : struct =>
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like to have these. for farther implementation.

Unsafe.As<byte, T>(ref @bytes[0]);
internal static T ToStruct<T>(this Span<byte> @bytes) where T : struct =>
Unsafe.As<byte, T>(ref @bytes[0]);

internal static T FromStream<T>(this Stream stream,
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like to have these. for farther implementation.

long skip = 0,
SeekOrigin origin = SeekOrigin.Current) where T : struct
{
var size = Marshal.SizeOf(typeof(T));
Span<byte> resultAsByte = stackalloc byte[size];
stream.Seek(skip, origin);

var read = stream.Read(resultAsByte);
Debug.Assert(read == size);

return resultAsByte.ToStruct<T>();
}

internal static byte ReadByteNotMove(this Stream stream, long index)
{
var current = stream.Position;
Expand Down
1 change: 0 additions & 1 deletion Src/PngDecoder/PngDecoder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

</Project>