Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit e12b2c8

Browse files
committed
Update native library loader for it to work correctly in .NET Standard 2.0
1 parent cbbc648 commit e12b2c8

File tree

22 files changed

+111
-60
lines changed

22 files changed

+111
-60
lines changed

src/Data.HashFunction.BernsteinHash/Data.HashFunction.BernsteinHash.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
<PropertyGroup>
44
<Description>Data.HashFunction implementation of Bernstein hash (http://www.eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx#djb).</Description>
5-
<Copyright>Copyright 2024</Copyright>
5+
<Copyright>© 2024, Deskasoft International. All rights reserved.</Copyright>
66
<AssemblyTitle>Data.HashFunction.BernsteinHash</AssemblyTitle>
77
<VersionPrefix>3.1.1</VersionPrefix>
8-
<Authors>Data.HashFunction Developers</Authors>
8+
<Authors>Deskasoft International</Authors>
99
<TargetFrameworks>netstandard2.0</TargetFrameworks>
1010
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1111
<AssemblyName>Data.HashFunction.BernsteinHash</AssemblyName>

src/Data.HashFunction.Blake2/Data.HashFunction.Blake2.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
<PropertyGroup>
44
<Description>Data.HashFunction implementation of BLAKE2 hash (http://blake2.net/).</Description>
5-
<Copyright>Copyright 2024</Copyright>
5+
<Copyright>© 2024, Deskasoft International. All rights reserved.</Copyright>
66
<AssemblyTitle>Data.HashFunction.Blake2</AssemblyTitle>
77
<VersionPrefix>3.1.1</VersionPrefix>
8-
<Authors>Data.HashFunction Developers</Authors>
8+
<Authors>Deskasoft International</Authors>
99
<TargetFrameworks>netstandard2.0</TargetFrameworks>
1010
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1111
<AssemblyName>Data.HashFunction.Blake2</AssemblyName>

src/Data.HashFunction.Blake3/Data.HashFunction.Blake3.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
<PropertyGroup>
44
<Description>Data.HashFunction implementation of BLAKE3 hash (https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE3).</Description>
5-
<Copyright>Copyright 2024</Copyright>
5+
<Copyright>© 2024, Deskasoft International. All rights reserved.</Copyright>
66
<AssemblyTitle>Data.HashFunction.Blake3</AssemblyTitle>
77
<VersionPrefix>3.1.1</VersionPrefix>
8-
<Authors>Data.HashFunction Developers</Authors>
8+
<Authors>Deskasoft International</Authors>
99
<TargetFrameworks>netstandard2.0</TargetFrameworks>
1010
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1111
<TargetFramework>netstandard2.0</TargetFramework>

src/Data.HashFunction.BuzHash/Data.HashFunction.BuzHash.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
<PropertyGroup>
44
<Description>Data.HashFunction implementation of BuzHash (http://www.serve.net/buz/hash.adt/java.002.html).</Description>
5-
<Copyright>Copyright 2024</Copyright>
5+
<Copyright>© 2024, Deskasoft International. All rights reserved.</Copyright>
66
<AssemblyTitle>Data.HashFunction.BuzHash</AssemblyTitle>
77
<VersionPrefix>3.1.1</VersionPrefix>
8-
<Authors>Data.HashFunction Developers</Authors>
8+
<Authors>Deskasoft International</Authors>
99
<TargetFrameworks>netstandard2.0</TargetFrameworks>
1010
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1111
<AssemblyName>Data.HashFunction.BuzHash</AssemblyName>

src/Data.HashFunction.CRC/Data.HashFunction.CRC.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
<PropertyGroup>
44
<Description>Data.HashFunction implementation of the cyclic redundancy check (CRC) error-detecting code (http://en.wikipedia.org/wiki/Cyclic_redundancy_check). Implementation is generalized to encompass all possible CRC parameters from 1 to 64 bits.</Description>
5-
<Copyright>Copyright 2024</Copyright>
5+
<Copyright>© 2024, Deskasoft International. All rights reserved.</Copyright>
66
<AssemblyTitle>Data.HashFunction.CRC</AssemblyTitle>
77
<VersionPrefix>3.1.1</VersionPrefix>
8-
<Authors>Data.HashFunction Developers</Authors>
8+
<Authors>Deskasoft International</Authors>
99
<TargetFrameworks>netstandard2.0</TargetFrameworks>
1010
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1111
<AssemblyName>Data.HashFunction.CRC</AssemblyName>

src/Data.HashFunction.CityHash/Data.HashFunction.CityHash.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
<PropertyGroup>
44
<Description>Data.HashFunction implementation of CityHash (https://code.google.com/p/cityhash/).</Description>
5-
<Copyright>Copyright 2024</Copyright>
5+
<Copyright>© 2024, Deskasoft International. All rights reserved.</Copyright>
66
<AssemblyTitle>Data.HashFunction.CityHash</AssemblyTitle>
77
<VersionPrefix>3.1.1</VersionPrefix>
8-
<Authors>Data.HashFunction Developers</Authors>
8+
<Authors>Deskasoft International</Authors>
99
<TargetFrameworks>netstandard2.0</TargetFrameworks>
1010
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1111
<AssemblyName>Data.HashFunction.CityHash</AssemblyName>

src/Data.HashFunction.Core/Data.HashFunction.Core.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
<PropertyGroup>
44
<Description>C# library to create a common interface to non-cryptographic hash functions (http://en.wikipedia.org/wiki/List_of_hash_functions#Non-cryptographic_hash_functions) and provide implementations of public hash functions. Includes wrapper for HashAlgorithm-based hash functions.</Description>
5-
<Copyright>Copyright 2024</Copyright>
5+
<Copyright>© 2024, Deskasoft International. All rights reserved.</Copyright>
66
<AssemblyTitle>Data.HashFunction.Core</AssemblyTitle>
77
<VersionPrefix>3.1.1</VersionPrefix>
8-
<Authors>Data.HashFunction Developers</Authors>
8+
<Authors>Deskasoft International</Authors>
99
<TargetFrameworks>netstandard2.0</TargetFrameworks>
1010
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1111
<AssemblyName>Data.HashFunction.Core</AssemblyName>

src/Data.HashFunction.Core/NativeLibraryLoader/Kernel32.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ namespace Data.HashFunction.Core.LibraryLoader
77
{
88
internal static class Kernel32
99
{
10-
[DllImport("kernel32")]
10+
[DllImport("kernel32.dll")]
1111
public static extern IntPtr LoadLibrary(string fileName);
1212

13-
[DllImport("kernel32")]
13+
[DllImport("kernel32.dll")]
1414
public static extern IntPtr GetProcAddress(IntPtr module, string procName);
1515

16-
[DllImport("kernel32")]
16+
[DllImport("kernel32.dll")]
1717
public static extern int FreeLibrary(IntPtr module);
1818
}
1919
}

src/Data.HashFunction.Core/NativeLibraryLoader/Libdl.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,32 @@ namespace Data.HashFunction.Core.LibraryLoader
77
{
88
internal static class Libdl
99
{
10-
[DllImport("libdl")]
11-
public static extern IntPtr dlopen(string fileName, int flags);
10+
[DllImport("libdl.so", EntryPoint = "dlopen")]
11+
public static extern IntPtr dlopen_linux(string fileName, int flags);
1212

13-
[DllImport("libdl")]
14-
public static extern IntPtr dlsym(IntPtr handle, string name);
13+
[DllImport("libdl.so", EntryPoint = "dlsym")]
14+
public static extern IntPtr dlsym_linux(IntPtr handle, string name);
1515

16-
[DllImport("libdl")]
17-
public static extern int dlclose(IntPtr handle);
16+
[DllImport("libdl.so", EntryPoint = "dlclose")]
17+
public static extern int dlclose_linux(IntPtr handle);
1818

19-
[DllImport("libdl")]
20-
public static extern string dlerror();
19+
[DllImport("libdl.so", EntryPoint = "dlerror")]
20+
public static extern string dlerror_linux();
2121

22-
public const int RTLD_NOW = 0x002;
22+
23+
[DllImport("libdl.dylib", EntryPoint = "dlopen")]
24+
public static extern IntPtr dlopen_osx(string fileName, int flags);
25+
26+
[DllImport("libdl.dylib", EntryPoint = "dlsym")]
27+
public static extern IntPtr dlsym_osx(IntPtr handle, string name);
28+
29+
[DllImport("libdl.dylib", EntryPoint = "dlclose")]
30+
public static extern int dlclose_osx(IntPtr handle);
31+
32+
[DllImport("libdl.dylib", EntryPoint = "dlerror")]
33+
public static extern string dlerror_osx();
34+
35+
public const int RTLD_NOW = 0x002;
2336
}
2437
}
2538

src/Data.HashFunction.Core/NativeLibraryLoader/NativeLibraryLoader.cs

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,14 @@ public static NativeLibraryLoader Load(string library)
160160
{
161161
return new WindowsNativeLibrary(library);
162162
}
163-
else if (IsOSPlatform(PlatformType.Android) || IsOSPlatform(PlatformType.Linux) || IsOSPlatform(PlatformType.MacOS))
163+
else if (IsOSPlatform(PlatformType.Android) || IsOSPlatform(PlatformType.Linux))
164164
{
165-
return new UnixNativeLibrary(library);
165+
return new LinuxNativeLibrary(library);
166166
}
167+
else if (IsOSPlatform(PlatformType.MacOS))
168+
{
169+
return new OSXNativeLibrary(library);
170+
}
167171
else
168172
{
169173
throw new PlatformNotSupportedException("Cannot load native libraries on this platform: " + RuntimeInformation.OSDescription);
@@ -192,23 +196,23 @@ public override IntPtr LoadFunction(string functionName)
192196
}
193197
}
194198

195-
private class UnixNativeLibrary : NativeLibraryLoader
199+
private class LinuxNativeLibrary : NativeLibraryLoader
196200
{
197-
public UnixNativeLibrary(string library) : base(library)
201+
public LinuxNativeLibrary(string library) : base(library)
198202
{
199203
}
200204

201205
protected override IntPtr LoadLibrary(string libraryName)
202206
{
203-
Libdl.dlerror();
204-
IntPtr handle = Libdl.dlopen(libraryName, Libdl.RTLD_NOW);
207+
Libdl.dlerror_linux();
208+
IntPtr handle = Libdl.dlopen_linux(libraryName, Libdl.RTLD_NOW);
205209
if (handle == IntPtr.Zero && !Path.IsPathRooted(libraryName))
206210
{
207211
string baseDir = AppContext.BaseDirectory;
208212
if (!string.IsNullOrWhiteSpace(baseDir))
209213
{
210214
string localPath = Path.Combine(baseDir, libraryName);
211-
handle = Libdl.dlopen(localPath, Libdl.RTLD_NOW);
215+
handle = Libdl.dlopen_linux(localPath, Libdl.RTLD_NOW);
212216
}
213217
}
214218

@@ -217,14 +221,48 @@ protected override IntPtr LoadLibrary(string libraryName)
217221

218222
protected override void FreeLibrary(IntPtr libraryHandle)
219223
{
220-
Libdl.dlclose(libraryHandle);
224+
Libdl.dlclose_linux(libraryHandle);
221225
}
222226

223227
public override IntPtr LoadFunction(string functionName)
224228
{
225-
return Libdl.dlsym(libraryHandle, functionName);
229+
return Libdl.dlsym_linux(libraryHandle, functionName);
226230
}
227231
}
228-
}
232+
233+
private class OSXNativeLibrary : NativeLibraryLoader
234+
{
235+
public OSXNativeLibrary(string library) : base(library)
236+
{
237+
}
238+
239+
protected override IntPtr LoadLibrary(string libraryName)
240+
{
241+
Libdl.dlerror_osx();
242+
IntPtr handle = Libdl.dlopen_osx(libraryName, Libdl.RTLD_NOW);
243+
if (handle == IntPtr.Zero && !Path.IsPathRooted(libraryName))
244+
{
245+
string baseDir = AppContext.BaseDirectory;
246+
if (!string.IsNullOrWhiteSpace(baseDir))
247+
{
248+
string localPath = Path.Combine(baseDir, libraryName);
249+
handle = Libdl.dlopen_osx(localPath, Libdl.RTLD_NOW);
250+
}
251+
}
252+
253+
return handle;
254+
}
255+
256+
protected override void FreeLibrary(IntPtr libraryHandle)
257+
{
258+
Libdl.dlclose_osx(libraryHandle);
259+
}
260+
261+
public override IntPtr LoadFunction(string functionName)
262+
{
263+
return Libdl.dlsym_osx(libraryHandle, functionName);
264+
}
265+
}
266+
}
229267
}
230268

0 commit comments

Comments
 (0)