2026-03-21 20:07:20 +01:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.IO.Compression;
|
2014-09-04 13:58:49 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient.Protocol.Handlers
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Quick Zlib compression handling for network packet compression.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static class ZlibUtils
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Compress a byte array into another bytes array using Zlib compression
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="to_compress">Data to compress</param>
|
|
|
|
|
|
/// <returns>Compressed data as a byte array</returns>
|
|
|
|
|
|
public static byte[] Compress(byte[] to_compress)
|
|
|
|
|
|
{
|
2026-03-21 20:07:20 +01:00
|
|
|
|
using MemoryStream memstream = new();
|
|
|
|
|
|
using (ZLibStream stream = new(memstream, CompressionMode.Compress, leaveOpen: true))
|
2014-09-04 13:58:49 +02:00
|
|
|
|
{
|
2026-03-21 20:07:20 +01:00
|
|
|
|
stream.Write(to_compress, 0, to_compress.Length);
|
2014-09-04 13:58:49 +02:00
|
|
|
|
}
|
2026-03-21 20:07:20 +01:00
|
|
|
|
|
|
|
|
|
|
return memstream.ToArray();
|
2014-09-04 13:58:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Decompress a byte array into another byte array of the specified size
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="to_decompress">Data to decompress</param>
|
|
|
|
|
|
/// <param name="size_uncompressed">Size of the data once decompressed</param>
|
|
|
|
|
|
/// <returns>Decompressed data as a byte array</returns>
|
|
|
|
|
|
public static byte[] Decompress(byte[] to_decompress, int size_uncompressed)
|
|
|
|
|
|
{
|
2026-03-21 20:07:20 +01:00
|
|
|
|
using MemoryStream compressedStream = new(to_decompress, writable: false);
|
|
|
|
|
|
using ZLibStream stream = new(compressedStream, CompressionMode.Decompress);
|
|
|
|
|
|
|
2014-09-04 13:58:49 +02:00
|
|
|
|
byte[] packetData_decompressed = new byte[size_uncompressed];
|
2026-03-21 20:07:20 +01:00
|
|
|
|
int totalRead = 0;
|
|
|
|
|
|
while (totalRead < size_uncompressed)
|
|
|
|
|
|
{
|
|
|
|
|
|
int read = stream.Read(packetData_decompressed, totalRead, size_uncompressed - totalRead);
|
|
|
|
|
|
if (read <= 0)
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
totalRead += read;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-09-04 13:58:49 +02:00
|
|
|
|
return packetData_decompressed;
|
|
|
|
|
|
}
|
2016-08-21 15:44:15 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Decompress a byte array into another byte array of a potentially unlimited size (!)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="to_decompress">Data to decompress</param>
|
|
|
|
|
|
/// <returns>Decompressed data as byte array</returns>
|
|
|
|
|
|
public static byte[] Decompress(byte[] to_decompress)
|
|
|
|
|
|
{
|
2026-03-21 20:07:20 +01:00
|
|
|
|
using MemoryStream compressedStream = new(to_decompress, writable: false);
|
|
|
|
|
|
using ZLibStream stream = new(compressedStream, CompressionMode.Decompress);
|
2016-08-21 15:44:15 +02:00
|
|
|
|
byte[] buffer = new byte[16 * 1024];
|
2026-03-21 20:07:20 +01:00
|
|
|
|
using MemoryStream decompressedBuffer = new();
|
2022-10-02 18:31:08 +08:00
|
|
|
|
int read;
|
|
|
|
|
|
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
|
|
|
|
|
|
decompressedBuffer.Write(buffer, 0, read);
|
2026-03-21 20:07:20 +01:00
|
|
|
|
|
2022-10-02 18:31:08 +08:00
|
|
|
|
return decompressedBuffer.ToArray();
|
2016-08-21 15:44:15 +02:00
|
|
|
|
}
|
2014-09-04 13:58:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|