Skip to content

Commit

Permalink
removed UnsafeCoerce functions and usages (broken in net8.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
krauthaufen committed May 17, 2024
1 parent 01b9b97 commit 070372d
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 129 deletions.
134 changes: 113 additions & 21 deletions src/Aardvark.Base/Extensions/ArrayExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
Expand Down Expand Up @@ -2355,16 +2356,38 @@ public static byte[] ComputeMD5Hash(this byte[] data)
return bytes;
}
}

/// <summary>
/// Computes the MD5 hash of the data array.
/// </summary>
/// <returns>128bit/16byte data hash</returns>
public static byte[] ComputeMD5Hash(this Array data)
public static unsafe byte[] ComputeMD5Hash(byte* data, int length)
{
byte[] hash = null;
data.UnsafeCoercedApply<byte>(array => hash = array.ComputeMD5Hash());
return hash;
using (var stream = new UnmanagedMemoryStream(data, length))
using (var md5 = SHA1.Create())
{
var bytes = md5.ComputeHash(stream);
Array.Resize(ref bytes, 16);
return bytes;
}
}

/// <summary>
/// Computes the MD5 hash of the data array.
/// </summary>
/// <returns>128bit/16byte data hash</returns>
public static unsafe byte[] ComputeMD5Hash(this Array data)
{
Type el = data.GetType().GetElementType();
var gc = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
return ComputeMD5Hash((byte*)gc.AddrOfPinnedObject(), Marshal.SizeOf(el) * data.Length);
}
finally
{
gc.Free();
}
}

/// <summary>
Expand All @@ -2385,16 +2408,34 @@ public static byte[] ComputeSHA1Hash(this byte[] data)
using (var sha1 = SHA1.Create())
return sha1.ComputeHash(data);
}

/// <summary>
/// Computes the SHA1 hash of the data array.
/// </summary>
/// <returns>160bit/20byte data hash</returns>
public static unsafe byte[] ComputeSHA1Hash(byte* data, int length)
{
using(var stream = new UnmanagedMemoryStream(data, length))
using (var sha1 = SHA1.Create())
return sha1.ComputeHash(stream);
}

/// <summary>
/// Computes the SHA1 hash of the data array.
/// </summary>
/// <returns>160bit/20byte data hash</returns>
public static byte[] ComputeSHA1Hash(this Array data)
public static unsafe byte[] ComputeSHA1Hash(this Array data)
{
byte[] hash = null;
data.UnsafeCoercedApply<byte>(array => hash = array.ComputeSHA1Hash());
return hash;
Type el = data.GetType().GetElementType();
var gc = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
return ComputeSHA1Hash((byte*)gc.AddrOfPinnedObject(), Marshal.SizeOf(el) * data.Length);
}
finally
{
gc.Free();
}
}

/// <summary>
Expand All @@ -2420,11 +2461,29 @@ public static byte[] ComputeSHA256Hash(this byte[] data)
/// Computes the SHA256 hash of the data array.
/// </summary>
/// <returns>256bit/32byte data hash</returns>
public static byte[] ComputeSHA256Hash(this Array data)
public static unsafe byte[] ComputeSHA256Hash(byte* data, int length)
{
byte[] hash = null;
data.UnsafeCoercedApply<byte>(array => hash = array.ComputeSHA256Hash());
return hash;
using (var stream = new UnmanagedMemoryStream(data, length))
using (var sha256 = SHA256.Create())
return sha256.ComputeHash(stream);
}

/// <summary>
/// Computes the SHA256 hash of the data array.
/// </summary>
/// <returns>256bit/32byte data hash</returns>
public static unsafe byte[] ComputeSHA256Hash(this Array data)
{
Type el = data.GetType().GetElementType();
var gc = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
return ComputeSHA256Hash((byte*)gc.AddrOfPinnedObject(), Marshal.SizeOf(el) * data.Length);
}
finally
{
gc.Free();
}
}

/// <summary>
Expand All @@ -2446,15 +2505,30 @@ public static byte[] ComputeSHA512Hash(this byte[] data)
return sha512.ComputeHash(data);
}


public static unsafe byte[] ComputeSHA512Hash(byte* data, int length)
{
using (var stream = new UnmanagedMemoryStream(data, length))
using (var sha512 = SHA512.Create())
return sha512.ComputeHash(stream);
}

/// <summary>
/// Computes the SHA512 hash of the data array.
/// </summary>
/// <returns>512bit/64byte data hash</returns>
public static byte[] ComputeSHA512Hash(this Array data)
public static unsafe byte[] ComputeSHA512Hash(this Array data)
{
byte[] hash = null;
data.UnsafeCoercedApply<byte>(array => hash = array.ComputeSHA512Hash());
return hash;
Type el = data.GetType().GetElementType();
var gc = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
return ComputeSHA512Hash((byte*)gc.AddrOfPinnedObject(), Marshal.SizeOf(el) * data.Length);
}
finally
{
gc.Free();
}
}

/// <summary>
Expand All @@ -2476,14 +2550,32 @@ public static uint ComputeAdler32Checksum(this byte[] data)
return a.Checksum;
}


public static unsafe uint ComputeAdler32Checksum(byte* data, int length)
{
var a = new Adler32();
using (var stream = new UnmanagedMemoryStream(data, length))
{
a.Update(stream);
return a.Checksum;
}
}

/// <summary>
/// Computes a checksum of the data array using the Adler-32 algorithm (<see cref="Adler32"/>).
/// </summary>
public static uint ComputeAdler32Checksum(this Array data)
public static unsafe uint ComputeAdler32Checksum(this Array data)
{
var a = new Adler32();
data.UnsafeCoercedApply<byte>(array => a.Update(array));
return a.Checksum;
Type el = data.GetType().GetElementType();
var gc = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
return ComputeAdler32Checksum((byte*)gc.AddrOfPinnedObject(), Marshal.SizeOf(el) * data.Length);
}
finally
{
gc.Free();
}
}

/// <summary>
Expand Down
106 changes: 0 additions & 106 deletions src/Aardvark.Base/Extensions/UnsafeCoerce.cs

This file was deleted.

12 changes: 10 additions & 2 deletions src/Aardvark.Base/Random/ForcedRandomSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ForcedRandomSeries : IRandomSeries
/// Reads a file that contains a raw V2i binary array as V2i[].
/// The binary data is supposed to contain NxN points as (int, int) in random order.
/// </summary>
public static V2i[] ReadSeries(string frsSqFile)
public static unsafe V2i[] ReadSeries(string frsSqFile)
{
var bytes = File.ReadAllBytes(frsSqFile);
var matrixSize = (int)(bytes.Length / 8).Sqrt();
Expand All @@ -28,7 +28,15 @@ public static V2i[] ReadSeries(string frsSqFile)
if (matrixSize * matrixSize * 8 != bytes.Length)
throw new InvalidDataException("Forced Random series data has invalid length.");

return bytes.UnsafeCoerce<V2i>();
var result = new V2i[matrixSize * matrixSize];
fixed (V2i* pResult = result)
{
var src = new System.Span<byte>(bytes);
var dst = new System.Span<byte>(pResult, result.Length * sizeof(V2i));
src.CopyTo(dst);
}

return result;
}

/// <summary>
Expand Down

0 comments on commit 070372d

Please sign in to comment.