Skip to content

Commit 45818c2

Browse files
committed
Determine DLL platform in runtime
1 parent 0714d67 commit 45818c2

13 files changed

+48
-64
lines changed

.gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
[Dd]ebugPublic/
2020
[Rr]elease/
2121
[Rr]eleases/
22-
x64/
23-
x86/
2422
bld/
2523
[Bb]in/
2624
[Oo]bj/

ZstdNet.Tests/ZstdNet.Tests.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@
3838
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
3939
<DebugSymbols>true</DebugSymbols>
4040
<OutputPath>bin\x64\Debug\</OutputPath>
41-
<DefineConstants>TRACE;DEBUG;BUILD64</DefineConstants>
41+
<DefineConstants>TRACE;DEBUG</DefineConstants>
4242
<DebugType>full</DebugType>
4343
<PlatformTarget>x64</PlatformTarget>
4444
<ErrorReport>prompt</ErrorReport>
4545
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
4646
</PropertyGroup>
4747
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
4848
<OutputPath>bin\x64\Release\</OutputPath>
49-
<DefineConstants>TRACE;BUILD64</DefineConstants>
49+
<DefineConstants>TRACE</DefineConstants>
5050
<Optimize>true</Optimize>
5151
<DebugType>pdbonly</DebugType>
5252
<PlatformTarget>x64</PlatformTarget>
@@ -58,7 +58,7 @@
5858
<OutputPath>bin\Debug\</OutputPath>
5959
<DefineConstants>DEBUG;TRACE</DefineConstants>
6060
<DebugType>full</DebugType>
61-
<PlatformTarget>x86</PlatformTarget>
61+
<PlatformTarget>AnyCPU</PlatformTarget>
6262
<ErrorReport>prompt</ErrorReport>
6363
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
6464
</PropertyGroup>

ZstdNet/CompressionOptions.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using System;
2-
#if BUILD64
3-
using size_t = System.UInt64;
4-
#else
5-
using size_t = System.UInt32;
6-
#endif
2+
using size_t = System.UIntPtr;
73

84
namespace ZstdNet
95
{

ZstdNet/Compressor.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using System;
2-
#if BUILD64
3-
using size_t = System.UInt64;
4-
#else
5-
using size_t = System.UInt32;
6-
#endif
2+
using size_t = System.UIntPtr;
73

84
namespace ZstdNet
95
{

ZstdNet/DecompressionOptions.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using System;
2-
#if BUILD64
3-
using size_t = System.UInt64;
4-
#else
5-
using size_t = System.UInt32;
6-
#endif
2+
using size_t = System.UIntPtr;
73

84
namespace ZstdNet
95
{

ZstdNet/Decompressor.cs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using System;
2-
#if BUILD64
3-
using size_t = System.UInt64;
4-
#else
5-
using size_t = System.UInt32;
6-
#endif
2+
using size_t = System.UIntPtr;
73

84
namespace ZstdNet
95
{
@@ -43,20 +39,20 @@ private void Dispose(bool disposing)
4339

4440
private bool disposed = false;
4541

46-
public byte[] Unwrap(byte[] src, size_t maxDecompressedSize = size_t.MaxValue)
42+
public byte[] Unwrap(byte[] src, int maxDecompressedSize = int.MaxValue)
4743
{
4844
return Unwrap(new ArraySegment<byte>(src), maxDecompressedSize);
4945
}
5046

51-
public byte[] Unwrap(ArraySegment<byte> src, size_t maxDecompressedSize = size_t.MaxValue)
47+
public byte[] Unwrap(ArraySegment<byte> src, int maxDecompressedSize = int.MaxValue)
5248
{
5349
if(src.Count == 0)
5450
return new byte[0];
5551

5652
var expectedDstSize = GetDecompressedSize(src);
5753
if(expectedDstSize == 0)
5854
throw new ZstdException("Can't create buffer for data with unspecified decompressed size (provide your own buffer to Unwrap instead)");
59-
if(expectedDstSize > maxDecompressedSize)
55+
if(expectedDstSize > (ulong)maxDecompressedSize)
6056
throw new ArgumentOutOfRangeException(string.Format("Decompressed size is too big ({0} bytes > authorized {1} bytes)", expectedDstSize, maxDecompressedSize));
6157
var dst = new byte[expectedDstSize];
6258

ZstdNet/DictBuilder.cs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
#if BUILD64
5-
using size_t = System.UInt64;
6-
#else
7-
using size_t = System.UInt32;
8-
#endif
4+
using size_t = System.UIntPtr;
95

106
namespace ZstdNet
117
{
@@ -16,12 +12,12 @@ public static byte[] TrainFromBuffer(ICollection<byte[]> samples, int dictCapaci
1612
var samplesBuffer = samples.SelectMany(sample => sample).ToArray();
1713
var samplesSizes = samples.Select(sample => (size_t)sample.Length).ToArray();
1814
var dictBuffer = new byte[dictCapacity];
19-
var dictSize = ExternMethods.ZDICT_trainFromBuffer(dictBuffer, (size_t)dictCapacity, samplesBuffer, samplesSizes, (uint)samples.Count).EnsureZdictSuccess();
15+
var dictSize = (int)ExternMethods.ZDICT_trainFromBuffer(dictBuffer, (size_t)dictCapacity, samplesBuffer, samplesSizes, (uint)samples.Count).EnsureZdictSuccess();
2016

21-
if (dictCapacity == (int)dictSize)
17+
if (dictCapacity == dictSize)
2218
return dictBuffer;
2319
var result = new byte[dictSize];
24-
Array.Copy(dictBuffer, result, (int)dictSize);
20+
Array.Copy(dictBuffer, result, dictSize);
2521
return result;
2622
}
2723

ZstdNet/ExternMethods.cs

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
using System;
2+
using System.IO;
3+
using System.Reflection;
24
using System.Runtime.InteropServices;
3-
#if BUILD64
4-
using size_t = System.UInt64;
5-
#else
6-
using size_t = System.UInt32;
7-
#endif
5+
using size_t = System.UIntPtr;
86

97
namespace ZstdNet
108
{
119
internal static class ExternMethods
1210
{
13-
#if BUILD64
14-
private const string DllName = "zstdlib_x64.dll";
15-
#else
16-
private const string DllName = "zstdlib_x86.dll";
17-
#endif
11+
static ExternMethods()
12+
{
13+
var assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
14+
if (assemblyPath == null)
15+
throw new InvalidOperationException("Failed to get assembly directory");
16+
var platform = Environment.Is64BitProcess ? "x64" : "x86";
17+
18+
var ok = SetDllDirectory(Path.Combine(assemblyPath, platform));
19+
if (!ok)
20+
throw new InvalidOperationException("Failed to set DLL directory");
21+
}
22+
23+
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
24+
private static extern bool SetDllDirectory(string path);
25+
26+
private const string DllName = "zstdlib.dll";
1827

1928
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
2029
public static extern size_t ZDICT_trainFromBuffer(byte[] dictBuffer, size_t dictBufferCapacity, byte[] samplesBuffer, size_t[] samplesSizes, uint nbSamples);

ZstdNet/ThrowHelper.cs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
using System;
22
using System.Runtime.InteropServices;
3-
#if BUILD64
4-
using size_t = System.UInt64;
5-
#else
6-
using size_t = System.UInt32;
7-
#endif
3+
using size_t = System.UIntPtr;
84

95
namespace ZstdNet
106
{
@@ -26,14 +22,15 @@ public static size_t EnsureZstdSuccess(this size_t returnValue)
2622

2723
private static void ThrowException(size_t returnValue, string message)
2824
{
29-
if(unchecked(0 - returnValue) == ZSTD_error_dstSize_tooSmall)
25+
var code = unchecked(0 - (uint) (ulong) returnValue); // Negate returnValue (UintPtr)
26+
if (code == ZSTD_error_dstSize_tooSmall)
3027
throw new InsufficientMemoryException(message);
3128
throw new ZstdException(message);
3229
}
3330

3431
// ReSharper disable once InconsistentNaming
3532
// NOTE that this const may change on zstdlib update
36-
private const size_t ZSTD_error_dstSize_tooSmall = 11;
33+
private const int ZSTD_error_dstSize_tooSmall = 11;
3734

3835
public static IntPtr EnsureZstdSuccess(this IntPtr returnValue)
3936
{

ZstdNet/ZstdNet.csproj

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
1616
<DebugSymbols>true</DebugSymbols>
1717
<OutputPath>bin\x64\Debug\</OutputPath>
18-
<DefineConstants>TRACE;DEBUG;BUILD64</DefineConstants>
18+
<DefineConstants>TRACE;DEBUG</DefineConstants>
1919
<DebugType>full</DebugType>
2020
<PlatformTarget>x64</PlatformTarget>
2121
<ErrorReport>prompt</ErrorReport>
2222
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
2323
</PropertyGroup>
2424
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
2525
<OutputPath>bin\x64\Release\</OutputPath>
26-
<DefineConstants>TRACE;BUILD64</DefineConstants>
26+
<DefineConstants>TRACE</DefineConstants>
2727
<Optimize>true</Optimize>
2828
<DebugType>pdbonly</DebugType>
2929
<PlatformTarget>x64</PlatformTarget>
@@ -53,7 +53,7 @@
5353
<OutputPath>bin\Debug\</OutputPath>
5454
<DefineConstants>DEBUG;TRACE</DefineConstants>
5555
<DebugType>full</DebugType>
56-
<PlatformTarget>x86</PlatformTarget>
56+
<PlatformTarget>AnyCPU</PlatformTarget>
5757
<ErrorReport>prompt</ErrorReport>
5858
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
5959
</PropertyGroup>
@@ -81,12 +81,12 @@
8181
<Compile Include="ThrowHelper.cs" />
8282
</ItemGroup>
8383
<ItemGroup>
84-
<None Include="build\zstdlib_x64.dll" Condition="'$(Platform)' == 'x64'">
85-
<Link>%(FileName)%(Extension)</Link>
84+
<None Include="build\x64\zstdlib.dll" Condition="'$(Platform)' == 'x64' OR '$(Platform)' == 'AnyCPU'">
85+
<Link>x64\%(FileName)%(Extension)</Link>
8686
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
8787
</None>
88-
<None Include="build\zstdlib_x86.dll" Condition="'$(Platform)' != 'x64'">
89-
<Link>%(FileName)%(Extension)</Link>
88+
<None Include="build\x86\zstdlib.dll" Condition="'$(Platform)' == 'x86' OR '$(Platform)' == 'AnyCPU'">
89+
<Link>x86\%(FileName)%(Extension)</Link>
9090
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
9191
</None>
9292
</ItemGroup>

ZstdNet/build/ZstdNet.targets

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<ItemGroup>
4-
<None Include="$(MSBuildThisFileDirectory)\zstdlib_x64.dll" Condition="'$(Platform)' == 'x64'">
5-
<Link>%(FileName)%(Extension)</Link>
4+
<None Include="$(MSBuildThisFileDirectory)x64\zstdlib.dll" Condition="'$(Platform)' == 'x64' OR '$(Platform)' == 'AnyCPU'">
5+
<Link>x64\%(FileName)%(Extension)</Link>
66
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
77
</None>
8-
<None Include="$(MSBuildThisFileDirectory)\zstdlib_x86.dll" Condition="'$(Platform)' != 'x64'">
9-
<Link>%(FileName)%(Extension)</Link>
8+
<None Include="$(MSBuildThisFileDirectory)x86\zstdlib.dll" Condition="'$(Platform)' == 'x86' OR '$(Platform)' == 'AnyCPU'">
9+
<Link>x86\%(FileName)%(Extension)</Link>
1010
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1111
</None>
1212
</ItemGroup>
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)