Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize zlib #38

Merged
merged 21 commits into from
Oct 2, 2024
Merged
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
4 changes: 2 additions & 2 deletions Fuyu.Common/Fuyu.Common.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>net8.0;netstandard2.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../Zlib.Managed/Zlib.Managed.csproj" />
<ProjectReference Include="../Fuyu.Compression/Fuyu.Compression.csproj" />
</ItemGroup>

</Project>
4 changes: 0 additions & 4 deletions Fuyu.Common/Networking/Context.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using Fuyu.Common.Compression;
using Fuyu.Common.Serialization;

namespace Fuyu.Common.Networking
{
Expand Down
2 changes: 0 additions & 2 deletions Fuyu.Common/Networking/Controller.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Text;
using Fuyu.Common.Compression;

namespace Fuyu.Common.Networking
{
Expand Down
8 changes: 4 additions & 4 deletions Fuyu.Common/Networking/EftHttpClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Net.Http;
using Fuyu.Common.Compression;
using Fuyu.Compression;

namespace Fuyu.Common.Networking
{
Expand All @@ -15,14 +15,14 @@ public EftHttpClient(string address, string sessionId) : base(address)

protected override byte[] OnSendBody(byte[] body)
{
return Zlib.Compress(body, ZlibCompression.Level9);
return MemoryZlib.Compress(body, CompressionLevel.BestCompression);
}

protected override byte[] OnReceiveBody(byte[] body)
{
if (Zlib.IsCompressed(body))
if (MemoryZlib.IsCompressed(body))
{
body = Zlib.Decompress(body);
body = MemoryZlib.Decompress(body);
}

return body;
Expand Down
8 changes: 4 additions & 4 deletions Fuyu.Common/Networking/HttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Fuyu.Common.Compression;
using Fuyu.Compression;
using Fuyu.Common.Serialization;

namespace Fuyu.Common.Networking
Expand All @@ -25,9 +25,9 @@ public async Task<byte[]> GetBinaryAsync()
await Request.InputStream.CopyToAsync(ms);
var body = ms.ToArray();

if (Zlib.IsCompressed(body))
if (MemoryZlib.IsCompressed(body))
{
body = Zlib.Decompress(body);
body = MemoryZlib.Decompress(body);
}

return body;
Expand Down Expand Up @@ -61,7 +61,7 @@ protected async Task SendAsync(byte[] data, string mime, HttpStatusCode status,

if (zipped)
{
data = Zlib.Compress(data, ZlibCompression.Level9);
data = MemoryZlib.Compress(data, CompressionLevel.BestCompression);
}

Response.StatusCode = (int)status;
Expand Down
4 changes: 0 additions & 4 deletions Fuyu.Common/Networking/WsContext.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Fuyu.Common.Compression;
using Fuyu.Common.Serialization;

namespace Fuyu.Common.Networking
{
Expand Down
100 changes: 100 additions & 0 deletions Fuyu.Compression/ComponentAce.Compression.Libs.zlib/SimpleZlib.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Text;
using Elskom.Generic.Libs;
using Fuyu.Compression;

namespace ComponentAce.Compression.Libs.zlib
{
public static class SimpleZlib
{
public const int bufsize = 4096;

public static string Compress(string text, int compressLevel, Encoding encoding = null)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}

if (encoding == null)
{
encoding = Encoding.UTF8;
}

var bytes = encoding.GetBytes(text);
var deflated = MemoryZlib.Compress(bytes, (CompressionLevel)compressLevel);

return encoding.GetString(deflated);
}

public static int CompressToBytesNonAlloc(string text, int compressLevel, byte[] encodingGetBytesBuffer, byte[] resultBuffer, Encoding encoding = null)
{
if (encoding == null)
{
encoding = Encoding.UTF8;
}

_ = encoding.GetBytes(text, 0, text.Length, encodingGetBytesBuffer, 0);
resultBuffer = MemoryZlib.Compress(encodingGetBytesBuffer, (CompressionLevel)compressLevel);

return resultBuffer.Length;
}

public static byte[] CompressToBytes(string text, int compressLevel, Encoding encoding = null)
{
if (encoding == null)
{
encoding = Encoding.UTF8;
}

var bytes = encoding.GetBytes(text);

return MemoryZlib.Compress(bytes, (CompressionLevel)compressLevel);
}

public static byte[] CompressToBytes(byte[] bytes, int length, int compressLevel)
{
return MemoryZlib.Compress(bytes, (CompressionLevel)compressLevel);
}

public static int CompressWithZStream(ref ZStream zstream, byte[] bytes, int startIndex, int length, byte[] compressedBytes, int compressLevel)
{
throw new NotImplementedException();
}

public static string Decompress(string compressed, Encoding encoding = null)
{
if (string.IsNullOrEmpty(compressed))
{
return string.Empty;
}

if (encoding == null)
{
encoding = Encoding.UTF8;
}

var bytes = encoding.GetBytes(compressed);
var inflated = MemoryZlib.Decompress(bytes);

return encoding.GetString(inflated);
}

public static string Decompress(byte[] bytes, Encoding encoding = null)
{
if (encoding == null)
{
encoding = Encoding.UTF8;
}

var inflated = MemoryZlib.Decompress(bytes);

return encoding.GetString(inflated);
}

public static byte[] DecompressToBytes(byte[] compressedBytes)
{
return MemoryZlib.Decompress(compressedBytes);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,58 +1,73 @@
namespace Fuyu.Common.Compression
namespace Fuyu.Compression
{
/// <summary>
/// Compression levels for zlib.
/// </summary>
public enum ZlibCompression
public enum CompressionLevel
{
/// <summary>
/// No compression.
/// </summary>
NoCompression,
NoCompression = 0,

/// <summary>
/// Compression level 1. Best speed.
/// </summary>
Level1,
Level1 = 1,

/// <summary>
/// Compression level 2.
/// </summary>
Level2,
Level2 = 2,

/// <summary>
/// Compression level 3.
/// </summary>
Level3,
Level3 = 3,

/// <summary>
/// Compression level 4.
/// </summary>
Level4,
Level4 = 4,

/// <summary>
/// Compression level 5.
/// </summary>
Level5,
Level5 = 5,

/// <summary>
/// Compression level 6. Default.
/// </summary>
Level6,
Level6 = 6,

/// <summary>
/// Compression level 7.
/// </summary>
Level7,
Level7 = 7,

/// <summary>
/// Compression level 8.
/// </summary>
Level8,
Level8 = 8,

/// <summary>
/// Compression level 9. Highest compression.
/// </summary>
Level9
Level9 = 9,

/// <summary>
/// Optimal balance.
/// </summary>
DefaultCompression = Level6,

/// <summary>
/// Best speed.
/// </summary>
BestSpeed = Level1,

/// <summary>
/// Highest compression.
/// </summary>
BestCompression = Level9
}
}
57 changes: 57 additions & 0 deletions Fuyu.Compression/Elskom/Adler32.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;

namespace Elskom.Generic.Libs
{
internal static class Adler32
{
// largest prime smaller than 65536
private const int BASE = 65521;

// NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
private const int NMAX = 5552;

public static long Calculate(long adler, ReadOnlySpan<byte> buffer, int index, int length)
{
if (buffer == null)
{
return 1L;
}

var s1 = adler & 0xFFFF;
var s2 = (adler >> 16) & 0xFFFF;
int k;

while (length > 0)
{
k = length < NMAX ? length : NMAX;
length -= k;

while (k >= 16)
{
for (var i = 0; i < 16; ++i)
{
s1 += buffer[index++] & 0xFF;
s2 += s1;
}

k -= 16;
}

if (k != 0)
{
do
{
s1 += buffer[index++] & 0xFF;
s2 += s1;
}
while (--k != 0);
}

s1 %= BASE;
s2 %= BASE;
}

return (s2 << 16) | s1;
}
}
}
9 changes: 9 additions & 0 deletions Fuyu.Compression/Elskom/CompressionFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Elskom.Generic.Libs
{
internal enum CompressionFunction
{
Stored = 0,
Fast = 1,
Slow = 2
}
}
20 changes: 20 additions & 0 deletions Fuyu.Compression/Elskom/CompressionStrategy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Elskom.Generic.Libs
{
internal enum CompressionStrategy
{
/// <summary>
/// The default compression. Used for normal data.
/// </summary>
DefaultStrategy = 0,

/// <summary>
/// Filtered compression. Used for data produced by a filter (or predictor).
/// </summary>
Filtered = 1,

/// <summary>
/// Force Huffman encoding only (no string match).
/// </summary>
HuffmanOnly = 2
}
}
Loading