Bring ZLib in locally. (#132)

This commit is contained in:
Kamron Batman 2020-05-06 20:07:28 -07:00 • committed by GitHub
parent 477c79bb4d
commit 2347533b9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 331 additions and 260 deletions

View file

@ -1,4 +1,3 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
@ -25,7 +24,7 @@ namespace Server.Accounting.Security
static Argon2()
{
if (RuntimeUtility.Unix)
if (RuntimeUtility.IsUnix)
Library = new UnixArgon2();
else
Library = new WindowsArgon2();

View file

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RuntimeIdentifiers>win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
<PackageVersion>1.0.0</PackageVersion>
<PackageVersion>1.1.0</PackageVersion>
<RootNamespace>Server</RootNamespace>
<AssemblyName>Argon2.Bindings</AssemblyName>
<PlatformTarget>x64</PlatformTarget>
@ -9,10 +9,9 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>true</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<Configurations>Debug;Release;Analyze</Configurations>
<DefineConstants Condition="'$(RuntimeIdentifier)' == 'win-x64'">WINDOWS</DefineConstants>
<AssemblyVersion>1.0.0</AssemblyVersion>
<Platforms>x64</Platforms>
</PropertyGroup>
@ -33,10 +32,4 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Target Name="CopyPackage" AfterTargets="Pack">
<Exec Command="dotnet nuget push $(OutputPath)..\..\$(PackageId).$(PackageVersion).nupkg -s local" />
</Target>
<!-- <Target Name="CopyPackage" AfterTargets="Pack">
<Exec Command="nuget add $(OutputPath)..\$(PackageId).$(PackageVersion).nupkg -Source ../../packages" />
</Target> -->
</Project>

View file

@ -4,10 +4,10 @@ namespace Server
{
internal static class RuntimeUtility
{
public static bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
public static bool IsDarwin = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
public static bool IsFreeBSD = RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD);
public static bool IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || IsFreeBSD;
public static bool Unix = IsDarwin || IsFreeBSD || IsLinux;
internal static readonly bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
internal static readonly bool IsDarwin = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
internal static readonly bool IsFreeBSD = RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD);
internal static readonly bool IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || IsFreeBSD;
internal static readonly bool IsUnix = IsDarwin || IsFreeBSD || IsLinux;
}
}

View file

@ -10,6 +10,7 @@ using Server.Items;
using Server.Mobiles;
using Server.Network;
using Server.Spells;
using Server.Compression;
namespace Server.Multis
{
@ -2324,7 +2325,7 @@ namespace Server.Multis
byte[] inflatedBuffer = m_PlaneBuffers[i];
int deflatedLength = m_DeflatedBuffer.Length;
ZLibError ce = NetworkCompression.Pack(m_DeflatedBuffer, ref deflatedLength, inflatedBuffer, size,
ZLibError ce = ZLib.Pack(m_DeflatedBuffer, ref deflatedLength, inflatedBuffer, size,
ZLibQuality.Default);
if (ce != ZLibError.Okay)
@ -2360,7 +2361,7 @@ namespace Server.Multis
byte[] inflatedBuffer = m_StairBuffers[i];
int deflatedLength = m_DeflatedBuffer.Length;
ZLibError ce = NetworkCompression.Pack(m_DeflatedBuffer, ref deflatedLength, inflatedBuffer, size,
ZLibError ce = ZLib.Pack(m_DeflatedBuffer, ref deflatedLength, inflatedBuffer, size,
ZLibQuality.Default);
if (ce != ZLibError.Okay)

View file

@ -66,7 +66,8 @@
<PackageReference Include="Microsoft.AspNetCore.Connections.Abstractions" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.3" />
<PackageReference Include="Argon2.Bindings" Version="1.0.0" />
<PackageReference Include="Argon2.Bindings" Version="1.1.0" />
<PackageReference Include="Zlib.Bindings" Version="1.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)|$(Platform)'=='Analyze|AnyCPU'">
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="all" />

View file

@ -22,7 +22,7 @@ using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using Server.Network;
using Server.Compression;
namespace Server
{
@ -174,7 +174,7 @@ namespace Server
if (compressionMethod == 1)
{
data = new byte[decompressedSize];
NetworkCompression.Unpack(data, ref decompressedSize, sourceData, compressedSize);
ZLib.Unpack(data, ref decompressedSize, sourceData, compressedSize);
}
else
{

View file

@ -19,8 +19,6 @@
***************************************************************************/
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Server.Network
{
@ -45,9 +43,6 @@ namespace Server.Network
// If our input exceeds this length, we cannot possibly compress it within the buffer
private const int DefiniteOverflow = (BufferSize * 8 - TerminalCodeLength) / MinimalCodeLength;
// If our input exceeds this length, we may potentially overflow the buffer
private const int PossibleOverflow = (BufferSize * 8 - TerminalCodeLength) / MaximalCodeLength;
private static readonly int[] _huffmanTable =
{
0x2, 0x000, 0x5, 0x01F, 0x6, 0x022, 0x7, 0x034, 0x7, 0x075, 0x6, 0x028, 0x6, 0x03B, 0x7, 0x032,
@ -85,16 +80,6 @@ namespace Server.Network
0x4, 0x00D
};
public static readonly ICompressor Compressor;
static NetworkCompression()
{
if (Core.Unix)
Compressor = new UnixCompressor();
else
Compressor = new Compressor();
}
public static unsafe void Compress(ReadOnlySpan<byte> input, int offset, int count, Span<byte> output, out int length)
{
if (input == null) throw new ArgumentNullException(nameof(input));
@ -181,194 +166,5 @@ namespace Server.Network
}
}
}
public static int MaxPackSize(int sourceLength) => (int)Compressor.CompressBound((ulong)sourceLength);
public static unsafe ZLibError Pack(Span<byte> dest, ref int destLength, ReadOnlySpan<byte> source, int sourceLength)
{
var destLengthLong = (ulong)destLength;
fixed (byte* dPtr = &MemoryMarshal.GetReference(dest), sPtr = &MemoryMarshal.GetReference(source))
{
var e = Compressor.Compress(Unsafe.AsRef<int>(dPtr), ref destLengthLong, Unsafe.AsRef<int>(sPtr),
(ulong)sourceLength);
destLength = (int)destLengthLong;
return e;
}
}
public static unsafe ZLibError Pack(Span<byte> dest, ref int destLength, ReadOnlySpan<byte> source, ZLibQuality quality)
{
var destLengthLong = (ulong)destLength;
fixed (byte* dPtr = &MemoryMarshal.GetReference(dest), sPtr = &MemoryMarshal.GetReference(source))
{
var e = Compressor.Compress(Unsafe.AsRef<int>(dPtr), ref destLengthLong, Unsafe.AsRef<int>(sPtr),
(ulong)source.Length, quality);
destLength = (int)destLengthLong;
return e;
}
}
public static unsafe ZLibError Pack(Span<byte> dest, ref int destLength, ReadOnlySpan<byte> source, int sourceLength,
ZLibQuality quality)
{
var destLengthLong = (ulong)destLength;
fixed (byte* dPtr = &MemoryMarshal.GetReference(dest), sPtr = &MemoryMarshal.GetReference(source))
{
var e = Compressor.Compress(Unsafe.AsRef<int>(dPtr), ref destLengthLong, Unsafe.AsRef<int>(sPtr),
(ulong)sourceLength, quality);
destLength = (int)destLengthLong;
return e;
}
}
public static unsafe ZLibError Unpack(Span<byte> dest, ref int destLength, ReadOnlySpan<byte> source, int sourceLength)
{
var destLengthLong = (ulong)destLength;
fixed (byte* dPtr = &MemoryMarshal.GetReference(dest), sPtr = &MemoryMarshal.GetReference(source))
{
var e = Compressor.Decompress(Unsafe.AsRef<int>(dPtr), ref destLengthLong, Unsafe.AsRef<int>(sPtr),
(ulong)sourceLength);
destLength = (int)destLengthLong;
return e;
}
}
public static ulong MaxPackSize(ulong sourceLength) => Compressor.CompressBound(sourceLength);
public static unsafe ZLibError Pack(Span<byte> dest, ref ulong destLength, ReadOnlySpan<byte> source, ulong sourceLength)
{
fixed (byte* dPtr = &MemoryMarshal.GetReference(dest), sPtr = &MemoryMarshal.GetReference(source))
{
return Compressor.Compress(Unsafe.AsRef<int>(dPtr), ref destLength, Unsafe.AsRef<int>(sPtr), sourceLength);
}
}
public static unsafe ZLibError Pack(Span<byte> dest, ref ulong destLength, ReadOnlySpan<byte> source,
ZLibQuality quality)
{
fixed (byte* dPtr = &MemoryMarshal.GetReference(dest), sPtr = &MemoryMarshal.GetReference(source))
{
return Compressor.Compress(Unsafe.AsRef<int>(dPtr), ref destLength, Unsafe.AsRef<int>(sPtr), (ulong)source.Length,
quality);
}
}
public static unsafe ZLibError Pack(Span<byte> dest, ref ulong destLength, ReadOnlySpan<byte> source, ulong sourceLength,
ZLibQuality quality)
{
fixed (byte* dPtr = &MemoryMarshal.GetReference(dest), sPtr = &MemoryMarshal.GetReference(source))
{
return Compressor.Compress(Unsafe.AsRef<int>(dPtr), ref destLength, Unsafe.AsRef<int>(sPtr), sourceLength, quality);
}
}
public static unsafe ZLibError Unpack(Span<byte> dest, ref ulong destLength, ReadOnlySpan<byte> source,
ulong sourceLength)
{
fixed (byte* dPtr = &MemoryMarshal.GetReference(dest), sPtr = &MemoryMarshal.GetReference(source))
{
return Compressor.Decompress(Unsafe.AsRef<int>(dPtr), ref destLength, Unsafe.AsRef<int>(sPtr), sourceLength);
}
}
}
public interface ICompressor
{
string Version { get; }
ZLibError Compress(in int dest, ref ulong destLength, in int source, ulong sourceLength);
ZLibError Compress(in int dest, ref ulong destLength, in int source, ulong sourceLength, ZLibQuality quality);
ZLibError Decompress(in int dest, ref ulong destLength, in int source, ulong sourceLength);
ulong CompressBound(ulong sourceLength);
}
public class Compressor : ICompressor
{
public string Version => SafeNativeMethods.zlibVersion();
public ZLibError Compress(in int dest, ref ulong destLength, in int source, ulong sourceLength) =>
SafeNativeMethods.compress(dest, ref destLength, source, sourceLength);
public ZLibError Compress(in int dest, ref ulong destLength, in int source, ulong sourceLength,
ZLibQuality quality) => SafeNativeMethods.compress2(dest, ref destLength, source, sourceLength, quality);
public ZLibError Decompress(in int dest, ref ulong destLength, in int source, ulong sourceLength) =>
SafeNativeMethods.uncompress(dest, ref destLength, source, sourceLength);
public ulong CompressBound(ulong sourceLength) => SafeNativeMethods.compressBound(sourceLength);
private static class SafeNativeMethods
{
[DllImport("zlib", CharSet = CharSet.Unicode)]
internal static extern string zlibVersion();
[DllImport("zlib")]
internal static extern ZLibError compress(in int dest, ref ulong destLength, in int source, ulong sourceLength);
[DllImport("zlib")]
internal static extern ZLibError compress2(in int dest, ref ulong destLength, in int source, ulong sourceLength,
ZLibQuality quality);
[DllImport("zlib")]
internal static extern ZLibError uncompress(in int dest, ref ulong destLength, in int source, ulong sourceLength);
[DllImport("zlib")]
internal static extern ulong compressBound(ulong sourceLen);
}
}
public class UnixCompressor : ICompressor
{
public string Version => SafeNativeMethods.zlibVersion();
public ZLibError Compress(in int dest, ref ulong destLength, in int source, ulong sourceLength) =>
SafeNativeMethods.compress(dest, ref destLength, source, sourceLength);
public ZLibError Compress(in int dest, ref ulong destLength, in int source, ulong sourceLength,
ZLibQuality quality) => SafeNativeMethods.compress2(dest, ref destLength, source, sourceLength, quality);
public ZLibError Decompress(in int dest, ref ulong destLength, in int source, ulong sourceLength) =>
SafeNativeMethods.uncompress(dest, ref destLength, source, sourceLength);
public ulong CompressBound(ulong sourceLength) => SafeNativeMethods.compressBound(sourceLength);
internal static class SafeNativeMethods
{
[DllImport("libz", CharSet = CharSet.Unicode)]
internal static extern string zlibVersion();
[DllImport("libz")]
internal static extern ZLibError compress(in int dest, ref ulong destLength, in int source, ulong sourceLength);
[DllImport("libz")]
internal static extern ZLibError compress2(in int dest, ref ulong destLength, in int source, ulong sourceLength,
ZLibQuality quality);
[DllImport("libz")]
internal static extern ZLibError uncompress(in int dest, ref ulong destLength, in int source, ulong sourceLength);
[DllImport("libz")]
internal static extern ulong compressBound(ulong sourceLen);
}
}
public enum ZLibError
{
VersionError = -6,
BufferError = -5,
MemoryError = -4,
DataError = -3,
StreamError = -2,
FileError = -1,
Okay = 0,
StreamEnd = 1,
NeedDictionary = 2
}
public enum ZLibQuality
{
Default = -1,
None = 0,
Speed = 1,
Size = 9
}
}

View file

@ -26,6 +26,7 @@ using System.Net;
using System.Text;
using System.Threading;
using Server.Accounting;
using Server.Compression;
using Server.ContextMenus;
using Server.Gumps;
using Server.HuePickers;
@ -2119,7 +2120,7 @@ namespace Server.Network
var packLength = wantLength;
NetworkCompression.Pack(packBuffer, ref packLength, buffer, length, ZLibQuality.Default);
ZLib.Pack(packBuffer, ref packLength, buffer, length, ZLibQuality.Default);
Stream.Write(4 + packLength);
Stream.Write(length);

View file

@ -29,6 +29,11 @@
<Target Name="CleanPub" AfterTargets="Clean">
<Message Text="Removing distribution files..." />
<Delete Files="$(SolutionDir)\Distribution\libuv.dylib" ContinueOnError="true" />
<Delete Files="$(SolutionDir)\Distribution\libuv.so" ContinueOnError="true" />
<Delete Files="$(SolutionDir)\Distribution\libuv.dll" ContinueOnError="true" />
<Delete Files="$(SolutionDir)\Distribution\zlib.dll" ContinueOnError="true" />
<Delete Files="$(SolutionDir)\Distribution\libz.dylib" ContinueOnError="true" />
<Delete Files="$(SolutionDir)\Distribution\libz.so" ContinueOnError="true" />
<Delete Files="$(SolutionDir)\Distribution\Microsoft.AspNetCore.Connections.Abstractions.dll" ContinueOnError="true" />
<Delete Files="$(SolutionDir)\Distribution\Microsoft.AspNetCore.Http.Features.dll" ContinueOnError="true" />
<Delete Files="$(SolutionDir)\Distribution\Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.dll" ContinueOnError="true" />
@ -67,16 +72,17 @@
<Link>libdrng.dll</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Assemblies\zlib.dll" Condition="'$(RuntimeIdentifier)' == 'win-x64'">
<!-- <Content Include="Assemblies\zlib.dll" Condition="'$(RuntimeIdentifier)' == 'win-x64'">
<Link>zlib.dll</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</Content> -->
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Connections.Abstractions" Version="3.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.3" />
<PackageReference Include="System.IO.Pipelines" Version="4.7.1" />
<PackageReference Include="Zlib.Bindings" Version="1.0.0" />
</ItemGroup>
<ItemGroup>
<Compile Remove="Kestrel\Transport.Libuv\**" />

13
Projects/ZLib/Utility.cs Normal file
View file

@ -0,0 +1,13 @@
using System.Runtime.InteropServices;
namespace Server
{
internal static class RuntimeUtility
{
internal static readonly bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
internal static readonly bool IsDarwin = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
internal static readonly bool IsFreeBSD = RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD);
internal static readonly bool IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || IsFreeBSD;
internal static readonly bool IsUnix = IsDarwin || IsFreeBSD || IsLinux;
}
}

208
Projects/ZLib/ZLib.cs Normal file
View file

@ -0,0 +1,208 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Server.Compression
{
public static class ZLib
{
public static readonly ICompressor Compressor;
static ZLib()
{
if (RuntimeUtility.IsUnix)
Compressor = new UnixCompressor();
else
Compressor = new Compressor();
}
public static int MaxPackSize(int sourceLength) => (int)Compressor.CompressBound((ulong)sourceLength);
public static unsafe ZLibError Pack(Span<byte> dest, ref int destLength, ReadOnlySpan<byte> source, int sourceLength)
{
var destLengthLong = (ulong)destLength;
fixed (byte* dPtr = &MemoryMarshal.GetReference(dest), sPtr = &MemoryMarshal.GetReference(source))
{
var e = Compressor.Compress(Unsafe.AsRef<int>(dPtr), ref destLengthLong, Unsafe.AsRef<int>(sPtr),
(ulong)sourceLength);
destLength = (int)destLengthLong;
return e;
}
}
public static unsafe ZLibError Pack(Span<byte> dest, ref int destLength, ReadOnlySpan<byte> source, ZLibQuality quality)
{
var destLengthLong = (ulong)destLength;
fixed (byte* dPtr = &MemoryMarshal.GetReference(dest), sPtr = &MemoryMarshal.GetReference(source))
{
var e = Compressor.Compress(Unsafe.AsRef<int>(dPtr), ref destLengthLong, Unsafe.AsRef<int>(sPtr),
(ulong)source.Length, quality);
destLength = (int)destLengthLong;
return e;
}
}
public static unsafe ZLibError Pack(Span<byte> dest, ref int destLength, ReadOnlySpan<byte> source, int sourceLength,
ZLibQuality quality)
{
var destLengthLong = (ulong)destLength;
fixed (byte* dPtr = &MemoryMarshal.GetReference(dest), sPtr = &MemoryMarshal.GetReference(source))
{
var e = Compressor.Compress(Unsafe.AsRef<int>(dPtr), ref destLengthLong, Unsafe.AsRef<int>(sPtr),
(ulong)sourceLength, quality);
destLength = (int)destLengthLong;
return e;
}
}
public static unsafe ZLibError Unpack(Span<byte> dest, ref int destLength, ReadOnlySpan<byte> source, int sourceLength)
{
var destLengthLong = (ulong)destLength;
fixed (byte* dPtr = &MemoryMarshal.GetReference(dest), sPtr = &MemoryMarshal.GetReference(source))
{
var e = Compressor.Decompress(Unsafe.AsRef<int>(dPtr), ref destLengthLong, Unsafe.AsRef<int>(sPtr),
(ulong)sourceLength);
destLength = (int)destLengthLong;
return e;
}
}
public static ulong MaxPackSize(ulong sourceLength) => Compressor.CompressBound(sourceLength);
public static unsafe ZLibError Pack(Span<byte> dest, ref ulong destLength, ReadOnlySpan<byte> source, ulong sourceLength)
{
fixed (byte* dPtr = &MemoryMarshal.GetReference(dest), sPtr = &MemoryMarshal.GetReference(source))
{
return Compressor.Compress(Unsafe.AsRef<int>(dPtr), ref destLength, Unsafe.AsRef<int>(sPtr), sourceLength);
}
}
public static unsafe ZLibError Pack(Span<byte> dest, ref ulong destLength, ReadOnlySpan<byte> source,
ZLibQuality quality)
{
fixed (byte* dPtr = &MemoryMarshal.GetReference(dest), sPtr = &MemoryMarshal.GetReference(source))
{
return Compressor.Compress(Unsafe.AsRef<int>(dPtr), ref destLength, Unsafe.AsRef<int>(sPtr), (ulong)source.Length,
quality);
}
}
public static unsafe ZLibError Pack(Span<byte> dest, ref ulong destLength, ReadOnlySpan<byte> source, ulong sourceLength,
ZLibQuality quality)
{
fixed (byte* dPtr = &MemoryMarshal.GetReference(dest), sPtr = &MemoryMarshal.GetReference(source))
{
return Compressor.Compress(Unsafe.AsRef<int>(dPtr), ref destLength, Unsafe.AsRef<int>(sPtr), sourceLength, quality);
}
}
public static unsafe ZLibError Unpack(Span<byte> dest, ref ulong destLength, ReadOnlySpan<byte> source,
ulong sourceLength)
{
fixed (byte* dPtr = &MemoryMarshal.GetReference(dest), sPtr = &MemoryMarshal.GetReference(source))
{
return Compressor.Decompress(Unsafe.AsRef<int>(dPtr), ref destLength, Unsafe.AsRef<int>(sPtr), sourceLength);
}
}
}
public interface ICompressor
{
string Version { get; }
ZLibError Compress(in int dest, ref ulong destLength, in int source, ulong sourceLength);
ZLibError Compress(in int dest, ref ulong destLength, in int source, ulong sourceLength, ZLibQuality quality);
ZLibError Decompress(in int dest, ref ulong destLength, in int source, ulong sourceLength);
ulong CompressBound(ulong sourceLength);
}
public class Compressor : ICompressor
{
public string Version => SafeNativeMethods.zlibVersion();
public ZLibError Compress(in int dest, ref ulong destLength, in int source, ulong sourceLength) =>
SafeNativeMethods.compress(dest, ref destLength, source, sourceLength);
public ZLibError Compress(in int dest, ref ulong destLength, in int source, ulong sourceLength,
ZLibQuality quality) => SafeNativeMethods.compress2(dest, ref destLength, source, sourceLength, quality);
public ZLibError Decompress(in int dest, ref ulong destLength, in int source, ulong sourceLength) =>
SafeNativeMethods.uncompress(dest, ref destLength, source, sourceLength);
public ulong CompressBound(ulong sourceLength) => SafeNativeMethods.compressBound(sourceLength);
private static class SafeNativeMethods
{
[DllImport("zlib", CharSet = CharSet.Unicode)]
internal static extern string zlibVersion();
[DllImport("zlib")]
internal static extern ZLibError compress(in int dest, ref ulong destLength, in int source, ulong sourceLength);
[DllImport("zlib")]
internal static extern ZLibError compress2(in int dest, ref ulong destLength, in int source, ulong sourceLength,
ZLibQuality quality);
[DllImport("zlib")]
internal static extern ZLibError uncompress(in int dest, ref ulong destLength, in int source, ulong sourceLength);
[DllImport("zlib")]
internal static extern ulong compressBound(ulong sourceLen);
}
}
public class UnixCompressor : ICompressor
{
public string Version => SafeNativeMethods.zlibVersion();
public ZLibError Compress(in int dest, ref ulong destLength, in int source, ulong sourceLength) =>
SafeNativeMethods.compress(dest, ref destLength, source, sourceLength);
public ZLibError Compress(in int dest, ref ulong destLength, in int source, ulong sourceLength,
ZLibQuality quality) => SafeNativeMethods.compress2(dest, ref destLength, source, sourceLength, quality);
public ZLibError Decompress(in int dest, ref ulong destLength, in int source, ulong sourceLength) =>
SafeNativeMethods.uncompress(dest, ref destLength, source, sourceLength);
public ulong CompressBound(ulong sourceLength) => SafeNativeMethods.compressBound(sourceLength);
internal static class SafeNativeMethods
{
[DllImport("libz", CharSet = CharSet.Unicode)]
internal static extern string zlibVersion();
[DllImport("libz")]
internal static extern ZLibError compress(in int dest, ref ulong destLength, in int source, ulong sourceLength);
[DllImport("libz")]
internal static extern ZLibError compress2(in int dest, ref ulong destLength, in int source, ulong sourceLength,
ZLibQuality quality);
[DllImport("libz")]
internal static extern ZLibError uncompress(in int dest, ref ulong destLength, in int source, ulong sourceLength);
[DllImport("libz")]
internal static extern ulong compressBound(ulong sourceLen);
}
}
public enum ZLibError
{
VersionError = -6,
BufferError = -5,
MemoryError = -4,
DataError = -3,
StreamError = -2,
FileError = -1,
Okay = 0,
StreamEnd = 1,
NeedDictionary = 2
}
public enum ZLibQuality
{
Default = -1,
None = 0,
Speed = 1,
Size = 9
}
}

35
Projects/ZLib/ZLib.csproj Normal file
View file

@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RuntimeIdentifiers>win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
<PackageVersion>1.0.0</PackageVersion>
<RootNamespace>Server</RootNamespace>
<AssemblyName>ZLib.Bindings</AssemblyName>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>8.0</LangVersion>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>true</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<Configurations>Debug;Release;Analyze</Configurations>
<AssemblyVersion>1.0.0</AssemblyVersion>
<Platforms>x64</Platforms>
</PropertyGroup>
<ItemGroup>
<Content Include="runtimes\win-x64\native\zlib.dll">
<Pack>true</Pack>
<PackagePath>runtimes/win-x64/native</PackagePath>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="runtimes\osx-x64\native\libz.dylib">
<Pack>true</Pack>
<PackagePath>runtimes/osx-x64/native</PackagePath>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="runtimes\linux-x64\native\libz.so">
<Pack>true</Pack>
<PackagePath>runtimes/linux-x64/native</PackagePath>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

Binary file not shown.

Binary file not shown.